xfs_trans_buf.c 22 KB

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