ufs_quota.c revision 1.69 1 1.69 bouyer /* $NetBSD: ufs_quota.c,v 1.69 2011/03/06 17:08:39 bouyer Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.11 fvdl * Copyright (c) 1982, 1986, 1990, 1993, 1995
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * This code is derived from software contributed to Berkeley by
8 1.1 mycroft * Robert Elz at The University of Melbourne.
9 1.1 mycroft *
10 1.1 mycroft * Redistribution and use in source and binary forms, with or without
11 1.1 mycroft * modification, are permitted provided that the following conditions
12 1.1 mycroft * are met:
13 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.1 mycroft * notice, this list of conditions and the following disclaimer.
15 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.1 mycroft * documentation and/or other materials provided with the distribution.
18 1.28 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 mycroft * may be used to endorse or promote products derived from this software
20 1.1 mycroft * without specific prior written permission.
21 1.1 mycroft *
22 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 mycroft * SUCH DAMAGE.
33 1.1 mycroft *
34 1.11 fvdl * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
35 1.1 mycroft */
36 1.22 lukem
37 1.22 lukem #include <sys/cdefs.h>
38 1.69 bouyer __KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.69 2011/03/06 17:08:39 bouyer Exp $");
39 1.22 lukem
40 1.69 bouyer #if defined(_KERNEL_OPT)
41 1.69 bouyer #include "opt_quota.h"
42 1.69 bouyer #endif
43 1.1 mycroft #include <sys/param.h>
44 1.1 mycroft #include <sys/kernel.h>
45 1.1 mycroft #include <sys/systm.h>
46 1.1 mycroft #include <sys/namei.h>
47 1.1 mycroft #include <sys/file.h>
48 1.1 mycroft #include <sys/proc.h>
49 1.1 mycroft #include <sys/vnode.h>
50 1.1 mycroft #include <sys/mount.h>
51 1.39 elad #include <sys/kauth.h>
52 1.1 mycroft
53 1.1 mycroft #include <ufs/ufs/quota.h>
54 1.1 mycroft #include <ufs/ufs/inode.h>
55 1.1 mycroft #include <ufs/ufs/ufsmount.h>
56 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
57 1.69 bouyer #include <ufs/ufs/ufs_quota.h>
58 1.69 bouyer #include <ufs/ufs/quota2_prop.h>
59 1.1 mycroft
60 1.69 bouyer kmutex_t dqlock;
61 1.69 bouyer kcondvar_t dqcv;
62 1.48 hannken
63 1.69 bouyer const char *quotatypes[MAXQUOTAS] = INITQFNAMES;
64 1.48 hannken
65 1.48 hannken /*
66 1.69 bouyer * Code pertaining to management of the in-core dquot data structures.
67 1.1 mycroft */
68 1.69 bouyer #define DQHASH(dqvp, id) \
69 1.69 bouyer (((((long)(dqvp)) >> 8) + id) & dqhash)
70 1.69 bouyer static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
71 1.69 bouyer static u_long dqhash;
72 1.69 bouyer static pool_cache_t dquot_cache;
73 1.1 mycroft
74 1.1 mycroft
75 1.69 bouyer static int quota_handle_cmd_get_version(struct mount *, struct lwp *,
76 1.69 bouyer prop_dictionary_t, prop_array_t);
77 1.69 bouyer static int quota_handle_cmd_get(struct mount *, struct lwp *,
78 1.69 bouyer prop_dictionary_t, int, prop_array_t);
79 1.69 bouyer static int quota_handle_cmd_set(struct mount *, struct lwp *,
80 1.69 bouyer prop_dictionary_t, int, prop_array_t);
81 1.69 bouyer static int quota_handle_cmd_getall(struct mount *, struct lwp *,
82 1.69 bouyer prop_dictionary_t, int, prop_array_t);
83 1.69 bouyer static int quota_handle_cmd_clear(struct mount *, struct lwp *,
84 1.69 bouyer prop_dictionary_t, int, prop_array_t);
85 1.69 bouyer static int quota_handle_cmd_quotaon(struct mount *, struct lwp *,
86 1.69 bouyer prop_dictionary_t, int, prop_array_t);
87 1.69 bouyer static int quota_handle_cmd_quotaoff(struct mount *, struct lwp *,
88 1.69 bouyer prop_dictionary_t, int, prop_array_t);
89 1.1 mycroft /*
90 1.48 hannken * Initialize the quota fields of an inode.
91 1.48 hannken */
92 1.48 hannken void
93 1.48 hannken ufsquota_init(struct inode *ip)
94 1.48 hannken {
95 1.48 hannken int i;
96 1.48 hannken
97 1.48 hannken for (i = 0; i < MAXQUOTAS; i++)
98 1.48 hannken ip->i_dquot[i] = NODQUOT;
99 1.48 hannken }
100 1.48 hannken
101 1.48 hannken /*
102 1.48 hannken * Release the quota fields from an inode.
103 1.48 hannken */
104 1.48 hannken void
105 1.48 hannken ufsquota_free(struct inode *ip)
106 1.48 hannken {
107 1.48 hannken int i;
108 1.48 hannken
109 1.48 hannken for (i = 0; i < MAXQUOTAS; i++) {
110 1.48 hannken dqrele(ITOV(ip), ip->i_dquot[i]);
111 1.48 hannken ip->i_dquot[i] = NODQUOT;
112 1.48 hannken }
113 1.48 hannken }
114 1.48 hannken
115 1.48 hannken /*
116 1.1 mycroft * Update disk usage, and take corrective action.
117 1.1 mycroft */
118 1.1 mycroft int
119 1.39 elad chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
120 1.1 mycroft {
121 1.69 bouyer /* do not track snapshot usage, or we will deadlock */
122 1.69 bouyer if ((ip->i_flags & SF_SNAPSHOT) != 0)
123 1.69 bouyer return 0;
124 1.1 mycroft
125 1.69 bouyer #ifdef QUOTA
126 1.69 bouyer if (ip->i_ump->um_flags & UFS_QUOTA)
127 1.69 bouyer return chkdq1(ip, change, cred, flags);
128 1.69 bouyer #endif
129 1.69 bouyer #ifdef QUOTA2
130 1.69 bouyer if (ip->i_ump->um_flags & UFS_QUOTA2)
131 1.69 bouyer return chkdq2(ip, change, cred, flags);
132 1.69 bouyer #endif
133 1.69 bouyer return 0;
134 1.1 mycroft }
135 1.1 mycroft
136 1.1 mycroft /*
137 1.69 bouyer * Check the inode limit, applying corrective action.
138 1.1 mycroft */
139 1.69 bouyer int
140 1.69 bouyer chkiq(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
141 1.1 mycroft {
142 1.69 bouyer /* do not track snapshot usage, or we will deadlock */
143 1.69 bouyer if ((ip->i_flags & SF_SNAPSHOT) != 0)
144 1.69 bouyer return 0;
145 1.69 bouyer #ifdef QUOTA
146 1.69 bouyer if (ip->i_ump->um_flags & UFS_QUOTA)
147 1.69 bouyer return chkiq1(ip, change, cred, flags);
148 1.69 bouyer #endif
149 1.69 bouyer #ifdef QUOTA2
150 1.69 bouyer if (ip->i_ump->um_flags & UFS_QUOTA2)
151 1.69 bouyer return chkiq2(ip, change, cred, flags);
152 1.69 bouyer #endif
153 1.69 bouyer return 0;
154 1.1 mycroft }
155 1.1 mycroft
156 1.1 mycroft int
157 1.69 bouyer quota_handle_cmd(struct mount *mp, struct lwp *l, prop_dictionary_t cmddict)
158 1.69 bouyer {
159 1.69 bouyer int error = 0;
160 1.69 bouyer const char *cmd, *type;
161 1.69 bouyer prop_array_t datas;
162 1.69 bouyer int q2type;
163 1.69 bouyer
164 1.69 bouyer if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
165 1.69 bouyer return EINVAL;
166 1.69 bouyer if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
167 1.69 bouyer return EINVAL;
168 1.69 bouyer if (!strcmp(type, "user")) {
169 1.69 bouyer q2type = USRQUOTA;
170 1.69 bouyer } else if (!strcmp(type, "group")) {
171 1.69 bouyer q2type = GRPQUOTA;
172 1.69 bouyer } else
173 1.69 bouyer return EOPNOTSUPP;
174 1.69 bouyer datas = prop_dictionary_get(cmddict, "data");
175 1.69 bouyer if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
176 1.69 bouyer return EINVAL;
177 1.69 bouyer
178 1.69 bouyer prop_object_retain(datas);
179 1.69 bouyer prop_dictionary_remove(cmddict, "data"); /* prepare for return */
180 1.69 bouyer
181 1.69 bouyer if (strcmp(cmd, "get version") == 0) {
182 1.69 bouyer error = quota_handle_cmd_get_version(mp, l, cmddict, datas);
183 1.69 bouyer goto end;
184 1.69 bouyer }
185 1.69 bouyer if (strcmp(cmd, "quotaon") == 0) {
186 1.69 bouyer error = quota_handle_cmd_quotaon(mp, l, cmddict,
187 1.69 bouyer q2type, datas);
188 1.69 bouyer goto end;
189 1.69 bouyer }
190 1.69 bouyer if (strcmp(cmd, "quotaoff") == 0) {
191 1.69 bouyer error = quota_handle_cmd_quotaoff(mp, l, cmddict,
192 1.69 bouyer q2type, datas);
193 1.69 bouyer goto end;
194 1.69 bouyer }
195 1.69 bouyer if (strcmp(cmd, "get") == 0) {
196 1.69 bouyer error = quota_handle_cmd_get(mp, l, cmddict, q2type, datas);
197 1.69 bouyer goto end;
198 1.69 bouyer }
199 1.69 bouyer if (strcmp(cmd, "set") == 0) {
200 1.69 bouyer error = quota_handle_cmd_set(mp, l, cmddict, q2type, datas);
201 1.69 bouyer goto end;
202 1.69 bouyer }
203 1.69 bouyer if (strcmp(cmd, "getall") == 0) {
204 1.69 bouyer error = quota_handle_cmd_getall(mp, l, cmddict, q2type, datas);
205 1.69 bouyer goto end;
206 1.69 bouyer }
207 1.69 bouyer if (strcmp(cmd, "clear") == 0) {
208 1.69 bouyer error = quota_handle_cmd_clear(mp, l, cmddict, q2type, datas);
209 1.69 bouyer goto end;
210 1.69 bouyer }
211 1.69 bouyer error = EOPNOTSUPP;
212 1.69 bouyer end:
213 1.69 bouyer error = (prop_dictionary_set_int8(cmddict, "return",
214 1.69 bouyer error) ? 0 : ENOMEM);
215 1.69 bouyer prop_object_release(datas);
216 1.69 bouyer return error;
217 1.69 bouyer }
218 1.69 bouyer
219 1.69 bouyer static int
220 1.69 bouyer quota_handle_cmd_get_version(struct mount *mp, struct lwp *l,
221 1.69 bouyer prop_dictionary_t cmddict, prop_array_t datas)
222 1.1 mycroft {
223 1.69 bouyer struct ufsmount *ump = VFSTOUFS(mp);
224 1.69 bouyer prop_array_t replies;
225 1.69 bouyer prop_dictionary_t data;
226 1.69 bouyer int error = 0;
227 1.69 bouyer
228 1.69 bouyer if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
229 1.69 bouyer return EOPNOTSUPP;
230 1.69 bouyer
231 1.69 bouyer replies = prop_array_create();
232 1.69 bouyer if (replies == NULL)
233 1.69 bouyer return ENOMEM;
234 1.1 mycroft
235 1.69 bouyer data = prop_dictionary_create();
236 1.69 bouyer if (data == NULL) {
237 1.69 bouyer prop_object_release(replies);
238 1.69 bouyer return ENOMEM;
239 1.1 mycroft }
240 1.69 bouyer
241 1.69 bouyer #ifdef QUOTA
242 1.69 bouyer if (ump->um_flags & UFS_QUOTA) {
243 1.69 bouyer if (!prop_dictionary_set_int8(data, "version", 1))
244 1.69 bouyer error = ENOMEM;
245 1.69 bouyer } else
246 1.69 bouyer #endif
247 1.69 bouyer #ifdef QUOTA2
248 1.69 bouyer if (ump->um_flags & UFS_QUOTA2) {
249 1.69 bouyer if (!prop_dictionary_set_int8(data, "version", 2))
250 1.69 bouyer error = ENOMEM;
251 1.69 bouyer } else
252 1.69 bouyer #endif
253 1.69 bouyer error = 0;
254 1.69 bouyer if (error)
255 1.69 bouyer prop_object_release(data);
256 1.69 bouyer else if (!prop_array_add_and_rel(replies, data))
257 1.69 bouyer error = ENOMEM;
258 1.69 bouyer if (error)
259 1.69 bouyer prop_object_release(replies);
260 1.69 bouyer else if (!prop_dictionary_set_and_rel(cmddict, "data", replies))
261 1.69 bouyer error = ENOMEM;
262 1.69 bouyer return error;
263 1.1 mycroft }
264 1.1 mycroft
265 1.69 bouyer /* XXX shouldn't all this be in kauth ? */
266 1.48 hannken static int
267 1.69 bouyer quota_get_auth(struct mount *mp, struct lwp *l, uid_t id) {
268 1.69 bouyer /* The user can always query about his own quota. */
269 1.69 bouyer if (id == kauth_cred_getuid(l->l_cred))
270 1.69 bouyer return 0;
271 1.69 bouyer return kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
272 1.69 bouyer KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(id), NULL);
273 1.69 bouyer }
274 1.69 bouyer
275 1.69 bouyer static int
276 1.69 bouyer quota_handle_cmd_get(struct mount *mp, struct lwp *l,
277 1.69 bouyer prop_dictionary_t cmddict, int type, prop_array_t datas)
278 1.69 bouyer {
279 1.69 bouyer prop_array_t replies;
280 1.69 bouyer prop_object_iterator_t iter;
281 1.69 bouyer prop_dictionary_t data;
282 1.69 bouyer uint32_t id;
283 1.69 bouyer struct ufsmount *ump = VFSTOUFS(mp);
284 1.69 bouyer int error, defaultq = 0;
285 1.69 bouyer const char *idstr;
286 1.69 bouyer
287 1.69 bouyer if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
288 1.69 bouyer return EOPNOTSUPP;
289 1.69 bouyer
290 1.69 bouyer replies = prop_array_create();
291 1.69 bouyer if (replies == NULL)
292 1.69 bouyer return ENOMEM;
293 1.1 mycroft
294 1.69 bouyer iter = prop_array_iterator(datas);
295 1.69 bouyer if (iter == NULL) {
296 1.69 bouyer prop_object_release(replies);
297 1.69 bouyer return ENOMEM;
298 1.1 mycroft }
299 1.69 bouyer while ((data = prop_object_iterator_next(iter)) != NULL) {
300 1.69 bouyer if (!prop_dictionary_get_uint32(data, "id", &id)) {
301 1.69 bouyer if (!prop_dictionary_get_cstring_nocopy(data, "id",
302 1.69 bouyer &idstr))
303 1.69 bouyer continue;
304 1.69 bouyer if (strcmp(idstr, "default")) {
305 1.69 bouyer error = EINVAL;
306 1.69 bouyer goto err;
307 1.1 mycroft }
308 1.69 bouyer id = 0;
309 1.69 bouyer defaultq = 1;
310 1.69 bouyer } else {
311 1.69 bouyer defaultq = 0;
312 1.1 mycroft }
313 1.69 bouyer error = quota_get_auth(mp, l, id);
314 1.69 bouyer if (error == EPERM)
315 1.69 bouyer continue;
316 1.69 bouyer if (error != 0)
317 1.69 bouyer goto err;
318 1.69 bouyer #ifdef QUOTA
319 1.69 bouyer if (ump->um_flags & UFS_QUOTA)
320 1.69 bouyer error = quota1_handle_cmd_get(ump, type, id, defaultq,
321 1.69 bouyer replies);
322 1.69 bouyer else
323 1.69 bouyer #endif
324 1.69 bouyer #ifdef QUOTA2
325 1.69 bouyer if (ump->um_flags & UFS_QUOTA2) {
326 1.69 bouyer error = quota2_handle_cmd_get(ump, type, id, defaultq,
327 1.69 bouyer replies);
328 1.69 bouyer } else
329 1.69 bouyer #endif
330 1.69 bouyer panic("quota_handle_cmd_get: no support ?");
331 1.69 bouyer
332 1.69 bouyer if (error == ENOENT)
333 1.69 bouyer continue;
334 1.69 bouyer if (error != 0)
335 1.69 bouyer goto err;
336 1.69 bouyer }
337 1.69 bouyer prop_object_iterator_release(iter);
338 1.69 bouyer if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
339 1.69 bouyer error = ENOMEM;
340 1.69 bouyer } else {
341 1.69 bouyer error = 0;
342 1.1 mycroft }
343 1.69 bouyer return error;
344 1.69 bouyer err:
345 1.69 bouyer prop_object_iterator_release(iter);
346 1.69 bouyer prop_object_release(replies);
347 1.69 bouyer return error;
348 1.69 bouyer }
349 1.69 bouyer
350 1.69 bouyer static int
351 1.69 bouyer quota_handle_cmd_set(struct mount *mp, struct lwp *l,
352 1.69 bouyer prop_dictionary_t cmddict, int type, prop_array_t datas)
353 1.69 bouyer {
354 1.69 bouyer prop_array_t replies;
355 1.69 bouyer prop_object_iterator_t iter;
356 1.69 bouyer prop_dictionary_t data;
357 1.69 bouyer uint32_t id;
358 1.11 fvdl struct ufsmount *ump = VFSTOUFS(mp);
359 1.69 bouyer int error, defaultq = 0;
360 1.69 bouyer const char *idstr;
361 1.1 mycroft
362 1.69 bouyer if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
363 1.69 bouyer return EOPNOTSUPP;
364 1.69 bouyer
365 1.69 bouyer replies = prop_array_create();
366 1.69 bouyer if (replies == NULL)
367 1.69 bouyer return ENOMEM;
368 1.61 ad
369 1.69 bouyer iter = prop_array_iterator(datas);
370 1.69 bouyer if (iter == NULL) {
371 1.69 bouyer prop_object_release(replies);
372 1.69 bouyer return ENOMEM;
373 1.68 dholland }
374 1.69 bouyer while ((data = prop_object_iterator_next(iter)) != NULL) {
375 1.69 bouyer if (!prop_dictionary_get_uint32(data, "id", &id)) {
376 1.69 bouyer if (!prop_dictionary_get_cstring_nocopy(data, "id",
377 1.69 bouyer &idstr))
378 1.69 bouyer continue;
379 1.69 bouyer if (strcmp(idstr, "default"))
380 1.69 bouyer continue;
381 1.69 bouyer id = 0;
382 1.69 bouyer defaultq = 1;
383 1.69 bouyer } else {
384 1.69 bouyer defaultq = 0;
385 1.69 bouyer }
386 1.69 bouyer error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
387 1.69 bouyer KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(id), NULL);
388 1.69 bouyer if (error != 0)
389 1.69 bouyer goto err;
390 1.69 bouyer #ifdef QUOTA
391 1.69 bouyer if (ump->um_flags & UFS_QUOTA)
392 1.69 bouyer error = quota1_handle_cmd_set(ump, type, id, defaultq,
393 1.69 bouyer data);
394 1.69 bouyer else
395 1.69 bouyer #endif
396 1.69 bouyer #ifdef QUOTA2
397 1.69 bouyer if (ump->um_flags & UFS_QUOTA2) {
398 1.69 bouyer error = quota2_handle_cmd_set(ump, type, id, defaultq,
399 1.69 bouyer data);
400 1.69 bouyer } else
401 1.69 bouyer #endif
402 1.69 bouyer panic("quota_handle_cmd_get: no support ?");
403 1.69 bouyer
404 1.69 bouyer if (error && error != ENOENT)
405 1.69 bouyer goto err;
406 1.1 mycroft }
407 1.69 bouyer prop_object_iterator_release(iter);
408 1.69 bouyer if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
409 1.53 ad error = ENOMEM;
410 1.69 bouyer } else {
411 1.69 bouyer error = 0;
412 1.1 mycroft }
413 1.69 bouyer return error;
414 1.69 bouyer err:
415 1.69 bouyer prop_object_iterator_release(iter);
416 1.69 bouyer prop_object_release(replies);
417 1.69 bouyer return error;
418 1.69 bouyer }
419 1.69 bouyer
420 1.69 bouyer static int
421 1.69 bouyer quota_handle_cmd_clear(struct mount *mp, struct lwp *l,
422 1.69 bouyer prop_dictionary_t cmddict, int type, prop_array_t datas)
423 1.69 bouyer {
424 1.69 bouyer prop_array_t replies;
425 1.69 bouyer prop_object_iterator_t iter;
426 1.69 bouyer prop_dictionary_t data;
427 1.69 bouyer uint32_t id;
428 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
429 1.69 bouyer int error, defaultq = 0;
430 1.69 bouyer const char *idstr;
431 1.33 perry
432 1.69 bouyer if ((ump->um_flags & UFS_QUOTA2) == 0)
433 1.69 bouyer return EOPNOTSUPP;
434 1.69 bouyer
435 1.69 bouyer replies = prop_array_create();
436 1.69 bouyer if (replies == NULL)
437 1.53 ad return ENOMEM;
438 1.53 ad
439 1.69 bouyer iter = prop_array_iterator(datas);
440 1.69 bouyer if (iter == NULL) {
441 1.69 bouyer prop_object_release(replies);
442 1.69 bouyer return ENOMEM;
443 1.69 bouyer }
444 1.69 bouyer while ((data = prop_object_iterator_next(iter)) != NULL) {
445 1.69 bouyer if (!prop_dictionary_get_uint32(data, "id", &id)) {
446 1.69 bouyer if (!prop_dictionary_get_cstring_nocopy(data, "id",
447 1.69 bouyer &idstr))
448 1.69 bouyer continue;
449 1.69 bouyer if (strcmp(idstr, "default"))
450 1.69 bouyer continue;
451 1.69 bouyer id = 0;
452 1.69 bouyer defaultq = 1;
453 1.69 bouyer } else {
454 1.69 bouyer defaultq = 0;
455 1.69 bouyer }
456 1.69 bouyer error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
457 1.69 bouyer KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(id), NULL);
458 1.69 bouyer if (error != 0)
459 1.69 bouyer goto err;
460 1.69 bouyer #ifdef QUOTA2
461 1.69 bouyer if (ump->um_flags & UFS_QUOTA2) {
462 1.69 bouyer error = quota2_handle_cmd_clear(ump, type, id, defaultq,
463 1.69 bouyer data);
464 1.69 bouyer } else
465 1.69 bouyer #endif
466 1.69 bouyer panic("quota_handle_cmd_get: no support ?");
467 1.69 bouyer
468 1.69 bouyer if (error && error != ENOENT)
469 1.69 bouyer goto err;
470 1.50 hannken }
471 1.69 bouyer prop_object_iterator_release(iter);
472 1.69 bouyer if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
473 1.69 bouyer error = ENOMEM;
474 1.69 bouyer } else {
475 1.69 bouyer error = 0;
476 1.1 mycroft }
477 1.69 bouyer return error;
478 1.69 bouyer err:
479 1.69 bouyer prop_object_iterator_release(iter);
480 1.69 bouyer prop_object_release(replies);
481 1.69 bouyer return error;
482 1.1 mycroft }
483 1.1 mycroft
484 1.69 bouyer static int
485 1.69 bouyer quota_handle_cmd_getall(struct mount *mp, struct lwp *l,
486 1.69 bouyer prop_dictionary_t cmddict, int type, prop_array_t datas)
487 1.1 mycroft {
488 1.69 bouyer prop_array_t replies;
489 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
490 1.1 mycroft int error;
491 1.1 mycroft
492 1.69 bouyer if ((ump->um_flags & UFS_QUOTA2) == 0)
493 1.69 bouyer return EOPNOTSUPP;
494 1.69 bouyer
495 1.69 bouyer error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
496 1.69 bouyer KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
497 1.8 christos if (error)
498 1.69 bouyer return error;
499 1.69 bouyer
500 1.69 bouyer replies = prop_array_create();
501 1.69 bouyer if (replies == NULL)
502 1.69 bouyer return ENOMEM;
503 1.69 bouyer
504 1.69 bouyer #ifdef QUOTA2
505 1.69 bouyer if (ump->um_flags & UFS_QUOTA2) {
506 1.69 bouyer error = quota2_handle_cmd_getall(ump, type, replies);
507 1.69 bouyer } else
508 1.69 bouyer #endif
509 1.69 bouyer panic("quota_handle_cmd_getall: no support ?");
510 1.69 bouyer if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
511 1.69 bouyer error = ENOMEM;
512 1.69 bouyer } else {
513 1.69 bouyer error = 0;
514 1.69 bouyer }
515 1.69 bouyer return error;
516 1.1 mycroft }
517 1.1 mycroft
518 1.69 bouyer static int
519 1.69 bouyer quota_handle_cmd_quotaon(struct mount *mp, struct lwp *l,
520 1.69 bouyer prop_dictionary_t cmddict, int type, prop_array_t datas)
521 1.1 mycroft {
522 1.69 bouyer prop_dictionary_t data;
523 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
524 1.1 mycroft int error;
525 1.69 bouyer const char *qfile;
526 1.69 bouyer
527 1.69 bouyer if ((ump->um_flags & UFS_QUOTA2) != 0)
528 1.69 bouyer return EBUSY;
529 1.69 bouyer
530 1.69 bouyer if (prop_array_count(datas) != 1)
531 1.69 bouyer return EINVAL;
532 1.1 mycroft
533 1.69 bouyer data = prop_array_get(datas, 0);
534 1.69 bouyer if (data == NULL)
535 1.69 bouyer return ENOMEM;
536 1.69 bouyer if (!prop_dictionary_get_cstring_nocopy(data, "quotafile",
537 1.69 bouyer &qfile))
538 1.69 bouyer return EINVAL;
539 1.69 bouyer
540 1.69 bouyer error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
541 1.69 bouyer KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
542 1.69 bouyer if (error != 0) {
543 1.69 bouyer return error;
544 1.69 bouyer }
545 1.69 bouyer #ifdef QUOTA
546 1.69 bouyer error = quota1_handle_cmd_quotaon(l, ump, type, qfile);
547 1.69 bouyer #else
548 1.69 bouyer error = EOPNOTSUPP;
549 1.69 bouyer #endif
550 1.69 bouyer
551 1.69 bouyer return error;
552 1.1 mycroft }
553 1.1 mycroft
554 1.69 bouyer static int
555 1.69 bouyer quota_handle_cmd_quotaoff(struct mount *mp, struct lwp *l,
556 1.69 bouyer prop_dictionary_t cmddict, int type, prop_array_t datas)
557 1.1 mycroft {
558 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
559 1.69 bouyer int error;
560 1.1 mycroft
561 1.69 bouyer if ((ump->um_flags & UFS_QUOTA2) != 0)
562 1.69 bouyer return EOPNOTSUPP;
563 1.69 bouyer
564 1.69 bouyer if (prop_array_count(datas) != 0)
565 1.69 bouyer return EINVAL;
566 1.69 bouyer
567 1.69 bouyer error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
568 1.69 bouyer KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
569 1.69 bouyer if (error != 0) {
570 1.69 bouyer return error;
571 1.1 mycroft }
572 1.69 bouyer #ifdef QUOTA
573 1.69 bouyer error = quota1_handle_cmd_quotaoff(l, ump, type);
574 1.69 bouyer #else
575 1.69 bouyer error = EOPNOTSUPP;
576 1.69 bouyer #endif
577 1.69 bouyer
578 1.69 bouyer return error;
579 1.1 mycroft }
580 1.1 mycroft
581 1.1 mycroft /*
582 1.1 mycroft * Initialize the quota system.
583 1.1 mycroft */
584 1.1 mycroft void
585 1.35 thorpej dqinit(void)
586 1.1 mycroft {
587 1.47 pooka
588 1.50 hannken mutex_init(&dqlock, MUTEX_DEFAULT, IPL_NONE);
589 1.50 hannken cv_init(&dqcv, "quota");
590 1.60 ad dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
591 1.55 ad dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "ufsdq",
592 1.55 ad NULL, IPL_NONE, NULL, NULL, NULL);
593 1.14 jdolecek }
594 1.14 jdolecek
595 1.21 chs void
596 1.35 thorpej dqreinit(void)
597 1.21 chs {
598 1.21 chs struct dquot *dq;
599 1.21 chs struct dqhashhead *oldhash, *hash;
600 1.21 chs struct vnode *dqvp;
601 1.21 chs u_long oldmask, mask, hashval;
602 1.21 chs int i;
603 1.21 chs
604 1.60 ad hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
605 1.50 hannken mutex_enter(&dqlock);
606 1.21 chs oldhash = dqhashtbl;
607 1.21 chs oldmask = dqhash;
608 1.21 chs dqhashtbl = hash;
609 1.21 chs dqhash = mask;
610 1.21 chs for (i = 0; i <= oldmask; i++) {
611 1.21 chs while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
612 1.21 chs dqvp = dq->dq_ump->um_quotas[dq->dq_type];
613 1.21 chs LIST_REMOVE(dq, dq_hash);
614 1.21 chs hashval = DQHASH(dqvp, dq->dq_id);
615 1.21 chs LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
616 1.21 chs }
617 1.21 chs }
618 1.50 hannken mutex_exit(&dqlock);
619 1.60 ad hashdone(oldhash, HASH_LIST, oldmask);
620 1.21 chs }
621 1.21 chs
622 1.14 jdolecek /*
623 1.14 jdolecek * Free resources held by quota system.
624 1.14 jdolecek */
625 1.14 jdolecek void
626 1.35 thorpej dqdone(void)
627 1.14 jdolecek {
628 1.47 pooka
629 1.55 ad pool_cache_destroy(dquot_cache);
630 1.60 ad hashdone(dqhashtbl, HASH_LIST, dqhash);
631 1.50 hannken cv_destroy(&dqcv);
632 1.50 hannken mutex_destroy(&dqlock);
633 1.1 mycroft }
634 1.1 mycroft
635 1.1 mycroft /*
636 1.69 bouyer * Set up the quotas for an inode.
637 1.69 bouyer *
638 1.69 bouyer * This routine completely defines the semantics of quotas.
639 1.69 bouyer * If other criterion want to be used to establish quotas, the
640 1.69 bouyer * MAXQUOTAS value in quotas.h should be increased, and the
641 1.69 bouyer * additional dquots set up here.
642 1.69 bouyer */
643 1.69 bouyer int
644 1.69 bouyer getinoquota(struct inode *ip)
645 1.69 bouyer {
646 1.69 bouyer struct ufsmount *ump = ip->i_ump;
647 1.69 bouyer struct vnode *vp = ITOV(ip);
648 1.69 bouyer int i, error;
649 1.69 bouyer u_int32_t ino_ids[MAXQUOTAS];
650 1.69 bouyer
651 1.69 bouyer /*
652 1.69 bouyer * To avoid deadlocks never update quotas for quota files
653 1.69 bouyer * on the same file system
654 1.69 bouyer */
655 1.69 bouyer for (i = 0; i < MAXQUOTAS; i++)
656 1.69 bouyer if (vp == ump->um_quotas[i])
657 1.69 bouyer return 0;
658 1.69 bouyer
659 1.69 bouyer ino_ids[USRQUOTA] = ip->i_uid;
660 1.69 bouyer ino_ids[GRPQUOTA] = ip->i_gid;
661 1.69 bouyer for (i = 0; i < MAXQUOTAS; i++) {
662 1.69 bouyer /*
663 1.69 bouyer * If the file id changed the quota needs update.
664 1.69 bouyer */
665 1.69 bouyer if (ip->i_dquot[i] != NODQUOT &&
666 1.69 bouyer ip->i_dquot[i]->dq_id != ino_ids[i]) {
667 1.69 bouyer dqrele(ITOV(ip), ip->i_dquot[i]);
668 1.69 bouyer ip->i_dquot[i] = NODQUOT;
669 1.69 bouyer }
670 1.69 bouyer /*
671 1.69 bouyer * Set up the quota based on file id.
672 1.69 bouyer * ENODEV means that quotas are not enabled.
673 1.69 bouyer */
674 1.69 bouyer if (ip->i_dquot[i] == NODQUOT &&
675 1.69 bouyer (error = dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
676 1.69 bouyer error != ENODEV)
677 1.69 bouyer return (error);
678 1.69 bouyer }
679 1.69 bouyer return 0;
680 1.69 bouyer }
681 1.69 bouyer
682 1.69 bouyer /*
683 1.1 mycroft * Obtain a dquot structure for the specified identifier and quota file
684 1.1 mycroft * reading the information from the file if necessary.
685 1.1 mycroft */
686 1.69 bouyer int
687 1.35 thorpej dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
688 1.35 thorpej struct dquot **dqp)
689 1.1 mycroft {
690 1.50 hannken struct dquot *dq, *ndq;
691 1.21 chs struct dqhashhead *dqh;
692 1.11 fvdl struct vnode *dqvp;
693 1.69 bouyer int error = 0; /* XXX gcc */
694 1.1 mycroft
695 1.50 hannken /* Lock to see an up to date value for QTF_CLOSING. */
696 1.50 hannken mutex_enter(&dqlock);
697 1.69 bouyer if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0) {
698 1.50 hannken mutex_exit(&dqlock);
699 1.1 mycroft *dqp = NODQUOT;
700 1.69 bouyer return (ENODEV);
701 1.69 bouyer }
702 1.69 bouyer dqvp = ump->um_quotas[type];
703 1.69 bouyer #ifdef QUOTA
704 1.69 bouyer if (ump->um_flags & UFS_QUOTA) {
705 1.69 bouyer if (dqvp == NULLVP || (ump->umq1_qflags[type] & QTF_CLOSING)) {
706 1.69 bouyer mutex_exit(&dqlock);
707 1.69 bouyer *dqp = NODQUOT;
708 1.69 bouyer return (ENODEV);
709 1.69 bouyer }
710 1.1 mycroft }
711 1.69 bouyer #endif
712 1.69 bouyer #ifdef QUOTA2
713 1.69 bouyer if (ump->um_flags & UFS_QUOTA2) {
714 1.69 bouyer if (dqvp == NULLVP) {
715 1.69 bouyer mutex_exit(&dqlock);
716 1.69 bouyer *dqp = NODQUOT;
717 1.69 bouyer return (ENODEV);
718 1.69 bouyer }
719 1.69 bouyer }
720 1.69 bouyer #endif
721 1.50 hannken KASSERT(dqvp != vp);
722 1.1 mycroft /*
723 1.1 mycroft * Check the cache first.
724 1.1 mycroft */
725 1.21 chs dqh = &dqhashtbl[DQHASH(dqvp, id)];
726 1.21 chs LIST_FOREACH(dq, dqh, dq_hash) {
727 1.1 mycroft if (dq->dq_id != id ||
728 1.1 mycroft dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
729 1.1 mycroft continue;
730 1.50 hannken KASSERT(dq->dq_cnt > 0);
731 1.37 chs dqref(dq);
732 1.50 hannken mutex_exit(&dqlock);
733 1.1 mycroft *dqp = dq;
734 1.1 mycroft return (0);
735 1.1 mycroft }
736 1.1 mycroft /*
737 1.1 mycroft * Not in cache, allocate a new one.
738 1.1 mycroft */
739 1.50 hannken mutex_exit(&dqlock);
740 1.55 ad ndq = pool_cache_get(dquot_cache, PR_WAITOK);
741 1.1 mycroft /*
742 1.1 mycroft * Initialize the contents of the dquot structure.
743 1.1 mycroft */
744 1.50 hannken memset((char *)ndq, 0, sizeof *ndq);
745 1.50 hannken ndq->dq_flags = 0;
746 1.50 hannken ndq->dq_id = id;
747 1.50 hannken ndq->dq_ump = ump;
748 1.50 hannken ndq->dq_type = type;
749 1.50 hannken mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
750 1.50 hannken mutex_enter(&dqlock);
751 1.49 hannken dqh = &dqhashtbl[DQHASH(dqvp, id)];
752 1.50 hannken LIST_FOREACH(dq, dqh, dq_hash) {
753 1.50 hannken if (dq->dq_id != id ||
754 1.50 hannken dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
755 1.50 hannken continue;
756 1.50 hannken /*
757 1.50 hannken * Another thread beat us allocating this dquot.
758 1.50 hannken */
759 1.50 hannken KASSERT(dq->dq_cnt > 0);
760 1.50 hannken dqref(dq);
761 1.50 hannken mutex_exit(&dqlock);
762 1.64 bouyer mutex_destroy(&ndq->dq_interlock);
763 1.55 ad pool_cache_put(dquot_cache, ndq);
764 1.50 hannken *dqp = dq;
765 1.50 hannken return 0;
766 1.50 hannken }
767 1.50 hannken dq = ndq;
768 1.5 mycroft LIST_INSERT_HEAD(dqh, dq, dq_hash);
769 1.37 chs dqref(dq);
770 1.50 hannken mutex_enter(&dq->dq_interlock);
771 1.50 hannken mutex_exit(&dqlock);
772 1.69 bouyer #ifdef QUOTA
773 1.69 bouyer if (ump->um_flags & UFS_QUOTA)
774 1.69 bouyer error = dq1get(dqvp, id, ump, type, dq);
775 1.69 bouyer #endif
776 1.69 bouyer #ifdef QUOTA2
777 1.69 bouyer if (ump->um_flags & UFS_QUOTA2)
778 1.69 bouyer error = dq2get(dqvp, id, ump, type, dq);
779 1.69 bouyer #endif
780 1.1 mycroft /*
781 1.1 mycroft * I/O error in reading quota file, release
782 1.1 mycroft * quota structure and reflect problem to caller.
783 1.1 mycroft */
784 1.1 mycroft if (error) {
785 1.50 hannken mutex_enter(&dqlock);
786 1.5 mycroft LIST_REMOVE(dq, dq_hash);
787 1.50 hannken mutex_exit(&dqlock);
788 1.49 hannken mutex_exit(&dq->dq_interlock);
789 1.1 mycroft dqrele(vp, dq);
790 1.1 mycroft *dqp = NODQUOT;
791 1.1 mycroft return (error);
792 1.1 mycroft }
793 1.49 hannken mutex_exit(&dq->dq_interlock);
794 1.1 mycroft *dqp = dq;
795 1.1 mycroft return (0);
796 1.1 mycroft }
797 1.1 mycroft
798 1.1 mycroft /*
799 1.1 mycroft * Obtain a reference to a dquot.
800 1.1 mycroft */
801 1.69 bouyer void
802 1.35 thorpej dqref(struct dquot *dq)
803 1.1 mycroft {
804 1.1 mycroft
805 1.50 hannken KASSERT(mutex_owned(&dqlock));
806 1.1 mycroft dq->dq_cnt++;
807 1.46 hannken KASSERT(dq->dq_cnt > 0);
808 1.1 mycroft }
809 1.1 mycroft
810 1.1 mycroft /*
811 1.1 mycroft * Release a reference to a dquot.
812 1.1 mycroft */
813 1.69 bouyer void
814 1.35 thorpej dqrele(struct vnode *vp, struct dquot *dq)
815 1.1 mycroft {
816 1.1 mycroft
817 1.1 mycroft if (dq == NODQUOT)
818 1.1 mycroft return;
819 1.50 hannken mutex_enter(&dq->dq_interlock);
820 1.50 hannken for (;;) {
821 1.50 hannken mutex_enter(&dqlock);
822 1.50 hannken if (dq->dq_cnt > 1) {
823 1.50 hannken dq->dq_cnt--;
824 1.50 hannken mutex_exit(&dqlock);
825 1.50 hannken mutex_exit(&dq->dq_interlock);
826 1.50 hannken return;
827 1.50 hannken }
828 1.50 hannken if ((dq->dq_flags & DQ_MOD) == 0)
829 1.50 hannken break;
830 1.50 hannken mutex_exit(&dqlock);
831 1.69 bouyer #ifdef QUOTA
832 1.69 bouyer if (dq->dq_ump->um_flags & UFS_QUOTA)
833 1.69 bouyer (void) dq1sync(vp, dq);
834 1.69 bouyer #endif
835 1.69 bouyer #ifdef QUOTA2
836 1.69 bouyer if (dq->dq_ump->um_flags & UFS_QUOTA2)
837 1.69 bouyer (void) dq2sync(vp, dq);
838 1.69 bouyer #endif
839 1.1 mycroft }
840 1.50 hannken KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
841 1.50 hannken LIST_REMOVE(dq, dq_hash);
842 1.50 hannken mutex_exit(&dqlock);
843 1.50 hannken mutex_exit(&dq->dq_interlock);
844 1.50 hannken mutex_destroy(&dq->dq_interlock);
845 1.55 ad pool_cache_put(dquot_cache, dq);
846 1.1 mycroft }
847 1.1 mycroft
848 1.69 bouyer int
849 1.69 bouyer qsync(struct mount *mp)
850 1.1 mycroft {
851 1.69 bouyer struct ufsmount *ump = VFSTOUFS(mp);
852 1.69 bouyer #ifdef QUOTA
853 1.69 bouyer if (ump->um_flags & UFS_QUOTA)
854 1.69 bouyer return q1sync(mp);
855 1.69 bouyer #endif
856 1.69 bouyer #ifdef QUOTA2
857 1.69 bouyer if (ump->um_flags & UFS_QUOTA2)
858 1.69 bouyer return q2sync(mp);
859 1.69 bouyer #endif
860 1.69 bouyer return 0;
861 1.1 mycroft }
862 1.1 mycroft
863 1.50 hannken #ifdef DIAGNOSTIC
864 1.1 mycroft /*
865 1.50 hannken * Check the hash chains for stray dquot's.
866 1.1 mycroft */
867 1.69 bouyer void
868 1.35 thorpej dqflush(struct vnode *vp)
869 1.1 mycroft {
870 1.50 hannken struct dquot *dq;
871 1.50 hannken int i;
872 1.1 mycroft
873 1.50 hannken mutex_enter(&dqlock);
874 1.50 hannken for (i = 0; i <= dqhash; i++)
875 1.50 hannken LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
876 1.50 hannken KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
877 1.50 hannken mutex_exit(&dqlock);
878 1.1 mycroft }
879 1.50 hannken #endif
880