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