xfs_buf_item.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. xfs_buf_lock(bp);
  483. xfs_buf_ioerror(bp, EIO);
  484. XFS_BUF_UNDONE(bp);
  485. xfs_buf_stale(bp);
  486. xfs_buf_ioend(bp, 0);
  487. }
  488. }
  489. STATIC uint
  490. xfs_buf_item_push(
  491. struct xfs_log_item *lip,
  492. struct list_head *buffer_list)
  493. {
  494. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  495. struct xfs_buf *bp = bip->bli_buf;
  496. uint rval = XFS_ITEM_SUCCESS;
  497. if (xfs_buf_ispinned(bp))
  498. return XFS_ITEM_PINNED;
  499. if (!xfs_buf_trylock(bp))
  500. return XFS_ITEM_LOCKED;
  501. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  502. trace_xfs_buf_item_push(bip);
  503. if (!xfs_buf_delwri_queue(bp, buffer_list))
  504. rval = XFS_ITEM_FLUSHING;
  505. xfs_buf_unlock(bp);
  506. return rval;
  507. }
  508. /*
  509. * Release the buffer associated with the buf log item. If there is no dirty
  510. * logged data associated with the buffer recorded in the buf log item, then
  511. * free the buf log item and remove the reference to it in the buffer.
  512. *
  513. * This call ignores the recursion count. It is only called when the buffer
  514. * should REALLY be unlocked, regardless of the recursion count.
  515. *
  516. * We unconditionally drop the transaction's reference to the log item. If the
  517. * item was logged, then another reference was taken when it was pinned, so we
  518. * can safely drop the transaction reference now. This also allows us to avoid
  519. * potential races with the unpin code freeing the bli by not referencing the
  520. * bli after we've dropped the reference count.
  521. *
  522. * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
  523. * if necessary but do not unlock the buffer. This is for support of
  524. * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
  525. * free the item.
  526. */
  527. STATIC void
  528. xfs_buf_item_unlock(
  529. struct xfs_log_item *lip)
  530. {
  531. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  532. struct xfs_buf *bp = bip->bli_buf;
  533. int aborted;
  534. uint hold;
  535. /* Clear the buffer's association with this transaction. */
  536. bp->b_transp = NULL;
  537. /*
  538. * If this is a transaction abort, don't return early. Instead, allow
  539. * the brelse to happen. Normally it would be done for stale
  540. * (cancelled) buffers at unpin time, but we'll never go through the
  541. * pin/unpin cycle if we abort inside commit.
  542. */
  543. aborted = (lip->li_flags & XFS_LI_ABORTED) != 0;
  544. /*
  545. * Before possibly freeing the buf item, determine if we should
  546. * release the buffer at the end of this routine.
  547. */
  548. hold = bip->bli_flags & XFS_BLI_HOLD;
  549. /* Clear the per transaction state. */
  550. bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD);
  551. /*
  552. * If the buf item is marked stale, then don't do anything. We'll
  553. * unlock the buffer and free the buf item when the buffer is unpinned
  554. * for the last time.
  555. */
  556. if (bip->bli_flags & XFS_BLI_STALE) {
  557. trace_xfs_buf_item_unlock_stale(bip);
  558. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  559. if (!aborted) {
  560. atomic_dec(&bip->bli_refcount);
  561. return;
  562. }
  563. }
  564. trace_xfs_buf_item_unlock(bip);
  565. /*
  566. * If the buf item isn't tracking any data, free it, otherwise drop the
  567. * reference we hold to it.
  568. */
  569. if (xfs_bitmap_empty(bip->bli_format.blf_data_map,
  570. bip->bli_format.blf_map_size))
  571. xfs_buf_item_relse(bp);
  572. else
  573. atomic_dec(&bip->bli_refcount);
  574. if (!hold)
  575. xfs_buf_relse(bp);
  576. }
  577. /*
  578. * This is called to find out where the oldest active copy of the
  579. * buf log item in the on disk log resides now that the last log
  580. * write of it completed at the given lsn.
  581. * We always re-log all the dirty data in a buffer, so usually the
  582. * latest copy in the on disk log is the only one that matters. For
  583. * those cases we simply return the given lsn.
  584. *
  585. * The one exception to this is for buffers full of newly allocated
  586. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  587. * flag set, indicating that only the di_next_unlinked fields from the
  588. * inodes in the buffers will be replayed during recovery. If the
  589. * original newly allocated inode images have not yet been flushed
  590. * when the buffer is so relogged, then we need to make sure that we
  591. * keep the old images in the 'active' portion of the log. We do this
  592. * by returning the original lsn of that transaction here rather than
  593. * the current one.
  594. */
  595. STATIC xfs_lsn_t
  596. xfs_buf_item_committed(
  597. struct xfs_log_item *lip,
  598. xfs_lsn_t lsn)
  599. {
  600. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  601. trace_xfs_buf_item_committed(bip);
  602. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
  603. return lip->li_lsn;
  604. return lsn;
  605. }
  606. STATIC void
  607. xfs_buf_item_committing(
  608. struct xfs_log_item *lip,
  609. xfs_lsn_t commit_lsn)
  610. {
  611. }
  612. /*
  613. * This is the ops vector shared by all buf log items.
  614. */
  615. static const struct xfs_item_ops xfs_buf_item_ops = {
  616. .iop_size = xfs_buf_item_size,
  617. .iop_format = xfs_buf_item_format,
  618. .iop_pin = xfs_buf_item_pin,
  619. .iop_unpin = xfs_buf_item_unpin,
  620. .iop_unlock = xfs_buf_item_unlock,
  621. .iop_committed = xfs_buf_item_committed,
  622. .iop_push = xfs_buf_item_push,
  623. .iop_committing = xfs_buf_item_committing
  624. };
  625. STATIC int
  626. xfs_buf_item_get_format(
  627. struct xfs_buf_log_item *bip,
  628. int count)
  629. {
  630. ASSERT(bip->bli_formats == NULL);
  631. bip->bli_format_count = count;
  632. if (count == 1) {
  633. bip->bli_formats = &bip->bli_format;
  634. return 0;
  635. }
  636. bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
  637. KM_SLEEP);
  638. if (!bip->bli_formats)
  639. return ENOMEM;
  640. return 0;
  641. }
  642. STATIC void
  643. xfs_buf_item_free_format(
  644. struct xfs_buf_log_item *bip)
  645. {
  646. if (bip->bli_formats != &bip->bli_format) {
  647. kmem_free(bip->bli_formats);
  648. bip->bli_formats = NULL;
  649. }
  650. }
  651. /*
  652. * Allocate a new buf log item to go with the given buffer.
  653. * Set the buffer's b_fsprivate field to point to the new
  654. * buf log item. If there are other item's attached to the
  655. * buffer (see xfs_buf_attach_iodone() below), then put the
  656. * buf log item at the front.
  657. */
  658. void
  659. xfs_buf_item_init(
  660. xfs_buf_t *bp,
  661. xfs_mount_t *mp)
  662. {
  663. xfs_log_item_t *lip = bp->b_fspriv;
  664. xfs_buf_log_item_t *bip;
  665. int chunks;
  666. int map_size;
  667. int error;
  668. int i;
  669. /*
  670. * Check to see if there is already a buf log item for
  671. * this buffer. If there is, it is guaranteed to be
  672. * the first. If we do already have one, there is
  673. * nothing to do here so return.
  674. */
  675. ASSERT(bp->b_target->bt_mount == mp);
  676. if (lip != NULL && lip->li_type == XFS_LI_BUF)
  677. return;
  678. bip = kmem_zone_zalloc(xfs_buf_item_zone, KM_SLEEP);
  679. xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
  680. bip->bli_buf = bp;
  681. xfs_buf_hold(bp);
  682. /*
  683. * chunks is the number of XFS_BLF_CHUNK size pieces the buffer
  684. * can be divided into. Make sure not to truncate any pieces.
  685. * map_size is the size of the bitmap needed to describe the
  686. * chunks of the buffer.
  687. *
  688. * Discontiguous buffer support follows the layout of the underlying
  689. * buffer. This makes the implementation as simple as possible.
  690. */
  691. error = xfs_buf_item_get_format(bip, bp->b_map_count);
  692. ASSERT(error == 0);
  693. for (i = 0; i < bip->bli_format_count; i++) {
  694. chunks = DIV_ROUND_UP(BBTOB(bp->b_maps[i].bm_len),
  695. XFS_BLF_CHUNK);
  696. map_size = DIV_ROUND_UP(chunks, NBWORD);
  697. bip->bli_formats[i].blf_type = XFS_LI_BUF;
  698. bip->bli_formats[i].blf_blkno = bp->b_maps[i].bm_bn;
  699. bip->bli_formats[i].blf_len = bp->b_maps[i].bm_len;
  700. bip->bli_formats[i].blf_map_size = map_size;
  701. }
  702. #ifdef XFS_TRANS_DEBUG
  703. /*
  704. * Allocate the arrays for tracking what needs to be logged
  705. * and what our callers request to be logged. bli_orig
  706. * holds a copy of the original, clean buffer for comparison
  707. * against, and bli_logged keeps a 1 bit flag per byte in
  708. * the buffer to indicate which bytes the callers have asked
  709. * to have logged.
  710. */
  711. bip->bli_orig = kmem_alloc(BBTOB(bp->b_length), KM_SLEEP);
  712. memcpy(bip->bli_orig, bp->b_addr, BBTOB(bp->b_length));
  713. bip->bli_logged = kmem_zalloc(BBTOB(bp->b_length) / NBBY, KM_SLEEP);
  714. #endif
  715. /*
  716. * Put the buf item into the list of items attached to the
  717. * buffer at the front.
  718. */
  719. if (bp->b_fspriv)
  720. bip->bli_item.li_bio_list = bp->b_fspriv;
  721. bp->b_fspriv = bip;
  722. }
  723. /*
  724. * Mark bytes first through last inclusive as dirty in the buf
  725. * item's bitmap.
  726. */
  727. void
  728. xfs_buf_item_log_segment(
  729. struct xfs_buf_log_item *bip,
  730. uint first,
  731. uint last,
  732. uint *map)
  733. {
  734. uint first_bit;
  735. uint last_bit;
  736. uint bits_to_set;
  737. uint bits_set;
  738. uint word_num;
  739. uint *wordp;
  740. uint bit;
  741. uint end_bit;
  742. uint mask;
  743. /*
  744. * Convert byte offsets to bit numbers.
  745. */
  746. first_bit = first >> XFS_BLF_SHIFT;
  747. last_bit = last >> XFS_BLF_SHIFT;
  748. /*
  749. * Calculate the total number of bits to be set.
  750. */
  751. bits_to_set = last_bit - first_bit + 1;
  752. /*
  753. * Get a pointer to the first word in the bitmap
  754. * to set a bit in.
  755. */
  756. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  757. wordp = &map[word_num];
  758. /*
  759. * Calculate the starting bit in the first word.
  760. */
  761. bit = first_bit & (uint)(NBWORD - 1);
  762. /*
  763. * First set any bits in the first word of our range.
  764. * If it starts at bit 0 of the word, it will be
  765. * set below rather than here. That is what the variable
  766. * bit tells us. The variable bits_set tracks the number
  767. * of bits that have been set so far. End_bit is the number
  768. * of the last bit to be set in this word plus one.
  769. */
  770. if (bit) {
  771. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  772. mask = ((1 << (end_bit - bit)) - 1) << bit;
  773. *wordp |= mask;
  774. wordp++;
  775. bits_set = end_bit - bit;
  776. } else {
  777. bits_set = 0;
  778. }
  779. /*
  780. * Now set bits a whole word at a time that are between
  781. * first_bit and last_bit.
  782. */
  783. while ((bits_to_set - bits_set) >= NBWORD) {
  784. *wordp |= 0xffffffff;
  785. bits_set += NBWORD;
  786. wordp++;
  787. }
  788. /*
  789. * Finally, set any bits left to be set in one last partial word.
  790. */
  791. end_bit = bits_to_set - bits_set;
  792. if (end_bit) {
  793. mask = (1 << end_bit) - 1;
  794. *wordp |= mask;
  795. }
  796. xfs_buf_item_log_debug(bip, first, last);
  797. }
  798. /*
  799. * Mark bytes first through last inclusive as dirty in the buf
  800. * item's bitmap.
  801. */
  802. void
  803. xfs_buf_item_log(
  804. xfs_buf_log_item_t *bip,
  805. uint first,
  806. uint last)
  807. {
  808. int i;
  809. uint start;
  810. uint end;
  811. struct xfs_buf *bp = bip->bli_buf;
  812. /*
  813. * Mark the item as having some dirty data for
  814. * quick reference in xfs_buf_item_dirty.
  815. */
  816. bip->bli_flags |= XFS_BLI_DIRTY;
  817. /*
  818. * walk each buffer segment and mark them dirty appropriately.
  819. */
  820. start = 0;
  821. for (i = 0; i < bip->bli_format_count; i++) {
  822. if (start > last)
  823. break;
  824. end = start + BBTOB(bp->b_maps[i].bm_len);
  825. if (first > end) {
  826. start += BBTOB(bp->b_maps[i].bm_len);
  827. continue;
  828. }
  829. if (first < start)
  830. first = start;
  831. if (end > last)
  832. end = last;
  833. xfs_buf_item_log_segment(bip, first, end,
  834. &bip->bli_formats[i].blf_data_map[0]);
  835. start += bp->b_maps[i].bm_len;
  836. }
  837. }
  838. /*
  839. * Return 1 if the buffer has some data that has been logged (at any
  840. * point, not just the current transaction) and 0 if not.
  841. */
  842. uint
  843. xfs_buf_item_dirty(
  844. xfs_buf_log_item_t *bip)
  845. {
  846. return (bip->bli_flags & XFS_BLI_DIRTY);
  847. }
  848. STATIC void
  849. xfs_buf_item_free(
  850. xfs_buf_log_item_t *bip)
  851. {
  852. #ifdef XFS_TRANS_DEBUG
  853. kmem_free(bip->bli_orig);
  854. kmem_free(bip->bli_logged);
  855. #endif /* XFS_TRANS_DEBUG */
  856. xfs_buf_item_free_format(bip);
  857. kmem_zone_free(xfs_buf_item_zone, bip);
  858. }
  859. /*
  860. * This is called when the buf log item is no longer needed. It should
  861. * free the buf log item associated with the given buffer and clear
  862. * the buffer's pointer to the buf log item. If there are no more
  863. * items in the list, clear the b_iodone field of the buffer (see
  864. * xfs_buf_attach_iodone() below).
  865. */
  866. void
  867. xfs_buf_item_relse(
  868. xfs_buf_t *bp)
  869. {
  870. xfs_buf_log_item_t *bip;
  871. trace_xfs_buf_item_relse(bp, _RET_IP_);
  872. bip = bp->b_fspriv;
  873. bp->b_fspriv = bip->bli_item.li_bio_list;
  874. if (bp->b_fspriv == NULL)
  875. bp->b_iodone = NULL;
  876. xfs_buf_rele(bp);
  877. xfs_buf_item_free(bip);
  878. }
  879. /*
  880. * Add the given log item with its callback to the list of callbacks
  881. * to be called when the buffer's I/O completes. If it is not set
  882. * already, set the buffer's b_iodone() routine to be
  883. * xfs_buf_iodone_callbacks() and link the log item into the list of
  884. * items rooted at b_fsprivate. Items are always added as the second
  885. * entry in the list if there is a first, because the buf item code
  886. * assumes that the buf log item is first.
  887. */
  888. void
  889. xfs_buf_attach_iodone(
  890. xfs_buf_t *bp,
  891. void (*cb)(xfs_buf_t *, xfs_log_item_t *),
  892. xfs_log_item_t *lip)
  893. {
  894. xfs_log_item_t *head_lip;
  895. ASSERT(xfs_buf_islocked(bp));
  896. lip->li_cb = cb;
  897. head_lip = bp->b_fspriv;
  898. if (head_lip) {
  899. lip->li_bio_list = head_lip->li_bio_list;
  900. head_lip->li_bio_list = lip;
  901. } else {
  902. bp->b_fspriv = lip;
  903. }
  904. ASSERT(bp->b_iodone == NULL ||
  905. bp->b_iodone == xfs_buf_iodone_callbacks);
  906. bp->b_iodone = xfs_buf_iodone_callbacks;
  907. }
  908. /*
  909. * We can have many callbacks on a buffer. Running the callbacks individually
  910. * can cause a lot of contention on the AIL lock, so we allow for a single
  911. * callback to be able to scan the remaining lip->li_bio_list for other items
  912. * of the same type and callback to be processed in the first call.
  913. *
  914. * As a result, the loop walking the callback list below will also modify the
  915. * list. it removes the first item from the list and then runs the callback.
  916. * The loop then restarts from the new head of the list. This allows the
  917. * callback to scan and modify the list attached to the buffer and we don't
  918. * have to care about maintaining a next item pointer.
  919. */
  920. STATIC void
  921. xfs_buf_do_callbacks(
  922. struct xfs_buf *bp)
  923. {
  924. struct xfs_log_item *lip;
  925. while ((lip = bp->b_fspriv) != NULL) {
  926. bp->b_fspriv = lip->li_bio_list;
  927. ASSERT(lip->li_cb != NULL);
  928. /*
  929. * Clear the next pointer so we don't have any
  930. * confusion if the item is added to another buf.
  931. * Don't touch the log item after calling its
  932. * callback, because it could have freed itself.
  933. */
  934. lip->li_bio_list = NULL;
  935. lip->li_cb(bp, lip);
  936. }
  937. }
  938. /*
  939. * This is the iodone() function for buffers which have had callbacks
  940. * attached to them by xfs_buf_attach_iodone(). It should remove each
  941. * log item from the buffer's list and call the callback of each in turn.
  942. * When done, the buffer's fsprivate field is set to NULL and the buffer
  943. * is unlocked with a call to iodone().
  944. */
  945. void
  946. xfs_buf_iodone_callbacks(
  947. struct xfs_buf *bp)
  948. {
  949. struct xfs_log_item *lip = bp->b_fspriv;
  950. struct xfs_mount *mp = lip->li_mountp;
  951. static ulong lasttime;
  952. static xfs_buftarg_t *lasttarg;
  953. if (likely(!xfs_buf_geterror(bp)))
  954. goto do_callbacks;
  955. /*
  956. * If we've already decided to shutdown the filesystem because of
  957. * I/O errors, there's no point in giving this a retry.
  958. */
  959. if (XFS_FORCED_SHUTDOWN(mp)) {
  960. xfs_buf_stale(bp);
  961. XFS_BUF_DONE(bp);
  962. trace_xfs_buf_item_iodone(bp, _RET_IP_);
  963. goto do_callbacks;
  964. }
  965. if (bp->b_target != lasttarg ||
  966. time_after(jiffies, (lasttime + 5*HZ))) {
  967. lasttime = jiffies;
  968. xfs_buf_ioerror_alert(bp, __func__);
  969. }
  970. lasttarg = bp->b_target;
  971. /*
  972. * If the write was asynchronous then no one will be looking for the
  973. * error. Clear the error state and write the buffer out again.
  974. *
  975. * XXX: This helps against transient write errors, but we need to find
  976. * a way to shut the filesystem down if the writes keep failing.
  977. *
  978. * In practice we'll shut the filesystem down soon as non-transient
  979. * erorrs tend to affect the whole device and a failing log write
  980. * will make us give up. But we really ought to do better here.
  981. */
  982. if (XFS_BUF_ISASYNC(bp)) {
  983. ASSERT(bp->b_iodone != NULL);
  984. trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
  985. xfs_buf_ioerror(bp, 0); /* errno of 0 unsets the flag */
  986. if (!XFS_BUF_ISSTALE(bp)) {
  987. bp->b_flags |= XBF_WRITE | XBF_ASYNC | XBF_DONE;
  988. xfs_buf_iorequest(bp);
  989. } else {
  990. xfs_buf_relse(bp);
  991. }
  992. return;
  993. }
  994. /*
  995. * If the write of the buffer was synchronous, we want to make
  996. * sure to return the error to the caller of xfs_bwrite().
  997. */
  998. xfs_buf_stale(bp);
  999. XFS_BUF_DONE(bp);
  1000. trace_xfs_buf_error_relse(bp, _RET_IP_);
  1001. do_callbacks:
  1002. xfs_buf_do_callbacks(bp);
  1003. bp->b_fspriv = NULL;
  1004. bp->b_iodone = NULL;
  1005. xfs_buf_ioend(bp, 0);
  1006. }
  1007. /*
  1008. * This is the iodone() function for buffers which have been
  1009. * logged. It is called when they are eventually flushed out.
  1010. * It should remove the buf item from the AIL, and free the buf item.
  1011. * It is called by xfs_buf_iodone_callbacks() above which will take
  1012. * care of cleaning up the buffer itself.
  1013. */
  1014. void
  1015. xfs_buf_iodone(
  1016. struct xfs_buf *bp,
  1017. struct xfs_log_item *lip)
  1018. {
  1019. struct xfs_ail *ailp = lip->li_ailp;
  1020. ASSERT(BUF_ITEM(lip)->bli_buf == bp);
  1021. xfs_buf_rele(bp);
  1022. /*
  1023. * If we are forcibly shutting down, this may well be
  1024. * off the AIL already. That's because we simulate the
  1025. * log-committed callbacks to unpin these buffers. Or we may never
  1026. * have put this item on AIL because of the transaction was
  1027. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  1028. *
  1029. * Either way, AIL is useless if we're forcing a shutdown.
  1030. */
  1031. spin_lock(&ailp->xa_lock);
  1032. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  1033. xfs_buf_item_free(BUF_ITEM(lip));
  1034. }