The thread/proc pointer argument in the VFS subsystem originally existed
[dragonfly.git] / sys / vfs / 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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
37  * $FreeBSD: src/sys/ufs/ufs/ufs_quota.c,v 1.27.2.3 2002/01/15 10:33:32 phk Exp $
38  * $DragonFly: src/sys/vfs/ufs/ufs_quota.c,v 1.21 2006/05/06 02:43:14 dillon Exp $
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/fcntl.h>
46 #include <sys/proc.h>
47 #include <sys/nlookup.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <vm/vm_zone.h>
51
52 #include "quota.h"
53 #include "inode.h"
54 #include "ufsmount.h"
55
56 static MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
57
58 /*
59  * Quota name to error message mapping.
60  */
61 static char *quotatypes[] = INITQFNAMES;
62
63 static int ufs_chkdqchg (struct inode *, long, struct ucred *, int);
64 static int ufs_chkiqchg (struct inode *, long, struct ucred *, int);
65 static int ufs_dqget (struct vnode *,
66                 u_long, struct ufsmount *, int, struct ufs_dquot **);
67 static int ufs_dqsync (struct vnode *, struct ufs_dquot *);
68 static void ufs_dqflush (struct vnode *);
69
70 #ifdef DIAGNOSTIC
71 static void ufs_dqref (struct ufs_dquot *);
72 static void ufs_chkdquot (struct inode *);
73 #endif
74
75 /*
76  * Set up the quotas for an inode.
77  *
78  * This routine completely defines the semantics of quotas.
79  * If other criterion want to be used to establish quotas, the
80  * MAXQUOTAS value in quotas.h should be increased, and the
81  * additional dquots set up here.
82  */
83 int
84 ufs_getinoquota(struct inode *ip)
85 {
86         struct ufsmount *ump;
87         struct vnode *vp = ITOV(ip);
88         int error;
89
90         ump = VFSTOUFS(vp->v_mount);
91         /*
92          * Set up the user quota based on file uid.
93          * EINVAL means that quotas are not enabled.
94          */
95         if (ip->i_dquot[USRQUOTA] == NODQUOT &&
96             (error = ufs_dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
97             error != EINVAL)
98                 return (error);
99         /*
100          * Set up the group quota based on file gid.
101          * EINVAL means that quotas are not enabled.
102          */
103         if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
104             (error = ufs_dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
105             error != EINVAL)
106                 return (error);
107         return (0);
108 }
109
110 /*
111  * Update disk usage, and take corrective action.
112  */
113 int
114 ufs_chkdq(struct inode *ip, long change, struct ucred *cred, int flags)
115 {
116         struct ufs_dquot *dq;
117         int i;
118         int ncurblocks, error;
119
120 #ifdef DIAGNOSTIC
121         if ((flags & CHOWN) == 0)
122                 ufs_chkdquot(ip);
123 #endif
124         if (change == 0)
125                 return (0);
126         if (change < 0) {
127                 for (i = 0; i < MAXQUOTAS; i++) {
128                         if ((dq = ip->i_dquot[i]) == NODQUOT)
129                                 continue;
130                         while (dq->dq_flags & DQ_LOCK) {
131                                 dq->dq_flags |= DQ_WANT;
132                                 (void) tsleep((caddr_t)dq, 0, "chkdq1", 0);
133                         }
134                         ncurblocks = dq->dq_curblocks + change;
135                         if (ncurblocks >= 0)
136                                 dq->dq_curblocks = ncurblocks;
137                         else
138                                 dq->dq_curblocks = 0;
139                         dq->dq_flags &= ~DQ_BLKS;
140                         dq->dq_flags |= DQ_MOD;
141                 }
142                 return (0);
143         }
144         if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
145                 for (i = 0; i < MAXQUOTAS; i++) {
146                         if ((dq = ip->i_dquot[i]) == NODQUOT)
147                                 continue;
148                         error = ufs_chkdqchg(ip, change, cred, i);
149                         if (error)
150                                 return (error);
151                 }
152         }
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((caddr_t)dq, 0, "chkdq2", 0);
159                 }
160                 /* Reset timer when crossing soft limit */
161                 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
162                     dq->dq_curblocks < dq->dq_bsoftlimit)
163                         dq->dq_btime = time_second +
164                             VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
165                 dq->dq_curblocks += change;
166                 dq->dq_flags |= DQ_MOD;
167         }
168         return (0);
169 }
170
171 /*
172  * Check for a valid change to a users allocation.
173  * Issue an error message if appropriate.
174  */
175 static int
176 ufs_chkdqchg(struct inode *ip, long change, struct ucred *cred, int type)
177 {
178         struct ufs_dquot *dq = ip->i_dquot[type];
179         long ncurblocks = dq->dq_curblocks + change;
180
181         /*
182          * If user would exceed their hard limit, disallow space allocation.
183          */
184         if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
185                 if ((dq->dq_flags & DQ_BLKS) == 0 &&
186                     ip->i_uid == cred->cr_uid) {
187                         uprintf("\n%s: write failed, %s disk limit reached\n",
188                             ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
189                             quotatypes[type]);
190                         dq->dq_flags |= DQ_BLKS;
191                 }
192                 return (EDQUOT);
193         }
194         /*
195          * If user is over their soft limit for too long, disallow space
196          * allocation. Reset time limit as they cross their soft limit.
197          */
198         if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
199                 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
200                         dq->dq_btime = time_second +
201                             VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
202                         if (ip->i_uid == cred->cr_uid)
203                                 uprintf("\n%s: warning, %s %s\n",
204                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
205                                     quotatypes[type], "disk quota exceeded");
206                         return (0);
207                 }
208                 if (time_second > dq->dq_btime) {
209                         if ((dq->dq_flags & DQ_BLKS) == 0 &&
210                             ip->i_uid == cred->cr_uid) {
211                                 uprintf("\n%s: write failed, %s %s\n",
212                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
213                                     quotatypes[type],
214                                     "disk quota exceeded for too long");
215                                 dq->dq_flags |= DQ_BLKS;
216                         }
217                         return (EDQUOT);
218                 }
219         }
220         return (0);
221 }
222
223 /*
224  * Check the inode limit, applying corrective action.
225  */
226 int
227 ufs_chkiq(struct inode *ip, long change, struct ucred *cred, int flags)
228 {
229         struct ufs_dquot *dq;
230         int i;
231         int ncurinodes, error;
232
233 #ifdef DIAGNOSTIC
234         if ((flags & CHOWN) == 0)
235                 ufs_chkdquot(ip);
236 #endif
237         if (change == 0)
238                 return (0);
239         if (change < 0) {
240                 for (i = 0; i < MAXQUOTAS; i++) {
241                         if ((dq = ip->i_dquot[i]) == NODQUOT)
242                                 continue;
243                         while (dq->dq_flags & DQ_LOCK) {
244                                 dq->dq_flags |= DQ_WANT;
245                                 (void) tsleep((caddr_t)dq, 0, "chkiq1", 0);
246                         }
247                         ncurinodes = dq->dq_curinodes + change;
248                         if (ncurinodes >= 0)
249                                 dq->dq_curinodes = ncurinodes;
250                         else
251                                 dq->dq_curinodes = 0;
252                         dq->dq_flags &= ~DQ_INODS;
253                         dq->dq_flags |= DQ_MOD;
254                 }
255                 return (0);
256         }
257         if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
258                 for (i = 0; i < MAXQUOTAS; i++) {
259                         if ((dq = ip->i_dquot[i]) == NODQUOT)
260                                 continue;
261                         error = ufs_chkiqchg(ip, change, cred, i);
262                         if (error)
263                                 return (error);
264                 }
265         }
266         for (i = 0; i < MAXQUOTAS; i++) {
267                 if ((dq = ip->i_dquot[i]) == NODQUOT)
268                         continue;
269                 while (dq->dq_flags & DQ_LOCK) {
270                         dq->dq_flags |= DQ_WANT;
271                         (void) tsleep((caddr_t)dq, 0, "chkiq2", 0);
272                 }
273                 /* Reset timer when crossing soft limit */
274                 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
275                     dq->dq_curinodes < dq->dq_isoftlimit)
276                         dq->dq_itime = time_second +
277                             VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
278                 dq->dq_curinodes += change;
279                 dq->dq_flags |= DQ_MOD;
280         }
281         return (0);
282 }
283
284 /*
285  * Check for a valid change to a users allocation.
286  * Issue an error message if appropriate.
287  */
288 static int
289 ufs_chkiqchg(struct inode *ip, long change, struct ucred *cred, int type)
290 {
291         struct ufs_dquot *dq = ip->i_dquot[type];
292         long ncurinodes = dq->dq_curinodes + change;
293
294         /*
295          * If user would exceed their hard limit, disallow inode allocation.
296          */
297         if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
298                 if ((dq->dq_flags & DQ_INODS) == 0 &&
299                     ip->i_uid == cred->cr_uid) {
300                         uprintf("\n%s: write failed, %s inode limit reached\n",
301                             ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
302                             quotatypes[type]);
303                         dq->dq_flags |= DQ_INODS;
304                 }
305                 return (EDQUOT);
306         }
307         /*
308          * If user is over their soft limit for too long, disallow inode
309          * allocation. Reset time limit as they cross their soft limit.
310          */
311         if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
312                 if (dq->dq_curinodes < dq->dq_isoftlimit) {
313                         dq->dq_itime = time_second +
314                             VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
315                         if (ip->i_uid == cred->cr_uid)
316                                 uprintf("\n%s: warning, %s %s\n",
317                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
318                                     quotatypes[type], "inode quota exceeded");
319                         return (0);
320                 }
321                 if (time_second > dq->dq_itime) {
322                         if ((dq->dq_flags & DQ_INODS) == 0 &&
323                             ip->i_uid == cred->cr_uid) {
324                                 uprintf("\n%s: write failed, %s %s\n",
325                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
326                                     quotatypes[type],
327                                     "inode quota exceeded for too long");
328                                 dq->dq_flags |= DQ_INODS;
329                         }
330                         return (EDQUOT);
331                 }
332         }
333         return (0);
334 }
335
336 #ifdef DIAGNOSTIC
337 /*
338  * On filesystems with quotas enabled, it is an error for a file to change
339  * size and not to have a dquot structure associated with it.
340  */
341 static void
342 ufs_chkdquot(struct inode *ip)
343 {
344         struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
345         int i;
346
347         for (i = 0; i < MAXQUOTAS; i++) {
348                 if (ump->um_quotas[i] == NULLVP ||
349                     (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
350                         continue;
351                 if (ip->i_dquot[i] == NODQUOT) {
352                         vprint("chkdquot: missing dquot", ITOV(ip));
353                         panic("chkdquot: missing dquot");
354                 }
355         }
356 }
357 #endif
358
359 /*
360  * Code to process quotactl commands.
361  */
362
363 struct scaninfo {
364         thread_t td;
365         int rescan;
366         int type;
367 };
368
369 /*
370  * Q_QUOTAON - set up a quota file for a particular filesystem.
371  */
372 static int ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data);
373
374 int
375 ufs_quotaon(struct thread *td, struct mount *mp, int type, caddr_t fname)
376 {
377         struct ufsmount *ump = VFSTOUFS(mp);
378         struct vnode *vp, **vpp;
379         struct ufs_dquot *dq;
380         int error;
381         struct nlookupdata nd;
382         struct ucred *cred;
383         struct scaninfo scaninfo;
384
385         KKASSERT(td->td_proc);
386         cred = td->td_proc->p_ucred;
387
388         vpp = &ump->um_quotas[type];
389         error = nlookup_init(&nd, fname, UIO_USERSPACE, NLC_FOLLOW|NLC_LOCKVP);
390         if (error == 0)
391                 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
392         if (error == 0 && nd.nl_open_vp->v_type != VREG)
393                 error = EACCES;
394         if (error) {
395                 nlookup_done(&nd);
396                 return (error);
397         }
398         vp = nd.nl_open_vp;
399         nd.nl_open_vp = NULL;
400         nlookup_done(&nd);
401
402         VOP_UNLOCK(vp, 0);
403         if (*vpp != vp)
404                 ufs_quotaoff(td, mp, type);
405         ump->um_qflags[type] |= QTF_OPENING;
406         mp->mnt_flag |= MNT_QUOTA;
407         vp->v_flag |= VSYSTEM;
408         *vpp = vp;
409         /* XXX release duplicate vp if *vpp == vp? */
410         /*
411          * Save the credential of the process that turned on quotas.
412          * Set up the time limits for this quota.
413          */
414         ump->um_cred[type] = crhold(cred);
415         ump->um_btime[type] = MAX_DQ_TIME;
416         ump->um_itime[type] = MAX_IQ_TIME;
417         if (ufs_dqget(NULLVP, 0, ump, type, &dq) == 0) {
418                 if (dq->dq_btime > 0)
419                         ump->um_btime[type] = dq->dq_btime;
420                 if (dq->dq_itime > 0)
421                         ump->um_itime[type] = dq->dq_itime;
422                 ufs_dqrele(NULLVP, dq);
423         }
424         /*
425          * Search vnodes associated with this mount point,
426          * adding references to quota file being opened.
427          * NB: only need to add dquot's for inodes being modified.
428          */
429         scaninfo.rescan = 1;
430         scaninfo.td = td;
431         while (scaninfo.rescan) {
432                 scaninfo.rescan = 0;
433                 error = vmntvnodescan(mp, VMSC_GETVP,
434                                         NULL, ufs_quotaon_scan, &scaninfo);
435                 if (error)
436                         break;
437         }
438         ump->um_qflags[type] &= ~QTF_OPENING;
439         if (error)
440                 ufs_quotaoff(td, mp, type);
441         return (error);
442 }
443
444 static int
445 ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data)
446 {
447         int error;
448         /*struct scaninfo *info = data;*/
449
450         if (vp->v_writecount == 0)
451                 return(0);
452         error = ufs_getinoquota(VTOI(vp));
453         return(error);
454 }
455
456 /*
457  * Q_QUOTAOFF - turn off disk quotas for a filesystem.
458  */
459
460 static int ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data);
461
462 int
463 ufs_quotaoff(struct thread *td, struct mount *mp, int type)
464 {
465         struct vnode *qvp;
466         struct ufsmount *ump = VFSTOUFS(mp);
467         struct ucred *cred;
468         int error;
469         struct scaninfo scaninfo;
470
471         KKASSERT(td->td_proc);
472         cred = td->td_proc->p_ucred;
473
474         if ((qvp = ump->um_quotas[type]) == NULLVP)
475                 return (0);
476         ump->um_qflags[type] |= QTF_CLOSING;
477
478         /*
479          * Search vnodes associated with this mount point,
480          * deleting any references to quota file being closed.
481          */
482         scaninfo.rescan = 1;
483         scaninfo.td = td;
484         scaninfo.type = type;
485         while (scaninfo.rescan) {
486                 scaninfo.rescan = 0;
487                 vmntvnodescan(mp, VMSC_GETVP, NULL, ufs_quotaoff_scan, &scaninfo);
488         }
489         ufs_dqflush(qvp);
490         qvp->v_flag &= ~VSYSTEM;
491         error = vn_close(qvp, FREAD|FWRITE);
492         ump->um_quotas[type] = NULLVP;
493         crfree(ump->um_cred[type]);
494         ump->um_cred[type] = NOCRED;
495         ump->um_qflags[type] &= ~QTF_CLOSING;
496         for (type = 0; type < MAXQUOTAS; type++) {
497                 if (ump->um_quotas[type] != NULLVP)
498                         break;
499         }
500         if (type == MAXQUOTAS)
501                 mp->mnt_flag &= ~MNT_QUOTA;
502         return (error);
503 }
504
505 static int
506 ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data)
507 {
508         struct scaninfo *info = data;
509         struct ufs_dquot *dq;
510         struct inode *ip;
511
512         if (vp->v_type == VNON) {
513                 return(0);
514         }
515         ip = VTOI(vp);
516         dq = ip->i_dquot[info->type];
517         ip->i_dquot[info->type] = NODQUOT;
518         ufs_dqrele(vp, dq);
519         return(0);
520 }
521
522 /*
523  * Q_GETQUOTA - return current values in a dqblk structure.
524  */
525 int
526 ufs_getquota(struct mount *mp, u_long id, int type, caddr_t addr)
527 {
528         struct ufs_dquot *dq;
529         int error;
530
531         error = ufs_dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
532         if (error)
533                 return (error);
534         error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct ufs_dqblk));
535         ufs_dqrele(NULLVP, dq);
536         return (error);
537 }
538
539 /*
540  * Q_SETQUOTA - assign an entire dqblk structure.
541  */
542 int
543 ufs_setquota(struct mount *mp, u_long id, int type, caddr_t addr)
544 {
545         struct ufs_dquot *dq;
546         struct ufs_dquot *ndq;
547         struct ufsmount *ump = VFSTOUFS(mp);
548         struct ufs_dqblk newlim;
549         int error;
550
551         error = copyin(addr, (caddr_t)&newlim, sizeof (struct ufs_dqblk));
552         if (error)
553                 return (error);
554         error = ufs_dqget(NULLVP, id, ump, type, &ndq);
555         if (error)
556                 return (error);
557         dq = ndq;
558         while (dq->dq_flags & DQ_LOCK) {
559                 dq->dq_flags |= DQ_WANT;
560                 (void) tsleep((caddr_t)dq, 0, "setqta", 0);
561         }
562         /*
563          * Copy all but the current values.
564          * Reset time limit if previously had no soft limit or were
565          * under it, but now have a soft limit and are over it.
566          */
567         newlim.dqb_curblocks = dq->dq_curblocks;
568         newlim.dqb_curinodes = dq->dq_curinodes;
569         if (dq->dq_id != 0) {
570                 newlim.dqb_btime = dq->dq_btime;
571                 newlim.dqb_itime = dq->dq_itime;
572         }
573         if (newlim.dqb_bsoftlimit &&
574             dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
575             (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
576                 newlim.dqb_btime = time_second + ump->um_btime[type];
577         if (newlim.dqb_isoftlimit &&
578             dq->dq_curinodes >= newlim.dqb_isoftlimit &&
579             (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
580                 newlim.dqb_itime = time_second + ump->um_itime[type];
581         dq->dq_dqb = newlim;
582         if (dq->dq_curblocks < dq->dq_bsoftlimit)
583                 dq->dq_flags &= ~DQ_BLKS;
584         if (dq->dq_curinodes < dq->dq_isoftlimit)
585                 dq->dq_flags &= ~DQ_INODS;
586         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
587             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
588                 dq->dq_flags |= DQ_FAKE;
589         else
590                 dq->dq_flags &= ~DQ_FAKE;
591         dq->dq_flags |= DQ_MOD;
592         ufs_dqrele(NULLVP, dq);
593         return (0);
594 }
595
596 /*
597  * Q_SETUSE - set current inode and block usage.
598  */
599 int
600 ufs_setuse(struct mount *mp, u_long id, int type, caddr_t addr)
601 {
602         struct ufs_dquot *dq;
603         struct ufsmount *ump = VFSTOUFS(mp);
604         struct ufs_dquot *ndq;
605         struct ufs_dqblk usage;
606         int error;
607
608         error = copyin(addr, (caddr_t)&usage, sizeof (struct ufs_dqblk));
609         if (error)
610                 return (error);
611         error = ufs_dqget(NULLVP, id, ump, type, &ndq);
612         if (error)
613                 return (error);
614         dq = ndq;
615         while (dq->dq_flags & DQ_LOCK) {
616                 dq->dq_flags |= DQ_WANT;
617                 (void) tsleep((caddr_t)dq, 0, "setuse", 0);
618         }
619         /*
620          * Reset time limit if have a soft limit and were
621          * previously under it, but are now over it.
622          */
623         if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
624             usage.dqb_curblocks >= dq->dq_bsoftlimit)
625                 dq->dq_btime = time_second + ump->um_btime[type];
626         if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
627             usage.dqb_curinodes >= dq->dq_isoftlimit)
628                 dq->dq_itime = time_second + ump->um_itime[type];
629         dq->dq_curblocks = usage.dqb_curblocks;
630         dq->dq_curinodes = usage.dqb_curinodes;
631         if (dq->dq_curblocks < dq->dq_bsoftlimit)
632                 dq->dq_flags &= ~DQ_BLKS;
633         if (dq->dq_curinodes < dq->dq_isoftlimit)
634                 dq->dq_flags &= ~DQ_INODS;
635         dq->dq_flags |= DQ_MOD;
636         ufs_dqrele(NULLVP, dq);
637         return (0);
638 }
639
640 /*
641  * Q_SYNC - sync quota files to disk.
642  */
643
644 static int ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data);
645
646 int
647 ufs_qsync(struct mount *mp)
648 {
649         struct ufsmount *ump = VFSTOUFS(mp);
650         struct thread *td = curthread;          /* XXX */
651         struct scaninfo scaninfo;
652         int i;
653
654         /*
655          * Check if the mount point has any quotas.
656          * If not, simply return.
657          */
658         for (i = 0; i < MAXQUOTAS; i++)
659                 if (ump->um_quotas[i] != NULLVP)
660                         break;
661         if (i == MAXQUOTAS)
662                 return (0);
663         /*
664          * Search vnodes associated with this mount point,
665          * synchronizing any modified ufs_dquot structures.
666          */
667         scaninfo.rescan = 1;
668         scaninfo.td = td;
669         while (scaninfo.rescan) {
670                 scaninfo.rescan = 0;
671                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
672                                 NULL, ufs_qsync_scan, &scaninfo);
673         }
674         return (0);
675 }
676
677 static int
678 ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data)
679 {
680         /*struct scaninfo *info = data;*/
681         struct ufs_dquot *dq;
682         /* int error;*/
683         int i;
684
685         for (i = 0; i < MAXQUOTAS; i++) {
686                 dq = VTOI(vp)->i_dquot[i];
687                 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
688                         ufs_dqsync(vp, dq);
689         }
690         return(0);
691 }
692
693 /*
694  * Code pertaining to management of the in-core dquot data structures.
695  */
696 #define DQHASH(dqvp, id) \
697         (&ufs_dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & ufs_dqhash])
698 static LIST_HEAD(ufs_dqhash, ufs_dquot) *ufs_dqhashtbl;
699 static u_long ufs_dqhash;
700
701 /*
702  * Dquot free list.
703  */
704 #define DQUOTINC        5       /* minimum free dquots desired */
705 static TAILQ_HEAD(ufs_dqfreelist, ufs_dquot) ufs_dqfreelist;
706 static long ufs_numdquot, ufs_desireddquot = DQUOTINC;
707
708 /*
709  * Initialize the quota system.
710  */
711 void
712 ufs_dqinit(void)
713 {
714         ufs_dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &ufs_dqhash);
715         TAILQ_INIT(&ufs_dqfreelist);
716 }
717
718 /*
719  * Obtain a dquot structure for the specified identifier and quota file
720  * reading the information from the file if necessary.
721  */
722 static int
723 ufs_dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
724       struct ufs_dquot **dqp)
725 {
726         struct ufs_dquot *dq;
727         struct ufs_dqhash *dqh;
728         struct vnode *dqvp;
729         struct iovec aiov;
730         struct uio auio;
731         int error;
732
733         dqvp = ump->um_quotas[type];
734         if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
735                 *dqp = NODQUOT;
736                 return (EINVAL);
737         }
738         /*
739          * Check the cache first.
740          */
741         dqh = DQHASH(dqvp, id);
742         for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
743                 if (dq->dq_id != id ||
744                     dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
745                         continue;
746                 /*
747                  * Cache hit with no references.  Take
748                  * the structure off the free list.
749                  */
750                 if (dq->dq_cnt == 0)
751                         TAILQ_REMOVE(&ufs_dqfreelist, dq, dq_freelist);
752                 DQREF(dq);
753                 *dqp = dq;
754                 return (0);
755         }
756         /*
757          * Not in cache, allocate a new one.
758          */
759         if (TAILQ_EMPTY(&ufs_dqfreelist) && ufs_numdquot < MAXQUOTAS * desiredvnodes)
760                 ufs_desireddquot += DQUOTINC;
761         if (ufs_numdquot < ufs_desireddquot) {
762                 dq = (struct ufs_dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
763                 bzero((char *)dq, sizeof *dq);
764                 ufs_numdquot++;
765         } else {
766                 if ((dq = TAILQ_FIRST(&ufs_dqfreelist)) == NULL) {
767                         tablefull("dquot");
768                         *dqp = NODQUOT;
769                         return (EUSERS);
770                 }
771                 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
772                         panic("dqget: free dquot isn't");
773                 TAILQ_REMOVE(&ufs_dqfreelist, dq, dq_freelist);
774                 if (dq->dq_ump != NULL)
775                         LIST_REMOVE(dq, dq_hash);
776         }
777         /*
778          * Initialize the contents of the dquot structure.
779          */
780         if (vp != dqvp)
781                 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
782         LIST_INSERT_HEAD(dqh, dq, dq_hash);
783         DQREF(dq);
784         dq->dq_flags = DQ_LOCK;
785         dq->dq_id = id;
786         dq->dq_ump = ump;
787         dq->dq_type = type;
788         auio.uio_iov = &aiov;
789         auio.uio_iovcnt = 1;
790         aiov.iov_base = (caddr_t)&dq->dq_dqb;
791         aiov.iov_len = sizeof (struct ufs_dqblk);
792         auio.uio_resid = sizeof (struct ufs_dqblk);
793         auio.uio_offset = (off_t)(id * sizeof (struct ufs_dqblk));
794         auio.uio_segflg = UIO_SYSSPACE;
795         auio.uio_rw = UIO_READ;
796         auio.uio_td = NULL;
797         error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
798         if (auio.uio_resid == sizeof(struct ufs_dqblk) && error == 0)
799                 bzero((caddr_t)&dq->dq_dqb, sizeof(struct ufs_dqblk));
800         if (vp != dqvp)
801                 VOP_UNLOCK(dqvp, 0);
802         if (dq->dq_flags & DQ_WANT)
803                 wakeup((caddr_t)dq);
804         dq->dq_flags = 0;
805         /*
806          * I/O error in reading quota file, release
807          * quota structure and reflect problem to caller.
808          */
809         if (error) {
810                 LIST_REMOVE(dq, dq_hash);
811                 ufs_dqrele(vp, dq);
812                 *dqp = NODQUOT;
813                 return (error);
814         }
815         /*
816          * Check for no limit to enforce.
817          * Initialize time values if necessary.
818          */
819         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
820             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
821                 dq->dq_flags |= DQ_FAKE;
822         if (dq->dq_id != 0) {
823                 if (dq->dq_btime == 0)
824                         dq->dq_btime = time_second + ump->um_btime[type];
825                 if (dq->dq_itime == 0)
826                         dq->dq_itime = time_second + ump->um_itime[type];
827         }
828         *dqp = dq;
829         return (0);
830 }
831
832 #ifdef DIAGNOSTIC
833 /*
834  * Obtain a reference to a dquot.
835  */
836 static void
837 ufs_dqref(struct ufs_dquot *dq)
838 {
839         dq->dq_cnt++;
840 }
841 #endif
842
843 /*
844  * Release a reference to a dquot.
845  */
846 void
847 ufs_dqrele(struct vnode *vp, struct ufs_dquot *dq)
848 {
849         if (dq == NODQUOT)
850                 return;
851         if (dq->dq_cnt > 1) {
852                 dq->dq_cnt--;
853                 return;
854         }
855         if (dq->dq_flags & DQ_MOD)
856                 (void)ufs_dqsync(vp, dq);
857         if (--dq->dq_cnt > 0)
858                 return;
859         TAILQ_INSERT_TAIL(&ufs_dqfreelist, dq, dq_freelist);
860 }
861
862 /*
863  * Update the disk quota in the quota file.
864  */
865 static int
866 ufs_dqsync(struct vnode *vp, struct ufs_dquot *dq)
867 {
868         struct vnode *dqvp;
869         struct iovec aiov;
870         struct uio auio;
871         int error;
872
873         if (dq == NODQUOT)
874                 panic("dqsync: dquot");
875         if ((dq->dq_flags & DQ_MOD) == 0)
876                 return (0);
877         if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
878                 panic("dqsync: file");
879         if (vp != dqvp)
880                 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
881         while (dq->dq_flags & DQ_LOCK) {
882                 dq->dq_flags |= DQ_WANT;
883                 (void) tsleep((caddr_t)dq, 0, "dqsync", 0);
884                 if ((dq->dq_flags & DQ_MOD) == 0) {
885                         if (vp != dqvp)
886                                 VOP_UNLOCK(dqvp, 0);
887                         return (0);
888                 }
889         }
890         dq->dq_flags |= DQ_LOCK;
891         auio.uio_iov = &aiov;
892         auio.uio_iovcnt = 1;
893         aiov.iov_base = (caddr_t)&dq->dq_dqb;
894         aiov.iov_len = sizeof (struct ufs_dqblk);
895         auio.uio_resid = sizeof (struct ufs_dqblk);
896         auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct ufs_dqblk));
897         auio.uio_segflg = UIO_SYSSPACE;
898         auio.uio_rw = UIO_WRITE;
899         auio.uio_td = NULL;
900         error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
901         if (auio.uio_resid && error == 0)
902                 error = EIO;
903         if (dq->dq_flags & DQ_WANT)
904                 wakeup((caddr_t)dq);
905         dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
906         if (vp != dqvp)
907                 VOP_UNLOCK(dqvp, 0);
908         return (error);
909 }
910
911 /*
912  * Flush all entries from the cache for a particular vnode.
913  */
914 static void
915 ufs_dqflush(struct vnode *vp)
916 {
917         struct ufs_dquot *dq, *nextdq;
918         struct ufs_dqhash *dqh;
919
920         /*
921          * Move all dquot's that used to refer to this quota
922          * file off their hash chains (they will eventually
923          * fall off the head of the free list and be re-used).
924          */
925         for (dqh = &ufs_dqhashtbl[ufs_dqhash]; dqh >= ufs_dqhashtbl; dqh--) {
926                 for (dq = dqh->lh_first; dq; dq = nextdq) {
927                         nextdq = dq->dq_hash.le_next;
928                         if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
929                                 continue;
930                         if (dq->dq_cnt)
931                                 panic("dqflush: stray dquot");
932                         LIST_REMOVE(dq, dq_hash);
933                         dq->dq_ump = (struct ufsmount *)0;
934                 }
935         }
936 }