xfs_buf_item.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dmapi.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_buf_item.h"
  30. #include "xfs_trans_priv.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. kmem_zone_t *xfs_buf_item_zone;
  34. #ifdef XFS_TRANS_DEBUG
  35. /*
  36. * This function uses an alternate strategy for tracking the bytes
  37. * that the user requests to be logged. This can then be used
  38. * in conjunction with the bli_orig array in the buf log item to
  39. * catch bugs in our callers' code.
  40. *
  41. * We also double check the bits set in xfs_buf_item_log using a
  42. * simple algorithm to check that every byte is accounted for.
  43. */
  44. STATIC void
  45. xfs_buf_item_log_debug(
  46. xfs_buf_log_item_t *bip,
  47. uint first,
  48. uint last)
  49. {
  50. uint x;
  51. uint byte;
  52. uint nbytes;
  53. uint chunk_num;
  54. uint word_num;
  55. uint bit_num;
  56. uint bit_set;
  57. uint *wordp;
  58. ASSERT(bip->bli_logged != NULL);
  59. byte = first;
  60. nbytes = last - first + 1;
  61. bfset(bip->bli_logged, first, nbytes);
  62. for (x = 0; x < nbytes; x++) {
  63. chunk_num = byte >> XFS_BLF_SHIFT;
  64. word_num = chunk_num >> BIT_TO_WORD_SHIFT;
  65. bit_num = chunk_num & (NBWORD - 1);
  66. wordp = &(bip->bli_format.blf_data_map[word_num]);
  67. bit_set = *wordp & (1 << bit_num);
  68. ASSERT(bit_set);
  69. byte++;
  70. }
  71. }
  72. /*
  73. * This function is called when we flush something into a buffer without
  74. * logging it. This happens for things like inodes which are logged
  75. * separately from the buffer.
  76. */
  77. void
  78. xfs_buf_item_flush_log_debug(
  79. xfs_buf_t *bp,
  80. uint first,
  81. uint last)
  82. {
  83. xfs_buf_log_item_t *bip;
  84. uint nbytes;
  85. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
  86. if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) {
  87. return;
  88. }
  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(XFS_BUF_COUNT(bp) > 0);
  114. ASSERT(XFS_BUF_PTR(bp) != NULL);
  115. orig = bip->bli_orig;
  116. buffer = XFS_BUF_PTR(bp);
  117. for (x = 0; x < XFS_BUF_COUNT(bp); x++) {
  118. if (orig[x] != buffer[x] && !btst(bip->bli_logged, x))
  119. cmn_err(CE_PANIC,
  120. "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
  121. bip, bp, orig, x);
  122. }
  123. }
  124. #else
  125. #define xfs_buf_item_log_debug(x,y,z)
  126. #define xfs_buf_item_log_check(x)
  127. #endif
  128. STATIC void xfs_buf_error_relse(xfs_buf_t *bp);
  129. STATIC void xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip);
  130. /*
  131. * This returns the number of log iovecs needed to log the
  132. * given buf log item.
  133. *
  134. * It calculates this as 1 iovec for the buf log format structure
  135. * and 1 for each stretch of non-contiguous chunks to be logged.
  136. * Contiguous chunks are logged in a single iovec.
  137. *
  138. * If the XFS_BLI_STALE flag has been set, then log nothing.
  139. */
  140. STATIC uint
  141. xfs_buf_item_size(
  142. xfs_buf_log_item_t *bip)
  143. {
  144. uint nvecs;
  145. int next_bit;
  146. int last_bit;
  147. xfs_buf_t *bp;
  148. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  149. if (bip->bli_flags & XFS_BLI_STALE) {
  150. /*
  151. * The buffer is stale, so all we need to log
  152. * is the buf log format structure with the
  153. * cancel flag in it.
  154. */
  155. trace_xfs_buf_item_size_stale(bip);
  156. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  157. return 1;
  158. }
  159. bp = bip->bli_buf;
  160. ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
  161. nvecs = 1;
  162. last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  163. bip->bli_format.blf_map_size, 0);
  164. ASSERT(last_bit != -1);
  165. nvecs++;
  166. while (last_bit != -1) {
  167. /*
  168. * This takes the bit number to start looking from and
  169. * returns the next set bit from there. It returns -1
  170. * if there are no more bits set or the start bit is
  171. * beyond the end of the bitmap.
  172. */
  173. next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  174. bip->bli_format.blf_map_size,
  175. last_bit + 1);
  176. /*
  177. * If we run out of bits, leave the loop,
  178. * else if we find a new set of bits bump the number of vecs,
  179. * else keep scanning the current set of bits.
  180. */
  181. if (next_bit == -1) {
  182. last_bit = -1;
  183. } else if (next_bit != last_bit + 1) {
  184. last_bit = next_bit;
  185. nvecs++;
  186. } else if (xfs_buf_offset(bp, next_bit * XFS_BLF_CHUNK) !=
  187. (xfs_buf_offset(bp, last_bit * XFS_BLF_CHUNK) +
  188. XFS_BLF_CHUNK)) {
  189. last_bit = next_bit;
  190. nvecs++;
  191. } else {
  192. last_bit++;
  193. }
  194. }
  195. trace_xfs_buf_item_size(bip);
  196. return nvecs;
  197. }
  198. /*
  199. * This is called to fill in the vector of log iovecs for the
  200. * given log buf item. It fills the first entry with a buf log
  201. * format structure, and the rest point to contiguous chunks
  202. * within the buffer.
  203. */
  204. STATIC void
  205. xfs_buf_item_format(
  206. xfs_buf_log_item_t *bip,
  207. xfs_log_iovec_t *log_vector)
  208. {
  209. uint base_size;
  210. uint nvecs;
  211. xfs_log_iovec_t *vecp;
  212. xfs_buf_t *bp;
  213. int first_bit;
  214. int last_bit;
  215. int next_bit;
  216. uint nbits;
  217. uint buffer_offset;
  218. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  219. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  220. (bip->bli_flags & XFS_BLI_STALE));
  221. bp = bip->bli_buf;
  222. vecp = log_vector;
  223. /*
  224. * The size of the base structure is the size of the
  225. * declared structure plus the space for the extra words
  226. * of the bitmap. We subtract one from the map size, because
  227. * the first element of the bitmap is accounted for in the
  228. * size of the base structure.
  229. */
  230. base_size =
  231. (uint)(sizeof(xfs_buf_log_format_t) +
  232. ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
  233. vecp->i_addr = (xfs_caddr_t)&bip->bli_format;
  234. vecp->i_len = base_size;
  235. vecp->i_type = XLOG_REG_TYPE_BFORMAT;
  236. vecp++;
  237. nvecs = 1;
  238. if (bip->bli_flags & XFS_BLI_STALE) {
  239. /*
  240. * The buffer is stale, so all we need to log
  241. * is the buf log format structure with the
  242. * cancel flag in it.
  243. */
  244. trace_xfs_buf_item_format_stale(bip);
  245. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  246. bip->bli_format.blf_size = nvecs;
  247. return;
  248. }
  249. /*
  250. * Fill in an iovec for each set of contiguous chunks.
  251. */
  252. first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  253. bip->bli_format.blf_map_size, 0);
  254. ASSERT(first_bit != -1);
  255. last_bit = first_bit;
  256. nbits = 1;
  257. for (;;) {
  258. /*
  259. * This takes the bit number to start looking from and
  260. * returns the next set bit from there. It returns -1
  261. * if there are no more bits set or the start bit is
  262. * beyond the end of the bitmap.
  263. */
  264. next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
  265. bip->bli_format.blf_map_size,
  266. (uint)last_bit + 1);
  267. /*
  268. * If we run out of bits fill in the last iovec and get
  269. * out of the loop.
  270. * Else if we start a new set of bits then fill in the
  271. * iovec for the series we were looking at and start
  272. * counting the bits in the new one.
  273. * Else we're still in the same set of bits so just
  274. * keep counting and scanning.
  275. */
  276. if (next_bit == -1) {
  277. buffer_offset = first_bit * XFS_BLF_CHUNK;
  278. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  279. vecp->i_len = nbits * XFS_BLF_CHUNK;
  280. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  281. nvecs++;
  282. break;
  283. } else if (next_bit != last_bit + 1) {
  284. buffer_offset = first_bit * XFS_BLF_CHUNK;
  285. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  286. vecp->i_len = nbits * XFS_BLF_CHUNK;
  287. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  288. nvecs++;
  289. vecp++;
  290. first_bit = next_bit;
  291. last_bit = next_bit;
  292. nbits = 1;
  293. } else if (xfs_buf_offset(bp, next_bit << XFS_BLF_SHIFT) !=
  294. (xfs_buf_offset(bp, last_bit << XFS_BLF_SHIFT) +
  295. XFS_BLF_CHUNK)) {
  296. buffer_offset = first_bit * XFS_BLF_CHUNK;
  297. vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
  298. vecp->i_len = nbits * XFS_BLF_CHUNK;
  299. vecp->i_type = XLOG_REG_TYPE_BCHUNK;
  300. /* You would think we need to bump the nvecs here too, but we do not
  301. * this number is used by recovery, and it gets confused by the boundary
  302. * split here
  303. * nvecs++;
  304. */
  305. vecp++;
  306. first_bit = next_bit;
  307. last_bit = next_bit;
  308. nbits = 1;
  309. } else {
  310. last_bit++;
  311. nbits++;
  312. }
  313. }
  314. bip->bli_format.blf_size = nvecs;
  315. /*
  316. * Check to make sure everything is consistent.
  317. */
  318. trace_xfs_buf_item_format(bip);
  319. xfs_buf_item_log_check(bip);
  320. }
  321. /*
  322. * This is called to pin the buffer associated with the buf log item in memory
  323. * so it cannot be written out. Simply call bpin() on the buffer to do this.
  324. *
  325. * We also always take a reference to the buffer log item here so that the bli
  326. * is held while the item is pinned in memory. This means that we can
  327. * unconditionally drop the reference count a transaction holds when the
  328. * transaction is completed.
  329. */
  330. STATIC void
  331. xfs_buf_item_pin(
  332. xfs_buf_log_item_t *bip)
  333. {
  334. xfs_buf_t *bp;
  335. bp = bip->bli_buf;
  336. ASSERT(XFS_BUF_ISBUSY(bp));
  337. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  338. ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  339. (bip->bli_flags & XFS_BLI_STALE));
  340. atomic_inc(&bip->bli_refcount);
  341. trace_xfs_buf_item_pin(bip);
  342. xfs_bpin(bp);
  343. }
  344. /*
  345. * This is called to unpin the buffer associated with the buf log
  346. * item which was previously pinned with a call to xfs_buf_item_pin().
  347. * Just call bunpin() on the buffer to do this.
  348. *
  349. * Also drop the reference to the buf item for the current transaction.
  350. * If the XFS_BLI_STALE flag is set and we are the last reference,
  351. * then free up the buf log item and unlock the buffer.
  352. */
  353. STATIC void
  354. xfs_buf_item_unpin(
  355. xfs_buf_log_item_t *bip)
  356. {
  357. struct xfs_ail *ailp;
  358. xfs_buf_t *bp;
  359. int freed;
  360. int stale = bip->bli_flags & XFS_BLI_STALE;
  361. bp = bip->bli_buf;
  362. ASSERT(bp != NULL);
  363. ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip);
  364. ASSERT(atomic_read(&bip->bli_refcount) > 0);
  365. trace_xfs_buf_item_unpin(bip);
  366. freed = atomic_dec_and_test(&bip->bli_refcount);
  367. ailp = bip->bli_item.li_ailp;
  368. xfs_bunpin(bp);
  369. if (freed && stale) {
  370. ASSERT(bip->bli_flags & XFS_BLI_STALE);
  371. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  372. ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
  373. ASSERT(XFS_BUF_ISSTALE(bp));
  374. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  375. trace_xfs_buf_item_unpin_stale(bip);
  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, (xfs_log_item_t *)bip);
  384. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  385. XFS_BUF_CLR_IODONE_FUNC(bp);
  386. } else {
  387. spin_lock(&ailp->xa_lock);
  388. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)bip);
  389. xfs_buf_item_relse(bp);
  390. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL);
  391. }
  392. xfs_buf_relse(bp);
  393. }
  394. }
  395. /*
  396. * this is called from uncommit in the forced-shutdown path.
  397. * we need to check to see if the reference count on the log item
  398. * is going to drop to zero. If so, unpin will free the log item
  399. * so we need to free the item's descriptor (that points to the item)
  400. * in the transaction.
  401. */
  402. STATIC void
  403. xfs_buf_item_unpin_remove(
  404. xfs_buf_log_item_t *bip,
  405. xfs_trans_t *tp)
  406. {
  407. /* will xfs_buf_item_unpin() call xfs_buf_item_relse()? */
  408. if ((atomic_read(&bip->bli_refcount) == 1) &&
  409. (bip->bli_flags & XFS_BLI_STALE)) {
  410. /*
  411. * yes -- We can safely do some work here and then call
  412. * buf_item_unpin to do the rest because we are
  413. * are holding the buffer locked so no one else will be
  414. * able to bump up the refcount. We have to remove the
  415. * log item from the transaction as we are about to release
  416. * our reference to the buffer. If we don't, the unlock that
  417. * occurs later in the xfs_trans_uncommit() will try to
  418. * reference the buffer which we no longer have a hold on.
  419. */
  420. struct xfs_log_item_desc *lidp;
  421. ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0);
  422. trace_xfs_buf_item_unpin_stale(bip);
  423. lidp = xfs_trans_find_item(tp, (xfs_log_item_t *)bip);
  424. xfs_trans_free_item(tp, lidp);
  425. /*
  426. * Since the transaction no longer refers to the buffer, the
  427. * buffer should no longer refer to the transaction.
  428. */
  429. XFS_BUF_SET_FSPRIVATE2(bip->bli_buf, NULL);
  430. }
  431. xfs_buf_item_unpin(bip);
  432. }
  433. /*
  434. * This is called to attempt to lock the buffer associated with this
  435. * buf log item. Don't sleep on the buffer lock. If we can't get
  436. * the lock right away, return 0. If we can get the lock, take a
  437. * reference to the buffer. If this is a delayed write buffer that
  438. * needs AIL help to be written back, invoke the pushbuf routine
  439. * rather than the normal success path.
  440. */
  441. STATIC uint
  442. xfs_buf_item_trylock(
  443. xfs_buf_log_item_t *bip)
  444. {
  445. xfs_buf_t *bp;
  446. bp = bip->bli_buf;
  447. if (XFS_BUF_ISPINNED(bp))
  448. return XFS_ITEM_PINNED;
  449. if (!XFS_BUF_CPSEMA(bp))
  450. return XFS_ITEM_LOCKED;
  451. /* take a reference to the buffer. */
  452. XFS_BUF_HOLD(bp);
  453. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  454. trace_xfs_buf_item_trylock(bip);
  455. if (XFS_BUF_ISDELAYWRITE(bp))
  456. return XFS_ITEM_PUSHBUF;
  457. return XFS_ITEM_SUCCESS;
  458. }
  459. /*
  460. * Release the buffer associated with the buf log item. If there is no dirty
  461. * logged data associated with the buffer recorded in the buf log item, then
  462. * free the buf log item and remove the reference to it in the buffer.
  463. *
  464. * This call ignores the recursion count. It is only called when the buffer
  465. * should REALLY be unlocked, regardless of the recursion count.
  466. *
  467. * We unconditionally drop the transaction's reference to the log item. If the
  468. * item was logged, then another reference was taken when it was pinned, so we
  469. * can safely drop the transaction reference now. This also allows us to avoid
  470. * potential races with the unpin code freeing the bli by not referencing the
  471. * bli after we've dropped the reference count.
  472. *
  473. * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
  474. * if necessary but do not unlock the buffer. This is for support of
  475. * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
  476. * free the item.
  477. */
  478. STATIC void
  479. xfs_buf_item_unlock(
  480. xfs_buf_log_item_t *bip)
  481. {
  482. int aborted;
  483. xfs_buf_t *bp;
  484. uint hold;
  485. bp = bip->bli_buf;
  486. /* Clear the buffer's association with this transaction. */
  487. XFS_BUF_SET_FSPRIVATE2(bp, NULL);
  488. /*
  489. * If this is a transaction abort, don't return early. Instead, allow
  490. * the brelse to happen. Normally it would be done for stale
  491. * (cancelled) buffers at unpin time, but we'll never go through the
  492. * pin/unpin cycle if we abort inside commit.
  493. */
  494. aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
  495. /*
  496. * Before possibly freeing the buf item, determine if we should
  497. * release the buffer at the end of this routine.
  498. */
  499. hold = bip->bli_flags & XFS_BLI_HOLD;
  500. /* Clear the per transaction state. */
  501. bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD);
  502. /*
  503. * If the buf item is marked stale, then don't do anything. We'll
  504. * unlock the buffer and free the buf item when the buffer is unpinned
  505. * for the last time.
  506. */
  507. if (bip->bli_flags & XFS_BLI_STALE) {
  508. trace_xfs_buf_item_unlock_stale(bip);
  509. ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
  510. if (!aborted) {
  511. atomic_dec(&bip->bli_refcount);
  512. return;
  513. }
  514. }
  515. trace_xfs_buf_item_unlock(bip);
  516. /*
  517. * If the buf item isn't tracking any data, free it, otherwise drop the
  518. * reference we hold to it.
  519. */
  520. if (xfs_bitmap_empty(bip->bli_format.blf_data_map,
  521. bip->bli_format.blf_map_size))
  522. xfs_buf_item_relse(bp);
  523. else
  524. atomic_dec(&bip->bli_refcount);
  525. if (!hold)
  526. xfs_buf_relse(bp);
  527. }
  528. /*
  529. * This is called to find out where the oldest active copy of the
  530. * buf log item in the on disk log resides now that the last log
  531. * write of it completed at the given lsn.
  532. * We always re-log all the dirty data in a buffer, so usually the
  533. * latest copy in the on disk log is the only one that matters. For
  534. * those cases we simply return the given lsn.
  535. *
  536. * The one exception to this is for buffers full of newly allocated
  537. * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
  538. * flag set, indicating that only the di_next_unlinked fields from the
  539. * inodes in the buffers will be replayed during recovery. If the
  540. * original newly allocated inode images have not yet been flushed
  541. * when the buffer is so relogged, then we need to make sure that we
  542. * keep the old images in the 'active' portion of the log. We do this
  543. * by returning the original lsn of that transaction here rather than
  544. * the current one.
  545. */
  546. STATIC xfs_lsn_t
  547. xfs_buf_item_committed(
  548. xfs_buf_log_item_t *bip,
  549. xfs_lsn_t lsn)
  550. {
  551. trace_xfs_buf_item_committed(bip);
  552. if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
  553. (bip->bli_item.li_lsn != 0)) {
  554. return bip->bli_item.li_lsn;
  555. }
  556. return (lsn);
  557. }
  558. /*
  559. * The buffer is locked, but is not a delayed write buffer. This happens
  560. * if we race with IO completion and hence we don't want to try to write it
  561. * again. Just release the buffer.
  562. */
  563. STATIC void
  564. xfs_buf_item_push(
  565. xfs_buf_log_item_t *bip)
  566. {
  567. xfs_buf_t *bp;
  568. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  569. trace_xfs_buf_item_push(bip);
  570. bp = bip->bli_buf;
  571. ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
  572. xfs_buf_relse(bp);
  573. }
  574. /*
  575. * The buffer is locked and is a delayed write buffer. Promote the buffer
  576. * in the delayed write queue as the caller knows that they must invoke
  577. * the xfsbufd to get this buffer written. We have to unlock the buffer
  578. * to allow the xfsbufd to write it, too.
  579. */
  580. STATIC void
  581. xfs_buf_item_pushbuf(
  582. xfs_buf_log_item_t *bip)
  583. {
  584. xfs_buf_t *bp;
  585. ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
  586. trace_xfs_buf_item_pushbuf(bip);
  587. bp = bip->bli_buf;
  588. ASSERT(XFS_BUF_ISDELAYWRITE(bp));
  589. xfs_buf_delwri_promote(bp);
  590. xfs_buf_relse(bp);
  591. }
  592. /* ARGSUSED */
  593. STATIC void
  594. xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
  595. {
  596. }
  597. /*
  598. * This is the ops vector shared by all buf log items.
  599. */
  600. static struct xfs_item_ops xfs_buf_item_ops = {
  601. .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
  602. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  603. xfs_buf_item_format,
  604. .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
  605. .iop_unpin = (void(*)(xfs_log_item_t*))xfs_buf_item_unpin,
  606. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
  607. xfs_buf_item_unpin_remove,
  608. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
  609. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
  610. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  611. xfs_buf_item_committed,
  612. .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
  613. .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_buf_item_pushbuf,
  614. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  615. xfs_buf_item_committing
  616. };
  617. /*
  618. * Allocate a new buf log item to go with the given buffer.
  619. * Set the buffer's b_fsprivate field to point to the new
  620. * buf log item. If there are other item's attached to the
  621. * buffer (see xfs_buf_attach_iodone() below), then put the
  622. * buf log item at the front.
  623. */
  624. void
  625. xfs_buf_item_init(
  626. xfs_buf_t *bp,
  627. xfs_mount_t *mp)
  628. {
  629. xfs_log_item_t *lip;
  630. xfs_buf_log_item_t *bip;
  631. int chunks;
  632. int map_size;
  633. /*
  634. * Check to see if there is already a buf log item for
  635. * this buffer. If there is, it is guaranteed to be
  636. * the first. If we do already have one, there is
  637. * nothing to do here so return.
  638. */
  639. if (bp->b_mount != mp)
  640. bp->b_mount = mp;
  641. XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
  642. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  643. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  644. if (lip->li_type == XFS_LI_BUF) {
  645. return;
  646. }
  647. }
  648. /*
  649. * chunks is the number of XFS_BLF_CHUNK size pieces
  650. * the buffer can be divided into. Make sure not to
  651. * truncate any pieces. map_size is the size of the
  652. * bitmap needed to describe the chunks of the buffer.
  653. */
  654. chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLF_CHUNK - 1)) >> XFS_BLF_SHIFT);
  655. map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
  656. bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
  657. KM_SLEEP);
  658. xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
  659. bip->bli_buf = bp;
  660. xfs_buf_hold(bp);
  661. bip->bli_format.blf_type = XFS_LI_BUF;
  662. bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
  663. bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
  664. bip->bli_format.blf_map_size = map_size;
  665. #ifdef XFS_TRANS_DEBUG
  666. /*
  667. * Allocate the arrays for tracking what needs to be logged
  668. * and what our callers request to be logged. bli_orig
  669. * holds a copy of the original, clean buffer for comparison
  670. * against, and bli_logged keeps a 1 bit flag per byte in
  671. * the buffer to indicate which bytes the callers have asked
  672. * to have logged.
  673. */
  674. bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
  675. memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
  676. bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
  677. #endif
  678. /*
  679. * Put the buf item into the list of items attached to the
  680. * buffer at the front.
  681. */
  682. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  683. bip->bli_item.li_bio_list =
  684. XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  685. }
  686. XFS_BUF_SET_FSPRIVATE(bp, bip);
  687. }
  688. /*
  689. * Mark bytes first through last inclusive as dirty in the buf
  690. * item's bitmap.
  691. */
  692. void
  693. xfs_buf_item_log(
  694. xfs_buf_log_item_t *bip,
  695. uint first,
  696. uint last)
  697. {
  698. uint first_bit;
  699. uint last_bit;
  700. uint bits_to_set;
  701. uint bits_set;
  702. uint word_num;
  703. uint *wordp;
  704. uint bit;
  705. uint end_bit;
  706. uint mask;
  707. /*
  708. * Mark the item as having some dirty data for
  709. * quick reference in xfs_buf_item_dirty.
  710. */
  711. bip->bli_flags |= XFS_BLI_DIRTY;
  712. /*
  713. * Convert byte offsets to bit numbers.
  714. */
  715. first_bit = first >> XFS_BLF_SHIFT;
  716. last_bit = last >> XFS_BLF_SHIFT;
  717. /*
  718. * Calculate the total number of bits to be set.
  719. */
  720. bits_to_set = last_bit - first_bit + 1;
  721. /*
  722. * Get a pointer to the first word in the bitmap
  723. * to set a bit in.
  724. */
  725. word_num = first_bit >> BIT_TO_WORD_SHIFT;
  726. wordp = &(bip->bli_format.blf_data_map[word_num]);
  727. /*
  728. * Calculate the starting bit in the first word.
  729. */
  730. bit = first_bit & (uint)(NBWORD - 1);
  731. /*
  732. * First set any bits in the first word of our range.
  733. * If it starts at bit 0 of the word, it will be
  734. * set below rather than here. That is what the variable
  735. * bit tells us. The variable bits_set tracks the number
  736. * of bits that have been set so far. End_bit is the number
  737. * of the last bit to be set in this word plus one.
  738. */
  739. if (bit) {
  740. end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
  741. mask = ((1 << (end_bit - bit)) - 1) << bit;
  742. *wordp |= mask;
  743. wordp++;
  744. bits_set = end_bit - bit;
  745. } else {
  746. bits_set = 0;
  747. }
  748. /*
  749. * Now set bits a whole word at a time that are between
  750. * first_bit and last_bit.
  751. */
  752. while ((bits_to_set - bits_set) >= NBWORD) {
  753. *wordp |= 0xffffffff;
  754. bits_set += NBWORD;
  755. wordp++;
  756. }
  757. /*
  758. * Finally, set any bits left to be set in one last partial word.
  759. */
  760. end_bit = bits_to_set - bits_set;
  761. if (end_bit) {
  762. mask = (1 << end_bit) - 1;
  763. *wordp |= mask;
  764. }
  765. xfs_buf_item_log_debug(bip, first, last);
  766. }
  767. /*
  768. * Return 1 if the buffer has some data that has been logged (at any
  769. * point, not just the current transaction) and 0 if not.
  770. */
  771. uint
  772. xfs_buf_item_dirty(
  773. xfs_buf_log_item_t *bip)
  774. {
  775. return (bip->bli_flags & XFS_BLI_DIRTY);
  776. }
  777. STATIC void
  778. xfs_buf_item_free(
  779. xfs_buf_log_item_t *bip)
  780. {
  781. #ifdef XFS_TRANS_DEBUG
  782. kmem_free(bip->bli_orig);
  783. kmem_free(bip->bli_logged);
  784. #endif /* XFS_TRANS_DEBUG */
  785. kmem_zone_free(xfs_buf_item_zone, bip);
  786. }
  787. /*
  788. * This is called when the buf log item is no longer needed. It should
  789. * free the buf log item associated with the given buffer and clear
  790. * the buffer's pointer to the buf log item. If there are no more
  791. * items in the list, clear the b_iodone field of the buffer (see
  792. * xfs_buf_attach_iodone() below).
  793. */
  794. void
  795. xfs_buf_item_relse(
  796. xfs_buf_t *bp)
  797. {
  798. xfs_buf_log_item_t *bip;
  799. trace_xfs_buf_item_relse(bp, _RET_IP_);
  800. bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
  801. XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
  802. if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
  803. (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
  804. XFS_BUF_CLR_IODONE_FUNC(bp);
  805. }
  806. xfs_buf_rele(bp);
  807. xfs_buf_item_free(bip);
  808. }
  809. /*
  810. * Add the given log item with its callback to the list of callbacks
  811. * to be called when the buffer's I/O completes. If it is not set
  812. * already, set the buffer's b_iodone() routine to be
  813. * xfs_buf_iodone_callbacks() and link the log item into the list of
  814. * items rooted at b_fsprivate. Items are always added as the second
  815. * entry in the list if there is a first, because the buf item code
  816. * assumes that the buf log item is first.
  817. */
  818. void
  819. xfs_buf_attach_iodone(
  820. xfs_buf_t *bp,
  821. void (*cb)(xfs_buf_t *, xfs_log_item_t *),
  822. xfs_log_item_t *lip)
  823. {
  824. xfs_log_item_t *head_lip;
  825. ASSERT(XFS_BUF_ISBUSY(bp));
  826. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  827. lip->li_cb = cb;
  828. if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
  829. head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  830. lip->li_bio_list = head_lip->li_bio_list;
  831. head_lip->li_bio_list = lip;
  832. } else {
  833. XFS_BUF_SET_FSPRIVATE(bp, lip);
  834. }
  835. ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
  836. (XFS_BUF_IODONE_FUNC(bp) == NULL));
  837. XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
  838. }
  839. STATIC void
  840. xfs_buf_do_callbacks(
  841. xfs_buf_t *bp,
  842. xfs_log_item_t *lip)
  843. {
  844. xfs_log_item_t *nlip;
  845. while (lip != NULL) {
  846. nlip = lip->li_bio_list;
  847. ASSERT(lip->li_cb != NULL);
  848. /*
  849. * Clear the next pointer so we don't have any
  850. * confusion if the item is added to another buf.
  851. * Don't touch the log item after calling its
  852. * callback, because it could have freed itself.
  853. */
  854. lip->li_bio_list = NULL;
  855. lip->li_cb(bp, lip);
  856. lip = nlip;
  857. }
  858. }
  859. /*
  860. * This is the iodone() function for buffers which have had callbacks
  861. * attached to them by xfs_buf_attach_iodone(). It should remove each
  862. * log item from the buffer's list and call the callback of each in turn.
  863. * When done, the buffer's fsprivate field is set to NULL and the buffer
  864. * is unlocked with a call to iodone().
  865. */
  866. void
  867. xfs_buf_iodone_callbacks(
  868. xfs_buf_t *bp)
  869. {
  870. xfs_log_item_t *lip;
  871. static ulong lasttime;
  872. static xfs_buftarg_t *lasttarg;
  873. xfs_mount_t *mp;
  874. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  875. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  876. if (XFS_BUF_GETERROR(bp) != 0) {
  877. /*
  878. * If we've already decided to shutdown the filesystem
  879. * because of IO errors, there's no point in giving this
  880. * a retry.
  881. */
  882. mp = lip->li_mountp;
  883. if (XFS_FORCED_SHUTDOWN(mp)) {
  884. ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
  885. XFS_BUF_SUPER_STALE(bp);
  886. trace_xfs_buf_item_iodone(bp, _RET_IP_);
  887. xfs_buf_do_callbacks(bp, lip);
  888. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  889. XFS_BUF_CLR_IODONE_FUNC(bp);
  890. xfs_biodone(bp);
  891. return;
  892. }
  893. if ((XFS_BUF_TARGET(bp) != lasttarg) ||
  894. (time_after(jiffies, (lasttime + 5*HZ)))) {
  895. lasttime = jiffies;
  896. cmn_err(CE_ALERT, "Device %s, XFS metadata write error"
  897. " block 0x%llx in %s",
  898. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  899. (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
  900. }
  901. lasttarg = XFS_BUF_TARGET(bp);
  902. if (XFS_BUF_ISASYNC(bp)) {
  903. /*
  904. * If the write was asynchronous then noone will be
  905. * looking for the error. Clear the error state
  906. * and write the buffer out again delayed write.
  907. *
  908. * XXXsup This is OK, so long as we catch these
  909. * before we start the umount; we don't want these
  910. * DELWRI metadata bufs to be hanging around.
  911. */
  912. XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
  913. if (!(XFS_BUF_ISSTALE(bp))) {
  914. XFS_BUF_DELAYWRITE(bp);
  915. XFS_BUF_DONE(bp);
  916. XFS_BUF_SET_START(bp);
  917. }
  918. ASSERT(XFS_BUF_IODONE_FUNC(bp));
  919. trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
  920. xfs_buf_relse(bp);
  921. } else {
  922. /*
  923. * If the write of the buffer was not asynchronous,
  924. * then we want to make sure to return the error
  925. * to the caller of bwrite(). Because of this we
  926. * cannot clear the B_ERROR state at this point.
  927. * Instead we install a callback function that
  928. * will be called when the buffer is released, and
  929. * that routine will clear the error state and
  930. * set the buffer to be written out again after
  931. * some delay.
  932. */
  933. /* We actually overwrite the existing b-relse
  934. function at times, but we're gonna be shutting down
  935. anyway. */
  936. XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
  937. XFS_BUF_DONE(bp);
  938. XFS_BUF_FINISH_IOWAIT(bp);
  939. }
  940. return;
  941. }
  942. xfs_buf_do_callbacks(bp, lip);
  943. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  944. XFS_BUF_CLR_IODONE_FUNC(bp);
  945. xfs_biodone(bp);
  946. }
  947. /*
  948. * This is a callback routine attached to a buffer which gets an error
  949. * when being written out synchronously.
  950. */
  951. STATIC void
  952. xfs_buf_error_relse(
  953. xfs_buf_t *bp)
  954. {
  955. xfs_log_item_t *lip;
  956. xfs_mount_t *mp;
  957. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  958. mp = (xfs_mount_t *)lip->li_mountp;
  959. ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
  960. XFS_BUF_STALE(bp);
  961. XFS_BUF_DONE(bp);
  962. XFS_BUF_UNDELAYWRITE(bp);
  963. XFS_BUF_ERROR(bp,0);
  964. trace_xfs_buf_error_relse(bp, _RET_IP_);
  965. if (! XFS_FORCED_SHUTDOWN(mp))
  966. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  967. /*
  968. * We have to unpin the pinned buffers so do the
  969. * callbacks.
  970. */
  971. xfs_buf_do_callbacks(bp, lip);
  972. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  973. XFS_BUF_CLR_IODONE_FUNC(bp);
  974. XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
  975. xfs_buf_relse(bp);
  976. }
  977. /*
  978. * This is the iodone() function for buffers which have been
  979. * logged. It is called when they are eventually flushed out.
  980. * It should remove the buf item from the AIL, and free the buf item.
  981. * It is called by xfs_buf_iodone_callbacks() above which will take
  982. * care of cleaning up the buffer itself.
  983. */
  984. /* ARGSUSED */
  985. void
  986. xfs_buf_iodone(
  987. xfs_buf_t *bp,
  988. xfs_buf_log_item_t *bip)
  989. {
  990. struct xfs_ail *ailp = bip->bli_item.li_ailp;
  991. ASSERT(bip->bli_buf == bp);
  992. xfs_buf_rele(bp);
  993. /*
  994. * If we are forcibly shutting down, this may well be
  995. * off the AIL already. That's because we simulate the
  996. * log-committed callbacks to unpin these buffers. Or we may never
  997. * have put this item on AIL because of the transaction was
  998. * aborted forcibly. xfs_trans_ail_delete() takes care of these.
  999. *
  1000. * Either way, AIL is useless if we're forcing a shutdown.
  1001. */
  1002. spin_lock(&ailp->xa_lock);
  1003. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)bip);
  1004. xfs_buf_item_free(bip);
  1005. }