xfs_trans_buf.c 30 KB

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