xfs_trans_buf.c 23 KB

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