xfs_trans_buf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. XFS_BUF_DONE(bp);
  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. *bpp = bp;
  299. return 0;
  300. }
  301. /*
  302. * Release the buffer bp which was previously acquired with one of the
  303. * xfs_trans_... buffer allocation routines if the buffer has not
  304. * been modified within this transaction. If the buffer is modified
  305. * within this transaction, do decrement the recursion count but do
  306. * not release the buffer even if the count goes to 0. If the buffer is not
  307. * modified within the transaction, decrement the recursion count and
  308. * release the buffer if the recursion count goes to 0.
  309. *
  310. * If the buffer is to be released and it was not modified before
  311. * this transaction began, then free the buf_log_item associated with it.
  312. *
  313. * If the transaction pointer is NULL, make this just a normal
  314. * brelse() call.
  315. */
  316. void
  317. xfs_trans_brelse(xfs_trans_t *tp,
  318. xfs_buf_t *bp)
  319. {
  320. xfs_buf_log_item_t *bip;
  321. /*
  322. * Default to a normal brelse() call if the tp is NULL.
  323. */
  324. if (tp == NULL) {
  325. ASSERT(bp->b_transp == NULL);
  326. xfs_buf_relse(bp);
  327. return;
  328. }
  329. ASSERT(bp->b_transp == tp);
  330. bip = bp->b_fspriv;
  331. ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
  332. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  333. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  334. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  335. trace_xfs_trans_brelse(bip);
  336. /*
  337. * If the release is just for a recursive lock,
  338. * then decrement the count and return.
  339. */
  340. if (bip->bli_recur > 0) {
  341. bip->bli_recur--;
  342. return;
  343. }
  344. /*
  345. * If the buffer is dirty within this transaction, we can't
  346. * release it until we commit.
  347. */
  348. if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
  349. return;
  350. /*
  351. * If the buffer has been invalidated, then we can't release
  352. * it until the transaction commits to disk unless it is re-dirtied
  353. * as part of this transaction. This prevents us from pulling
  354. * the item from the AIL before we should.
  355. */
  356. if (bip->bli_flags & XFS_BLI_STALE)
  357. return;
  358. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  359. /*
  360. * Free up the log item descriptor tracking the released item.
  361. */
  362. xfs_trans_del_item(&bip->bli_item);
  363. /*
  364. * Clear the hold flag in the buf log item if it is set.
  365. * We wouldn't want the next user of the buffer to
  366. * get confused.
  367. */
  368. if (bip->bli_flags & XFS_BLI_HOLD) {
  369. bip->bli_flags &= ~XFS_BLI_HOLD;
  370. }
  371. /*
  372. * Drop our reference to the buf log item.
  373. */
  374. atomic_dec(&bip->bli_refcount);
  375. /*
  376. * If the buf item is not tracking data in the log, then
  377. * we must free it before releasing the buffer back to the
  378. * free pool. Before releasing the buffer to the free pool,
  379. * clear the transaction pointer in b_fsprivate2 to dissolve
  380. * its relation to this transaction.
  381. */
  382. if (!xfs_buf_item_dirty(bip)) {
  383. /***
  384. ASSERT(bp->b_pincount == 0);
  385. ***/
  386. ASSERT(atomic_read(&bip->bli_refcount) == 0);
  387. ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
  388. ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
  389. xfs_buf_item_relse(bp);
  390. }
  391. bp->b_transp = NULL;
  392. xfs_buf_relse(bp);
  393. }
  394. /*
  395. * Mark the buffer as not needing to be unlocked when the buf item's
  396. * iop_unlock() routine is called. The buffer must already be locked
  397. * and associated with the given transaction.
  398. */
  399. /* ARGSUSED */
  400. void
  401. xfs_trans_bhold(xfs_trans_t *tp,
  402. xfs_buf_t *bp)
  403. {
  404. xfs_buf_log_item_t *bip = bp->b_fspriv;
  405. ASSERT(bp->b_transp == tp);
  406. ASSERT(bip != NULL);
  407. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  408. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  409. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  410. bip->bli_flags |= XFS_BLI_HOLD;
  411. trace_xfs_trans_bhold(bip);
  412. }
  413. /*
  414. * Cancel the previous buffer hold request made on this buffer
  415. * for this transaction.
  416. */
  417. void
  418. xfs_trans_bhold_release(xfs_trans_t *tp,
  419. xfs_buf_t *bp)
  420. {
  421. xfs_buf_log_item_t *bip = bp->b_fspriv;
  422. ASSERT(bp->b_transp == tp);
  423. ASSERT(bip != NULL);
  424. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  425. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  426. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  427. ASSERT(bip->bli_flags & XFS_BLI_HOLD);
  428. bip->bli_flags &= ~XFS_BLI_HOLD;
  429. trace_xfs_trans_bhold_release(bip);
  430. }
  431. /*
  432. * This is called to mark bytes first through last inclusive of the given
  433. * buffer as needing to be logged when the transaction is committed.
  434. * The buffer must already be associated with the given transaction.
  435. *
  436. * First and last are numbers relative to the beginning of this buffer,
  437. * so the first byte in the buffer is numbered 0 regardless of the
  438. * value of b_blkno.
  439. */
  440. void
  441. xfs_trans_log_buf(xfs_trans_t *tp,
  442. xfs_buf_t *bp,
  443. uint first,
  444. uint last)
  445. {
  446. xfs_buf_log_item_t *bip = bp->b_fspriv;
  447. ASSERT(bp->b_transp == tp);
  448. ASSERT(bip != NULL);
  449. ASSERT(first <= last && last < BBTOB(bp->b_length));
  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. XFS_BUF_DONE(bp);
  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. trace_xfs_trans_log_buf(bip);
  467. /*
  468. * If we invalidated the buffer within this transaction, then
  469. * cancel the invalidation now that we're dirtying the buffer
  470. * again. There are no races with the code in xfs_buf_item_unpin(),
  471. * because we have a reference to the buffer this entire time.
  472. */
  473. if (bip->bli_flags & XFS_BLI_STALE) {
  474. bip->bli_flags &= ~XFS_BLI_STALE;
  475. ASSERT(XFS_BUF_ISSTALE(bp));
  476. XFS_BUF_UNSTALE(bp);
  477. bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
  478. }
  479. tp->t_flags |= XFS_TRANS_DIRTY;
  480. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  481. /*
  482. * If we have an ordered buffer we are not logging any dirty range but
  483. * it still needs to be marked dirty and that it has been logged.
  484. */
  485. bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
  486. if (!(bip->bli_flags & XFS_BLI_ORDERED))
  487. xfs_buf_item_log(bip, first, last);
  488. }
  489. /*
  490. * Invalidate a buffer that is being used within a transaction.
  491. *
  492. * Typically this is because the blocks in the buffer are being freed, so we
  493. * need to prevent it from being written out when we're done. Allowing it
  494. * to be written again might overwrite data in the free blocks if they are
  495. * reallocated to a file.
  496. *
  497. * We prevent the buffer from being written out by marking it stale. We can't
  498. * get rid of the buf log item at this point because the buffer may still be
  499. * pinned by another transaction. If that is the case, then we'll wait until
  500. * the buffer is committed to disk for the last time (we can tell by the ref
  501. * count) and free it in xfs_buf_item_unpin(). Until that happens we will
  502. * keep the buffer locked so that the buffer and buf log item are not reused.
  503. *
  504. * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
  505. * the buf item. This will be used at recovery time to determine that copies
  506. * of the buffer in the log before this should not be replayed.
  507. *
  508. * We mark the item descriptor and the transaction dirty so that we'll hold
  509. * the buffer until after the commit.
  510. *
  511. * Since we're invalidating the buffer, we also clear the state about which
  512. * parts of the buffer have been logged. We also clear the flag indicating
  513. * that this is an inode buffer since the data in the buffer will no longer
  514. * be valid.
  515. *
  516. * We set the stale bit in the buffer as well since we're getting rid of it.
  517. */
  518. void
  519. xfs_trans_binval(
  520. xfs_trans_t *tp,
  521. xfs_buf_t *bp)
  522. {
  523. xfs_buf_log_item_t *bip = bp->b_fspriv;
  524. int i;
  525. ASSERT(bp->b_transp == tp);
  526. ASSERT(bip != NULL);
  527. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  528. trace_xfs_trans_binval(bip);
  529. if (bip->bli_flags & XFS_BLI_STALE) {
  530. /*
  531. * If the buffer is already invalidated, then
  532. * just return.
  533. */
  534. ASSERT(XFS_BUF_ISSTALE(bp));
  535. ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
  536. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
  537. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
  538. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  539. ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
  540. ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
  541. return;
  542. }
  543. xfs_buf_stale(bp);
  544. bip->bli_flags |= XFS_BLI_STALE;
  545. bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
  546. bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
  547. bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
  548. bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
  549. for (i = 0; i < bip->bli_format_count; i++) {
  550. memset(bip->bli_formats[i].blf_data_map, 0,
  551. (bip->bli_formats[i].blf_map_size * sizeof(uint)));
  552. }
  553. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  554. tp->t_flags |= XFS_TRANS_DIRTY;
  555. }
  556. /*
  557. * This call is used to indicate that the buffer contains on-disk inodes which
  558. * must be handled specially during recovery. They require special handling
  559. * because only the di_next_unlinked from the inodes in the buffer should be
  560. * recovered. The rest of the data in the buffer is logged via the inodes
  561. * themselves.
  562. *
  563. * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
  564. * transferred to the buffer's log format structure so that we'll know what to
  565. * do at recovery time.
  566. */
  567. void
  568. xfs_trans_inode_buf(
  569. xfs_trans_t *tp,
  570. xfs_buf_t *bp)
  571. {
  572. xfs_buf_log_item_t *bip = bp->b_fspriv;
  573. ASSERT(bp->b_transp == tp);
  574. ASSERT(bip != NULL);
  575. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  576. bip->bli_flags |= XFS_BLI_INODE_BUF;
  577. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  578. }
  579. /*
  580. * This call is used to indicate that the buffer is going to
  581. * be staled and was an inode buffer. This means it gets
  582. * special processing during unpin - where any inodes
  583. * associated with the buffer should be removed from ail.
  584. * There is also special processing during recovery,
  585. * any replay of the inodes in the buffer needs to be
  586. * prevented as the buffer may have been reused.
  587. */
  588. void
  589. xfs_trans_stale_inode_buf(
  590. xfs_trans_t *tp,
  591. xfs_buf_t *bp)
  592. {
  593. xfs_buf_log_item_t *bip = bp->b_fspriv;
  594. ASSERT(bp->b_transp == tp);
  595. ASSERT(bip != NULL);
  596. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  597. bip->bli_flags |= XFS_BLI_STALE_INODE;
  598. bip->bli_item.li_cb = xfs_buf_iodone;
  599. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  600. }
  601. /*
  602. * Mark the buffer as being one which contains newly allocated
  603. * inodes. We need to make sure that even if this buffer is
  604. * relogged as an 'inode buf' we still recover all of the inode
  605. * images in the face of a crash. This works in coordination with
  606. * xfs_buf_item_committed() to ensure that the buffer remains in the
  607. * AIL at its original location even after it has been relogged.
  608. */
  609. /* ARGSUSED */
  610. void
  611. xfs_trans_inode_alloc_buf(
  612. xfs_trans_t *tp,
  613. xfs_buf_t *bp)
  614. {
  615. xfs_buf_log_item_t *bip = bp->b_fspriv;
  616. ASSERT(bp->b_transp == tp);
  617. ASSERT(bip != NULL);
  618. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  619. bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
  620. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  621. }
  622. /*
  623. * Mark the buffer as ordered for this transaction. This means
  624. * that the contents of the buffer are not recorded in the transaction
  625. * but it is tracked in the AIL as though it was. This allows us
  626. * to record logical changes in transactions rather than the physical
  627. * changes we make to the buffer without changing writeback ordering
  628. * constraints of metadata buffers.
  629. */
  630. void
  631. xfs_trans_ordered_buf(
  632. struct xfs_trans *tp,
  633. struct xfs_buf *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_ORDERED;
  640. trace_xfs_buf_item_ordered(bip);
  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_fspriv;
  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_fspriv;
  666. struct xfs_buf_log_item *dbip = dst_bp->b_fspriv;
  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. xfs_buf_t *bp,
  686. uint type)
  687. {
  688. struct xfs_buf_log_item *bip = bp->b_fspriv;
  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. xfs_trans_buf_set_type(tp, bp, type);
  708. }