xfs_trans.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 (XFS_ERROR(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 = XFS_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. if (whole)
  436. /*
  437. * Log the whole thing, the fields are noncontiguous.
  438. */
  439. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  440. else
  441. /*
  442. * Since all the modifiable fields are contiguous, we
  443. * can get away with this.
  444. */
  445. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  446. offsetof(xfs_dsb_t, sb_frextents) +
  447. sizeof(sbp->sb_frextents) - 1);
  448. }
  449. /*
  450. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  451. * and apply superblock counter changes to the in-core superblock. The
  452. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  453. * applied to the in-core superblock. The idea is that that has already been
  454. * done.
  455. *
  456. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  457. * However, we have to ensure that we only modify each superblock field only
  458. * once because the application of the delta values may not be atomic. That can
  459. * lead to ENOSPC races occurring if we have two separate modifcations of the
  460. * free space counter to put back the entire reservation and then take away
  461. * what we used.
  462. *
  463. * If we are not logging superblock counters, then the inode allocated/free and
  464. * used block counts are not updated in the on disk superblock. In this case,
  465. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  466. * still need to update the incore superblock with the changes.
  467. */
  468. void
  469. xfs_trans_unreserve_and_mod_sb(
  470. xfs_trans_t *tp)
  471. {
  472. xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
  473. xfs_mod_sb_t *msbp;
  474. xfs_mount_t *mp = tp->t_mountp;
  475. /* REFERENCED */
  476. int error;
  477. int rsvd;
  478. int64_t blkdelta = 0;
  479. int64_t rtxdelta = 0;
  480. int64_t idelta = 0;
  481. int64_t ifreedelta = 0;
  482. msbp = msb;
  483. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  484. /* calculate deltas */
  485. if (tp->t_blk_res > 0)
  486. blkdelta = tp->t_blk_res;
  487. if ((tp->t_fdblocks_delta != 0) &&
  488. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  489. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  490. blkdelta += tp->t_fdblocks_delta;
  491. if (tp->t_rtx_res > 0)
  492. rtxdelta = tp->t_rtx_res;
  493. if ((tp->t_frextents_delta != 0) &&
  494. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  495. rtxdelta += tp->t_frextents_delta;
  496. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  497. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  498. idelta = tp->t_icount_delta;
  499. ifreedelta = tp->t_ifree_delta;
  500. }
  501. /* apply the per-cpu counters */
  502. if (blkdelta) {
  503. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  504. blkdelta, rsvd);
  505. if (error)
  506. goto out;
  507. }
  508. if (idelta) {
  509. error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
  510. idelta, rsvd);
  511. if (error)
  512. goto out_undo_fdblocks;
  513. }
  514. if (ifreedelta) {
  515. error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
  516. ifreedelta, rsvd);
  517. if (error)
  518. goto out_undo_icount;
  519. }
  520. /* apply remaining deltas */
  521. if (rtxdelta != 0) {
  522. msbp->msb_field = XFS_SBS_FREXTENTS;
  523. msbp->msb_delta = rtxdelta;
  524. msbp++;
  525. }
  526. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  527. if (tp->t_dblocks_delta != 0) {
  528. msbp->msb_field = XFS_SBS_DBLOCKS;
  529. msbp->msb_delta = tp->t_dblocks_delta;
  530. msbp++;
  531. }
  532. if (tp->t_agcount_delta != 0) {
  533. msbp->msb_field = XFS_SBS_AGCOUNT;
  534. msbp->msb_delta = tp->t_agcount_delta;
  535. msbp++;
  536. }
  537. if (tp->t_imaxpct_delta != 0) {
  538. msbp->msb_field = XFS_SBS_IMAX_PCT;
  539. msbp->msb_delta = tp->t_imaxpct_delta;
  540. msbp++;
  541. }
  542. if (tp->t_rextsize_delta != 0) {
  543. msbp->msb_field = XFS_SBS_REXTSIZE;
  544. msbp->msb_delta = tp->t_rextsize_delta;
  545. msbp++;
  546. }
  547. if (tp->t_rbmblocks_delta != 0) {
  548. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  549. msbp->msb_delta = tp->t_rbmblocks_delta;
  550. msbp++;
  551. }
  552. if (tp->t_rblocks_delta != 0) {
  553. msbp->msb_field = XFS_SBS_RBLOCKS;
  554. msbp->msb_delta = tp->t_rblocks_delta;
  555. msbp++;
  556. }
  557. if (tp->t_rextents_delta != 0) {
  558. msbp->msb_field = XFS_SBS_REXTENTS;
  559. msbp->msb_delta = tp->t_rextents_delta;
  560. msbp++;
  561. }
  562. if (tp->t_rextslog_delta != 0) {
  563. msbp->msb_field = XFS_SBS_REXTSLOG;
  564. msbp->msb_delta = tp->t_rextslog_delta;
  565. msbp++;
  566. }
  567. }
  568. /*
  569. * If we need to change anything, do it.
  570. */
  571. if (msbp > msb) {
  572. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  573. (uint)(msbp - msb), rsvd);
  574. if (error)
  575. goto out_undo_ifreecount;
  576. }
  577. return;
  578. out_undo_ifreecount:
  579. if (ifreedelta)
  580. xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
  581. out_undo_icount:
  582. if (idelta)
  583. xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
  584. out_undo_fdblocks:
  585. if (blkdelta)
  586. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
  587. out:
  588. ASSERT(error == 0);
  589. return;
  590. }
  591. /*
  592. * Add the given log item to the transaction's list of log items.
  593. *
  594. * The log item will now point to its new descriptor with its li_desc field.
  595. */
  596. void
  597. xfs_trans_add_item(
  598. struct xfs_trans *tp,
  599. struct xfs_log_item *lip)
  600. {
  601. struct xfs_log_item_desc *lidp;
  602. ASSERT(lip->li_mountp == tp->t_mountp);
  603. ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
  604. lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
  605. lidp->lid_item = lip;
  606. lidp->lid_flags = 0;
  607. list_add_tail(&lidp->lid_trans, &tp->t_items);
  608. lip->li_desc = lidp;
  609. }
  610. STATIC void
  611. xfs_trans_free_item_desc(
  612. struct xfs_log_item_desc *lidp)
  613. {
  614. list_del_init(&lidp->lid_trans);
  615. kmem_zone_free(xfs_log_item_desc_zone, lidp);
  616. }
  617. /*
  618. * Unlink and free the given descriptor.
  619. */
  620. void
  621. xfs_trans_del_item(
  622. struct xfs_log_item *lip)
  623. {
  624. xfs_trans_free_item_desc(lip->li_desc);
  625. lip->li_desc = NULL;
  626. }
  627. /*
  628. * Unlock all of the items of a transaction and free all the descriptors
  629. * of that transaction.
  630. */
  631. void
  632. xfs_trans_free_items(
  633. struct xfs_trans *tp,
  634. xfs_lsn_t commit_lsn,
  635. int flags)
  636. {
  637. struct xfs_log_item_desc *lidp, *next;
  638. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  639. struct xfs_log_item *lip = lidp->lid_item;
  640. lip->li_desc = NULL;
  641. if (commit_lsn != NULLCOMMITLSN)
  642. lip->li_ops->iop_committing(lip, commit_lsn);
  643. if (flags & XFS_TRANS_ABORT)
  644. lip->li_flags |= XFS_LI_ABORTED;
  645. lip->li_ops->iop_unlock(lip);
  646. xfs_trans_free_item_desc(lidp);
  647. }
  648. }
  649. static inline void
  650. xfs_log_item_batch_insert(
  651. struct xfs_ail *ailp,
  652. struct xfs_ail_cursor *cur,
  653. struct xfs_log_item **log_items,
  654. int nr_items,
  655. xfs_lsn_t commit_lsn)
  656. {
  657. int i;
  658. spin_lock(&ailp->xa_lock);
  659. /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
  660. xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
  661. for (i = 0; i < nr_items; i++) {
  662. struct xfs_log_item *lip = log_items[i];
  663. lip->li_ops->iop_unpin(lip, 0);
  664. }
  665. }
  666. /*
  667. * Bulk operation version of xfs_trans_committed that takes a log vector of
  668. * items to insert into the AIL. This uses bulk AIL insertion techniques to
  669. * minimise lock traffic.
  670. *
  671. * If we are called with the aborted flag set, it is because a log write during
  672. * a CIL checkpoint commit has failed. In this case, all the items in the
  673. * checkpoint have already gone through iop_commited and iop_unlock, which
  674. * means that checkpoint commit abort handling is treated exactly the same
  675. * as an iclog write error even though we haven't started any IO yet. Hence in
  676. * this case all we need to do is iop_committed processing, followed by an
  677. * iop_unpin(aborted) call.
  678. *
  679. * The AIL cursor is used to optimise the insert process. If commit_lsn is not
  680. * at the end of the AIL, the insert cursor avoids the need to walk
  681. * the AIL to find the insertion point on every xfs_log_item_batch_insert()
  682. * call. This saves a lot of needless list walking and is a net win, even
  683. * though it slightly increases that amount of AIL lock traffic to set it up
  684. * and tear it down.
  685. */
  686. void
  687. xfs_trans_committed_bulk(
  688. struct xfs_ail *ailp,
  689. struct xfs_log_vec *log_vector,
  690. xfs_lsn_t commit_lsn,
  691. int aborted)
  692. {
  693. #define LOG_ITEM_BATCH_SIZE 32
  694. struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
  695. struct xfs_log_vec *lv;
  696. struct xfs_ail_cursor cur;
  697. int i = 0;
  698. spin_lock(&ailp->xa_lock);
  699. xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
  700. spin_unlock(&ailp->xa_lock);
  701. /* unpin all the log items */
  702. for (lv = log_vector; lv; lv = lv->lv_next ) {
  703. struct xfs_log_item *lip = lv->lv_item;
  704. xfs_lsn_t item_lsn;
  705. if (aborted)
  706. lip->li_flags |= XFS_LI_ABORTED;
  707. item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
  708. /* item_lsn of -1 means the item needs no further processing */
  709. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  710. continue;
  711. /*
  712. * if we are aborting the operation, no point in inserting the
  713. * object into the AIL as we are in a shutdown situation.
  714. */
  715. if (aborted) {
  716. ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
  717. lip->li_ops->iop_unpin(lip, 1);
  718. continue;
  719. }
  720. if (item_lsn != commit_lsn) {
  721. /*
  722. * Not a bulk update option due to unusual item_lsn.
  723. * Push into AIL immediately, rechecking the lsn once
  724. * we have the ail lock. Then unpin the item. This does
  725. * not affect the AIL cursor the bulk insert path is
  726. * using.
  727. */
  728. spin_lock(&ailp->xa_lock);
  729. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
  730. xfs_trans_ail_update(ailp, lip, item_lsn);
  731. else
  732. spin_unlock(&ailp->xa_lock);
  733. lip->li_ops->iop_unpin(lip, 0);
  734. continue;
  735. }
  736. /* Item is a candidate for bulk AIL insert. */
  737. log_items[i++] = lv->lv_item;
  738. if (i >= LOG_ITEM_BATCH_SIZE) {
  739. xfs_log_item_batch_insert(ailp, &cur, log_items,
  740. LOG_ITEM_BATCH_SIZE, commit_lsn);
  741. i = 0;
  742. }
  743. }
  744. /* make sure we insert the remainder! */
  745. if (i)
  746. xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
  747. spin_lock(&ailp->xa_lock);
  748. xfs_trans_ail_cursor_done(ailp, &cur);
  749. spin_unlock(&ailp->xa_lock);
  750. }
  751. /*
  752. * Commit the given transaction to the log.
  753. *
  754. * XFS disk error handling mechanism is not based on a typical
  755. * transaction abort mechanism. Logically after the filesystem
  756. * gets marked 'SHUTDOWN', we can't let any new transactions
  757. * be durable - ie. committed to disk - because some metadata might
  758. * be inconsistent. In such cases, this returns an error, and the
  759. * caller may assume that all locked objects joined to the transaction
  760. * have already been unlocked as if the commit had succeeded.
  761. * Do not reference the transaction structure after this call.
  762. */
  763. int
  764. xfs_trans_commit(
  765. struct xfs_trans *tp,
  766. uint flags)
  767. {
  768. struct xfs_mount *mp = tp->t_mountp;
  769. xfs_lsn_t commit_lsn = -1;
  770. int error = 0;
  771. int log_flags = 0;
  772. int sync = tp->t_flags & XFS_TRANS_SYNC;
  773. /*
  774. * Determine whether this commit is releasing a permanent
  775. * log reservation or not.
  776. */
  777. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  778. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  779. log_flags = XFS_LOG_REL_PERM_RESERV;
  780. }
  781. /*
  782. * If there is nothing to be logged by the transaction,
  783. * then unlock all of the items associated with the
  784. * transaction and free the transaction structure.
  785. * Also make sure to return any reserved blocks to
  786. * the free pool.
  787. */
  788. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  789. goto out_unreserve;
  790. if (XFS_FORCED_SHUTDOWN(mp)) {
  791. error = XFS_ERROR(EIO);
  792. goto out_unreserve;
  793. }
  794. ASSERT(tp->t_ticket != NULL);
  795. /*
  796. * If we need to update the superblock, then do it now.
  797. */
  798. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  799. xfs_trans_apply_sb_deltas(tp);
  800. xfs_trans_apply_dquot_deltas(tp);
  801. error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
  802. if (error == ENOMEM) {
  803. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  804. error = XFS_ERROR(EIO);
  805. goto out_unreserve;
  806. }
  807. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  808. xfs_trans_free(tp);
  809. /*
  810. * If the transaction needs to be synchronous, then force the
  811. * log out now and wait for it.
  812. */
  813. if (sync) {
  814. if (!error) {
  815. error = _xfs_log_force_lsn(mp, commit_lsn,
  816. XFS_LOG_SYNC, NULL);
  817. }
  818. XFS_STATS_INC(xs_trans_sync);
  819. } else {
  820. XFS_STATS_INC(xs_trans_async);
  821. }
  822. return error;
  823. out_unreserve:
  824. xfs_trans_unreserve_and_mod_sb(tp);
  825. /*
  826. * It is indeed possible for the transaction to be not dirty but
  827. * the dqinfo portion to be. All that means is that we have some
  828. * (non-persistent) quota reservations that need to be unreserved.
  829. */
  830. xfs_trans_unreserve_and_mod_dquots(tp);
  831. if (tp->t_ticket) {
  832. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  833. if (commit_lsn == -1 && !error)
  834. error = XFS_ERROR(EIO);
  835. }
  836. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  837. xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
  838. xfs_trans_free(tp);
  839. XFS_STATS_INC(xs_trans_empty);
  840. return error;
  841. }
  842. /*
  843. * Unlock all of the transaction's items and free the transaction.
  844. * The transaction must not have modified any of its items, because
  845. * there is no way to restore them to their previous state.
  846. *
  847. * If the transaction has made a log reservation, make sure to release
  848. * it as well.
  849. */
  850. void
  851. xfs_trans_cancel(
  852. xfs_trans_t *tp,
  853. int flags)
  854. {
  855. int log_flags;
  856. xfs_mount_t *mp = tp->t_mountp;
  857. /*
  858. * See if the caller is being too lazy to figure out if
  859. * the transaction really needs an abort.
  860. */
  861. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  862. flags &= ~XFS_TRANS_ABORT;
  863. /*
  864. * See if the caller is relying on us to shut down the
  865. * filesystem. This happens in paths where we detect
  866. * corruption and decide to give up.
  867. */
  868. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  869. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  870. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  871. }
  872. #ifdef DEBUG
  873. if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
  874. struct xfs_log_item_desc *lidp;
  875. list_for_each_entry(lidp, &tp->t_items, lid_trans)
  876. ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
  877. }
  878. #endif
  879. xfs_trans_unreserve_and_mod_sb(tp);
  880. xfs_trans_unreserve_and_mod_dquots(tp);
  881. if (tp->t_ticket) {
  882. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  883. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  884. log_flags = XFS_LOG_REL_PERM_RESERV;
  885. } else {
  886. log_flags = 0;
  887. }
  888. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  889. }
  890. /* mark this thread as no longer being in a transaction */
  891. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  892. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  893. xfs_trans_free(tp);
  894. }
  895. /*
  896. * Roll from one trans in the sequence of PERMANENT transactions to
  897. * the next: permanent transactions are only flushed out when
  898. * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
  899. * as possible to let chunks of it go to the log. So we commit the
  900. * chunk we've been working on and get a new transaction to continue.
  901. */
  902. int
  903. xfs_trans_roll(
  904. struct xfs_trans **tpp,
  905. struct xfs_inode *dp)
  906. {
  907. struct xfs_trans *trans;
  908. struct xfs_trans_res tres;
  909. int error;
  910. /*
  911. * Ensure that the inode is always logged.
  912. */
  913. trans = *tpp;
  914. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  915. /*
  916. * Copy the critical parameters from one trans to the next.
  917. */
  918. tres.tr_logres = trans->t_log_res;
  919. tres.tr_logcount = trans->t_log_count;
  920. *tpp = xfs_trans_dup(trans);
  921. /*
  922. * Commit the current transaction.
  923. * If this commit failed, then it'd just unlock those items that
  924. * are not marked ihold. That also means that a filesystem shutdown
  925. * is in progress. The caller takes the responsibility to cancel
  926. * the duplicate transaction that gets returned.
  927. */
  928. error = xfs_trans_commit(trans, 0);
  929. if (error)
  930. return (error);
  931. trans = *tpp;
  932. /*
  933. * transaction commit worked ok so we can drop the extra ticket
  934. * reference that we gained in xfs_trans_dup()
  935. */
  936. xfs_log_ticket_put(trans->t_ticket);
  937. /*
  938. * Reserve space in the log for th next transaction.
  939. * This also pushes items in the "AIL", the list of logged items,
  940. * out to disk if they are taking up space at the tail of the log
  941. * that we want to use. This requires that either nothing be locked
  942. * across this call, or that anything that is locked be logged in
  943. * the prior and the next transactions.
  944. */
  945. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  946. error = xfs_trans_reserve(trans, &tres, 0, 0);
  947. /*
  948. * Ensure that the inode is in the new transaction and locked.
  949. */
  950. if (error)
  951. return error;
  952. xfs_trans_ijoin(trans, dp, 0);
  953. return 0;
  954. }