xfs_buf_item.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_bit.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_trans_priv.h"
  29. #include "xfs_error.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_log.h"
  32. kmem_zone_t *xfs_buf_item_zone;
  33. static inline struct xfs_buf_log_item *BUF_ITEM(struct xfs_log_item *lip)
  34. {
  35. return container_of(lip, struct xfs_buf_log_item, bli_item);
  36. }
  37. STATIC void xfs_buf_do_callbacks(struct xfs_buf *bp);
  38. static inline int
  39. xfs_buf_log_format_size(
  40. struct xfs_buf_log_format *blfp)
  41. {
  42. return offsetof(struct xfs_buf_log_format, blf_data_map) +
  43. (blfp->blf_map_size * sizeof(blfp->blf_data_map[0]));
  44. }
  45. /*
  46. * This returns the number of log iovecs needed to log the
  47. * given buf log item.
  48. *
  49. * It calculates this as 1 iovec for the buf log format structure
  50. * and 1 for each stretch of non-contiguous chunks to be logged.
  51. * Contiguous chunks are logged in a single iovec.
  52. *
  53. * If the XFS_BLI_STALE flag has been set, then log nothing.
  54. */
  55. STATIC void
  56. xfs_buf_item_size_segment(
  57. struct xfs_buf_log_item *bip,
  58. struct xfs_buf_log_format *blfp,
  59. int *nvecs,
  60. int *nbytes)
  61. {
  62. struct xfs_buf *bp = bip->bli_buf;
  63. int next_bit;
  64. int last_bit;
  65. last_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
  66. if (last_bit == -1)
  67. return;
  68. /*
  69. * initial count for a dirty buffer is 2 vectors - the format structure
  70. * and the first dirty region.
  71. */
  72. *nvecs += 2;
  73. *nbytes += xfs_buf_log_format_size(blfp) + XFS_BLF_CHUNK;
  74. while (last_bit != -1) {
  75. /*
  76. * This takes the bit number to start looking from and
  77. * returns the next set bit from there. It returns -1
  78. * if there are no more bits set or the start bit is
  79. * beyond the end of the bitmap.
  80. */
  81. next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
  82. last_bit + 1);
  83. /*
  84. * If we run out of bits, leave the loop,
  85. * else if we find a new set of bits bump the number of vecs,
  86. * else keep scanning the current set of bits.
  87. */
  88. if (next_bit == -1) {
  89. break;
  90. } else if (next_bit != last_bit + 1) {
  91. last_bit = next_bit;
  92. (*nvecs)++;
  93. } else if (xfs_buf_offset(bp, next_bit * XFS_BLF_CHUNK) !=
  94. (xfs_buf_offset(bp, last_bit * XFS_BLF_CHUNK) +
  95. XFS_BLF_CHUNK)) {
  96. last_bit = next_bit;
  97. (*nvecs)++;
  98. } else {
  99. last_bit++;
  100. }
  101. *nbytes += XFS_BLF_CHUNK;
  102. }
  103. }
  104. /*
  105. * This returns the number of log iovecs needed to log the given buf log item.
  106. *
  107. * It calculates this as 1 iovec for the buf log format structure and 1 for each
  108. * stretch of non-contiguous chunks to be logged. Contiguous chunks are logged
  109. * in a single iovec.
  110. *
  111. * Discontiguous buffers need a format structure per region that that is being
  112. * logged. This makes the changes in the buffer appear to log recovery as though
  113. * they came from separate buffers, just like would occur if multiple buffers
  114. * were used instead of a single discontiguous buffer. This enables
  115. * discontiguous buffers to be in-memory constructs, completely transparent to
  116. * what ends up on disk.
  117. *
  118. * If the XFS_BLI_STALE flag has been set, then log nothing but the buf log
  119. * format structures.
  120. */
  121. STATIC void
  122. xfs_buf_item_size(
  123. struct xfs_log_item *lip,
  124. int *nvecs,
  125. int *nbytes)
  126. {
  127. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  128. int i;
  129. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  130. if (bip->bli_flags & XFS_BLI_STALE) {
  131. /*
  132. * The buffer is stale, so all we need to log
  133. * is the buf log format structure with the
  134. * cancel flag in it.
  135. */
  136. trace_xfs_buf_item_size_stale(bip);
  137. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  138. *nvecs += bip->bli_format_count;
  139. for (i = 0; i < bip->bli_format_count; i++) {
  140. *nbytes += xfs_buf_log_format_size(&bip->bli_formats[i]);
  141. }
  142. return;
  143. }
  144. ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
  145. if (bip->bli_flags & XFS_BLI_ORDERED) {
  146. /*
  147. * The buffer has been logged just to order it.
  148. * It is not being included in the transaction
  149. * commit, so no vectors are used at all.
  150. */
  151. trace_xfs_buf_item_size_ordered(bip);
  152. *nvecs = XFS_LOG_VEC_ORDERED;
  153. return;
  154. }
  155. /*
  156. * the vector count is based on the number of buffer vectors we have
  157. * dirty bits in. This will only be greater than one when we have a
  158. * compound buffer with more than one segment dirty. Hence for compound
  159. * buffers we need to track which segment the dirty bits correspond to,
  160. * and when we move from one segment to the next increment the vector
  161. * count for the extra buf log format structure that will need to be
  162. * written.
  163. */
  164. for (i = 0; i < bip->bli_format_count; i++) {
  165. xfs_buf_item_size_segment(bip, &bip->bli_formats[i],
  166. nvecs, nbytes);
  167. }
  168. trace_xfs_buf_item_size(bip);
  169. }
  170. static inline void
  171. xfs_buf_item_copy_iovec(
  172. struct xfs_log_vec *lv,
  173. struct xfs_log_iovec **vecp,
  174. struct xfs_buf *bp,
  175. uint offset,
  176. int first_bit,
  177. uint nbits)
  178. {
  179. offset += first_bit * XFS_BLF_CHUNK;
  180. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BCHUNK,
  181. xfs_buf_offset(bp, offset),
  182. nbits * XFS_BLF_CHUNK);
  183. }
  184. static inline bool
  185. xfs_buf_item_straddle(
  186. struct xfs_buf *bp,
  187. uint offset,
  188. int next_bit,
  189. int last_bit)
  190. {
  191. return xfs_buf_offset(bp, offset + (next_bit << XFS_BLF_SHIFT)) !=
  192. (xfs_buf_offset(bp, offset + (last_bit << XFS_BLF_SHIFT)) +
  193. XFS_BLF_CHUNK);
  194. }
  195. static void
  196. xfs_buf_item_format_segment(
  197. struct xfs_buf_log_item *bip,
  198. struct xfs_log_vec *lv,
  199. struct xfs_log_iovec **vecp,
  200. uint offset,
  201. struct xfs_buf_log_format *blfp)
  202. {
  203. struct xfs_buf *bp = bip->bli_buf;
  204. uint base_size;
  205. int first_bit;
  206. int last_bit;
  207. int next_bit;
  208. uint nbits;
  209. /* copy the flags across from the base format item */
  210. blfp->blf_flags = bip->__bli_format.blf_flags;
  211. /*
  212. * Base size is the actual size of the ondisk structure - it reflects
  213. * the actual size of the dirty bitmap rather than the size of the in
  214. * memory structure.
  215. */
  216. base_size = xfs_buf_log_format_size(blfp);
  217. first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
  218. if (!(bip->bli_flags & XFS_BLI_STALE) && first_bit == -1) {
  219. /*
  220. * If the map is not be dirty in the transaction, mark
  221. * the size as zero and do not advance the vector pointer.
  222. */
  223. return;
  224. }
  225. blfp = xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BFORMAT, blfp, base_size);
  226. blfp->blf_size = 1;
  227. if (bip->bli_flags & XFS_BLI_STALE) {
  228. /*
  229. * The buffer is stale, so all we need to log
  230. * is the buf log format structure with the
  231. * cancel flag in it.
  232. */
  233. trace_xfs_buf_item_format_stale(bip);
  234. ASSERT(blfp->blf_flags & XFS_BLF_CANCEL);
  235. return;
  236. }
  237. /*
  238. * Fill in an iovec for each set of contiguous chunks.
  239. */
  240. last_bit = first_bit;
  241. nbits = 1;
  242. for (;;) {
  243. /*
  244. * This takes the bit number to start looking from and
  245. * returns the next set bit from there. It returns -1
  246. * if there are no more bits set or the start bit is
  247. * beyond the end of the bitmap.
  248. */
  249. next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
  250. (uint)last_bit + 1);
  251. /*
  252. * If we run out of bits fill in the last iovec and get out of
  253. * the loop. Else if we start a new set of bits then fill in
  254. * the iovec for the series we were looking at and start
  255. * counting the bits in the new one. Else we're still in the
  256. * same set of bits so just keep counting and scanning.
  257. */
  258. if (next_bit == -1) {
  259. xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
  260. first_bit, nbits);
  261. blfp->blf_size++;
  262. break;
  263. } else if (next_bit != last_bit + 1 ||
  264. xfs_buf_item_straddle(bp, offset, next_bit, last_bit)) {
  265. xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
  266. first_bit, nbits);
  267. blfp->blf_size++;
  268. first_bit = next_bit;
  269. last_bit = next_bit;
  270. nbits = 1;
  271. } else {
  272. last_bit++;
  273. nbits++;
  274. }
  275. }
  276. }
  277. /*
  278. * This is called to fill in the vector of log iovecs for the
  279. * given log buf item. It fills the first entry with a buf log
  280. * format structure, and the rest point to contiguous chunks
  281. * within the buffer.
  282. */
  283. STATIC void
  284. xfs_buf_item_format(
  285. struct xfs_log_item *lip,
  286. struct xfs_log_vec *lv)
  287. {
  288. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  289. struct xfs_buf *bp = bip->bli_buf;
  290. struct xfs_log_iovec *vecp = NULL;
  291. uint offset = 0;
  292. int i;
  293. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  294. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  295. (bip->bli_flags & XFS_BLI_STALE));
  296. ASSERT((bip->bli_flags & XFS_BLI_STALE) ||
  297. (xfs_blft_from_flags(&bip->__bli_format) > XFS_BLFT_UNKNOWN_BUF
  298. && xfs_blft_from_flags(&bip->__bli_format) < XFS_BLFT_MAX_BUF));
  299. /*
  300. * If it is an inode buffer, transfer the in-memory state to the
  301. * format flags and clear the in-memory state.
  302. *
  303. * For buffer based inode allocation, we do not transfer
  304. * this state if the inode buffer allocation has not yet been committed
  305. * to the log as setting the XFS_BLI_INODE_BUF flag will prevent
  306. * correct replay of the inode allocation.
  307. *
  308. * For icreate item based inode allocation, the buffers aren't written
  309. * to the journal during allocation, and hence we should always tag the
  310. * buffer as an inode buffer so that the correct unlinked list replay
  311. * occurs during recovery.
  312. */
  313. if (bip->bli_flags & XFS_BLI_INODE_BUF) {
  314. if (xfs_sb_version_hascrc(&lip->li_mountp->m_sb) ||
  315. !((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
  316. xfs_log_item_in_current_chkpt(lip)))
  317. bip->__bli_format.blf_flags |= XFS_BLF_INODE_BUF;
  318. bip->bli_flags &= ~XFS_BLI_INODE_BUF;
  319. }
  320. if ((bip->bli_flags & (XFS_BLI_ORDERED|XFS_BLI_STALE)) ==
  321. XFS_BLI_ORDERED) {
  322. /*
  323. * The buffer has been logged just to order it. It is not being
  324. * included in the transaction commit, so don't format it.
  325. */
  326. trace_xfs_buf_item_format_ordered(bip);
  327. return;
  328. }
  329. for (i = 0; i < bip->bli_format_count; i++) {
  330. xfs_buf_item_format_segment(bip, lv, &vecp, offset,
  331. &bip->bli_formats[i]);
  332. offset += bp->b_maps[i].bm_len;
  333. }
  334. /*
  335. * Check to make sure everything is consistent.
  336. */
  337. trace_xfs_buf_item_format(bip);
  338. }
  339. /*
  340. * This is called to pin the buffer associated with the buf log item in memory
  341. * so it cannot be written out.
  342. *
  343. * We also always take a reference to the buffer log item here so that the bli
  344. * is held while the item is pinned in memory. This means that we can
  345. * unconditionally drop the reference count a transaction holds when the
  346. * transaction is completed.
  347. */
  348. STATIC void
  349. xfs_buf_item_pin(
  350. struct xfs_log_item *lip)
  351. {
  352. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  353. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  354. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  355. (bip->bli_flags & XFS_BLI_ORDERED) ||
  356. (bip->bli_flags & XFS_BLI_STALE));
  357. trace_xfs_buf_item_pin(bip);
  358. atomic_inc(&bip->bli_refcount);
  359. atomic_inc(&bip->bli_buf->b_pin_count);
  360. }
  361. /*
  362. * This is called to unpin the buffer associated with the buf log
  363. * item which was previously pinned with a call to xfs_buf_item_pin().
  364. *
  365. * Also drop the reference to the buf item for the current transaction.
  366. * If the XFS_BLI_STALE flag is set and we are the last reference,
  367. * then free up the buf log item and unlock the buffer.
  368. *
  369. * If the remove flag is set we are called from uncommit in the
  370. * forced-shutdown path. If that is true and the reference count on
  371. * the log item is going to drop to zero we need to free the item's
  372. * descriptor in the transaction.
  373. */
  374. STATIC void
  375. xfs_buf_item_unpin(
  376. struct xfs_log_item *lip,
  377. int remove)
  378. {
  379. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  380. xfs_buf_t *bp = bip->bli_buf;
  381. struct xfs_ail *ailp = lip->li_ailp;
  382. int stale = bip->bli_flags & XFS_BLI_STALE;
  383. int freed;
  384. ASSERT(bp->b_fspriv == bip);
  385. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  386. trace_xfs_buf_item_unpin(bip);
  387. freed = atomic_dec_and_test(&bip->bli_refcount);
  388. if (atomic_dec_and_test(&bp->b_pin_count))
  389. wake_up_all(&bp->b_waiters);
  390. if (freed && stale) {
  391. ASSERT(bip->bli_flags & XFS_BLI_STALE);
  392. ASSERT(xfs_buf_islocked(bp));
  393. ASSERT(XFS_BUF_ISSTALE(bp));
  394. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  395. trace_xfs_buf_item_unpin_stale(bip);
  396. if (remove) {
  397. /*
  398. * If we are in a transaction context, we have to
  399. * remove the log item from the transaction as we are
  400. * about to release our reference to the buffer. If we
  401. * don't, the unlock that occurs later in
  402. * xfs_trans_uncommit() will try to reference the
  403. * buffer which we no longer have a hold on.
  404. */
  405. if (lip->li_desc)
  406. xfs_trans_del_item(lip);
  407. /*
  408. * Since the transaction no longer refers to the buffer,
  409. * the buffer should no longer refer to the transaction.
  410. */
  411. bp->b_transp = NULL;
  412. }
  413. /*
  414. * If we get called here because of an IO error, we may
  415. * or may not have the item on the AIL. xfs_trans_ail_delete()
  416. * will take care of that situation.
  417. * xfs_trans_ail_delete() drops the AIL lock.
  418. */
  419. if (bip->bli_flags & XFS_BLI_STALE_INODE) {
  420. xfs_buf_do_callbacks(bp);
  421. bp->b_fspriv = NULL;
  422. bp->b_iodone = NULL;
  423. } else {
  424. spin_lock(&ailp->xa_lock);
  425. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_LOG_IO_ERROR);
  426. xfs_buf_item_relse(bp);
  427. ASSERT(bp->b_fspriv == NULL);
  428. }
  429. xfs_buf_relse(bp);
  430. } else if (freed && remove) {
  431. /*
  432. * There are currently two references to the buffer - the active
  433. * LRU reference and the buf log item. What we are about to do
  434. * here - simulate a failed IO completion - requires 3
  435. * references.
  436. *
  437. * The LRU reference is removed by the xfs_buf_stale() call. The
  438. * buf item reference is removed by the xfs_buf_iodone()
  439. * callback that is run by xfs_buf_do_callbacks() during ioend
  440. * processing (via the bp->b_iodone callback), and then finally
  441. * the ioend processing will drop the IO reference if the buffer
  442. * is marked XBF_ASYNC.
  443. *
  444. * Hence we need to take an additional reference here so that IO
  445. * completion processing doesn't free the buffer prematurely.
  446. */
  447. xfs_buf_lock(bp);
  448. xfs_buf_hold(bp);
  449. bp->b_flags |= XBF_ASYNC;
  450. xfs_buf_ioerror(bp, -EIO);
  451. XFS_BUF_UNDONE(bp);
  452. xfs_buf_stale(bp);
  453. xfs_buf_ioend(bp);
  454. }
  455. }
  456. /*
  457. * Buffer IO error rate limiting. Limit it to no more than 10 messages per 30
  458. * seconds so as to not spam logs too much on repeated detection of the same
  459. * buffer being bad..
  460. */
  461. static DEFINE_RATELIMIT_STATE(xfs_buf_write_fail_rl_state, 30 * HZ, 10);
  462. STATIC uint
  463. xfs_buf_item_push(
  464. struct xfs_log_item *lip,
  465. struct list_head *buffer_list)
  466. {
  467. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  468. struct xfs_buf *bp = bip->bli_buf;
  469. uint rval = XFS_ITEM_SUCCESS;
  470. if (xfs_buf_ispinned(bp))
  471. return XFS_ITEM_PINNED;
  472. if (!xfs_buf_trylock(bp)) {
  473. /*
  474. * If we have just raced with a buffer being pinned and it has
  475. * been marked stale, we could end up stalling until someone else
  476. * issues a log force to unpin the stale buffer. Check for the
  477. * race condition here so xfsaild recognizes the buffer is pinned
  478. * and queues a log force to move it along.
  479. */
  480. if (xfs_buf_ispinned(bp))
  481. return XFS_ITEM_PINNED;
  482. return XFS_ITEM_LOCKED;
  483. }
  484. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  485. trace_xfs_buf_item_push(bip);
  486. /* has a previous flush failed due to IO errors? */
  487. if ((bp->b_flags & XBF_WRITE_FAIL) &&
  488. ___ratelimit(&xfs_buf_write_fail_rl_state, "XFS: Failing async write")) {
  489. xfs_warn(bp->b_target->bt_mount,
  490. "Failing async write on buffer block 0x%llx. Retrying async write.",
  491. (long long)bp->b_bn);
  492. }
  493. if (!xfs_buf_delwri_queue(bp, buffer_list))
  494. rval = XFS_ITEM_FLUSHING;
  495. xfs_buf_unlock(bp);
  496. return rval;
  497. }
  498. /*
  499. * Release the buffer associated with the buf log item. If there is no dirty
  500. * logged data associated with the buffer recorded in the buf log item, then
  501. * free the buf log item and remove the reference to it in the buffer.
  502. *
  503. * This call ignores the recursion count. It is only called when the buffer
  504. * should REALLY be unlocked, regardless of the recursion count.
  505. *
  506. * We unconditionally drop the transaction's reference to the log item. If the
  507. * item was logged, then another reference was taken when it was pinned, so we
  508. * can safely drop the transaction reference now. This also allows us to avoid
  509. * potential races with the unpin code freeing the bli by not referencing the
  510. * bli after we've dropped the reference count.
  511. *
  512. * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
  513. * if necessary but do not unlock the buffer. This is for support of
  514. * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
  515. * free the item.
  516. */
  517. STATIC void
  518. xfs_buf_item_unlock(
  519. struct xfs_log_item *lip)
  520. {
  521. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  522. struct xfs_buf *bp = bip->bli_buf;
  523. bool clean;
  524. bool aborted;
  525. int flags;
  526. /* Clear the buffer's association with this transaction. */
  527. bp->b_transp = NULL;
  528. /*
  529. * If this is a transaction abort, don't return early. Instead, allow
  530. * the brelse to happen. Normally it would be done for stale
  531. * (cancelled) buffers at unpin time, but we'll never go through the
  532. * pin/unpin cycle if we abort inside commit.
  533. */
  534. aborted = (lip->li_flags & XFS_LI_ABORTED) ? true : false;
  535. /*
  536. * Before possibly freeing the buf item, copy the per-transaction state
  537. * so we can reference it safely later after clearing it from the
  538. * buffer log item.
  539. */
  540. flags = bip->bli_flags;
  541. bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD | XFS_BLI_ORDERED);
  542. /*
  543. * If the buf item is marked stale, then don't do anything. We'll
  544. * unlock the buffer and free the buf item when the buffer is unpinned
  545. * for the last time.
  546. */
  547. if (flags & XFS_BLI_STALE) {
  548. trace_xfs_buf_item_unlock_stale(bip);
  549. ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
  550. if (!aborted) {
  551. atomic_dec(&bip->bli_refcount);
  552. return;
  553. }
  554. }
  555. trace_xfs_buf_item_unlock(bip);
  556. /*
  557. * If the buf item isn't tracking any data, free it, otherwise drop the
  558. * reference we hold to it. If we are aborting the transaction, this may
  559. * be the only reference to the buf item, so we free it anyway
  560. * regardless of whether it is dirty or not. A dirty abort implies a
  561. * shutdown, anyway.
  562. *
  563. * Ordered buffers are dirty but may have no recorded changes, so ensure
  564. * we only release clean items here.
  565. */
  566. clean = (flags & XFS_BLI_DIRTY) ? false : true;
  567. if (clean) {
  568. int i;
  569. for (i = 0; i < bip->bli_format_count; i++) {
  570. if (!xfs_bitmap_empty(bip->bli_formats[i].blf_data_map,
  571. bip->bli_formats[i].blf_map_size)) {
  572. clean = false;
  573. break;
  574. }
  575. }
  576. }
  577. /*
  578. * Clean buffers, by definition, cannot be in the AIL. However, aborted
  579. * buffers may be dirty and hence in the AIL. Therefore if we are
  580. * aborting a buffer and we've just taken the last refernce away, we
  581. * have to check if it is in the AIL before freeing it. We need to free
  582. * it in this case, because an aborted transaction has already shut the
  583. * filesystem down and this is the last chance we will have to do so.
  584. */
  585. if (atomic_dec_and_test(&bip->bli_refcount)) {
  586. if (clean)
  587. xfs_buf_item_relse(bp);
  588. else if (aborted) {
  589. ASSERT(XFS_FORCED_SHUTDOWN(lip->li_mountp));
  590. if (lip->li_flags & XFS_LI_IN_AIL) {
  591. spin_lock(&lip->li_ailp->xa_lock);
  592. xfs_trans_ail_delete(lip->li_ailp, lip,
  593. SHUTDOWN_LOG_IO_ERROR);
  594. }
  595. xfs_buf_item_relse(bp);
  596. }
  597. }
  598. if (!(flags & XFS_BLI_HOLD))
  599. xfs_buf_relse(bp);
  600. }
  601. /*
  602. * This is called to find out where the oldest active copy of the
  603. * buf log item in the on disk log resides now that the last log
  604. * write of it completed at the given lsn.
  605. * We always re-log all the dirty data in a buffer, so usually the
  606. * latest copy in the on disk log is the only one that matters. For
  607. * those cases we simply return the given lsn.
  608. *
  609. * The one exception to this is for buffers full of newly allocated
  610. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  611. * flag set, indicating that only the di_next_unlinked fields from the
  612. * inodes in the buffers will be replayed during recovery. If the
  613. * original newly allocated inode images have not yet been flushed
  614. * when the buffer is so relogged, then we need to make sure that we
  615. * keep the old images in the 'active' portion of the log. We do this
  616. * by returning the original lsn of that transaction here rather than
  617. * the current one.
  618. */
  619. STATIC xfs_lsn_t
  620. xfs_buf_item_committed(
  621. struct xfs_log_item *lip,
  622. xfs_lsn_t lsn)
  623. {
  624. struct xfs_buf_log_item *bip = BUF_ITEM(lip);
  625. trace_xfs_buf_item_committed(bip);
  626. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
  627. return lip->li_lsn;
  628. return lsn;
  629. }
  630. STATIC void
  631. xfs_buf_item_committing(
  632. struct xfs_log_item *lip,
  633. xfs_lsn_t commit_lsn)
  634. {
  635. }
  636. /*
  637. * This is the ops vector shared by all buf log items.
  638. */
  639. static const struct xfs_item_ops xfs_buf_item_ops = {
  640. .iop_size = xfs_buf_item_size,
  641. .iop_format = xfs_buf_item_format,
  642. .iop_pin = xfs_buf_item_pin,
  643. .iop_unpin = xfs_buf_item_unpin,
  644. .iop_unlock = xfs_buf_item_unlock,
  645. .iop_committed = xfs_buf_item_committed,
  646. .iop_push = xfs_buf_item_push,
  647. .iop_committing = xfs_buf_item_committing
  648. };
  649. STATIC int
  650. xfs_buf_item_get_format(
  651. struct xfs_buf_log_item *bip,
  652. int count)
  653. {
  654. ASSERT(bip->bli_formats == NULL);
  655. bip->bli_format_count = count;
  656. if (count == 1) {
  657. bip->bli_formats = &bip->__bli_format;
  658. return 0;
  659. }
  660. bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
  661. KM_SLEEP);
  662. if (!bip->bli_formats)
  663. return -ENOMEM;
  664. return 0;
  665. }
  666. STATIC void
  667. xfs_buf_item_free_format(
  668. struct xfs_buf_log_item *bip)
  669. {
  670. if (bip->bli_formats != &bip->__bli_format) {
  671. kmem_free(bip->bli_formats);
  672. bip->bli_formats = NULL;
  673. }
  674. }
  675. /*
  676. * Allocate a new buf log item to go with the given buffer.
  677. * Set the buffer's b_fsprivate field to point to the new
  678. * buf log item. If there are other item's attached to the
  679. * buffer (see xfs_buf_attach_iodone() below), then put the
  680. * buf log item at the front.
  681. */
  682. void
  683. xfs_buf_item_init(
  684. xfs_buf_t *bp,
  685. xfs_mount_t *mp)
  686. {
  687. xfs_log_item_t *lip = bp->b_fspriv;
  688. xfs_buf_log_item_t *bip;
  689. int chunks;
  690. int map_size;
  691. int error;
  692. int i;
  693. /*
  694. * Check to see if there is already a buf log item for
  695. * this buffer. If there is, it is guaranteed to be
  696. * the first. If we do already have one, there is
  697. * nothing to do here so return.
  698. */
  699. ASSERT(bp->b_target->bt_mount == mp);
  700. if (lip != NULL && lip->li_type == XFS_LI_BUF)
  701. return;
  702. bip = kmem_zone_zalloc(xfs_buf_item_zone, KM_SLEEP);
  703. xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
  704. bip->bli_buf = bp;
  705. xfs_buf_hold(bp);
  706. /*
  707. * chunks is the number of XFS_BLF_CHUNK size pieces the buffer
  708. * can be divided into. Make sure not to truncate any pieces.
  709. * map_size is the size of the bitmap needed to describe the
  710. * chunks of the buffer.
  711. *
  712. * Discontiguous buffer support follows the layout of the underlying
  713. * buffer. This makes the implementation as simple as possible.
  714. */
  715. error = xfs_buf_item_get_format(bip, bp->b_map_count);
  716. ASSERT(error == 0);
  717. for (i = 0; i < bip->bli_format_count; i++) {
  718. chunks = DIV_ROUND_UP(BBTOB(bp->b_maps[i].bm_len),
  719. XFS_BLF_CHUNK);
  720. map_size = DIV_ROUND_UP(chunks, NBWORD);
  721. bip->bli_formats[i].blf_type = XFS_LI_BUF;
  722. bip->bli_formats[i].blf_blkno = bp->b_maps[i].bm_bn;
  723. bip->bli_formats[i].blf_len = bp->b_maps[i].bm_len;
  724. bip->bli_formats[i].blf_map_size = map_size;
  725. }
  726. /*
  727. * Put the buf item into the list of items attached to the
  728. * buffer at the front.
  729. */
  730. if (bp->b_fspriv)
  731. bip->bli_item.li_bio_list = bp->b_fspriv;
  732. bp->b_fspriv = bip;
  733. }
  734. /*
  735. * Mark bytes first through last inclusive as dirty in the buf
  736. * item's bitmap.
  737. */
  738. static void
  739. xfs_buf_item_log_segment(
  740. uint first,
  741. uint last,
  742. uint *map)
  743. {
  744. uint first_bit;
  745. uint last_bit;
  746. uint bits_to_set;
  747. uint bits_set;
  748. uint word_num;
  749. uint *wordp;
  750. uint bit;
  751. uint end_bit;
  752. uint mask;
  753. /*
  754. * Convert byte offsets to bit numbers.
  755. */
  756. first_bit = first >> XFS_BLF_SHIFT;
  757. last_bit = last >> XFS_BLF_SHIFT;
  758. /*
  759. * Calculate the total number of bits to be set.
  760. */
  761. bits_to_set = last_bit - first_bit + 1;
  762. /*
  763. * Get a pointer to the first word in the bitmap
  764. * to set a bit in.
  765. */
  766. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  767. wordp = &map[word_num];
  768. /*
  769. * Calculate the starting bit in the first word.
  770. */
  771. bit = first_bit & (uint)(NBWORD - 1);
  772. /*
  773. * First set any bits in the first word of our range.
  774. * If it starts at bit 0 of the word, it will be
  775. * set below rather than here. That is what the variable
  776. * bit tells us. The variable bits_set tracks the number
  777. * of bits that have been set so far. End_bit is the number
  778. * of the last bit to be set in this word plus one.
  779. */
  780. if (bit) {
  781. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  782. mask = ((1 << (end_bit - bit)) - 1) << bit;
  783. *wordp |= mask;
  784. wordp++;
  785. bits_set = end_bit - bit;
  786. } else {
  787. bits_set = 0;
  788. }
  789. /*
  790. * Now set bits a whole word at a time that are between
  791. * first_bit and last_bit.
  792. */
  793. while ((bits_to_set - bits_set) >= NBWORD) {
  794. *wordp |= 0xffffffff;
  795. bits_set += NBWORD;
  796. wordp++;
  797. }
  798. /*
  799. * Finally, set any bits left to be set in one last partial word.
  800. */
  801. end_bit = bits_to_set - bits_set;
  802. if (end_bit) {
  803. mask = (1 << end_bit) - 1;
  804. *wordp |= mask;
  805. }
  806. }
  807. /*
  808. * Mark bytes first through last inclusive as dirty in the buf
  809. * item's bitmap.
  810. */
  811. void
  812. xfs_buf_item_log(
  813. xfs_buf_log_item_t *bip,
  814. uint first,
  815. uint last)
  816. {
  817. int i;
  818. uint start;
  819. uint end;
  820. struct xfs_buf *bp = bip->bli_buf;
  821. /*
  822. * walk each buffer segment and mark them dirty appropriately.
  823. */
  824. start = 0;
  825. for (i = 0; i < bip->bli_format_count; i++) {
  826. if (start > last)
  827. break;
  828. end = start + BBTOB(bp->b_maps[i].bm_len);
  829. if (first > end) {
  830. start += BBTOB(bp->b_maps[i].bm_len);
  831. continue;
  832. }
  833. if (first < start)
  834. first = start;
  835. if (end > last)
  836. end = last;
  837. xfs_buf_item_log_segment(first, end,
  838. &bip->bli_formats[i].blf_data_map[0]);
  839. start += bp->b_maps[i].bm_len;
  840. }
  841. }
  842. /*
  843. * Return 1 if the buffer has been logged or ordered in a transaction (at any
  844. * point, not just the current transaction) and 0 if not.
  845. */
  846. uint
  847. xfs_buf_item_dirty(
  848. xfs_buf_log_item_t *bip)
  849. {
  850. return (bip->bli_flags & XFS_BLI_DIRTY);
  851. }
  852. STATIC void
  853. xfs_buf_item_free(
  854. xfs_buf_log_item_t *bip)
  855. {
  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 = bp->b_fspriv;
  871. trace_xfs_buf_item_relse(bp, _RET_IP_);
  872. ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
  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(!bp->b_error))
  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. * errors 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 (!(bp->b_flags & (XBF_STALE|XBF_WRITE_FAIL))) {
  987. bp->b_flags |= XBF_WRITE | XBF_ASYNC |
  988. XBF_DONE | XBF_WRITE_FAIL;
  989. xfs_buf_submit(bp);
  990. } else {
  991. xfs_buf_relse(bp);
  992. }
  993. return;
  994. }
  995. /*
  996. * If the write of the buffer was synchronous, we want to make
  997. * sure to return the error to the caller of xfs_bwrite().
  998. */
  999. xfs_buf_stale(bp);
  1000. XFS_BUF_DONE(bp);
  1001. trace_xfs_buf_error_relse(bp, _RET_IP_);
  1002. do_callbacks:
  1003. xfs_buf_do_callbacks(bp);
  1004. bp->b_fspriv = NULL;
  1005. bp->b_iodone = NULL;
  1006. xfs_buf_ioend(bp);
  1007. }
  1008. /*
  1009. * This is the iodone() function for buffers which have been
  1010. * logged. It is called when they are eventually flushed out.
  1011. * It should remove the buf item from the AIL, and free the buf item.
  1012. * It is called by xfs_buf_iodone_callbacks() above which will take
  1013. * care of cleaning up the buffer itself.
  1014. */
  1015. void
  1016. xfs_buf_iodone(
  1017. struct xfs_buf *bp,
  1018. struct xfs_log_item *lip)
  1019. {
  1020. struct xfs_ail *ailp = lip->li_ailp;
  1021. ASSERT(BUF_ITEM(lip)->bli_buf == bp);
  1022. xfs_buf_rele(bp);
  1023. /*
  1024. * If we are forcibly shutting down, this may well be
  1025. * off the AIL already. That's because we simulate the
  1026. * log-committed callbacks to unpin these buffers. Or we may never
  1027. * have put this item on AIL because of the transaction was
  1028. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  1029. *
  1030. * Either way, AIL is useless if we're forcing a shutdown.
  1031. */
  1032. spin_lock(&ailp->xa_lock);
  1033. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  1034. xfs_buf_item_free(BUF_ITEM(lip));
  1035. }