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