xfs_trans_buf.c 28 KB

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