]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_quota.c
xnu-1228.0.2.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_quota.c
1 /*
2 * Copyright (c) 2002-2005 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 * @(#)hfs_quota.c
64 * derived from @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
65 */
66
67 #include <sys/param.h>
68 #include <sys/kernel.h>
69 #include <sys/systm.h>
70 #include <sys/mount.h>
71 #include <sys/malloc.h>
72 #include <sys/file.h>
73 #include <sys/proc.h>
74 #include <sys/kauth.h>
75 #include <sys/vnode.h>
76 #include <sys/vnode_internal.h>
77 #include <sys/quota.h>
78 #include <sys/proc_internal.h>
79 #include <kern/kalloc.h>
80
81 #include <hfs/hfs.h>
82 #include <hfs/hfs_cnode.h>
83 #include <hfs/hfs_quota.h>
84 #include <hfs/hfs_mount.h>
85
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 /*
175 * This use of proc_ucred() is safe because kernproc credential never
176 * changes.
177 */
178 if (!IS_VALID_CRED(cred))
179 cred = proc_ucred(kernproc);
180 if (suser(cred, NULL) || proc_forcequota(p)) {
181 for (i = 0; i < MAXQUOTAS; i++) {
182 if ((dq = cp->c_dquot[i]) == NODQUOT)
183 continue;
184 error = hfs_chkdqchg(cp, change, cred, i);
185 if (error) {
186 break;
187 }
188 }
189 }
190 if ((flags & FORCE) || error == 0) {
191 for (i = 0; i < MAXQUOTAS; i++) {
192 if ((dq = cp->c_dquot[i]) == NODQUOT)
193 continue;
194 dqlock(dq);
195
196 dq->dq_curbytes += change;
197 dq->dq_flags |= DQ_MOD;
198
199 dqunlock(dq);
200 }
201 }
202 return (error);
203 }
204
205 /*
206 * Check for a valid change to a users allocation.
207 * Issue an error message if appropriate.
208 */
209 int
210 hfs_chkdqchg(cp, change, cred, type)
211 struct cnode *cp;
212 int64_t change;
213 kauth_cred_t cred;
214 int type;
215 {
216 register struct dquot *dq = cp->c_dquot[type];
217 u_int64_t ncurbytes;
218 struct vnode *vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
219
220 dqlock(dq);
221
222 ncurbytes = dq->dq_curbytes + change;
223 /*
224 * If user would exceed their hard limit, disallow space allocation.
225 */
226 if (ncurbytes >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
227 if ((dq->dq_flags & DQ_BLKS) == 0 &&
228 cp->c_uid == kauth_cred_getuid(cred)) {
229 #if 0
230 printf("\nwrite failed, %s disk limit reached\n",
231 quotatypes[type]);
232 #endif
233 dq->dq_flags |= DQ_BLKS;
234 }
235 dqunlock(dq);
236
237 return (EDQUOT);
238 }
239 /*
240 * If user is over their soft limit for too long, disallow space
241 * allocation. Reset time limit as they cross their soft limit.
242 */
243 if (ncurbytes >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
244 struct timeval tv;
245
246 microuptime(&tv);
247 if (dq->dq_curbytes < dq->dq_bsoftlimit) {
248 dq->dq_btime = tv.tv_sec +
249 VTOHFS(vp)->hfs_qfiles[type].qf_btime;
250 #if 0
251 if (cp->c_uid == kauth_cred_getuid(cred))
252 printf("\nwarning, %s %s\n",
253 quotatypes[type], "disk quota exceeded");
254 #endif
255 dqunlock(dq);
256
257 return (0);
258 }
259 if (tv.tv_sec > dq->dq_btime) {
260 if ((dq->dq_flags & DQ_BLKS) == 0 &&
261 cp->c_uid == kauth_cred_getuid(cred)) {
262 #if 0
263 printf("\nwrite failed, %s %s\n",
264 quotatypes[type],
265 "disk quota exceeded for too long");
266 #endif
267 dq->dq_flags |= DQ_BLKS;
268 }
269 dqunlock(dq);
270
271 return (EDQUOT);
272 }
273 }
274 dqunlock(dq);
275
276 return (0);
277 }
278
279 /*
280 * Check the inode limit, applying corrective action.
281 */
282 int
283 hfs_chkiq(cp, change, cred, flags)
284 register struct cnode *cp;
285 long change;
286 kauth_cred_t cred;
287 int flags;
288 {
289 register struct dquot *dq;
290 register int i;
291 int ncurinodes, error=0;
292 struct proc *p;
293
294 #if DIAGNOSTIC
295 if ((flags & CHOWN) == 0)
296 hfs_chkdquot(cp);
297 #endif
298 if (change == 0)
299 return (0);
300 if (change < 0) {
301 for (i = 0; i < MAXQUOTAS; i++) {
302 if ((dq = cp->c_dquot[i]) == NODQUOT)
303 continue;
304 dqlock(dq);
305
306 ncurinodes = dq->dq_curinodes + change;
307 if (ncurinodes >= 0)
308 dq->dq_curinodes = ncurinodes;
309 else
310 dq->dq_curinodes = 0;
311 dq->dq_flags &= ~DQ_INODS;
312 dq->dq_flags |= DQ_MOD;
313
314 dqunlock(dq);
315 }
316 return (0);
317 }
318 p = current_proc();
319 /*
320 * This use of proc_ucred() is safe because kernproc credential never
321 * changes.
322 */
323 if (!IS_VALID_CRED(cred))
324 cred = proc_ucred(kernproc);
325 if (suser(cred, NULL) || proc_forcequota(p)) {
326 for (i = 0; i < MAXQUOTAS; i++) {
327 if ((dq = cp->c_dquot[i]) == NODQUOT)
328 continue;
329 error = hfs_chkiqchg(cp, change, cred, i);
330 if (error) {
331 break;
332 }
333 }
334 }
335 if ((flags & FORCE) || error == 0) {
336 for (i = 0; i < MAXQUOTAS; i++) {
337 if ((dq = cp->c_dquot[i]) == NODQUOT)
338 continue;
339 dqlock(dq);
340
341 dq->dq_curinodes += change;
342 dq->dq_flags |= DQ_MOD;
343
344 dqunlock(dq);
345 }
346 }
347 return (error);
348 }
349
350 /*
351 * Check for a valid change to a users allocation.
352 * Issue an error message if appropriate.
353 */
354 int
355 hfs_chkiqchg(cp, change, cred, type)
356 struct cnode *cp;
357 long change;
358 kauth_cred_t cred;
359 int type;
360 {
361 register struct dquot *dq = cp->c_dquot[type];
362 unsigned long ncurinodes;
363 struct vnode *vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
364
365 dqlock(dq);
366
367 ncurinodes = dq->dq_curinodes + change;
368 /*
369 * If user would exceed their hard limit, disallow cnode allocation.
370 */
371 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
372 if ((dq->dq_flags & DQ_INODS) == 0 &&
373 cp->c_uid == kauth_cred_getuid(cred)) {
374 #if 0
375 printf("\nwrite failed, %s cnode limit reached\n",
376 quotatypes[type]);
377 #endif
378 dq->dq_flags |= DQ_INODS;
379 }
380 dqunlock(dq);
381
382 return (EDQUOT);
383 }
384 /*
385 * If user is over their soft limit for too long, disallow cnode
386 * allocation. Reset time limit as they cross their soft limit.
387 */
388 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
389 struct timeval tv;
390
391 microuptime(&tv);
392 if (dq->dq_curinodes < dq->dq_isoftlimit) {
393 dq->dq_itime = tv.tv_sec +
394 VTOHFS(vp)->hfs_qfiles[type].qf_itime;
395 #if 0
396 if (cp->c_uid == kauth_cred_getuid(cred))
397 printf("\nwarning, %s %s\n",
398 quotatypes[type], "cnode quota exceeded");
399 #endif
400 dqunlock(dq);
401
402 return (0);
403 }
404 if (tv.tv_sec > dq->dq_itime) {
405 if ((dq->dq_flags & DQ_INODS) == 0 &&
406 cp->c_uid == kauth_cred_getuid(cred)) {
407 #if 0
408 printf("\nwrite failed, %s %s\n",
409 quotatypes[type],
410 "cnode quota exceeded for too long");
411 #endif
412 dq->dq_flags |= DQ_INODS;
413 }
414 dqunlock(dq);
415
416 return (EDQUOT);
417 }
418 }
419 dqunlock(dq);
420
421 return (0);
422 }
423
424 #if DIAGNOSTIC
425 /*
426 * On filesystems with quotas enabled, it is an error for a file to change
427 * size and not to have a dquot structure associated with it.
428 */
429 void
430 hfs_chkdquot(cp)
431 register struct cnode *cp;
432 {
433 struct vnode *vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
434 struct hfsmount *hfsmp = VTOHFS(vp);
435 register int i;
436
437 for (i = 0; i < MAXQUOTAS; i++) {
438 if (hfsmp->hfs_qfiles[i].qf_vp == NULLVP)
439 continue;
440 if (cp->c_dquot[i] == NODQUOT) {
441 vprint("chkdquot: missing dquot", vp);
442 panic("missing dquot");
443 }
444 }
445 }
446 #endif
447
448 /*
449 * Code to process quotactl commands.
450 */
451
452 /*
453 * Q_QUOTAON - set up a quota file for a particular file system.
454 */
455 struct hfs_quotaon_cargs {
456 int error;
457 };
458
459 static int
460 hfs_quotaon_callback(struct vnode *vp, void *cargs)
461 {
462 struct hfs_quotaon_cargs *args;
463
464 args = (struct hfs_quotaon_cargs *)cargs;
465
466 args->error = hfs_getinoquota(VTOC(vp));
467 if (args->error)
468 return (VNODE_RETURNED_DONE);
469
470 return (VNODE_RETURNED);
471 }
472
473 int
474 hfs_quotaon(p, mp, type, fnamep)
475 struct proc *p;
476 struct mount *mp;
477 register int type;
478 caddr_t fnamep;
479 {
480 struct hfsmount *hfsmp = VFSTOHFS(mp);
481 struct quotafile *qfp;
482 struct vnode *vp;
483 int error = 0;
484 struct hfs_quotaon_cargs args;
485
486 /* Finish setting up quota structures. */
487 dqhashinit();
488
489 qfp = &hfsmp->hfs_qfiles[type];
490
491 if ( (qf_get(qfp, QTF_OPENING)) )
492 return (0);
493
494 error = vnode_open(fnamep, FREAD|FWRITE, 0, 0, &vp, NULL);
495 if (error) {
496 goto out;
497 }
498 if (!vnode_isreg(vp)) {
499 (void) vnode_close(vp, FREAD|FWRITE, NULL);
500 error = EACCES;
501 goto out;
502 }
503 vfs_setflags(mp, (u_int64_t)((unsigned int)MNT_QUOTA));
504 HFS_MOUNT_LOCK(hfsmp, TRUE)
505 hfsmp->hfs_flags |= HFS_QUOTAS;
506 HFS_MOUNT_UNLOCK(hfsmp, TRUE);
507 vnode_setnoflush(vp);
508 /*
509 * Save the credential of the process that turned on quotas.
510 */
511 qfp->qf_cred = kauth_cred_proc_ref(p);
512 qfp->qf_vp = vp;
513 /*
514 * Finish initializing the quota file
515 */
516 error = dqfileopen(qfp, type);
517 if (error) {
518 (void) vnode_close(vp, FREAD|FWRITE, NULL);
519
520 if (IS_VALID_CRED(qfp->qf_cred))
521 kauth_cred_unref(&qfp->qf_cred);
522 qfp->qf_vp = NULLVP;
523 goto out;
524 }
525 qf_put(qfp, QTF_OPENING);
526
527 /*
528 * Search vnodes associated with this mount point,
529 * adding references to quota file being opened.
530 * NB: only need to add dquot's for cnodes being modified.
531 *
532 * hfs_quota_callback will be called for each vnode open for
533 * 'write' (VNODE_WRITEABLE) hung off of this mount point
534 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
535 * properly referenced and unreferenced around the callback
536 */
537 args.error = 0;
538
539 vnode_iterate(mp, VNODE_WRITEABLE | VNODE_WAIT, hfs_quotaon_callback, (void *)&args);
540
541 error = args.error;
542
543 if (error) {
544 hfs_quotaoff(p, mp, type);
545 }
546 return (error);
547
548 out:
549 qf_put(qfp, QTF_OPENING);
550
551 return (error);
552 }
553
554
555 /*
556 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
557 */
558 struct hfs_quotaoff_cargs {
559 int type;
560 };
561
562 static int
563 hfs_quotaoff_callback(struct vnode *vp, void *cargs)
564 {
565 struct hfs_quotaoff_cargs *args;
566 struct cnode *cp;
567 struct dquot *dq;
568
569 args = (struct hfs_quotaoff_cargs *)cargs;
570
571 cp = VTOC(vp);
572
573 dq = cp->c_dquot[args->type];
574 cp->c_dquot[args->type] = NODQUOT;
575
576 dqrele(dq);
577
578 return (VNODE_RETURNED);
579 }
580
581 int
582 hfs_quotaoff(__unused struct proc *p, struct mount *mp, register int type)
583 {
584 struct vnode *qvp;
585 struct hfsmount *hfsmp = VFSTOHFS(mp);
586 struct quotafile *qfp;
587 int error;
588 struct hfs_quotaoff_cargs args;
589
590 /*
591 * If quotas haven't been initialized, there's no work to be done.
592 */
593 if (!dqisinitialized())
594 return (0);
595
596 qfp = &hfsmp->hfs_qfiles[type];
597
598 if ( (qf_get(qfp, QTF_CLOSING)) )
599 return (0);
600 qvp = qfp->qf_vp;
601
602 /*
603 * Sync out any orpaned dirty dquot entries.
604 */
605 dqsync_orphans(qfp);
606
607 /*
608 * Search vnodes associated with this mount point,
609 * deleting any references to quota file being closed.
610 *
611 * hfs_quotaoff_callback will be called for each vnode
612 * hung off of this mount point
613 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
614 * properly referenced and unreferenced around the callback
615 */
616 args.type = type;
617
618 vnode_iterate(mp, VNODE_WAIT, hfs_quotaoff_callback, (void *)&args);
619
620 dqflush(qvp);
621 /* Finish tearing down the quota file */
622 dqfileclose(qfp, type);
623
624 vnode_clearnoflush(qvp);
625 error = vnode_close(qvp, FREAD|FWRITE, NULL);
626
627 qfp->qf_vp = NULLVP;
628
629 if (IS_VALID_CRED(qfp->qf_cred))
630 kauth_cred_unref(&qfp->qf_cred);
631 for (type = 0; type < MAXQUOTAS; type++)
632 if (hfsmp->hfs_qfiles[type].qf_vp != NULLVP)
633 break;
634 if (type == MAXQUOTAS) {
635 vfs_clearflags(mp, (u_int64_t)((unsigned int)MNT_QUOTA));
636 HFS_MOUNT_LOCK(hfsmp, TRUE)
637 hfsmp->hfs_flags &= ~HFS_QUOTAS;
638 HFS_MOUNT_UNLOCK(hfsmp, TRUE);
639 }
640
641 qf_put(qfp, QTF_CLOSING);
642
643 return (error);
644 }
645
646 /*
647 * Q_GETQUOTA - return current values in a dqblk structure.
648 */
649 int
650 hfs_getquota(mp, id, type, datap)
651 struct mount *mp;
652 u_long id;
653 int type;
654 caddr_t datap;
655 {
656 struct dquot *dq;
657 int error;
658
659 error = dqget(id, &VFSTOHFS(mp)->hfs_qfiles[type], type, &dq);
660 if (error)
661 return (error);
662 dqlock(dq);
663
664 bcopy(&dq->dq_dqb, datap, sizeof(dq->dq_dqb));
665
666 dqunlock(dq);
667 dqrele(dq);
668
669 return (error);
670 }
671
672 /*
673 * Q_SETQUOTA - assign an entire dqblk structure.
674 */
675 int
676 hfs_setquota(mp, id, type, datap)
677 struct mount *mp;
678 u_long id;
679 int type;
680 caddr_t datap;
681 {
682 struct dquot *dq;
683 struct hfsmount *hfsmp = VFSTOHFS(mp);
684 struct dqblk * newlimp = (struct dqblk *) datap;
685 struct timeval tv;
686 int error;
687
688 error = dqget(id, &hfsmp->hfs_qfiles[type], type, &dq);
689 if (error)
690 return (error);
691 dqlock(dq);
692
693 /*
694 * Copy all but the current values.
695 * Reset time limit if previously had no soft limit or were
696 * under it, but now have a soft limit and are over it.
697 */
698 newlimp->dqb_curbytes = dq->dq_curbytes;
699 newlimp->dqb_curinodes = dq->dq_curinodes;
700 if (dq->dq_id != 0) {
701 newlimp->dqb_btime = dq->dq_btime;
702 newlimp->dqb_itime = dq->dq_itime;
703 }
704 if (newlimp->dqb_bsoftlimit &&
705 dq->dq_curbytes >= newlimp->dqb_bsoftlimit &&
706 (dq->dq_bsoftlimit == 0 || dq->dq_curbytes < dq->dq_bsoftlimit)) {
707 microuptime(&tv);
708 newlimp->dqb_btime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_btime;
709 }
710 if (newlimp->dqb_isoftlimit &&
711 dq->dq_curinodes >= newlimp->dqb_isoftlimit &&
712 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit)) {
713 microuptime(&tv);
714 newlimp->dqb_itime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_itime;
715 }
716 bcopy(newlimp, &dq->dq_dqb, sizeof(dq->dq_dqb));
717 if (dq->dq_curbytes < dq->dq_bsoftlimit)
718 dq->dq_flags &= ~DQ_BLKS;
719 if (dq->dq_curinodes < dq->dq_isoftlimit)
720 dq->dq_flags &= ~DQ_INODS;
721 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
722 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
723 dq->dq_flags |= DQ_FAKE;
724 else
725 dq->dq_flags &= ~DQ_FAKE;
726 dq->dq_flags |= DQ_MOD;
727
728 dqunlock(dq);
729 dqrele(dq);
730
731 return (0);
732 }
733
734 /*
735 * Q_SETUSE - set current cnode and byte usage.
736 */
737 int
738 hfs_setuse(mp, id, type, datap)
739 struct mount *mp;
740 u_long id;
741 int type;
742 caddr_t datap;
743 {
744 struct hfsmount *hfsmp = VFSTOHFS(mp);
745 struct dquot *dq;
746 struct timeval tv;
747 int error;
748 struct dqblk *quotablkp = (struct dqblk *) datap;
749
750 error = dqget(id, &hfsmp->hfs_qfiles[type], type, &dq);
751 if (error)
752 return (error);
753 dqlock(dq);
754
755 /*
756 * Reset time limit if have a soft limit and were
757 * previously under it, but are now over it.
758 */
759 if (dq->dq_bsoftlimit && dq->dq_curbytes < dq->dq_bsoftlimit &&
760 quotablkp->dqb_curbytes >= dq->dq_bsoftlimit) {
761 microuptime(&tv);
762 dq->dq_btime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_btime;
763 }
764 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
765 quotablkp->dqb_curinodes >= dq->dq_isoftlimit) {
766 microuptime(&tv);
767 dq->dq_itime = tv.tv_sec + hfsmp->hfs_qfiles[type].qf_itime;
768 }
769 dq->dq_curbytes = quotablkp->dqb_curbytes;
770 dq->dq_curinodes = quotablkp->dqb_curinodes;
771 if (dq->dq_curbytes < dq->dq_bsoftlimit)
772 dq->dq_flags &= ~DQ_BLKS;
773 if (dq->dq_curinodes < dq->dq_isoftlimit)
774 dq->dq_flags &= ~DQ_INODS;
775 dq->dq_flags |= DQ_MOD;
776
777 dqunlock(dq);
778 dqrele(dq);
779
780 return (0);
781 }
782
783
784 /*
785 * Q_SYNC - sync quota files to disk.
786 */
787 static int
788 hfs_qsync_callback(struct vnode *vp, __unused void *cargs)
789 {
790 struct cnode *cp;
791 struct dquot *dq;
792 int i;
793
794 cp = VTOC(vp);
795
796 for (i = 0; i < MAXQUOTAS; i++) {
797 dq = cp->c_dquot[i];
798 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
799 dqsync(dq);
800 }
801 return (VNODE_RETURNED);
802 }
803
804 int
805 hfs_qsync(mp)
806 struct mount *mp;
807 {
808 struct hfsmount *hfsmp = VFSTOHFS(mp);
809 int i;
810
811 if (!dqisinitialized())
812 return (0);
813
814 /*
815 * Check if the mount point has any quotas.
816 * If not, simply return.
817 */
818 for (i = 0; i < MAXQUOTAS; i++)
819 if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP)
820 break;
821 if (i == MAXQUOTAS)
822 return (0);
823
824 /*
825 * Sync out any orpaned dirty dquot entries.
826 */
827 for (i = 0; i < MAXQUOTAS; i++)
828 if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP)
829 dqsync_orphans(&hfsmp->hfs_qfiles[i]);
830
831 /*
832 * Search vnodes associated with this mount point,
833 * synchronizing any modified dquot structures.
834 *
835 * hfs_qsync_callback will be called for each vnode
836 * hung off of this mount point
837 * the vnode will be
838 * properly referenced and unreferenced around the callback
839 */
840 vnode_iterate(mp, 0, hfs_qsync_callback, (void *)NULL);
841
842 return (0);
843 }
844
845 /*
846 * Q_QUOTASTAT - get quota on/off status
847 */
848 int
849 hfs_quotastat(mp, type, datap)
850 struct mount *mp;
851 register int type;
852 caddr_t datap;
853 {
854 struct hfsmount *hfsmp = VFSTOHFS(mp);
855 int error = 0;
856 int qstat;
857
858 if ((((unsigned int)vfs_flags(mp)) & MNT_QUOTA) && (hfsmp->hfs_qfiles[type].qf_vp != NULLVP))
859 qstat = 1; /* quotas are on for this type */
860 else
861 qstat = 0; /* quotas are off for this type */
862
863 *((int *)datap) = qstat;
864 return (error);
865 }
866