xfs_trans_buf.c 31 KB

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