xfs_buf_item.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_bit.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_trans_priv.h"
  29. #include "xfs_error.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_log.h"
  32. kmem_zone_t *xfs_buf_item_zone;
  33. static inline struct xfs_buf_log_item *BUF_ITEM(struct xfs_log_item *lip)
  34. {
  35. return container_of(lip, struct xfs_buf_log_item, bli_item);
  36. }
  37. STATIC void xfs_buf_do_callbacks(struct xfs_buf *bp);
  38. static inline int
  39. xfs_buf_log_format_size(
  40. struct xfs_buf_log_format *blfp)
  41. {
  42. return offsetof(struct xfs_buf_log_format, blf_data_map) +
  43. (blfp->blf_map_size * sizeof(blfp->blf_data_map[0]));
  44. }
  45. /*
  46. * This returns the number of log iovecs needed to log the
  47. * given buf log item.
  48. *
  49. * It calculates this as 1 iovec for the buf log format structure
  50. * and 1 for each stretch of non-contiguous chunks to be logged.
  51. * Contiguous chunks are logged in a single iovec.
  52. *
  53. * If the XFS_BLI_STALE flag has been set, then log nothing.
  54. */
  55. STATIC void
  56. xfs_buf_item_size_segment(
  57. struct xfs_buf_log_item *bip,
  58. struct xfs_buf_log_format *blfp,
  59. int *nvecs,
  60. int *nbytes)
  61. {
  62. struct xfs_buf *bp = bip->bli_buf;
  63. int next_bit;
  64. int last_bit;
  65. last_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
  66. if (last_bit == -1)
  67. return;
  68. /*
  69. * initial count for a dirty buffer is 2 vectors - the format structure
  70. * and the first dirty region.
  71. */
  72. *nvecs += 2;
  73. *nbytes += xfs_buf_log_format_size(blfp) + XFS_BLF_CHUNK;
  74. while (last_bit != -1) {
  75. /*
  76. * This takes the bit number to start looking from and
  77. * returns the next set bit from there. It returns -1
  78. * if there are no more bits set or the start bit is
  79. * beyond the end of the bitmap.
  80. */
  81. next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
  82. last_bit + 1);
  83. /*
  84. * If we run out of bits, leave the loop,
  85. * else if we find a new set of bits bump the number of vecs,
  86. * else keep scanning the current set of bits.
  87. */
  88. if (next_bit == -1) {
  89. break;
  90. } else if (next_bit != last_bit + 1) {
  91. last_bit = next_bit;
  92. (*nvecs)++;
  93. } else if (xfs_buf_offset(bp, next_bit * XFS_BLF_CHUNK) !=
  94. (xfs_buf_offset(bp, last_bit * XFS_BLF_CHUNK) +
  95. XFS_BLF_CHUNK)) {
  96. last_bit = next_bit;
  97. (*nvecs)++;
  98. } else {
  99. last_bit++;
  100. }
  101. *nbytes += XFS_BLF_CHUNK;
  102. }
  103. }
  104. /*
  105. * This returns the number of log iovecs needed to log the given buf log item.
  106. *
  107. * It calculates this as 1 iovec for the buf log format structure and 1 for each
  108. * stretch of non-contiguous chunks to be logged. Contiguous chunks are logged
  109. * in a single iovec.
  110. *
  111. * Discontiguous buffers need a format structure per region that that is being
  112. * logged. This makes the changes in the buffer appear to log recovery as though
  113. * they came from separate buffers, just like would occur if multiple buffers
  114. * were used instead of a single discontiguous buffer. This enables
  115. * discontiguous buffers to be in-memory constructs, completely transparent to
  116. * what ends up on disk.
  117. *
  118. * If the XFS_BLI_STALE flag has been set, then log nothing but the buf log
  119. * format structures.
  120. */
  121. STATIC void
  122. xfs_buf_item_size(
  123. struct xfs_log_item *lip,
  124. int *nvecs,
  125. int *nbytes)
  126. {
  127. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  128. int i;
  129. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  130. if (bip->bli_flags & XFS_BLI_STALE) {
  131. /*
  132. * The buffer is stale, so all we need to log
  133. * is the buf log format structure with the
  134. * cancel flag in it.
  135. */
  136. trace_xfs_buf_item_size_stale(bip);
  137. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  138. *nvecs += bip->bli_format_count;
  139. for (i = 0; i < bip->bli_format_count; i++) {
  140. *nbytes += xfs_buf_log_format_size(&bip->bli_formats[i]);
  141. }
  142. return;
  143. }
  144. ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
  145. if (bip->bli_flags & XFS_BLI_ORDERED) {
  146. /*
  147. * The buffer has been logged just to order it.
  148. * It is not being included in the transaction
  149. * commit, so no vectors are used at all.
  150. */
  151. trace_xfs_buf_item_size_ordered(bip);
  152. *nvecs = XFS_LOG_VEC_ORDERED;
  153. return;
  154. }
  155. /*
  156. * the vector count is based on the number of buffer vectors we have
  157. * dirty bits in. This will only be greater than one when we have a
  158. * compound buffer with more than one segment dirty. Hence for compound
  159. * buffers we need to track which segment the dirty bits correspond to,
  160. * and when we move from one segment to the next increment the vector
  161. * count for the extra buf log format structure that will need to be
  162. * written.
  163. */
  164. for (i = 0; i < bip->bli_format_count; i++) {
  165. xfs_buf_item_size_segment(bip, &bip->bli_formats[i],
  166. nvecs, nbytes);
  167. }
  168. trace_xfs_buf_item_size(bip);
  169. }
  170. static inline void
  171. xfs_buf_item_copy_iovec(
  172. struct xfs_log_vec *lv,
  173. struct xfs_log_iovec **vecp,
  174. struct xfs_buf *bp,
  175. uint offset,
  176. int first_bit,
  177. uint nbits)
  178. {
  179. offset += first_bit * XFS_BLF_CHUNK;
  180. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BCHUNK,
  181. xfs_buf_offset(bp, offset),
  182. nbits * XFS_BLF_CHUNK);
  183. }
  184. static inline bool
  185. xfs_buf_item_straddle(
  186. struct xfs_buf *bp,
  187. uint offset,
  188. int next_bit,
  189. int last_bit)
  190. {
  191. return xfs_buf_offset(bp, offset + (next_bit << XFS_BLF_SHIFT)) !=
  192. (xfs_buf_offset(bp, offset + (last_bit << XFS_BLF_SHIFT)) +
  193. XFS_BLF_CHUNK);
  194. }
  195. static void
  196. xfs_buf_item_format_segment(
  197. struct xfs_buf_log_item *bip,
  198. struct xfs_log_vec *lv,
  199. struct xfs_log_iovec **vecp,
  200. uint offset,
  201. struct xfs_buf_log_format *blfp)
  202. {
  203. struct xfs_buf *bp = bip->bli_buf;
  204. uint base_size;
  205. int first_bit;
  206. int last_bit;
  207. int next_bit;
  208. uint nbits;
  209. /* copy the flags across from the base format item */
  210. blfp->blf_flags = bip->__bli_format.blf_flags;
  211. /*
  212. * Base size is the actual size of the ondisk structure - it reflects
  213. * the actual size of the dirty bitmap rather than the size of the in
  214. * memory structure.
  215. */
  216. base_size = xfs_buf_log_format_size(blfp);
  217. first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
  218. if (!(bip->bli_flags & XFS_BLI_STALE) && first_bit == -1) {
  219. /*
  220. * If the map is not be dirty in the transaction, mark
  221. * the size as zero and do not advance the vector pointer.
  222. */
  223. return;
  224. }
  225. blfp = xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BFORMAT, blfp, base_size);
  226. blfp->blf_size = 1;
  227. if (bip->bli_flags & XFS_BLI_STALE) {
  228. /*
  229. * The buffer is stale, so all we need to log
  230. * is the buf log format structure with the
  231. * cancel flag in it.
  232. */
  233. trace_xfs_buf_item_format_stale(bip);
  234. ASSERT(blfp->blf_flags & XFS_BLF_CANCEL);
  235. return;
  236. }
  237. /*
  238. * Fill in an iovec for each set of contiguous chunks.
  239. */
  240. last_bit = first_bit;
  241. nbits = 1;
  242. for (;;) {
  243. /*
  244. * This takes the bit number to start looking from and
  245. * returns the next set bit from there. It returns -1
  246. * if there are no more bits set or the start bit is
  247. * beyond the end of the bitmap.
  248. */
  249. next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
  250. (uint)last_bit + 1);
  251. /*
  252. * If we run out of bits fill in the last iovec and get out of
  253. * the loop. Else if we start a new set of bits then fill in
  254. * the iovec for the series we were looking at and start
  255. * counting the bits in the new one. Else we're still in the
  256. * same set of bits so just keep counting and scanning.
  257. */
  258. if (next_bit == -1) {
  259. xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
  260. first_bit, nbits);
  261. blfp->blf_size++;
  262. break;
  263. } else if (next_bit != last_bit + 1 ||
  264. xfs_buf_item_straddle(bp, offset, next_bit, last_bit)) {
  265. xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
  266. first_bit, nbits);
  267. blfp->blf_size++;
  268. first_bit = next_bit;
  269. last_bit = next_bit;
  270. nbits = 1;
  271. } else {
  272. last_bit++;
  273. nbits++;
  274. }
  275. }
  276. }
  277. /*
  278. * This is called to fill in the vector of log iovecs for the
  279. * given log buf item. It fills the first entry with a buf log
  280. * format structure, and the rest point to contiguous chunks
  281. * within the buffer.
  282. */
  283. STATIC void
  284. xfs_buf_item_format(
  285. struct xfs_log_item *lip,
  286. struct xfs_log_vec *lv)
  287. {
  288. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  289. struct xfs_buf *bp = bip->bli_buf;
  290. struct xfs_log_iovec *vecp = NULL;
  291. uint offset = 0;
  292. int i;
  293. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  294. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  295. (bip->bli_flags & XFS_BLI_STALE));
  296. ASSERT((bip->bli_flags & XFS_BLI_STALE) ||
  297. (xfs_blft_from_flags(&bip->__bli_format) > XFS_BLFT_UNKNOWN_BUF
  298. && xfs_blft_from_flags(&bip->__bli_format) < XFS_BLFT_MAX_BUF));
  299. /*
  300. * If it is an inode buffer, transfer the in-memory state to the
  301. * format flags and clear the in-memory state.
  302. *
  303. * For buffer based inode allocation, we do not transfer
  304. * this state if the inode buffer allocation has not yet been committed
  305. * to the log as setting the XFS_BLI_INODE_BUF flag will prevent
  306. * correct replay of the inode allocation.
  307. *
  308. * For icreate item based inode allocation, the buffers aren't written
  309. * to the journal during allocation, and hence we should always tag the
  310. * buffer as an inode buffer so that the correct unlinked list replay
  311. * occurs during recovery.
  312. */
  313. if (bip->bli_flags & XFS_BLI_INODE_BUF) {
  314. if (xfs_sb_version_hascrc(&lip->li_mountp->m_sb) ||
  315. !((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
  316. xfs_log_item_in_current_chkpt(lip)))
  317. bip->__bli_format.blf_flags |= XFS_BLF_INODE_BUF;
  318. bip->bli_flags &= ~XFS_BLI_INODE_BUF;
  319. }
  320. if ((bip->bli_flags & (XFS_BLI_ORDERED|XFS_BLI_STALE)) ==
  321. XFS_BLI_ORDERED) {
  322. /*
  323. * The buffer has been logged just to order it. It is not being
  324. * included in the transaction commit, so don't format it.
  325. */
  326. trace_xfs_buf_item_format_ordered(bip);
  327. return;
  328. }
  329. for (i = 0; i < bip->bli_format_count; i++) {
  330. xfs_buf_item_format_segment(bip, lv, &vecp, offset,
  331. &bip->bli_formats[i]);
  332. offset += bp->b_maps[i].bm_len;
  333. }
  334. /*
  335. * Check to make sure everything is consistent.
  336. */
  337. trace_xfs_buf_item_format(bip);
  338. }
  339. /*
  340. * This is called to pin the buffer associated with the buf log item in memory
  341. * so it cannot be written out.
  342. *
  343. * We also always take a reference to the buffer log item here so that the bli
  344. * is held while the item is pinned in memory. This means that we can
  345. * unconditionally drop the reference count a transaction holds when the
  346. * transaction is completed.
  347. */
  348. STATIC void
  349. xfs_buf_item_pin(
  350. struct xfs_log_item *lip)
  351. {
  352. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  353. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  354. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  355. (bip->bli_flags & XFS_BLI_ORDERED) ||
  356. (bip->bli_flags & XFS_BLI_STALE));
  357. trace_xfs_buf_item_pin(bip);
  358. atomic_inc(&bip->bli_refcount);
  359. atomic_inc(&bip->bli_buf->b_pin_count);
  360. }
  361. /*
  362. * This is called to unpin the buffer associated with the buf log
  363. * item which was previously pinned with a call to xfs_buf_item_pin().
  364. *
  365. * Also drop the reference to the buf item for the current transaction.
  366. * If the XFS_BLI_STALE flag is set and we are the last reference,
  367. * then free up the buf log item and unlock the buffer.
  368. *
  369. * If the remove flag is set we are called from uncommit in the
  370. * forced-shutdown path. If that is true and the reference count on
  371. * the log item is going to drop to zero we need to free the item's
  372. * descriptor in the transaction.
  373. */
  374. STATIC void
  375. xfs_buf_item_unpin(
  376. struct xfs_log_item *lip,
  377. int remove)
  378. {
  379. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  380. xfs_buf_t *bp = bip->bli_buf;
  381. struct xfs_ail *ailp = lip->li_ailp;
  382. int stale = bip->bli_flags & XFS_BLI_STALE;
  383. int freed;
  384. ASSERT(bp->b_fspriv == bip);
  385. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  386. trace_xfs_buf_item_unpin(bip);
  387. freed = atomic_dec_and_test(&bip->bli_refcount);
  388. if (atomic_dec_and_test(&bp->b_pin_count))
  389. wake_up_all(&bp->b_waiters);
  390. if (freed && stale) {
  391. ASSERT(bip->bli_flags & XFS_BLI_STALE);
  392. ASSERT(xfs_buf_islocked(bp));
  393. ASSERT(XFS_BUF_ISSTALE(bp));
  394. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  395. trace_xfs_buf_item_unpin_stale(bip);
  396. if (remove) {
  397. /*
  398. * If we are in a transaction context, we have to
  399. * remove the log item from the transaction as we are
  400. * about to release our reference to the buffer. If we
  401. * don't, the unlock that occurs later in
  402. * xfs_trans_uncommit() will try to reference the
  403. * buffer which we no longer have a hold on.
  404. */
  405. if (lip->li_desc)
  406. xfs_trans_del_item(lip);
  407. /*
  408. * Since the transaction no longer refers to the buffer,
  409. * the buffer should no longer refer to the transaction.
  410. */
  411. bp->b_transp = NULL;
  412. }
  413. /*
  414. * If we get called here because of an IO error, we may
  415. * or may not have the item on the AIL. xfs_trans_ail_delete()
  416. * will take care of that situation.
  417. * xfs_trans_ail_delete() drops the AIL lock.
  418. */
  419. if (bip->bli_flags & XFS_BLI_STALE_INODE) {
  420. xfs_buf_do_callbacks(bp);
  421. bp->b_fspriv = NULL;
  422. bp->b_iodone = NULL;
  423. } else {
  424. spin_lock(&ailp->xa_lock);
  425. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_LOG_IO_ERROR);
  426. xfs_buf_item_relse(bp);
  427. ASSERT(bp->b_fspriv == NULL);
  428. }
  429. xfs_buf_relse(bp);
  430. } else if (freed && remove) {
  431. /*
  432. * There are currently two references to the buffer - the active
  433. * LRU reference and the buf log item. What we are about to do
  434. * here - simulate a failed IO completion - requires 3
  435. * references.
  436. *
  437. * The LRU reference is removed by the xfs_buf_stale() call. The
  438. * buf item reference is removed by the xfs_buf_iodone()
  439. * callback that is run by xfs_buf_do_callbacks() during ioend
  440. * processing (via the bp->b_iodone callback), and then finally
  441. * the ioend processing will drop the IO reference if the buffer
  442. * is marked XBF_ASYNC.
  443. *
  444. * Hence we need to take an additional reference here so that IO
  445. * completion processing doesn't free the buffer prematurely.
  446. */
  447. xfs_buf_lock(bp);
  448. xfs_buf_hold(bp);
  449. bp->b_flags |= XBF_ASYNC;
  450. xfs_buf_ioerror(bp, -EIO);
  451. XFS_BUF_UNDONE(bp);
  452. xfs_buf_stale(bp);
  453. xfs_buf_ioend(bp);
  454. }
  455. }
  456. /*
  457. * Buffer IO error rate limiting. Limit it to no more than 10 messages per 30
  458. * seconds so as to not spam logs too much on repeated detection of the same
  459. * buffer being bad..
  460. */
  461. static DEFINE_RATELIMIT_STATE(xfs_buf_write_fail_rl_state, 30 * HZ, 10);
  462. STATIC uint
  463. xfs_buf_item_push(
  464. struct xfs_log_item *lip,
  465. struct list_head *buffer_list)
  466. {
  467. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  468. struct xfs_buf *bp = bip->bli_buf;
  469. uint rval = XFS_ITEM_SUCCESS;
  470. if (xfs_buf_ispinned(bp))
  471. return XFS_ITEM_PINNED;
  472. if (!xfs_buf_trylock(bp)) {
  473. /*
  474. * If we have just raced with a buffer being pinned and it has
  475. * been marked stale, we could end up stalling until someone else
  476. * issues a log force to unpin the stale buffer. Check for the
  477. * race condition here so xfsaild recognizes the buffer is pinned
  478. * and queues a log force to move it along.
  479. */
  480. if (xfs_buf_ispinned(bp))
  481. return XFS_ITEM_PINNED;
  482. return XFS_ITEM_LOCKED;
  483. }
  484. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  485. trace_xfs_buf_item_push(bip);
  486. /* has a previous flush failed due to IO errors? */
  487. if ((bp->b_flags & XBF_WRITE_FAIL) &&
  488. ___ratelimit(&xfs_buf_write_fail_rl_state, "XFS: Failing async write")) {
  489. xfs_warn(bp->b_target->bt_mount,
  490. "Failing async write on buffer block 0x%llx. Retrying async write.",
  491. (long long)bp->b_bn);
  492. }
  493. if (!xfs_buf_delwri_queue(bp, buffer_list))
  494. rval = XFS_ITEM_FLUSHING;
  495. xfs_buf_unlock(bp);
  496. return rval;
  497. }
  498. /*
  499. * Release the buffer associated with the buf log item. If there is no dirty
  500. * logged data associated with the buffer recorded in the buf log item, then
  501. * free the buf log item and remove the reference to it in the buffer.
  502. *
  503. * This call ignores the recursion count. It is only called when the buffer
  504. * should REALLY be unlocked, regardless of the recursion count.
  505. *
  506. * We unconditionally drop the transaction's reference to the log item. If the
  507. * item was logged, then another reference was taken when it was pinned, so we
  508. * can safely drop the transaction reference now. This also allows us to avoid
  509. * potential races with the unpin code freeing the bli by not referencing the
  510. * bli after we've dropped the reference count.
  511. *
  512. * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
  513. * if necessary but do not unlock the buffer. This is for support of
  514. * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
  515. * free the item.
  516. */
  517. STATIC void
  518. xfs_buf_item_unlock(
  519. struct xfs_log_item *lip)
  520. {
  521. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  522. struct xfs_buf *bp = bip->bli_buf;
  523. bool clean;
  524. bool aborted;
  525. int flags;
  526. /* Clear the buffer's association with this transaction. */
  527. bp->b_transp = NULL;
  528. /*
  529. * If this is a transaction abort, don't return early. Instead, allow
  530. * the brelse to happen. Normally it would be done for stale
  531. * (cancelled) buffers at unpin time, but we'll never go through the
  532. * pin/unpin cycle if we abort inside commit.
  533. */
  534. aborted = (lip->li_flags & XFS_LI_ABORTED) ? true : false;
  535. /*
  536. * Before possibly freeing the buf item, copy the per-transaction state
  537. * so we can reference it safely later after clearing it from the
  538. * buffer log item.
  539. */
  540. flags = bip->bli_flags;
  541. bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD | XFS_BLI_ORDERED);
  542. /*
  543. * If the buf item is marked stale, then don't do anything. We'll
  544. * unlock the buffer and free the buf item when the buffer is unpinned
  545. * for the last time.
  546. */
  547. if (flags & XFS_BLI_STALE) {
  548. trace_xfs_buf_item_unlock_stale(bip);
  549. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  550. if (!aborted) {
  551. atomic_dec(&bip->bli_refcount);
  552. return;
  553. }
  554. }
  555. trace_xfs_buf_item_unlock(bip);
  556. /*
  557. * If the buf item isn't tracking any data, free it, otherwise drop the
  558. * reference we hold to it. If we are aborting the transaction, this may
  559. * be the only reference to the buf item, so we free it anyway
  560. * regardless of whether it is dirty or not. A dirty abort implies a
  561. * shutdown, anyway.
  562. *
  563. * Ordered buffers are dirty but may have no recorded changes, so ensure
  564. * we only release clean items here.
  565. */
  566. clean = (flags & XFS_BLI_DIRTY) ? false : true;
  567. if (clean) {
  568. int i;
  569. for (i = 0; i < bip->bli_format_count; i++) {
  570. if (!xfs_bitmap_empty(bip->bli_formats[i].blf_data_map,
  571. bip->bli_formats[i].blf_map_size)) {
  572. clean = false;
  573. break;
  574. }
  575. }
  576. }
  577. /*
  578. * Clean buffers, by definition, cannot be in the AIL. However, aborted
  579. * buffers may be dirty and hence in the AIL. Therefore if we are
  580. * aborting a buffer and we've just taken the last refernce away, we
  581. * have to check if it is in the AIL before freeing it. We need to free
  582. * it in this case, because an aborted transaction has already shut the
  583. * filesystem down and this is the last chance we will have to do so.
  584. */
  585. if (atomic_dec_and_test(&bip->bli_refcount)) {
  586. if (clean)
  587. xfs_buf_item_relse(bp);
  588. else if (aborted) {
  589. ASSERT(XFS_FORCED_SHUTDOWN(lip->li_mountp));
  590. xfs_trans_ail_remove(lip, SHUTDOWN_LOG_IO_ERROR);
  591. xfs_buf_item_relse(bp);
  592. }
  593. }
  594. if (!(flags & XFS_BLI_HOLD))
  595. xfs_buf_relse(bp);
  596. }
  597. /*
  598. * This is called to find out where the oldest active copy of the
  599. * buf log item in the on disk log resides now that the last log
  600. * write of it completed at the given lsn.
  601. * We always re-log all the dirty data in a buffer, so usually the
  602. * latest copy in the on disk log is the only one that matters. For
  603. * those cases we simply return the given lsn.
  604. *
  605. * The one exception to this is for buffers full of newly allocated
  606. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  607. * flag set, indicating that only the di_next_unlinked fields from the
  608. * inodes in the buffers will be replayed during recovery. If the
  609. * original newly allocated inode images have not yet been flushed
  610. * when the buffer is so relogged, then we need to make sure that we
  611. * keep the old images in the 'active' portion of the log. We do this
  612. * by returning the original lsn of that transaction here rather than
  613. * the current one.
  614. */
  615. STATIC xfs_lsn_t
  616. xfs_buf_item_committed(
  617. struct xfs_log_item *lip,
  618. xfs_lsn_t lsn)
  619. {
  620. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  621. trace_xfs_buf_item_committed(bip);
  622. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
  623. return lip->li_lsn;
  624. return lsn;
  625. }
  626. STATIC void
  627. xfs_buf_item_committing(
  628. struct xfs_log_item *lip,
  629. xfs_lsn_t commit_lsn)
  630. {
  631. }
  632. /*
  633. * This is the ops vector shared by all buf log items.
  634. */
  635. static const struct xfs_item_ops xfs_buf_item_ops = {
  636. .iop_size = xfs_buf_item_size,
  637. .iop_format = xfs_buf_item_format,
  638. .iop_pin = xfs_buf_item_pin,
  639. .iop_unpin = xfs_buf_item_unpin,
  640. .iop_unlock = xfs_buf_item_unlock,
  641. .iop_committed = xfs_buf_item_committed,
  642. .iop_push = xfs_buf_item_push,
  643. .iop_committing = xfs_buf_item_committing
  644. };
  645. STATIC int
  646. xfs_buf_item_get_format(
  647. struct xfs_buf_log_item *bip,
  648. int count)
  649. {
  650. ASSERT(bip->bli_formats == NULL);
  651. bip->bli_format_count = count;
  652. if (count == 1) {
  653. bip->bli_formats = &bip->__bli_format;
  654. return 0;
  655. }
  656. bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
  657. KM_SLEEP);
  658. if (!bip->bli_formats)
  659. return -ENOMEM;
  660. return 0;
  661. }
  662. STATIC void
  663. xfs_buf_item_free_format(
  664. struct xfs_buf_log_item *bip)
  665. {
  666. if (bip->bli_formats != &bip->__bli_format) {
  667. kmem_free(bip->bli_formats);
  668. bip->bli_formats = NULL;
  669. }
  670. }
  671. /*
  672. * Allocate a new buf log item to go with the given buffer.
  673. * Set the buffer's b_fsprivate field to point to the new
  674. * buf log item. If there are other item's attached to the
  675. * buffer (see xfs_buf_attach_iodone() below), then put the
  676. * buf log item at the front.
  677. */
  678. int
  679. xfs_buf_item_init(
  680. struct xfs_buf *bp,
  681. struct xfs_mount *mp)
  682. {
  683. struct xfs_log_item *lip = bp->b_fspriv;
  684. struct xfs_buf_log_item *bip;
  685. int chunks;
  686. int map_size;
  687. int error;
  688. int i;
  689. /*
  690. * Check to see if there is already a buf log item for
  691. * this buffer. If there is, it is guaranteed to be
  692. * the first. If we do already have one, there is
  693. * nothing to do here so return.
  694. */
  695. ASSERT(bp->b_target->bt_mount == mp);
  696. if (lip != NULL && lip->li_type == XFS_LI_BUF)
  697. return 0;
  698. bip = kmem_zone_zalloc(xfs_buf_item_zone, KM_SLEEP);
  699. xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
  700. bip->bli_buf = bp;
  701. /*
  702. * chunks is the number of XFS_BLF_CHUNK size pieces the buffer
  703. * can be divided into. Make sure not to truncate any pieces.
  704. * map_size is the size of the bitmap needed to describe the
  705. * chunks of the buffer.
  706. *
  707. * Discontiguous buffer support follows the layout of the underlying
  708. * buffer. This makes the implementation as simple as possible.
  709. */
  710. error = xfs_buf_item_get_format(bip, bp->b_map_count);
  711. ASSERT(error == 0);
  712. if (error) { /* to stop gcc throwing set-but-unused warnings */
  713. kmem_zone_free(xfs_buf_item_zone, bip);
  714. return error;
  715. }
  716. for (i = 0; i < bip->bli_format_count; i++) {
  717. chunks = DIV_ROUND_UP(BBTOB(bp->b_maps[i].bm_len),
  718. XFS_BLF_CHUNK);
  719. map_size = DIV_ROUND_UP(chunks, NBWORD);
  720. bip->bli_formats[i].blf_type = XFS_LI_BUF;
  721. bip->bli_formats[i].blf_blkno = bp->b_maps[i].bm_bn;
  722. bip->bli_formats[i].blf_len = bp->b_maps[i].bm_len;
  723. bip->bli_formats[i].blf_map_size = map_size;
  724. }
  725. /*
  726. * Put the buf item into the list of items attached to the
  727. * buffer at the front.
  728. */
  729. if (bp->b_fspriv)
  730. bip->bli_item.li_bio_list = bp->b_fspriv;
  731. bp->b_fspriv = bip;
  732. xfs_buf_hold(bp);
  733. return 0;
  734. }
  735. /*
  736. * Mark bytes first through last inclusive as dirty in the buf
  737. * item's bitmap.
  738. */
  739. static void
  740. xfs_buf_item_log_segment(
  741. uint first,
  742. uint last,
  743. uint *map)
  744. {
  745. uint first_bit;
  746. uint last_bit;
  747. uint bits_to_set;
  748. uint bits_set;
  749. uint word_num;
  750. uint *wordp;
  751. uint bit;
  752. uint end_bit;
  753. uint mask;
  754. /*
  755. * Convert byte offsets to bit numbers.
  756. */
  757. first_bit = first >> XFS_BLF_SHIFT;
  758. last_bit = last >> XFS_BLF_SHIFT;
  759. /*
  760. * Calculate the total number of bits to be set.
  761. */
  762. bits_to_set = last_bit - first_bit + 1;
  763. /*
  764. * Get a pointer to the first word in the bitmap
  765. * to set a bit in.
  766. */
  767. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  768. wordp = &map[word_num];
  769. /*
  770. * Calculate the starting bit in the first word.
  771. */
  772. bit = first_bit & (uint)(NBWORD - 1);
  773. /*
  774. * First set any bits in the first word of our range.
  775. * If it starts at bit 0 of the word, it will be
  776. * set below rather than here. That is what the variable
  777. * bit tells us. The variable bits_set tracks the number
  778. * of bits that have been set so far. End_bit is the number
  779. * of the last bit to be set in this word plus one.
  780. */
  781. if (bit) {
  782. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  783. mask = ((1 << (end_bit - bit)) - 1) << bit;
  784. *wordp |= mask;
  785. wordp++;
  786. bits_set = end_bit - bit;
  787. } else {
  788. bits_set = 0;
  789. }
  790. /*
  791. * Now set bits a whole word at a time that are between
  792. * first_bit and last_bit.
  793. */
  794. while ((bits_to_set - bits_set) >= NBWORD) {
  795. *wordp |= 0xffffffff;
  796. bits_set += NBWORD;
  797. wordp++;
  798. }
  799. /*
  800. * Finally, set any bits left to be set in one last partial word.
  801. */
  802. end_bit = bits_to_set - bits_set;
  803. if (end_bit) {
  804. mask = (1 << end_bit) - 1;
  805. *wordp |= mask;
  806. }
  807. }
  808. /*
  809. * Mark bytes first through last inclusive as dirty in the buf
  810. * item's bitmap.
  811. */
  812. void
  813. xfs_buf_item_log(
  814. xfs_buf_log_item_t *bip,
  815. uint first,
  816. uint last)
  817. {
  818. int i;
  819. uint start;
  820. uint end;
  821. struct xfs_buf *bp = bip->bli_buf;
  822. /*
  823. * walk each buffer segment and mark them dirty appropriately.
  824. */
  825. start = 0;
  826. for (i = 0; i < bip->bli_format_count; i++) {
  827. if (start > last)
  828. break;
  829. end = start + BBTOB(bp->b_maps[i].bm_len);
  830. if (first > end) {
  831. start += BBTOB(bp->b_maps[i].bm_len);
  832. continue;
  833. }
  834. if (first < start)
  835. first = start;
  836. if (end > last)
  837. end = last;
  838. xfs_buf_item_log_segment(first, end,
  839. &bip->bli_formats[i].blf_data_map[0]);
  840. start += bp->b_maps[i].bm_len;
  841. }
  842. }
  843. /*
  844. * Return 1 if the buffer has been logged or ordered in a transaction (at any
  845. * point, not just the current transaction) and 0 if not.
  846. */
  847. uint
  848. xfs_buf_item_dirty(
  849. xfs_buf_log_item_t *bip)
  850. {
  851. return (bip->bli_flags & XFS_BLI_DIRTY);
  852. }
  853. STATIC void
  854. xfs_buf_item_free(
  855. xfs_buf_log_item_t *bip)
  856. {
  857. xfs_buf_item_free_format(bip);
  858. kmem_zone_free(xfs_buf_item_zone, bip);
  859. }
  860. /*
  861. * This is called when the buf log item is no longer needed. It should
  862. * free the buf log item associated with the given buffer and clear
  863. * the buffer's pointer to the buf log item. If there are no more
  864. * items in the list, clear the b_iodone field of the buffer (see
  865. * xfs_buf_attach_iodone() below).
  866. */
  867. void
  868. xfs_buf_item_relse(
  869. xfs_buf_t *bp)
  870. {
  871. xfs_buf_log_item_t *bip = bp->b_fspriv;
  872. trace_xfs_buf_item_relse(bp, _RET_IP_);
  873. ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
  874. bp->b_fspriv = bip->bli_item.li_bio_list;
  875. if (bp->b_fspriv == NULL)
  876. bp->b_iodone = NULL;
  877. xfs_buf_rele(bp);
  878. xfs_buf_item_free(bip);
  879. }
  880. /*
  881. * Add the given log item with its callback to the list of callbacks
  882. * to be called when the buffer's I/O completes. If it is not set
  883. * already, set the buffer's b_iodone() routine to be
  884. * xfs_buf_iodone_callbacks() and link the log item into the list of
  885. * items rooted at b_fsprivate. Items are always added as the second
  886. * entry in the list if there is a first, because the buf item code
  887. * assumes that the buf log item is first.
  888. */
  889. void
  890. xfs_buf_attach_iodone(
  891. xfs_buf_t *bp,
  892. void (*cb)(xfs_buf_t *, xfs_log_item_t *),
  893. xfs_log_item_t *lip)
  894. {
  895. xfs_log_item_t *head_lip;
  896. ASSERT(xfs_buf_islocked(bp));
  897. lip->li_cb = cb;
  898. head_lip = bp->b_fspriv;
  899. if (head_lip) {
  900. lip->li_bio_list = head_lip->li_bio_list;
  901. head_lip->li_bio_list = lip;
  902. } else {
  903. bp->b_fspriv = lip;
  904. }
  905. ASSERT(bp->b_iodone == NULL ||
  906. bp->b_iodone == xfs_buf_iodone_callbacks);
  907. bp->b_iodone = xfs_buf_iodone_callbacks;
  908. }
  909. /*
  910. * We can have many callbacks on a buffer. Running the callbacks individually
  911. * can cause a lot of contention on the AIL lock, so we allow for a single
  912. * callback to be able to scan the remaining lip->li_bio_list for other items
  913. * of the same type and callback to be processed in the first call.
  914. *
  915. * As a result, the loop walking the callback list below will also modify the
  916. * list. it removes the first item from the list and then runs the callback.
  917. * The loop then restarts from the new head of the list. This allows the
  918. * callback to scan and modify the list attached to the buffer and we don't
  919. * have to care about maintaining a next item pointer.
  920. */
  921. STATIC void
  922. xfs_buf_do_callbacks(
  923. struct xfs_buf *bp)
  924. {
  925. struct xfs_log_item *lip;
  926. while ((lip = bp->b_fspriv) != NULL) {
  927. bp->b_fspriv = lip->li_bio_list;
  928. ASSERT(lip->li_cb != NULL);
  929. /*
  930. * Clear the next pointer so we don't have any
  931. * confusion if the item is added to another buf.
  932. * Don't touch the log item after calling its
  933. * callback, because it could have freed itself.
  934. */
  935. lip->li_bio_list = NULL;
  936. lip->li_cb(bp, lip);
  937. }
  938. }
  939. /*
  940. * This is the iodone() function for buffers which have had callbacks
  941. * attached to them by xfs_buf_attach_iodone(). It should remove each
  942. * log item from the buffer's list and call the callback of each in turn.
  943. * When done, the buffer's fsprivate field is set to NULL and the buffer
  944. * is unlocked with a call to iodone().
  945. */
  946. void
  947. xfs_buf_iodone_callbacks(
  948. struct xfs_buf *bp)
  949. {
  950. struct xfs_log_item *lip = bp->b_fspriv;
  951. struct xfs_mount *mp = lip->li_mountp;
  952. static ulong lasttime;
  953. static xfs_buftarg_t *lasttarg;
  954. if (likely(!bp->b_error))
  955. goto do_callbacks;
  956. /*
  957. * If we've already decided to shutdown the filesystem because of
  958. * I/O errors, there's no point in giving this a retry.
  959. */
  960. if (XFS_FORCED_SHUTDOWN(mp)) {
  961. xfs_buf_stale(bp);
  962. XFS_BUF_DONE(bp);
  963. trace_xfs_buf_item_iodone(bp, _RET_IP_);
  964. goto do_callbacks;
  965. }
  966. if (bp->b_target != lasttarg ||
  967. time_after(jiffies, (lasttime + 5*HZ))) {
  968. lasttime = jiffies;
  969. xfs_buf_ioerror_alert(bp, __func__);
  970. }
  971. lasttarg = bp->b_target;
  972. /*
  973. * If the write was asynchronous then no one will be looking for the
  974. * error. Clear the error state and write the buffer out again.
  975. *
  976. * XXX: This helps against transient write errors, but we need to find
  977. * a way to shut the filesystem down if the writes keep failing.
  978. *
  979. * In practice we'll shut the filesystem down soon as non-transient
  980. * errors tend to affect the whole device and a failing log write
  981. * will make us give up. But we really ought to do better here.
  982. */
  983. if (XFS_BUF_ISASYNC(bp)) {
  984. ASSERT(bp->b_iodone != NULL);
  985. trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
  986. xfs_buf_ioerror(bp, 0); /* errno of 0 unsets the flag */
  987. if (!(bp->b_flags & (XBF_STALE|XBF_WRITE_FAIL))) {
  988. bp->b_flags |= XBF_WRITE | XBF_ASYNC |
  989. XBF_DONE | XBF_WRITE_FAIL;
  990. xfs_buf_submit(bp);
  991. } else {
  992. xfs_buf_relse(bp);
  993. }
  994. return;
  995. }
  996. /*
  997. * If the write of the buffer was synchronous, we want to make
  998. * sure to return the error to the caller of xfs_bwrite().
  999. */
  1000. xfs_buf_stale(bp);
  1001. XFS_BUF_DONE(bp);
  1002. trace_xfs_buf_error_relse(bp, _RET_IP_);
  1003. do_callbacks:
  1004. xfs_buf_do_callbacks(bp);
  1005. bp->b_fspriv = NULL;
  1006. bp->b_iodone = NULL;
  1007. xfs_buf_ioend(bp);
  1008. }
  1009. /*
  1010. * This is the iodone() function for buffers which have been
  1011. * logged. It is called when they are eventually flushed out.
  1012. * It should remove the buf item from the AIL, and free the buf item.
  1013. * It is called by xfs_buf_iodone_callbacks() above which will take
  1014. * care of cleaning up the buffer itself.
  1015. */
  1016. void
  1017. xfs_buf_iodone(
  1018. struct xfs_buf *bp,
  1019. struct xfs_log_item *lip)
  1020. {
  1021. struct xfs_ail *ailp = lip->li_ailp;
  1022. ASSERT(BUF_ITEM(lip)->bli_buf == bp);
  1023. xfs_buf_rele(bp);
  1024. /*
  1025. * If we are forcibly shutting down, this may well be
  1026. * off the AIL already. That's because we simulate the
  1027. * log-committed callbacks to unpin these buffers. Or we may never
  1028. * have put this item on AIL because of the transaction was
  1029. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  1030. *
  1031. * Either way, AIL is useless if we're forcing a shutdown.
  1032. */
  1033. spin_lock(&ailp->xa_lock);
  1034. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  1035. xfs_buf_item_free(BUF_ITEM(lip));
  1036. }