xfs_buf_item.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 += BBTOB(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(bp->b_flags & XBF_STALE);
  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. bp->b_flags &= ~XBF_DONE;
  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 in the AIL regardless of dirty state. An aborted
  580. * transaction that invalidates a buffer already in the AIL may have
  581. * marked it stale and cleared the dirty state, for example.
  582. *
  583. * Therefore if we are aborting a buffer and we've just taken the last
  584. * reference away, we have to check if it is in the AIL before freeing
  585. * it. We need to free it in this case, because an aborted transaction
  586. * has already shut the filesystem down and this is the last chance we
  587. * will have to do so.
  588. */
  589. if (atomic_dec_and_test(&bip->bli_refcount)) {
  590. if (aborted) {
  591. ASSERT(XFS_FORCED_SHUTDOWN(lip->li_mountp));
  592. xfs_trans_ail_remove(lip, SHUTDOWN_LOG_IO_ERROR);
  593. xfs_buf_item_relse(bp);
  594. } else if (clean)
  595. xfs_buf_item_relse(bp);
  596. }
  597. if (!(flags & XFS_BLI_HOLD))
  598. xfs_buf_relse(bp);
  599. }
  600. /*
  601. * This is called to find out where the oldest active copy of the
  602. * buf log item in the on disk log resides now that the last log
  603. * write of it completed at the given lsn.
  604. * We always re-log all the dirty data in a buffer, so usually the
  605. * latest copy in the on disk log is the only one that matters. For
  606. * those cases we simply return the given lsn.
  607. *
  608. * The one exception to this is for buffers full of newly allocated
  609. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  610. * flag set, indicating that only the di_next_unlinked fields from the
  611. * inodes in the buffers will be replayed during recovery. If the
  612. * original newly allocated inode images have not yet been flushed
  613. * when the buffer is so relogged, then we need to make sure that we
  614. * keep the old images in the 'active' portion of the log. We do this
  615. * by returning the original lsn of that transaction here rather than
  616. * the current one.
  617. */
  618. STATIC xfs_lsn_t
  619. xfs_buf_item_committed(
  620. struct xfs_log_item *lip,
  621. xfs_lsn_t lsn)
  622. {
  623. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  624. trace_xfs_buf_item_committed(bip);
  625. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
  626. return lip->li_lsn;
  627. return lsn;
  628. }
  629. STATIC void
  630. xfs_buf_item_committing(
  631. struct xfs_log_item *lip,
  632. xfs_lsn_t commit_lsn)
  633. {
  634. }
  635. /*
  636. * This is the ops vector shared by all buf log items.
  637. */
  638. static const struct xfs_item_ops xfs_buf_item_ops = {
  639. .iop_size = xfs_buf_item_size,
  640. .iop_format = xfs_buf_item_format,
  641. .iop_pin = xfs_buf_item_pin,
  642. .iop_unpin = xfs_buf_item_unpin,
  643. .iop_unlock = xfs_buf_item_unlock,
  644. .iop_committed = xfs_buf_item_committed,
  645. .iop_push = xfs_buf_item_push,
  646. .iop_committing = xfs_buf_item_committing
  647. };
  648. STATIC int
  649. xfs_buf_item_get_format(
  650. struct xfs_buf_log_item *bip,
  651. int count)
  652. {
  653. ASSERT(bip->bli_formats == NULL);
  654. bip->bli_format_count = count;
  655. if (count == 1) {
  656. bip->bli_formats = &bip->__bli_format;
  657. return 0;
  658. }
  659. bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
  660. KM_SLEEP);
  661. if (!bip->bli_formats)
  662. return -ENOMEM;
  663. return 0;
  664. }
  665. STATIC void
  666. xfs_buf_item_free_format(
  667. struct xfs_buf_log_item *bip)
  668. {
  669. if (bip->bli_formats != &bip->__bli_format) {
  670. kmem_free(bip->bli_formats);
  671. bip->bli_formats = NULL;
  672. }
  673. }
  674. /*
  675. * Allocate a new buf log item to go with the given buffer.
  676. * Set the buffer's b_fsprivate field to point to the new
  677. * buf log item. If there are other item's attached to the
  678. * buffer (see xfs_buf_attach_iodone() below), then put the
  679. * buf log item at the front.
  680. */
  681. int
  682. xfs_buf_item_init(
  683. struct xfs_buf *bp,
  684. struct xfs_mount *mp)
  685. {
  686. struct xfs_log_item *lip = bp->b_fspriv;
  687. struct xfs_buf_log_item *bip;
  688. int chunks;
  689. int map_size;
  690. int error;
  691. int i;
  692. /*
  693. * Check to see if there is already a buf log item for
  694. * this buffer. If there is, it is guaranteed to be
  695. * the first. If we do already have one, there is
  696. * nothing to do here so return.
  697. */
  698. ASSERT(bp->b_target->bt_mount == mp);
  699. if (lip != NULL && lip->li_type == XFS_LI_BUF)
  700. return 0;
  701. bip = kmem_zone_zalloc(xfs_buf_item_zone, KM_SLEEP);
  702. xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
  703. bip->bli_buf = bp;
  704. /*
  705. * chunks is the number of XFS_BLF_CHUNK size pieces the buffer
  706. * can be divided into. Make sure not to truncate any pieces.
  707. * map_size is the size of the bitmap needed to describe the
  708. * chunks of the buffer.
  709. *
  710. * Discontiguous buffer support follows the layout of the underlying
  711. * buffer. This makes the implementation as simple as possible.
  712. */
  713. error = xfs_buf_item_get_format(bip, bp->b_map_count);
  714. ASSERT(error == 0);
  715. if (error) { /* to stop gcc throwing set-but-unused warnings */
  716. kmem_zone_free(xfs_buf_item_zone, bip);
  717. return error;
  718. }
  719. for (i = 0; i < bip->bli_format_count; i++) {
  720. chunks = DIV_ROUND_UP(BBTOB(bp->b_maps[i].bm_len),
  721. XFS_BLF_CHUNK);
  722. map_size = DIV_ROUND_UP(chunks, NBWORD);
  723. bip->bli_formats[i].blf_type = XFS_LI_BUF;
  724. bip->bli_formats[i].blf_blkno = bp->b_maps[i].bm_bn;
  725. bip->bli_formats[i].blf_len = bp->b_maps[i].bm_len;
  726. bip->bli_formats[i].blf_map_size = map_size;
  727. }
  728. /*
  729. * Put the buf item into the list of items attached to the
  730. * buffer at the front.
  731. */
  732. if (bp->b_fspriv)
  733. bip->bli_item.li_bio_list = bp->b_fspriv;
  734. bp->b_fspriv = bip;
  735. xfs_buf_hold(bp);
  736. return 0;
  737. }
  738. /*
  739. * Mark bytes first through last inclusive as dirty in the buf
  740. * item's bitmap.
  741. */
  742. static void
  743. xfs_buf_item_log_segment(
  744. uint first,
  745. uint last,
  746. uint *map)
  747. {
  748. uint first_bit;
  749. uint last_bit;
  750. uint bits_to_set;
  751. uint bits_set;
  752. uint word_num;
  753. uint *wordp;
  754. uint bit;
  755. uint end_bit;
  756. uint mask;
  757. /*
  758. * Convert byte offsets to bit numbers.
  759. */
  760. first_bit = first >> XFS_BLF_SHIFT;
  761. last_bit = last >> XFS_BLF_SHIFT;
  762. /*
  763. * Calculate the total number of bits to be set.
  764. */
  765. bits_to_set = last_bit - first_bit + 1;
  766. /*
  767. * Get a pointer to the first word in the bitmap
  768. * to set a bit in.
  769. */
  770. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  771. wordp = &map[word_num];
  772. /*
  773. * Calculate the starting bit in the first word.
  774. */
  775. bit = first_bit & (uint)(NBWORD - 1);
  776. /*
  777. * First set any bits in the first word of our range.
  778. * If it starts at bit 0 of the word, it will be
  779. * set below rather than here. That is what the variable
  780. * bit tells us. The variable bits_set tracks the number
  781. * of bits that have been set so far. End_bit is the number
  782. * of the last bit to be set in this word plus one.
  783. */
  784. if (bit) {
  785. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  786. mask = ((1U << (end_bit - bit)) - 1) << bit;
  787. *wordp |= mask;
  788. wordp++;
  789. bits_set = end_bit - bit;
  790. } else {
  791. bits_set = 0;
  792. }
  793. /*
  794. * Now set bits a whole word at a time that are between
  795. * first_bit and last_bit.
  796. */
  797. while ((bits_to_set - bits_set) >= NBWORD) {
  798. *wordp |= 0xffffffff;
  799. bits_set += NBWORD;
  800. wordp++;
  801. }
  802. /*
  803. * Finally, set any bits left to be set in one last partial word.
  804. */
  805. end_bit = bits_to_set - bits_set;
  806. if (end_bit) {
  807. mask = (1U << end_bit) - 1;
  808. *wordp |= mask;
  809. }
  810. }
  811. /*
  812. * Mark bytes first through last inclusive as dirty in the buf
  813. * item's bitmap.
  814. */
  815. void
  816. xfs_buf_item_log(
  817. xfs_buf_log_item_t *bip,
  818. uint first,
  819. uint last)
  820. {
  821. int i;
  822. uint start;
  823. uint end;
  824. struct xfs_buf *bp = bip->bli_buf;
  825. /*
  826. * walk each buffer segment and mark them dirty appropriately.
  827. */
  828. start = 0;
  829. for (i = 0; i < bip->bli_format_count; i++) {
  830. if (start > last)
  831. break;
  832. end = start + BBTOB(bp->b_maps[i].bm_len) - 1;
  833. /* skip to the map that includes the first byte to log */
  834. if (first > end) {
  835. start += BBTOB(bp->b_maps[i].bm_len);
  836. continue;
  837. }
  838. /*
  839. * Trim the range to this segment and mark it in the bitmap.
  840. * Note that we must convert buffer offsets to segment relative
  841. * offsets (e.g., the first byte of each segment is byte 0 of
  842. * that segment).
  843. */
  844. if (first < start)
  845. first = start;
  846. if (end > last)
  847. end = last;
  848. xfs_buf_item_log_segment(first - start, end - start,
  849. &bip->bli_formats[i].blf_data_map[0]);
  850. start += BBTOB(bp->b_maps[i].bm_len);
  851. }
  852. }
  853. /*
  854. * Return 1 if the buffer has been logged or ordered in a transaction (at any
  855. * point, not just the current transaction) and 0 if not.
  856. */
  857. uint
  858. xfs_buf_item_dirty(
  859. xfs_buf_log_item_t *bip)
  860. {
  861. return (bip->bli_flags & XFS_BLI_DIRTY);
  862. }
  863. STATIC void
  864. xfs_buf_item_free(
  865. xfs_buf_log_item_t *bip)
  866. {
  867. xfs_buf_item_free_format(bip);
  868. kmem_free(bip->bli_item.li_lv_shadow);
  869. kmem_zone_free(xfs_buf_item_zone, bip);
  870. }
  871. /*
  872. * This is called when the buf log item is no longer needed. It should
  873. * free the buf log item associated with the given buffer and clear
  874. * the buffer's pointer to the buf log item. If there are no more
  875. * items in the list, clear the b_iodone field of the buffer (see
  876. * xfs_buf_attach_iodone() below).
  877. */
  878. void
  879. xfs_buf_item_relse(
  880. xfs_buf_t *bp)
  881. {
  882. xfs_buf_log_item_t *bip = bp->b_fspriv;
  883. trace_xfs_buf_item_relse(bp, _RET_IP_);
  884. ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
  885. bp->b_fspriv = bip->bli_item.li_bio_list;
  886. if (bp->b_fspriv == NULL)
  887. bp->b_iodone = NULL;
  888. xfs_buf_rele(bp);
  889. xfs_buf_item_free(bip);
  890. }
  891. /*
  892. * Add the given log item with its callback to the list of callbacks
  893. * to be called when the buffer's I/O completes. If it is not set
  894. * already, set the buffer's b_iodone() routine to be
  895. * xfs_buf_iodone_callbacks() and link the log item into the list of
  896. * items rooted at b_fsprivate. Items are always added as the second
  897. * entry in the list if there is a first, because the buf item code
  898. * assumes that the buf log item is first.
  899. */
  900. void
  901. xfs_buf_attach_iodone(
  902. xfs_buf_t *bp,
  903. void (*cb)(xfs_buf_t *, xfs_log_item_t *),
  904. xfs_log_item_t *lip)
  905. {
  906. xfs_log_item_t *head_lip;
  907. ASSERT(xfs_buf_islocked(bp));
  908. lip->li_cb = cb;
  909. head_lip = bp->b_fspriv;
  910. if (head_lip) {
  911. lip->li_bio_list = head_lip->li_bio_list;
  912. head_lip->li_bio_list = lip;
  913. } else {
  914. bp->b_fspriv = lip;
  915. }
  916. ASSERT(bp->b_iodone == NULL ||
  917. bp->b_iodone == xfs_buf_iodone_callbacks);
  918. bp->b_iodone = xfs_buf_iodone_callbacks;
  919. }
  920. /*
  921. * We can have many callbacks on a buffer. Running the callbacks individually
  922. * can cause a lot of contention on the AIL lock, so we allow for a single
  923. * callback to be able to scan the remaining lip->li_bio_list for other items
  924. * of the same type and callback to be processed in the first call.
  925. *
  926. * As a result, the loop walking the callback list below will also modify the
  927. * list. it removes the first item from the list and then runs the callback.
  928. * The loop then restarts from the new head of the list. This allows the
  929. * callback to scan and modify the list attached to the buffer and we don't
  930. * have to care about maintaining a next item pointer.
  931. */
  932. STATIC void
  933. xfs_buf_do_callbacks(
  934. struct xfs_buf *bp)
  935. {
  936. struct xfs_log_item *lip;
  937. while ((lip = bp->b_fspriv) != NULL) {
  938. bp->b_fspriv = lip->li_bio_list;
  939. ASSERT(lip->li_cb != NULL);
  940. /*
  941. * Clear the next pointer so we don't have any
  942. * confusion if the item is added to another buf.
  943. * Don't touch the log item after calling its
  944. * callback, because it could have freed itself.
  945. */
  946. lip->li_bio_list = NULL;
  947. lip->li_cb(bp, lip);
  948. }
  949. }
  950. static bool
  951. xfs_buf_iodone_callback_error(
  952. struct xfs_buf *bp)
  953. {
  954. struct xfs_log_item *lip = bp->b_fspriv;
  955. struct xfs_mount *mp = lip->li_mountp;
  956. static ulong lasttime;
  957. static xfs_buftarg_t *lasttarg;
  958. struct xfs_error_cfg *cfg;
  959. /*
  960. * If we've already decided to shutdown the filesystem because of
  961. * I/O errors, there's no point in giving this a retry.
  962. */
  963. if (XFS_FORCED_SHUTDOWN(mp))
  964. goto out_stale;
  965. if (bp->b_target != lasttarg ||
  966. time_after(jiffies, (lasttime + 5*HZ))) {
  967. lasttime = jiffies;
  968. xfs_buf_ioerror_alert(bp, __func__);
  969. }
  970. lasttarg = bp->b_target;
  971. /* synchronous writes will have callers process the error */
  972. if (!(bp->b_flags & XBF_ASYNC))
  973. goto out_stale;
  974. trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
  975. ASSERT(bp->b_iodone != NULL);
  976. cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
  977. /*
  978. * If the write was asynchronous then no one will be looking for the
  979. * error. If this is the first failure of this type, clear the error
  980. * state and write the buffer out again. This means we always retry an
  981. * async write failure at least once, but we also need to set the buffer
  982. * up to behave correctly now for repeated failures.
  983. */
  984. if (!(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL)) ||
  985. bp->b_last_error != bp->b_error) {
  986. bp->b_flags |= (XBF_WRITE | XBF_DONE | XBF_WRITE_FAIL);
  987. bp->b_last_error = bp->b_error;
  988. if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
  989. !bp->b_first_retry_time)
  990. bp->b_first_retry_time = jiffies;
  991. xfs_buf_ioerror(bp, 0);
  992. xfs_buf_submit(bp);
  993. return true;
  994. }
  995. /*
  996. * Repeated failure on an async write. Take action according to the
  997. * error configuration we have been set up to use.
  998. */
  999. if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
  1000. ++bp->b_retries > cfg->max_retries)
  1001. goto permanent_error;
  1002. if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
  1003. time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time))
  1004. goto permanent_error;
  1005. /* At unmount we may treat errors differently */
  1006. if ((mp->m_flags & XFS_MOUNT_UNMOUNTING) && mp->m_fail_unmount)
  1007. goto permanent_error;
  1008. /* still a transient error, higher layers will retry */
  1009. xfs_buf_ioerror(bp, 0);
  1010. xfs_buf_relse(bp);
  1011. return true;
  1012. /*
  1013. * Permanent error - we need to trigger a shutdown if we haven't already
  1014. * to indicate that inconsistency will result from this action.
  1015. */
  1016. permanent_error:
  1017. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1018. out_stale:
  1019. xfs_buf_stale(bp);
  1020. bp->b_flags |= XBF_DONE;
  1021. trace_xfs_buf_error_relse(bp, _RET_IP_);
  1022. return false;
  1023. }
  1024. /*
  1025. * This is the iodone() function for buffers which have had callbacks attached
  1026. * to them by xfs_buf_attach_iodone(). We need to iterate the items on the
  1027. * callback list, mark the buffer as having no more callbacks and then push the
  1028. * buffer through IO completion processing.
  1029. */
  1030. void
  1031. xfs_buf_iodone_callbacks(
  1032. struct xfs_buf *bp)
  1033. {
  1034. /*
  1035. * If there is an error, process it. Some errors require us
  1036. * to run callbacks after failure processing is done so we
  1037. * detect that and take appropriate action.
  1038. */
  1039. if (bp->b_error && xfs_buf_iodone_callback_error(bp))
  1040. return;
  1041. /*
  1042. * Successful IO or permanent error. Either way, we can clear the
  1043. * retry state here in preparation for the next error that may occur.
  1044. */
  1045. bp->b_last_error = 0;
  1046. bp->b_retries = 0;
  1047. bp->b_first_retry_time = 0;
  1048. xfs_buf_do_callbacks(bp);
  1049. bp->b_fspriv = NULL;
  1050. bp->b_iodone = NULL;
  1051. xfs_buf_ioend(bp);
  1052. }
  1053. /*
  1054. * This is the iodone() function for buffers which have been
  1055. * logged. It is called when they are eventually flushed out.
  1056. * It should remove the buf item from the AIL, and free the buf item.
  1057. * It is called by xfs_buf_iodone_callbacks() above which will take
  1058. * care of cleaning up the buffer itself.
  1059. */
  1060. void
  1061. xfs_buf_iodone(
  1062. struct xfs_buf *bp,
  1063. struct xfs_log_item *lip)
  1064. {
  1065. struct xfs_ail *ailp = lip->li_ailp;
  1066. ASSERT(BUF_ITEM(lip)->bli_buf == bp);
  1067. xfs_buf_rele(bp);
  1068. /*
  1069. * If we are forcibly shutting down, this may well be
  1070. * off the AIL already. That's because we simulate the
  1071. * log-committed callbacks to unpin these buffers. Or we may never
  1072. * have put this item on AIL because of the transaction was
  1073. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  1074. *
  1075. * Either way, AIL is useless if we're forcing a shutdown.
  1076. */
  1077. spin_lock(&ailp->xa_lock);
  1078. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  1079. xfs_buf_item_free(BUF_ITEM(lip));
  1080. }