xfs_trans_buf.c 31 KB

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