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