]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/ufs/ufs/ufs_quota.c
MFC r362623:
[FreeBSD/stable/8.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/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/mutex.h>
48 #include <sys/namei.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <sys/sysctl.h>
54 #include <sys/vnode.h>
55
56 #include <ufs/ufs/extattr.h>
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/inode.h>
59 #include <ufs/ufs/ufsmount.h>
60 #include <ufs/ufs/ufs_extern.h>
61
62 static int unprivileged_get_quota = 0;
63 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
64     &unprivileged_get_quota, 0,
65     "Unprivileged processes may retrieve quotas for other uids and gids");
66
67 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
68
69 /*
70  * Quota name to error message mapping.
71  */
72 static char *quotatypes[] = INITQFNAMES;
73
74 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
75 static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
76 static int dqget(struct vnode *,
77         u_long, struct ufsmount *, int, struct dquot **);
78 static int dqsync(struct vnode *, struct dquot *);
79 static void dqflush(struct vnode *);
80 static int quotaoff1(struct thread *td, struct mount *mp, int type);
81 static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
82
83 #ifdef DIAGNOSTIC
84 static void dqref(struct dquot *);
85 static void chkdquot(struct inode *);
86 #endif
87
88 /*
89  * Set up the quotas for an inode.
90  *
91  * This routine completely defines the semantics of quotas.
92  * If other criterion want to be used to establish quotas, the
93  * MAXQUOTAS value in quotas.h should be increased, and the
94  * additional dquots set up here.
95  */
96 int
97 getinoquota(struct inode *ip)
98 {
99         struct ufsmount *ump;
100         struct vnode *vp;
101         int error;
102
103         vp = ITOV(ip);
104
105         /*
106          * Disk quotas must be turned off for system files.  Currently
107          * snapshot and quota files.
108          */
109         if ((vp->v_vflag & VV_SYSTEM) != 0)
110                 return (0);
111         /*
112          * XXX: Turn off quotas for files with a negative UID or GID.
113          * This prevents the creation of 100GB+ quota files.
114          */
115         if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
116                 return (0);
117         ump = VFSTOUFS(vp->v_mount);
118         /*
119          * Set up the user quota based on file uid.
120          * EINVAL means that quotas are not enabled.
121          */
122         if ((error =
123                 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
124             error != EINVAL)
125                 return (error);
126         /*
127          * Set up the group quota based on file gid.
128          * EINVAL means that quotas are not enabled.
129          */
130         if ((error =
131                 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
132             error != EINVAL)
133                 return (error);
134         return (0);
135 }
136
137 /*
138  * Update disk usage, and take corrective action.
139  */
140 int
141 chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags)
142 {
143         struct dquot *dq;
144         ufs2_daddr_t ncurblocks;
145         struct vnode *vp = ITOV(ip);
146         int i, error, warn, do_check;
147
148         /*
149          * Disk quotas must be turned off for system files.  Currently
150          * snapshot and quota files.
151          */
152         if ((vp->v_vflag & VV_SYSTEM) != 0)
153                 return (0);
154         /*
155          * XXX: Turn off quotas for files with a negative UID or GID.
156          * This prevents the creation of 100GB+ quota files.
157          */
158         if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
159                 return (0);
160 #ifdef DIAGNOSTIC
161         if ((flags & CHOWN) == 0)
162                 chkdquot(ip);
163 #endif
164         if (change == 0)
165                 return (0);
166         if (change < 0) {
167                 for (i = 0; i < MAXQUOTAS; i++) {
168                         if ((dq = ip->i_dquot[i]) == NODQUOT)
169                                 continue;
170                         DQI_LOCK(dq);
171                         DQI_WAIT(dq, PINOD+1, "chkdq1");
172                         ncurblocks = dq->dq_curblocks + change;
173                         if (ncurblocks >= 0)
174                                 dq->dq_curblocks = ncurblocks;
175                         else
176                                 dq->dq_curblocks = 0;
177                         dq->dq_flags &= ~DQ_BLKS;
178                         dq->dq_flags |= DQ_MOD;
179                         DQI_UNLOCK(dq);
180                 }
181                 return (0);
182         }
183         if ((flags & FORCE) == 0 &&
184             priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
185                 do_check = 1;
186         else
187                 do_check = 0;
188         for (i = 0; i < MAXQUOTAS; i++) {
189                 if ((dq = ip->i_dquot[i]) == NODQUOT)
190                         continue;
191                 warn = 0;
192                 DQI_LOCK(dq);
193                 DQI_WAIT(dq, PINOD+1, "chkdq2");
194                 if (do_check) {
195                         error = chkdqchg(ip, change, cred, i, &warn);
196                         if (error) {
197                                 /*
198                                  * Roll back user quota changes when
199                                  * group quota failed.
200                                  */
201                                 while (i > 0) {
202                                         --i;
203                                         dq = ip->i_dquot[i];
204                                         if (dq == NODQUOT)
205                                                 continue;
206                                         DQI_LOCK(dq);
207                                         DQI_WAIT(dq, PINOD+1, "chkdq3");
208                                         ncurblocks = dq->dq_curblocks - change;
209                                         if (ncurblocks >= 0)
210                                                 dq->dq_curblocks = ncurblocks;
211                                         else
212                                                 dq->dq_curblocks = 0;
213                                         dq->dq_flags &= ~DQ_BLKS;
214                                         dq->dq_flags |= DQ_MOD;
215                                         DQI_UNLOCK(dq);
216                                 }
217                                 return (error);
218                         }
219                 }
220                 /* Reset timer when crossing soft limit */
221                 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
222                     dq->dq_curblocks < dq->dq_bsoftlimit)
223                         dq->dq_btime = time_second + ip->i_ump->um_btime[i];
224                 dq->dq_curblocks += change;
225                 dq->dq_flags |= DQ_MOD;
226                 DQI_UNLOCK(dq);
227                 if (warn)
228                         uprintf("\n%s: warning, %s %s\n",
229                                 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
230                                 quotatypes[i], "disk quota exceeded");
231         }
232         return (0);
233 }
234
235 /*
236  * Check for a valid change to a users allocation.
237  * Issue an error message if appropriate.
238  */
239 static int
240 chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
241     int type, int *warn)
242 {
243         struct dquot *dq = ip->i_dquot[type];
244         ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
245
246         /*
247          * If user would exceed their hard limit, disallow space allocation.
248          */
249         if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
250                 if ((dq->dq_flags & DQ_BLKS) == 0 &&
251                     ip->i_uid == cred->cr_uid) {
252                         dq->dq_flags |= DQ_BLKS;
253                         DQI_UNLOCK(dq);
254                         uprintf("\n%s: write failed, %s disk limit reached\n",
255                             ITOV(ip)->v_mount->mnt_stat.f_mntonname,
256                             quotatypes[type]);
257                         return (EDQUOT);
258                 }
259                 DQI_UNLOCK(dq);
260                 return (EDQUOT);
261         }
262         /*
263          * If user is over their soft limit for too long, disallow space
264          * allocation. Reset time limit as they cross their soft limit.
265          */
266         if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
267                 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
268                         dq->dq_btime = time_second + ip->i_ump->um_btime[type];
269                         if (ip->i_uid == cred->cr_uid)
270                                 *warn = 1;
271                         return (0);
272                 }
273                 if (time_second > dq->dq_btime) {
274                         if ((dq->dq_flags & DQ_BLKS) == 0 &&
275                             ip->i_uid == cred->cr_uid) {
276                                 dq->dq_flags |= DQ_BLKS;
277                                 DQI_UNLOCK(dq);
278                                 uprintf("\n%s: write failed, %s %s\n",
279                                     ITOV(ip)->v_mount->mnt_stat.f_mntonname,
280                                     quotatypes[type],
281                                     "disk quota exceeded for too long");
282                                 return (EDQUOT);
283                         }
284                         DQI_UNLOCK(dq);
285                         return (EDQUOT);
286                 }
287         }
288         return (0);
289 }
290
291 /*
292  * Check the inode limit, applying corrective action.
293  */
294 int
295 chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
296 {
297         struct dquot *dq;
298         ino_t ncurinodes;
299         int i, error, warn, do_check;
300
301 #ifdef DIAGNOSTIC
302         if ((flags & CHOWN) == 0)
303                 chkdquot(ip);
304 #endif
305         if (change == 0)
306                 return (0);
307         if (change < 0) {
308                 for (i = 0; i < MAXQUOTAS; i++) {
309                         if ((dq = ip->i_dquot[i]) == NODQUOT)
310                                 continue;
311                         DQI_LOCK(dq);
312                         DQI_WAIT(dq, PINOD+1, "chkiq1");
313                         ncurinodes = dq->dq_curinodes + change;
314                         /* XXX: ncurinodes is unsigned */
315                         if (dq->dq_curinodes != 0 && ncurinodes >= 0)
316                                 dq->dq_curinodes = ncurinodes;
317                         else
318                                 dq->dq_curinodes = 0;
319                         dq->dq_flags &= ~DQ_INODS;
320                         dq->dq_flags |= DQ_MOD;
321                         DQI_UNLOCK(dq);
322                 }
323                 return (0);
324         }
325         if ((flags & FORCE) == 0 &&
326             priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
327                 do_check = 1;
328         else
329                 do_check = 0;
330         for (i = 0; i < MAXQUOTAS; i++) {
331                 if ((dq = ip->i_dquot[i]) == NODQUOT)
332                         continue;
333                 warn = 0;
334                 DQI_LOCK(dq);
335                 DQI_WAIT(dq, PINOD+1, "chkiq2");
336                 if (do_check) {
337                         error = chkiqchg(ip, change, cred, i, &warn);
338                         if (error) {
339                                 /*
340                                  * Roll back user quota changes when
341                                  * group quota failed.
342                                  */
343                                 while (i > 0) {
344                                         --i;
345                                         dq = ip->i_dquot[i];
346                                         if (dq == NODQUOT)
347                                                 continue;
348                                         DQI_LOCK(dq);
349                                         DQI_WAIT(dq, PINOD+1, "chkiq3");
350                                         ncurinodes = dq->dq_curinodes - change;
351                                         /* XXX: ncurinodes is unsigned */
352                                         if (dq->dq_curinodes != 0 &&
353                                             ncurinodes >= 0)
354                                                 dq->dq_curinodes = ncurinodes;
355                                         else
356                                                 dq->dq_curinodes = 0;
357                                         dq->dq_flags &= ~DQ_INODS;
358                                         dq->dq_flags |= DQ_MOD;
359                                         DQI_UNLOCK(dq);
360                                 }
361                                 return (error);
362                         }
363                 }
364                 /* Reset timer when crossing soft limit */
365                 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
366                     dq->dq_curinodes < dq->dq_isoftlimit)
367                         dq->dq_itime = time_second + ip->i_ump->um_itime[i];
368                 dq->dq_curinodes += change;
369                 dq->dq_flags |= DQ_MOD;
370                 DQI_UNLOCK(dq);
371                 if (warn)
372                         uprintf("\n%s: warning, %s %s\n",
373                                 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
374                                 quotatypes[i], "inode quota exceeded");
375         }
376         return (0);
377 }
378
379 /*
380  * Check for a valid change to a users allocation.
381  * Issue an error message if appropriate.
382  */
383 static int
384 chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
385 {
386         struct dquot *dq = ip->i_dquot[type];
387         ino_t ncurinodes = dq->dq_curinodes + change;
388
389         /*
390          * If user would exceed their hard limit, disallow inode allocation.
391          */
392         if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
393                 if ((dq->dq_flags & DQ_INODS) == 0 &&
394                     ip->i_uid == cred->cr_uid) {
395                         dq->dq_flags |= DQ_INODS;
396                         DQI_UNLOCK(dq);
397                         uprintf("\n%s: write failed, %s inode limit reached\n",
398                             ITOV(ip)->v_mount->mnt_stat.f_mntonname,
399                             quotatypes[type]);
400                         return (EDQUOT);
401                 }
402                 DQI_UNLOCK(dq);
403                 return (EDQUOT);
404         }
405         /*
406          * If user is over their soft limit for too long, disallow inode
407          * allocation. Reset time limit as they cross their soft limit.
408          */
409         if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
410                 if (dq->dq_curinodes < dq->dq_isoftlimit) {
411                         dq->dq_itime = time_second + ip->i_ump->um_itime[type];
412                         if (ip->i_uid == cred->cr_uid)
413                                 *warn = 1;
414                         return (0);
415                 }
416                 if (time_second > dq->dq_itime) {
417                         if ((dq->dq_flags & DQ_INODS) == 0 &&
418                             ip->i_uid == cred->cr_uid) {
419                                 dq->dq_flags |= DQ_INODS;
420                                 DQI_UNLOCK(dq);
421                                 uprintf("\n%s: write failed, %s %s\n",
422                                         ITOV(ip)->v_mount->mnt_stat.f_mntonname,
423                                         quotatypes[type],
424                                         "inode quota exceeded for too long");
425                                 return (EDQUOT);
426                         }
427                         DQI_UNLOCK(dq);
428                         return (EDQUOT);
429                 }
430         }
431         return (0);
432 }
433
434 #ifdef DIAGNOSTIC
435 /*
436  * On filesystems with quotas enabled, it is an error for a file to change
437  * size and not to have a dquot structure associated with it.
438  */
439 static void
440 chkdquot(struct inode *ip)
441 {
442         struct ufsmount *ump = ip->i_ump;
443         struct vnode *vp = ITOV(ip);
444         int i;
445
446         /*
447          * Disk quotas must be turned off for system files.  Currently
448          * these are snapshots and quota files.
449          */
450         if ((vp->v_vflag & VV_SYSTEM) != 0)
451                 return;
452         /*
453          * XXX: Turn off quotas for files with a negative UID or GID.
454          * This prevents the creation of 100GB+ quota files.
455          */
456         if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
457                 return;
458
459         UFS_LOCK(ump);
460         for (i = 0; i < MAXQUOTAS; i++) {
461                 if (ump->um_quotas[i] == NULLVP ||
462                     (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
463                         continue;
464                 if (ip->i_dquot[i] == NODQUOT) {
465                         UFS_UNLOCK(ump);
466                         vprint("chkdquot: missing dquot", ITOV(ip));
467                         panic("chkdquot: missing dquot");
468                 }
469         }
470         UFS_UNLOCK(ump);
471 }
472 #endif
473
474 /*
475  * Code to process quotactl commands.
476  */
477
478 /*
479  * Q_QUOTAON - set up a quota file for a particular filesystem.
480  */
481 int
482 quotaon(struct thread *td, struct mount *mp, int type, void *fname)
483 {
484         struct ufsmount *ump;
485         struct vnode *vp, **vpp;
486         struct vnode *mvp;
487         struct dquot *dq;
488         int error, flags, vfslocked;
489         struct nameidata nd;
490
491         error = priv_check(td, PRIV_UFS_QUOTAON);
492         if (error)
493                 return (error);
494
495         ump = VFSTOUFS(mp);
496         dq = NODQUOT;
497
498         NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, fname, td);
499         flags = FREAD | FWRITE;
500         error = vn_open(&nd, &flags, 0, NULL);
501         if (error)
502                 return (error);
503         vfslocked = NDHASGIANT(&nd);
504         NDFREE(&nd, NDF_ONLY_PNBUF);
505         vp = nd.ni_vp;
506         VOP_UNLOCK(vp, 0);
507         if (vp->v_type != VREG) {
508                 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
509                 VFS_UNLOCK_GIANT(vfslocked);
510                 return (EACCES);
511         }
512
513         UFS_LOCK(ump);
514         if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
515                 UFS_UNLOCK(ump);
516                 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
517                 VFS_UNLOCK_GIANT(vfslocked);
518                 return (EALREADY);
519         }
520         ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
521         MNT_ILOCK(mp);
522         mp->mnt_flag |= MNT_QUOTA;
523         MNT_IUNLOCK(mp);
524         UFS_UNLOCK(ump);
525
526         vpp = &ump->um_quotas[type];
527         if (*vpp != vp)
528                 quotaoff1(td, mp, type);
529
530         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
531         vp->v_vflag |= VV_SYSTEM;
532         VOP_UNLOCK(vp, 0);
533         *vpp = vp;
534         VFS_UNLOCK_GIANT(vfslocked);
535         /*
536          * Save the credential of the process that turned on quotas.
537          * Set up the time limits for this quota.
538          */
539         ump->um_cred[type] = crhold(td->td_ucred);
540         ump->um_btime[type] = MAX_DQ_TIME;
541         ump->um_itime[type] = MAX_IQ_TIME;
542         if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
543                 if (dq->dq_btime > 0)
544                         ump->um_btime[type] = dq->dq_btime;
545                 if (dq->dq_itime > 0)
546                         ump->um_itime[type] = dq->dq_itime;
547                 dqrele(NULLVP, dq);
548         }
549         /*
550          * Allow the getdq from getinoquota below to read the quota
551          * from file.
552          */
553         UFS_LOCK(ump);
554         ump->um_qflags[type] &= ~QTF_CLOSING;
555         UFS_UNLOCK(ump);
556         /*
557          * Search vnodes associated with this mount point,
558          * adding references to quota file being opened.
559          * NB: only need to add dquot's for inodes being modified.
560          */
561         MNT_ILOCK(mp);
562 again:
563         MNT_VNODE_FOREACH(vp, mp, mvp) {
564                 VI_LOCK(vp);
565                 MNT_IUNLOCK(mp);
566                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
567                         MNT_ILOCK(mp);
568                         MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
569                         goto again;
570                 }
571                 if (vp->v_type == VNON || vp->v_writecount == 0) {
572                         VOP_UNLOCK(vp, 0);
573                         vrele(vp);
574                         MNT_ILOCK(mp);
575                         continue;
576                 }
577                 error = getinoquota(VTOI(vp));
578                 VOP_UNLOCK(vp, 0);
579                 vrele(vp);
580                 MNT_ILOCK(mp);
581                 if (error) {
582                         MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
583                         break;
584                 }
585         }
586         MNT_IUNLOCK(mp);
587
588         if (error)
589                 quotaoff_inchange(td, mp, type);
590         UFS_LOCK(ump);
591         ump->um_qflags[type] &= ~QTF_OPENING;
592         KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
593                 ("quotaon: leaking flags"));
594         UFS_UNLOCK(ump);
595
596         return (error);
597 }
598
599 /*
600  * Main code to turn off disk quotas for a filesystem. Does not change
601  * flags.
602  */
603 static int
604 quotaoff1(struct thread *td, struct mount *mp, int type)
605 {
606         struct vnode *vp;
607         struct vnode *qvp, *mvp;
608         struct ufsmount *ump;
609         struct dquot *dq;
610         struct inode *ip;
611         struct ucred *cr;
612         int vfslocked;
613         int error;
614
615         ump = VFSTOUFS(mp);
616
617         UFS_LOCK(ump);
618         KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
619                 ("quotaoff1: flags are invalid"));
620         if ((qvp = ump->um_quotas[type]) == NULLVP) {
621                 UFS_UNLOCK(ump);
622                 return (0);
623         }
624         cr = ump->um_cred[type];
625         UFS_UNLOCK(ump);
626
627         /*
628          * Search vnodes associated with this mount point,
629          * deleting any references to quota file being closed.
630          */
631         MNT_ILOCK(mp);
632 again:
633         MNT_VNODE_FOREACH(vp, mp, mvp) {
634                 VI_LOCK(vp);
635                 MNT_IUNLOCK(mp);
636                 if (vp->v_type == VNON) {
637                         VI_UNLOCK(vp);
638                         MNT_ILOCK(mp);
639                         continue;
640                 }
641                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
642                         MNT_ILOCK(mp);
643                         MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
644                         goto again;
645                 }
646                 ip = VTOI(vp);
647                 dq = ip->i_dquot[type];
648                 ip->i_dquot[type] = NODQUOT;
649                 dqrele(vp, dq);
650                 VOP_UNLOCK(vp, 0);
651                 vrele(vp);
652                 MNT_ILOCK(mp);
653         }
654         MNT_IUNLOCK(mp);
655
656         dqflush(qvp);
657         /* Clear um_quotas before closing the quota vnode to prevent
658          * access to the closed vnode from dqget/dqsync
659          */
660         UFS_LOCK(ump);
661         ump->um_quotas[type] = NULLVP;
662         ump->um_cred[type] = NOCRED;
663         UFS_UNLOCK(ump);
664
665         vfslocked = VFS_LOCK_GIANT(qvp->v_mount);
666         vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
667         qvp->v_vflag &= ~VV_SYSTEM;
668         VOP_UNLOCK(qvp, 0);
669         error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
670         VFS_UNLOCK_GIANT(vfslocked);
671         crfree(cr);
672
673         return (error);
674 }
675
676 /*
677  * Turns off quotas, assumes that ump->um_qflags are already checked
678  * and QTF_CLOSING is set to indicate operation in progress. Fixes
679  * ump->um_qflags and mp->mnt_flag after.
680  */
681 int
682 quotaoff_inchange(struct thread *td, struct mount *mp, int type)
683 {
684         struct ufsmount *ump;
685         int i;
686         int error;
687
688         error = quotaoff1(td, mp, type);
689
690         ump = VFSTOUFS(mp);
691         UFS_LOCK(ump);
692         ump->um_qflags[type] &= ~QTF_CLOSING;
693         for (i = 0; i < MAXQUOTAS; i++)
694                 if (ump->um_quotas[i] != NULLVP)
695                         break;
696         if (i == MAXQUOTAS) {
697                 MNT_ILOCK(mp);
698                 mp->mnt_flag &= ~MNT_QUOTA;
699                 MNT_IUNLOCK(mp);
700         }
701         UFS_UNLOCK(ump);
702         return (error);
703 }
704
705 /*
706  * Q_QUOTAOFF - turn off disk quotas for a filesystem.
707  */
708 int
709 quotaoff(struct thread *td, struct mount *mp, int type)
710 {
711         struct ufsmount *ump;
712         int error;
713
714         error = priv_check(td, PRIV_UFS_QUOTAOFF);
715         if (error)
716                 return (error);
717
718         ump = VFSTOUFS(mp);
719         UFS_LOCK(ump);
720         if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
721                 UFS_UNLOCK(ump);
722                 return (EALREADY);
723         }
724         ump->um_qflags[type] |= QTF_CLOSING;
725         UFS_UNLOCK(ump);
726
727         return (quotaoff_inchange(td, mp, type));
728 }
729
730 /*
731  * Q_GETQUOTA - return current values in a dqblk structure.
732  */
733 int
734 getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
735 {
736         struct dquot *dq;
737         int error;
738
739         switch (type) {
740         case USRQUOTA:
741                 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
742                         error = priv_check(td, PRIV_VFS_GETQUOTA);
743                         if (error)
744                                 return (error);
745                 }
746                 break;
747
748         case GRPQUOTA:
749                 if (!groupmember(id, td->td_ucred) &&
750                     !unprivileged_get_quota) {
751                         error = priv_check(td, PRIV_VFS_GETQUOTA);
752                         if (error)
753                                 return (error);
754                 }
755                 break;
756
757         default:
758                 return (EINVAL);
759         }
760
761         dq = NODQUOT;
762         error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
763         if (error)
764                 return (error);
765         error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
766         dqrele(NULLVP, dq);
767         return (error);
768 }
769
770 /*
771  * Q_SETQUOTA - assign an entire dqblk structure.
772  */
773 int
774 setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
775 {
776         struct dquot *dq;
777         struct dquot *ndq;
778         struct ufsmount *ump;
779         struct dqblk newlim;
780         int error;
781
782         error = priv_check(td, PRIV_VFS_SETQUOTA);
783         if (error)
784                 return (error);
785
786         ump = VFSTOUFS(mp);
787         error = copyin(addr, &newlim, sizeof (struct dqblk));
788         if (error)
789                 return (error);
790
791         ndq = NODQUOT;
792         ump = VFSTOUFS(mp);
793
794         error = dqget(NULLVP, id, ump, type, &ndq);
795         if (error)
796                 return (error);
797         dq = ndq;
798         DQI_LOCK(dq);
799         DQI_WAIT(dq, PINOD+1, "setqta");
800         /*
801          * Copy all but the current values.
802          * Reset time limit if previously had no soft limit or were
803          * under it, but now have a soft limit and are over it.
804          */
805         newlim.dqb_curblocks = dq->dq_curblocks;
806         newlim.dqb_curinodes = dq->dq_curinodes;
807         if (dq->dq_id != 0) {
808                 newlim.dqb_btime = dq->dq_btime;
809                 newlim.dqb_itime = dq->dq_itime;
810         }
811         if (newlim.dqb_bsoftlimit &&
812             dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
813             (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
814                 newlim.dqb_btime = time_second + ump->um_btime[type];
815         if (newlim.dqb_isoftlimit &&
816             dq->dq_curinodes >= newlim.dqb_isoftlimit &&
817             (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
818                 newlim.dqb_itime = time_second + ump->um_itime[type];
819         dq->dq_dqb = newlim;
820         if (dq->dq_curblocks < dq->dq_bsoftlimit)
821                 dq->dq_flags &= ~DQ_BLKS;
822         if (dq->dq_curinodes < dq->dq_isoftlimit)
823                 dq->dq_flags &= ~DQ_INODS;
824         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
825             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
826                 dq->dq_flags |= DQ_FAKE;
827         else
828                 dq->dq_flags &= ~DQ_FAKE;
829         dq->dq_flags |= DQ_MOD;
830         DQI_UNLOCK(dq);
831         dqrele(NULLVP, dq);
832         return (0);
833 }
834
835 /*
836  * Q_SETUSE - set current inode and block usage.
837  */
838 int
839 setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
840 {
841         struct dquot *dq;
842         struct ufsmount *ump;
843         struct dquot *ndq;
844         struct dqblk usage;
845         int error;
846
847         error = priv_check(td, PRIV_UFS_SETUSE);
848         if (error)
849                 return (error);
850
851         ump = VFSTOUFS(mp);
852         error = copyin(addr, &usage, sizeof (struct dqblk));
853         if (error)
854                 return (error);
855
856         ump = VFSTOUFS(mp);
857         ndq = NODQUOT;
858
859         error = dqget(NULLVP, id, ump, type, &ndq);
860         if (error)
861                 return (error);
862         dq = ndq;
863         DQI_LOCK(dq);
864         DQI_WAIT(dq, PINOD+1, "setuse");
865         /*
866          * Reset time limit if have a soft limit and were
867          * previously under it, but are now over it.
868          */
869         if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
870             usage.dqb_curblocks >= dq->dq_bsoftlimit)
871                 dq->dq_btime = time_second + ump->um_btime[type];
872         if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
873             usage.dqb_curinodes >= dq->dq_isoftlimit)
874                 dq->dq_itime = time_second + ump->um_itime[type];
875         dq->dq_curblocks = usage.dqb_curblocks;
876         dq->dq_curinodes = usage.dqb_curinodes;
877         if (dq->dq_curblocks < dq->dq_bsoftlimit)
878                 dq->dq_flags &= ~DQ_BLKS;
879         if (dq->dq_curinodes < dq->dq_isoftlimit)
880                 dq->dq_flags &= ~DQ_INODS;
881         dq->dq_flags |= DQ_MOD;
882         DQI_UNLOCK(dq);
883         dqrele(NULLVP, dq);
884         return (0);
885 }
886
887 /*
888  * Q_SYNC - sync quota files to disk.
889  */
890 int
891 qsync(struct mount *mp)
892 {
893         struct ufsmount *ump = VFSTOUFS(mp);
894         struct thread *td = curthread;          /* XXX */
895         struct vnode *vp, *mvp;
896         struct dquot *dq;
897         int i, error;
898
899         /*
900          * Check if the mount point has any quotas.
901          * If not, simply return.
902          */
903         UFS_LOCK(ump);
904         for (i = 0; i < MAXQUOTAS; i++)
905                 if (ump->um_quotas[i] != NULLVP)
906                         break;
907         UFS_UNLOCK(ump);
908         if (i == MAXQUOTAS)
909                 return (0);
910         /*
911          * Search vnodes associated with this mount point,
912          * synchronizing any modified dquot structures.
913          */
914         MNT_ILOCK(mp);
915 again:
916         MNT_VNODE_FOREACH(vp, mp, mvp) {
917                 VI_LOCK(vp);
918                 if (vp->v_type == VNON) {
919                         VI_UNLOCK(vp);
920                         continue;
921                 }
922                 MNT_IUNLOCK(mp);
923                 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
924                 if (error) {
925                         MNT_ILOCK(mp);
926                         if (error == ENOENT) {
927                                 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
928                                 goto again;
929                         }
930                         continue;
931                 }
932                 for (i = 0; i < MAXQUOTAS; i++) {
933                         dq = VTOI(vp)->i_dquot[i];
934                         if (dq != NODQUOT)
935                                 dqsync(vp, dq);
936                 }
937                 vput(vp);
938                 MNT_ILOCK(mp);
939         }
940         MNT_IUNLOCK(mp);
941         return (0);
942 }
943
944 /*
945  * Code pertaining to management of the in-core dquot data structures.
946  */
947 #define DQHASH(dqvp, id) \
948         (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
949 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
950 static u_long dqhash;
951
952 /*
953  * Dquot free list.
954  */
955 #define DQUOTINC        5       /* minimum free dquots desired */
956 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
957 static long numdquot, desireddquot = DQUOTINC;
958
959 /*
960  * Lock to protect quota hash, dq free list and dq_cnt ref counters of
961  * _all_ dqs.
962  */
963 struct mtx dqhlock;
964
965 #define DQH_LOCK()      mtx_lock(&dqhlock)
966 #define DQH_UNLOCK()    mtx_unlock(&dqhlock)
967
968 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
969         struct vnode *dqvp);
970
971 /*
972  * Initialize the quota system.
973  */
974 void
975 dqinit(void)
976 {
977
978         mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
979         dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
980         TAILQ_INIT(&dqfreelist);
981 }
982
983 /*
984  * Shut down the quota system.
985  */
986 void
987 dquninit(void)
988 {
989         struct dquot *dq;
990
991         hashdestroy(dqhashtbl, M_DQUOT, dqhash);
992         while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
993                 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
994                 mtx_destroy(&dq->dq_lock);
995                 free(dq, M_DQUOT);
996         }
997         mtx_destroy(&dqhlock);
998 }
999
1000 static struct dquot *
1001 dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1002 {
1003         struct dquot *dq;
1004
1005         mtx_assert(&dqhlock, MA_OWNED);
1006         LIST_FOREACH(dq, dqh, dq_hash) {
1007                 if (dq->dq_id != id ||
1008                     dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1009                         continue;
1010                 /*
1011                  * Cache hit with no references.  Take
1012                  * the structure off the free list.
1013                  */
1014                 if (dq->dq_cnt == 0)
1015                         TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1016                 DQREF(dq);
1017                 return (dq);
1018         }
1019         return (NODQUOT);
1020 }
1021
1022 /*
1023  * Obtain a dquot structure for the specified identifier and quota file
1024  * reading the information from the file if necessary.
1025  */
1026 static int
1027 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1028     struct dquot **dqp)
1029 {
1030         struct dquot *dq, *dq1;
1031         struct dqhash *dqh;
1032         struct vnode *dqvp;
1033         struct iovec aiov;
1034         struct uio auio;
1035         int vfslocked, dqvplocked, error;
1036
1037 #ifdef DEBUG_VFS_LOCKS
1038         if (vp != NULLVP)
1039                 ASSERT_VOP_ELOCKED(vp, "dqget");
1040 #endif
1041
1042         if (vp != NULLVP && *dqp != NODQUOT) {
1043                 return (0);
1044         }
1045
1046         /* XXX: Disallow negative id values to prevent the
1047         * creation of 100GB+ quota data files.
1048         */
1049         if ((int)id < 0)
1050                 return (EINVAL);
1051
1052         UFS_LOCK(ump);
1053         dqvp = ump->um_quotas[type];
1054         if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1055                 *dqp = NODQUOT;
1056                 UFS_UNLOCK(ump);
1057                 return (EINVAL);
1058         }
1059         vref(dqvp);
1060         UFS_UNLOCK(ump);
1061         error = 0;
1062         dqvplocked = 0;
1063
1064         /*
1065          * Check the cache first.
1066          */
1067         dqh = DQHASH(dqvp, id);
1068         DQH_LOCK();
1069         dq = dqhashfind(dqh, id, dqvp);
1070         if (dq != NULL) {
1071                 DQH_UNLOCK();
1072 hfound:         DQI_LOCK(dq);
1073                 DQI_WAIT(dq, PINOD+1, "dqget");
1074                 DQI_UNLOCK(dq);
1075                 if (dq->dq_ump == NULL) {
1076                         dqrele(vp, dq);
1077                         dq = NODQUOT;
1078                         error = EIO;
1079                 }
1080                 *dqp = dq;
1081                 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1082                 if (dqvplocked)
1083                         vput(dqvp);
1084                 else
1085                         vrele(dqvp);
1086                 VFS_UNLOCK_GIANT(vfslocked);
1087                 return (error);
1088         }
1089
1090         /*
1091          * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1092          * since new dq will appear on the hash chain DQ_LOCKed.
1093          */
1094         if (vp != dqvp) {
1095                 DQH_UNLOCK();
1096                 vn_lock(dqvp, LK_SHARED | LK_RETRY);
1097                 dqvplocked = 1;
1098                 DQH_LOCK();
1099                 /*
1100                  * Recheck the cache after sleep for quota vnode lock.
1101                  */
1102                 dq = dqhashfind(dqh, id, dqvp);
1103                 if (dq != NULL) {
1104                         DQH_UNLOCK();
1105                         goto hfound;
1106                 }
1107         }
1108
1109         /*
1110          * Not in cache, allocate a new one or take it from the
1111          * free list.
1112          */
1113         if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1114             numdquot < MAXQUOTAS * desiredvnodes)
1115                 desireddquot += DQUOTINC;
1116         if (numdquot < desireddquot) {
1117                 numdquot++;
1118                 DQH_UNLOCK();
1119                 dq1 = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
1120                     M_WAITOK | M_ZERO);
1121                 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1122                 DQH_LOCK();
1123                 /*
1124                  * Recheck the cache after sleep for memory.
1125                  */
1126                 dq = dqhashfind(dqh, id, dqvp);
1127                 if (dq != NULL) {
1128                         numdquot--;
1129                         DQH_UNLOCK();
1130                         mtx_destroy(&dq1->dq_lock);
1131                         free(dq1, M_DQUOT);
1132                         goto hfound;
1133                 }
1134                 dq = dq1;
1135         } else {
1136                 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1137                         DQH_UNLOCK();
1138                         tablefull("dquot");
1139                         *dqp = NODQUOT;
1140                         vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1141                         if (dqvplocked)
1142                                 vput(dqvp);
1143                         else
1144                                 vrele(dqvp);
1145                         VFS_UNLOCK_GIANT(vfslocked);
1146                         return (EUSERS);
1147                 }
1148                 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1149                         panic("dqget: free dquot isn't %p", dq);
1150                 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1151                 if (dq->dq_ump != NULL)
1152                         LIST_REMOVE(dq, dq_hash);
1153         }
1154
1155         /*
1156          * Dq is put into hash already locked to prevent parallel
1157          * usage while it is being read from file.
1158          */
1159         dq->dq_flags = DQ_LOCK;
1160         dq->dq_id = id;
1161         dq->dq_type = type;
1162         dq->dq_ump = ump;
1163         LIST_INSERT_HEAD(dqh, dq, dq_hash);
1164         DQREF(dq);
1165         DQH_UNLOCK();
1166
1167         auio.uio_iov = &aiov;
1168         auio.uio_iovcnt = 1;
1169         aiov.iov_base = &dq->dq_dqb;
1170         aiov.iov_len = sizeof (struct dqblk);
1171         auio.uio_resid = sizeof (struct dqblk);
1172         auio.uio_offset = (off_t)id * sizeof (struct dqblk);
1173         auio.uio_segflg = UIO_SYSSPACE;
1174         auio.uio_rw = UIO_READ;
1175         auio.uio_td = (struct thread *)0;
1176
1177         vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1178         error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1179         if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
1180                 bzero(&dq->dq_dqb, sizeof(struct dqblk));
1181         if (dqvplocked)
1182                 vput(dqvp);
1183         else
1184                 vrele(dqvp);
1185         VFS_UNLOCK_GIANT(vfslocked);
1186         /*
1187          * I/O error in reading quota file, release
1188          * quota structure and reflect problem to caller.
1189          */
1190         if (error) {
1191                 DQH_LOCK();
1192                 dq->dq_ump = NULL;
1193                 LIST_REMOVE(dq, dq_hash);
1194                 DQH_UNLOCK();
1195                 DQI_LOCK(dq);
1196                 if (dq->dq_flags & DQ_WANT)
1197                         wakeup(dq);
1198                 dq->dq_flags = 0;
1199                 DQI_UNLOCK(dq);
1200                 dqrele(vp, dq);
1201                 *dqp = NODQUOT;
1202                 return (error);
1203         }
1204         DQI_LOCK(dq);
1205         /*
1206          * Check for no limit to enforce.
1207          * Initialize time values if necessary.
1208          */
1209         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1210             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1211                 dq->dq_flags |= DQ_FAKE;
1212         if (dq->dq_id != 0) {
1213                 if (dq->dq_btime == 0) {
1214                         dq->dq_btime = time_second + ump->um_btime[type];
1215                         if (dq->dq_bsoftlimit &&
1216                             dq->dq_curblocks >= dq->dq_bsoftlimit)
1217                                 dq->dq_flags |= DQ_MOD;
1218                 }
1219                 if (dq->dq_itime == 0) {
1220                         dq->dq_itime = time_second + ump->um_itime[type];
1221                         if (dq->dq_isoftlimit &&
1222                             dq->dq_curinodes >= dq->dq_isoftlimit)
1223                                 dq->dq_flags |= DQ_MOD;
1224                 }
1225         }
1226         DQI_WAKEUP(dq);
1227         DQI_UNLOCK(dq);
1228         *dqp = dq;
1229         return (0);
1230 }
1231
1232 #ifdef DIAGNOSTIC
1233 /*
1234  * Obtain a reference to a dquot.
1235  */
1236 static void
1237 dqref(struct dquot *dq)
1238 {
1239
1240         dq->dq_cnt++;
1241 }
1242 #endif
1243
1244 /*
1245  * Release a reference to a dquot.
1246  */
1247 void
1248 dqrele(struct vnode *vp, struct dquot *dq)
1249 {
1250
1251         if (dq == NODQUOT)
1252                 return;
1253         DQH_LOCK();
1254         if (dq->dq_cnt > 1) {
1255                 dq->dq_cnt--;
1256                 DQH_UNLOCK();
1257                 return;
1258         }
1259         DQH_UNLOCK();
1260 sync:
1261         (void) dqsync(vp, dq);
1262
1263         DQH_LOCK();
1264         if (--dq->dq_cnt > 0)
1265         {
1266                 DQH_UNLOCK();
1267                 return;
1268         }
1269
1270         /*
1271          * The dq may become dirty after it is synced but before it is
1272          * put to the free list. Checking the DQ_MOD there without
1273          * locking dq should be safe since no other references to the
1274          * dq exist.
1275          */
1276         if ((dq->dq_flags & DQ_MOD) != 0) {
1277                 dq->dq_cnt++;
1278                 DQH_UNLOCK();
1279                 goto sync;
1280         }
1281         TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1282         DQH_UNLOCK();
1283 }
1284
1285 /*
1286  * Update the disk quota in the quota file.
1287  */
1288 static int
1289 dqsync(struct vnode *vp, struct dquot *dq)
1290 {
1291         struct vnode *dqvp;
1292         struct iovec aiov;
1293         struct uio auio;
1294         int vfslocked, error;
1295         struct mount *mp;
1296         struct ufsmount *ump;
1297
1298 #ifdef DEBUG_VFS_LOCKS
1299         if (vp != NULL)
1300                 ASSERT_VOP_ELOCKED(vp, "dqsync");
1301 #endif
1302
1303         mp = NULL;
1304         error = 0;
1305         if (dq == NODQUOT)
1306                 panic("dqsync: dquot");
1307         if ((ump = dq->dq_ump) == NULL)
1308                 return (0);
1309         UFS_LOCK(ump);
1310         if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP)
1311                 panic("dqsync: file");
1312         vref(dqvp);
1313         UFS_UNLOCK(ump);
1314
1315         vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1316         DQI_LOCK(dq);
1317         if ((dq->dq_flags & DQ_MOD) == 0) {
1318                 DQI_UNLOCK(dq);
1319                 vrele(dqvp);
1320                 VFS_UNLOCK_GIANT(vfslocked);
1321                 return (0);
1322         }
1323         DQI_UNLOCK(dq);
1324
1325         (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1326         if (vp != dqvp)
1327                 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1328
1329         VFS_UNLOCK_GIANT(vfslocked);
1330         DQI_LOCK(dq);
1331         DQI_WAIT(dq, PINOD+2, "dqsync");
1332         if ((dq->dq_flags & DQ_MOD) == 0)
1333                 goto out;
1334         dq->dq_flags |= DQ_LOCK;
1335         DQI_UNLOCK(dq);
1336
1337         auio.uio_iov = &aiov;
1338         auio.uio_iovcnt = 1;
1339         aiov.iov_base = &dq->dq_dqb;
1340         aiov.iov_len = sizeof (struct dqblk);
1341         auio.uio_resid = sizeof (struct dqblk);
1342         auio.uio_offset = (off_t)dq->dq_id * sizeof (struct dqblk);
1343         auio.uio_segflg = UIO_SYSSPACE;
1344         auio.uio_rw = UIO_WRITE;
1345         auio.uio_td = (struct thread *)0;
1346         vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1347         error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1348         VFS_UNLOCK_GIANT(vfslocked);
1349         if (auio.uio_resid && error == 0)
1350                 error = EIO;
1351
1352         DQI_LOCK(dq);
1353         DQI_WAKEUP(dq);
1354         dq->dq_flags &= ~DQ_MOD;
1355 out:    DQI_UNLOCK(dq);
1356         vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1357         if (vp != dqvp)
1358                 vput(dqvp);
1359         else
1360                 vrele(dqvp);
1361         vn_finished_secondary_write(mp);
1362         VFS_UNLOCK_GIANT(vfslocked);
1363         return (error);
1364 }
1365
1366 /*
1367  * Flush all entries from the cache for a particular vnode.
1368  */
1369 static void
1370 dqflush(struct vnode *vp)
1371 {
1372         struct dquot *dq, *nextdq;
1373         struct dqhash *dqh;
1374
1375         /*
1376          * Move all dquot's that used to refer to this quota
1377          * file off their hash chains (they will eventually
1378          * fall off the head of the free list and be re-used).
1379          */
1380         DQH_LOCK();
1381         for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1382                 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1383                         nextdq = LIST_NEXT(dq, dq_hash);
1384                         if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1385                                 continue;
1386                         if (dq->dq_cnt)
1387                                 panic("dqflush: stray dquot");
1388                         LIST_REMOVE(dq, dq_hash);
1389                         dq->dq_ump = (struct ufsmount *)0;
1390                 }
1391         }
1392         DQH_UNLOCK();
1393 }