xfs_trans.c 29 KB

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