ufs_quota.c revision 1.66 1 /* $NetBSD: ufs_quota.c,v 1.66 2010/06/24 13:03:20 hannken 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.66 2010/06/24 13:03:20 hannken 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/file.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/kauth.h>
49
50 #include <ufs/ufs/quota.h>
51 #include <ufs/ufs/inode.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ufs/ufs_extern.h>
54
55 /*
56 * The following structure records disk usage for a user or group on a
57 * filesystem. There is one allocated for each quota that exists on any
58 * filesystem for the current user or group. A cache is kept of recently
59 * used entries.
60 * Field markings and the corresponding locks:
61 * h: dqlock
62 * d: dq_interlock
63 *
64 * Lock order is: dq_interlock -> dqlock
65 * dq_interlock -> dqvp
66 */
67 struct dquot {
68 LIST_ENTRY(dquot) dq_hash; /* h: hash list */
69 u_int16_t dq_flags; /* d: flags, see below */
70 u_int16_t dq_type; /* d: quota type of this dquot */
71 u_int32_t dq_cnt; /* h: count of active references */
72 u_int32_t dq_id; /* d: identifier this applies to */
73 struct ufsmount *dq_ump; /* d: filesystem this is taken from */
74 kmutex_t dq_interlock; /* d: lock this dquot */
75 struct dqblk dq_dqb; /* d: actual usage & quotas */
76 };
77 /*
78 * Flag values.
79 */
80 #define DQ_MOD 0x04 /* this quota modified since read */
81 #define DQ_FAKE 0x08 /* no limits here, just usage */
82 #define DQ_BLKS 0x10 /* has been warned about blk limit */
83 #define DQ_INODS 0x20 /* has been warned about inode limit */
84 /*
85 * Shorthand notation.
86 */
87 #define dq_bhardlimit dq_dqb.dqb_bhardlimit
88 #define dq_bsoftlimit dq_dqb.dqb_bsoftlimit
89 #define dq_curblocks dq_dqb.dqb_curblocks
90 #define dq_ihardlimit dq_dqb.dqb_ihardlimit
91 #define dq_isoftlimit dq_dqb.dqb_isoftlimit
92 #define dq_curinodes dq_dqb.dqb_curinodes
93 #define dq_btime dq_dqb.dqb_btime
94 #define dq_itime dq_dqb.dqb_itime
95 /*
96 * If the system has never checked for a quota for this file, then it is
97 * set to NODQUOT. Once a write attempt is made the inode pointer is set
98 * to reference a dquot structure.
99 */
100 #define NODQUOT NULL
101
102 static int chkdqchg(struct inode *, int64_t, kauth_cred_t, int);
103 static int chkiqchg(struct inode *, int32_t, kauth_cred_t, int);
104 #ifdef DIAGNOSTIC
105 static void dqflush(struct vnode *);
106 #endif
107 static int dqget(struct vnode *, u_long, struct ufsmount *, int,
108 struct dquot **);
109 static void dqref(struct dquot *);
110 static void dqrele(struct vnode *, struct dquot *);
111 static int dqsync(struct vnode *, struct dquot *);
112
113 static kmutex_t dqlock;
114 static kcondvar_t dqcv;
115 /*
116 * Quota name to error message mapping.
117 */
118 static const char *quotatypes[] = INITQFNAMES;
119
120 /*
121 * Set up the quotas for an inode.
122 *
123 * This routine completely defines the semantics of quotas.
124 * If other criterion want to be used to establish quotas, the
125 * MAXQUOTAS value in quotas.h should be increased, and the
126 * additional dquots set up here.
127 */
128 int
129 getinoquota(struct inode *ip)
130 {
131 struct ufsmount *ump = ip->i_ump;
132 struct vnode *vp = ITOV(ip);
133 int i, error;
134 u_int32_t ino_ids[MAXQUOTAS];
135
136 /*
137 * To avoid deadlocks never update quotas for quota files
138 * on the same file system
139 */
140 for (i = 0; i < MAXQUOTAS; i++)
141 if (ITOV(ip) == ump->um_quotas[i])
142 return 0;
143
144 ino_ids[USRQUOTA] = ip->i_uid;
145 ino_ids[GRPQUOTA] = ip->i_gid;
146 for (i = 0; i < MAXQUOTAS; i++) {
147 /*
148 * If the file id changed the quota needs update.
149 */
150 if (ip->i_dquot[i] != NODQUOT &&
151 ip->i_dquot[i]->dq_id != ino_ids[i]) {
152 dqrele(ITOV(ip), ip->i_dquot[i]);
153 ip->i_dquot[i] = NODQUOT;
154 }
155 /*
156 * Set up the quota based on file id.
157 * EINVAL means that quotas are not enabled.
158 */
159 if (ip->i_dquot[i] == NODQUOT &&
160 (error = dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
161 error != EINVAL)
162 return (error);
163 }
164 return 0;
165 }
166
167 /*
168 * Initialize the quota fields of an inode.
169 */
170 void
171 ufsquota_init(struct inode *ip)
172 {
173 int i;
174
175 for (i = 0; i < MAXQUOTAS; i++)
176 ip->i_dquot[i] = NODQUOT;
177 }
178
179 /*
180 * Release the quota fields from an inode.
181 */
182 void
183 ufsquota_free(struct inode *ip)
184 {
185 int i;
186
187 for (i = 0; i < MAXQUOTAS; i++) {
188 dqrele(ITOV(ip), ip->i_dquot[i]);
189 ip->i_dquot[i] = NODQUOT;
190 }
191 }
192
193 /*
194 * Update disk usage, and take corrective action.
195 */
196 int
197 chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
198 {
199 struct dquot *dq;
200 int i;
201 int ncurblocks, error;
202
203 if ((error = getinoquota(ip)) != 0)
204 return error;
205 if (change == 0)
206 return (0);
207 if (change < 0) {
208 for (i = 0; i < MAXQUOTAS; i++) {
209 if ((dq = ip->i_dquot[i]) == NODQUOT)
210 continue;
211 mutex_enter(&dq->dq_interlock);
212 ncurblocks = dq->dq_curblocks + change;
213 if (ncurblocks >= 0)
214 dq->dq_curblocks = ncurblocks;
215 else
216 dq->dq_curblocks = 0;
217 dq->dq_flags &= ~DQ_BLKS;
218 dq->dq_flags |= DQ_MOD;
219 mutex_exit(&dq->dq_interlock);
220 }
221 return (0);
222 }
223 if ((flags & FORCE) == 0 &&
224 kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA,
225 KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT, NULL, NULL, 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_system(cred,
330 KAUTH_SYSTEM_FS_QUOTA, KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT, NULL,
331 NULL, NULL) != 0) {
332 for (i = 0; i < MAXQUOTAS; i++) {
333 if ((dq = ip->i_dquot[i]) == NODQUOT)
334 continue;
335 mutex_enter(&dq->dq_interlock);
336 error = chkiqchg(ip, change, cred, i);
337 mutex_exit(&dq->dq_interlock);
338 if (error != 0)
339 return (error);
340 }
341 }
342 for (i = 0; i < MAXQUOTAS; i++) {
343 if ((dq = ip->i_dquot[i]) == NODQUOT)
344 continue;
345 mutex_enter(&dq->dq_interlock);
346 dq->dq_curinodes += change;
347 dq->dq_flags |= DQ_MOD;
348 mutex_exit(&dq->dq_interlock);
349 }
350 return (0);
351 }
352
353 /*
354 * Check for a valid change to a users allocation.
355 * Issue an error message if appropriate.
356 */
357 static int
358 chkiqchg(struct inode *ip, int32_t change, kauth_cred_t cred, int type)
359 {
360 struct dquot *dq = ip->i_dquot[type];
361 long ncurinodes = dq->dq_curinodes + change;
362
363 KASSERT(mutex_owned(&dq->dq_interlock));
364 /*
365 * If user would exceed their hard limit, disallow inode allocation.
366 */
367 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
368 if ((dq->dq_flags & DQ_INODS) == 0 &&
369 ip->i_uid == kauth_cred_geteuid(cred)) {
370 uprintf("\n%s: write failed, %s inode limit reached\n",
371 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
372 quotatypes[type]);
373 dq->dq_flags |= DQ_INODS;
374 }
375 return (EDQUOT);
376 }
377 /*
378 * If user is over their soft limit for too long, disallow inode
379 * allocation. Reset time limit as they cross their soft limit.
380 */
381 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
382 if (dq->dq_curinodes < dq->dq_isoftlimit) {
383 dq->dq_itime = time_second + ip->i_ump->um_itime[type];
384 if (ip->i_uid == kauth_cred_geteuid(cred))
385 uprintf("\n%s: warning, %s %s\n",
386 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
387 quotatypes[type], "inode quota exceeded");
388 return (0);
389 }
390 if (time_second > dq->dq_itime) {
391 if ((dq->dq_flags & DQ_INODS) == 0 &&
392 ip->i_uid == kauth_cred_geteuid(cred)) {
393 uprintf("\n%s: write failed, %s %s\n",
394 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
395 quotatypes[type],
396 "inode quota exceeded for too long");
397 dq->dq_flags |= DQ_INODS;
398 }
399 return (EDQUOT);
400 }
401 }
402 return (0);
403 }
404
405 /*
406 * Code to process quotactl commands.
407 */
408
409 /*
410 * Q_QUOTAON - set up a quota file for a particular file system.
411 */
412 int
413 quotaon(struct lwp *l, struct mount *mp, int type, void *fname)
414 {
415 struct ufsmount *ump = VFSTOUFS(mp);
416 struct vnode *vp, **vpp, *mvp;
417 struct dquot *dq;
418 int error;
419 struct nameidata nd;
420
421 /* XXX XXX XXX */
422 if (mp->mnt_wapbl != NULL) {
423 printf("%s: quotas cannot yet be used with -o log\n",
424 mp->mnt_stat.f_mntonname);
425 return (EOPNOTSUPP);
426 }
427
428 vpp = &ump->um_quotas[type];
429 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname);
430 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
431 return (error);
432 vp = nd.ni_vp;
433 VOP_UNLOCK(vp);
434 if (vp->v_type != VREG) {
435 (void) vn_close(vp, FREAD|FWRITE, l->l_cred);
436 return (EACCES);
437 }
438 if (*vpp != vp)
439 quotaoff(l, mp, type);
440 mutex_enter(&dqlock);
441 while ((ump->um_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
442 cv_wait(&dqcv, &dqlock);
443 ump->um_qflags[type] |= QTF_OPENING;
444 mutex_exit(&dqlock);
445 mp->mnt_flag |= MNT_QUOTA;
446 vp->v_vflag |= VV_SYSTEM; /* XXXSMP */
447 *vpp = vp;
448 /*
449 * Save the credential of the process that turned on quotas.
450 * Set up the time limits for this quota.
451 */
452 kauth_cred_hold(l->l_cred);
453 ump->um_cred[type] = l->l_cred;
454 ump->um_btime[type] = MAX_DQ_TIME;
455 ump->um_itime[type] = MAX_IQ_TIME;
456 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
457 if (dq->dq_btime > 0)
458 ump->um_btime[type] = dq->dq_btime;
459 if (dq->dq_itime > 0)
460 ump->um_itime[type] = dq->dq_itime;
461 dqrele(NULLVP, dq);
462 }
463 /* Allocate a marker vnode. */
464 if ((mvp = vnalloc(mp)) == NULL) {
465 error = ENOMEM;
466 goto out;
467 }
468 /*
469 * Search vnodes associated with this mount point,
470 * adding references to quota file being opened.
471 * NB: only need to add dquot's for inodes being modified.
472 */
473 mutex_enter(&mntvnode_lock);
474 again:
475 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
476 vmark(mvp, vp);
477 mutex_enter(&vp->v_interlock);
478 if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
479 vp->v_type == VNON || vp->v_writecount == 0 ||
480 (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
481 mutex_exit(&vp->v_interlock);
482 continue;
483 }
484 mutex_exit(&mntvnode_lock);
485 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
486 mutex_enter(&mntvnode_lock);
487 (void)vunmark(mvp);
488 goto again;
489 }
490 if ((error = getinoquota(VTOI(vp))) != 0) {
491 vput(vp);
492 mutex_enter(&mntvnode_lock);
493 (void)vunmark(mvp);
494 break;
495 }
496 vput(vp);
497 mutex_enter(&mntvnode_lock);
498 }
499 mutex_exit(&mntvnode_lock);
500 vnfree(mvp);
501 out:
502 mutex_enter(&dqlock);
503 ump->um_qflags[type] &= ~QTF_OPENING;
504 cv_broadcast(&dqcv);
505 mutex_exit(&dqlock);
506 if (error)
507 quotaoff(l, mp, type);
508 return (error);
509 }
510
511 /*
512 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
513 */
514 int
515 quotaoff(struct lwp *l, struct mount *mp, int type)
516 {
517 struct vnode *vp;
518 struct vnode *qvp, *mvp;
519 struct ufsmount *ump = VFSTOUFS(mp);
520 struct dquot *dq;
521 struct inode *ip;
522 kauth_cred_t cred;
523 int i, error;
524
525 /* Allocate a marker vnode. */
526 if ((mvp = vnalloc(mp)) == NULL)
527 return ENOMEM;
528
529 mutex_enter(&dqlock);
530 while ((ump->um_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
531 cv_wait(&dqcv, &dqlock);
532 if ((qvp = ump->um_quotas[type]) == NULLVP) {
533 mutex_exit(&dqlock);
534 vnfree(mvp);
535 return (0);
536 }
537 ump->um_qflags[type] |= QTF_CLOSING;
538 mutex_exit(&dqlock);
539 /*
540 * Search vnodes associated with this mount point,
541 * deleting any references to quota file being closed.
542 */
543 mutex_enter(&mntvnode_lock);
544 again:
545 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
546 vmark(mvp, vp);
547 mutex_enter(&vp->v_interlock);
548 if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
549 vp->v_type == VNON ||
550 (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
551 mutex_exit(&vp->v_interlock);
552 continue;
553 }
554 mutex_exit(&mntvnode_lock);
555 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
556 mutex_enter(&mntvnode_lock);
557 (void)vunmark(mvp);
558 goto again;
559 }
560 ip = VTOI(vp);
561 dq = ip->i_dquot[type];
562 ip->i_dquot[type] = NODQUOT;
563 dqrele(vp, dq);
564 vput(vp);
565 mutex_enter(&mntvnode_lock);
566 }
567 mutex_exit(&mntvnode_lock);
568 #ifdef DIAGNOSTIC
569 dqflush(qvp);
570 #endif
571 qvp->v_vflag &= ~VV_SYSTEM;
572 error = vn_close(qvp, FREAD|FWRITE, l->l_cred);
573 mutex_enter(&dqlock);
574 ump->um_quotas[type] = NULLVP;
575 cred = ump->um_cred[type];
576 ump->um_cred[type] = NOCRED;
577 for (i = 0; i < MAXQUOTAS; i++)
578 if (ump->um_quotas[i] != NULLVP)
579 break;
580 ump->um_qflags[type] &= ~QTF_CLOSING;
581 cv_broadcast(&dqcv);
582 mutex_exit(&dqlock);
583 kauth_cred_free(cred);
584 if (i == MAXQUOTAS)
585 mp->mnt_flag &= ~MNT_QUOTA;
586 return (error);
587 }
588
589 /*
590 * Q_GETQUOTA - return current values in a dqblk structure.
591 */
592 int
593 getquota(struct mount *mp, u_long id, int type, void *addr)
594 {
595 struct dquot *dq;
596 int error;
597
598 if ((error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq)) != 0)
599 return (error);
600 error = copyout((void *)&dq->dq_dqb, addr, sizeof (struct dqblk));
601 dqrele(NULLVP, dq);
602 return (error);
603 }
604
605 /*
606 * Q_SETQUOTA - assign an entire dqblk structure.
607 */
608 int
609 setquota(struct mount *mp, u_long id, int type, void *addr)
610 {
611 struct dquot *dq;
612 struct dquot *ndq;
613 struct ufsmount *ump = VFSTOUFS(mp);
614 struct dqblk newlim;
615 int error;
616
617 error = copyin(addr, (void *)&newlim, sizeof (struct dqblk));
618 if (error)
619 return (error);
620 if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
621 return (error);
622 dq = ndq;
623 mutex_enter(&dq->dq_interlock);
624 /*
625 * Copy all but the current values.
626 * Reset time limit if previously had no soft limit or were
627 * under it, but now have a soft limit and are over it.
628 */
629 newlim.dqb_curblocks = dq->dq_curblocks;
630 newlim.dqb_curinodes = dq->dq_curinodes;
631 if (dq->dq_id != 0) {
632 newlim.dqb_btime = dq->dq_btime;
633 newlim.dqb_itime = dq->dq_itime;
634 }
635 if (newlim.dqb_bsoftlimit &&
636 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
637 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
638 newlim.dqb_btime = time_second + ump->um_btime[type];
639 if (newlim.dqb_isoftlimit &&
640 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
641 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
642 newlim.dqb_itime = time_second + ump->um_itime[type];
643 dq->dq_dqb = newlim;
644 if (dq->dq_curblocks < dq->dq_bsoftlimit)
645 dq->dq_flags &= ~DQ_BLKS;
646 if (dq->dq_curinodes < dq->dq_isoftlimit)
647 dq->dq_flags &= ~DQ_INODS;
648 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
649 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
650 dq->dq_flags |= DQ_FAKE;
651 else
652 dq->dq_flags &= ~DQ_FAKE;
653 dq->dq_flags |= DQ_MOD;
654 mutex_exit(&dq->dq_interlock);
655 dqrele(NULLVP, dq);
656 return (0);
657 }
658
659 /*
660 * Q_SETUSE - set current inode and block usage.
661 */
662 int
663 setuse(struct mount *mp, u_long id, int type, void *addr)
664 {
665 struct dquot *dq;
666 struct ufsmount *ump = VFSTOUFS(mp);
667 struct dquot *ndq;
668 struct dqblk usage;
669 int error;
670
671 error = copyin(addr, (void *)&usage, sizeof (struct dqblk));
672 if (error)
673 return (error);
674 if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
675 return (error);
676 dq = ndq;
677 mutex_enter(&dq->dq_interlock);
678 /*
679 * Reset time limit if have a soft limit and were
680 * previously under it, but are now over it.
681 */
682 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
683 usage.dqb_curblocks >= dq->dq_bsoftlimit)
684 dq->dq_btime = time_second + ump->um_btime[type];
685 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
686 usage.dqb_curinodes >= dq->dq_isoftlimit)
687 dq->dq_itime = time_second + ump->um_itime[type];
688 dq->dq_curblocks = usage.dqb_curblocks;
689 dq->dq_curinodes = usage.dqb_curinodes;
690 if (dq->dq_curblocks < dq->dq_bsoftlimit)
691 dq->dq_flags &= ~DQ_BLKS;
692 if (dq->dq_curinodes < dq->dq_isoftlimit)
693 dq->dq_flags &= ~DQ_INODS;
694 dq->dq_flags |= DQ_MOD;
695 mutex_exit(&dq->dq_interlock);
696 dqrele(NULLVP, dq);
697 return (0);
698 }
699
700 /*
701 * Q_SYNC - sync quota files to disk.
702 */
703 int
704 qsync(struct mount *mp)
705 {
706 struct ufsmount *ump = VFSTOUFS(mp);
707 struct vnode *vp, *mvp;
708 struct dquot *dq;
709 int i, error;
710
711 /*
712 * Check if the mount point has any quotas.
713 * If not, simply return.
714 */
715 for (i = 0; i < MAXQUOTAS; i++)
716 if (ump->um_quotas[i] != NULLVP)
717 break;
718 if (i == MAXQUOTAS)
719 return (0);
720
721 /* Allocate a marker vnode. */
722 if ((mvp = vnalloc(mp)) == NULL)
723 return (ENOMEM);
724
725 /*
726 * Search vnodes associated with this mount point,
727 * synchronizing any modified dquot structures.
728 */
729 mutex_enter(&mntvnode_lock);
730 again:
731 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
732 vmark(mvp, vp);
733 mutex_enter(&vp->v_interlock);
734 if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
735 vp->v_type == VNON ||
736 (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
737 mutex_exit(&vp->v_interlock);
738 continue;
739 }
740 mutex_exit(&mntvnode_lock);
741 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
742 if (error) {
743 mutex_enter(&mntvnode_lock);
744 if (error == ENOENT) {
745 (void)vunmark(mvp);
746 goto again;
747 }
748 continue;
749 }
750 for (i = 0; i < MAXQUOTAS; i++) {
751 dq = VTOI(vp)->i_dquot[i];
752 if (dq == NODQUOT)
753 continue;
754 mutex_enter(&dq->dq_interlock);
755 if (dq->dq_flags & DQ_MOD)
756 dqsync(vp, dq);
757 mutex_exit(&dq->dq_interlock);
758 }
759 vput(vp);
760 mutex_enter(&mntvnode_lock);
761 }
762 mutex_exit(&mntvnode_lock);
763 vnfree(mvp);
764 return (0);
765 }
766
767 /*
768 * Code pertaining to management of the in-core dquot data structures.
769 */
770 #define DQHASH(dqvp, id) \
771 (((((long)(dqvp)) >> 8) + id) & dqhash)
772 static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
773 static u_long dqhash;
774 static pool_cache_t dquot_cache;
775
776 /*
777 * Initialize the quota system.
778 */
779 void
780 dqinit(void)
781 {
782
783 mutex_init(&dqlock, MUTEX_DEFAULT, IPL_NONE);
784 cv_init(&dqcv, "quota");
785 dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
786 dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "ufsdq",
787 NULL, IPL_NONE, NULL, NULL, NULL);
788 }
789
790 void
791 dqreinit(void)
792 {
793 struct dquot *dq;
794 struct dqhashhead *oldhash, *hash;
795 struct vnode *dqvp;
796 u_long oldmask, mask, hashval;
797 int i;
798
799 hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
800 mutex_enter(&dqlock);
801 oldhash = dqhashtbl;
802 oldmask = dqhash;
803 dqhashtbl = hash;
804 dqhash = mask;
805 for (i = 0; i <= oldmask; i++) {
806 while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
807 dqvp = dq->dq_ump->um_quotas[dq->dq_type];
808 LIST_REMOVE(dq, dq_hash);
809 hashval = DQHASH(dqvp, dq->dq_id);
810 LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
811 }
812 }
813 mutex_exit(&dqlock);
814 hashdone(oldhash, HASH_LIST, oldmask);
815 }
816
817 /*
818 * Free resources held by quota system.
819 */
820 void
821 dqdone(void)
822 {
823
824 pool_cache_destroy(dquot_cache);
825 hashdone(dqhashtbl, HASH_LIST, dqhash);
826 cv_destroy(&dqcv);
827 mutex_destroy(&dqlock);
828 }
829
830 /*
831 * Obtain a dquot structure for the specified identifier and quota file
832 * reading the information from the file if necessary.
833 */
834 static int
835 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
836 struct dquot **dqp)
837 {
838 struct dquot *dq, *ndq;
839 struct dqhashhead *dqh;
840 struct vnode *dqvp;
841 struct iovec aiov;
842 struct uio auio;
843 int error;
844
845 /* Lock to see an up to date value for QTF_CLOSING. */
846 mutex_enter(&dqlock);
847 dqvp = ump->um_quotas[type];
848 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
849 mutex_exit(&dqlock);
850 *dqp = NODQUOT;
851 return (EINVAL);
852 }
853 KASSERT(dqvp != vp);
854 /*
855 * Check the cache first.
856 */
857 dqh = &dqhashtbl[DQHASH(dqvp, id)];
858 LIST_FOREACH(dq, dqh, dq_hash) {
859 if (dq->dq_id != id ||
860 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
861 continue;
862 KASSERT(dq->dq_cnt > 0);
863 dqref(dq);
864 mutex_exit(&dqlock);
865 *dqp = dq;
866 return (0);
867 }
868 /*
869 * Not in cache, allocate a new one.
870 */
871 mutex_exit(&dqlock);
872 ndq = pool_cache_get(dquot_cache, PR_WAITOK);
873 /*
874 * Initialize the contents of the dquot structure.
875 */
876 memset((char *)ndq, 0, sizeof *ndq);
877 ndq->dq_flags = 0;
878 ndq->dq_id = id;
879 ndq->dq_ump = ump;
880 ndq->dq_type = type;
881 mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
882 mutex_enter(&dqlock);
883 dqh = &dqhashtbl[DQHASH(dqvp, id)];
884 LIST_FOREACH(dq, dqh, dq_hash) {
885 if (dq->dq_id != id ||
886 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
887 continue;
888 /*
889 * Another thread beat us allocating this dquot.
890 */
891 KASSERT(dq->dq_cnt > 0);
892 dqref(dq);
893 mutex_exit(&dqlock);
894 mutex_destroy(&ndq->dq_interlock);
895 pool_cache_put(dquot_cache, ndq);
896 *dqp = dq;
897 return 0;
898 }
899 dq = ndq;
900 LIST_INSERT_HEAD(dqh, dq, dq_hash);
901 dqref(dq);
902 mutex_enter(&dq->dq_interlock);
903 mutex_exit(&dqlock);
904 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
905 auio.uio_iov = &aiov;
906 auio.uio_iovcnt = 1;
907 aiov.iov_base = (void *)&dq->dq_dqb;
908 aiov.iov_len = sizeof (struct dqblk);
909 auio.uio_resid = sizeof (struct dqblk);
910 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
911 auio.uio_rw = UIO_READ;
912 UIO_SETUP_SYSSPACE(&auio);
913 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
914 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
915 memset((void *)&dq->dq_dqb, 0, sizeof(struct dqblk));
916 VOP_UNLOCK(dqvp);
917 /*
918 * I/O error in reading quota file, release
919 * quota structure and reflect problem to caller.
920 */
921 if (error) {
922 mutex_enter(&dqlock);
923 LIST_REMOVE(dq, dq_hash);
924 mutex_exit(&dqlock);
925 mutex_exit(&dq->dq_interlock);
926 dqrele(vp, dq);
927 *dqp = NODQUOT;
928 return (error);
929 }
930 /*
931 * Check for no limit to enforce.
932 * Initialize time values if necessary.
933 */
934 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
935 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
936 dq->dq_flags |= DQ_FAKE;
937 if (dq->dq_id != 0) {
938 if (dq->dq_btime == 0)
939 dq->dq_btime = time_second + ump->um_btime[type];
940 if (dq->dq_itime == 0)
941 dq->dq_itime = time_second + ump->um_itime[type];
942 }
943 mutex_exit(&dq->dq_interlock);
944 *dqp = dq;
945 return (0);
946 }
947
948 /*
949 * Obtain a reference to a dquot.
950 */
951 static void
952 dqref(struct dquot *dq)
953 {
954
955 KASSERT(mutex_owned(&dqlock));
956 dq->dq_cnt++;
957 KASSERT(dq->dq_cnt > 0);
958 }
959
960 /*
961 * Release a reference to a dquot.
962 */
963 static void
964 dqrele(struct vnode *vp, struct dquot *dq)
965 {
966
967 if (dq == NODQUOT)
968 return;
969 mutex_enter(&dq->dq_interlock);
970 for (;;) {
971 mutex_enter(&dqlock);
972 if (dq->dq_cnt > 1) {
973 dq->dq_cnt--;
974 mutex_exit(&dqlock);
975 mutex_exit(&dq->dq_interlock);
976 return;
977 }
978 if ((dq->dq_flags & DQ_MOD) == 0)
979 break;
980 mutex_exit(&dqlock);
981 (void) dqsync(vp, dq);
982 }
983 KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
984 LIST_REMOVE(dq, dq_hash);
985 mutex_exit(&dqlock);
986 mutex_exit(&dq->dq_interlock);
987 mutex_destroy(&dq->dq_interlock);
988 pool_cache_put(dquot_cache, dq);
989 }
990
991 /*
992 * Update the disk quota in the quota file.
993 */
994 static int
995 dqsync(struct vnode *vp, struct dquot *dq)
996 {
997 struct vnode *dqvp;
998 struct iovec aiov;
999 struct uio auio;
1000 int error;
1001
1002 if (dq == NODQUOT)
1003 panic("dqsync: dquot");
1004 KASSERT(mutex_owned(&dq->dq_interlock));
1005 if ((dq->dq_flags & DQ_MOD) == 0)
1006 return (0);
1007 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
1008 panic("dqsync: file");
1009 KASSERT(dqvp != vp);
1010 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1011 auio.uio_iov = &aiov;
1012 auio.uio_iovcnt = 1;
1013 aiov.iov_base = (void *)&dq->dq_dqb;
1014 aiov.iov_len = sizeof (struct dqblk);
1015 auio.uio_resid = sizeof (struct dqblk);
1016 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
1017 auio.uio_rw = UIO_WRITE;
1018 UIO_SETUP_SYSSPACE(&auio);
1019 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1020 if (auio.uio_resid && error == 0)
1021 error = EIO;
1022 dq->dq_flags &= ~DQ_MOD;
1023 VOP_UNLOCK(dqvp);
1024 return (error);
1025 }
1026
1027 #ifdef DIAGNOSTIC
1028 /*
1029 * Check the hash chains for stray dquot's.
1030 */
1031 static void
1032 dqflush(struct vnode *vp)
1033 {
1034 struct dquot *dq;
1035 int i;
1036
1037 mutex_enter(&dqlock);
1038 for (i = 0; i <= dqhash; i++)
1039 LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
1040 KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
1041 mutex_exit(&dqlock);
1042 }
1043 #endif
1044