xfs_buf_item.c 30 KB

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