| @@ -1,1097 +1,1080 @@ | | | @@ -1,1097 +1,1080 @@ |
1 | /* $NetBSD: ufs_quota2.c,v 1.7 2012/01/29 06:41:42 dholland Exp $ */ | | 1 | /* $NetBSD: ufs_quota2.c,v 1.8 2012/01/29 06:46:50 dholland Exp $ */ |
2 | /*- | | 2 | /*- |
3 | * Copyright (c) 2010 Manuel Bouyer | | 3 | * Copyright (c) 2010 Manuel Bouyer |
4 | * All rights reserved. | | 4 | * All rights reserved. |
5 | * | | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | | 6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions | | 7 | * modification, are permitted provided that the following conditions |
8 | * are met: | | 8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright | | 9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. | | 10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright | | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the | | 12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. | | 13 | * documentation and/or other materials provided with the distribution. |
14 | * | | 14 | * |
15 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 15 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
16 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 16 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
17 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 17 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 | * POSSIBILITY OF SUCH DAMAGE. | | 25 | * POSSIBILITY OF SUCH DAMAGE. |
26 | */ | | 26 | */ |
27 | | | 27 | |
28 | #include <sys/cdefs.h> | | 28 | #include <sys/cdefs.h> |
29 | __KERNEL_RCSID(0, "$NetBSD: ufs_quota2.c,v 1.7 2012/01/29 06:41:42 dholland Exp $"); | | 29 | __KERNEL_RCSID(0, "$NetBSD: ufs_quota2.c,v 1.8 2012/01/29 06:46:50 dholland Exp $"); |
30 | | | 30 | |
31 | #include <sys/buf.h> | | 31 | #include <sys/buf.h> |
32 | #include <sys/param.h> | | 32 | #include <sys/param.h> |
33 | #include <sys/kernel.h> | | 33 | #include <sys/kernel.h> |
34 | #include <sys/systm.h> | | 34 | #include <sys/systm.h> |
35 | #include <sys/malloc.h> | | 35 | #include <sys/malloc.h> |
36 | #include <sys/namei.h> | | 36 | #include <sys/namei.h> |
37 | #include <sys/file.h> | | 37 | #include <sys/file.h> |
38 | #include <sys/proc.h> | | 38 | #include <sys/proc.h> |
39 | #include <sys/vnode.h> | | 39 | #include <sys/vnode.h> |
40 | #include <sys/mount.h> | | 40 | #include <sys/mount.h> |
41 | #include <sys/fstrans.h> | | 41 | #include <sys/fstrans.h> |
42 | #include <sys/kauth.h> | | 42 | #include <sys/kauth.h> |
43 | #include <sys/wapbl.h> | | 43 | #include <sys/wapbl.h> |
44 | #include <sys/quota.h> | | 44 | #include <sys/quota.h> |
45 | | | 45 | |
46 | #include <ufs/ufs/quota2.h> | | 46 | #include <ufs/ufs/quota2.h> |
47 | #include <ufs/ufs/inode.h> | | 47 | #include <ufs/ufs/inode.h> |
48 | #include <ufs/ufs/ufsmount.h> | | 48 | #include <ufs/ufs/ufsmount.h> |
49 | #include <ufs/ufs/ufs_bswap.h> | | 49 | #include <ufs/ufs/ufs_bswap.h> |
50 | #include <ufs/ufs/ufs_extern.h> | | 50 | #include <ufs/ufs/ufs_extern.h> |
51 | #include <ufs/ufs/ufs_quota.h> | | 51 | #include <ufs/ufs/ufs_quota.h> |
52 | #include <ufs/ufs/ufs_wapbl.h> | | 52 | #include <ufs/ufs/ufs_wapbl.h> |
53 | #include <quota/quotaprop.h> | | 53 | #include <quota/quotaprop.h> |
54 | | | 54 | |
55 | /* | | 55 | /* |
56 | * LOCKING: | | 56 | * LOCKING: |
57 | * Data in the entries are protected by the associated struct dquot's | | 57 | * Data in the entries are protected by the associated struct dquot's |
58 | * dq_interlock (this means we can't read or change a quota entry without | | 58 | * dq_interlock (this means we can't read or change a quota entry without |
59 | * grabing a dquot for it). | | 59 | * grabing a dquot for it). |
60 | * The header and lists (including pointers in the data entries, and q2e_uid) | | 60 | * The header and lists (including pointers in the data entries, and q2e_uid) |
61 | * are protected by the global dqlock. | | 61 | * are protected by the global dqlock. |
62 | * the locking order is dq_interlock -> dqlock | | 62 | * the locking order is dq_interlock -> dqlock |
63 | */ | | 63 | */ |
64 | | | 64 | |
65 | static int quota2_bwrite(struct mount *, struct buf *); | | 65 | static int quota2_bwrite(struct mount *, struct buf *); |
66 | static int getinoquota2(struct inode *, bool, bool, struct buf **, | | 66 | static int getinoquota2(struct inode *, bool, bool, struct buf **, |
67 | struct quota2_entry **); | | 67 | struct quota2_entry **); |
68 | static int getq2h(struct ufsmount *, int, struct buf **, | | 68 | static int getq2h(struct ufsmount *, int, struct buf **, |
69 | struct quota2_header **, int); | | 69 | struct quota2_header **, int); |
70 | static int getq2e(struct ufsmount *, int, daddr_t, int, struct buf **, | | 70 | static int getq2e(struct ufsmount *, int, daddr_t, int, struct buf **, |
71 | struct quota2_entry **, int); | | 71 | struct quota2_entry **, int); |
72 | static int quota2_walk_list(struct ufsmount *, struct buf *, int, | | 72 | static int quota2_walk_list(struct ufsmount *, struct buf *, int, |
73 | uint64_t *, int, void *, | | 73 | uint64_t *, int, void *, |
74 | int (*func)(struct ufsmount *, uint64_t *, struct quota2_entry *, | | 74 | int (*func)(struct ufsmount *, uint64_t *, struct quota2_entry *, |
75 | uint64_t, void *)); | | 75 | uint64_t, void *)); |
76 | | | 76 | |
77 | static int quota2_dict_update_q2e_limits(prop_dictionary_t, | | | |
78 | struct quota2_entry *); | | | |
79 | static prop_dictionary_t q2etoprop(struct quota2_entry *, int); | | 77 | static prop_dictionary_t q2etoprop(struct quota2_entry *, int); |
80 | | | 78 | |
81 | static const char *limnames[] = INITQLNAMES; | | 79 | static const char *limnames[] = INITQLNAMES; |
82 | | | 80 | |
83 | static int | | 81 | static void |
84 | quota2_dict_update_q2e_limits(prop_dictionary_t data, | | 82 | quota2_dict_update_q2e_limits(const struct quotaval *blocks, |
| | | 83 | const struct quotaval *files, |
85 | struct quota2_entry *q2e) | | 84 | struct quota2_entry *q2e) |
86 | { | | 85 | { |
87 | const char *val_limitsonly_names[] = INITQVNAMES_LIMITSONLY; | | 86 | q2e->q2e_val[QL_BLOCK].q2v_hardlimit = blocks->qv_hardlimit; |
88 | | | 87 | q2e->q2e_val[QL_BLOCK].q2v_softlimit = blocks->qv_softlimit; |
89 | int i, error; | | 88 | q2e->q2e_val[QL_BLOCK].q2v_grace = blocks->qv_grace; |
90 | prop_dictionary_t val; | | 89 | |
91 | | | 90 | q2e->q2e_val[QL_FILE].q2v_hardlimit = blocks->qv_hardlimit; |
92 | for (i = 0; i < N_QL; i++) { | | 91 | q2e->q2e_val[QL_FILE].q2v_softlimit = blocks->qv_softlimit; |
93 | if (!prop_dictionary_get_dict(data, limnames[i], &val)) | | 92 | q2e->q2e_val[QL_FILE].q2v_grace = blocks->qv_grace; |
94 | return EINVAL; | | | |
95 | error = quotaprop_dict_get_uint64(val, | | | |
96 | &q2e->q2e_val[i].q2v_hardlimit, | | | |
97 | val_limitsonly_names, N_QV, true); | | | |
98 | if (error) | | | |
99 | return error; | | | |
100 | } | | | |
101 | return 0; | | | |
102 | } | | 93 | } |
| | | 94 | |
103 | static prop_dictionary_t | | 95 | static prop_dictionary_t |
104 | q2etoprop(struct quota2_entry *q2e, int def) | | 96 | q2etoprop(struct quota2_entry *q2e, int def) |
105 | { | | 97 | { |
106 | const char *val_names[] = INITQVNAMES_ALL; | | 98 | const char *val_names[] = INITQVNAMES_ALL; |
107 | prop_dictionary_t dict1 = prop_dictionary_create(); | | 99 | prop_dictionary_t dict1 = prop_dictionary_create(); |
108 | prop_dictionary_t dict2; | | 100 | prop_dictionary_t dict2; |
109 | int i; | | 101 | int i; |
110 | | | 102 | |
111 | if (dict1 == NULL) | | 103 | if (dict1 == NULL) |
112 | return NULL; | | 104 | return NULL; |
113 | | | 105 | |
114 | if (def) { | | 106 | if (def) { |
115 | if (!prop_dictionary_set_cstring_nocopy(dict1, "id", | | 107 | if (!prop_dictionary_set_cstring_nocopy(dict1, "id", |
116 | "default")) { | | 108 | "default")) { |
117 | goto err; | | 109 | goto err; |
118 | } | | 110 | } |
119 | } else { | | 111 | } else { |
120 | if (!prop_dictionary_set_uint32(dict1, "id", q2e->q2e_uid)) { | | 112 | if (!prop_dictionary_set_uint32(dict1, "id", q2e->q2e_uid)) { |
121 | goto err; | | 113 | goto err; |
122 | } | | 114 | } |
123 | } | | 115 | } |
124 | for (i = 0; i < N_QL; i++) { | | 116 | for (i = 0; i < N_QL; i++) { |
125 | dict2 = limits64toprop(&q2e->q2e_val[i].q2v_hardlimit, | | 117 | dict2 = limits64toprop(&q2e->q2e_val[i].q2v_hardlimit, |
126 | val_names, N_QV); | | 118 | val_names, N_QV); |
127 | if (dict2 == NULL) | | 119 | if (dict2 == NULL) |
128 | goto err; | | 120 | goto err; |
129 | if (!prop_dictionary_set_and_rel(dict1, limnames[i], dict2)) | | 121 | if (!prop_dictionary_set_and_rel(dict1, limnames[i], dict2)) |
130 | goto err; | | 122 | goto err; |
131 | } | | 123 | } |
132 | return dict1; | | 124 | return dict1; |
133 | | | 125 | |
134 | err: | | 126 | err: |
135 | prop_object_release(dict1); | | 127 | prop_object_release(dict1); |
136 | return NULL; | | 128 | return NULL; |
137 | } | | 129 | } |
138 | | | 130 | |
139 | /* | | 131 | /* |
140 | * Convert internal representation to FS-independent representation. | | 132 | * Convert internal representation to FS-independent representation. |
141 | * (Note that while the two types are currently identical, the | | 133 | * (Note that while the two types are currently identical, the |
142 | * internal representation is an on-disk struct and the FS-independent | | 134 | * internal representation is an on-disk struct and the FS-independent |
143 | * representation is not, and they might diverge in the future.) | | 135 | * representation is not, and they might diverge in the future.) |
144 | */ | | 136 | */ |
145 | static void | | 137 | static void |
146 | q2val_to_quotaval(struct quota2_val *q2v, struct quotaval *qv) | | 138 | q2val_to_quotaval(struct quota2_val *q2v, struct quotaval *qv) |
147 | { | | 139 | { |
148 | qv->qv_softlimit = q2v->q2v_softlimit; | | 140 | qv->qv_softlimit = q2v->q2v_softlimit; |
149 | qv->qv_hardlimit = q2v->q2v_hardlimit; | | 141 | qv->qv_hardlimit = q2v->q2v_hardlimit; |
150 | qv->qv_usage = q2v->q2v_cur; | | 142 | qv->qv_usage = q2v->q2v_cur; |
151 | qv->qv_expiretime = q2v->q2v_time; | | 143 | qv->qv_expiretime = q2v->q2v_time; |
152 | qv->qv_grace = q2v->q2v_grace; | | 144 | qv->qv_grace = q2v->q2v_grace; |
153 | } | | 145 | } |
154 | | | 146 | |
155 | /* | | 147 | /* |
156 | * Convert a quota2entry and default-flag to the FS-independent | | 148 | * Convert a quota2entry and default-flag to the FS-independent |
157 | * representation. | | 149 | * representation. |
158 | */ | | 150 | */ |
159 | static void | | 151 | static void |
160 | q2e_to_quotaval(struct quota2_entry *q2e, int def, | | 152 | q2e_to_quotaval(struct quota2_entry *q2e, int def, |
161 | id_t *id, int objtype, struct quotaval *ret) | | 153 | id_t *id, int objtype, struct quotaval *ret) |
162 | { | | 154 | { |
163 | if (def) { | | 155 | if (def) { |
164 | *id = QUOTA_DEFAULTID; | | 156 | *id = QUOTA_DEFAULTID; |
165 | } else { | | 157 | } else { |
166 | *id = q2e->q2e_uid; | | 158 | *id = q2e->q2e_uid; |
167 | } | | 159 | } |
168 | | | 160 | |
169 | KASSERT(objtype >= 0 && objtype < N_QL); | | 161 | KASSERT(objtype >= 0 && objtype < N_QL); |
170 | q2val_to_quotaval(&q2e->q2e_val[objtype], ret); | | 162 | q2val_to_quotaval(&q2e->q2e_val[objtype], ret); |
171 | } | | 163 | } |
172 | | | 164 | |
173 | | | 165 | |
174 | static int | | 166 | static int |
175 | quota2_bwrite(struct mount *mp, struct buf *bp) | | 167 | quota2_bwrite(struct mount *mp, struct buf *bp) |
176 | { | | 168 | { |
177 | if (mp->mnt_flag & MNT_SYNCHRONOUS) | | 169 | if (mp->mnt_flag & MNT_SYNCHRONOUS) |
178 | return bwrite(bp); | | 170 | return bwrite(bp); |
179 | else { | | 171 | else { |
180 | bdwrite(bp); | | 172 | bdwrite(bp); |
181 | return 0; | | 173 | return 0; |
182 | } | | 174 | } |
183 | } | | 175 | } |
184 | | | 176 | |
185 | static int | | 177 | static int |
186 | getq2h(struct ufsmount *ump, int type, | | 178 | getq2h(struct ufsmount *ump, int type, |
187 | struct buf **bpp, struct quota2_header **q2hp, int flags) | | 179 | struct buf **bpp, struct quota2_header **q2hp, int flags) |
188 | { | | 180 | { |
189 | #ifdef FFS_EI | | 181 | #ifdef FFS_EI |
190 | const int needswap = UFS_MPNEEDSWAP(ump); | | 182 | const int needswap = UFS_MPNEEDSWAP(ump); |
191 | #endif | | 183 | #endif |
192 | int error; | | 184 | int error; |
193 | struct buf *bp; | | 185 | struct buf *bp; |
194 | struct quota2_header *q2h; | | 186 | struct quota2_header *q2h; |
195 | | | 187 | |
196 | KASSERT(mutex_owned(&dqlock)); | | 188 | KASSERT(mutex_owned(&dqlock)); |
197 | error = bread(ump->um_quotas[type], 0, ump->umq2_bsize, | | 189 | error = bread(ump->um_quotas[type], 0, ump->umq2_bsize, |
198 | ump->um_cred[type], flags, &bp); | | 190 | ump->um_cred[type], flags, &bp); |
199 | if (error) | | 191 | if (error) |
200 | return error; | | 192 | return error; |
201 | if (bp->b_resid != 0) | | 193 | if (bp->b_resid != 0) |
202 | panic("dq2get: %s quota file truncated", quotatypes[type]); | | 194 | panic("dq2get: %s quota file truncated", quotatypes[type]); |
203 | | | 195 | |
204 | q2h = (void *)bp->b_data; | | 196 | q2h = (void *)bp->b_data; |
205 | if (ufs_rw32(q2h->q2h_magic_number, needswap) != Q2_HEAD_MAGIC || | | 197 | if (ufs_rw32(q2h->q2h_magic_number, needswap) != Q2_HEAD_MAGIC || |
206 | q2h->q2h_type != type) | | 198 | q2h->q2h_type != type) |
207 | panic("dq2get: corrupted %s quota header", quotatypes[type]); | | 199 | panic("dq2get: corrupted %s quota header", quotatypes[type]); |
208 | *bpp = bp; | | 200 | *bpp = bp; |
209 | *q2hp = q2h; | | 201 | *q2hp = q2h; |
210 | return 0; | | 202 | return 0; |
211 | } | | 203 | } |
212 | | | 204 | |
213 | static int | | 205 | static int |
214 | getq2e(struct ufsmount *ump, int type, daddr_t lblkno, int blkoffset, | | 206 | getq2e(struct ufsmount *ump, int type, daddr_t lblkno, int blkoffset, |
215 | struct buf **bpp, struct quota2_entry **q2ep, int flags) | | 207 | struct buf **bpp, struct quota2_entry **q2ep, int flags) |
216 | { | | 208 | { |
217 | int error; | | 209 | int error; |
218 | struct buf *bp; | | 210 | struct buf *bp; |
219 | | | 211 | |
220 | if (blkoffset & (sizeof(uint64_t) - 1)) { | | 212 | if (blkoffset & (sizeof(uint64_t) - 1)) { |
221 | panic("dq2get: %s quota file corrupted", | | 213 | panic("dq2get: %s quota file corrupted", |
222 | quotatypes[type]); | | 214 | quotatypes[type]); |
223 | } | | 215 | } |
224 | error = bread(ump->um_quotas[type], lblkno, ump->umq2_bsize, | | 216 | error = bread(ump->um_quotas[type], lblkno, ump->umq2_bsize, |
225 | ump->um_cred[type], flags, &bp); | | 217 | ump->um_cred[type], flags, &bp); |
226 | if (error) | | 218 | if (error) |
227 | return error; | | 219 | return error; |
228 | if (bp->b_resid != 0) { | | 220 | if (bp->b_resid != 0) { |
229 | panic("dq2get: %s quota file corrupted", | | 221 | panic("dq2get: %s quota file corrupted", |
230 | quotatypes[type]); | | 222 | quotatypes[type]); |
231 | } | | 223 | } |
232 | *q2ep = (void *)((char *)bp->b_data + blkoffset); | | 224 | *q2ep = (void *)((char *)bp->b_data + blkoffset); |
233 | *bpp = bp; | | 225 | *bpp = bp; |
234 | return 0; | | 226 | return 0; |
235 | } | | 227 | } |
236 | | | 228 | |
237 | /* walk a quota entry list, calling the callback for each entry */ | | 229 | /* walk a quota entry list, calling the callback for each entry */ |
238 | #define Q2WL_ABORT 0x10000000 | | 230 | #define Q2WL_ABORT 0x10000000 |
239 | | | 231 | |
240 | static int | | 232 | static int |
241 | quota2_walk_list(struct ufsmount *ump, struct buf *hbp, int type, | | 233 | quota2_walk_list(struct ufsmount *ump, struct buf *hbp, int type, |
242 | uint64_t *offp, int flags, void *a, | | 234 | uint64_t *offp, int flags, void *a, |
243 | int (*func)(struct ufsmount *, uint64_t *, struct quota2_entry *, uint64_t, void *)) | | 235 | int (*func)(struct ufsmount *, uint64_t *, struct quota2_entry *, uint64_t, void *)) |
244 | { | | 236 | { |
245 | #ifdef FFS_EI | | 237 | #ifdef FFS_EI |
246 | const int needswap = UFS_MPNEEDSWAP(ump); | | 238 | const int needswap = UFS_MPNEEDSWAP(ump); |
247 | #endif | | 239 | #endif |
248 | daddr_t off = ufs_rw64(*offp, needswap); | | 240 | daddr_t off = ufs_rw64(*offp, needswap); |
249 | struct buf *bp, *obp = hbp; | | 241 | struct buf *bp, *obp = hbp; |
250 | int ret = 0, ret2 = 0; | | 242 | int ret = 0, ret2 = 0; |
251 | struct quota2_entry *q2e; | | 243 | struct quota2_entry *q2e; |
252 | daddr_t lblkno, blkoff, olblkno = 0; | | 244 | daddr_t lblkno, blkoff, olblkno = 0; |
253 | | | 245 | |
254 | KASSERT(mutex_owner(&dqlock)); | | 246 | KASSERT(mutex_owner(&dqlock)); |
255 | | | 247 | |
256 | while (off != 0) { | | 248 | while (off != 0) { |
257 | lblkno = (off >> ump->um_mountp->mnt_fs_bshift); | | 249 | lblkno = (off >> ump->um_mountp->mnt_fs_bshift); |
258 | blkoff = (off & ump->umq2_bmask); | | 250 | blkoff = (off & ump->umq2_bmask); |
259 | if (lblkno == 0) { | | 251 | if (lblkno == 0) { |
260 | /* in the header block */ | | 252 | /* in the header block */ |
261 | bp = hbp; | | 253 | bp = hbp; |
262 | } else if (lblkno == olblkno) { | | 254 | } else if (lblkno == olblkno) { |
263 | /* still in the same buf */ | | 255 | /* still in the same buf */ |
264 | bp = obp; | | 256 | bp = obp; |
265 | } else { | | 257 | } else { |
266 | ret = bread(ump->um_quotas[type], lblkno, | | 258 | ret = bread(ump->um_quotas[type], lblkno, |
267 | ump->umq2_bsize, | | 259 | ump->umq2_bsize, |
268 | ump->um_cred[type], flags, &bp); | | 260 | ump->um_cred[type], flags, &bp); |
269 | if (ret) | | 261 | if (ret) |
270 | return ret; | | 262 | return ret; |
271 | if (bp->b_resid != 0) { | | 263 | if (bp->b_resid != 0) { |
272 | panic("quota2_walk_list: %s quota file corrupted", | | 264 | panic("quota2_walk_list: %s quota file corrupted", |
273 | quotatypes[type]); | | 265 | quotatypes[type]); |
274 | } | | 266 | } |
275 | } | | 267 | } |
276 | q2e = (void *)((char *)(bp->b_data) + blkoff); | | 268 | q2e = (void *)((char *)(bp->b_data) + blkoff); |
277 | ret = (*func)(ump, offp, q2e, off, a); | | 269 | ret = (*func)(ump, offp, q2e, off, a); |
278 | if (off != ufs_rw64(*offp, needswap)) { | | 270 | if (off != ufs_rw64(*offp, needswap)) { |
279 | /* callback changed parent's pointer, redo */ | | 271 | /* callback changed parent's pointer, redo */ |
280 | off = ufs_rw64(*offp, needswap); | | 272 | off = ufs_rw64(*offp, needswap); |
281 | if (bp != hbp && bp != obp) | | 273 | if (bp != hbp && bp != obp) |
282 | ret2 = bwrite(bp); | | 274 | ret2 = bwrite(bp); |
283 | } else { | | 275 | } else { |
284 | /* parent if now current */ | | 276 | /* parent if now current */ |
285 | if (obp != bp && obp != hbp) { | | 277 | if (obp != bp && obp != hbp) { |
286 | if (flags & B_MODIFY) | | 278 | if (flags & B_MODIFY) |
287 | ret2 = bwrite(obp); | | 279 | ret2 = bwrite(obp); |
288 | else | | 280 | else |
289 | brelse(obp, 0); | | 281 | brelse(obp, 0); |
290 | } | | 282 | } |
291 | obp = bp; | | 283 | obp = bp; |
292 | olblkno = lblkno; | | 284 | olblkno = lblkno; |
293 | offp = &(q2e->q2e_next); | | 285 | offp = &(q2e->q2e_next); |
294 | off = ufs_rw64(*offp, needswap); | | 286 | off = ufs_rw64(*offp, needswap); |
295 | } | | 287 | } |
296 | if (ret) | | 288 | if (ret) |
297 | break; | | 289 | break; |
298 | if (ret2) { | | 290 | if (ret2) { |
299 | ret = ret2; | | 291 | ret = ret2; |
300 | break; | | 292 | break; |
301 | } | | 293 | } |
302 | } | | 294 | } |
303 | if (obp != hbp) { | | 295 | if (obp != hbp) { |
304 | if (flags & B_MODIFY) | | 296 | if (flags & B_MODIFY) |
305 | ret2 = bwrite(obp); | | 297 | ret2 = bwrite(obp); |
306 | else | | 298 | else |
307 | brelse(obp, 0); | | 299 | brelse(obp, 0); |
308 | } | | 300 | } |
309 | if (ret & Q2WL_ABORT) | | 301 | if (ret & Q2WL_ABORT) |
310 | return 0; | | 302 | return 0; |
311 | if (ret == 0) | | 303 | if (ret == 0) |
312 | return ret2; | | 304 | return ret2; |
313 | return ret; | | 305 | return ret; |
314 | } | | 306 | } |
315 | | | 307 | |
316 | int | | 308 | int |
317 | quota2_umount(struct mount *mp, int flags) | | 309 | quota2_umount(struct mount *mp, int flags) |
318 | { | | 310 | { |
319 | int i, error; | | 311 | int i, error; |
320 | struct ufsmount *ump = VFSTOUFS(mp); | | 312 | struct ufsmount *ump = VFSTOUFS(mp); |
321 | | | 313 | |
322 | if ((ump->um_flags & UFS_QUOTA2) == 0) | | 314 | if ((ump->um_flags & UFS_QUOTA2) == 0) |
323 | return 0; | | 315 | return 0; |
324 | | | 316 | |
325 | for (i = 0; i < MAXQUOTAS; i++) { | | 317 | for (i = 0; i < MAXQUOTAS; i++) { |
326 | if (ump->um_quotas[i] != NULLVP) { | | 318 | if (ump->um_quotas[i] != NULLVP) { |
327 | error = vn_close(ump->um_quotas[i], FREAD|FWRITE, | | 319 | error = vn_close(ump->um_quotas[i], FREAD|FWRITE, |
328 | ump->um_cred[i]); | | 320 | ump->um_cred[i]); |
329 | if (error) { | | 321 | if (error) { |
330 | printf("quota2_umount failed: close(%p) %d\n", | | 322 | printf("quota2_umount failed: close(%p) %d\n", |
331 | ump->um_quotas[i], error); | | 323 | ump->um_quotas[i], error); |
332 | return error; | | 324 | return error; |
333 | } | | 325 | } |
334 | } | | 326 | } |
335 | ump->um_quotas[i] = NULLVP; | | 327 | ump->um_quotas[i] = NULLVP; |
336 | } | | 328 | } |
337 | return 0; | | 329 | return 0; |
338 | } | | 330 | } |
339 | | | 331 | |
340 | static int | | 332 | static int |
341 | quota2_q2ealloc(struct ufsmount *ump, int type, uid_t uid, struct dquot *dq, | | 333 | quota2_q2ealloc(struct ufsmount *ump, int type, uid_t uid, struct dquot *dq, |
342 | struct buf **bpp, struct quota2_entry **q2ep) | | 334 | struct buf **bpp, struct quota2_entry **q2ep) |
343 | { | | 335 | { |
344 | int error, error2; | | 336 | int error, error2; |
345 | struct buf *hbp, *bp; | | 337 | struct buf *hbp, *bp; |
346 | struct quota2_header *q2h; | | 338 | struct quota2_header *q2h; |
347 | struct quota2_entry *q2e; | | 339 | struct quota2_entry *q2e; |
348 | daddr_t offset; | | 340 | daddr_t offset; |
349 | u_long hash_mask; | | 341 | u_long hash_mask; |
350 | const int needswap = UFS_MPNEEDSWAP(ump); | | 342 | const int needswap = UFS_MPNEEDSWAP(ump); |
351 | | | 343 | |
352 | KASSERT(mutex_owned(&dq->dq_interlock)); | | 344 | KASSERT(mutex_owned(&dq->dq_interlock)); |
353 | KASSERT(mutex_owned(&dqlock)); | | 345 | KASSERT(mutex_owned(&dqlock)); |
354 | error = getq2h(ump, type, &hbp, &q2h, B_MODIFY); | | 346 | error = getq2h(ump, type, &hbp, &q2h, B_MODIFY); |
355 | if (error) | | 347 | if (error) |
356 | return error; | | 348 | return error; |
357 | offset = ufs_rw64(q2h->q2h_free, needswap); | | 349 | offset = ufs_rw64(q2h->q2h_free, needswap); |
358 | if (offset == 0) { | | 350 | if (offset == 0) { |
359 | struct vnode *vp = ump->um_quotas[type]; | | 351 | struct vnode *vp = ump->um_quotas[type]; |
360 | struct inode *ip = VTOI(vp); | | 352 | struct inode *ip = VTOI(vp); |
361 | uint64_t size = ip->i_size; | | 353 | uint64_t size = ip->i_size; |
362 | /* need to alocate a new disk block */ | | 354 | /* need to alocate a new disk block */ |
363 | error = UFS_BALLOC(vp, size, ump->umq2_bsize, | | 355 | error = UFS_BALLOC(vp, size, ump->umq2_bsize, |
364 | ump->um_cred[type], B_CLRBUF | B_SYNC, &bp); | | 356 | ump->um_cred[type], B_CLRBUF | B_SYNC, &bp); |
365 | if (error) { | | 357 | if (error) { |
366 | brelse(hbp, 0); | | 358 | brelse(hbp, 0); |
367 | return error; | | 359 | return error; |
368 | } | | 360 | } |
369 | KASSERT((ip->i_size % ump->umq2_bsize) == 0); | | 361 | KASSERT((ip->i_size % ump->umq2_bsize) == 0); |
370 | ip->i_size += ump->umq2_bsize; | | 362 | ip->i_size += ump->umq2_bsize; |
371 | DIP_ASSIGN(ip, size, ip->i_size); | | 363 | DIP_ASSIGN(ip, size, ip->i_size); |
372 | ip->i_flag |= IN_CHANGE | IN_UPDATE; | | 364 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
373 | uvm_vnp_setsize(vp, ip->i_size); | | 365 | uvm_vnp_setsize(vp, ip->i_size); |
374 | quota2_addfreeq2e(q2h, bp->b_data, size, ump->umq2_bsize, | | 366 | quota2_addfreeq2e(q2h, bp->b_data, size, ump->umq2_bsize, |
375 | needswap); | | 367 | needswap); |
376 | error = bwrite(bp); | | 368 | error = bwrite(bp); |
377 | error2 = UFS_UPDATE(vp, NULL, NULL, UPDATE_WAIT); | | 369 | error2 = UFS_UPDATE(vp, NULL, NULL, UPDATE_WAIT); |
378 | if (error || error2) { | | 370 | if (error || error2) { |
379 | brelse(hbp, 0); | | 371 | brelse(hbp, 0); |
380 | if (error) | | 372 | if (error) |
381 | return error; | | 373 | return error; |
382 | return error2; | | 374 | return error2; |
383 | } | | 375 | } |
384 | offset = ufs_rw64(q2h->q2h_free, needswap); | | 376 | offset = ufs_rw64(q2h->q2h_free, needswap); |
385 | KASSERT(offset != 0); | | 377 | KASSERT(offset != 0); |
386 | } | | 378 | } |
387 | dq->dq2_lblkno = (offset >> ump->um_mountp->mnt_fs_bshift); | | 379 | dq->dq2_lblkno = (offset >> ump->um_mountp->mnt_fs_bshift); |
388 | dq->dq2_blkoff = (offset & ump->umq2_bmask); | | 380 | dq->dq2_blkoff = (offset & ump->umq2_bmask); |
389 | if (dq->dq2_lblkno == 0) { | | 381 | if (dq->dq2_lblkno == 0) { |
390 | bp = hbp; | | 382 | bp = hbp; |
391 | q2e = (void *)((char *)bp->b_data + dq->dq2_blkoff); | | 383 | q2e = (void *)((char *)bp->b_data + dq->dq2_blkoff); |
392 | } else { | | 384 | } else { |
393 | error = getq2e(ump, type, dq->dq2_lblkno, | | 385 | error = getq2e(ump, type, dq->dq2_lblkno, |
394 | dq->dq2_blkoff, &bp, &q2e, B_MODIFY); | | 386 | dq->dq2_blkoff, &bp, &q2e, B_MODIFY); |
395 | if (error) { | | 387 | if (error) { |
396 | brelse(hbp, 0); | | 388 | brelse(hbp, 0); |
397 | return error; | | 389 | return error; |
398 | } | | 390 | } |
399 | } | | 391 | } |
400 | hash_mask = ((1 << q2h->q2h_hash_shift) - 1); | | 392 | hash_mask = ((1 << q2h->q2h_hash_shift) - 1); |
401 | /* remove from free list */ | | 393 | /* remove from free list */ |
402 | q2h->q2h_free = q2e->q2e_next; | | 394 | q2h->q2h_free = q2e->q2e_next; |
403 | | | 395 | |
404 | memcpy(q2e, &q2h->q2h_defentry, sizeof(*q2e)); | | 396 | memcpy(q2e, &q2h->q2h_defentry, sizeof(*q2e)); |
405 | q2e->q2e_uid = ufs_rw32(uid, needswap); | | 397 | q2e->q2e_uid = ufs_rw32(uid, needswap); |
406 | /* insert in hash list */ | | 398 | /* insert in hash list */ |
407 | q2e->q2e_next = q2h->q2h_entries[uid & hash_mask]; | | 399 | q2e->q2e_next = q2h->q2h_entries[uid & hash_mask]; |
408 | q2h->q2h_entries[uid & hash_mask] = ufs_rw64(offset, needswap); | | 400 | q2h->q2h_entries[uid & hash_mask] = ufs_rw64(offset, needswap); |
409 | if (hbp != bp) { | | 401 | if (hbp != bp) { |
410 | bwrite(hbp); | | 402 | bwrite(hbp); |
411 | } | | 403 | } |
412 | *q2ep = q2e; | | 404 | *q2ep = q2e; |
413 | *bpp = bp; | | 405 | *bpp = bp; |
414 | return 0; | | 406 | return 0; |
415 | } | | 407 | } |
416 | | | 408 | |
417 | static int | | 409 | static int |
418 | getinoquota2(struct inode *ip, bool alloc, bool modify, struct buf **bpp, | | 410 | getinoquota2(struct inode *ip, bool alloc, bool modify, struct buf **bpp, |
419 | struct quota2_entry **q2ep) | | 411 | struct quota2_entry **q2ep) |
420 | { | | 412 | { |
421 | int error; | | 413 | int error; |
422 | int i; | | 414 | int i; |
423 | struct dquot *dq; | | 415 | struct dquot *dq; |
424 | struct ufsmount *ump = ip->i_ump; | | 416 | struct ufsmount *ump = ip->i_ump; |
425 | u_int32_t ino_ids[MAXQUOTAS]; | | 417 | u_int32_t ino_ids[MAXQUOTAS]; |
426 | | | 418 | |
427 | error = getinoquota(ip); | | 419 | error = getinoquota(ip); |
428 | if (error) | | 420 | if (error) |
429 | return error; | | 421 | return error; |
430 | | | 422 | |
431 | if (alloc) { | | 423 | if (alloc) { |
432 | UFS_WAPBL_JLOCK_ASSERT(ump->um_mountp); | | 424 | UFS_WAPBL_JLOCK_ASSERT(ump->um_mountp); |
433 | } | | 425 | } |
434 | ino_ids[USRQUOTA] = ip->i_uid; | | 426 | ino_ids[USRQUOTA] = ip->i_uid; |
435 | ino_ids[GRPQUOTA] = ip->i_gid; | | 427 | ino_ids[GRPQUOTA] = ip->i_gid; |
436 | /* first get the interlock for all dquot */ | | 428 | /* first get the interlock for all dquot */ |
437 | for (i = 0; i < MAXQUOTAS; i++) { | | 429 | for (i = 0; i < MAXQUOTAS; i++) { |
438 | dq = ip->i_dquot[i]; | | 430 | dq = ip->i_dquot[i]; |
439 | if (dq == NODQUOT) | | 431 | if (dq == NODQUOT) |
440 | continue; | | 432 | continue; |
441 | mutex_enter(&dq->dq_interlock); | | 433 | mutex_enter(&dq->dq_interlock); |
442 | } | | 434 | } |
443 | /* now get the corresponding quota entry */ | | 435 | /* now get the corresponding quota entry */ |
444 | for (i = 0; i < MAXQUOTAS; i++) { | | 436 | for (i = 0; i < MAXQUOTAS; i++) { |
445 | bpp[i] = NULL; | | 437 | bpp[i] = NULL; |
446 | q2ep[i] = NULL; | | 438 | q2ep[i] = NULL; |
447 | dq = ip->i_dquot[i]; | | 439 | dq = ip->i_dquot[i]; |
448 | if (dq == NODQUOT) | | 440 | if (dq == NODQUOT) |
449 | continue; | | 441 | continue; |
450 | if (__predict_false(ump->um_quotas[i] == NULL)) { | | 442 | if (__predict_false(ump->um_quotas[i] == NULL)) { |
451 | /* | | 443 | /* |
452 | * quotas have been turned off. This can happen | | 444 | * quotas have been turned off. This can happen |
453 | * at umount time. | | 445 | * at umount time. |
454 | */ | | 446 | */ |
455 | mutex_exit(&dq->dq_interlock); | | 447 | mutex_exit(&dq->dq_interlock); |
456 | dqrele(NULLVP, dq); | | 448 | dqrele(NULLVP, dq); |
457 | ip->i_dquot[i] = NULL; | | 449 | ip->i_dquot[i] = NULL; |
458 | continue; | | 450 | continue; |
459 | } | | 451 | } |
460 | | | 452 | |
461 | if ((dq->dq2_lblkno | dq->dq2_blkoff) == 0) { | | 453 | if ((dq->dq2_lblkno | dq->dq2_blkoff) == 0) { |
462 | if (!alloc) { | | 454 | if (!alloc) { |
463 | continue; | | 455 | continue; |
464 | } | | 456 | } |
465 | /* need to alloc a new on-disk quot */ | | 457 | /* need to alloc a new on-disk quot */ |
466 | mutex_enter(&dqlock); | | 458 | mutex_enter(&dqlock); |
467 | error = quota2_q2ealloc(ump, i, ino_ids[i], dq, | | 459 | error = quota2_q2ealloc(ump, i, ino_ids[i], dq, |
468 | &bpp[i], &q2ep[i]); | | 460 | &bpp[i], &q2ep[i]); |
469 | mutex_exit(&dqlock); | | 461 | mutex_exit(&dqlock); |
470 | if (error) | | 462 | if (error) |
471 | return error; | | 463 | return error; |
472 | } else { | | 464 | } else { |
473 | error = getq2e(ump, i, dq->dq2_lblkno, | | 465 | error = getq2e(ump, i, dq->dq2_lblkno, |
474 | dq->dq2_blkoff, &bpp[i], &q2ep[i], | | 466 | dq->dq2_blkoff, &bpp[i], &q2ep[i], |
475 | modify ? B_MODIFY : 0); | | 467 | modify ? B_MODIFY : 0); |
476 | if (error) | | 468 | if (error) |
477 | return error; | | 469 | return error; |
478 | } | | 470 | } |
479 | } | | 471 | } |
480 | return 0; | | 472 | return 0; |
481 | } | | 473 | } |
482 | | | 474 | |
483 | static int | | 475 | static int |
484 | quota2_check(struct inode *ip, int vtype, int64_t change, kauth_cred_t cred, | | 476 | quota2_check(struct inode *ip, int vtype, int64_t change, kauth_cred_t cred, |
485 | int flags) | | 477 | int flags) |
486 | { | | 478 | { |
487 | int error; | | 479 | int error; |
488 | struct buf *bp[MAXQUOTAS]; | | 480 | struct buf *bp[MAXQUOTAS]; |
489 | struct quota2_entry *q2e[MAXQUOTAS]; | | 481 | struct quota2_entry *q2e[MAXQUOTAS]; |
490 | struct quota2_val *q2vp; | | 482 | struct quota2_val *q2vp; |
491 | struct dquot *dq; | | 483 | struct dquot *dq; |
492 | uint64_t ncurblks; | | 484 | uint64_t ncurblks; |
493 | struct ufsmount *ump = ip->i_ump; | | 485 | struct ufsmount *ump = ip->i_ump; |
494 | struct mount *mp = ump->um_mountp; | | 486 | struct mount *mp = ump->um_mountp; |
495 | const int needswap = UFS_MPNEEDSWAP(ump); | | 487 | const int needswap = UFS_MPNEEDSWAP(ump); |
496 | int i; | | 488 | int i; |
497 | | | 489 | |
498 | if ((error = getinoquota2(ip, change > 0, change != 0, bp, q2e)) != 0) | | 490 | if ((error = getinoquota2(ip, change > 0, change != 0, bp, q2e)) != 0) |
499 | return error; | | 491 | return error; |
500 | if (change == 0) { | | 492 | if (change == 0) { |
501 | for (i = 0; i < MAXQUOTAS; i++) { | | 493 | for (i = 0; i < MAXQUOTAS; i++) { |
502 | dq = ip->i_dquot[i]; | | 494 | dq = ip->i_dquot[i]; |
503 | if (dq == NODQUOT) | | 495 | if (dq == NODQUOT) |
504 | continue; | | 496 | continue; |
505 | if (bp[i]) | | 497 | if (bp[i]) |
506 | brelse(bp[i], 0); | | 498 | brelse(bp[i], 0); |
507 | mutex_exit(&dq->dq_interlock); | | 499 | mutex_exit(&dq->dq_interlock); |
508 | } | | 500 | } |
509 | return 0; | | 501 | return 0; |
510 | } | | 502 | } |
511 | if (change < 0) { | | 503 | if (change < 0) { |
512 | for (i = 0; i < MAXQUOTAS; i++) { | | 504 | for (i = 0; i < MAXQUOTAS; i++) { |
513 | dq = ip->i_dquot[i]; | | 505 | dq = ip->i_dquot[i]; |
514 | if (dq == NODQUOT) | | 506 | if (dq == NODQUOT) |
515 | continue; | | 507 | continue; |
516 | if (q2e[i] == NULL) { | | 508 | if (q2e[i] == NULL) { |
517 | mutex_exit(&dq->dq_interlock); | | 509 | mutex_exit(&dq->dq_interlock); |
518 | continue; | | 510 | continue; |
519 | } | | 511 | } |
520 | q2vp = &q2e[i]->q2e_val[vtype]; | | 512 | q2vp = &q2e[i]->q2e_val[vtype]; |
521 | ncurblks = ufs_rw64(q2vp->q2v_cur, needswap); | | 513 | ncurblks = ufs_rw64(q2vp->q2v_cur, needswap); |
522 | if (ncurblks < -change) | | 514 | if (ncurblks < -change) |
523 | ncurblks = 0; | | 515 | ncurblks = 0; |
524 | else | | 516 | else |
525 | ncurblks += change; | | 517 | ncurblks += change; |
526 | q2vp->q2v_cur = ufs_rw64(ncurblks, needswap); | | 518 | q2vp->q2v_cur = ufs_rw64(ncurblks, needswap); |
527 | quota2_bwrite(mp, bp[i]); | | 519 | quota2_bwrite(mp, bp[i]); |
528 | mutex_exit(&dq->dq_interlock); | | 520 | mutex_exit(&dq->dq_interlock); |
529 | } | | 521 | } |
530 | return 0; | | 522 | return 0; |
531 | } | | 523 | } |
532 | /* see if the allocation is allowed */ | | 524 | /* see if the allocation is allowed */ |
533 | for (i = 0; i < MAXQUOTAS; i++) { | | 525 | for (i = 0; i < MAXQUOTAS; i++) { |
534 | struct quota2_val q2v; | | 526 | struct quota2_val q2v; |
535 | int ql_stat; | | 527 | int ql_stat; |
536 | dq = ip->i_dquot[i]; | | 528 | dq = ip->i_dquot[i]; |
537 | if (dq == NODQUOT) | | 529 | if (dq == NODQUOT) |
538 | continue; | | 530 | continue; |
539 | KASSERT(q2e[i] != NULL); | | 531 | KASSERT(q2e[i] != NULL); |
540 | quota2_ufs_rwq2v(&q2e[i]->q2e_val[vtype], &q2v, needswap); | | 532 | quota2_ufs_rwq2v(&q2e[i]->q2e_val[vtype], &q2v, needswap); |
541 | ql_stat = quota2_check_limit(&q2v, change, time_second); | | 533 | ql_stat = quota2_check_limit(&q2v, change, time_second); |
542 | | | 534 | |
543 | if ((flags & FORCE) == 0 && | | 535 | if ((flags & FORCE) == 0 && |
544 | kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA, | | 536 | kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA, |
545 | KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT, | | 537 | KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT, |
546 | KAUTH_ARG(i), KAUTH_ARG(vtype), NULL) != 0) { | | 538 | KAUTH_ARG(i), KAUTH_ARG(vtype), NULL) != 0) { |
547 | /* enforce this limit */ | | 539 | /* enforce this limit */ |
548 | switch(QL_STATUS(ql_stat)) { | | 540 | switch(QL_STATUS(ql_stat)) { |
549 | case QL_S_DENY_HARD: | | 541 | case QL_S_DENY_HARD: |
550 | if ((dq->dq_flags & DQ_WARN(vtype)) == 0) { | | 542 | if ((dq->dq_flags & DQ_WARN(vtype)) == 0) { |
551 | uprintf("\n%s: write failed, %s %s " | | 543 | uprintf("\n%s: write failed, %s %s " |
552 | "limit reached\n", | | 544 | "limit reached\n", |
553 | mp->mnt_stat.f_mntonname, | | 545 | mp->mnt_stat.f_mntonname, |
554 | quotatypes[i], limnames[vtype]); | | 546 | quotatypes[i], limnames[vtype]); |
555 | dq->dq_flags |= DQ_WARN(vtype); | | 547 | dq->dq_flags |= DQ_WARN(vtype); |
556 | } | | 548 | } |
557 | error = EDQUOT; | | 549 | error = EDQUOT; |
558 | break; | | 550 | break; |
559 | case QL_S_DENY_GRACE: | | 551 | case QL_S_DENY_GRACE: |
560 | if ((dq->dq_flags & DQ_WARN(vtype)) == 0) { | | 552 | if ((dq->dq_flags & DQ_WARN(vtype)) == 0) { |
561 | uprintf("\n%s: write failed, %s %s " | | 553 | uprintf("\n%s: write failed, %s %s " |
562 | "limit reached\n", | | 554 | "limit reached\n", |
563 | mp->mnt_stat.f_mntonname, | | 555 | mp->mnt_stat.f_mntonname, |
564 | quotatypes[i], limnames[vtype]); | | 556 | quotatypes[i], limnames[vtype]); |
565 | dq->dq_flags |= DQ_WARN(vtype); | | 557 | dq->dq_flags |= DQ_WARN(vtype); |
566 | } | | 558 | } |
567 | error = EDQUOT; | | 559 | error = EDQUOT; |
568 | break; | | 560 | break; |
569 | case QL_S_ALLOW_SOFT: | | 561 | case QL_S_ALLOW_SOFT: |
570 | if ((dq->dq_flags & DQ_WARN(vtype)) == 0) { | | 562 | if ((dq->dq_flags & DQ_WARN(vtype)) == 0) { |
571 | uprintf("\n%s: warning, %s %s " | | 563 | uprintf("\n%s: warning, %s %s " |
572 | "quota exceeded\n", | | 564 | "quota exceeded\n", |
573 | mp->mnt_stat.f_mntonname, | | 565 | mp->mnt_stat.f_mntonname, |
574 | quotatypes[i], limnames[vtype]); | | 566 | quotatypes[i], limnames[vtype]); |
575 | dq->dq_flags |= DQ_WARN(vtype); | | 567 | dq->dq_flags |= DQ_WARN(vtype); |
576 | } | | 568 | } |
577 | break; | | 569 | break; |
578 | } | | 570 | } |
579 | } | | 571 | } |
580 | /* | | 572 | /* |
581 | * always do this; we don't know if the allocation will | | 573 | * always do this; we don't know if the allocation will |
582 | * succed or not in the end. if we don't do the allocation | | 574 | * succed or not in the end. if we don't do the allocation |
583 | * q2v_time will be ignored anyway | | 575 | * q2v_time will be ignored anyway |
584 | */ | | 576 | */ |
585 | if (ql_stat & QL_F_CROSS) { | | 577 | if (ql_stat & QL_F_CROSS) { |
586 | q2v.q2v_time = time_second + q2v.q2v_grace; | | 578 | q2v.q2v_time = time_second + q2v.q2v_grace; |
587 | quota2_ufs_rwq2v(&q2v, &q2e[i]->q2e_val[vtype], | | 579 | quota2_ufs_rwq2v(&q2v, &q2e[i]->q2e_val[vtype], |
588 | needswap); | | 580 | needswap); |
589 | } | | 581 | } |
590 | } | | 582 | } |
591 | | | 583 | |
592 | /* now do the allocation if allowed */ | | 584 | /* now do the allocation if allowed */ |
593 | for (i = 0; i < MAXQUOTAS; i++) { | | 585 | for (i = 0; i < MAXQUOTAS; i++) { |
594 | dq = ip->i_dquot[i]; | | 586 | dq = ip->i_dquot[i]; |
595 | if (dq == NODQUOT) | | 587 | if (dq == NODQUOT) |
596 | continue; | | 588 | continue; |
597 | KASSERT(q2e[i] != NULL); | | 589 | KASSERT(q2e[i] != NULL); |
598 | if (error == 0) { | | 590 | if (error == 0) { |
599 | q2vp = &q2e[i]->q2e_val[vtype]; | | 591 | q2vp = &q2e[i]->q2e_val[vtype]; |
600 | ncurblks = ufs_rw64(q2vp->q2v_cur, needswap); | | 592 | ncurblks = ufs_rw64(q2vp->q2v_cur, needswap); |
601 | q2vp->q2v_cur = ufs_rw64(ncurblks + change, needswap); | | 593 | q2vp->q2v_cur = ufs_rw64(ncurblks + change, needswap); |
602 | quota2_bwrite(mp, bp[i]); | | 594 | quota2_bwrite(mp, bp[i]); |
603 | } else | | 595 | } else |
604 | brelse(bp[i], 0); | | 596 | brelse(bp[i], 0); |
605 | mutex_exit(&dq->dq_interlock); | | 597 | mutex_exit(&dq->dq_interlock); |
606 | } | | 598 | } |
607 | return error; | | 599 | return error; |
608 | } | | 600 | } |
609 | | | 601 | |
610 | int | | 602 | int |
611 | chkdq2(struct inode *ip, int64_t change, kauth_cred_t cred, int flags) | | 603 | chkdq2(struct inode *ip, int64_t change, kauth_cred_t cred, int flags) |
612 | { | | 604 | { |
613 | return quota2_check(ip, QL_BLOCK, change, cred, flags); | | 605 | return quota2_check(ip, QL_BLOCK, change, cred, flags); |
614 | } | | 606 | } |
615 | | | 607 | |
616 | int | | 608 | int |
617 | chkiq2(struct inode *ip, int32_t change, kauth_cred_t cred, int flags) | | 609 | chkiq2(struct inode *ip, int32_t change, kauth_cred_t cred, int flags) |
618 | { | | 610 | { |
619 | return quota2_check(ip, QL_FILE, change, cred, flags); | | 611 | return quota2_check(ip, QL_FILE, change, cred, flags); |
620 | } | | 612 | } |
621 | | | 613 | |
622 | int | | 614 | int |
623 | quota2_handle_cmd_set(struct ufsmount *ump, int type, int id, | | 615 | quota2_handle_cmd_set(struct ufsmount *ump, int type, int id, |
624 | int defaultq, prop_dictionary_t data) | | 616 | int defaultq, const struct quotaval *blocks, const struct quotaval *files) |
625 | { | | 617 | { |
626 | int error; | | 618 | int error; |
627 | struct dquot *dq; | | 619 | struct dquot *dq; |
628 | struct quota2_header *q2h; | | 620 | struct quota2_header *q2h; |
629 | struct quota2_entry q2e, *q2ep; | | 621 | struct quota2_entry q2e, *q2ep; |
630 | struct buf *bp; | | 622 | struct buf *bp; |
631 | const int needswap = UFS_MPNEEDSWAP(ump); | | 623 | const int needswap = UFS_MPNEEDSWAP(ump); |
632 | | | 624 | |
633 | if (ump->um_quotas[type] == NULLVP) | | 625 | if (ump->um_quotas[type] == NULLVP) |
634 | return ENODEV; | | 626 | return ENODEV; |
635 | error = UFS_WAPBL_BEGIN(ump->um_mountp); | | 627 | error = UFS_WAPBL_BEGIN(ump->um_mountp); |
636 | if (error) | | 628 | if (error) |
637 | return error; | | 629 | return error; |
638 | | | 630 | |
639 | if (defaultq) { | | 631 | if (defaultq) { |
640 | mutex_enter(&dqlock); | | 632 | mutex_enter(&dqlock); |
641 | error = getq2h(ump, type, &bp, &q2h, B_MODIFY); | | 633 | error = getq2h(ump, type, &bp, &q2h, B_MODIFY); |
642 | if (error) { | | 634 | if (error) { |
643 | mutex_exit(&dqlock); | | 635 | mutex_exit(&dqlock); |
644 | goto out_wapbl; | | 636 | goto out_wapbl; |
645 | } | | 637 | } |
646 | quota2_ufs_rwq2e(&q2h->q2h_defentry, &q2e, needswap); | | 638 | quota2_ufs_rwq2e(&q2h->q2h_defentry, &q2e, needswap); |
647 | error = quota2_dict_update_q2e_limits(data, &q2e); | | 639 | quota2_dict_update_q2e_limits(blocks, files, &q2e); |
648 | if (error) { | | | |
649 | mutex_exit(&dqlock); | | | |
650 | brelse(bp, 0); | | | |
651 | goto out_wapbl; | | | |
652 | } | | | |
653 | quota2_ufs_rwq2e(&q2e, &q2h->q2h_defentry, needswap); | | 640 | quota2_ufs_rwq2e(&q2e, &q2h->q2h_defentry, needswap); |
654 | mutex_exit(&dqlock); | | 641 | mutex_exit(&dqlock); |
655 | quota2_bwrite(ump->um_mountp, bp); | | 642 | quota2_bwrite(ump->um_mountp, bp); |
656 | goto out_wapbl; | | 643 | goto out_wapbl; |
657 | } | | 644 | } |
658 | | | 645 | |
659 | error = dqget(NULLVP, id, ump, type, &dq); | | 646 | error = dqget(NULLVP, id, ump, type, &dq); |
660 | if (error) | | 647 | if (error) |
661 | goto out_wapbl; | | 648 | goto out_wapbl; |
662 | | | 649 | |
663 | mutex_enter(&dq->dq_interlock); | | 650 | mutex_enter(&dq->dq_interlock); |
664 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { | | 651 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { |
665 | /* need to alloc a new on-disk quot */ | | 652 | /* need to alloc a new on-disk quot */ |
666 | mutex_enter(&dqlock); | | 653 | mutex_enter(&dqlock); |
667 | error = quota2_q2ealloc(ump, type, id, dq, &bp, &q2ep); | | 654 | error = quota2_q2ealloc(ump, type, id, dq, &bp, &q2ep); |
668 | mutex_exit(&dqlock); | | 655 | mutex_exit(&dqlock); |
669 | } else { | | 656 | } else { |
670 | error = getq2e(ump, type, dq->dq2_lblkno, dq->dq2_blkoff, | | 657 | error = getq2e(ump, type, dq->dq2_lblkno, dq->dq2_blkoff, |
671 | &bp, &q2ep, B_MODIFY); | | 658 | &bp, &q2ep, B_MODIFY); |
672 | } | | 659 | } |
673 | if (error) | | 660 | if (error) |
674 | goto out_il; | | 661 | goto out_il; |
675 | | | 662 | |
676 | quota2_ufs_rwq2e(q2ep, &q2e, needswap); | | 663 | quota2_ufs_rwq2e(q2ep, &q2e, needswap); |
677 | error = quota2_dict_update_q2e_limits(data, &q2e); | | 664 | quota2_dict_update_q2e_limits(blocks, files, &q2e); |
678 | if (error) { | | | |
679 | brelse(bp, 0); | | | |
680 | goto out_il; | | | |
681 | } | | | |
682 | quota2_ufs_rwq2e(&q2e, q2ep, needswap); | | 665 | quota2_ufs_rwq2e(&q2e, q2ep, needswap); |
683 | quota2_bwrite(ump->um_mountp, bp); | | 666 | quota2_bwrite(ump->um_mountp, bp); |
684 | | | 667 | |
685 | out_il: | | 668 | out_il: |
686 | mutex_exit(&dq->dq_interlock); | | 669 | mutex_exit(&dq->dq_interlock); |
687 | dqrele(NULLVP, dq); | | 670 | dqrele(NULLVP, dq); |
688 | out_wapbl: | | 671 | out_wapbl: |
689 | UFS_WAPBL_END(ump->um_mountp); | | 672 | UFS_WAPBL_END(ump->um_mountp); |
690 | return error; | | 673 | return error; |
691 | } | | 674 | } |
692 | | | 675 | |
693 | struct dq2clear_callback { | | 676 | struct dq2clear_callback { |
694 | uid_t id; | | 677 | uid_t id; |
695 | struct dquot *dq; | | 678 | struct dquot *dq; |
696 | struct quota2_header *q2h; | | 679 | struct quota2_header *q2h; |
697 | }; | | 680 | }; |
698 | | | 681 | |
699 | static int | | 682 | static int |
700 | dq2clear_callback(struct ufsmount *ump, uint64_t *offp, struct quota2_entry *q2e, | | 683 | dq2clear_callback(struct ufsmount *ump, uint64_t *offp, struct quota2_entry *q2e, |
701 | uint64_t off, void *v) | | 684 | uint64_t off, void *v) |
702 | { | | 685 | { |
703 | struct dq2clear_callback *c = v; | | 686 | struct dq2clear_callback *c = v; |
704 | #ifdef FFS_EI | | 687 | #ifdef FFS_EI |
705 | const int needswap = UFS_MPNEEDSWAP(ump); | | 688 | const int needswap = UFS_MPNEEDSWAP(ump); |
706 | #endif | | 689 | #endif |
707 | uint64_t myoff; | | 690 | uint64_t myoff; |
708 | | | 691 | |
709 | if (ufs_rw32(q2e->q2e_uid, needswap) == c->id) { | | 692 | if (ufs_rw32(q2e->q2e_uid, needswap) == c->id) { |
710 | KASSERT(mutex_owned(&c->dq->dq_interlock)); | | 693 | KASSERT(mutex_owned(&c->dq->dq_interlock)); |
711 | c->dq->dq2_lblkno = 0; | | 694 | c->dq->dq2_lblkno = 0; |
712 | c->dq->dq2_blkoff = 0; | | 695 | c->dq->dq2_blkoff = 0; |
713 | myoff = *offp; | | 696 | myoff = *offp; |
714 | /* remove from hash list */ | | 697 | /* remove from hash list */ |
715 | *offp = q2e->q2e_next; | | 698 | *offp = q2e->q2e_next; |
716 | /* add to free list */ | | 699 | /* add to free list */ |
717 | q2e->q2e_next = c->q2h->q2h_free; | | 700 | q2e->q2e_next = c->q2h->q2h_free; |
718 | c->q2h->q2h_free = myoff; | | 701 | c->q2h->q2h_free = myoff; |
719 | return Q2WL_ABORT; | | 702 | return Q2WL_ABORT; |
720 | } | | 703 | } |
721 | return 0; | | 704 | return 0; |
722 | } | | 705 | } |
723 | int | | 706 | int |
724 | quota2_handle_cmd_clear(struct ufsmount *ump, int type, int id, | | 707 | quota2_handle_cmd_clear(struct ufsmount *ump, int type, int id, |
725 | int defaultq, prop_dictionary_t data) | | 708 | int defaultq, prop_dictionary_t data) |
726 | { | | 709 | { |
727 | int error, i; | | 710 | int error, i; |
728 | struct dquot *dq; | | 711 | struct dquot *dq; |
729 | struct quota2_header *q2h; | | 712 | struct quota2_header *q2h; |
730 | struct quota2_entry q2e, *q2ep; | | 713 | struct quota2_entry q2e, *q2ep; |
731 | struct buf *hbp, *bp; | | 714 | struct buf *hbp, *bp; |
732 | u_long hash_mask; | | 715 | u_long hash_mask; |
733 | struct dq2clear_callback c; | | 716 | struct dq2clear_callback c; |
734 | | | 717 | |
735 | if (ump->um_quotas[type] == NULLVP) | | 718 | if (ump->um_quotas[type] == NULLVP) |
736 | return ENODEV; | | 719 | return ENODEV; |
737 | if (defaultq) | | 720 | if (defaultq) |
738 | return EOPNOTSUPP; | | 721 | return EOPNOTSUPP; |
739 | | | 722 | |
740 | /* get the default entry before locking the entry's buffer */ | | 723 | /* get the default entry before locking the entry's buffer */ |
741 | mutex_enter(&dqlock); | | 724 | mutex_enter(&dqlock); |
742 | error = getq2h(ump, type, &hbp, &q2h, 0); | | 725 | error = getq2h(ump, type, &hbp, &q2h, 0); |
743 | if (error) { | | 726 | if (error) { |
744 | mutex_exit(&dqlock); | | 727 | mutex_exit(&dqlock); |
745 | return error; | | 728 | return error; |
746 | } | | 729 | } |
747 | /* we'll copy to another disk entry, so no need to swap */ | | 730 | /* we'll copy to another disk entry, so no need to swap */ |
748 | memcpy(&q2e, &q2h->q2h_defentry, sizeof(q2e)); | | 731 | memcpy(&q2e, &q2h->q2h_defentry, sizeof(q2e)); |
749 | mutex_exit(&dqlock); | | 732 | mutex_exit(&dqlock); |
750 | brelse(hbp, 0); | | 733 | brelse(hbp, 0); |
751 | | | 734 | |
752 | error = dqget(NULLVP, id, ump, type, &dq); | | 735 | error = dqget(NULLVP, id, ump, type, &dq); |
753 | if (error) | | 736 | if (error) |
754 | return error; | | 737 | return error; |
755 | | | 738 | |
756 | mutex_enter(&dq->dq_interlock); | | 739 | mutex_enter(&dq->dq_interlock); |
757 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { | | 740 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { |
758 | /* already clear, nothing to do */ | | 741 | /* already clear, nothing to do */ |
759 | error = ENOENT; | | 742 | error = ENOENT; |
760 | goto out_il; | | 743 | goto out_il; |
761 | } | | 744 | } |
762 | error = UFS_WAPBL_BEGIN(ump->um_mountp); | | 745 | error = UFS_WAPBL_BEGIN(ump->um_mountp); |
763 | if (error) | | 746 | if (error) |
764 | goto out_dq; | | 747 | goto out_dq; |
765 | | | 748 | |
766 | error = getq2e(ump, type, dq->dq2_lblkno, dq->dq2_blkoff, | | 749 | error = getq2e(ump, type, dq->dq2_lblkno, dq->dq2_blkoff, |
767 | &bp, &q2ep, B_MODIFY); | | 750 | &bp, &q2ep, B_MODIFY); |
768 | if (error) | | 751 | if (error) |
769 | goto out_wapbl; | | 752 | goto out_wapbl; |
770 | | | 753 | |
771 | if (q2ep->q2e_val[QL_BLOCK].q2v_cur != 0 || | | 754 | if (q2ep->q2e_val[QL_BLOCK].q2v_cur != 0 || |
772 | q2ep->q2e_val[QL_FILE].q2v_cur != 0) { | | 755 | q2ep->q2e_val[QL_FILE].q2v_cur != 0) { |
773 | /* can't free this entry; revert to default */ | | 756 | /* can't free this entry; revert to default */ |
774 | for (i = 0; i < N_QL; i++) { | | 757 | for (i = 0; i < N_QL; i++) { |
775 | q2ep->q2e_val[i].q2v_softlimit = | | 758 | q2ep->q2e_val[i].q2v_softlimit = |
776 | q2e.q2e_val[i].q2v_softlimit; | | 759 | q2e.q2e_val[i].q2v_softlimit; |
777 | q2ep->q2e_val[i].q2v_hardlimit = | | 760 | q2ep->q2e_val[i].q2v_hardlimit = |
778 | q2e.q2e_val[i].q2v_hardlimit; | | 761 | q2e.q2e_val[i].q2v_hardlimit; |
779 | q2ep->q2e_val[i].q2v_grace = | | 762 | q2ep->q2e_val[i].q2v_grace = |
780 | q2e.q2e_val[i].q2v_grace; | | 763 | q2e.q2e_val[i].q2v_grace; |
781 | q2ep->q2e_val[i].q2v_time = 0; | | 764 | q2ep->q2e_val[i].q2v_time = 0; |
782 | } | | 765 | } |
783 | quota2_bwrite(ump->um_mountp, bp); | | 766 | quota2_bwrite(ump->um_mountp, bp); |
784 | goto out_wapbl; | | 767 | goto out_wapbl; |
785 | } | | 768 | } |
786 | /* we can free it. release bp so we can walk the list */ | | 769 | /* we can free it. release bp so we can walk the list */ |
787 | brelse(bp, 0); | | 770 | brelse(bp, 0); |
788 | mutex_enter(&dqlock); | | 771 | mutex_enter(&dqlock); |
789 | error = getq2h(ump, type, &hbp, &q2h, 0); | | 772 | error = getq2h(ump, type, &hbp, &q2h, 0); |
790 | if (error) | | 773 | if (error) |
791 | goto out_dqlock; | | 774 | goto out_dqlock; |
792 | | | 775 | |
793 | hash_mask = ((1 << q2h->q2h_hash_shift) - 1); | | 776 | hash_mask = ((1 << q2h->q2h_hash_shift) - 1); |
794 | c.dq = dq; | | 777 | c.dq = dq; |
795 | c.id = id; | | 778 | c.id = id; |
796 | c.q2h = q2h; | | 779 | c.q2h = q2h; |
797 | error = quota2_walk_list(ump, hbp, type, | | 780 | error = quota2_walk_list(ump, hbp, type, |
798 | &q2h->q2h_entries[id & hash_mask], B_MODIFY, &c, | | 781 | &q2h->q2h_entries[id & hash_mask], B_MODIFY, &c, |
799 | dq2clear_callback); | | 782 | dq2clear_callback); |
800 | | | 783 | |
801 | bwrite(hbp); | | 784 | bwrite(hbp); |
802 | | | 785 | |
803 | out_dqlock: | | 786 | out_dqlock: |
804 | mutex_exit(&dqlock); | | 787 | mutex_exit(&dqlock); |
805 | out_wapbl: | | 788 | out_wapbl: |
806 | UFS_WAPBL_END(ump->um_mountp); | | 789 | UFS_WAPBL_END(ump->um_mountp); |
807 | out_il: | | 790 | out_il: |
808 | mutex_exit(&dq->dq_interlock); | | 791 | mutex_exit(&dq->dq_interlock); |
809 | out_dq: | | 792 | out_dq: |
810 | dqrele(NULLVP, dq); | | 793 | dqrele(NULLVP, dq); |
811 | return error; | | 794 | return error; |
812 | } | | 795 | } |
813 | | | 796 | |
814 | static int | | 797 | static int |
815 | quota2_array_add_q2e(struct ufsmount *ump, int type, | | 798 | quota2_array_add_q2e(struct ufsmount *ump, int type, |
816 | int id, prop_array_t replies) | | 799 | int id, prop_array_t replies) |
817 | { | | 800 | { |
818 | struct dquot *dq; | | 801 | struct dquot *dq; |
819 | int error; | | 802 | int error; |
820 | struct quota2_entry *q2ep, q2e; | | 803 | struct quota2_entry *q2ep, q2e; |
821 | struct buf *bp; | | 804 | struct buf *bp; |
822 | const int needswap = UFS_MPNEEDSWAP(ump); | | 805 | const int needswap = UFS_MPNEEDSWAP(ump); |
823 | prop_dictionary_t dict; | | 806 | prop_dictionary_t dict; |
824 | | | 807 | |
825 | error = dqget(NULLVP, id, ump, type, &dq); | | 808 | error = dqget(NULLVP, id, ump, type, &dq); |
826 | if (error) | | 809 | if (error) |
827 | return error; | | 810 | return error; |
828 | | | 811 | |
829 | mutex_enter(&dq->dq_interlock); | | 812 | mutex_enter(&dq->dq_interlock); |
830 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { | | 813 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { |
831 | mutex_exit(&dq->dq_interlock); | | 814 | mutex_exit(&dq->dq_interlock); |
832 | dqrele(NULLVP, dq); | | 815 | dqrele(NULLVP, dq); |
833 | return ENOENT; | | 816 | return ENOENT; |
834 | } | | 817 | } |
835 | error = getq2e(ump, type, dq->dq2_lblkno, dq->dq2_blkoff, | | 818 | error = getq2e(ump, type, dq->dq2_lblkno, dq->dq2_blkoff, |
836 | &bp, &q2ep, 0); | | 819 | &bp, &q2ep, 0); |
837 | if (error) { | | 820 | if (error) { |
838 | mutex_exit(&dq->dq_interlock); | | 821 | mutex_exit(&dq->dq_interlock); |
839 | dqrele(NULLVP, dq); | | 822 | dqrele(NULLVP, dq); |
840 | return error; | | 823 | return error; |
841 | } | | 824 | } |
842 | quota2_ufs_rwq2e(q2ep, &q2e, needswap); | | 825 | quota2_ufs_rwq2e(q2ep, &q2e, needswap); |
843 | brelse(bp, 0); | | 826 | brelse(bp, 0); |
844 | mutex_exit(&dq->dq_interlock); | | 827 | mutex_exit(&dq->dq_interlock); |
845 | dqrele(NULLVP, dq); | | 828 | dqrele(NULLVP, dq); |
846 | | | 829 | |
847 | dict = q2etoprop(&q2e, 0); | | 830 | dict = q2etoprop(&q2e, 0); |
848 | if (dict == NULL) | | 831 | if (dict == NULL) |
849 | return ENOMEM; | | 832 | return ENOMEM; |
850 | if (!prop_array_add_and_rel(replies, dict)) | | 833 | if (!prop_array_add_and_rel(replies, dict)) |
851 | return ENOMEM; | | 834 | return ENOMEM; |
852 | return 0; | | 835 | return 0; |
853 | } | | 836 | } |
854 | | | 837 | |
855 | static int | | 838 | static int |
856 | quota2_fetch_q2e(struct ufsmount *ump, const struct quotakey *qk, | | 839 | quota2_fetch_q2e(struct ufsmount *ump, const struct quotakey *qk, |
857 | struct quotaval *ret) | | 840 | struct quotaval *ret) |
858 | { | | 841 | { |
859 | struct dquot *dq; | | 842 | struct dquot *dq; |
860 | int error; | | 843 | int error; |
861 | struct quota2_entry *q2ep, q2e; | | 844 | struct quota2_entry *q2ep, q2e; |
862 | struct buf *bp; | | 845 | struct buf *bp; |
863 | const int needswap = UFS_MPNEEDSWAP(ump); | | 846 | const int needswap = UFS_MPNEEDSWAP(ump); |
864 | id_t id2; | | 847 | id_t id2; |
865 | | | 848 | |
866 | error = dqget(NULLVP, qk->qk_id, ump, qk->qk_idtype, &dq); | | 849 | error = dqget(NULLVP, qk->qk_id, ump, qk->qk_idtype, &dq); |
867 | if (error) | | 850 | if (error) |
868 | return error; | | 851 | return error; |
869 | | | 852 | |
870 | mutex_enter(&dq->dq_interlock); | | 853 | mutex_enter(&dq->dq_interlock); |
871 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { | | 854 | if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) { |
872 | mutex_exit(&dq->dq_interlock); | | 855 | mutex_exit(&dq->dq_interlock); |
873 | dqrele(NULLVP, dq); | | 856 | dqrele(NULLVP, dq); |
874 | return ENOENT; | | 857 | return ENOENT; |
875 | } | | 858 | } |
876 | error = getq2e(ump, qk->qk_idtype, dq->dq2_lblkno, dq->dq2_blkoff, | | 859 | error = getq2e(ump, qk->qk_idtype, dq->dq2_lblkno, dq->dq2_blkoff, |
877 | &bp, &q2ep, 0); | | 860 | &bp, &q2ep, 0); |
878 | if (error) { | | 861 | if (error) { |
879 | mutex_exit(&dq->dq_interlock); | | 862 | mutex_exit(&dq->dq_interlock); |
880 | dqrele(NULLVP, dq); | | 863 | dqrele(NULLVP, dq); |
881 | return error; | | 864 | return error; |
882 | } | | 865 | } |
883 | quota2_ufs_rwq2e(q2ep, &q2e, needswap); | | 866 | quota2_ufs_rwq2e(q2ep, &q2e, needswap); |
884 | brelse(bp, 0); | | 867 | brelse(bp, 0); |
885 | mutex_exit(&dq->dq_interlock); | | 868 | mutex_exit(&dq->dq_interlock); |
886 | dqrele(NULLVP, dq); | | 869 | dqrele(NULLVP, dq); |
887 | | | 870 | |
888 | q2e_to_quotaval(&q2e, 0, &id2, qk->qk_objtype, ret); | | 871 | q2e_to_quotaval(&q2e, 0, &id2, qk->qk_objtype, ret); |
889 | KASSERT(id2 == qk->qk_id); | | 872 | KASSERT(id2 == qk->qk_id); |
890 | return 0; | | 873 | return 0; |
891 | } | | 874 | } |
892 | | | 875 | |
893 | int | | 876 | int |
894 | quota2_handle_cmd_get(struct ufsmount *ump, const struct quotakey *qk, | | 877 | quota2_handle_cmd_get(struct ufsmount *ump, const struct quotakey *qk, |
895 | struct quotaval *ret) | | 878 | struct quotaval *ret) |
896 | { | | 879 | { |
897 | int error; | | 880 | int error; |
898 | struct quota2_header *q2h; | | 881 | struct quota2_header *q2h; |
899 | struct quota2_entry q2e; | | 882 | struct quota2_entry q2e; |
900 | struct buf *bp; | | 883 | struct buf *bp; |
901 | const int needswap = UFS_MPNEEDSWAP(ump); | | 884 | const int needswap = UFS_MPNEEDSWAP(ump); |
902 | id_t id2; | | 885 | id_t id2; |
903 | | | 886 | |
904 | /* | | 887 | /* |
905 | * Make sure the FS-independent codes match the internal ones, | | 888 | * Make sure the FS-independent codes match the internal ones, |
906 | * so we can use the passed-in objtype without having to | | 889 | * so we can use the passed-in objtype without having to |
907 | * convert it explicitly to QL_BLOCK/QL_FILE. | | 890 | * convert it explicitly to QL_BLOCK/QL_FILE. |
908 | */ | | 891 | */ |
909 | CTASSERT(QL_BLOCK == QUOTA_OBJTYPE_BLOCKS); | | 892 | CTASSERT(QL_BLOCK == QUOTA_OBJTYPE_BLOCKS); |
910 | CTASSERT(QL_FILE == QUOTA_OBJTYPE_FILES); | | 893 | CTASSERT(QL_FILE == QUOTA_OBJTYPE_FILES); |
911 | CTASSERT(N_QL == 2); | | 894 | CTASSERT(N_QL == 2); |
912 | | | 895 | |
913 | if (qk->qk_objtype < 0 || qk->qk_objtype >= N_QL) { | | 896 | if (qk->qk_objtype < 0 || qk->qk_objtype >= N_QL) { |
914 | return EINVAL; | | 897 | return EINVAL; |
915 | } | | 898 | } |
916 | | | 899 | |
917 | if (ump->um_quotas[qk->qk_idtype] == NULLVP) | | 900 | if (ump->um_quotas[qk->qk_idtype] == NULLVP) |
918 | return ENODEV; | | 901 | return ENODEV; |
919 | if (qk->qk_id == QUOTA_DEFAULTID) { | | 902 | if (qk->qk_id == QUOTA_DEFAULTID) { |
920 | mutex_enter(&dqlock); | | 903 | mutex_enter(&dqlock); |
921 | error = getq2h(ump, qk->qk_idtype, &bp, &q2h, 0); | | 904 | error = getq2h(ump, qk->qk_idtype, &bp, &q2h, 0); |
922 | if (error) { | | 905 | if (error) { |
923 | mutex_exit(&dqlock); | | 906 | mutex_exit(&dqlock); |
924 | return error; | | 907 | return error; |
925 | } | | 908 | } |
926 | quota2_ufs_rwq2e(&q2h->q2h_defentry, &q2e, needswap); | | 909 | quota2_ufs_rwq2e(&q2h->q2h_defentry, &q2e, needswap); |
927 | mutex_exit(&dqlock); | | 910 | mutex_exit(&dqlock); |
928 | brelse(bp, 0); | | 911 | brelse(bp, 0); |
929 | q2e_to_quotaval(&q2e, qk->qk_id == QUOTA_DEFAULTID, &id2, | | 912 | q2e_to_quotaval(&q2e, qk->qk_id == QUOTA_DEFAULTID, &id2, |
930 | qk->qk_objtype, ret); | | 913 | qk->qk_objtype, ret); |
931 | (void)id2; | | 914 | (void)id2; |
932 | } else | | 915 | } else |
933 | error = quota2_fetch_q2e(ump, qk, ret); | | 916 | error = quota2_fetch_q2e(ump, qk, ret); |
934 | | | 917 | |
935 | return error; | | 918 | return error; |
936 | } | | 919 | } |
937 | | | 920 | |
938 | struct getuids { | | 921 | struct getuids { |
939 | long nuids; /* number of uids in array */ | | 922 | long nuids; /* number of uids in array */ |
940 | long size; /* size of array */ | | 923 | long size; /* size of array */ |
941 | uid_t *uids; /* array of uids, dynamically allocated */ | | 924 | uid_t *uids; /* array of uids, dynamically allocated */ |
942 | }; | | 925 | }; |
943 | | | 926 | |
944 | static int | | 927 | static int |
945 | quota2_getuids_callback(struct ufsmount *ump, uint64_t *offp, | | 928 | quota2_getuids_callback(struct ufsmount *ump, uint64_t *offp, |
946 | struct quota2_entry *q2ep, uint64_t off, void *v) | | 929 | struct quota2_entry *q2ep, uint64_t off, void *v) |
947 | { | | 930 | { |
948 | struct getuids *gu = v; | | 931 | struct getuids *gu = v; |
949 | uid_t *newuids; | | 932 | uid_t *newuids; |
950 | #ifdef FFS_EI | | 933 | #ifdef FFS_EI |
951 | const int needswap = UFS_MPNEEDSWAP(ump); | | 934 | const int needswap = UFS_MPNEEDSWAP(ump); |
952 | #endif | | 935 | #endif |
953 | | | 936 | |
954 | if (gu->nuids == gu->size) { | | 937 | if (gu->nuids == gu->size) { |
955 | newuids = realloc(gu->uids, gu->size + PAGE_SIZE, M_TEMP, | | 938 | newuids = realloc(gu->uids, gu->size + PAGE_SIZE, M_TEMP, |
956 | M_WAITOK); | | 939 | M_WAITOK); |
957 | if (newuids == NULL) { | | 940 | if (newuids == NULL) { |
958 | free(gu->uids, M_TEMP); | | 941 | free(gu->uids, M_TEMP); |
959 | return ENOMEM; | | 942 | return ENOMEM; |
960 | } | | 943 | } |
961 | gu->uids = newuids; | | 944 | gu->uids = newuids; |
962 | gu->size += (PAGE_SIZE / sizeof(uid_t)); | | 945 | gu->size += (PAGE_SIZE / sizeof(uid_t)); |
963 | } | | 946 | } |
964 | gu->uids[gu->nuids] = ufs_rw32(q2ep->q2e_uid, needswap); | | 947 | gu->uids[gu->nuids] = ufs_rw32(q2ep->q2e_uid, needswap); |
965 | gu->nuids++; | | 948 | gu->nuids++; |
966 | return 0; | | 949 | return 0; |
967 | } | | 950 | } |
968 | | | 951 | |
969 | int | | 952 | int |
970 | quota2_handle_cmd_getall(struct ufsmount *ump, int type, prop_array_t replies) | | 953 | quota2_handle_cmd_getall(struct ufsmount *ump, int type, prop_array_t replies) |
971 | { | | 954 | { |
972 | int error; | | 955 | int error; |
973 | struct quota2_header *q2h; | | 956 | struct quota2_header *q2h; |
974 | struct quota2_entry q2e; | | 957 | struct quota2_entry q2e; |
975 | struct buf *hbp; | | 958 | struct buf *hbp; |
976 | prop_dictionary_t dict; | | 959 | prop_dictionary_t dict; |
977 | uint64_t offset; | | 960 | uint64_t offset; |
978 | int i, j; | | 961 | int i, j; |
979 | int quota2_hash_size; | | 962 | int quota2_hash_size; |
980 | const int needswap = UFS_MPNEEDSWAP(ump); | | 963 | const int needswap = UFS_MPNEEDSWAP(ump); |
981 | struct getuids gu; | | 964 | struct getuids gu; |
982 | | | 965 | |
983 | if (ump->um_quotas[type] == NULLVP) | | 966 | if (ump->um_quotas[type] == NULLVP) |
984 | return ENODEV; | | 967 | return ENODEV; |
985 | mutex_enter(&dqlock); | | 968 | mutex_enter(&dqlock); |
986 | error = getq2h(ump, type, &hbp, &q2h, 0); | | 969 | error = getq2h(ump, type, &hbp, &q2h, 0); |
987 | if (error) { | | 970 | if (error) { |
988 | mutex_exit(&dqlock); | | 971 | mutex_exit(&dqlock); |
989 | return error; | | 972 | return error; |
990 | } | | 973 | } |
991 | quota2_ufs_rwq2e(&q2h->q2h_defentry, &q2e, needswap); | | 974 | quota2_ufs_rwq2e(&q2h->q2h_defentry, &q2e, needswap); |
992 | dict = q2etoprop(&q2e, 1); | | 975 | dict = q2etoprop(&q2e, 1); |
993 | if (!prop_array_add_and_rel(replies, dict)) { | | 976 | if (!prop_array_add_and_rel(replies, dict)) { |
994 | error = ENOMEM; | | 977 | error = ENOMEM; |
995 | goto error_bp; | | 978 | goto error_bp; |
996 | } | | 979 | } |
997 | /* | | 980 | /* |
998 | * we can't directly get entries as we can't walk the list | | 981 | * we can't directly get entries as we can't walk the list |
999 | * with qdlock and grab dq_interlock to read the entries | | 982 | * with qdlock and grab dq_interlock to read the entries |
1000 | * at the same time. So just walk the lists to build a list of uid, | | 983 | * at the same time. So just walk the lists to build a list of uid, |
1001 | * and then read entries for these uids | | 984 | * and then read entries for these uids |
1002 | */ | | 985 | */ |
1003 | memset(&gu, 0, sizeof(gu)); | | 986 | memset(&gu, 0, sizeof(gu)); |
1004 | quota2_hash_size = ufs_rw16(q2h->q2h_hash_size, needswap); | | 987 | quota2_hash_size = ufs_rw16(q2h->q2h_hash_size, needswap); |
1005 | for (i = 0; i < quota2_hash_size ; i++) { | | 988 | for (i = 0; i < quota2_hash_size ; i++) { |
1006 | offset = q2h->q2h_entries[i]; | | 989 | offset = q2h->q2h_entries[i]; |
1007 | error = quota2_walk_list(ump, hbp, type, &offset, 0, &gu, | | 990 | error = quota2_walk_list(ump, hbp, type, &offset, 0, &gu, |
1008 | quota2_getuids_callback); | | 991 | quota2_getuids_callback); |
1009 | if (error) { | | 992 | if (error) { |
1010 | if (gu.uids != NULL) | | 993 | if (gu.uids != NULL) |
1011 | free(gu.uids, M_TEMP); | | 994 | free(gu.uids, M_TEMP); |
1012 | break; | | 995 | break; |
1013 | } | | 996 | } |
1014 | } | | 997 | } |
1015 | error_bp: | | 998 | error_bp: |
1016 | mutex_exit(&dqlock); | | 999 | mutex_exit(&dqlock); |
1017 | brelse(hbp, 0); | | 1000 | brelse(hbp, 0); |
1018 | if (error) | | 1001 | if (error) |
1019 | return error; | | 1002 | return error; |
1020 | for (j = 0; j < gu.nuids; j++) { | | 1003 | for (j = 0; j < gu.nuids; j++) { |
1021 | error = quota2_array_add_q2e(ump, type, | | 1004 | error = quota2_array_add_q2e(ump, type, |
1022 | gu.uids[j], replies); | | 1005 | gu.uids[j], replies); |
1023 | if (error && error != ENOENT) | | 1006 | if (error && error != ENOENT) |
1024 | break; | | 1007 | break; |
1025 | } | | 1008 | } |
1026 | free(gu.uids, M_TEMP); | | 1009 | free(gu.uids, M_TEMP); |
1027 | return error; | | 1010 | return error; |
1028 | } | | 1011 | } |
1029 | | | 1012 | |
1030 | int | | 1013 | int |
1031 | q2sync(struct mount *mp) | | 1014 | q2sync(struct mount *mp) |
1032 | { | | 1015 | { |
1033 | return 0; | | 1016 | return 0; |
1034 | } | | 1017 | } |
1035 | | | 1018 | |
1036 | struct dq2get_callback { | | 1019 | struct dq2get_callback { |
1037 | uid_t id; | | 1020 | uid_t id; |
1038 | struct dquot *dq; | | 1021 | struct dquot *dq; |
1039 | }; | | 1022 | }; |
1040 | | | 1023 | |
1041 | static int | | 1024 | static int |
1042 | dq2get_callback(struct ufsmount *ump, uint64_t *offp, struct quota2_entry *q2e, | | 1025 | dq2get_callback(struct ufsmount *ump, uint64_t *offp, struct quota2_entry *q2e, |
1043 | uint64_t off, void *v) | | 1026 | uint64_t off, void *v) |
1044 | { | | 1027 | { |
1045 | struct dq2get_callback *c = v; | | 1028 | struct dq2get_callback *c = v; |
1046 | daddr_t lblkno; | | 1029 | daddr_t lblkno; |
1047 | int blkoff; | | 1030 | int blkoff; |
1048 | #ifdef FFS_EI | | 1031 | #ifdef FFS_EI |
1049 | const int needswap = UFS_MPNEEDSWAP(ump); | | 1032 | const int needswap = UFS_MPNEEDSWAP(ump); |
1050 | #endif | | 1033 | #endif |
1051 | | | 1034 | |
1052 | if (ufs_rw32(q2e->q2e_uid, needswap) == c->id) { | | 1035 | if (ufs_rw32(q2e->q2e_uid, needswap) == c->id) { |
1053 | KASSERT(mutex_owned(&c->dq->dq_interlock)); | | 1036 | KASSERT(mutex_owned(&c->dq->dq_interlock)); |
1054 | lblkno = (off >> ump->um_mountp->mnt_fs_bshift); | | 1037 | lblkno = (off >> ump->um_mountp->mnt_fs_bshift); |
1055 | blkoff = (off & ump->umq2_bmask); | | 1038 | blkoff = (off & ump->umq2_bmask); |
1056 | c->dq->dq2_lblkno = lblkno; | | 1039 | c->dq->dq2_lblkno = lblkno; |
1057 | c->dq->dq2_blkoff = blkoff; | | 1040 | c->dq->dq2_blkoff = blkoff; |
1058 | return Q2WL_ABORT; | | 1041 | return Q2WL_ABORT; |
1059 | } | | 1042 | } |
1060 | return 0; | | 1043 | return 0; |
1061 | } | | 1044 | } |
1062 | | | 1045 | |
1063 | int | | 1046 | int |
1064 | dq2get(struct vnode *dqvp, u_long id, struct ufsmount *ump, int type, | | 1047 | dq2get(struct vnode *dqvp, u_long id, struct ufsmount *ump, int type, |
1065 | struct dquot *dq) | | 1048 | struct dquot *dq) |
1066 | { | | 1049 | { |
1067 | struct buf *bp; | | 1050 | struct buf *bp; |
1068 | struct quota2_header *q2h; | | 1051 | struct quota2_header *q2h; |
1069 | int error; | | 1052 | int error; |
1070 | daddr_t offset; | | 1053 | daddr_t offset; |
1071 | u_long hash_mask; | | 1054 | u_long hash_mask; |
1072 | struct dq2get_callback c = { | | 1055 | struct dq2get_callback c = { |
1073 | .id = id, | | 1056 | .id = id, |
1074 | .dq = dq | | 1057 | .dq = dq |
1075 | }; | | 1058 | }; |
1076 | | | 1059 | |
1077 | KASSERT(mutex_owned(&dq->dq_interlock)); | | 1060 | KASSERT(mutex_owned(&dq->dq_interlock)); |
1078 | mutex_enter(&dqlock); | | 1061 | mutex_enter(&dqlock); |
1079 | error = getq2h(ump, type, &bp, &q2h, 0); | | 1062 | error = getq2h(ump, type, &bp, &q2h, 0); |
1080 | if (error) | | 1063 | if (error) |
1081 | goto out_mutex; | | 1064 | goto out_mutex; |
1082 | /* look for our entry */ | | 1065 | /* look for our entry */ |
1083 | hash_mask = ((1 << q2h->q2h_hash_shift) - 1); | | 1066 | hash_mask = ((1 << q2h->q2h_hash_shift) - 1); |
1084 | offset = q2h->q2h_entries[id & hash_mask]; | | 1067 | offset = q2h->q2h_entries[id & hash_mask]; |
1085 | error = quota2_walk_list(ump, bp, type, &offset, 0, (void *)&c, | | 1068 | error = quota2_walk_list(ump, bp, type, &offset, 0, (void *)&c, |
1086 | dq2get_callback); | | 1069 | dq2get_callback); |
1087 | brelse(bp, 0); | | 1070 | brelse(bp, 0); |
1088 | out_mutex: | | 1071 | out_mutex: |
1089 | mutex_exit(&dqlock); | | 1072 | mutex_exit(&dqlock); |
1090 | return error; | | 1073 | return error; |
1091 | } | | 1074 | } |
1092 | | | 1075 | |
1093 | int | | 1076 | int |
1094 | dq2sync(struct vnode *vp, struct dquot *dq) | | 1077 | dq2sync(struct vnode *vp, struct dquot *dq) |
1095 | { | | 1078 | { |
1096 | return 0; | | 1079 | return 0; |
1097 | } | | 1080 | } |