xfs_trans_buf.c 26 KB

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