xfs_trans_buf.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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(
  484. bip->bli_item.li_mountp,
  485. lip);
  486. }
  487. }
  488. xfs_buf_relse(bp);
  489. return;
  490. }
  491. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  492. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  493. ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
  494. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  495. ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
  496. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  497. /*
  498. * Find the item descriptor pointing to this buffer's
  499. * log item. It must be there.
  500. */
  501. lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
  502. ASSERT(lidp != NULL);
  503. /*
  504. * If the release is just for a recursive lock,
  505. * then decrement the count and return.
  506. */
  507. if (bip->bli_recur > 0) {
  508. bip->bli_recur--;
  509. xfs_buf_item_trace("RELSE RECUR", bip);
  510. return;
  511. }
  512. /*
  513. * If the buffer is dirty within this transaction, we can't
  514. * release it until we commit.
  515. */
  516. if (lidp->lid_flags & XFS_LID_DIRTY) {
  517. xfs_buf_item_trace("RELSE DIRTY", bip);
  518. return;
  519. }
  520. /*
  521. * If the buffer has been invalidated, then we can't release
  522. * it until the transaction commits to disk unless it is re-dirtied
  523. * as part of this transaction. This prevents us from pulling
  524. * the item from the AIL before we should.
  525. */
  526. if (bip->bli_flags & XFS_BLI_STALE) {
  527. xfs_buf_item_trace("RELSE STALE", bip);
  528. return;
  529. }
  530. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  531. xfs_buf_item_trace("RELSE", bip);
  532. /*
  533. * Free up the log item descriptor tracking the released item.
  534. */
  535. xfs_trans_free_item(tp, lidp);
  536. /*
  537. * Clear the hold flag in the buf log item if it is set.
  538. * We wouldn't want the next user of the buffer to
  539. * get confused.
  540. */
  541. if (bip->bli_flags & XFS_BLI_HOLD) {
  542. bip->bli_flags &= ~XFS_BLI_HOLD;
  543. }
  544. /*
  545. * Drop our reference to the buf log item.
  546. */
  547. atomic_dec(&bip->bli_refcount);
  548. /*
  549. * If the buf item is not tracking data in the log, then
  550. * we must free it before releasing the buffer back to the
  551. * free pool. Before releasing the buffer to the free pool,
  552. * clear the transaction pointer in b_fsprivate2 to dissolve
  553. * its relation to this transaction.
  554. */
  555. if (!xfs_buf_item_dirty(bip)) {
  556. /***
  557. ASSERT(bp->b_pincount == 0);
  558. ***/
  559. ASSERT(atomic_read(&bip->bli_refcount) == 0);
  560. ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
  561. ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
  562. xfs_buf_item_relse(bp);
  563. bip = NULL;
  564. }
  565. XFS_BUF_SET_FSPRIVATE2(bp, NULL);
  566. /*
  567. * If we've still got a buf log item on the buffer, then
  568. * tell the AIL that the buffer is being unlocked.
  569. */
  570. if (bip != NULL) {
  571. xfs_trans_unlocked_item(bip->bli_item.li_mountp,
  572. (xfs_log_item_t*)bip);
  573. }
  574. xfs_buf_relse(bp);
  575. return;
  576. }
  577. /*
  578. * Add the locked buffer to the transaction.
  579. * The buffer must be locked, and it cannot be associated with any
  580. * transaction.
  581. *
  582. * If the buffer does not yet have a buf log item associated with it,
  583. * then allocate one for it. Then add the buf item to the transaction.
  584. */
  585. void
  586. xfs_trans_bjoin(xfs_trans_t *tp,
  587. xfs_buf_t *bp)
  588. {
  589. xfs_buf_log_item_t *bip;
  590. ASSERT(XFS_BUF_ISBUSY(bp));
  591. ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
  592. /*
  593. * The xfs_buf_log_item pointer is stored in b_fsprivate. If
  594. * it doesn't have one yet, then allocate one and initialize it.
  595. * The checks to see if one is there are in xfs_buf_item_init().
  596. */
  597. xfs_buf_item_init(bp, tp->t_mountp);
  598. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  599. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  600. ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
  601. ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
  602. /*
  603. * Take a reference for this transaction on the buf item.
  604. */
  605. atomic_inc(&bip->bli_refcount);
  606. /*
  607. * Get a log_item_desc to point at the new item.
  608. */
  609. (void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
  610. /*
  611. * Initialize b_fsprivate2 so we can find it with incore_match()
  612. * in xfs_trans_get_buf() and friends above.
  613. */
  614. XFS_BUF_SET_FSPRIVATE2(bp, tp);
  615. xfs_buf_item_trace("BJOIN", bip);
  616. }
  617. /*
  618. * Mark the buffer as not needing to be unlocked when the buf item's
  619. * IOP_UNLOCK() routine is called. The buffer must already be locked
  620. * and associated with the given transaction.
  621. */
  622. /* ARGSUSED */
  623. void
  624. xfs_trans_bhold(xfs_trans_t *tp,
  625. xfs_buf_t *bp)
  626. {
  627. xfs_buf_log_item_t *bip;
  628. ASSERT(XFS_BUF_ISBUSY(bp));
  629. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  630. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  631. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  632. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  633. ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
  634. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  635. bip->bli_flags |= XFS_BLI_HOLD;
  636. xfs_buf_item_trace("BHOLD", bip);
  637. }
  638. /*
  639. * Cancel the previous buffer hold request made on this buffer
  640. * for this transaction.
  641. */
  642. void
  643. xfs_trans_bhold_release(xfs_trans_t *tp,
  644. xfs_buf_t *bp)
  645. {
  646. xfs_buf_log_item_t *bip;
  647. ASSERT(XFS_BUF_ISBUSY(bp));
  648. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  649. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  650. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  651. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  652. ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
  653. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  654. ASSERT(bip->bli_flags & XFS_BLI_HOLD);
  655. bip->bli_flags &= ~XFS_BLI_HOLD;
  656. xfs_buf_item_trace("BHOLD RELEASE", bip);
  657. }
  658. /*
  659. * This is called to mark bytes first through last inclusive of the given
  660. * buffer as needing to be logged when the transaction is committed.
  661. * The buffer must already be associated with the given transaction.
  662. *
  663. * First and last are numbers relative to the beginning of this buffer,
  664. * so the first byte in the buffer is numbered 0 regardless of the
  665. * value of b_blkno.
  666. */
  667. void
  668. xfs_trans_log_buf(xfs_trans_t *tp,
  669. xfs_buf_t *bp,
  670. uint first,
  671. uint last)
  672. {
  673. xfs_buf_log_item_t *bip;
  674. xfs_log_item_desc_t *lidp;
  675. ASSERT(XFS_BUF_ISBUSY(bp));
  676. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  677. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  678. ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
  679. ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) ||
  680. (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks));
  681. /*
  682. * Mark the buffer as needing to be written out eventually,
  683. * and set its iodone function to remove the buffer's buf log
  684. * item from the AIL and free it when the buffer is flushed
  685. * to disk. See xfs_buf_attach_iodone() for more details
  686. * on li_cb and xfs_buf_iodone_callbacks().
  687. * If we end up aborting this transaction, we trap this buffer
  688. * inside the b_bdstrat callback so that this won't get written to
  689. * disk.
  690. */
  691. XFS_BUF_DELAYWRITE(bp);
  692. XFS_BUF_DONE(bp);
  693. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  694. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  695. XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
  696. bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone;
  697. /*
  698. * If we invalidated the buffer within this transaction, then
  699. * cancel the invalidation now that we're dirtying the buffer
  700. * again. There are no races with the code in xfs_buf_item_unpin(),
  701. * because we have a reference to the buffer this entire time.
  702. */
  703. if (bip->bli_flags & XFS_BLI_STALE) {
  704. xfs_buf_item_trace("BLOG UNSTALE", bip);
  705. bip->bli_flags &= ~XFS_BLI_STALE;
  706. ASSERT(XFS_BUF_ISSTALE(bp));
  707. XFS_BUF_UNSTALE(bp);
  708. bip->bli_format.blf_flags &= ~XFS_BLI_CANCEL;
  709. }
  710. lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
  711. ASSERT(lidp != NULL);
  712. tp->t_flags |= XFS_TRANS_DIRTY;
  713. lidp->lid_flags |= XFS_LID_DIRTY;
  714. lidp->lid_flags &= ~XFS_LID_BUF_STALE;
  715. bip->bli_flags |= XFS_BLI_LOGGED;
  716. xfs_buf_item_log(bip, first, last);
  717. xfs_buf_item_trace("BLOG", bip);
  718. }
  719. /*
  720. * This called to invalidate a buffer that is being used within
  721. * a transaction. Typically this is because the blocks in the
  722. * buffer are being freed, so we need to prevent it from being
  723. * written out when we're done. Allowing it to be written again
  724. * might overwrite data in the free blocks if they are reallocated
  725. * to a file.
  726. *
  727. * We prevent the buffer from being written out by clearing the
  728. * B_DELWRI flag. We can't always
  729. * get rid of the buf log item at this point, though, because
  730. * the buffer may still be pinned by another transaction. If that
  731. * is the case, then we'll wait until the buffer is committed to
  732. * disk for the last time (we can tell by the ref count) and
  733. * free it in xfs_buf_item_unpin(). Until it is cleaned up we
  734. * will keep the buffer locked so that the buffer and buf log item
  735. * are not reused.
  736. */
  737. void
  738. xfs_trans_binval(
  739. xfs_trans_t *tp,
  740. xfs_buf_t *bp)
  741. {
  742. xfs_log_item_desc_t *lidp;
  743. xfs_buf_log_item_t *bip;
  744. ASSERT(XFS_BUF_ISBUSY(bp));
  745. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  746. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  747. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  748. lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
  749. ASSERT(lidp != NULL);
  750. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  751. if (bip->bli_flags & XFS_BLI_STALE) {
  752. /*
  753. * If the buffer is already invalidated, then
  754. * just return.
  755. */
  756. ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
  757. ASSERT(XFS_BUF_ISSTALE(bp));
  758. ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
  759. ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_INODE_BUF));
  760. ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
  761. ASSERT(lidp->lid_flags & XFS_LID_DIRTY);
  762. ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
  763. xfs_buftrace("XFS_BINVAL RECUR", bp);
  764. xfs_buf_item_trace("BINVAL RECUR", bip);
  765. return;
  766. }
  767. /*
  768. * Clear the dirty bit in the buffer and set the STALE flag
  769. * in the buf log item. The STALE flag will be used in
  770. * xfs_buf_item_unpin() to determine if it should clean up
  771. * when the last reference to the buf item is given up.
  772. * We set the XFS_BLI_CANCEL flag in the buf log format structure
  773. * and log the buf item. This will be used at recovery time
  774. * to determine that copies of the buffer in the log before
  775. * this should not be replayed.
  776. * We mark the item descriptor and the transaction dirty so
  777. * that we'll hold the buffer until after the commit.
  778. *
  779. * Since we're invalidating the buffer, we also clear the state
  780. * about which parts of the buffer have been logged. We also
  781. * clear the flag indicating that this is an inode buffer since
  782. * the data in the buffer will no longer be valid.
  783. *
  784. * We set the stale bit in the buffer as well since we're getting
  785. * rid of it.
  786. */
  787. XFS_BUF_UNDELAYWRITE(bp);
  788. XFS_BUF_STALE(bp);
  789. bip->bli_flags |= XFS_BLI_STALE;
  790. bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_DIRTY);
  791. bip->bli_format.blf_flags &= ~XFS_BLI_INODE_BUF;
  792. bip->bli_format.blf_flags |= XFS_BLI_CANCEL;
  793. memset((char *)(bip->bli_format.blf_data_map), 0,
  794. (bip->bli_format.blf_map_size * sizeof(uint)));
  795. lidp->lid_flags |= XFS_LID_DIRTY|XFS_LID_BUF_STALE;
  796. tp->t_flags |= XFS_TRANS_DIRTY;
  797. xfs_buftrace("XFS_BINVAL", bp);
  798. xfs_buf_item_trace("BINVAL", bip);
  799. }
  800. /*
  801. * This call is used to indicate that the buffer contains on-disk
  802. * inodes which must be handled specially during recovery. They
  803. * require special handling because only the di_next_unlinked from
  804. * the inodes in the buffer should be recovered. The rest of the
  805. * data in the buffer is logged via the inodes themselves.
  806. *
  807. * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log
  808. * format structure so that we'll know what to do at recovery time.
  809. */
  810. /* ARGSUSED */
  811. void
  812. xfs_trans_inode_buf(
  813. xfs_trans_t *tp,
  814. xfs_buf_t *bp)
  815. {
  816. xfs_buf_log_item_t *bip;
  817. ASSERT(XFS_BUF_ISBUSY(bp));
  818. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  819. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  820. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  821. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  822. bip->bli_format.blf_flags |= XFS_BLI_INODE_BUF;
  823. }
  824. /*
  825. * This call is used to indicate that the buffer is going to
  826. * be staled and was an inode buffer. This means it gets
  827. * special processing during unpin - where any inodes
  828. * associated with the buffer should be removed from ail.
  829. * There is also special processing during recovery,
  830. * any replay of the inodes in the buffer needs to be
  831. * prevented as the buffer may have been reused.
  832. */
  833. void
  834. xfs_trans_stale_inode_buf(
  835. xfs_trans_t *tp,
  836. xfs_buf_t *bp)
  837. {
  838. xfs_buf_log_item_t *bip;
  839. ASSERT(XFS_BUF_ISBUSY(bp));
  840. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  841. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  842. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  843. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  844. bip->bli_flags |= XFS_BLI_STALE_INODE;
  845. bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))
  846. xfs_buf_iodone;
  847. }
  848. /*
  849. * Mark the buffer as being one which contains newly allocated
  850. * inodes. We need to make sure that even if this buffer is
  851. * relogged as an 'inode buf' we still recover all of the inode
  852. * images in the face of a crash. This works in coordination with
  853. * xfs_buf_item_committed() to ensure that the buffer remains in the
  854. * AIL at its original location even after it has been relogged.
  855. */
  856. /* ARGSUSED */
  857. void
  858. xfs_trans_inode_alloc_buf(
  859. xfs_trans_t *tp,
  860. xfs_buf_t *bp)
  861. {
  862. xfs_buf_log_item_t *bip;
  863. ASSERT(XFS_BUF_ISBUSY(bp));
  864. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  865. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  866. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  867. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  868. bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
  869. }
  870. /*
  871. * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
  872. * dquots. However, unlike in inode buffer recovery, dquot buffers get
  873. * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
  874. * The only thing that makes dquot buffers different from regular
  875. * buffers is that we must not replay dquot bufs when recovering
  876. * if a _corresponding_ quotaoff has happened. We also have to distinguish
  877. * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
  878. * can be turned off independently.
  879. */
  880. /* ARGSUSED */
  881. void
  882. xfs_trans_dquot_buf(
  883. xfs_trans_t *tp,
  884. xfs_buf_t *bp,
  885. uint type)
  886. {
  887. xfs_buf_log_item_t *bip;
  888. ASSERT(XFS_BUF_ISBUSY(bp));
  889. ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
  890. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  891. ASSERT(type == XFS_BLI_UDQUOT_BUF ||
  892. type == XFS_BLI_PDQUOT_BUF ||
  893. type == XFS_BLI_GDQUOT_BUF);
  894. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
  895. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  896. bip->bli_format.blf_flags |= type;
  897. }
  898. /*
  899. * Check to see if a buffer matching the given parameters is already
  900. * a part of the given transaction. Only check the first, embedded
  901. * chunk, since we don't want to spend all day scanning large transactions.
  902. */
  903. STATIC xfs_buf_t *
  904. xfs_trans_buf_item_match(
  905. xfs_trans_t *tp,
  906. xfs_buftarg_t *target,
  907. xfs_daddr_t blkno,
  908. int len)
  909. {
  910. xfs_log_item_chunk_t *licp;
  911. xfs_log_item_desc_t *lidp;
  912. xfs_buf_log_item_t *blip;
  913. xfs_buf_t *bp;
  914. int i;
  915. bp = NULL;
  916. len = BBTOB(len);
  917. licp = &tp->t_items;
  918. if (!xfs_lic_are_all_free(licp)) {
  919. for (i = 0; i < licp->lic_unused; i++) {
  920. /*
  921. * Skip unoccupied slots.
  922. */
  923. if (xfs_lic_isfree(licp, i)) {
  924. continue;
  925. }
  926. lidp = xfs_lic_slot(licp, i);
  927. blip = (xfs_buf_log_item_t *)lidp->lid_item;
  928. if (blip->bli_item.li_type != XFS_LI_BUF) {
  929. continue;
  930. }
  931. bp = blip->bli_buf;
  932. if ((XFS_BUF_TARGET(bp) == target) &&
  933. (XFS_BUF_ADDR(bp) == blkno) &&
  934. (XFS_BUF_COUNT(bp) == len)) {
  935. /*
  936. * We found it. Break out and
  937. * return the pointer to the buffer.
  938. */
  939. break;
  940. } else {
  941. bp = NULL;
  942. }
  943. }
  944. }
  945. return bp;
  946. }
  947. /*
  948. * Check to see if a buffer matching the given parameters is already
  949. * a part of the given transaction. Check all the chunks, we
  950. * want to be thorough.
  951. */
  952. STATIC xfs_buf_t *
  953. xfs_trans_buf_item_match_all(
  954. xfs_trans_t *tp,
  955. xfs_buftarg_t *target,
  956. xfs_daddr_t blkno,
  957. int len)
  958. {
  959. xfs_log_item_chunk_t *licp;
  960. xfs_log_item_desc_t *lidp;
  961. xfs_buf_log_item_t *blip;
  962. xfs_buf_t *bp;
  963. int i;
  964. bp = NULL;
  965. len = BBTOB(len);
  966. for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) {
  967. if (xfs_lic_are_all_free(licp)) {
  968. ASSERT(licp == &tp->t_items);
  969. ASSERT(licp->lic_next == NULL);
  970. return NULL;
  971. }
  972. for (i = 0; i < licp->lic_unused; i++) {
  973. /*
  974. * Skip unoccupied slots.
  975. */
  976. if (xfs_lic_isfree(licp, i)) {
  977. continue;
  978. }
  979. lidp = xfs_lic_slot(licp, i);
  980. blip = (xfs_buf_log_item_t *)lidp->lid_item;
  981. if (blip->bli_item.li_type != XFS_LI_BUF) {
  982. continue;
  983. }
  984. bp = blip->bli_buf;
  985. if ((XFS_BUF_TARGET(bp) == target) &&
  986. (XFS_BUF_ADDR(bp) == blkno) &&
  987. (XFS_BUF_COUNT(bp) == len)) {
  988. /*
  989. * We found it. Break out and
  990. * return the pointer to the buffer.
  991. */
  992. return bp;
  993. }
  994. }
  995. }
  996. return NULL;
  997. }