]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_quota.c
11278c5f5876fae716bb5af66bf1b1d759efd15f
[apple/xnu.git] / bsd / hfs / hfs_quota.c
1 /*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1982, 1986, 1990, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
25 *
26 * This code is derived from software contributed to Berkeley by
27 * Robert Elz at The University of Melbourne.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 *
57 * @(#)hfs_quota.c
58 * derived from @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
59 */
60
61 #include <sys/param.h>
62 #include <sys/kernel.h>
63 #include <sys/systm.h>
64 #include <sys/mount.h>
65 #include <sys/namei.h>
66 #include <sys/malloc.h>
67 #include <sys/file.h>
68 #include <sys/proc.h>
69 #include <sys/vnode.h>
70 #include <sys/quota.h>
71
72 #include <hfs/hfs.h>
73 #include <hfs/hfs_cnode.h>
74 #include <hfs/hfs_quota.h>
75 #include <hfs/hfs_mount.h>
76
77 /*
78 * Quota name to error message mapping.
79 */
80 static char *quotatypes[] = INITQFNAMES;
81
82 /*
83 * Set up the quotas for a cnode.
84 *
85 * This routine completely defines the semantics of quotas.
86 * If other criterion want to be used to establish quotas, the
87 * MAXQUOTAS value in quotas.h should be increased, and the
88 * additional dquots set up here.
89 */
90 int
91 hfs_getinoquota(cp)
92 register struct cnode *cp;
93 {
94 struct hfsmount *hfsmp;
95 struct vnode *vp;
96 int error;
97
98 vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
99 hfsmp = VFSTOHFS(vp->v_mount);
100 /*
101 * Set up the user quota based on file uid.
102 * EINVAL means that quotas are not enabled.
103 */
104 if (cp->c_dquot[USRQUOTA] == NODQUOT &&
105 (error =
106 dqget(vp, cp->c_uid, &hfsmp->hfs_qfiles[USRQUOTA], USRQUOTA, &cp->c_dquot[USRQUOTA])) &&
107 error != EINVAL)
108 return (error);
109 /*
110 * Set up the group quota based on file gid.
111 * EINVAL means that quotas are not enabled.
112 */
113 if (cp->c_dquot[GRPQUOTA] == NODQUOT &&
114 (error =
115 dqget(vp, cp->c_gid, &hfsmp->hfs_qfiles[GRPQUOTA], GRPQUOTA, &cp->c_dquot[GRPQUOTA])) &&
116 error != EINVAL)
117 return (error);
118 return (0);
119 }
120
121 /*
122 * Update disk usage, and take corrective action.
123 */
124 int
125 hfs_chkdq(cp, change, cred, flags)
126 register struct cnode *cp;
127 int64_t change;
128 struct ucred *cred;
129 int flags;
130 {
131 register struct dquot *dq;
132 register int i;
133 int64_t ncurbytes;
134 int error=0;
135 struct proc *p;
136
137 #if DIAGNOSTIC
138 if ((flags & CHOWN) == 0)
139 hfs_chkdquot(cp);
140 #endif
141 if (change == 0)
142 return (0);
143 if (change < 0) {
144 for (i = 0; i < MAXQUOTAS; i++) {
145 if ((dq = cp->c_dquot[i]) == NODQUOT)
146 continue;
147 while (dq->dq_flags & DQ_LOCK) {
148 dq->dq_flags |= DQ_WANT;
149 sleep((caddr_t)dq, PINOD+1);
150 }
151 ncurbytes = dq->dq_curbytes + change;
152 if (ncurbytes >= 0)
153 dq->dq_curbytes = ncurbytes;
154 else
155 dq->dq_curbytes = 0;
156 dq->dq_flags &= ~DQ_BLKS;
157 dq->dq_flags |= DQ_MOD;
158 }
159 return (0);
160 }
161 p = current_proc();
162 if (cred == NOCRED)
163 cred = kernproc->p_ucred;
164 if ((cred->cr_uid != 0) || (p->p_flag & P_FORCEQUOTA)) {
165 for (i = 0; i < MAXQUOTAS; i++) {
166 if ((dq = cp->c_dquot[i]) == NODQUOT)
167 continue;
168 error = hfs_chkdqchg(cp, change, cred, i);
169 if (error) {
170 break;
171 }
172 }
173 }
174 if ((flags & FORCE) || error == 0) {
175 for (i = 0; i < MAXQUOTAS; i++) {
176 if ((dq = cp->c_dquot[i]) == NODQUOT)
177 continue;
178 while (dq->dq_flags & DQ_LOCK) {
179 dq->dq_flags |= DQ_WANT;
180 sleep((caddr_t)dq, PINOD+1);
181 }
182 dq->dq_curbytes += change;
183 dq->dq_flags |= DQ_MOD;
184 }
185 }
186 return (error);
187 }
188
189 /*
190 * Check for a valid change to a users allocation.
191 * Issue an error message if appropriate.
192 */
193 int
194 hfs_chkdqchg(cp, change, cred, type)
195 struct cnode *cp;
196 int64_t change;
197 struct ucred *cred;
198 int type;
199 {
200 register struct dquot *dq = cp->c_dquot[type];
201 u_int64_t ncurbytes = dq->dq_curbytes + change;
202 struct vnode *vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
203
204 /*
205 * If user would exceed their hard limit, disallow space allocation.
206 */
207 if (ncurbytes >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
208 if ((dq->dq_flags & DQ_BLKS) == 0 &&
209 cp->c_uid == cred->cr_uid) {
210 #if 0
211 printf("\n%s: write failed, %s disk limit reached\n",
212 vp->v_mount->mnt_stat.f_mntonname,
213 quotatypes[type]);
214 #endif
215 dq->dq_flags |= DQ_BLKS;
216 }
217 return (EDQUOT);
218 }
219 /*
220 * If user is over their soft limit for too long, disallow space
221 * allocation. Reset time limit as they cross their soft limit.
222 */
223 if (ncurbytes >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
224 if (dq->dq_curbytes < dq->dq_bsoftlimit) {
225 dq->dq_btime = time.tv_sec +
226 VFSTOHFS(vp->v_mount)->hfs_qfiles[type].qf_btime;
227 #if 0
228 if (cp->c_uid == cred->cr_uid)
229 printf("\n%s: warning, %s %s\n",
230 vp->v_mount->mnt_stat.f_mntonname,
231 quotatypes[type], "disk quota exceeded");
232 #endif
233 return (0);
234 }
235 if (time.tv_sec > dq->dq_btime) {
236 if ((dq->dq_flags & DQ_BLKS) == 0 &&
237 cp->c_uid == cred->cr_uid) {
238 #if 0
239 printf("\n%s: write failed, %s %s\n",
240 vp->v_mount->mnt_stat.f_mntonname,
241 quotatypes[type],
242 "disk quota exceeded for too long");
243 #endif
244 dq->dq_flags |= DQ_BLKS;
245 }
246 return (EDQUOT);
247 }
248 }
249 return (0);
250 }
251
252 /*
253 * Check the inode limit, applying corrective action.
254 */
255 int
256 hfs_chkiq(cp, change, cred, flags)
257 register struct cnode *cp;
258 long change;
259 struct ucred *cred;
260 int flags;
261 {
262 register struct dquot *dq;
263 register int i;
264 int ncurinodes, error=0;
265 struct proc *p;
266
267 #if DIAGNOSTIC
268 if ((flags & CHOWN) == 0)
269 hfs_chkdquot(cp);
270 #endif
271 if (change == 0)
272 return (0);
273 if (change < 0) {
274 for (i = 0; i < MAXQUOTAS; i++) {
275 if ((dq = cp->c_dquot[i]) == NODQUOT)
276 continue;
277 while (dq->dq_flags & DQ_LOCK) {
278 dq->dq_flags |= DQ_WANT;
279 sleep((caddr_t)dq, PINOD+1);
280 }
281 ncurinodes = dq->dq_curinodes + change;
282 if (ncurinodes >= 0)
283 dq->dq_curinodes = ncurinodes;
284 else
285 dq->dq_curinodes = 0;
286 dq->dq_flags &= ~DQ_INODS;
287 dq->dq_flags |= DQ_MOD;
288 }
289 return (0);
290 }
291 p = current_proc();
292 if (cred == NOCRED)
293 cred = kernproc->p_ucred;
294 if ((cred->cr_uid != 0) || (p->p_flag & P_FORCEQUOTA)) {
295 for (i = 0; i < MAXQUOTAS; i++) {
296 if ((dq = cp->c_dquot[i]) == NODQUOT)
297 continue;
298 error = hfs_chkiqchg(cp, change, cred, i);
299 if (error) {
300 break;
301 }
302 }
303 }
304 if ((flags & FORCE) || error == 0) {
305 for (i = 0; i < MAXQUOTAS; i++) {
306 if ((dq = cp->c_dquot[i]) == NODQUOT)
307 continue;
308 while (dq->dq_flags & DQ_LOCK) {
309 dq->dq_flags |= DQ_WANT;
310 sleep((caddr_t)dq, PINOD+1);
311 }
312 dq->dq_curinodes += change;
313 dq->dq_flags |= DQ_MOD;
314 }
315 }
316 return (error);
317 }
318
319 /*
320 * Check for a valid change to a users allocation.
321 * Issue an error message if appropriate.
322 */
323 int
324 hfs_chkiqchg(cp, change, cred, type)
325 struct cnode *cp;
326 long change;
327 struct ucred *cred;
328 int type;
329 {
330 register struct dquot *dq = cp->c_dquot[type];
331 long ncurinodes = dq->dq_curinodes + change;
332 struct vnode *vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
333
334 /*
335 * If user would exceed their hard limit, disallow cnode allocation.
336 */
337 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
338 if ((dq->dq_flags & DQ_INODS) == 0 &&
339 cp->c_uid == cred->cr_uid) {
340 #if 0
341 printf("\n%s: write failed, %s cnode limit reached\n",
342 vp->v_mount->mnt_stat.f_mntonname,
343 quotatypes[type]);
344 #endif
345 dq->dq_flags |= DQ_INODS;
346 }
347 return (EDQUOT);
348 }
349 /*
350 * If user is over their soft limit for too long, disallow cnode
351 * allocation. Reset time limit as they cross their soft limit.
352 */
353 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
354 if (dq->dq_curinodes < dq->dq_isoftlimit) {
355 dq->dq_itime = time.tv_sec +
356 VFSTOHFS(vp->v_mount)->hfs_qfiles[type].qf_itime;
357 #if 0
358 if (cp->c_uid == cred->cr_uid)
359 printf("\n%s: warning, %s %s\n",
360 vp->v_mount->mnt_stat.f_mntonname,
361 quotatypes[type], "cnode quota exceeded");
362 #endif
363 return (0);
364 }
365 if (time.tv_sec > dq->dq_itime) {
366 if ((dq->dq_flags & DQ_INODS) == 0 &&
367 cp->c_uid == cred->cr_uid) {
368 #if 0
369 printf("\n%s: write failed, %s %s\n",
370 vp->v_mount->mnt_stat.f_mntonname,
371 quotatypes[type],
372 "cnode quota exceeded for too long");
373 #endif
374 dq->dq_flags |= DQ_INODS;
375 }
376 return (EDQUOT);
377 }
378 }
379 return (0);
380 }
381
382 #if DIAGNOSTIC
383 /*
384 * On filesystems with quotas enabled, it is an error for a file to change
385 * size and not to have a dquot structure associated with it.
386 */
387 void
388 hfs_chkdquot(cp)
389 register struct cnode *cp;
390 {
391 struct vnode *vp = cp->c_vp ? cp->c_vp : cp->c_rsrc_vp;
392 struct hfsmount *hfsmp = VFSTOHFS(vp->v_mount);
393 register int i;
394
395 for (i = 0; i < MAXQUOTAS; i++) {
396 if (hfsmp->hfs_qfiles[i].qf_vp == NULLVP ||
397 (hfsmp->hfs_qfiles[i].qf_qflags & (QTF_OPENING|QTF_CLOSING)))
398 continue;
399 if (cp->c_dquot[i] == NODQUOT) {
400 vprint("chkdquot: missing dquot", vp);
401 panic("missing dquot");
402 }
403 }
404 }
405 #endif
406
407 /*
408 * Code to process quotactl commands.
409 */
410
411 /*
412 * Q_QUOTAON - set up a quota file for a particular file system.
413 */
414 int
415 hfs_quotaon(p, mp, type, fname, segflg)
416 struct proc *p;
417 struct mount *mp;
418 register int type;
419 caddr_t fname;
420 enum uio_seg segflg;
421 {
422 struct hfsmount *hfsmp = VFSTOHFS(mp);
423 struct vnode *vp, **vpp;
424 struct vnode *nextvp;
425 struct dquot *dq;
426 int error;
427 struct nameidata nd;
428
429 vpp = &hfsmp->hfs_qfiles[type].qf_vp;
430 NDINIT(&nd, LOOKUP, FOLLOW, segflg, fname, p);
431 if (error = vn_open(&nd, FREAD|FWRITE, 0))
432 return (error);
433 vp = nd.ni_vp;
434 VOP_UNLOCK(vp, 0, p);
435 if (vp->v_type != VREG) {
436 (void) vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
437 return (EACCES);
438 }
439 if (*vpp != vp)
440 hfs_quotaoff(p, mp, type);
441 hfsmp->hfs_qfiles[type].qf_qflags |= QTF_OPENING;
442 mp->mnt_flag |= MNT_QUOTA;
443 vp->v_flag |= VNOFLUSH;
444 *vpp = vp;
445 /*
446 * Save the credential of the process that turned on quotas.
447 */
448 crhold(p->p_ucred);
449 hfsmp->hfs_qfiles[type].qf_cred = p->p_ucred;
450 /* Finish initializing the quota file */
451 if (error = dqfileopen(&hfsmp->hfs_qfiles[type], type))
452 goto exit;
453 /*
454 * Search vnodes associated with this mount point,
455 * adding references to quota file being opened.
456 * NB: only need to add dquot's for cnodes being modified.
457 */
458 again:
459 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
460 nextvp = vp->v_mntvnodes.le_next;
461 if (vp->v_writecount == 0)
462 continue;
463 if (vget(vp, LK_EXCLUSIVE, p))
464 goto again;
465 if (error = hfs_getinoquota(VTOC(vp))) {
466 vput(vp);
467 break;
468 }
469 vput(vp);
470 if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
471 goto again;
472 }
473 exit:
474 hfsmp->hfs_qfiles[type].qf_qflags &= ~QTF_OPENING;
475 if (error)
476 hfs_quotaoff(p, mp, type);
477 return (error);
478 }
479
480 /*
481 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
482 */
483 int
484 hfs_quotaoff(p, mp, type)
485 struct proc *p;
486 struct mount *mp;
487 register int type;
488 {
489 struct vnode *vp;
490 struct vnode *qvp, *nextvp;
491 struct hfsmount *hfsmp = VFSTOHFS(mp);
492 struct dquot *dq;
493 struct cnode *cp;
494 int error;
495 struct ucred *cred;
496
497 if ((qvp = hfsmp->hfs_qfiles[type].qf_vp) == NULLVP)
498 return (0);
499 hfsmp->hfs_qfiles[type].qf_qflags |= QTF_CLOSING;
500
501 /*
502 * Sync out any orpaned dirty dquot entries.
503 */
504 dqsync_orphans(&hfsmp->hfs_qfiles[type]);
505
506 /*
507 * Search vnodes associated with this mount point,
508 * deleting any references to quota file being closed.
509 */
510 again:
511 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
512 nextvp = vp->v_mntvnodes.le_next;
513 if (vget(vp, LK_EXCLUSIVE, p))
514 goto again;
515 cp = VTOC(vp);
516 dq = cp->c_dquot[type];
517 cp->c_dquot[type] = NODQUOT;
518 dqrele(vp, dq);
519 vput(vp);
520 if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
521 goto again;
522 }
523 dqflush(qvp);
524 /* Finish tearing down the quota file */
525 dqfileclose(&hfsmp->hfs_qfiles[type], type);
526 qvp->v_flag &= ~VNOFLUSH;
527 error = vn_close(qvp, FREAD|FWRITE, p->p_ucred, p);
528 hfsmp->hfs_qfiles[type].qf_vp = NULLVP;
529 cred = hfsmp->hfs_qfiles[type].qf_cred;
530 if (cred != NOCRED) {
531 hfsmp->hfs_qfiles[type].qf_cred = NOCRED;
532 crfree(cred);
533 }
534 hfsmp->hfs_qfiles[type].qf_qflags &= ~QTF_CLOSING;
535 for (type = 0; type < MAXQUOTAS; type++)
536 if (hfsmp->hfs_qfiles[type].qf_vp != NULLVP)
537 break;
538 if (type == MAXQUOTAS)
539 mp->mnt_flag &= ~MNT_QUOTA;
540 return (error);
541 }
542
543 /*
544 * Q_GETQUOTA - return current values in a dqblk structure.
545 */
546 int
547 hfs_getquota(mp, id, type, addr)
548 struct mount *mp;
549 u_long id;
550 int type;
551 caddr_t addr;
552 {
553 struct dquot *dq;
554 int error;
555
556 if (error = dqget(NULLVP, id, &VFSTOHFS(mp)->hfs_qfiles[type], type, &dq))
557 return (error);
558 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
559 dqrele(NULLVP, dq);
560 return (error);
561 }
562
563 /*
564 * Q_SETQUOTA - assign an entire dqblk structure.
565 */
566 int
567 hfs_setquota(mp, id, type, addr)
568 struct mount *mp;
569 u_long id;
570 int type;
571 caddr_t addr;
572 {
573 register struct dquot *dq;
574 struct dquot *ndq;
575 struct hfsmount *hfsmp = VFSTOHFS(mp);
576 struct dqblk newlim;
577 int error;
578
579 if (error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk)))
580 return (error);
581 if (error = dqget(NULLVP, id, &hfsmp->hfs_qfiles[type], type, &ndq))
582 return (error);
583 dq = ndq;
584 while (dq->dq_flags & DQ_LOCK) {
585 dq->dq_flags |= DQ_WANT;
586 sleep((caddr_t)dq, PINOD+1);
587 }
588 /*
589 * Copy all but the current values.
590 * Reset time limit if previously had no soft limit or were
591 * under it, but now have a soft limit and are over it.
592 */
593 newlim.dqb_curbytes = dq->dq_curbytes;
594 newlim.dqb_curinodes = dq->dq_curinodes;
595 if (dq->dq_id != 0) {
596 newlim.dqb_btime = dq->dq_btime;
597 newlim.dqb_itime = dq->dq_itime;
598 }
599 if (newlim.dqb_bsoftlimit &&
600 dq->dq_curbytes >= newlim.dqb_bsoftlimit &&
601 (dq->dq_bsoftlimit == 0 || dq->dq_curbytes < dq->dq_bsoftlimit))
602 newlim.dqb_btime = time.tv_sec + hfsmp->hfs_qfiles[type].qf_btime;
603 if (newlim.dqb_isoftlimit &&
604 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
605 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
606 newlim.dqb_itime = time.tv_sec + hfsmp->hfs_qfiles[type].qf_itime;
607 dq->dq_dqb = newlim;
608 if (dq->dq_curbytes < dq->dq_bsoftlimit)
609 dq->dq_flags &= ~DQ_BLKS;
610 if (dq->dq_curinodes < dq->dq_isoftlimit)
611 dq->dq_flags &= ~DQ_INODS;
612 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
613 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
614 dq->dq_flags |= DQ_FAKE;
615 else
616 dq->dq_flags &= ~DQ_FAKE;
617 dq->dq_flags |= DQ_MOD;
618 dqrele(NULLVP, dq);
619 return (0);
620 }
621
622 /*
623 * Q_SETUSE - set current cnode and byte usage.
624 */
625 int
626 hfs_setuse(mp, id, type, addr)
627 struct mount *mp;
628 u_long id;
629 int type;
630 caddr_t addr;
631 {
632 register struct dquot *dq;
633 struct hfsmount *hfsmp = VFSTOHFS(mp);
634 struct dquot *ndq;
635 struct dqblk usage;
636 int error;
637
638 if (error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk)))
639 return (error);
640 if (error = dqget(NULLVP, id, &hfsmp->hfs_qfiles[type], type, &ndq))
641 return (error);
642 dq = ndq;
643 while (dq->dq_flags & DQ_LOCK) {
644 dq->dq_flags |= DQ_WANT;
645 sleep((caddr_t)dq, PINOD+1);
646 }
647 /*
648 * Reset time limit if have a soft limit and were
649 * previously under it, but are now over it.
650 */
651 if (dq->dq_bsoftlimit && dq->dq_curbytes < dq->dq_bsoftlimit &&
652 usage.dqb_curbytes >= dq->dq_bsoftlimit)
653 dq->dq_btime = time.tv_sec + hfsmp->hfs_qfiles[type].qf_btime;
654 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
655 usage.dqb_curinodes >= dq->dq_isoftlimit)
656 dq->dq_itime = time.tv_sec + hfsmp->hfs_qfiles[type].qf_itime;
657 dq->dq_curbytes = usage.dqb_curbytes;
658 dq->dq_curinodes = usage.dqb_curinodes;
659 if (dq->dq_curbytes < dq->dq_bsoftlimit)
660 dq->dq_flags &= ~DQ_BLKS;
661 if (dq->dq_curinodes < dq->dq_isoftlimit)
662 dq->dq_flags &= ~DQ_INODS;
663 dq->dq_flags |= DQ_MOD;
664 dqrele(NULLVP, dq);
665 return (0);
666 }
667
668 /*
669 * Q_SYNC - sync quota files to disk.
670 */
671 int
672 hfs_qsync(mp)
673 struct mount *mp;
674 {
675 struct hfsmount *hfsmp = VFSTOHFS(mp);
676 struct proc *p = current_proc(); /* XXX */
677 struct vnode *vp, *nextvp;
678 struct dquot *dq;
679 int i, error;
680
681 /*
682 * Check if the mount point has any quotas.
683 * If not, simply return.
684 */
685 for (i = 0; i < MAXQUOTAS; i++)
686 if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP)
687 break;
688 if (i == MAXQUOTAS)
689 return (0);
690
691 /*
692 * Sync out any orpaned dirty dquot entries.
693 */
694 for (i = 0; i < MAXQUOTAS; i++)
695 if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP)
696 dqsync_orphans(&hfsmp->hfs_qfiles[i]);
697
698 /*
699 * Search vnodes associated with this mount point,
700 * synchronizing any modified dquot structures.
701 */
702 simple_lock(&mntvnode_slock);
703 again:
704 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
705 if (vp->v_mount != mp)
706 goto again;
707
708 nextvp = vp->v_mntvnodes.le_next;
709 simple_lock(&vp->v_interlock);
710 simple_unlock(&mntvnode_slock);
711
712 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
713 if (error) {
714 simple_lock(&mntvnode_slock);
715 if (error == ENOENT)
716 goto again;
717 continue;
718 }
719
720 // Make sure that this is really an hfs vnode.
721 //
722 if ( vp->v_mount != mp
723 || vp->v_type == VNON
724 || vp->v_tag != VT_HFS
725 || VTOC(vp) == NULL) {
726
727 vput(vp);
728 simple_lock(&mntvnode_slock);
729 goto again;
730 }
731
732 for (i = 0; i < MAXQUOTAS; i++) {
733 dq = VTOC(vp)->c_dquot[i];
734 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
735 dqsync(vp, dq);
736 }
737 vput(vp);
738 simple_lock(&mntvnode_slock);
739 if (vp->v_mntvnodes.le_next != nextvp)
740 goto again;
741 }
742 simple_unlock(&mntvnode_slock);
743 return (0);
744 }
745
746 /*
747 * Q_QUOTASTAT - get quota on/off status
748 */
749 int
750 hfs_quotastat(mp, type, addr)
751 struct mount *mp;
752 register int type;
753 caddr_t addr;
754 {
755 struct hfsmount *hfsmp = VFSTOHFS(mp);
756 int error = 0;
757 int qstat;
758
759 if ((mp->mnt_flag & MNT_QUOTA) && (hfsmp->hfs_qfiles[type].qf_vp != NULLVP))
760 qstat = 1; /* quotas are on for this type */
761 else
762 qstat = 0; /* quotas are off for this type */
763
764 error = copyout ((caddr_t)&qstat, addr, sizeof(qstat));
765 return (error);
766 }
767