]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_quota.c
2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1990, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
27 * Robert Elz at The University of Melbourne.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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
58 * derived from @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
61 #include <sys/param.h>
62 #include <sys/kernel.h>
63 #include <sys/systm.h>
64 #include <sys/mount.h>
65 #include <sys/malloc.h>
68 #include <sys/kauth.h>
69 #include <sys/vnode.h>
70 #include <sys/quota.h>
71 #include <sys/proc_internal.h>
72 #include <kern/kalloc.h>
75 #include <hfs/hfs_cnode.h>
76 #include <hfs/hfs_quota.h>
77 #include <hfs/hfs_mount.h>
80 * Quota name to error message mapping.
83 static char *quotatypes
[] = INITQFNAMES
;
87 * Set up the quotas for a cnode.
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.
96 register struct cnode
*cp
;
98 struct hfsmount
*hfsmp
;
102 vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
105 * Set up the user quota based on file uid.
106 * EINVAL means that quotas are not enabled.
108 if (cp
->c_dquot
[USRQUOTA
] == NODQUOT
&&
110 dqget(cp
->c_uid
, &hfsmp
->hfs_qfiles
[USRQUOTA
], USRQUOTA
, &cp
->c_dquot
[USRQUOTA
])) &&
114 * Set up the group quota based on file gid.
115 * EINVAL means that quotas are not enabled.
117 if (cp
->c_dquot
[GRPQUOTA
] == NODQUOT
&&
119 dqget(cp
->c_gid
, &hfsmp
->hfs_qfiles
[GRPQUOTA
], GRPQUOTA
, &cp
->c_dquot
[GRPQUOTA
])) &&
126 * Update disk usage, and take corrective action.
129 hfs_chkdq(cp
, change
, cred
, flags
)
130 register struct cnode
*cp
;
135 register struct dquot
*dq
;
142 if ((flags
& CHOWN
) == 0)
148 for (i
= 0; i
< MAXQUOTAS
; i
++) {
149 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
153 ncurbytes
= dq
->dq_curbytes
+ change
;
155 dq
->dq_curbytes
= ncurbytes
;
158 dq
->dq_flags
&= ~DQ_BLKS
;
159 dq
->dq_flags
|= DQ_MOD
;
167 * This use of proc_ucred() is safe because kernproc credential never
170 if (!IS_VALID_CRED(cred
))
171 cred
= proc_ucred(kernproc
);
172 if (suser(cred
, NULL
) || proc_forcequota(p
)) {
173 for (i
= 0; i
< MAXQUOTAS
; i
++) {
174 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
176 error
= hfs_chkdqchg(cp
, change
, cred
, i
);
182 if ((flags
& FORCE
) || error
== 0) {
183 for (i
= 0; i
< MAXQUOTAS
; i
++) {
184 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
188 dq
->dq_curbytes
+= change
;
189 dq
->dq_flags
|= DQ_MOD
;
198 * Check for a valid change to a users allocation.
199 * Issue an error message if appropriate.
202 hfs_chkdqchg(cp
, change
, cred
, type
)
208 register struct dquot
*dq
= cp
->c_dquot
[type
];
210 struct vnode
*vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
214 ncurbytes
= dq
->dq_curbytes
+ change
;
216 * If user would exceed their hard limit, disallow space allocation.
218 if (ncurbytes
>= dq
->dq_bhardlimit
&& dq
->dq_bhardlimit
) {
219 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
220 cp
->c_uid
== kauth_cred_getuid(cred
)) {
222 printf("\nwrite failed, %s disk limit reached\n",
225 dq
->dq_flags
|= DQ_BLKS
;
232 * If user is over their soft limit for too long, disallow space
233 * allocation. Reset time limit as they cross their soft limit.
235 if (ncurbytes
>= dq
->dq_bsoftlimit
&& dq
->dq_bsoftlimit
) {
239 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
) {
240 dq
->dq_btime
= tv
.tv_sec
+
241 VTOHFS(vp
)->hfs_qfiles
[type
].qf_btime
;
243 if (cp
->c_uid
== kauth_cred_getuid(cred
))
244 printf("\nwarning, %s %s\n",
245 quotatypes
[type
], "disk quota exceeded");
251 if (tv
.tv_sec
> dq
->dq_btime
) {
252 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
253 cp
->c_uid
== kauth_cred_getuid(cred
)) {
255 printf("\nwrite failed, %s %s\n",
257 "disk quota exceeded for too long");
259 dq
->dq_flags
|= DQ_BLKS
;
272 * Check the inode limit, applying corrective action.
275 hfs_chkiq(cp
, change
, cred
, flags
)
276 register struct cnode
*cp
;
281 register struct dquot
*dq
;
283 int ncurinodes
, error
=0;
287 if ((flags
& CHOWN
) == 0)
293 for (i
= 0; i
< MAXQUOTAS
; i
++) {
294 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
298 ncurinodes
= dq
->dq_curinodes
+ change
;
300 dq
->dq_curinodes
= ncurinodes
;
302 dq
->dq_curinodes
= 0;
303 dq
->dq_flags
&= ~DQ_INODS
;
304 dq
->dq_flags
|= DQ_MOD
;
312 * This use of proc_ucred() is safe because kernproc credential never
315 if (!IS_VALID_CRED(cred
))
316 cred
= proc_ucred(kernproc
);
317 if (suser(cred
, NULL
) || proc_forcequota(p
)) {
318 for (i
= 0; i
< MAXQUOTAS
; i
++) {
319 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
321 error
= hfs_chkiqchg(cp
, change
, cred
, i
);
327 if ((flags
& FORCE
) || error
== 0) {
328 for (i
= 0; i
< MAXQUOTAS
; i
++) {
329 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
333 dq
->dq_curinodes
+= change
;
334 dq
->dq_flags
|= DQ_MOD
;
343 * Check for a valid change to a users allocation.
344 * Issue an error message if appropriate.
347 hfs_chkiqchg(cp
, change
, cred
, type
)
353 register struct dquot
*dq
= cp
->c_dquot
[type
];
355 struct vnode
*vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
359 ncurinodes
= dq
->dq_curinodes
+ change
;
361 * If user would exceed their hard limit, disallow cnode allocation.
363 if (ncurinodes
>= dq
->dq_ihardlimit
&& dq
->dq_ihardlimit
) {
364 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
365 cp
->c_uid
== kauth_cred_getuid(cred
)) {
367 printf("\nwrite failed, %s cnode limit reached\n",
370 dq
->dq_flags
|= DQ_INODS
;
377 * If user is over their soft limit for too long, disallow cnode
378 * allocation. Reset time limit as they cross their soft limit.
380 if (ncurinodes
>= dq
->dq_isoftlimit
&& dq
->dq_isoftlimit
) {
384 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
) {
385 dq
->dq_itime
= tv
.tv_sec
+
386 VTOHFS(vp
)->hfs_qfiles
[type
].qf_itime
;
388 if (cp
->c_uid
== kauth_cred_getuid(cred
))
389 printf("\nwarning, %s %s\n",
390 quotatypes
[type
], "cnode quota exceeded");
396 if (tv
.tv_sec
> dq
->dq_itime
) {
397 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
398 cp
->c_uid
== kauth_cred_getuid(cred
)) {
400 printf("\nwrite failed, %s %s\n",
402 "cnode quota exceeded for too long");
404 dq
->dq_flags
|= DQ_INODS
;
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.
423 register struct cnode
*cp
;
425 struct vnode
*vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
426 struct hfsmount
*hfsmp
= VTOHFS(vp
);
429 for (i
= 0; i
< MAXQUOTAS
; i
++) {
430 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
432 if (cp
->c_dquot
[i
] == NODQUOT
) {
433 vprint("chkdquot: missing dquot", vp
);
434 panic("missing dquot");
441 * Code to process quotactl commands.
445 * Q_QUOTAON - set up a quota file for a particular file system.
447 struct hfs_quotaon_cargs
{
452 hfs_quotaon_callback(struct vnode
*vp
, void *cargs
)
454 struct hfs_quotaon_cargs
*args
;
456 args
= (struct hfs_quotaon_cargs
*)cargs
;
458 args
->error
= hfs_getinoquota(VTOC(vp
));
460 return (VNODE_RETURNED_DONE
);
462 return (VNODE_RETURNED
);
466 hfs_quotaon(p
, mp
, type
, fnamep
)
472 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
473 struct quotafile
*qfp
;
476 struct hfs_quotaon_cargs args
;
478 qfp
= &hfsmp
->hfs_qfiles
[type
];
480 if ( (qf_get(qfp
, QTF_OPENING
)) )
483 error
= vnode_open(fnamep
, FREAD
|FWRITE
, 0, 0, &vp
, NULL
);
487 if (!vnode_isreg(vp
)) {
488 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
492 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_QUOTA
));
493 vnode_setnoflush(vp
);
495 * Save the credential of the process that turned on quotas.
497 qfp
->qf_cred
= kauth_cred_proc_ref(p
);
500 * Finish initializing the quota file
502 error
= dqfileopen(qfp
, type
);
504 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
506 if (IS_VALID_CRED(qfp
->qf_cred
))
507 kauth_cred_unref(&qfp
->qf_cred
);
511 qf_put(qfp
, QTF_OPENING
);
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.
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
525 vnode_iterate(mp
, VNODE_WRITEABLE
| VNODE_WAIT
, hfs_quotaon_callback
, (void *)&args
);
530 hfs_quotaoff(p
, mp
, type
);
535 qf_put(qfp
, QTF_OPENING
);
542 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
544 struct hfs_quotaoff_cargs
{
549 hfs_quotaoff_callback(struct vnode
*vp
, void *cargs
)
551 struct hfs_quotaoff_cargs
*args
;
555 args
= (struct hfs_quotaoff_cargs
*)cargs
;
559 dq
= cp
->c_dquot
[args
->type
];
560 cp
->c_dquot
[args
->type
] = NODQUOT
;
564 return (VNODE_RETURNED
);
568 hfs_quotaoff(__unused
struct proc
*p
, struct mount
*mp
, register int type
)
571 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
572 struct quotafile
*qfp
;
574 struct hfs_quotaoff_cargs args
;
576 qfp
= &hfsmp
->hfs_qfiles
[type
];
578 if ( (qf_get(qfp
, QTF_CLOSING
)) )
583 * Sync out any orpaned dirty dquot entries.
588 * Search vnodes associated with this mount point,
589 * deleting any references to quota file being closed.
591 * hfs_quotaoff_callback will be called for each vnode
592 * hung off of this mount point
593 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
594 * properly referenced and unreferenced around the callback
598 vnode_iterate(mp
, VNODE_WAIT
, hfs_quotaoff_callback
, (void *)&args
);
601 /* Finish tearing down the quota file */
602 dqfileclose(qfp
, type
);
604 vnode_clearnoflush(qvp
);
605 error
= vnode_close(qvp
, FREAD
|FWRITE
, NULL
);
609 if (IS_VALID_CRED(qfp
->qf_cred
))
610 kauth_cred_unref(&qfp
->qf_cred
);
611 for (type
= 0; type
< MAXQUOTAS
; type
++)
612 if (hfsmp
->hfs_qfiles
[type
].qf_vp
!= NULLVP
)
614 if (type
== MAXQUOTAS
)
615 vfs_clearflags(mp
, (uint64_t)((unsigned int)MNT_QUOTA
));
617 qf_put(qfp
, QTF_CLOSING
);
623 * Q_GETQUOTA - return current values in a dqblk structure.
626 hfs_getquota(mp
, id
, type
, datap
)
635 error
= dqget(id
, &VFSTOHFS(mp
)->hfs_qfiles
[type
], type
, &dq
);
640 bcopy(&dq
->dq_dqb
, datap
, sizeof(dq
->dq_dqb
));
649 * Q_SETQUOTA - assign an entire dqblk structure.
652 hfs_setquota(mp
, id
, type
, datap
)
659 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
660 struct dqblk
* newlimp
= (struct dqblk
*) datap
;
664 error
= dqget(id
, &hfsmp
->hfs_qfiles
[type
], type
, &dq
);
670 * Copy all but the current values.
671 * Reset time limit if previously had no soft limit or were
672 * under it, but now have a soft limit and are over it.
674 newlimp
->dqb_curbytes
= dq
->dq_curbytes
;
675 newlimp
->dqb_curinodes
= dq
->dq_curinodes
;
676 if (dq
->dq_id
!= 0) {
677 newlimp
->dqb_btime
= dq
->dq_btime
;
678 newlimp
->dqb_itime
= dq
->dq_itime
;
680 if (newlimp
->dqb_bsoftlimit
&&
681 dq
->dq_curbytes
>= newlimp
->dqb_bsoftlimit
&&
682 (dq
->dq_bsoftlimit
== 0 || dq
->dq_curbytes
< dq
->dq_bsoftlimit
)) {
684 newlimp
->dqb_btime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_btime
;
686 if (newlimp
->dqb_isoftlimit
&&
687 dq
->dq_curinodes
>= newlimp
->dqb_isoftlimit
&&
688 (dq
->dq_isoftlimit
== 0 || dq
->dq_curinodes
< dq
->dq_isoftlimit
)) {
690 newlimp
->dqb_itime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_itime
;
692 bcopy(newlimp
, &dq
->dq_dqb
, sizeof(dq
->dq_dqb
));
693 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
)
694 dq
->dq_flags
&= ~DQ_BLKS
;
695 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
)
696 dq
->dq_flags
&= ~DQ_INODS
;
697 if (dq
->dq_isoftlimit
== 0 && dq
->dq_bsoftlimit
== 0 &&
698 dq
->dq_ihardlimit
== 0 && dq
->dq_bhardlimit
== 0)
699 dq
->dq_flags
|= DQ_FAKE
;
701 dq
->dq_flags
&= ~DQ_FAKE
;
702 dq
->dq_flags
|= DQ_MOD
;
711 * Q_SETUSE - set current cnode and byte usage.
714 hfs_setuse(mp
, id
, type
, datap
)
720 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
724 struct dqblk
*quotablkp
= (struct dqblk
*) datap
;
726 error
= dqget(id
, &hfsmp
->hfs_qfiles
[type
], type
, &dq
);
732 * Reset time limit if have a soft limit and were
733 * previously under it, but are now over it.
735 if (dq
->dq_bsoftlimit
&& dq
->dq_curbytes
< dq
->dq_bsoftlimit
&&
736 quotablkp
->dqb_curbytes
>= dq
->dq_bsoftlimit
) {
738 dq
->dq_btime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_btime
;
740 if (dq
->dq_isoftlimit
&& dq
->dq_curinodes
< dq
->dq_isoftlimit
&&
741 quotablkp
->dqb_curinodes
>= dq
->dq_isoftlimit
) {
743 dq
->dq_itime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_itime
;
745 dq
->dq_curbytes
= quotablkp
->dqb_curbytes
;
746 dq
->dq_curinodes
= quotablkp
->dqb_curinodes
;
747 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
)
748 dq
->dq_flags
&= ~DQ_BLKS
;
749 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
)
750 dq
->dq_flags
&= ~DQ_INODS
;
751 dq
->dq_flags
|= DQ_MOD
;
761 * Q_SYNC - sync quota files to disk.
764 hfs_qsync_callback(struct vnode
*vp
, __unused
void *cargs
)
772 for (i
= 0; i
< MAXQUOTAS
; i
++) {
774 if (dq
!= NODQUOT
&& (dq
->dq_flags
& DQ_MOD
))
777 return (VNODE_RETURNED
);
784 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
788 * Check if the mount point has any quotas.
789 * If not, simply return.
791 for (i
= 0; i
< MAXQUOTAS
; i
++)
792 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
798 * Sync out any orpaned dirty dquot entries.
800 for (i
= 0; i
< MAXQUOTAS
; i
++)
801 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
802 dqsync_orphans(&hfsmp
->hfs_qfiles
[i
]);
805 * Search vnodes associated with this mount point,
806 * synchronizing any modified dquot structures.
808 * hfs_qsync_callback will be called for each vnode
809 * hung off of this mount point
811 * properly referenced and unreferenced around the callback
813 vnode_iterate(mp
, 0, hfs_qsync_callback
, (void *)NULL
);
819 * Q_QUOTASTAT - get quota on/off status
822 hfs_quotastat(mp
, type
, datap
)
827 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
831 if ((((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) && (hfsmp
->hfs_qfiles
[type
].qf_vp
!= NULLVP
))
832 qstat
= 1; /* quotas are on for this type */
834 qstat
= 0; /* quotas are off for this type */
836 *((int *)datap
) = qstat
;