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