]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_quota.c
debab6cc4ba0253ba8c75bb0c853afca952f3899
[apple/xnu.git] / bsd / hfs / hfs_quota.c
1 /*
2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1982, 1986, 1990, 1993, 1995
32 * The Regents of the University of California. All rights reserved.
33 *
34 * This code is derived from software contributed to Berkeley by
35 * Robert Elz at The University of Melbourne.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)hfs_quota.c
66 * derived from @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
67 */
68
69 #include <sys/param.h>
70 #include <sys/kernel.h>
71 #include <sys/systm.h>
72 #include <sys/mount.h>
73 #include <sys/malloc.h>
74 #include <sys/file.h>
75 #include <sys/proc.h>
76 #include <sys/kauth.h>
77 #include <sys/vnode.h>
78 #include <sys/quota.h>
79 #include <sys/proc_internal.h>
80 #include <kern/kalloc.h>
81
82 #include <hfs/hfs.h>
83 #include <hfs/hfs_cnode.h>
84 #include <hfs/hfs_quota.h>
85 #include <hfs/hfs_mount.h>
86
87 /*
88 * Quota name to error message mapping.
89 */
90 #if 0
91 static char *quotatypes[] = INITQFNAMES;
92 #endif
93
94 /*
95 * Set up the quotas for a cnode.
96 *
97 * This routine completely defines the semantics of quotas.
98 * If other criterion want to be used to establish quotas, the
99 * MAXQUOTAS value in quotas.h should be increased, and the
100 * additional dquots set up here.
101 */
102 int
103 hfs_getinoquota(cp)
104 register struct cnode *cp;
105 {
106 struct hfsmount *hfsmp;
107 struct vnode *vp;
108 int error;
109
110 vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
111 hfsmp = VTOHFS(vp);
112 /*
113 * Set up the user quota based on file uid.
114 * EINVAL means that quotas are not enabled.
115 */
116 if (cp->c_dquot[USRQUOTA] == NODQUOT &&
117 (error =
118 dqget(cp->c_uid, &hfsmp->hfs_qfiles[USRQUOTA], USRQUOTA, &cp->c_dquot[USRQUOTA])) &&
119 error != EINVAL)
120 return (error);
121 /*
122 * Set up the group quota based on file gid.
123 * EINVAL means that quotas are not enabled.
124 */
125 if (cp->c_dquot[GRPQUOTA] == NODQUOT &&
126 (error =
127 dqget(cp->c_gid, &hfsmp->hfs_qfiles[GRPQUOTA], GRPQUOTA, &cp->c_dquot[GRPQUOTA])) &&
128 error != EINVAL)
129 return (error);
130 return (0);
131 }
132
133 /*
134 * Update disk usage, and take corrective action.
135 */
136 int
137 hfs_chkdq(cp, change, cred, flags)
138 register struct cnode *cp;
139 int64_t change;
140 kauth_cred_t cred;
141 int flags;
142 {
143 register struct dquot *dq;
144 register int i;
145 int64_t ncurbytes;
146 int error=0;
147 struct proc *p;
148
149 #if DIAGNOSTIC
150 if ((flags & CHOWN) == 0)
151 hfs_chkdquot(cp);
152 #endif
153 if (change == 0)
154 return (0);
155 if (change < 0) {
156 for (i = 0; i < MAXQUOTAS; i++) {
157 if ((dq = cp->c_dquot[i]) == NODQUOT)
158 continue;
159 dqlock(dq);
160
161 ncurbytes = dq->dq_curbytes + change;
162 if (ncurbytes >= 0)
163 dq->dq_curbytes = ncurbytes;
164 else
165 dq->dq_curbytes = 0;
166 dq->dq_flags &= ~DQ_BLKS;
167 dq->dq_flags |= DQ_MOD;
168
169 dqunlock(dq);
170 }
171 return (0);
172 }
173 p = current_proc();
174 if (cred == NOCRED)
175 cred = proc_ucred(kernproc);
176 if (suser(cred, NULL) || proc_forcequota(p)) {
177 for (i = 0; i < MAXQUOTAS; i++) {
178 if ((dq = cp->c_dquot[i]) == NODQUOT)
179 continue;
180 error = hfs_chkdqchg(cp, change, cred, i);
181 if (error) {
182 break;
183 }
184 }
185 }
186 if ((flags & FORCE) || error == 0) {
187 for (i = 0; i < MAXQUOTAS; i++) {
188 if ((dq = cp->c_dquot[i]) == NODQUOT)
189 continue;
190 dqlock(dq);
191
192 dq->dq_curbytes += change;
193 dq->dq_flags |= DQ_MOD;
194
195 dqunlock(dq);
196 }
197 }
198 return (error);
199 }
200
201 /*
202 * Check for a valid change to a users allocation.
203 * Issue an error message if appropriate.
204 */
205 int
206 hfs_chkdqchg(cp, change, cred, type)
207 struct cnode *cp;
208 int64_t change;
209 kauth_cred_t cred;
210 int type;
211 {
212 register struct dquot *dq = cp->c_dquot[type];
213 u_int64_t ncurbytes;
214 struct vnode *vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
215
216 dqlock(dq);
217
218 ncurbytes = dq->dq_curbytes + change;
219 /*
220 * If user would exceed their hard limit, disallow space allocation.
221 */
222 if (ncurbytes >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
223 if ((dq->dq_flags & DQ_BLKS) == 0 &&
224 cp->c_uid == kauth_cred_getuid(cred)) {
225 #if 0
226 printf("\nwrite failed, %s disk limit reached\n",
227 quotatypes[type]);
228 #endif
229 dq->dq_flags |= DQ_BLKS;
230 }
231 dqunlock(dq);
232
233 return (EDQUOT);
234 }
235 /*
236 * If user is over their soft limit for too long, disallow space
237 * allocation. Reset time limit as they cross their soft limit.
238 */
239 if (ncurbytes >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
240 struct timeval tv;
241
242 microuptime(&tv);
243 if (dq->dq_curbytes < dq->dq_bsoftlimit) {
244 dq->dq_btime = tv.tv_sec +
245 VTOHFS(vp)->hfs_qfiles[type].qf_btime;
246 #if 0
247 if (cp->c_uid == kauth_cred_getuid(cred))
248 printf("\nwarning, %s %s\n",
249 quotatypes[type], "disk quota exceeded");
250 #endif
251 dqunlock(dq);
252
253 return (0);
254 }
255 if (tv.tv_sec > dq->dq_btime) {
256 if ((dq->dq_flags & DQ_BLKS) == 0 &&
257 cp->c_uid == kauth_cred_getuid(cred)) {
258 #if 0
259 printf("\nwrite failed, %s %s\n",
260 quotatypes[type],
261 "disk quota exceeded for too long");
262 #endif
263 dq->dq_flags |= DQ_BLKS;
264 }
265 dqunlock(dq);
266
267 return (EDQUOT);
268 }
269 }
270 dqunlock(dq);
271
272 return (0);
273 }
274
275 /*
276 * Check the inode limit, applying corrective action.
277 */
278 int
279 hfs_chkiq(cp, change, cred, flags)
280 register struct cnode *cp;
281 long change;
282 kauth_cred_t cred;
283 int flags;
284 {
285 register struct dquot *dq;
286 register int i;
287 int ncurinodes, error=0;
288 struct proc *p;
289
290 #if DIAGNOSTIC
291 if ((flags & CHOWN) == 0)
292 hfs_chkdquot(cp);
293 #endif
294 if (change == 0)
295 return (0);
296 if (change < 0) {
297 for (i = 0; i < MAXQUOTAS; i++) {
298 if ((dq = cp->c_dquot[i]) == NODQUOT)
299 continue;
300 dqlock(dq);
301
302 ncurinodes = dq->dq_curinodes + change;
303 if (ncurinodes >= 0)
304 dq->dq_curinodes = ncurinodes;
305 else
306 dq->dq_curinodes = 0;
307 dq->dq_flags &= ~DQ_INODS;
308 dq->dq_flags |= DQ_MOD;
309
310 dqunlock(dq);
311 }
312 return (0);
313 }
314 p = current_proc();
315 if (cred == NOCRED)
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 kauth_cred_rele(qfp->qf_cred);
507 qfp->qf_cred = NOCRED;
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 kauth_cred_t cred;
575 struct hfs_quotaoff_cargs args;
576
577 qfp = &hfsmp->hfs_qfiles[type];
578
579 if ( (qf_get(qfp, QTF_CLOSING)) )
580 return (0);
581 qvp = qfp->qf_vp;
582
583 /*
584 * Sync out any orpaned dirty dquot entries.
585 */
586 dqsync_orphans(qfp);
587
588 /*
589 * Search vnodes associated with this mount point,
590 * deleting any references to quota file being closed.
591 *
592 * hfs_quotaoff_callback will be called for each vnode
593 * hung off of this mount point
594 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
595 * properly referenced and unreferenced around the callback
596 */
597 args.type = type;
598
599 vnode_iterate(mp, VNODE_WAIT, hfs_quotaoff_callback, (void *)&args);
600
601 dqflush(qvp);
602 /* Finish tearing down the quota file */
603 dqfileclose(qfp, type);
604
605 vnode_clearnoflush(qvp);
606 error = vnode_close(qvp, FREAD|FWRITE, NULL);
607
608 qfp->qf_vp = NULLVP;
609 cred = qfp->qf_cred;
610 if (cred != NOCRED) {
611 qfp->qf_cred = NOCRED;
612 kauth_cred_rele(cred);
613 }
614 for (type = 0; type < MAXQUOTAS; type++)
615 if (hfsmp->hfs_qfiles[type].qf_vp != NULLVP)
616 break;
617 if (type == MAXQUOTAS)
618 vfs_clearflags(mp, (uint64_t)((unsigned int)MNT_QUOTA));
619
620 qf_put(qfp, QTF_CLOSING);
621
622 return (error);
623 }
624
625 /*
626 * Q_GETQUOTA - return current values in a dqblk structure.
627 */
628 int
629 hfs_getquota(mp, id, type, datap)
630 struct mount *mp;
631 u_long id;
632 int type;
633 caddr_t datap;
634 {
635 struct dquot *dq;
636 int error;
637
638 error = dqget(id, &VFSTOHFS(mp)->hfs_qfiles[type], type, &dq);
639 if (error)
640 return (error);
641 dqlock(dq);
642
643 bcopy(&dq->dq_dqb, datap, sizeof(dq->dq_dqb));
644
645 dqunlock(dq);
646 dqrele(dq);
647
648 return (error);
649 }
650
651 /*
652 * Q_SETQUOTA - assign an entire dqblk structure.
653 */
654 int
655 hfs_setquota(mp, id, type, datap)
656 struct mount *mp;
657 u_long id;
658 int type;
659 caddr_t datap;
660 {
661 struct dquot *dq;
662 struct hfsmount *hfsmp = VFSTOHFS(mp);
663 struct dqblk * newlimp = (struct dqblk *) datap;
664 struct timeval tv;
665 int error;
666
667 error = dqget(id, &hfsmp->hfs_qfiles[type], type, &dq);
668 if (error)
669 return (error);
670 dqlock(dq);
671
672 /*
673 * Copy all but the current values.
674 * Reset time limit if previously had no soft limit or were
675 * under it, but now have a soft limit and are over it.
676 */
677 newlimp->dqb_curbytes = dq->dq_curbytes;
678 newlimp->dqb_curinodes = dq->dq_curinodes;
679 if (dq->dq_id != 0) {
680 newlimp->dqb_btime = dq->dq_btime;
681 newlimp->dqb_itime = dq->dq_itime;
682 }
683 if (newlimp->dqb_bsoftlimit &&
684 dq->dq_curbytes >= newlimp->dqb_bsoftlimit &&
685 (dq->dq_bsoftlimit == 0 || dq->dq_curbytes < dq->dq_bsoftlimit)) {
686 microuptime(&tv);
687 newlimp->dqb_btime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_btime;
688 }
689 if (newlimp->dqb_isoftlimit &&
690 dq->dq_curinodes >= newlimp->dqb_isoftlimit &&
691 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit)) {
692 microuptime(&tv);
693 newlimp->dqb_itime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_itime;
694 }
695 bcopy(newlimp, &dq->dq_dqb, sizeof(dq->dq_dqb));
696 if (dq->dq_curbytes < dq->dq_bsoftlimit)
697 dq->dq_flags &= ~DQ_BLKS;
698 if (dq->dq_curinodes < dq->dq_isoftlimit)
699 dq->dq_flags &= ~DQ_INODS;
700 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
701 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
702 dq->dq_flags |= DQ_FAKE;
703 else
704 dq->dq_flags &= ~DQ_FAKE;
705 dq->dq_flags |= DQ_MOD;
706
707 dqunlock(dq);
708 dqrele(dq);
709
710 return (0);
711 }
712
713 /*
714 * Q_SETUSE - set current cnode and byte usage.
715 */
716 int
717 hfs_setuse(mp, id, type, datap)
718 struct mount *mp;
719 u_long id;
720 int type;
721 caddr_t datap;
722 {
723 struct hfsmount *hfsmp = VFSTOHFS(mp);
724 struct dquot *dq;
725 struct timeval tv;
726 int error;
727 struct dqblk *quotablkp = (struct dqblk *) datap;
728
729 error = dqget(id, &hfsmp->hfs_qfiles[type], type, &dq);
730 if (error)
731 return (error);
732 dqlock(dq);
733
734 /*
735 * Reset time limit if have a soft limit and were
736 * previously under it, but are now over it.
737 */
738 if (dq->dq_bsoftlimit && dq->dq_curbytes < dq->dq_bsoftlimit &&
739 quotablkp->dqb_curbytes >= dq->dq_bsoftlimit) {
740 microuptime(&tv);
741 dq->dq_btime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_btime;
742 }
743 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
744 quotablkp->dqb_curinodes >= dq->dq_isoftlimit) {
745 microuptime(&tv);
746 dq->dq_itime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_itime;
747 }
748 dq->dq_curbytes = quotablkp->dqb_curbytes;
749 dq->dq_curinodes = quotablkp->dqb_curinodes;
750 if (dq->dq_curbytes < dq->dq_bsoftlimit)
751 dq->dq_flags &= ~DQ_BLKS;
752 if (dq->dq_curinodes < dq->dq_isoftlimit)
753 dq->dq_flags &= ~DQ_INODS;
754 dq->dq_flags |= DQ_MOD;
755
756 dqunlock(dq);
757 dqrele(dq);
758
759 return (0);
760 }
761
762
763 /*
764 * Q_SYNC - sync quota files to disk.
765 */
766 static int
767 hfs_qsync_callback(struct vnode *vp, __unused void *cargs)
768 {
769 struct cnode *cp;
770 struct dquot *dq;
771 int i;
772
773 cp = VTOC(vp);
774
775 for (i = 0; i < MAXQUOTAS; i++) {
776 dq = cp->c_dquot[i];
777 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
778 dqsync(dq);
779 }
780 return (VNODE_RETURNED);
781 }
782
783 int
784 hfs_qsync(mp)
785 struct mount *mp;
786 {
787 struct hfsmount *hfsmp = VFSTOHFS(mp);
788 int i;
789
790 /*
791 * Check if the mount point has any quotas.
792 * If not, simply return.
793 */
794 for (i = 0; i < MAXQUOTAS; i++)
795 if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP)
796 break;
797 if (i == MAXQUOTAS)
798 return (0);
799
800 /*
801 * Sync out any orpaned dirty dquot entries.
802 */
803 for (i = 0; i < MAXQUOTAS; i++)
804 if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP)
805 dqsync_orphans(&hfsmp->hfs_qfiles[i]);
806
807 /*
808 * Search vnodes associated with this mount point,
809 * synchronizing any modified dquot structures.
810 *
811 * hfs_qsync_callback will be called for each vnode
812 * hung off of this mount point
813 * the vnode will be
814 * properly referenced and unreferenced around the callback
815 */
816 vnode_iterate(mp, 0, hfs_qsync_callback, (void *)NULL);
817
818 return (0);
819 }
820
821 /*
822 * Q_QUOTASTAT - get quota on/off status
823 */
824 int
825 hfs_quotastat(mp, type, datap)
826 struct mount *mp;
827 register int type;
828 caddr_t datap;
829 {
830 struct hfsmount *hfsmp = VFSTOHFS(mp);
831 int error = 0;
832 int qstat;
833
834 if ((((unsigned int)vfs_flags(mp)) & MNT_QUOTA) && (hfsmp->hfs_qfiles[type].qf_vp != NULLVP))
835 qstat = 1; /* quotas are on for this type */
836 else
837 qstat = 0; /* quotas are off for this type */
838
839 *((int *)datap) = qstat;
840 return (error);
841 }
842