xfs_buf_item.c 28 KB

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