xfs_trans_buf.c 26 KB

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