xfs_trans.c 29 KB

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