ufs_quota.c revision 1.51 1 /* $NetBSD: ufs_quota.c,v 1.51 2007/10/10 20:42:40 ad Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1990, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.51 2007/10/10 20:42:40 ad Exp $");
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/malloc.h>
45 #include <sys/file.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/kauth.h>
50
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/ufsmount.h>
54 #include <ufs/ufs/ufs_extern.h>
55
56 /*
57 * The following structure records disk usage for a user or group on a
58 * filesystem. There is one allocated for each quota that exists on any
59 * filesystem for the current user or group. A cache is kept of recently
60 * used entries.
61 * Field markings and the corresponding locks:
62 * h: dqlock
63 * d: dq_interlock
64 *
65 * Lock order is: dq_interlock -> dqlock
66 * dq_interlock -> dqvp
67 */
68 struct dquot {
69 LIST_ENTRY(dquot) dq_hash; /* h: hash list */
70 u_int16_t dq_flags; /* d: flags, see below */
71 u_int16_t dq_type; /* d: quota type of this dquot */
72 u_int32_t dq_cnt; /* h: count of active references */
73 u_int32_t dq_id; /* d: identifier this applies to */
74 struct ufsmount *dq_ump; /* d: filesystem this is taken from */
75 kmutex_t dq_interlock; /* d: lock this dquot */
76 struct dqblk dq_dqb; /* d: actual usage & quotas */
77 };
78 /*
79 * Flag values.
80 */
81 #define DQ_MOD 0x04 /* this quota modified since read */
82 #define DQ_FAKE 0x08 /* no limits here, just usage */
83 #define DQ_BLKS 0x10 /* has been warned about blk limit */
84 #define DQ_INODS 0x20 /* has been warned about inode limit */
85 /*
86 * Shorthand notation.
87 */
88 #define dq_bhardlimit dq_dqb.dqb_bhardlimit
89 #define dq_bsoftlimit dq_dqb.dqb_bsoftlimit
90 #define dq_curblocks dq_dqb.dqb_curblocks
91 #define dq_ihardlimit dq_dqb.dqb_ihardlimit
92 #define dq_isoftlimit dq_dqb.dqb_isoftlimit
93 #define dq_curinodes dq_dqb.dqb_curinodes
94 #define dq_btime dq_dqb.dqb_btime
95 #define dq_itime dq_dqb.dqb_itime
96 /*
97 * If the system has never checked for a quota for this file, then it is
98 * set to NODQUOT. Once a write attempt is made the inode pointer is set
99 * to reference a dquot structure.
100 */
101 #define NODQUOT NULL
102
103 static int chkdqchg(struct inode *, int64_t, kauth_cred_t, int);
104 static int chkiqchg(struct inode *, int32_t, kauth_cred_t, int);
105 #ifdef DIAGNOSTIC
106 static void dqflush(struct vnode *);
107 #endif
108 static int dqget(struct vnode *, u_long, struct ufsmount *, int,
109 struct dquot **);
110 static void dqref(struct dquot *);
111 static void dqrele(struct vnode *, struct dquot *);
112 static int dqsync(struct vnode *, struct dquot *);
113
114 static kmutex_t dqlock;
115 static kcondvar_t dqcv;
116 /*
117 * Quota name to error message mapping.
118 */
119 static const char *quotatypes[] = INITQFNAMES;
120
121 /*
122 * Set up the quotas for an inode.
123 *
124 * This routine completely defines the semantics of quotas.
125 * If other criterion want to be used to establish quotas, the
126 * MAXQUOTAS value in quotas.h should be increased, and the
127 * additional dquots set up here.
128 */
129 int
130 getinoquota(struct inode *ip)
131 {
132 struct ufsmount *ump = ip->i_ump;
133 struct vnode *vp = ITOV(ip);
134 int i, error;
135 u_int32_t ino_ids[MAXQUOTAS];
136
137 /*
138 * To avoid deadlocks never update quotas for quota files
139 * on the same file system
140 */
141 for (i = 0; i < MAXQUOTAS; i++)
142 if (ITOV(ip) == ump->um_quotas[i])
143 return 0;
144
145 ino_ids[USRQUOTA] = ip->i_uid;
146 ino_ids[GRPQUOTA] = ip->i_gid;
147 for (i = 0; i < MAXQUOTAS; i++) {
148 /*
149 * If the file id changed the quota needs update.
150 */
151 if (ip->i_dquot[i] != NODQUOT &&
152 ip->i_dquot[i]->dq_id != ino_ids[i]) {
153 dqrele(ITOV(ip), ip->i_dquot[i]);
154 ip->i_dquot[i] = NODQUOT;
155 }
156 /*
157 * Set up the quota based on file id.
158 * EINVAL means that quotas are not enabled.
159 */
160 if (ip->i_dquot[i] == NODQUOT &&
161 (error = dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
162 error != EINVAL)
163 return (error);
164 }
165 return 0;
166 }
167
168 /*
169 * Initialize the quota fields of an inode.
170 */
171 void
172 ufsquota_init(struct inode *ip)
173 {
174 int i;
175
176 for (i = 0; i < MAXQUOTAS; i++)
177 ip->i_dquot[i] = NODQUOT;
178 }
179
180 /*
181 * Release the quota fields from an inode.
182 */
183 void
184 ufsquota_free(struct inode *ip)
185 {
186 int i;
187
188 for (i = 0; i < MAXQUOTAS; i++) {
189 dqrele(ITOV(ip), ip->i_dquot[i]);
190 ip->i_dquot[i] = NODQUOT;
191 }
192 }
193
194 /*
195 * Update disk usage, and take corrective action.
196 */
197 int
198 chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
199 {
200 struct dquot *dq;
201 int i;
202 int ncurblocks, error;
203
204 if ((error = getinoquota(ip)) != 0)
205 return error;
206 if (change == 0)
207 return (0);
208 if (change < 0) {
209 for (i = 0; i < MAXQUOTAS; i++) {
210 if ((dq = ip->i_dquot[i]) == NODQUOT)
211 continue;
212 mutex_enter(&dq->dq_interlock);
213 ncurblocks = dq->dq_curblocks + change;
214 if (ncurblocks >= 0)
215 dq->dq_curblocks = ncurblocks;
216 else
217 dq->dq_curblocks = 0;
218 dq->dq_flags &= ~DQ_BLKS;
219 dq->dq_flags |= DQ_MOD;
220 mutex_exit(&dq->dq_interlock);
221 }
222 return (0);
223 }
224 if ((flags & FORCE) == 0 &&
225 kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
226 for (i = 0; i < MAXQUOTAS; i++) {
227 if ((dq = ip->i_dquot[i]) == NODQUOT)
228 continue;
229 mutex_enter(&dq->dq_interlock);
230 error = chkdqchg(ip, change, cred, i);
231 mutex_exit(&dq->dq_interlock);
232 if (error != 0)
233 return (error);
234 }
235 }
236 for (i = 0; i < MAXQUOTAS; i++) {
237 if ((dq = ip->i_dquot[i]) == NODQUOT)
238 continue;
239 mutex_enter(&dq->dq_interlock);
240 dq->dq_curblocks += change;
241 dq->dq_flags |= DQ_MOD;
242 mutex_exit(&dq->dq_interlock);
243 }
244 return (0);
245 }
246
247 /*
248 * Check for a valid change to a users allocation.
249 * Issue an error message if appropriate.
250 */
251 static int
252 chkdqchg(struct inode *ip, int64_t change, kauth_cred_t cred, int type)
253 {
254 struct dquot *dq = ip->i_dquot[type];
255 long ncurblocks = dq->dq_curblocks + change;
256
257 KASSERT(mutex_owned(&dq->dq_interlock));
258 /*
259 * If user would exceed their hard limit, disallow space allocation.
260 */
261 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
262 if ((dq->dq_flags & DQ_BLKS) == 0 &&
263 ip->i_uid == kauth_cred_geteuid(cred)) {
264 uprintf("\n%s: write failed, %s disk limit reached\n",
265 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
266 quotatypes[type]);
267 dq->dq_flags |= DQ_BLKS;
268 }
269 return (EDQUOT);
270 }
271 /*
272 * If user is over their soft limit for too long, disallow space
273 * allocation. Reset time limit as they cross their soft limit.
274 */
275 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
276 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
277 dq->dq_btime = time_second + ip->i_ump->um_btime[type];
278 if (ip->i_uid == kauth_cred_geteuid(cred))
279 uprintf("\n%s: warning, %s %s\n",
280 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
281 quotatypes[type], "disk quota exceeded");
282 return (0);
283 }
284 if (time_second > dq->dq_btime) {
285 if ((dq->dq_flags & DQ_BLKS) == 0 &&
286 ip->i_uid == kauth_cred_geteuid(cred)) {
287 uprintf("\n%s: write failed, %s %s\n",
288 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
289 quotatypes[type],
290 "disk quota exceeded for too long");
291 dq->dq_flags |= DQ_BLKS;
292 }
293 return (EDQUOT);
294 }
295 }
296 return (0);
297 }
298
299 /*
300 * Check the inode limit, applying corrective action.
301 */
302 int
303 chkiq(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
304 {
305 struct dquot *dq;
306 int i;
307 int ncurinodes, error;
308
309 if ((error = getinoquota(ip)) != 0)
310 return error;
311 if (change == 0)
312 return (0);
313 if (change < 0) {
314 for (i = 0; i < MAXQUOTAS; i++) {
315 if ((dq = ip->i_dquot[i]) == NODQUOT)
316 continue;
317 mutex_enter(&dq->dq_interlock);
318 ncurinodes = dq->dq_curinodes + change;
319 if (ncurinodes >= 0)
320 dq->dq_curinodes = ncurinodes;
321 else
322 dq->dq_curinodes = 0;
323 dq->dq_flags &= ~DQ_INODS;
324 dq->dq_flags |= DQ_MOD;
325 mutex_exit(&dq->dq_interlock);
326 }
327 return (0);
328 }
329 if ((flags & FORCE) == 0 && kauth_authorize_generic(cred,
330 KAUTH_GENERIC_ISSUSER, NULL) != 0) {
331 for (i = 0; i < MAXQUOTAS; i++) {
332 if ((dq = ip->i_dquot[i]) == NODQUOT)
333 continue;
334 mutex_enter(&dq->dq_interlock);
335 error = chkiqchg(ip, change, cred, i);
336 mutex_exit(&dq->dq_interlock);
337 if (error != 0)
338 return (error);
339 }
340 }
341 for (i = 0; i < MAXQUOTAS; i++) {
342 if ((dq = ip->i_dquot[i]) == NODQUOT)
343 continue;
344 mutex_enter(&dq->dq_interlock);
345 dq->dq_curinodes += change;
346 dq->dq_flags |= DQ_MOD;
347 mutex_exit(&dq->dq_interlock);
348 }
349 return (0);
350 }
351
352 /*
353 * Check for a valid change to a users allocation.
354 * Issue an error message if appropriate.
355 */
356 static int
357 chkiqchg(struct inode *ip, int32_t change, kauth_cred_t cred, int type)
358 {
359 struct dquot *dq = ip->i_dquot[type];
360 long ncurinodes = dq->dq_curinodes + change;
361
362 KASSERT(mutex_owned(&dq->dq_interlock));
363 /*
364 * If user would exceed their hard limit, disallow inode allocation.
365 */
366 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
367 if ((dq->dq_flags & DQ_INODS) == 0 &&
368 ip->i_uid == kauth_cred_geteuid(cred)) {
369 uprintf("\n%s: write failed, %s inode limit reached\n",
370 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
371 quotatypes[type]);
372 dq->dq_flags |= DQ_INODS;
373 }
374 return (EDQUOT);
375 }
376 /*
377 * If user is over their soft limit for too long, disallow inode
378 * allocation. Reset time limit as they cross their soft limit.
379 */
380 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
381 if (dq->dq_curinodes < dq->dq_isoftlimit) {
382 dq->dq_itime = time_second + ip->i_ump->um_itime[type];
383 if (ip->i_uid == kauth_cred_geteuid(cred))
384 uprintf("\n%s: warning, %s %s\n",
385 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
386 quotatypes[type], "inode quota exceeded");
387 return (0);
388 }
389 if (time_second > dq->dq_itime) {
390 if ((dq->dq_flags & DQ_INODS) == 0 &&
391 ip->i_uid == kauth_cred_geteuid(cred)) {
392 uprintf("\n%s: write failed, %s %s\n",
393 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
394 quotatypes[type],
395 "inode quota exceeded for too long");
396 dq->dq_flags |= DQ_INODS;
397 }
398 return (EDQUOT);
399 }
400 }
401 return (0);
402 }
403
404 /*
405 * Code to process quotactl commands.
406 */
407
408 /*
409 * Q_QUOTAON - set up a quota file for a particular file system.
410 */
411 int
412 quotaon(struct lwp *l, struct mount *mp, int type, void *fname)
413 {
414 struct ufsmount *ump = VFSTOUFS(mp);
415 struct vnode *vp, **vpp;
416 struct vnode *nextvp;
417 struct dquot *dq;
418 int error;
419 struct nameidata nd;
420
421 vpp = &ump->um_quotas[type];
422 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, l);
423 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
424 return (error);
425 vp = nd.ni_vp;
426 VOP_UNLOCK(vp, 0);
427 if (vp->v_type != VREG) {
428 (void) vn_close(vp, FREAD|FWRITE, l->l_cred, l);
429 return (EACCES);
430 }
431 if (*vpp != vp)
432 quotaoff(l, mp, type);
433 mutex_enter(&dqlock);
434 while ((ump->um_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
435 cv_wait(&dqcv, &dqlock);
436 ump->um_qflags[type] |= QTF_OPENING;
437 mutex_exit(&dqlock);
438 mp->mnt_flag |= MNT_QUOTA;
439 vp->v_vflag |= VV_SYSTEM; /* XXXSMP */
440 *vpp = vp;
441 /*
442 * Save the credential of the process that turned on quotas.
443 * Set up the time limits for this quota.
444 */
445 kauth_cred_hold(l->l_cred);
446 ump->um_cred[type] = l->l_cred;
447 ump->um_btime[type] = MAX_DQ_TIME;
448 ump->um_itime[type] = MAX_IQ_TIME;
449 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
450 if (dq->dq_btime > 0)
451 ump->um_btime[type] = dq->dq_btime;
452 if (dq->dq_itime > 0)
453 ump->um_itime[type] = dq->dq_itime;
454 dqrele(NULLVP, dq);
455 }
456 /*
457 * Search vnodes associated with this mount point,
458 * adding references to quota file being opened.
459 * NB: only need to add dquot's for inodes being modified.
460 */
461 again:
462 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
463 nextvp = TAILQ_NEXT(vp, v_mntvnodes);
464 if (vp->v_mount != mp)
465 goto again;
466 if (vp->v_type == VNON ||vp->v_writecount == 0)
467 continue;
468 if (vget(vp, LK_EXCLUSIVE))
469 goto again;
470 if ((error = getinoquota(VTOI(vp))) != 0) {
471 vput(vp);
472 break;
473 }
474 vput(vp);
475 /* if the list changed, start again */
476 if (TAILQ_NEXT(vp, v_mntvnodes) != nextvp)
477 goto again;
478 }
479 mutex_enter(&dqlock);
480 ump->um_qflags[type] &= ~QTF_OPENING;
481 cv_broadcast(&dqcv);
482 mutex_exit(&dqlock);
483 if (error)
484 quotaoff(l, mp, type);
485 return (error);
486 }
487
488 /*
489 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
490 */
491 int
492 quotaoff(struct lwp *l, struct mount *mp, int type)
493 {
494 struct vnode *vp;
495 struct vnode *qvp, *nextvp;
496 struct ufsmount *ump = VFSTOUFS(mp);
497 struct dquot *dq;
498 struct inode *ip;
499 kauth_cred_t cred;
500 int i, error;
501
502 mutex_enter(&dqlock);
503 while ((ump->um_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
504 cv_wait(&dqcv, &dqlock);
505 if ((qvp = ump->um_quotas[type]) == NULLVP) {
506 mutex_exit(&dqlock);
507 return (0);
508 }
509 ump->um_qflags[type] |= QTF_CLOSING;
510 mutex_exit(&dqlock);
511 /*
512 * Search vnodes associated with this mount point,
513 * deleting any references to quota file being closed.
514 */
515 again:
516 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
517 nextvp = TAILQ_NEXT(vp, v_mntvnodes);
518 if (vp->v_mount != mp)
519 goto again;
520 if (vp->v_type == VNON)
521 continue;
522 if (vget(vp, LK_EXCLUSIVE))
523 goto again;
524 ip = VTOI(vp);
525 dq = ip->i_dquot[type];
526 ip->i_dquot[type] = NODQUOT;
527 dqrele(vp, dq);
528 vput(vp);
529 /* if the list changed, start again */
530 if (TAILQ_NEXT(vp, v_mntvnodes) != nextvp)
531 goto again;
532 }
533 #ifdef DIAGNOSTIC
534 dqflush(qvp);
535 #endif
536 qvp->v_vflag &= ~VV_SYSTEM;
537 error = vn_close(qvp, FREAD|FWRITE, l->l_cred, l);
538 mutex_enter(&dqlock);
539 ump->um_quotas[type] = NULLVP;
540 cred = ump->um_cred[type];
541 ump->um_cred[type] = NOCRED;
542 for (i = 0; i < MAXQUOTAS; i++)
543 if (ump->um_quotas[i] != NULLVP)
544 break;
545 ump->um_qflags[type] &= ~QTF_CLOSING;
546 cv_broadcast(&dqcv);
547 mutex_exit(&dqlock);
548 kauth_cred_free(cred);
549 if (i == MAXQUOTAS)
550 mp->mnt_flag &= ~MNT_QUOTA;
551 return (error);
552 }
553
554 /*
555 * Q_GETQUOTA - return current values in a dqblk structure.
556 */
557 int
558 getquota(struct mount *mp, u_long id, int type, void *addr)
559 {
560 struct dquot *dq;
561 int error;
562
563 if ((error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq)) != 0)
564 return (error);
565 error = copyout((void *)&dq->dq_dqb, addr, sizeof (struct dqblk));
566 dqrele(NULLVP, dq);
567 return (error);
568 }
569
570 /*
571 * Q_SETQUOTA - assign an entire dqblk structure.
572 */
573 int
574 setquota(struct mount *mp, u_long id, int type, void *addr)
575 {
576 struct dquot *dq;
577 struct dquot *ndq;
578 struct ufsmount *ump = VFSTOUFS(mp);
579 struct dqblk newlim;
580 int error;
581
582 error = copyin(addr, (void *)&newlim, sizeof (struct dqblk));
583 if (error)
584 return (error);
585 if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
586 return (error);
587 dq = ndq;
588 mutex_enter(&dq->dq_interlock);
589 /*
590 * Copy all but the current values.
591 * Reset time limit if previously had no soft limit or were
592 * under it, but now have a soft limit and are over it.
593 */
594 newlim.dqb_curblocks = dq->dq_curblocks;
595 newlim.dqb_curinodes = dq->dq_curinodes;
596 if (dq->dq_id != 0) {
597 newlim.dqb_btime = dq->dq_btime;
598 newlim.dqb_itime = dq->dq_itime;
599 }
600 if (newlim.dqb_bsoftlimit &&
601 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
602 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
603 newlim.dqb_btime = time_second + ump->um_btime[type];
604 if (newlim.dqb_isoftlimit &&
605 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
606 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
607 newlim.dqb_itime = time_second + ump->um_itime[type];
608 dq->dq_dqb = newlim;
609 if (dq->dq_curblocks < dq->dq_bsoftlimit)
610 dq->dq_flags &= ~DQ_BLKS;
611 if (dq->dq_curinodes < dq->dq_isoftlimit)
612 dq->dq_flags &= ~DQ_INODS;
613 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
614 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
615 dq->dq_flags |= DQ_FAKE;
616 else
617 dq->dq_flags &= ~DQ_FAKE;
618 dq->dq_flags |= DQ_MOD;
619 mutex_exit(&dq->dq_interlock);
620 dqrele(NULLVP, dq);
621 return (0);
622 }
623
624 /*
625 * Q_SETUSE - set current inode and block usage.
626 */
627 int
628 setuse(struct mount *mp, u_long id, int type, void *addr)
629 {
630 struct dquot *dq;
631 struct ufsmount *ump = VFSTOUFS(mp);
632 struct dquot *ndq;
633 struct dqblk usage;
634 int error;
635
636 error = copyin(addr, (void *)&usage, sizeof (struct dqblk));
637 if (error)
638 return (error);
639 if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
640 return (error);
641 dq = ndq;
642 mutex_enter(&dq->dq_interlock);
643 /*
644 * Reset time limit if have a soft limit and were
645 * previously under it, but are now over it.
646 */
647 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
648 usage.dqb_curblocks >= dq->dq_bsoftlimit)
649 dq->dq_btime = time_second + ump->um_btime[type];
650 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
651 usage.dqb_curinodes >= dq->dq_isoftlimit)
652 dq->dq_itime = time_second + ump->um_itime[type];
653 dq->dq_curblocks = usage.dqb_curblocks;
654 dq->dq_curinodes = usage.dqb_curinodes;
655 if (dq->dq_curblocks < dq->dq_bsoftlimit)
656 dq->dq_flags &= ~DQ_BLKS;
657 if (dq->dq_curinodes < dq->dq_isoftlimit)
658 dq->dq_flags &= ~DQ_INODS;
659 dq->dq_flags |= DQ_MOD;
660 mutex_exit(&dq->dq_interlock);
661 dqrele(NULLVP, dq);
662 return (0);
663 }
664
665 /*
666 * Q_SYNC - sync quota files to disk.
667 */
668 int
669 qsync(struct mount *mp)
670 {
671 struct ufsmount *ump = VFSTOUFS(mp);
672 struct vnode *vp, *nextvp;
673 struct dquot *dq;
674 int i, error;
675
676 /*
677 * Check if the mount point has any quotas.
678 * If not, simply return.
679 */
680 for (i = 0; i < MAXQUOTAS; i++)
681 if (ump->um_quotas[i] != NULLVP)
682 break;
683 if (i == MAXQUOTAS)
684 return (0);
685 /*
686 * Search vnodes associated with this mount point,
687 * synchronizing any modified dquot structures.
688 */
689 simple_lock(&mntvnode_slock);
690 again:
691 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
692 nextvp = TAILQ_NEXT(vp, v_mntvnodes);
693 if (vp->v_mount != mp)
694 goto again;
695 if (vp->v_type == VNON)
696 continue;
697 simple_lock(&vp->v_interlock);
698 simple_unlock(&mntvnode_slock);
699 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
700 if (error) {
701 simple_lock(&mntvnode_slock);
702 if (error == ENOENT)
703 goto again;
704 continue;
705 }
706 for (i = 0; i < MAXQUOTAS; i++) {
707 dq = VTOI(vp)->i_dquot[i];
708 if (dq == NODQUOT)
709 continue;
710 mutex_enter(&dq->dq_interlock);
711 if (dq->dq_flags & DQ_MOD)
712 dqsync(vp, dq);
713 mutex_exit(&dq->dq_interlock);
714 }
715 vput(vp);
716 simple_lock(&mntvnode_slock);
717 /* if the list changed, start again */
718 if (TAILQ_NEXT(vp, v_mntvnodes) != nextvp)
719 goto again;
720 }
721 simple_unlock(&mntvnode_slock);
722 return (0);
723 }
724
725 /*
726 * Code pertaining to management of the in-core dquot data structures.
727 */
728 #define DQHASH(dqvp, id) \
729 (((((long)(dqvp)) >> 8) + id) & dqhash)
730 static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
731 static u_long dqhash;
732 static struct pool dquot_pool;
733
734 MALLOC_JUSTDEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
735
736 /*
737 * Initialize the quota system.
738 */
739 void
740 dqinit(void)
741 {
742
743 mutex_init(&dqlock, MUTEX_DEFAULT, IPL_NONE);
744 cv_init(&dqcv, "quota");
745 malloc_type_attach(M_DQUOT);
746 dqhashtbl =
747 hashinit(desiredvnodes, HASH_LIST, M_DQUOT, M_WAITOK, &dqhash);
748 pool_init(&dquot_pool, sizeof(struct dquot), 0, 0, 0, "ufsdqpl",
749 &pool_allocator_nointr, IPL_NONE);
750 }
751
752 void
753 dqreinit(void)
754 {
755 struct dquot *dq;
756 struct dqhashhead *oldhash, *hash;
757 struct vnode *dqvp;
758 u_long oldmask, mask, hashval;
759 int i;
760
761 hash = hashinit(desiredvnodes, HASH_LIST, M_DQUOT, M_WAITOK, &mask);
762 mutex_enter(&dqlock);
763 oldhash = dqhashtbl;
764 oldmask = dqhash;
765 dqhashtbl = hash;
766 dqhash = mask;
767 for (i = 0; i <= oldmask; i++) {
768 while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
769 dqvp = dq->dq_ump->um_quotas[dq->dq_type];
770 LIST_REMOVE(dq, dq_hash);
771 hashval = DQHASH(dqvp, dq->dq_id);
772 LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
773 }
774 }
775 mutex_exit(&dqlock);
776 hashdone(oldhash, M_DQUOT);
777 }
778
779 /*
780 * Free resources held by quota system.
781 */
782 void
783 dqdone(void)
784 {
785
786 pool_destroy(&dquot_pool);
787 hashdone(dqhashtbl, M_DQUOT);
788 malloc_type_detach(M_DQUOT);
789 cv_destroy(&dqcv);
790 mutex_destroy(&dqlock);
791 }
792
793 /*
794 * Obtain a dquot structure for the specified identifier and quota file
795 * reading the information from the file if necessary.
796 */
797 static int
798 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
799 struct dquot **dqp)
800 {
801 struct dquot *dq, *ndq;
802 struct dqhashhead *dqh;
803 struct vnode *dqvp;
804 struct iovec aiov;
805 struct uio auio;
806 int error;
807
808 /* Lock to see an up to date value for QTF_CLOSING. */
809 mutex_enter(&dqlock);
810 dqvp = ump->um_quotas[type];
811 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
812 mutex_exit(&dqlock);
813 *dqp = NODQUOT;
814 return (EINVAL);
815 }
816 KASSERT(dqvp != vp);
817 /*
818 * Check the cache first.
819 */
820 dqh = &dqhashtbl[DQHASH(dqvp, id)];
821 LIST_FOREACH(dq, dqh, dq_hash) {
822 if (dq->dq_id != id ||
823 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
824 continue;
825 KASSERT(dq->dq_cnt > 0);
826 dqref(dq);
827 mutex_exit(&dqlock);
828 *dqp = dq;
829 return (0);
830 }
831 /*
832 * Not in cache, allocate a new one.
833 */
834 mutex_exit(&dqlock);
835 ndq = pool_get(&dquot_pool, PR_WAITOK);
836 /*
837 * Initialize the contents of the dquot structure.
838 */
839 memset((char *)ndq, 0, sizeof *ndq);
840 ndq->dq_flags = 0;
841 ndq->dq_id = id;
842 ndq->dq_ump = ump;
843 ndq->dq_type = type;
844 mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
845 mutex_enter(&dqlock);
846 dqh = &dqhashtbl[DQHASH(dqvp, id)];
847 LIST_FOREACH(dq, dqh, dq_hash) {
848 if (dq->dq_id != id ||
849 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
850 continue;
851 /*
852 * Another thread beat us allocating this dquot.
853 */
854 KASSERT(dq->dq_cnt > 0);
855 dqref(dq);
856 mutex_exit(&dqlock);
857 pool_put(&dquot_pool, ndq);
858 *dqp = dq;
859 return 0;
860 }
861 dq = ndq;
862 LIST_INSERT_HEAD(dqh, dq, dq_hash);
863 dqref(dq);
864 mutex_enter(&dq->dq_interlock);
865 mutex_exit(&dqlock);
866 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
867 auio.uio_iov = &aiov;
868 auio.uio_iovcnt = 1;
869 aiov.iov_base = (void *)&dq->dq_dqb;
870 aiov.iov_len = sizeof (struct dqblk);
871 auio.uio_resid = sizeof (struct dqblk);
872 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
873 auio.uio_rw = UIO_READ;
874 UIO_SETUP_SYSSPACE(&auio);
875 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
876 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
877 memset((void *)&dq->dq_dqb, 0, sizeof(struct dqblk));
878 VOP_UNLOCK(dqvp, 0);
879 /*
880 * I/O error in reading quota file, release
881 * quota structure and reflect problem to caller.
882 */
883 if (error) {
884 mutex_enter(&dqlock);
885 LIST_REMOVE(dq, dq_hash);
886 mutex_exit(&dqlock);
887 mutex_exit(&dq->dq_interlock);
888 dqrele(vp, dq);
889 *dqp = NODQUOT;
890 return (error);
891 }
892 /*
893 * Check for no limit to enforce.
894 * Initialize time values if necessary.
895 */
896 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
897 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
898 dq->dq_flags |= DQ_FAKE;
899 if (dq->dq_id != 0) {
900 if (dq->dq_btime == 0)
901 dq->dq_btime = time_second + ump->um_btime[type];
902 if (dq->dq_itime == 0)
903 dq->dq_itime = time_second + ump->um_itime[type];
904 }
905 mutex_exit(&dq->dq_interlock);
906 *dqp = dq;
907 return (0);
908 }
909
910 /*
911 * Obtain a reference to a dquot.
912 */
913 static void
914 dqref(struct dquot *dq)
915 {
916
917 KASSERT(mutex_owned(&dqlock));
918 dq->dq_cnt++;
919 KASSERT(dq->dq_cnt > 0);
920 }
921
922 /*
923 * Release a reference to a dquot.
924 */
925 static void
926 dqrele(struct vnode *vp, struct dquot *dq)
927 {
928
929 if (dq == NODQUOT)
930 return;
931 mutex_enter(&dq->dq_interlock);
932 for (;;) {
933 mutex_enter(&dqlock);
934 if (dq->dq_cnt > 1) {
935 dq->dq_cnt--;
936 mutex_exit(&dqlock);
937 mutex_exit(&dq->dq_interlock);
938 return;
939 }
940 if ((dq->dq_flags & DQ_MOD) == 0)
941 break;
942 mutex_exit(&dqlock);
943 (void) dqsync(vp, dq);
944 }
945 KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
946 LIST_REMOVE(dq, dq_hash);
947 mutex_exit(&dqlock);
948 mutex_exit(&dq->dq_interlock);
949 mutex_destroy(&dq->dq_interlock);
950 pool_put(&dquot_pool, dq);
951 }
952
953 /*
954 * Update the disk quota in the quota file.
955 */
956 static int
957 dqsync(struct vnode *vp, struct dquot *dq)
958 {
959 struct vnode *dqvp;
960 struct iovec aiov;
961 struct uio auio;
962 int error;
963
964 if (dq == NODQUOT)
965 panic("dqsync: dquot");
966 KASSERT(mutex_owned(&dq->dq_interlock));
967 if ((dq->dq_flags & DQ_MOD) == 0)
968 return (0);
969 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
970 panic("dqsync: file");
971 KASSERT(dqvp != vp);
972 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
973 auio.uio_iov = &aiov;
974 auio.uio_iovcnt = 1;
975 aiov.iov_base = (void *)&dq->dq_dqb;
976 aiov.iov_len = sizeof (struct dqblk);
977 auio.uio_resid = sizeof (struct dqblk);
978 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
979 auio.uio_rw = UIO_WRITE;
980 UIO_SETUP_SYSSPACE(&auio);
981 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
982 if (auio.uio_resid && error == 0)
983 error = EIO;
984 dq->dq_flags &= ~DQ_MOD;
985 VOP_UNLOCK(dqvp, 0);
986 return (error);
987 }
988
989 #ifdef DIAGNOSTIC
990 /*
991 * Check the hash chains for stray dquot's.
992 */
993 static void
994 dqflush(struct vnode *vp)
995 {
996 struct dquot *dq;
997 int i;
998
999 mutex_enter(&dqlock);
1000 for (i = 0; i <= dqhash; i++)
1001 LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
1002 KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
1003 mutex_exit(&dqlock);
1004 }
1005 #endif
1006