xfs_trans_buf.c 31 KB

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