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