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