xfs_trans_buf.c 30 KB

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