xfs_trans_buf.c 22 KB

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