xfs_buf_item.c 30 KB

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