xfs_trans.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_extent_busy.h"
  28. #include "xfs_quota.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_trans_priv.h"
  31. #include "xfs_log.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_error.h"
  34. kmem_zone_t *xfs_trans_zone;
  35. kmem_zone_t *xfs_log_item_desc_zone;
  36. /*
  37. * Initialize the precomputed transaction reservation values
  38. * in the mount structure.
  39. */
  40. void
  41. xfs_trans_init(
  42. struct xfs_mount *mp)
  43. {
  44. xfs_trans_resv_calc(mp, M_RES(mp));
  45. }
  46. /*
  47. * Free the transaction structure. If there is more clean up
  48. * to do when the structure is freed, add it here.
  49. */
  50. STATIC void
  51. xfs_trans_free(
  52. struct xfs_trans *tp)
  53. {
  54. xfs_extent_busy_sort(&tp->t_busy);
  55. xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
  56. atomic_dec(&tp->t_mountp->m_active_trans);
  57. if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
  58. sb_end_intwrite(tp->t_mountp->m_super);
  59. xfs_trans_free_dqinfo(tp);
  60. kmem_zone_free(xfs_trans_zone, tp);
  61. }
  62. /*
  63. * This is called to create a new transaction which will share the
  64. * permanent log reservation of the given transaction. The remaining
  65. * unused block and rt extent reservations are also inherited. This
  66. * implies that the original transaction is no longer allowed to allocate
  67. * blocks. Locks and log items, however, are no inherited. They must
  68. * be added to the new transaction explicitly.
  69. */
  70. STATIC xfs_trans_t *
  71. xfs_trans_dup(
  72. xfs_trans_t *tp)
  73. {
  74. xfs_trans_t *ntp;
  75. ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  76. /*
  77. * Initialize the new transaction structure.
  78. */
  79. ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
  80. ntp->t_mountp = tp->t_mountp;
  81. INIT_LIST_HEAD(&ntp->t_items);
  82. INIT_LIST_HEAD(&ntp->t_busy);
  83. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  84. ASSERT(tp->t_ticket != NULL);
  85. ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
  86. (tp->t_flags & XFS_TRANS_RESERVE) |
  87. (tp->t_flags & XFS_TRANS_NO_WRITECOUNT);
  88. /* We gave our writer reference to the new transaction */
  89. tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
  90. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  91. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  92. tp->t_blk_res = tp->t_blk_res_used;
  93. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  94. tp->t_rtx_res = tp->t_rtx_res_used;
  95. ntp->t_pflags = tp->t_pflags;
  96. xfs_trans_dup_dqinfo(tp, ntp);
  97. atomic_inc(&tp->t_mountp->m_active_trans);
  98. return ntp;
  99. }
  100. /*
  101. * This is called to reserve free disk blocks and log space for the
  102. * given transaction. This must be done before allocating any resources
  103. * within the transaction.
  104. *
  105. * This will return ENOSPC if there are not enough blocks available.
  106. * It will sleep waiting for available log space.
  107. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  108. * is used by long running transactions. If any one of the reservations
  109. * fails then they will all be backed out.
  110. *
  111. * This does not do quota reservations. That typically is done by the
  112. * caller afterwards.
  113. */
  114. static int
  115. xfs_trans_reserve(
  116. struct xfs_trans *tp,
  117. struct xfs_trans_res *resp,
  118. uint blocks,
  119. uint rtextents)
  120. {
  121. int error = 0;
  122. bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  123. /* Mark this thread as being in a transaction */
  124. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  125. /*
  126. * Attempt to reserve the needed disk blocks by decrementing
  127. * the number needed from the number available. This will
  128. * fail if the count would go below zero.
  129. */
  130. if (blocks > 0) {
  131. error = xfs_mod_fdblocks(tp->t_mountp, -((int64_t)blocks), rsvd);
  132. if (error != 0) {
  133. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  134. return -ENOSPC;
  135. }
  136. tp->t_blk_res += blocks;
  137. }
  138. /*
  139. * Reserve the log space needed for this transaction.
  140. */
  141. if (resp->tr_logres > 0) {
  142. bool permanent = false;
  143. ASSERT(tp->t_log_res == 0 ||
  144. tp->t_log_res == resp->tr_logres);
  145. ASSERT(tp->t_log_count == 0 ||
  146. tp->t_log_count == resp->tr_logcount);
  147. if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
  148. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  149. permanent = true;
  150. } else {
  151. ASSERT(tp->t_ticket == NULL);
  152. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  153. }
  154. if (tp->t_ticket != NULL) {
  155. ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
  156. error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
  157. } else {
  158. error = xfs_log_reserve(tp->t_mountp,
  159. resp->tr_logres,
  160. resp->tr_logcount,
  161. &tp->t_ticket, XFS_TRANSACTION,
  162. permanent);
  163. }
  164. if (error)
  165. goto undo_blocks;
  166. tp->t_log_res = resp->tr_logres;
  167. tp->t_log_count = resp->tr_logcount;
  168. }
  169. /*
  170. * Attempt to reserve the needed realtime extents by decrementing
  171. * the number needed from the number available. This will
  172. * fail if the count would go below zero.
  173. */
  174. if (rtextents > 0) {
  175. error = xfs_mod_frextents(tp->t_mountp, -((int64_t)rtextents));
  176. if (error) {
  177. error = -ENOSPC;
  178. goto undo_log;
  179. }
  180. tp->t_rtx_res += rtextents;
  181. }
  182. return 0;
  183. /*
  184. * Error cases jump to one of these labels to undo any
  185. * reservations which have already been performed.
  186. */
  187. undo_log:
  188. if (resp->tr_logres > 0) {
  189. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, false);
  190. tp->t_ticket = NULL;
  191. tp->t_log_res = 0;
  192. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  193. }
  194. undo_blocks:
  195. if (blocks > 0) {
  196. xfs_mod_fdblocks(tp->t_mountp, (int64_t)blocks, rsvd);
  197. tp->t_blk_res = 0;
  198. }
  199. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  200. return error;
  201. }
  202. int
  203. xfs_trans_alloc(
  204. struct xfs_mount *mp,
  205. struct xfs_trans_res *resp,
  206. uint blocks,
  207. uint rtextents,
  208. uint flags,
  209. struct xfs_trans **tpp)
  210. {
  211. struct xfs_trans *tp;
  212. int error;
  213. if (!(flags & XFS_TRANS_NO_WRITECOUNT))
  214. sb_start_intwrite(mp->m_super);
  215. WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
  216. atomic_inc(&mp->m_active_trans);
  217. tp = kmem_zone_zalloc(xfs_trans_zone,
  218. (flags & XFS_TRANS_NOFS) ? KM_NOFS : KM_SLEEP);
  219. tp->t_magic = XFS_TRANS_HEADER_MAGIC;
  220. tp->t_flags = flags;
  221. tp->t_mountp = mp;
  222. INIT_LIST_HEAD(&tp->t_items);
  223. INIT_LIST_HEAD(&tp->t_busy);
  224. error = xfs_trans_reserve(tp, resp, blocks, rtextents);
  225. if (error) {
  226. xfs_trans_cancel(tp);
  227. return error;
  228. }
  229. *tpp = tp;
  230. return 0;
  231. }
  232. /*
  233. * Record the indicated change to the given field for application
  234. * to the file system's superblock when the transaction commits.
  235. * For now, just store the change in the transaction structure.
  236. *
  237. * Mark the transaction structure to indicate that the superblock
  238. * needs to be updated before committing.
  239. *
  240. * Because we may not be keeping track of allocated/free inodes and
  241. * used filesystem blocks in the superblock, we do not mark the
  242. * superblock dirty in this transaction if we modify these fields.
  243. * We still need to update the transaction deltas so that they get
  244. * applied to the incore superblock, but we don't want them to
  245. * cause the superblock to get locked and logged if these are the
  246. * only fields in the superblock that the transaction modifies.
  247. */
  248. void
  249. xfs_trans_mod_sb(
  250. xfs_trans_t *tp,
  251. uint field,
  252. int64_t delta)
  253. {
  254. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  255. xfs_mount_t *mp = tp->t_mountp;
  256. switch (field) {
  257. case XFS_TRANS_SB_ICOUNT:
  258. tp->t_icount_delta += delta;
  259. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  260. flags &= ~XFS_TRANS_SB_DIRTY;
  261. break;
  262. case XFS_TRANS_SB_IFREE:
  263. tp->t_ifree_delta += delta;
  264. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  265. flags &= ~XFS_TRANS_SB_DIRTY;
  266. break;
  267. case XFS_TRANS_SB_FDBLOCKS:
  268. /*
  269. * Track the number of blocks allocated in the
  270. * transaction. Make sure it does not exceed the
  271. * number reserved.
  272. */
  273. if (delta < 0) {
  274. tp->t_blk_res_used += (uint)-delta;
  275. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  276. }
  277. tp->t_fdblocks_delta += delta;
  278. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  279. flags &= ~XFS_TRANS_SB_DIRTY;
  280. break;
  281. case XFS_TRANS_SB_RES_FDBLOCKS:
  282. /*
  283. * The allocation has already been applied to the
  284. * in-core superblock's counter. This should only
  285. * be applied to the on-disk superblock.
  286. */
  287. tp->t_res_fdblocks_delta += delta;
  288. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  289. flags &= ~XFS_TRANS_SB_DIRTY;
  290. break;
  291. case XFS_TRANS_SB_FREXTENTS:
  292. /*
  293. * Track the number of blocks allocated in the
  294. * transaction. Make sure it does not exceed the
  295. * number reserved.
  296. */
  297. if (delta < 0) {
  298. tp->t_rtx_res_used += (uint)-delta;
  299. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  300. }
  301. tp->t_frextents_delta += delta;
  302. break;
  303. case XFS_TRANS_SB_RES_FREXTENTS:
  304. /*
  305. * The allocation has already been applied to the
  306. * in-core superblock's counter. This should only
  307. * be applied to the on-disk superblock.
  308. */
  309. ASSERT(delta < 0);
  310. tp->t_res_frextents_delta += delta;
  311. break;
  312. case XFS_TRANS_SB_DBLOCKS:
  313. ASSERT(delta > 0);
  314. tp->t_dblocks_delta += delta;
  315. break;
  316. case XFS_TRANS_SB_AGCOUNT:
  317. ASSERT(delta > 0);
  318. tp->t_agcount_delta += delta;
  319. break;
  320. case XFS_TRANS_SB_IMAXPCT:
  321. tp->t_imaxpct_delta += delta;
  322. break;
  323. case XFS_TRANS_SB_REXTSIZE:
  324. tp->t_rextsize_delta += delta;
  325. break;
  326. case XFS_TRANS_SB_RBMBLOCKS:
  327. tp->t_rbmblocks_delta += delta;
  328. break;
  329. case XFS_TRANS_SB_RBLOCKS:
  330. tp->t_rblocks_delta += delta;
  331. break;
  332. case XFS_TRANS_SB_REXTENTS:
  333. tp->t_rextents_delta += delta;
  334. break;
  335. case XFS_TRANS_SB_REXTSLOG:
  336. tp->t_rextslog_delta += delta;
  337. break;
  338. default:
  339. ASSERT(0);
  340. return;
  341. }
  342. tp->t_flags |= flags;
  343. }
  344. /*
  345. * xfs_trans_apply_sb_deltas() is called from the commit code
  346. * to bring the superblock buffer into the current transaction
  347. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  348. *
  349. * For now we just look at each field allowed to change and change
  350. * it if necessary.
  351. */
  352. STATIC void
  353. xfs_trans_apply_sb_deltas(
  354. xfs_trans_t *tp)
  355. {
  356. xfs_dsb_t *sbp;
  357. xfs_buf_t *bp;
  358. int whole = 0;
  359. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  360. sbp = XFS_BUF_TO_SBP(bp);
  361. /*
  362. * Check that superblock mods match the mods made to AGF counters.
  363. */
  364. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  365. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  366. tp->t_ag_btree_delta));
  367. /*
  368. * Only update the superblock counters if we are logging them
  369. */
  370. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  371. if (tp->t_icount_delta)
  372. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  373. if (tp->t_ifree_delta)
  374. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  375. if (tp->t_fdblocks_delta)
  376. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  377. if (tp->t_res_fdblocks_delta)
  378. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  379. }
  380. if (tp->t_frextents_delta)
  381. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  382. if (tp->t_res_frextents_delta)
  383. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  384. if (tp->t_dblocks_delta) {
  385. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  386. whole = 1;
  387. }
  388. if (tp->t_agcount_delta) {
  389. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  390. whole = 1;
  391. }
  392. if (tp->t_imaxpct_delta) {
  393. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  394. whole = 1;
  395. }
  396. if (tp->t_rextsize_delta) {
  397. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  398. whole = 1;
  399. }
  400. if (tp->t_rbmblocks_delta) {
  401. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  402. whole = 1;
  403. }
  404. if (tp->t_rblocks_delta) {
  405. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  406. whole = 1;
  407. }
  408. if (tp->t_rextents_delta) {
  409. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  410. whole = 1;
  411. }
  412. if (tp->t_rextslog_delta) {
  413. sbp->sb_rextslog += tp->t_rextslog_delta;
  414. whole = 1;
  415. }
  416. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
  417. if (whole)
  418. /*
  419. * Log the whole thing, the fields are noncontiguous.
  420. */
  421. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  422. else
  423. /*
  424. * Since all the modifiable fields are contiguous, we
  425. * can get away with this.
  426. */
  427. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  428. offsetof(xfs_dsb_t, sb_frextents) +
  429. sizeof(sbp->sb_frextents) - 1);
  430. }
  431. STATIC int
  432. xfs_sb_mod8(
  433. uint8_t *field,
  434. int8_t delta)
  435. {
  436. int8_t counter = *field;
  437. counter += delta;
  438. if (counter < 0) {
  439. ASSERT(0);
  440. return -EINVAL;
  441. }
  442. *field = counter;
  443. return 0;
  444. }
  445. STATIC int
  446. xfs_sb_mod32(
  447. uint32_t *field,
  448. int32_t delta)
  449. {
  450. int32_t counter = *field;
  451. counter += delta;
  452. if (counter < 0) {
  453. ASSERT(0);
  454. return -EINVAL;
  455. }
  456. *field = counter;
  457. return 0;
  458. }
  459. STATIC int
  460. xfs_sb_mod64(
  461. uint64_t *field,
  462. int64_t delta)
  463. {
  464. int64_t counter = *field;
  465. counter += delta;
  466. if (counter < 0) {
  467. ASSERT(0);
  468. return -EINVAL;
  469. }
  470. *field = counter;
  471. return 0;
  472. }
  473. /*
  474. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  475. * and apply superblock counter changes to the in-core superblock. The
  476. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  477. * applied to the in-core superblock. The idea is that that has already been
  478. * done.
  479. *
  480. * If we are not logging superblock counters, then the inode allocated/free and
  481. * used block counts are not updated in the on disk superblock. In this case,
  482. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  483. * still need to update the incore superblock with the changes.
  484. */
  485. void
  486. xfs_trans_unreserve_and_mod_sb(
  487. struct xfs_trans *tp)
  488. {
  489. struct xfs_mount *mp = tp->t_mountp;
  490. bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  491. int64_t blkdelta = 0;
  492. int64_t rtxdelta = 0;
  493. int64_t idelta = 0;
  494. int64_t ifreedelta = 0;
  495. int error;
  496. /* calculate deltas */
  497. if (tp->t_blk_res > 0)
  498. blkdelta = tp->t_blk_res;
  499. if ((tp->t_fdblocks_delta != 0) &&
  500. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  501. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  502. blkdelta += tp->t_fdblocks_delta;
  503. if (tp->t_rtx_res > 0)
  504. rtxdelta = tp->t_rtx_res;
  505. if ((tp->t_frextents_delta != 0) &&
  506. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  507. rtxdelta += tp->t_frextents_delta;
  508. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  509. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  510. idelta = tp->t_icount_delta;
  511. ifreedelta = tp->t_ifree_delta;
  512. }
  513. /* apply the per-cpu counters */
  514. if (blkdelta) {
  515. error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
  516. if (error)
  517. goto out;
  518. }
  519. if (idelta) {
  520. error = xfs_mod_icount(mp, idelta);
  521. if (error)
  522. goto out_undo_fdblocks;
  523. }
  524. if (ifreedelta) {
  525. error = xfs_mod_ifree(mp, ifreedelta);
  526. if (error)
  527. goto out_undo_icount;
  528. }
  529. if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY))
  530. return;
  531. /* apply remaining deltas */
  532. spin_lock(&mp->m_sb_lock);
  533. if (rtxdelta) {
  534. error = xfs_sb_mod64(&mp->m_sb.sb_frextents, rtxdelta);
  535. if (error)
  536. goto out_undo_ifree;
  537. }
  538. if (tp->t_dblocks_delta != 0) {
  539. error = xfs_sb_mod64(&mp->m_sb.sb_dblocks, tp->t_dblocks_delta);
  540. if (error)
  541. goto out_undo_frextents;
  542. }
  543. if (tp->t_agcount_delta != 0) {
  544. error = xfs_sb_mod32(&mp->m_sb.sb_agcount, tp->t_agcount_delta);
  545. if (error)
  546. goto out_undo_dblocks;
  547. }
  548. if (tp->t_imaxpct_delta != 0) {
  549. error = xfs_sb_mod8(&mp->m_sb.sb_imax_pct, tp->t_imaxpct_delta);
  550. if (error)
  551. goto out_undo_agcount;
  552. }
  553. if (tp->t_rextsize_delta != 0) {
  554. error = xfs_sb_mod32(&mp->m_sb.sb_rextsize,
  555. tp->t_rextsize_delta);
  556. if (error)
  557. goto out_undo_imaxpct;
  558. }
  559. if (tp->t_rbmblocks_delta != 0) {
  560. error = xfs_sb_mod32(&mp->m_sb.sb_rbmblocks,
  561. tp->t_rbmblocks_delta);
  562. if (error)
  563. goto out_undo_rextsize;
  564. }
  565. if (tp->t_rblocks_delta != 0) {
  566. error = xfs_sb_mod64(&mp->m_sb.sb_rblocks, tp->t_rblocks_delta);
  567. if (error)
  568. goto out_undo_rbmblocks;
  569. }
  570. if (tp->t_rextents_delta != 0) {
  571. error = xfs_sb_mod64(&mp->m_sb.sb_rextents,
  572. tp->t_rextents_delta);
  573. if (error)
  574. goto out_undo_rblocks;
  575. }
  576. if (tp->t_rextslog_delta != 0) {
  577. error = xfs_sb_mod8(&mp->m_sb.sb_rextslog,
  578. tp->t_rextslog_delta);
  579. if (error)
  580. goto out_undo_rextents;
  581. }
  582. spin_unlock(&mp->m_sb_lock);
  583. return;
  584. out_undo_rextents:
  585. if (tp->t_rextents_delta)
  586. xfs_sb_mod64(&mp->m_sb.sb_rextents, -tp->t_rextents_delta);
  587. out_undo_rblocks:
  588. if (tp->t_rblocks_delta)
  589. xfs_sb_mod64(&mp->m_sb.sb_rblocks, -tp->t_rblocks_delta);
  590. out_undo_rbmblocks:
  591. if (tp->t_rbmblocks_delta)
  592. xfs_sb_mod32(&mp->m_sb.sb_rbmblocks, -tp->t_rbmblocks_delta);
  593. out_undo_rextsize:
  594. if (tp->t_rextsize_delta)
  595. xfs_sb_mod32(&mp->m_sb.sb_rextsize, -tp->t_rextsize_delta);
  596. out_undo_imaxpct:
  597. if (tp->t_rextsize_delta)
  598. xfs_sb_mod8(&mp->m_sb.sb_imax_pct, -tp->t_imaxpct_delta);
  599. out_undo_agcount:
  600. if (tp->t_agcount_delta)
  601. xfs_sb_mod32(&mp->m_sb.sb_agcount, -tp->t_agcount_delta);
  602. out_undo_dblocks:
  603. if (tp->t_dblocks_delta)
  604. xfs_sb_mod64(&mp->m_sb.sb_dblocks, -tp->t_dblocks_delta);
  605. out_undo_frextents:
  606. if (rtxdelta)
  607. xfs_sb_mod64(&mp->m_sb.sb_frextents, -rtxdelta);
  608. out_undo_ifree:
  609. spin_unlock(&mp->m_sb_lock);
  610. if (ifreedelta)
  611. xfs_mod_ifree(mp, -ifreedelta);
  612. out_undo_icount:
  613. if (idelta)
  614. xfs_mod_icount(mp, -idelta);
  615. out_undo_fdblocks:
  616. if (blkdelta)
  617. xfs_mod_fdblocks(mp, -blkdelta, rsvd);
  618. out:
  619. ASSERT(error == 0);
  620. return;
  621. }
  622. /*
  623. * Add the given log item to the transaction's list of log items.
  624. *
  625. * The log item will now point to its new descriptor with its li_desc field.
  626. */
  627. void
  628. xfs_trans_add_item(
  629. struct xfs_trans *tp,
  630. struct xfs_log_item *lip)
  631. {
  632. struct xfs_log_item_desc *lidp;
  633. ASSERT(lip->li_mountp == tp->t_mountp);
  634. ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
  635. lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
  636. lidp->lid_item = lip;
  637. lidp->lid_flags = 0;
  638. list_add_tail(&lidp->lid_trans, &tp->t_items);
  639. lip->li_desc = lidp;
  640. }
  641. STATIC void
  642. xfs_trans_free_item_desc(
  643. struct xfs_log_item_desc *lidp)
  644. {
  645. list_del_init(&lidp->lid_trans);
  646. kmem_zone_free(xfs_log_item_desc_zone, lidp);
  647. }
  648. /*
  649. * Unlink and free the given descriptor.
  650. */
  651. void
  652. xfs_trans_del_item(
  653. struct xfs_log_item *lip)
  654. {
  655. xfs_trans_free_item_desc(lip->li_desc);
  656. lip->li_desc = NULL;
  657. }
  658. /*
  659. * Unlock all of the items of a transaction and free all the descriptors
  660. * of that transaction.
  661. */
  662. void
  663. xfs_trans_free_items(
  664. struct xfs_trans *tp,
  665. xfs_lsn_t commit_lsn,
  666. bool abort)
  667. {
  668. struct xfs_log_item_desc *lidp, *next;
  669. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  670. struct xfs_log_item *lip = lidp->lid_item;
  671. lip->li_desc = NULL;
  672. if (commit_lsn != NULLCOMMITLSN)
  673. lip->li_ops->iop_committing(lip, commit_lsn);
  674. if (abort)
  675. lip->li_flags |= XFS_LI_ABORTED;
  676. lip->li_ops->iop_unlock(lip);
  677. xfs_trans_free_item_desc(lidp);
  678. }
  679. }
  680. static inline void
  681. xfs_log_item_batch_insert(
  682. struct xfs_ail *ailp,
  683. struct xfs_ail_cursor *cur,
  684. struct xfs_log_item **log_items,
  685. int nr_items,
  686. xfs_lsn_t commit_lsn)
  687. {
  688. int i;
  689. spin_lock(&ailp->xa_lock);
  690. /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
  691. xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
  692. for (i = 0; i < nr_items; i++) {
  693. struct xfs_log_item *lip = log_items[i];
  694. lip->li_ops->iop_unpin(lip, 0);
  695. }
  696. }
  697. /*
  698. * Bulk operation version of xfs_trans_committed that takes a log vector of
  699. * items to insert into the AIL. This uses bulk AIL insertion techniques to
  700. * minimise lock traffic.
  701. *
  702. * If we are called with the aborted flag set, it is because a log write during
  703. * a CIL checkpoint commit has failed. In this case, all the items in the
  704. * checkpoint have already gone through iop_commited and iop_unlock, which
  705. * means that checkpoint commit abort handling is treated exactly the same
  706. * as an iclog write error even though we haven't started any IO yet. Hence in
  707. * this case all we need to do is iop_committed processing, followed by an
  708. * iop_unpin(aborted) call.
  709. *
  710. * The AIL cursor is used to optimise the insert process. If commit_lsn is not
  711. * at the end of the AIL, the insert cursor avoids the need to walk
  712. * the AIL to find the insertion point on every xfs_log_item_batch_insert()
  713. * call. This saves a lot of needless list walking and is a net win, even
  714. * though it slightly increases that amount of AIL lock traffic to set it up
  715. * and tear it down.
  716. */
  717. void
  718. xfs_trans_committed_bulk(
  719. struct xfs_ail *ailp,
  720. struct xfs_log_vec *log_vector,
  721. xfs_lsn_t commit_lsn,
  722. int aborted)
  723. {
  724. #define LOG_ITEM_BATCH_SIZE 32
  725. struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
  726. struct xfs_log_vec *lv;
  727. struct xfs_ail_cursor cur;
  728. int i = 0;
  729. spin_lock(&ailp->xa_lock);
  730. xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
  731. spin_unlock(&ailp->xa_lock);
  732. /* unpin all the log items */
  733. for (lv = log_vector; lv; lv = lv->lv_next ) {
  734. struct xfs_log_item *lip = lv->lv_item;
  735. xfs_lsn_t item_lsn;
  736. if (aborted)
  737. lip->li_flags |= XFS_LI_ABORTED;
  738. item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
  739. /* item_lsn of -1 means the item needs no further processing */
  740. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  741. continue;
  742. /*
  743. * if we are aborting the operation, no point in inserting the
  744. * object into the AIL as we are in a shutdown situation.
  745. */
  746. if (aborted) {
  747. ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
  748. lip->li_ops->iop_unpin(lip, 1);
  749. continue;
  750. }
  751. if (item_lsn != commit_lsn) {
  752. /*
  753. * Not a bulk update option due to unusual item_lsn.
  754. * Push into AIL immediately, rechecking the lsn once
  755. * we have the ail lock. Then unpin the item. This does
  756. * not affect the AIL cursor the bulk insert path is
  757. * using.
  758. */
  759. spin_lock(&ailp->xa_lock);
  760. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
  761. xfs_trans_ail_update(ailp, lip, item_lsn);
  762. else
  763. spin_unlock(&ailp->xa_lock);
  764. lip->li_ops->iop_unpin(lip, 0);
  765. continue;
  766. }
  767. /* Item is a candidate for bulk AIL insert. */
  768. log_items[i++] = lv->lv_item;
  769. if (i >= LOG_ITEM_BATCH_SIZE) {
  770. xfs_log_item_batch_insert(ailp, &cur, log_items,
  771. LOG_ITEM_BATCH_SIZE, commit_lsn);
  772. i = 0;
  773. }
  774. }
  775. /* make sure we insert the remainder! */
  776. if (i)
  777. xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
  778. spin_lock(&ailp->xa_lock);
  779. xfs_trans_ail_cursor_done(&cur);
  780. spin_unlock(&ailp->xa_lock);
  781. }
  782. /*
  783. * Commit the given transaction to the log.
  784. *
  785. * XFS disk error handling mechanism is not based on a typical
  786. * transaction abort mechanism. Logically after the filesystem
  787. * gets marked 'SHUTDOWN', we can't let any new transactions
  788. * be durable - ie. committed to disk - because some metadata might
  789. * be inconsistent. In such cases, this returns an error, and the
  790. * caller may assume that all locked objects joined to the transaction
  791. * have already been unlocked as if the commit had succeeded.
  792. * Do not reference the transaction structure after this call.
  793. */
  794. static int
  795. __xfs_trans_commit(
  796. struct xfs_trans *tp,
  797. bool regrant)
  798. {
  799. struct xfs_mount *mp = tp->t_mountp;
  800. xfs_lsn_t commit_lsn = -1;
  801. int error = 0;
  802. int sync = tp->t_flags & XFS_TRANS_SYNC;
  803. /*
  804. * If there is nothing to be logged by the transaction,
  805. * then unlock all of the items associated with the
  806. * transaction and free the transaction structure.
  807. * Also make sure to return any reserved blocks to
  808. * the free pool.
  809. */
  810. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  811. goto out_unreserve;
  812. if (XFS_FORCED_SHUTDOWN(mp)) {
  813. error = -EIO;
  814. goto out_unreserve;
  815. }
  816. ASSERT(tp->t_ticket != NULL);
  817. /*
  818. * If we need to update the superblock, then do it now.
  819. */
  820. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  821. xfs_trans_apply_sb_deltas(tp);
  822. xfs_trans_apply_dquot_deltas(tp);
  823. xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
  824. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  825. xfs_trans_free(tp);
  826. /*
  827. * If the transaction needs to be synchronous, then force the
  828. * log out now and wait for it.
  829. */
  830. if (sync) {
  831. error = _xfs_log_force_lsn(mp, commit_lsn, XFS_LOG_SYNC, NULL);
  832. XFS_STATS_INC(mp, xs_trans_sync);
  833. } else {
  834. XFS_STATS_INC(mp, xs_trans_async);
  835. }
  836. return error;
  837. out_unreserve:
  838. xfs_trans_unreserve_and_mod_sb(tp);
  839. /*
  840. * It is indeed possible for the transaction to be not dirty but
  841. * the dqinfo portion to be. All that means is that we have some
  842. * (non-persistent) quota reservations that need to be unreserved.
  843. */
  844. xfs_trans_unreserve_and_mod_dquots(tp);
  845. if (tp->t_ticket) {
  846. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, regrant);
  847. if (commit_lsn == -1 && !error)
  848. error = -EIO;
  849. }
  850. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  851. xfs_trans_free_items(tp, NULLCOMMITLSN, !!error);
  852. xfs_trans_free(tp);
  853. XFS_STATS_INC(mp, xs_trans_empty);
  854. return error;
  855. }
  856. int
  857. xfs_trans_commit(
  858. struct xfs_trans *tp)
  859. {
  860. return __xfs_trans_commit(tp, false);
  861. }
  862. /*
  863. * Unlock all of the transaction's items and free the transaction.
  864. * The transaction must not have modified any of its items, because
  865. * there is no way to restore them to their previous state.
  866. *
  867. * If the transaction has made a log reservation, make sure to release
  868. * it as well.
  869. */
  870. void
  871. xfs_trans_cancel(
  872. struct xfs_trans *tp)
  873. {
  874. struct xfs_mount *mp = tp->t_mountp;
  875. bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
  876. /*
  877. * See if the caller is relying on us to shut down the
  878. * filesystem. This happens in paths where we detect
  879. * corruption and decide to give up.
  880. */
  881. if (dirty && !XFS_FORCED_SHUTDOWN(mp)) {
  882. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  883. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  884. }
  885. #ifdef DEBUG
  886. if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) {
  887. struct xfs_log_item_desc *lidp;
  888. list_for_each_entry(lidp, &tp->t_items, lid_trans)
  889. ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
  890. }
  891. #endif
  892. xfs_trans_unreserve_and_mod_sb(tp);
  893. xfs_trans_unreserve_and_mod_dquots(tp);
  894. if (tp->t_ticket)
  895. xfs_log_done(mp, tp->t_ticket, NULL, false);
  896. /* mark this thread as no longer being in a transaction */
  897. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  898. xfs_trans_free_items(tp, NULLCOMMITLSN, dirty);
  899. xfs_trans_free(tp);
  900. }
  901. /*
  902. * Roll from one trans in the sequence of PERMANENT transactions to
  903. * the next: permanent transactions are only flushed out when
  904. * committed with xfs_trans_commit(), but we still want as soon
  905. * as possible to let chunks of it go to the log. So we commit the
  906. * chunk we've been working on and get a new transaction to continue.
  907. */
  908. int
  909. __xfs_trans_roll(
  910. struct xfs_trans **tpp,
  911. struct xfs_inode *dp,
  912. int *committed)
  913. {
  914. struct xfs_trans *trans;
  915. struct xfs_trans_res tres;
  916. int error;
  917. *committed = 0;
  918. /*
  919. * Ensure that the inode is always logged.
  920. */
  921. trans = *tpp;
  922. if (dp)
  923. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  924. /*
  925. * Copy the critical parameters from one trans to the next.
  926. */
  927. tres.tr_logres = trans->t_log_res;
  928. tres.tr_logcount = trans->t_log_count;
  929. *tpp = xfs_trans_dup(trans);
  930. /*
  931. * Commit the current transaction.
  932. * If this commit failed, then it'd just unlock those items that
  933. * are not marked ihold. That also means that a filesystem shutdown
  934. * is in progress. The caller takes the responsibility to cancel
  935. * the duplicate transaction that gets returned.
  936. */
  937. error = __xfs_trans_commit(trans, true);
  938. if (error)
  939. return error;
  940. *committed = 1;
  941. trans = *tpp;
  942. /*
  943. * Reserve space in the log for th next transaction.
  944. * This also pushes items in the "AIL", the list of logged items,
  945. * out to disk if they are taking up space at the tail of the log
  946. * that we want to use. This requires that either nothing be locked
  947. * across this call, or that anything that is locked be logged in
  948. * the prior and the next transactions.
  949. */
  950. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  951. error = xfs_trans_reserve(trans, &tres, 0, 0);
  952. /*
  953. * Ensure that the inode is in the new transaction and locked.
  954. */
  955. if (error)
  956. return error;
  957. if (dp)
  958. xfs_trans_ijoin(trans, dp, 0);
  959. return 0;
  960. }
  961. int
  962. xfs_trans_roll(
  963. struct xfs_trans **tpp,
  964. struct xfs_inode *dp)
  965. {
  966. int committed;
  967. return __xfs_trans_roll(tpp, dp, &committed);
  968. }