xfs_trans_buf.c 31 KB

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