xfs_trans_buf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. }
  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. /*
  323. * Default to a normal brelse() call if the tp is NULL.
  324. */
  325. if (tp == NULL) {
  326. ASSERT(bp->b_transp == NULL);
  327. xfs_buf_relse(bp);
  328. return;
  329. }
  330. ASSERT(bp->b_transp == tp);
  331. bip = bp->b_fspriv;
  332. ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
  333. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  334. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  335. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  336. trace_xfs_trans_brelse(bip);
  337. /*
  338. * If the release is just for a recursive lock,
  339. * then decrement the count and return.
  340. */
  341. if (bip->bli_recur > 0) {
  342. bip->bli_recur--;
  343. return;
  344. }
  345. /*
  346. * If the buffer is dirty within this transaction, we can't
  347. * release it until we commit.
  348. */
  349. if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
  350. return;
  351. /*
  352. * If the buffer has been invalidated, then we can't release
  353. * it until the transaction commits to disk unless it is re-dirtied
  354. * as part of this transaction. This prevents us from pulling
  355. * the item from the AIL before we should.
  356. */
  357. if (bip->bli_flags & XFS_BLI_STALE)
  358. return;
  359. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  360. /*
  361. * Free up the log item descriptor tracking the released item.
  362. */
  363. xfs_trans_del_item(&bip->bli_item);
  364. /*
  365. * Clear the hold flag in the buf log item if it is set.
  366. * We wouldn't want the next user of the buffer to
  367. * get confused.
  368. */
  369. if (bip->bli_flags & XFS_BLI_HOLD) {
  370. bip->bli_flags &= ~XFS_BLI_HOLD;
  371. }
  372. /*
  373. * Drop our reference to the buf log item.
  374. */
  375. atomic_dec(&bip->bli_refcount);
  376. /*
  377. * If the buf item is not tracking data in the log, then
  378. * we must free it before releasing the buffer back to the
  379. * free pool. Before releasing the buffer to the free pool,
  380. * clear the transaction pointer in b_fsprivate2 to dissolve
  381. * its relation to this transaction.
  382. */
  383. if (!xfs_buf_item_dirty(bip)) {
  384. /***
  385. ASSERT(bp->b_pincount == 0);
  386. ***/
  387. ASSERT(atomic_read(&bip->bli_refcount) == 0);
  388. ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
  389. ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
  390. xfs_buf_item_relse(bp);
  391. }
  392. bp->b_transp = NULL;
  393. xfs_buf_relse(bp);
  394. }
  395. /*
  396. * Mark the buffer as not needing to be unlocked when the buf item's
  397. * iop_unlock() routine is called. The buffer must already be locked
  398. * and associated with the given transaction.
  399. */
  400. /* ARGSUSED */
  401. void
  402. xfs_trans_bhold(xfs_trans_t *tp,
  403. xfs_buf_t *bp)
  404. {
  405. xfs_buf_log_item_t *bip = bp->b_fspriv;
  406. ASSERT(bp->b_transp == tp);
  407. ASSERT(bip != NULL);
  408. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  409. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  410. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  411. bip->bli_flags |= XFS_BLI_HOLD;
  412. trace_xfs_trans_bhold(bip);
  413. }
  414. /*
  415. * Cancel the previous buffer hold request made on this buffer
  416. * for this transaction.
  417. */
  418. void
  419. xfs_trans_bhold_release(xfs_trans_t *tp,
  420. xfs_buf_t *bp)
  421. {
  422. xfs_buf_log_item_t *bip = bp->b_fspriv;
  423. ASSERT(bp->b_transp == tp);
  424. ASSERT(bip != NULL);
  425. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  426. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
  427. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  428. ASSERT(bip->bli_flags & XFS_BLI_HOLD);
  429. bip->bli_flags &= ~XFS_BLI_HOLD;
  430. trace_xfs_trans_bhold_release(bip);
  431. }
  432. /*
  433. * This is called to mark bytes first through last inclusive of the given
  434. * buffer as needing to be logged when the transaction is committed.
  435. * The buffer must already be associated with the given transaction.
  436. *
  437. * First and last are numbers relative to the beginning of this buffer,
  438. * so the first byte in the buffer is numbered 0 regardless of the
  439. * value of b_blkno.
  440. */
  441. void
  442. xfs_trans_log_buf(xfs_trans_t *tp,
  443. xfs_buf_t *bp,
  444. uint first,
  445. uint last)
  446. {
  447. xfs_buf_log_item_t *bip = bp->b_fspriv;
  448. ASSERT(bp->b_transp == tp);
  449. ASSERT(bip != NULL);
  450. ASSERT(first <= last && last < BBTOB(bp->b_length));
  451. ASSERT(bp->b_iodone == NULL ||
  452. bp->b_iodone == xfs_buf_iodone_callbacks);
  453. /*
  454. * Mark the buffer as needing to be written out eventually,
  455. * and set its iodone function to remove the buffer's buf log
  456. * item from the AIL and free it when the buffer is flushed
  457. * to disk. See xfs_buf_attach_iodone() for more details
  458. * on li_cb and xfs_buf_iodone_callbacks().
  459. * If we end up aborting this transaction, we trap this buffer
  460. * inside the b_bdstrat callback so that this won't get written to
  461. * disk.
  462. */
  463. XFS_BUF_DONE(bp);
  464. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  465. bp->b_iodone = xfs_buf_iodone_callbacks;
  466. bip->bli_item.li_cb = xfs_buf_iodone;
  467. trace_xfs_trans_log_buf(bip);
  468. /*
  469. * If we invalidated the buffer within this transaction, then
  470. * cancel the invalidation now that we're dirtying the buffer
  471. * again. There are no races with the code in xfs_buf_item_unpin(),
  472. * because we have a reference to the buffer this entire time.
  473. */
  474. if (bip->bli_flags & XFS_BLI_STALE) {
  475. bip->bli_flags &= ~XFS_BLI_STALE;
  476. ASSERT(XFS_BUF_ISSTALE(bp));
  477. XFS_BUF_UNSTALE(bp);
  478. bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
  479. }
  480. tp->t_flags |= XFS_TRANS_DIRTY;
  481. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  482. /*
  483. * If we have an ordered buffer we are not logging any dirty range but
  484. * it still needs to be marked dirty and that it has been logged.
  485. */
  486. bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
  487. if (!(bip->bli_flags & XFS_BLI_ORDERED))
  488. xfs_buf_item_log(bip, first, last);
  489. }
  490. /*
  491. * Invalidate a buffer that is being used within a transaction.
  492. *
  493. * Typically this is because the blocks in the buffer are being freed, so we
  494. * need to prevent it from being written out when we're done. Allowing it
  495. * to be written again might overwrite data in the free blocks if they are
  496. * reallocated to a file.
  497. *
  498. * We prevent the buffer from being written out by marking it stale. We can't
  499. * get rid of the buf log item at this point because the buffer may still be
  500. * pinned by another transaction. If that is the case, then we'll wait until
  501. * the buffer is committed to disk for the last time (we can tell by the ref
  502. * count) and free it in xfs_buf_item_unpin(). Until that happens we will
  503. * keep the buffer locked so that the buffer and buf log item are not reused.
  504. *
  505. * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
  506. * the buf item. This will be used at recovery time to determine that copies
  507. * of the buffer in the log before this should not be replayed.
  508. *
  509. * We mark the item descriptor and the transaction dirty so that we'll hold
  510. * the buffer until after the commit.
  511. *
  512. * Since we're invalidating the buffer, we also clear the state about which
  513. * parts of the buffer have been logged. We also clear the flag indicating
  514. * that this is an inode buffer since the data in the buffer will no longer
  515. * be valid.
  516. *
  517. * We set the stale bit in the buffer as well since we're getting rid of it.
  518. */
  519. void
  520. xfs_trans_binval(
  521. xfs_trans_t *tp,
  522. xfs_buf_t *bp)
  523. {
  524. xfs_buf_log_item_t *bip = bp->b_fspriv;
  525. int i;
  526. ASSERT(bp->b_transp == tp);
  527. ASSERT(bip != NULL);
  528. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  529. trace_xfs_trans_binval(bip);
  530. if (bip->bli_flags & XFS_BLI_STALE) {
  531. /*
  532. * If the buffer is already invalidated, then
  533. * just return.
  534. */
  535. ASSERT(XFS_BUF_ISSTALE(bp));
  536. ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
  537. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
  538. ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
  539. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  540. ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
  541. ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
  542. return;
  543. }
  544. xfs_buf_stale(bp);
  545. bip->bli_flags |= XFS_BLI_STALE;
  546. bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
  547. bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
  548. bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
  549. bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
  550. for (i = 0; i < bip->bli_format_count; i++) {
  551. memset(bip->bli_formats[i].blf_data_map, 0,
  552. (bip->bli_formats[i].blf_map_size * sizeof(uint)));
  553. }
  554. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  555. tp->t_flags |= XFS_TRANS_DIRTY;
  556. }
  557. /*
  558. * This call is used to indicate that the buffer contains on-disk inodes which
  559. * must be handled specially during recovery. They require special handling
  560. * because only the di_next_unlinked from the inodes in the buffer should be
  561. * recovered. The rest of the data in the buffer is logged via the inodes
  562. * themselves.
  563. *
  564. * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
  565. * transferred to the buffer's log format structure so that we'll know what to
  566. * do at recovery time.
  567. */
  568. void
  569. xfs_trans_inode_buf(
  570. xfs_trans_t *tp,
  571. xfs_buf_t *bp)
  572. {
  573. xfs_buf_log_item_t *bip = bp->b_fspriv;
  574. ASSERT(bp->b_transp == tp);
  575. ASSERT(bip != NULL);
  576. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  577. bip->bli_flags |= XFS_BLI_INODE_BUF;
  578. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  579. }
  580. /*
  581. * This call is used to indicate that the buffer is going to
  582. * be staled and was an inode buffer. This means it gets
  583. * special processing during unpin - where any inodes
  584. * associated with the buffer should be removed from ail.
  585. * There is also special processing during recovery,
  586. * any replay of the inodes in the buffer needs to be
  587. * prevented as the buffer may have been reused.
  588. */
  589. void
  590. xfs_trans_stale_inode_buf(
  591. xfs_trans_t *tp,
  592. xfs_buf_t *bp)
  593. {
  594. xfs_buf_log_item_t *bip = bp->b_fspriv;
  595. ASSERT(bp->b_transp == tp);
  596. ASSERT(bip != NULL);
  597. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  598. bip->bli_flags |= XFS_BLI_STALE_INODE;
  599. bip->bli_item.li_cb = xfs_buf_iodone;
  600. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  601. }
  602. /*
  603. * Mark the buffer as being one which contains newly allocated
  604. * inodes. We need to make sure that even if this buffer is
  605. * relogged as an 'inode buf' we still recover all of the inode
  606. * images in the face of a crash. This works in coordination with
  607. * xfs_buf_item_committed() to ensure that the buffer remains in the
  608. * AIL at its original location even after it has been relogged.
  609. */
  610. /* ARGSUSED */
  611. void
  612. xfs_trans_inode_alloc_buf(
  613. xfs_trans_t *tp,
  614. xfs_buf_t *bp)
  615. {
  616. xfs_buf_log_item_t *bip = bp->b_fspriv;
  617. ASSERT(bp->b_transp == tp);
  618. ASSERT(bip != NULL);
  619. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  620. bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
  621. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
  622. }
  623. /*
  624. * Mark the buffer as ordered for this transaction. This means
  625. * that the contents of the buffer are not recorded in the transaction
  626. * but it is tracked in the AIL as though it was. This allows us
  627. * to record logical changes in transactions rather than the physical
  628. * changes we make to the buffer without changing writeback ordering
  629. * constraints of metadata buffers.
  630. */
  631. void
  632. xfs_trans_ordered_buf(
  633. struct xfs_trans *tp,
  634. struct xfs_buf *bp)
  635. {
  636. struct xfs_buf_log_item *bip = bp->b_fspriv;
  637. ASSERT(bp->b_transp == tp);
  638. ASSERT(bip != NULL);
  639. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  640. bip->bli_flags |= XFS_BLI_ORDERED;
  641. trace_xfs_buf_item_ordered(bip);
  642. }
  643. /*
  644. * Set the type of the buffer for log recovery so that it can correctly identify
  645. * and hence attach the correct buffer ops to the buffer after replay.
  646. */
  647. void
  648. xfs_trans_buf_set_type(
  649. struct xfs_trans *tp,
  650. struct xfs_buf *bp,
  651. enum xfs_blft type)
  652. {
  653. struct xfs_buf_log_item *bip = bp->b_fspriv;
  654. if (!tp)
  655. return;
  656. ASSERT(bp->b_transp == tp);
  657. ASSERT(bip != NULL);
  658. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  659. xfs_blft_to_flags(&bip->__bli_format, type);
  660. }
  661. void
  662. xfs_trans_buf_copy_type(
  663. struct xfs_buf *dst_bp,
  664. struct xfs_buf *src_bp)
  665. {
  666. struct xfs_buf_log_item *sbip = src_bp->b_fspriv;
  667. struct xfs_buf_log_item *dbip = dst_bp->b_fspriv;
  668. enum xfs_blft type;
  669. type = xfs_blft_from_flags(&sbip->__bli_format);
  670. xfs_blft_to_flags(&dbip->__bli_format, type);
  671. }
  672. /*
  673. * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
  674. * dquots. However, unlike in inode buffer recovery, dquot buffers get
  675. * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
  676. * The only thing that makes dquot buffers different from regular
  677. * buffers is that we must not replay dquot bufs when recovering
  678. * if a _corresponding_ quotaoff has happened. We also have to distinguish
  679. * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
  680. * can be turned off independently.
  681. */
  682. /* ARGSUSED */
  683. void
  684. xfs_trans_dquot_buf(
  685. xfs_trans_t *tp,
  686. xfs_buf_t *bp,
  687. uint type)
  688. {
  689. struct xfs_buf_log_item *bip = bp->b_fspriv;
  690. ASSERT(type == XFS_BLF_UDQUOT_BUF ||
  691. type == XFS_BLF_PDQUOT_BUF ||
  692. type == XFS_BLF_GDQUOT_BUF);
  693. bip->__bli_format.blf_flags |= type;
  694. switch (type) {
  695. case XFS_BLF_UDQUOT_BUF:
  696. type = XFS_BLFT_UDQUOT_BUF;
  697. break;
  698. case XFS_BLF_PDQUOT_BUF:
  699. type = XFS_BLFT_PDQUOT_BUF;
  700. break;
  701. case XFS_BLF_GDQUOT_BUF:
  702. type = XFS_BLFT_GDQUOT_BUF;
  703. break;
  704. default:
  705. type = XFS_BLFT_UNKNOWN_BUF;
  706. break;
  707. }
  708. xfs_trans_buf_set_type(tp, bp, type);
  709. }