xfs_trans.c 29 KB

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