xfs_trans_buf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002,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_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_buf_item.h"
  16. #include "xfs_trans_priv.h"
  17. #include "xfs_error.h"
  18. #include "xfs_trace.h"
  19. /*
  20. * Check to see if a buffer matching the given parameters is already
  21. * a part of the given transaction.
  22. */
  23. STATIC struct xfs_buf *
  24. xfs_trans_buf_item_match(
  25. struct xfs_trans *tp,
  26. struct xfs_buftarg *target,
  27. struct xfs_buf_map *map,
  28. int nmaps)
  29. {
  30. struct xfs_log_item *lip;
  31. struct xfs_buf_log_item *blip;
  32. int len = 0;
  33. int i;
  34. for (i = 0; i < nmaps; i++)
  35. len += map[i].bm_len;
  36. list_for_each_entry(lip, &tp->t_items, li_trans) {
  37. blip = (struct xfs_buf_log_item *)lip;
  38. if (blip->bli_item.li_type == XFS_LI_BUF &&
  39. blip->bli_buf->b_target == target &&
  40. XFS_BUF_ADDR(blip->bli_buf) == map[0].bm_bn &&
  41. blip->bli_buf->b_length == len) {
  42. ASSERT(blip->bli_buf->b_map_count == nmaps);
  43. return blip->bli_buf;
  44. }
  45. }
  46. return NULL;
  47. }
  48. /*
  49. * Add the locked buffer to the transaction.
  50. *
  51. * The buffer must be locked, and it cannot be associated with any
  52. * transaction.
  53. *
  54. * If the buffer does not yet have a buf log item associated with it,
  55. * then allocate one for it. Then add the buf item to the transaction.
  56. */
  57. STATIC void
  58. _xfs_trans_bjoin(
  59. struct xfs_trans *tp,
  60. struct xfs_buf *bp,
  61. int reset_recur)
  62. {
  63. struct xfs_buf_log_item *bip;
  64. ASSERT(bp->b_transp == NULL);
  65. /*
  66. * The xfs_buf_log_item pointer is stored in b_log_item. If
  67. * it doesn't have one yet, then allocate one and initialize it.
  68. * The checks to see if one is there are in xfs_buf_item_init().
  69. */
  70. xfs_buf_item_init(bp, tp->t_mountp);
  71. bip = bp->b_log_item;
  72. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  73. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  74. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  75. if (reset_recur)
  76. bip->bli_recur = 0;
  77. /*
  78. * Take a reference for this transaction on the buf item.
  79. */
  80. atomic_inc(&bip->bli_refcount);
  81. /*
  82. * Attach the item to the transaction so we can find it in
  83. * xfs_trans_get_buf() and friends.
  84. */
  85. xfs_trans_add_item(tp, &bip->bli_item);
  86. bp->b_transp = tp;
  87. }
  88. void
  89. xfs_trans_bjoin(
  90. struct xfs_trans *tp,
  91. struct xfs_buf *bp)
  92. {
  93. _xfs_trans_bjoin(tp, bp, 0);
  94. trace_xfs_trans_bjoin(bp->b_log_item);
  95. }
  96. /*
  97. * Get and lock the buffer for the caller if it is not already
  98. * locked within the given transaction. If it is already locked
  99. * within the transaction, just increment its lock recursion count
  100. * and return a pointer to it.
  101. *
  102. * If the transaction pointer is NULL, make this just a normal
  103. * get_buf() call.
  104. */
  105. struct xfs_buf *
  106. xfs_trans_get_buf_map(
  107. struct xfs_trans *tp,
  108. struct xfs_buftarg *target,
  109. struct xfs_buf_map *map,
  110. int nmaps,
  111. xfs_buf_flags_t flags)
  112. {
  113. xfs_buf_t *bp;
  114. struct xfs_buf_log_item *bip;
  115. if (!tp)
  116. return xfs_buf_get_map(target, map, nmaps, flags);
  117. /*
  118. * If we find the buffer in the cache with this transaction
  119. * pointer in its b_fsprivate2 field, then we know we already
  120. * have it locked. In this case we just increment the lock
  121. * recursion count and return the buffer to the caller.
  122. */
  123. bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
  124. if (bp != NULL) {
  125. ASSERT(xfs_buf_islocked(bp));
  126. if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
  127. xfs_buf_stale(bp);
  128. bp->b_flags |= XBF_DONE;
  129. }
  130. ASSERT(bp->b_transp == tp);
  131. bip = bp->b_log_item;
  132. ASSERT(bip != NULL);
  133. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  134. bip->bli_recur++;
  135. trace_xfs_trans_get_buf_recur(bip);
  136. return bp;
  137. }
  138. bp = xfs_buf_get_map(target, map, nmaps, flags);
  139. if (bp == NULL) {
  140. return NULL;
  141. }
  142. ASSERT(!bp->b_error);
  143. _xfs_trans_bjoin(tp, bp, 1);
  144. trace_xfs_trans_get_buf(bp->b_log_item);
  145. return bp;
  146. }
  147. /*
  148. * Get and lock the superblock buffer of this file system for the
  149. * given transaction.
  150. *
  151. * We don't need to use incore_match() here, because the superblock
  152. * buffer is a private buffer which we keep a pointer to in the
  153. * mount structure.
  154. */
  155. xfs_buf_t *
  156. xfs_trans_getsb(
  157. xfs_trans_t *tp,
  158. struct xfs_mount *mp,
  159. int flags)
  160. {
  161. xfs_buf_t *bp;
  162. struct xfs_buf_log_item *bip;
  163. /*
  164. * Default to just trying to lock the superblock buffer
  165. * if tp is NULL.
  166. */
  167. if (tp == NULL)
  168. return xfs_getsb(mp, flags);
  169. /*
  170. * If the superblock buffer already has this transaction
  171. * pointer in its b_fsprivate2 field, then we know we already
  172. * have it locked. In this case we just increment the lock
  173. * recursion count and return the buffer to the caller.
  174. */
  175. bp = mp->m_sb_bp;
  176. if (bp->b_transp == tp) {
  177. bip = bp->b_log_item;
  178. ASSERT(bip != NULL);
  179. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  180. bip->bli_recur++;
  181. trace_xfs_trans_getsb_recur(bip);
  182. return bp;
  183. }
  184. bp = xfs_getsb(mp, flags);
  185. if (bp == NULL)
  186. return NULL;
  187. _xfs_trans_bjoin(tp, bp, 1);
  188. trace_xfs_trans_getsb(bp->b_log_item);
  189. return bp;
  190. }
  191. /*
  192. * Get and lock the buffer for the caller if it is not already
  193. * locked within the given transaction. If it has not yet been
  194. * read in, read it from disk. If it is already locked
  195. * within the transaction and already read in, just increment its
  196. * lock recursion count and return a pointer to it.
  197. *
  198. * If the transaction pointer is NULL, make this just a normal
  199. * read_buf() call.
  200. */
  201. int
  202. xfs_trans_read_buf_map(
  203. struct xfs_mount *mp,
  204. struct xfs_trans *tp,
  205. struct xfs_buftarg *target,
  206. struct xfs_buf_map *map,
  207. int nmaps,
  208. xfs_buf_flags_t flags,
  209. struct xfs_buf **bpp,
  210. const struct xfs_buf_ops *ops)
  211. {
  212. struct xfs_buf *bp = NULL;
  213. struct xfs_buf_log_item *bip;
  214. int error;
  215. *bpp = NULL;
  216. /*
  217. * If we find the buffer in the cache with this transaction
  218. * pointer in its b_fsprivate2 field, then we know we already
  219. * have it locked. If it is already read in we just increment
  220. * the lock recursion count and return the buffer to the caller.
  221. * If the buffer is not yet read in, then we read it in, increment
  222. * the lock recursion count, and return it to the caller.
  223. */
  224. if (tp)
  225. bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
  226. if (bp) {
  227. ASSERT(xfs_buf_islocked(bp));
  228. ASSERT(bp->b_transp == tp);
  229. ASSERT(bp->b_log_item != NULL);
  230. ASSERT(!bp->b_error);
  231. ASSERT(bp->b_flags & XBF_DONE);
  232. /*
  233. * We never locked this buf ourselves, so we shouldn't
  234. * brelse it either. Just get out.
  235. */
  236. if (XFS_FORCED_SHUTDOWN(mp)) {
  237. trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
  238. return -EIO;
  239. }
  240. bip = bp->b_log_item;
  241. bip->bli_recur++;
  242. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  243. trace_xfs_trans_read_buf_recur(bip);
  244. *bpp = bp;
  245. return 0;
  246. }
  247. bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
  248. if (!bp) {
  249. if (!(flags & XBF_TRYLOCK))
  250. return -ENOMEM;
  251. return tp ? 0 : -EAGAIN;
  252. }
  253. /*
  254. * If we've had a read error, then the contents of the buffer are
  255. * invalid and should not be used. To ensure that a followup read tries
  256. * to pull the buffer from disk again, we clear the XBF_DONE flag and
  257. * mark the buffer stale. This ensures that anyone who has a current
  258. * reference to the buffer will interpret it's contents correctly and
  259. * future cache lookups will also treat it as an empty, uninitialised
  260. * buffer.
  261. */
  262. if (bp->b_error) {
  263. error = bp->b_error;
  264. if (!XFS_FORCED_SHUTDOWN(mp))
  265. xfs_buf_ioerror_alert(bp, __func__);
  266. bp->b_flags &= ~XBF_DONE;
  267. xfs_buf_stale(bp);
  268. if (tp && (tp->t_flags & XFS_TRANS_DIRTY))
  269. xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
  270. xfs_buf_relse(bp);
  271. /* bad CRC means corrupted metadata */
  272. if (error == -EFSBADCRC)
  273. error = -EFSCORRUPTED;
  274. return error;
  275. }
  276. if (XFS_FORCED_SHUTDOWN(mp)) {
  277. xfs_buf_relse(bp);
  278. trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
  279. return -EIO;
  280. }
  281. if (tp) {
  282. _xfs_trans_bjoin(tp, bp, 1);
  283. trace_xfs_trans_read_buf(bp->b_log_item);
  284. }
  285. *bpp = bp;
  286. return 0;
  287. }
  288. /*
  289. * Release a buffer previously joined to the transaction. If the buffer is
  290. * modified within this transaction, decrement the recursion count but do not
  291. * release the buffer even if the count goes to 0. If the buffer is not modified
  292. * within the transaction, decrement the recursion count and release the buffer
  293. * if the recursion count goes to 0.
  294. *
  295. * If the buffer is to be released and it was not already dirty before this
  296. * transaction began, then also free the buf_log_item associated with it.
  297. *
  298. * If the transaction pointer is NULL, this is a normal xfs_buf_relse() call.
  299. */
  300. void
  301. xfs_trans_brelse(
  302. struct xfs_trans *tp,
  303. struct xfs_buf *bp)
  304. {
  305. struct xfs_buf_log_item *bip = bp->b_log_item;
  306. ASSERT(bp->b_transp == tp);
  307. if (!tp) {
  308. xfs_buf_relse(bp);
  309. return;
  310. }
  311. trace_xfs_trans_brelse(bip);
  312. ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
  313. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  314. /*
  315. * If the release is for a recursive lookup, then decrement the count
  316. * and return.
  317. */
  318. if (bip->bli_recur > 0) {
  319. bip->bli_recur--;
  320. return;
  321. }
  322. /*
  323. * If the buffer is invalidated or dirty in this transaction, we can't
  324. * release it until we commit.
  325. */
  326. if (test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags))
  327. return;
  328. if (bip->bli_flags & XFS_BLI_STALE)
  329. return;
  330. /*
  331. * Unlink the log item from the transaction and clear the hold flag, if
  332. * set. We wouldn't want the next user of the buffer to get confused.
  333. */
  334. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  335. xfs_trans_del_item(&bip->bli_item);
  336. bip->bli_flags &= ~XFS_BLI_HOLD;
  337. /* drop the reference to the bli */
  338. xfs_buf_item_put(bip);
  339. bp->b_transp = NULL;
  340. xfs_buf_relse(bp);
  341. }
  342. /*
  343. * Mark the buffer as not needing to be unlocked when the buf item's
  344. * iop_unlock() routine is called. The buffer must already be locked
  345. * and associated with the given transaction.
  346. */
  347. /* ARGSUSED */
  348. void
  349. xfs_trans_bhold(
  350. xfs_trans_t *tp,
  351. xfs_buf_t *bp)
  352. {
  353. struct xfs_buf_log_item *bip = bp->b_log_item;
  354. ASSERT(bp->b_transp == tp);
  355. ASSERT(bip != NULL);
  356. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  357. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  358. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  359. bip->bli_flags |= XFS_BLI_HOLD;
  360. trace_xfs_trans_bhold(bip);
  361. }
  362. /*
  363. * Cancel the previous buffer hold request made on this buffer
  364. * for this transaction.
  365. */
  366. void
  367. xfs_trans_bhold_release(
  368. xfs_trans_t *tp,
  369. xfs_buf_t *bp)
  370. {
  371. struct xfs_buf_log_item *bip = bp->b_log_item;
  372. ASSERT(bp->b_transp == tp);
  373. ASSERT(bip != NULL);
  374. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  375. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  376. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  377. ASSERT(bip->bli_flags & XFS_BLI_HOLD);
  378. bip->bli_flags &= ~XFS_BLI_HOLD;
  379. trace_xfs_trans_bhold_release(bip);
  380. }
  381. /*
  382. * Mark a buffer dirty in the transaction.
  383. */
  384. void
  385. xfs_trans_dirty_buf(
  386. struct xfs_trans *tp,
  387. struct xfs_buf *bp)
  388. {
  389. struct xfs_buf_log_item *bip = bp->b_log_item;
  390. ASSERT(bp->b_transp == tp);
  391. ASSERT(bip != NULL);
  392. ASSERT(bp->b_iodone == NULL ||
  393. bp->b_iodone == xfs_buf_iodone_callbacks);
  394. /*
  395. * Mark the buffer as needing to be written out eventually,
  396. * and set its iodone function to remove the buffer's buf log
  397. * item from the AIL and free it when the buffer is flushed
  398. * to disk. See xfs_buf_attach_iodone() for more details
  399. * on li_cb and xfs_buf_iodone_callbacks().
  400. * If we end up aborting this transaction, we trap this buffer
  401. * inside the b_bdstrat callback so that this won't get written to
  402. * disk.
  403. */
  404. bp->b_flags |= XBF_DONE;
  405. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  406. bp->b_iodone = xfs_buf_iodone_callbacks;
  407. bip->bli_item.li_cb = xfs_buf_iodone;
  408. /*
  409. * If we invalidated the buffer within this transaction, then
  410. * cancel the invalidation now that we're dirtying the buffer
  411. * again. There are no races with the code in xfs_buf_item_unpin(),
  412. * because we have a reference to the buffer this entire time.
  413. */
  414. if (bip->bli_flags & XFS_BLI_STALE) {
  415. bip->bli_flags &= ~XFS_BLI_STALE;
  416. ASSERT(bp->b_flags & XBF_STALE);
  417. bp->b_flags &= ~XBF_STALE;
  418. bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
  419. }
  420. bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
  421. tp->t_flags |= XFS_TRANS_DIRTY;
  422. set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
  423. }
  424. /*
  425. * This is called to mark bytes first through last inclusive of the given
  426. * buffer as needing to be logged when the transaction is committed.
  427. * The buffer must already be associated with the given transaction.
  428. *
  429. * First and last are numbers relative to the beginning of this buffer,
  430. * so the first byte in the buffer is numbered 0 regardless of the
  431. * value of b_blkno.
  432. */
  433. void
  434. xfs_trans_log_buf(
  435. struct xfs_trans *tp,
  436. struct xfs_buf *bp,
  437. uint first,
  438. uint last)
  439. {
  440. struct xfs_buf_log_item *bip = bp->b_log_item;
  441. ASSERT(first <= last && last < BBTOB(bp->b_length));
  442. ASSERT(!(bip->bli_flags & XFS_BLI_ORDERED));
  443. xfs_trans_dirty_buf(tp, bp);
  444. trace_xfs_trans_log_buf(bip);
  445. xfs_buf_item_log(bip, first, last);
  446. }
  447. /*
  448. * Invalidate a buffer that is being used within a transaction.
  449. *
  450. * Typically this is because the blocks in the buffer are being freed, so we
  451. * need to prevent it from being written out when we're done. Allowing it
  452. * to be written again might overwrite data in the free blocks if they are
  453. * reallocated to a file.
  454. *
  455. * We prevent the buffer from being written out by marking it stale. We can't
  456. * get rid of the buf log item at this point because the buffer may still be
  457. * pinned by another transaction. If that is the case, then we'll wait until
  458. * the buffer is committed to disk for the last time (we can tell by the ref
  459. * count) and free it in xfs_buf_item_unpin(). Until that happens we will
  460. * keep the buffer locked so that the buffer and buf log item are not reused.
  461. *
  462. * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
  463. * the buf item. This will be used at recovery time to determine that copies
  464. * of the buffer in the log before this should not be replayed.
  465. *
  466. * We mark the item descriptor and the transaction dirty so that we'll hold
  467. * the buffer until after the commit.
  468. *
  469. * Since we're invalidating the buffer, we also clear the state about which
  470. * parts of the buffer have been logged. We also clear the flag indicating
  471. * that this is an inode buffer since the data in the buffer will no longer
  472. * be valid.
  473. *
  474. * We set the stale bit in the buffer as well since we're getting rid of it.
  475. */
  476. void
  477. xfs_trans_binval(
  478. xfs_trans_t *tp,
  479. xfs_buf_t *bp)
  480. {
  481. struct xfs_buf_log_item *bip = bp->b_log_item;
  482. int i;
  483. ASSERT(bp->b_transp == tp);
  484. ASSERT(bip != NULL);
  485. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  486. trace_xfs_trans_binval(bip);
  487. if (bip->bli_flags & XFS_BLI_STALE) {
  488. /*
  489. * If the buffer is already invalidated, then
  490. * just return.
  491. */
  492. ASSERT(bp->b_flags & XBF_STALE);
  493. ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
  494. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
  495. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
  496. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  497. ASSERT(test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags));
  498. ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
  499. return;
  500. }
  501. xfs_buf_stale(bp);
  502. bip->bli_flags |= XFS_BLI_STALE;
  503. bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
  504. bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
  505. bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
  506. bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
  507. for (i = 0; i < bip->bli_format_count; i++) {
  508. memset(bip->bli_formats[i].blf_data_map, 0,
  509. (bip->bli_formats[i].blf_map_size * sizeof(uint)));
  510. }
  511. set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
  512. tp->t_flags |= XFS_TRANS_DIRTY;
  513. }
  514. /*
  515. * This call is used to indicate that the buffer contains on-disk inodes which
  516. * must be handled specially during recovery. They require special handling
  517. * because only the di_next_unlinked from the inodes in the buffer should be
  518. * recovered. The rest of the data in the buffer is logged via the inodes
  519. * themselves.
  520. *
  521. * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
  522. * transferred to the buffer's log format structure so that we'll know what to
  523. * do at recovery time.
  524. */
  525. void
  526. xfs_trans_inode_buf(
  527. xfs_trans_t *tp,
  528. xfs_buf_t *bp)
  529. {
  530. struct xfs_buf_log_item *bip = bp->b_log_item;
  531. ASSERT(bp->b_transp == tp);
  532. ASSERT(bip != NULL);
  533. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  534. bip->bli_flags |= XFS_BLI_INODE_BUF;
  535. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  536. }
  537. /*
  538. * This call is used to indicate that the buffer is going to
  539. * be staled and was an inode buffer. This means it gets
  540. * special processing during unpin - where any inodes
  541. * associated with the buffer should be removed from ail.
  542. * There is also special processing during recovery,
  543. * any replay of the inodes in the buffer needs to be
  544. * prevented as the buffer may have been reused.
  545. */
  546. void
  547. xfs_trans_stale_inode_buf(
  548. xfs_trans_t *tp,
  549. xfs_buf_t *bp)
  550. {
  551. struct xfs_buf_log_item *bip = bp->b_log_item;
  552. ASSERT(bp->b_transp == tp);
  553. ASSERT(bip != NULL);
  554. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  555. bip->bli_flags |= XFS_BLI_STALE_INODE;
  556. bip->bli_item.li_cb = xfs_buf_iodone;
  557. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  558. }
  559. /*
  560. * Mark the buffer as being one which contains newly allocated
  561. * inodes. We need to make sure that even if this buffer is
  562. * relogged as an 'inode buf' we still recover all of the inode
  563. * images in the face of a crash. This works in coordination with
  564. * xfs_buf_item_committed() to ensure that the buffer remains in the
  565. * AIL at its original location even after it has been relogged.
  566. */
  567. /* ARGSUSED */
  568. void
  569. xfs_trans_inode_alloc_buf(
  570. xfs_trans_t *tp,
  571. xfs_buf_t *bp)
  572. {
  573. struct xfs_buf_log_item *bip = bp->b_log_item;
  574. ASSERT(bp->b_transp == tp);
  575. ASSERT(bip != NULL);
  576. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  577. bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
  578. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  579. }
  580. /*
  581. * Mark the buffer as ordered for this transaction. This means that the contents
  582. * of the buffer are not recorded in the transaction but it is tracked in the
  583. * AIL as though it was. This allows us to record logical changes in
  584. * transactions rather than the physical changes we make to the buffer without
  585. * changing writeback ordering constraints of metadata buffers.
  586. */
  587. bool
  588. xfs_trans_ordered_buf(
  589. struct xfs_trans *tp,
  590. struct xfs_buf *bp)
  591. {
  592. struct xfs_buf_log_item *bip = bp->b_log_item;
  593. ASSERT(bp->b_transp == tp);
  594. ASSERT(bip != NULL);
  595. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  596. if (xfs_buf_item_dirty_format(bip))
  597. return false;
  598. bip->bli_flags |= XFS_BLI_ORDERED;
  599. trace_xfs_buf_item_ordered(bip);
  600. /*
  601. * We don't log a dirty range of an ordered buffer but it still needs
  602. * to be marked dirty and that it has been logged.
  603. */
  604. xfs_trans_dirty_buf(tp, bp);
  605. return true;
  606. }
  607. /*
  608. * Set the type of the buffer for log recovery so that it can correctly identify
  609. * and hence attach the correct buffer ops to the buffer after replay.
  610. */
  611. void
  612. xfs_trans_buf_set_type(
  613. struct xfs_trans *tp,
  614. struct xfs_buf *bp,
  615. enum xfs_blft type)
  616. {
  617. struct xfs_buf_log_item *bip = bp->b_log_item;
  618. if (!tp)
  619. return;
  620. ASSERT(bp->b_transp == tp);
  621. ASSERT(bip != NULL);
  622. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  623. xfs_blft_to_flags(&bip->__bli_format, type);
  624. }
  625. void
  626. xfs_trans_buf_copy_type(
  627. struct xfs_buf *dst_bp,
  628. struct xfs_buf *src_bp)
  629. {
  630. struct xfs_buf_log_item *sbip = src_bp->b_log_item;
  631. struct xfs_buf_log_item *dbip = dst_bp->b_log_item;
  632. enum xfs_blft type;
  633. type = xfs_blft_from_flags(&sbip->__bli_format);
  634. xfs_blft_to_flags(&dbip->__bli_format, type);
  635. }
  636. /*
  637. * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
  638. * dquots. However, unlike in inode buffer recovery, dquot buffers get
  639. * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
  640. * The only thing that makes dquot buffers different from regular
  641. * buffers is that we must not replay dquot bufs when recovering
  642. * if a _corresponding_ quotaoff has happened. We also have to distinguish
  643. * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
  644. * can be turned off independently.
  645. */
  646. /* ARGSUSED */
  647. void
  648. xfs_trans_dquot_buf(
  649. xfs_trans_t *tp,
  650. xfs_buf_t *bp,
  651. uint type)
  652. {
  653. struct xfs_buf_log_item *bip = bp->b_log_item;
  654. ASSERT(type == XFS_BLF_UDQUOT_BUF ||
  655. type == XFS_BLF_PDQUOT_BUF ||
  656. type == XFS_BLF_GDQUOT_BUF);
  657. bip->__bli_format.blf_flags |= type;
  658. switch (type) {
  659. case XFS_BLF_UDQUOT_BUF:
  660. type = XFS_BLFT_UDQUOT_BUF;
  661. break;
  662. case XFS_BLF_PDQUOT_BUF:
  663. type = XFS_BLFT_PDQUOT_BUF;
  664. break;
  665. case XFS_BLF_GDQUOT_BUF:
  666. type = XFS_BLFT_GDQUOT_BUF;
  667. break;
  668. default:
  669. type = XFS_BLFT_UNKNOWN_BUF;
  670. break;
  671. }
  672. xfs_trans_buf_set_type(tp, bp, type);
  673. }