xfs_trans.c 28 KB

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