xfs_trans.c 28 KB

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