xfs_buf_item.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_trans_priv.h"
  29. #include "xfs_error.h"
  30. #include "xfs_trace.h"
  31. kmem_zone_t *xfs_buf_item_zone;
  32. static inline struct xfs_buf_log_item *BUF_ITEM(struct xfs_log_item *lip)
  33. {
  34. return container_of(lip, struct xfs_buf_log_item, bli_item);
  35. }
  36. #ifdef XFS_TRANS_DEBUG
  37. /*
  38. * This function uses an alternate strategy for tracking the bytes
  39. * that the user requests to be logged. This can then be used
  40. * in conjunction with the bli_orig array in the buf log item to
  41. * catch bugs in our callers' code.
  42. *
  43. * We also double check the bits set in xfs_buf_item_log using a
  44. * simple algorithm to check that every byte is accounted for.
  45. */
  46. STATIC void
  47. xfs_buf_item_log_debug(
  48. xfs_buf_log_item_t *bip,
  49. uint first,
  50. uint last)
  51. {
  52. uint x;
  53. uint byte;
  54. uint nbytes;
  55. uint chunk_num;
  56. uint word_num;
  57. uint bit_num;
  58. uint bit_set;
  59. uint *wordp;
  60. ASSERT(bip->bli_logged != NULL);
  61. byte = first;
  62. nbytes = last - first + 1;
  63. bfset(bip->bli_logged, first, nbytes);
  64. for (x = 0; x < nbytes; x++) {
  65. chunk_num = byte >> XFS_BLF_SHIFT;
  66. word_num = chunk_num >> BIT_TO_WORD_SHIFT;
  67. bit_num = chunk_num & (NBWORD - 1);
  68. wordp = &(bip->bli_format.blf_data_map[word_num]);
  69. bit_set = *wordp & (1 << bit_num);
  70. ASSERT(bit_set);
  71. byte++;
  72. }
  73. }
  74. /*
  75. * This function is called when we flush something into a buffer without
  76. * logging it. This happens for things like inodes which are logged
  77. * separately from the buffer.
  78. */
  79. void
  80. xfs_buf_item_flush_log_debug(
  81. xfs_buf_t *bp,
  82. uint first,
  83. uint last)
  84. {
  85. xfs_buf_log_item_t *bip = bp->b_fspriv;
  86. uint nbytes;
  87. if (bip == NULL || (bip->bli_item.li_type != XFS_LI_BUF))
  88. return;
  89. ASSERT(bip->bli_logged != NULL);
  90. nbytes = last - first + 1;
  91. bfset(bip->bli_logged, first, nbytes);
  92. }
  93. /*
  94. * This function is called to verify that our callers have logged
  95. * all the bytes that they changed.
  96. *
  97. * It does this by comparing the original copy of the buffer stored in
  98. * the buf log item's bli_orig array to the current copy of the buffer
  99. * and ensuring that all bytes which mismatch are set in the bli_logged
  100. * array of the buf log item.
  101. */
  102. STATIC void
  103. xfs_buf_item_log_check(
  104. xfs_buf_log_item_t *bip)
  105. {
  106. char *orig;
  107. char *buffer;
  108. int x;
  109. xfs_buf_t *bp;
  110. ASSERT(bip->bli_orig != NULL);
  111. ASSERT(bip->bli_logged != NULL);
  112. bp = bip->bli_buf;
  113. ASSERT(bp->b_length > 0);
  114. ASSERT(bp->b_addr != NULL);
  115. orig = bip->bli_orig;
  116. buffer = bp->b_addr;
  117. for (x = 0; x < BBTOB(bp->b_length); x++) {
  118. if (orig[x] != buffer[x] && !btst(bip->bli_logged, x)) {
  119. xfs_emerg(bp->b_mount,
  120. "%s: bip %x buffer %x orig %x index %d",
  121. __func__, bip, bp, orig, x);
  122. ASSERT(0);
  123. }
  124. }
  125. }
  126. #else
  127. #define xfs_buf_item_log_debug(x,y,z)
  128. #define xfs_buf_item_log_check(x)
  129. #endif
  130. STATIC void xfs_buf_do_callbacks(struct xfs_buf *bp);
  131. /*
  132. * This returns the number of log iovecs needed to log the
  133. * given buf log item.
  134. *
  135. * It calculates this as 1 iovec for the buf log format structure
  136. * and 1 for each stretch of non-contiguous chunks to be logged.
  137. * Contiguous chunks are logged in a single iovec.
  138. *
  139. * If the XFS_BLI_STALE flag has been set, then log nothing.
  140. */
  141. STATIC uint
  142. xfs_buf_item_size(
  143. struct xfs_log_item *lip)
  144. {
  145. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  146. struct xfs_buf *bp = bip->bli_buf;
  147. uint nvecs;
  148. int next_bit;
  149. int last_bit;
  150. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  151. if (bip->bli_flags & XFS_BLI_STALE) {
  152. /*
  153. * The buffer is stale, so all we need to log
  154. * is the buf log format structure with the
  155. * cancel flag in it.
  156. */
  157. trace_xfs_buf_item_size_stale(bip);
  158. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  159. return 1;
  160. }
  161. ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
  162. nvecs = 1;
  163. last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  164. bip->bli_format.blf_map_size, 0);
  165. ASSERT(last_bit != -1);
  166. nvecs++;
  167. while (last_bit != -1) {
  168. /*
  169. * This takes the bit number to start looking from and
  170. * returns the next set bit from there. It returns -1
  171. * if there are no more bits set or the start bit is
  172. * beyond the end of the bitmap.
  173. */
  174. next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  175. bip->bli_format.blf_map_size,
  176. last_bit + 1);
  177. /*
  178. * If we run out of bits, leave the loop,
  179. * else if we find a new set of bits bump the number of vecs,
  180. * else keep scanning the current set of bits.
  181. */
  182. if (next_bit == -1) {
  183. last_bit = -1;
  184. } else if (next_bit != last_bit + 1) {
  185. last_bit = next_bit;
  186. nvecs++;
  187. } else if (xfs_buf_offset(bp, next_bit * XFS_BLF_CHUNK) !=
  188. (xfs_buf_offset(bp, last_bit * XFS_BLF_CHUNK) +
  189. XFS_BLF_CHUNK)) {
  190. last_bit = next_bit;
  191. nvecs++;
  192. } else {
  193. last_bit++;
  194. }
  195. }
  196. trace_xfs_buf_item_size(bip);
  197. return nvecs;
  198. }
  199. /*
  200. * This is called to fill in the vector of log iovecs for the
  201. * given log buf item. It fills the first entry with a buf log
  202. * format structure, and the rest point to contiguous chunks
  203. * within the buffer.
  204. */
  205. STATIC void
  206. xfs_buf_item_format(
  207. struct xfs_log_item *lip,
  208. struct xfs_log_iovec *vecp)
  209. {
  210. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  211. struct xfs_buf *bp = bip->bli_buf;
  212. uint base_size;
  213. uint nvecs;
  214. int first_bit;
  215. int last_bit;
  216. int next_bit;
  217. uint nbits;
  218. uint buffer_offset;
  219. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  220. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  221. (bip->bli_flags & XFS_BLI_STALE));
  222. /*
  223. * The size of the base structure is the size of the
  224. * declared structure plus the space for the extra words
  225. * of the bitmap. We subtract one from the map size, because
  226. * the first element of the bitmap is accounted for in the
  227. * size of the base structure.
  228. */
  229. base_size =
  230. (uint)(sizeof(xfs_buf_log_format_t) +
  231. ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
  232. vecp->i_addr = &bip->bli_format;
  233. vecp->i_len = base_size;
  234. vecp->i_type = XLOG_REG_TYPE_BFORMAT;
  235. vecp++;
  236. nvecs = 1;
  237. /*
  238. * If it is an inode buffer, transfer the in-memory state to the
  239. * format flags and clear the in-memory state. We do not transfer
  240. * this state if the inode buffer allocation has not yet been committed
  241. * to the log as setting the XFS_BLI_INODE_BUF flag will prevent
  242. * correct replay of the inode allocation.
  243. */
  244. if (bip->bli_flags & XFS_BLI_INODE_BUF) {
  245. if (!((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
  246. xfs_log_item_in_current_chkpt(lip)))
  247. bip->bli_format.blf_flags |= XFS_BLF_INODE_BUF;
  248. bip->bli_flags &= ~XFS_BLI_INODE_BUF;
  249. }
  250. if (bip->bli_flags & XFS_BLI_STALE) {
  251. /*
  252. * The buffer is stale, so all we need to log
  253. * is the buf log format structure with the
  254. * cancel flag in it.
  255. */
  256. trace_xfs_buf_item_format_stale(bip);
  257. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  258. bip->bli_format.blf_size = nvecs;
  259. return;
  260. }
  261. /*
  262. * Fill in an iovec for each set of contiguous chunks.
  263. */
  264. first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  265. bip->bli_format.blf_map_size, 0);
  266. ASSERT(first_bit != -1);
  267. last_bit = first_bit;
  268. nbits = 1;
  269. for (;;) {
  270. /*
  271. * This takes the bit number to start looking from and
  272. * returns the next set bit from there. It returns -1
  273. * if there are no more bits set or the start bit is
  274. * beyond the end of the bitmap.
  275. */
  276. next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  277. bip->bli_format.blf_map_size,
  278. (uint)last_bit + 1);
  279. /*
  280. * If we run out of bits fill in the last iovec and get
  281. * out of the loop.
  282. * Else if we start a new set of bits then fill in the
  283. * iovec for the series we were looking at and start
  284. * counting the bits in the new one.
  285. * Else we're still in the same set of bits so just
  286. * keep counting and scanning.
  287. */
  288. if (next_bit == -1) {
  289. buffer_offset = first_bit * XFS_BLF_CHUNK;
  290. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  291. vecp->i_len = nbits * XFS_BLF_CHUNK;
  292. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  293. nvecs++;
  294. break;
  295. } else if (next_bit != last_bit + 1) {
  296. buffer_offset = first_bit * XFS_BLF_CHUNK;
  297. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  298. vecp->i_len = nbits * XFS_BLF_CHUNK;
  299. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  300. nvecs++;
  301. vecp++;
  302. first_bit = next_bit;
  303. last_bit = next_bit;
  304. nbits = 1;
  305. } else if (xfs_buf_offset(bp, next_bit << XFS_BLF_SHIFT) !=
  306. (xfs_buf_offset(bp, last_bit << XFS_BLF_SHIFT) +
  307. XFS_BLF_CHUNK)) {
  308. buffer_offset = first_bit * XFS_BLF_CHUNK;
  309. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  310. vecp->i_len = nbits * XFS_BLF_CHUNK;
  311. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  312. /* You would think we need to bump the nvecs here too, but we do not
  313. * this number is used by recovery, and it gets confused by the boundary
  314. * split here
  315. * nvecs++;
  316. */
  317. vecp++;
  318. first_bit = next_bit;
  319. last_bit = next_bit;
  320. nbits = 1;
  321. } else {
  322. last_bit++;
  323. nbits++;
  324. }
  325. }
  326. bip->bli_format.blf_size = nvecs;
  327. /*
  328. * Check to make sure everything is consistent.
  329. */
  330. trace_xfs_buf_item_format(bip);
  331. xfs_buf_item_log_check(bip);
  332. }
  333. /*
  334. * This is called to pin the buffer associated with the buf log item in memory
  335. * so it cannot be written out.
  336. *
  337. * We also always take a reference to the buffer log item here so that the bli
  338. * is held while the item is pinned in memory. This means that we can
  339. * unconditionally drop the reference count a transaction holds when the
  340. * transaction is completed.
  341. */
  342. STATIC void
  343. xfs_buf_item_pin(
  344. struct xfs_log_item *lip)
  345. {
  346. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  347. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  348. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  349. (bip->bli_flags & XFS_BLI_STALE));
  350. trace_xfs_buf_item_pin(bip);
  351. atomic_inc(&bip->bli_refcount);
  352. atomic_inc(&bip->bli_buf->b_pin_count);
  353. }
  354. /*
  355. * This is called to unpin the buffer associated with the buf log
  356. * item which was previously pinned with a call to xfs_buf_item_pin().
  357. *
  358. * Also drop the reference to the buf item for the current transaction.
  359. * If the XFS_BLI_STALE flag is set and we are the last reference,
  360. * then free up the buf log item and unlock the buffer.
  361. *
  362. * If the remove flag is set we are called from uncommit in the
  363. * forced-shutdown path. If that is true and the reference count on
  364. * the log item is going to drop to zero we need to free the item's
  365. * descriptor in the transaction.
  366. */
  367. STATIC void
  368. xfs_buf_item_unpin(
  369. struct xfs_log_item *lip,
  370. int remove)
  371. {
  372. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  373. xfs_buf_t *bp = bip->bli_buf;
  374. struct xfs_ail *ailp = lip->li_ailp;
  375. int stale = bip->bli_flags & XFS_BLI_STALE;
  376. int freed;
  377. ASSERT(bp->b_fspriv == bip);
  378. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  379. trace_xfs_buf_item_unpin(bip);
  380. freed = atomic_dec_and_test(&bip->bli_refcount);
  381. if (atomic_dec_and_test(&bp->b_pin_count))
  382. wake_up_all(&bp->b_waiters);
  383. if (freed && stale) {
  384. ASSERT(bip->bli_flags & XFS_BLI_STALE);
  385. ASSERT(xfs_buf_islocked(bp));
  386. ASSERT(XFS_BUF_ISSTALE(bp));
  387. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  388. trace_xfs_buf_item_unpin_stale(bip);
  389. if (remove) {
  390. /*
  391. * If we are in a transaction context, we have to
  392. * remove the log item from the transaction as we are
  393. * about to release our reference to the buffer. If we
  394. * don't, the unlock that occurs later in
  395. * xfs_trans_uncommit() will try to reference the
  396. * buffer which we no longer have a hold on.
  397. */
  398. if (lip->li_desc)
  399. xfs_trans_del_item(lip);
  400. /*
  401. * Since the transaction no longer refers to the buffer,
  402. * the buffer should no longer refer to the transaction.
  403. */
  404. bp->b_transp = NULL;
  405. }
  406. /*
  407. * If we get called here because of an IO error, we may
  408. * or may not have the item on the AIL. xfs_trans_ail_delete()
  409. * will take care of that situation.
  410. * xfs_trans_ail_delete() drops the AIL lock.
  411. */
  412. if (bip->bli_flags & XFS_BLI_STALE_INODE) {
  413. xfs_buf_do_callbacks(bp);
  414. bp->b_fspriv = NULL;
  415. bp->b_iodone = NULL;
  416. } else {
  417. spin_lock(&ailp->xa_lock);
  418. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_LOG_IO_ERROR);
  419. xfs_buf_item_relse(bp);
  420. ASSERT(bp->b_fspriv == NULL);
  421. }
  422. xfs_buf_relse(bp);
  423. } else if (freed && remove) {
  424. xfs_buf_lock(bp);
  425. xfs_buf_ioerror(bp, EIO);
  426. XFS_BUF_UNDONE(bp);
  427. xfs_buf_stale(bp);
  428. xfs_buf_ioend(bp, 0);
  429. }
  430. }
  431. STATIC uint
  432. xfs_buf_item_push(
  433. struct xfs_log_item *lip,
  434. struct list_head *buffer_list)
  435. {
  436. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  437. struct xfs_buf *bp = bip->bli_buf;
  438. uint rval = XFS_ITEM_SUCCESS;
  439. if (xfs_buf_ispinned(bp))
  440. return XFS_ITEM_PINNED;
  441. if (!xfs_buf_trylock(bp))
  442. return XFS_ITEM_LOCKED;
  443. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  444. trace_xfs_buf_item_push(bip);
  445. if (!xfs_buf_delwri_queue(bp, buffer_list))
  446. rval = XFS_ITEM_FLUSHING;
  447. xfs_buf_unlock(bp);
  448. return rval;
  449. }
  450. /*
  451. * Release the buffer associated with the buf log item. If there is no dirty
  452. * logged data associated with the buffer recorded in the buf log item, then
  453. * free the buf log item and remove the reference to it in the buffer.
  454. *
  455. * This call ignores the recursion count. It is only called when the buffer
  456. * should REALLY be unlocked, regardless of the recursion count.
  457. *
  458. * We unconditionally drop the transaction's reference to the log item. If the
  459. * item was logged, then another reference was taken when it was pinned, so we
  460. * can safely drop the transaction reference now. This also allows us to avoid
  461. * potential races with the unpin code freeing the bli by not referencing the
  462. * bli after we've dropped the reference count.
  463. *
  464. * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
  465. * if necessary but do not unlock the buffer. This is for support of
  466. * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
  467. * free the item.
  468. */
  469. STATIC void
  470. xfs_buf_item_unlock(
  471. struct xfs_log_item *lip)
  472. {
  473. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  474. struct xfs_buf *bp = bip->bli_buf;
  475. int aborted;
  476. uint hold;
  477. /* Clear the buffer's association with this transaction. */
  478. bp->b_transp = NULL;
  479. /*
  480. * If this is a transaction abort, don't return early. Instead, allow
  481. * the brelse to happen. Normally it would be done for stale
  482. * (cancelled) buffers at unpin time, but we'll never go through the
  483. * pin/unpin cycle if we abort inside commit.
  484. */
  485. aborted = (lip->li_flags & XFS_LI_ABORTED) != 0;
  486. /*
  487. * Before possibly freeing the buf item, determine if we should
  488. * release the buffer at the end of this routine.
  489. */
  490. hold = bip->bli_flags & XFS_BLI_HOLD;
  491. /* Clear the per transaction state. */
  492. bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD);
  493. /*
  494. * If the buf item is marked stale, then don't do anything. We'll
  495. * unlock the buffer and free the buf item when the buffer is unpinned
  496. * for the last time.
  497. */
  498. if (bip->bli_flags & XFS_BLI_STALE) {
  499. trace_xfs_buf_item_unlock_stale(bip);
  500. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  501. if (!aborted) {
  502. atomic_dec(&bip->bli_refcount);
  503. return;
  504. }
  505. }
  506. trace_xfs_buf_item_unlock(bip);
  507. /*
  508. * If the buf item isn't tracking any data, free it, otherwise drop the
  509. * reference we hold to it.
  510. */
  511. if (xfs_bitmap_empty(bip->bli_format.blf_data_map,
  512. bip->bli_format.blf_map_size))
  513. xfs_buf_item_relse(bp);
  514. else
  515. atomic_dec(&bip->bli_refcount);
  516. if (!hold)
  517. xfs_buf_relse(bp);
  518. }
  519. /*
  520. * This is called to find out where the oldest active copy of the
  521. * buf log item in the on disk log resides now that the last log
  522. * write of it completed at the given lsn.
  523. * We always re-log all the dirty data in a buffer, so usually the
  524. * latest copy in the on disk log is the only one that matters. For
  525. * those cases we simply return the given lsn.
  526. *
  527. * The one exception to this is for buffers full of newly allocated
  528. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  529. * flag set, indicating that only the di_next_unlinked fields from the
  530. * inodes in the buffers will be replayed during recovery. If the
  531. * original newly allocated inode images have not yet been flushed
  532. * when the buffer is so relogged, then we need to make sure that we
  533. * keep the old images in the 'active' portion of the log. We do this
  534. * by returning the original lsn of that transaction here rather than
  535. * the current one.
  536. */
  537. STATIC xfs_lsn_t
  538. xfs_buf_item_committed(
  539. struct xfs_log_item *lip,
  540. xfs_lsn_t lsn)
  541. {
  542. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  543. trace_xfs_buf_item_committed(bip);
  544. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
  545. return lip->li_lsn;
  546. return lsn;
  547. }
  548. STATIC void
  549. xfs_buf_item_committing(
  550. struct xfs_log_item *lip,
  551. xfs_lsn_t commit_lsn)
  552. {
  553. }
  554. /*
  555. * This is the ops vector shared by all buf log items.
  556. */
  557. static const struct xfs_item_ops xfs_buf_item_ops = {
  558. .iop_size = xfs_buf_item_size,
  559. .iop_format = xfs_buf_item_format,
  560. .iop_pin = xfs_buf_item_pin,
  561. .iop_unpin = xfs_buf_item_unpin,
  562. .iop_unlock = xfs_buf_item_unlock,
  563. .iop_committed = xfs_buf_item_committed,
  564. .iop_push = xfs_buf_item_push,
  565. .iop_committing = xfs_buf_item_committing
  566. };
  567. /*
  568. * Allocate a new buf log item to go with the given buffer.
  569. * Set the buffer's b_fsprivate field to point to the new
  570. * buf log item. If there are other item's attached to the
  571. * buffer (see xfs_buf_attach_iodone() below), then put the
  572. * buf log item at the front.
  573. */
  574. void
  575. xfs_buf_item_init(
  576. xfs_buf_t *bp,
  577. xfs_mount_t *mp)
  578. {
  579. xfs_log_item_t *lip = bp->b_fspriv;
  580. xfs_buf_log_item_t *bip;
  581. int chunks;
  582. int map_size;
  583. /*
  584. * Check to see if there is already a buf log item for
  585. * this buffer. If there is, it is guaranteed to be
  586. * the first. If we do already have one, there is
  587. * nothing to do here so return.
  588. */
  589. ASSERT(bp->b_target->bt_mount == mp);
  590. if (lip != NULL && lip->li_type == XFS_LI_BUF)
  591. return;
  592. /*
  593. * chunks is the number of XFS_BLF_CHUNK size pieces
  594. * the buffer can be divided into. Make sure not to
  595. * truncate any pieces. map_size is the size of the
  596. * bitmap needed to describe the chunks of the buffer.
  597. */
  598. chunks = (int)((BBTOB(bp->b_length) + (XFS_BLF_CHUNK - 1)) >>
  599. XFS_BLF_SHIFT);
  600. map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
  601. bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
  602. KM_SLEEP);
  603. xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
  604. bip->bli_buf = bp;
  605. xfs_buf_hold(bp);
  606. bip->bli_format.blf_type = XFS_LI_BUF;
  607. bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
  608. bip->bli_format.blf_len = (ushort)bp->b_length;
  609. bip->bli_format.blf_map_size = map_size;
  610. #ifdef XFS_TRANS_DEBUG
  611. /*
  612. * Allocate the arrays for tracking what needs to be logged
  613. * and what our callers request to be logged. bli_orig
  614. * holds a copy of the original, clean buffer for comparison
  615. * against, and bli_logged keeps a 1 bit flag per byte in
  616. * the buffer to indicate which bytes the callers have asked
  617. * to have logged.
  618. */
  619. bip->bli_orig = kmem_alloc(BBTOB(bp->b_length), KM_SLEEP);
  620. memcpy(bip->bli_orig, bp->b_addr, BBTOB(bp->b_length));
  621. bip->bli_logged = kmem_zalloc(BBTOB(bp->b_length) / NBBY, KM_SLEEP);
  622. #endif
  623. /*
  624. * Put the buf item into the list of items attached to the
  625. * buffer at the front.
  626. */
  627. if (bp->b_fspriv)
  628. bip->bli_item.li_bio_list = bp->b_fspriv;
  629. bp->b_fspriv = bip;
  630. }
  631. /*
  632. * Mark bytes first through last inclusive as dirty in the buf
  633. * item's bitmap.
  634. */
  635. void
  636. xfs_buf_item_log(
  637. xfs_buf_log_item_t *bip,
  638. uint first,
  639. uint last)
  640. {
  641. uint first_bit;
  642. uint last_bit;
  643. uint bits_to_set;
  644. uint bits_set;
  645. uint word_num;
  646. uint *wordp;
  647. uint bit;
  648. uint end_bit;
  649. uint mask;
  650. /*
  651. * Mark the item as having some dirty data for
  652. * quick reference in xfs_buf_item_dirty.
  653. */
  654. bip->bli_flags |= XFS_BLI_DIRTY;
  655. /*
  656. * Convert byte offsets to bit numbers.
  657. */
  658. first_bit = first >> XFS_BLF_SHIFT;
  659. last_bit = last >> XFS_BLF_SHIFT;
  660. /*
  661. * Calculate the total number of bits to be set.
  662. */
  663. bits_to_set = last_bit - first_bit + 1;
  664. /*
  665. * Get a pointer to the first word in the bitmap
  666. * to set a bit in.
  667. */
  668. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  669. wordp = &(bip->bli_format.blf_data_map[word_num]);
  670. /*
  671. * Calculate the starting bit in the first word.
  672. */
  673. bit = first_bit & (uint)(NBWORD - 1);
  674. /*
  675. * First set any bits in the first word of our range.
  676. * If it starts at bit 0 of the word, it will be
  677. * set below rather than here. That is what the variable
  678. * bit tells us. The variable bits_set tracks the number
  679. * of bits that have been set so far. End_bit is the number
  680. * of the last bit to be set in this word plus one.
  681. */
  682. if (bit) {
  683. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  684. mask = ((1 << (end_bit - bit)) - 1) << bit;
  685. *wordp |= mask;
  686. wordp++;
  687. bits_set = end_bit - bit;
  688. } else {
  689. bits_set = 0;
  690. }
  691. /*
  692. * Now set bits a whole word at a time that are between
  693. * first_bit and last_bit.
  694. */
  695. while ((bits_to_set - bits_set) >= NBWORD) {
  696. *wordp |= 0xffffffff;
  697. bits_set += NBWORD;
  698. wordp++;
  699. }
  700. /*
  701. * Finally, set any bits left to be set in one last partial word.
  702. */
  703. end_bit = bits_to_set - bits_set;
  704. if (end_bit) {
  705. mask = (1 << end_bit) - 1;
  706. *wordp |= mask;
  707. }
  708. xfs_buf_item_log_debug(bip, first, last);
  709. }
  710. /*
  711. * Return 1 if the buffer has some data that has been logged (at any
  712. * point, not just the current transaction) and 0 if not.
  713. */
  714. uint
  715. xfs_buf_item_dirty(
  716. xfs_buf_log_item_t *bip)
  717. {
  718. return (bip->bli_flags & XFS_BLI_DIRTY);
  719. }
  720. STATIC void
  721. xfs_buf_item_free(
  722. xfs_buf_log_item_t *bip)
  723. {
  724. #ifdef XFS_TRANS_DEBUG
  725. kmem_free(bip->bli_orig);
  726. kmem_free(bip->bli_logged);
  727. #endif /* XFS_TRANS_DEBUG */
  728. kmem_zone_free(xfs_buf_item_zone, bip);
  729. }
  730. /*
  731. * This is called when the buf log item is no longer needed. It should
  732. * free the buf log item associated with the given buffer and clear
  733. * the buffer's pointer to the buf log item. If there are no more
  734. * items in the list, clear the b_iodone field of the buffer (see
  735. * xfs_buf_attach_iodone() below).
  736. */
  737. void
  738. xfs_buf_item_relse(
  739. xfs_buf_t *bp)
  740. {
  741. xfs_buf_log_item_t *bip;
  742. trace_xfs_buf_item_relse(bp, _RET_IP_);
  743. bip = bp->b_fspriv;
  744. bp->b_fspriv = bip->bli_item.li_bio_list;
  745. if (bp->b_fspriv == NULL)
  746. bp->b_iodone = NULL;
  747. xfs_buf_rele(bp);
  748. xfs_buf_item_free(bip);
  749. }
  750. /*
  751. * Add the given log item with its callback to the list of callbacks
  752. * to be called when the buffer's I/O completes. If it is not set
  753. * already, set the buffer's b_iodone() routine to be
  754. * xfs_buf_iodone_callbacks() and link the log item into the list of
  755. * items rooted at b_fsprivate. Items are always added as the second
  756. * entry in the list if there is a first, because the buf item code
  757. * assumes that the buf log item is first.
  758. */
  759. void
  760. xfs_buf_attach_iodone(
  761. xfs_buf_t *bp,
  762. void (*cb)(xfs_buf_t *, xfs_log_item_t *),
  763. xfs_log_item_t *lip)
  764. {
  765. xfs_log_item_t *head_lip;
  766. ASSERT(xfs_buf_islocked(bp));
  767. lip->li_cb = cb;
  768. head_lip = bp->b_fspriv;
  769. if (head_lip) {
  770. lip->li_bio_list = head_lip->li_bio_list;
  771. head_lip->li_bio_list = lip;
  772. } else {
  773. bp->b_fspriv = lip;
  774. }
  775. ASSERT(bp->b_iodone == NULL ||
  776. bp->b_iodone == xfs_buf_iodone_callbacks);
  777. bp->b_iodone = xfs_buf_iodone_callbacks;
  778. }
  779. /*
  780. * We can have many callbacks on a buffer. Running the callbacks individually
  781. * can cause a lot of contention on the AIL lock, so we allow for a single
  782. * callback to be able to scan the remaining lip->li_bio_list for other items
  783. * of the same type and callback to be processed in the first call.
  784. *
  785. * As a result, the loop walking the callback list below will also modify the
  786. * list. it removes the first item from the list and then runs the callback.
  787. * The loop then restarts from the new head of the list. This allows the
  788. * callback to scan and modify the list attached to the buffer and we don't
  789. * have to care about maintaining a next item pointer.
  790. */
  791. STATIC void
  792. xfs_buf_do_callbacks(
  793. struct xfs_buf *bp)
  794. {
  795. struct xfs_log_item *lip;
  796. while ((lip = bp->b_fspriv) != NULL) {
  797. bp->b_fspriv = lip->li_bio_list;
  798. ASSERT(lip->li_cb != NULL);
  799. /*
  800. * Clear the next pointer so we don't have any
  801. * confusion if the item is added to another buf.
  802. * Don't touch the log item after calling its
  803. * callback, because it could have freed itself.
  804. */
  805. lip->li_bio_list = NULL;
  806. lip->li_cb(bp, lip);
  807. }
  808. }
  809. /*
  810. * This is the iodone() function for buffers which have had callbacks
  811. * attached to them by xfs_buf_attach_iodone(). It should remove each
  812. * log item from the buffer's list and call the callback of each in turn.
  813. * When done, the buffer's fsprivate field is set to NULL and the buffer
  814. * is unlocked with a call to iodone().
  815. */
  816. void
  817. xfs_buf_iodone_callbacks(
  818. struct xfs_buf *bp)
  819. {
  820. struct xfs_log_item *lip = bp->b_fspriv;
  821. struct xfs_mount *mp = lip->li_mountp;
  822. static ulong lasttime;
  823. static xfs_buftarg_t *lasttarg;
  824. if (likely(!xfs_buf_geterror(bp)))
  825. goto do_callbacks;
  826. /*
  827. * If we've already decided to shutdown the filesystem because of
  828. * I/O errors, there's no point in giving this a retry.
  829. */
  830. if (XFS_FORCED_SHUTDOWN(mp)) {
  831. xfs_buf_stale(bp);
  832. XFS_BUF_DONE(bp);
  833. trace_xfs_buf_item_iodone(bp, _RET_IP_);
  834. goto do_callbacks;
  835. }
  836. if (bp->b_target != lasttarg ||
  837. time_after(jiffies, (lasttime + 5*HZ))) {
  838. lasttime = jiffies;
  839. xfs_buf_ioerror_alert(bp, __func__);
  840. }
  841. lasttarg = bp->b_target;
  842. /*
  843. * If the write was asynchronous then no one will be looking for the
  844. * error. Clear the error state and write the buffer out again.
  845. *
  846. * XXX: This helps against transient write errors, but we need to find
  847. * a way to shut the filesystem down if the writes keep failing.
  848. *
  849. * In practice we'll shut the filesystem down soon as non-transient
  850. * erorrs tend to affect the whole device and a failing log write
  851. * will make us give up. But we really ought to do better here.
  852. */
  853. if (XFS_BUF_ISASYNC(bp)) {
  854. ASSERT(bp->b_iodone != NULL);
  855. trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
  856. xfs_buf_ioerror(bp, 0); /* errno of 0 unsets the flag */
  857. if (!XFS_BUF_ISSTALE(bp)) {
  858. bp->b_flags |= XBF_WRITE | XBF_ASYNC | XBF_DONE;
  859. xfs_bdstrat_cb(bp);
  860. } else {
  861. xfs_buf_relse(bp);
  862. }
  863. return;
  864. }
  865. /*
  866. * If the write of the buffer was synchronous, we want to make
  867. * sure to return the error to the caller of xfs_bwrite().
  868. */
  869. xfs_buf_stale(bp);
  870. XFS_BUF_DONE(bp);
  871. trace_xfs_buf_error_relse(bp, _RET_IP_);
  872. do_callbacks:
  873. xfs_buf_do_callbacks(bp);
  874. bp->b_fspriv = NULL;
  875. bp->b_iodone = NULL;
  876. xfs_buf_ioend(bp, 0);
  877. }
  878. /*
  879. * This is the iodone() function for buffers which have been
  880. * logged. It is called when they are eventually flushed out.
  881. * It should remove the buf item from the AIL, and free the buf item.
  882. * It is called by xfs_buf_iodone_callbacks() above which will take
  883. * care of cleaning up the buffer itself.
  884. */
  885. void
  886. xfs_buf_iodone(
  887. struct xfs_buf *bp,
  888. struct xfs_log_item *lip)
  889. {
  890. struct xfs_ail *ailp = lip->li_ailp;
  891. ASSERT(BUF_ITEM(lip)->bli_buf == bp);
  892. xfs_buf_rele(bp);
  893. /*
  894. * If we are forcibly shutting down, this may well be
  895. * off the AIL already. That's because we simulate the
  896. * log-committed callbacks to unpin these buffers. Or we may never
  897. * have put this item on AIL because of the transaction was
  898. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  899. *
  900. * Either way, AIL is useless if we're forcing a shutdown.
  901. */
  902. spin_lock(&ailp->xa_lock);
  903. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  904. xfs_buf_item_free(BUF_ITEM(lip));
  905. }