1 /* 2 * Copyright (c) 1982, 1986, 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Robert Elz at The University of Melbourne. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95 37 * $FreeBSD: src/sys/ufs/ufs/ufs_quota.c,v 1.27.2.3 2002/01/15 10:33:32 phk Exp $ 38 * $DragonFly: src/sys/vfs/ufs/ufs_quota.c,v 1.12 2004/03/01 06:33:23 dillon Exp $ 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/fcntl.h> 46 #include <sys/proc.h> 47 #include <sys/namei.h> 48 #include <sys/vnode.h> 49 #include <sys/mount.h> 50 #include <vm/vm_zone.h> 51 52 #include "quota.h" 53 #include "inode.h" 54 #include "ufsmount.h" 55 56 static MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries"); 57 58 /* 59 * Quota name to error message mapping. 60 */ 61 static char *quotatypes[] = INITQFNAMES; 62 63 static int chkdqchg (struct inode *, long, struct ucred *, int); 64 static int chkiqchg (struct inode *, long, struct ucred *, int); 65 static int dqget (struct vnode *, 66 u_long, struct ufsmount *, int, struct dquot **); 67 static int dqsync (struct vnode *, struct dquot *); 68 static void dqflush (struct vnode *); 69 70 #ifdef DIAGNOSTIC 71 static void dqref (struct dquot *); 72 static void chkdquot (struct inode *); 73 #endif 74 75 /* 76 * Set up the quotas for an inode. 77 * 78 * This routine completely defines the semantics of quotas. 79 * If other criterion want to be used to establish quotas, the 80 * MAXQUOTAS value in quotas.h should be increased, and the 81 * additional dquots set up here. 82 */ 83 int 84 getinoquota(ip) 85 struct inode *ip; 86 { 87 struct ufsmount *ump; 88 struct vnode *vp = ITOV(ip); 89 int error; 90 91 ump = VFSTOUFS(vp->v_mount); 92 /* 93 * Set up the user quota based on file uid. 94 * EINVAL means that quotas are not enabled. 95 */ 96 if (ip->i_dquot[USRQUOTA] == NODQUOT && 97 (error = 98 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) && 99 error != EINVAL) 100 return (error); 101 /* 102 * Set up the group quota based on file gid. 103 * EINVAL means that quotas are not enabled. 104 */ 105 if (ip->i_dquot[GRPQUOTA] == NODQUOT && 106 (error = 107 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) && 108 error != EINVAL) 109 return (error); 110 return (0); 111 } 112 113 /* 114 * Update disk usage, and take corrective action. 115 */ 116 int 117 chkdq(ip, change, cred, flags) 118 struct inode *ip; 119 long change; 120 struct ucred *cred; 121 int flags; 122 { 123 struct dquot *dq; 124 int i; 125 int ncurblocks, error; 126 127 #ifdef DIAGNOSTIC 128 if ((flags & CHOWN) == 0) 129 chkdquot(ip); 130 #endif 131 if (change == 0) 132 return (0); 133 if (change < 0) { 134 for (i = 0; i < MAXQUOTAS; i++) { 135 if ((dq = ip->i_dquot[i]) == NODQUOT) 136 continue; 137 while (dq->dq_flags & DQ_LOCK) { 138 dq->dq_flags |= DQ_WANT; 139 (void) tsleep((caddr_t)dq, 0, "chkdq1", 0); 140 } 141 ncurblocks = dq->dq_curblocks + change; 142 if (ncurblocks >= 0) 143 dq->dq_curblocks = ncurblocks; 144 else 145 dq->dq_curblocks = 0; 146 dq->dq_flags &= ~DQ_BLKS; 147 dq->dq_flags |= DQ_MOD; 148 } 149 return (0); 150 } 151 if ((flags & FORCE) == 0 && cred->cr_uid != 0) { 152 for (i = 0; i < MAXQUOTAS; i++) { 153 if ((dq = ip->i_dquot[i]) == NODQUOT) 154 continue; 155 error = chkdqchg(ip, change, cred, i); 156 if (error) 157 return (error); 158 } 159 } 160 for (i = 0; i < MAXQUOTAS; i++) { 161 if ((dq = ip->i_dquot[i]) == NODQUOT) 162 continue; 163 while (dq->dq_flags & DQ_LOCK) { 164 dq->dq_flags |= DQ_WANT; 165 (void) tsleep((caddr_t)dq, 0, "chkdq2", 0); 166 } 167 /* Reset timer when crossing soft limit */ 168 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit && 169 dq->dq_curblocks < dq->dq_bsoftlimit) 170 dq->dq_btime = time_second + 171 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i]; 172 dq->dq_curblocks += change; 173 dq->dq_flags |= DQ_MOD; 174 } 175 return (0); 176 } 177 178 /* 179 * Check for a valid change to a users allocation. 180 * Issue an error message if appropriate. 181 */ 182 static int 183 chkdqchg(ip, change, cred, type) 184 struct inode *ip; 185 long change; 186 struct ucred *cred; 187 int type; 188 { 189 struct dquot *dq = ip->i_dquot[type]; 190 long ncurblocks = dq->dq_curblocks + change; 191 192 /* 193 * If user would exceed their hard limit, disallow space allocation. 194 */ 195 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) { 196 if ((dq->dq_flags & DQ_BLKS) == 0 && 197 ip->i_uid == cred->cr_uid) { 198 uprintf("\n%s: write failed, %s disk limit reached\n", 199 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 200 quotatypes[type]); 201 dq->dq_flags |= DQ_BLKS; 202 } 203 return (EDQUOT); 204 } 205 /* 206 * If user is over their soft limit for too long, disallow space 207 * allocation. Reset time limit as they cross their soft limit. 208 */ 209 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) { 210 if (dq->dq_curblocks < dq->dq_bsoftlimit) { 211 dq->dq_btime = time_second + 212 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type]; 213 if (ip->i_uid == cred->cr_uid) 214 uprintf("\n%s: warning, %s %s\n", 215 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 216 quotatypes[type], "disk quota exceeded"); 217 return (0); 218 } 219 if (time_second > dq->dq_btime) { 220 if ((dq->dq_flags & DQ_BLKS) == 0 && 221 ip->i_uid == cred->cr_uid) { 222 uprintf("\n%s: write failed, %s %s\n", 223 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 224 quotatypes[type], 225 "disk quota exceeded for too long"); 226 dq->dq_flags |= DQ_BLKS; 227 } 228 return (EDQUOT); 229 } 230 } 231 return (0); 232 } 233 234 /* 235 * Check the inode limit, applying corrective action. 236 */ 237 int 238 chkiq(ip, change, cred, flags) 239 struct inode *ip; 240 long change; 241 struct ucred *cred; 242 int flags; 243 { 244 struct dquot *dq; 245 int i; 246 int ncurinodes, error; 247 248 #ifdef DIAGNOSTIC 249 if ((flags & CHOWN) == 0) 250 chkdquot(ip); 251 #endif 252 if (change == 0) 253 return (0); 254 if (change < 0) { 255 for (i = 0; i < MAXQUOTAS; i++) { 256 if ((dq = ip->i_dquot[i]) == NODQUOT) 257 continue; 258 while (dq->dq_flags & DQ_LOCK) { 259 dq->dq_flags |= DQ_WANT; 260 (void) tsleep((caddr_t)dq, 0, "chkiq1", 0); 261 } 262 ncurinodes = dq->dq_curinodes + change; 263 if (ncurinodes >= 0) 264 dq->dq_curinodes = ncurinodes; 265 else 266 dq->dq_curinodes = 0; 267 dq->dq_flags &= ~DQ_INODS; 268 dq->dq_flags |= DQ_MOD; 269 } 270 return (0); 271 } 272 if ((flags & FORCE) == 0 && cred->cr_uid != 0) { 273 for (i = 0; i < MAXQUOTAS; i++) { 274 if ((dq = ip->i_dquot[i]) == NODQUOT) 275 continue; 276 error = chkiqchg(ip, change, cred, i); 277 if (error) 278 return (error); 279 } 280 } 281 for (i = 0; i < MAXQUOTAS; i++) { 282 if ((dq = ip->i_dquot[i]) == NODQUOT) 283 continue; 284 while (dq->dq_flags & DQ_LOCK) { 285 dq->dq_flags |= DQ_WANT; 286 (void) tsleep((caddr_t)dq, 0, "chkiq2", 0); 287 } 288 /* Reset timer when crossing soft limit */ 289 if (dq->dq_curinodes + change >= dq->dq_isoftlimit && 290 dq->dq_curinodes < dq->dq_isoftlimit) 291 dq->dq_itime = time_second + 292 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i]; 293 dq->dq_curinodes += change; 294 dq->dq_flags |= DQ_MOD; 295 } 296 return (0); 297 } 298 299 /* 300 * Check for a valid change to a users allocation. 301 * Issue an error message if appropriate. 302 */ 303 static int 304 chkiqchg(ip, change, cred, type) 305 struct inode *ip; 306 long change; 307 struct ucred *cred; 308 int type; 309 { 310 struct dquot *dq = ip->i_dquot[type]; 311 long ncurinodes = dq->dq_curinodes + change; 312 313 /* 314 * If user would exceed their hard limit, disallow inode allocation. 315 */ 316 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) { 317 if ((dq->dq_flags & DQ_INODS) == 0 && 318 ip->i_uid == cred->cr_uid) { 319 uprintf("\n%s: write failed, %s inode limit reached\n", 320 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 321 quotatypes[type]); 322 dq->dq_flags |= DQ_INODS; 323 } 324 return (EDQUOT); 325 } 326 /* 327 * If user is over their soft limit for too long, disallow inode 328 * allocation. Reset time limit as they cross their soft limit. 329 */ 330 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) { 331 if (dq->dq_curinodes < dq->dq_isoftlimit) { 332 dq->dq_itime = time_second + 333 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type]; 334 if (ip->i_uid == cred->cr_uid) 335 uprintf("\n%s: warning, %s %s\n", 336 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 337 quotatypes[type], "inode quota exceeded"); 338 return (0); 339 } 340 if (time_second > dq->dq_itime) { 341 if ((dq->dq_flags & DQ_INODS) == 0 && 342 ip->i_uid == cred->cr_uid) { 343 uprintf("\n%s: write failed, %s %s\n", 344 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 345 quotatypes[type], 346 "inode quota exceeded for too long"); 347 dq->dq_flags |= DQ_INODS; 348 } 349 return (EDQUOT); 350 } 351 } 352 return (0); 353 } 354 355 #ifdef DIAGNOSTIC 356 /* 357 * On filesystems with quotas enabled, it is an error for a file to change 358 * size and not to have a dquot structure associated with it. 359 */ 360 static void 361 chkdquot(ip) 362 struct inode *ip; 363 { 364 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount); 365 int i; 366 367 for (i = 0; i < MAXQUOTAS; i++) { 368 if (ump->um_quotas[i] == NULLVP || 369 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING))) 370 continue; 371 if (ip->i_dquot[i] == NODQUOT) { 372 vprint("chkdquot: missing dquot", ITOV(ip)); 373 panic("chkdquot: missing dquot"); 374 } 375 } 376 } 377 #endif 378 379 /* 380 * Code to process quotactl commands. 381 */ 382 383 struct scaninfo { 384 thread_t td; 385 int rescan; 386 int type; 387 }; 388 389 /* 390 * Q_QUOTAON - set up a quota file for a particular file system. 391 */ 392 static int quotaon_scan(struct mount *mp, struct vnode *vp, 393 lwkt_tokref_t vlock, void *data); 394 395 int 396 quotaon(td, mp, type, fname) 397 struct thread *td; 398 struct mount *mp; 399 int type; 400 caddr_t fname; 401 { 402 struct ufsmount *ump = VFSTOUFS(mp); 403 struct vnode *vp, **vpp; 404 struct dquot *dq; 405 int error; 406 struct nameidata nd; 407 struct ucred *cred; 408 struct scaninfo scaninfo; 409 410 KKASSERT(td->td_proc); 411 cred = td->td_proc->p_ucred; 412 413 vpp = &ump->um_quotas[type]; 414 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, fname, td); 415 error = vn_open(&nd, FREAD|FWRITE, 0); 416 if (error) 417 return (error); 418 NDFREE(&nd, NDF_ONLY_PNBUF); 419 vp = nd.ni_vp; 420 VOP_UNLOCK(vp, NULL, 0, td); 421 if (vp->v_type != VREG) { 422 (void) vn_close(vp, FREAD|FWRITE, td); 423 return (EACCES); 424 } 425 if (*vpp != vp) 426 quotaoff(td, mp, type); 427 ump->um_qflags[type] |= QTF_OPENING; 428 mp->mnt_flag |= MNT_QUOTA; 429 vp->v_flag |= VSYSTEM; 430 *vpp = vp; 431 /* 432 * Save the credential of the process that turned on quotas. 433 * Set up the time limits for this quota. 434 */ 435 ump->um_cred[type] = crhold(cred); 436 ump->um_btime[type] = MAX_DQ_TIME; 437 ump->um_itime[type] = MAX_IQ_TIME; 438 if (dqget(NULLVP, 0, ump, type, &dq) == 0) { 439 if (dq->dq_btime > 0) 440 ump->um_btime[type] = dq->dq_btime; 441 if (dq->dq_itime > 0) 442 ump->um_itime[type] = dq->dq_itime; 443 dqrele(NULLVP, dq); 444 } 445 /* 446 * Search vnodes associated with this mount point, 447 * adding references to quota file being opened. 448 * NB: only need to add dquot's for inodes being modified. 449 */ 450 scaninfo.rescan = 1; 451 scaninfo.td = td; 452 while (scaninfo.rescan) { 453 scaninfo.rescan = 0; 454 error = vmntvnodescan(mp, NULL, quotaon_scan, &scaninfo); 455 if (error) 456 break; 457 } 458 ump->um_qflags[type] &= ~QTF_OPENING; 459 if (error) 460 quotaoff(td, mp, type); 461 return (error); 462 } 463 464 static 465 int 466 quotaon_scan(struct mount *mp, struct vnode *vp, 467 lwkt_tokref_t vlock, void *data) 468 { 469 int error; 470 struct scaninfo *info = data; 471 472 if (vp->v_type == VNON || vp->v_writecount == 0) { 473 lwkt_reltoken(vlock); 474 return(0); 475 } 476 if (vget(vp, vlock, LK_INTERLOCK|LK_EXCLUSIVE, info->td)) { 477 info->rescan = 1; 478 return(0); 479 } 480 error = getinoquota(VTOI(vp)); 481 vput(vp); 482 return(error); 483 } 484 485 /* 486 * Q_QUOTAOFF - turn off disk quotas for a filesystem. 487 */ 488 489 static int quotaoff_scan(struct mount *mp, struct vnode *vp, 490 lwkt_tokref_t vlock, void *data); 491 492 int 493 quotaoff(struct thread *td, struct mount *mp, int type) 494 { 495 struct vnode *qvp; 496 struct ufsmount *ump = VFSTOUFS(mp); 497 struct ucred *cred; 498 int error; 499 struct scaninfo scaninfo; 500 501 KKASSERT(td->td_proc); 502 cred = td->td_proc->p_ucred; 503 504 if ((qvp = ump->um_quotas[type]) == NULLVP) 505 return (0); 506 ump->um_qflags[type] |= QTF_CLOSING; 507 508 /* 509 * Search vnodes associated with this mount point, 510 * deleting any references to quota file being closed. 511 */ 512 scaninfo.rescan = 1; 513 scaninfo.td = td; 514 scaninfo.type = type; 515 while (scaninfo.rescan) { 516 scaninfo.rescan = 0; 517 vmntvnodescan(mp, NULL, quotaoff_scan, &scaninfo); 518 } 519 dqflush(qvp); 520 qvp->v_flag &= ~VSYSTEM; 521 error = vn_close(qvp, FREAD|FWRITE, td); 522 ump->um_quotas[type] = NULLVP; 523 crfree(ump->um_cred[type]); 524 ump->um_cred[type] = NOCRED; 525 ump->um_qflags[type] &= ~QTF_CLOSING; 526 for (type = 0; type < MAXQUOTAS; type++) { 527 if (ump->um_quotas[type] != NULLVP) 528 break; 529 } 530 if (type == MAXQUOTAS) 531 mp->mnt_flag &= ~MNT_QUOTA; 532 return (error); 533 } 534 535 static 536 int 537 quotaoff_scan(struct mount *mp, struct vnode *vp, 538 lwkt_tokref_t vlock, void *data) 539 { 540 struct scaninfo *info = data; 541 struct dquot *dq; 542 struct inode *ip; 543 544 if (vp->v_type == VNON) { 545 lwkt_reltoken(vlock); 546 return(0); 547 } 548 if (vget(vp, vlock, LK_INTERLOCK|LK_EXCLUSIVE, info->td)) { 549 info->rescan = 1; 550 return(0); 551 } 552 ip = VTOI(vp); 553 dq = ip->i_dquot[info->type]; 554 ip->i_dquot[info->type] = NODQUOT; 555 dqrele(vp, dq); 556 vput(vp); 557 return(0); 558 } 559 560 /* 561 * Q_GETQUOTA - return current values in a dqblk structure. 562 */ 563 int 564 getquota(mp, id, type, addr) 565 struct mount *mp; 566 u_long id; 567 int type; 568 caddr_t addr; 569 { 570 struct dquot *dq; 571 int error; 572 573 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq); 574 if (error) 575 return (error); 576 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk)); 577 dqrele(NULLVP, dq); 578 return (error); 579 } 580 581 /* 582 * Q_SETQUOTA - assign an entire dqblk structure. 583 */ 584 int 585 setquota(mp, id, type, addr) 586 struct mount *mp; 587 u_long id; 588 int type; 589 caddr_t addr; 590 { 591 struct dquot *dq; 592 struct dquot *ndq; 593 struct ufsmount *ump = VFSTOUFS(mp); 594 struct dqblk newlim; 595 int error; 596 597 error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk)); 598 if (error) 599 return (error); 600 error = dqget(NULLVP, id, ump, type, &ndq); 601 if (error) 602 return (error); 603 dq = ndq; 604 while (dq->dq_flags & DQ_LOCK) { 605 dq->dq_flags |= DQ_WANT; 606 (void) tsleep((caddr_t)dq, 0, "setqta", 0); 607 } 608 /* 609 * Copy all but the current values. 610 * Reset time limit if previously had no soft limit or were 611 * under it, but now have a soft limit and are over it. 612 */ 613 newlim.dqb_curblocks = dq->dq_curblocks; 614 newlim.dqb_curinodes = dq->dq_curinodes; 615 if (dq->dq_id != 0) { 616 newlim.dqb_btime = dq->dq_btime; 617 newlim.dqb_itime = dq->dq_itime; 618 } 619 if (newlim.dqb_bsoftlimit && 620 dq->dq_curblocks >= newlim.dqb_bsoftlimit && 621 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit)) 622 newlim.dqb_btime = time_second + ump->um_btime[type]; 623 if (newlim.dqb_isoftlimit && 624 dq->dq_curinodes >= newlim.dqb_isoftlimit && 625 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit)) 626 newlim.dqb_itime = time_second + ump->um_itime[type]; 627 dq->dq_dqb = newlim; 628 if (dq->dq_curblocks < dq->dq_bsoftlimit) 629 dq->dq_flags &= ~DQ_BLKS; 630 if (dq->dq_curinodes < dq->dq_isoftlimit) 631 dq->dq_flags &= ~DQ_INODS; 632 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 && 633 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0) 634 dq->dq_flags |= DQ_FAKE; 635 else 636 dq->dq_flags &= ~DQ_FAKE; 637 dq->dq_flags |= DQ_MOD; 638 dqrele(NULLVP, dq); 639 return (0); 640 } 641 642 /* 643 * Q_SETUSE - set current inode and block usage. 644 */ 645 int 646 setuse(mp, id, type, addr) 647 struct mount *mp; 648 u_long id; 649 int type; 650 caddr_t addr; 651 { 652 struct dquot *dq; 653 struct ufsmount *ump = VFSTOUFS(mp); 654 struct dquot *ndq; 655 struct dqblk usage; 656 int error; 657 658 error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk)); 659 if (error) 660 return (error); 661 error = dqget(NULLVP, id, ump, type, &ndq); 662 if (error) 663 return (error); 664 dq = ndq; 665 while (dq->dq_flags & DQ_LOCK) { 666 dq->dq_flags |= DQ_WANT; 667 (void) tsleep((caddr_t)dq, 0, "setuse", 0); 668 } 669 /* 670 * Reset time limit if have a soft limit and were 671 * previously under it, but are now over it. 672 */ 673 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit && 674 usage.dqb_curblocks >= dq->dq_bsoftlimit) 675 dq->dq_btime = time_second + ump->um_btime[type]; 676 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit && 677 usage.dqb_curinodes >= dq->dq_isoftlimit) 678 dq->dq_itime = time_second + ump->um_itime[type]; 679 dq->dq_curblocks = usage.dqb_curblocks; 680 dq->dq_curinodes = usage.dqb_curinodes; 681 if (dq->dq_curblocks < dq->dq_bsoftlimit) 682 dq->dq_flags &= ~DQ_BLKS; 683 if (dq->dq_curinodes < dq->dq_isoftlimit) 684 dq->dq_flags &= ~DQ_INODS; 685 dq->dq_flags |= DQ_MOD; 686 dqrele(NULLVP, dq); 687 return (0); 688 } 689 690 /* 691 * Q_SYNC - sync quota files to disk. 692 */ 693 694 static int qsync_scan(struct mount *mp, struct vnode *vp, 695 lwkt_tokref_t vlock, void *data); 696 int 697 qsync(struct mount *mp) 698 { 699 struct ufsmount *ump = VFSTOUFS(mp); 700 struct thread *td = curthread; /* XXX */ 701 struct scaninfo scaninfo; 702 int i; 703 704 /* 705 * Check if the mount point has any quotas. 706 * If not, simply return. 707 */ 708 for (i = 0; i < MAXQUOTAS; i++) 709 if (ump->um_quotas[i] != NULLVP) 710 break; 711 if (i == MAXQUOTAS) 712 return (0); 713 /* 714 * Search vnodes associated with this mount point, 715 * synchronizing any modified dquot structures. 716 */ 717 scaninfo.rescan = 1; 718 scaninfo.td = td; 719 while (scaninfo.rescan) { 720 scaninfo.rescan = 0; 721 vmntvnodescan(mp, NULL, qsync_scan, &scaninfo); 722 } 723 return (0); 724 } 725 726 static 727 int 728 qsync_scan(struct mount *mp, struct vnode *vp, 729 lwkt_tokref_t vlock, void *data) 730 { 731 struct scaninfo *info = data; 732 struct dquot *dq; 733 int error; 734 int i; 735 736 if (vp->v_type == VNON) { 737 lwkt_reltoken(vlock); 738 return(0); 739 } 740 error = vget(vp, vlock, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, info->td); 741 if (error) { 742 if (error == ENOENT) 743 info->rescan = 1; 744 return(0); 745 } 746 for (i = 0; i < MAXQUOTAS; i++) { 747 dq = VTOI(vp)->i_dquot[i]; 748 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD)) 749 dqsync(vp, dq); 750 } 751 vput(vp); 752 return(0); 753 } 754 755 /* 756 * Code pertaining to management of the in-core dquot data structures. 757 */ 758 #define DQHASH(dqvp, id) \ 759 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash]) 760 static LIST_HEAD(dqhash, dquot) *dqhashtbl; 761 static u_long dqhash; 762 763 /* 764 * Dquot free list. 765 */ 766 #define DQUOTINC 5 /* minimum free dquots desired */ 767 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist; 768 static long numdquot, desireddquot = DQUOTINC; 769 770 /* 771 * Initialize the quota system. 772 */ 773 void 774 dqinit() 775 { 776 777 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash); 778 TAILQ_INIT(&dqfreelist); 779 } 780 781 /* 782 * Obtain a dquot structure for the specified identifier and quota file 783 * reading the information from the file if necessary. 784 */ 785 static int 786 dqget(vp, id, ump, type, dqp) 787 struct vnode *vp; 788 u_long id; 789 struct ufsmount *ump; 790 int type; 791 struct dquot **dqp; 792 { 793 struct thread *td = curthread; /* XXX */ 794 struct dquot *dq; 795 struct dqhash *dqh; 796 struct vnode *dqvp; 797 struct iovec aiov; 798 struct uio auio; 799 int error; 800 801 dqvp = ump->um_quotas[type]; 802 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) { 803 *dqp = NODQUOT; 804 return (EINVAL); 805 } 806 /* 807 * Check the cache first. 808 */ 809 dqh = DQHASH(dqvp, id); 810 for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) { 811 if (dq->dq_id != id || 812 dq->dq_ump->um_quotas[dq->dq_type] != dqvp) 813 continue; 814 /* 815 * Cache hit with no references. Take 816 * the structure off the free list. 817 */ 818 if (dq->dq_cnt == 0) 819 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist); 820 DQREF(dq); 821 *dqp = dq; 822 return (0); 823 } 824 /* 825 * Not in cache, allocate a new one. 826 */ 827 if (dqfreelist.tqh_first == NODQUOT && 828 numdquot < MAXQUOTAS * desiredvnodes) 829 desireddquot += DQUOTINC; 830 if (numdquot < desireddquot) { 831 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK); 832 bzero((char *)dq, sizeof *dq); 833 numdquot++; 834 } else { 835 if ((dq = dqfreelist.tqh_first) == NULL) { 836 tablefull("dquot"); 837 *dqp = NODQUOT; 838 return (EUSERS); 839 } 840 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD)) 841 panic("dqget: free dquot isn't"); 842 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist); 843 if (dq->dq_ump != NULL) 844 LIST_REMOVE(dq, dq_hash); 845 } 846 /* 847 * Initialize the contents of the dquot structure. 848 */ 849 if (vp != dqvp) 850 vn_lock(dqvp, NULL, LK_EXCLUSIVE | LK_RETRY, td); 851 LIST_INSERT_HEAD(dqh, dq, dq_hash); 852 DQREF(dq); 853 dq->dq_flags = DQ_LOCK; 854 dq->dq_id = id; 855 dq->dq_ump = ump; 856 dq->dq_type = type; 857 auio.uio_iov = &aiov; 858 auio.uio_iovcnt = 1; 859 aiov.iov_base = (caddr_t)&dq->dq_dqb; 860 aiov.iov_len = sizeof (struct dqblk); 861 auio.uio_resid = sizeof (struct dqblk); 862 auio.uio_offset = (off_t)(id * sizeof (struct dqblk)); 863 auio.uio_segflg = UIO_SYSSPACE; 864 auio.uio_rw = UIO_READ; 865 auio.uio_td = NULL; 866 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]); 867 if (auio.uio_resid == sizeof(struct dqblk) && error == 0) 868 bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk)); 869 if (vp != dqvp) 870 VOP_UNLOCK(dqvp, NULL, 0, td); 871 if (dq->dq_flags & DQ_WANT) 872 wakeup((caddr_t)dq); 873 dq->dq_flags = 0; 874 /* 875 * I/O error in reading quota file, release 876 * quota structure and reflect problem to caller. 877 */ 878 if (error) { 879 LIST_REMOVE(dq, dq_hash); 880 dqrele(vp, dq); 881 *dqp = NODQUOT; 882 return (error); 883 } 884 /* 885 * Check for no limit to enforce. 886 * Initialize time values if necessary. 887 */ 888 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 && 889 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0) 890 dq->dq_flags |= DQ_FAKE; 891 if (dq->dq_id != 0) { 892 if (dq->dq_btime == 0) 893 dq->dq_btime = time_second + ump->um_btime[type]; 894 if (dq->dq_itime == 0) 895 dq->dq_itime = time_second + ump->um_itime[type]; 896 } 897 *dqp = dq; 898 return (0); 899 } 900 901 #ifdef DIAGNOSTIC 902 /* 903 * Obtain a reference to a dquot. 904 */ 905 static void 906 dqref(dq) 907 struct dquot *dq; 908 { 909 910 dq->dq_cnt++; 911 } 912 #endif 913 914 /* 915 * Release a reference to a dquot. 916 */ 917 void 918 dqrele(vp, dq) 919 struct vnode *vp; 920 struct dquot *dq; 921 { 922 923 if (dq == NODQUOT) 924 return; 925 if (dq->dq_cnt > 1) { 926 dq->dq_cnt--; 927 return; 928 } 929 if (dq->dq_flags & DQ_MOD) 930 (void) dqsync(vp, dq); 931 if (--dq->dq_cnt > 0) 932 return; 933 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist); 934 } 935 936 /* 937 * Update the disk quota in the quota file. 938 */ 939 static int 940 dqsync(struct vnode *vp, struct dquot *dq) 941 { 942 struct thread *td = curthread; /* XXX */ 943 struct vnode *dqvp; 944 struct iovec aiov; 945 struct uio auio; 946 int error; 947 948 if (dq == NODQUOT) 949 panic("dqsync: dquot"); 950 if ((dq->dq_flags & DQ_MOD) == 0) 951 return (0); 952 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP) 953 panic("dqsync: file"); 954 if (vp != dqvp) 955 vn_lock(dqvp, NULL, LK_EXCLUSIVE | LK_RETRY, td); 956 while (dq->dq_flags & DQ_LOCK) { 957 dq->dq_flags |= DQ_WANT; 958 (void) tsleep((caddr_t)dq, 0, "dqsync", 0); 959 if ((dq->dq_flags & DQ_MOD) == 0) { 960 if (vp != dqvp) 961 VOP_UNLOCK(dqvp, NULL, 0, td); 962 return (0); 963 } 964 } 965 dq->dq_flags |= DQ_LOCK; 966 auio.uio_iov = &aiov; 967 auio.uio_iovcnt = 1; 968 aiov.iov_base = (caddr_t)&dq->dq_dqb; 969 aiov.iov_len = sizeof (struct dqblk); 970 auio.uio_resid = sizeof (struct dqblk); 971 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk)); 972 auio.uio_segflg = UIO_SYSSPACE; 973 auio.uio_rw = UIO_WRITE; 974 auio.uio_td = NULL; 975 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]); 976 if (auio.uio_resid && error == 0) 977 error = EIO; 978 if (dq->dq_flags & DQ_WANT) 979 wakeup((caddr_t)dq); 980 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT); 981 if (vp != dqvp) 982 VOP_UNLOCK(dqvp, NULL, 0, td); 983 return (error); 984 } 985 986 /* 987 * Flush all entries from the cache for a particular vnode. 988 */ 989 static void 990 dqflush(vp) 991 struct vnode *vp; 992 { 993 struct dquot *dq, *nextdq; 994 struct dqhash *dqh; 995 996 /* 997 * Move all dquot's that used to refer to this quota 998 * file off their hash chains (they will eventually 999 * fall off the head of the free list and be re-used). 1000 */ 1001 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) { 1002 for (dq = dqh->lh_first; dq; dq = nextdq) { 1003 nextdq = dq->dq_hash.le_next; 1004 if (dq->dq_ump->um_quotas[dq->dq_type] != vp) 1005 continue; 1006 if (dq->dq_cnt) 1007 panic("dqflush: stray dquot"); 1008 LIST_REMOVE(dq, dq_hash); 1009 dq->dq_ump = (struct ufsmount *)0; 1010 } 1011 } 1012 } 1013