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