xfs_trans_buf.c 23 KB

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