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