xfs_trans_buf.c 31 KB

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