xfs_trans_buf.c 23 KB

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