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