xfs_trans_buf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 the buffer bp which was previously acquired with one of the
  290. * xfs_trans_... buffer allocation routines if the buffer has not
  291. * been modified within this transaction. If the buffer is modified
  292. * within this transaction, do decrement the recursion count but do
  293. * not release the buffer even if the count goes to 0. If the buffer is not
  294. * modified within the transaction, decrement the recursion count and
  295. * release the buffer if the recursion count goes to 0.
  296. *
  297. * If the buffer is to be released and it was not modified before
  298. * this transaction began, then free the buf_log_item associated with it.
  299. *
  300. * If the transaction pointer is NULL, make this just a normal
  301. * brelse() call.
  302. */
  303. void
  304. xfs_trans_brelse(
  305. xfs_trans_t *tp,
  306. xfs_buf_t *bp)
  307. {
  308. struct xfs_buf_log_item *bip;
  309. int freed;
  310. /*
  311. * Default to a normal brelse() call if the tp is NULL.
  312. */
  313. if (tp == NULL) {
  314. ASSERT(bp->b_transp == NULL);
  315. xfs_buf_relse(bp);
  316. return;
  317. }
  318. ASSERT(bp->b_transp == tp);
  319. bip = bp->b_log_item;
  320. ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
  321. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  322. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  323. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  324. trace_xfs_trans_brelse(bip);
  325. /*
  326. * If the release is just for a recursive lock,
  327. * then decrement the count and return.
  328. */
  329. if (bip->bli_recur > 0) {
  330. bip->bli_recur--;
  331. return;
  332. }
  333. /*
  334. * If the buffer is dirty within this transaction, we can't
  335. * release it until we commit.
  336. */
  337. if (test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags))
  338. return;
  339. /*
  340. * If the buffer has been invalidated, then we can't release
  341. * it until the transaction commits to disk unless it is re-dirtied
  342. * as part of this transaction. This prevents us from pulling
  343. * the item from the AIL before we should.
  344. */
  345. if (bip->bli_flags & XFS_BLI_STALE)
  346. return;
  347. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  348. /*
  349. * Free up the log item descriptor tracking the released item.
  350. */
  351. xfs_trans_del_item(&bip->bli_item);
  352. /*
  353. * Clear the hold flag in the buf log item if it is set.
  354. * We wouldn't want the next user of the buffer to
  355. * get confused.
  356. */
  357. if (bip->bli_flags & XFS_BLI_HOLD) {
  358. bip->bli_flags &= ~XFS_BLI_HOLD;
  359. }
  360. /*
  361. * Drop our reference to the buf log item.
  362. */
  363. freed = atomic_dec_and_test(&bip->bli_refcount);
  364. /*
  365. * If the buf item is not tracking data in the log, then we must free it
  366. * before releasing the buffer back to the free pool.
  367. *
  368. * If the fs has shutdown and we dropped the last reference, it may fall
  369. * on us to release a (possibly dirty) bli if it never made it to the
  370. * AIL (e.g., the aborted unpin already happened and didn't release it
  371. * due to our reference). Since we're already shutdown and need
  372. * ail_lock, just force remove from the AIL and release the bli here.
  373. */
  374. if (XFS_FORCED_SHUTDOWN(tp->t_mountp) && freed) {
  375. xfs_trans_ail_remove(&bip->bli_item, SHUTDOWN_LOG_IO_ERROR);
  376. xfs_buf_item_relse(bp);
  377. } else if (!(bip->bli_flags & XFS_BLI_DIRTY)) {
  378. /***
  379. ASSERT(bp->b_pincount == 0);
  380. ***/
  381. ASSERT(atomic_read(&bip->bli_refcount) == 0);
  382. ASSERT(!test_bit(XFS_LI_IN_AIL, &bip->bli_item.li_flags));
  383. ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
  384. xfs_buf_item_relse(bp);
  385. }
  386. bp->b_transp = NULL;
  387. xfs_buf_relse(bp);
  388. }
  389. /*
  390. * Mark the buffer as not needing to be unlocked when the buf item's
  391. * iop_unlock() routine is called. The buffer must already be locked
  392. * and associated with the given transaction.
  393. */
  394. /* ARGSUSED */
  395. void
  396. xfs_trans_bhold(
  397. xfs_trans_t *tp,
  398. xfs_buf_t *bp)
  399. {
  400. struct xfs_buf_log_item *bip = bp->b_log_item;
  401. ASSERT(bp->b_transp == tp);
  402. ASSERT(bip != NULL);
  403. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  404. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  405. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  406. bip->bli_flags |= XFS_BLI_HOLD;
  407. trace_xfs_trans_bhold(bip);
  408. }
  409. /*
  410. * Cancel the previous buffer hold request made on this buffer
  411. * for this transaction.
  412. */
  413. void
  414. xfs_trans_bhold_release(
  415. xfs_trans_t *tp,
  416. xfs_buf_t *bp)
  417. {
  418. struct xfs_buf_log_item *bip = bp->b_log_item;
  419. ASSERT(bp->b_transp == tp);
  420. ASSERT(bip != NULL);
  421. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  422. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  423. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  424. ASSERT(bip->bli_flags & XFS_BLI_HOLD);
  425. bip->bli_flags &= ~XFS_BLI_HOLD;
  426. trace_xfs_trans_bhold_release(bip);
  427. }
  428. /*
  429. * Mark a buffer dirty in the transaction.
  430. */
  431. void
  432. xfs_trans_dirty_buf(
  433. struct xfs_trans *tp,
  434. struct xfs_buf *bp)
  435. {
  436. struct xfs_buf_log_item *bip = bp->b_log_item;
  437. ASSERT(bp->b_transp == tp);
  438. ASSERT(bip != NULL);
  439. ASSERT(bp->b_iodone == NULL ||
  440. bp->b_iodone == xfs_buf_iodone_callbacks);
  441. /*
  442. * Mark the buffer as needing to be written out eventually,
  443. * and set its iodone function to remove the buffer's buf log
  444. * item from the AIL and free it when the buffer is flushed
  445. * to disk. See xfs_buf_attach_iodone() for more details
  446. * on li_cb and xfs_buf_iodone_callbacks().
  447. * If we end up aborting this transaction, we trap this buffer
  448. * inside the b_bdstrat callback so that this won't get written to
  449. * disk.
  450. */
  451. bp->b_flags |= XBF_DONE;
  452. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  453. bp->b_iodone = xfs_buf_iodone_callbacks;
  454. bip->bli_item.li_cb = xfs_buf_iodone;
  455. /*
  456. * If we invalidated the buffer within this transaction, then
  457. * cancel the invalidation now that we're dirtying the buffer
  458. * again. There are no races with the code in xfs_buf_item_unpin(),
  459. * because we have a reference to the buffer this entire time.
  460. */
  461. if (bip->bli_flags & XFS_BLI_STALE) {
  462. bip->bli_flags &= ~XFS_BLI_STALE;
  463. ASSERT(bp->b_flags & XBF_STALE);
  464. bp->b_flags &= ~XBF_STALE;
  465. bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
  466. }
  467. bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
  468. tp->t_flags |= XFS_TRANS_DIRTY;
  469. set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
  470. }
  471. /*
  472. * This is called to mark bytes first through last inclusive of the given
  473. * buffer as needing to be logged when the transaction is committed.
  474. * The buffer must already be associated with the given transaction.
  475. *
  476. * First and last are numbers relative to the beginning of this buffer,
  477. * so the first byte in the buffer is numbered 0 regardless of the
  478. * value of b_blkno.
  479. */
  480. void
  481. xfs_trans_log_buf(
  482. struct xfs_trans *tp,
  483. struct xfs_buf *bp,
  484. uint first,
  485. uint last)
  486. {
  487. struct xfs_buf_log_item *bip = bp->b_log_item;
  488. ASSERT(first <= last && last < BBTOB(bp->b_length));
  489. ASSERT(!(bip->bli_flags & XFS_BLI_ORDERED));
  490. xfs_trans_dirty_buf(tp, bp);
  491. trace_xfs_trans_log_buf(bip);
  492. xfs_buf_item_log(bip, first, last);
  493. }
  494. /*
  495. * Invalidate a buffer that is being used within a transaction.
  496. *
  497. * Typically this is because the blocks in the buffer are being freed, so we
  498. * need to prevent it from being written out when we're done. Allowing it
  499. * to be written again might overwrite data in the free blocks if they are
  500. * reallocated to a file.
  501. *
  502. * We prevent the buffer from being written out by marking it stale. We can't
  503. * get rid of the buf log item at this point because the buffer may still be
  504. * pinned by another transaction. If that is the case, then we'll wait until
  505. * the buffer is committed to disk for the last time (we can tell by the ref
  506. * count) and free it in xfs_buf_item_unpin(). Until that happens we will
  507. * keep the buffer locked so that the buffer and buf log item are not reused.
  508. *
  509. * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
  510. * the buf item. This will be used at recovery time to determine that copies
  511. * of the buffer in the log before this should not be replayed.
  512. *
  513. * We mark the item descriptor and the transaction dirty so that we'll hold
  514. * the buffer until after the commit.
  515. *
  516. * Since we're invalidating the buffer, we also clear the state about which
  517. * parts of the buffer have been logged. We also clear the flag indicating
  518. * that this is an inode buffer since the data in the buffer will no longer
  519. * be valid.
  520. *
  521. * We set the stale bit in the buffer as well since we're getting rid of it.
  522. */
  523. void
  524. xfs_trans_binval(
  525. xfs_trans_t *tp,
  526. xfs_buf_t *bp)
  527. {
  528. struct xfs_buf_log_item *bip = bp->b_log_item;
  529. int i;
  530. ASSERT(bp->b_transp == tp);
  531. ASSERT(bip != NULL);
  532. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  533. trace_xfs_trans_binval(bip);
  534. if (bip->bli_flags & XFS_BLI_STALE) {
  535. /*
  536. * If the buffer is already invalidated, then
  537. * just return.
  538. */
  539. ASSERT(bp->b_flags & XBF_STALE);
  540. ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
  541. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
  542. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
  543. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  544. ASSERT(test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags));
  545. ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
  546. return;
  547. }
  548. xfs_buf_stale(bp);
  549. bip->bli_flags |= XFS_BLI_STALE;
  550. bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
  551. bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
  552. bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
  553. bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
  554. for (i = 0; i < bip->bli_format_count; i++) {
  555. memset(bip->bli_formats[i].blf_data_map, 0,
  556. (bip->bli_formats[i].blf_map_size * sizeof(uint)));
  557. }
  558. set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
  559. tp->t_flags |= XFS_TRANS_DIRTY;
  560. }
  561. /*
  562. * This call is used to indicate that the buffer contains on-disk inodes which
  563. * must be handled specially during recovery. They require special handling
  564. * because only the di_next_unlinked from the inodes in the buffer should be
  565. * recovered. The rest of the data in the buffer is logged via the inodes
  566. * themselves.
  567. *
  568. * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
  569. * transferred to the buffer's log format structure so that we'll know what to
  570. * do at recovery time.
  571. */
  572. void
  573. xfs_trans_inode_buf(
  574. xfs_trans_t *tp,
  575. xfs_buf_t *bp)
  576. {
  577. struct xfs_buf_log_item *bip = bp->b_log_item;
  578. ASSERT(bp->b_transp == tp);
  579. ASSERT(bip != NULL);
  580. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  581. bip->bli_flags |= XFS_BLI_INODE_BUF;
  582. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  583. }
  584. /*
  585. * This call is used to indicate that the buffer is going to
  586. * be staled and was an inode buffer. This means it gets
  587. * special processing during unpin - where any inodes
  588. * associated with the buffer should be removed from ail.
  589. * There is also special processing during recovery,
  590. * any replay of the inodes in the buffer needs to be
  591. * prevented as the buffer may have been reused.
  592. */
  593. void
  594. xfs_trans_stale_inode_buf(
  595. xfs_trans_t *tp,
  596. xfs_buf_t *bp)
  597. {
  598. struct xfs_buf_log_item *bip = bp->b_log_item;
  599. ASSERT(bp->b_transp == tp);
  600. ASSERT(bip != NULL);
  601. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  602. bip->bli_flags |= XFS_BLI_STALE_INODE;
  603. bip->bli_item.li_cb = xfs_buf_iodone;
  604. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  605. }
  606. /*
  607. * Mark the buffer as being one which contains newly allocated
  608. * inodes. We need to make sure that even if this buffer is
  609. * relogged as an 'inode buf' we still recover all of the inode
  610. * images in the face of a crash. This works in coordination with
  611. * xfs_buf_item_committed() to ensure that the buffer remains in the
  612. * AIL at its original location even after it has been relogged.
  613. */
  614. /* ARGSUSED */
  615. void
  616. xfs_trans_inode_alloc_buf(
  617. xfs_trans_t *tp,
  618. xfs_buf_t *bp)
  619. {
  620. struct xfs_buf_log_item *bip = bp->b_log_item;
  621. ASSERT(bp->b_transp == tp);
  622. ASSERT(bip != NULL);
  623. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  624. bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
  625. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  626. }
  627. /*
  628. * Mark the buffer as ordered for this transaction. This means that the contents
  629. * of the buffer are not recorded in the transaction but it is tracked in the
  630. * AIL as though it was. This allows us to record logical changes in
  631. * transactions rather than the physical changes we make to the buffer without
  632. * changing writeback ordering constraints of metadata buffers.
  633. */
  634. bool
  635. xfs_trans_ordered_buf(
  636. struct xfs_trans *tp,
  637. struct xfs_buf *bp)
  638. {
  639. struct xfs_buf_log_item *bip = bp->b_log_item;
  640. ASSERT(bp->b_transp == tp);
  641. ASSERT(bip != NULL);
  642. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  643. if (xfs_buf_item_dirty_format(bip))
  644. return false;
  645. bip->bli_flags |= XFS_BLI_ORDERED;
  646. trace_xfs_buf_item_ordered(bip);
  647. /*
  648. * We don't log a dirty range of an ordered buffer but it still needs
  649. * to be marked dirty and that it has been logged.
  650. */
  651. xfs_trans_dirty_buf(tp, bp);
  652. return true;
  653. }
  654. /*
  655. * Set the type of the buffer for log recovery so that it can correctly identify
  656. * and hence attach the correct buffer ops to the buffer after replay.
  657. */
  658. void
  659. xfs_trans_buf_set_type(
  660. struct xfs_trans *tp,
  661. struct xfs_buf *bp,
  662. enum xfs_blft type)
  663. {
  664. struct xfs_buf_log_item *bip = bp->b_log_item;
  665. if (!tp)
  666. return;
  667. ASSERT(bp->b_transp == tp);
  668. ASSERT(bip != NULL);
  669. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  670. xfs_blft_to_flags(&bip->__bli_format, type);
  671. }
  672. void
  673. xfs_trans_buf_copy_type(
  674. struct xfs_buf *dst_bp,
  675. struct xfs_buf *src_bp)
  676. {
  677. struct xfs_buf_log_item *sbip = src_bp->b_log_item;
  678. struct xfs_buf_log_item *dbip = dst_bp->b_log_item;
  679. enum xfs_blft type;
  680. type = xfs_blft_from_flags(&sbip->__bli_format);
  681. xfs_blft_to_flags(&dbip->__bli_format, type);
  682. }
  683. /*
  684. * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
  685. * dquots. However, unlike in inode buffer recovery, dquot buffers get
  686. * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
  687. * The only thing that makes dquot buffers different from regular
  688. * buffers is that we must not replay dquot bufs when recovering
  689. * if a _corresponding_ quotaoff has happened. We also have to distinguish
  690. * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
  691. * can be turned off independently.
  692. */
  693. /* ARGSUSED */
  694. void
  695. xfs_trans_dquot_buf(
  696. xfs_trans_t *tp,
  697. xfs_buf_t *bp,
  698. uint type)
  699. {
  700. struct xfs_buf_log_item *bip = bp->b_log_item;
  701. ASSERT(type == XFS_BLF_UDQUOT_BUF ||
  702. type == XFS_BLF_PDQUOT_BUF ||
  703. type == XFS_BLF_GDQUOT_BUF);
  704. bip->__bli_format.blf_flags |= type;
  705. switch (type) {
  706. case XFS_BLF_UDQUOT_BUF:
  707. type = XFS_BLFT_UDQUOT_BUF;
  708. break;
  709. case XFS_BLF_PDQUOT_BUF:
  710. type = XFS_BLFT_PDQUOT_BUF;
  711. break;
  712. case XFS_BLF_GDQUOT_BUF:
  713. type = XFS_BLFT_GDQUOT_BUF;
  714. break;
  715. default:
  716. type = XFS_BLFT_UNKNOWN_BUF;
  717. break;
  718. }
  719. xfs_trans_buf_set_type(tp, bp, type);
  720. }