xfs_trans.c 28 KB

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