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