xfs_trans_buf.c 23 KB

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