xfs_trans_buf.c 31 KB

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