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