xfs_trans.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. ASSERT(delta < 0);
  288. tp->t_res_fdblocks_delta += delta;
  289. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  290. flags &= ~XFS_TRANS_SB_DIRTY;
  291. break;
  292. case XFS_TRANS_SB_FREXTENTS:
  293. /*
  294. * Track the number of blocks allocated in the
  295. * transaction. Make sure it does not exceed the
  296. * number reserved.
  297. */
  298. if (delta < 0) {
  299. tp->t_rtx_res_used += (uint)-delta;
  300. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  301. }
  302. tp->t_frextents_delta += delta;
  303. break;
  304. case XFS_TRANS_SB_RES_FREXTENTS:
  305. /*
  306. * The allocation has already been applied to the
  307. * in-core superblock's counter. This should only
  308. * be applied to the on-disk superblock.
  309. */
  310. ASSERT(delta < 0);
  311. tp->t_res_frextents_delta += delta;
  312. break;
  313. case XFS_TRANS_SB_DBLOCKS:
  314. ASSERT(delta > 0);
  315. tp->t_dblocks_delta += delta;
  316. break;
  317. case XFS_TRANS_SB_AGCOUNT:
  318. ASSERT(delta > 0);
  319. tp->t_agcount_delta += delta;
  320. break;
  321. case XFS_TRANS_SB_IMAXPCT:
  322. tp->t_imaxpct_delta += delta;
  323. break;
  324. case XFS_TRANS_SB_REXTSIZE:
  325. tp->t_rextsize_delta += delta;
  326. break;
  327. case XFS_TRANS_SB_RBMBLOCKS:
  328. tp->t_rbmblocks_delta += delta;
  329. break;
  330. case XFS_TRANS_SB_RBLOCKS:
  331. tp->t_rblocks_delta += delta;
  332. break;
  333. case XFS_TRANS_SB_REXTENTS:
  334. tp->t_rextents_delta += delta;
  335. break;
  336. case XFS_TRANS_SB_REXTSLOG:
  337. tp->t_rextslog_delta += delta;
  338. break;
  339. default:
  340. ASSERT(0);
  341. return;
  342. }
  343. tp->t_flags |= flags;
  344. }
  345. /*
  346. * xfs_trans_apply_sb_deltas() is called from the commit code
  347. * to bring the superblock buffer into the current transaction
  348. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  349. *
  350. * For now we just look at each field allowed to change and change
  351. * it if necessary.
  352. */
  353. STATIC void
  354. xfs_trans_apply_sb_deltas(
  355. xfs_trans_t *tp)
  356. {
  357. xfs_dsb_t *sbp;
  358. xfs_buf_t *bp;
  359. int whole = 0;
  360. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  361. sbp = XFS_BUF_TO_SBP(bp);
  362. /*
  363. * Check that superblock mods match the mods made to AGF counters.
  364. */
  365. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  366. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  367. tp->t_ag_btree_delta));
  368. /*
  369. * Only update the superblock counters if we are logging them
  370. */
  371. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  372. if (tp->t_icount_delta)
  373. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  374. if (tp->t_ifree_delta)
  375. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  376. if (tp->t_fdblocks_delta)
  377. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  378. if (tp->t_res_fdblocks_delta)
  379. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  380. }
  381. if (tp->t_frextents_delta)
  382. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  383. if (tp->t_res_frextents_delta)
  384. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  385. if (tp->t_dblocks_delta) {
  386. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  387. whole = 1;
  388. }
  389. if (tp->t_agcount_delta) {
  390. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  391. whole = 1;
  392. }
  393. if (tp->t_imaxpct_delta) {
  394. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  395. whole = 1;
  396. }
  397. if (tp->t_rextsize_delta) {
  398. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  399. whole = 1;
  400. }
  401. if (tp->t_rbmblocks_delta) {
  402. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  403. whole = 1;
  404. }
  405. if (tp->t_rblocks_delta) {
  406. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  407. whole = 1;
  408. }
  409. if (tp->t_rextents_delta) {
  410. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  411. whole = 1;
  412. }
  413. if (tp->t_rextslog_delta) {
  414. sbp->sb_rextslog += tp->t_rextslog_delta;
  415. whole = 1;
  416. }
  417. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
  418. if (whole)
  419. /*
  420. * Log the whole thing, the fields are noncontiguous.
  421. */
  422. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  423. else
  424. /*
  425. * Since all the modifiable fields are contiguous, we
  426. * can get away with this.
  427. */
  428. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  429. offsetof(xfs_dsb_t, sb_frextents) +
  430. sizeof(sbp->sb_frextents) - 1);
  431. }
  432. STATIC int
  433. xfs_sb_mod8(
  434. uint8_t *field,
  435. int8_t delta)
  436. {
  437. int8_t counter = *field;
  438. counter += delta;
  439. if (counter < 0) {
  440. ASSERT(0);
  441. return -EINVAL;
  442. }
  443. *field = counter;
  444. return 0;
  445. }
  446. STATIC int
  447. xfs_sb_mod32(
  448. uint32_t *field,
  449. int32_t delta)
  450. {
  451. int32_t counter = *field;
  452. counter += delta;
  453. if (counter < 0) {
  454. ASSERT(0);
  455. return -EINVAL;
  456. }
  457. *field = counter;
  458. return 0;
  459. }
  460. STATIC int
  461. xfs_sb_mod64(
  462. uint64_t *field,
  463. int64_t delta)
  464. {
  465. int64_t counter = *field;
  466. counter += delta;
  467. if (counter < 0) {
  468. ASSERT(0);
  469. return -EINVAL;
  470. }
  471. *field = counter;
  472. return 0;
  473. }
  474. /*
  475. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  476. * and apply superblock counter changes to the in-core superblock. The
  477. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  478. * applied to the in-core superblock. The idea is that that has already been
  479. * done.
  480. *
  481. * If we are not logging superblock counters, then the inode allocated/free and
  482. * used block counts are not updated in the on disk superblock. In this case,
  483. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  484. * still need to update the incore superblock with the changes.
  485. */
  486. void
  487. xfs_trans_unreserve_and_mod_sb(
  488. struct xfs_trans *tp)
  489. {
  490. struct xfs_mount *mp = tp->t_mountp;
  491. bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  492. int64_t blkdelta = 0;
  493. int64_t rtxdelta = 0;
  494. int64_t idelta = 0;
  495. int64_t ifreedelta = 0;
  496. int error;
  497. /* calculate deltas */
  498. if (tp->t_blk_res > 0)
  499. blkdelta = tp->t_blk_res;
  500. if ((tp->t_fdblocks_delta != 0) &&
  501. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  502. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  503. blkdelta += tp->t_fdblocks_delta;
  504. if (tp->t_rtx_res > 0)
  505. rtxdelta = tp->t_rtx_res;
  506. if ((tp->t_frextents_delta != 0) &&
  507. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  508. rtxdelta += tp->t_frextents_delta;
  509. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  510. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  511. idelta = tp->t_icount_delta;
  512. ifreedelta = tp->t_ifree_delta;
  513. }
  514. /* apply the per-cpu counters */
  515. if (blkdelta) {
  516. error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
  517. if (error)
  518. goto out;
  519. }
  520. if (idelta) {
  521. error = xfs_mod_icount(mp, idelta);
  522. if (error)
  523. goto out_undo_fdblocks;
  524. }
  525. if (ifreedelta) {
  526. error = xfs_mod_ifree(mp, ifreedelta);
  527. if (error)
  528. goto out_undo_icount;
  529. }
  530. if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY))
  531. return;
  532. /* apply remaining deltas */
  533. spin_lock(&mp->m_sb_lock);
  534. if (rtxdelta) {
  535. error = xfs_sb_mod64(&mp->m_sb.sb_frextents, rtxdelta);
  536. if (error)
  537. goto out_undo_ifree;
  538. }
  539. if (tp->t_dblocks_delta != 0) {
  540. error = xfs_sb_mod64(&mp->m_sb.sb_dblocks, tp->t_dblocks_delta);
  541. if (error)
  542. goto out_undo_frextents;
  543. }
  544. if (tp->t_agcount_delta != 0) {
  545. error = xfs_sb_mod32(&mp->m_sb.sb_agcount, tp->t_agcount_delta);
  546. if (error)
  547. goto out_undo_dblocks;
  548. }
  549. if (tp->t_imaxpct_delta != 0) {
  550. error = xfs_sb_mod8(&mp->m_sb.sb_imax_pct, tp->t_imaxpct_delta);
  551. if (error)
  552. goto out_undo_agcount;
  553. }
  554. if (tp->t_rextsize_delta != 0) {
  555. error = xfs_sb_mod32(&mp->m_sb.sb_rextsize,
  556. tp->t_rextsize_delta);
  557. if (error)
  558. goto out_undo_imaxpct;
  559. }
  560. if (tp->t_rbmblocks_delta != 0) {
  561. error = xfs_sb_mod32(&mp->m_sb.sb_rbmblocks,
  562. tp->t_rbmblocks_delta);
  563. if (error)
  564. goto out_undo_rextsize;
  565. }
  566. if (tp->t_rblocks_delta != 0) {
  567. error = xfs_sb_mod64(&mp->m_sb.sb_rblocks, tp->t_rblocks_delta);
  568. if (error)
  569. goto out_undo_rbmblocks;
  570. }
  571. if (tp->t_rextents_delta != 0) {
  572. error = xfs_sb_mod64(&mp->m_sb.sb_rextents,
  573. tp->t_rextents_delta);
  574. if (error)
  575. goto out_undo_rblocks;
  576. }
  577. if (tp->t_rextslog_delta != 0) {
  578. error = xfs_sb_mod8(&mp->m_sb.sb_rextslog,
  579. tp->t_rextslog_delta);
  580. if (error)
  581. goto out_undo_rextents;
  582. }
  583. spin_unlock(&mp->m_sb_lock);
  584. return;
  585. out_undo_rextents:
  586. if (tp->t_rextents_delta)
  587. xfs_sb_mod64(&mp->m_sb.sb_rextents, -tp->t_rextents_delta);
  588. out_undo_rblocks:
  589. if (tp->t_rblocks_delta)
  590. xfs_sb_mod64(&mp->m_sb.sb_rblocks, -tp->t_rblocks_delta);
  591. out_undo_rbmblocks:
  592. if (tp->t_rbmblocks_delta)
  593. xfs_sb_mod32(&mp->m_sb.sb_rbmblocks, -tp->t_rbmblocks_delta);
  594. out_undo_rextsize:
  595. if (tp->t_rextsize_delta)
  596. xfs_sb_mod32(&mp->m_sb.sb_rextsize, -tp->t_rextsize_delta);
  597. out_undo_imaxpct:
  598. if (tp->t_rextsize_delta)
  599. xfs_sb_mod8(&mp->m_sb.sb_imax_pct, -tp->t_imaxpct_delta);
  600. out_undo_agcount:
  601. if (tp->t_agcount_delta)
  602. xfs_sb_mod32(&mp->m_sb.sb_agcount, -tp->t_agcount_delta);
  603. out_undo_dblocks:
  604. if (tp->t_dblocks_delta)
  605. xfs_sb_mod64(&mp->m_sb.sb_dblocks, -tp->t_dblocks_delta);
  606. out_undo_frextents:
  607. if (rtxdelta)
  608. xfs_sb_mod64(&mp->m_sb.sb_frextents, -rtxdelta);
  609. out_undo_ifree:
  610. spin_unlock(&mp->m_sb_lock);
  611. if (ifreedelta)
  612. xfs_mod_ifree(mp, -ifreedelta);
  613. out_undo_icount:
  614. if (idelta)
  615. xfs_mod_icount(mp, -idelta);
  616. out_undo_fdblocks:
  617. if (blkdelta)
  618. xfs_mod_fdblocks(mp, -blkdelta, rsvd);
  619. out:
  620. ASSERT(error == 0);
  621. return;
  622. }
  623. /*
  624. * Add the given log item to the transaction's list of log items.
  625. *
  626. * The log item will now point to its new descriptor with its li_desc field.
  627. */
  628. void
  629. xfs_trans_add_item(
  630. struct xfs_trans *tp,
  631. struct xfs_log_item *lip)
  632. {
  633. struct xfs_log_item_desc *lidp;
  634. ASSERT(lip->li_mountp == tp->t_mountp);
  635. ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
  636. lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
  637. lidp->lid_item = lip;
  638. lidp->lid_flags = 0;
  639. list_add_tail(&lidp->lid_trans, &tp->t_items);
  640. lip->li_desc = lidp;
  641. }
  642. STATIC void
  643. xfs_trans_free_item_desc(
  644. struct xfs_log_item_desc *lidp)
  645. {
  646. list_del_init(&lidp->lid_trans);
  647. kmem_zone_free(xfs_log_item_desc_zone, lidp);
  648. }
  649. /*
  650. * Unlink and free the given descriptor.
  651. */
  652. void
  653. xfs_trans_del_item(
  654. struct xfs_log_item *lip)
  655. {
  656. xfs_trans_free_item_desc(lip->li_desc);
  657. lip->li_desc = NULL;
  658. }
  659. /*
  660. * Unlock all of the items of a transaction and free all the descriptors
  661. * of that transaction.
  662. */
  663. void
  664. xfs_trans_free_items(
  665. struct xfs_trans *tp,
  666. xfs_lsn_t commit_lsn,
  667. bool abort)
  668. {
  669. struct xfs_log_item_desc *lidp, *next;
  670. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  671. struct xfs_log_item *lip = lidp->lid_item;
  672. lip->li_desc = NULL;
  673. if (commit_lsn != NULLCOMMITLSN)
  674. lip->li_ops->iop_committing(lip, commit_lsn);
  675. if (abort)
  676. lip->li_flags |= XFS_LI_ABORTED;
  677. lip->li_ops->iop_unlock(lip);
  678. xfs_trans_free_item_desc(lidp);
  679. }
  680. }
  681. static inline void
  682. xfs_log_item_batch_insert(
  683. struct xfs_ail *ailp,
  684. struct xfs_ail_cursor *cur,
  685. struct xfs_log_item **log_items,
  686. int nr_items,
  687. xfs_lsn_t commit_lsn)
  688. {
  689. int i;
  690. spin_lock(&ailp->xa_lock);
  691. /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
  692. xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
  693. for (i = 0; i < nr_items; i++) {
  694. struct xfs_log_item *lip = log_items[i];
  695. lip->li_ops->iop_unpin(lip, 0);
  696. }
  697. }
  698. /*
  699. * Bulk operation version of xfs_trans_committed that takes a log vector of
  700. * items to insert into the AIL. This uses bulk AIL insertion techniques to
  701. * minimise lock traffic.
  702. *
  703. * If we are called with the aborted flag set, it is because a log write during
  704. * a CIL checkpoint commit has failed. In this case, all the items in the
  705. * checkpoint have already gone through iop_commited and iop_unlock, which
  706. * means that checkpoint commit abort handling is treated exactly the same
  707. * as an iclog write error even though we haven't started any IO yet. Hence in
  708. * this case all we need to do is iop_committed processing, followed by an
  709. * iop_unpin(aborted) call.
  710. *
  711. * The AIL cursor is used to optimise the insert process. If commit_lsn is not
  712. * at the end of the AIL, the insert cursor avoids the need to walk
  713. * the AIL to find the insertion point on every xfs_log_item_batch_insert()
  714. * call. This saves a lot of needless list walking and is a net win, even
  715. * though it slightly increases that amount of AIL lock traffic to set it up
  716. * and tear it down.
  717. */
  718. void
  719. xfs_trans_committed_bulk(
  720. struct xfs_ail *ailp,
  721. struct xfs_log_vec *log_vector,
  722. xfs_lsn_t commit_lsn,
  723. int aborted)
  724. {
  725. #define LOG_ITEM_BATCH_SIZE 32
  726. struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
  727. struct xfs_log_vec *lv;
  728. struct xfs_ail_cursor cur;
  729. int i = 0;
  730. spin_lock(&ailp->xa_lock);
  731. xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
  732. spin_unlock(&ailp->xa_lock);
  733. /* unpin all the log items */
  734. for (lv = log_vector; lv; lv = lv->lv_next ) {
  735. struct xfs_log_item *lip = lv->lv_item;
  736. xfs_lsn_t item_lsn;
  737. if (aborted)
  738. lip->li_flags |= XFS_LI_ABORTED;
  739. item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
  740. /* item_lsn of -1 means the item needs no further processing */
  741. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  742. continue;
  743. /*
  744. * if we are aborting the operation, no point in inserting the
  745. * object into the AIL as we are in a shutdown situation.
  746. */
  747. if (aborted) {
  748. ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
  749. lip->li_ops->iop_unpin(lip, 1);
  750. continue;
  751. }
  752. if (item_lsn != commit_lsn) {
  753. /*
  754. * Not a bulk update option due to unusual item_lsn.
  755. * Push into AIL immediately, rechecking the lsn once
  756. * we have the ail lock. Then unpin the item. This does
  757. * not affect the AIL cursor the bulk insert path is
  758. * using.
  759. */
  760. spin_lock(&ailp->xa_lock);
  761. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
  762. xfs_trans_ail_update(ailp, lip, item_lsn);
  763. else
  764. spin_unlock(&ailp->xa_lock);
  765. lip->li_ops->iop_unpin(lip, 0);
  766. continue;
  767. }
  768. /* Item is a candidate for bulk AIL insert. */
  769. log_items[i++] = lv->lv_item;
  770. if (i >= LOG_ITEM_BATCH_SIZE) {
  771. xfs_log_item_batch_insert(ailp, &cur, log_items,
  772. LOG_ITEM_BATCH_SIZE, commit_lsn);
  773. i = 0;
  774. }
  775. }
  776. /* make sure we insert the remainder! */
  777. if (i)
  778. xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
  779. spin_lock(&ailp->xa_lock);
  780. xfs_trans_ail_cursor_done(&cur);
  781. spin_unlock(&ailp->xa_lock);
  782. }
  783. /*
  784. * Commit the given transaction to the log.
  785. *
  786. * XFS disk error handling mechanism is not based on a typical
  787. * transaction abort mechanism. Logically after the filesystem
  788. * gets marked 'SHUTDOWN', we can't let any new transactions
  789. * be durable - ie. committed to disk - because some metadata might
  790. * be inconsistent. In such cases, this returns an error, and the
  791. * caller may assume that all locked objects joined to the transaction
  792. * have already been unlocked as if the commit had succeeded.
  793. * Do not reference the transaction structure after this call.
  794. */
  795. static int
  796. __xfs_trans_commit(
  797. struct xfs_trans *tp,
  798. bool regrant)
  799. {
  800. struct xfs_mount *mp = tp->t_mountp;
  801. xfs_lsn_t commit_lsn = -1;
  802. int error = 0;
  803. int sync = tp->t_flags & XFS_TRANS_SYNC;
  804. /*
  805. * If there is nothing to be logged by the transaction,
  806. * then unlock all of the items associated with the
  807. * transaction and free the transaction structure.
  808. * Also make sure to return any reserved blocks to
  809. * the free pool.
  810. */
  811. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  812. goto out_unreserve;
  813. if (XFS_FORCED_SHUTDOWN(mp)) {
  814. error = -EIO;
  815. goto out_unreserve;
  816. }
  817. ASSERT(tp->t_ticket != NULL);
  818. /*
  819. * If we need to update the superblock, then do it now.
  820. */
  821. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  822. xfs_trans_apply_sb_deltas(tp);
  823. xfs_trans_apply_dquot_deltas(tp);
  824. xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
  825. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  826. xfs_trans_free(tp);
  827. /*
  828. * If the transaction needs to be synchronous, then force the
  829. * log out now and wait for it.
  830. */
  831. if (sync) {
  832. error = _xfs_log_force_lsn(mp, commit_lsn, XFS_LOG_SYNC, NULL);
  833. XFS_STATS_INC(mp, xs_trans_sync);
  834. } else {
  835. XFS_STATS_INC(mp, xs_trans_async);
  836. }
  837. return error;
  838. out_unreserve:
  839. xfs_trans_unreserve_and_mod_sb(tp);
  840. /*
  841. * It is indeed possible for the transaction to be not dirty but
  842. * the dqinfo portion to be. All that means is that we have some
  843. * (non-persistent) quota reservations that need to be unreserved.
  844. */
  845. xfs_trans_unreserve_and_mod_dquots(tp);
  846. if (tp->t_ticket) {
  847. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, regrant);
  848. if (commit_lsn == -1 && !error)
  849. error = -EIO;
  850. }
  851. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  852. xfs_trans_free_items(tp, NULLCOMMITLSN, !!error);
  853. xfs_trans_free(tp);
  854. XFS_STATS_INC(mp, xs_trans_empty);
  855. return error;
  856. }
  857. int
  858. xfs_trans_commit(
  859. struct xfs_trans *tp)
  860. {
  861. return __xfs_trans_commit(tp, false);
  862. }
  863. /*
  864. * Unlock all of the transaction's items and free the transaction.
  865. * The transaction must not have modified any of its items, because
  866. * there is no way to restore them to their previous state.
  867. *
  868. * If the transaction has made a log reservation, make sure to release
  869. * it as well.
  870. */
  871. void
  872. xfs_trans_cancel(
  873. struct xfs_trans *tp)
  874. {
  875. struct xfs_mount *mp = tp->t_mountp;
  876. bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
  877. /*
  878. * See if the caller is relying on us to shut down the
  879. * filesystem. This happens in paths where we detect
  880. * corruption and decide to give up.
  881. */
  882. if (dirty && !XFS_FORCED_SHUTDOWN(mp)) {
  883. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  884. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  885. }
  886. #ifdef DEBUG
  887. if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) {
  888. struct xfs_log_item_desc *lidp;
  889. list_for_each_entry(lidp, &tp->t_items, lid_trans)
  890. ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
  891. }
  892. #endif
  893. xfs_trans_unreserve_and_mod_sb(tp);
  894. xfs_trans_unreserve_and_mod_dquots(tp);
  895. if (tp->t_ticket)
  896. xfs_log_done(mp, tp->t_ticket, NULL, false);
  897. /* mark this thread as no longer being in a transaction */
  898. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  899. xfs_trans_free_items(tp, NULLCOMMITLSN, dirty);
  900. xfs_trans_free(tp);
  901. }
  902. /*
  903. * Roll from one trans in the sequence of PERMANENT transactions to
  904. * the next: permanent transactions are only flushed out when
  905. * committed with xfs_trans_commit(), but we still want as soon
  906. * as possible to let chunks of it go to the log. So we commit the
  907. * chunk we've been working on and get a new transaction to continue.
  908. */
  909. int
  910. __xfs_trans_roll(
  911. struct xfs_trans **tpp,
  912. struct xfs_inode *dp,
  913. int *committed)
  914. {
  915. struct xfs_trans *trans;
  916. struct xfs_trans_res tres;
  917. int error;
  918. *committed = 0;
  919. /*
  920. * Ensure that the inode is always logged.
  921. */
  922. trans = *tpp;
  923. if (dp)
  924. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  925. /*
  926. * Copy the critical parameters from one trans to the next.
  927. */
  928. tres.tr_logres = trans->t_log_res;
  929. tres.tr_logcount = trans->t_log_count;
  930. *tpp = xfs_trans_dup(trans);
  931. /*
  932. * Commit the current transaction.
  933. * If this commit failed, then it'd just unlock those items that
  934. * are not marked ihold. That also means that a filesystem shutdown
  935. * is in progress. The caller takes the responsibility to cancel
  936. * the duplicate transaction that gets returned.
  937. */
  938. error = __xfs_trans_commit(trans, true);
  939. if (error)
  940. return error;
  941. *committed = 1;
  942. trans = *tpp;
  943. /*
  944. * Reserve space in the log for th next transaction.
  945. * This also pushes items in the "AIL", the list of logged items,
  946. * out to disk if they are taking up space at the tail of the log
  947. * that we want to use. This requires that either nothing be locked
  948. * across this call, or that anything that is locked be logged in
  949. * the prior and the next transactions.
  950. */
  951. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  952. error = xfs_trans_reserve(trans, &tres, 0, 0);
  953. /*
  954. * Ensure that the inode is in the new transaction and locked.
  955. */
  956. if (error)
  957. return error;
  958. if (dp)
  959. xfs_trans_ijoin(trans, dp, 0);
  960. return 0;
  961. }
  962. int
  963. xfs_trans_roll(
  964. struct xfs_trans **tpp,
  965. struct xfs_inode *dp)
  966. {
  967. int committed;
  968. return __xfs_trans_roll(tpp, dp, &committed);
  969. }