xfs_buf_item.c 32 KB

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