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 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)ufs_quota.c	8.5 (Berkeley) 5/20/95
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/10/sys/ufs/ufs/ufs_quota.c 306178 2016-09-22 10:47:56Z kib $");
37
38#include "opt_ffs.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/endian.h>
43#include <sys/fcntl.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
46#include <sys/malloc.h>
47#include <sys/mount.h>
48#include <sys/mutex.h>
49#include <sys/namei.h>
50#include <sys/priv.h>
51#include <sys/proc.h>
52#include <sys/socket.h>
53#include <sys/stat.h>
54#include <sys/sysctl.h>
55#include <sys/vnode.h>
56
57#include <ufs/ufs/extattr.h>
58#include <ufs/ufs/quota.h>
59#include <ufs/ufs/inode.h>
60#include <ufs/ufs/ufsmount.h>
61#include <ufs/ufs/ufs_extern.h>
62
63CTASSERT(sizeof(struct dqblk64) == sizeof(struct dqhdr64));
64
65static int unprivileged_get_quota = 0;
66SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
67    &unprivileged_get_quota, 0,
68    "Unprivileged processes may retrieve quotas for other uids and gids");
69
70static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
71
72/*
73 * Quota name to error message mapping.
74 */
75static char *quotatypes[] = INITQFNAMES;
76
77static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
78static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
79static int dqopen(struct vnode *, struct ufsmount *, int);
80static int dqget(struct vnode *,
81	u_long, struct ufsmount *, int, struct dquot **);
82static int dqsync(struct vnode *, struct dquot *);
83static int dqflush(struct vnode *);
84static int quotaoff1(struct thread *td, struct mount *mp, int type);
85static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
86
87/* conversion functions - from_to() */
88static void dqb32_dq(const struct dqblk32 *, struct dquot *);
89static void dqb64_dq(const struct dqblk64 *, struct dquot *);
90static void dq_dqb32(const struct dquot *, struct dqblk32 *);
91static void dq_dqb64(const struct dquot *, struct dqblk64 *);
92static void dqb32_dqb64(const struct dqblk32 *, struct dqblk64 *);
93static void dqb64_dqb32(const struct dqblk64 *, struct dqblk32 *);
94
95#ifdef DIAGNOSTIC
96static void dqref(struct dquot *);
97static void chkdquot(struct inode *);
98#endif
99
100/*
101 * Set up the quotas for an inode.
102 *
103 * This routine completely defines the semantics of quotas.
104 * If other criterion want to be used to establish quotas, the
105 * MAXQUOTAS value in quota.h should be increased, and the
106 * additional dquots set up here.
107 */
108int
109getinoquota(struct inode *ip)
110{
111	struct ufsmount *ump;
112	struct vnode *vp;
113	int error;
114
115	vp = ITOV(ip);
116
117	/*
118	 * Disk quotas must be turned off for system files.  Currently
119	 * snapshot and quota files.
120	 */
121	if ((vp->v_vflag & VV_SYSTEM) != 0)
122		return (0);
123	/*
124	 * XXX: Turn off quotas for files with a negative UID or GID.
125	 * This prevents the creation of 100GB+ quota files.
126	 */
127	if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
128		return (0);
129	ump = VFSTOUFS(vp->v_mount);
130	/*
131	 * Set up the user quota based on file uid.
132	 * EINVAL means that quotas are not enabled.
133	 */
134	if ((error =
135		dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
136	    error != EINVAL)
137		return (error);
138	/*
139	 * Set up the group quota based on file gid.
140	 * EINVAL means that quotas are not enabled.
141	 */
142	if ((error =
143		dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
144	    error != EINVAL)
145		return (error);
146	return (0);
147}
148
149/*
150 * Update disk usage, and take corrective action.
151 */
152int
153chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags)
154{
155	struct dquot *dq;
156	ufs2_daddr_t ncurblocks;
157	struct vnode *vp = ITOV(ip);
158	int i, error, warn, do_check;
159
160	/*
161	 * Disk quotas must be turned off for system files.  Currently
162	 * snapshot and quota files.
163	 */
164	if ((vp->v_vflag & VV_SYSTEM) != 0)
165		return (0);
166	/*
167	 * XXX: Turn off quotas for files with a negative UID or GID.
168	 * This prevents the creation of 100GB+ quota files.
169	 */
170	if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
171		return (0);
172#ifdef DIAGNOSTIC
173	if ((flags & CHOWN) == 0)
174		chkdquot(ip);
175#endif
176	if (change == 0)
177		return (0);
178	if (change < 0) {
179		for (i = 0; i < MAXQUOTAS; i++) {
180			if ((dq = ip->i_dquot[i]) == NODQUOT)
181				continue;
182			DQI_LOCK(dq);
183			DQI_WAIT(dq, PINOD+1, "chkdq1");
184			ncurblocks = dq->dq_curblocks + change;
185			if (ncurblocks >= 0)
186				dq->dq_curblocks = ncurblocks;
187			else
188				dq->dq_curblocks = 0;
189			dq->dq_flags &= ~DQ_BLKS;
190			dq->dq_flags |= DQ_MOD;
191			DQI_UNLOCK(dq);
192		}
193		return (0);
194	}
195	if ((flags & FORCE) == 0 &&
196	    priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
197		do_check = 1;
198	else
199		do_check = 0;
200	for (i = 0; i < MAXQUOTAS; i++) {
201		if ((dq = ip->i_dquot[i]) == NODQUOT)
202			continue;
203		warn = 0;
204		DQI_LOCK(dq);
205		DQI_WAIT(dq, PINOD+1, "chkdq2");
206		if (do_check) {
207			error = chkdqchg(ip, change, cred, i, &warn);
208			if (error) {
209				/*
210				 * Roll back user quota changes when
211				 * group quota failed.
212				 */
213				while (i > 0) {
214					--i;
215					dq = ip->i_dquot[i];
216					if (dq == NODQUOT)
217						continue;
218					DQI_LOCK(dq);
219					DQI_WAIT(dq, PINOD+1, "chkdq3");
220					ncurblocks = dq->dq_curblocks - change;
221					if (ncurblocks >= 0)
222						dq->dq_curblocks = ncurblocks;
223					else
224						dq->dq_curblocks = 0;
225					dq->dq_flags &= ~DQ_BLKS;
226					dq->dq_flags |= DQ_MOD;
227					DQI_UNLOCK(dq);
228				}
229				return (error);
230			}
231		}
232		/* Reset timer when crossing soft limit */
233		if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
234		    dq->dq_curblocks < dq->dq_bsoftlimit)
235			dq->dq_btime = time_second + ip->i_ump->um_btime[i];
236		dq->dq_curblocks += change;
237		dq->dq_flags |= DQ_MOD;
238		DQI_UNLOCK(dq);
239		if (warn)
240			uprintf("\n%s: warning, %s disk quota exceeded\n",
241			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
242			    quotatypes[i]);
243	}
244	return (0);
245}
246
247/*
248 * Check for a valid change to a users allocation.
249 * Issue an error message if appropriate.
250 */
251static int
252chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
253    int type, int *warn)
254{
255	struct dquot *dq = ip->i_dquot[type];
256	ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
257
258	/*
259	 * If user would exceed their hard limit, disallow space allocation.
260	 */
261	if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
262		if ((dq->dq_flags & DQ_BLKS) == 0 &&
263		    ip->i_uid == cred->cr_uid) {
264			dq->dq_flags |= DQ_BLKS;
265			DQI_UNLOCK(dq);
266			uprintf("\n%s: write failed, %s disk limit reached\n",
267			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
268			    quotatypes[type]);
269			return (EDQUOT);
270		}
271		DQI_UNLOCK(dq);
272		return (EDQUOT);
273	}
274	/*
275	 * If user is over their soft limit for too long, disallow space
276	 * allocation. Reset time limit as they cross their soft limit.
277	 */
278	if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
279		if (dq->dq_curblocks < dq->dq_bsoftlimit) {
280			dq->dq_btime = time_second + ip->i_ump->um_btime[type];
281			if (ip->i_uid == cred->cr_uid)
282				*warn = 1;
283			return (0);
284		}
285		if (time_second > dq->dq_btime) {
286			if ((dq->dq_flags & DQ_BLKS) == 0 &&
287			    ip->i_uid == cred->cr_uid) {
288				dq->dq_flags |= DQ_BLKS;
289				DQI_UNLOCK(dq);
290				uprintf("\n%s: write failed, %s "
291				    "disk quota exceeded for too long\n",
292				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
293				    quotatypes[type]);
294				return (EDQUOT);
295			}
296			DQI_UNLOCK(dq);
297			return (EDQUOT);
298		}
299	}
300	return (0);
301}
302
303/*
304 * Check the inode limit, applying corrective action.
305 */
306int
307chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
308{
309	struct dquot *dq;
310	int i, error, warn, do_check;
311
312#ifdef DIAGNOSTIC
313	if ((flags & CHOWN) == 0)
314		chkdquot(ip);
315#endif
316	if (change == 0)
317		return (0);
318	if (change < 0) {
319		for (i = 0; i < MAXQUOTAS; i++) {
320			if ((dq = ip->i_dquot[i]) == NODQUOT)
321				continue;
322			DQI_LOCK(dq);
323			DQI_WAIT(dq, PINOD+1, "chkiq1");
324			if (dq->dq_curinodes >= -change)
325				dq->dq_curinodes += change;
326			else
327				dq->dq_curinodes = 0;
328			dq->dq_flags &= ~DQ_INODS;
329			dq->dq_flags |= DQ_MOD;
330			DQI_UNLOCK(dq);
331		}
332		return (0);
333	}
334	if ((flags & FORCE) == 0 &&
335	    priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
336		do_check = 1;
337	else
338		do_check = 0;
339	for (i = 0; i < MAXQUOTAS; i++) {
340		if ((dq = ip->i_dquot[i]) == NODQUOT)
341			continue;
342		warn = 0;
343		DQI_LOCK(dq);
344		DQI_WAIT(dq, PINOD+1, "chkiq2");
345		if (do_check) {
346			error = chkiqchg(ip, change, cred, i, &warn);
347			if (error) {
348				/*
349				 * Roll back user quota changes when
350				 * group quota failed.
351				 */
352				while (i > 0) {
353					--i;
354					dq = ip->i_dquot[i];
355					if (dq == NODQUOT)
356						continue;
357					DQI_LOCK(dq);
358					DQI_WAIT(dq, PINOD+1, "chkiq3");
359					if (dq->dq_curinodes >= change)
360						dq->dq_curinodes -= change;
361					else
362						dq->dq_curinodes = 0;
363					dq->dq_flags &= ~DQ_INODS;
364					dq->dq_flags |= DQ_MOD;
365					DQI_UNLOCK(dq);
366				}
367				return (error);
368			}
369		}
370		/* Reset timer when crossing soft limit */
371		if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
372		    dq->dq_curinodes < dq->dq_isoftlimit)
373			dq->dq_itime = time_second + ip->i_ump->um_itime[i];
374		dq->dq_curinodes += change;
375		dq->dq_flags |= DQ_MOD;
376		DQI_UNLOCK(dq);
377		if (warn)
378			uprintf("\n%s: warning, %s inode quota exceeded\n",
379			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
380			    quotatypes[i]);
381	}
382	return (0);
383}
384
385/*
386 * Check for a valid change to a users allocation.
387 * Issue an error message if appropriate.
388 */
389static int
390chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
391{
392	struct dquot *dq = ip->i_dquot[type];
393	ino_t ncurinodes = dq->dq_curinodes + change;
394
395	/*
396	 * If user would exceed their hard limit, disallow inode allocation.
397	 */
398	if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
399		if ((dq->dq_flags & DQ_INODS) == 0 &&
400		    ip->i_uid == cred->cr_uid) {
401			dq->dq_flags |= DQ_INODS;
402			DQI_UNLOCK(dq);
403			uprintf("\n%s: write failed, %s inode limit reached\n",
404			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
405			    quotatypes[type]);
406			return (EDQUOT);
407		}
408		DQI_UNLOCK(dq);
409		return (EDQUOT);
410	}
411	/*
412	 * If user is over their soft limit for too long, disallow inode
413	 * allocation. Reset time limit as they cross their soft limit.
414	 */
415	if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
416		if (dq->dq_curinodes < dq->dq_isoftlimit) {
417			dq->dq_itime = time_second + ip->i_ump->um_itime[type];
418			if (ip->i_uid == cred->cr_uid)
419				*warn = 1;
420			return (0);
421		}
422		if (time_second > dq->dq_itime) {
423			if ((dq->dq_flags & DQ_INODS) == 0 &&
424			    ip->i_uid == cred->cr_uid) {
425				dq->dq_flags |= DQ_INODS;
426				DQI_UNLOCK(dq);
427				uprintf("\n%s: write failed, %s "
428				    "inode quota exceeded for too long\n",
429				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
430				    quotatypes[type]);
431				return (EDQUOT);
432			}
433			DQI_UNLOCK(dq);
434			return (EDQUOT);
435		}
436	}
437	return (0);
438}
439
440#ifdef DIAGNOSTIC
441/*
442 * On filesystems with quotas enabled, it is an error for a file to change
443 * size and not to have a dquot structure associated with it.
444 */
445static void
446chkdquot(struct inode *ip)
447{
448	struct ufsmount *ump = ip->i_ump;
449	struct vnode *vp = ITOV(ip);
450	int i;
451
452	/*
453	 * Disk quotas must be turned off for system files.  Currently
454	 * these are snapshots and quota files.
455	 */
456	if ((vp->v_vflag & VV_SYSTEM) != 0)
457		return;
458	/*
459	 * XXX: Turn off quotas for files with a negative UID or GID.
460	 * This prevents the creation of 100GB+ quota files.
461	 */
462	if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
463		return;
464
465	UFS_LOCK(ump);
466	for (i = 0; i < MAXQUOTAS; i++) {
467		if (ump->um_quotas[i] == NULLVP ||
468		    (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
469			continue;
470		if (ip->i_dquot[i] == NODQUOT) {
471			UFS_UNLOCK(ump);
472			vprint("chkdquot: missing dquot", ITOV(ip));
473			panic("chkdquot: missing dquot");
474		}
475	}
476	UFS_UNLOCK(ump);
477}
478#endif
479
480/*
481 * Code to process quotactl commands.
482 */
483
484/*
485 * Q_QUOTAON - set up a quota file for a particular filesystem.
486 */
487int
488quotaon(struct thread *td, struct mount *mp, int type, void *fname)
489{
490	struct ufsmount *ump;
491	struct vnode *vp, **vpp;
492	struct vnode *mvp;
493	struct dquot *dq;
494	int error, flags;
495	struct nameidata nd;
496
497	error = priv_check(td, PRIV_UFS_QUOTAON);
498	if (error != 0) {
499		vfs_unbusy(mp);
500		return (error);
501	}
502
503	if ((mp->mnt_flag & MNT_RDONLY) != 0) {
504		vfs_unbusy(mp);
505		return (EROFS);
506	}
507
508	ump = VFSTOUFS(mp);
509	dq = NODQUOT;
510
511	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
512	flags = FREAD | FWRITE;
513	vfs_ref(mp);
514	vfs_unbusy(mp);
515	error = vn_open(&nd, &flags, 0, NULL);
516	if (error != 0) {
517		vfs_rel(mp);
518		return (error);
519	}
520	NDFREE(&nd, NDF_ONLY_PNBUF);
521	vp = nd.ni_vp;
522	error = vfs_busy(mp, MBF_NOWAIT);
523	vfs_rel(mp);
524	if (error == 0) {
525		if (vp->v_type != VREG) {
526			error = EACCES;
527			vfs_unbusy(mp);
528		}
529	}
530	if (error != 0) {
531		VOP_UNLOCK(vp, 0);
532		(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
533		return (error);
534	}
535
536	UFS_LOCK(ump);
537	if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
538		UFS_UNLOCK(ump);
539		VOP_UNLOCK(vp, 0);
540		(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
541		vfs_unbusy(mp);
542		return (EALREADY);
543	}
544	ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
545	UFS_UNLOCK(ump);
546	if ((error = dqopen(vp, ump, type)) != 0) {
547		VOP_UNLOCK(vp, 0);
548		UFS_LOCK(ump);
549		ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING);
550		UFS_UNLOCK(ump);
551		(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
552		vfs_unbusy(mp);
553		return (error);
554	}
555	VOP_UNLOCK(vp, 0);
556	MNT_ILOCK(mp);
557	mp->mnt_flag |= MNT_QUOTA;
558	MNT_IUNLOCK(mp);
559
560	vpp = &ump->um_quotas[type];
561	if (*vpp != vp)
562		quotaoff1(td, mp, type);
563
564	/*
565	 * When the directory vnode containing the quota file is
566	 * inactivated, due to the shared lookup of the quota file
567	 * vput()ing the dvp, the qsyncvp() call for the containing
568	 * directory would try to acquire the quota lock exclusive.
569	 * At the same time, lookup already locked the quota vnode
570	 * shared.  Mark the quota vnode lock as allowing recursion
571	 * and automatically converting shared locks to exclusive.
572	 *
573	 * Also mark quota vnode as system.
574	 */
575	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
576	vp->v_vflag |= VV_SYSTEM;
577	VN_LOCK_AREC(vp);
578	VN_LOCK_DSHARE(vp);
579	VOP_UNLOCK(vp, 0);
580	*vpp = vp;
581	/*
582	 * Save the credential of the process that turned on quotas.
583	 * Set up the time limits for this quota.
584	 */
585	ump->um_cred[type] = crhold(td->td_ucred);
586	ump->um_btime[type] = MAX_DQ_TIME;
587	ump->um_itime[type] = MAX_IQ_TIME;
588	if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
589		if (dq->dq_btime > 0)
590			ump->um_btime[type] = dq->dq_btime;
591		if (dq->dq_itime > 0)
592			ump->um_itime[type] = dq->dq_itime;
593		dqrele(NULLVP, dq);
594	}
595	/*
596	 * Allow the getdq from getinoquota below to read the quota
597	 * from file.
598	 */
599	UFS_LOCK(ump);
600	ump->um_qflags[type] &= ~QTF_CLOSING;
601	UFS_UNLOCK(ump);
602	/*
603	 * Search vnodes associated with this mount point,
604	 * adding references to quota file being opened.
605	 * NB: only need to add dquot's for inodes being modified.
606	 */
607again:
608	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
609		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
610			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
611			goto again;
612		}
613		if (vp->v_type == VNON || vp->v_writecount == 0) {
614			VOP_UNLOCK(vp, 0);
615			vrele(vp);
616			continue;
617		}
618		error = getinoquota(VTOI(vp));
619		VOP_UNLOCK(vp, 0);
620		vrele(vp);
621		if (error) {
622			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
623			break;
624		}
625	}
626
627        if (error)
628		quotaoff_inchange(td, mp, type);
629	UFS_LOCK(ump);
630	ump->um_qflags[type] &= ~QTF_OPENING;
631	KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
632		("quotaon: leaking flags"));
633	UFS_UNLOCK(ump);
634
635	vfs_unbusy(mp);
636	return (error);
637}
638
639/*
640 * Main code to turn off disk quotas for a filesystem. Does not change
641 * flags.
642 */
643static int
644quotaoff1(struct thread *td, struct mount *mp, int type)
645{
646	struct vnode *vp;
647	struct vnode *qvp, *mvp;
648	struct ufsmount *ump;
649	struct dquot *dq;
650	struct inode *ip;
651	struct ucred *cr;
652	int error;
653
654	ump = VFSTOUFS(mp);
655
656	UFS_LOCK(ump);
657	KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
658		("quotaoff1: flags are invalid"));
659	if ((qvp = ump->um_quotas[type]) == NULLVP) {
660		UFS_UNLOCK(ump);
661		return (0);
662	}
663	cr = ump->um_cred[type];
664	UFS_UNLOCK(ump);
665
666	/*
667	 * Search vnodes associated with this mount point,
668	 * deleting any references to quota file being closed.
669	 */
670again:
671	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
672		if (vp->v_type == VNON) {
673			VI_UNLOCK(vp);
674			continue;
675		}
676		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
677			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
678			goto again;
679		}
680		ip = VTOI(vp);
681		dq = ip->i_dquot[type];
682		ip->i_dquot[type] = NODQUOT;
683		dqrele(vp, dq);
684		VOP_UNLOCK(vp, 0);
685		vrele(vp);
686	}
687
688	error = dqflush(qvp);
689	if (error != 0)
690		return (error);
691
692	/*
693	 * Clear um_quotas before closing the quota vnode to prevent
694	 * access to the closed vnode from dqget/dqsync
695	 */
696	UFS_LOCK(ump);
697	ump->um_quotas[type] = NULLVP;
698	ump->um_cred[type] = NOCRED;
699	UFS_UNLOCK(ump);
700
701	vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
702	qvp->v_vflag &= ~VV_SYSTEM;
703	VOP_UNLOCK(qvp, 0);
704	error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
705	crfree(cr);
706
707	return (error);
708}
709
710/*
711 * Turns off quotas, assumes that ump->um_qflags are already checked
712 * and QTF_CLOSING is set to indicate operation in progress. Fixes
713 * ump->um_qflags and mp->mnt_flag after.
714 */
715int
716quotaoff_inchange(struct thread *td, struct mount *mp, int type)
717{
718	struct ufsmount *ump;
719	int i;
720	int error;
721
722	error = quotaoff1(td, mp, type);
723
724	ump = VFSTOUFS(mp);
725	UFS_LOCK(ump);
726	ump->um_qflags[type] &= ~QTF_CLOSING;
727	for (i = 0; i < MAXQUOTAS; i++)
728		if (ump->um_quotas[i] != NULLVP)
729			break;
730	if (i == MAXQUOTAS) {
731		MNT_ILOCK(mp);
732		mp->mnt_flag &= ~MNT_QUOTA;
733		MNT_IUNLOCK(mp);
734	}
735	UFS_UNLOCK(ump);
736	return (error);
737}
738
739/*
740 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
741 */
742int
743quotaoff(struct thread *td, struct mount *mp, int type)
744{
745	struct ufsmount *ump;
746	int error;
747
748	error = priv_check(td, PRIV_UFS_QUOTAOFF);
749	if (error)
750		return (error);
751
752	ump = VFSTOUFS(mp);
753	UFS_LOCK(ump);
754	if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
755		UFS_UNLOCK(ump);
756		return (EALREADY);
757	}
758	ump->um_qflags[type] |= QTF_CLOSING;
759	UFS_UNLOCK(ump);
760
761	return (quotaoff_inchange(td, mp, type));
762}
763
764/*
765 * Q_GETQUOTA - return current values in a dqblk structure.
766 */
767static int
768_getquota(struct thread *td, struct mount *mp, u_long id, int type,
769    struct dqblk64 *dqb)
770{
771	struct dquot *dq;
772	int error;
773
774	switch (type) {
775	case USRQUOTA:
776		if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
777			error = priv_check(td, PRIV_VFS_GETQUOTA);
778			if (error)
779				return (error);
780		}
781		break;
782
783	case GRPQUOTA:
784		if (!groupmember(id, td->td_ucred) &&
785		    !unprivileged_get_quota) {
786			error = priv_check(td, PRIV_VFS_GETQUOTA);
787			if (error)
788				return (error);
789		}
790		break;
791
792	default:
793		return (EINVAL);
794	}
795
796	dq = NODQUOT;
797	error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
798	if (error)
799		return (error);
800	*dqb = dq->dq_dqb;
801	dqrele(NULLVP, dq);
802	return (error);
803}
804
805/*
806 * Q_SETQUOTA - assign an entire dqblk structure.
807 */
808static int
809_setquota(struct thread *td, struct mount *mp, u_long id, int type,
810    struct dqblk64 *dqb)
811{
812	struct dquot *dq;
813	struct dquot *ndq;
814	struct ufsmount *ump;
815	struct dqblk64 newlim;
816	int error;
817
818	error = priv_check(td, PRIV_VFS_SETQUOTA);
819	if (error)
820		return (error);
821
822	newlim = *dqb;
823
824	ndq = NODQUOT;
825	ump = VFSTOUFS(mp);
826
827	error = dqget(NULLVP, id, ump, type, &ndq);
828	if (error)
829		return (error);
830	dq = ndq;
831	DQI_LOCK(dq);
832	DQI_WAIT(dq, PINOD+1, "setqta");
833	/*
834	 * Copy all but the current values.
835	 * Reset time limit if previously had no soft limit or were
836	 * under it, but now have a soft limit and are over it.
837	 */
838	newlim.dqb_curblocks = dq->dq_curblocks;
839	newlim.dqb_curinodes = dq->dq_curinodes;
840	if (dq->dq_id != 0) {
841		newlim.dqb_btime = dq->dq_btime;
842		newlim.dqb_itime = dq->dq_itime;
843	}
844	if (newlim.dqb_bsoftlimit &&
845	    dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
846	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
847		newlim.dqb_btime = time_second + ump->um_btime[type];
848	if (newlim.dqb_isoftlimit &&
849	    dq->dq_curinodes >= newlim.dqb_isoftlimit &&
850	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
851		newlim.dqb_itime = time_second + ump->um_itime[type];
852	dq->dq_dqb = newlim;
853	if (dq->dq_curblocks < dq->dq_bsoftlimit)
854		dq->dq_flags &= ~DQ_BLKS;
855	if (dq->dq_curinodes < dq->dq_isoftlimit)
856		dq->dq_flags &= ~DQ_INODS;
857	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
858	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
859		dq->dq_flags |= DQ_FAKE;
860	else
861		dq->dq_flags &= ~DQ_FAKE;
862	dq->dq_flags |= DQ_MOD;
863	DQI_UNLOCK(dq);
864	dqrele(NULLVP, dq);
865	return (0);
866}
867
868/*
869 * Q_SETUSE - set current inode and block usage.
870 */
871static int
872_setuse(struct thread *td, struct mount *mp, u_long id, int type,
873    struct dqblk64 *dqb)
874{
875	struct dquot *dq;
876	struct ufsmount *ump;
877	struct dquot *ndq;
878	struct dqblk64 usage;
879	int error;
880
881	error = priv_check(td, PRIV_UFS_SETUSE);
882	if (error)
883		return (error);
884
885	usage = *dqb;
886
887	ump = VFSTOUFS(mp);
888	ndq = NODQUOT;
889
890	error = dqget(NULLVP, id, ump, type, &ndq);
891	if (error)
892		return (error);
893	dq = ndq;
894	DQI_LOCK(dq);
895	DQI_WAIT(dq, PINOD+1, "setuse");
896	/*
897	 * Reset time limit if have a soft limit and were
898	 * previously under it, but are now over it.
899	 */
900	if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
901	    usage.dqb_curblocks >= dq->dq_bsoftlimit)
902		dq->dq_btime = time_second + ump->um_btime[type];
903	if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
904	    usage.dqb_curinodes >= dq->dq_isoftlimit)
905		dq->dq_itime = time_second + ump->um_itime[type];
906	dq->dq_curblocks = usage.dqb_curblocks;
907	dq->dq_curinodes = usage.dqb_curinodes;
908	if (dq->dq_curblocks < dq->dq_bsoftlimit)
909		dq->dq_flags &= ~DQ_BLKS;
910	if (dq->dq_curinodes < dq->dq_isoftlimit)
911		dq->dq_flags &= ~DQ_INODS;
912	dq->dq_flags |= DQ_MOD;
913	DQI_UNLOCK(dq);
914	dqrele(NULLVP, dq);
915	return (0);
916}
917
918int
919getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
920{
921	struct dqblk32 dqb32;
922	struct dqblk64 dqb64;
923	int error;
924
925	error = _getquota(td, mp, id, type, &dqb64);
926	if (error)
927		return (error);
928	dqb64_dqb32(&dqb64, &dqb32);
929	error = copyout(&dqb32, addr, sizeof(dqb32));
930	return (error);
931}
932
933int
934setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
935{
936	struct dqblk32 dqb32;
937	struct dqblk64 dqb64;
938	int error;
939
940	error = copyin(addr, &dqb32, sizeof(dqb32));
941	if (error)
942		return (error);
943	dqb32_dqb64(&dqb32, &dqb64);
944	error = _setquota(td, mp, id, type, &dqb64);
945	return (error);
946}
947
948int
949setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
950{
951	struct dqblk32 dqb32;
952	struct dqblk64 dqb64;
953	int error;
954
955	error = copyin(addr, &dqb32, sizeof(dqb32));
956	if (error)
957		return (error);
958	dqb32_dqb64(&dqb32, &dqb64);
959	error = _setuse(td, mp, id, type, &dqb64);
960	return (error);
961}
962
963int
964getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
965{
966	struct dqblk64 dqb64;
967	int error;
968
969	error = _getquota(td, mp, id, type, &dqb64);
970	if (error)
971		return (error);
972	error = copyout(&dqb64, addr, sizeof(dqb64));
973	return (error);
974}
975
976int
977setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
978{
979	struct dqblk64 dqb64;
980	int error;
981
982	error = copyin(addr, &dqb64, sizeof(dqb64));
983	if (error)
984		return (error);
985	error = _setquota(td, mp, id, type, &dqb64);
986	return (error);
987}
988
989int
990setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
991{
992	struct dqblk64 dqb64;
993	int error;
994
995	error = copyin(addr, &dqb64, sizeof(dqb64));
996	if (error)
997		return (error);
998	error = _setuse(td, mp, id, type, &dqb64);
999	return (error);
1000}
1001
1002/*
1003 * Q_GETQUOTASIZE - get bit-size of quota file fields
1004 */
1005int
1006getquotasize(struct thread *td, struct mount *mp, u_long id, int type,
1007    void *sizep)
1008{
1009	struct ufsmount *ump = VFSTOUFS(mp);
1010	int bitsize;
1011
1012	UFS_LOCK(ump);
1013	if (ump->um_quotas[type] == NULLVP ||
1014	    (ump->um_qflags[type] & QTF_CLOSING)) {
1015		UFS_UNLOCK(ump);
1016		return (EINVAL);
1017	}
1018	if ((ump->um_qflags[type] & QTF_64BIT) != 0)
1019		bitsize = 64;
1020	else
1021		bitsize = 32;
1022	UFS_UNLOCK(ump);
1023	return (copyout(&bitsize, sizep, sizeof(int)));
1024}
1025
1026/*
1027 * Q_SYNC - sync quota files to disk.
1028 */
1029int
1030qsync(struct mount *mp)
1031{
1032	struct ufsmount *ump = VFSTOUFS(mp);
1033	struct thread *td = curthread;		/* XXX */
1034	struct vnode *vp, *mvp;
1035	struct dquot *dq;
1036	int i, error;
1037
1038	/*
1039	 * Check if the mount point has any quotas.
1040	 * If not, simply return.
1041	 */
1042	UFS_LOCK(ump);
1043	for (i = 0; i < MAXQUOTAS; i++)
1044		if (ump->um_quotas[i] != NULLVP)
1045			break;
1046	UFS_UNLOCK(ump);
1047	if (i == MAXQUOTAS)
1048		return (0);
1049	/*
1050	 * Search vnodes associated with this mount point,
1051	 * synchronizing any modified dquot structures.
1052	 */
1053again:
1054	MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1055		if (vp->v_type == VNON) {
1056			VI_UNLOCK(vp);
1057			continue;
1058		}
1059		error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
1060		if (error) {
1061			if (error == ENOENT) {
1062				MNT_VNODE_FOREACH_ACTIVE_ABORT(mp, mvp);
1063				goto again;
1064			}
1065			continue;
1066		}
1067		for (i = 0; i < MAXQUOTAS; i++) {
1068			dq = VTOI(vp)->i_dquot[i];
1069			if (dq != NODQUOT)
1070				dqsync(vp, dq);
1071		}
1072		vput(vp);
1073	}
1074	return (0);
1075}
1076
1077/*
1078 * Sync quota file for given vnode to disk.
1079 */
1080int
1081qsyncvp(struct vnode *vp)
1082{
1083	struct ufsmount *ump = VFSTOUFS(vp->v_mount);
1084	struct dquot *dq;
1085	int i;
1086
1087	/*
1088	 * Check if the mount point has any quotas.
1089	 * If not, simply return.
1090	 */
1091	UFS_LOCK(ump);
1092	for (i = 0; i < MAXQUOTAS; i++)
1093		if (ump->um_quotas[i] != NULLVP)
1094			break;
1095	UFS_UNLOCK(ump);
1096	if (i == MAXQUOTAS)
1097		return (0);
1098	/*
1099	 * Search quotas associated with this vnode
1100	 * synchronizing any modified dquot structures.
1101	 */
1102	for (i = 0; i < MAXQUOTAS; i++) {
1103		dq = VTOI(vp)->i_dquot[i];
1104		if (dq != NODQUOT)
1105			dqsync(vp, dq);
1106	}
1107	return (0);
1108}
1109
1110/*
1111 * Code pertaining to management of the in-core dquot data structures.
1112 */
1113#define DQHASH(dqvp, id) \
1114	(&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
1115static LIST_HEAD(dqhash, dquot) *dqhashtbl;
1116static u_long dqhash;
1117
1118/*
1119 * Dquot free list.
1120 */
1121#define	DQUOTINC	5	/* minimum free dquots desired */
1122static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
1123static long numdquot, desireddquot = DQUOTINC;
1124
1125/*
1126 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
1127 * _all_ dqs.
1128 */
1129struct mtx dqhlock;
1130
1131#define	DQH_LOCK()	mtx_lock(&dqhlock)
1132#define	DQH_UNLOCK()	mtx_unlock(&dqhlock)
1133
1134static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
1135	struct vnode *dqvp);
1136
1137/*
1138 * Initialize the quota system.
1139 */
1140void
1141dqinit(void)
1142{
1143
1144	mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
1145	dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
1146	TAILQ_INIT(&dqfreelist);
1147}
1148
1149/*
1150 * Shut down the quota system.
1151 */
1152void
1153dquninit(void)
1154{
1155	struct dquot *dq;
1156
1157	hashdestroy(dqhashtbl, M_DQUOT, dqhash);
1158	while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
1159		TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1160		mtx_destroy(&dq->dq_lock);
1161		free(dq, M_DQUOT);
1162	}
1163	mtx_destroy(&dqhlock);
1164}
1165
1166static struct dquot *
1167dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1168{
1169	struct dquot *dq;
1170
1171	mtx_assert(&dqhlock, MA_OWNED);
1172	LIST_FOREACH(dq, dqh, dq_hash) {
1173		if (dq->dq_id != id ||
1174		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1175			continue;
1176		/*
1177		 * Cache hit with no references.  Take
1178		 * the structure off the free list.
1179		 */
1180		if (dq->dq_cnt == 0)
1181			TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1182		DQREF(dq);
1183		return (dq);
1184	}
1185	return (NODQUOT);
1186}
1187
1188/*
1189 * Determine the quota file type.
1190 *
1191 * A 32-bit quota file is simply an array of struct dqblk32.
1192 *
1193 * A 64-bit quota file is a struct dqhdr64 followed by an array of struct
1194 * dqblk64.  The header contains various magic bits which allow us to be
1195 * reasonably confident that it is indeeda 64-bit quota file and not just
1196 * a 32-bit quota file that just happens to "look right".
1197 *
1198 */
1199static int
1200dqopen(struct vnode *vp, struct ufsmount *ump, int type)
1201{
1202	struct dqhdr64 dqh;
1203	struct iovec aiov;
1204	struct uio auio;
1205	int error;
1206
1207	ASSERT_VOP_LOCKED(vp, "dqopen");
1208	auio.uio_iov = &aiov;
1209	auio.uio_iovcnt = 1;
1210	aiov.iov_base = &dqh;
1211	aiov.iov_len = sizeof(dqh);
1212	auio.uio_resid = sizeof(dqh);
1213	auio.uio_offset = 0;
1214	auio.uio_segflg = UIO_SYSSPACE;
1215	auio.uio_rw = UIO_READ;
1216	auio.uio_td = (struct thread *)0;
1217	error = VOP_READ(vp, &auio, 0, ump->um_cred[type]);
1218
1219	if (error != 0)
1220		return (error);
1221	if (auio.uio_resid > 0) {
1222		/* assume 32 bits */
1223		return (0);
1224	}
1225
1226	UFS_LOCK(ump);
1227	if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 &&
1228	    be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION &&
1229	    be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) &&
1230	    be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) {
1231		/* XXX: what if the magic matches, but the sizes are wrong? */
1232		ump->um_qflags[type] |= QTF_64BIT;
1233	} else {
1234		ump->um_qflags[type] &= ~QTF_64BIT;
1235	}
1236	UFS_UNLOCK(ump);
1237
1238	return (0);
1239}
1240
1241/*
1242 * Obtain a dquot structure for the specified identifier and quota file
1243 * reading the information from the file if necessary.
1244 */
1245static int
1246dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1247    struct dquot **dqp)
1248{
1249	uint8_t buf[sizeof(struct dqblk64)];
1250	off_t base, recsize;
1251	struct dquot *dq, *dq1;
1252	struct dqhash *dqh;
1253	struct vnode *dqvp;
1254	struct iovec aiov;
1255	struct uio auio;
1256	int dqvplocked, error;
1257
1258#ifdef DEBUG_VFS_LOCKS
1259	if (vp != NULLVP)
1260		ASSERT_VOP_ELOCKED(vp, "dqget");
1261#endif
1262
1263	if (vp != NULLVP && *dqp != NODQUOT) {
1264		return (0);
1265	}
1266
1267	/* XXX: Disallow negative id values to prevent the
1268	* creation of 100GB+ quota data files.
1269	*/
1270	if ((int)id < 0)
1271		return (EINVAL);
1272
1273	UFS_LOCK(ump);
1274	dqvp = ump->um_quotas[type];
1275	if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1276		*dqp = NODQUOT;
1277		UFS_UNLOCK(ump);
1278		return (EINVAL);
1279	}
1280	vref(dqvp);
1281	UFS_UNLOCK(ump);
1282	error = 0;
1283	dqvplocked = 0;
1284
1285	/*
1286	 * Check the cache first.
1287	 */
1288	dqh = DQHASH(dqvp, id);
1289	DQH_LOCK();
1290	dq = dqhashfind(dqh, id, dqvp);
1291	if (dq != NULL) {
1292		DQH_UNLOCK();
1293hfound:		DQI_LOCK(dq);
1294		DQI_WAIT(dq, PINOD+1, "dqget");
1295		DQI_UNLOCK(dq);
1296		if (dq->dq_ump == NULL) {
1297			dqrele(vp, dq);
1298			dq = NODQUOT;
1299			error = EIO;
1300		}
1301		*dqp = dq;
1302		if (dqvplocked)
1303			vput(dqvp);
1304		else
1305			vrele(dqvp);
1306		return (error);
1307	}
1308
1309	/*
1310	 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1311	 * since new dq will appear on the hash chain DQ_LOCKed.
1312	 */
1313	if (vp != dqvp) {
1314		DQH_UNLOCK();
1315		vn_lock(dqvp, LK_SHARED | LK_RETRY);
1316		dqvplocked = 1;
1317		DQH_LOCK();
1318		/*
1319		 * Recheck the cache after sleep for quota vnode lock.
1320		 */
1321		dq = dqhashfind(dqh, id, dqvp);
1322		if (dq != NULL) {
1323			DQH_UNLOCK();
1324			goto hfound;
1325		}
1326	}
1327
1328	/*
1329	 * Not in cache, allocate a new one or take it from the
1330	 * free list.
1331	 */
1332	if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1333	    numdquot < MAXQUOTAS * desiredvnodes)
1334		desireddquot += DQUOTINC;
1335	if (numdquot < desireddquot) {
1336		numdquot++;
1337		DQH_UNLOCK();
1338		dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
1339		mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1340		DQH_LOCK();
1341		/*
1342		 * Recheck the cache after sleep for memory.
1343		 */
1344		dq = dqhashfind(dqh, id, dqvp);
1345		if (dq != NULL) {
1346			numdquot--;
1347			DQH_UNLOCK();
1348			mtx_destroy(&dq1->dq_lock);
1349			free(dq1, M_DQUOT);
1350			goto hfound;
1351		}
1352		dq = dq1;
1353	} else {
1354		if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1355			DQH_UNLOCK();
1356			tablefull("dquot");
1357			*dqp = NODQUOT;
1358			if (dqvplocked)
1359				vput(dqvp);
1360			else
1361				vrele(dqvp);
1362			return (EUSERS);
1363		}
1364		if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1365			panic("dqget: free dquot isn't %p", dq);
1366		TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1367		if (dq->dq_ump != NULL)
1368			LIST_REMOVE(dq, dq_hash);
1369	}
1370
1371	/*
1372	 * Dq is put into hash already locked to prevent parallel
1373	 * usage while it is being read from file.
1374	 */
1375	dq->dq_flags = DQ_LOCK;
1376	dq->dq_id = id;
1377	dq->dq_type = type;
1378	dq->dq_ump = ump;
1379	LIST_INSERT_HEAD(dqh, dq, dq_hash);
1380	DQREF(dq);
1381	DQH_UNLOCK();
1382
1383	/*
1384	 * Read the requested quota record from the quota file, performing
1385	 * any necessary conversions.
1386	 */
1387	if (ump->um_qflags[type] & QTF_64BIT) {
1388		recsize = sizeof(struct dqblk64);
1389		base = sizeof(struct dqhdr64);
1390	} else {
1391		recsize = sizeof(struct dqblk32);
1392		base = 0;
1393	}
1394	auio.uio_iov = &aiov;
1395	auio.uio_iovcnt = 1;
1396	aiov.iov_base = buf;
1397	aiov.iov_len = recsize;
1398	auio.uio_resid = recsize;
1399	auio.uio_offset = base + id * recsize;
1400	auio.uio_segflg = UIO_SYSSPACE;
1401	auio.uio_rw = UIO_READ;
1402	auio.uio_td = (struct thread *)0;
1403
1404	error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1405	if (auio.uio_resid == recsize && error == 0) {
1406		bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
1407	} else {
1408		if (ump->um_qflags[type] & QTF_64BIT)
1409			dqb64_dq((struct dqblk64 *)buf, dq);
1410		else
1411			dqb32_dq((struct dqblk32 *)buf, dq);
1412	}
1413	if (dqvplocked)
1414		vput(dqvp);
1415	else
1416		vrele(dqvp);
1417	/*
1418	 * I/O error in reading quota file, release
1419	 * quota structure and reflect problem to caller.
1420	 */
1421	if (error) {
1422		DQH_LOCK();
1423		dq->dq_ump = NULL;
1424		LIST_REMOVE(dq, dq_hash);
1425		DQH_UNLOCK();
1426		DQI_LOCK(dq);
1427		if (dq->dq_flags & DQ_WANT)
1428			wakeup(dq);
1429		dq->dq_flags = 0;
1430		DQI_UNLOCK(dq);
1431		dqrele(vp, dq);
1432		*dqp = NODQUOT;
1433		return (error);
1434	}
1435	DQI_LOCK(dq);
1436	/*
1437	 * Check for no limit to enforce.
1438	 * Initialize time values if necessary.
1439	 */
1440	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1441	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1442		dq->dq_flags |= DQ_FAKE;
1443	if (dq->dq_id != 0) {
1444		if (dq->dq_btime == 0) {
1445			dq->dq_btime = time_second + ump->um_btime[type];
1446			if (dq->dq_bsoftlimit &&
1447			    dq->dq_curblocks >= dq->dq_bsoftlimit)
1448				dq->dq_flags |= DQ_MOD;
1449		}
1450		if (dq->dq_itime == 0) {
1451			dq->dq_itime = time_second + ump->um_itime[type];
1452			if (dq->dq_isoftlimit &&
1453			    dq->dq_curinodes >= dq->dq_isoftlimit)
1454				dq->dq_flags |= DQ_MOD;
1455		}
1456	}
1457	DQI_WAKEUP(dq);
1458	DQI_UNLOCK(dq);
1459	*dqp = dq;
1460	return (0);
1461}
1462
1463#ifdef DIAGNOSTIC
1464/*
1465 * Obtain a reference to a dquot.
1466 */
1467static void
1468dqref(struct dquot *dq)
1469{
1470
1471	dq->dq_cnt++;
1472}
1473#endif
1474
1475/*
1476 * Release a reference to a dquot.
1477 */
1478void
1479dqrele(struct vnode *vp, struct dquot *dq)
1480{
1481
1482	if (dq == NODQUOT)
1483		return;
1484	DQH_LOCK();
1485	KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 1", dq));
1486	if (dq->dq_cnt > 1) {
1487		dq->dq_cnt--;
1488		DQH_UNLOCK();
1489		return;
1490	}
1491	DQH_UNLOCK();
1492sync:
1493	(void) dqsync(vp, dq);
1494
1495	DQH_LOCK();
1496	KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 2", dq));
1497	if (--dq->dq_cnt > 0)
1498	{
1499		DQH_UNLOCK();
1500		return;
1501	}
1502
1503	/*
1504	 * The dq may become dirty after it is synced but before it is
1505	 * put to the free list. Checking the DQ_MOD there without
1506	 * locking dq should be safe since no other references to the
1507	 * dq exist.
1508	 */
1509	if ((dq->dq_flags & DQ_MOD) != 0) {
1510		dq->dq_cnt++;
1511		DQH_UNLOCK();
1512		goto sync;
1513	}
1514	TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1515	DQH_UNLOCK();
1516}
1517
1518/*
1519 * Update the disk quota in the quota file.
1520 */
1521static int
1522dqsync(struct vnode *vp, struct dquot *dq)
1523{
1524	uint8_t buf[sizeof(struct dqblk64)];
1525	off_t base, recsize;
1526	struct vnode *dqvp;
1527	struct iovec aiov;
1528	struct uio auio;
1529	int error;
1530	struct mount *mp;
1531	struct ufsmount *ump;
1532
1533#ifdef DEBUG_VFS_LOCKS
1534	if (vp != NULL)
1535		ASSERT_VOP_ELOCKED(vp, "dqsync");
1536#endif
1537
1538	mp = NULL;
1539	error = 0;
1540	if (dq == NODQUOT)
1541		panic("dqsync: dquot");
1542	if ((ump = dq->dq_ump) == NULL)
1543		return (0);
1544	UFS_LOCK(ump);
1545	if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP) {
1546		if (vp == NULL) {
1547			UFS_UNLOCK(ump);
1548			return (0);
1549		} else
1550			panic("dqsync: file");
1551	}
1552	vref(dqvp);
1553	UFS_UNLOCK(ump);
1554
1555	DQI_LOCK(dq);
1556	if ((dq->dq_flags & DQ_MOD) == 0) {
1557		DQI_UNLOCK(dq);
1558		vrele(dqvp);
1559		return (0);
1560	}
1561	DQI_UNLOCK(dq);
1562
1563	(void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1564	if (vp != dqvp)
1565		vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1566
1567	DQI_LOCK(dq);
1568	DQI_WAIT(dq, PINOD+2, "dqsync");
1569	if ((dq->dq_flags & DQ_MOD) == 0)
1570		goto out;
1571	dq->dq_flags |= DQ_LOCK;
1572	DQI_UNLOCK(dq);
1573
1574	/*
1575	 * Write the quota record to the quota file, performing any
1576	 * necessary conversions.  See dqget() for additional details.
1577	 */
1578	if (ump->um_qflags[dq->dq_type] & QTF_64BIT) {
1579		dq_dqb64(dq, (struct dqblk64 *)buf);
1580		recsize = sizeof(struct dqblk64);
1581		base = sizeof(struct dqhdr64);
1582	} else {
1583		dq_dqb32(dq, (struct dqblk32 *)buf);
1584		recsize = sizeof(struct dqblk32);
1585		base = 0;
1586	}
1587
1588	auio.uio_iov = &aiov;
1589	auio.uio_iovcnt = 1;
1590	aiov.iov_base = buf;
1591	aiov.iov_len = recsize;
1592	auio.uio_resid = recsize;
1593	auio.uio_offset = base + dq->dq_id * recsize;
1594	auio.uio_segflg = UIO_SYSSPACE;
1595	auio.uio_rw = UIO_WRITE;
1596	auio.uio_td = (struct thread *)0;
1597	error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1598	if (auio.uio_resid && error == 0)
1599		error = EIO;
1600
1601	DQI_LOCK(dq);
1602	DQI_WAKEUP(dq);
1603	dq->dq_flags &= ~DQ_MOD;
1604out:
1605	DQI_UNLOCK(dq);
1606	if (vp != dqvp)
1607		vput(dqvp);
1608	else
1609		vrele(dqvp);
1610	vn_finished_secondary_write(mp);
1611	return (error);
1612}
1613
1614/*
1615 * Flush all entries from the cache for a particular vnode.
1616 */
1617static int
1618dqflush(struct vnode *vp)
1619{
1620	struct dquot *dq, *nextdq;
1621	struct dqhash *dqh;
1622	int error;
1623
1624	/*
1625	 * Move all dquot's that used to refer to this quota
1626	 * file off their hash chains (they will eventually
1627	 * fall off the head of the free list and be re-used).
1628	 */
1629	error = 0;
1630	DQH_LOCK();
1631	for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1632		for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1633			nextdq = LIST_NEXT(dq, dq_hash);
1634			if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1635				continue;
1636			if (dq->dq_cnt)
1637				error = EBUSY;
1638			else {
1639				LIST_REMOVE(dq, dq_hash);
1640				dq->dq_ump = NULL;
1641			}
1642		}
1643	}
1644	DQH_UNLOCK();
1645	return (error);
1646}
1647
1648/*
1649 * The following three functions are provided for the adjustment of
1650 * quotas by the soft updates code.
1651 */
1652#ifdef SOFTUPDATES
1653/*
1654 * Acquire a reference to the quota structures associated with a vnode.
1655 * Return count of number of quota structures found.
1656 */
1657int
1658quotaref(vp, qrp)
1659	struct vnode *vp;
1660	struct dquot **qrp;
1661{
1662	struct inode *ip;
1663	struct dquot *dq;
1664	int i, found;
1665
1666	for (i = 0; i < MAXQUOTAS; i++)
1667		qrp[i] = NODQUOT;
1668	/*
1669	 * Disk quotas must be turned off for system files.  Currently
1670	 * snapshot and quota files.
1671	 */
1672	if ((vp->v_vflag & VV_SYSTEM) != 0)
1673		return (0);
1674	/*
1675	 * Iterate through and copy active quotas.
1676	 */
1677	found = 0;
1678	ip = VTOI(vp);
1679	mtx_lock(&dqhlock);
1680	for (i = 0; i < MAXQUOTAS; i++) {
1681		if ((dq = ip->i_dquot[i]) == NODQUOT)
1682			continue;
1683		DQREF(dq);
1684		qrp[i] = dq;
1685		found++;
1686	}
1687	mtx_unlock(&dqhlock);
1688	return (found);
1689}
1690
1691/*
1692 * Release a set of quota structures obtained from a vnode.
1693 */
1694void
1695quotarele(qrp)
1696	struct dquot **qrp;
1697{
1698	struct dquot *dq;
1699	int i;
1700
1701	for (i = 0; i < MAXQUOTAS; i++) {
1702		if ((dq = qrp[i]) == NODQUOT)
1703			continue;
1704		dqrele(NULL, dq);
1705	}
1706}
1707
1708/*
1709 * Adjust the number of blocks associated with a quota.
1710 * Positive numbers when adding blocks; negative numbers when freeing blocks.
1711 */
1712void
1713quotaadj(qrp, ump, blkcount)
1714	struct dquot **qrp;
1715	struct ufsmount *ump;
1716	int64_t blkcount;
1717{
1718	struct dquot *dq;
1719	ufs2_daddr_t ncurblocks;
1720	int i;
1721
1722	if (blkcount == 0)
1723		return;
1724	for (i = 0; i < MAXQUOTAS; i++) {
1725		if ((dq = qrp[i]) == NODQUOT)
1726			continue;
1727		DQI_LOCK(dq);
1728		DQI_WAIT(dq, PINOD+1, "adjqta");
1729		ncurblocks = dq->dq_curblocks + blkcount;
1730		if (ncurblocks >= 0)
1731			dq->dq_curblocks = ncurblocks;
1732		else
1733			dq->dq_curblocks = 0;
1734		if (blkcount < 0)
1735			dq->dq_flags &= ~DQ_BLKS;
1736		else if (dq->dq_curblocks + blkcount >= dq->dq_bsoftlimit &&
1737			 dq->dq_curblocks < dq->dq_bsoftlimit)
1738			dq->dq_btime = time_second + ump->um_btime[i];
1739		dq->dq_flags |= DQ_MOD;
1740		DQI_UNLOCK(dq);
1741	}
1742}
1743#endif /* SOFTUPDATES */
1744
1745/*
1746 * 32-bit / 64-bit conversion functions.
1747 *
1748 * 32-bit quota records are stored in native byte order.  Attention must
1749 * be paid to overflow issues.
1750 *
1751 * 64-bit quota records are stored in network byte order.
1752 */
1753
1754#define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64)
1755
1756/*
1757 * Convert 32-bit host-order structure to dquot.
1758 */
1759static void
1760dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq)
1761{
1762
1763	dq->dq_bhardlimit = dqb32->dqb_bhardlimit;
1764	dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit;
1765	dq->dq_curblocks = dqb32->dqb_curblocks;
1766	dq->dq_ihardlimit = dqb32->dqb_ihardlimit;
1767	dq->dq_isoftlimit = dqb32->dqb_isoftlimit;
1768	dq->dq_curinodes = dqb32->dqb_curinodes;
1769	dq->dq_btime = dqb32->dqb_btime;
1770	dq->dq_itime = dqb32->dqb_itime;
1771}
1772
1773/*
1774 * Convert 64-bit network-order structure to dquot.
1775 */
1776static void
1777dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq)
1778{
1779
1780	dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit);
1781	dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit);
1782	dq->dq_curblocks = be64toh(dqb64->dqb_curblocks);
1783	dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit);
1784	dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit);
1785	dq->dq_curinodes = be64toh(dqb64->dqb_curinodes);
1786	dq->dq_btime = be64toh(dqb64->dqb_btime);
1787	dq->dq_itime = be64toh(dqb64->dqb_itime);
1788}
1789
1790/*
1791 * Convert dquot to 32-bit host-order structure.
1792 */
1793static void
1794dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32)
1795{
1796
1797	dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit);
1798	dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit);
1799	dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks);
1800	dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit);
1801	dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit);
1802	dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes);
1803	dqb32->dqb_btime = CLIP32(dq->dq_btime);
1804	dqb32->dqb_itime = CLIP32(dq->dq_itime);
1805}
1806
1807/*
1808 * Convert dquot to 64-bit network-order structure.
1809 */
1810static void
1811dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64)
1812{
1813
1814	dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit);
1815	dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit);
1816	dqb64->dqb_curblocks = htobe64(dq->dq_curblocks);
1817	dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit);
1818	dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit);
1819	dqb64->dqb_curinodes = htobe64(dq->dq_curinodes);
1820	dqb64->dqb_btime = htobe64(dq->dq_btime);
1821	dqb64->dqb_itime = htobe64(dq->dq_itime);
1822}
1823
1824/*
1825 * Convert 64-bit host-order structure to 32-bit host-order structure.
1826 */
1827static void
1828dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32)
1829{
1830
1831	dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit);
1832	dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit);
1833	dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks);
1834	dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit);
1835	dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit);
1836	dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes);
1837	dqb32->dqb_btime = CLIP32(dqb64->dqb_btime);
1838	dqb32->dqb_itime = CLIP32(dqb64->dqb_itime);
1839}
1840
1841/*
1842 * Convert 32-bit host-order structure to 64-bit host-order structure.
1843 */
1844static void
1845dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64)
1846{
1847
1848	dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit;
1849	dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit;
1850	dqb64->dqb_curblocks = dqb32->dqb_curblocks;
1851	dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit;
1852	dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit;
1853	dqb64->dqb_curinodes = dqb32->dqb_curinodes;
1854	dqb64->dqb_btime = dqb32->dqb_btime;
1855	dqb64->dqb_itime = dqb32->dqb_itime;
1856}
1857