xfs_trans_buf.c 21 KB

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