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