xfs_buf_item.c 36 KB

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