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