xfs_buf_item.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. XLOG_VEC_SET_TYPE(vecp, 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. XLOG_VEC_SET_TYPE(vecp, 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. XLOG_VEC_SET_TYPE(vecp, 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. XLOG_VEC_SET_TYPE(vecp, 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, pull the
  437. * buffer from the free list, mark it busy, and return 1.
  438. */
  439. STATIC uint
  440. xfs_buf_item_trylock(
  441. xfs_buf_log_item_t *bip)
  442. {
  443. xfs_buf_t *bp;
  444. bp = bip->bli_buf;
  445. if (XFS_BUF_ISPINNED(bp)) {
  446. return XFS_ITEM_PINNED;
  447. }
  448. if (!XFS_BUF_CPSEMA(bp)) {
  449. return XFS_ITEM_LOCKED;
  450. }
  451. /*
  452. * Remove the buffer from the free list. Only do this
  453. * if it's on the free list. Private buffers like the
  454. * superblock buffer are not.
  455. */
  456. XFS_BUF_HOLD(bp);
  457. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  458. trace_xfs_buf_item_trylock(bip);
  459. return XFS_ITEM_SUCCESS;
  460. }
  461. /*
  462. * Release the buffer associated with the buf log item.
  463. * If there is no dirty logged data associated with the
  464. * buffer recorded in the buf log item, then free the
  465. * buf log item and remove the reference to it in the
  466. * buffer.
  467. *
  468. * This call ignores the recursion count. It is only called
  469. * when the buffer should REALLY be unlocked, regardless
  470. * of the recursion count.
  471. *
  472. * If the XFS_BLI_HOLD flag is set in the buf log item, then
  473. * free the log item if necessary but do not unlock the buffer.
  474. * This is for support of xfs_trans_bhold(). Make sure the
  475. * XFS_BLI_HOLD field is cleared if we don't free the item.
  476. */
  477. STATIC void
  478. xfs_buf_item_unlock(
  479. xfs_buf_log_item_t *bip)
  480. {
  481. int aborted;
  482. xfs_buf_t *bp;
  483. uint hold;
  484. bp = bip->bli_buf;
  485. /*
  486. * Clear the buffer's association with this transaction.
  487. */
  488. XFS_BUF_SET_FSPRIVATE2(bp, NULL);
  489. /*
  490. * If this is a transaction abort, don't return early.
  491. * Instead, allow the brelse to happen.
  492. * Normally it would be done for stale (cancelled) buffers
  493. * at unpin time, but we'll never go through the pin/unpin
  494. * cycle if we abort inside commit.
  495. */
  496. aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
  497. /*
  498. * If the buf item is marked stale, then don't do anything.
  499. * We'll unlock the buffer and free the buf item when the
  500. * buffer is unpinned for the last time.
  501. */
  502. if (bip->bli_flags & XFS_BLI_STALE) {
  503. bip->bli_flags &= ~XFS_BLI_LOGGED;
  504. trace_xfs_buf_item_unlock_stale(bip);
  505. ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
  506. if (!aborted)
  507. return;
  508. }
  509. /*
  510. * Drop the transaction's reference to the log item if
  511. * it was not logged as part of the transaction. Otherwise
  512. * we'll drop the reference in xfs_buf_item_unpin() when
  513. * the transaction is really through with the buffer.
  514. */
  515. if (!(bip->bli_flags & XFS_BLI_LOGGED)) {
  516. atomic_dec(&bip->bli_refcount);
  517. } else {
  518. /*
  519. * Clear the logged flag since this is per
  520. * transaction state.
  521. */
  522. bip->bli_flags &= ~XFS_BLI_LOGGED;
  523. }
  524. /*
  525. * Before possibly freeing the buf item, determine if we should
  526. * release the buffer at the end of this routine.
  527. */
  528. hold = bip->bli_flags & XFS_BLI_HOLD;
  529. trace_xfs_buf_item_unlock(bip);
  530. /*
  531. * If the buf item isn't tracking any data, free it.
  532. * Otherwise, if XFS_BLI_HOLD is set clear it.
  533. */
  534. if (xfs_bitmap_empty(bip->bli_format.blf_data_map,
  535. bip->bli_format.blf_map_size)) {
  536. xfs_buf_item_relse(bp);
  537. } else if (hold) {
  538. bip->bli_flags &= ~XFS_BLI_HOLD;
  539. }
  540. /*
  541. * Release the buffer if XFS_BLI_HOLD was not set.
  542. */
  543. if (!hold) {
  544. xfs_buf_relse(bp);
  545. }
  546. }
  547. /*
  548. * This is called to find out where the oldest active copy of the
  549. * buf log item in the on disk log resides now that the last log
  550. * write of it completed at the given lsn.
  551. * We always re-log all the dirty data in a buffer, so usually the
  552. * latest copy in the on disk log is the only one that matters. For
  553. * those cases we simply return the given lsn.
  554. *
  555. * The one exception to this is for buffers full of newly allocated
  556. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  557. * flag set, indicating that only the di_next_unlinked fields from the
  558. * inodes in the buffers will be replayed during recovery. If the
  559. * original newly allocated inode images have not yet been flushed
  560. * when the buffer is so relogged, then we need to make sure that we
  561. * keep the old images in the 'active' portion of the log. We do this
  562. * by returning the original lsn of that transaction here rather than
  563. * the current one.
  564. */
  565. STATIC xfs_lsn_t
  566. xfs_buf_item_committed(
  567. xfs_buf_log_item_t *bip,
  568. xfs_lsn_t lsn)
  569. {
  570. trace_xfs_buf_item_committed(bip);
  571. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
  572. (bip->bli_item.li_lsn != 0)) {
  573. return bip->bli_item.li_lsn;
  574. }
  575. return (lsn);
  576. }
  577. /*
  578. * This is called to asynchronously write the buffer associated with this
  579. * buf log item out to disk. The buffer will already have been locked by
  580. * a successful call to xfs_buf_item_trylock(). If the buffer still has
  581. * B_DELWRI set, then get it going out to disk with a call to bawrite().
  582. * If not, then just release the buffer.
  583. */
  584. STATIC void
  585. xfs_buf_item_push(
  586. xfs_buf_log_item_t *bip)
  587. {
  588. xfs_buf_t *bp;
  589. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  590. trace_xfs_buf_item_push(bip);
  591. bp = bip->bli_buf;
  592. if (XFS_BUF_ISDELAYWRITE(bp)) {
  593. int error;
  594. error = xfs_bawrite(bip->bli_item.li_mountp, bp);
  595. if (error)
  596. xfs_fs_cmn_err(CE_WARN, bip->bli_item.li_mountp,
  597. "xfs_buf_item_push: pushbuf error %d on bip %p, bp %p",
  598. error, bip, bp);
  599. } else {
  600. xfs_buf_relse(bp);
  601. }
  602. }
  603. /* ARGSUSED */
  604. STATIC void
  605. xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
  606. {
  607. }
  608. /*
  609. * This is the ops vector shared by all buf log items.
  610. */
  611. static struct xfs_item_ops xfs_buf_item_ops = {
  612. .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
  613. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  614. xfs_buf_item_format,
  615. .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
  616. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin,
  617. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
  618. xfs_buf_item_unpin_remove,
  619. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
  620. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
  621. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  622. xfs_buf_item_committed,
  623. .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
  624. .iop_pushbuf = NULL,
  625. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  626. xfs_buf_item_committing
  627. };
  628. /*
  629. * Allocate a new buf log item to go with the given buffer.
  630. * Set the buffer's b_fsprivate field to point to the new
  631. * buf log item. If there are other item's attached to the
  632. * buffer (see xfs_buf_attach_iodone() below), then put the
  633. * buf log item at the front.
  634. */
  635. void
  636. xfs_buf_item_init(
  637. xfs_buf_t *bp,
  638. xfs_mount_t *mp)
  639. {
  640. xfs_log_item_t *lip;
  641. xfs_buf_log_item_t *bip;
  642. int chunks;
  643. int map_size;
  644. /*
  645. * Check to see if there is already a buf log item for
  646. * this buffer. If there is, it is guaranteed to be
  647. * the first. If we do already have one, there is
  648. * nothing to do here so return.
  649. */
  650. if (bp->b_mount != mp)
  651. bp->b_mount = mp;
  652. XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
  653. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  654. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  655. if (lip->li_type == XFS_LI_BUF) {
  656. return;
  657. }
  658. }
  659. /*
  660. * chunks is the number of XFS_BLI_CHUNK size pieces
  661. * the buffer can be divided into. Make sure not to
  662. * truncate any pieces. map_size is the size of the
  663. * bitmap needed to describe the chunks of the buffer.
  664. */
  665. chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT);
  666. map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
  667. bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
  668. KM_SLEEP);
  669. bip->bli_item.li_type = XFS_LI_BUF;
  670. bip->bli_item.li_ops = &xfs_buf_item_ops;
  671. bip->bli_item.li_mountp = mp;
  672. bip->bli_item.li_ailp = mp->m_ail;
  673. bip->bli_buf = bp;
  674. xfs_buf_hold(bp);
  675. bip->bli_format.blf_type = XFS_LI_BUF;
  676. bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
  677. bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
  678. bip->bli_format.blf_map_size = map_size;
  679. #ifdef XFS_TRANS_DEBUG
  680. /*
  681. * Allocate the arrays for tracking what needs to be logged
  682. * and what our callers request to be logged. bli_orig
  683. * holds a copy of the original, clean buffer for comparison
  684. * against, and bli_logged keeps a 1 bit flag per byte in
  685. * the buffer to indicate which bytes the callers have asked
  686. * to have logged.
  687. */
  688. bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
  689. memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
  690. bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
  691. #endif
  692. /*
  693. * Put the buf item into the list of items attached to the
  694. * buffer at the front.
  695. */
  696. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  697. bip->bli_item.li_bio_list =
  698. XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  699. }
  700. XFS_BUF_SET_FSPRIVATE(bp, bip);
  701. }
  702. /*
  703. * Mark bytes first through last inclusive as dirty in the buf
  704. * item's bitmap.
  705. */
  706. void
  707. xfs_buf_item_log(
  708. xfs_buf_log_item_t *bip,
  709. uint first,
  710. uint last)
  711. {
  712. uint first_bit;
  713. uint last_bit;
  714. uint bits_to_set;
  715. uint bits_set;
  716. uint word_num;
  717. uint *wordp;
  718. uint bit;
  719. uint end_bit;
  720. uint mask;
  721. /*
  722. * Mark the item as having some dirty data for
  723. * quick reference in xfs_buf_item_dirty.
  724. */
  725. bip->bli_flags |= XFS_BLI_DIRTY;
  726. /*
  727. * Convert byte offsets to bit numbers.
  728. */
  729. first_bit = first >> XFS_BLI_SHIFT;
  730. last_bit = last >> XFS_BLI_SHIFT;
  731. /*
  732. * Calculate the total number of bits to be set.
  733. */
  734. bits_to_set = last_bit - first_bit + 1;
  735. /*
  736. * Get a pointer to the first word in the bitmap
  737. * to set a bit in.
  738. */
  739. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  740. wordp = &(bip->bli_format.blf_data_map[word_num]);
  741. /*
  742. * Calculate the starting bit in the first word.
  743. */
  744. bit = first_bit & (uint)(NBWORD - 1);
  745. /*
  746. * First set any bits in the first word of our range.
  747. * If it starts at bit 0 of the word, it will be
  748. * set below rather than here. That is what the variable
  749. * bit tells us. The variable bits_set tracks the number
  750. * of bits that have been set so far. End_bit is the number
  751. * of the last bit to be set in this word plus one.
  752. */
  753. if (bit) {
  754. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  755. mask = ((1 << (end_bit - bit)) - 1) << bit;
  756. *wordp |= mask;
  757. wordp++;
  758. bits_set = end_bit - bit;
  759. } else {
  760. bits_set = 0;
  761. }
  762. /*
  763. * Now set bits a whole word at a time that are between
  764. * first_bit and last_bit.
  765. */
  766. while ((bits_to_set - bits_set) >= NBWORD) {
  767. *wordp |= 0xffffffff;
  768. bits_set += NBWORD;
  769. wordp++;
  770. }
  771. /*
  772. * Finally, set any bits left to be set in one last partial word.
  773. */
  774. end_bit = bits_to_set - bits_set;
  775. if (end_bit) {
  776. mask = (1 << end_bit) - 1;
  777. *wordp |= mask;
  778. }
  779. xfs_buf_item_log_debug(bip, first, last);
  780. }
  781. /*
  782. * Return 1 if the buffer has some data that has been logged (at any
  783. * point, not just the current transaction) and 0 if not.
  784. */
  785. uint
  786. xfs_buf_item_dirty(
  787. xfs_buf_log_item_t *bip)
  788. {
  789. return (bip->bli_flags & XFS_BLI_DIRTY);
  790. }
  791. STATIC void
  792. xfs_buf_item_free(
  793. xfs_buf_log_item_t *bip)
  794. {
  795. #ifdef XFS_TRANS_DEBUG
  796. kmem_free(bip->bli_orig);
  797. kmem_free(bip->bli_logged);
  798. #endif /* XFS_TRANS_DEBUG */
  799. kmem_zone_free(xfs_buf_item_zone, bip);
  800. }
  801. /*
  802. * This is called when the buf log item is no longer needed. It should
  803. * free the buf log item associated with the given buffer and clear
  804. * the buffer's pointer to the buf log item. If there are no more
  805. * items in the list, clear the b_iodone field of the buffer (see
  806. * xfs_buf_attach_iodone() below).
  807. */
  808. void
  809. xfs_buf_item_relse(
  810. xfs_buf_t *bp)
  811. {
  812. xfs_buf_log_item_t *bip;
  813. trace_xfs_buf_item_relse(bp, _RET_IP_);
  814. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
  815. XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
  816. if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
  817. (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
  818. XFS_BUF_CLR_IODONE_FUNC(bp);
  819. }
  820. xfs_buf_rele(bp);
  821. xfs_buf_item_free(bip);
  822. }
  823. /*
  824. * Add the given log item with its callback to the list of callbacks
  825. * to be called when the buffer's I/O completes. If it is not set
  826. * already, set the buffer's b_iodone() routine to be
  827. * xfs_buf_iodone_callbacks() and link the log item into the list of
  828. * items rooted at b_fsprivate. Items are always added as the second
  829. * entry in the list if there is a first, because the buf item code
  830. * assumes that the buf log item is first.
  831. */
  832. void
  833. xfs_buf_attach_iodone(
  834. xfs_buf_t *bp,
  835. void (*cb)(xfs_buf_t *, xfs_log_item_t *),
  836. xfs_log_item_t *lip)
  837. {
  838. xfs_log_item_t *head_lip;
  839. ASSERT(XFS_BUF_ISBUSY(bp));
  840. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  841. lip->li_cb = cb;
  842. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  843. head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  844. lip->li_bio_list = head_lip->li_bio_list;
  845. head_lip->li_bio_list = lip;
  846. } else {
  847. XFS_BUF_SET_FSPRIVATE(bp, lip);
  848. }
  849. ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
  850. (XFS_BUF_IODONE_FUNC(bp) == NULL));
  851. XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
  852. }
  853. STATIC void
  854. xfs_buf_do_callbacks(
  855. xfs_buf_t *bp,
  856. xfs_log_item_t *lip)
  857. {
  858. xfs_log_item_t *nlip;
  859. while (lip != NULL) {
  860. nlip = lip->li_bio_list;
  861. ASSERT(lip->li_cb != NULL);
  862. /*
  863. * Clear the next pointer so we don't have any
  864. * confusion if the item is added to another buf.
  865. * Don't touch the log item after calling its
  866. * callback, because it could have freed itself.
  867. */
  868. lip->li_bio_list = NULL;
  869. lip->li_cb(bp, lip);
  870. lip = nlip;
  871. }
  872. }
  873. /*
  874. * This is the iodone() function for buffers which have had callbacks
  875. * attached to them by xfs_buf_attach_iodone(). It should remove each
  876. * log item from the buffer's list and call the callback of each in turn.
  877. * When done, the buffer's fsprivate field is set to NULL and the buffer
  878. * is unlocked with a call to iodone().
  879. */
  880. void
  881. xfs_buf_iodone_callbacks(
  882. xfs_buf_t *bp)
  883. {
  884. xfs_log_item_t *lip;
  885. static ulong lasttime;
  886. static xfs_buftarg_t *lasttarg;
  887. xfs_mount_t *mp;
  888. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  889. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  890. if (XFS_BUF_GETERROR(bp) != 0) {
  891. /*
  892. * If we've already decided to shutdown the filesystem
  893. * because of IO errors, there's no point in giving this
  894. * a retry.
  895. */
  896. mp = lip->li_mountp;
  897. if (XFS_FORCED_SHUTDOWN(mp)) {
  898. ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
  899. XFS_BUF_SUPER_STALE(bp);
  900. trace_xfs_buf_item_iodone(bp, _RET_IP_);
  901. xfs_buf_do_callbacks(bp, lip);
  902. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  903. XFS_BUF_CLR_IODONE_FUNC(bp);
  904. xfs_biodone(bp);
  905. return;
  906. }
  907. if ((XFS_BUF_TARGET(bp) != lasttarg) ||
  908. (time_after(jiffies, (lasttime + 5*HZ)))) {
  909. lasttime = jiffies;
  910. cmn_err(CE_ALERT, "Device %s, XFS metadata write error"
  911. " block 0x%llx in %s",
  912. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  913. (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
  914. }
  915. lasttarg = XFS_BUF_TARGET(bp);
  916. if (XFS_BUF_ISASYNC(bp)) {
  917. /*
  918. * If the write was asynchronous then noone will be
  919. * looking for the error. Clear the error state
  920. * and write the buffer out again delayed write.
  921. *
  922. * XXXsup This is OK, so long as we catch these
  923. * before we start the umount; we don't want these
  924. * DELWRI metadata bufs to be hanging around.
  925. */
  926. XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
  927. if (!(XFS_BUF_ISSTALE(bp))) {
  928. XFS_BUF_DELAYWRITE(bp);
  929. XFS_BUF_DONE(bp);
  930. XFS_BUF_SET_START(bp);
  931. }
  932. ASSERT(XFS_BUF_IODONE_FUNC(bp));
  933. trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
  934. xfs_buf_relse(bp);
  935. } else {
  936. /*
  937. * If the write of the buffer was not asynchronous,
  938. * then we want to make sure to return the error
  939. * to the caller of bwrite(). Because of this we
  940. * cannot clear the B_ERROR state at this point.
  941. * Instead we install a callback function that
  942. * will be called when the buffer is released, and
  943. * that routine will clear the error state and
  944. * set the buffer to be written out again after
  945. * some delay.
  946. */
  947. /* We actually overwrite the existing b-relse
  948. function at times, but we're gonna be shutting down
  949. anyway. */
  950. XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
  951. XFS_BUF_DONE(bp);
  952. XFS_BUF_FINISH_IOWAIT(bp);
  953. }
  954. return;
  955. }
  956. xfs_buf_do_callbacks(bp, lip);
  957. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  958. XFS_BUF_CLR_IODONE_FUNC(bp);
  959. xfs_biodone(bp);
  960. }
  961. /*
  962. * This is a callback routine attached to a buffer which gets an error
  963. * when being written out synchronously.
  964. */
  965. STATIC void
  966. xfs_buf_error_relse(
  967. xfs_buf_t *bp)
  968. {
  969. xfs_log_item_t *lip;
  970. xfs_mount_t *mp;
  971. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  972. mp = (xfs_mount_t *)lip->li_mountp;
  973. ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
  974. XFS_BUF_STALE(bp);
  975. XFS_BUF_DONE(bp);
  976. XFS_BUF_UNDELAYWRITE(bp);
  977. XFS_BUF_ERROR(bp,0);
  978. trace_xfs_buf_error_relse(bp, _RET_IP_);
  979. if (! XFS_FORCED_SHUTDOWN(mp))
  980. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  981. /*
  982. * We have to unpin the pinned buffers so do the
  983. * callbacks.
  984. */
  985. xfs_buf_do_callbacks(bp, lip);
  986. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  987. XFS_BUF_CLR_IODONE_FUNC(bp);
  988. XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
  989. xfs_buf_relse(bp);
  990. }
  991. /*
  992. * This is the iodone() function for buffers which have been
  993. * logged. It is called when they are eventually flushed out.
  994. * It should remove the buf item from the AIL, and free the buf item.
  995. * It is called by xfs_buf_iodone_callbacks() above which will take
  996. * care of cleaning up the buffer itself.
  997. */
  998. /* ARGSUSED */
  999. void
  1000. xfs_buf_iodone(
  1001. xfs_buf_t *bp,
  1002. xfs_buf_log_item_t *bip)
  1003. {
  1004. struct xfs_ail *ailp = bip->bli_item.li_ailp;
  1005. ASSERT(bip->bli_buf == bp);
  1006. xfs_buf_rele(bp);
  1007. /*
  1008. * If we are forcibly shutting down, this may well be
  1009. * off the AIL already. That's because we simulate the
  1010. * log-committed callbacks to unpin these buffers. Or we may never
  1011. * have put this item on AIL because of the transaction was
  1012. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  1013. *
  1014. * Either way, AIL is useless if we're forcing a shutdown.
  1015. */
  1016. spin_lock(&ailp->xa_lock);
  1017. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)bip);
  1018. xfs_buf_item_free(bip);
  1019. }