1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2003 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs.h" 7 #include "xfs_fs.h" 8 #include "xfs_format.h" 9 #include "xfs_log_format.h" 10 #include "xfs_shared.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_bit.h" 13 #include "xfs_mount.h" 14 #include "xfs_defer.h" 15 #include "xfs_inode.h" 16 #include "xfs_bmap.h" 17 #include "xfs_quota.h" 18 #include "xfs_trans.h" 19 #include "xfs_buf_item.h" 20 #include "xfs_trans_space.h" 21 #include "xfs_trans_priv.h" 22 #include "xfs_qm.h" 23 #include "xfs_trace.h" 24 #include "xfs_log.h" 25 #include "xfs_bmap_btree.h" 26 #include "xfs_error.h" 27 28 /* 29 * Lock order: 30 * 31 * ip->i_lock 32 * qi->qi_tree_lock 33 * dquot->q_qlock (xfs_dqlock() and friends) 34 * dquot->q_flush (xfs_dqflock() and friends) 35 * qi->qi_lru_lock 36 * 37 * If two dquots need to be locked the order is user before group/project, 38 * otherwise by the lowest id first, see xfs_dqlock2. 39 */ 40 41 struct kmem_zone *xfs_qm_dqtrxzone; 42 static struct kmem_zone *xfs_qm_dqzone; 43 44 static struct lock_class_key xfs_dquot_group_class; 45 static struct lock_class_key xfs_dquot_project_class; 46 47 /* 48 * This is called to free all the memory associated with a dquot 49 */ 50 void 51 xfs_qm_dqdestroy( 52 struct xfs_dquot *dqp) 53 { 54 ASSERT(list_empty(&dqp->q_lru)); 55 56 kmem_free(dqp->q_logitem.qli_item.li_lv_shadow); 57 mutex_destroy(&dqp->q_qlock); 58 59 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot); 60 kmem_cache_free(xfs_qm_dqzone, dqp); 61 } 62 63 /* 64 * If default limits are in force, push them into the dquot now. 65 * We overwrite the dquot limits only if they are zero and this 66 * is not the root dquot. 67 */ 68 void 69 xfs_qm_adjust_dqlimits( 70 struct xfs_mount *mp, 71 struct xfs_dquot *dq) 72 { 73 struct xfs_quotainfo *q = mp->m_quotainfo; 74 struct xfs_disk_dquot *d = &dq->q_core; 75 struct xfs_def_quota *defq; 76 int prealloc = 0; 77 78 ASSERT(d->d_id); 79 defq = xfs_get_defquota(q, xfs_dquot_type(dq)); 80 81 if (defq->bsoftlimit && !d->d_blk_softlimit) { 82 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit); 83 prealloc = 1; 84 } 85 if (defq->bhardlimit && !d->d_blk_hardlimit) { 86 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit); 87 prealloc = 1; 88 } 89 if (defq->isoftlimit && !d->d_ino_softlimit) 90 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit); 91 if (defq->ihardlimit && !d->d_ino_hardlimit) 92 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit); 93 if (defq->rtbsoftlimit && !d->d_rtb_softlimit) 94 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit); 95 if (defq->rtbhardlimit && !d->d_rtb_hardlimit) 96 d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit); 97 98 if (prealloc) 99 xfs_dquot_set_prealloc_limits(dq); 100 } 101 102 /* 103 * Check the limits and timers of a dquot and start or reset timers 104 * if necessary. 105 * This gets called even when quota enforcement is OFF, which makes our 106 * life a little less complicated. (We just don't reject any quota 107 * reservations in that case, when enforcement is off). 108 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when 109 * enforcement's off. 110 * In contrast, warnings are a little different in that they don't 111 * 'automatically' get started when limits get exceeded. They do 112 * get reset to zero, however, when we find the count to be under 113 * the soft limit (they are only ever set non-zero via userspace). 114 */ 115 void 116 xfs_qm_adjust_dqtimers( 117 struct xfs_mount *mp, 118 struct xfs_dquot *dq) 119 { 120 struct xfs_quotainfo *qi = mp->m_quotainfo; 121 struct xfs_disk_dquot *d = &dq->q_core; 122 struct xfs_def_quota *defq; 123 124 ASSERT(d->d_id); 125 defq = xfs_get_defquota(qi, xfs_dquot_type(dq)); 126 127 #ifdef DEBUG 128 if (d->d_blk_hardlimit) 129 ASSERT(be64_to_cpu(d->d_blk_softlimit) <= 130 be64_to_cpu(d->d_blk_hardlimit)); 131 if (d->d_ino_hardlimit) 132 ASSERT(be64_to_cpu(d->d_ino_softlimit) <= 133 be64_to_cpu(d->d_ino_hardlimit)); 134 if (d->d_rtb_hardlimit) 135 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <= 136 be64_to_cpu(d->d_rtb_hardlimit)); 137 #endif 138 139 if (!d->d_btimer) { 140 if ((d->d_blk_softlimit && 141 (be64_to_cpu(d->d_bcount) > 142 be64_to_cpu(d->d_blk_softlimit))) || 143 (d->d_blk_hardlimit && 144 (be64_to_cpu(d->d_bcount) > 145 be64_to_cpu(d->d_blk_hardlimit)))) { 146 d->d_btimer = cpu_to_be32(ktime_get_real_seconds() + 147 defq->btimelimit); 148 } else { 149 d->d_bwarns = 0; 150 } 151 } else { 152 if ((!d->d_blk_softlimit || 153 (be64_to_cpu(d->d_bcount) <= 154 be64_to_cpu(d->d_blk_softlimit))) && 155 (!d->d_blk_hardlimit || 156 (be64_to_cpu(d->d_bcount) <= 157 be64_to_cpu(d->d_blk_hardlimit)))) { 158 d->d_btimer = 0; 159 } 160 } 161 162 if (!d->d_itimer) { 163 if ((d->d_ino_softlimit && 164 (be64_to_cpu(d->d_icount) > 165 be64_to_cpu(d->d_ino_softlimit))) || 166 (d->d_ino_hardlimit && 167 (be64_to_cpu(d->d_icount) > 168 be64_to_cpu(d->d_ino_hardlimit)))) { 169 d->d_itimer = cpu_to_be32(ktime_get_real_seconds() + 170 defq->itimelimit); 171 } else { 172 d->d_iwarns = 0; 173 } 174 } else { 175 if ((!d->d_ino_softlimit || 176 (be64_to_cpu(d->d_icount) <= 177 be64_to_cpu(d->d_ino_softlimit))) && 178 (!d->d_ino_hardlimit || 179 (be64_to_cpu(d->d_icount) <= 180 be64_to_cpu(d->d_ino_hardlimit)))) { 181 d->d_itimer = 0; 182 } 183 } 184 185 if (!d->d_rtbtimer) { 186 if ((d->d_rtb_softlimit && 187 (be64_to_cpu(d->d_rtbcount) > 188 be64_to_cpu(d->d_rtb_softlimit))) || 189 (d->d_rtb_hardlimit && 190 (be64_to_cpu(d->d_rtbcount) > 191 be64_to_cpu(d->d_rtb_hardlimit)))) { 192 d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() + 193 defq->rtbtimelimit); 194 } else { 195 d->d_rtbwarns = 0; 196 } 197 } else { 198 if ((!d->d_rtb_softlimit || 199 (be64_to_cpu(d->d_rtbcount) <= 200 be64_to_cpu(d->d_rtb_softlimit))) && 201 (!d->d_rtb_hardlimit || 202 (be64_to_cpu(d->d_rtbcount) <= 203 be64_to_cpu(d->d_rtb_hardlimit)))) { 204 d->d_rtbtimer = 0; 205 } 206 } 207 } 208 209 /* 210 * initialize a buffer full of dquots and log the whole thing 211 */ 212 STATIC void 213 xfs_qm_init_dquot_blk( 214 struct xfs_trans *tp, 215 struct xfs_mount *mp, 216 xfs_dqid_t id, 217 uint type, 218 struct xfs_buf *bp) 219 { 220 struct xfs_quotainfo *q = mp->m_quotainfo; 221 struct xfs_dqblk *d; 222 xfs_dqid_t curid; 223 unsigned int qflag; 224 unsigned int blftype; 225 int i; 226 227 ASSERT(tp); 228 ASSERT(xfs_buf_islocked(bp)); 229 230 d = bp->b_addr; 231 232 /* 233 * ID of the first dquot in the block - id's are zero based. 234 */ 235 curid = id - (id % q->qi_dqperchunk); 236 memset(d, 0, BBTOB(q->qi_dqchunklen)); 237 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) { 238 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC); 239 d->dd_diskdq.d_version = XFS_DQUOT_VERSION; 240 d->dd_diskdq.d_id = cpu_to_be32(curid); 241 d->dd_diskdq.d_flags = type; 242 if (xfs_sb_version_hascrc(&mp->m_sb)) { 243 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid); 244 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk), 245 XFS_DQUOT_CRC_OFF); 246 } 247 } 248 249 if (type & XFS_DQ_USER) { 250 qflag = XFS_UQUOTA_CHKD; 251 blftype = XFS_BLF_UDQUOT_BUF; 252 } else if (type & XFS_DQ_PROJ) { 253 qflag = XFS_PQUOTA_CHKD; 254 blftype = XFS_BLF_PDQUOT_BUF; 255 } else { 256 qflag = XFS_GQUOTA_CHKD; 257 blftype = XFS_BLF_GDQUOT_BUF; 258 } 259 260 xfs_trans_dquot_buf(tp, bp, blftype); 261 262 /* 263 * quotacheck uses delayed writes to update all the dquots on disk in an 264 * efficient manner instead of logging the individual dquot changes as 265 * they are made. However if we log the buffer allocated here and crash 266 * after quotacheck while the logged initialisation is still in the 267 * active region of the log, log recovery can replay the dquot buffer 268 * initialisation over the top of the checked dquots and corrupt quota 269 * accounting. 270 * 271 * To avoid this problem, quotacheck cannot log the initialised buffer. 272 * We must still dirty the buffer and write it back before the 273 * allocation transaction clears the log. Therefore, mark the buffer as 274 * ordered instead of logging it directly. This is safe for quotacheck 275 * because it detects and repairs allocated but initialized dquot blocks 276 * in the quota inodes. 277 */ 278 if (!(mp->m_qflags & qflag)) 279 xfs_trans_ordered_buf(tp, bp); 280 else 281 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1); 282 } 283 284 /* 285 * Initialize the dynamic speculative preallocation thresholds. The lo/hi 286 * watermarks correspond to the soft and hard limits by default. If a soft limit 287 * is not specified, we use 95% of the hard limit. 288 */ 289 void 290 xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp) 291 { 292 uint64_t space; 293 294 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit); 295 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit); 296 if (!dqp->q_prealloc_lo_wmark) { 297 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark; 298 do_div(dqp->q_prealloc_lo_wmark, 100); 299 dqp->q_prealloc_lo_wmark *= 95; 300 } 301 302 space = dqp->q_prealloc_hi_wmark; 303 304 do_div(space, 100); 305 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space; 306 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3; 307 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5; 308 } 309 310 /* 311 * Ensure that the given in-core dquot has a buffer on disk backing it, and 312 * return the buffer locked and held. This is called when the bmapi finds a 313 * hole. 314 */ 315 STATIC int 316 xfs_dquot_disk_alloc( 317 struct xfs_trans **tpp, 318 struct xfs_dquot *dqp, 319 struct xfs_buf **bpp) 320 { 321 struct xfs_bmbt_irec map; 322 struct xfs_trans *tp = *tpp; 323 struct xfs_mount *mp = tp->t_mountp; 324 struct xfs_buf *bp; 325 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags); 326 int nmaps = 1; 327 int error; 328 329 trace_xfs_dqalloc(dqp); 330 331 xfs_ilock(quotip, XFS_ILOCK_EXCL); 332 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) { 333 /* 334 * Return if this type of quotas is turned off while we didn't 335 * have an inode lock 336 */ 337 xfs_iunlock(quotip, XFS_ILOCK_EXCL); 338 return -ESRCH; 339 } 340 341 /* Create the block mapping. */ 342 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL); 343 error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset, 344 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map, 345 &nmaps); 346 if (error) 347 return error; 348 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB); 349 ASSERT(nmaps == 1); 350 ASSERT((map.br_startblock != DELAYSTARTBLOCK) && 351 (map.br_startblock != HOLESTARTBLOCK)); 352 353 /* 354 * Keep track of the blkno to save a lookup later 355 */ 356 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock); 357 358 /* now we can just get the buffer (there's nothing to read yet) */ 359 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno, 360 mp->m_quotainfo->qi_dqchunklen, 0, &bp); 361 if (error) 362 return error; 363 bp->b_ops = &xfs_dquot_buf_ops; 364 365 /* 366 * Make a chunk of dquots out of this buffer and log 367 * the entire thing. 368 */ 369 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id), 370 dqp->dq_flags & XFS_DQ_ALLTYPES, bp); 371 xfs_buf_set_ref(bp, XFS_DQUOT_REF); 372 373 /* 374 * Hold the buffer and join it to the dfops so that we'll still own 375 * the buffer when we return to the caller. The buffer disposal on 376 * error must be paid attention to very carefully, as it has been 377 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota 378 * code when allocating a new dquot record" in 2005, and the later 379 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep 380 * the buffer locked across the _defer_finish call. We can now do 381 * this correctly with xfs_defer_bjoin. 382 * 383 * Above, we allocated a disk block for the dquot information and used 384 * get_buf to initialize the dquot. If the _defer_finish fails, the old 385 * transaction is gone but the new buffer is not joined or held to any 386 * transaction, so we must _buf_relse it. 387 * 388 * If everything succeeds, the caller of this function is returned a 389 * buffer that is locked and held to the transaction. The caller 390 * is responsible for unlocking any buffer passed back, either 391 * manually or by committing the transaction. On error, the buffer is 392 * released and not passed back. 393 */ 394 xfs_trans_bhold(tp, bp); 395 error = xfs_defer_finish(tpp); 396 if (error) { 397 xfs_trans_bhold_release(*tpp, bp); 398 xfs_trans_brelse(*tpp, bp); 399 return error; 400 } 401 *bpp = bp; 402 return 0; 403 } 404 405 /* 406 * Read in the in-core dquot's on-disk metadata and return the buffer. 407 * Returns ENOENT to signal a hole. 408 */ 409 STATIC int 410 xfs_dquot_disk_read( 411 struct xfs_mount *mp, 412 struct xfs_dquot *dqp, 413 struct xfs_buf **bpp) 414 { 415 struct xfs_bmbt_irec map; 416 struct xfs_buf *bp; 417 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags); 418 uint lock_mode; 419 int nmaps = 1; 420 int error; 421 422 lock_mode = xfs_ilock_data_map_shared(quotip); 423 if (!xfs_this_quota_on(mp, dqp->dq_flags)) { 424 /* 425 * Return if this type of quotas is turned off while we 426 * didn't have the quota inode lock. 427 */ 428 xfs_iunlock(quotip, lock_mode); 429 return -ESRCH; 430 } 431 432 /* 433 * Find the block map; no allocations yet 434 */ 435 error = xfs_bmapi_read(quotip, dqp->q_fileoffset, 436 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0); 437 xfs_iunlock(quotip, lock_mode); 438 if (error) 439 return error; 440 441 ASSERT(nmaps == 1); 442 ASSERT(map.br_blockcount >= 1); 443 ASSERT(map.br_startblock != DELAYSTARTBLOCK); 444 if (map.br_startblock == HOLESTARTBLOCK) 445 return -ENOENT; 446 447 trace_xfs_dqtobp_read(dqp); 448 449 /* 450 * store the blkno etc so that we don't have to do the 451 * mapping all the time 452 */ 453 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock); 454 455 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno, 456 mp->m_quotainfo->qi_dqchunklen, 0, &bp, 457 &xfs_dquot_buf_ops); 458 if (error) { 459 ASSERT(bp == NULL); 460 return error; 461 } 462 463 ASSERT(xfs_buf_islocked(bp)); 464 xfs_buf_set_ref(bp, XFS_DQUOT_REF); 465 *bpp = bp; 466 467 return 0; 468 } 469 470 /* Allocate and initialize everything we need for an incore dquot. */ 471 STATIC struct xfs_dquot * 472 xfs_dquot_alloc( 473 struct xfs_mount *mp, 474 xfs_dqid_t id, 475 uint type) 476 { 477 struct xfs_dquot *dqp; 478 479 dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0); 480 481 dqp->dq_flags = type; 482 dqp->q_core.d_id = cpu_to_be32(id); 483 dqp->q_mount = mp; 484 INIT_LIST_HEAD(&dqp->q_lru); 485 mutex_init(&dqp->q_qlock); 486 init_waitqueue_head(&dqp->q_pinwait); 487 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk; 488 /* 489 * Offset of dquot in the (fixed sized) dquot chunk. 490 */ 491 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) * 492 sizeof(xfs_dqblk_t); 493 494 /* 495 * Because we want to use a counting completion, complete 496 * the flush completion once to allow a single access to 497 * the flush completion without blocking. 498 */ 499 init_completion(&dqp->q_flush); 500 complete(&dqp->q_flush); 501 502 /* 503 * Make sure group quotas have a different lock class than user 504 * quotas. 505 */ 506 switch (type) { 507 case XFS_DQ_USER: 508 /* uses the default lock class */ 509 break; 510 case XFS_DQ_GROUP: 511 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class); 512 break; 513 case XFS_DQ_PROJ: 514 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class); 515 break; 516 default: 517 ASSERT(0); 518 break; 519 } 520 521 xfs_qm_dquot_logitem_init(dqp); 522 523 XFS_STATS_INC(mp, xs_qm_dquot); 524 return dqp; 525 } 526 527 /* Copy the in-core quota fields in from the on-disk buffer. */ 528 STATIC int 529 xfs_dquot_from_disk( 530 struct xfs_dquot *dqp, 531 struct xfs_buf *bp) 532 { 533 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset; 534 535 /* 536 * Ensure that we got the type and ID we were looking for. 537 * Everything else was checked by the dquot buffer verifier. 538 */ 539 if ((ddqp->d_flags & XFS_DQ_ALLTYPES) != dqp->dq_flags || 540 ddqp->d_id != dqp->q_core.d_id) { 541 xfs_alert_tag(bp->b_mount, XFS_PTAG_VERIFIER_ERROR, 542 "Metadata corruption detected at %pS, quota %u", 543 __this_address, be32_to_cpu(dqp->q_core.d_id)); 544 xfs_alert(bp->b_mount, "Unmount and run xfs_repair"); 545 return -EFSCORRUPTED; 546 } 547 548 /* copy everything from disk dquot to the incore dquot */ 549 memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot)); 550 551 /* 552 * Reservation counters are defined as reservation plus current usage 553 * to avoid having to add every time. 554 */ 555 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount); 556 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount); 557 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount); 558 559 /* initialize the dquot speculative prealloc thresholds */ 560 xfs_dquot_set_prealloc_limits(dqp); 561 return 0; 562 } 563 564 /* Allocate and initialize the dquot buffer for this in-core dquot. */ 565 static int 566 xfs_qm_dqread_alloc( 567 struct xfs_mount *mp, 568 struct xfs_dquot *dqp, 569 struct xfs_buf **bpp) 570 { 571 struct xfs_trans *tp; 572 int error; 573 574 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc, 575 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp); 576 if (error) 577 goto err; 578 579 error = xfs_dquot_disk_alloc(&tp, dqp, bpp); 580 if (error) 581 goto err_cancel; 582 583 error = xfs_trans_commit(tp); 584 if (error) { 585 /* 586 * Buffer was held to the transaction, so we have to unlock it 587 * manually here because we're not passing it back. 588 */ 589 xfs_buf_relse(*bpp); 590 *bpp = NULL; 591 goto err; 592 } 593 return 0; 594 595 err_cancel: 596 xfs_trans_cancel(tp); 597 err: 598 return error; 599 } 600 601 /* 602 * Read in the ondisk dquot using dqtobp() then copy it to an incore version, 603 * and release the buffer immediately. If @can_alloc is true, fill any 604 * holes in the on-disk metadata. 605 */ 606 static int 607 xfs_qm_dqread( 608 struct xfs_mount *mp, 609 xfs_dqid_t id, 610 uint type, 611 bool can_alloc, 612 struct xfs_dquot **dqpp) 613 { 614 struct xfs_dquot *dqp; 615 struct xfs_buf *bp; 616 int error; 617 618 dqp = xfs_dquot_alloc(mp, id, type); 619 trace_xfs_dqread(dqp); 620 621 /* Try to read the buffer, allocating if necessary. */ 622 error = xfs_dquot_disk_read(mp, dqp, &bp); 623 if (error == -ENOENT && can_alloc) 624 error = xfs_qm_dqread_alloc(mp, dqp, &bp); 625 if (error) 626 goto err; 627 628 /* 629 * At this point we should have a clean locked buffer. Copy the data 630 * to the incore dquot and release the buffer since the incore dquot 631 * has its own locking protocol so we needn't tie up the buffer any 632 * further. 633 */ 634 ASSERT(xfs_buf_islocked(bp)); 635 error = xfs_dquot_from_disk(dqp, bp); 636 xfs_buf_relse(bp); 637 if (error) 638 goto err; 639 640 *dqpp = dqp; 641 return error; 642 643 err: 644 trace_xfs_dqread_fail(dqp); 645 xfs_qm_dqdestroy(dqp); 646 *dqpp = NULL; 647 return error; 648 } 649 650 /* 651 * Advance to the next id in the current chunk, or if at the 652 * end of the chunk, skip ahead to first id in next allocated chunk 653 * using the SEEK_DATA interface. 654 */ 655 static int 656 xfs_dq_get_next_id( 657 struct xfs_mount *mp, 658 uint type, 659 xfs_dqid_t *id) 660 { 661 struct xfs_inode *quotip = xfs_quota_inode(mp, type); 662 xfs_dqid_t next_id = *id + 1; /* simple advance */ 663 uint lock_flags; 664 struct xfs_bmbt_irec got; 665 struct xfs_iext_cursor cur; 666 xfs_fsblock_t start; 667 int error = 0; 668 669 /* If we'd wrap past the max ID, stop */ 670 if (next_id < *id) 671 return -ENOENT; 672 673 /* If new ID is within the current chunk, advancing it sufficed */ 674 if (next_id % mp->m_quotainfo->qi_dqperchunk) { 675 *id = next_id; 676 return 0; 677 } 678 679 /* Nope, next_id is now past the current chunk, so find the next one */ 680 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk; 681 682 lock_flags = xfs_ilock_data_map_shared(quotip); 683 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) { 684 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK); 685 if (error) 686 return error; 687 } 688 689 if (xfs_iext_lookup_extent(quotip, "ip->i_df, start, &cur, &got)) { 690 /* contiguous chunk, bump startoff for the id calculation */ 691 if (got.br_startoff < start) 692 got.br_startoff = start; 693 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk; 694 } else { 695 error = -ENOENT; 696 } 697 698 xfs_iunlock(quotip, lock_flags); 699 700 return error; 701 } 702 703 /* 704 * Look up the dquot in the in-core cache. If found, the dquot is returned 705 * locked and ready to go. 706 */ 707 static struct xfs_dquot * 708 xfs_qm_dqget_cache_lookup( 709 struct xfs_mount *mp, 710 struct xfs_quotainfo *qi, 711 struct radix_tree_root *tree, 712 xfs_dqid_t id) 713 { 714 struct xfs_dquot *dqp; 715 716 restart: 717 mutex_lock(&qi->qi_tree_lock); 718 dqp = radix_tree_lookup(tree, id); 719 if (!dqp) { 720 mutex_unlock(&qi->qi_tree_lock); 721 XFS_STATS_INC(mp, xs_qm_dqcachemisses); 722 return NULL; 723 } 724 725 xfs_dqlock(dqp); 726 if (dqp->q_flags & XFS_DQFLAG_FREEING) { 727 xfs_dqunlock(dqp); 728 mutex_unlock(&qi->qi_tree_lock); 729 trace_xfs_dqget_freeing(dqp); 730 delay(1); 731 goto restart; 732 } 733 734 dqp->q_nrefs++; 735 mutex_unlock(&qi->qi_tree_lock); 736 737 trace_xfs_dqget_hit(dqp); 738 XFS_STATS_INC(mp, xs_qm_dqcachehits); 739 return dqp; 740 } 741 742 /* 743 * Try to insert a new dquot into the in-core cache. If an error occurs the 744 * caller should throw away the dquot and start over. Otherwise, the dquot 745 * is returned locked (and held by the cache) as if there had been a cache 746 * hit. 747 */ 748 static int 749 xfs_qm_dqget_cache_insert( 750 struct xfs_mount *mp, 751 struct xfs_quotainfo *qi, 752 struct radix_tree_root *tree, 753 xfs_dqid_t id, 754 struct xfs_dquot *dqp) 755 { 756 int error; 757 758 mutex_lock(&qi->qi_tree_lock); 759 error = radix_tree_insert(tree, id, dqp); 760 if (unlikely(error)) { 761 /* Duplicate found! Caller must try again. */ 762 WARN_ON(error != -EEXIST); 763 mutex_unlock(&qi->qi_tree_lock); 764 trace_xfs_dqget_dup(dqp); 765 return error; 766 } 767 768 /* Return a locked dquot to the caller, with a reference taken. */ 769 xfs_dqlock(dqp); 770 dqp->q_nrefs = 1; 771 772 qi->qi_dquots++; 773 mutex_unlock(&qi->qi_tree_lock); 774 775 return 0; 776 } 777 778 /* Check our input parameters. */ 779 static int 780 xfs_qm_dqget_checks( 781 struct xfs_mount *mp, 782 uint type) 783 { 784 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp))) 785 return -ESRCH; 786 787 switch (type) { 788 case XFS_DQ_USER: 789 if (!XFS_IS_UQUOTA_ON(mp)) 790 return -ESRCH; 791 return 0; 792 case XFS_DQ_GROUP: 793 if (!XFS_IS_GQUOTA_ON(mp)) 794 return -ESRCH; 795 return 0; 796 case XFS_DQ_PROJ: 797 if (!XFS_IS_PQUOTA_ON(mp)) 798 return -ESRCH; 799 return 0; 800 default: 801 WARN_ON_ONCE(0); 802 return -EINVAL; 803 } 804 } 805 806 /* 807 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked 808 * dquot, doing an allocation (if requested) as needed. 809 */ 810 int 811 xfs_qm_dqget( 812 struct xfs_mount *mp, 813 xfs_dqid_t id, 814 uint type, 815 bool can_alloc, 816 struct xfs_dquot **O_dqpp) 817 { 818 struct xfs_quotainfo *qi = mp->m_quotainfo; 819 struct radix_tree_root *tree = xfs_dquot_tree(qi, type); 820 struct xfs_dquot *dqp; 821 int error; 822 823 error = xfs_qm_dqget_checks(mp, type); 824 if (error) 825 return error; 826 827 restart: 828 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id); 829 if (dqp) { 830 *O_dqpp = dqp; 831 return 0; 832 } 833 834 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp); 835 if (error) 836 return error; 837 838 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp); 839 if (error) { 840 /* 841 * Duplicate found. Just throw away the new dquot and start 842 * over. 843 */ 844 xfs_qm_dqdestroy(dqp); 845 XFS_STATS_INC(mp, xs_qm_dquot_dups); 846 goto restart; 847 } 848 849 trace_xfs_dqget_miss(dqp); 850 *O_dqpp = dqp; 851 return 0; 852 } 853 854 /* 855 * Given a dquot id and type, read and initialize a dquot from the on-disk 856 * metadata. This function is only for use during quota initialization so 857 * it ignores the dquot cache assuming that the dquot shrinker isn't set up. 858 * The caller is responsible for _qm_dqdestroy'ing the returned dquot. 859 */ 860 int 861 xfs_qm_dqget_uncached( 862 struct xfs_mount *mp, 863 xfs_dqid_t id, 864 uint type, 865 struct xfs_dquot **dqpp) 866 { 867 int error; 868 869 error = xfs_qm_dqget_checks(mp, type); 870 if (error) 871 return error; 872 873 return xfs_qm_dqread(mp, id, type, 0, dqpp); 874 } 875 876 /* Return the quota id for a given inode and type. */ 877 xfs_dqid_t 878 xfs_qm_id_for_quotatype( 879 struct xfs_inode *ip, 880 uint type) 881 { 882 switch (type) { 883 case XFS_DQ_USER: 884 return i_uid_read(VFS_I(ip)); 885 case XFS_DQ_GROUP: 886 return i_gid_read(VFS_I(ip)); 887 case XFS_DQ_PROJ: 888 return ip->i_d.di_projid; 889 } 890 ASSERT(0); 891 return 0; 892 } 893 894 /* 895 * Return the dquot for a given inode and type. If @can_alloc is true, then 896 * allocate blocks if needed. The inode's ILOCK must be held and it must not 897 * have already had an inode attached. 898 */ 899 int 900 xfs_qm_dqget_inode( 901 struct xfs_inode *ip, 902 uint type, 903 bool can_alloc, 904 struct xfs_dquot **O_dqpp) 905 { 906 struct xfs_mount *mp = ip->i_mount; 907 struct xfs_quotainfo *qi = mp->m_quotainfo; 908 struct radix_tree_root *tree = xfs_dquot_tree(qi, type); 909 struct xfs_dquot *dqp; 910 xfs_dqid_t id; 911 int error; 912 913 error = xfs_qm_dqget_checks(mp, type); 914 if (error) 915 return error; 916 917 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 918 ASSERT(xfs_inode_dquot(ip, type) == NULL); 919 920 id = xfs_qm_id_for_quotatype(ip, type); 921 922 restart: 923 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id); 924 if (dqp) { 925 *O_dqpp = dqp; 926 return 0; 927 } 928 929 /* 930 * Dquot cache miss. We don't want to keep the inode lock across 931 * a (potential) disk read. Also we don't want to deal with the lock 932 * ordering between quotainode and this inode. OTOH, dropping the inode 933 * lock here means dealing with a chown that can happen before 934 * we re-acquire the lock. 935 */ 936 xfs_iunlock(ip, XFS_ILOCK_EXCL); 937 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp); 938 xfs_ilock(ip, XFS_ILOCK_EXCL); 939 if (error) 940 return error; 941 942 /* 943 * A dquot could be attached to this inode by now, since we had 944 * dropped the ilock. 945 */ 946 if (xfs_this_quota_on(mp, type)) { 947 struct xfs_dquot *dqp1; 948 949 dqp1 = xfs_inode_dquot(ip, type); 950 if (dqp1) { 951 xfs_qm_dqdestroy(dqp); 952 dqp = dqp1; 953 xfs_dqlock(dqp); 954 goto dqret; 955 } 956 } else { 957 /* inode stays locked on return */ 958 xfs_qm_dqdestroy(dqp); 959 return -ESRCH; 960 } 961 962 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp); 963 if (error) { 964 /* 965 * Duplicate found. Just throw away the new dquot and start 966 * over. 967 */ 968 xfs_qm_dqdestroy(dqp); 969 XFS_STATS_INC(mp, xs_qm_dquot_dups); 970 goto restart; 971 } 972 973 dqret: 974 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 975 trace_xfs_dqget_miss(dqp); 976 *O_dqpp = dqp; 977 return 0; 978 } 979 980 /* 981 * Starting at @id and progressing upwards, look for an initialized incore 982 * dquot, lock it, and return it. 983 */ 984 int 985 xfs_qm_dqget_next( 986 struct xfs_mount *mp, 987 xfs_dqid_t id, 988 uint type, 989 struct xfs_dquot **dqpp) 990 { 991 struct xfs_dquot *dqp; 992 int error = 0; 993 994 *dqpp = NULL; 995 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) { 996 error = xfs_qm_dqget(mp, id, type, false, &dqp); 997 if (error == -ENOENT) 998 continue; 999 else if (error != 0) 1000 break; 1001 1002 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) { 1003 *dqpp = dqp; 1004 return 0; 1005 } 1006 1007 xfs_qm_dqput(dqp); 1008 } 1009 1010 return error; 1011 } 1012 1013 /* 1014 * Release a reference to the dquot (decrement ref-count) and unlock it. 1015 * 1016 * If there is a group quota attached to this dquot, carefully release that 1017 * too without tripping over deadlocks'n'stuff. 1018 */ 1019 void 1020 xfs_qm_dqput( 1021 struct xfs_dquot *dqp) 1022 { 1023 ASSERT(dqp->q_nrefs > 0); 1024 ASSERT(XFS_DQ_IS_LOCKED(dqp)); 1025 1026 trace_xfs_dqput(dqp); 1027 1028 if (--dqp->q_nrefs == 0) { 1029 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo; 1030 trace_xfs_dqput_free(dqp); 1031 1032 if (list_lru_add(&qi->qi_lru, &dqp->q_lru)) 1033 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused); 1034 } 1035 xfs_dqunlock(dqp); 1036 } 1037 1038 /* 1039 * Release a dquot. Flush it if dirty, then dqput() it. 1040 * dquot must not be locked. 1041 */ 1042 void 1043 xfs_qm_dqrele( 1044 struct xfs_dquot *dqp) 1045 { 1046 if (!dqp) 1047 return; 1048 1049 trace_xfs_dqrele(dqp); 1050 1051 xfs_dqlock(dqp); 1052 /* 1053 * We don't care to flush it if the dquot is dirty here. 1054 * That will create stutters that we want to avoid. 1055 * Instead we do a delayed write when we try to reclaim 1056 * a dirty dquot. Also xfs_sync will take part of the burden... 1057 */ 1058 xfs_qm_dqput(dqp); 1059 } 1060 1061 /* 1062 * This is the dquot flushing I/O completion routine. It is called 1063 * from interrupt level when the buffer containing the dquot is 1064 * flushed to disk. It is responsible for removing the dquot logitem 1065 * from the AIL if it has not been re-logged, and unlocking the dquot's 1066 * flush lock. This behavior is very similar to that of inodes.. 1067 */ 1068 static void 1069 xfs_qm_dqflush_done( 1070 struct xfs_log_item *lip) 1071 { 1072 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip; 1073 struct xfs_dquot *dqp = qip->qli_dquot; 1074 struct xfs_ail *ailp = lip->li_ailp; 1075 xfs_lsn_t tail_lsn; 1076 1077 /* 1078 * We only want to pull the item from the AIL if its 1079 * location in the log has not changed since we started the flush. 1080 * Thus, we only bother if the dquot's lsn has 1081 * not changed. First we check the lsn outside the lock 1082 * since it's cheaper, and then we recheck while 1083 * holding the lock before removing the dquot from the AIL. 1084 */ 1085 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) && 1086 ((lip->li_lsn == qip->qli_flush_lsn) || 1087 test_bit(XFS_LI_FAILED, &lip->li_flags))) { 1088 1089 spin_lock(&ailp->ail_lock); 1090 xfs_clear_li_failed(lip); 1091 if (lip->li_lsn == qip->qli_flush_lsn) { 1092 /* xfs_ail_update_finish() drops the AIL lock */ 1093 tail_lsn = xfs_ail_delete_one(ailp, lip); 1094 xfs_ail_update_finish(ailp, tail_lsn); 1095 } else { 1096 spin_unlock(&ailp->ail_lock); 1097 } 1098 } 1099 1100 /* 1101 * Release the dq's flush lock since we're done with it. 1102 */ 1103 xfs_dqfunlock(dqp); 1104 } 1105 1106 void 1107 xfs_dquot_done( 1108 struct xfs_buf *bp) 1109 { 1110 struct xfs_log_item *lip, *n; 1111 1112 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) { 1113 list_del_init(&lip->li_bio_list); 1114 xfs_qm_dqflush_done(lip); 1115 } 1116 } 1117 1118 /* 1119 * Write a modified dquot to disk. 1120 * The dquot must be locked and the flush lock too taken by caller. 1121 * The flush lock will not be unlocked until the dquot reaches the disk, 1122 * but the dquot is free to be unlocked and modified by the caller 1123 * in the interim. Dquot is still locked on return. This behavior is 1124 * identical to that of inodes. 1125 */ 1126 int 1127 xfs_qm_dqflush( 1128 struct xfs_dquot *dqp, 1129 struct xfs_buf **bpp) 1130 { 1131 struct xfs_mount *mp = dqp->q_mount; 1132 struct xfs_log_item *lip = &dqp->q_logitem.qli_item; 1133 struct xfs_buf *bp; 1134 struct xfs_dqblk *dqb; 1135 struct xfs_disk_dquot *ddqp; 1136 xfs_failaddr_t fa; 1137 int error; 1138 1139 ASSERT(XFS_DQ_IS_LOCKED(dqp)); 1140 ASSERT(!completion_done(&dqp->q_flush)); 1141 1142 trace_xfs_dqflush(dqp); 1143 1144 *bpp = NULL; 1145 1146 xfs_qm_dqunpin_wait(dqp); 1147 1148 /* 1149 * Get the buffer containing the on-disk dquot 1150 */ 1151 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno, 1152 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK, 1153 &bp, &xfs_dquot_buf_ops); 1154 if (error == -EAGAIN) 1155 goto out_unlock; 1156 if (error) 1157 goto out_abort; 1158 1159 /* 1160 * Calculate the location of the dquot inside the buffer. 1161 */ 1162 dqb = bp->b_addr + dqp->q_bufoffset; 1163 ddqp = &dqb->dd_diskdq; 1164 1165 /* sanity check the in-core structure before we flush */ 1166 fa = xfs_dquot_verify(mp, &dqp->q_core, be32_to_cpu(dqp->q_core.d_id), 1167 0); 1168 if (fa) { 1169 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS", 1170 be32_to_cpu(dqp->q_core.d_id), fa); 1171 xfs_buf_relse(bp); 1172 error = -EFSCORRUPTED; 1173 goto out_abort; 1174 } 1175 1176 /* This is the only portion of data that needs to persist */ 1177 memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot)); 1178 1179 /* 1180 * Clear the dirty field and remember the flush lsn for later use. 1181 */ 1182 dqp->q_flags &= ~XFS_DQFLAG_DIRTY; 1183 1184 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn, 1185 &dqp->q_logitem.qli_item.li_lsn); 1186 1187 /* 1188 * copy the lsn into the on-disk dquot now while we have the in memory 1189 * dquot here. This can't be done later in the write verifier as we 1190 * can't get access to the log item at that point in time. 1191 * 1192 * We also calculate the CRC here so that the on-disk dquot in the 1193 * buffer always has a valid CRC. This ensures there is no possibility 1194 * of a dquot without an up-to-date CRC getting to disk. 1195 */ 1196 if (xfs_sb_version_hascrc(&mp->m_sb)) { 1197 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn); 1198 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk), 1199 XFS_DQUOT_CRC_OFF); 1200 } 1201 1202 /* 1203 * Attach the dquot to the buffer so that we can remove this dquot from 1204 * the AIL and release the flush lock once the dquot is synced to disk. 1205 */ 1206 bp->b_flags |= _XBF_DQUOTS; 1207 list_add_tail(&dqp->q_logitem.qli_item.li_bio_list, &bp->b_li_list); 1208 1209 /* 1210 * If the buffer is pinned then push on the log so we won't 1211 * get stuck waiting in the write for too long. 1212 */ 1213 if (xfs_buf_ispinned(bp)) { 1214 trace_xfs_dqflush_force(dqp); 1215 xfs_log_force(mp, 0); 1216 } 1217 1218 trace_xfs_dqflush_done(dqp); 1219 *bpp = bp; 1220 return 0; 1221 1222 out_abort: 1223 dqp->q_flags &= ~XFS_DQFLAG_DIRTY; 1224 xfs_trans_ail_delete(lip, 0); 1225 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 1226 out_unlock: 1227 xfs_dqfunlock(dqp); 1228 return error; 1229 } 1230 1231 /* 1232 * Lock two xfs_dquot structures. 1233 * 1234 * To avoid deadlocks we always lock the quota structure with 1235 * the lowerd id first. 1236 */ 1237 void 1238 xfs_dqlock2( 1239 struct xfs_dquot *d1, 1240 struct xfs_dquot *d2) 1241 { 1242 if (d1 && d2) { 1243 ASSERT(d1 != d2); 1244 if (be32_to_cpu(d1->q_core.d_id) > 1245 be32_to_cpu(d2->q_core.d_id)) { 1246 mutex_lock(&d2->q_qlock); 1247 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED); 1248 } else { 1249 mutex_lock(&d1->q_qlock); 1250 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED); 1251 } 1252 } else if (d1) { 1253 mutex_lock(&d1->q_qlock); 1254 } else if (d2) { 1255 mutex_lock(&d2->q_qlock); 1256 } 1257 } 1258 1259 int __init 1260 xfs_qm_init(void) 1261 { 1262 xfs_qm_dqzone = kmem_cache_create("xfs_dquot", 1263 sizeof(struct xfs_dquot), 1264 0, 0, NULL); 1265 if (!xfs_qm_dqzone) 1266 goto out; 1267 1268 xfs_qm_dqtrxzone = kmem_cache_create("xfs_dqtrx", 1269 sizeof(struct xfs_dquot_acct), 1270 0, 0, NULL); 1271 if (!xfs_qm_dqtrxzone) 1272 goto out_free_dqzone; 1273 1274 return 0; 1275 1276 out_free_dqzone: 1277 kmem_cache_destroy(xfs_qm_dqzone); 1278 out: 1279 return -ENOMEM; 1280 } 1281 1282 void 1283 xfs_qm_exit(void) 1284 { 1285 kmem_cache_destroy(xfs_qm_dqtrxzone); 1286 kmem_cache_destroy(xfs_qm_dqzone); 1287 } 1288 1289 /* 1290 * Iterate every dquot of a particular type. The caller must ensure that the 1291 * particular quota type is active. iter_fn can return negative error codes, 1292 * or -ECANCELED to indicate that it wants to stop iterating. 1293 */ 1294 int 1295 xfs_qm_dqiterate( 1296 struct xfs_mount *mp, 1297 uint dqtype, 1298 xfs_qm_dqiterate_fn iter_fn, 1299 void *priv) 1300 { 1301 struct xfs_dquot *dq; 1302 xfs_dqid_t id = 0; 1303 int error; 1304 1305 do { 1306 error = xfs_qm_dqget_next(mp, id, dqtype, &dq); 1307 if (error == -ENOENT) 1308 return 0; 1309 if (error) 1310 return error; 1311 1312 error = iter_fn(dq, dqtype, priv); 1313 id = be32_to_cpu(dq->q_core.d_id); 1314 xfs_qm_dqput(dq); 1315 id++; 1316 } while (error == 0 && id != 0); 1317 1318 return error; 1319 } 1320