xfs_buf_item.c 31 KB

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