xfs_buf_item.c 32 KB

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