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