xfs_trans_buf.c 25 KB

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