ulfs_quota2_subr.c revision 1.1 1 1.1 dholland /* $NetBSD: ulfs_quota2_subr.c,v 1.1 2013/06/06 00:40:55 dholland Exp $ */
2 1.1 dholland /* from NetBSD: quota2_subr.c,v 1.5 2012/02/05 14:19:04 dholland Exp */
3 1.1 dholland
4 1.1 dholland /*-
5 1.1 dholland * Copyright (c) 2010, 2011 Manuel Bouyer
6 1.1 dholland * All rights reserved.
7 1.1 dholland *
8 1.1 dholland * Redistribution and use in source and binary forms, with or without
9 1.1 dholland * modification, are permitted provided that the following conditions
10 1.1 dholland * are met:
11 1.1 dholland * 1. Redistributions of source code must retain the above copyright
12 1.1 dholland * notice, this list of conditions and the following disclaimer.
13 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer in the
15 1.1 dholland * documentation and/or other materials provided with the distribution.
16 1.1 dholland *
17 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 dholland * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 dholland * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 dholland * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 dholland * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 dholland * POSSIBILITY OF SUCH DAMAGE.
28 1.1 dholland */
29 1.1 dholland
30 1.1 dholland #include <sys/cdefs.h>
31 1.1 dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_quota2_subr.c,v 1.1 2013/06/06 00:40:55 dholland Exp $");
32 1.1 dholland
33 1.1 dholland #include <sys/param.h>
34 1.1 dholland #include <sys/time.h>
35 1.1 dholland
36 1.1 dholland #include <ufs/ufs/dinode.h>
37 1.1 dholland #include <ufs/ffs/fs.h>
38 1.1 dholland #include <ufs/ffs/ffs_extern.h>
39 1.1 dholland #include <ufs/ufs/ufs_bswap.h>
40 1.1 dholland #include <ufs/ufs/quota2.h>
41 1.1 dholland
42 1.1 dholland #ifndef _KERNEL
43 1.1 dholland #include <string.h>
44 1.1 dholland #endif
45 1.1 dholland
46 1.1 dholland void
47 1.1 dholland quota2_addfreeq2e(struct quota2_header *q2h, void *bp, uint64_t baseoff,
48 1.1 dholland uint64_t bsize, int ns)
49 1.1 dholland {
50 1.1 dholland uint64_t blkoff = baseoff % bsize;
51 1.1 dholland int i, nq2e;
52 1.1 dholland struct quota2_entry *q2e;
53 1.1 dholland
54 1.1 dholland q2e = (void *)((char *)bp + blkoff);
55 1.1 dholland nq2e = (bsize - blkoff) / sizeof(*q2e);
56 1.1 dholland for (i = 0; i < nq2e; i++) {
57 1.1 dholland q2e[i].q2e_next = q2h->q2h_free;
58 1.1 dholland q2h->q2h_free = ufs_rw64(i * sizeof(*q2e) + baseoff, ns);
59 1.1 dholland }
60 1.1 dholland }
61 1.1 dholland
62 1.1 dholland void
63 1.1 dholland quota2_create_blk0(uint64_t bsize, void *bp, int q2h_hash_shift, int type,
64 1.1 dholland int ns)
65 1.1 dholland {
66 1.1 dholland struct quota2_header *q2h;
67 1.1 dholland const int quota2_hash_size = 1 << q2h_hash_shift;
68 1.1 dholland const int quota2_full_header_size = sizeof(struct quota2_header) +
69 1.1 dholland sizeof(q2h->q2h_entries[0]) * quota2_hash_size;
70 1.1 dholland int i;
71 1.1 dholland
72 1.1 dholland memset(bp, 0, bsize);
73 1.1 dholland q2h = bp;
74 1.1 dholland q2h->q2h_magic_number = ufs_rw32(Q2_HEAD_MAGIC, ns);
75 1.1 dholland q2h->q2h_type = type;
76 1.1 dholland q2h->q2h_hash_shift = q2h_hash_shift;
77 1.1 dholland q2h->q2h_hash_size = ufs_rw16(quota2_hash_size, ns);
78 1.1 dholland /* setup defaut entry: unlimited, 7 days grace */
79 1.1 dholland for (i = 0; i < N_QL; i++) {
80 1.1 dholland q2h->q2h_defentry.q2e_val[i].q2v_hardlimit =
81 1.1 dholland q2h->q2h_defentry.q2e_val[i].q2v_softlimit =
82 1.1 dholland ufs_rw64(UQUAD_MAX, ns);
83 1.1 dholland q2h->q2h_defentry.q2e_val[i].q2v_grace =
84 1.1 dholland ufs_rw64(7ULL * 24ULL * 3600ULL, ns);
85 1.1 dholland }
86 1.1 dholland
87 1.1 dholland /* first quota entry, after the hash table */
88 1.1 dholland quota2_addfreeq2e(q2h, bp, quota2_full_header_size, bsize, ns);
89 1.1 dholland }
90 1.1 dholland
91 1.1 dholland void
92 1.1 dholland quota2_ufs_rwq2v(const struct quota2_val *s, struct quota2_val *d, int needswap)
93 1.1 dholland {
94 1.1 dholland d->q2v_hardlimit = ufs_rw64(s->q2v_hardlimit, needswap);
95 1.1 dholland d->q2v_softlimit = ufs_rw64(s->q2v_softlimit, needswap);
96 1.1 dholland d->q2v_cur = ufs_rw64(s->q2v_cur, needswap);
97 1.1 dholland d->q2v_time = ufs_rw64(s->q2v_time, needswap);
98 1.1 dholland d->q2v_grace = ufs_rw64(s->q2v_grace, needswap);
99 1.1 dholland }
100 1.1 dholland
101 1.1 dholland void
102 1.1 dholland quota2_ufs_rwq2e(const struct quota2_entry *s, struct quota2_entry *d,
103 1.1 dholland int needswap)
104 1.1 dholland {
105 1.1 dholland quota2_ufs_rwq2v(&s->q2e_val[QL_BLOCK], &d->q2e_val[QL_BLOCK],
106 1.1 dholland needswap);
107 1.1 dholland quota2_ufs_rwq2v(&s->q2e_val[QL_FILE], &d->q2e_val[QL_FILE],
108 1.1 dholland needswap);
109 1.1 dholland d->q2e_uid = ufs_rw32(s->q2e_uid, needswap);
110 1.1 dholland }
111 1.1 dholland
112 1.1 dholland int
113 1.1 dholland quota_check_limit(uint64_t cur, uint64_t change, uint64_t soft, uint64_t hard,
114 1.1 dholland time_t expire, time_t now)
115 1.1 dholland {
116 1.1 dholland if (cur + change > hard) {
117 1.1 dholland if (cur <= soft)
118 1.1 dholland return (QL_F_CROSS | QL_S_DENY_HARD);
119 1.1 dholland return QL_S_DENY_HARD;
120 1.1 dholland } else if (cur + change > soft) {
121 1.1 dholland if (cur <= soft)
122 1.1 dholland return (QL_F_CROSS | QL_S_ALLOW_SOFT);
123 1.1 dholland if (now > expire) {
124 1.1 dholland return QL_S_DENY_GRACE;
125 1.1 dholland }
126 1.1 dholland return QL_S_ALLOW_SOFT;
127 1.1 dholland }
128 1.1 dholland return QL_S_ALLOW_OK;
129 1.1 dholland }
130