]>
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_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * This code is derived from software contributed to Berkeley by
33 * Robert Elz at The University of Melbourne.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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
64 * derived from @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
67 #include <sys/param.h>
68 #include <sys/kernel.h>
69 #include <sys/systm.h>
70 #include <sys/mount.h>
71 #include <sys/malloc.h>
74 #include <sys/kauth.h>
75 #include <sys/vnode.h>
76 #include <sys/quota.h>
77 #include <sys/proc_internal.h>
78 #include <kern/kalloc.h>
81 #include <hfs/hfs_cnode.h>
82 #include <hfs/hfs_quota.h>
83 #include <hfs/hfs_mount.h>
86 * Quota name to error message mapping.
89 static char *quotatypes
[] = INITQFNAMES
;
93 * Set up the quotas for a cnode.
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.
102 register struct cnode
*cp
;
104 struct hfsmount
*hfsmp
;
108 vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
111 * Set up the user quota based on file uid.
112 * EINVAL means that quotas are not enabled.
114 if (cp
->c_dquot
[USRQUOTA
] == NODQUOT
&&
116 dqget(cp
->c_uid
, &hfsmp
->hfs_qfiles
[USRQUOTA
], USRQUOTA
, &cp
->c_dquot
[USRQUOTA
])) &&
120 * Set up the group quota based on file gid.
121 * EINVAL means that quotas are not enabled.
123 if (cp
->c_dquot
[GRPQUOTA
] == NODQUOT
&&
125 dqget(cp
->c_gid
, &hfsmp
->hfs_qfiles
[GRPQUOTA
], GRPQUOTA
, &cp
->c_dquot
[GRPQUOTA
])) &&
132 * Update disk usage, and take corrective action.
135 hfs_chkdq(cp
, change
, cred
, flags
)
136 register struct cnode
*cp
;
141 register struct dquot
*dq
;
148 if ((flags
& CHOWN
) == 0)
154 for (i
= 0; i
< MAXQUOTAS
; i
++) {
155 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
159 ncurbytes
= dq
->dq_curbytes
+ change
;
161 dq
->dq_curbytes
= ncurbytes
;
164 dq
->dq_flags
&= ~DQ_BLKS
;
165 dq
->dq_flags
|= DQ_MOD
;
173 * This use of proc_ucred() is safe because kernproc credential never
176 if (!IS_VALID_CRED(cred
))
177 cred
= proc_ucred(kernproc
);
178 if (suser(cred
, NULL
) || proc_forcequota(p
)) {
179 for (i
= 0; i
< MAXQUOTAS
; i
++) {
180 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
182 error
= hfs_chkdqchg(cp
, change
, cred
, i
);
188 if ((flags
& FORCE
) || error
== 0) {
189 for (i
= 0; i
< MAXQUOTAS
; i
++) {
190 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
194 dq
->dq_curbytes
+= change
;
195 dq
->dq_flags
|= DQ_MOD
;
204 * Check for a valid change to a users allocation.
205 * Issue an error message if appropriate.
208 hfs_chkdqchg(cp
, change
, cred
, type
)
214 register struct dquot
*dq
= cp
->c_dquot
[type
];
216 struct vnode
*vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
220 ncurbytes
= dq
->dq_curbytes
+ change
;
222 * If user would exceed their hard limit, disallow space allocation.
224 if (ncurbytes
>= dq
->dq_bhardlimit
&& dq
->dq_bhardlimit
) {
225 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
226 cp
->c_uid
== kauth_cred_getuid(cred
)) {
228 printf("\nwrite failed, %s disk limit reached\n",
231 dq
->dq_flags
|= DQ_BLKS
;
238 * If user is over their soft limit for too long, disallow space
239 * allocation. Reset time limit as they cross their soft limit.
241 if (ncurbytes
>= dq
->dq_bsoftlimit
&& dq
->dq_bsoftlimit
) {
245 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
) {
246 dq
->dq_btime
= tv
.tv_sec
+
247 VTOHFS(vp
)->hfs_qfiles
[type
].qf_btime
;
249 if (cp
->c_uid
== kauth_cred_getuid(cred
))
250 printf("\nwarning, %s %s\n",
251 quotatypes
[type
], "disk quota exceeded");
257 if (tv
.tv_sec
> dq
->dq_btime
) {
258 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
259 cp
->c_uid
== kauth_cred_getuid(cred
)) {
261 printf("\nwrite failed, %s %s\n",
263 "disk quota exceeded for too long");
265 dq
->dq_flags
|= DQ_BLKS
;
278 * Check the inode limit, applying corrective action.
281 hfs_chkiq(cp
, change
, cred
, flags
)
282 register struct cnode
*cp
;
287 register struct dquot
*dq
;
289 int ncurinodes
, error
=0;
293 if ((flags
& CHOWN
) == 0)
299 for (i
= 0; i
< MAXQUOTAS
; i
++) {
300 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
304 ncurinodes
= dq
->dq_curinodes
+ change
;
306 dq
->dq_curinodes
= ncurinodes
;
308 dq
->dq_curinodes
= 0;
309 dq
->dq_flags
&= ~DQ_INODS
;
310 dq
->dq_flags
|= DQ_MOD
;
318 * This use of proc_ucred() is safe because kernproc credential never
321 if (!IS_VALID_CRED(cred
))
322 cred
= proc_ucred(kernproc
);
323 if (suser(cred
, NULL
) || proc_forcequota(p
)) {
324 for (i
= 0; i
< MAXQUOTAS
; i
++) {
325 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
327 error
= hfs_chkiqchg(cp
, change
, cred
, i
);
333 if ((flags
& FORCE
) || error
== 0) {
334 for (i
= 0; i
< MAXQUOTAS
; i
++) {
335 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
339 dq
->dq_curinodes
+= change
;
340 dq
->dq_flags
|= DQ_MOD
;
349 * Check for a valid change to a users allocation.
350 * Issue an error message if appropriate.
353 hfs_chkiqchg(cp
, change
, cred
, type
)
359 register struct dquot
*dq
= cp
->c_dquot
[type
];
361 struct vnode
*vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
365 ncurinodes
= dq
->dq_curinodes
+ change
;
367 * If user would exceed their hard limit, disallow cnode allocation.
369 if (ncurinodes
>= dq
->dq_ihardlimit
&& dq
->dq_ihardlimit
) {
370 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
371 cp
->c_uid
== kauth_cred_getuid(cred
)) {
373 printf("\nwrite failed, %s cnode limit reached\n",
376 dq
->dq_flags
|= DQ_INODS
;
383 * If user is over their soft limit for too long, disallow cnode
384 * allocation. Reset time limit as they cross their soft limit.
386 if (ncurinodes
>= dq
->dq_isoftlimit
&& dq
->dq_isoftlimit
) {
390 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
) {
391 dq
->dq_itime
= tv
.tv_sec
+
392 VTOHFS(vp
)->hfs_qfiles
[type
].qf_itime
;
394 if (cp
->c_uid
== kauth_cred_getuid(cred
))
395 printf("\nwarning, %s %s\n",
396 quotatypes
[type
], "cnode quota exceeded");
402 if (tv
.tv_sec
> dq
->dq_itime
) {
403 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
404 cp
->c_uid
== kauth_cred_getuid(cred
)) {
406 printf("\nwrite failed, %s %s\n",
408 "cnode quota exceeded for too long");
410 dq
->dq_flags
|= DQ_INODS
;
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.
429 register struct cnode
*cp
;
431 struct vnode
*vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
432 struct hfsmount
*hfsmp
= VTOHFS(vp
);
435 for (i
= 0; i
< MAXQUOTAS
; i
++) {
436 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
438 if (cp
->c_dquot
[i
] == NODQUOT
) {
439 vprint("chkdquot: missing dquot", vp
);
440 panic("missing dquot");
447 * Code to process quotactl commands.
451 * Q_QUOTAON - set up a quota file for a particular file system.
453 struct hfs_quotaon_cargs
{
458 hfs_quotaon_callback(struct vnode
*vp
, void *cargs
)
460 struct hfs_quotaon_cargs
*args
;
462 args
= (struct hfs_quotaon_cargs
*)cargs
;
464 args
->error
= hfs_getinoquota(VTOC(vp
));
466 return (VNODE_RETURNED_DONE
);
468 return (VNODE_RETURNED
);
472 hfs_quotaon(p
, mp
, type
, fnamep
)
478 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
479 struct quotafile
*qfp
;
482 struct hfs_quotaon_cargs args
;
484 qfp
= &hfsmp
->hfs_qfiles
[type
];
486 if ( (qf_get(qfp
, QTF_OPENING
)) )
489 error
= vnode_open(fnamep
, FREAD
|FWRITE
, 0, 0, &vp
, NULL
);
493 if (!vnode_isreg(vp
)) {
494 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
498 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_QUOTA
));
499 vnode_setnoflush(vp
);
501 * Save the credential of the process that turned on quotas.
503 qfp
->qf_cred
= kauth_cred_proc_ref(p
);
506 * Finish initializing the quota file
508 error
= dqfileopen(qfp
, type
);
510 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
512 if (IS_VALID_CRED(qfp
->qf_cred
))
513 kauth_cred_unref(&qfp
->qf_cred
);
517 qf_put(qfp
, QTF_OPENING
);
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.
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
531 vnode_iterate(mp
, VNODE_WRITEABLE
| VNODE_WAIT
, hfs_quotaon_callback
, (void *)&args
);
536 hfs_quotaoff(p
, mp
, type
);
541 qf_put(qfp
, QTF_OPENING
);
548 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
550 struct hfs_quotaoff_cargs
{
555 hfs_quotaoff_callback(struct vnode
*vp
, void *cargs
)
557 struct hfs_quotaoff_cargs
*args
;
561 args
= (struct hfs_quotaoff_cargs
*)cargs
;
565 dq
= cp
->c_dquot
[args
->type
];
566 cp
->c_dquot
[args
->type
] = NODQUOT
;
570 return (VNODE_RETURNED
);
574 hfs_quotaoff(__unused
struct proc
*p
, struct mount
*mp
, register int type
)
577 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
578 struct quotafile
*qfp
;
580 struct hfs_quotaoff_cargs args
;
582 qfp
= &hfsmp
->hfs_qfiles
[type
];
584 if ( (qf_get(qfp
, QTF_CLOSING
)) )
589 * Sync out any orpaned dirty dquot entries.
594 * Search vnodes associated with this mount point,
595 * deleting any references to quota file being closed.
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
604 vnode_iterate(mp
, VNODE_WAIT
, hfs_quotaoff_callback
, (void *)&args
);
607 /* Finish tearing down the quota file */
608 dqfileclose(qfp
, type
);
610 vnode_clearnoflush(qvp
);
611 error
= vnode_close(qvp
, FREAD
|FWRITE
, NULL
);
615 if (IS_VALID_CRED(qfp
->qf_cred
))
616 kauth_cred_unref(&qfp
->qf_cred
);
617 for (type
= 0; type
< MAXQUOTAS
; type
++)
618 if (hfsmp
->hfs_qfiles
[type
].qf_vp
!= NULLVP
)
620 if (type
== MAXQUOTAS
)
621 vfs_clearflags(mp
, (uint64_t)((unsigned int)MNT_QUOTA
));
623 qf_put(qfp
, QTF_CLOSING
);
629 * Q_GETQUOTA - return current values in a dqblk structure.
632 hfs_getquota(mp
, id
, type
, datap
)
641 error
= dqget(id
, &VFSTOHFS(mp
)->hfs_qfiles
[type
], type
, &dq
);
646 bcopy(&dq
->dq_dqb
, datap
, sizeof(dq
->dq_dqb
));
655 * Q_SETQUOTA - assign an entire dqblk structure.
658 hfs_setquota(mp
, id
, type
, datap
)
665 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
666 struct dqblk
* newlimp
= (struct dqblk
*) datap
;
670 error
= dqget(id
, &hfsmp
->hfs_qfiles
[type
], type
, &dq
);
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.
680 newlimp
->dqb_curbytes
= dq
->dq_curbytes
;
681 newlimp
->dqb_curinodes
= dq
->dq_curinodes
;
682 if (dq
->dq_id
!= 0) {
683 newlimp
->dqb_btime
= dq
->dq_btime
;
684 newlimp
->dqb_itime
= dq
->dq_itime
;
686 if (newlimp
->dqb_bsoftlimit
&&
687 dq
->dq_curbytes
>= newlimp
->dqb_bsoftlimit
&&
688 (dq
->dq_bsoftlimit
== 0 || dq
->dq_curbytes
< dq
->dq_bsoftlimit
)) {
690 newlimp
->dqb_btime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_btime
;
692 if (newlimp
->dqb_isoftlimit
&&
693 dq
->dq_curinodes
>= newlimp
->dqb_isoftlimit
&&
694 (dq
->dq_isoftlimit
== 0 || dq
->dq_curinodes
< dq
->dq_isoftlimit
)) {
696 newlimp
->dqb_itime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_itime
;
698 bcopy(newlimp
, &dq
->dq_dqb
, sizeof(dq
->dq_dqb
));
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
;
707 dq
->dq_flags
&= ~DQ_FAKE
;
708 dq
->dq_flags
|= DQ_MOD
;
717 * Q_SETUSE - set current cnode and byte usage.
720 hfs_setuse(mp
, id
, type
, datap
)
726 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
730 struct dqblk
*quotablkp
= (struct dqblk
*) datap
;
732 error
= dqget(id
, &hfsmp
->hfs_qfiles
[type
], type
, &dq
);
738 * Reset time limit if have a soft limit and were
739 * previously under it, but are now over it.
741 if (dq
->dq_bsoftlimit
&& dq
->dq_curbytes
< dq
->dq_bsoftlimit
&&
742 quotablkp
->dqb_curbytes
>= dq
->dq_bsoftlimit
) {
744 dq
->dq_btime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_btime
;
746 if (dq
->dq_isoftlimit
&& dq
->dq_curinodes
< dq
->dq_isoftlimit
&&
747 quotablkp
->dqb_curinodes
>= dq
->dq_isoftlimit
) {
749 dq
->dq_itime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_itime
;
751 dq
->dq_curbytes
= quotablkp
->dqb_curbytes
;
752 dq
->dq_curinodes
= quotablkp
->dqb_curinodes
;
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
;
767 * Q_SYNC - sync quota files to disk.
770 hfs_qsync_callback(struct vnode
*vp
, __unused
void *cargs
)
778 for (i
= 0; i
< MAXQUOTAS
; i
++) {
780 if (dq
!= NODQUOT
&& (dq
->dq_flags
& DQ_MOD
))
783 return (VNODE_RETURNED
);
790 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
794 * Check if the mount point has any quotas.
795 * If not, simply return.
797 for (i
= 0; i
< MAXQUOTAS
; i
++)
798 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
804 * Sync out any orpaned dirty dquot entries.
806 for (i
= 0; i
< MAXQUOTAS
; i
++)
807 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
808 dqsync_orphans(&hfsmp
->hfs_qfiles
[i
]);
811 * Search vnodes associated with this mount point,
812 * synchronizing any modified dquot structures.
814 * hfs_qsync_callback will be called for each vnode
815 * hung off of this mount point
817 * properly referenced and unreferenced around the callback
819 vnode_iterate(mp
, 0, hfs_qsync_callback
, (void *)NULL
);
825 * Q_QUOTASTAT - get quota on/off status
828 hfs_quotastat(mp
, type
, datap
)
833 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
837 if ((((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) && (hfsmp
->hfs_qfiles
[type
].qf_vp
!= NULLVP
))
838 qstat
= 1; /* quotas are on for this type */
840 qstat
= 0; /* quotas are off for this type */
842 *((int *)datap
) = qstat
;