xfs_trans.c 29 KB

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