ufs_quota.c revision 217357
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 * 4. 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 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_quota.c 217357 2011-01-13 16:29:27Z pluknet $"); 37 38#include "opt_ffs.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/endian.h> 43#include <sys/fcntl.h> 44#include <sys/kernel.h> 45#include <sys/lock.h> 46#include <sys/malloc.h> 47#include <sys/mount.h> 48#include <sys/mutex.h> 49#include <sys/namei.h> 50#include <sys/priv.h> 51#include <sys/proc.h> 52#include <sys/socket.h> 53#include <sys/stat.h> 54#include <sys/sysctl.h> 55#include <sys/vnode.h> 56 57#include <ufs/ufs/extattr.h> 58#include <ufs/ufs/quota.h> 59#include <ufs/ufs/inode.h> 60#include <ufs/ufs/ufsmount.h> 61#include <ufs/ufs/ufs_extern.h> 62 63CTASSERT(sizeof(struct dqblk64) == sizeof(struct dqhdr64)); 64 65static int unprivileged_get_quota = 0; 66SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW, 67 &unprivileged_get_quota, 0, 68 "Unprivileged processes may retrieve quotas for other uids and gids"); 69 70static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries"); 71 72/* 73 * Quota name to error message mapping. 74 */ 75static char *quotatypes[] = INITQFNAMES; 76 77static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *); 78static int chkiqchg(struct inode *, int, struct ucred *, int, int *); 79static int dqopen(struct vnode *, struct ufsmount *, int); 80static int dqget(struct vnode *, 81 u_long, struct ufsmount *, int, struct dquot **); 82static int dqsync(struct vnode *, struct dquot *); 83static void dqflush(struct vnode *); 84static int quotaoff1(struct thread *td, struct mount *mp, int type); 85static int quotaoff_inchange(struct thread *td, struct mount *mp, int type); 86 87/* conversion functions - from_to() */ 88static void dqb32_dq(const struct dqblk32 *, struct dquot *); 89static void dqb64_dq(const struct dqblk64 *, struct dquot *); 90static void dq_dqb32(const struct dquot *, struct dqblk32 *); 91static void dq_dqb64(const struct dquot *, struct dqblk64 *); 92static void dqb32_dqb64(const struct dqblk32 *, struct dqblk64 *); 93static void dqb64_dqb32(const struct dqblk64 *, struct dqblk32 *); 94 95#ifdef DIAGNOSTIC 96static void dqref(struct dquot *); 97static void chkdquot(struct inode *); 98#endif 99 100/* 101 * Set up the quotas for an inode. 102 * 103 * This routine completely defines the semantics of quotas. 104 * If other criterion want to be used to establish quotas, the 105 * MAXQUOTAS value in quota.h should be increased, and the 106 * additional dquots set up here. 107 */ 108int 109getinoquota(struct inode *ip) 110{ 111 struct ufsmount *ump; 112 struct vnode *vp; 113 int error; 114 115 vp = ITOV(ip); 116 117 /* 118 * Disk quotas must be turned off for system files. Currently 119 * snapshot and quota files. 120 */ 121 if ((vp->v_vflag & VV_SYSTEM) != 0) 122 return (0); 123 /* 124 * XXX: Turn off quotas for files with a negative UID or GID. 125 * This prevents the creation of 100GB+ quota files. 126 */ 127 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0) 128 return (0); 129 ump = VFSTOUFS(vp->v_mount); 130 /* 131 * Set up the user quota based on file uid. 132 * EINVAL means that quotas are not enabled. 133 */ 134 if ((error = 135 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) && 136 error != EINVAL) 137 return (error); 138 /* 139 * Set up the group quota based on file gid. 140 * EINVAL means that quotas are not enabled. 141 */ 142 if ((error = 143 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) && 144 error != EINVAL) 145 return (error); 146 return (0); 147} 148 149/* 150 * Update disk usage, and take corrective action. 151 */ 152int 153chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags) 154{ 155 struct dquot *dq; 156 ufs2_daddr_t ncurblocks; 157 struct vnode *vp = ITOV(ip); 158 int i, error, warn, do_check; 159 160 /* 161 * Disk quotas must be turned off for system files. Currently 162 * snapshot and quota files. 163 */ 164 if ((vp->v_vflag & VV_SYSTEM) != 0) 165 return (0); 166 /* 167 * XXX: Turn off quotas for files with a negative UID or GID. 168 * This prevents the creation of 100GB+ quota files. 169 */ 170 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0) 171 return (0); 172#ifdef DIAGNOSTIC 173 if ((flags & CHOWN) == 0) 174 chkdquot(ip); 175#endif 176 if (change == 0) 177 return (0); 178 if (change < 0) { 179 for (i = 0; i < MAXQUOTAS; i++) { 180 if ((dq = ip->i_dquot[i]) == NODQUOT) 181 continue; 182 DQI_LOCK(dq); 183 DQI_WAIT(dq, PINOD+1, "chkdq1"); 184 ncurblocks = dq->dq_curblocks + change; 185 if (ncurblocks >= 0) 186 dq->dq_curblocks = ncurblocks; 187 else 188 dq->dq_curblocks = 0; 189 dq->dq_flags &= ~DQ_BLKS; 190 dq->dq_flags |= DQ_MOD; 191 DQI_UNLOCK(dq); 192 } 193 return (0); 194 } 195 if ((flags & FORCE) == 0 && 196 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0)) 197 do_check = 1; 198 else 199 do_check = 0; 200 for (i = 0; i < MAXQUOTAS; i++) { 201 if ((dq = ip->i_dquot[i]) == NODQUOT) 202 continue; 203 warn = 0; 204 DQI_LOCK(dq); 205 DQI_WAIT(dq, PINOD+1, "chkdq2"); 206 if (do_check) { 207 error = chkdqchg(ip, change, cred, i, &warn); 208 if (error) { 209 /* 210 * Roll back user quota changes when 211 * group quota failed. 212 */ 213 while (i > 0) { 214 --i; 215 dq = ip->i_dquot[i]; 216 if (dq == NODQUOT) 217 continue; 218 DQI_LOCK(dq); 219 DQI_WAIT(dq, PINOD+1, "chkdq3"); 220 ncurblocks = dq->dq_curblocks - change; 221 if (ncurblocks >= 0) 222 dq->dq_curblocks = ncurblocks; 223 else 224 dq->dq_curblocks = 0; 225 dq->dq_flags &= ~DQ_BLKS; 226 dq->dq_flags |= DQ_MOD; 227 DQI_UNLOCK(dq); 228 } 229 return (error); 230 } 231 } 232 /* Reset timer when crossing soft limit */ 233 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit && 234 dq->dq_curblocks < dq->dq_bsoftlimit) 235 dq->dq_btime = time_second + 236 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i]; 237 dq->dq_curblocks += change; 238 dq->dq_flags |= DQ_MOD; 239 DQI_UNLOCK(dq); 240 if (warn) 241 uprintf("\n%s: warning, %s disk quota exceeded\n", 242 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 243 quotatypes[i]); 244 } 245 return (0); 246} 247 248/* 249 * Check for a valid change to a users allocation. 250 * Issue an error message if appropriate. 251 */ 252static int 253chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, 254 int type, int *warn) 255{ 256 struct dquot *dq = ip->i_dquot[type]; 257 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change; 258 259 /* 260 * If user would exceed their hard limit, disallow space allocation. 261 */ 262 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) { 263 if ((dq->dq_flags & DQ_BLKS) == 0 && 264 ip->i_uid == cred->cr_uid) { 265 dq->dq_flags |= DQ_BLKS; 266 DQI_UNLOCK(dq); 267 uprintf("\n%s: write failed, %s disk limit reached\n", 268 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 269 quotatypes[type]); 270 return (EDQUOT); 271 } 272 DQI_UNLOCK(dq); 273 return (EDQUOT); 274 } 275 /* 276 * If user is over their soft limit for too long, disallow space 277 * allocation. Reset time limit as they cross their soft limit. 278 */ 279 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) { 280 if (dq->dq_curblocks < dq->dq_bsoftlimit) { 281 dq->dq_btime = time_second + 282 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type]; 283 if (ip->i_uid == cred->cr_uid) 284 *warn = 1; 285 return (0); 286 } 287 if (time_second > dq->dq_btime) { 288 if ((dq->dq_flags & DQ_BLKS) == 0 && 289 ip->i_uid == cred->cr_uid) { 290 dq->dq_flags |= DQ_BLKS; 291 DQI_UNLOCK(dq); 292 uprintf("\n%s: write failed, %s " 293 "disk quota exceeded for too long\n", 294 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 295 quotatypes[type]); 296 return (EDQUOT); 297 } 298 DQI_UNLOCK(dq); 299 return (EDQUOT); 300 } 301 } 302 return (0); 303} 304 305/* 306 * Check the inode limit, applying corrective action. 307 */ 308int 309chkiq(struct inode *ip, int change, struct ucred *cred, int flags) 310{ 311 struct dquot *dq; 312 ino_t ncurinodes; 313 int i, error, warn, do_check; 314 315#ifdef DIAGNOSTIC 316 if ((flags & CHOWN) == 0) 317 chkdquot(ip); 318#endif 319 if (change == 0) 320 return (0); 321 if (change < 0) { 322 for (i = 0; i < MAXQUOTAS; i++) { 323 if ((dq = ip->i_dquot[i]) == NODQUOT) 324 continue; 325 DQI_LOCK(dq); 326 DQI_WAIT(dq, PINOD+1, "chkiq1"); 327 ncurinodes = dq->dq_curinodes + change; 328 /* XXX: ncurinodes is unsigned */ 329 if (dq->dq_curinodes != 0 && ncurinodes >= 0) 330 dq->dq_curinodes = ncurinodes; 331 else 332 dq->dq_curinodes = 0; 333 dq->dq_flags &= ~DQ_INODS; 334 dq->dq_flags |= DQ_MOD; 335 DQI_UNLOCK(dq); 336 } 337 return (0); 338 } 339 if ((flags & FORCE) == 0 && 340 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0)) 341 do_check = 1; 342 else 343 do_check = 0; 344 for (i = 0; i < MAXQUOTAS; i++) { 345 if ((dq = ip->i_dquot[i]) == NODQUOT) 346 continue; 347 warn = 0; 348 DQI_LOCK(dq); 349 DQI_WAIT(dq, PINOD+1, "chkiq2"); 350 if (do_check) { 351 error = chkiqchg(ip, change, cred, i, &warn); 352 if (error) { 353 /* 354 * Roll back user quota changes when 355 * group quota failed. 356 */ 357 while (i > 0) { 358 --i; 359 dq = ip->i_dquot[i]; 360 if (dq == NODQUOT) 361 continue; 362 DQI_LOCK(dq); 363 DQI_WAIT(dq, PINOD+1, "chkiq3"); 364 ncurinodes = dq->dq_curinodes - change; 365 /* XXX: ncurinodes is unsigned */ 366 if (dq->dq_curinodes != 0 && 367 ncurinodes >= 0) 368 dq->dq_curinodes = ncurinodes; 369 else 370 dq->dq_curinodes = 0; 371 dq->dq_flags &= ~DQ_INODS; 372 dq->dq_flags |= DQ_MOD; 373 DQI_UNLOCK(dq); 374 } 375 return (error); 376 } 377 } 378 /* Reset timer when crossing soft limit */ 379 if (dq->dq_curinodes + change >= dq->dq_isoftlimit && 380 dq->dq_curinodes < dq->dq_isoftlimit) 381 dq->dq_itime = time_second + 382 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i]; 383 dq->dq_curinodes += change; 384 dq->dq_flags |= DQ_MOD; 385 DQI_UNLOCK(dq); 386 if (warn) 387 uprintf("\n%s: warning, %s inode quota exceeded\n", 388 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 389 quotatypes[i]); 390 } 391 return (0); 392} 393 394/* 395 * Check for a valid change to a users allocation. 396 * Issue an error message if appropriate. 397 */ 398static int 399chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn) 400{ 401 struct dquot *dq = ip->i_dquot[type]; 402 ino_t ncurinodes = dq->dq_curinodes + change; 403 404 /* 405 * If user would exceed their hard limit, disallow inode allocation. 406 */ 407 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) { 408 if ((dq->dq_flags & DQ_INODS) == 0 && 409 ip->i_uid == cred->cr_uid) { 410 dq->dq_flags |= DQ_INODS; 411 DQI_UNLOCK(dq); 412 uprintf("\n%s: write failed, %s inode limit reached\n", 413 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 414 quotatypes[type]); 415 return (EDQUOT); 416 } 417 DQI_UNLOCK(dq); 418 return (EDQUOT); 419 } 420 /* 421 * If user is over their soft limit for too long, disallow inode 422 * allocation. Reset time limit as they cross their soft limit. 423 */ 424 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) { 425 if (dq->dq_curinodes < dq->dq_isoftlimit) { 426 dq->dq_itime = time_second + 427 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type]; 428 if (ip->i_uid == cred->cr_uid) 429 *warn = 1; 430 return (0); 431 } 432 if (time_second > dq->dq_itime) { 433 if ((dq->dq_flags & DQ_INODS) == 0 && 434 ip->i_uid == cred->cr_uid) { 435 dq->dq_flags |= DQ_INODS; 436 DQI_UNLOCK(dq); 437 uprintf("\n%s: write failed, %s " 438 "inode quota exceeded for too long\n", 439 ITOV(ip)->v_mount->mnt_stat.f_mntonname, 440 quotatypes[type]); 441 return (EDQUOT); 442 } 443 DQI_UNLOCK(dq); 444 return (EDQUOT); 445 } 446 } 447 return (0); 448} 449 450#ifdef DIAGNOSTIC 451/* 452 * On filesystems with quotas enabled, it is an error for a file to change 453 * size and not to have a dquot structure associated with it. 454 */ 455static void 456chkdquot(struct inode *ip) 457{ 458 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount); 459 struct vnode *vp = ITOV(ip); 460 int i; 461 462 /* 463 * Disk quotas must be turned off for system files. Currently 464 * these are snapshots and quota files. 465 */ 466 if ((vp->v_vflag & VV_SYSTEM) != 0) 467 return; 468 /* 469 * XXX: Turn off quotas for files with a negative UID or GID. 470 * This prevents the creation of 100GB+ quota files. 471 */ 472 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0) 473 return; 474 475 UFS_LOCK(ump); 476 for (i = 0; i < MAXQUOTAS; i++) { 477 if (ump->um_quotas[i] == NULLVP || 478 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING))) 479 continue; 480 if (ip->i_dquot[i] == NODQUOT) { 481 UFS_UNLOCK(ump); 482 vprint("chkdquot: missing dquot", ITOV(ip)); 483 panic("chkdquot: missing dquot"); 484 } 485 } 486 UFS_UNLOCK(ump); 487} 488#endif 489 490/* 491 * Code to process quotactl commands. 492 */ 493 494/* 495 * Q_QUOTAON - set up a quota file for a particular filesystem. 496 */ 497int 498quotaon(struct thread *td, struct mount *mp, int type, void *fname) 499{ 500 struct ufsmount *ump; 501 struct vnode *vp, **vpp; 502 struct vnode *mvp; 503 struct dquot *dq; 504 int error, flags, vfslocked; 505 struct nameidata nd; 506 507 error = priv_check(td, PRIV_UFS_QUOTAON); 508 if (error) 509 return (error); 510 511 if (mp->mnt_flag & MNT_RDONLY) 512 return (EROFS); 513 514 ump = VFSTOUFS(mp); 515 dq = NODQUOT; 516 517 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, fname, td); 518 flags = FREAD | FWRITE; 519 error = vn_open(&nd, &flags, 0, NULL); 520 if (error) 521 return (error); 522 vfslocked = NDHASGIANT(&nd); 523 NDFREE(&nd, NDF_ONLY_PNBUF); 524 vp = nd.ni_vp; 525 if (vp->v_type != VREG) { 526 VOP_UNLOCK(vp, 0); 527 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 528 VFS_UNLOCK_GIANT(vfslocked); 529 return (EACCES); 530 } 531 532 UFS_LOCK(ump); 533 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) { 534 UFS_UNLOCK(ump); 535 VOP_UNLOCK(vp, 0); 536 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 537 VFS_UNLOCK_GIANT(vfslocked); 538 return (EALREADY); 539 } 540 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING; 541 UFS_UNLOCK(ump); 542 if ((error = dqopen(vp, ump, type)) != 0) { 543 VOP_UNLOCK(vp, 0); 544 UFS_LOCK(ump); 545 ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING); 546 UFS_UNLOCK(ump); 547 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 548 VFS_UNLOCK_GIANT(vfslocked); 549 return (error); 550 } 551 VOP_UNLOCK(vp, 0); 552 MNT_ILOCK(mp); 553 mp->mnt_flag |= MNT_QUOTA; 554 MNT_IUNLOCK(mp); 555 556 vpp = &ump->um_quotas[type]; 557 if (*vpp != vp) 558 quotaoff1(td, mp, type); 559 560 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 561 vp->v_vflag |= VV_SYSTEM; 562 VOP_UNLOCK(vp, 0); 563 *vpp = vp; 564 VFS_UNLOCK_GIANT(vfslocked); 565 /* 566 * Save the credential of the process that turned on quotas. 567 * Set up the time limits for this quota. 568 */ 569 ump->um_cred[type] = crhold(td->td_ucred); 570 ump->um_btime[type] = MAX_DQ_TIME; 571 ump->um_itime[type] = MAX_IQ_TIME; 572 if (dqget(NULLVP, 0, ump, type, &dq) == 0) { 573 if (dq->dq_btime > 0) 574 ump->um_btime[type] = dq->dq_btime; 575 if (dq->dq_itime > 0) 576 ump->um_itime[type] = dq->dq_itime; 577 dqrele(NULLVP, dq); 578 } 579 /* 580 * Allow the getdq from getinoquota below to read the quota 581 * from file. 582 */ 583 UFS_LOCK(ump); 584 ump->um_qflags[type] &= ~QTF_CLOSING; 585 UFS_UNLOCK(ump); 586 /* 587 * Search vnodes associated with this mount point, 588 * adding references to quota file being opened. 589 * NB: only need to add dquot's for inodes being modified. 590 */ 591 MNT_ILOCK(mp); 592again: 593 MNT_VNODE_FOREACH(vp, mp, mvp) { 594 VI_LOCK(vp); 595 MNT_IUNLOCK(mp); 596 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) { 597 MNT_ILOCK(mp); 598 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp); 599 goto again; 600 } 601 if (vp->v_type == VNON || vp->v_writecount == 0) { 602 VOP_UNLOCK(vp, 0); 603 vrele(vp); 604 MNT_ILOCK(mp); 605 continue; 606 } 607 error = getinoquota(VTOI(vp)); 608 VOP_UNLOCK(vp, 0); 609 vrele(vp); 610 MNT_ILOCK(mp); 611 if (error) { 612 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp); 613 break; 614 } 615 } 616 MNT_IUNLOCK(mp); 617 618 if (error) 619 quotaoff_inchange(td, mp, type); 620 UFS_LOCK(ump); 621 ump->um_qflags[type] &= ~QTF_OPENING; 622 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0, 623 ("quotaon: leaking flags")); 624 UFS_UNLOCK(ump); 625 626 return (error); 627} 628 629/* 630 * Main code to turn off disk quotas for a filesystem. Does not change 631 * flags. 632 */ 633static int 634quotaoff1(struct thread *td, struct mount *mp, int type) 635{ 636 struct vnode *vp; 637 struct vnode *qvp, *mvp; 638 struct ufsmount *ump; 639 struct dquot *dq; 640 struct inode *ip; 641 struct ucred *cr; 642 int vfslocked; 643 int error; 644 645 ump = VFSTOUFS(mp); 646 647 UFS_LOCK(ump); 648 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0, 649 ("quotaoff1: flags are invalid")); 650 if ((qvp = ump->um_quotas[type]) == NULLVP) { 651 UFS_UNLOCK(ump); 652 return (0); 653 } 654 cr = ump->um_cred[type]; 655 UFS_UNLOCK(ump); 656 657 /* 658 * Search vnodes associated with this mount point, 659 * deleting any references to quota file being closed. 660 */ 661 MNT_ILOCK(mp); 662again: 663 MNT_VNODE_FOREACH(vp, mp, mvp) { 664 VI_LOCK(vp); 665 MNT_IUNLOCK(mp); 666 if (vp->v_type == VNON) { 667 VI_UNLOCK(vp); 668 MNT_ILOCK(mp); 669 continue; 670 } 671 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) { 672 MNT_ILOCK(mp); 673 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp); 674 goto again; 675 } 676 ip = VTOI(vp); 677 dq = ip->i_dquot[type]; 678 ip->i_dquot[type] = NODQUOT; 679 dqrele(vp, dq); 680 VOP_UNLOCK(vp, 0); 681 vrele(vp); 682 MNT_ILOCK(mp); 683 } 684 MNT_IUNLOCK(mp); 685 686 dqflush(qvp); 687 /* Clear um_quotas before closing the quota vnode to prevent 688 * access to the closed vnode from dqget/dqsync 689 */ 690 UFS_LOCK(ump); 691 ump->um_quotas[type] = NULLVP; 692 ump->um_cred[type] = NOCRED; 693 UFS_UNLOCK(ump); 694 695 vfslocked = VFS_LOCK_GIANT(qvp->v_mount); 696 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY); 697 qvp->v_vflag &= ~VV_SYSTEM; 698 VOP_UNLOCK(qvp, 0); 699 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td); 700 VFS_UNLOCK_GIANT(vfslocked); 701 crfree(cr); 702 703 return (error); 704} 705 706/* 707 * Turns off quotas, assumes that ump->um_qflags are already checked 708 * and QTF_CLOSING is set to indicate operation in progress. Fixes 709 * ump->um_qflags and mp->mnt_flag after. 710 */ 711int 712quotaoff_inchange(struct thread *td, struct mount *mp, int type) 713{ 714 struct ufsmount *ump; 715 int i; 716 int error; 717 718 error = quotaoff1(td, mp, type); 719 720 ump = VFSTOUFS(mp); 721 UFS_LOCK(ump); 722 ump->um_qflags[type] &= ~QTF_CLOSING; 723 for (i = 0; i < MAXQUOTAS; i++) 724 if (ump->um_quotas[i] != NULLVP) 725 break; 726 if (i == MAXQUOTAS) { 727 MNT_ILOCK(mp); 728 mp->mnt_flag &= ~MNT_QUOTA; 729 MNT_IUNLOCK(mp); 730 } 731 UFS_UNLOCK(ump); 732 return (error); 733} 734 735/* 736 * Q_QUOTAOFF - turn off disk quotas for a filesystem. 737 */ 738int 739quotaoff(struct thread *td, struct mount *mp, int type) 740{ 741 struct ufsmount *ump; 742 int error; 743 744 error = priv_check(td, PRIV_UFS_QUOTAOFF); 745 if (error) 746 return (error); 747 748 ump = VFSTOUFS(mp); 749 UFS_LOCK(ump); 750 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) { 751 UFS_UNLOCK(ump); 752 return (EALREADY); 753 } 754 ump->um_qflags[type] |= QTF_CLOSING; 755 UFS_UNLOCK(ump); 756 757 return (quotaoff_inchange(td, mp, type)); 758} 759 760/* 761 * Q_GETQUOTA - return current values in a dqblk structure. 762 */ 763static int 764_getquota(struct thread *td, struct mount *mp, u_long id, int type, 765 struct dqblk64 *dqb) 766{ 767 struct dquot *dq; 768 int error; 769 770 switch (type) { 771 case USRQUOTA: 772 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) { 773 error = priv_check(td, PRIV_VFS_GETQUOTA); 774 if (error) 775 return (error); 776 } 777 break; 778 779 case GRPQUOTA: 780 if (!groupmember(id, td->td_ucred) && 781 !unprivileged_get_quota) { 782 error = priv_check(td, PRIV_VFS_GETQUOTA); 783 if (error) 784 return (error); 785 } 786 break; 787 788 default: 789 return (EINVAL); 790 } 791 792 dq = NODQUOT; 793 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq); 794 if (error) 795 return (error); 796 *dqb = dq->dq_dqb; 797 dqrele(NULLVP, dq); 798 return (error); 799} 800 801/* 802 * Q_SETQUOTA - assign an entire dqblk structure. 803 */ 804static int 805_setquota(struct thread *td, struct mount *mp, u_long id, int type, 806 struct dqblk64 *dqb) 807{ 808 struct dquot *dq; 809 struct dquot *ndq; 810 struct ufsmount *ump; 811 struct dqblk64 newlim; 812 int error; 813 814 error = priv_check(td, PRIV_VFS_SETQUOTA); 815 if (error) 816 return (error); 817 818 newlim = *dqb; 819 820 ndq = NODQUOT; 821 ump = VFSTOUFS(mp); 822 823 error = dqget(NULLVP, id, ump, type, &ndq); 824 if (error) 825 return (error); 826 dq = ndq; 827 DQI_LOCK(dq); 828 DQI_WAIT(dq, PINOD+1, "setqta"); 829 /* 830 * Copy all but the current values. 831 * Reset time limit if previously had no soft limit or were 832 * under it, but now have a soft limit and are over it. 833 */ 834 newlim.dqb_curblocks = dq->dq_curblocks; 835 newlim.dqb_curinodes = dq->dq_curinodes; 836 if (dq->dq_id != 0) { 837 newlim.dqb_btime = dq->dq_btime; 838 newlim.dqb_itime = dq->dq_itime; 839 } 840 if (newlim.dqb_bsoftlimit && 841 dq->dq_curblocks >= newlim.dqb_bsoftlimit && 842 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit)) 843 newlim.dqb_btime = time_second + ump->um_btime[type]; 844 if (newlim.dqb_isoftlimit && 845 dq->dq_curinodes >= newlim.dqb_isoftlimit && 846 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit)) 847 newlim.dqb_itime = time_second + ump->um_itime[type]; 848 dq->dq_dqb = newlim; 849 if (dq->dq_curblocks < dq->dq_bsoftlimit) 850 dq->dq_flags &= ~DQ_BLKS; 851 if (dq->dq_curinodes < dq->dq_isoftlimit) 852 dq->dq_flags &= ~DQ_INODS; 853 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 && 854 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0) 855 dq->dq_flags |= DQ_FAKE; 856 else 857 dq->dq_flags &= ~DQ_FAKE; 858 dq->dq_flags |= DQ_MOD; 859 DQI_UNLOCK(dq); 860 dqrele(NULLVP, dq); 861 return (0); 862} 863 864/* 865 * Q_SETUSE - set current inode and block usage. 866 */ 867static int 868_setuse(struct thread *td, struct mount *mp, u_long id, int type, 869 struct dqblk64 *dqb) 870{ 871 struct dquot *dq; 872 struct ufsmount *ump; 873 struct dquot *ndq; 874 struct dqblk64 usage; 875 int error; 876 877 error = priv_check(td, PRIV_UFS_SETUSE); 878 if (error) 879 return (error); 880 881 usage = *dqb; 882 883 ump = VFSTOUFS(mp); 884 ndq = NODQUOT; 885 886 error = dqget(NULLVP, id, ump, type, &ndq); 887 if (error) 888 return (error); 889 dq = ndq; 890 DQI_LOCK(dq); 891 DQI_WAIT(dq, PINOD+1, "setuse"); 892 /* 893 * Reset time limit if have a soft limit and were 894 * previously under it, but are now over it. 895 */ 896 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit && 897 usage.dqb_curblocks >= dq->dq_bsoftlimit) 898 dq->dq_btime = time_second + ump->um_btime[type]; 899 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit && 900 usage.dqb_curinodes >= dq->dq_isoftlimit) 901 dq->dq_itime = time_second + ump->um_itime[type]; 902 dq->dq_curblocks = usage.dqb_curblocks; 903 dq->dq_curinodes = usage.dqb_curinodes; 904 if (dq->dq_curblocks < dq->dq_bsoftlimit) 905 dq->dq_flags &= ~DQ_BLKS; 906 if (dq->dq_curinodes < dq->dq_isoftlimit) 907 dq->dq_flags &= ~DQ_INODS; 908 dq->dq_flags |= DQ_MOD; 909 DQI_UNLOCK(dq); 910 dqrele(NULLVP, dq); 911 return (0); 912} 913 914int 915getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr) 916{ 917 struct dqblk32 dqb32; 918 struct dqblk64 dqb64; 919 int error; 920 921 error = _getquota(td, mp, id, type, &dqb64); 922 if (error) 923 return (error); 924 dqb64_dqb32(&dqb64, &dqb32); 925 error = copyout(&dqb32, addr, sizeof(dqb32)); 926 return (error); 927} 928 929int 930setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr) 931{ 932 struct dqblk32 dqb32; 933 struct dqblk64 dqb64; 934 int error; 935 936 error = copyin(addr, &dqb32, sizeof(dqb32)); 937 if (error) 938 return (error); 939 dqb32_dqb64(&dqb32, &dqb64); 940 error = _setquota(td, mp, id, type, &dqb64); 941 return (error); 942} 943 944int 945setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr) 946{ 947 struct dqblk32 dqb32; 948 struct dqblk64 dqb64; 949 int error; 950 951 error = copyin(addr, &dqb32, sizeof(dqb32)); 952 if (error) 953 return (error); 954 dqb32_dqb64(&dqb32, &dqb64); 955 error = _setuse(td, mp, id, type, &dqb64); 956 return (error); 957} 958 959int 960getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr) 961{ 962 struct dqblk64 dqb64; 963 int error; 964 965 error = _getquota(td, mp, id, type, &dqb64); 966 if (error) 967 return (error); 968 error = copyout(&dqb64, addr, sizeof(dqb64)); 969 return (error); 970} 971 972int 973setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr) 974{ 975 struct dqblk64 dqb64; 976 int error; 977 978 error = copyin(addr, &dqb64, sizeof(dqb64)); 979 if (error) 980 return (error); 981 error = _setquota(td, mp, id, type, &dqb64); 982 return (error); 983} 984 985int 986setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr) 987{ 988 struct dqblk64 dqb64; 989 int error; 990 991 error = copyin(addr, &dqb64, sizeof(dqb64)); 992 if (error) 993 return (error); 994 error = _setuse(td, mp, id, type, &dqb64); 995 return (error); 996} 997 998/* 999 * Q_GETQUOTASIZE - get bit-size of quota file fields 1000 */ 1001int 1002getquotasize(struct thread *td, struct mount *mp, u_long id, int type, 1003 void *sizep) 1004{ 1005 struct ufsmount *ump = VFSTOUFS(mp); 1006 int bitsize; 1007 1008 UFS_LOCK(ump); 1009 if (ump->um_quotas[type] == NULLVP || 1010 (ump->um_qflags[type] & QTF_CLOSING)) { 1011 UFS_UNLOCK(ump); 1012 return (EINVAL); 1013 } 1014 if ((ump->um_qflags[type] & QTF_64BIT) != 0) 1015 bitsize = 64; 1016 else 1017 bitsize = 32; 1018 UFS_UNLOCK(ump); 1019 return (copyout(&bitsize, sizep, sizeof(int))); 1020} 1021 1022/* 1023 * Q_SYNC - sync quota files to disk. 1024 */ 1025int 1026qsync(struct mount *mp) 1027{ 1028 struct ufsmount *ump = VFSTOUFS(mp); 1029 struct thread *td = curthread; /* XXX */ 1030 struct vnode *vp, *mvp; 1031 struct dquot *dq; 1032 int i, error; 1033 1034 /* 1035 * Check if the mount point has any quotas. 1036 * If not, simply return. 1037 */ 1038 UFS_LOCK(ump); 1039 for (i = 0; i < MAXQUOTAS; i++) 1040 if (ump->um_quotas[i] != NULLVP) 1041 break; 1042 UFS_UNLOCK(ump); 1043 if (i == MAXQUOTAS) 1044 return (0); 1045 /* 1046 * Search vnodes associated with this mount point, 1047 * synchronizing any modified dquot structures. 1048 */ 1049 MNT_ILOCK(mp); 1050again: 1051 MNT_VNODE_FOREACH(vp, mp, mvp) { 1052 VI_LOCK(vp); 1053 MNT_IUNLOCK(mp); 1054 if (vp->v_type == VNON) { 1055 VI_UNLOCK(vp); 1056 MNT_ILOCK(mp); 1057 continue; 1058 } 1059 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td); 1060 if (error) { 1061 MNT_ILOCK(mp); 1062 if (error == ENOENT) { 1063 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp); 1064 goto again; 1065 } 1066 continue; 1067 } 1068 for (i = 0; i < MAXQUOTAS; i++) { 1069 dq = VTOI(vp)->i_dquot[i]; 1070 if (dq != NODQUOT) 1071 dqsync(vp, dq); 1072 } 1073 vput(vp); 1074 MNT_ILOCK(mp); 1075 } 1076 MNT_IUNLOCK(mp); 1077 return (0); 1078} 1079 1080/* 1081 * Code pertaining to management of the in-core dquot data structures. 1082 */ 1083#define DQHASH(dqvp, id) \ 1084 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash]) 1085static LIST_HEAD(dqhash, dquot) *dqhashtbl; 1086static u_long dqhash; 1087 1088/* 1089 * Dquot free list. 1090 */ 1091#define DQUOTINC 5 /* minimum free dquots desired */ 1092static TAILQ_HEAD(dqfreelist, dquot) dqfreelist; 1093static long numdquot, desireddquot = DQUOTINC; 1094 1095/* 1096 * Lock to protect quota hash, dq free list and dq_cnt ref counters of 1097 * _all_ dqs. 1098 */ 1099struct mtx dqhlock; 1100 1101#define DQH_LOCK() mtx_lock(&dqhlock) 1102#define DQH_UNLOCK() mtx_unlock(&dqhlock) 1103 1104static struct dquot *dqhashfind(struct dqhash *dqh, u_long id, 1105 struct vnode *dqvp); 1106 1107/* 1108 * Initialize the quota system. 1109 */ 1110void 1111dqinit(void) 1112{ 1113 1114 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF); 1115 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash); 1116 TAILQ_INIT(&dqfreelist); 1117} 1118 1119/* 1120 * Shut down the quota system. 1121 */ 1122void 1123dquninit(void) 1124{ 1125 struct dquot *dq; 1126 1127 hashdestroy(dqhashtbl, M_DQUOT, dqhash); 1128 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) { 1129 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist); 1130 mtx_destroy(&dq->dq_lock); 1131 free(dq, M_DQUOT); 1132 } 1133 mtx_destroy(&dqhlock); 1134} 1135 1136static struct dquot * 1137dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp) 1138{ 1139 struct dquot *dq; 1140 1141 mtx_assert(&dqhlock, MA_OWNED); 1142 LIST_FOREACH(dq, dqh, dq_hash) { 1143 if (dq->dq_id != id || 1144 dq->dq_ump->um_quotas[dq->dq_type] != dqvp) 1145 continue; 1146 /* 1147 * Cache hit with no references. Take 1148 * the structure off the free list. 1149 */ 1150 if (dq->dq_cnt == 0) 1151 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist); 1152 DQREF(dq); 1153 return (dq); 1154 } 1155 return (NODQUOT); 1156} 1157 1158/* 1159 * Determine the quota file type. 1160 * 1161 * A 32-bit quota file is simply an array of struct dqblk32. 1162 * 1163 * A 64-bit quota file is a struct dqhdr64 followed by an array of struct 1164 * dqblk64. The header contains various magic bits which allow us to be 1165 * reasonably confident that it is indeeda 64-bit quota file and not just 1166 * a 32-bit quota file that just happens to "look right". 1167 * 1168 */ 1169static int 1170dqopen(struct vnode *vp, struct ufsmount *ump, int type) 1171{ 1172 struct dqhdr64 dqh; 1173 struct iovec aiov; 1174 struct uio auio; 1175 int error; 1176 1177 ASSERT_VOP_LOCKED(vp, "dqopen"); 1178 auio.uio_iov = &aiov; 1179 auio.uio_iovcnt = 1; 1180 aiov.iov_base = &dqh; 1181 aiov.iov_len = sizeof(dqh); 1182 auio.uio_resid = sizeof(dqh); 1183 auio.uio_offset = 0; 1184 auio.uio_segflg = UIO_SYSSPACE; 1185 auio.uio_rw = UIO_READ; 1186 auio.uio_td = (struct thread *)0; 1187 error = VOP_READ(vp, &auio, 0, ump->um_cred[type]); 1188 1189 if (error != 0) 1190 return (error); 1191 if (auio.uio_resid > 0) { 1192 /* assume 32 bits */ 1193 return (0); 1194 } 1195 1196 UFS_LOCK(ump); 1197 if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 && 1198 be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION && 1199 be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) && 1200 be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) { 1201 /* XXX: what if the magic matches, but the sizes are wrong? */ 1202 ump->um_qflags[type] |= QTF_64BIT; 1203 } else { 1204 ump->um_qflags[type] &= ~QTF_64BIT; 1205 } 1206 UFS_UNLOCK(ump); 1207 1208 return (0); 1209} 1210 1211/* 1212 * Obtain a dquot structure for the specified identifier and quota file 1213 * reading the information from the file if necessary. 1214 */ 1215static int 1216dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type, 1217 struct dquot **dqp) 1218{ 1219 uint8_t buf[sizeof(struct dqblk64)]; 1220 off_t base, recsize; 1221 struct dquot *dq, *dq1; 1222 struct dqhash *dqh; 1223 struct vnode *dqvp; 1224 struct iovec aiov; 1225 struct uio auio; 1226 int vfslocked, dqvplocked, error; 1227 1228#ifdef DEBUG_VFS_LOCKS 1229 if (vp != NULLVP) 1230 ASSERT_VOP_ELOCKED(vp, "dqget"); 1231#endif 1232 1233 if (vp != NULLVP && *dqp != NODQUOT) { 1234 return (0); 1235 } 1236 1237 /* XXX: Disallow negative id values to prevent the 1238 * creation of 100GB+ quota data files. 1239 */ 1240 if ((int)id < 0) 1241 return (EINVAL); 1242 1243 UFS_LOCK(ump); 1244 dqvp = ump->um_quotas[type]; 1245 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) { 1246 *dqp = NODQUOT; 1247 UFS_UNLOCK(ump); 1248 return (EINVAL); 1249 } 1250 vref(dqvp); 1251 UFS_UNLOCK(ump); 1252 error = 0; 1253 dqvplocked = 0; 1254 1255 /* 1256 * Check the cache first. 1257 */ 1258 dqh = DQHASH(dqvp, id); 1259 DQH_LOCK(); 1260 dq = dqhashfind(dqh, id, dqvp); 1261 if (dq != NULL) { 1262 DQH_UNLOCK(); 1263hfound: DQI_LOCK(dq); 1264 DQI_WAIT(dq, PINOD+1, "dqget"); 1265 DQI_UNLOCK(dq); 1266 if (dq->dq_ump == NULL) { 1267 dqrele(vp, dq); 1268 dq = NODQUOT; 1269 error = EIO; 1270 } 1271 *dqp = dq; 1272 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount); 1273 if (dqvplocked) 1274 vput(dqvp); 1275 else 1276 vrele(dqvp); 1277 VFS_UNLOCK_GIANT(vfslocked); 1278 return (error); 1279 } 1280 1281 /* 1282 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there 1283 * since new dq will appear on the hash chain DQ_LOCKed. 1284 */ 1285 if (vp != dqvp) { 1286 DQH_UNLOCK(); 1287 vn_lock(dqvp, LK_SHARED | LK_RETRY); 1288 dqvplocked = 1; 1289 DQH_LOCK(); 1290 /* 1291 * Recheck the cache after sleep for quota vnode lock. 1292 */ 1293 dq = dqhashfind(dqh, id, dqvp); 1294 if (dq != NULL) { 1295 DQH_UNLOCK(); 1296 goto hfound; 1297 } 1298 } 1299 1300 /* 1301 * Not in cache, allocate a new one or take it from the 1302 * free list. 1303 */ 1304 if (TAILQ_FIRST(&dqfreelist) == NODQUOT && 1305 numdquot < MAXQUOTAS * desiredvnodes) 1306 desireddquot += DQUOTINC; 1307 if (numdquot < desireddquot) { 1308 numdquot++; 1309 DQH_UNLOCK(); 1310 dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO); 1311 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF); 1312 DQH_LOCK(); 1313 /* 1314 * Recheck the cache after sleep for memory. 1315 */ 1316 dq = dqhashfind(dqh, id, dqvp); 1317 if (dq != NULL) { 1318 numdquot--; 1319 DQH_UNLOCK(); 1320 mtx_destroy(&dq1->dq_lock); 1321 free(dq1, M_DQUOT); 1322 goto hfound; 1323 } 1324 dq = dq1; 1325 } else { 1326 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) { 1327 DQH_UNLOCK(); 1328 tablefull("dquot"); 1329 *dqp = NODQUOT; 1330 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount); 1331 if (dqvplocked) 1332 vput(dqvp); 1333 else 1334 vrele(dqvp); 1335 VFS_UNLOCK_GIANT(vfslocked); 1336 return (EUSERS); 1337 } 1338 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD)) 1339 panic("dqget: free dquot isn't %p", dq); 1340 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist); 1341 if (dq->dq_ump != NULL) 1342 LIST_REMOVE(dq, dq_hash); 1343 } 1344 1345 /* 1346 * Dq is put into hash already locked to prevent parallel 1347 * usage while it is being read from file. 1348 */ 1349 dq->dq_flags = DQ_LOCK; 1350 dq->dq_id = id; 1351 dq->dq_type = type; 1352 dq->dq_ump = ump; 1353 LIST_INSERT_HEAD(dqh, dq, dq_hash); 1354 DQREF(dq); 1355 DQH_UNLOCK(); 1356 1357 /* 1358 * Read the requested quota record from the quota file, performing 1359 * any necessary conversions. 1360 */ 1361 if (ump->um_qflags[type] & QTF_64BIT) { 1362 recsize = sizeof(struct dqblk64); 1363 base = sizeof(struct dqhdr64); 1364 } else { 1365 recsize = sizeof(struct dqblk32); 1366 base = 0; 1367 } 1368 auio.uio_iov = &aiov; 1369 auio.uio_iovcnt = 1; 1370 aiov.iov_base = buf; 1371 aiov.iov_len = recsize; 1372 auio.uio_resid = recsize; 1373 auio.uio_offset = base + id * recsize; 1374 auio.uio_segflg = UIO_SYSSPACE; 1375 auio.uio_rw = UIO_READ; 1376 auio.uio_td = (struct thread *)0; 1377 1378 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount); 1379 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]); 1380 if (auio.uio_resid == recsize && error == 0) { 1381 bzero(&dq->dq_dqb, sizeof(dq->dq_dqb)); 1382 } else { 1383 if (ump->um_qflags[type] & QTF_64BIT) 1384 dqb64_dq((struct dqblk64 *)buf, dq); 1385 else 1386 dqb32_dq((struct dqblk32 *)buf, dq); 1387 } 1388 if (dqvplocked) 1389 vput(dqvp); 1390 else 1391 vrele(dqvp); 1392 VFS_UNLOCK_GIANT(vfslocked); 1393 /* 1394 * I/O error in reading quota file, release 1395 * quota structure and reflect problem to caller. 1396 */ 1397 if (error) { 1398 DQH_LOCK(); 1399 dq->dq_ump = NULL; 1400 LIST_REMOVE(dq, dq_hash); 1401 DQH_UNLOCK(); 1402 DQI_LOCK(dq); 1403 if (dq->dq_flags & DQ_WANT) 1404 wakeup(dq); 1405 dq->dq_flags = 0; 1406 DQI_UNLOCK(dq); 1407 dqrele(vp, dq); 1408 *dqp = NODQUOT; 1409 return (error); 1410 } 1411 DQI_LOCK(dq); 1412 /* 1413 * Check for no limit to enforce. 1414 * Initialize time values if necessary. 1415 */ 1416 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 && 1417 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0) 1418 dq->dq_flags |= DQ_FAKE; 1419 if (dq->dq_id != 0) { 1420 if (dq->dq_btime == 0) { 1421 dq->dq_btime = time_second + ump->um_btime[type]; 1422 if (dq->dq_bsoftlimit && 1423 dq->dq_curblocks >= dq->dq_bsoftlimit) 1424 dq->dq_flags |= DQ_MOD; 1425 } 1426 if (dq->dq_itime == 0) { 1427 dq->dq_itime = time_second + ump->um_itime[type]; 1428 if (dq->dq_isoftlimit && 1429 dq->dq_curinodes >= dq->dq_isoftlimit) 1430 dq->dq_flags |= DQ_MOD; 1431 } 1432 } 1433 DQI_WAKEUP(dq); 1434 DQI_UNLOCK(dq); 1435 *dqp = dq; 1436 return (0); 1437} 1438 1439#ifdef DIAGNOSTIC 1440/* 1441 * Obtain a reference to a dquot. 1442 */ 1443static void 1444dqref(struct dquot *dq) 1445{ 1446 1447 dq->dq_cnt++; 1448} 1449#endif 1450 1451/* 1452 * Release a reference to a dquot. 1453 */ 1454void 1455dqrele(struct vnode *vp, struct dquot *dq) 1456{ 1457 1458 if (dq == NODQUOT) 1459 return; 1460 DQH_LOCK(); 1461 if (dq->dq_cnt > 1) { 1462 dq->dq_cnt--; 1463 DQH_UNLOCK(); 1464 return; 1465 } 1466 DQH_UNLOCK(); 1467sync: 1468 (void) dqsync(vp, dq); 1469 1470 DQH_LOCK(); 1471 if (--dq->dq_cnt > 0) 1472 { 1473 DQH_UNLOCK(); 1474 return; 1475 } 1476 1477 /* 1478 * The dq may become dirty after it is synced but before it is 1479 * put to the free list. Checking the DQ_MOD there without 1480 * locking dq should be safe since no other references to the 1481 * dq exist. 1482 */ 1483 if ((dq->dq_flags & DQ_MOD) != 0) { 1484 dq->dq_cnt++; 1485 DQH_UNLOCK(); 1486 goto sync; 1487 } 1488 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist); 1489 DQH_UNLOCK(); 1490} 1491 1492/* 1493 * Update the disk quota in the quota file. 1494 */ 1495static int 1496dqsync(struct vnode *vp, struct dquot *dq) 1497{ 1498 uint8_t buf[sizeof(struct dqblk64)]; 1499 off_t base, recsize; 1500 struct vnode *dqvp; 1501 struct iovec aiov; 1502 struct uio auio; 1503 int vfslocked, error; 1504 struct mount *mp; 1505 struct ufsmount *ump; 1506 1507#ifdef DEBUG_VFS_LOCKS 1508 if (vp != NULL) 1509 ASSERT_VOP_ELOCKED(vp, "dqsync"); 1510#endif 1511 1512 mp = NULL; 1513 error = 0; 1514 if (dq == NODQUOT) 1515 panic("dqsync: dquot"); 1516 if ((ump = dq->dq_ump) == NULL) 1517 return (0); 1518 UFS_LOCK(ump); 1519 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP) 1520 panic("dqsync: file"); 1521 vref(dqvp); 1522 UFS_UNLOCK(ump); 1523 1524 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount); 1525 DQI_LOCK(dq); 1526 if ((dq->dq_flags & DQ_MOD) == 0) { 1527 DQI_UNLOCK(dq); 1528 vrele(dqvp); 1529 VFS_UNLOCK_GIANT(vfslocked); 1530 return (0); 1531 } 1532 DQI_UNLOCK(dq); 1533 1534 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT); 1535 if (vp != dqvp) 1536 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY); 1537 1538 VFS_UNLOCK_GIANT(vfslocked); 1539 DQI_LOCK(dq); 1540 DQI_WAIT(dq, PINOD+2, "dqsync"); 1541 if ((dq->dq_flags & DQ_MOD) == 0) 1542 goto out; 1543 dq->dq_flags |= DQ_LOCK; 1544 DQI_UNLOCK(dq); 1545 1546 /* 1547 * Write the quota record to the quota file, performing any 1548 * necessary conversions. See dqget() for additional details. 1549 */ 1550 if (ump->um_qflags[dq->dq_type] & QTF_64BIT) { 1551 dq_dqb64(dq, (struct dqblk64 *)buf); 1552 recsize = sizeof(struct dqblk64); 1553 base = sizeof(struct dqhdr64); 1554 } else { 1555 dq_dqb32(dq, (struct dqblk32 *)buf); 1556 recsize = sizeof(struct dqblk32); 1557 base = 0; 1558 } 1559 1560 auio.uio_iov = &aiov; 1561 auio.uio_iovcnt = 1; 1562 aiov.iov_base = buf; 1563 aiov.iov_len = recsize; 1564 auio.uio_resid = recsize; 1565 auio.uio_offset = base + dq->dq_id * recsize; 1566 auio.uio_segflg = UIO_SYSSPACE; 1567 auio.uio_rw = UIO_WRITE; 1568 auio.uio_td = (struct thread *)0; 1569 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount); 1570 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]); 1571 VFS_UNLOCK_GIANT(vfslocked); 1572 if (auio.uio_resid && error == 0) 1573 error = EIO; 1574 1575 DQI_LOCK(dq); 1576 DQI_WAKEUP(dq); 1577 dq->dq_flags &= ~DQ_MOD; 1578out: 1579 DQI_UNLOCK(dq); 1580 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount); 1581 if (vp != dqvp) 1582 vput(dqvp); 1583 else 1584 vrele(dqvp); 1585 vn_finished_secondary_write(mp); 1586 VFS_UNLOCK_GIANT(vfslocked); 1587 return (error); 1588} 1589 1590/* 1591 * Flush all entries from the cache for a particular vnode. 1592 */ 1593static void 1594dqflush(struct vnode *vp) 1595{ 1596 struct dquot *dq, *nextdq; 1597 struct dqhash *dqh; 1598 1599 /* 1600 * Move all dquot's that used to refer to this quota 1601 * file off their hash chains (they will eventually 1602 * fall off the head of the free list and be re-used). 1603 */ 1604 DQH_LOCK(); 1605 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) { 1606 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) { 1607 nextdq = LIST_NEXT(dq, dq_hash); 1608 if (dq->dq_ump->um_quotas[dq->dq_type] != vp) 1609 continue; 1610 if (dq->dq_cnt) 1611 panic("dqflush: stray dquot"); 1612 LIST_REMOVE(dq, dq_hash); 1613 dq->dq_ump = (struct ufsmount *)0; 1614 } 1615 } 1616 DQH_UNLOCK(); 1617} 1618 1619/* 1620 * 32-bit / 64-bit conversion functions. 1621 * 1622 * 32-bit quota records are stored in native byte order. Attention must 1623 * be paid to overflow issues. 1624 * 1625 * 64-bit quota records are stored in network byte order. 1626 */ 1627 1628#define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64) 1629 1630/* 1631 * Convert 32-bit host-order structure to dquot. 1632 */ 1633static void 1634dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq) 1635{ 1636 1637 dq->dq_bhardlimit = dqb32->dqb_bhardlimit; 1638 dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit; 1639 dq->dq_curblocks = dqb32->dqb_curblocks; 1640 dq->dq_ihardlimit = dqb32->dqb_ihardlimit; 1641 dq->dq_isoftlimit = dqb32->dqb_isoftlimit; 1642 dq->dq_curinodes = dqb32->dqb_curinodes; 1643 dq->dq_btime = dqb32->dqb_btime; 1644 dq->dq_itime = dqb32->dqb_itime; 1645} 1646 1647/* 1648 * Convert 64-bit network-order structure to dquot. 1649 */ 1650static void 1651dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq) 1652{ 1653 1654 dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit); 1655 dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit); 1656 dq->dq_curblocks = be64toh(dqb64->dqb_curblocks); 1657 dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit); 1658 dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit); 1659 dq->dq_curinodes = be64toh(dqb64->dqb_curinodes); 1660 dq->dq_btime = be64toh(dqb64->dqb_btime); 1661 dq->dq_itime = be64toh(dqb64->dqb_itime); 1662} 1663 1664/* 1665 * Convert dquot to 32-bit host-order structure. 1666 */ 1667static void 1668dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32) 1669{ 1670 1671 dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit); 1672 dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit); 1673 dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks); 1674 dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit); 1675 dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit); 1676 dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes); 1677 dqb32->dqb_btime = CLIP32(dq->dq_btime); 1678 dqb32->dqb_itime = CLIP32(dq->dq_itime); 1679} 1680 1681/* 1682 * Convert dquot to 64-bit network-order structure. 1683 */ 1684static void 1685dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64) 1686{ 1687 1688 dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit); 1689 dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit); 1690 dqb64->dqb_curblocks = htobe64(dq->dq_curblocks); 1691 dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit); 1692 dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit); 1693 dqb64->dqb_curinodes = htobe64(dq->dq_curinodes); 1694 dqb64->dqb_btime = htobe64(dq->dq_btime); 1695 dqb64->dqb_itime = htobe64(dq->dq_itime); 1696} 1697 1698/* 1699 * Convert 64-bit host-order structure to 32-bit host-order structure. 1700 */ 1701static void 1702dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32) 1703{ 1704 1705 dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit); 1706 dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit); 1707 dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks); 1708 dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit); 1709 dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit); 1710 dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes); 1711 dqb32->dqb_btime = CLIP32(dqb64->dqb_btime); 1712 dqb32->dqb_itime = CLIP32(dqb64->dqb_itime); 1713} 1714 1715/* 1716 * Convert 32-bit host-order structure to 64-bit host-order structure. 1717 */ 1718static void 1719dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64) 1720{ 1721 1722 dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit; 1723 dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit; 1724 dqb64->dqb_curblocks = dqb32->dqb_curblocks; 1725 dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit; 1726 dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit; 1727 dqb64->dqb_curinodes = dqb32->dqb_curinodes; 1728 dqb64->dqb_btime = dqb32->dqb_btime; 1729 dqb64->dqb_itime = dqb32->dqb_itime; 1730} 1731