xfs_trans_buf.c 31 KB

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