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