xfs_trans_buf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 (!xfs_buf_item_dirty(bip)) {
  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. * This is called to mark bytes first through last inclusive of the given
  441. * buffer as needing to be logged when the transaction is committed.
  442. * The buffer must already be associated with the given transaction.
  443. *
  444. * First and last are numbers relative to the beginning of this buffer,
  445. * so the first byte in the buffer is numbered 0 regardless of the
  446. * value of b_blkno.
  447. */
  448. void
  449. xfs_trans_log_buf(xfs_trans_t *tp,
  450. xfs_buf_t *bp,
  451. uint first,
  452. uint last)
  453. {
  454. xfs_buf_log_item_t *bip = bp->b_fspriv;
  455. ASSERT(bp->b_transp == tp);
  456. ASSERT(bip != NULL);
  457. ASSERT(first <= last && last < BBTOB(bp->b_length));
  458. ASSERT(bp->b_iodone == NULL ||
  459. bp->b_iodone == xfs_buf_iodone_callbacks);
  460. /*
  461. * Mark the buffer as needing to be written out eventually,
  462. * and set its iodone function to remove the buffer's buf log
  463. * item from the AIL and free it when the buffer is flushed
  464. * to disk. See xfs_buf_attach_iodone() for more details
  465. * on li_cb and xfs_buf_iodone_callbacks().
  466. * If we end up aborting this transaction, we trap this buffer
  467. * inside the b_bdstrat callback so that this won't get written to
  468. * disk.
  469. */
  470. bp->b_flags |= XBF_DONE;
  471. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  472. bp->b_iodone = xfs_buf_iodone_callbacks;
  473. bip->bli_item.li_cb = xfs_buf_iodone;
  474. trace_xfs_trans_log_buf(bip);
  475. /*
  476. * If we invalidated the buffer within this transaction, then
  477. * cancel the invalidation now that we're dirtying the buffer
  478. * again. There are no races with the code in xfs_buf_item_unpin(),
  479. * because we have a reference to the buffer this entire time.
  480. */
  481. if (bip->bli_flags & XFS_BLI_STALE) {
  482. bip->bli_flags &= ~XFS_BLI_STALE;
  483. ASSERT(bp->b_flags & XBF_STALE);
  484. bp->b_flags &= ~XBF_STALE;
  485. bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
  486. }
  487. tp->t_flags |= XFS_TRANS_DIRTY;
  488. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  489. /*
  490. * If we have an ordered buffer we are not logging any dirty range but
  491. * it still needs to be marked dirty and that it has been logged.
  492. */
  493. bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
  494. if (!(bip->bli_flags & XFS_BLI_ORDERED))
  495. xfs_buf_item_log(bip, first, last);
  496. }
  497. /*
  498. * Invalidate a buffer that is being used within a transaction.
  499. *
  500. * Typically this is because the blocks in the buffer are being freed, so we
  501. * need to prevent it from being written out when we're done. Allowing it
  502. * to be written again might overwrite data in the free blocks if they are
  503. * reallocated to a file.
  504. *
  505. * We prevent the buffer from being written out by marking it stale. We can't
  506. * get rid of the buf log item at this point because the buffer may still be
  507. * pinned by another transaction. If that is the case, then we'll wait until
  508. * the buffer is committed to disk for the last time (we can tell by the ref
  509. * count) and free it in xfs_buf_item_unpin(). Until that happens we will
  510. * keep the buffer locked so that the buffer and buf log item are not reused.
  511. *
  512. * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
  513. * the buf item. This will be used at recovery time to determine that copies
  514. * of the buffer in the log before this should not be replayed.
  515. *
  516. * We mark the item descriptor and the transaction dirty so that we'll hold
  517. * the buffer until after the commit.
  518. *
  519. * Since we're invalidating the buffer, we also clear the state about which
  520. * parts of the buffer have been logged. We also clear the flag indicating
  521. * that this is an inode buffer since the data in the buffer will no longer
  522. * be valid.
  523. *
  524. * We set the stale bit in the buffer as well since we're getting rid of it.
  525. */
  526. void
  527. xfs_trans_binval(
  528. xfs_trans_t *tp,
  529. xfs_buf_t *bp)
  530. {
  531. xfs_buf_log_item_t *bip = bp->b_fspriv;
  532. int i;
  533. ASSERT(bp->b_transp == tp);
  534. ASSERT(bip != NULL);
  535. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  536. trace_xfs_trans_binval(bip);
  537. if (bip->bli_flags & XFS_BLI_STALE) {
  538. /*
  539. * If the buffer is already invalidated, then
  540. * just return.
  541. */
  542. ASSERT(bp->b_flags & XBF_STALE);
  543. ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
  544. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
  545. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
  546. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  547. ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
  548. ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
  549. return;
  550. }
  551. xfs_buf_stale(bp);
  552. bip->bli_flags |= XFS_BLI_STALE;
  553. bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
  554. bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
  555. bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
  556. bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
  557. for (i = 0; i < bip->bli_format_count; i++) {
  558. memset(bip->bli_formats[i].blf_data_map, 0,
  559. (bip->bli_formats[i].blf_map_size * sizeof(uint)));
  560. }
  561. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  562. tp->t_flags |= XFS_TRANS_DIRTY;
  563. }
  564. /*
  565. * This call is used to indicate that the buffer contains on-disk inodes which
  566. * must be handled specially during recovery. They require special handling
  567. * because only the di_next_unlinked from the inodes in the buffer should be
  568. * recovered. The rest of the data in the buffer is logged via the inodes
  569. * themselves.
  570. *
  571. * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
  572. * transferred to the buffer's log format structure so that we'll know what to
  573. * do at recovery time.
  574. */
  575. void
  576. xfs_trans_inode_buf(
  577. xfs_trans_t *tp,
  578. xfs_buf_t *bp)
  579. {
  580. xfs_buf_log_item_t *bip = bp->b_fspriv;
  581. ASSERT(bp->b_transp == tp);
  582. ASSERT(bip != NULL);
  583. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  584. bip->bli_flags |= XFS_BLI_INODE_BUF;
  585. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  586. }
  587. /*
  588. * This call is used to indicate that the buffer is going to
  589. * be staled and was an inode buffer. This means it gets
  590. * special processing during unpin - where any inodes
  591. * associated with the buffer should be removed from ail.
  592. * There is also special processing during recovery,
  593. * any replay of the inodes in the buffer needs to be
  594. * prevented as the buffer may have been reused.
  595. */
  596. void
  597. xfs_trans_stale_inode_buf(
  598. xfs_trans_t *tp,
  599. xfs_buf_t *bp)
  600. {
  601. xfs_buf_log_item_t *bip = bp->b_fspriv;
  602. ASSERT(bp->b_transp == tp);
  603. ASSERT(bip != NULL);
  604. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  605. bip->bli_flags |= XFS_BLI_STALE_INODE;
  606. bip->bli_item.li_cb = xfs_buf_iodone;
  607. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  608. }
  609. /*
  610. * Mark the buffer as being one which contains newly allocated
  611. * inodes. We need to make sure that even if this buffer is
  612. * relogged as an 'inode buf' we still recover all of the inode
  613. * images in the face of a crash. This works in coordination with
  614. * xfs_buf_item_committed() to ensure that the buffer remains in the
  615. * AIL at its original location even after it has been relogged.
  616. */
  617. /* ARGSUSED */
  618. void
  619. xfs_trans_inode_alloc_buf(
  620. xfs_trans_t *tp,
  621. xfs_buf_t *bp)
  622. {
  623. xfs_buf_log_item_t *bip = bp->b_fspriv;
  624. ASSERT(bp->b_transp == tp);
  625. ASSERT(bip != NULL);
  626. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  627. bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
  628. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  629. }
  630. /*
  631. * Mark the buffer as ordered for this transaction. This means
  632. * that the contents of the buffer are not recorded in the transaction
  633. * but it is tracked in the AIL as though it was. This allows us
  634. * to record logical changes in transactions rather than the physical
  635. * changes we make to the buffer without changing writeback ordering
  636. * constraints of metadata buffers.
  637. */
  638. void
  639. xfs_trans_ordered_buf(
  640. struct xfs_trans *tp,
  641. struct xfs_buf *bp)
  642. {
  643. struct xfs_buf_log_item *bip = bp->b_fspriv;
  644. ASSERT(bp->b_transp == tp);
  645. ASSERT(bip != NULL);
  646. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  647. bip->bli_flags |= XFS_BLI_ORDERED;
  648. trace_xfs_buf_item_ordered(bip);
  649. }
  650. /*
  651. * Set the type of the buffer for log recovery so that it can correctly identify
  652. * and hence attach the correct buffer ops to the buffer after replay.
  653. */
  654. void
  655. xfs_trans_buf_set_type(
  656. struct xfs_trans *tp,
  657. struct xfs_buf *bp,
  658. enum xfs_blft type)
  659. {
  660. struct xfs_buf_log_item *bip = bp->b_fspriv;
  661. if (!tp)
  662. return;
  663. ASSERT(bp->b_transp == tp);
  664. ASSERT(bip != NULL);
  665. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  666. xfs_blft_to_flags(&bip->__bli_format, type);
  667. }
  668. void
  669. xfs_trans_buf_copy_type(
  670. struct xfs_buf *dst_bp,
  671. struct xfs_buf *src_bp)
  672. {
  673. struct xfs_buf_log_item *sbip = src_bp->b_fspriv;
  674. struct xfs_buf_log_item *dbip = dst_bp->b_fspriv;
  675. enum xfs_blft type;
  676. type = xfs_blft_from_flags(&sbip->__bli_format);
  677. xfs_blft_to_flags(&dbip->__bli_format, type);
  678. }
  679. /*
  680. * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
  681. * dquots. However, unlike in inode buffer recovery, dquot buffers get
  682. * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
  683. * The only thing that makes dquot buffers different from regular
  684. * buffers is that we must not replay dquot bufs when recovering
  685. * if a _corresponding_ quotaoff has happened. We also have to distinguish
  686. * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
  687. * can be turned off independently.
  688. */
  689. /* ARGSUSED */
  690. void
  691. xfs_trans_dquot_buf(
  692. xfs_trans_t *tp,
  693. xfs_buf_t *bp,
  694. uint type)
  695. {
  696. struct xfs_buf_log_item *bip = bp->b_fspriv;
  697. ASSERT(type == XFS_BLF_UDQUOT_BUF ||
  698. type == XFS_BLF_PDQUOT_BUF ||
  699. type == XFS_BLF_GDQUOT_BUF);
  700. bip->__bli_format.blf_flags |= type;
  701. switch (type) {
  702. case XFS_BLF_UDQUOT_BUF:
  703. type = XFS_BLFT_UDQUOT_BUF;
  704. break;
  705. case XFS_BLF_PDQUOT_BUF:
  706. type = XFS_BLFT_PDQUOT_BUF;
  707. break;
  708. case XFS_BLF_GDQUOT_BUF:
  709. type = XFS_BLFT_GDQUOT_BUF;
  710. break;
  711. default:
  712. type = XFS_BLFT_UNKNOWN_BUF;
  713. break;
  714. }
  715. xfs_trans_buf_set_type(tp, bp, type);
  716. }