]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_quota.c
a1704ed379916a7d227748d8cace9a5f5b667c12
2 * Copyright (c) 2000 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
65 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
68 #include <sys/param.h>
69 #include <sys/kernel.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
74 #include <sys/kauth.h>
75 #include <sys/vnode_internal.h>
76 #include <sys/mount_internal.h>
77 #include <sys/namei.h>
78 #include <sys/quota.h>
80 #include <ufs/ufs/quota.h>
81 #include <ufs/ufs/inode.h>
82 #include <ufs/ufs/ufsmount.h>
83 #include <ufs/ufs/ufs_extern.h>
86 * Quota name to error message mapping.
88 static char *quotatypes
[] = INITQFNAMES
;
91 * Set up the quotas for an inode.
93 * This routine completely defines the semantics of quotas.
94 * If other criterion want to be used to establish quotas, the
95 * MAXQUOTAS value in quotas.h should be increased, and the
96 * additional dquots set up here.
100 register struct inode
*ip
;
102 struct ufsmount
*ump
;
103 struct vnode
*vp
= ITOV(ip
);
106 ump
= VFSTOUFS(vp
->v_mount
);
108 * Set up the user quota based on file uid.
109 * EINVAL means that quotas are not enabled.
111 if (ip
->i_dquot
[USRQUOTA
] == NODQUOT
&&
113 dqget(ip
->i_uid
, &ump
->um_qfiles
[USRQUOTA
], USRQUOTA
, &ip
->i_dquot
[USRQUOTA
])) &&
117 * Set up the group quota based on file gid.
118 * EINVAL means that quotas are not enabled.
120 if (ip
->i_dquot
[GRPQUOTA
] == NODQUOT
&&
122 dqget(ip
->i_gid
, &ump
->um_qfiles
[GRPQUOTA
], GRPQUOTA
, &ip
->i_dquot
[GRPQUOTA
])) &&
129 * Update disk usage, and take corrective action.
132 chkdq(struct inode
*ip
, int64_t change
, kauth_cred_t cred
, int flags
)
134 register struct dquot
*dq
;
141 if ((flags
& CHOWN
) == 0)
147 for (i
= 0; i
< MAXQUOTAS
; i
++) {
148 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
152 ncurbytes
= dq
->dq_curbytes
+ change
;
154 dq
->dq_curbytes
= ncurbytes
;
157 dq
->dq_flags
&= ~DQ_BLKS
;
158 dq
->dq_flags
|= DQ_MOD
;
164 #warning "hack for no cred passed to chkdq()"
167 cred
= proc_ucred(kernproc
);
168 if ((flags
& FORCE
) == 0 && (suser(cred
, NULL
) || (proc_forcequota(p
)))) {
169 for (i
= 0; i
< MAXQUOTAS
; i
++) {
170 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
172 if ( (error
= chkdqchg(ip
, change
, cred
, i
)) )
176 for (i
= 0; i
< MAXQUOTAS
; i
++) {
177 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
181 dq
->dq_curbytes
+= change
;
182 dq
->dq_flags
|= DQ_MOD
;
190 * Check for a valid change to a users allocation.
191 * Issue an error message if appropriate.
194 chkdqchg(struct inode
*ip
, int64_t change
, kauth_cred_t cred
, int type
)
196 register struct dquot
*dq
= ip
->i_dquot
[type
];
201 ncurbytes
= dq
->dq_curbytes
+ change
;
203 * If user would exceed their hard limit, disallow space allocation.
205 if (ncurbytes
>= dq
->dq_bhardlimit
&& dq
->dq_bhardlimit
) {
206 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
207 ip
->i_uid
== kauth_cred_getuid(cred
)) {
209 printf("\n%s: write failed, %s disk limit reached\n",
210 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
213 dq
->dq_flags
|= DQ_BLKS
;
220 * If user is over their soft limit for too long, disallow space
221 * allocation. Reset time limit as they cross their soft limit.
223 if (ncurbytes
>= dq
->dq_bsoftlimit
&& dq
->dq_bsoftlimit
) {
227 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
) {
228 dq
->dq_btime
= tv
.tv_sec
+
229 VFSTOUFS(ITOV(ip
)->v_mount
)->um_qfiles
[type
].qf_btime
;
231 if (ip
->i_uid
== kauth_cred_getuid(cred
))
232 printf("\n%s: warning, %s %s\n",
233 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
234 quotatypes
[type
], "disk quota exceeded");
240 if (tv
.tv_sec
> dq
->dq_btime
) {
241 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
242 ip
->i_uid
== kauth_cred_getuid(cred
)) {
244 printf("\n%s: write failed, %s %s\n",
245 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
247 "disk quota exceeded for too long");
249 dq
->dq_flags
|= DQ_BLKS
;
262 * Check the inode limit, applying corrective action.
265 chkiq(struct inode
*ip
, long change
, kauth_cred_t cred
, int flags
)
267 register struct dquot
*dq
;
269 int ncurinodes
, error
;
273 if ((flags
& CHOWN
) == 0)
279 for (i
= 0; i
< MAXQUOTAS
; i
++) {
280 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
284 ncurinodes
= dq
->dq_curinodes
+ change
;
286 dq
->dq_curinodes
= ncurinodes
;
288 dq
->dq_curinodes
= 0;
289 dq
->dq_flags
&= ~DQ_INODS
;
290 dq
->dq_flags
|= DQ_MOD
;
296 #warning "hack for no cred passed to chkiq()"
299 cred
= proc_ucred(kernproc
);
300 if ((flags
& FORCE
) == 0 && (suser(cred
, NULL
) || (proc_forcequota(p
)))) {
301 for (i
= 0; i
< MAXQUOTAS
; i
++) {
302 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
304 if ( (error
= chkiqchg(ip
, change
, cred
, i
)) )
308 for (i
= 0; i
< MAXQUOTAS
; i
++) {
309 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
313 dq
->dq_curinodes
+= change
;
314 dq
->dq_flags
|= DQ_MOD
;
322 * Check for a valid change to a users allocation.
323 * Issue an error message if appropriate.
326 chkiqchg(struct inode
*ip
, long change
, kauth_cred_t cred
, int type
)
328 register struct dquot
*dq
= ip
->i_dquot
[type
];
333 ncurinodes
= dq
->dq_curinodes
+ change
;
335 * If user would exceed their hard limit, disallow inode allocation.
337 if (ncurinodes
>= dq
->dq_ihardlimit
&& dq
->dq_ihardlimit
) {
338 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
339 ip
->i_uid
== kauth_cred_getuid(cred
)) {
341 printf("\n%s: write failed, %s inode limit reached\n",
342 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
345 dq
->dq_flags
|= DQ_INODS
;
352 * If user is over their soft limit for too long, disallow inode
353 * allocation. Reset time limit as they cross their soft limit.
355 if (ncurinodes
>= dq
->dq_isoftlimit
&& dq
->dq_isoftlimit
) {
359 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
) {
360 dq
->dq_itime
= tv
.tv_sec
+
361 VFSTOUFS(ITOV(ip
)->v_mount
)->um_qfiles
[type
].qf_itime
;
363 if (ip
->i_uid
== kauth_cred_getuid(cred
))
364 printf("\n%s: warning, %s %s\n",
365 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
366 quotatypes
[type
], "inode quota exceeded");
372 if (tv
.tv_sec
> dq
->dq_itime
) {
373 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
374 ip
->i_uid
== kauth_cred_getuid(cred
)) {
376 printf("\n%s: write failed, %s %s\n",
377 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
379 "inode quota exceeded for too long");
381 dq
->dq_flags
|= DQ_INODS
;
395 * On filesystems with quotas enabled, it is an error for a file to change
396 * size and not to have a dquot structure associated with it.
400 register struct inode
*ip
;
402 struct ufsmount
*ump
= VFSTOUFS(ITOV(ip
)->v_mount
);
405 for (i
= 0; i
< MAXQUOTAS
; i
++) {
406 if (ump
->um_qfiles
[i
].qf_vp
== NULLVP
)
408 if (ip
->i_dquot
[i
] == NODQUOT
) {
409 vprint("chkdquot: missing dquot", ITOV(ip
));
410 panic("missing dquot");
417 * Code to process quotactl commands.
421 struct ufs_quotaon_cargs
{
427 ufs_quotaon_callback(struct vnode
*vp
, void *cargs
)
429 struct ufs_quotaon_cargs
*args
;
431 args
= (struct ufs_quotaon_cargs
*)cargs
;
433 if ( (args
->error
= getinoquota(VTOI(vp
))) )
434 return (VNODE_RETURNED_DONE
);
436 return (VNODE_RETURNED
);
441 * Q_QUOTAON - set up a quota file for a particular file system.
444 quotaon(context
, mp
, type
, fnamep
)
445 vfs_context_t context
;
450 struct ufsmount
*ump
= VFSTOUFS(mp
);
451 struct quotafile
*qfp
;
454 struct ufs_quotaon_cargs args
;
456 qfp
= &ump
->um_qfiles
[type
];
458 if ( (qf_get(qfp
, QTF_OPENING
)) )
461 error
= vnode_open(fnamep
, FREAD
|FWRITE
, 0, 0, &vp
, NULL
);
465 if (!vnode_isreg(vp
)) {
466 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
470 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_QUOTA
));
471 vnode_setnoflush(vp
);
473 * Save the credential of the process that turned on quotas.
476 qfp
->qf_cred
= vfs_context_ucred(context
);
477 kauth_cred_ref(qfp
->qf_cred
);
480 * Finish initializing the quota file
482 if ( (error
= dqfileopen(&ump
->um_qfiles
[type
], type
)) ) {
483 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
485 kauth_cred_rele(qfp
->qf_cred
);
486 qfp
->qf_cred
= NOCRED
;
490 qf_put(qfp
, QTF_OPENING
);
493 * Search vnodes associated with this mount point,
494 * adding references to quota file being opened.
495 * NB: only need to add dquot's for inodes being modified.
497 * ufs_quota_callback will be called for each vnode open for
498 * 'write' (VNODE_WRITEABLE) hung off of this mount point
499 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
500 * properly referenced and unreferenced around the callback
504 vnode_iterate(mp
, VNODE_WRITEABLE
| VNODE_WAIT
, ufs_quotaon_callback
, (void *)&args
);
512 qf_put(qfp
, QTF_OPENING
);
519 struct ufs_quotaoff_cargs
{
524 ufs_quotaoff_callback(struct vnode
*vp
, void *cargs
)
526 struct ufs_quotaoff_cargs
*args
;
530 args
= (struct ufs_quotaoff_cargs
*)cargs
;
534 dq
= ip
->i_dquot
[args
->type
];
535 ip
->i_dquot
[args
->type
] = NODQUOT
;
539 return (VNODE_RETURNED
);
543 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
546 quotaoff(struct mount
*mp
, register int type
)
549 struct ufsmount
*ump
= VFSTOUFS(mp
);
550 struct quotafile
*qfp
;
553 struct ufs_quotaoff_cargs args
;
555 qfp
= &ump
->um_qfiles
[type
];
557 if ( (qf_get(qfp
, QTF_CLOSING
)) )
562 * Sync out any orpaned dirty dquot entries.
567 * Search vnodes associated with this mount point,
568 * deleting any references to quota file being closed.
570 * ufs_quotaoff_callback will be called for each vnode
571 * hung off of this mount point
572 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
573 * properly referenced and unreferenced around the callback
577 vnode_iterate(mp
, VNODE_WAIT
, ufs_quotaoff_callback
, (void *)&args
);
580 /* Finish tearing down the quota file */
581 dqfileclose(qfp
, type
);
583 vnode_clearnoflush(qvp
);
584 error
= vnode_close(qvp
, FREAD
|FWRITE
, NULL
);
588 if (cred
!= NOCRED
) {
589 qfp
->qf_cred
= NOCRED
;
590 kauth_cred_rele(cred
);
592 for (type
= 0; type
< MAXQUOTAS
; type
++)
593 if (ump
->um_qfiles
[type
].qf_vp
!= NULLVP
)
595 if (type
== MAXQUOTAS
)
596 mp
->mnt_flag
&= ~MNT_QUOTA
;
598 qf_put(qfp
, QTF_CLOSING
);
604 * Q_GETQUOTA - return current values in a dqblk structure.
607 getquota(mp
, id
, type
, datap
)
616 if ( (error
= dqget(id
, &VFSTOUFS(mp
)->um_qfiles
[type
], type
, &dq
)) )
620 bcopy(&dq
->dq_dqb
, datap
, sizeof(dq
->dq_dqb
));
629 * Q_SETQUOTA - assign an entire dqblk structure.
632 setquota(mp
, id
, type
, datap
)
639 struct ufsmount
*ump
= VFSTOUFS(mp
);
640 struct dqblk
* newlimp
= (struct dqblk
*) datap
;
644 error
= dqget(id
, &ump
->um_qfiles
[type
], type
, &dq
);
650 * Copy all but the current values.
651 * Reset time limit if previously had no soft limit or were
652 * under it, but now have a soft limit and are over it.
654 newlimp
->dqb_curbytes
= dq
->dq_curbytes
;
655 newlimp
->dqb_curinodes
= dq
->dq_curinodes
;
656 if (dq
->dq_id
!= 0) {
657 newlimp
->dqb_btime
= dq
->dq_btime
;
658 newlimp
->dqb_itime
= dq
->dq_itime
;
660 if (newlimp
->dqb_bsoftlimit
&&
661 dq
->dq_curbytes
>= newlimp
->dqb_bsoftlimit
&&
662 (dq
->dq_bsoftlimit
== 0 || dq
->dq_curbytes
< dq
->dq_bsoftlimit
)) {
664 newlimp
->dqb_btime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_btime
;
666 if (newlimp
->dqb_isoftlimit
&&
667 dq
->dq_curinodes
>= newlimp
->dqb_isoftlimit
&&
668 (dq
->dq_isoftlimit
== 0 || dq
->dq_curinodes
< dq
->dq_isoftlimit
)) {
670 newlimp
->dqb_itime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_itime
;
672 bcopy(newlimp
, &dq
->dq_dqb
, sizeof(dq
->dq_dqb
));
673 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
)
674 dq
->dq_flags
&= ~DQ_BLKS
;
675 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
)
676 dq
->dq_flags
&= ~DQ_INODS
;
677 if (dq
->dq_isoftlimit
== 0 && dq
->dq_bsoftlimit
== 0 &&
678 dq
->dq_ihardlimit
== 0 && dq
->dq_bhardlimit
== 0)
679 dq
->dq_flags
|= DQ_FAKE
;
681 dq
->dq_flags
&= ~DQ_FAKE
;
682 dq
->dq_flags
|= DQ_MOD
;
691 * Q_SETUSE - set current inode and byte usage.
694 setuse(mp
, id
, type
, datap
)
701 struct ufsmount
*ump
= VFSTOUFS(mp
);
704 struct dqblk
*quotablkp
= (struct dqblk
*) datap
;
706 error
= dqget(id
, &ump
->um_qfiles
[type
], type
, &dq
);
712 * Reset time limit if have a soft limit and were
713 * previously under it, but are now over it.
715 if (dq
->dq_bsoftlimit
&& dq
->dq_curbytes
< dq
->dq_bsoftlimit
&&
716 quotablkp
->dqb_curbytes
>= dq
->dq_bsoftlimit
) {
718 dq
->dq_btime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_btime
;
720 if (dq
->dq_isoftlimit
&& dq
->dq_curinodes
< dq
->dq_isoftlimit
&&
721 quotablkp
->dqb_curinodes
>= dq
->dq_isoftlimit
) {
723 dq
->dq_itime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_itime
;
725 dq
->dq_curbytes
= quotablkp
->dqb_curbytes
;
726 dq
->dq_curinodes
= quotablkp
->dqb_curinodes
;
727 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
)
728 dq
->dq_flags
&= ~DQ_BLKS
;
729 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
)
730 dq
->dq_flags
&= ~DQ_INODS
;
731 dq
->dq_flags
|= DQ_MOD
;
742 ufs_qsync_callback(struct vnode
*vp
, __unused
void *cargs
)
750 for (i
= 0; i
< MAXQUOTAS
; i
++) {
752 if (dq
!= NODQUOT
&& (dq
->dq_flags
& DQ_MOD
))
755 return (VNODE_RETURNED
);
760 * Q_SYNC - sync quota files to disk.
766 struct ufsmount
*ump
= VFSTOUFS(mp
);
770 * Check if the mount point has any quotas.
771 * If not, simply return.
773 for (i
= 0; i
< MAXQUOTAS
; i
++)
774 if (ump
->um_qfiles
[i
].qf_vp
!= NULLVP
)
779 * Search vnodes associated with this mount point,
780 * synchronizing any modified dquot structures.
782 * ufs_qsync_callback will be called for each vnode
783 * hung off of this mount point
785 * properly referenced and unreferenced around the callback
787 vnode_iterate(mp
, 0, ufs_qsync_callback
, (void *)NULL
);
793 * Q_QUOTASTAT - get quota on/off status
796 quotastat(mp
, type
, datap
)
801 struct ufsmount
*ump
= VFSTOUFS(mp
);
805 if ((mp
->mnt_flag
& MNT_QUOTA
) && (ump
->um_qfiles
[type
].qf_vp
!= NULLVP
))
806 qstat
= 1; /* quotas are on for this type */
808 qstat
= 0; /* quotas are off for this type */
809 *((int *)datap
) = qstat
;