xfs_trans_buf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. XFS_BUF_COUNT(blip->bli_buf) == 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. /*
  153. * If the buffer is stale then it was binval'ed
  154. * since last read. This doesn't matter since the
  155. * caller isn't allowed to use the data anyway.
  156. */
  157. else if (XFS_BUF_ISSTALE(bp))
  158. ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
  159. ASSERT(bp->b_transp == tp);
  160. bip = bp->b_fspriv;
  161. ASSERT(bip != NULL);
  162. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  163. bip->bli_recur++;
  164. trace_xfs_trans_get_buf_recur(bip);
  165. return (bp);
  166. }
  167. /*
  168. * We always specify the XBF_DONT_BLOCK flag within a transaction
  169. * so that get_buf does not try to push out a delayed write buffer
  170. * which might cause another transaction to take place (if the
  171. * buffer was delayed alloc). Such recursive transactions can
  172. * easily deadlock with our current transaction as well as cause
  173. * us to run out of stack space.
  174. */
  175. bp = xfs_buf_get(target_dev, blkno, len, flags | XBF_DONT_BLOCK);
  176. if (bp == NULL) {
  177. return NULL;
  178. }
  179. ASSERT(!bp->b_error);
  180. _xfs_trans_bjoin(tp, bp, 1);
  181. trace_xfs_trans_get_buf(bp->b_fspriv);
  182. return (bp);
  183. }
  184. /*
  185. * Get and lock the superblock buffer of this file system for the
  186. * given transaction.
  187. *
  188. * We don't need to use incore_match() here, because the superblock
  189. * buffer is a private buffer which we keep a pointer to in the
  190. * mount structure.
  191. */
  192. xfs_buf_t *
  193. xfs_trans_getsb(xfs_trans_t *tp,
  194. struct xfs_mount *mp,
  195. int flags)
  196. {
  197. xfs_buf_t *bp;
  198. xfs_buf_log_item_t *bip;
  199. /*
  200. * Default to just trying to lock the superblock buffer
  201. * if tp is NULL.
  202. */
  203. if (tp == NULL) {
  204. return (xfs_getsb(mp, flags));
  205. }
  206. /*
  207. * If the superblock buffer already has this transaction
  208. * pointer in its b_fsprivate2 field, then we know we already
  209. * have it locked. In this case we just increment the lock
  210. * recursion count and return the buffer to the caller.
  211. */
  212. bp = mp->m_sb_bp;
  213. if (bp->b_transp == tp) {
  214. bip = bp->b_fspriv;
  215. ASSERT(bip != NULL);
  216. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  217. bip->bli_recur++;
  218. trace_xfs_trans_getsb_recur(bip);
  219. return (bp);
  220. }
  221. bp = xfs_getsb(mp, flags);
  222. if (bp == NULL)
  223. return NULL;
  224. _xfs_trans_bjoin(tp, bp, 1);
  225. trace_xfs_trans_getsb(bp->b_fspriv);
  226. return (bp);
  227. }
  228. #ifdef DEBUG
  229. xfs_buftarg_t *xfs_error_target;
  230. int xfs_do_error;
  231. int xfs_req_num;
  232. int xfs_error_mod = 33;
  233. #endif
  234. /*
  235. * Get and lock the buffer for the caller if it is not already
  236. * locked within the given transaction. If it has not yet been
  237. * read in, read it from disk. If it is already locked
  238. * within the transaction and already read in, just increment its
  239. * lock recursion count and return a pointer to it.
  240. *
  241. * If the transaction pointer is NULL, make this just a normal
  242. * read_buf() call.
  243. */
  244. int
  245. xfs_trans_read_buf(
  246. xfs_mount_t *mp,
  247. xfs_trans_t *tp,
  248. xfs_buftarg_t *target,
  249. xfs_daddr_t blkno,
  250. int len,
  251. uint flags,
  252. xfs_buf_t **bpp)
  253. {
  254. xfs_buf_t *bp;
  255. xfs_buf_log_item_t *bip;
  256. int error;
  257. if (flags == 0)
  258. flags = XBF_LOCK | XBF_MAPPED;
  259. /*
  260. * Default to a normal get_buf() call if the tp is NULL.
  261. */
  262. if (tp == NULL) {
  263. bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK);
  264. if (!bp)
  265. return (flags & XBF_TRYLOCK) ?
  266. EAGAIN : XFS_ERROR(ENOMEM);
  267. if (bp->b_error) {
  268. error = bp->b_error;
  269. xfs_ioerror_alert("xfs_trans_read_buf", mp,
  270. bp, blkno);
  271. xfs_buf_relse(bp);
  272. return error;
  273. }
  274. #ifdef DEBUG
  275. if (xfs_do_error) {
  276. if (xfs_error_target == target) {
  277. if (((xfs_req_num++) % xfs_error_mod) == 0) {
  278. xfs_buf_relse(bp);
  279. xfs_debug(mp, "Returning error!");
  280. return XFS_ERROR(EIO);
  281. }
  282. }
  283. }
  284. #endif
  285. if (XFS_FORCED_SHUTDOWN(mp))
  286. goto shutdown_abort;
  287. *bpp = bp;
  288. return 0;
  289. }
  290. /*
  291. * If we find the buffer in the cache with this transaction
  292. * pointer in its b_fsprivate2 field, then we know we already
  293. * have it locked. If it is already read in we just increment
  294. * the lock recursion count and return the buffer to the caller.
  295. * If the buffer is not yet read in, then we read it in, increment
  296. * the lock recursion count, and return it to the caller.
  297. */
  298. bp = xfs_trans_buf_item_match(tp, target, blkno, len);
  299. if (bp != NULL) {
  300. ASSERT(xfs_buf_islocked(bp));
  301. ASSERT(bp->b_transp == tp);
  302. ASSERT(bp->b_fspriv != NULL);
  303. ASSERT(!bp->b_error);
  304. if (!(XFS_BUF_ISDONE(bp))) {
  305. trace_xfs_trans_read_buf_io(bp, _RET_IP_);
  306. ASSERT(!XFS_BUF_ISASYNC(bp));
  307. XFS_BUF_READ(bp);
  308. xfsbdstrat(tp->t_mountp, bp);
  309. error = xfs_buf_iowait(bp);
  310. if (error) {
  311. xfs_ioerror_alert("xfs_trans_read_buf", mp,
  312. bp, blkno);
  313. xfs_buf_relse(bp);
  314. /*
  315. * We can gracefully recover from most read
  316. * errors. Ones we can't are those that happen
  317. * after the transaction's already dirty.
  318. */
  319. if (tp->t_flags & XFS_TRANS_DIRTY)
  320. xfs_force_shutdown(tp->t_mountp,
  321. SHUTDOWN_META_IO_ERROR);
  322. return error;
  323. }
  324. }
  325. /*
  326. * We never locked this buf ourselves, so we shouldn't
  327. * brelse it either. Just get out.
  328. */
  329. if (XFS_FORCED_SHUTDOWN(mp)) {
  330. trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
  331. *bpp = NULL;
  332. return XFS_ERROR(EIO);
  333. }
  334. bip = bp->b_fspriv;
  335. bip->bli_recur++;
  336. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  337. trace_xfs_trans_read_buf_recur(bip);
  338. *bpp = bp;
  339. return 0;
  340. }
  341. /*
  342. * We always specify the XBF_DONT_BLOCK flag within a transaction
  343. * so that get_buf does not try to push out a delayed write buffer
  344. * which might cause another transaction to take place (if the
  345. * buffer was delayed alloc). Such recursive transactions can
  346. * easily deadlock with our current transaction as well as cause
  347. * us to run out of stack space.
  348. */
  349. bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK);
  350. if (bp == NULL) {
  351. *bpp = NULL;
  352. return (flags & XBF_TRYLOCK) ?
  353. 0 : XFS_ERROR(ENOMEM);
  354. }
  355. if (bp->b_error) {
  356. error = bp->b_error;
  357. xfs_buf_stale(bp);
  358. XFS_BUF_DONE(bp);
  359. xfs_ioerror_alert("xfs_trans_read_buf", mp,
  360. bp, blkno);
  361. if (tp->t_flags & XFS_TRANS_DIRTY)
  362. xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
  363. xfs_buf_relse(bp);
  364. return error;
  365. }
  366. #ifdef DEBUG
  367. if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
  368. if (xfs_error_target == target) {
  369. if (((xfs_req_num++) % xfs_error_mod) == 0) {
  370. xfs_force_shutdown(tp->t_mountp,
  371. SHUTDOWN_META_IO_ERROR);
  372. xfs_buf_relse(bp);
  373. xfs_debug(mp, "Returning trans error!");
  374. return XFS_ERROR(EIO);
  375. }
  376. }
  377. }
  378. #endif
  379. if (XFS_FORCED_SHUTDOWN(mp))
  380. goto shutdown_abort;
  381. _xfs_trans_bjoin(tp, bp, 1);
  382. trace_xfs_trans_read_buf(bp->b_fspriv);
  383. *bpp = bp;
  384. return 0;
  385. shutdown_abort:
  386. /*
  387. * the theory here is that buffer is good but we're
  388. * bailing out because the filesystem is being forcibly
  389. * shut down. So we should leave the b_flags alone since
  390. * the buffer's not staled and just get out.
  391. */
  392. #if defined(DEBUG)
  393. if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp))
  394. xfs_notice(mp, "about to pop assert, bp == 0x%p", bp);
  395. #endif
  396. ASSERT((bp->b_flags & (XBF_STALE|XBF_DELWRI)) !=
  397. (XBF_STALE|XBF_DELWRI));
  398. trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
  399. xfs_buf_relse(bp);
  400. *bpp = NULL;
  401. return XFS_ERROR(EIO);
  402. }
  403. /*
  404. * Release the buffer bp which was previously acquired with one of the
  405. * xfs_trans_... buffer allocation routines if the buffer has not
  406. * been modified within this transaction. If the buffer is modified
  407. * within this transaction, do decrement the recursion count but do
  408. * not release the buffer even if the count goes to 0. If the buffer is not
  409. * modified within the transaction, decrement the recursion count and
  410. * release the buffer if the recursion count goes to 0.
  411. *
  412. * If the buffer is to be released and it was not modified before
  413. * this transaction began, then free the buf_log_item associated with it.
  414. *
  415. * If the transaction pointer is NULL, make this just a normal
  416. * brelse() call.
  417. */
  418. void
  419. xfs_trans_brelse(xfs_trans_t *tp,
  420. xfs_buf_t *bp)
  421. {
  422. xfs_buf_log_item_t *bip;
  423. /*
  424. * Default to a normal brelse() call if the tp is NULL.
  425. */
  426. if (tp == NULL) {
  427. struct xfs_log_item *lip = bp->b_fspriv;
  428. ASSERT(bp->b_transp == NULL);
  429. /*
  430. * If there's a buf log item attached to the buffer,
  431. * then let the AIL know that the buffer is being
  432. * unlocked.
  433. */
  434. if (lip != NULL && lip->li_type == XFS_LI_BUF) {
  435. bip = bp->b_fspriv;
  436. xfs_trans_unlocked_item(bip->bli_item.li_ailp, lip);
  437. }
  438. xfs_buf_relse(bp);
  439. return;
  440. }
  441. ASSERT(bp->b_transp == tp);
  442. bip = bp->b_fspriv;
  443. ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
  444. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  445. ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
  446. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  447. trace_xfs_trans_brelse(bip);
  448. /*
  449. * If the release is just for a recursive lock,
  450. * then decrement the count and return.
  451. */
  452. if (bip->bli_recur > 0) {
  453. bip->bli_recur--;
  454. return;
  455. }
  456. /*
  457. * If the buffer is dirty within this transaction, we can't
  458. * release it until we commit.
  459. */
  460. if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
  461. return;
  462. /*
  463. * If the buffer has been invalidated, then we can't release
  464. * it until the transaction commits to disk unless it is re-dirtied
  465. * as part of this transaction. This prevents us from pulling
  466. * the item from the AIL before we should.
  467. */
  468. if (bip->bli_flags & XFS_BLI_STALE)
  469. return;
  470. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  471. /*
  472. * Free up the log item descriptor tracking the released item.
  473. */
  474. xfs_trans_del_item(&bip->bli_item);
  475. /*
  476. * Clear the hold flag in the buf log item if it is set.
  477. * We wouldn't want the next user of the buffer to
  478. * get confused.
  479. */
  480. if (bip->bli_flags & XFS_BLI_HOLD) {
  481. bip->bli_flags &= ~XFS_BLI_HOLD;
  482. }
  483. /*
  484. * Drop our reference to the buf log item.
  485. */
  486. atomic_dec(&bip->bli_refcount);
  487. /*
  488. * If the buf item is not tracking data in the log, then
  489. * we must free it before releasing the buffer back to the
  490. * free pool. Before releasing the buffer to the free pool,
  491. * clear the transaction pointer in b_fsprivate2 to dissolve
  492. * its relation to this transaction.
  493. */
  494. if (!xfs_buf_item_dirty(bip)) {
  495. /***
  496. ASSERT(bp->b_pincount == 0);
  497. ***/
  498. ASSERT(atomic_read(&bip->bli_refcount) == 0);
  499. ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
  500. ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
  501. xfs_buf_item_relse(bp);
  502. bip = NULL;
  503. }
  504. bp->b_transp = NULL;
  505. /*
  506. * If we've still got a buf log item on the buffer, then
  507. * tell the AIL that the buffer is being unlocked.
  508. */
  509. if (bip != NULL) {
  510. xfs_trans_unlocked_item(bip->bli_item.li_ailp,
  511. (xfs_log_item_t*)bip);
  512. }
  513. xfs_buf_relse(bp);
  514. return;
  515. }
  516. /*
  517. * Mark the buffer as not needing to be unlocked when the buf item's
  518. * IOP_UNLOCK() routine is called. The buffer must already be locked
  519. * and associated with the given transaction.
  520. */
  521. /* ARGSUSED */
  522. void
  523. xfs_trans_bhold(xfs_trans_t *tp,
  524. xfs_buf_t *bp)
  525. {
  526. xfs_buf_log_item_t *bip = bp->b_fspriv;
  527. ASSERT(bp->b_transp == tp);
  528. ASSERT(bip != NULL);
  529. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  530. ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
  531. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  532. bip->bli_flags |= XFS_BLI_HOLD;
  533. trace_xfs_trans_bhold(bip);
  534. }
  535. /*
  536. * Cancel the previous buffer hold request made on this buffer
  537. * for this transaction.
  538. */
  539. void
  540. xfs_trans_bhold_release(xfs_trans_t *tp,
  541. xfs_buf_t *bp)
  542. {
  543. xfs_buf_log_item_t *bip = bp->b_fspriv;
  544. ASSERT(bp->b_transp == tp);
  545. ASSERT(bip != NULL);
  546. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  547. ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
  548. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  549. ASSERT(bip->bli_flags & XFS_BLI_HOLD);
  550. bip->bli_flags &= ~XFS_BLI_HOLD;
  551. trace_xfs_trans_bhold_release(bip);
  552. }
  553. /*
  554. * This is called to mark bytes first through last inclusive of the given
  555. * buffer as needing to be logged when the transaction is committed.
  556. * The buffer must already be associated with the given transaction.
  557. *
  558. * First and last are numbers relative to the beginning of this buffer,
  559. * so the first byte in the buffer is numbered 0 regardless of the
  560. * value of b_blkno.
  561. */
  562. void
  563. xfs_trans_log_buf(xfs_trans_t *tp,
  564. xfs_buf_t *bp,
  565. uint first,
  566. uint last)
  567. {
  568. xfs_buf_log_item_t *bip = bp->b_fspriv;
  569. ASSERT(bp->b_transp == tp);
  570. ASSERT(bip != NULL);
  571. ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
  572. ASSERT(bp->b_iodone == NULL ||
  573. bp->b_iodone == xfs_buf_iodone_callbacks);
  574. /*
  575. * Mark the buffer as needing to be written out eventually,
  576. * and set its iodone function to remove the buffer's buf log
  577. * item from the AIL and free it when the buffer is flushed
  578. * to disk. See xfs_buf_attach_iodone() for more details
  579. * on li_cb and xfs_buf_iodone_callbacks().
  580. * If we end up aborting this transaction, we trap this buffer
  581. * inside the b_bdstrat callback so that this won't get written to
  582. * disk.
  583. */
  584. XFS_BUF_DONE(bp);
  585. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  586. bp->b_iodone = xfs_buf_iodone_callbacks;
  587. bip->bli_item.li_cb = xfs_buf_iodone;
  588. xfs_buf_delwri_queue(bp);
  589. trace_xfs_trans_log_buf(bip);
  590. /*
  591. * If we invalidated the buffer within this transaction, then
  592. * cancel the invalidation now that we're dirtying the buffer
  593. * again. There are no races with the code in xfs_buf_item_unpin(),
  594. * because we have a reference to the buffer this entire time.
  595. */
  596. if (bip->bli_flags & XFS_BLI_STALE) {
  597. bip->bli_flags &= ~XFS_BLI_STALE;
  598. ASSERT(XFS_BUF_ISSTALE(bp));
  599. XFS_BUF_UNSTALE(bp);
  600. bip->bli_format.blf_flags &= ~XFS_BLF_CANCEL;
  601. }
  602. tp->t_flags |= XFS_TRANS_DIRTY;
  603. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  604. bip->bli_flags |= XFS_BLI_LOGGED;
  605. xfs_buf_item_log(bip, first, last);
  606. }
  607. /*
  608. * This called to invalidate a buffer that is being used within
  609. * a transaction. Typically this is because the blocks in the
  610. * buffer are being freed, so we need to prevent it from being
  611. * written out when we're done. Allowing it to be written again
  612. * might overwrite data in the free blocks if they are reallocated
  613. * to a file.
  614. *
  615. * We prevent the buffer from being written out by clearing the
  616. * B_DELWRI flag. We can't always
  617. * get rid of the buf log item at this point, though, because
  618. * the buffer may still be pinned by another transaction. If that
  619. * is the case, then we'll wait until the buffer is committed to
  620. * disk for the last time (we can tell by the ref count) and
  621. * free it in xfs_buf_item_unpin(). Until it is cleaned up we
  622. * will keep the buffer locked so that the buffer and buf log item
  623. * are not reused.
  624. */
  625. void
  626. xfs_trans_binval(
  627. xfs_trans_t *tp,
  628. xfs_buf_t *bp)
  629. {
  630. xfs_buf_log_item_t *bip = bp->b_fspriv;
  631. ASSERT(bp->b_transp == tp);
  632. ASSERT(bip != NULL);
  633. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  634. trace_xfs_trans_binval(bip);
  635. if (bip->bli_flags & XFS_BLI_STALE) {
  636. /*
  637. * If the buffer is already invalidated, then
  638. * just return.
  639. */
  640. ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
  641. ASSERT(XFS_BUF_ISSTALE(bp));
  642. ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
  643. ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_INODE_BUF));
  644. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  645. ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
  646. ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
  647. return;
  648. }
  649. /*
  650. * Clear the dirty bit in the buffer and set the STALE flag
  651. * in the buf log item. The STALE flag will be used in
  652. * xfs_buf_item_unpin() to determine if it should clean up
  653. * when the last reference to the buf item is given up.
  654. * We set the XFS_BLF_CANCEL flag in the buf log format structure
  655. * and log the buf item. This will be used at recovery time
  656. * to determine that copies of the buffer in the log before
  657. * this should not be replayed.
  658. * We mark the item descriptor and the transaction dirty so
  659. * that we'll hold the buffer until after the commit.
  660. *
  661. * Since we're invalidating the buffer, we also clear the state
  662. * about which parts of the buffer have been logged. We also
  663. * clear the flag indicating that this is an inode buffer since
  664. * the data in the buffer will no longer be valid.
  665. *
  666. * We set the stale bit in the buffer as well since we're getting
  667. * rid of it.
  668. */
  669. xfs_buf_stale(bp);
  670. bip->bli_flags |= XFS_BLI_STALE;
  671. bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
  672. bip->bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
  673. bip->bli_format.blf_flags |= XFS_BLF_CANCEL;
  674. memset((char *)(bip->bli_format.blf_data_map), 0,
  675. (bip->bli_format.blf_map_size * sizeof(uint)));
  676. bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  677. tp->t_flags |= XFS_TRANS_DIRTY;
  678. }
  679. /*
  680. * This call is used to indicate that the buffer contains on-disk inodes which
  681. * must be handled specially during recovery. They require special handling
  682. * because only the di_next_unlinked from the inodes in the buffer should be
  683. * recovered. The rest of the data in the buffer is logged via the inodes
  684. * themselves.
  685. *
  686. * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
  687. * transferred to the buffer's log format structure so that we'll know what to
  688. * do at recovery time.
  689. */
  690. void
  691. xfs_trans_inode_buf(
  692. xfs_trans_t *tp,
  693. xfs_buf_t *bp)
  694. {
  695. xfs_buf_log_item_t *bip = bp->b_fspriv;
  696. ASSERT(bp->b_transp == tp);
  697. ASSERT(bip != NULL);
  698. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  699. bip->bli_flags |= XFS_BLI_INODE_BUF;
  700. }
  701. /*
  702. * This call is used to indicate that the buffer is going to
  703. * be staled and was an inode buffer. This means it gets
  704. * special processing during unpin - where any inodes
  705. * associated with the buffer should be removed from ail.
  706. * There is also special processing during recovery,
  707. * any replay of the inodes in the buffer needs to be
  708. * prevented as the buffer may have been reused.
  709. */
  710. void
  711. xfs_trans_stale_inode_buf(
  712. xfs_trans_t *tp,
  713. xfs_buf_t *bp)
  714. {
  715. xfs_buf_log_item_t *bip = bp->b_fspriv;
  716. ASSERT(bp->b_transp == tp);
  717. ASSERT(bip != NULL);
  718. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  719. bip->bli_flags |= XFS_BLI_STALE_INODE;
  720. bip->bli_item.li_cb = xfs_buf_iodone;
  721. }
  722. /*
  723. * Mark the buffer as being one which contains newly allocated
  724. * inodes. We need to make sure that even if this buffer is
  725. * relogged as an 'inode buf' we still recover all of the inode
  726. * images in the face of a crash. This works in coordination with
  727. * xfs_buf_item_committed() to ensure that the buffer remains in the
  728. * AIL at its original location even after it has been relogged.
  729. */
  730. /* ARGSUSED */
  731. void
  732. xfs_trans_inode_alloc_buf(
  733. xfs_trans_t *tp,
  734. xfs_buf_t *bp)
  735. {
  736. xfs_buf_log_item_t *bip = bp->b_fspriv;
  737. ASSERT(bp->b_transp == tp);
  738. ASSERT(bip != NULL);
  739. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  740. bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
  741. }
  742. /*
  743. * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
  744. * dquots. However, unlike in inode buffer recovery, dquot buffers get
  745. * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
  746. * The only thing that makes dquot buffers different from regular
  747. * buffers is that we must not replay dquot bufs when recovering
  748. * if a _corresponding_ quotaoff has happened. We also have to distinguish
  749. * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
  750. * can be turned off independently.
  751. */
  752. /* ARGSUSED */
  753. void
  754. xfs_trans_dquot_buf(
  755. xfs_trans_t *tp,
  756. xfs_buf_t *bp,
  757. uint type)
  758. {
  759. xfs_buf_log_item_t *bip = bp->b_fspriv;
  760. ASSERT(bp->b_transp == tp);
  761. ASSERT(bip != NULL);
  762. ASSERT(type == XFS_BLF_UDQUOT_BUF ||
  763. type == XFS_BLF_PDQUOT_BUF ||
  764. type == XFS_BLF_GDQUOT_BUF);
  765. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  766. bip->bli_format.blf_flags |= type;
  767. }