]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_quota.c
5708eb0856b00aae8566124df7c3e1973d753f10
[apple/xnu.git] / bsd / ufs / ufs / ufs_quota.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * This code is derived from software contributed to Berkeley by
33 * Robert Elz at The University of Melbourne.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
64 */
65
66 #include <sys/param.h>
67 #include <sys/kernel.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <sys/file.h>
71 #include <sys/proc.h>
72 #include <sys/kauth.h>
73 #include <sys/vnode_internal.h>
74 #include <sys/mount_internal.h>
75 #include <sys/namei.h>
76 #include <sys/quota.h>
77
78 #include <ufs/ufs/quota.h>
79 #include <ufs/ufs/inode.h>
80 #include <ufs/ufs/ufsmount.h>
81 #include <ufs/ufs/ufs_extern.h>
82
83 /*
84 * Quota name to error message mapping.
85 */
86 static char *quotatypes[] = INITQFNAMES;
87
88 /*
89 * Set up the quotas for an inode.
90 *
91 * This routine completely defines the semantics of quotas.
92 * If other criterion want to be used to establish quotas, the
93 * MAXQUOTAS value in quotas.h should be increased, and the
94 * additional dquots set up here.
95 */
96 int
97 getinoquota(ip)
98 register struct inode *ip;
99 {
100 struct ufsmount *ump;
101 struct vnode *vp = ITOV(ip);
102 int error;
103
104 ump = VFSTOUFS(vp->v_mount);
105 /*
106 * Set up the user quota based on file uid.
107 * EINVAL means that quotas are not enabled.
108 */
109 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
110 (error =
111 dqget(ip->i_uid, &ump->um_qfiles[USRQUOTA], USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
112 error != EINVAL)
113 return (error);
114 /*
115 * Set up the group quota based on file gid.
116 * EINVAL means that quotas are not enabled.
117 */
118 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
119 (error =
120 dqget(ip->i_gid, &ump->um_qfiles[GRPQUOTA], GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
121 error != EINVAL)
122 return (error);
123 return (0);
124 }
125
126 /*
127 * Update disk usage, and take corrective action.
128 */
129 int
130 chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
131 {
132 register struct dquot *dq;
133 register int i;
134 int64_t ncurbytes;
135 int error;
136 struct proc *p;
137
138 #if DIAGNOSTIC
139 if ((flags & CHOWN) == 0)
140 chkdquot(ip);
141 #endif
142 if (change == 0)
143 return (0);
144 if (change < 0) {
145 for (i = 0; i < MAXQUOTAS; i++) {
146 if ((dq = ip->i_dquot[i]) == NODQUOT)
147 continue;
148 dqlock(dq);
149
150 ncurbytes = dq->dq_curbytes + change;
151 if (ncurbytes >= 0)
152 dq->dq_curbytes = ncurbytes;
153 else
154 dq->dq_curbytes = 0;
155 dq->dq_flags &= ~DQ_BLKS;
156 dq->dq_flags |= DQ_MOD;
157
158 dqunlock(dq);
159 }
160 return (0);
161 }
162 #warning "hack for no cred passed to chkdq()"
163 /*
164 * This use of proc_ucred() is safe because kernproc credential never
165 * changes.
166 */
167 p = current_proc();
168 if (!IS_VALID_CRED(cred))
169 cred = proc_ucred(kernproc);
170 if ((flags & FORCE) == 0 && (suser(cred, NULL) || (proc_forcequota(p)))) {
171 for (i = 0; i < MAXQUOTAS; i++) {
172 if ((dq = ip->i_dquot[i]) == NODQUOT)
173 continue;
174 if ( (error = chkdqchg(ip, change, cred, i)) )
175 return (error);
176 }
177 }
178 for (i = 0; i < MAXQUOTAS; i++) {
179 if ((dq = ip->i_dquot[i]) == NODQUOT)
180 continue;
181 dqlock(dq);
182
183 dq->dq_curbytes += change;
184 dq->dq_flags |= DQ_MOD;
185
186 dqunlock(dq);
187 }
188 return (0);
189 }
190
191 /*
192 * Check for a valid change to a users allocation.
193 * Issue an error message if appropriate.
194 */
195 int
196 chkdqchg(struct inode *ip, int64_t change, kauth_cred_t cred, int type)
197 {
198 register struct dquot *dq = ip->i_dquot[type];
199 u_int64_t ncurbytes;
200
201 dqlock(dq);
202
203 ncurbytes = dq->dq_curbytes + change;
204 /*
205 * If user would exceed their hard limit, disallow space allocation.
206 */
207 if (ncurbytes >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
208 if ((dq->dq_flags & DQ_BLKS) == 0 &&
209 ip->i_uid == kauth_cred_getuid(cred)) {
210 #if 1
211 printf("\n%s: write failed, %s disk limit reached\n",
212 ITOV(ip)->v_mount->mnt_vfsstat.f_mntonname,
213 quotatypes[type]);
214 #endif
215 dq->dq_flags |= DQ_BLKS;
216 }
217 dqunlock(dq);
218
219 return (EDQUOT);
220 }
221 /*
222 * If user is over their soft limit for too long, disallow space
223 * allocation. Reset time limit as they cross their soft limit.
224 */
225 if (ncurbytes >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
226 struct timeval tv;
227
228 microtime(&tv);
229 if (dq->dq_curbytes < dq->dq_bsoftlimit) {
230 dq->dq_btime = tv.tv_sec +
231 VFSTOUFS(ITOV(ip)->v_mount)->um_qfiles[type].qf_btime;
232 #if 1
233 if (ip->i_uid == kauth_cred_getuid(cred))
234 printf("\n%s: warning, %s %s\n",
235 ITOV(ip)->v_mount->mnt_vfsstat.f_mntonname,
236 quotatypes[type], "disk quota exceeded");
237 #endif
238 dqunlock(dq);
239
240 return (0);
241 }
242 if (tv.tv_sec > dq->dq_btime) {
243 if ((dq->dq_flags & DQ_BLKS) == 0 &&
244 ip->i_uid == kauth_cred_getuid(cred)) {
245 #if 1
246 printf("\n%s: write failed, %s %s\n",
247 ITOV(ip)->v_mount->mnt_vfsstat.f_mntonname,
248 quotatypes[type],
249 "disk quota exceeded for too long");
250 #endif
251 dq->dq_flags |= DQ_BLKS;
252 }
253 dqunlock(dq);
254
255 return (EDQUOT);
256 }
257 }
258 dqunlock(dq);
259
260 return (0);
261 }
262
263 /*
264 * Check the inode limit, applying corrective action.
265 */
266 int
267 chkiq(struct inode *ip, long change, kauth_cred_t cred, int flags)
268 {
269 register struct dquot *dq;
270 register int i;
271 int ncurinodes, error;
272 struct proc *p;
273
274 #if DIAGNOSTIC
275 if ((flags & CHOWN) == 0)
276 chkdquot(ip);
277 #endif
278 if (change == 0)
279 return (0);
280 if (change < 0) {
281 for (i = 0; i < MAXQUOTAS; i++) {
282 if ((dq = ip->i_dquot[i]) == NODQUOT)
283 continue;
284 dqlock(dq);
285
286 ncurinodes = dq->dq_curinodes + change;
287 if (ncurinodes >= 0)
288 dq->dq_curinodes = ncurinodes;
289 else
290 dq->dq_curinodes = 0;
291 dq->dq_flags &= ~DQ_INODS;
292 dq->dq_flags |= DQ_MOD;
293
294 dqunlock(dq);
295 }
296 return (0);
297 }
298 #warning "hack for no cred passed to chkiq()"
299 /*
300 * This use of proc_ucred() is safe because kernproc credential never
301 * changes.
302 */
303 p = current_proc();
304 if (!IS_VALID_CRED(cred))
305 cred = proc_ucred(kernproc);
306 if ((flags & FORCE) == 0 && (suser(cred, NULL) || (proc_forcequota(p)))) {
307 for (i = 0; i < MAXQUOTAS; i++) {
308 if ((dq = ip->i_dquot[i]) == NODQUOT)
309 continue;
310 if ( (error = chkiqchg(ip, change, cred, i)) )
311 return (error);
312 }
313 }
314 for (i = 0; i < MAXQUOTAS; i++) {
315 if ((dq = ip->i_dquot[i]) == NODQUOT)
316 continue;
317 dqlock(dq);
318
319 dq->dq_curinodes += change;
320 dq->dq_flags |= DQ_MOD;
321
322 dqunlock(dq);
323 }
324 return (0);
325 }
326
327 /*
328 * Check for a valid change to a users allocation.
329 * Issue an error message if appropriate.
330 */
331 int
332 chkiqchg(struct inode *ip, long change, kauth_cred_t cred, int type)
333 {
334 register struct dquot *dq = ip->i_dquot[type];
335 long ncurinodes;
336
337 dqlock(dq);
338
339 ncurinodes = dq->dq_curinodes + change;
340 /*
341 * If user would exceed their hard limit, disallow inode allocation.
342 */
343 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
344 if ((dq->dq_flags & DQ_INODS) == 0 &&
345 ip->i_uid == kauth_cred_getuid(cred)) {
346 #if 1
347 printf("\n%s: write failed, %s inode limit reached\n",
348 ITOV(ip)->v_mount->mnt_vfsstat.f_mntonname,
349 quotatypes[type]);
350 #endif
351 dq->dq_flags |= DQ_INODS;
352 }
353 dqunlock(dq);
354
355 return (EDQUOT);
356 }
357 /*
358 * If user is over their soft limit for too long, disallow inode
359 * allocation. Reset time limit as they cross their soft limit.
360 */
361 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
362 struct timeval tv;
363
364 microtime(&tv);
365 if (dq->dq_curinodes < dq->dq_isoftlimit) {
366 dq->dq_itime = tv.tv_sec +
367 VFSTOUFS(ITOV(ip)->v_mount)->um_qfiles[type].qf_itime;
368 #if 1
369 if (ip->i_uid == kauth_cred_getuid(cred))
370 printf("\n%s: warning, %s %s\n",
371 ITOV(ip)->v_mount->mnt_vfsstat.f_mntonname,
372 quotatypes[type], "inode quota exceeded");
373 #endif
374 dqunlock(dq);
375
376 return (0);
377 }
378 if (tv.tv_sec > dq->dq_itime) {
379 if ((dq->dq_flags & DQ_INODS) == 0 &&
380 ip->i_uid == kauth_cred_getuid(cred)) {
381 #if 1
382 printf("\n%s: write failed, %s %s\n",
383 ITOV(ip)->v_mount->mnt_vfsstat.f_mntonname,
384 quotatypes[type],
385 "inode quota exceeded for too long");
386 #endif
387 dq->dq_flags |= DQ_INODS;
388 }
389 dqunlock(dq);
390
391 return (EDQUOT);
392 }
393 }
394 dqunlock(dq);
395
396 return (0);
397 }
398
399 #if DIAGNOSTIC
400 /*
401 * On filesystems with quotas enabled, it is an error for a file to change
402 * size and not to have a dquot structure associated with it.
403 */
404 void
405 chkdquot(ip)
406 register struct inode *ip;
407 {
408 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
409 register int i;
410
411 for (i = 0; i < MAXQUOTAS; i++) {
412 if (ump->um_qfiles[i].qf_vp == NULLVP)
413 continue;
414 if (ip->i_dquot[i] == NODQUOT) {
415 vprint("chkdquot: missing dquot", ITOV(ip));
416 panic("missing dquot");
417 }
418 }
419 }
420 #endif
421
422 /*
423 * Code to process quotactl commands.
424 */
425
426
427 struct ufs_quotaon_cargs {
428 int error;
429 };
430
431
432 static int
433 ufs_quotaon_callback(struct vnode *vp, void *cargs)
434 {
435 struct ufs_quotaon_cargs *args;
436
437 args = (struct ufs_quotaon_cargs *)cargs;
438
439 if ( (args->error = getinoquota(VTOI(vp))) )
440 return (VNODE_RETURNED_DONE);
441
442 return (VNODE_RETURNED);
443 }
444
445
446 /*
447 * Q_QUOTAON - set up a quota file for a particular file system.
448 */
449 int
450 quotaon(context, mp, type, fnamep)
451 vfs_context_t context;
452 struct mount *mp;
453 register int type;
454 caddr_t fnamep;
455 {
456 struct ufsmount *ump = VFSTOUFS(mp);
457 struct quotafile *qfp;
458 struct vnode *vp;
459 int error = 0;
460 struct ufs_quotaon_cargs args;
461
462 qfp = &ump->um_qfiles[type];
463
464 if ( (qf_get(qfp, QTF_OPENING)) )
465 return (0);
466
467 error = vnode_open(fnamep, FREAD|FWRITE, 0, 0, &vp, NULL);
468 if (error) {
469 goto out;
470 }
471 if (!vnode_isreg(vp)) {
472 (void) vnode_close(vp, FREAD|FWRITE, NULL);
473 error = EACCES;
474 goto out;
475 }
476 vfs_setflags(mp, (uint64_t)((unsigned int)MNT_QUOTA));
477 vnode_setnoflush(vp);
478 /*
479 * Save the credential of the process that turned on quotas.
480 */
481 qfp->qf_vp = vp;
482 qfp->qf_cred = vfs_context_ucred(context);
483 kauth_cred_ref(qfp->qf_cred);
484
485 /*
486 * Finish initializing the quota file
487 */
488 if ( (error = dqfileopen(&ump->um_qfiles[type], type)) ) {
489 (void) vnode_close(vp, FREAD|FWRITE, NULL);
490
491 kauth_cred_unref(&qfp->qf_cred);
492 qfp->qf_vp = NULLVP;
493 goto out;
494 }
495 qf_put(qfp, QTF_OPENING);
496
497 /*
498 * Search vnodes associated with this mount point,
499 * adding references to quota file being opened.
500 * NB: only need to add dquot's for inodes being modified.
501 *
502 * ufs_quota_callback will be called for each vnode open for
503 * 'write' (VNODE_WRITEABLE) hung off of this mount point
504 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
505 * properly referenced and unreferenced around the callback
506 */
507 args.error = 0;
508
509 vnode_iterate(mp, VNODE_WRITEABLE | VNODE_WAIT, ufs_quotaon_callback, (void *)&args);
510
511 error = args.error;
512
513 if (error)
514 quotaoff(mp, type);
515 return (error);
516 out:
517 qf_put(qfp, QTF_OPENING);
518
519 return (error);
520 }
521
522
523
524 struct ufs_quotaoff_cargs {
525 int type;
526 };
527
528 static int
529 ufs_quotaoff_callback(struct vnode *vp, void *cargs)
530 {
531 struct ufs_quotaoff_cargs *args;
532 struct inode *ip;
533 struct dquot *dq;
534
535 args = (struct ufs_quotaoff_cargs *)cargs;
536
537 ip = VTOI(vp);
538
539 dq = ip->i_dquot[args->type];
540 ip->i_dquot[args->type] = NODQUOT;
541
542 dqrele(dq);
543
544 return (VNODE_RETURNED);
545 }
546
547 /*
548 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
549 */
550 int
551 quotaoff(struct mount *mp, register int type)
552 {
553 struct vnode *qvp;
554 struct ufsmount *ump = VFSTOUFS(mp);
555 struct quotafile *qfp;
556 int error = 0;
557 struct ufs_quotaoff_cargs args;
558
559 qfp = &ump->um_qfiles[type];
560
561 if ( (qf_get(qfp, QTF_CLOSING)) )
562 return (0);
563 qvp = qfp->qf_vp;
564
565 /*
566 * Sync out any orpaned dirty dquot entries.
567 */
568 dqsync_orphans(qfp);
569
570 /*
571 * Search vnodes associated with this mount point,
572 * deleting any references to quota file being closed.
573 *
574 * ufs_quotaoff_callback will be called for each vnode
575 * hung off of this mount point
576 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
577 * properly referenced and unreferenced around the callback
578 */
579 args.type = type;
580
581 vnode_iterate(mp, VNODE_WAIT, ufs_quotaoff_callback, (void *)&args);
582
583 dqflush(qvp);
584 /* Finish tearing down the quota file */
585 dqfileclose(qfp, type);
586
587 vnode_clearnoflush(qvp);
588 error = vnode_close(qvp, FREAD|FWRITE, NULL);
589
590 qfp->qf_vp = NULLVP;
591 if (IS_VALID_CRED(qfp->qf_cred)) {
592 kauth_cred_unref(&qfp->qf_cred);
593 }
594 for (type = 0; type < MAXQUOTAS; type++)
595 if (ump->um_qfiles[type].qf_vp != NULLVP)
596 break;
597 if (type == MAXQUOTAS)
598 mp->mnt_flag &= ~MNT_QUOTA;
599
600 qf_put(qfp, QTF_CLOSING);
601
602 return (error);
603 }
604
605 /*
606 * Q_GETQUOTA - return current values in a dqblk structure.
607 */
608 int
609 getquota(mp, id, type, datap)
610 struct mount *mp;
611 u_long id;
612 int type;
613 caddr_t datap;
614 {
615 struct dquot *dq;
616 int error;
617
618 if ( (error = dqget(id, &VFSTOUFS(mp)->um_qfiles[type], type, &dq)) )
619 return (error);
620 dqlock(dq);
621
622 bcopy(&dq->dq_dqb, datap, sizeof(dq->dq_dqb));
623
624 dqunlock(dq);
625 dqrele(dq);
626
627 return (error);
628 }
629
630 /*
631 * Q_SETQUOTA - assign an entire dqblk structure.
632 */
633 int
634 setquota(mp, id, type, datap)
635 struct mount *mp;
636 u_long id;
637 int type;
638 caddr_t datap;
639 {
640 struct dquot *dq;
641 struct ufsmount *ump = VFSTOUFS(mp);
642 struct dqblk * newlimp = (struct dqblk *) datap;
643 struct timeval tv;
644 int error;
645
646 error = dqget(id, &ump->um_qfiles[type], type, &dq);
647 if (error)
648 return (error);
649 dqlock(dq);
650
651 /*
652 * Copy all but the current values.
653 * Reset time limit if previously had no soft limit or were
654 * under it, but now have a soft limit and are over it.
655 */
656 newlimp->dqb_curbytes = dq->dq_curbytes;
657 newlimp->dqb_curinodes = dq->dq_curinodes;
658 if (dq->dq_id != 0) {
659 newlimp->dqb_btime = dq->dq_btime;
660 newlimp->dqb_itime = dq->dq_itime;
661 }
662 if (newlimp->dqb_bsoftlimit &&
663 dq->dq_curbytes >= newlimp->dqb_bsoftlimit &&
664 (dq->dq_bsoftlimit == 0 || dq->dq_curbytes < dq->dq_bsoftlimit)) {
665 microtime(&tv);
666 newlimp->dqb_btime = tv.tv_sec + ump->um_qfiles[type].qf_btime;
667 }
668 if (newlimp->dqb_isoftlimit &&
669 dq->dq_curinodes >= newlimp->dqb_isoftlimit &&
670 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit)) {
671 microtime(&tv);
672 newlimp->dqb_itime = tv.tv_sec + ump->um_qfiles[type].qf_itime;
673 }
674 bcopy(newlimp, &dq->dq_dqb, sizeof(dq->dq_dqb));
675 if (dq->dq_curbytes < dq->dq_bsoftlimit)
676 dq->dq_flags &= ~DQ_BLKS;
677 if (dq->dq_curinodes < dq->dq_isoftlimit)
678 dq->dq_flags &= ~DQ_INODS;
679 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
680 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
681 dq->dq_flags |= DQ_FAKE;
682 else
683 dq->dq_flags &= ~DQ_FAKE;
684 dq->dq_flags |= DQ_MOD;
685
686 dqunlock(dq);
687 dqrele(dq);
688
689 return (0);
690 }
691
692 /*
693 * Q_SETUSE - set current inode and byte usage.
694 */
695 int
696 setuse(mp, id, type, datap)
697 struct mount *mp;
698 u_long id;
699 int type;
700 caddr_t datap;
701 {
702 struct dquot *dq;
703 struct ufsmount *ump = VFSTOUFS(mp);
704 struct timeval tv;
705 int error;
706 struct dqblk *quotablkp = (struct dqblk *) datap;
707
708 error = dqget(id, &ump->um_qfiles[type], type, &dq);
709 if (error)
710 return (error);
711 dqlock(dq);
712
713 /*
714 * Reset time limit if have a soft limit and were
715 * previously under it, but are now over it.
716 */
717 if (dq->dq_bsoftlimit && dq->dq_curbytes < dq->dq_bsoftlimit &&
718 quotablkp->dqb_curbytes >= dq->dq_bsoftlimit) {
719 microtime(&tv);
720 dq->dq_btime = tv.tv_sec + ump->um_qfiles[type].qf_btime;
721 }
722 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
723 quotablkp->dqb_curinodes >= dq->dq_isoftlimit) {
724 microtime(&tv);
725 dq->dq_itime = tv.tv_sec + ump->um_qfiles[type].qf_itime;
726 }
727 dq->dq_curbytes = quotablkp->dqb_curbytes;
728 dq->dq_curinodes = quotablkp->dqb_curinodes;
729 if (dq->dq_curbytes < dq->dq_bsoftlimit)
730 dq->dq_flags &= ~DQ_BLKS;
731 if (dq->dq_curinodes < dq->dq_isoftlimit)
732 dq->dq_flags &= ~DQ_INODS;
733 dq->dq_flags |= DQ_MOD;
734
735 dqunlock(dq);
736 dqrele(dq);
737
738 return (0);
739 }
740
741
742
743 static int
744 ufs_qsync_callback(struct vnode *vp, __unused void *cargs)
745 {
746 struct inode *ip;
747 struct dquot *dq;
748 int i;
749
750 ip = VTOI(vp);
751
752 for (i = 0; i < MAXQUOTAS; i++) {
753 dq = ip->i_dquot[i];
754 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
755 dqsync(dq);
756 }
757 return (VNODE_RETURNED);
758 }
759
760
761 /*
762 * Q_SYNC - sync quota files to disk.
763 */
764 int
765 qsync(mp)
766 struct mount *mp;
767 {
768 struct ufsmount *ump = VFSTOUFS(mp);
769 int i;
770
771 /*
772 * Check if the mount point has any quotas.
773 * If not, simply return.
774 */
775 for (i = 0; i < MAXQUOTAS; i++)
776 if (ump->um_qfiles[i].qf_vp != NULLVP)
777 break;
778 if (i == MAXQUOTAS)
779 return (0);
780 /*
781 * Search vnodes associated with this mount point,
782 * synchronizing any modified dquot structures.
783 *
784 * ufs_qsync_callback will be called for each vnode
785 * hung off of this mount point
786 * the vnode will be
787 * properly referenced and unreferenced around the callback
788 */
789 vnode_iterate(mp, 0, ufs_qsync_callback, (void *)NULL);
790
791 return (0);
792 }
793
794 /*
795 * Q_QUOTASTAT - get quota on/off status
796 */
797 int
798 quotastat(mp, type, datap)
799 struct mount *mp;
800 register int type;
801 caddr_t datap;
802 {
803 struct ufsmount *ump = VFSTOUFS(mp);
804 int error = 0;
805 int qstat;
806
807 if ((mp->mnt_flag & MNT_QUOTA) && (ump->um_qfiles[type].qf_vp != NULLVP))
808 qstat = 1; /* quotas are on for this type */
809 else
810 qstat = 0; /* quotas are off for this type */
811 *((int *)datap) = qstat;
812 return (error);
813 }
814