xfs_trans_buf.c 32 KB

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