]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ufs/ufs/ufs_quota.c
Use mount interlock to protect all changes to mnt_flag and mnt_kern_flag.
[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/proc.h>
50 #include <sys/socket.h>
51 #include <sys/stat.h>
52 #include <sys/sysctl.h>
53 #include <sys/vnode.h>
54
55 #include <ufs/ufs/extattr.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60
61 static int unprivileged_get_quota = 0;
62 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
63     &unprivileged_get_quota, 0,
64     "Unprivileged processes may retrieve quotas for other uids and gids");
65
66 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
67
68 /*
69  * Quota name to error message mapping.
70  */
71 static char *quotatypes[] = INITQFNAMES;
72
73 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int);
74 static int chkiqchg(struct inode *, ino_t, struct ucred *, int);
75 static int dqget(struct vnode *,
76                 u_long, struct ufsmount *, int, struct dquot **);
77 static int dqsync(struct vnode *, struct dquot *);
78 static void dqflush(struct vnode *);
79
80 #ifdef DIAGNOSTIC
81 static void dqref(struct dquot *);
82 static void chkdquot(struct inode *);
83 #endif
84
85 /*
86  * Set up the quotas for an inode.
87  *
88  * This routine completely defines the semantics of quotas.
89  * If other criterion want to be used to establish quotas, the
90  * MAXQUOTAS value in quotas.h should be increased, and the
91  * additional dquots set up here.
92  */
93 int
94 getinoquota(ip)
95         struct inode *ip;
96 {
97         struct ufsmount *ump;
98         struct vnode *vp = ITOV(ip);
99         int error;
100
101 #ifndef NO_FFS_SNAPSHOT
102         /*
103          * Disk quotas must be turned off for snapshot files.
104          */
105         if ((ip->i_flags & SF_SNAPSHOT) != 0)
106                 return (0);
107 #endif
108         ump = VFSTOUFS(vp->v_mount);
109         /*
110          * Set up the user quota based on file uid.
111          * EINVAL means that quotas are not enabled.
112          */
113         if (ip->i_dquot[USRQUOTA] == NODQUOT &&
114             (error =
115                 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
116             error != EINVAL)
117                 return (error);
118         /*
119          * Set up the group quota based on file gid.
120          * EINVAL means that quotas are not enabled.
121          */
122         if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
123             (error =
124                 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
125             error != EINVAL)
126                 return (error);
127         return (0);
128 }
129
130 /*
131  * Update disk usage, and take corrective action.
132  */
133 int
134 chkdq(ip, change, cred, flags)
135         struct inode *ip;
136         ufs2_daddr_t change;
137         struct ucred *cred;
138         int flags;
139 {
140         struct dquot *dq;
141         ufs2_daddr_t ncurblocks;
142         int i, error;
143
144 #ifdef DIAGNOSTIC
145         if ((flags & CHOWN) == 0)
146                 chkdquot(ip);
147 #endif
148         if (change == 0)
149                 return (0);
150         if (change < 0) {
151                 for (i = 0; i < MAXQUOTAS; i++) {
152                         if ((dq = ip->i_dquot[i]) == NODQUOT)
153                                 continue;
154                         while (dq->dq_flags & DQ_LOCK) {
155                                 dq->dq_flags |= DQ_WANT;
156                                 (void) tsleep(dq, PINOD+1, "chkdq1", 0);
157                         }
158                         ncurblocks = dq->dq_curblocks + change;
159                         if (ncurblocks >= 0)
160                                 dq->dq_curblocks = ncurblocks;
161                         else
162                                 dq->dq_curblocks = 0;
163                         dq->dq_flags &= ~DQ_BLKS;
164                         dq->dq_flags |= DQ_MOD;
165                 }
166                 return (0);
167         }
168         if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
169                 for (i = 0; i < MAXQUOTAS; i++) {
170                         if ((dq = ip->i_dquot[i]) == NODQUOT)
171                                 continue;
172                         error = chkdqchg(ip, change, cred, i);
173                         if (error)
174                                 return (error);
175                 }
176         }
177         for (i = 0; i < MAXQUOTAS; i++) {
178                 if ((dq = ip->i_dquot[i]) == NODQUOT)
179                         continue;
180                 while (dq->dq_flags & DQ_LOCK) {
181                         dq->dq_flags |= DQ_WANT;
182                         (void) tsleep(dq, PINOD+1, "chkdq2", 0);
183                 }
184                 /* Reset timer when crossing soft limit */
185                 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
186                     dq->dq_curblocks < dq->dq_bsoftlimit)
187                         dq->dq_btime = time_second +
188                             VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
189                 dq->dq_curblocks += change;
190                 dq->dq_flags |= DQ_MOD;
191         }
192         return (0);
193 }
194
195 /*
196  * Check for a valid change to a users allocation.
197  * Issue an error message if appropriate.
198  */
199 static int
200 chkdqchg(ip, change, cred, type)
201         struct inode *ip;
202         ufs2_daddr_t change;
203         struct ucred *cred;
204         int type;
205 {
206         struct dquot *dq = ip->i_dquot[type];
207         ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
208
209         /*
210          * If user would exceed their hard limit, disallow space allocation.
211          */
212         if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
213                 if ((dq->dq_flags & DQ_BLKS) == 0 &&
214                     ip->i_uid == cred->cr_uid) {
215                         uprintf("\n%s: write failed, %s disk limit reached\n",
216                             ITOV(ip)->v_mount->mnt_stat.f_mntonname,
217                             quotatypes[type]);
218                         dq->dq_flags |= DQ_BLKS;
219                 }
220                 return (EDQUOT);
221         }
222         /*
223          * If user is over their soft limit for too long, disallow space
224          * allocation. Reset time limit as they cross their soft limit.
225          */
226         if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
227                 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
228                         dq->dq_btime = time_second +
229                             VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
230                         if (ip->i_uid == cred->cr_uid)
231                                 uprintf("\n%s: warning, %s %s\n",
232                                     ITOV(ip)->v_mount->mnt_stat.f_mntonname,
233                                     quotatypes[type], "disk quota exceeded");
234                         return (0);
235                 }
236                 if (time_second > dq->dq_btime) {
237                         if ((dq->dq_flags & DQ_BLKS) == 0 &&
238                             ip->i_uid == cred->cr_uid) {
239                                 uprintf("\n%s: write failed, %s %s\n",
240                                     ITOV(ip)->v_mount->mnt_stat.f_mntonname,
241                                     quotatypes[type],
242                                     "disk quota exceeded for too long");
243                                 dq->dq_flags |= DQ_BLKS;
244                         }
245                         return (EDQUOT);
246                 }
247         }
248         return (0);
249 }
250
251 /*
252  * Check the inode limit, applying corrective action.
253  */
254 int
255 chkiq(ip, change, cred, flags)
256         struct inode *ip;
257         ino_t change;
258         struct ucred *cred;
259         int flags;
260 {
261         struct dquot *dq;
262         ino_t ncurinodes;
263         int i, error;
264
265 #ifdef DIAGNOSTIC
266         if ((flags & CHOWN) == 0)
267                 chkdquot(ip);
268 #endif
269         if (change == 0)
270                 return (0);
271         /* XXX: change is unsigned */
272         if (change < 0) {
273                 for (i = 0; i < MAXQUOTAS; i++) {
274                         if ((dq = ip->i_dquot[i]) == NODQUOT)
275                                 continue;
276                         while (dq->dq_flags & DQ_LOCK) {
277                                 dq->dq_flags |= DQ_WANT;
278                                 (void) tsleep(dq, PINOD+1, "chkiq1", 0);
279                         }
280                         ncurinodes = dq->dq_curinodes + change;
281                         /* XXX: ncurinodes is unsigned */
282                         if (ncurinodes >= 0)
283                                 dq->dq_curinodes = ncurinodes;
284                         else
285                                 dq->dq_curinodes = 0;
286                         dq->dq_flags &= ~DQ_INODS;
287                         dq->dq_flags |= DQ_MOD;
288                 }
289                 return (0);
290         }
291         if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
292                 for (i = 0; i < MAXQUOTAS; i++) {
293                         if ((dq = ip->i_dquot[i]) == NODQUOT)
294                                 continue;
295                         error = chkiqchg(ip, change, cred, i);
296                         if (error)
297                                 return (error);
298                 }
299         }
300         for (i = 0; i < MAXQUOTAS; i++) {
301                 if ((dq = ip->i_dquot[i]) == NODQUOT)
302                         continue;
303                 while (dq->dq_flags & DQ_LOCK) {
304                         dq->dq_flags |= DQ_WANT;
305                         (void) tsleep(dq, PINOD+1, "chkiq2", 0);
306                 }
307                 /* Reset timer when crossing soft limit */
308                 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
309                     dq->dq_curinodes < dq->dq_isoftlimit)
310                         dq->dq_itime = time_second +
311                             VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
312                 dq->dq_curinodes += change;
313                 dq->dq_flags |= DQ_MOD;
314         }
315         return (0);
316 }
317
318 /*
319  * Check for a valid change to a users allocation.
320  * Issue an error message if appropriate.
321  */
322 static int
323 chkiqchg(ip, change, cred, type)
324         struct inode *ip;
325         ino_t change;
326         struct ucred *cred;
327         int type;
328 {
329         struct dquot *dq = ip->i_dquot[type];
330         ino_t ncurinodes = dq->dq_curinodes + change;
331
332         /*
333          * If user would exceed their hard limit, disallow inode allocation.
334          */
335         if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
336                 if ((dq->dq_flags & DQ_INODS) == 0 &&
337                     ip->i_uid == cred->cr_uid) {
338                         uprintf("\n%s: write failed, %s inode limit reached\n",
339                             ITOV(ip)->v_mount->mnt_stat.f_mntonname,
340                             quotatypes[type]);
341                         dq->dq_flags |= DQ_INODS;
342                 }
343                 return (EDQUOT);
344         }
345         /*
346          * If user is over their soft limit for too long, disallow inode
347          * allocation. Reset time limit as they cross their soft limit.
348          */
349         if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
350                 if (dq->dq_curinodes < dq->dq_isoftlimit) {
351                         dq->dq_itime = time_second +
352                             VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
353                         if (ip->i_uid == cred->cr_uid)
354                                 uprintf("\n%s: warning, %s %s\n",
355                                     ITOV(ip)->v_mount->mnt_stat.f_mntonname,
356                                     quotatypes[type], "inode quota exceeded");
357                         return (0);
358                 }
359                 if (time_second > dq->dq_itime) {
360                         if ((dq->dq_flags & DQ_INODS) == 0 &&
361                             ip->i_uid == cred->cr_uid) {
362                                 uprintf("\n%s: write failed, %s %s\n",
363                                     ITOV(ip)->v_mount->mnt_stat.f_mntonname,
364                                     quotatypes[type],
365                                     "inode quota exceeded for too long");
366                                 dq->dq_flags |= DQ_INODS;
367                         }
368                         return (EDQUOT);
369                 }
370         }
371         return (0);
372 }
373
374 #ifdef DIAGNOSTIC
375 /*
376  * On filesystems with quotas enabled, it is an error for a file to change
377  * size and not to have a dquot structure associated with it.
378  */
379 static void
380 chkdquot(ip)
381         struct inode *ip;
382 {
383         struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
384         int i;
385
386 #ifndef NO_FFS_SNAPSHOT
387         /*
388          * Disk quotas must be turned off for snapshot files.
389          */
390         if ((ip->i_flags & SF_SNAPSHOT) != 0)
391                 return;
392 #endif
393         for (i = 0; i < MAXQUOTAS; i++) {
394                 if (ump->um_quotas[i] == NULLVP ||
395                     (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
396                         continue;
397                 if (ip->i_dquot[i] == NODQUOT) {
398                         vprint("chkdquot: missing dquot", ITOV(ip));
399                         panic("chkdquot: missing dquot");
400                 }
401         }
402 }
403 #endif
404
405 /*
406  * Code to process quotactl commands.
407  */
408
409 /*
410  * Q_QUOTAON - set up a quota file for a particular filesystem.
411  */
412 int
413 quotaon(td, mp, type, fname)
414         struct thread *td;
415         struct mount *mp;
416         int type;
417         void *fname;
418 {
419         struct ufsmount *ump = VFSTOUFS(mp);
420         struct vnode *vp, **vpp;
421         struct vnode *mvp;
422         struct dquot *dq;
423         int error, flags;
424         struct nameidata nd;
425
426         error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
427         if (error)
428                 return (error);
429
430         vpp = &ump->um_quotas[type];
431         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
432         flags = FREAD | FWRITE;
433         error = vn_open(&nd, &flags, 0, -1);
434         if (error)
435                 return (error);
436         NDFREE(&nd, NDF_ONLY_PNBUF);
437         vp = nd.ni_vp;
438         VOP_UNLOCK(vp, 0, td);
439         if (vp->v_type != VREG) {
440                 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
441                 return (EACCES);
442         }
443         if (*vpp != vp)
444                 quotaoff(td, mp, type);
445         ump->um_qflags[type] |= QTF_OPENING;
446         MNT_ILOCK(mp);
447         mp->mnt_flag |= MNT_QUOTA;
448         MNT_IUNLOCK(mp);
449         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
450         vp->v_vflag |= VV_SYSTEM;
451         VOP_UNLOCK(vp, 0, td);
452         *vpp = vp;
453         /*
454          * Save the credential of the process that turned on quotas.
455          * Set up the time limits for this quota.
456          */
457         ump->um_cred[type] = crhold(td->td_ucred);
458         ump->um_btime[type] = MAX_DQ_TIME;
459         ump->um_itime[type] = MAX_IQ_TIME;
460         if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
461                 if (dq->dq_btime > 0)
462                         ump->um_btime[type] = dq->dq_btime;
463                 if (dq->dq_itime > 0)
464                         ump->um_itime[type] = dq->dq_itime;
465                 dqrele(NULLVP, dq);
466         }
467         /*
468          * Search vnodes associated with this mount point,
469          * adding references to quota file being opened.
470          * NB: only need to add dquot's for inodes being modified.
471          */
472         MNT_ILOCK(mp);
473 again:
474         MNT_VNODE_FOREACH(vp, mp, mvp) {
475                 VI_LOCK(vp);
476                 MNT_IUNLOCK(mp);
477                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
478                         MNT_ILOCK(mp);
479                         MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
480                         goto again;
481                 }
482                 if (vp->v_type == VNON || vp->v_writecount == 0) {
483                         VOP_UNLOCK(vp, 0, td);
484                         vrele(vp);
485                         MNT_ILOCK(mp);
486                         continue;
487                 }
488                 error = getinoquota(VTOI(vp));
489                 VOP_UNLOCK(vp, 0, td);
490                 vrele(vp);
491                 MNT_ILOCK(mp);
492                 if (error) {
493                         MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
494                         break;
495                 }
496         }
497         MNT_IUNLOCK(mp);
498         ump->um_qflags[type] &= ~QTF_OPENING;
499         if (error)
500                 quotaoff(td, mp, type);
501         return (error);
502 }
503
504 /*
505  * Q_QUOTAOFF - turn off disk quotas for a filesystem.
506  */
507 int
508 quotaoff(td, mp, type)
509         struct thread *td;
510         struct mount *mp;
511         int type;
512 {
513         struct vnode *vp;
514         struct vnode *qvp, *mvp;
515         struct ufsmount *ump = VFSTOUFS(mp);
516         struct dquot *dq;
517         struct inode *ip;
518         int error;
519
520         error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
521         if (error)
522                 return (error);
523
524         if ((qvp = ump->um_quotas[type]) == NULLVP)
525                 return (0);
526         ump->um_qflags[type] |= QTF_CLOSING;
527         /*
528          * Search vnodes associated with this mount point,
529          * deleting any references to quota file being closed.
530          */
531         MNT_ILOCK(mp);
532 again:
533         MNT_VNODE_FOREACH(vp, mp, mvp) {
534                 VI_LOCK(vp);
535                 MNT_IUNLOCK(mp);
536                 if (vp->v_type == VNON) {
537                         VI_UNLOCK(vp);
538                         MNT_ILOCK(mp);
539                         continue;
540                 }
541                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
542                         MNT_ILOCK(mp);
543                         MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
544                         goto again;
545                 }
546                 ip = VTOI(vp);
547                 dq = ip->i_dquot[type];
548                 ip->i_dquot[type] = NODQUOT;
549                 dqrele(vp, dq);
550                 VOP_UNLOCK(vp, 0, td);
551                 vrele(vp);
552                 MNT_ILOCK(mp);
553         }
554         MNT_IUNLOCK(mp);
555         dqflush(qvp);
556         vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY, td);
557         qvp->v_vflag &= ~VV_SYSTEM;
558         VOP_UNLOCK(qvp, 0, td);
559         error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
560         ump->um_quotas[type] = NULLVP;
561         crfree(ump->um_cred[type]);
562         ump->um_cred[type] = NOCRED;
563         ump->um_qflags[type] &= ~QTF_CLOSING;
564         for (type = 0; type < MAXQUOTAS; type++)
565                 if (ump->um_quotas[type] != NULLVP)
566                         break;
567         if (type == MAXQUOTAS) {
568                 MNT_ILOCK(mp);
569                 mp->mnt_flag &= ~MNT_QUOTA;
570                 MNT_IUNLOCK(mp);
571         }
572         return (error);
573 }
574
575 /*
576  * Q_GETQUOTA - return current values in a dqblk structure.
577  */
578 int
579 getquota(td, mp, id, type, addr)
580         struct thread *td;
581         struct mount *mp;
582         u_long id;
583         int type;
584         void *addr;
585 {
586         struct dquot *dq;
587         int error;
588
589         switch (type) {
590         case USRQUOTA:
591                 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
592                         error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
593                         if (error)
594                                 return (error);
595                 }
596                 break;  
597
598         case GRPQUOTA:
599                 if (!groupmember(id, td->td_ucred) && !unprivileged_get_quota) {
600                         error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
601                         if (error)
602                                 return (error);
603                 }
604                 break;
605
606         default:
607                 return (EINVAL);
608         }
609
610         error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
611         if (error)
612                 return (error);
613         error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
614         dqrele(NULLVP, dq);
615         return (error);
616 }
617
618 /*
619  * Q_SETQUOTA - assign an entire dqblk structure.
620  */
621 int
622 setquota(td, mp, id, type, addr)
623         struct thread *td;
624         struct mount *mp;
625         u_long id;
626         int type;
627         void *addr;
628 {
629         struct dquot *dq;
630         struct dquot *ndq;
631         struct ufsmount *ump = VFSTOUFS(mp);
632         struct dqblk newlim;
633         int error;
634
635         error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
636         if (error)
637                 return (error);
638
639         error = copyin(addr, &newlim, sizeof (struct dqblk));
640         if (error)
641                 return (error);
642         error = dqget(NULLVP, id, ump, type, &ndq);
643         if (error)
644                 return (error);
645         dq = ndq;
646         while (dq->dq_flags & DQ_LOCK) {
647                 dq->dq_flags |= DQ_WANT;
648                 (void) tsleep(dq, PINOD+1, "setqta", 0);
649         }
650         /*
651          * Copy all but the current values.
652          * Reset time limit if previously had no soft limit or were
653          * under it, but now have a soft limit and are over it.
654          */
655         newlim.dqb_curblocks = dq->dq_curblocks;
656         newlim.dqb_curinodes = dq->dq_curinodes;
657         if (dq->dq_id != 0) {
658                 newlim.dqb_btime = dq->dq_btime;
659                 newlim.dqb_itime = dq->dq_itime;
660         }
661         if (newlim.dqb_bsoftlimit &&
662             dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
663             (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
664                 newlim.dqb_btime = time_second + ump->um_btime[type];
665         if (newlim.dqb_isoftlimit &&
666             dq->dq_curinodes >= newlim.dqb_isoftlimit &&
667             (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
668                 newlim.dqb_itime = time_second + ump->um_itime[type];
669         dq->dq_dqb = newlim;
670         if (dq->dq_curblocks < dq->dq_bsoftlimit)
671                 dq->dq_flags &= ~DQ_BLKS;
672         if (dq->dq_curinodes < dq->dq_isoftlimit)
673                 dq->dq_flags &= ~DQ_INODS;
674         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
675             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
676                 dq->dq_flags |= DQ_FAKE;
677         else
678                 dq->dq_flags &= ~DQ_FAKE;
679         dq->dq_flags |= DQ_MOD;
680         dqrele(NULLVP, dq);
681         return (0);
682 }
683
684 /*
685  * Q_SETUSE - set current inode and block usage.
686  */
687 int
688 setuse(td, mp, id, type, addr)
689         struct thread *td;
690         struct mount *mp;
691         u_long id;
692         int type;
693         void *addr;
694 {
695         struct dquot *dq;
696         struct ufsmount *ump = VFSTOUFS(mp);
697         struct dquot *ndq;
698         struct dqblk usage;
699         int error;
700
701         error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
702         if (error)
703                 return (error);
704
705         error = copyin(addr, &usage, sizeof (struct dqblk));
706         if (error)
707                 return (error);
708         error = dqget(NULLVP, id, ump, type, &ndq);
709         if (error)
710                 return (error);
711         dq = ndq;
712         while (dq->dq_flags & DQ_LOCK) {
713                 dq->dq_flags |= DQ_WANT;
714                 (void) tsleep(dq, PINOD+1, "setuse", 0);
715         }
716         /*
717          * Reset time limit if have a soft limit and were
718          * previously under it, but are now over it.
719          */
720         if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
721             usage.dqb_curblocks >= dq->dq_bsoftlimit)
722                 dq->dq_btime = time_second + ump->um_btime[type];
723         if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
724             usage.dqb_curinodes >= dq->dq_isoftlimit)
725                 dq->dq_itime = time_second + ump->um_itime[type];
726         dq->dq_curblocks = usage.dqb_curblocks;
727         dq->dq_curinodes = usage.dqb_curinodes;
728         if (dq->dq_curblocks < dq->dq_bsoftlimit)
729                 dq->dq_flags &= ~DQ_BLKS;
730         if (dq->dq_curinodes < dq->dq_isoftlimit)
731                 dq->dq_flags &= ~DQ_INODS;
732         dq->dq_flags |= DQ_MOD;
733         dqrele(NULLVP, dq);
734         return (0);
735 }
736
737 /*
738  * Q_SYNC - sync quota files to disk.
739  */
740 int
741 qsync(mp)
742         struct mount *mp;
743 {
744         struct ufsmount *ump = VFSTOUFS(mp);
745         struct thread *td = curthread;          /* XXX */
746         struct vnode *vp, *mvp;
747         struct dquot *dq;
748         int i, error;
749
750         /*
751          * Check if the mount point has any quotas.
752          * If not, simply return.
753          */
754         for (i = 0; i < MAXQUOTAS; i++)
755                 if (ump->um_quotas[i] != NULLVP)
756                         break;
757         if (i == MAXQUOTAS)
758                 return (0);
759         /*
760          * Search vnodes associated with this mount point,
761          * synchronizing any modified dquot structures.
762          */
763         MNT_ILOCK(mp);
764 again:
765         MNT_VNODE_FOREACH(vp, mp, mvp) {
766                 VI_LOCK(vp);
767                 MNT_IUNLOCK(mp);
768                 if (vp->v_type == VNON) {
769                         VI_UNLOCK(vp);
770                         MNT_ILOCK(mp);
771                         continue;
772                 }
773                 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
774                 if (error) {
775                         MNT_ILOCK(mp);
776                         if (error == ENOENT) {
777                                 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
778                                 goto again;
779                         }
780                         continue;
781                 }
782                 for (i = 0; i < MAXQUOTAS; i++) {
783                         dq = VTOI(vp)->i_dquot[i];
784                         if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
785                                 dqsync(vp, dq);
786                 }
787                 vput(vp);
788                 MNT_ILOCK(mp);
789         }
790         MNT_IUNLOCK(mp);
791         return (0);
792 }
793
794 /*
795  * Code pertaining to management of the in-core dquot data structures.
796  */
797 #define DQHASH(dqvp, id) \
798         (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
799 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
800 static u_long dqhash;
801
802 /*
803  * Dquot free list.
804  */
805 #define DQUOTINC        5       /* minimum free dquots desired */
806 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
807 static long numdquot, desireddquot = DQUOTINC;
808
809 /*
810  * Initialize the quota system.
811  */
812 void
813 dqinit()
814 {
815
816         dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
817         TAILQ_INIT(&dqfreelist);
818 }
819
820 /*
821  * Shut down the quota system.
822  */
823 void
824 dquninit()
825 {
826         struct dquot *dq;
827
828         hashdestroy(dqhashtbl, M_DQUOT, dqhash);
829         while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
830                 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
831                 free(dq, M_DQUOT);
832         }
833 }
834
835 /*
836  * Obtain a dquot structure for the specified identifier and quota file
837  * reading the information from the file if necessary.
838  */
839 static int
840 dqget(vp, id, ump, type, dqp)
841         struct vnode *vp;
842         u_long id;
843         struct ufsmount *ump;
844         int type;
845         struct dquot **dqp;
846 {
847         struct thread *td = curthread;          /* XXX */
848         struct dquot *dq;
849         struct dqhash *dqh;
850         struct vnode *dqvp;
851         struct iovec aiov;
852         struct uio auio;
853         int error;
854
855         dqvp = ump->um_quotas[type];
856         if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
857                 *dqp = NODQUOT;
858                 return (EINVAL);
859         }
860         /*
861          * Check the cache first.
862          */
863         dqh = DQHASH(dqvp, id);
864         LIST_FOREACH(dq, dqh, dq_hash) {
865                 if (dq->dq_id != id ||
866                     dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
867                         continue;
868                 /*
869                  * Cache hit with no references.  Take
870                  * the structure off the free list.
871                  */
872                 if (dq->dq_cnt == 0)
873                         TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
874                 DQREF(dq);
875                 *dqp = dq;
876                 return (0);
877         }
878         /*
879          * Not in cache, allocate a new one.
880          */
881         if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
882             numdquot < MAXQUOTAS * desiredvnodes)
883                 desireddquot += DQUOTINC;
884         if (numdquot < desireddquot) {
885                 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
886                     M_WAITOK | M_ZERO);
887                 numdquot++;
888         } else {
889                 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
890                         tablefull("dquot");
891                         *dqp = NODQUOT;
892                         return (EUSERS);
893                 }
894                 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
895                         panic("dqget: free dquot isn't");
896                 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
897                 if (dq->dq_ump != NULL)
898                         LIST_REMOVE(dq, dq_hash);
899         }
900         /*
901          * Initialize the contents of the dquot structure.
902          */
903         if (vp != dqvp)
904                 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
905         LIST_INSERT_HEAD(dqh, dq, dq_hash);
906         DQREF(dq);
907         dq->dq_flags = DQ_LOCK;
908         dq->dq_id = id;
909         dq->dq_ump = ump;
910         dq->dq_type = type;
911         auio.uio_iov = &aiov;
912         auio.uio_iovcnt = 1;
913         aiov.iov_base = &dq->dq_dqb;
914         aiov.iov_len = sizeof (struct dqblk);
915         auio.uio_resid = sizeof (struct dqblk);
916         auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
917         auio.uio_segflg = UIO_SYSSPACE;
918         auio.uio_rw = UIO_READ;
919         auio.uio_td = (struct thread *)0;
920         error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
921         if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
922                 bzero(&dq->dq_dqb, sizeof(struct dqblk));
923         if (vp != dqvp)
924                 VOP_UNLOCK(dqvp, 0, td);
925         if (dq->dq_flags & DQ_WANT)
926                 wakeup(dq);
927         dq->dq_flags = 0;
928         /*
929          * I/O error in reading quota file, release
930          * quota structure and reflect problem to caller.
931          */
932         if (error) {
933                 LIST_REMOVE(dq, dq_hash);
934                 dqrele(vp, dq);
935                 *dqp = NODQUOT;
936                 return (error);
937         }
938         /*
939          * Check for no limit to enforce.
940          * Initialize time values if necessary.
941          */
942         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
943             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
944                 dq->dq_flags |= DQ_FAKE;
945         if (dq->dq_id != 0) {
946                 if (dq->dq_btime == 0)
947                         dq->dq_btime = time_second + ump->um_btime[type];
948                 if (dq->dq_itime == 0)
949                         dq->dq_itime = time_second + ump->um_itime[type];
950         }
951         *dqp = dq;
952         return (0);
953 }
954
955 #ifdef DIAGNOSTIC
956 /*
957  * Obtain a reference to a dquot.
958  */
959 static void
960 dqref(dq)
961         struct dquot *dq;
962 {
963
964         dq->dq_cnt++;
965 }
966 #endif
967
968 /*
969  * Release a reference to a dquot.
970  */
971 void
972 dqrele(vp, dq)
973         struct vnode *vp;
974         struct dquot *dq;
975 {
976
977         if (dq == NODQUOT)
978                 return;
979         if (dq->dq_cnt > 1) {
980                 dq->dq_cnt--;
981                 return;
982         }
983         if (dq->dq_flags & DQ_MOD)
984                 (void) dqsync(vp, dq);
985         if (--dq->dq_cnt > 0)
986                 return;
987         TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
988 }
989
990 /*
991  * Update the disk quota in the quota file.
992  */
993 static int
994 dqsync(vp, dq)
995         struct vnode *vp;
996         struct dquot *dq;
997 {
998         struct thread *td = curthread;          /* XXX */
999         struct vnode *dqvp;
1000         struct iovec aiov;
1001         struct uio auio;
1002         int error;
1003         struct mount *mp;
1004
1005         mp = NULL;
1006         if (dq == NODQUOT)
1007                 panic("dqsync: dquot");
1008         if ((dq->dq_flags & DQ_MOD) == 0)
1009                 return (0);
1010         if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
1011                 panic("dqsync: file");
1012         (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1013         if (vp != dqvp)
1014                 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
1015         while (dq->dq_flags & DQ_LOCK) {
1016                 dq->dq_flags |= DQ_WANT;
1017                 (void) tsleep(dq, PINOD+2, "dqsync", 0);
1018                 if ((dq->dq_flags & DQ_MOD) == 0) {
1019                         if (vp != dqvp)
1020                                 VOP_UNLOCK(dqvp, 0, td);
1021                         vn_finished_secondary_write(mp);
1022                         return (0);
1023                 }
1024         }
1025         dq->dq_flags |= DQ_LOCK;
1026         auio.uio_iov = &aiov;
1027         auio.uio_iovcnt = 1;
1028         aiov.iov_base = &dq->dq_dqb;
1029         aiov.iov_len = sizeof (struct dqblk);
1030         auio.uio_resid = sizeof (struct dqblk);
1031         auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
1032         auio.uio_segflg = UIO_SYSSPACE;
1033         auio.uio_rw = UIO_WRITE;
1034         auio.uio_td = (struct thread *)0;
1035         error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1036         if (auio.uio_resid && error == 0)
1037                 error = EIO;
1038         if (dq->dq_flags & DQ_WANT)
1039                 wakeup(dq);
1040         dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
1041         if (vp != dqvp)
1042                 VOP_UNLOCK(dqvp, 0, td);
1043         vn_finished_secondary_write(mp);
1044         return (error);
1045 }
1046
1047 /*
1048  * Flush all entries from the cache for a particular vnode.
1049  */
1050 static void
1051 dqflush(vp)
1052         struct vnode *vp;
1053 {
1054         struct dquot *dq, *nextdq;
1055         struct dqhash *dqh;
1056
1057         /*
1058          * Move all dquot's that used to refer to this quota
1059          * file off their hash chains (they will eventually
1060          * fall off the head of the free list and be re-used).
1061          */
1062         for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1063                 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1064                         nextdq = LIST_NEXT(dq, dq_hash);
1065                         if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1066                                 continue;
1067                         if (dq->dq_cnt)
1068                                 panic("dqflush: stray dquot");
1069                         LIST_REMOVE(dq, dq_hash);
1070                         dq->dq_ump = (struct ufsmount *)0;
1071                 }
1072         }
1073 }