ufs_quota.c revision 1.27 1 /* $NetBSD: ufs_quota.c,v 1.27 2003/06/29 22:32:48 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1990, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.27 2003/06/29 22:32:48 fvdl Exp $");
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/namei.h>
48 #include <sys/malloc.h>
49 #include <sys/file.h>
50 #include <sys/proc.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/ufs_extern.h>
58
59 /*
60 * Quota name to error message mapping.
61 */
62 static char *quotatypes[] = INITQFNAMES;
63
64 /*
65 * Set up the quotas for an inode.
66 *
67 * This routine completely defines the semantics of quotas.
68 * If other criterion want to be used to establish quotas, the
69 * MAXQUOTAS value in quotas.h should be increased, and the
70 * additional dquots set up here.
71 */
72 int
73 getinoquota(ip)
74 struct inode *ip;
75 {
76 struct ufsmount *ump;
77 struct vnode *vp = ITOV(ip);
78 int error;
79
80 ump = VFSTOUFS(vp->v_mount);
81 /*
82 * Set up the user quota based on file uid.
83 * EINVAL means that quotas are not enabled.
84 */
85 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
86 (error =
87 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
88 error != EINVAL)
89 return (error);
90 /*
91 * Set up the group quota based on file gid.
92 * EINVAL means that quotas are not enabled.
93 */
94 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
95 (error =
96 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
97 error != EINVAL)
98 return (error);
99 return (0);
100 }
101
102 /*
103 * Update disk usage, and take corrective action.
104 */
105 int
106 chkdq(ip, change, cred, flags)
107 struct inode *ip;
108 int64_t change;
109 struct ucred *cred;
110 int flags;
111 {
112 struct dquot *dq;
113 int i;
114 int ncurblocks, error;
115
116 #ifdef DIAGNOSTIC
117 if ((flags & CHOWN) == 0)
118 chkdquot(ip);
119 #endif
120 if (change == 0)
121 return (0);
122 if (change < 0) {
123 for (i = 0; i < MAXQUOTAS; i++) {
124 if ((dq = ip->i_dquot[i]) == NODQUOT)
125 continue;
126 while (dq->dq_flags & DQ_LOCK) {
127 dq->dq_flags |= DQ_WANT;
128 (void) tsleep(dq, PINOD+1, "chkdq", 0);
129 }
130 ncurblocks = dq->dq_curblocks + change;
131 if (ncurblocks >= 0)
132 dq->dq_curblocks = ncurblocks;
133 else
134 dq->dq_curblocks = 0;
135 dq->dq_flags &= ~DQ_BLKS;
136 dq->dq_flags |= DQ_MOD;
137 }
138 return (0);
139 }
140 if ((flags & FORCE) == 0 &&
141 (cred != NOCRED && cred->cr_uid != 0)) {
142 for (i = 0; i < MAXQUOTAS; i++) {
143 if ((dq = ip->i_dquot[i]) == NODQUOT)
144 continue;
145 if ((error = chkdqchg(ip, change, cred, i)) != 0)
146 return (error);
147 }
148 }
149 for (i = 0; i < MAXQUOTAS; i++) {
150 if ((dq = ip->i_dquot[i]) == NODQUOT)
151 continue;
152 while (dq->dq_flags & DQ_LOCK) {
153 dq->dq_flags |= DQ_WANT;
154 (void) tsleep(dq, PINOD+1, "chkdq", 0);
155 }
156 dq->dq_curblocks += change;
157 dq->dq_flags |= DQ_MOD;
158 }
159 return (0);
160 }
161
162 /*
163 * Check for a valid change to a users allocation.
164 * Issue an error message if appropriate.
165 */
166 int
167 chkdqchg(ip, change, cred, type)
168 struct inode *ip;
169 int64_t change;
170 struct ucred *cred;
171 int type;
172 {
173 struct dquot *dq = ip->i_dquot[type];
174 long ncurblocks = dq->dq_curblocks + change;
175
176 /*
177 * If user would exceed their hard limit, disallow space allocation.
178 */
179 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
180 if ((dq->dq_flags & DQ_BLKS) == 0 &&
181 ip->i_uid == cred->cr_uid) {
182 uprintf("\n%s: write failed, %s disk limit reached\n",
183 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
184 quotatypes[type]);
185 dq->dq_flags |= DQ_BLKS;
186 }
187 return (EDQUOT);
188 }
189 /*
190 * If user is over their soft limit for too long, disallow space
191 * allocation. Reset time limit as they cross their soft limit.
192 */
193 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
194 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
195 dq->dq_btime = time.tv_sec +
196 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
197 if (ip->i_uid == cred->cr_uid)
198 uprintf("\n%s: warning, %s %s\n",
199 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
200 quotatypes[type], "disk quota exceeded");
201 return (0);
202 }
203 if (time.tv_sec > dq->dq_btime) {
204 if ((dq->dq_flags & DQ_BLKS) == 0 &&
205 ip->i_uid == cred->cr_uid) {
206 uprintf("\n%s: write failed, %s %s\n",
207 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
208 quotatypes[type],
209 "disk quota exceeded for too long");
210 dq->dq_flags |= DQ_BLKS;
211 }
212 return (EDQUOT);
213 }
214 }
215 return (0);
216 }
217
218 /*
219 * Check the inode limit, applying corrective action.
220 */
221 int
222 chkiq(ip, change, cred, flags)
223 struct inode *ip;
224 int32_t change;
225 struct ucred *cred;
226 int flags;
227 {
228 struct dquot *dq;
229 int i;
230 int ncurinodes, error;
231
232 #ifdef DIAGNOSTIC
233 if ((flags & CHOWN) == 0)
234 chkdquot(ip);
235 #endif
236 if (change == 0)
237 return (0);
238 if (change < 0) {
239 for (i = 0; i < MAXQUOTAS; i++) {
240 if ((dq = ip->i_dquot[i]) == NODQUOT)
241 continue;
242 while (dq->dq_flags & DQ_LOCK) {
243 dq->dq_flags |= DQ_WANT;
244 (void) tsleep(dq, PINOD+1, "chkiq", 0);
245 }
246 ncurinodes = dq->dq_curinodes + change;
247 if (ncurinodes >= 0)
248 dq->dq_curinodes = ncurinodes;
249 else
250 dq->dq_curinodes = 0;
251 dq->dq_flags &= ~DQ_INODS;
252 dq->dq_flags |= DQ_MOD;
253 }
254 return (0);
255 }
256 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
257 for (i = 0; i < MAXQUOTAS; i++) {
258 if ((dq = ip->i_dquot[i]) == NODQUOT)
259 continue;
260 if ((error = chkiqchg(ip, change, cred, i)) != 0)
261 return (error);
262 }
263 }
264 for (i = 0; i < MAXQUOTAS; i++) {
265 if ((dq = ip->i_dquot[i]) == NODQUOT)
266 continue;
267 while (dq->dq_flags & DQ_LOCK) {
268 dq->dq_flags |= DQ_WANT;
269 (void) tsleep(dq, PINOD+1, "chkiq", 0);
270 }
271 dq->dq_curinodes += change;
272 dq->dq_flags |= DQ_MOD;
273 }
274 return (0);
275 }
276
277 /*
278 * Check for a valid change to a users allocation.
279 * Issue an error message if appropriate.
280 */
281 int
282 chkiqchg(ip, change, cred, type)
283 struct inode *ip;
284 int32_t change;
285 struct ucred *cred;
286 int type;
287 {
288 struct dquot *dq = ip->i_dquot[type];
289 long ncurinodes = dq->dq_curinodes + change;
290
291 /*
292 * If user would exceed their hard limit, disallow inode allocation.
293 */
294 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
295 if ((dq->dq_flags & DQ_INODS) == 0 &&
296 ip->i_uid == cred->cr_uid) {
297 uprintf("\n%s: write failed, %s inode limit reached\n",
298 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
299 quotatypes[type]);
300 dq->dq_flags |= DQ_INODS;
301 }
302 return (EDQUOT);
303 }
304 /*
305 * If user is over their soft limit for too long, disallow inode
306 * allocation. Reset time limit as they cross their soft limit.
307 */
308 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
309 if (dq->dq_curinodes < dq->dq_isoftlimit) {
310 dq->dq_itime = time.tv_sec +
311 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
312 if (ip->i_uid == cred->cr_uid)
313 uprintf("\n%s: warning, %s %s\n",
314 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
315 quotatypes[type], "inode quota exceeded");
316 return (0);
317 }
318 if (time.tv_sec > dq->dq_itime) {
319 if ((dq->dq_flags & DQ_INODS) == 0 &&
320 ip->i_uid == cred->cr_uid) {
321 uprintf("\n%s: write failed, %s %s\n",
322 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
323 quotatypes[type],
324 "inode quota exceeded for too long");
325 dq->dq_flags |= DQ_INODS;
326 }
327 return (EDQUOT);
328 }
329 }
330 return (0);
331 }
332
333 #ifdef DIAGNOSTIC
334 /*
335 * On filesystems with quotas enabled, it is an error for a file to change
336 * size and not to have a dquot structure associated with it.
337 */
338 void
339 chkdquot(ip)
340 struct inode *ip;
341 {
342 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
343 int i;
344
345 for (i = 0; i < MAXQUOTAS; i++) {
346 if (ump->um_quotas[i] == NULLVP ||
347 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
348 continue;
349 if (ip->i_dquot[i] == NODQUOT) {
350 vprint("chkdquot: missing dquot", ITOV(ip));
351 panic("missing dquot");
352 }
353 }
354 }
355 #endif
356
357 /*
358 * Code to process quotactl commands.
359 */
360
361 /*
362 * Q_QUOTAON - set up a quota file for a particular file system.
363 */
364 int
365 quotaon(p, mp, type, fname)
366 struct proc *p;
367 struct mount *mp;
368 int type;
369 caddr_t fname;
370 {
371 struct ufsmount *ump = VFSTOUFS(mp);
372 struct vnode *vp, **vpp;
373 struct vnode *nextvp;
374 struct dquot *dq;
375 int error;
376 struct nameidata nd;
377
378 vpp = &ump->um_quotas[type];
379 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, p);
380 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
381 return (error);
382 vp = nd.ni_vp;
383 VOP_UNLOCK(vp, 0);
384 if (vp->v_type != VREG) {
385 (void) vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
386 return (EACCES);
387 }
388 if (*vpp != vp)
389 quotaoff(p, mp, type);
390 ump->um_qflags[type] |= QTF_OPENING;
391 mp->mnt_flag |= MNT_QUOTA;
392 vp->v_flag |= VSYSTEM;
393 *vpp = vp;
394 /*
395 * Save the credential of the process that turned on quotas.
396 * Set up the time limits for this quota.
397 */
398 crhold(p->p_ucred);
399 ump->um_cred[type] = p->p_ucred;
400 ump->um_btime[type] = MAX_DQ_TIME;
401 ump->um_itime[type] = MAX_IQ_TIME;
402 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
403 if (dq->dq_btime > 0)
404 ump->um_btime[type] = dq->dq_btime;
405 if (dq->dq_itime > 0)
406 ump->um_itime[type] = dq->dq_itime;
407 dqrele(NULLVP, dq);
408 }
409 /*
410 * Search vnodes associated with this mount point,
411 * adding references to quota file being opened.
412 * NB: only need to add dquot's for inodes being modified.
413 */
414 again:
415 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nextvp) {
416 nextvp = LIST_NEXT(vp, v_mntvnodes);
417 if (vp->v_type == VNON ||vp->v_writecount == 0)
418 continue;
419 if (vget(vp, LK_EXCLUSIVE))
420 goto again;
421 if ((error = getinoquota(VTOI(vp))) != 0) {
422 vput(vp);
423 break;
424 }
425 vput(vp);
426 if (LIST_NEXT(vp, v_mntvnodes) != nextvp || vp->v_mount != mp)
427 goto again;
428 }
429 ump->um_qflags[type] &= ~QTF_OPENING;
430 if (error)
431 quotaoff(p, mp, type);
432 return (error);
433 }
434
435 /*
436 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
437 */
438 int
439 quotaoff(p, mp, type)
440 struct proc *p;
441 struct mount *mp;
442 int type;
443 {
444 struct vnode *vp;
445 struct vnode *qvp, *nextvp;
446 struct ufsmount *ump = VFSTOUFS(mp);
447 struct dquot *dq;
448 struct inode *ip;
449 int error;
450
451 if ((qvp = ump->um_quotas[type]) == NULLVP)
452 return (0);
453 ump->um_qflags[type] |= QTF_CLOSING;
454 /*
455 * Search vnodes associated with this mount point,
456 * deleting any references to quota file being closed.
457 */
458 again:
459 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nextvp) {
460 nextvp = LIST_NEXT(vp, v_mntvnodes);
461 if (vp->v_type == VNON)
462 continue;
463 if (vget(vp, LK_EXCLUSIVE))
464 goto again;
465 ip = VTOI(vp);
466 dq = ip->i_dquot[type];
467 ip->i_dquot[type] = NODQUOT;
468 dqrele(vp, dq);
469 vput(vp);
470 if (LIST_NEXT(vp, v_mntvnodes) != nextvp || vp->v_mount != mp)
471 goto again;
472 }
473 dqflush(qvp);
474 qvp->v_flag &= ~VSYSTEM;
475 error = vn_close(qvp, FREAD|FWRITE, p->p_ucred, p);
476 ump->um_quotas[type] = NULLVP;
477 crfree(ump->um_cred[type]);
478 ump->um_cred[type] = NOCRED;
479 ump->um_qflags[type] &= ~QTF_CLOSING;
480 for (type = 0; type < MAXQUOTAS; type++)
481 if (ump->um_quotas[type] != NULLVP)
482 break;
483 if (type == MAXQUOTAS)
484 mp->mnt_flag &= ~MNT_QUOTA;
485 return (error);
486 }
487
488 /*
489 * Q_GETQUOTA - return current values in a dqblk structure.
490 */
491 int
492 getquota(mp, id, type, addr)
493 struct mount *mp;
494 u_long id;
495 int type;
496 caddr_t addr;
497 {
498 struct dquot *dq;
499 int error;
500
501 if ((error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq)) != 0)
502 return (error);
503 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
504 dqrele(NULLVP, dq);
505 return (error);
506 }
507
508 /*
509 * Q_SETQUOTA - assign an entire dqblk structure.
510 */
511 int
512 setquota(mp, id, type, addr)
513 struct mount *mp;
514 u_long id;
515 int type;
516 caddr_t addr;
517 {
518 struct dquot *dq;
519 struct dquot *ndq;
520 struct ufsmount *ump = VFSTOUFS(mp);
521 struct dqblk newlim;
522 int error;
523
524 error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
525 if (error)
526 return (error);
527 if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
528 return (error);
529 dq = ndq;
530 while (dq->dq_flags & DQ_LOCK) {
531 dq->dq_flags |= DQ_WANT;
532 (void) tsleep(dq, PINOD+1, "setquota", 0);
533 }
534 /*
535 * Copy all but the current values.
536 * Reset time limit if previously had no soft limit or were
537 * under it, but now have a soft limit and are over it.
538 */
539 newlim.dqb_curblocks = dq->dq_curblocks;
540 newlim.dqb_curinodes = dq->dq_curinodes;
541 if (dq->dq_id != 0) {
542 newlim.dqb_btime = dq->dq_btime;
543 newlim.dqb_itime = dq->dq_itime;
544 }
545 if (newlim.dqb_bsoftlimit &&
546 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
547 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
548 newlim.dqb_btime = time.tv_sec + ump->um_btime[type];
549 if (newlim.dqb_isoftlimit &&
550 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
551 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
552 newlim.dqb_itime = time.tv_sec + ump->um_itime[type];
553 dq->dq_dqb = newlim;
554 if (dq->dq_curblocks < dq->dq_bsoftlimit)
555 dq->dq_flags &= ~DQ_BLKS;
556 if (dq->dq_curinodes < dq->dq_isoftlimit)
557 dq->dq_flags &= ~DQ_INODS;
558 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
559 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
560 dq->dq_flags |= DQ_FAKE;
561 else
562 dq->dq_flags &= ~DQ_FAKE;
563 dq->dq_flags |= DQ_MOD;
564 dqrele(NULLVP, dq);
565 return (0);
566 }
567
568 /*
569 * Q_SETUSE - set current inode and block usage.
570 */
571 int
572 setuse(mp, id, type, addr)
573 struct mount *mp;
574 u_long id;
575 int type;
576 caddr_t addr;
577 {
578 struct dquot *dq;
579 struct ufsmount *ump = VFSTOUFS(mp);
580 struct dquot *ndq;
581 struct dqblk usage;
582 int error;
583
584 error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
585 if (error)
586 return (error);
587 if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
588 return (error);
589 dq = ndq;
590 while (dq->dq_flags & DQ_LOCK) {
591 dq->dq_flags |= DQ_WANT;
592 (void) tsleep(dq, PINOD+1, "setuse", 0);
593 }
594 /*
595 * Reset time limit if have a soft limit and were
596 * previously under it, but are now over it.
597 */
598 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
599 usage.dqb_curblocks >= dq->dq_bsoftlimit)
600 dq->dq_btime = time.tv_sec + ump->um_btime[type];
601 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
602 usage.dqb_curinodes >= dq->dq_isoftlimit)
603 dq->dq_itime = time.tv_sec + ump->um_itime[type];
604 dq->dq_curblocks = usage.dqb_curblocks;
605 dq->dq_curinodes = usage.dqb_curinodes;
606 if (dq->dq_curblocks < dq->dq_bsoftlimit)
607 dq->dq_flags &= ~DQ_BLKS;
608 if (dq->dq_curinodes < dq->dq_isoftlimit)
609 dq->dq_flags &= ~DQ_INODS;
610 dq->dq_flags |= DQ_MOD;
611 dqrele(NULLVP, dq);
612 return (0);
613 }
614
615 /*
616 * Q_SYNC - sync quota files to disk.
617 */
618 int
619 qsync(mp)
620 struct mount *mp;
621 {
622 struct ufsmount *ump = VFSTOUFS(mp);
623 struct vnode *vp, *nextvp;
624 struct dquot *dq;
625 int i, error;
626
627 /*
628 * Check if the mount point has any quotas.
629 * If not, simply return.
630 */
631 for (i = 0; i < MAXQUOTAS; i++)
632 if (ump->um_quotas[i] != NULLVP)
633 break;
634 if (i == MAXQUOTAS)
635 return (0);
636 /*
637 * Search vnodes associated with this mount point,
638 * synchronizing any modified dquot structures.
639 */
640 simple_lock(&mntvnode_slock);
641 again:
642 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nextvp) {
643 if (vp->v_mount != mp)
644 goto again;
645 nextvp = LIST_NEXT(vp, v_mntvnodes);
646 if (vp->v_type == VNON)
647 continue;
648 simple_lock(&vp->v_interlock);
649 simple_unlock(&mntvnode_slock);
650 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
651 if (error) {
652 simple_lock(&mntvnode_slock);
653 if (error == ENOENT)
654 goto again;
655 continue;
656 }
657 for (i = 0; i < MAXQUOTAS; i++) {
658 dq = VTOI(vp)->i_dquot[i];
659 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
660 dqsync(vp, dq);
661 }
662 vput(vp);
663 simple_lock(&mntvnode_slock);
664 if (LIST_NEXT(vp, v_mntvnodes) != nextvp)
665 goto again;
666 }
667 simple_unlock(&mntvnode_slock);
668 return (0);
669 }
670
671 /*
672 * Code pertaining to management of the in-core dquot data structures.
673 */
674 #define DQHASH(dqvp, id) \
675 (((((long)(dqvp)) >> 8) + id) & dqhash)
676 LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
677 u_long dqhash;
678
679 /*
680 * Dquot free list.
681 */
682 #define DQUOTINC 5 /* minimum free dquots desired */
683 TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
684 long numdquot, desireddquot = DQUOTINC;
685
686 MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
687
688 /*
689 * Initialize the quota system.
690 */
691 void
692 dqinit()
693 {
694 dqhashtbl =
695 hashinit(desiredvnodes, HASH_LIST, M_DQUOT, M_WAITOK, &dqhash);
696 TAILQ_INIT(&dqfreelist);
697 }
698
699 void
700 dqreinit()
701 {
702 struct dquot *dq;
703 struct dqhashhead *oldhash, *hash;
704 struct vnode *dqvp;
705 u_long oldmask, mask, hashval;
706 int i;
707
708 hash = hashinit(desiredvnodes, HASH_LIST, M_DQUOT, M_WAITOK, &mask);
709 oldhash = dqhashtbl;
710 oldmask = dqhash;
711 dqhashtbl = hash;
712 dqhash = mask;
713 for (i = 0; i <= oldmask; i++) {
714 while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
715 dqvp = dq->dq_ump->um_quotas[dq->dq_type];
716 LIST_REMOVE(dq, dq_hash);
717 hashval = DQHASH(dqvp, dq->dq_id);
718 LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
719 }
720 }
721 hashdone(oldhash, M_DQUOT);
722 }
723
724 /*
725 * Free resources held by quota system.
726 */
727 void
728 dqdone()
729 {
730 hashdone(dqhashtbl, M_DQUOT);
731 }
732
733 /*
734 * Obtain a dquot structure for the specified identifier and quota file
735 * reading the information from the file if necessary.
736 */
737 int
738 dqget(vp, id, ump, type, dqp)
739 struct vnode *vp;
740 u_long id;
741 struct ufsmount *ump;
742 int type;
743 struct dquot **dqp;
744 {
745 struct dquot *dq;
746 struct dqhashhead *dqh;
747 struct vnode *dqvp;
748 struct iovec aiov;
749 struct uio auio;
750 int error;
751
752 dqvp = ump->um_quotas[type];
753 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
754 *dqp = NODQUOT;
755 return (EINVAL);
756 }
757 /*
758 * Check the cache first.
759 */
760 dqh = &dqhashtbl[DQHASH(dqvp, id)];
761 LIST_FOREACH(dq, dqh, dq_hash) {
762 if (dq->dq_id != id ||
763 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
764 continue;
765 /*
766 * Cache hit with no references. Take
767 * the structure off the free list.
768 */
769 if (dq->dq_cnt == 0)
770 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
771 DQREF(dq);
772 *dqp = dq;
773 return (0);
774 }
775 /*
776 * Not in cache, allocate a new one.
777 */
778 if (dqfreelist.tqh_first == NODQUOT &&
779 numdquot < MAXQUOTAS * desiredvnodes)
780 desireddquot += DQUOTINC;
781 if (numdquot < desireddquot) {
782 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
783 memset((char *)dq, 0, sizeof *dq);
784 numdquot++;
785 } else {
786 if ((dq = dqfreelist.tqh_first) == NULL) {
787 tablefull("dquot",
788 "increase kern.maxvnodes or NVNODE");
789 *dqp = NODQUOT;
790 return (EUSERS);
791 }
792 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
793 panic("free dquot isn't");
794 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
795 LIST_REMOVE(dq, dq_hash);
796 }
797 /*
798 * Initialize the contents of the dquot structure.
799 */
800 if (vp != dqvp)
801 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
802 LIST_INSERT_HEAD(dqh, dq, dq_hash);
803 DQREF(dq);
804 dq->dq_flags = DQ_LOCK;
805 dq->dq_id = id;
806 dq->dq_ump = ump;
807 dq->dq_type = type;
808 auio.uio_iov = &aiov;
809 auio.uio_iovcnt = 1;
810 aiov.iov_base = (caddr_t)&dq->dq_dqb;
811 aiov.iov_len = sizeof (struct dqblk);
812 auio.uio_resid = sizeof (struct dqblk);
813 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
814 auio.uio_segflg = UIO_SYSSPACE;
815 auio.uio_rw = UIO_READ;
816 auio.uio_procp = (struct proc *)0;
817 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
818 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
819 memset((caddr_t)&dq->dq_dqb, 0, sizeof(struct dqblk));
820 if (vp != dqvp)
821 VOP_UNLOCK(dqvp, 0);
822 if (dq->dq_flags & DQ_WANT)
823 wakeup((caddr_t)dq);
824 dq->dq_flags = 0;
825 /*
826 * I/O error in reading quota file, release
827 * quota structure and reflect problem to caller.
828 */
829 if (error) {
830 LIST_REMOVE(dq, dq_hash);
831 dqrele(vp, dq);
832 *dqp = NODQUOT;
833 return (error);
834 }
835 /*
836 * Check for no limit to enforce.
837 * Initialize time values if necessary.
838 */
839 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
840 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
841 dq->dq_flags |= DQ_FAKE;
842 if (dq->dq_id != 0) {
843 if (dq->dq_btime == 0)
844 dq->dq_btime = time.tv_sec + ump->um_btime[type];
845 if (dq->dq_itime == 0)
846 dq->dq_itime = time.tv_sec + ump->um_itime[type];
847 }
848 *dqp = dq;
849 return (0);
850 }
851
852 /*
853 * Obtain a reference to a dquot.
854 */
855 void
856 dqref(dq)
857 struct dquot *dq;
858 {
859
860 dq->dq_cnt++;
861 }
862
863 /*
864 * Release a reference to a dquot.
865 */
866 void
867 dqrele(vp, dq)
868 struct vnode *vp;
869 struct dquot *dq;
870 {
871
872 if (dq == NODQUOT)
873 return;
874 if (dq->dq_cnt > 1) {
875 dq->dq_cnt--;
876 return;
877 }
878 if (dq->dq_flags & DQ_MOD)
879 (void) dqsync(vp, dq);
880 if (--dq->dq_cnt > 0)
881 return;
882 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
883 }
884
885 /*
886 * Update the disk quota in the quota file.
887 */
888 int
889 dqsync(vp, dq)
890 struct vnode *vp;
891 struct dquot *dq;
892 {
893 struct vnode *dqvp;
894 struct iovec aiov;
895 struct uio auio;
896 int error;
897
898 if (dq == NODQUOT)
899 panic("dqsync: dquot");
900 if ((dq->dq_flags & DQ_MOD) == 0)
901 return (0);
902 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
903 panic("dqsync: file");
904 if (vp != dqvp)
905 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
906 while (dq->dq_flags & DQ_LOCK) {
907 dq->dq_flags |= DQ_WANT;
908 (void) tsleep(dq, PINOD+2, "dqsync", 0);
909 if ((dq->dq_flags & DQ_MOD) == 0) {
910 if (vp != dqvp)
911 VOP_UNLOCK(dqvp, 0);
912 return (0);
913 }
914 }
915 dq->dq_flags |= DQ_LOCK;
916 auio.uio_iov = &aiov;
917 auio.uio_iovcnt = 1;
918 aiov.iov_base = (caddr_t)&dq->dq_dqb;
919 aiov.iov_len = sizeof (struct dqblk);
920 auio.uio_resid = sizeof (struct dqblk);
921 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
922 auio.uio_segflg = UIO_SYSSPACE;
923 auio.uio_rw = UIO_WRITE;
924 auio.uio_procp = (struct proc *)0;
925 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
926 if (auio.uio_resid && error == 0)
927 error = EIO;
928 if (dq->dq_flags & DQ_WANT)
929 wakeup((caddr_t)dq);
930 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
931 if (vp != dqvp)
932 VOP_UNLOCK(dqvp, 0);
933 return (error);
934 }
935
936 /*
937 * Flush all entries from the cache for a particular vnode.
938 */
939 void
940 dqflush(vp)
941 struct vnode *vp;
942 {
943 struct dquot *dq, *nextdq;
944 struct dqhashhead *dqh;
945
946 /*
947 * Move all dquot's that used to refer to this quota
948 * file off their hash chains (they will eventually
949 * fall off the head of the free list and be re-used).
950 */
951 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
952 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
953 nextdq = LIST_NEXT(dq, dq_hash);
954 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
955 continue;
956 if (dq->dq_cnt)
957 panic("dqflush: stray dquot");
958 LIST_REMOVE(dq, dq_hash);
959 dq->dq_ump = NULL;
960 }
961 }
962 }
963