kernel: Use LIST_FOREACH in some places.
[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  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/fcntl.h>
45 #include <sys/proc.h>
46 #include <sys/nlookup.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <vm/vm_zone.h>
50
51 #include "quota.h"
52 #include "inode.h"
53 #include "ufsmount.h"
54
55 static MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
56
57 /*
58  * Quota name to error message mapping.
59  */
60 static char *quotatypes[] = INITQFNAMES;
61
62 static int ufs_chkdqchg (struct inode *, long, struct ucred *, int);
63 static int ufs_chkiqchg (struct inode *, long, struct ucred *, int);
64 static int ufs_dqget (struct vnode *,
65                 u_long, struct ufsmount *, int, struct ufs_dquot **);
66 static int ufs_dqsync (struct vnode *, struct ufs_dquot *);
67 static void ufs_dqflush (struct vnode *);
68 static void ufs_quotawarn(struct ufs_dquot *dq);
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                         if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
131                                 ufs_quotawarn(dq);
132                                 continue;
133                         }
134                         while (dq->dq_flags & DQ_LOCK) {
135                                 dq->dq_flags |= DQ_WANT;
136                                 (void) tsleep((caddr_t)dq, 0, "chkdq1", 0);
137                         }
138                         ncurblocks = dq->dq_curblocks + change;
139                         if (ncurblocks >= 0)
140                                 dq->dq_curblocks = ncurblocks;
141                         else
142                                 dq->dq_curblocks = 0;
143                         dq->dq_flags &= ~DQ_BLKS;
144                         dq->dq_flags |= DQ_MOD;
145                 }
146                 return (0);
147         }
148         if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
149                 for (i = 0; i < MAXQUOTAS; i++) {
150                         if ((dq = ip->i_dquot[i]) == NODQUOT)
151                                 continue;
152                         if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
153                                 ufs_quotawarn(dq);
154                                 continue;
155                         }
156                         error = ufs_chkdqchg(ip, change, cred, i);
157                         if (error)
158                                 return (error);
159                 }
160         }
161         for (i = 0; i < MAXQUOTAS; i++) {
162                 if ((dq = ip->i_dquot[i]) == NODQUOT)
163                         continue;
164                 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
165                         ufs_quotawarn(dq);
166                         continue;
167                 }
168                 while (dq->dq_flags & DQ_LOCK) {
169                         dq->dq_flags |= DQ_WANT;
170                         (void) tsleep((caddr_t)dq, 0, "chkdq2", 0);
171                 }
172                 /* Reset timer when crossing soft limit */
173                 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
174                     dq->dq_curblocks < dq->dq_bsoftlimit)
175                         dq->dq_btime = time_second +
176                             VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
177                 dq->dq_curblocks += change;
178                 dq->dq_flags |= DQ_MOD;
179         }
180         return (0);
181 }
182
183 /*
184  * Check for a valid change to a users allocation.
185  * Issue an error message if appropriate.
186  */
187 static int
188 ufs_chkdqchg(struct inode *ip, long change, struct ucred *cred, int type)
189 {
190         struct ufs_dquot *dq = ip->i_dquot[type];
191         long ncurblocks = dq->dq_curblocks + change;
192
193         /*
194          * If user would exceed their hard limit, disallow space allocation.
195          */
196         if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
197                 if ((dq->dq_flags & DQ_BLKS) == 0 &&
198                     ip->i_uid == cred->cr_uid) {
199                         uprintf("\n%s: write failed, %s disk limit reached\n",
200                             ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
201                             quotatypes[type]);
202                         dq->dq_flags |= DQ_BLKS;
203                 }
204                 return (EDQUOT);
205         }
206         /*
207          * If user is over their soft limit for too long, disallow space
208          * allocation. Reset time limit as they cross their soft limit.
209          */
210         if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
211                 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
212                         dq->dq_btime = time_second +
213                             VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
214                         if (ip->i_uid == cred->cr_uid)
215                                 uprintf("\n%s: warning, %s %s\n",
216                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
217                                     quotatypes[type], "disk quota exceeded");
218                         return (0);
219                 }
220                 if (time_second > dq->dq_btime) {
221                         if ((dq->dq_flags & DQ_BLKS) == 0 &&
222                             ip->i_uid == cred->cr_uid) {
223                                 uprintf("\n%s: write failed, %s %s\n",
224                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
225                                     quotatypes[type],
226                                     "disk quota exceeded for too long");
227                                 dq->dq_flags |= DQ_BLKS;
228                         }
229                         return (EDQUOT);
230                 }
231         }
232         return (0);
233 }
234
235 /*
236  * Check the inode limit, applying corrective action.
237  */
238 int
239 ufs_chkiq(struct inode *ip, long change, struct ucred *cred, int flags)
240 {
241         struct ufs_dquot *dq;
242         int i;
243         int ncurinodes, error;
244
245 #ifdef DIAGNOSTIC
246         if ((flags & CHOWN) == 0)
247                 ufs_chkdquot(ip);
248 #endif
249         if (change == 0)
250                 return (0);
251         if (change < 0) {
252                 for (i = 0; i < MAXQUOTAS; i++) {
253                         if ((dq = ip->i_dquot[i]) == NODQUOT)
254                                 continue;
255                         if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
256                                 ufs_quotawarn(dq);
257                                 continue;
258                         }
259                         while (dq->dq_flags & DQ_LOCK) {
260                                 dq->dq_flags |= DQ_WANT;
261                                 (void) tsleep((caddr_t)dq, 0, "chkiq1", 0);
262                         }
263                         ncurinodes = dq->dq_curinodes + change;
264                         if (ncurinodes >= 0)
265                                 dq->dq_curinodes = ncurinodes;
266                         else
267                                 dq->dq_curinodes = 0;
268                         dq->dq_flags &= ~DQ_INODS;
269                         dq->dq_flags |= DQ_MOD;
270                 }
271                 return (0);
272         }
273         if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
274                 for (i = 0; i < MAXQUOTAS; i++) {
275                         if ((dq = ip->i_dquot[i]) == NODQUOT)
276                                 continue;
277                         if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
278                                 ufs_quotawarn(dq);
279                                 continue;
280                         }
281                         error = ufs_chkiqchg(ip, change, cred, i);
282                         if (error)
283                                 return (error);
284                 }
285         }
286         for (i = 0; i < MAXQUOTAS; i++) {
287                 if ((dq = ip->i_dquot[i]) == NODQUOT)
288                         continue;
289                 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
290                         ufs_quotawarn(dq);
291                         continue;
292                 }
293                 while (dq->dq_flags & DQ_LOCK) {
294                         dq->dq_flags |= DQ_WANT;
295                         (void) tsleep((caddr_t)dq, 0, "chkiq2", 0);
296                 }
297                 /* Reset timer when crossing soft limit */
298                 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
299                     dq->dq_curinodes < dq->dq_isoftlimit)
300                         dq->dq_itime = time_second +
301                             VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
302                 dq->dq_curinodes += change;
303                 dq->dq_flags |= DQ_MOD;
304         }
305         return (0);
306 }
307
308 /*
309  * Check for a valid change to a users allocation.
310  * Issue an error message if appropriate.
311  */
312 static int
313 ufs_chkiqchg(struct inode *ip, long change, struct ucred *cred, int type)
314 {
315         struct ufs_dquot *dq = ip->i_dquot[type];
316         long ncurinodes = dq->dq_curinodes + change;
317
318         /*
319          * If user would exceed their hard limit, disallow inode allocation.
320          */
321         if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
322                 if ((dq->dq_flags & DQ_INODS) == 0 &&
323                     ip->i_uid == cred->cr_uid) {
324                         uprintf("\n%s: write failed, %s inode limit reached\n",
325                             ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
326                             quotatypes[type]);
327                         dq->dq_flags |= DQ_INODS;
328                 }
329                 return (EDQUOT);
330         }
331         /*
332          * If user is over their soft limit for too long, disallow inode
333          * allocation. Reset time limit as they cross their soft limit.
334          */
335         if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
336                 if (dq->dq_curinodes < dq->dq_isoftlimit) {
337                         dq->dq_itime = time_second +
338                             VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
339                         if (ip->i_uid == cred->cr_uid)
340                                 uprintf("\n%s: warning, %s %s\n",
341                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
342                                     quotatypes[type], "inode quota exceeded");
343                         return (0);
344                 }
345                 if (time_second > dq->dq_itime) {
346                         if ((dq->dq_flags & DQ_INODS) == 0 &&
347                             ip->i_uid == cred->cr_uid) {
348                                 uprintf("\n%s: write failed, %s %s\n",
349                                     ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
350                                     quotatypes[type],
351                                     "inode quota exceeded for too long");
352                                 dq->dq_flags |= DQ_INODS;
353                         }
354                         return (EDQUOT);
355                 }
356         }
357         return (0);
358 }
359
360 /*
361  * To avoid a deadlock we disallow quota operations on the quota file itself.
362  * This generally means that quotacheck was not run on the filesystem.
363  */
364 static
365 void
366 ufs_quotawarn(struct ufs_dquot *dq)
367 {
368         static int dqticks;
369
370         if (dqticks != ticks / hz) {
371                 dqticks = ticks / hz;
372                 uprintf("%s: warning, quota file expanded, quotacheck "
373                         "was not run!\n",
374                         dq->dq_ump->um_mountp->mnt_stat.f_mntfromname); 
375         }
376 }
377
378 #ifdef DIAGNOSTIC
379 /*
380  * On filesystems with quotas enabled, it is an error for a file to change
381  * size and not to have a dquot structure associated with it.
382  */
383 static void
384 ufs_chkdquot(struct inode *ip)
385 {
386         struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
387         int i;
388
389         for (i = 0; i < MAXQUOTAS; i++) {
390                 if (ump->um_quotas[i] == NULLVP ||
391                     (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
392                         continue;
393                 if (ip->i_dquot[i] == NODQUOT) {
394                         vprint("chkdquot: missing dquot", ITOV(ip));
395                         panic("chkdquot: missing dquot");
396                 }
397         }
398 }
399 #endif
400
401 /*
402  * Code to process quotactl commands.
403  */
404
405 struct scaninfo {
406         int rescan;
407         int type;
408 };
409
410 /*
411  * Q_QUOTAON - set up a quota file for a particular filesystem.
412  */
413 static int ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data);
414
415 int
416 ufs_quotaon(struct ucred *cred, struct mount *mp, int type, caddr_t fname)
417 {
418         struct ufsmount *ump = VFSTOUFS(mp);
419         struct vnode *vp, **vpp;
420         struct ufs_dquot *dq;
421         int error;
422         struct nlookupdata nd;
423         struct scaninfo scaninfo;
424
425         vpp = &ump->um_quotas[type];
426         error = nlookup_init(&nd, fname, UIO_USERSPACE, NLC_FOLLOW|NLC_LOCKVP);
427         if (error == 0)
428                 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
429         if (error == 0 && nd.nl_open_vp->v_type != VREG)
430                 error = EACCES;
431         if (error) {
432                 nlookup_done(&nd);
433                 return (error);
434         }
435         vp = nd.nl_open_vp;
436         nd.nl_open_vp = NULL;
437         nlookup_done(&nd);
438
439         vn_unlock(vp);
440         if (*vpp != vp)
441                 ufs_quotaoff(mp, type);
442         ump->um_qflags[type] |= QTF_OPENING;
443         mp->mnt_flag |= MNT_QUOTA;
444         vsetflags(vp, VSYSTEM);
445         *vpp = vp;
446         /* XXX release duplicate vp if *vpp == vp? */
447         /*
448          * Save the credential of the process that turned on quotas.
449          * Set up the time limits for this quota.
450          */
451         ump->um_cred[type] = crhold(cred);
452         ump->um_btime[type] = MAX_DQ_TIME;
453         ump->um_itime[type] = MAX_IQ_TIME;
454         if (ufs_dqget(NULLVP, 0, ump, type, &dq) == 0) {
455                 if (dq->dq_btime > 0)
456                         ump->um_btime[type] = dq->dq_btime;
457                 if (dq->dq_itime > 0)
458                         ump->um_itime[type] = dq->dq_itime;
459                 ufs_dqrele(NULLVP, dq);
460         }
461         /*
462          * Search vnodes associated with this mount point,
463          * adding references to quota file being opened.
464          * NB: only need to add dquot's for inodes being modified.
465          */
466         scaninfo.rescan = 1;
467         while (scaninfo.rescan) {
468                 scaninfo.rescan = 0;
469                 error = vmntvnodescan(mp, VMSC_GETVP,
470                                         NULL, ufs_quotaon_scan, &scaninfo);
471                 if (error)
472                         break;
473         }
474         ump->um_qflags[type] &= ~QTF_OPENING;
475         if (error)
476                 ufs_quotaoff(mp, type);
477         return (error);
478 }
479
480 static int
481 ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data)
482 {
483         int error;
484         /*struct scaninfo *info = data;*/
485
486         if (vp->v_writecount == 0)
487                 return(0);
488         error = ufs_getinoquota(VTOI(vp));
489         return(error);
490 }
491
492 /*
493  * Q_QUOTAOFF - turn off disk quotas for a filesystem.
494  */
495
496 static int ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data);
497
498 int
499 ufs_quotaoff(struct mount *mp, int type)
500 {
501         struct vnode *qvp;
502         struct ufsmount *ump = VFSTOUFS(mp);
503         int error;
504         struct scaninfo scaninfo;
505
506         if ((qvp = ump->um_quotas[type]) == NULLVP)
507                 return (0);
508         ump->um_qflags[type] |= QTF_CLOSING;
509
510         /*
511          * Search vnodes associated with this mount point,
512          * deleting any references to quota file being closed.
513          */
514         scaninfo.rescan = 1;
515         scaninfo.type = type;
516         while (scaninfo.rescan) {
517                 scaninfo.rescan = 0;
518                 vmntvnodescan(mp, VMSC_GETVP, NULL, ufs_quotaoff_scan, &scaninfo);
519         }
520         ufs_dqflush(qvp);
521         vclrflags(qvp, VSYSTEM);
522         error = vn_close(qvp, FREAD|FWRITE);
523         ump->um_quotas[type] = NULLVP;
524         crfree(ump->um_cred[type]);
525         ump->um_cred[type] = NOCRED;
526         ump->um_qflags[type] &= ~QTF_CLOSING;
527         for (type = 0; type < MAXQUOTAS; type++) {
528                 if (ump->um_quotas[type] != NULLVP)
529                         break;
530         }
531         if (type == MAXQUOTAS)
532                 mp->mnt_flag &= ~MNT_QUOTA;
533         return (error);
534 }
535
536 static int
537 ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data)
538 {
539         struct scaninfo *info = data;
540         struct ufs_dquot *dq;
541         struct inode *ip;
542
543         if (vp->v_type == VNON) {
544                 return(0);
545         }
546         ip = VTOI(vp);
547         dq = ip->i_dquot[info->type];
548         ip->i_dquot[info->type] = NODQUOT;
549         ufs_dqrele(vp, dq);
550         return(0);
551 }
552
553 /*
554  * Q_GETQUOTA - return current values in a dqblk structure.
555  */
556 int
557 ufs_getquota(struct mount *mp, u_long id, int type, caddr_t addr)
558 {
559         struct ufs_dquot *dq;
560         int error;
561
562         error = ufs_dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
563         if (error)
564                 return (error);
565         error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct ufs_dqblk));
566         ufs_dqrele(NULLVP, dq);
567         return (error);
568 }
569
570 /*
571  * Q_SETQUOTA - assign an entire dqblk structure.
572  */
573 int
574 ufs_setquota(struct mount *mp, u_long id, int type, caddr_t addr)
575 {
576         struct ufs_dquot *dq;
577         struct ufs_dquot *ndq;
578         struct ufsmount *ump = VFSTOUFS(mp);
579         struct ufs_dqblk newlim;
580         int error;
581
582         error = copyin(addr, (caddr_t)&newlim, sizeof (struct ufs_dqblk));
583         if (error)
584                 return (error);
585         error = ufs_dqget(NULLVP, id, ump, type, &ndq);
586         if (error)
587                 return (error);
588         dq = ndq;
589         while (dq->dq_flags & DQ_LOCK) {
590                 dq->dq_flags |= DQ_WANT;
591                 (void) tsleep((caddr_t)dq, 0, "setqta", 0);
592         }
593         /*
594          * Copy all but the current values.
595          * Reset time limit if previously had no soft limit or were
596          * under it, but now have a soft limit and are over it.
597          */
598         newlim.dqb_curblocks = dq->dq_curblocks;
599         newlim.dqb_curinodes = dq->dq_curinodes;
600         if (dq->dq_id != 0) {
601                 newlim.dqb_btime = dq->dq_btime;
602                 newlim.dqb_itime = dq->dq_itime;
603         }
604         if (newlim.dqb_bsoftlimit &&
605             dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
606             (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
607                 newlim.dqb_btime = time_second + ump->um_btime[type];
608         if (newlim.dqb_isoftlimit &&
609             dq->dq_curinodes >= newlim.dqb_isoftlimit &&
610             (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
611                 newlim.dqb_itime = time_second + ump->um_itime[type];
612         dq->dq_dqb = newlim;
613         if (dq->dq_curblocks < dq->dq_bsoftlimit)
614                 dq->dq_flags &= ~DQ_BLKS;
615         if (dq->dq_curinodes < dq->dq_isoftlimit)
616                 dq->dq_flags &= ~DQ_INODS;
617         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
618             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
619                 dq->dq_flags |= DQ_FAKE;
620         else
621                 dq->dq_flags &= ~DQ_FAKE;
622         dq->dq_flags |= DQ_MOD;
623         ufs_dqrele(NULLVP, dq);
624         return (0);
625 }
626
627 /*
628  * Q_SETUSE - set current inode and block usage.
629  */
630 int
631 ufs_setuse(struct mount *mp, u_long id, int type, caddr_t addr)
632 {
633         struct ufs_dquot *dq;
634         struct ufsmount *ump = VFSTOUFS(mp);
635         struct ufs_dquot *ndq;
636         struct ufs_dqblk usage;
637         int error;
638
639         error = copyin(addr, (caddr_t)&usage, sizeof (struct ufs_dqblk));
640         if (error)
641                 return (error);
642         error = ufs_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((caddr_t)dq, 0, "setuse", 0);
649         }
650         /*
651          * Reset time limit if have a soft limit and were
652          * previously under it, but are now over it.
653          */
654         if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
655             usage.dqb_curblocks >= dq->dq_bsoftlimit)
656                 dq->dq_btime = time_second + ump->um_btime[type];
657         if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
658             usage.dqb_curinodes >= dq->dq_isoftlimit)
659                 dq->dq_itime = time_second + ump->um_itime[type];
660         dq->dq_curblocks = usage.dqb_curblocks;
661         dq->dq_curinodes = usage.dqb_curinodes;
662         if (dq->dq_curblocks < dq->dq_bsoftlimit)
663                 dq->dq_flags &= ~DQ_BLKS;
664         if (dq->dq_curinodes < dq->dq_isoftlimit)
665                 dq->dq_flags &= ~DQ_INODS;
666         dq->dq_flags |= DQ_MOD;
667         ufs_dqrele(NULLVP, dq);
668         return (0);
669 }
670
671 /*
672  * Q_SYNC - sync quota files to disk.
673  */
674
675 static int ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data);
676
677 int
678 ufs_qsync(struct mount *mp)
679 {
680         struct ufsmount *ump = VFSTOUFS(mp);
681         struct scaninfo scaninfo;
682         int i;
683
684         /*
685          * Check if the mount point has any quotas.
686          * If not, simply return.
687          */
688         for (i = 0; i < MAXQUOTAS; i++)
689                 if (ump->um_quotas[i] != NULLVP)
690                         break;
691         if (i == MAXQUOTAS)
692                 return (0);
693         /*
694          * Search vnodes associated with this mount point,
695          * synchronizing any modified ufs_dquot structures.
696          */
697         scaninfo.rescan = 1;
698         while (scaninfo.rescan) {
699                 scaninfo.rescan = 0;
700                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
701                                 NULL, ufs_qsync_scan, &scaninfo);
702         }
703         return (0);
704 }
705
706 static int
707 ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data)
708 {
709         /*struct scaninfo *info = data;*/
710         struct ufs_dquot *dq;
711         /* int error;*/
712         int i;
713
714         for (i = 0; i < MAXQUOTAS; i++) {
715                 dq = VTOI(vp)->i_dquot[i];
716                 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
717                         ufs_dqsync(vp, dq);
718         }
719         return(0);
720 }
721
722 /*
723  * Code pertaining to management of the in-core dquot data structures.
724  */
725 #define DQHASH(dqvp, id) \
726         (&ufs_dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & ufs_dqhash])
727 static LIST_HEAD(ufs_dqhash, ufs_dquot) *ufs_dqhashtbl;
728 static u_long ufs_dqhash;
729
730 /*
731  * Dquot free list.
732  */
733 #define DQUOTINC        5       /* minimum free dquots desired */
734 static TAILQ_HEAD(ufs_dqfreelist, ufs_dquot) ufs_dqfreelist;
735 static long ufs_numdquot, ufs_desireddquot = DQUOTINC;
736
737 /*
738  * Initialize the quota system.
739  */
740 void
741 ufs_dqinit(void)
742 {
743         ufs_dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &ufs_dqhash);
744         TAILQ_INIT(&ufs_dqfreelist);
745 }
746
747 /*
748  * Obtain a dquot structure for the specified identifier and quota file
749  * reading the information from the file if necessary.
750  */
751 static int
752 ufs_dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
753       struct ufs_dquot **dqp)
754 {
755         struct ufs_dquot *dq;
756         struct ufs_dqhash *dqh;
757         struct vnode *dqvp;
758         struct iovec aiov;
759         struct uio auio;
760         int error;
761
762         dqvp = ump->um_quotas[type];
763         if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
764                 *dqp = NODQUOT;
765                 return (EINVAL);
766         }
767         /*
768          * Check the cache first.
769          */
770         dqh = DQHASH(dqvp, id);
771         LIST_FOREACH(dq, dqh, dq_hash) {
772                 if (dq->dq_id != id ||
773                     dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
774                         continue;
775                 /*
776                  * Cache hit with no references.  Take
777                  * the structure off the free list.
778                  */
779                 if (dq->dq_cnt == 0)
780                         TAILQ_REMOVE(&ufs_dqfreelist, dq, dq_freelist);
781                 DQREF(dq);
782                 *dqp = dq;
783                 return (0);
784         }
785         /*
786          * Not in cache, allocate a new one.
787          */
788         if (TAILQ_EMPTY(&ufs_dqfreelist) && ufs_numdquot < MAXQUOTAS * desiredvnodes)
789                 ufs_desireddquot += DQUOTINC;
790         if (ufs_numdquot < ufs_desireddquot) {
791                 dq = (struct ufs_dquot *)
792                         kmalloc(sizeof *dq, M_DQUOT, M_WAITOK | M_ZERO);
793                 ufs_numdquot++;
794         } else {
795                 if ((dq = TAILQ_FIRST(&ufs_dqfreelist)) == NULL) {
796                         tablefull("dquot");
797                         *dqp = NODQUOT;
798                         return (EUSERS);
799                 }
800                 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
801                         panic("dqget: free dquot isn't");
802                 TAILQ_REMOVE(&ufs_dqfreelist, dq, dq_freelist);
803                 if (dq->dq_ump != NULL)
804                         LIST_REMOVE(dq, dq_hash);
805         }
806         /*
807          * Initialize the contents of the dquot structure.
808          */
809         if (vp != dqvp)
810                 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
811         LIST_INSERT_HEAD(dqh, dq, dq_hash);
812         DQREF(dq);
813         dq->dq_flags = DQ_LOCK;
814         dq->dq_id = id;
815         dq->dq_ump = ump;
816         dq->dq_type = type;
817         auio.uio_iov = &aiov;
818         auio.uio_iovcnt = 1;
819         aiov.iov_base = (caddr_t)&dq->dq_dqb;
820         aiov.iov_len = sizeof (struct ufs_dqblk);
821         auio.uio_resid = sizeof (struct ufs_dqblk);
822         auio.uio_offset = (off_t)(id * sizeof (struct ufs_dqblk));
823         auio.uio_segflg = UIO_SYSSPACE;
824         auio.uio_rw = UIO_READ;
825         auio.uio_td = NULL;
826         error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
827         if (auio.uio_resid == sizeof(struct ufs_dqblk) && error == 0)
828                 bzero((caddr_t)&dq->dq_dqb, sizeof(struct ufs_dqblk));
829         if (vp != dqvp)
830                 vn_unlock(dqvp);
831         if (dq->dq_flags & DQ_WANT)
832                 wakeup((caddr_t)dq);
833         dq->dq_flags = 0;
834         /*
835          * I/O error in reading quota file, release
836          * quota structure and reflect problem to caller.
837          */
838         if (error) {
839                 LIST_REMOVE(dq, dq_hash);
840                 ufs_dqrele(vp, dq);
841                 *dqp = NODQUOT;
842                 return (error);
843         }
844         /*
845          * Check for no limit to enforce.
846          * Initialize time values if necessary.
847          */
848         if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
849             dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
850                 dq->dq_flags |= DQ_FAKE;
851         if (dq->dq_id != 0) {
852                 if (dq->dq_btime == 0)
853                         dq->dq_btime = time_second + ump->um_btime[type];
854                 if (dq->dq_itime == 0)
855                         dq->dq_itime = time_second + ump->um_itime[type];
856         }
857         *dqp = dq;
858         return (0);
859 }
860
861 #ifdef DIAGNOSTIC
862 /*
863  * Obtain a reference to a dquot.
864  */
865 static void
866 ufs_dqref(struct ufs_dquot *dq)
867 {
868         dq->dq_cnt++;
869 }
870 #endif
871
872 /*
873  * Release a reference to a dquot.
874  */
875 void
876 ufs_dqrele(struct vnode *vp, struct ufs_dquot *dq)
877 {
878         if (dq == NODQUOT)
879                 return;
880         if (dq->dq_cnt > 1) {
881                 dq->dq_cnt--;
882                 return;
883         }
884         if (dq->dq_flags & DQ_MOD)
885                 (void)ufs_dqsync(vp, dq);
886         if (--dq->dq_cnt > 0)
887                 return;
888         TAILQ_INSERT_TAIL(&ufs_dqfreelist, dq, dq_freelist);
889 }
890
891 /*
892  * Update the disk quota in the quota file.
893  */
894 static int
895 ufs_dqsync(struct vnode *vp, struct ufs_dquot *dq)
896 {
897         struct vnode *dqvp;
898         struct iovec aiov;
899         struct uio auio;
900         int error;
901
902         if (dq == NODQUOT)
903                 panic("dqsync: dquot");
904         if ((dq->dq_flags & DQ_MOD) == 0)
905                 return (0);
906         if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
907                 panic("dqsync: file");
908         if (vp != dqvp)
909                 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
910         while (dq->dq_flags & DQ_LOCK) {
911                 dq->dq_flags |= DQ_WANT;
912                 (void) tsleep((caddr_t)dq, 0, "dqsync", 0);
913                 if ((dq->dq_flags & DQ_MOD) == 0) {
914                         if (vp != dqvp)
915                                 vn_unlock(dqvp);
916                         return (0);
917                 }
918         }
919         dq->dq_flags |= DQ_LOCK;
920         auio.uio_iov = &aiov;
921         auio.uio_iovcnt = 1;
922         aiov.iov_base = (caddr_t)&dq->dq_dqb;
923         aiov.iov_len = sizeof (struct ufs_dqblk);
924         auio.uio_resid = sizeof (struct ufs_dqblk);
925         auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct ufs_dqblk));
926         auio.uio_segflg = UIO_SYSSPACE;
927         auio.uio_rw = UIO_WRITE;
928         auio.uio_td = NULL;
929         error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
930         if (auio.uio_resid && error == 0)
931                 error = EIO;
932         if (dq->dq_flags & DQ_WANT)
933                 wakeup((caddr_t)dq);
934         dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
935         if (vp != dqvp)
936                 vn_unlock(dqvp);
937         return (error);
938 }
939
940 /*
941  * Flush all entries from the cache for a particular vnode.
942  */
943 static void
944 ufs_dqflush(struct vnode *vp)
945 {
946         struct ufs_dquot *dq, *nextdq;
947         struct ufs_dqhash *dqh;
948
949         /*
950          * Move all dquot's that used to refer to this quota
951          * file off their hash chains (they will eventually
952          * fall off the head of the free list and be re-used).
953          */
954         for (dqh = &ufs_dqhashtbl[ufs_dqhash]; dqh >= ufs_dqhashtbl; dqh--) {
955                 for (dq = dqh->lh_first; dq; dq = nextdq) {
956                         nextdq = dq->dq_hash.le_next;
957                         if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
958                                 continue;
959                         if (dq->dq_cnt)
960                                 panic("dqflush: stray dquot");
961                         LIST_REMOVE(dq, dq_hash);
962                         dq->dq_ump = NULL;
963                 }
964         }
965 }