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