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