xfs_trans_buf.c 26 KB

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