xfs_buf_item.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * Copyright (c) 2000-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_dmapi.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_buf_item.h"
  30. #include "xfs_trans_priv.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. kmem_zone_t *xfs_buf_item_zone;
  34. #ifdef XFS_TRANS_DEBUG
  35. /*
  36. * This function uses an alternate strategy for tracking the bytes
  37. * that the user requests to be logged. This can then be used
  38. * in conjunction with the bli_orig array in the buf log item to
  39. * catch bugs in our callers' code.
  40. *
  41. * We also double check the bits set in xfs_buf_item_log using a
  42. * simple algorithm to check that every byte is accounted for.
  43. */
  44. STATIC void
  45. xfs_buf_item_log_debug(
  46. xfs_buf_log_item_t *bip,
  47. uint first,
  48. uint last)
  49. {
  50. uint x;
  51. uint byte;
  52. uint nbytes;
  53. uint chunk_num;
  54. uint word_num;
  55. uint bit_num;
  56. uint bit_set;
  57. uint *wordp;
  58. ASSERT(bip->bli_logged != NULL);
  59. byte = first;
  60. nbytes = last - first + 1;
  61. bfset(bip->bli_logged, first, nbytes);
  62. for (x = 0; x < nbytes; x++) {
  63. chunk_num = byte >> XFS_BLI_SHIFT;
  64. word_num = chunk_num >> BIT_TO_WORD_SHIFT;
  65. bit_num = chunk_num & (NBWORD - 1);
  66. wordp = &(bip->bli_format.blf_data_map[word_num]);
  67. bit_set = *wordp & (1 << bit_num);
  68. ASSERT(bit_set);
  69. byte++;
  70. }
  71. }
  72. /*
  73. * This function is called when we flush something into a buffer without
  74. * logging it. This happens for things like inodes which are logged
  75. * separately from the buffer.
  76. */
  77. void
  78. xfs_buf_item_flush_log_debug(
  79. xfs_buf_t *bp,
  80. uint first,
  81. uint last)
  82. {
  83. xfs_buf_log_item_t *bip;
  84. uint nbytes;
  85. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
  86. if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) {
  87. return;
  88. }
  89. ASSERT(bip->bli_logged != NULL);
  90. nbytes = last - first + 1;
  91. bfset(bip->bli_logged, first, nbytes);
  92. }
  93. /*
  94. * This function is called to verify that our callers have logged
  95. * all the bytes that they changed.
  96. *
  97. * It does this by comparing the original copy of the buffer stored in
  98. * the buf log item's bli_orig array to the current copy of the buffer
  99. * and ensuring that all bytes which mismatch are set in the bli_logged
  100. * array of the buf log item.
  101. */
  102. STATIC void
  103. xfs_buf_item_log_check(
  104. xfs_buf_log_item_t *bip)
  105. {
  106. char *orig;
  107. char *buffer;
  108. int x;
  109. xfs_buf_t *bp;
  110. ASSERT(bip->bli_orig != NULL);
  111. ASSERT(bip->bli_logged != NULL);
  112. bp = bip->bli_buf;
  113. ASSERT(XFS_BUF_COUNT(bp) > 0);
  114. ASSERT(XFS_BUF_PTR(bp) != NULL);
  115. orig = bip->bli_orig;
  116. buffer = XFS_BUF_PTR(bp);
  117. for (x = 0; x < XFS_BUF_COUNT(bp); x++) {
  118. if (orig[x] != buffer[x] && !btst(bip->bli_logged, x))
  119. cmn_err(CE_PANIC,
  120. "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
  121. bip, bp, orig, x);
  122. }
  123. }
  124. #else
  125. #define xfs_buf_item_log_debug(x,y,z)
  126. #define xfs_buf_item_log_check(x)
  127. #endif
  128. STATIC void xfs_buf_error_relse(xfs_buf_t *bp);
  129. STATIC void xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip);
  130. /*
  131. * This returns the number of log iovecs needed to log the
  132. * given buf log item.
  133. *
  134. * It calculates this as 1 iovec for the buf log format structure
  135. * and 1 for each stretch of non-contiguous chunks to be logged.
  136. * Contiguous chunks are logged in a single iovec.
  137. *
  138. * If the XFS_BLI_STALE flag has been set, then log nothing.
  139. */
  140. STATIC uint
  141. xfs_buf_item_size(
  142. xfs_buf_log_item_t *bip)
  143. {
  144. uint nvecs;
  145. int next_bit;
  146. int last_bit;
  147. xfs_buf_t *bp;
  148. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  149. if (bip->bli_flags & XFS_BLI_STALE) {
  150. /*
  151. * The buffer is stale, so all we need to log
  152. * is the buf log format structure with the
  153. * cancel flag in it.
  154. */
  155. trace_xfs_buf_item_size_stale(bip);
  156. ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
  157. return 1;
  158. }
  159. bp = bip->bli_buf;
  160. ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
  161. nvecs = 1;
  162. last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  163. bip->bli_format.blf_map_size, 0);
  164. ASSERT(last_bit != -1);
  165. nvecs++;
  166. while (last_bit != -1) {
  167. /*
  168. * This takes the bit number to start looking from and
  169. * returns the next set bit from there. It returns -1
  170. * if there are no more bits set or the start bit is
  171. * beyond the end of the bitmap.
  172. */
  173. next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  174. bip->bli_format.blf_map_size,
  175. last_bit + 1);
  176. /*
  177. * If we run out of bits, leave the loop,
  178. * else if we find a new set of bits bump the number of vecs,
  179. * else keep scanning the current set of bits.
  180. */
  181. if (next_bit == -1) {
  182. last_bit = -1;
  183. } else if (next_bit != last_bit + 1) {
  184. last_bit = next_bit;
  185. nvecs++;
  186. } else if (xfs_buf_offset(bp, next_bit * XFS_BLI_CHUNK) !=
  187. (xfs_buf_offset(bp, last_bit * XFS_BLI_CHUNK) +
  188. XFS_BLI_CHUNK)) {
  189. last_bit = next_bit;
  190. nvecs++;
  191. } else {
  192. last_bit++;
  193. }
  194. }
  195. trace_xfs_buf_item_size(bip);
  196. return nvecs;
  197. }
  198. /*
  199. * This is called to fill in the vector of log iovecs for the
  200. * given log buf item. It fills the first entry with a buf log
  201. * format structure, and the rest point to contiguous chunks
  202. * within the buffer.
  203. */
  204. STATIC void
  205. xfs_buf_item_format(
  206. xfs_buf_log_item_t *bip,
  207. xfs_log_iovec_t *log_vector)
  208. {
  209. uint base_size;
  210. uint nvecs;
  211. xfs_log_iovec_t *vecp;
  212. xfs_buf_t *bp;
  213. int first_bit;
  214. int last_bit;
  215. int next_bit;
  216. uint nbits;
  217. uint buffer_offset;
  218. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  219. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  220. (bip->bli_flags & XFS_BLI_STALE));
  221. bp = bip->bli_buf;
  222. vecp = log_vector;
  223. /*
  224. * The size of the base structure is the size of the
  225. * declared structure plus the space for the extra words
  226. * of the bitmap. We subtract one from the map size, because
  227. * the first element of the bitmap is accounted for in the
  228. * size of the base structure.
  229. */
  230. base_size =
  231. (uint)(sizeof(xfs_buf_log_format_t) +
  232. ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
  233. vecp->i_addr = (xfs_caddr_t)&bip->bli_format;
  234. vecp->i_len = base_size;
  235. vecp->i_type = XLOG_REG_TYPE_BFORMAT;
  236. vecp++;
  237. nvecs = 1;
  238. if (bip->bli_flags & XFS_BLI_STALE) {
  239. /*
  240. * The buffer is stale, so all we need to log
  241. * is the buf log format structure with the
  242. * cancel flag in it.
  243. */
  244. trace_xfs_buf_item_format_stale(bip);
  245. ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
  246. bip->bli_format.blf_size = nvecs;
  247. return;
  248. }
  249. /*
  250. * Fill in an iovec for each set of contiguous chunks.
  251. */
  252. first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  253. bip->bli_format.blf_map_size, 0);
  254. ASSERT(first_bit != -1);
  255. last_bit = first_bit;
  256. nbits = 1;
  257. for (;;) {
  258. /*
  259. * This takes the bit number to start looking from and
  260. * returns the next set bit from there. It returns -1
  261. * if there are no more bits set or the start bit is
  262. * beyond the end of the bitmap.
  263. */
  264. next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  265. bip->bli_format.blf_map_size,
  266. (uint)last_bit + 1);
  267. /*
  268. * If we run out of bits fill in the last iovec and get
  269. * out of the loop.
  270. * Else if we start a new set of bits then fill in the
  271. * iovec for the series we were looking at and start
  272. * counting the bits in the new one.
  273. * Else we're still in the same set of bits so just
  274. * keep counting and scanning.
  275. */
  276. if (next_bit == -1) {
  277. buffer_offset = first_bit * XFS_BLI_CHUNK;
  278. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  279. vecp->i_len = nbits * XFS_BLI_CHUNK;
  280. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  281. nvecs++;
  282. break;
  283. } else if (next_bit != last_bit + 1) {
  284. buffer_offset = first_bit * XFS_BLI_CHUNK;
  285. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  286. vecp->i_len = nbits * XFS_BLI_CHUNK;
  287. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  288. nvecs++;
  289. vecp++;
  290. first_bit = next_bit;
  291. last_bit = next_bit;
  292. nbits = 1;
  293. } else if (xfs_buf_offset(bp, next_bit << XFS_BLI_SHIFT) !=
  294. (xfs_buf_offset(bp, last_bit << XFS_BLI_SHIFT) +
  295. XFS_BLI_CHUNK)) {
  296. buffer_offset = first_bit * XFS_BLI_CHUNK;
  297. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  298. vecp->i_len = nbits * XFS_BLI_CHUNK;
  299. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  300. /* You would think we need to bump the nvecs here too, but we do not
  301. * this number is used by recovery, and it gets confused by the boundary
  302. * split here
  303. * nvecs++;
  304. */
  305. vecp++;
  306. first_bit = next_bit;
  307. last_bit = next_bit;
  308. nbits = 1;
  309. } else {
  310. last_bit++;
  311. nbits++;
  312. }
  313. }
  314. bip->bli_format.blf_size = nvecs;
  315. /*
  316. * Check to make sure everything is consistent.
  317. */
  318. trace_xfs_buf_item_format(bip);
  319. xfs_buf_item_log_check(bip);
  320. }
  321. /*
  322. * This is called to pin the buffer associated with the buf log
  323. * item in memory so it cannot be written out. Simply call bpin()
  324. * on the buffer to do this.
  325. */
  326. STATIC void
  327. xfs_buf_item_pin(
  328. xfs_buf_log_item_t *bip)
  329. {
  330. xfs_buf_t *bp;
  331. bp = bip->bli_buf;
  332. ASSERT(XFS_BUF_ISBUSY(bp));
  333. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  334. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  335. (bip->bli_flags & XFS_BLI_STALE));
  336. trace_xfs_buf_item_pin(bip);
  337. xfs_bpin(bp);
  338. }
  339. /*
  340. * This is called to unpin the buffer associated with the buf log
  341. * item which was previously pinned with a call to xfs_buf_item_pin().
  342. * Just call bunpin() on the buffer to do this.
  343. *
  344. * Also drop the reference to the buf item for the current transaction.
  345. * If the XFS_BLI_STALE flag is set and we are the last reference,
  346. * then free up the buf log item and unlock the buffer.
  347. */
  348. STATIC void
  349. xfs_buf_item_unpin(
  350. xfs_buf_log_item_t *bip,
  351. int stale)
  352. {
  353. struct xfs_ail *ailp;
  354. xfs_buf_t *bp;
  355. int freed;
  356. bp = bip->bli_buf;
  357. ASSERT(bp != NULL);
  358. ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip);
  359. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  360. trace_xfs_buf_item_unpin(bip);
  361. freed = atomic_dec_and_test(&bip->bli_refcount);
  362. ailp = bip->bli_item.li_ailp;
  363. xfs_bunpin(bp);
  364. if (freed && stale) {
  365. ASSERT(bip->bli_flags & XFS_BLI_STALE);
  366. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  367. ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
  368. ASSERT(XFS_BUF_ISSTALE(bp));
  369. ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
  370. trace_xfs_buf_item_unpin_stale(bip);
  371. /*
  372. * If we get called here because of an IO error, we may
  373. * or may not have the item on the AIL. xfs_trans_ail_delete()
  374. * will take care of that situation.
  375. * xfs_trans_ail_delete() drops the AIL lock.
  376. */
  377. if (bip->bli_flags & XFS_BLI_STALE_INODE) {
  378. xfs_buf_do_callbacks(bp, (xfs_log_item_t *)bip);
  379. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  380. XFS_BUF_CLR_IODONE_FUNC(bp);
  381. } else {
  382. spin_lock(&ailp->xa_lock);
  383. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)bip);
  384. xfs_buf_item_relse(bp);
  385. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL);
  386. }
  387. xfs_buf_relse(bp);
  388. }
  389. }
  390. /*
  391. * this is called from uncommit in the forced-shutdown path.
  392. * we need to check to see if the reference count on the log item
  393. * is going to drop to zero. If so, unpin will free the log item
  394. * so we need to free the item's descriptor (that points to the item)
  395. * in the transaction.
  396. */
  397. STATIC void
  398. xfs_buf_item_unpin_remove(
  399. xfs_buf_log_item_t *bip,
  400. xfs_trans_t *tp)
  401. {
  402. xfs_buf_t *bp;
  403. xfs_log_item_desc_t *lidp;
  404. int stale = 0;
  405. bp = bip->bli_buf;
  406. /*
  407. * will xfs_buf_item_unpin() call xfs_buf_item_relse()?
  408. */
  409. if ((atomic_read(&bip->bli_refcount) == 1) &&
  410. (bip->bli_flags & XFS_BLI_STALE)) {
  411. ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0);
  412. trace_xfs_buf_item_unpin_stale(bip);
  413. /*
  414. * yes -- clear the xaction descriptor in-use flag
  415. * and free the chunk if required. We can safely
  416. * do some work here and then call buf_item_unpin
  417. * to do the rest because if the if is true, then
  418. * we are holding the buffer locked so no one else
  419. * will be able to bump up the refcount.
  420. */
  421. lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) bip);
  422. stale = lidp->lid_flags & XFS_LID_BUF_STALE;
  423. xfs_trans_free_item(tp, lidp);
  424. /*
  425. * Since the transaction no longer refers to the buffer,
  426. * the buffer should no longer refer to the transaction.
  427. */
  428. XFS_BUF_SET_FSPRIVATE2(bp, NULL);
  429. }
  430. xfs_buf_item_unpin(bip, stale);
  431. return;
  432. }
  433. /*
  434. * This is called to attempt to lock the buffer associated with this
  435. * buf log item. Don't sleep on the buffer lock. If we can't get
  436. * the lock right away, return 0. If we can get the lock, take a
  437. * reference to the buffer. If this is a delayed write buffer that
  438. * needs AIL help to be written back, invoke the pushbuf routine
  439. * rather than the normal success path.
  440. */
  441. STATIC uint
  442. xfs_buf_item_trylock(
  443. xfs_buf_log_item_t *bip)
  444. {
  445. xfs_buf_t *bp;
  446. bp = bip->bli_buf;
  447. if (XFS_BUF_ISPINNED(bp))
  448. return XFS_ITEM_PINNED;
  449. if (!XFS_BUF_CPSEMA(bp))
  450. return XFS_ITEM_LOCKED;
  451. /* take a reference to the buffer. */
  452. XFS_BUF_HOLD(bp);
  453. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  454. trace_xfs_buf_item_trylock(bip);
  455. if (XFS_BUF_ISDELAYWRITE(bp))
  456. return XFS_ITEM_PUSHBUF;
  457. return XFS_ITEM_SUCCESS;
  458. }
  459. /*
  460. * Release the buffer associated with the buf log item.
  461. * If there is no dirty logged data associated with the
  462. * buffer recorded in the buf log item, then free the
  463. * buf log item and remove the reference to it in the
  464. * buffer.
  465. *
  466. * This call ignores the recursion count. It is only called
  467. * when the buffer should REALLY be unlocked, regardless
  468. * of the recursion count.
  469. *
  470. * If the XFS_BLI_HOLD flag is set in the buf log item, then
  471. * free the log item if necessary but do not unlock the buffer.
  472. * This is for support of xfs_trans_bhold(). Make sure the
  473. * XFS_BLI_HOLD field is cleared if we don't free the item.
  474. */
  475. STATIC void
  476. xfs_buf_item_unlock(
  477. xfs_buf_log_item_t *bip)
  478. {
  479. int aborted;
  480. xfs_buf_t *bp;
  481. uint hold;
  482. bp = bip->bli_buf;
  483. /*
  484. * Clear the buffer's association with this transaction.
  485. */
  486. XFS_BUF_SET_FSPRIVATE2(bp, NULL);
  487. /*
  488. * If this is a transaction abort, don't return early.
  489. * Instead, allow the brelse to happen.
  490. * Normally it would be done for stale (cancelled) buffers
  491. * at unpin time, but we'll never go through the pin/unpin
  492. * cycle if we abort inside commit.
  493. */
  494. aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
  495. /*
  496. * If the buf item is marked stale, then don't do anything.
  497. * We'll unlock the buffer and free the buf item when the
  498. * buffer is unpinned for the last time.
  499. */
  500. if (bip->bli_flags & XFS_BLI_STALE) {
  501. bip->bli_flags &= ~XFS_BLI_LOGGED;
  502. trace_xfs_buf_item_unlock_stale(bip);
  503. ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
  504. if (!aborted)
  505. return;
  506. }
  507. /*
  508. * Drop the transaction's reference to the log item if
  509. * it was not logged as part of the transaction. Otherwise
  510. * we'll drop the reference in xfs_buf_item_unpin() when
  511. * the transaction is really through with the buffer.
  512. */
  513. if (!(bip->bli_flags & XFS_BLI_LOGGED)) {
  514. atomic_dec(&bip->bli_refcount);
  515. } else {
  516. /*
  517. * Clear the logged flag since this is per
  518. * transaction state.
  519. */
  520. bip->bli_flags &= ~XFS_BLI_LOGGED;
  521. }
  522. /*
  523. * Before possibly freeing the buf item, determine if we should
  524. * release the buffer at the end of this routine.
  525. */
  526. hold = bip->bli_flags & XFS_BLI_HOLD;
  527. trace_xfs_buf_item_unlock(bip);
  528. /*
  529. * If the buf item isn't tracking any data, free it.
  530. * Otherwise, if XFS_BLI_HOLD is set clear it.
  531. */
  532. if (xfs_bitmap_empty(bip->bli_format.blf_data_map,
  533. bip->bli_format.blf_map_size)) {
  534. xfs_buf_item_relse(bp);
  535. } else if (hold) {
  536. bip->bli_flags &= ~XFS_BLI_HOLD;
  537. }
  538. /*
  539. * Release the buffer if XFS_BLI_HOLD was not set.
  540. */
  541. if (!hold) {
  542. xfs_buf_relse(bp);
  543. }
  544. }
  545. /*
  546. * This is called to find out where the oldest active copy of the
  547. * buf log item in the on disk log resides now that the last log
  548. * write of it completed at the given lsn.
  549. * We always re-log all the dirty data in a buffer, so usually the
  550. * latest copy in the on disk log is the only one that matters. For
  551. * those cases we simply return the given lsn.
  552. *
  553. * The one exception to this is for buffers full of newly allocated
  554. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  555. * flag set, indicating that only the di_next_unlinked fields from the
  556. * inodes in the buffers will be replayed during recovery. If the
  557. * original newly allocated inode images have not yet been flushed
  558. * when the buffer is so relogged, then we need to make sure that we
  559. * keep the old images in the 'active' portion of the log. We do this
  560. * by returning the original lsn of that transaction here rather than
  561. * the current one.
  562. */
  563. STATIC xfs_lsn_t
  564. xfs_buf_item_committed(
  565. xfs_buf_log_item_t *bip,
  566. xfs_lsn_t lsn)
  567. {
  568. trace_xfs_buf_item_committed(bip);
  569. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
  570. (bip->bli_item.li_lsn != 0)) {
  571. return bip->bli_item.li_lsn;
  572. }
  573. return (lsn);
  574. }
  575. /*
  576. * The buffer is locked, but is not a delayed write buffer. This happens
  577. * if we race with IO completion and hence we don't want to try to write it
  578. * again. Just release the buffer.
  579. */
  580. STATIC void
  581. xfs_buf_item_push(
  582. xfs_buf_log_item_t *bip)
  583. {
  584. xfs_buf_t *bp;
  585. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  586. trace_xfs_buf_item_push(bip);
  587. bp = bip->bli_buf;
  588. ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
  589. xfs_buf_relse(bp);
  590. }
  591. /*
  592. * The buffer is locked and is a delayed write buffer. Promote the buffer
  593. * in the delayed write queue as the caller knows that they must invoke
  594. * the xfsbufd to get this buffer written. We have to unlock the buffer
  595. * to allow the xfsbufd to write it, too.
  596. */
  597. STATIC void
  598. xfs_buf_item_pushbuf(
  599. xfs_buf_log_item_t *bip)
  600. {
  601. xfs_buf_t *bp;
  602. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  603. trace_xfs_buf_item_pushbuf(bip);
  604. bp = bip->bli_buf;
  605. ASSERT(XFS_BUF_ISDELAYWRITE(bp));
  606. xfs_buf_delwri_promote(bp);
  607. xfs_buf_relse(bp);
  608. }
  609. /* ARGSUSED */
  610. STATIC void
  611. xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
  612. {
  613. }
  614. /*
  615. * This is the ops vector shared by all buf log items.
  616. */
  617. static struct xfs_item_ops xfs_buf_item_ops = {
  618. .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
  619. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  620. xfs_buf_item_format,
  621. .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
  622. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin,
  623. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
  624. xfs_buf_item_unpin_remove,
  625. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
  626. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
  627. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  628. xfs_buf_item_committed,
  629. .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
  630. .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_buf_item_pushbuf,
  631. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  632. xfs_buf_item_committing
  633. };
  634. /*
  635. * Allocate a new buf log item to go with the given buffer.
  636. * Set the buffer's b_fsprivate field to point to the new
  637. * buf log item. If there are other item's attached to the
  638. * buffer (see xfs_buf_attach_iodone() below), then put the
  639. * buf log item at the front.
  640. */
  641. void
  642. xfs_buf_item_init(
  643. xfs_buf_t *bp,
  644. xfs_mount_t *mp)
  645. {
  646. xfs_log_item_t *lip;
  647. xfs_buf_log_item_t *bip;
  648. int chunks;
  649. int map_size;
  650. /*
  651. * Check to see if there is already a buf log item for
  652. * this buffer. If there is, it is guaranteed to be
  653. * the first. If we do already have one, there is
  654. * nothing to do here so return.
  655. */
  656. if (bp->b_mount != mp)
  657. bp->b_mount = mp;
  658. XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
  659. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  660. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  661. if (lip->li_type == XFS_LI_BUF) {
  662. return;
  663. }
  664. }
  665. /*
  666. * chunks is the number of XFS_BLI_CHUNK size pieces
  667. * the buffer can be divided into. Make sure not to
  668. * truncate any pieces. map_size is the size of the
  669. * bitmap needed to describe the chunks of the buffer.
  670. */
  671. chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT);
  672. map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
  673. bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
  674. KM_SLEEP);
  675. bip->bli_item.li_type = XFS_LI_BUF;
  676. bip->bli_item.li_ops = &xfs_buf_item_ops;
  677. bip->bli_item.li_mountp = mp;
  678. bip->bli_item.li_ailp = mp->m_ail;
  679. bip->bli_buf = bp;
  680. xfs_buf_hold(bp);
  681. bip->bli_format.blf_type = XFS_LI_BUF;
  682. bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
  683. bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
  684. bip->bli_format.blf_map_size = map_size;
  685. #ifdef XFS_TRANS_DEBUG
  686. /*
  687. * Allocate the arrays for tracking what needs to be logged
  688. * and what our callers request to be logged. bli_orig
  689. * holds a copy of the original, clean buffer for comparison
  690. * against, and bli_logged keeps a 1 bit flag per byte in
  691. * the buffer to indicate which bytes the callers have asked
  692. * to have logged.
  693. */
  694. bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
  695. memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
  696. bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
  697. #endif
  698. /*
  699. * Put the buf item into the list of items attached to the
  700. * buffer at the front.
  701. */
  702. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  703. bip->bli_item.li_bio_list =
  704. XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  705. }
  706. XFS_BUF_SET_FSPRIVATE(bp, bip);
  707. }
  708. /*
  709. * Mark bytes first through last inclusive as dirty in the buf
  710. * item's bitmap.
  711. */
  712. void
  713. xfs_buf_item_log(
  714. xfs_buf_log_item_t *bip,
  715. uint first,
  716. uint last)
  717. {
  718. uint first_bit;
  719. uint last_bit;
  720. uint bits_to_set;
  721. uint bits_set;
  722. uint word_num;
  723. uint *wordp;
  724. uint bit;
  725. uint end_bit;
  726. uint mask;
  727. /*
  728. * Mark the item as having some dirty data for
  729. * quick reference in xfs_buf_item_dirty.
  730. */
  731. bip->bli_flags |= XFS_BLI_DIRTY;
  732. /*
  733. * Convert byte offsets to bit numbers.
  734. */
  735. first_bit = first >> XFS_BLI_SHIFT;
  736. last_bit = last >> XFS_BLI_SHIFT;
  737. /*
  738. * Calculate the total number of bits to be set.
  739. */
  740. bits_to_set = last_bit - first_bit + 1;
  741. /*
  742. * Get a pointer to the first word in the bitmap
  743. * to set a bit in.
  744. */
  745. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  746. wordp = &(bip->bli_format.blf_data_map[word_num]);
  747. /*
  748. * Calculate the starting bit in the first word.
  749. */
  750. bit = first_bit & (uint)(NBWORD - 1);
  751. /*
  752. * First set any bits in the first word of our range.
  753. * If it starts at bit 0 of the word, it will be
  754. * set below rather than here. That is what the variable
  755. * bit tells us. The variable bits_set tracks the number
  756. * of bits that have been set so far. End_bit is the number
  757. * of the last bit to be set in this word plus one.
  758. */
  759. if (bit) {
  760. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  761. mask = ((1 << (end_bit - bit)) - 1) << bit;
  762. *wordp |= mask;
  763. wordp++;
  764. bits_set = end_bit - bit;
  765. } else {
  766. bits_set = 0;
  767. }
  768. /*
  769. * Now set bits a whole word at a time that are between
  770. * first_bit and last_bit.
  771. */
  772. while ((bits_to_set - bits_set) >= NBWORD) {
  773. *wordp |= 0xffffffff;
  774. bits_set += NBWORD;
  775. wordp++;
  776. }
  777. /*
  778. * Finally, set any bits left to be set in one last partial word.
  779. */
  780. end_bit = bits_to_set - bits_set;
  781. if (end_bit) {
  782. mask = (1 << end_bit) - 1;
  783. *wordp |= mask;
  784. }
  785. xfs_buf_item_log_debug(bip, first, last);
  786. }
  787. /*
  788. * Return 1 if the buffer has some data that has been logged (at any
  789. * point, not just the current transaction) and 0 if not.
  790. */
  791. uint
  792. xfs_buf_item_dirty(
  793. xfs_buf_log_item_t *bip)
  794. {
  795. return (bip->bli_flags & XFS_BLI_DIRTY);
  796. }
  797. STATIC void
  798. xfs_buf_item_free(
  799. xfs_buf_log_item_t *bip)
  800. {
  801. #ifdef XFS_TRANS_DEBUG
  802. kmem_free(bip->bli_orig);
  803. kmem_free(bip->bli_logged);
  804. #endif /* XFS_TRANS_DEBUG */
  805. kmem_zone_free(xfs_buf_item_zone, bip);
  806. }
  807. /*
  808. * This is called when the buf log item is no longer needed. It should
  809. * free the buf log item associated with the given buffer and clear
  810. * the buffer's pointer to the buf log item. If there are no more
  811. * items in the list, clear the b_iodone field of the buffer (see
  812. * xfs_buf_attach_iodone() below).
  813. */
  814. void
  815. xfs_buf_item_relse(
  816. xfs_buf_t *bp)
  817. {
  818. xfs_buf_log_item_t *bip;
  819. trace_xfs_buf_item_relse(bp, _RET_IP_);
  820. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
  821. XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
  822. if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
  823. (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
  824. XFS_BUF_CLR_IODONE_FUNC(bp);
  825. }
  826. xfs_buf_rele(bp);
  827. xfs_buf_item_free(bip);
  828. }
  829. /*
  830. * Add the given log item with its callback to the list of callbacks
  831. * to be called when the buffer's I/O completes. If it is not set
  832. * already, set the buffer's b_iodone() routine to be
  833. * xfs_buf_iodone_callbacks() and link the log item into the list of
  834. * items rooted at b_fsprivate. Items are always added as the second
  835. * entry in the list if there is a first, because the buf item code
  836. * assumes that the buf log item is first.
  837. */
  838. void
  839. xfs_buf_attach_iodone(
  840. xfs_buf_t *bp,
  841. void (*cb)(xfs_buf_t *, xfs_log_item_t *),
  842. xfs_log_item_t *lip)
  843. {
  844. xfs_log_item_t *head_lip;
  845. ASSERT(XFS_BUF_ISBUSY(bp));
  846. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  847. lip->li_cb = cb;
  848. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  849. head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  850. lip->li_bio_list = head_lip->li_bio_list;
  851. head_lip->li_bio_list = lip;
  852. } else {
  853. XFS_BUF_SET_FSPRIVATE(bp, lip);
  854. }
  855. ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
  856. (XFS_BUF_IODONE_FUNC(bp) == NULL));
  857. XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
  858. }
  859. STATIC void
  860. xfs_buf_do_callbacks(
  861. xfs_buf_t *bp,
  862. xfs_log_item_t *lip)
  863. {
  864. xfs_log_item_t *nlip;
  865. while (lip != NULL) {
  866. nlip = lip->li_bio_list;
  867. ASSERT(lip->li_cb != NULL);
  868. /*
  869. * Clear the next pointer so we don't have any
  870. * confusion if the item is added to another buf.
  871. * Don't touch the log item after calling its
  872. * callback, because it could have freed itself.
  873. */
  874. lip->li_bio_list = NULL;
  875. lip->li_cb(bp, lip);
  876. lip = nlip;
  877. }
  878. }
  879. /*
  880. * This is the iodone() function for buffers which have had callbacks
  881. * attached to them by xfs_buf_attach_iodone(). It should remove each
  882. * log item from the buffer's list and call the callback of each in turn.
  883. * When done, the buffer's fsprivate field is set to NULL and the buffer
  884. * is unlocked with a call to iodone().
  885. */
  886. void
  887. xfs_buf_iodone_callbacks(
  888. xfs_buf_t *bp)
  889. {
  890. xfs_log_item_t *lip;
  891. static ulong lasttime;
  892. static xfs_buftarg_t *lasttarg;
  893. xfs_mount_t *mp;
  894. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  895. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  896. if (XFS_BUF_GETERROR(bp) != 0) {
  897. /*
  898. * If we've already decided to shutdown the filesystem
  899. * because of IO errors, there's no point in giving this
  900. * a retry.
  901. */
  902. mp = lip->li_mountp;
  903. if (XFS_FORCED_SHUTDOWN(mp)) {
  904. ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
  905. XFS_BUF_SUPER_STALE(bp);
  906. trace_xfs_buf_item_iodone(bp, _RET_IP_);
  907. xfs_buf_do_callbacks(bp, lip);
  908. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  909. XFS_BUF_CLR_IODONE_FUNC(bp);
  910. xfs_biodone(bp);
  911. return;
  912. }
  913. if ((XFS_BUF_TARGET(bp) != lasttarg) ||
  914. (time_after(jiffies, (lasttime + 5*HZ)))) {
  915. lasttime = jiffies;
  916. cmn_err(CE_ALERT, "Device %s, XFS metadata write error"
  917. " block 0x%llx in %s",
  918. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  919. (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
  920. }
  921. lasttarg = XFS_BUF_TARGET(bp);
  922. if (XFS_BUF_ISASYNC(bp)) {
  923. /*
  924. * If the write was asynchronous then noone will be
  925. * looking for the error. Clear the error state
  926. * and write the buffer out again delayed write.
  927. *
  928. * XXXsup This is OK, so long as we catch these
  929. * before we start the umount; we don't want these
  930. * DELWRI metadata bufs to be hanging around.
  931. */
  932. XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
  933. if (!(XFS_BUF_ISSTALE(bp))) {
  934. XFS_BUF_DELAYWRITE(bp);
  935. XFS_BUF_DONE(bp);
  936. XFS_BUF_SET_START(bp);
  937. }
  938. ASSERT(XFS_BUF_IODONE_FUNC(bp));
  939. trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
  940. xfs_buf_relse(bp);
  941. } else {
  942. /*
  943. * If the write of the buffer was not asynchronous,
  944. * then we want to make sure to return the error
  945. * to the caller of bwrite(). Because of this we
  946. * cannot clear the B_ERROR state at this point.
  947. * Instead we install a callback function that
  948. * will be called when the buffer is released, and
  949. * that routine will clear the error state and
  950. * set the buffer to be written out again after
  951. * some delay.
  952. */
  953. /* We actually overwrite the existing b-relse
  954. function at times, but we're gonna be shutting down
  955. anyway. */
  956. XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
  957. XFS_BUF_DONE(bp);
  958. XFS_BUF_FINISH_IOWAIT(bp);
  959. }
  960. return;
  961. }
  962. xfs_buf_do_callbacks(bp, lip);
  963. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  964. XFS_BUF_CLR_IODONE_FUNC(bp);
  965. xfs_biodone(bp);
  966. }
  967. /*
  968. * This is a callback routine attached to a buffer which gets an error
  969. * when being written out synchronously.
  970. */
  971. STATIC void
  972. xfs_buf_error_relse(
  973. xfs_buf_t *bp)
  974. {
  975. xfs_log_item_t *lip;
  976. xfs_mount_t *mp;
  977. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  978. mp = (xfs_mount_t *)lip->li_mountp;
  979. ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
  980. XFS_BUF_STALE(bp);
  981. XFS_BUF_DONE(bp);
  982. XFS_BUF_UNDELAYWRITE(bp);
  983. XFS_BUF_ERROR(bp,0);
  984. trace_xfs_buf_error_relse(bp, _RET_IP_);
  985. if (! XFS_FORCED_SHUTDOWN(mp))
  986. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  987. /*
  988. * We have to unpin the pinned buffers so do the
  989. * callbacks.
  990. */
  991. xfs_buf_do_callbacks(bp, lip);
  992. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  993. XFS_BUF_CLR_IODONE_FUNC(bp);
  994. XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
  995. xfs_buf_relse(bp);
  996. }
  997. /*
  998. * This is the iodone() function for buffers which have been
  999. * logged. It is called when they are eventually flushed out.
  1000. * It should remove the buf item from the AIL, and free the buf item.
  1001. * It is called by xfs_buf_iodone_callbacks() above which will take
  1002. * care of cleaning up the buffer itself.
  1003. */
  1004. /* ARGSUSED */
  1005. void
  1006. xfs_buf_iodone(
  1007. xfs_buf_t *bp,
  1008. xfs_buf_log_item_t *bip)
  1009. {
  1010. struct xfs_ail *ailp = bip->bli_item.li_ailp;
  1011. ASSERT(bip->bli_buf == bp);
  1012. xfs_buf_rele(bp);
  1013. /*
  1014. * If we are forcibly shutting down, this may well be
  1015. * off the AIL already. That's because we simulate the
  1016. * log-committed callbacks to unpin these buffers. Or we may never
  1017. * have put this item on AIL because of the transaction was
  1018. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  1019. *
  1020. * Either way, AIL is useless if we're forcing a shutdown.
  1021. */
  1022. spin_lock(&ailp->xa_lock);
  1023. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)bip);
  1024. xfs_buf_item_free(bip);
  1025. }