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