1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs_platform.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_bit.h" 13 #include "xfs_sb.h" 14 #include "xfs_mount.h" 15 #include "xfs_inode.h" 16 #include "xfs_iwalk.h" 17 #include "xfs_quota.h" 18 #include "xfs_bmap.h" 19 #include "xfs_bmap_util.h" 20 #include "xfs_trans.h" 21 #include "xfs_trans_space.h" 22 #include "xfs_qm.h" 23 #include "xfs_trace.h" 24 #include "xfs_icache.h" 25 #include "xfs_error.h" 26 #include "xfs_ag.h" 27 #include "xfs_ialloc.h" 28 #include "xfs_log_priv.h" 29 #include "xfs_health.h" 30 #include "xfs_da_format.h" 31 #include "xfs_metafile.h" 32 #include "xfs_rtgroup.h" 33 34 /* 35 * The global quota manager. There is only one of these for the entire 36 * system, _not_ one per file system. XQM keeps track of the overall 37 * quota functionality, including maintaining the freelist and hash 38 * tables of dquots. 39 */ 40 STATIC int xfs_qm_init_quotainos(struct xfs_mount *mp); 41 STATIC int xfs_qm_init_quotainfo(struct xfs_mount *mp); 42 43 STATIC void xfs_qm_dqfree_one(struct xfs_dquot *dqp); 44 /* 45 * We use the batch lookup interface to iterate over the dquots as it 46 * currently is the only interface into the radix tree code that allows 47 * fuzzy lookups instead of exact matches. Holding the lock over multiple 48 * operations is fine as all callers are used either during mount/umount 49 * or quotaoff. 50 */ 51 #define XFS_DQ_LOOKUP_BATCH 32 52 53 STATIC int 54 xfs_qm_dquot_walk( 55 struct xfs_mount *mp, 56 xfs_dqtype_t type, 57 int (*execute)(struct xfs_dquot *dqp, void *data), 58 void *data) 59 { 60 struct xfs_quotainfo *qi = mp->m_quotainfo; 61 struct radix_tree_root *tree = xfs_dquot_tree(qi, type); 62 uint32_t next_index; 63 int last_error = 0; 64 int skipped; 65 int nr_found; 66 67 restart: 68 skipped = 0; 69 next_index = 0; 70 nr_found = 0; 71 72 while (1) { 73 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH]; 74 int error; 75 int i; 76 77 mutex_lock(&qi->qi_tree_lock); 78 nr_found = radix_tree_gang_lookup(tree, (void **)batch, 79 next_index, XFS_DQ_LOOKUP_BATCH); 80 if (!nr_found) { 81 mutex_unlock(&qi->qi_tree_lock); 82 break; 83 } 84 85 for (i = 0; i < nr_found; i++) { 86 struct xfs_dquot *dqp = batch[i]; 87 88 next_index = dqp->q_id + 1; 89 90 error = execute(batch[i], data); 91 if (error == -EAGAIN) { 92 skipped++; 93 continue; 94 } 95 if (error && last_error != -EFSCORRUPTED) 96 last_error = error; 97 } 98 99 mutex_unlock(&qi->qi_tree_lock); 100 101 /* bail out if the filesystem is corrupted. */ 102 if (last_error == -EFSCORRUPTED) { 103 skipped = 0; 104 break; 105 } 106 /* we're done if id overflows back to zero */ 107 if (!next_index) 108 break; 109 } 110 111 if (skipped) { 112 delay(1); 113 goto restart; 114 } 115 116 return last_error; 117 } 118 119 120 /* 121 * Purge a dquot from all tracking data structures and free it. 122 */ 123 STATIC int 124 xfs_qm_dqpurge( 125 struct xfs_dquot *dqp, 126 void *data) 127 { 128 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo; 129 130 spin_lock(&dqp->q_lockref.lock); 131 if (dqp->q_lockref.count > 0 || lockref_is_dead(&dqp->q_lockref)) { 132 spin_unlock(&dqp->q_lockref.lock); 133 return -EAGAIN; 134 } 135 lockref_mark_dead(&dqp->q_lockref); 136 spin_unlock(&dqp->q_lockref.lock); 137 138 mutex_lock(&dqp->q_qlock); 139 xfs_qm_dqunpin_wait(dqp); 140 xfs_dqflock(dqp); 141 142 /* 143 * If we are turning this type of quotas off, we don't care 144 * about the dirty metadata sitting in this dquot. OTOH, if 145 * we're unmounting, we do care, so we flush it and wait. 146 */ 147 if (XFS_DQ_IS_DIRTY(dqp)) { 148 struct xfs_buf *bp = NULL; 149 int error; 150 151 /* 152 * We don't care about getting disk errors here. We need 153 * to purge this dquot anyway, so we go ahead regardless. 154 */ 155 error = xfs_dquot_use_attached_buf(dqp, &bp); 156 if (error == -EAGAIN) { 157 /* resurrect the refcount from the dead. */ 158 dqp->q_lockref.count = 0; 159 goto out_funlock; 160 } 161 if (!bp) 162 goto out_funlock; 163 164 /* 165 * dqflush completes dqflock on error, and the bwrite ioend 166 * does it on success. 167 */ 168 error = xfs_qm_dqflush(dqp, bp); 169 if (!error) 170 error = xfs_bwrite(bp); 171 xfs_buf_relse(bp); 172 xfs_dqflock(dqp); 173 } 174 xfs_dquot_detach_buf(dqp); 175 176 out_funlock: 177 ASSERT(atomic_read(&dqp->q_pincount) == 0); 178 ASSERT(xlog_is_shutdown(dqp->q_logitem.qli_item.li_log) || 179 !test_bit(XFS_LI_IN_AIL, &dqp->q_logitem.qli_item.li_flags)); 180 181 xfs_dqfunlock(dqp); 182 mutex_unlock(&dqp->q_qlock); 183 184 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id); 185 qi->qi_dquots--; 186 187 /* 188 * We move dquots to the freelist as soon as their reference count 189 * hits zero, so it really should be on the freelist here. 190 */ 191 ASSERT(!list_empty(&dqp->q_lru)); 192 list_lru_del_obj(&qi->qi_lru, &dqp->q_lru); 193 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused); 194 195 xfs_qm_dqdestroy(dqp); 196 return 0; 197 } 198 199 /* 200 * Purge the dquot cache. 201 */ 202 static void 203 xfs_qm_dqpurge_all( 204 struct xfs_mount *mp) 205 { 206 xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_dqpurge, NULL); 207 xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_dqpurge, NULL); 208 xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_dqpurge, NULL); 209 } 210 211 /* 212 * Just destroy the quotainfo structure. 213 */ 214 void 215 xfs_qm_unmount( 216 struct xfs_mount *mp) 217 { 218 if (mp->m_quotainfo) { 219 xfs_qm_dqpurge_all(mp); 220 xfs_qm_destroy_quotainfo(mp); 221 } 222 } 223 224 static void 225 xfs_qm_unmount_rt( 226 struct xfs_mount *mp) 227 { 228 struct xfs_rtgroup *rtg = xfs_rtgroup_grab(mp, 0); 229 230 if (!rtg) 231 return; 232 if (rtg_bitmap(rtg)) 233 xfs_qm_dqdetach(rtg_bitmap(rtg)); 234 if (rtg_summary(rtg)) 235 xfs_qm_dqdetach(rtg_summary(rtg)); 236 xfs_rtgroup_rele(rtg); 237 } 238 239 STATIC void 240 xfs_qm_destroy_quotainos( 241 struct xfs_quotainfo *qi) 242 { 243 if (qi->qi_uquotaip) { 244 xfs_irele(qi->qi_uquotaip); 245 qi->qi_uquotaip = NULL; /* paranoia */ 246 } 247 if (qi->qi_gquotaip) { 248 xfs_irele(qi->qi_gquotaip); 249 qi->qi_gquotaip = NULL; 250 } 251 if (qi->qi_pquotaip) { 252 xfs_irele(qi->qi_pquotaip); 253 qi->qi_pquotaip = NULL; 254 } 255 if (qi->qi_dirip) { 256 xfs_irele(qi->qi_dirip); 257 qi->qi_dirip = NULL; 258 } 259 } 260 261 /* 262 * Called from the vfsops layer. 263 */ 264 void 265 xfs_qm_unmount_quotas( 266 xfs_mount_t *mp) 267 { 268 /* 269 * Release the dquots that root inode, et al might be holding, 270 * before we flush quotas and blow away the quotainfo structure. 271 */ 272 ASSERT(mp->m_rootip); 273 xfs_qm_dqdetach(mp->m_rootip); 274 275 /* 276 * For pre-RTG file systems, the RT inodes have quotas attached, 277 * detach them now. 278 */ 279 if (!xfs_has_rtgroups(mp)) 280 xfs_qm_unmount_rt(mp); 281 282 /* 283 * Release the quota inodes. 284 */ 285 if (mp->m_quotainfo) 286 xfs_qm_destroy_quotainos(mp->m_quotainfo); 287 } 288 289 static bool 290 xfs_qm_need_dqattach( 291 struct xfs_inode *ip) 292 { 293 struct xfs_mount *mp = ip->i_mount; 294 295 if (!XFS_IS_QUOTA_ON(mp)) 296 return false; 297 if (!XFS_NOT_DQATTACHED(mp, ip)) 298 return false; 299 if (xfs_is_quota_inode(&mp->m_sb, I_INO(ip))) 300 return false; 301 if (xfs_is_metadir_inode(ip)) 302 return false; 303 return true; 304 } 305 306 /* 307 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON 308 * into account. 309 * If @doalloc is true, the dquot(s) will be allocated if needed. 310 * Inode may get unlocked and relocked in here, and the caller must deal with 311 * the consequences. 312 */ 313 int 314 xfs_qm_dqattach_locked( 315 xfs_inode_t *ip, 316 bool doalloc) 317 { 318 xfs_mount_t *mp = ip->i_mount; 319 int error = 0; 320 321 if (!xfs_qm_need_dqattach(ip)) 322 return 0; 323 324 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 325 ASSERT(!xfs_is_metadir_inode(ip)); 326 327 if (XFS_IS_UQUOTA_ON(mp) && !ip->i_udquot) { 328 error = xfs_qm_dqget_inode(ip, XFS_DQTYPE_USER, 329 doalloc, &ip->i_udquot); 330 if (error) 331 goto done; 332 ASSERT(ip->i_udquot); 333 } 334 335 if (XFS_IS_GQUOTA_ON(mp) && !ip->i_gdquot) { 336 error = xfs_qm_dqget_inode(ip, XFS_DQTYPE_GROUP, 337 doalloc, &ip->i_gdquot); 338 if (error) 339 goto done; 340 ASSERT(ip->i_gdquot); 341 } 342 343 if (XFS_IS_PQUOTA_ON(mp) && !ip->i_pdquot) { 344 error = xfs_qm_dqget_inode(ip, XFS_DQTYPE_PROJ, 345 doalloc, &ip->i_pdquot); 346 if (error) 347 goto done; 348 ASSERT(ip->i_pdquot); 349 } 350 351 done: 352 /* 353 * Don't worry about the dquots that we may have attached before any 354 * error - they'll get detached later if it has not already been done. 355 */ 356 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 357 return error; 358 } 359 360 int 361 xfs_qm_dqattach( 362 struct xfs_inode *ip) 363 { 364 int error; 365 366 if (!xfs_qm_need_dqattach(ip)) 367 return 0; 368 369 xfs_ilock(ip, XFS_ILOCK_EXCL); 370 error = xfs_qm_dqattach_locked(ip, false); 371 xfs_iunlock(ip, XFS_ILOCK_EXCL); 372 373 return error; 374 } 375 376 /* 377 * Release dquots (and their references) if any. 378 * The inode should be locked EXCL except when this's called by 379 * xfs_ireclaim. 380 */ 381 void 382 xfs_qm_dqdetach( 383 xfs_inode_t *ip) 384 { 385 if (xfs_is_metadir_inode(ip)) 386 return; 387 if (!(ip->i_udquot || ip->i_gdquot || ip->i_pdquot)) 388 return; 389 390 trace_xfs_dquot_dqdetach(ip); 391 392 ASSERT(!xfs_is_quota_inode(&ip->i_mount->m_sb, I_INO(ip))); 393 if (ip->i_udquot) { 394 xfs_qm_dqrele(ip->i_udquot); 395 ip->i_udquot = NULL; 396 } 397 if (ip->i_gdquot) { 398 xfs_qm_dqrele(ip->i_gdquot); 399 ip->i_gdquot = NULL; 400 } 401 if (ip->i_pdquot) { 402 xfs_qm_dqrele(ip->i_pdquot); 403 ip->i_pdquot = NULL; 404 } 405 } 406 407 struct xfs_qm_isolate { 408 struct list_head buffers; 409 struct list_head dispose; 410 }; 411 412 static enum lru_status 413 xfs_qm_dquot_isolate( 414 struct list_head *item, 415 struct list_lru_one *lru, 416 void *arg) 417 __releases(&lru->lock) __acquires(&lru->lock) 418 { 419 struct xfs_dquot *dqp = container_of(item, 420 struct xfs_dquot, q_lru); 421 struct xfs_qm_isolate *isol = arg; 422 enum lru_status ret = LRU_SKIP; 423 424 if (!spin_trylock(&dqp->q_lockref.lock)) 425 goto out_miss_busy; 426 427 /* 428 * If something else is freeing this dquot and hasn't yet removed it 429 * from the LRU, leave it for the freeing task to complete the freeing 430 * process rather than risk it being free from under us here. 431 */ 432 if (lockref_is_dead(&dqp->q_lockref)) 433 goto out_miss_unlock; 434 435 /* 436 * If the dquot is pinned or dirty, rotate it to the end of the LRU to 437 * give some time for it to be cleaned before we try to isolate it 438 * again. 439 */ 440 ret = LRU_ROTATE; 441 if (XFS_DQ_IS_DIRTY(dqp) || atomic_read(&dqp->q_pincount) > 0) 442 goto out_miss_unlock; 443 444 /* 445 * This dquot has acquired a reference in the meantime remove it from 446 * the freelist and try again. 447 */ 448 if (dqp->q_lockref.count) { 449 spin_unlock(&dqp->q_lockref.lock); 450 XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants); 451 452 trace_xfs_dqreclaim_want(dqp); 453 list_lru_isolate(lru, &dqp->q_lru); 454 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused); 455 return LRU_REMOVED; 456 } 457 458 /* 459 * The dquot may still be under IO, in which case the flush lock will be 460 * held. If we can't get the flush lock now, just skip over the dquot as 461 * if it was dirty. 462 */ 463 if (!xfs_dqflock_nowait(dqp)) 464 goto out_miss_unlock; 465 466 ASSERT(!XFS_DQ_IS_DIRTY(dqp)); 467 xfs_dquot_detach_buf(dqp); 468 xfs_dqfunlock(dqp); 469 470 /* 471 * Prevent lookups now that we are past the point of no return. 472 */ 473 lockref_mark_dead(&dqp->q_lockref); 474 spin_unlock(&dqp->q_lockref.lock); 475 476 list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose); 477 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused); 478 trace_xfs_dqreclaim_done(dqp); 479 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims); 480 return LRU_REMOVED; 481 482 out_miss_unlock: 483 spin_unlock(&dqp->q_lockref.lock); 484 out_miss_busy: 485 trace_xfs_dqreclaim_busy(dqp); 486 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses); 487 return ret; 488 } 489 490 static unsigned long 491 xfs_qm_shrink_scan( 492 struct shrinker *shrink, 493 struct shrink_control *sc) 494 { 495 struct xfs_quotainfo *qi = shrink->private_data; 496 struct xfs_qm_isolate isol; 497 unsigned long freed; 498 int error; 499 500 if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM)) 501 return 0; 502 503 INIT_LIST_HEAD(&isol.buffers); 504 INIT_LIST_HEAD(&isol.dispose); 505 506 freed = list_lru_shrink_walk(&qi->qi_lru, sc, 507 xfs_qm_dquot_isolate, &isol); 508 509 error = xfs_buf_delwri_submit(&isol.buffers); 510 if (error) 511 xfs_warn(NULL, "%s: dquot reclaim failed", __func__); 512 513 while (!list_empty(&isol.dispose)) { 514 struct xfs_dquot *dqp; 515 516 dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru); 517 list_del_init(&dqp->q_lru); 518 xfs_qm_dqfree_one(dqp); 519 } 520 521 return freed; 522 } 523 524 static unsigned long 525 xfs_qm_shrink_count( 526 struct shrinker *shrink, 527 struct shrink_control *sc) 528 { 529 struct xfs_quotainfo *qi = shrink->private_data; 530 531 return list_lru_shrink_count(&qi->qi_lru, sc); 532 } 533 534 STATIC void 535 xfs_qm_set_defquota( 536 struct xfs_mount *mp, 537 xfs_dqtype_t type, 538 struct xfs_quotainfo *qinf) 539 { 540 struct xfs_dquot *dqp; 541 struct xfs_def_quota *defq; 542 int error; 543 544 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp); 545 if (error) 546 return; 547 548 defq = xfs_get_defquota(qinf, xfs_dquot_type(dqp)); 549 550 /* 551 * Timers and warnings have been already set, let's just set the 552 * default limits for this quota type 553 */ 554 defq->blk.hard = dqp->q_blk.hardlimit; 555 defq->blk.soft = dqp->q_blk.softlimit; 556 defq->ino.hard = dqp->q_ino.hardlimit; 557 defq->ino.soft = dqp->q_ino.softlimit; 558 defq->rtb.hard = dqp->q_rtb.hardlimit; 559 defq->rtb.soft = dqp->q_rtb.softlimit; 560 xfs_qm_dqdestroy(dqp); 561 } 562 563 /* Initialize quota time limits from the root dquot. */ 564 static void 565 xfs_qm_init_timelimits( 566 struct xfs_mount *mp, 567 xfs_dqtype_t type) 568 { 569 struct xfs_quotainfo *qinf = mp->m_quotainfo; 570 struct xfs_def_quota *defq; 571 struct xfs_dquot *dqp; 572 int error; 573 574 defq = xfs_get_defquota(qinf, type); 575 576 defq->blk.time = XFS_QM_BTIMELIMIT; 577 defq->ino.time = XFS_QM_ITIMELIMIT; 578 defq->rtb.time = XFS_QM_RTBTIMELIMIT; 579 580 /* 581 * We try to get the limits from the superuser's limits fields. 582 * This is quite hacky, but it is standard quota practice. 583 * 584 * Since we may not have done a quotacheck by this point, just read 585 * the dquot without attaching it to any hashtables or lists. 586 */ 587 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp); 588 if (error) 589 return; 590 591 /* 592 * The warnings and timers set the grace period given to 593 * a user or group before he or she can not perform any 594 * more writing. If it is zero, a default is used. 595 */ 596 if (dqp->q_blk.timer) 597 defq->blk.time = dqp->q_blk.timer; 598 if (dqp->q_ino.timer) 599 defq->ino.time = dqp->q_ino.timer; 600 if (dqp->q_rtb.timer) 601 defq->rtb.time = dqp->q_rtb.timer; 602 603 xfs_qm_dqdestroy(dqp); 604 } 605 606 static int 607 xfs_qm_load_metadir_qinos( 608 struct xfs_mount *mp, 609 struct xfs_quotainfo *qi) 610 { 611 struct xfs_trans *tp; 612 int error; 613 614 tp = xfs_trans_alloc_empty(mp); 615 error = xfs_dqinode_load_parent(tp, &qi->qi_dirip); 616 if (error == -ENOENT) { 617 /* no quota dir directory, but we'll create one later */ 618 error = 0; 619 goto out_trans; 620 } 621 if (error) 622 goto out_trans; 623 624 if (XFS_IS_UQUOTA_ON(mp)) { 625 error = xfs_dqinode_load(tp, qi->qi_dirip, XFS_DQTYPE_USER, 626 &qi->qi_uquotaip); 627 if (error && error != -ENOENT) 628 goto out_trans; 629 } 630 631 if (XFS_IS_GQUOTA_ON(mp)) { 632 error = xfs_dqinode_load(tp, qi->qi_dirip, XFS_DQTYPE_GROUP, 633 &qi->qi_gquotaip); 634 if (error && error != -ENOENT) 635 goto out_trans; 636 } 637 638 if (XFS_IS_PQUOTA_ON(mp)) { 639 error = xfs_dqinode_load(tp, qi->qi_dirip, XFS_DQTYPE_PROJ, 640 &qi->qi_pquotaip); 641 if (error && error != -ENOENT) 642 goto out_trans; 643 } 644 645 error = 0; 646 out_trans: 647 xfs_trans_cancel(tp); 648 return error; 649 } 650 651 /* Create quota inodes in the metadata directory tree. */ 652 STATIC int 653 xfs_qm_create_metadir_qinos( 654 struct xfs_mount *mp, 655 struct xfs_quotainfo *qi) 656 { 657 int error; 658 659 if (!qi->qi_dirip) { 660 error = xfs_dqinode_mkdir_parent(mp, &qi->qi_dirip); 661 if (error && error != -EEXIST) 662 return error; 663 /* 664 * If the /quotas dirent points to an inode that isn't 665 * loadable, qi_dirip will be NULL but mkdir_parent will return 666 * -EEXIST. In this case the metadir is corrupt, so bail out. 667 */ 668 if (XFS_IS_CORRUPT(mp, qi->qi_dirip == NULL)) 669 return -EFSCORRUPTED; 670 } 671 672 if (XFS_IS_UQUOTA_ON(mp) && !qi->qi_uquotaip) { 673 error = xfs_dqinode_metadir_create(qi->qi_dirip, 674 XFS_DQTYPE_USER, &qi->qi_uquotaip); 675 if (error) 676 return error; 677 } 678 679 if (XFS_IS_GQUOTA_ON(mp) && !qi->qi_gquotaip) { 680 error = xfs_dqinode_metadir_create(qi->qi_dirip, 681 XFS_DQTYPE_GROUP, &qi->qi_gquotaip); 682 if (error) 683 return error; 684 } 685 686 if (XFS_IS_PQUOTA_ON(mp) && !qi->qi_pquotaip) { 687 error = xfs_dqinode_metadir_create(qi->qi_dirip, 688 XFS_DQTYPE_PROJ, &qi->qi_pquotaip); 689 if (error) 690 return error; 691 } 692 693 return 0; 694 } 695 696 /* 697 * Add QUOTABIT to sb_versionnum and initialize qflags in preparation for 698 * creating quota files on a metadir filesystem. 699 */ 700 STATIC int 701 xfs_qm_prep_metadir_sb( 702 struct xfs_mount *mp) 703 { 704 struct xfs_trans *tp; 705 int error; 706 707 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp); 708 if (error) 709 return error; 710 711 spin_lock(&mp->m_sb_lock); 712 713 xfs_add_quota(mp); 714 715 /* qflags will get updated fully _after_ quotacheck */ 716 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT; 717 718 spin_unlock(&mp->m_sb_lock); 719 xfs_log_sb(tp); 720 721 return xfs_trans_commit(tp); 722 } 723 724 /* 725 * Load existing quota inodes or create them. Since this is a V5 filesystem, 726 * we don't have to deal with the grp/prjquota switcheroo thing from V4. 727 */ 728 STATIC int 729 xfs_qm_init_metadir_qinos( 730 struct xfs_mount *mp) 731 { 732 struct xfs_quotainfo *qi = mp->m_quotainfo; 733 int error; 734 735 if (!xfs_has_quota(mp)) { 736 error = xfs_qm_prep_metadir_sb(mp); 737 if (error) 738 return error; 739 } 740 741 error = xfs_qm_load_metadir_qinos(mp, qi); 742 if (error) 743 goto out_err; 744 745 error = xfs_qm_create_metadir_qinos(mp, qi); 746 if (error) 747 goto out_err; 748 749 /* The only user of the quota dir inode is online fsck */ 750 #if !IS_ENABLED(CONFIG_XFS_ONLINE_SCRUB) 751 xfs_irele(qi->qi_dirip); 752 qi->qi_dirip = NULL; 753 #endif 754 return 0; 755 out_err: 756 xfs_qm_destroy_quotainos(mp->m_quotainfo); 757 return error; 758 } 759 760 /* 761 * This initializes all the quota information that's kept in the 762 * mount structure 763 */ 764 STATIC int 765 xfs_qm_init_quotainfo( 766 struct xfs_mount *mp) 767 { 768 struct xfs_quotainfo *qinf; 769 int error; 770 771 ASSERT(XFS_IS_QUOTA_ON(mp)); 772 773 qinf = mp->m_quotainfo = kzalloc_obj(struct xfs_quotainfo, 774 GFP_KERNEL | __GFP_NOFAIL); 775 776 error = list_lru_init(&qinf->qi_lru); 777 if (error) 778 goto out_free_qinf; 779 780 /* 781 * See if quotainodes are setup, and if not, allocate them, 782 * and change the superblock accordingly. 783 */ 784 if (xfs_has_metadir(mp)) 785 error = xfs_qm_init_metadir_qinos(mp); 786 else 787 error = xfs_qm_init_quotainos(mp); 788 if (error) 789 goto out_free_lru; 790 791 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_KERNEL); 792 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_KERNEL); 793 INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_KERNEL); 794 mutex_init(&qinf->qi_tree_lock); 795 796 /* mutex used to serialize quotaoffs */ 797 mutex_init(&qinf->qi_quotaofflock); 798 799 /* Precalc some constants */ 800 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB); 801 qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen); 802 if (xfs_has_bigtime(mp)) { 803 qinf->qi_expiry_min = 804 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN); 805 qinf->qi_expiry_max = 806 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX); 807 } else { 808 qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN; 809 qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX; 810 } 811 trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min, 812 qinf->qi_expiry_max); 813 814 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD); 815 816 xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER); 817 xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP); 818 xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ); 819 820 if (XFS_IS_UQUOTA_ON(mp)) 821 xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf); 822 if (XFS_IS_GQUOTA_ON(mp)) 823 xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf); 824 if (XFS_IS_PQUOTA_ON(mp)) 825 xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf); 826 827 qinf->qi_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "xfs-qm:%s", 828 mp->m_super->s_id); 829 if (!qinf->qi_shrinker) { 830 error = -ENOMEM; 831 goto out_free_inos; 832 } 833 834 qinf->qi_shrinker->count_objects = xfs_qm_shrink_count; 835 qinf->qi_shrinker->scan_objects = xfs_qm_shrink_scan; 836 qinf->qi_shrinker->private_data = qinf; 837 838 shrinker_register(qinf->qi_shrinker); 839 840 xfs_hooks_init(&qinf->qi_mod_ino_dqtrx_hooks); 841 xfs_hooks_init(&qinf->qi_apply_dqtrx_hooks); 842 843 return 0; 844 845 out_free_inos: 846 mutex_destroy(&qinf->qi_quotaofflock); 847 mutex_destroy(&qinf->qi_tree_lock); 848 xfs_qm_destroy_quotainos(qinf); 849 out_free_lru: 850 list_lru_destroy(&qinf->qi_lru); 851 out_free_qinf: 852 kfree(qinf); 853 mp->m_quotainfo = NULL; 854 return error; 855 } 856 857 /* 858 * Gets called when unmounting a filesystem or when all quotas get 859 * turned off. 860 * This purges the quota inodes, destroys locks and frees itself. 861 */ 862 void 863 xfs_qm_destroy_quotainfo( 864 struct xfs_mount *mp) 865 { 866 struct xfs_quotainfo *qi; 867 868 qi = mp->m_quotainfo; 869 ASSERT(qi != NULL); 870 871 shrinker_free(qi->qi_shrinker); 872 list_lru_destroy(&qi->qi_lru); 873 xfs_qm_destroy_quotainos(qi); 874 mutex_destroy(&qi->qi_tree_lock); 875 mutex_destroy(&qi->qi_quotaofflock); 876 kfree(qi); 877 mp->m_quotainfo = NULL; 878 } 879 880 static inline enum xfs_metafile_type 881 xfs_qm_metafile_type( 882 unsigned int flags) 883 { 884 if (flags & XFS_QMOPT_UQUOTA) 885 return XFS_METAFILE_USRQUOTA; 886 else if (flags & XFS_QMOPT_GQUOTA) 887 return XFS_METAFILE_GRPQUOTA; 888 return XFS_METAFILE_PRJQUOTA; 889 } 890 891 /* 892 * Create an inode and return with a reference already taken, but unlocked 893 * This is how we create quota inodes 894 */ 895 STATIC int 896 xfs_qm_qino_alloc( 897 struct xfs_mount *mp, 898 struct xfs_inode **ipp, 899 unsigned int flags) 900 { 901 struct xfs_trans *tp; 902 enum xfs_metafile_type metafile_type = xfs_qm_metafile_type(flags); 903 int error; 904 bool need_alloc = true; 905 906 *ipp = NULL; 907 /* 908 * With superblock that doesn't have separate pquotino, we 909 * share an inode between gquota and pquota. If the on-disk 910 * superblock has GQUOTA and the filesystem is now mounted 911 * with PQUOTA, just use sb_gquotino for sb_pquotino and 912 * vice-versa. 913 */ 914 if (!xfs_has_pquotino(mp) && 915 (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) { 916 xfs_ino_t ino = NULLFSINO; 917 918 if ((flags & XFS_QMOPT_PQUOTA) && 919 (mp->m_sb.sb_gquotino != NULLFSINO)) { 920 ino = mp->m_sb.sb_gquotino; 921 if (XFS_IS_CORRUPT(mp, 922 mp->m_sb.sb_pquotino != NULLFSINO)) { 923 xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA); 924 return -EFSCORRUPTED; 925 } 926 } else if ((flags & XFS_QMOPT_GQUOTA) && 927 (mp->m_sb.sb_pquotino != NULLFSINO)) { 928 ino = mp->m_sb.sb_pquotino; 929 if (XFS_IS_CORRUPT(mp, 930 mp->m_sb.sb_gquotino != NULLFSINO)) { 931 xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA); 932 return -EFSCORRUPTED; 933 } 934 } 935 if (ino != NULLFSINO) { 936 error = xfs_metafile_iget(mp, ino, metafile_type, ipp); 937 if (error) 938 return error; 939 940 mp->m_sb.sb_gquotino = NULLFSINO; 941 mp->m_sb.sb_pquotino = NULLFSINO; 942 need_alloc = false; 943 } 944 } 945 946 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create, 947 need_alloc ? XFS_QM_QINOCREATE_SPACE_RES(mp) : 0, 948 0, 0, &tp); 949 if (error) 950 return error; 951 952 if (need_alloc) { 953 struct xfs_icreate_args args = { 954 .mode = S_IFREG, 955 .flags = XFS_ICREATE_UNLINKABLE, 956 }; 957 xfs_ino_t ino; 958 959 error = xfs_dialloc(&tp, &args, &ino); 960 if (!error) 961 error = xfs_icreate(tp, ino, &args, ipp); 962 if (error) { 963 xfs_trans_cancel(tp); 964 return error; 965 } 966 if (xfs_has_metadir(mp)) 967 xfs_metafile_set_iflag(tp, *ipp, metafile_type); 968 } 969 970 /* 971 * Make the changes in the superblock, and log those too. 972 * sbfields arg may contain fields other than *QUOTINO; 973 * VERSIONNUM for example. 974 */ 975 spin_lock(&mp->m_sb_lock); 976 if (flags & XFS_QMOPT_SBVERSION) { 977 ASSERT(!xfs_has_quota(mp)); 978 979 xfs_add_quota(mp); 980 mp->m_sb.sb_uquotino = NULLFSINO; 981 mp->m_sb.sb_gquotino = NULLFSINO; 982 mp->m_sb.sb_pquotino = NULLFSINO; 983 984 /* qflags will get updated fully _after_ quotacheck */ 985 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT; 986 } 987 if (flags & XFS_QMOPT_UQUOTA) 988 mp->m_sb.sb_uquotino = I_INO(*ipp); 989 else if (flags & XFS_QMOPT_GQUOTA) 990 mp->m_sb.sb_gquotino = I_INO(*ipp); 991 else 992 mp->m_sb.sb_pquotino = I_INO(*ipp); 993 spin_unlock(&mp->m_sb_lock); 994 xfs_log_sb(tp); 995 996 error = xfs_trans_commit(tp); 997 if (error) { 998 ASSERT(xfs_is_shutdown(mp)); 999 xfs_alert(mp, "%s failed (error %d)!", __func__, error); 1000 } 1001 if (need_alloc) { 1002 xfs_iunlock(*ipp, XFS_ILOCK_EXCL); 1003 xfs_finish_inode_setup(*ipp); 1004 } 1005 return error; 1006 } 1007 1008 1009 STATIC void 1010 xfs_qm_reset_dqcounts( 1011 struct xfs_mount *mp, 1012 struct xfs_buf *bp, 1013 xfs_dqid_t id, 1014 xfs_dqtype_t type) 1015 { 1016 struct xfs_dqblk *dqb; 1017 int j; 1018 1019 trace_xfs_reset_dqcounts(bp, _RET_IP_); 1020 1021 /* 1022 * Reset all counters and timers. They'll be 1023 * started afresh by xfs_qm_quotacheck. 1024 */ 1025 #ifdef DEBUG 1026 j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) / 1027 sizeof(struct xfs_dqblk); 1028 ASSERT(mp->m_quotainfo->qi_dqperchunk == j); 1029 #endif 1030 dqb = bp->b_addr; 1031 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) { 1032 struct xfs_disk_dquot *ddq; 1033 1034 ddq = (struct xfs_disk_dquot *)&dqb[j]; 1035 1036 /* 1037 * Do a sanity check, and if needed, repair the dqblk. Don't 1038 * output any warnings because it's perfectly possible to 1039 * find uninitialised dquot blks. See comment in 1040 * xfs_dquot_verify. 1041 */ 1042 if (xfs_dqblk_verify(mp, &dqb[j], id + j) || 1043 (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type) 1044 xfs_dqblk_repair(mp, &dqb[j], id + j, type); 1045 1046 /* 1047 * Reset type in case we are reusing group quota file for 1048 * project quotas or vice versa 1049 */ 1050 ddq->d_type = type; 1051 ddq->d_bcount = 0; 1052 ddq->d_icount = 0; 1053 ddq->d_rtbcount = 0; 1054 1055 /* 1056 * dquot id 0 stores the default grace period and the maximum 1057 * warning limit that were set by the administrator, so we 1058 * should not reset them. 1059 */ 1060 if (ddq->d_id != 0) { 1061 ddq->d_btimer = 0; 1062 ddq->d_itimer = 0; 1063 ddq->d_rtbtimer = 0; 1064 ddq->d_bwarns = 0; 1065 ddq->d_iwarns = 0; 1066 ddq->d_rtbwarns = 0; 1067 if (xfs_has_bigtime(mp)) 1068 ddq->d_type |= XFS_DQTYPE_BIGTIME; 1069 } 1070 1071 if (xfs_has_crc(mp)) { 1072 xfs_update_cksum((char *)&dqb[j], 1073 sizeof(struct xfs_dqblk), 1074 XFS_DQUOT_CRC_OFF); 1075 } 1076 } 1077 } 1078 1079 STATIC int 1080 xfs_qm_reset_dqcounts_all( 1081 struct xfs_mount *mp, 1082 xfs_dqid_t firstid, 1083 xfs_fsblock_t bno, 1084 xfs_filblks_t blkcnt, 1085 xfs_dqtype_t type, 1086 struct list_head *buffer_list) 1087 { 1088 struct xfs_buf *bp; 1089 int error = 0; 1090 1091 ASSERT(blkcnt > 0); 1092 1093 /* 1094 * Blkcnt arg can be a very big number, and might even be 1095 * larger than the log itself. So, we have to break it up into 1096 * manageable-sized transactions. 1097 * Note that we don't start a permanent transaction here; we might 1098 * not be able to get a log reservation for the whole thing up front, 1099 * and we don't really care to either, because we just discard 1100 * everything if we were to crash in the middle of this loop. 1101 */ 1102 while (blkcnt--) { 1103 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, 1104 XFS_FSB_TO_DADDR(mp, bno), 1105 mp->m_quotainfo->qi_dqchunklen, 0, &bp, 1106 &xfs_dquot_buf_ops); 1107 1108 /* 1109 * CRC and validation errors will return a EFSCORRUPTED here. If 1110 * this occurs, re-read without CRC validation so that we can 1111 * repair the damage via xfs_qm_reset_dqcounts(). This process 1112 * will leave a trace in the log indicating corruption has 1113 * been detected. 1114 */ 1115 if (error == -EFSCORRUPTED) { 1116 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, 1117 XFS_FSB_TO_DADDR(mp, bno), 1118 mp->m_quotainfo->qi_dqchunklen, 0, &bp, 1119 NULL); 1120 } 1121 1122 if (error) 1123 break; 1124 1125 /* 1126 * A corrupt buffer might not have a verifier attached, so 1127 * make sure we have the correct one attached before writeback 1128 * occurs. 1129 */ 1130 bp->b_ops = &xfs_dquot_buf_ops; 1131 xfs_qm_reset_dqcounts(mp, bp, firstid, type); 1132 xfs_buf_delwri_queue(bp, buffer_list); 1133 xfs_buf_relse(bp); 1134 1135 /* goto the next block. */ 1136 bno++; 1137 firstid += mp->m_quotainfo->qi_dqperchunk; 1138 } 1139 1140 return error; 1141 } 1142 1143 /* 1144 * Iterate over all allocated dquot blocks in this quota inode, zeroing all 1145 * counters for every chunk of dquots that we find. 1146 */ 1147 STATIC int 1148 xfs_qm_reset_dqcounts_buf( 1149 struct xfs_mount *mp, 1150 struct xfs_inode *qip, 1151 xfs_dqtype_t type, 1152 struct list_head *buffer_list) 1153 { 1154 struct xfs_bmbt_irec *map; 1155 int i, nmaps; /* number of map entries */ 1156 int error; /* return value */ 1157 xfs_fileoff_t lblkno; 1158 xfs_filblks_t maxlblkcnt; 1159 xfs_dqid_t firstid; 1160 xfs_fsblock_t rablkno; 1161 xfs_filblks_t rablkcnt; 1162 1163 error = 0; 1164 /* 1165 * This looks racy, but we can't keep an inode lock across a 1166 * trans_reserve. But, this gets called during quotacheck, and that 1167 * happens only at mount time which is single threaded. 1168 */ 1169 if (qip->i_nblocks == 0) 1170 return 0; 1171 1172 map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 1173 GFP_KERNEL | __GFP_NOFAIL); 1174 1175 lblkno = 0; 1176 maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); 1177 do { 1178 uint lock_mode; 1179 1180 nmaps = XFS_DQITER_MAP_SIZE; 1181 /* 1182 * We aren't changing the inode itself. Just changing 1183 * some of its data. No new blocks are added here, and 1184 * the inode is never added to the transaction. 1185 */ 1186 lock_mode = xfs_ilock_data_map_shared(qip); 1187 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno, 1188 map, &nmaps, 0); 1189 xfs_iunlock(qip, lock_mode); 1190 if (error) 1191 break; 1192 1193 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE); 1194 for (i = 0; i < nmaps; i++) { 1195 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK); 1196 ASSERT(map[i].br_blockcount); 1197 1198 1199 lblkno += map[i].br_blockcount; 1200 1201 if (map[i].br_startblock == HOLESTARTBLOCK) 1202 continue; 1203 1204 firstid = (xfs_dqid_t) map[i].br_startoff * 1205 mp->m_quotainfo->qi_dqperchunk; 1206 /* 1207 * Do a read-ahead on the next extent. 1208 */ 1209 if ((i+1 < nmaps) && 1210 (map[i+1].br_startblock != HOLESTARTBLOCK)) { 1211 rablkcnt = map[i+1].br_blockcount; 1212 rablkno = map[i+1].br_startblock; 1213 while (rablkcnt--) { 1214 xfs_buf_readahead(mp->m_ddev_targp, 1215 XFS_FSB_TO_DADDR(mp, rablkno), 1216 mp->m_quotainfo->qi_dqchunklen, 1217 &xfs_dquot_buf_ops); 1218 rablkno++; 1219 } 1220 } 1221 /* 1222 * Iterate thru all the blks in the extent and 1223 * reset the counters of all the dquots inside them. 1224 */ 1225 error = xfs_qm_reset_dqcounts_all(mp, firstid, 1226 map[i].br_startblock, 1227 map[i].br_blockcount, 1228 type, buffer_list); 1229 if (error) 1230 goto out; 1231 } 1232 } while (nmaps > 0); 1233 1234 out: 1235 kfree(map); 1236 return error; 1237 } 1238 1239 /* 1240 * Called by dqusage_adjust in doing a quotacheck. 1241 * 1242 * Given the inode, and a dquot id this updates both the incore dqout as well 1243 * as the buffer copy. This is so that once the quotacheck is done, we can 1244 * just log all the buffers, as opposed to logging numerous updates to 1245 * individual dquots. 1246 */ 1247 STATIC int 1248 xfs_qm_quotacheck_dqadjust( 1249 struct xfs_inode *ip, 1250 xfs_dqtype_t type, 1251 xfs_qcnt_t nblks, 1252 xfs_qcnt_t rtblks) 1253 { 1254 struct xfs_mount *mp = ip->i_mount; 1255 struct xfs_dquot *dqp; 1256 xfs_dqid_t id; 1257 int error; 1258 1259 id = xfs_qm_id_for_quotatype(ip, type); 1260 error = xfs_qm_dqget(mp, id, type, true, &dqp); 1261 if (error) { 1262 /* 1263 * Shouldn't be able to turn off quotas here. 1264 */ 1265 ASSERT(error != -ESRCH); 1266 ASSERT(error != -ENOENT); 1267 return error; 1268 } 1269 1270 mutex_lock(&dqp->q_qlock); 1271 error = xfs_dquot_attach_buf(NULL, dqp); 1272 if (error) 1273 goto out_unlock; 1274 1275 trace_xfs_dqadjust(dqp); 1276 1277 /* 1278 * Adjust the inode count and the block count to reflect this inode's 1279 * resource usage. 1280 */ 1281 dqp->q_ino.count++; 1282 dqp->q_ino.reserved++; 1283 if (nblks) { 1284 dqp->q_blk.count += nblks; 1285 dqp->q_blk.reserved += nblks; 1286 } 1287 if (rtblks) { 1288 dqp->q_rtb.count += rtblks; 1289 dqp->q_rtb.reserved += rtblks; 1290 } 1291 1292 /* 1293 * Set default limits, adjust timers (since we changed usages) 1294 * 1295 * There are no timers for the default values set in the root dquot. 1296 */ 1297 if (dqp->q_id) { 1298 xfs_qm_adjust_dqlimits(dqp); 1299 xfs_qm_adjust_dqtimers(dqp); 1300 } 1301 1302 dqp->q_flags |= XFS_DQFLAG_DIRTY; 1303 out_unlock: 1304 mutex_unlock(&dqp->q_qlock); 1305 xfs_qm_dqrele(dqp); 1306 return error; 1307 } 1308 1309 /* 1310 * callback routine supplied to bulkstat(). Given an inumber, find its 1311 * dquots and update them to account for resources taken by that inode. 1312 */ 1313 /* ARGSUSED */ 1314 STATIC int 1315 xfs_qm_dqusage_adjust( 1316 struct xfs_mount *mp, 1317 struct xfs_trans *tp, 1318 xfs_ino_t ino, 1319 void *data) 1320 { 1321 struct xfs_inode *ip; 1322 xfs_filblks_t nblks, rtblks; 1323 unsigned int lock_mode; 1324 int error; 1325 1326 ASSERT(XFS_IS_QUOTA_ON(mp)); 1327 1328 /* 1329 * rootino must have its resources accounted for, not so with the quota 1330 * inodes. 1331 */ 1332 if (xfs_is_quota_inode(&mp->m_sb, ino)) 1333 return 0; 1334 1335 /* 1336 * We don't _need_ to take the ilock EXCL here because quotacheck runs 1337 * at mount time and therefore nobody will be racing chown/chproj. 1338 */ 1339 error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip); 1340 if (error == -EINVAL || error == -ENOENT) 1341 return 0; 1342 if (error) 1343 return error; 1344 1345 /* 1346 * Reload the incore unlinked list to avoid failure in inodegc. 1347 * Use an unlocked check here because unrecovered unlinked inodes 1348 * should be somewhat rare. 1349 */ 1350 if (xfs_inode_unlinked_incomplete(ip)) { 1351 error = xfs_inode_reload_unlinked(ip); 1352 if (error) { 1353 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 1354 goto error0; 1355 } 1356 } 1357 1358 /* Metadata directory files are not accounted to user-visible quotas. */ 1359 if (xfs_is_metadir_inode(ip)) 1360 goto error0; 1361 1362 ASSERT(ip->i_delayed_blks == 0); 1363 1364 lock_mode = xfs_ilock_data_map_shared(ip); 1365 if (XFS_IS_REALTIME_INODE(ip)) { 1366 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK); 1367 if (error) { 1368 xfs_iunlock(ip, lock_mode); 1369 goto error0; 1370 } 1371 } 1372 xfs_inode_count_blocks(tp, ip, &nblks, &rtblks); 1373 xfs_iflags_clear(ip, XFS_IQUOTAUNCHECKED); 1374 xfs_iunlock(ip, lock_mode); 1375 1376 /* 1377 * Add the (disk blocks and inode) resources occupied by this 1378 * inode to its dquots. We do this adjustment in the incore dquot, 1379 * and also copy the changes to its buffer. 1380 * We don't care about putting these changes in a transaction 1381 * envelope because if we crash in the middle of a 'quotacheck' 1382 * we have to start from the beginning anyway. 1383 * Once we're done, we'll log all the dquot bufs. 1384 * 1385 * The *QUOTA_ON checks below may look pretty racy, but quotachecks 1386 * and quotaoffs don't race. (Quotachecks happen at mount time only). 1387 */ 1388 if (XFS_IS_UQUOTA_ON(mp)) { 1389 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks, 1390 rtblks); 1391 if (error) 1392 goto error0; 1393 } 1394 1395 if (XFS_IS_GQUOTA_ON(mp)) { 1396 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks, 1397 rtblks); 1398 if (error) 1399 goto error0; 1400 } 1401 1402 if (XFS_IS_PQUOTA_ON(mp)) { 1403 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks, 1404 rtblks); 1405 if (error) 1406 goto error0; 1407 } 1408 1409 error0: 1410 xfs_irele(ip); 1411 return error; 1412 } 1413 1414 STATIC int 1415 xfs_qm_flush_one( 1416 struct xfs_dquot *dqp, 1417 void *data) 1418 { 1419 struct list_head *buffer_list = data; 1420 struct xfs_buf *bp = NULL; 1421 int error = 0; 1422 1423 if (!lockref_get_not_dead(&dqp->q_lockref)) 1424 return 0; 1425 1426 mutex_lock(&dqp->q_qlock); 1427 if (!XFS_DQ_IS_DIRTY(dqp)) 1428 goto out_unlock; 1429 1430 xfs_qm_dqunpin_wait(dqp); 1431 xfs_dqflock(dqp); 1432 1433 error = xfs_dquot_use_attached_buf(dqp, &bp); 1434 if (error) 1435 goto out_dqflock; 1436 if (!bp) { 1437 error = -EFSCORRUPTED; 1438 goto out_dqflock; 1439 } 1440 1441 error = xfs_qm_dqflush(dqp, bp); 1442 if (!error) 1443 xfs_buf_delwri_queue(bp, buffer_list); 1444 xfs_buf_relse(bp); 1445 mutex_unlock(&dqp->q_qlock); 1446 xfs_qm_dqrele(dqp); 1447 return error; 1448 1449 out_dqflock: 1450 xfs_dqfunlock(dqp); 1451 out_unlock: 1452 mutex_unlock(&dqp->q_qlock); 1453 xfs_qm_dqrele(dqp); 1454 return error; 1455 } 1456 1457 /* 1458 * Walk thru all the filesystem inodes and construct a consistent view 1459 * of the disk quota world. If the quotacheck fails, disable quotas. 1460 */ 1461 STATIC int 1462 xfs_qm_quotacheck( 1463 xfs_mount_t *mp) 1464 { 1465 int error, error2; 1466 uint flags; 1467 LIST_HEAD (buffer_list); 1468 struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip; 1469 struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip; 1470 struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip; 1471 1472 flags = 0; 1473 1474 ASSERT(uip || gip || pip); 1475 ASSERT(XFS_IS_QUOTA_ON(mp)); 1476 1477 xfs_notice(mp, "Quotacheck needed: Please wait."); 1478 1479 /* 1480 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset 1481 * their counters to zero. We need a clean slate. 1482 * We don't log our changes till later. 1483 */ 1484 if (uip) { 1485 error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER, 1486 &buffer_list); 1487 if (error) 1488 goto error_return; 1489 flags |= XFS_UQUOTA_CHKD; 1490 } 1491 1492 if (gip) { 1493 error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP, 1494 &buffer_list); 1495 if (error) 1496 goto error_return; 1497 flags |= XFS_GQUOTA_CHKD; 1498 } 1499 1500 if (pip) { 1501 error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ, 1502 &buffer_list); 1503 if (error) 1504 goto error_return; 1505 flags |= XFS_PQUOTA_CHKD; 1506 } 1507 1508 xfs_set_quotacheck_running(mp); 1509 error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true, 1510 NULL); 1511 xfs_clear_quotacheck_running(mp); 1512 1513 /* 1514 * On error, the inode walk may have partially populated the dquot 1515 * caches. We must purge them before disabling quota and tearing down 1516 * the quotainfo, or else the dquots will leak. 1517 */ 1518 if (error) 1519 goto error_purge; 1520 1521 /* 1522 * We've made all the changes that we need to make incore. Flush them 1523 * down to disk buffers if everything was updated successfully. 1524 */ 1525 if (XFS_IS_UQUOTA_ON(mp)) { 1526 error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one, 1527 &buffer_list); 1528 } 1529 if (XFS_IS_GQUOTA_ON(mp)) { 1530 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one, 1531 &buffer_list); 1532 if (!error) 1533 error = error2; 1534 } 1535 if (XFS_IS_PQUOTA_ON(mp)) { 1536 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one, 1537 &buffer_list); 1538 if (!error) 1539 error = error2; 1540 } 1541 1542 error2 = xfs_buf_delwri_submit(&buffer_list); 1543 if (!error) 1544 error = error2; 1545 1546 /* 1547 * We can get this error if we couldn't do a dquot allocation inside 1548 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the 1549 * dirty dquots that might be cached, we just want to get rid of them 1550 * and turn quotaoff. The dquots won't be attached to any of the inodes 1551 * at this point (because we intentionally didn't in dqget_noattach). 1552 */ 1553 if (error) 1554 goto error_purge; 1555 1556 /* 1557 * If one type of quotas is off, then it will lose its 1558 * quotachecked status, since we won't be doing accounting for 1559 * that type anymore. 1560 */ 1561 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD; 1562 mp->m_qflags |= flags; 1563 1564 error_return: 1565 xfs_buf_delwri_cancel(&buffer_list); 1566 1567 if (error) { 1568 xfs_warn(mp, 1569 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.", 1570 error); 1571 /* 1572 * We must turn off quotas. 1573 */ 1574 ASSERT(mp->m_quotainfo != NULL); 1575 xfs_qm_destroy_quotainfo(mp); 1576 if (xfs_mount_reset_sbqflags(mp)) { 1577 xfs_warn(mp, 1578 "Quotacheck: Failed to reset quota flags."); 1579 } 1580 xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK); 1581 } else { 1582 xfs_notice(mp, "Quotacheck: Done."); 1583 xfs_fs_mark_healthy(mp, XFS_SICK_FS_QUOTACHECK); 1584 } 1585 1586 return error; 1587 1588 error_purge: 1589 /* 1590 * On error, we may have inodes queued for inactivation. This may try 1591 * to attach dquots to the inode before running cleanup operations on 1592 * the inode and this can race with the xfs_qm_destroy_quotainfo() call 1593 * below that frees mp->m_quotainfo. To avoid this race, flush all the 1594 * pending inodegc operations before we purge the dquots from memory, 1595 * ensuring that background inactivation is idle whilst we turn off 1596 * quotas. 1597 */ 1598 xfs_inodegc_flush(mp); 1599 xfs_qm_dqpurge_all(mp); 1600 goto error_return; 1601 1602 } 1603 1604 /* 1605 * This is called from xfs_mountfs to start quotas and initialize all 1606 * necessary data structures like quotainfo. This is also responsible for 1607 * running a quotacheck as necessary. We are guaranteed that the superblock 1608 * is consistently read in at this point. 1609 * 1610 * If we fail here, the mount will continue with quota turned off. We don't 1611 * need to inidicate success or failure at all. 1612 */ 1613 void 1614 xfs_qm_mount_quotas( 1615 struct xfs_mount *mp) 1616 { 1617 int error = 0; 1618 uint sbf; 1619 1620 /* 1621 * If quotas on realtime volumes is not supported, disable quotas 1622 * immediately. We only support rtquota if rtgroups are enabled to 1623 * avoid problems with older kernels. 1624 */ 1625 if (mp->m_sb.sb_rextents && 1626 (!xfs_has_rtgroups(mp) || xfs_has_zoned(mp))) { 1627 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem"); 1628 mp->m_qflags = 0; 1629 goto write_changes; 1630 } 1631 1632 ASSERT(XFS_IS_QUOTA_ON(mp)); 1633 1634 /* 1635 * Allocate the quotainfo structure inside the mount struct, and 1636 * create quotainode(s), and change/rev superblock if necessary. 1637 */ 1638 error = xfs_qm_init_quotainfo(mp); 1639 if (error) { 1640 /* 1641 * We must turn off quotas. 1642 */ 1643 ASSERT(mp->m_quotainfo == NULL); 1644 mp->m_qflags = 0; 1645 goto write_changes; 1646 } 1647 /* 1648 * If any of the quotas are not consistent, do a quotacheck. 1649 */ 1650 if (XFS_QM_NEED_QUOTACHECK(mp)) { 1651 error = xfs_qm_quotacheck(mp); 1652 if (error) { 1653 /* Quotacheck failed and disabled quotas. */ 1654 return; 1655 } 1656 } 1657 /* 1658 * If one type of quotas is off, then it will lose its 1659 * quotachecked status, since we won't be doing accounting for 1660 * that type anymore. 1661 */ 1662 if (!XFS_IS_UQUOTA_ON(mp)) 1663 mp->m_qflags &= ~XFS_UQUOTA_CHKD; 1664 if (!XFS_IS_GQUOTA_ON(mp)) 1665 mp->m_qflags &= ~XFS_GQUOTA_CHKD; 1666 if (!XFS_IS_PQUOTA_ON(mp)) 1667 mp->m_qflags &= ~XFS_PQUOTA_CHKD; 1668 1669 write_changes: 1670 /* 1671 * We actually don't have to acquire the m_sb_lock at all. 1672 * This can only be called from mount, and that's single threaded. XXX 1673 */ 1674 spin_lock(&mp->m_sb_lock); 1675 sbf = mp->m_sb.sb_qflags; 1676 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL; 1677 spin_unlock(&mp->m_sb_lock); 1678 1679 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) { 1680 if (xfs_sync_sb(mp, false)) { 1681 /* 1682 * We could only have been turning quotas off. 1683 * We aren't in very good shape actually because 1684 * the incore structures are convinced that quotas are 1685 * off, but the on disk superblock doesn't know that ! 1686 */ 1687 ASSERT(!(XFS_IS_QUOTA_ON(mp))); 1688 xfs_alert(mp, "%s: Superblock update failed!", 1689 __func__); 1690 } 1691 } 1692 1693 if (error) { 1694 xfs_warn(mp, "Failed to initialize disk quotas, err %d.", error); 1695 return; 1696 } 1697 } 1698 1699 /* 1700 * Load the inode for a given type of quota, assuming that the sb fields have 1701 * been sorted out. This is not true when switching quota types on a V4 1702 * filesystem, so do not use this function for that. 1703 * 1704 * Returns -ENOENT if the quota inode field is NULLFSINO; 0 and an inode on 1705 * success; or a negative errno. 1706 */ 1707 int 1708 xfs_qm_qino_load( 1709 struct xfs_mount *mp, 1710 xfs_dqtype_t type, 1711 struct xfs_inode **ipp) 1712 { 1713 struct xfs_trans *tp; 1714 struct xfs_inode *dp = NULL; 1715 int error; 1716 1717 tp = xfs_trans_alloc_empty(mp); 1718 if (xfs_has_metadir(mp)) { 1719 error = xfs_dqinode_load_parent(tp, &dp); 1720 if (error) 1721 goto out_cancel; 1722 } 1723 1724 error = xfs_dqinode_load(tp, dp, type, ipp); 1725 if (dp) 1726 xfs_irele(dp); 1727 out_cancel: 1728 xfs_trans_cancel(tp); 1729 return error; 1730 } 1731 1732 /* 1733 * This is called after the superblock has been read in and we're ready to 1734 * iget the quota inodes. 1735 */ 1736 STATIC int 1737 xfs_qm_init_quotainos( 1738 xfs_mount_t *mp) 1739 { 1740 struct xfs_inode *uip = NULL; 1741 struct xfs_inode *gip = NULL; 1742 struct xfs_inode *pip = NULL; 1743 int error; 1744 uint flags = 0; 1745 1746 ASSERT(mp->m_quotainfo); 1747 1748 /* 1749 * Get the uquota and gquota inodes 1750 */ 1751 if (xfs_has_quota(mp)) { 1752 if (XFS_IS_UQUOTA_ON(mp) && 1753 mp->m_sb.sb_uquotino != NULLFSINO) { 1754 ASSERT(mp->m_sb.sb_uquotino > 0); 1755 error = xfs_qm_qino_load(mp, XFS_DQTYPE_USER, &uip); 1756 if (error) 1757 return error; 1758 } 1759 if (XFS_IS_GQUOTA_ON(mp) && 1760 mp->m_sb.sb_gquotino != NULLFSINO) { 1761 ASSERT(mp->m_sb.sb_gquotino > 0); 1762 error = xfs_qm_qino_load(mp, XFS_DQTYPE_GROUP, &gip); 1763 if (error) 1764 goto error_rele; 1765 } 1766 if (XFS_IS_PQUOTA_ON(mp) && 1767 mp->m_sb.sb_pquotino != NULLFSINO) { 1768 ASSERT(mp->m_sb.sb_pquotino > 0); 1769 error = xfs_qm_qino_load(mp, XFS_DQTYPE_PROJ, &pip); 1770 if (error) 1771 goto error_rele; 1772 } 1773 } else { 1774 flags |= XFS_QMOPT_SBVERSION; 1775 } 1776 1777 /* 1778 * Create the three inodes, if they don't exist already. The changes 1779 * made above will get added to a transaction and logged in one of 1780 * the qino_alloc calls below. If the device is readonly, 1781 * temporarily switch to read-write to do this. 1782 */ 1783 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) { 1784 error = xfs_qm_qino_alloc(mp, &uip, 1785 flags | XFS_QMOPT_UQUOTA); 1786 if (error) 1787 goto error_rele; 1788 1789 flags &= ~XFS_QMOPT_SBVERSION; 1790 } 1791 if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) { 1792 error = xfs_qm_qino_alloc(mp, &gip, 1793 flags | XFS_QMOPT_GQUOTA); 1794 if (error) 1795 goto error_rele; 1796 1797 flags &= ~XFS_QMOPT_SBVERSION; 1798 } 1799 if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) { 1800 error = xfs_qm_qino_alloc(mp, &pip, 1801 flags | XFS_QMOPT_PQUOTA); 1802 if (error) 1803 goto error_rele; 1804 } 1805 1806 mp->m_quotainfo->qi_uquotaip = uip; 1807 mp->m_quotainfo->qi_gquotaip = gip; 1808 mp->m_quotainfo->qi_pquotaip = pip; 1809 1810 return 0; 1811 1812 error_rele: 1813 if (uip) 1814 xfs_irele(uip); 1815 if (gip) 1816 xfs_irele(gip); 1817 if (pip) 1818 xfs_irele(pip); 1819 return error; 1820 } 1821 1822 STATIC void 1823 xfs_qm_dqfree_one( 1824 struct xfs_dquot *dqp) 1825 { 1826 struct xfs_mount *mp = dqp->q_mount; 1827 struct xfs_quotainfo *qi = mp->m_quotainfo; 1828 1829 mutex_lock(&qi->qi_tree_lock); 1830 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id); 1831 1832 qi->qi_dquots--; 1833 mutex_unlock(&qi->qi_tree_lock); 1834 1835 xfs_qm_dqdestroy(dqp); 1836 } 1837 1838 /* --------------- utility functions for vnodeops ---------------- */ 1839 1840 1841 /* 1842 * Given an inode, a uid, gid and prid make sure that we have 1843 * allocated relevant dquot(s) on disk, and that we won't exceed inode 1844 * quotas by creating this file. 1845 * This also attaches dquot(s) to the given inode after locking it, 1846 * and returns the dquots corresponding to the uid and/or gid. 1847 * 1848 * in : inode (unlocked) 1849 * out : udquot, gdquot with references taken and unlocked 1850 */ 1851 int 1852 xfs_qm_vop_dqalloc( 1853 struct xfs_inode *ip, 1854 kuid_t uid, 1855 kgid_t gid, 1856 prid_t prid, 1857 uint flags, 1858 struct xfs_dquot **O_udqpp, 1859 struct xfs_dquot **O_gdqpp, 1860 struct xfs_dquot **O_pdqpp) 1861 { 1862 struct xfs_mount *mp = ip->i_mount; 1863 struct inode *inode = VFS_I(ip); 1864 struct user_namespace *user_ns = inode->i_sb->s_user_ns; 1865 struct xfs_dquot *uq = NULL; 1866 struct xfs_dquot *gq = NULL; 1867 struct xfs_dquot *pq = NULL; 1868 int error; 1869 1870 if (!XFS_IS_QUOTA_ON(mp)) 1871 return 0; 1872 1873 ASSERT(!xfs_is_metadir_inode(ip)); 1874 1875 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip)) 1876 gid = inode->i_gid; 1877 1878 /* 1879 * Attach the dquot(s) to this inode, doing a dquot allocation 1880 * if necessary. The dquot(s) will not be locked. 1881 */ 1882 if (XFS_NOT_DQATTACHED(mp, ip)) { 1883 xfs_ilock(ip, XFS_ILOCK_EXCL); 1884 error = xfs_qm_dqattach_locked(ip, true); 1885 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1886 if (error) 1887 return error; 1888 } 1889 1890 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) { 1891 ASSERT(O_udqpp); 1892 if (!uid_eq(inode->i_uid, uid)) { 1893 error = xfs_qm_dqget(mp, from_kuid(user_ns, uid), 1894 XFS_DQTYPE_USER, true, &uq); 1895 if (error) { 1896 ASSERT(error != -ENOENT); 1897 return error; 1898 } 1899 } else { 1900 /* 1901 * Take an extra reference, because we'll return 1902 * this to caller 1903 */ 1904 ASSERT(ip->i_udquot); 1905 uq = xfs_qm_dqhold(ip->i_udquot); 1906 } 1907 } 1908 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) { 1909 ASSERT(O_gdqpp); 1910 if (!gid_eq(inode->i_gid, gid)) { 1911 error = xfs_qm_dqget(mp, from_kgid(user_ns, gid), 1912 XFS_DQTYPE_GROUP, true, &gq); 1913 if (error) { 1914 ASSERT(error != -ENOENT); 1915 goto error_rele; 1916 } 1917 } else { 1918 ASSERT(ip->i_gdquot); 1919 gq = xfs_qm_dqhold(ip->i_gdquot); 1920 } 1921 } 1922 if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) { 1923 ASSERT(O_pdqpp); 1924 if (ip->i_projid != prid) { 1925 error = xfs_qm_dqget(mp, prid, 1926 XFS_DQTYPE_PROJ, true, &pq); 1927 if (error) { 1928 ASSERT(error != -ENOENT); 1929 goto error_rele; 1930 } 1931 } else { 1932 ASSERT(ip->i_pdquot); 1933 pq = xfs_qm_dqhold(ip->i_pdquot); 1934 } 1935 } 1936 trace_xfs_dquot_dqalloc(ip); 1937 1938 if (O_udqpp) 1939 *O_udqpp = uq; 1940 else 1941 xfs_qm_dqrele(uq); 1942 if (O_gdqpp) 1943 *O_gdqpp = gq; 1944 else 1945 xfs_qm_dqrele(gq); 1946 if (O_pdqpp) 1947 *O_pdqpp = pq; 1948 else 1949 xfs_qm_dqrele(pq); 1950 return 0; 1951 1952 error_rele: 1953 xfs_qm_dqrele(gq); 1954 xfs_qm_dqrele(uq); 1955 return error; 1956 } 1957 1958 /* 1959 * Actually transfer ownership, and do dquot modifications. 1960 * These were already reserved. 1961 */ 1962 struct xfs_dquot * 1963 xfs_qm_vop_chown( 1964 struct xfs_trans *tp, 1965 struct xfs_inode *ip, 1966 struct xfs_dquot **IO_olddq, 1967 struct xfs_dquot *newdq) 1968 { 1969 struct xfs_dquot *prevdq; 1970 xfs_filblks_t dblocks, rblocks; 1971 bool isrt = XFS_IS_REALTIME_INODE(ip); 1972 1973 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 1974 ASSERT(XFS_IS_QUOTA_ON(ip->i_mount)); 1975 ASSERT(!xfs_is_metadir_inode(ip)); 1976 1977 /* old dquot */ 1978 prevdq = *IO_olddq; 1979 ASSERT(prevdq); 1980 ASSERT(prevdq != newdq); 1981 1982 xfs_inode_count_blocks(tp, ip, &dblocks, &rblocks); 1983 1984 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_BCOUNT, 1985 -(xfs_qcnt_t)dblocks); 1986 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_RTBCOUNT, 1987 -(xfs_qcnt_t)rblocks); 1988 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_ICOUNT, -1); 1989 1990 /* the sparkling new dquot */ 1991 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_BCOUNT, dblocks); 1992 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_RTBCOUNT, rblocks); 1993 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_ICOUNT, 1); 1994 1995 /* 1996 * Back when we made quota reservations for the chown, we reserved the 1997 * ondisk blocks + delalloc blocks with the new dquot. Now that we've 1998 * switched the dquots, decrease the new dquot's block reservation 1999 * (having already bumped up the real counter) so that we don't have 2000 * any reservation to give back when we commit. 2001 */ 2002 xfs_trans_mod_dquot(tp, newdq, 2003 isrt ? XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS, 2004 -ip->i_delayed_blks); 2005 2006 /* 2007 * Give the incore reservation for delalloc blocks back to the old 2008 * dquot. We don't normally handle delalloc quota reservations 2009 * transactionally, so just lock the dquot and subtract from the 2010 * reservation. Dirty the transaction because it's too late to turn 2011 * back now. 2012 */ 2013 tp->t_flags |= XFS_TRANS_DIRTY; 2014 mutex_lock(&prevdq->q_qlock); 2015 if (isrt) { 2016 ASSERT(prevdq->q_rtb.reserved >= ip->i_delayed_blks); 2017 prevdq->q_rtb.reserved -= ip->i_delayed_blks; 2018 } else { 2019 ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks); 2020 prevdq->q_blk.reserved -= ip->i_delayed_blks; 2021 } 2022 mutex_unlock(&prevdq->q_qlock); 2023 2024 /* 2025 * Take an extra reference, because the inode is going to keep 2026 * this dquot pointer even after the trans_commit. 2027 */ 2028 *IO_olddq = xfs_qm_dqhold(newdq); 2029 2030 return prevdq; 2031 } 2032 2033 int 2034 xfs_qm_vop_rename_dqattach( 2035 struct xfs_inode **i_tab) 2036 { 2037 struct xfs_mount *mp = i_tab[0]->i_mount; 2038 int i; 2039 2040 if (!XFS_IS_QUOTA_ON(mp)) 2041 return 0; 2042 2043 for (i = 0; (i < 4 && i_tab[i]); i++) { 2044 struct xfs_inode *ip = i_tab[i]; 2045 int error; 2046 2047 /* 2048 * Watch out for duplicate entries in the table. 2049 */ 2050 if (i == 0 || ip != i_tab[i-1]) { 2051 if (XFS_NOT_DQATTACHED(mp, ip)) { 2052 error = xfs_qm_dqattach(ip); 2053 if (error) 2054 return error; 2055 } 2056 } 2057 } 2058 return 0; 2059 } 2060 2061 void 2062 xfs_qm_vop_create_dqattach( 2063 struct xfs_trans *tp, 2064 struct xfs_inode *ip, 2065 struct xfs_dquot *udqp, 2066 struct xfs_dquot *gdqp, 2067 struct xfs_dquot *pdqp) 2068 { 2069 struct xfs_mount *mp = tp->t_mountp; 2070 2071 if (!XFS_IS_QUOTA_ON(mp)) 2072 return; 2073 2074 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 2075 ASSERT(!xfs_is_metadir_inode(ip)); 2076 2077 if (udqp && XFS_IS_UQUOTA_ON(mp)) { 2078 ASSERT(ip->i_udquot == NULL); 2079 ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id); 2080 2081 ip->i_udquot = xfs_qm_dqhold(udqp); 2082 } 2083 if (gdqp && XFS_IS_GQUOTA_ON(mp)) { 2084 ASSERT(ip->i_gdquot == NULL); 2085 ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id); 2086 2087 ip->i_gdquot = xfs_qm_dqhold(gdqp); 2088 } 2089 if (pdqp && XFS_IS_PQUOTA_ON(mp)) { 2090 ASSERT(ip->i_pdquot == NULL); 2091 ASSERT(ip->i_projid == pdqp->q_id); 2092 2093 ip->i_pdquot = xfs_qm_dqhold(pdqp); 2094 } 2095 2096 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, 1); 2097 } 2098 2099 /* Decide if this inode's dquot is near an enforcement boundary. */ 2100 bool 2101 xfs_inode_near_dquot_enforcement( 2102 struct xfs_inode *ip, 2103 xfs_dqtype_t type) 2104 { 2105 struct xfs_dquot *dqp; 2106 struct xfs_dquot_res *res; 2107 struct xfs_dquot_pre *pre; 2108 int64_t freesp; 2109 2110 /* We only care for quotas that are enabled and enforced. */ 2111 dqp = xfs_inode_dquot(ip, type); 2112 if (!dqp || !xfs_dquot_is_enforced(dqp)) 2113 return false; 2114 2115 if (xfs_dquot_res_over_limits(&dqp->q_ino) || 2116 xfs_dquot_res_over_limits(&dqp->q_blk) || 2117 xfs_dquot_res_over_limits(&dqp->q_rtb)) 2118 return true; 2119 2120 if (XFS_IS_REALTIME_INODE(ip)) { 2121 res = &dqp->q_rtb; 2122 pre = &dqp->q_rtb_prealloc; 2123 } else { 2124 res = &dqp->q_blk; 2125 pre = &dqp->q_blk_prealloc; 2126 } 2127 2128 /* For space on the data device, check the various thresholds. */ 2129 if (!pre->q_prealloc_hi_wmark) 2130 return false; 2131 2132 if (res->reserved < pre->q_prealloc_lo_wmark) 2133 return false; 2134 2135 if (res->reserved >= pre->q_prealloc_hi_wmark) 2136 return true; 2137 2138 freesp = pre->q_prealloc_hi_wmark - res->reserved; 2139 if (freesp < pre->q_low_space[XFS_QLOWSP_5_PCNT]) 2140 return true; 2141 2142 return false; 2143 } 2144