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 * For metadir filesystems, quota flag state persists across mounts, so update 698 * secondary superblocks in case xfs_repair ever has to recover the primary 699 * from a secondary. growfs also updates backup supers so lock against that. 700 * Note that the secondary backups are opportunistic, so we log but do not 701 * return write errors. 702 */ 703 void 704 __xfs_qm_update_secondary_sb( 705 struct xfs_mount *mp, 706 const char *func) 707 { 708 int error; 709 710 if (!xfs_has_metadir(mp)) 711 return; 712 713 mutex_lock(&mp->m_growlock); 714 error = xfs_update_secondary_sbs(mp); 715 mutex_unlock(&mp->m_growlock); 716 717 if (error) 718 xfs_alert(mp, "%s: Secondary super update failed, err=%d!", 719 func, error); 720 } 721 722 /* 723 * Add QUOTABIT to sb_versionnum and initialize qflags in preparation for 724 * creating quota files on a metadir filesystem. 725 */ 726 STATIC int 727 xfs_qm_prep_metadir_sb( 728 struct xfs_mount *mp) 729 { 730 struct xfs_trans *tp; 731 int error; 732 733 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp); 734 if (error) 735 return error; 736 737 spin_lock(&mp->m_sb_lock); 738 739 xfs_add_quota(mp); 740 741 /* qflags will get updated fully _after_ quotacheck */ 742 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT; 743 744 spin_unlock(&mp->m_sb_lock); 745 xfs_log_sb(tp); 746 747 error = xfs_trans_commit(tp); 748 if (error) 749 return error; 750 751 xfs_qm_update_secondary_sb(mp); 752 return 0; 753 } 754 755 /* 756 * Load existing quota inodes or create them. Since this is a V5 filesystem, 757 * we don't have to deal with the grp/prjquota switcheroo thing from V4. 758 */ 759 STATIC int 760 xfs_qm_init_metadir_qinos( 761 struct xfs_mount *mp) 762 { 763 struct xfs_quotainfo *qi = mp->m_quotainfo; 764 int error; 765 766 if (!xfs_has_quota(mp)) { 767 error = xfs_qm_prep_metadir_sb(mp); 768 if (error) 769 return error; 770 } 771 772 error = xfs_qm_load_metadir_qinos(mp, qi); 773 if (error) 774 goto out_err; 775 776 error = xfs_qm_create_metadir_qinos(mp, qi); 777 if (error) 778 goto out_err; 779 780 /* The only user of the quota dir inode is online fsck */ 781 #if !IS_ENABLED(CONFIG_XFS_ONLINE_SCRUB) 782 xfs_irele(qi->qi_dirip); 783 qi->qi_dirip = NULL; 784 #endif 785 return 0; 786 out_err: 787 xfs_qm_destroy_quotainos(mp->m_quotainfo); 788 return error; 789 } 790 791 /* 792 * This initializes all the quota information that's kept in the 793 * mount structure 794 */ 795 STATIC int 796 xfs_qm_init_quotainfo( 797 struct xfs_mount *mp) 798 { 799 struct xfs_quotainfo *qinf; 800 int error; 801 802 ASSERT(XFS_IS_QUOTA_ON(mp)); 803 804 qinf = mp->m_quotainfo = kzalloc_obj(struct xfs_quotainfo, 805 GFP_KERNEL | __GFP_NOFAIL); 806 807 error = list_lru_init(&qinf->qi_lru); 808 if (error) 809 goto out_free_qinf; 810 811 /* 812 * See if quotainodes are setup, and if not, allocate them, 813 * and change the superblock accordingly. 814 */ 815 if (xfs_has_metadir(mp)) 816 error = xfs_qm_init_metadir_qinos(mp); 817 else 818 error = xfs_qm_init_quotainos(mp); 819 if (error) 820 goto out_free_lru; 821 822 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_KERNEL); 823 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_KERNEL); 824 INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_KERNEL); 825 mutex_init(&qinf->qi_tree_lock); 826 827 /* mutex used to serialize quotaoffs */ 828 mutex_init(&qinf->qi_quotaofflock); 829 830 /* Precalc some constants */ 831 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB); 832 qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen); 833 if (xfs_has_bigtime(mp)) { 834 qinf->qi_expiry_min = 835 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN); 836 qinf->qi_expiry_max = 837 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX); 838 } else { 839 qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN; 840 qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX; 841 } 842 trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min, 843 qinf->qi_expiry_max); 844 845 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD); 846 847 xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER); 848 xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP); 849 xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ); 850 851 if (XFS_IS_UQUOTA_ON(mp)) 852 xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf); 853 if (XFS_IS_GQUOTA_ON(mp)) 854 xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf); 855 if (XFS_IS_PQUOTA_ON(mp)) 856 xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf); 857 858 qinf->qi_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "xfs-qm:%s", 859 mp->m_super->s_id); 860 if (!qinf->qi_shrinker) { 861 error = -ENOMEM; 862 goto out_free_inos; 863 } 864 865 qinf->qi_shrinker->count_objects = xfs_qm_shrink_count; 866 qinf->qi_shrinker->scan_objects = xfs_qm_shrink_scan; 867 qinf->qi_shrinker->private_data = qinf; 868 869 shrinker_register(qinf->qi_shrinker); 870 871 xfs_hooks_init(&qinf->qi_mod_ino_dqtrx_hooks); 872 xfs_hooks_init(&qinf->qi_apply_dqtrx_hooks); 873 874 return 0; 875 876 out_free_inos: 877 mutex_destroy(&qinf->qi_quotaofflock); 878 mutex_destroy(&qinf->qi_tree_lock); 879 xfs_qm_destroy_quotainos(qinf); 880 out_free_lru: 881 list_lru_destroy(&qinf->qi_lru); 882 out_free_qinf: 883 kfree(qinf); 884 mp->m_quotainfo = NULL; 885 return error; 886 } 887 888 /* 889 * Gets called when unmounting a filesystem or when all quotas get 890 * turned off. 891 * This purges the quota inodes, destroys locks and frees itself. 892 */ 893 void 894 xfs_qm_destroy_quotainfo( 895 struct xfs_mount *mp) 896 { 897 struct xfs_quotainfo *qi; 898 899 qi = mp->m_quotainfo; 900 ASSERT(qi != NULL); 901 902 shrinker_free(qi->qi_shrinker); 903 list_lru_destroy(&qi->qi_lru); 904 xfs_qm_destroy_quotainos(qi); 905 mutex_destroy(&qi->qi_tree_lock); 906 mutex_destroy(&qi->qi_quotaofflock); 907 kfree(qi); 908 mp->m_quotainfo = NULL; 909 } 910 911 static inline enum xfs_metafile_type 912 xfs_qm_metafile_type( 913 unsigned int flags) 914 { 915 if (flags & XFS_QMOPT_UQUOTA) 916 return XFS_METAFILE_USRQUOTA; 917 else if (flags & XFS_QMOPT_GQUOTA) 918 return XFS_METAFILE_GRPQUOTA; 919 return XFS_METAFILE_PRJQUOTA; 920 } 921 922 /* 923 * Create an inode and return with a reference already taken, but unlocked 924 * This is how we create quota inodes 925 */ 926 STATIC int 927 xfs_qm_qino_alloc( 928 struct xfs_mount *mp, 929 struct xfs_inode **ipp, 930 unsigned int flags) 931 { 932 struct xfs_trans *tp; 933 enum xfs_metafile_type metafile_type = xfs_qm_metafile_type(flags); 934 int error; 935 bool need_alloc = true; 936 937 *ipp = NULL; 938 /* 939 * With superblock that doesn't have separate pquotino, we 940 * share an inode between gquota and pquota. If the on-disk 941 * superblock has GQUOTA and the filesystem is now mounted 942 * with PQUOTA, just use sb_gquotino for sb_pquotino and 943 * vice-versa. 944 */ 945 if (!xfs_has_pquotino(mp) && 946 (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) { 947 xfs_ino_t ino = NULLFSINO; 948 949 if ((flags & XFS_QMOPT_PQUOTA) && 950 (mp->m_sb.sb_gquotino != NULLFSINO)) { 951 ino = mp->m_sb.sb_gquotino; 952 if (XFS_IS_CORRUPT(mp, 953 mp->m_sb.sb_pquotino != NULLFSINO)) { 954 xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA); 955 return -EFSCORRUPTED; 956 } 957 } else if ((flags & XFS_QMOPT_GQUOTA) && 958 (mp->m_sb.sb_pquotino != NULLFSINO)) { 959 ino = mp->m_sb.sb_pquotino; 960 if (XFS_IS_CORRUPT(mp, 961 mp->m_sb.sb_gquotino != NULLFSINO)) { 962 xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA); 963 return -EFSCORRUPTED; 964 } 965 } 966 if (ino != NULLFSINO) { 967 error = xfs_metafile_iget(mp, ino, metafile_type, ipp); 968 if (error) 969 return error; 970 971 mp->m_sb.sb_gquotino = NULLFSINO; 972 mp->m_sb.sb_pquotino = NULLFSINO; 973 need_alloc = false; 974 } 975 } 976 977 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create, 978 need_alloc ? XFS_QM_QINOCREATE_SPACE_RES(mp) : 0, 979 0, 0, &tp); 980 if (error) 981 return error; 982 983 if (need_alloc) { 984 struct xfs_icreate_args args = { 985 .mode = S_IFREG, 986 .flags = XFS_ICREATE_UNLINKABLE, 987 }; 988 xfs_ino_t ino; 989 990 error = xfs_dialloc(&tp, &args, &ino); 991 if (!error) 992 error = xfs_icreate(tp, ino, &args, ipp); 993 if (error) { 994 xfs_trans_cancel(tp); 995 return error; 996 } 997 if (xfs_has_metadir(mp)) 998 xfs_metafile_set_iflag(tp, *ipp, metafile_type); 999 } 1000 1001 /* 1002 * Make the changes in the superblock, and log those too. 1003 * sbfields arg may contain fields other than *QUOTINO; 1004 * VERSIONNUM for example. 1005 */ 1006 spin_lock(&mp->m_sb_lock); 1007 if (flags & XFS_QMOPT_SBVERSION) { 1008 ASSERT(!xfs_has_quota(mp)); 1009 1010 xfs_add_quota(mp); 1011 mp->m_sb.sb_uquotino = NULLFSINO; 1012 mp->m_sb.sb_gquotino = NULLFSINO; 1013 mp->m_sb.sb_pquotino = NULLFSINO; 1014 1015 /* qflags will get updated fully _after_ quotacheck */ 1016 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT; 1017 } 1018 if (flags & XFS_QMOPT_UQUOTA) 1019 mp->m_sb.sb_uquotino = I_INO(*ipp); 1020 else if (flags & XFS_QMOPT_GQUOTA) 1021 mp->m_sb.sb_gquotino = I_INO(*ipp); 1022 else 1023 mp->m_sb.sb_pquotino = I_INO(*ipp); 1024 spin_unlock(&mp->m_sb_lock); 1025 xfs_log_sb(tp); 1026 1027 error = xfs_trans_commit(tp); 1028 if (error) { 1029 ASSERT(xfs_is_shutdown(mp)); 1030 xfs_alert(mp, "%s failed (error %d)!", __func__, error); 1031 } 1032 if (need_alloc) { 1033 xfs_iunlock(*ipp, XFS_ILOCK_EXCL); 1034 xfs_finish_inode_setup(*ipp); 1035 } 1036 return error; 1037 } 1038 1039 1040 STATIC void 1041 xfs_qm_reset_dqcounts( 1042 struct xfs_mount *mp, 1043 struct xfs_buf *bp, 1044 xfs_dqid_t id, 1045 xfs_dqtype_t type) 1046 { 1047 struct xfs_dqblk *dqb; 1048 int j; 1049 1050 trace_xfs_reset_dqcounts(bp, _RET_IP_); 1051 1052 /* 1053 * Reset all counters and timers. They'll be 1054 * started afresh by xfs_qm_quotacheck. 1055 */ 1056 #ifdef DEBUG 1057 j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) / 1058 sizeof(struct xfs_dqblk); 1059 ASSERT(mp->m_quotainfo->qi_dqperchunk == j); 1060 #endif 1061 dqb = bp->b_addr; 1062 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) { 1063 struct xfs_disk_dquot *ddq; 1064 1065 ddq = (struct xfs_disk_dquot *)&dqb[j]; 1066 1067 /* 1068 * Do a sanity check, and if needed, repair the dqblk. Don't 1069 * output any warnings because it's perfectly possible to 1070 * find uninitialised dquot blks. See comment in 1071 * xfs_dquot_verify. 1072 */ 1073 if (xfs_dqblk_verify(mp, &dqb[j], id + j) || 1074 (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type) 1075 xfs_dqblk_repair(mp, &dqb[j], id + j, type); 1076 1077 /* 1078 * Reset type in case we are reusing group quota file for 1079 * project quotas or vice versa 1080 */ 1081 ddq->d_type = type; 1082 ddq->d_bcount = 0; 1083 ddq->d_icount = 0; 1084 ddq->d_rtbcount = 0; 1085 1086 /* 1087 * dquot id 0 stores the default grace period and the maximum 1088 * warning limit that were set by the administrator, so we 1089 * should not reset them. 1090 */ 1091 if (ddq->d_id != 0) { 1092 ddq->d_btimer = 0; 1093 ddq->d_itimer = 0; 1094 ddq->d_rtbtimer = 0; 1095 ddq->d_bwarns = 0; 1096 ddq->d_iwarns = 0; 1097 ddq->d_rtbwarns = 0; 1098 if (xfs_has_bigtime(mp)) 1099 ddq->d_type |= XFS_DQTYPE_BIGTIME; 1100 } 1101 1102 if (xfs_has_crc(mp)) { 1103 xfs_update_cksum((char *)&dqb[j], 1104 sizeof(struct xfs_dqblk), 1105 XFS_DQUOT_CRC_OFF); 1106 } 1107 } 1108 } 1109 1110 STATIC int 1111 xfs_qm_reset_dqcounts_all( 1112 struct xfs_mount *mp, 1113 xfs_dqid_t firstid, 1114 xfs_fsblock_t bno, 1115 xfs_filblks_t blkcnt, 1116 xfs_dqtype_t type, 1117 struct list_head *buffer_list) 1118 { 1119 struct xfs_buf *bp; 1120 int error = 0; 1121 1122 ASSERT(blkcnt > 0); 1123 1124 /* 1125 * Blkcnt arg can be a very big number, and might even be 1126 * larger than the log itself. So, we have to break it up into 1127 * manageable-sized transactions. 1128 * Note that we don't start a permanent transaction here; we might 1129 * not be able to get a log reservation for the whole thing up front, 1130 * and we don't really care to either, because we just discard 1131 * everything if we were to crash in the middle of this loop. 1132 */ 1133 while (blkcnt--) { 1134 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, 1135 XFS_FSB_TO_DADDR(mp, bno), 1136 mp->m_quotainfo->qi_dqchunklen, 0, &bp, 1137 &xfs_dquot_buf_ops); 1138 1139 /* 1140 * CRC and validation errors will return a EFSCORRUPTED here. If 1141 * this occurs, re-read without CRC validation so that we can 1142 * repair the damage via xfs_qm_reset_dqcounts(). This process 1143 * will leave a trace in the log indicating corruption has 1144 * been detected. 1145 */ 1146 if (error == -EFSCORRUPTED) { 1147 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, 1148 XFS_FSB_TO_DADDR(mp, bno), 1149 mp->m_quotainfo->qi_dqchunklen, 0, &bp, 1150 NULL); 1151 } 1152 1153 if (error) 1154 break; 1155 1156 /* 1157 * A corrupt buffer might not have a verifier attached, so 1158 * make sure we have the correct one attached before writeback 1159 * occurs. 1160 */ 1161 bp->b_ops = &xfs_dquot_buf_ops; 1162 xfs_qm_reset_dqcounts(mp, bp, firstid, type); 1163 xfs_buf_delwri_queue(bp, buffer_list); 1164 xfs_buf_relse(bp); 1165 1166 /* goto the next block. */ 1167 bno++; 1168 firstid += mp->m_quotainfo->qi_dqperchunk; 1169 } 1170 1171 return error; 1172 } 1173 1174 /* 1175 * Iterate over all allocated dquot blocks in this quota inode, zeroing all 1176 * counters for every chunk of dquots that we find. 1177 */ 1178 STATIC int 1179 xfs_qm_reset_dqcounts_buf( 1180 struct xfs_mount *mp, 1181 struct xfs_inode *qip, 1182 xfs_dqtype_t type, 1183 struct list_head *buffer_list) 1184 { 1185 struct xfs_bmbt_irec *map; 1186 int i, nmaps; /* number of map entries */ 1187 int error; /* return value */ 1188 xfs_fileoff_t lblkno; 1189 xfs_filblks_t maxlblkcnt; 1190 xfs_dqid_t firstid; 1191 xfs_fsblock_t rablkno; 1192 xfs_filblks_t rablkcnt; 1193 1194 error = 0; 1195 /* 1196 * This looks racy, but we can't keep an inode lock across a 1197 * trans_reserve. But, this gets called during quotacheck, and that 1198 * happens only at mount time which is single threaded. 1199 */ 1200 if (qip->i_nblocks == 0) 1201 return 0; 1202 1203 map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 1204 GFP_KERNEL | __GFP_NOFAIL); 1205 1206 lblkno = 0; 1207 maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); 1208 do { 1209 uint lock_mode; 1210 1211 nmaps = XFS_DQITER_MAP_SIZE; 1212 /* 1213 * We aren't changing the inode itself. Just changing 1214 * some of its data. No new blocks are added here, and 1215 * the inode is never added to the transaction. 1216 */ 1217 lock_mode = xfs_ilock_data_map_shared(qip); 1218 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno, 1219 map, &nmaps, 0); 1220 xfs_iunlock(qip, lock_mode); 1221 if (error) 1222 break; 1223 1224 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE); 1225 for (i = 0; i < nmaps; i++) { 1226 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK); 1227 ASSERT(map[i].br_blockcount); 1228 1229 1230 lblkno += map[i].br_blockcount; 1231 1232 if (map[i].br_startblock == HOLESTARTBLOCK) 1233 continue; 1234 1235 firstid = (xfs_dqid_t) map[i].br_startoff * 1236 mp->m_quotainfo->qi_dqperchunk; 1237 /* 1238 * Do a read-ahead on the next extent. 1239 */ 1240 if ((i+1 < nmaps) && 1241 (map[i+1].br_startblock != HOLESTARTBLOCK)) { 1242 rablkcnt = map[i+1].br_blockcount; 1243 rablkno = map[i+1].br_startblock; 1244 while (rablkcnt--) { 1245 xfs_buf_readahead(mp->m_ddev_targp, 1246 XFS_FSB_TO_DADDR(mp, rablkno), 1247 mp->m_quotainfo->qi_dqchunklen, 1248 &xfs_dquot_buf_ops); 1249 rablkno++; 1250 } 1251 } 1252 /* 1253 * Iterate thru all the blks in the extent and 1254 * reset the counters of all the dquots inside them. 1255 */ 1256 error = xfs_qm_reset_dqcounts_all(mp, firstid, 1257 map[i].br_startblock, 1258 map[i].br_blockcount, 1259 type, buffer_list); 1260 if (error) 1261 goto out; 1262 } 1263 } while (nmaps > 0); 1264 1265 out: 1266 kfree(map); 1267 return error; 1268 } 1269 1270 /* 1271 * Called by dqusage_adjust in doing a quotacheck. 1272 * 1273 * Given the inode, and a dquot id this updates both the incore dqout as well 1274 * as the buffer copy. This is so that once the quotacheck is done, we can 1275 * just log all the buffers, as opposed to logging numerous updates to 1276 * individual dquots. 1277 */ 1278 STATIC int 1279 xfs_qm_quotacheck_dqadjust( 1280 struct xfs_inode *ip, 1281 xfs_dqtype_t type, 1282 xfs_qcnt_t nblks, 1283 xfs_qcnt_t rtblks) 1284 { 1285 struct xfs_mount *mp = ip->i_mount; 1286 struct xfs_dquot *dqp; 1287 xfs_dqid_t id; 1288 int error; 1289 1290 id = xfs_qm_id_for_quotatype(ip, type); 1291 error = xfs_qm_dqget(mp, id, type, true, &dqp); 1292 if (error) { 1293 /* 1294 * Shouldn't be able to turn off quotas here. 1295 */ 1296 ASSERT(error != -ESRCH); 1297 ASSERT(error != -ENOENT); 1298 return error; 1299 } 1300 1301 mutex_lock(&dqp->q_qlock); 1302 error = xfs_dquot_attach_buf(NULL, dqp); 1303 if (error) 1304 goto out_unlock; 1305 1306 trace_xfs_dqadjust(dqp); 1307 1308 /* 1309 * Adjust the inode count and the block count to reflect this inode's 1310 * resource usage. 1311 */ 1312 dqp->q_ino.count++; 1313 dqp->q_ino.reserved++; 1314 if (nblks) { 1315 dqp->q_blk.count += nblks; 1316 dqp->q_blk.reserved += nblks; 1317 } 1318 if (rtblks) { 1319 dqp->q_rtb.count += rtblks; 1320 dqp->q_rtb.reserved += rtblks; 1321 } 1322 1323 /* 1324 * Set default limits, adjust timers (since we changed usages) 1325 * 1326 * There are no timers for the default values set in the root dquot. 1327 */ 1328 xfs_qm_adjust_dqenforcement(dqp); 1329 dqp->q_flags |= XFS_DQFLAG_DIRTY; 1330 out_unlock: 1331 mutex_unlock(&dqp->q_qlock); 1332 xfs_qm_dqrele(dqp); 1333 return error; 1334 } 1335 1336 /* 1337 * callback routine supplied to bulkstat(). Given an inumber, find its 1338 * dquots and update them to account for resources taken by that inode. 1339 */ 1340 /* ARGSUSED */ 1341 STATIC int 1342 xfs_qm_dqusage_adjust( 1343 struct xfs_mount *mp, 1344 struct xfs_trans *tp, 1345 xfs_ino_t ino, 1346 void *data) 1347 { 1348 struct xfs_inode *ip; 1349 xfs_filblks_t nblks, rtblks; 1350 unsigned int lock_mode; 1351 int error; 1352 1353 ASSERT(XFS_IS_QUOTA_ON(mp)); 1354 1355 /* 1356 * rootino must have its resources accounted for, not so with the quota 1357 * inodes. 1358 */ 1359 if (xfs_is_quota_inode(&mp->m_sb, ino)) 1360 return 0; 1361 1362 /* 1363 * We don't _need_ to take the ilock EXCL here because quotacheck runs 1364 * at mount time and therefore nobody will be racing chown/chproj. 1365 */ 1366 error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip); 1367 if (error == -EINVAL || error == -ENOENT) 1368 return 0; 1369 if (error) 1370 return error; 1371 1372 /* 1373 * Reload the incore unlinked list to avoid failure in inodegc. 1374 * Use an unlocked check here because unrecovered unlinked inodes 1375 * should be somewhat rare. 1376 */ 1377 if (xfs_inode_unlinked_incomplete(ip)) { 1378 error = xfs_inode_reload_unlinked(ip); 1379 if (error) { 1380 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 1381 goto error0; 1382 } 1383 } 1384 1385 /* Metadata directory files are not accounted to user-visible quotas. */ 1386 if (xfs_is_metadir_inode(ip)) 1387 goto error0; 1388 1389 ASSERT(ip->i_delayed_blks == 0); 1390 1391 lock_mode = xfs_ilock_data_map_shared(ip); 1392 if (XFS_IS_REALTIME_INODE(ip)) { 1393 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK); 1394 if (error) { 1395 xfs_iunlock(ip, lock_mode); 1396 goto error0; 1397 } 1398 } 1399 xfs_inode_count_blocks(tp, ip, &nblks, &rtblks); 1400 xfs_iflags_clear(ip, XFS_IQUOTAUNCHECKED); 1401 xfs_iunlock(ip, lock_mode); 1402 1403 /* 1404 * Add the (disk blocks and inode) resources occupied by this 1405 * inode to its dquots. We do this adjustment in the incore dquot, 1406 * and also copy the changes to its buffer. 1407 * We don't care about putting these changes in a transaction 1408 * envelope because if we crash in the middle of a 'quotacheck' 1409 * we have to start from the beginning anyway. 1410 * Once we're done, we'll log all the dquot bufs. 1411 * 1412 * The *QUOTA_ON checks below may look pretty racy, but quotachecks 1413 * and quotaoffs don't race. (Quotachecks happen at mount time only). 1414 */ 1415 if (XFS_IS_UQUOTA_ON(mp)) { 1416 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks, 1417 rtblks); 1418 if (error) 1419 goto error0; 1420 } 1421 1422 if (XFS_IS_GQUOTA_ON(mp)) { 1423 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks, 1424 rtblks); 1425 if (error) 1426 goto error0; 1427 } 1428 1429 if (XFS_IS_PQUOTA_ON(mp)) { 1430 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks, 1431 rtblks); 1432 if (error) 1433 goto error0; 1434 } 1435 1436 error0: 1437 xfs_irele(ip); 1438 return error; 1439 } 1440 1441 STATIC int 1442 xfs_qm_flush_one( 1443 struct xfs_dquot *dqp, 1444 void *data) 1445 { 1446 struct list_head *buffer_list = data; 1447 struct xfs_buf *bp = NULL; 1448 int error = 0; 1449 1450 if (!lockref_get_not_dead(&dqp->q_lockref)) 1451 return 0; 1452 1453 mutex_lock(&dqp->q_qlock); 1454 if (!XFS_DQ_IS_DIRTY(dqp)) 1455 goto out_unlock; 1456 1457 xfs_qm_dqunpin_wait(dqp); 1458 xfs_dqflock(dqp); 1459 1460 error = xfs_dquot_use_attached_buf(dqp, &bp); 1461 if (error) 1462 goto out_dqflock; 1463 if (!bp) { 1464 error = -EFSCORRUPTED; 1465 goto out_dqflock; 1466 } 1467 1468 error = xfs_qm_dqflush(dqp, bp); 1469 if (!error) 1470 xfs_buf_delwri_queue(bp, buffer_list); 1471 xfs_buf_relse(bp); 1472 mutex_unlock(&dqp->q_qlock); 1473 xfs_qm_dqrele(dqp); 1474 return error; 1475 1476 out_dqflock: 1477 xfs_dqfunlock(dqp); 1478 out_unlock: 1479 mutex_unlock(&dqp->q_qlock); 1480 xfs_qm_dqrele(dqp); 1481 return error; 1482 } 1483 1484 /* 1485 * Walk thru all the filesystem inodes and construct a consistent view 1486 * of the disk quota world. If the quotacheck fails, disable quotas. 1487 */ 1488 STATIC int 1489 xfs_qm_quotacheck( 1490 xfs_mount_t *mp) 1491 { 1492 int error, error2; 1493 uint flags; 1494 LIST_HEAD (buffer_list); 1495 struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip; 1496 struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip; 1497 struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip; 1498 1499 flags = 0; 1500 1501 ASSERT(uip || gip || pip); 1502 ASSERT(XFS_IS_QUOTA_ON(mp)); 1503 1504 xfs_notice(mp, "Quotacheck needed: Please wait."); 1505 1506 /* 1507 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset 1508 * their counters to zero. We need a clean slate. 1509 * We don't log our changes till later. 1510 */ 1511 if (uip) { 1512 error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER, 1513 &buffer_list); 1514 if (error) 1515 goto error_return; 1516 flags |= XFS_UQUOTA_CHKD; 1517 } 1518 1519 if (gip) { 1520 error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP, 1521 &buffer_list); 1522 if (error) 1523 goto error_return; 1524 flags |= XFS_GQUOTA_CHKD; 1525 } 1526 1527 if (pip) { 1528 error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ, 1529 &buffer_list); 1530 if (error) 1531 goto error_return; 1532 flags |= XFS_PQUOTA_CHKD; 1533 } 1534 1535 xfs_set_quotacheck_running(mp); 1536 error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true, 1537 NULL); 1538 xfs_clear_quotacheck_running(mp); 1539 1540 /* 1541 * On error, the inode walk may have partially populated the dquot 1542 * caches. We must purge them before disabling quota and tearing down 1543 * the quotainfo, or else the dquots will leak. 1544 */ 1545 if (error) 1546 goto error_purge; 1547 1548 /* 1549 * We've made all the changes that we need to make incore. Flush them 1550 * down to disk buffers if everything was updated successfully. 1551 */ 1552 if (XFS_IS_UQUOTA_ON(mp)) { 1553 error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one, 1554 &buffer_list); 1555 } 1556 if (XFS_IS_GQUOTA_ON(mp)) { 1557 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one, 1558 &buffer_list); 1559 if (!error) 1560 error = error2; 1561 } 1562 if (XFS_IS_PQUOTA_ON(mp)) { 1563 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one, 1564 &buffer_list); 1565 if (!error) 1566 error = error2; 1567 } 1568 1569 error2 = xfs_buf_delwri_submit(&buffer_list); 1570 if (!error) 1571 error = error2; 1572 1573 /* 1574 * We can get this error if we couldn't do a dquot allocation inside 1575 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the 1576 * dirty dquots that might be cached, we just want to get rid of them 1577 * and turn quotaoff. The dquots won't be attached to any of the inodes 1578 * at this point (because we intentionally didn't in dqget_noattach). 1579 */ 1580 if (error) 1581 goto error_purge; 1582 1583 /* 1584 * If one type of quotas is off, then it will lose its 1585 * quotachecked status, since we won't be doing accounting for 1586 * that type anymore. 1587 */ 1588 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD; 1589 mp->m_qflags |= flags; 1590 1591 error_return: 1592 xfs_buf_delwri_cancel(&buffer_list); 1593 1594 if (error) { 1595 xfs_warn(mp, 1596 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.", 1597 error); 1598 /* 1599 * We must turn off quotas. 1600 */ 1601 ASSERT(mp->m_quotainfo != NULL); 1602 xfs_qm_destroy_quotainfo(mp); 1603 if (xfs_mount_reset_sbqflags(mp)) { 1604 xfs_warn(mp, 1605 "Quotacheck: Failed to reset quota flags."); 1606 } 1607 xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK); 1608 } else { 1609 xfs_notice(mp, "Quotacheck: Done."); 1610 xfs_fs_mark_healthy(mp, XFS_SICK_FS_QUOTACHECK); 1611 } 1612 1613 return error; 1614 1615 error_purge: 1616 /* 1617 * On error, we may have inodes queued for inactivation. This may try 1618 * to attach dquots to the inode before running cleanup operations on 1619 * the inode and this can race with the xfs_qm_destroy_quotainfo() call 1620 * below that frees mp->m_quotainfo. To avoid this race, flush all the 1621 * pending inodegc operations before we purge the dquots from memory, 1622 * ensuring that background inactivation is idle whilst we turn off 1623 * quotas. 1624 */ 1625 xfs_inodegc_flush(mp); 1626 xfs_qm_dqpurge_all(mp); 1627 goto error_return; 1628 1629 } 1630 1631 /* 1632 * This is called from xfs_mountfs to start quotas and initialize all 1633 * necessary data structures like quotainfo. This is also responsible for 1634 * running a quotacheck as necessary. We are guaranteed that the superblock 1635 * is consistently read in at this point. 1636 * 1637 * If we fail here, the mount will continue with quota turned off. We don't 1638 * need to inidicate success or failure at all. 1639 */ 1640 void 1641 xfs_qm_mount_quotas( 1642 struct xfs_mount *mp) 1643 { 1644 int error = 0; 1645 uint sbf; 1646 1647 /* 1648 * If quotas on realtime volumes is not supported, disable quotas 1649 * immediately. We only support rtquota if rtgroups are enabled to 1650 * avoid problems with older kernels. 1651 */ 1652 if (mp->m_sb.sb_rextents && 1653 (!xfs_has_rtgroups(mp) || xfs_has_zoned(mp))) { 1654 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem"); 1655 mp->m_qflags = 0; 1656 goto write_changes; 1657 } 1658 1659 ASSERT(XFS_IS_QUOTA_ON(mp)); 1660 1661 /* 1662 * Allocate the quotainfo structure inside the mount struct, and 1663 * create quotainode(s), and change/rev superblock if necessary. 1664 */ 1665 error = xfs_qm_init_quotainfo(mp); 1666 if (error) { 1667 /* 1668 * We must turn off quotas. 1669 */ 1670 ASSERT(mp->m_quotainfo == NULL); 1671 mp->m_qflags = 0; 1672 goto write_changes; 1673 } 1674 /* 1675 * If any of the quotas are not consistent, do a quotacheck. 1676 */ 1677 if (XFS_QM_NEED_QUOTACHECK(mp)) { 1678 error = xfs_qm_quotacheck(mp); 1679 if (error) { 1680 /* Quotacheck failed and disabled quotas. */ 1681 return; 1682 } 1683 } 1684 /* 1685 * If one type of quotas is off, then it will lose its 1686 * quotachecked status, since we won't be doing accounting for 1687 * that type anymore. 1688 */ 1689 if (!XFS_IS_UQUOTA_ON(mp)) 1690 mp->m_qflags &= ~XFS_UQUOTA_CHKD; 1691 if (!XFS_IS_GQUOTA_ON(mp)) 1692 mp->m_qflags &= ~XFS_GQUOTA_CHKD; 1693 if (!XFS_IS_PQUOTA_ON(mp)) 1694 mp->m_qflags &= ~XFS_PQUOTA_CHKD; 1695 1696 write_changes: 1697 /* 1698 * We actually don't have to acquire the m_sb_lock at all. 1699 * This can only be called from mount, and that's single threaded. XXX 1700 */ 1701 spin_lock(&mp->m_sb_lock); 1702 sbf = mp->m_sb.sb_qflags; 1703 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL; 1704 spin_unlock(&mp->m_sb_lock); 1705 1706 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) { 1707 if (xfs_sync_sb(mp, false)) { 1708 /* 1709 * We could only have been turning quotas off. 1710 * We aren't in very good shape actually because 1711 * the incore structures are convinced that quotas are 1712 * off, but the on disk superblock doesn't know that ! 1713 */ 1714 ASSERT(!(XFS_IS_QUOTA_ON(mp))); 1715 xfs_alert(mp, "%s: Superblock update failed!", 1716 __func__); 1717 } else { 1718 /* Primary update succeeded, try to write secondary */ 1719 xfs_qm_update_secondary_sb(mp); 1720 } 1721 } 1722 1723 if (error) { 1724 xfs_warn(mp, "Failed to initialize disk quotas, err %d.", error); 1725 return; 1726 } 1727 } 1728 1729 /* 1730 * Load the inode for a given type of quota, assuming that the sb fields have 1731 * been sorted out. This is not true when switching quota types on a V4 1732 * filesystem, so do not use this function for that. 1733 * 1734 * Returns -ENOENT if the quota inode field is NULLFSINO; 0 and an inode on 1735 * success; or a negative errno. 1736 */ 1737 int 1738 xfs_qm_qino_load( 1739 struct xfs_mount *mp, 1740 xfs_dqtype_t type, 1741 struct xfs_inode **ipp) 1742 { 1743 struct xfs_trans *tp; 1744 struct xfs_inode *dp = NULL; 1745 int error; 1746 1747 tp = xfs_trans_alloc_empty(mp); 1748 if (xfs_has_metadir(mp)) { 1749 error = xfs_dqinode_load_parent(tp, &dp); 1750 if (error) 1751 goto out_cancel; 1752 } 1753 1754 error = xfs_dqinode_load(tp, dp, type, ipp); 1755 if (dp) 1756 xfs_irele(dp); 1757 out_cancel: 1758 xfs_trans_cancel(tp); 1759 return error; 1760 } 1761 1762 /* 1763 * This is called after the superblock has been read in and we're ready to 1764 * iget the quota inodes. 1765 */ 1766 STATIC int 1767 xfs_qm_init_quotainos( 1768 xfs_mount_t *mp) 1769 { 1770 struct xfs_inode *uip = NULL; 1771 struct xfs_inode *gip = NULL; 1772 struct xfs_inode *pip = NULL; 1773 int error; 1774 uint flags = 0; 1775 1776 ASSERT(mp->m_quotainfo); 1777 1778 /* 1779 * Get the uquota and gquota inodes 1780 */ 1781 if (xfs_has_quota(mp)) { 1782 if (XFS_IS_UQUOTA_ON(mp) && 1783 mp->m_sb.sb_uquotino != NULLFSINO) { 1784 ASSERT(mp->m_sb.sb_uquotino > 0); 1785 error = xfs_qm_qino_load(mp, XFS_DQTYPE_USER, &uip); 1786 if (error) 1787 return error; 1788 } 1789 if (XFS_IS_GQUOTA_ON(mp) && 1790 mp->m_sb.sb_gquotino != NULLFSINO) { 1791 ASSERT(mp->m_sb.sb_gquotino > 0); 1792 error = xfs_qm_qino_load(mp, XFS_DQTYPE_GROUP, &gip); 1793 if (error) 1794 goto error_rele; 1795 } 1796 if (XFS_IS_PQUOTA_ON(mp) && 1797 mp->m_sb.sb_pquotino != NULLFSINO) { 1798 ASSERT(mp->m_sb.sb_pquotino > 0); 1799 error = xfs_qm_qino_load(mp, XFS_DQTYPE_PROJ, &pip); 1800 if (error) 1801 goto error_rele; 1802 } 1803 } else { 1804 flags |= XFS_QMOPT_SBVERSION; 1805 } 1806 1807 /* 1808 * Create the three inodes, if they don't exist already. The changes 1809 * made above will get added to a transaction and logged in one of 1810 * the qino_alloc calls below. If the device is readonly, 1811 * temporarily switch to read-write to do this. 1812 */ 1813 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) { 1814 error = xfs_qm_qino_alloc(mp, &uip, 1815 flags | XFS_QMOPT_UQUOTA); 1816 if (error) 1817 goto error_rele; 1818 1819 flags &= ~XFS_QMOPT_SBVERSION; 1820 } 1821 if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) { 1822 error = xfs_qm_qino_alloc(mp, &gip, 1823 flags | XFS_QMOPT_GQUOTA); 1824 if (error) 1825 goto error_rele; 1826 1827 flags &= ~XFS_QMOPT_SBVERSION; 1828 } 1829 if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) { 1830 error = xfs_qm_qino_alloc(mp, &pip, 1831 flags | XFS_QMOPT_PQUOTA); 1832 if (error) 1833 goto error_rele; 1834 } 1835 1836 mp->m_quotainfo->qi_uquotaip = uip; 1837 mp->m_quotainfo->qi_gquotaip = gip; 1838 mp->m_quotainfo->qi_pquotaip = pip; 1839 1840 return 0; 1841 1842 error_rele: 1843 if (uip) 1844 xfs_irele(uip); 1845 if (gip) 1846 xfs_irele(gip); 1847 if (pip) 1848 xfs_irele(pip); 1849 return error; 1850 } 1851 1852 STATIC void 1853 xfs_qm_dqfree_one( 1854 struct xfs_dquot *dqp) 1855 { 1856 struct xfs_mount *mp = dqp->q_mount; 1857 struct xfs_quotainfo *qi = mp->m_quotainfo; 1858 1859 mutex_lock(&qi->qi_tree_lock); 1860 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id); 1861 1862 qi->qi_dquots--; 1863 mutex_unlock(&qi->qi_tree_lock); 1864 1865 xfs_qm_dqdestroy(dqp); 1866 } 1867 1868 /* --------------- utility functions for vnodeops ---------------- */ 1869 1870 1871 /* 1872 * Given an inode, a uid, gid and prid make sure that we have 1873 * allocated relevant dquot(s) on disk, and that we won't exceed inode 1874 * quotas by creating this file. 1875 * This also attaches dquot(s) to the given inode after locking it, 1876 * and returns the dquots corresponding to the uid and/or gid. 1877 * 1878 * in : inode (unlocked) 1879 * out : udquot, gdquot with references taken and unlocked 1880 */ 1881 int 1882 xfs_qm_vop_dqalloc( 1883 struct xfs_inode *ip, 1884 kuid_t uid, 1885 kgid_t gid, 1886 prid_t prid, 1887 uint flags, 1888 struct xfs_dquot **O_udqpp, 1889 struct xfs_dquot **O_gdqpp, 1890 struct xfs_dquot **O_pdqpp) 1891 { 1892 struct xfs_mount *mp = ip->i_mount; 1893 struct inode *inode = VFS_I(ip); 1894 struct user_namespace *user_ns = inode->i_sb->s_user_ns; 1895 struct xfs_dquot *uq = NULL; 1896 struct xfs_dquot *gq = NULL; 1897 struct xfs_dquot *pq = NULL; 1898 int error; 1899 1900 if (!XFS_IS_QUOTA_ON(mp)) 1901 return 0; 1902 1903 ASSERT(!xfs_is_metadir_inode(ip)); 1904 1905 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip)) 1906 gid = inode->i_gid; 1907 1908 /* 1909 * Attach the dquot(s) to this inode, doing a dquot allocation 1910 * if necessary. The dquot(s) will not be locked. 1911 */ 1912 if (XFS_NOT_DQATTACHED(mp, ip)) { 1913 xfs_ilock(ip, XFS_ILOCK_EXCL); 1914 error = xfs_qm_dqattach_locked(ip, true); 1915 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1916 if (error) 1917 return error; 1918 } 1919 1920 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) { 1921 ASSERT(O_udqpp); 1922 if (!uid_eq(inode->i_uid, uid)) { 1923 error = xfs_qm_dqget(mp, from_kuid(user_ns, uid), 1924 XFS_DQTYPE_USER, true, &uq); 1925 if (error) { 1926 ASSERT(error != -ENOENT); 1927 return error; 1928 } 1929 } else { 1930 /* 1931 * Take an extra reference, because we'll return 1932 * this to caller 1933 */ 1934 ASSERT(ip->i_udquot); 1935 uq = xfs_qm_dqhold(ip->i_udquot); 1936 } 1937 } 1938 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) { 1939 ASSERT(O_gdqpp); 1940 if (!gid_eq(inode->i_gid, gid)) { 1941 error = xfs_qm_dqget(mp, from_kgid(user_ns, gid), 1942 XFS_DQTYPE_GROUP, true, &gq); 1943 if (error) { 1944 ASSERT(error != -ENOENT); 1945 goto error_rele; 1946 } 1947 } else { 1948 ASSERT(ip->i_gdquot); 1949 gq = xfs_qm_dqhold(ip->i_gdquot); 1950 } 1951 } 1952 if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) { 1953 ASSERT(O_pdqpp); 1954 if (ip->i_projid != prid) { 1955 error = xfs_qm_dqget(mp, prid, 1956 XFS_DQTYPE_PROJ, true, &pq); 1957 if (error) { 1958 ASSERT(error != -ENOENT); 1959 goto error_rele; 1960 } 1961 } else { 1962 ASSERT(ip->i_pdquot); 1963 pq = xfs_qm_dqhold(ip->i_pdquot); 1964 } 1965 } 1966 trace_xfs_dquot_dqalloc(ip); 1967 1968 if (O_udqpp) 1969 *O_udqpp = uq; 1970 else 1971 xfs_qm_dqrele(uq); 1972 if (O_gdqpp) 1973 *O_gdqpp = gq; 1974 else 1975 xfs_qm_dqrele(gq); 1976 if (O_pdqpp) 1977 *O_pdqpp = pq; 1978 else 1979 xfs_qm_dqrele(pq); 1980 return 0; 1981 1982 error_rele: 1983 xfs_qm_dqrele(gq); 1984 xfs_qm_dqrele(uq); 1985 return error; 1986 } 1987 1988 /* 1989 * Actually transfer ownership, and do dquot modifications. 1990 * These were already reserved. 1991 */ 1992 struct xfs_dquot * 1993 xfs_qm_vop_chown( 1994 struct xfs_trans *tp, 1995 struct xfs_inode *ip, 1996 struct xfs_dquot **IO_olddq, 1997 struct xfs_dquot *newdq) 1998 { 1999 struct xfs_dquot *prevdq; 2000 xfs_filblks_t dblocks, rblocks; 2001 bool isrt = XFS_IS_REALTIME_INODE(ip); 2002 2003 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 2004 ASSERT(XFS_IS_QUOTA_ON(ip->i_mount)); 2005 ASSERT(!xfs_is_metadir_inode(ip)); 2006 2007 /* old dquot */ 2008 prevdq = *IO_olddq; 2009 ASSERT(prevdq); 2010 ASSERT(prevdq != newdq); 2011 2012 xfs_inode_count_blocks(tp, ip, &dblocks, &rblocks); 2013 2014 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_BCOUNT, 2015 -(xfs_qcnt_t)dblocks); 2016 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_RTBCOUNT, 2017 -(xfs_qcnt_t)rblocks); 2018 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_ICOUNT, -1); 2019 2020 /* the sparkling new dquot */ 2021 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_BCOUNT, dblocks); 2022 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_RTBCOUNT, rblocks); 2023 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_ICOUNT, 1); 2024 2025 /* 2026 * Back when we made quota reservations for the chown, we reserved the 2027 * ondisk blocks + delalloc blocks with the new dquot. Now that we've 2028 * switched the dquots, decrease the new dquot's block reservation 2029 * (having already bumped up the real counter) so that we don't have 2030 * any reservation to give back when we commit. 2031 */ 2032 xfs_trans_mod_dquot(tp, newdq, 2033 isrt ? XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS, 2034 -ip->i_delayed_blks); 2035 2036 /* 2037 * Give the incore reservation for delalloc blocks back to the old 2038 * dquot. We don't normally handle delalloc quota reservations 2039 * transactionally, so just lock the dquot and subtract from the 2040 * reservation. Dirty the transaction because it's too late to turn 2041 * back now. 2042 */ 2043 tp->t_flags |= XFS_TRANS_DIRTY; 2044 mutex_lock(&prevdq->q_qlock); 2045 if (isrt) { 2046 ASSERT(prevdq->q_rtb.reserved >= ip->i_delayed_blks); 2047 prevdq->q_rtb.reserved -= ip->i_delayed_blks; 2048 } else { 2049 ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks); 2050 prevdq->q_blk.reserved -= ip->i_delayed_blks; 2051 } 2052 mutex_unlock(&prevdq->q_qlock); 2053 2054 /* 2055 * Take an extra reference, because the inode is going to keep 2056 * this dquot pointer even after the trans_commit. 2057 */ 2058 *IO_olddq = xfs_qm_dqhold(newdq); 2059 2060 return prevdq; 2061 } 2062 2063 int 2064 xfs_qm_vop_rename_dqattach( 2065 struct xfs_inode **i_tab) 2066 { 2067 struct xfs_mount *mp = i_tab[0]->i_mount; 2068 int i; 2069 2070 if (!XFS_IS_QUOTA_ON(mp)) 2071 return 0; 2072 2073 for (i = 0; (i < 4 && i_tab[i]); i++) { 2074 struct xfs_inode *ip = i_tab[i]; 2075 int error; 2076 2077 /* 2078 * Watch out for duplicate entries in the table. 2079 */ 2080 if (i == 0 || ip != i_tab[i-1]) { 2081 if (XFS_NOT_DQATTACHED(mp, ip)) { 2082 error = xfs_qm_dqattach(ip); 2083 if (error) 2084 return error; 2085 } 2086 } 2087 } 2088 return 0; 2089 } 2090 2091 void 2092 xfs_qm_vop_create_dqattach( 2093 struct xfs_trans *tp, 2094 struct xfs_inode *ip, 2095 struct xfs_dquot *udqp, 2096 struct xfs_dquot *gdqp, 2097 struct xfs_dquot *pdqp) 2098 { 2099 struct xfs_mount *mp = tp->t_mountp; 2100 2101 if (!XFS_IS_QUOTA_ON(mp)) 2102 return; 2103 2104 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 2105 ASSERT(!xfs_is_metadir_inode(ip)); 2106 2107 if (udqp && XFS_IS_UQUOTA_ON(mp)) { 2108 ASSERT(ip->i_udquot == NULL); 2109 ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id); 2110 2111 ip->i_udquot = xfs_qm_dqhold(udqp); 2112 } 2113 if (gdqp && XFS_IS_GQUOTA_ON(mp)) { 2114 ASSERT(ip->i_gdquot == NULL); 2115 ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id); 2116 2117 ip->i_gdquot = xfs_qm_dqhold(gdqp); 2118 } 2119 if (pdqp && XFS_IS_PQUOTA_ON(mp)) { 2120 ASSERT(ip->i_pdquot == NULL); 2121 ASSERT(ip->i_projid == pdqp->q_id); 2122 2123 ip->i_pdquot = xfs_qm_dqhold(pdqp); 2124 } 2125 2126 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, 1); 2127 } 2128 2129 /* Decide if this inode's dquot is near an enforcement boundary. */ 2130 bool 2131 xfs_inode_near_dquot_enforcement( 2132 struct xfs_inode *ip, 2133 xfs_dqtype_t type) 2134 { 2135 struct xfs_dquot *dqp; 2136 struct xfs_dquot_res *res; 2137 struct xfs_dquot_pre *pre; 2138 int64_t freesp; 2139 2140 /* We only care for quotas that are enabled and enforced. */ 2141 dqp = xfs_inode_dquot(ip, type); 2142 if (!dqp || !xfs_dquot_is_enforced(dqp)) 2143 return false; 2144 2145 if (xfs_dquot_res_over_limits(&dqp->q_ino) || 2146 xfs_dquot_res_over_limits(&dqp->q_blk) || 2147 xfs_dquot_res_over_limits(&dqp->q_rtb)) 2148 return true; 2149 2150 if (XFS_IS_REALTIME_INODE(ip)) { 2151 res = &dqp->q_rtb; 2152 pre = &dqp->q_rtb_prealloc; 2153 } else { 2154 res = &dqp->q_blk; 2155 pre = &dqp->q_blk_prealloc; 2156 } 2157 2158 /* For space on the data device, check the various thresholds. */ 2159 if (!pre->q_prealloc_hi_wmark) 2160 return false; 2161 2162 if (res->reserved < pre->q_prealloc_lo_wmark) 2163 return false; 2164 2165 if (res->reserved >= pre->q_prealloc_hi_wmark) 2166 return true; 2167 2168 freesp = pre->q_prealloc_hi_wmark - res->reserved; 2169 if (freesp < pre->q_low_space[XFS_QLOWSP_5_PCNT]) 2170 return true; 2171 2172 return false; 2173 } 2174