xref: /dragonfly/sys/vfs/ufs/ufs_quota.c (revision 9dbf638f)
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.10 2003/08/20 09:56:34 rob 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(ip)
85 	struct inode *ip;
86 {
87 	struct ufsmount *ump;
88 	struct vnode *vp = ITOV(ip);
89 	int error;
90 
91 	ump = VFSTOUFS(vp->v_mount);
92 	/*
93 	 * Set up the user quota based on file uid.
94 	 * EINVAL means that quotas are not enabled.
95 	 */
96 	if (ip->i_dquot[USRQUOTA] == NODQUOT &&
97 	    (error =
98 		dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
99 	    error != EINVAL)
100 		return (error);
101 	/*
102 	 * Set up the group quota based on file gid.
103 	 * EINVAL means that quotas are not enabled.
104 	 */
105 	if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
106 	    (error =
107 		dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
108 	    error != EINVAL)
109 		return (error);
110 	return (0);
111 }
112 
113 /*
114  * Update disk usage, and take corrective action.
115  */
116 int
117 chkdq(ip, change, cred, flags)
118 	struct inode *ip;
119 	long change;
120 	struct ucred *cred;
121 	int flags;
122 {
123 	struct dquot *dq;
124 	int i;
125 	int ncurblocks, error;
126 
127 #ifdef DIAGNOSTIC
128 	if ((flags & CHOWN) == 0)
129 		chkdquot(ip);
130 #endif
131 	if (change == 0)
132 		return (0);
133 	if (change < 0) {
134 		for (i = 0; i < MAXQUOTAS; i++) {
135 			if ((dq = ip->i_dquot[i]) == NODQUOT)
136 				continue;
137 			while (dq->dq_flags & DQ_LOCK) {
138 				dq->dq_flags |= DQ_WANT;
139 				(void) tsleep((caddr_t)dq, 0, "chkdq1", 0);
140 			}
141 			ncurblocks = dq->dq_curblocks + change;
142 			if (ncurblocks >= 0)
143 				dq->dq_curblocks = ncurblocks;
144 			else
145 				dq->dq_curblocks = 0;
146 			dq->dq_flags &= ~DQ_BLKS;
147 			dq->dq_flags |= DQ_MOD;
148 		}
149 		return (0);
150 	}
151 	if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
152 		for (i = 0; i < MAXQUOTAS; i++) {
153 			if ((dq = ip->i_dquot[i]) == NODQUOT)
154 				continue;
155 			error = chkdqchg(ip, change, cred, i);
156 			if (error)
157 				return (error);
158 		}
159 	}
160 	for (i = 0; i < MAXQUOTAS; i++) {
161 		if ((dq = ip->i_dquot[i]) == NODQUOT)
162 			continue;
163 		while (dq->dq_flags & DQ_LOCK) {
164 			dq->dq_flags |= DQ_WANT;
165 			(void) tsleep((caddr_t)dq, 0, "chkdq2", 0);
166 		}
167 		/* Reset timer when crossing soft limit */
168 		if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
169 		    dq->dq_curblocks < dq->dq_bsoftlimit)
170 			dq->dq_btime = time_second +
171 			    VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
172 		dq->dq_curblocks += change;
173 		dq->dq_flags |= DQ_MOD;
174 	}
175 	return (0);
176 }
177 
178 /*
179  * Check for a valid change to a users allocation.
180  * Issue an error message if appropriate.
181  */
182 static int
183 chkdqchg(ip, change, cred, type)
184 	struct inode *ip;
185 	long change;
186 	struct ucred *cred;
187 	int type;
188 {
189 	struct dquot *dq = ip->i_dquot[type];
190 	long ncurblocks = dq->dq_curblocks + change;
191 
192 	/*
193 	 * If user would exceed their hard limit, disallow space allocation.
194 	 */
195 	if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
196 		if ((dq->dq_flags & DQ_BLKS) == 0 &&
197 		    ip->i_uid == cred->cr_uid) {
198 			uprintf("\n%s: write failed, %s disk limit reached\n",
199 			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
200 			    quotatypes[type]);
201 			dq->dq_flags |= DQ_BLKS;
202 		}
203 		return (EDQUOT);
204 	}
205 	/*
206 	 * If user is over their soft limit for too long, disallow space
207 	 * allocation. Reset time limit as they cross their soft limit.
208 	 */
209 	if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
210 		if (dq->dq_curblocks < dq->dq_bsoftlimit) {
211 			dq->dq_btime = time_second +
212 			    VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
213 			if (ip->i_uid == cred->cr_uid)
214 				uprintf("\n%s: warning, %s %s\n",
215 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
216 				    quotatypes[type], "disk quota exceeded");
217 			return (0);
218 		}
219 		if (time_second > dq->dq_btime) {
220 			if ((dq->dq_flags & DQ_BLKS) == 0 &&
221 			    ip->i_uid == cred->cr_uid) {
222 				uprintf("\n%s: write failed, %s %s\n",
223 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
224 				    quotatypes[type],
225 				    "disk quota exceeded for too long");
226 				dq->dq_flags |= DQ_BLKS;
227 			}
228 			return (EDQUOT);
229 		}
230 	}
231 	return (0);
232 }
233 
234 /*
235  * Check the inode limit, applying corrective action.
236  */
237 int
238 chkiq(ip, change, cred, flags)
239 	struct inode *ip;
240 	long change;
241 	struct ucred *cred;
242 	int flags;
243 {
244 	struct dquot *dq;
245 	int i;
246 	int ncurinodes, error;
247 
248 #ifdef DIAGNOSTIC
249 	if ((flags & CHOWN) == 0)
250 		chkdquot(ip);
251 #endif
252 	if (change == 0)
253 		return (0);
254 	if (change < 0) {
255 		for (i = 0; i < MAXQUOTAS; i++) {
256 			if ((dq = ip->i_dquot[i]) == NODQUOT)
257 				continue;
258 			while (dq->dq_flags & DQ_LOCK) {
259 				dq->dq_flags |= DQ_WANT;
260 				(void) tsleep((caddr_t)dq, 0, "chkiq1", 0);
261 			}
262 			ncurinodes = dq->dq_curinodes + change;
263 			if (ncurinodes >= 0)
264 				dq->dq_curinodes = ncurinodes;
265 			else
266 				dq->dq_curinodes = 0;
267 			dq->dq_flags &= ~DQ_INODS;
268 			dq->dq_flags |= DQ_MOD;
269 		}
270 		return (0);
271 	}
272 	if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
273 		for (i = 0; i < MAXQUOTAS; i++) {
274 			if ((dq = ip->i_dquot[i]) == NODQUOT)
275 				continue;
276 			error = chkiqchg(ip, change, cred, i);
277 			if (error)
278 				return (error);
279 		}
280 	}
281 	for (i = 0; i < MAXQUOTAS; i++) {
282 		if ((dq = ip->i_dquot[i]) == NODQUOT)
283 			continue;
284 		while (dq->dq_flags & DQ_LOCK) {
285 			dq->dq_flags |= DQ_WANT;
286 			(void) tsleep((caddr_t)dq, 0, "chkiq2", 0);
287 		}
288 		/* Reset timer when crossing soft limit */
289 		if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
290 		    dq->dq_curinodes < dq->dq_isoftlimit)
291 			dq->dq_itime = time_second +
292 			    VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
293 		dq->dq_curinodes += change;
294 		dq->dq_flags |= DQ_MOD;
295 	}
296 	return (0);
297 }
298 
299 /*
300  * Check for a valid change to a users allocation.
301  * Issue an error message if appropriate.
302  */
303 static int
304 chkiqchg(ip, change, cred, type)
305 	struct inode *ip;
306 	long change;
307 	struct ucred *cred;
308 	int type;
309 {
310 	struct dquot *dq = ip->i_dquot[type];
311 	long ncurinodes = dq->dq_curinodes + change;
312 
313 	/*
314 	 * If user would exceed their hard limit, disallow inode allocation.
315 	 */
316 	if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
317 		if ((dq->dq_flags & DQ_INODS) == 0 &&
318 		    ip->i_uid == cred->cr_uid) {
319 			uprintf("\n%s: write failed, %s inode limit reached\n",
320 			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
321 			    quotatypes[type]);
322 			dq->dq_flags |= DQ_INODS;
323 		}
324 		return (EDQUOT);
325 	}
326 	/*
327 	 * If user is over their soft limit for too long, disallow inode
328 	 * allocation. Reset time limit as they cross their soft limit.
329 	 */
330 	if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
331 		if (dq->dq_curinodes < dq->dq_isoftlimit) {
332 			dq->dq_itime = time_second +
333 			    VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
334 			if (ip->i_uid == cred->cr_uid)
335 				uprintf("\n%s: warning, %s %s\n",
336 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
337 				    quotatypes[type], "inode quota exceeded");
338 			return (0);
339 		}
340 		if (time_second > dq->dq_itime) {
341 			if ((dq->dq_flags & DQ_INODS) == 0 &&
342 			    ip->i_uid == cred->cr_uid) {
343 				uprintf("\n%s: write failed, %s %s\n",
344 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
345 				    quotatypes[type],
346 				    "inode quota exceeded for too long");
347 				dq->dq_flags |= DQ_INODS;
348 			}
349 			return (EDQUOT);
350 		}
351 	}
352 	return (0);
353 }
354 
355 #ifdef DIAGNOSTIC
356 /*
357  * On filesystems with quotas enabled, it is an error for a file to change
358  * size and not to have a dquot structure associated with it.
359  */
360 static void
361 chkdquot(ip)
362 	struct inode *ip;
363 {
364 	struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
365 	int i;
366 
367 	for (i = 0; i < MAXQUOTAS; i++) {
368 		if (ump->um_quotas[i] == NULLVP ||
369 		    (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
370 			continue;
371 		if (ip->i_dquot[i] == NODQUOT) {
372 			vprint("chkdquot: missing dquot", ITOV(ip));
373 			panic("chkdquot: missing dquot");
374 		}
375 	}
376 }
377 #endif
378 
379 /*
380  * Code to process quotactl commands.
381  */
382 
383 /*
384  * Q_QUOTAON - set up a quota file for a particular file system.
385  */
386 int
387 quotaon(td, mp, type, fname)
388 	struct thread *td;
389 	struct mount *mp;
390 	int type;
391 	caddr_t fname;
392 {
393 	struct ufsmount *ump = VFSTOUFS(mp);
394 	struct vnode *vp, **vpp;
395 	struct vnode *nextvp;
396 	struct dquot *dq;
397 	int error;
398 	struct nameidata nd;
399 	struct ucred *cred;
400 
401 	KKASSERT(td->td_proc);
402 	cred = td->td_proc->p_ucred;
403 
404 	vpp = &ump->um_quotas[type];
405 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
406 	error = vn_open(&nd, FREAD|FWRITE, 0);
407 	if (error)
408 		return (error);
409 	NDFREE(&nd, NDF_ONLY_PNBUF);
410 	vp = nd.ni_vp;
411 	VOP_UNLOCK(vp, 0, td);
412 	if (vp->v_type != VREG) {
413 		(void) vn_close(vp, FREAD|FWRITE, td);
414 		return (EACCES);
415 	}
416 	if (*vpp != vp)
417 		quotaoff(td, mp, type);
418 	ump->um_qflags[type] |= QTF_OPENING;
419 	mp->mnt_flag |= MNT_QUOTA;
420 	vp->v_flag |= VSYSTEM;
421 	*vpp = vp;
422 	/*
423 	 * Save the credential of the process that turned on quotas.
424 	 * Set up the time limits for this quota.
425 	 */
426 	ump->um_cred[type] = crhold(cred);
427 	ump->um_btime[type] = MAX_DQ_TIME;
428 	ump->um_itime[type] = MAX_IQ_TIME;
429 	if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
430 		if (dq->dq_btime > 0)
431 			ump->um_btime[type] = dq->dq_btime;
432 		if (dq->dq_itime > 0)
433 			ump->um_itime[type] = dq->dq_itime;
434 		dqrele(NULLVP, dq);
435 	}
436 	/*
437 	 * Search vnodes associated with this mount point,
438 	 * adding references to quota file being opened.
439 	 * NB: only need to add dquot's for inodes being modified.
440 	 */
441 again:
442 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
443 		nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
444 		if (vp->v_type == VNON || vp->v_writecount == 0)
445 			continue;
446 		if (vget(vp, LK_EXCLUSIVE, td))
447 			goto again;
448 		error = getinoquota(VTOI(vp));
449 		if (error) {
450 			vput(vp);
451 			break;
452 		}
453 		vput(vp);
454 		if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp || vp->v_mount != mp)
455 			goto again;
456 	}
457 	ump->um_qflags[type] &= ~QTF_OPENING;
458 	if (error)
459 		quotaoff(td, mp, type);
460 	return (error);
461 }
462 
463 /*
464  * Q_QUOTAOFF - turn off disk quotas for a filesystem.
465  */
466 int
467 quotaoff(struct thread *td, struct mount *mp, int type)
468 {
469 	struct vnode *vp;
470 	struct vnode *qvp, *nextvp;
471 	struct ufsmount *ump = VFSTOUFS(mp);
472 	struct dquot *dq;
473 	struct inode *ip;
474 	struct ucred *cred;
475 	int error;
476 
477 	KKASSERT(td->td_proc);
478 	cred = td->td_proc->p_ucred;
479 
480 	if ((qvp = ump->um_quotas[type]) == NULLVP)
481 		return (0);
482 	ump->um_qflags[type] |= QTF_CLOSING;
483 	/*
484 	 * Search vnodes associated with this mount point,
485 	 * deleting any references to quota file being closed.
486 	 */
487 again:
488 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
489 		nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
490 		if (vp->v_type == VNON)
491 			continue;
492 		if (vget(vp, LK_EXCLUSIVE, td))
493 			goto again;
494 		ip = VTOI(vp);
495 		dq = ip->i_dquot[type];
496 		ip->i_dquot[type] = NODQUOT;
497 		dqrele(vp, dq);
498 		vput(vp);
499 		if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp || vp->v_mount != mp)
500 			goto again;
501 	}
502 	dqflush(qvp);
503 	qvp->v_flag &= ~VSYSTEM;
504 	error = vn_close(qvp, FREAD|FWRITE, td);
505 	ump->um_quotas[type] = NULLVP;
506 	crfree(ump->um_cred[type]);
507 	ump->um_cred[type] = NOCRED;
508 	ump->um_qflags[type] &= ~QTF_CLOSING;
509 	for (type = 0; type < MAXQUOTAS; type++)
510 		if (ump->um_quotas[type] != NULLVP)
511 			break;
512 	if (type == MAXQUOTAS)
513 		mp->mnt_flag &= ~MNT_QUOTA;
514 	return (error);
515 }
516 
517 /*
518  * Q_GETQUOTA - return current values in a dqblk structure.
519  */
520 int
521 getquota(mp, id, type, addr)
522 	struct mount *mp;
523 	u_long id;
524 	int type;
525 	caddr_t addr;
526 {
527 	struct dquot *dq;
528 	int error;
529 
530 	error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
531 	if (error)
532 		return (error);
533 	error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
534 	dqrele(NULLVP, dq);
535 	return (error);
536 }
537 
538 /*
539  * Q_SETQUOTA - assign an entire dqblk structure.
540  */
541 int
542 setquota(mp, id, type, addr)
543 	struct mount *mp;
544 	u_long id;
545 	int type;
546 	caddr_t addr;
547 {
548 	struct dquot *dq;
549 	struct dquot *ndq;
550 	struct ufsmount *ump = VFSTOUFS(mp);
551 	struct dqblk newlim;
552 	int error;
553 
554 	error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
555 	if (error)
556 		return (error);
557 	error = dqget(NULLVP, id, ump, type, &ndq);
558 	if (error)
559 		return (error);
560 	dq = ndq;
561 	while (dq->dq_flags & DQ_LOCK) {
562 		dq->dq_flags |= DQ_WANT;
563 		(void) tsleep((caddr_t)dq, 0, "setqta", 0);
564 	}
565 	/*
566 	 * Copy all but the current values.
567 	 * Reset time limit if previously had no soft limit or were
568 	 * under it, but now have a soft limit and are over it.
569 	 */
570 	newlim.dqb_curblocks = dq->dq_curblocks;
571 	newlim.dqb_curinodes = dq->dq_curinodes;
572 	if (dq->dq_id != 0) {
573 		newlim.dqb_btime = dq->dq_btime;
574 		newlim.dqb_itime = dq->dq_itime;
575 	}
576 	if (newlim.dqb_bsoftlimit &&
577 	    dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
578 	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
579 		newlim.dqb_btime = time_second + ump->um_btime[type];
580 	if (newlim.dqb_isoftlimit &&
581 	    dq->dq_curinodes >= newlim.dqb_isoftlimit &&
582 	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
583 		newlim.dqb_itime = time_second + ump->um_itime[type];
584 	dq->dq_dqb = newlim;
585 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
586 		dq->dq_flags &= ~DQ_BLKS;
587 	if (dq->dq_curinodes < dq->dq_isoftlimit)
588 		dq->dq_flags &= ~DQ_INODS;
589 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
590 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
591 		dq->dq_flags |= DQ_FAKE;
592 	else
593 		dq->dq_flags &= ~DQ_FAKE;
594 	dq->dq_flags |= DQ_MOD;
595 	dqrele(NULLVP, dq);
596 	return (0);
597 }
598 
599 /*
600  * Q_SETUSE - set current inode and block usage.
601  */
602 int
603 setuse(mp, id, type, addr)
604 	struct mount *mp;
605 	u_long id;
606 	int type;
607 	caddr_t addr;
608 {
609 	struct dquot *dq;
610 	struct ufsmount *ump = VFSTOUFS(mp);
611 	struct dquot *ndq;
612 	struct dqblk usage;
613 	int error;
614 
615 	error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
616 	if (error)
617 		return (error);
618 	error = dqget(NULLVP, id, ump, type, &ndq);
619 	if (error)
620 		return (error);
621 	dq = ndq;
622 	while (dq->dq_flags & DQ_LOCK) {
623 		dq->dq_flags |= DQ_WANT;
624 		(void) tsleep((caddr_t)dq, 0, "setuse", 0);
625 	}
626 	/*
627 	 * Reset time limit if have a soft limit and were
628 	 * previously under it, but are now over it.
629 	 */
630 	if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
631 	    usage.dqb_curblocks >= dq->dq_bsoftlimit)
632 		dq->dq_btime = time_second + ump->um_btime[type];
633 	if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
634 	    usage.dqb_curinodes >= dq->dq_isoftlimit)
635 		dq->dq_itime = time_second + ump->um_itime[type];
636 	dq->dq_curblocks = usage.dqb_curblocks;
637 	dq->dq_curinodes = usage.dqb_curinodes;
638 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
639 		dq->dq_flags &= ~DQ_BLKS;
640 	if (dq->dq_curinodes < dq->dq_isoftlimit)
641 		dq->dq_flags &= ~DQ_INODS;
642 	dq->dq_flags |= DQ_MOD;
643 	dqrele(NULLVP, dq);
644 	return (0);
645 }
646 
647 /*
648  * Q_SYNC - sync quota files to disk.
649  */
650 int
651 qsync(struct mount *mp)
652 {
653 	struct ufsmount *ump = VFSTOUFS(mp);
654 	struct thread *td = curthread;		/* XXX */
655 	struct vnode *vp, *nextvp;
656 	struct dquot *dq;
657 	int i, error;
658 	int gen;
659 
660 	/*
661 	 * Check if the mount point has any quotas.
662 	 * If not, simply return.
663 	 */
664 	for (i = 0; i < MAXQUOTAS; i++)
665 		if (ump->um_quotas[i] != NULLVP)
666 			break;
667 	if (i == MAXQUOTAS)
668 		return (0);
669 	/*
670 	 * Search vnodes associated with this mount point,
671 	 * synchronizing any modified dquot structures.
672 	 */
673 	gen = lwkt_gettoken(&mntvnode_token);
674 again:
675 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
676 		if (vp->v_mount != mp)
677 			goto again;
678 		nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
679 		if (vp->v_type == VNON)
680 			continue;
681 		lwkt_gettoken(&vp->v_interlock);
682 		if (lwkt_gentoken(&mntvnode_token, &gen) != 0) {
683 			lwkt_reltoken(&vp->v_interlock);
684 			goto again;
685 		}
686 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
687 		if (error) {
688 			if (error == ENOENT) {
689 				lwkt_gentoken(&mntvnode_token, &gen);
690 				goto again;
691 			}
692 			continue;
693 		}
694 		for (i = 0; i < MAXQUOTAS; i++) {
695 			dq = VTOI(vp)->i_dquot[i];
696 			if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
697 				dqsync(vp, dq);
698 		}
699 		vput(vp);
700 		lwkt_gentoken(&mntvnode_token, &gen);
701 		if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp)
702 			goto again;
703 	}
704 	lwkt_reltoken(&mntvnode_token);
705 	return (0);
706 }
707 
708 /*
709  * Code pertaining to management of the in-core dquot data structures.
710  */
711 #define DQHASH(dqvp, id) \
712 	(&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
713 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
714 static u_long dqhash;
715 
716 /*
717  * Dquot free list.
718  */
719 #define	DQUOTINC	5	/* minimum free dquots desired */
720 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
721 static long numdquot, desireddquot = DQUOTINC;
722 
723 /*
724  * Initialize the quota system.
725  */
726 void
727 dqinit()
728 {
729 
730 	dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
731 	TAILQ_INIT(&dqfreelist);
732 }
733 
734 /*
735  * Obtain a dquot structure for the specified identifier and quota file
736  * reading the information from the file if necessary.
737  */
738 static int
739 dqget(vp, id, ump, type, dqp)
740 	struct vnode *vp;
741 	u_long id;
742 	struct ufsmount *ump;
743 	int type;
744 	struct dquot **dqp;
745 {
746 	struct thread *td = curthread;		/* XXX */
747 	struct dquot *dq;
748 	struct dqhash *dqh;
749 	struct vnode *dqvp;
750 	struct iovec aiov;
751 	struct uio auio;
752 	int error;
753 
754 	dqvp = ump->um_quotas[type];
755 	if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
756 		*dqp = NODQUOT;
757 		return (EINVAL);
758 	}
759 	/*
760 	 * Check the cache first.
761 	 */
762 	dqh = DQHASH(dqvp, id);
763 	for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
764 		if (dq->dq_id != id ||
765 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
766 			continue;
767 		/*
768 		 * Cache hit with no references.  Take
769 		 * the structure off the free list.
770 		 */
771 		if (dq->dq_cnt == 0)
772 			TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
773 		DQREF(dq);
774 		*dqp = dq;
775 		return (0);
776 	}
777 	/*
778 	 * Not in cache, allocate a new one.
779 	 */
780 	if (dqfreelist.tqh_first == NODQUOT &&
781 	    numdquot < MAXQUOTAS * desiredvnodes)
782 		desireddquot += DQUOTINC;
783 	if (numdquot < desireddquot) {
784 		dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
785 		bzero((char *)dq, sizeof *dq);
786 		numdquot++;
787 	} else {
788 		if ((dq = dqfreelist.tqh_first) == NULL) {
789 			tablefull("dquot");
790 			*dqp = NODQUOT;
791 			return (EUSERS);
792 		}
793 		if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
794 			panic("dqget: free dquot isn't");
795 		TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
796 		if (dq->dq_ump != NULL)
797 			LIST_REMOVE(dq, dq_hash);
798 	}
799 	/*
800 	 * Initialize the contents of the dquot structure.
801 	 */
802 	if (vp != dqvp)
803 		vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
804 	LIST_INSERT_HEAD(dqh, dq, dq_hash);
805 	DQREF(dq);
806 	dq->dq_flags = DQ_LOCK;
807 	dq->dq_id = id;
808 	dq->dq_ump = ump;
809 	dq->dq_type = type;
810 	auio.uio_iov = &aiov;
811 	auio.uio_iovcnt = 1;
812 	aiov.iov_base = (caddr_t)&dq->dq_dqb;
813 	aiov.iov_len = sizeof (struct dqblk);
814 	auio.uio_resid = sizeof (struct dqblk);
815 	auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
816 	auio.uio_segflg = UIO_SYSSPACE;
817 	auio.uio_rw = UIO_READ;
818 	auio.uio_td = NULL;
819 	error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
820 	if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
821 		bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk));
822 	if (vp != dqvp)
823 		VOP_UNLOCK(dqvp, 0, td);
824 	if (dq->dq_flags & DQ_WANT)
825 		wakeup((caddr_t)dq);
826 	dq->dq_flags = 0;
827 	/*
828 	 * I/O error in reading quota file, release
829 	 * quota structure and reflect problem to caller.
830 	 */
831 	if (error) {
832 		LIST_REMOVE(dq, dq_hash);
833 		dqrele(vp, dq);
834 		*dqp = NODQUOT;
835 		return (error);
836 	}
837 	/*
838 	 * Check for no limit to enforce.
839 	 * Initialize time values if necessary.
840 	 */
841 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
842 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
843 		dq->dq_flags |= DQ_FAKE;
844 	if (dq->dq_id != 0) {
845 		if (dq->dq_btime == 0)
846 			dq->dq_btime = time_second + ump->um_btime[type];
847 		if (dq->dq_itime == 0)
848 			dq->dq_itime = time_second + ump->um_itime[type];
849 	}
850 	*dqp = dq;
851 	return (0);
852 }
853 
854 #ifdef DIAGNOSTIC
855 /*
856  * Obtain a reference to a dquot.
857  */
858 static void
859 dqref(dq)
860 	struct dquot *dq;
861 {
862 
863 	dq->dq_cnt++;
864 }
865 #endif
866 
867 /*
868  * Release a reference to a dquot.
869  */
870 void
871 dqrele(vp, dq)
872 	struct vnode *vp;
873 	struct dquot *dq;
874 {
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, 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, 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, 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(vp)
944 	struct vnode *vp;
945 {
946 	struct dquot *dq, *nextdq;
947 	struct dqhash *dqh;
948 
949 	/*
950 	 * Move all dquot's that used to refer to this quota
951 	 * file off their hash chains (they will eventually
952 	 * fall off the head of the free list and be re-used).
953 	 */
954 	for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
955 		for (dq = dqh->lh_first; dq; dq = nextdq) {
956 			nextdq = dq->dq_hash.le_next;
957 			if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
958 				continue;
959 			if (dq->dq_cnt)
960 				panic("dqflush: stray dquot");
961 			LIST_REMOVE(dq, dq_hash);
962 			dq->dq_ump = (struct ufsmount *)0;
963 		}
964 	}
965 }
966