]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_quota.c
5708eb0856b00aae8566124df7c3e1973d753f10
2 * Copyright (c) 2000 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
63 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
66 #include <sys/param.h>
67 #include <sys/kernel.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
72 #include <sys/kauth.h>
73 #include <sys/vnode_internal.h>
74 #include <sys/mount_internal.h>
75 #include <sys/namei.h>
76 #include <sys/quota.h>
78 #include <ufs/ufs/quota.h>
79 #include <ufs/ufs/inode.h>
80 #include <ufs/ufs/ufsmount.h>
81 #include <ufs/ufs/ufs_extern.h>
84 * Quota name to error message mapping.
86 static char *quotatypes
[] = INITQFNAMES
;
89 * Set up the quotas for an inode.
91 * This routine completely defines the semantics of quotas.
92 * If other criterion want to be used to establish quotas, the
93 * MAXQUOTAS value in quotas.h should be increased, and the
94 * additional dquots set up here.
98 register struct inode
*ip
;
100 struct ufsmount
*ump
;
101 struct vnode
*vp
= ITOV(ip
);
104 ump
= VFSTOUFS(vp
->v_mount
);
106 * Set up the user quota based on file uid.
107 * EINVAL means that quotas are not enabled.
109 if (ip
->i_dquot
[USRQUOTA
] == NODQUOT
&&
111 dqget(ip
->i_uid
, &ump
->um_qfiles
[USRQUOTA
], USRQUOTA
, &ip
->i_dquot
[USRQUOTA
])) &&
115 * Set up the group quota based on file gid.
116 * EINVAL means that quotas are not enabled.
118 if (ip
->i_dquot
[GRPQUOTA
] == NODQUOT
&&
120 dqget(ip
->i_gid
, &ump
->um_qfiles
[GRPQUOTA
], GRPQUOTA
, &ip
->i_dquot
[GRPQUOTA
])) &&
127 * Update disk usage, and take corrective action.
130 chkdq(struct inode
*ip
, int64_t change
, kauth_cred_t cred
, int flags
)
132 register struct dquot
*dq
;
139 if ((flags
& CHOWN
) == 0)
145 for (i
= 0; i
< MAXQUOTAS
; i
++) {
146 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
150 ncurbytes
= dq
->dq_curbytes
+ change
;
152 dq
->dq_curbytes
= ncurbytes
;
155 dq
->dq_flags
&= ~DQ_BLKS
;
156 dq
->dq_flags
|= DQ_MOD
;
162 #warning "hack for no cred passed to chkdq()"
164 * This use of proc_ucred() is safe because kernproc credential never
168 if (!IS_VALID_CRED(cred
))
169 cred
= proc_ucred(kernproc
);
170 if ((flags
& FORCE
) == 0 && (suser(cred
, NULL
) || (proc_forcequota(p
)))) {
171 for (i
= 0; i
< MAXQUOTAS
; i
++) {
172 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
174 if ( (error
= chkdqchg(ip
, change
, cred
, i
)) )
178 for (i
= 0; i
< MAXQUOTAS
; i
++) {
179 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
183 dq
->dq_curbytes
+= change
;
184 dq
->dq_flags
|= DQ_MOD
;
192 * Check for a valid change to a users allocation.
193 * Issue an error message if appropriate.
196 chkdqchg(struct inode
*ip
, int64_t change
, kauth_cred_t cred
, int type
)
198 register struct dquot
*dq
= ip
->i_dquot
[type
];
203 ncurbytes
= dq
->dq_curbytes
+ change
;
205 * If user would exceed their hard limit, disallow space allocation.
207 if (ncurbytes
>= dq
->dq_bhardlimit
&& dq
->dq_bhardlimit
) {
208 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
209 ip
->i_uid
== kauth_cred_getuid(cred
)) {
211 printf("\n%s: write failed, %s disk limit reached\n",
212 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
215 dq
->dq_flags
|= DQ_BLKS
;
222 * If user is over their soft limit for too long, disallow space
223 * allocation. Reset time limit as they cross their soft limit.
225 if (ncurbytes
>= dq
->dq_bsoftlimit
&& dq
->dq_bsoftlimit
) {
229 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
) {
230 dq
->dq_btime
= tv
.tv_sec
+
231 VFSTOUFS(ITOV(ip
)->v_mount
)->um_qfiles
[type
].qf_btime
;
233 if (ip
->i_uid
== kauth_cred_getuid(cred
))
234 printf("\n%s: warning, %s %s\n",
235 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
236 quotatypes
[type
], "disk quota exceeded");
242 if (tv
.tv_sec
> dq
->dq_btime
) {
243 if ((dq
->dq_flags
& DQ_BLKS
) == 0 &&
244 ip
->i_uid
== kauth_cred_getuid(cred
)) {
246 printf("\n%s: write failed, %s %s\n",
247 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
249 "disk quota exceeded for too long");
251 dq
->dq_flags
|= DQ_BLKS
;
264 * Check the inode limit, applying corrective action.
267 chkiq(struct inode
*ip
, long change
, kauth_cred_t cred
, int flags
)
269 register struct dquot
*dq
;
271 int ncurinodes
, error
;
275 if ((flags
& CHOWN
) == 0)
281 for (i
= 0; i
< MAXQUOTAS
; i
++) {
282 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
286 ncurinodes
= dq
->dq_curinodes
+ change
;
288 dq
->dq_curinodes
= ncurinodes
;
290 dq
->dq_curinodes
= 0;
291 dq
->dq_flags
&= ~DQ_INODS
;
292 dq
->dq_flags
|= DQ_MOD
;
298 #warning "hack for no cred passed to chkiq()"
300 * This use of proc_ucred() is safe because kernproc credential never
304 if (!IS_VALID_CRED(cred
))
305 cred
= proc_ucred(kernproc
);
306 if ((flags
& FORCE
) == 0 && (suser(cred
, NULL
) || (proc_forcequota(p
)))) {
307 for (i
= 0; i
< MAXQUOTAS
; i
++) {
308 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
310 if ( (error
= chkiqchg(ip
, change
, cred
, i
)) )
314 for (i
= 0; i
< MAXQUOTAS
; i
++) {
315 if ((dq
= ip
->i_dquot
[i
]) == NODQUOT
)
319 dq
->dq_curinodes
+= change
;
320 dq
->dq_flags
|= DQ_MOD
;
328 * Check for a valid change to a users allocation.
329 * Issue an error message if appropriate.
332 chkiqchg(struct inode
*ip
, long change
, kauth_cred_t cred
, int type
)
334 register struct dquot
*dq
= ip
->i_dquot
[type
];
339 ncurinodes
= dq
->dq_curinodes
+ change
;
341 * If user would exceed their hard limit, disallow inode allocation.
343 if (ncurinodes
>= dq
->dq_ihardlimit
&& dq
->dq_ihardlimit
) {
344 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
345 ip
->i_uid
== kauth_cred_getuid(cred
)) {
347 printf("\n%s: write failed, %s inode limit reached\n",
348 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
351 dq
->dq_flags
|= DQ_INODS
;
358 * If user is over their soft limit for too long, disallow inode
359 * allocation. Reset time limit as they cross their soft limit.
361 if (ncurinodes
>= dq
->dq_isoftlimit
&& dq
->dq_isoftlimit
) {
365 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
) {
366 dq
->dq_itime
= tv
.tv_sec
+
367 VFSTOUFS(ITOV(ip
)->v_mount
)->um_qfiles
[type
].qf_itime
;
369 if (ip
->i_uid
== kauth_cred_getuid(cred
))
370 printf("\n%s: warning, %s %s\n",
371 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
372 quotatypes
[type
], "inode quota exceeded");
378 if (tv
.tv_sec
> dq
->dq_itime
) {
379 if ((dq
->dq_flags
& DQ_INODS
) == 0 &&
380 ip
->i_uid
== kauth_cred_getuid(cred
)) {
382 printf("\n%s: write failed, %s %s\n",
383 ITOV(ip
)->v_mount
->mnt_vfsstat
.f_mntonname
,
385 "inode quota exceeded for too long");
387 dq
->dq_flags
|= DQ_INODS
;
401 * On filesystems with quotas enabled, it is an error for a file to change
402 * size and not to have a dquot structure associated with it.
406 register struct inode
*ip
;
408 struct ufsmount
*ump
= VFSTOUFS(ITOV(ip
)->v_mount
);
411 for (i
= 0; i
< MAXQUOTAS
; i
++) {
412 if (ump
->um_qfiles
[i
].qf_vp
== NULLVP
)
414 if (ip
->i_dquot
[i
] == NODQUOT
) {
415 vprint("chkdquot: missing dquot", ITOV(ip
));
416 panic("missing dquot");
423 * Code to process quotactl commands.
427 struct ufs_quotaon_cargs
{
433 ufs_quotaon_callback(struct vnode
*vp
, void *cargs
)
435 struct ufs_quotaon_cargs
*args
;
437 args
= (struct ufs_quotaon_cargs
*)cargs
;
439 if ( (args
->error
= getinoquota(VTOI(vp
))) )
440 return (VNODE_RETURNED_DONE
);
442 return (VNODE_RETURNED
);
447 * Q_QUOTAON - set up a quota file for a particular file system.
450 quotaon(context
, mp
, type
, fnamep
)
451 vfs_context_t context
;
456 struct ufsmount
*ump
= VFSTOUFS(mp
);
457 struct quotafile
*qfp
;
460 struct ufs_quotaon_cargs args
;
462 qfp
= &ump
->um_qfiles
[type
];
464 if ( (qf_get(qfp
, QTF_OPENING
)) )
467 error
= vnode_open(fnamep
, FREAD
|FWRITE
, 0, 0, &vp
, NULL
);
471 if (!vnode_isreg(vp
)) {
472 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
476 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_QUOTA
));
477 vnode_setnoflush(vp
);
479 * Save the credential of the process that turned on quotas.
482 qfp
->qf_cred
= vfs_context_ucred(context
);
483 kauth_cred_ref(qfp
->qf_cred
);
486 * Finish initializing the quota file
488 if ( (error
= dqfileopen(&ump
->um_qfiles
[type
], type
)) ) {
489 (void) vnode_close(vp
, FREAD
|FWRITE
, NULL
);
491 kauth_cred_unref(&qfp
->qf_cred
);
495 qf_put(qfp
, QTF_OPENING
);
498 * Search vnodes associated with this mount point,
499 * adding references to quota file being opened.
500 * NB: only need to add dquot's for inodes being modified.
502 * ufs_quota_callback will be called for each vnode open for
503 * 'write' (VNODE_WRITEABLE) hung off of this mount point
504 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
505 * properly referenced and unreferenced around the callback
509 vnode_iterate(mp
, VNODE_WRITEABLE
| VNODE_WAIT
, ufs_quotaon_callback
, (void *)&args
);
517 qf_put(qfp
, QTF_OPENING
);
524 struct ufs_quotaoff_cargs
{
529 ufs_quotaoff_callback(struct vnode
*vp
, void *cargs
)
531 struct ufs_quotaoff_cargs
*args
;
535 args
= (struct ufs_quotaoff_cargs
*)cargs
;
539 dq
= ip
->i_dquot
[args
->type
];
540 ip
->i_dquot
[args
->type
] = NODQUOT
;
544 return (VNODE_RETURNED
);
548 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
551 quotaoff(struct mount
*mp
, register int type
)
554 struct ufsmount
*ump
= VFSTOUFS(mp
);
555 struct quotafile
*qfp
;
557 struct ufs_quotaoff_cargs args
;
559 qfp
= &ump
->um_qfiles
[type
];
561 if ( (qf_get(qfp
, QTF_CLOSING
)) )
566 * Sync out any orpaned dirty dquot entries.
571 * Search vnodes associated with this mount point,
572 * deleting any references to quota file being closed.
574 * ufs_quotaoff_callback will be called for each vnode
575 * hung off of this mount point
576 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
577 * properly referenced and unreferenced around the callback
581 vnode_iterate(mp
, VNODE_WAIT
, ufs_quotaoff_callback
, (void *)&args
);
584 /* Finish tearing down the quota file */
585 dqfileclose(qfp
, type
);
587 vnode_clearnoflush(qvp
);
588 error
= vnode_close(qvp
, FREAD
|FWRITE
, NULL
);
591 if (IS_VALID_CRED(qfp
->qf_cred
)) {
592 kauth_cred_unref(&qfp
->qf_cred
);
594 for (type
= 0; type
< MAXQUOTAS
; type
++)
595 if (ump
->um_qfiles
[type
].qf_vp
!= NULLVP
)
597 if (type
== MAXQUOTAS
)
598 mp
->mnt_flag
&= ~MNT_QUOTA
;
600 qf_put(qfp
, QTF_CLOSING
);
606 * Q_GETQUOTA - return current values in a dqblk structure.
609 getquota(mp
, id
, type
, datap
)
618 if ( (error
= dqget(id
, &VFSTOUFS(mp
)->um_qfiles
[type
], type
, &dq
)) )
622 bcopy(&dq
->dq_dqb
, datap
, sizeof(dq
->dq_dqb
));
631 * Q_SETQUOTA - assign an entire dqblk structure.
634 setquota(mp
, id
, type
, datap
)
641 struct ufsmount
*ump
= VFSTOUFS(mp
);
642 struct dqblk
* newlimp
= (struct dqblk
*) datap
;
646 error
= dqget(id
, &ump
->um_qfiles
[type
], type
, &dq
);
652 * Copy all but the current values.
653 * Reset time limit if previously had no soft limit or were
654 * under it, but now have a soft limit and are over it.
656 newlimp
->dqb_curbytes
= dq
->dq_curbytes
;
657 newlimp
->dqb_curinodes
= dq
->dq_curinodes
;
658 if (dq
->dq_id
!= 0) {
659 newlimp
->dqb_btime
= dq
->dq_btime
;
660 newlimp
->dqb_itime
= dq
->dq_itime
;
662 if (newlimp
->dqb_bsoftlimit
&&
663 dq
->dq_curbytes
>= newlimp
->dqb_bsoftlimit
&&
664 (dq
->dq_bsoftlimit
== 0 || dq
->dq_curbytes
< dq
->dq_bsoftlimit
)) {
666 newlimp
->dqb_btime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_btime
;
668 if (newlimp
->dqb_isoftlimit
&&
669 dq
->dq_curinodes
>= newlimp
->dqb_isoftlimit
&&
670 (dq
->dq_isoftlimit
== 0 || dq
->dq_curinodes
< dq
->dq_isoftlimit
)) {
672 newlimp
->dqb_itime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_itime
;
674 bcopy(newlimp
, &dq
->dq_dqb
, sizeof(dq
->dq_dqb
));
675 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
)
676 dq
->dq_flags
&= ~DQ_BLKS
;
677 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
)
678 dq
->dq_flags
&= ~DQ_INODS
;
679 if (dq
->dq_isoftlimit
== 0 && dq
->dq_bsoftlimit
== 0 &&
680 dq
->dq_ihardlimit
== 0 && dq
->dq_bhardlimit
== 0)
681 dq
->dq_flags
|= DQ_FAKE
;
683 dq
->dq_flags
&= ~DQ_FAKE
;
684 dq
->dq_flags
|= DQ_MOD
;
693 * Q_SETUSE - set current inode and byte usage.
696 setuse(mp
, id
, type
, datap
)
703 struct ufsmount
*ump
= VFSTOUFS(mp
);
706 struct dqblk
*quotablkp
= (struct dqblk
*) datap
;
708 error
= dqget(id
, &ump
->um_qfiles
[type
], type
, &dq
);
714 * Reset time limit if have a soft limit and were
715 * previously under it, but are now over it.
717 if (dq
->dq_bsoftlimit
&& dq
->dq_curbytes
< dq
->dq_bsoftlimit
&&
718 quotablkp
->dqb_curbytes
>= dq
->dq_bsoftlimit
) {
720 dq
->dq_btime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_btime
;
722 if (dq
->dq_isoftlimit
&& dq
->dq_curinodes
< dq
->dq_isoftlimit
&&
723 quotablkp
->dqb_curinodes
>= dq
->dq_isoftlimit
) {
725 dq
->dq_itime
= tv
.tv_sec
+ ump
->um_qfiles
[type
].qf_itime
;
727 dq
->dq_curbytes
= quotablkp
->dqb_curbytes
;
728 dq
->dq_curinodes
= quotablkp
->dqb_curinodes
;
729 if (dq
->dq_curbytes
< dq
->dq_bsoftlimit
)
730 dq
->dq_flags
&= ~DQ_BLKS
;
731 if (dq
->dq_curinodes
< dq
->dq_isoftlimit
)
732 dq
->dq_flags
&= ~DQ_INODS
;
733 dq
->dq_flags
|= DQ_MOD
;
744 ufs_qsync_callback(struct vnode
*vp
, __unused
void *cargs
)
752 for (i
= 0; i
< MAXQUOTAS
; i
++) {
754 if (dq
!= NODQUOT
&& (dq
->dq_flags
& DQ_MOD
))
757 return (VNODE_RETURNED
);
762 * Q_SYNC - sync quota files to disk.
768 struct ufsmount
*ump
= VFSTOUFS(mp
);
772 * Check if the mount point has any quotas.
773 * If not, simply return.
775 for (i
= 0; i
< MAXQUOTAS
; i
++)
776 if (ump
->um_qfiles
[i
].qf_vp
!= NULLVP
)
781 * Search vnodes associated with this mount point,
782 * synchronizing any modified dquot structures.
784 * ufs_qsync_callback will be called for each vnode
785 * hung off of this mount point
787 * properly referenced and unreferenced around the callback
789 vnode_iterate(mp
, 0, ufs_qsync_callback
, (void *)NULL
);
795 * Q_QUOTASTAT - get quota on/off status
798 quotastat(mp
, type
, datap
)
803 struct ufsmount
*ump
= VFSTOUFS(mp
);
807 if ((mp
->mnt_flag
& MNT_QUOTA
) && (ump
->um_qfiles
[type
].qf_vp
!= NULLVP
))
808 qstat
= 1; /* quotas are on for this type */
810 qstat
= 0; /* quotas are off for this type */
811 *((int *)datap
) = qstat
;