xfs_trans.c 28 KB

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