xfs_trans.c 28 KB

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