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