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