xfs_trans.c 28 KB

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