]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_quota.c
debab6cc4ba0253ba8c75bb0c853afca952f3899
2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1990, 1993, 1995
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
35 * Robert Elz at The University of Melbourne.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
66 * derived from @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
69 #include <sys/param.h>
70 #include <sys/kernel.h>
71 #include <sys/systm.h>
72 #include <sys/mount.h>
73 #include <sys/malloc.h>
76 #include <sys/kauth.h>
77 #include <sys/vnode.h>
78 #include <sys/quota.h>
79 #include <sys/proc_internal.h>
80 #include <kern/kalloc.h>
83 #include <hfs/hfs_cnode.h>
84 #include <hfs/hfs_quota.h>
85 #include <hfs/hfs_mount.h>
88 * Quota name to error message mapping.
91 static char *quotatypes
[] = INITQFNAMES
;
95 * Set up the quotas for a cnode.
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.
104 register struct cnode
*cp
;
106 struct hfsmount
*hfsmp
;
110 vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
113 * Set up the user quota based on file uid.
114 * EINVAL means that quotas are not enabled.
116 if (cp
->c_dquot
[USRQUOTA
] == NODQUOT
&&
118 dqget(cp
->c_uid
, &hfsmp
->hfs_qfiles
[USRQUOTA
], USRQUOTA
, &cp
->c_dquot
[USRQUOTA
])) &&
122 * Set up the group quota based on file gid.
123 * EINVAL means that quotas are not enabled.
125 if (cp
->c_dquot
[GRPQUOTA
] == NODQUOT
&&
127 dqget(cp
->c_gid
, &hfsmp
->hfs_qfiles
[GRPQUOTA
], GRPQUOTA
, &cp
->c_dquot
[GRPQUOTA
])) &&
134 * Update disk usage, and take corrective action.
137 hfs_chkdq(cp
, change
, cred
, flags
)
138 register struct cnode
*cp
;
143 register struct dquot
*dq
;
150 if ((flags
& CHOWN
) == 0)
156 for (i
= 0; i
< MAXQUOTAS
; i
++) {
157 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
161 ncurbytes
= dq
->dq_curbytes
+ change
;
163 dq
->dq_curbytes
= ncurbytes
;
166 dq
->dq_flags
&= ~DQ_BLKS
;
167 dq
->dq_flags
|= DQ_MOD
;
175 cred
= proc_ucred(kernproc
);
176 if (suser(cred
, NULL
) || proc_forcequota(p
)) {
177 for (i
= 0; i
< MAXQUOTAS
; i
++) {
178 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
180 error
= hfs_chkdqchg(cp
, change
, cred
, i
);
186 if ((flags
& FORCE
) || error
== 0) {
187 for (i
= 0; i
< MAXQUOTAS
; i
++) {
188 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
192 dq
->dq_curbytes
+= change
;
193 dq
->dq_flags
|= DQ_MOD
;
202 * Check for a valid change to a users allocation.
203 * Issue an error message if appropriate.
206 hfs_chkdqchg(cp
, change
, cred
, type
)
212 register struct dquot
*dq
= cp
->c_dquot
[type
];
214 struct vnode
*vp
= cp
->c_vp
? cp
->c_vp
: cp
->c_rsrc_vp
;
218 ncurbytes
= dq
->dq_curbytes
+ change
;
220 * If user would exceed their hard limit, disallow space allocation.
222 if (ncurbytes
>= dq
->dq_bhardlimit
&& dq
->dq_bhardlimit
) {
223 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
224 cp
->c_uid
== kauth_cred_getuid(cred
)) {
226 printf("\nwrite failed, %s disk limit reached\n",
229 dq
->dq_flags
|= DQ_BLKS
;
236 * If user is over their soft limit for too long, disallow space
237 * allocation. Reset time limit as they cross their soft limit.
239 if (ncurbytes
>= dq
->dq_bsoftlimit
&& dq
->dq_bsoftlimit
) {
243 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
) {
244 dq
->dq_btime
= tv
.tv_sec
+
245 VTOHFS(vp
)->hfs_qfiles
[type
].qf_btime
;
247 if (cp
->c_uid
== kauth_cred_getuid(cred
))
248 printf("\nwarning, %s %s\n",
249 quotatypes
[type
], "disk quota exceeded");
255 if (tv
.tv_sec
> dq
->dq_btime
) {
256 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
257 cp
->c_uid
== kauth_cred_getuid(cred
)) {
259 printf("\nwrite failed, %s %s\n",
261 "disk quota exceeded for too long");
263 dq
->dq_flags
|= DQ_BLKS
;
276 * Check the inode limit, applying corrective action.
279 hfs_chkiq(cp
, change
, cred
, flags
)
280 register struct cnode
*cp
;
285 register struct dquot
*dq
;
287 int ncurinodes
, error
=0;
291 if ((flags
& CHOWN
) == 0)
297 for (i
= 0; i
< MAXQUOTAS
; i
++) {
298 if ((dq
= cp
->c_dquot
[i
]) == NODQUOT
)
302 ncurinodes
= dq
->dq_curinodes
+ change
;
304 dq
->dq_curinodes
= ncurinodes
;
306 dq
->dq_curinodes
= 0;
307 dq
->dq_flags
&= ~DQ_INODS
;
308 dq
->dq_flags
|= DQ_MOD
;
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 kauth_cred_rele(qfp
->qf_cred
);
507 qfp
->qf_cred
= NOCRED
;
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
;
575 struct hfs_quotaoff_cargs args
;
577 qfp
= &hfsmp
->hfs_qfiles
[type
];
579 if ( (qf_get(qfp
, QTF_CLOSING
)) )
584 * Sync out any orpaned dirty dquot entries.
589 * Search vnodes associated with this mount point,
590 * deleting any references to quota file being closed.
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
599 vnode_iterate(mp
, VNODE_WAIT
, hfs_quotaoff_callback
, (void *)&args
);
602 /* Finish tearing down the quota file */
603 dqfileclose(qfp
, type
);
605 vnode_clearnoflush(qvp
);
606 error
= vnode_close(qvp
, FREAD
|FWRITE
, NULL
);
610 if (cred
!= NOCRED
) {
611 qfp
->qf_cred
= NOCRED
;
612 kauth_cred_rele(cred
);
614 for (type
= 0; type
< MAXQUOTAS
; type
++)
615 if (hfsmp
->hfs_qfiles
[type
].qf_vp
!= NULLVP
)
617 if (type
== MAXQUOTAS
)
618 vfs_clearflags(mp
, (uint64_t)((unsigned int)MNT_QUOTA
));
620 qf_put(qfp
, QTF_CLOSING
);
626 * Q_GETQUOTA - return current values in a dqblk structure.
629 hfs_getquota(mp
, id
, type
, datap
)
638 error
= dqget(id
, &VFSTOHFS(mp
)->hfs_qfiles
[type
], type
, &dq
);
643 bcopy(&dq
->dq_dqb
, datap
, sizeof(dq
->dq_dqb
));
652 * Q_SETQUOTA - assign an entire dqblk structure.
655 hfs_setquota(mp
, id
, type
, datap
)
662 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
663 struct dqblk
* newlimp
= (struct dqblk
*) datap
;
667 error
= dqget(id
, &hfsmp
->hfs_qfiles
[type
], type
, &dq
);
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.
677 newlimp
->dqb_curbytes
= dq
->dq_curbytes
;
678 newlimp
->dqb_curinodes
= dq
->dq_curinodes
;
679 if (dq
->dq_id
!= 0) {
680 newlimp
->dqb_btime
= dq
->dq_btime
;
681 newlimp
->dqb_itime
= dq
->dq_itime
;
683 if (newlimp
->dqb_bsoftlimit
&&
684 dq
->dq_curbytes
>= newlimp
->dqb_bsoftlimit
&&
685 (dq
->dq_bsoftlimit
== 0 || dq
->dq_curbytes
< dq
->dq_bsoftlimit
)) {
687 newlimp
->dqb_btime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_btime
;
689 if (newlimp
->dqb_isoftlimit
&&
690 dq
->dq_curinodes
>= newlimp
->dqb_isoftlimit
&&
691 (dq
->dq_isoftlimit
== 0 || dq
->dq_curinodes
< dq
->dq_isoftlimit
)) {
693 newlimp
->dqb_itime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_itime
;
695 bcopy(newlimp
, &dq
->dq_dqb
, sizeof(dq
->dq_dqb
));
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
;
704 dq
->dq_flags
&= ~DQ_FAKE
;
705 dq
->dq_flags
|= DQ_MOD
;
714 * Q_SETUSE - set current cnode and byte usage.
717 hfs_setuse(mp
, id
, type
, datap
)
723 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
727 struct dqblk
*quotablkp
= (struct dqblk
*) datap
;
729 error
= dqget(id
, &hfsmp
->hfs_qfiles
[type
], type
, &dq
);
735 * Reset time limit if have a soft limit and were
736 * previously under it, but are now over it.
738 if (dq
->dq_bsoftlimit
&& dq
->dq_curbytes
< dq
->dq_bsoftlimit
&&
739 quotablkp
->dqb_curbytes
>= dq
->dq_bsoftlimit
) {
741 dq
->dq_btime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_btime
;
743 if (dq
->dq_isoftlimit
&& dq
->dq_curinodes
< dq
->dq_isoftlimit
&&
744 quotablkp
->dqb_curinodes
>= dq
->dq_isoftlimit
) {
746 dq
->dq_itime
= tv
.tv_sec
+ hfsmp
->hfs_qfiles
[type
].qf_itime
;
748 dq
->dq_curbytes
= quotablkp
->dqb_curbytes
;
749 dq
->dq_curinodes
= quotablkp
->dqb_curinodes
;
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
;
764 * Q_SYNC - sync quota files to disk.
767 hfs_qsync_callback(struct vnode
*vp
, __unused
void *cargs
)
775 for (i
= 0; i
< MAXQUOTAS
; i
++) {
777 if (dq
!= NODQUOT
&& (dq
->dq_flags
& DQ_MOD
))
780 return (VNODE_RETURNED
);
787 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
791 * Check if the mount point has any quotas.
792 * If not, simply return.
794 for (i
= 0; i
< MAXQUOTAS
; i
++)
795 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
801 * Sync out any orpaned dirty dquot entries.
803 for (i
= 0; i
< MAXQUOTAS
; i
++)
804 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
805 dqsync_orphans(&hfsmp
->hfs_qfiles
[i
]);
808 * Search vnodes associated with this mount point,
809 * synchronizing any modified dquot structures.
811 * hfs_qsync_callback will be called for each vnode
812 * hung off of this mount point
814 * properly referenced and unreferenced around the callback
816 vnode_iterate(mp
, 0, hfs_qsync_callback
, (void *)NULL
);
822 * Q_QUOTASTAT - get quota on/off status
825 hfs_quotastat(mp
, type
, datap
)
830 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
834 if ((((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) && (hfsmp
->hfs_qfiles
[type
].qf_vp
!= NULLVP
))
835 qstat
= 1; /* quotas are on for this type */
837 qstat
= 0; /* quotas are off for this type */
839 *((int *)datap
) = qstat
;