xfs_trans.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  4. * Copyright (C) 2010 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_extent_busy.h"
  15. #include "xfs_quota.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_trans_priv.h"
  18. #include "xfs_log.h"
  19. #include "xfs_log_priv.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_error.h"
  22. #include "xfs_defer.h"
  23. #include "xfs_inode.h"
  24. #include "xfs_dquot_item.h"
  25. #include "xfs_dquot.h"
  26. #include "xfs_icache.h"
  27. struct kmem_cache *xfs_trans_cache;
  28. #if defined(CONFIG_TRACEPOINTS)
  29. static void
  30. xfs_trans_trace_reservations(
  31. struct xfs_mount *mp)
  32. {
  33. struct xfs_trans_res *res;
  34. struct xfs_trans_res *end_res;
  35. int i;
  36. res = (struct xfs_trans_res *)M_RES(mp);
  37. end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
  38. for (i = 0; res < end_res; i++, res++)
  39. trace_xfs_trans_resv_calc(mp, i, res);
  40. }
  41. #else
  42. # define xfs_trans_trace_reservations(mp)
  43. #endif
  44. /*
  45. * Initialize the precomputed transaction reservation values
  46. * in the mount structure.
  47. */
  48. void
  49. xfs_trans_init(
  50. struct xfs_mount *mp)
  51. {
  52. xfs_trans_resv_calc(mp, M_RES(mp));
  53. xfs_trans_trace_reservations(mp);
  54. }
  55. /*
  56. * Free the transaction structure. If there is more clean up
  57. * to do when the structure is freed, add it here.
  58. */
  59. STATIC void
  60. xfs_trans_free(
  61. struct xfs_trans *tp)
  62. {
  63. xfs_extent_busy_sort(&tp->t_busy);
  64. xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
  65. trace_xfs_trans_free(tp, _RET_IP_);
  66. xfs_trans_clear_context(tp);
  67. if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
  68. sb_end_intwrite(tp->t_mountp->m_super);
  69. xfs_trans_free_dqinfo(tp);
  70. kmem_cache_free(xfs_trans_cache, tp);
  71. }
  72. /*
  73. * This is called to create a new transaction which will share the
  74. * permanent log reservation of the given transaction. The remaining
  75. * unused block and rt extent reservations are also inherited. This
  76. * implies that the original transaction is no longer allowed to allocate
  77. * blocks. Locks and log items, however, are no inherited. They must
  78. * be added to the new transaction explicitly.
  79. */
  80. STATIC struct xfs_trans *
  81. xfs_trans_dup(
  82. struct xfs_trans *tp)
  83. {
  84. struct xfs_trans *ntp;
  85. trace_xfs_trans_dup(tp, _RET_IP_);
  86. ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
  87. /*
  88. * Initialize the new transaction structure.
  89. */
  90. ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
  91. ntp->t_mountp = tp->t_mountp;
  92. INIT_LIST_HEAD(&ntp->t_items);
  93. INIT_LIST_HEAD(&ntp->t_busy);
  94. INIT_LIST_HEAD(&ntp->t_dfops);
  95. ntp->t_firstblock = NULLFSBLOCK;
  96. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  97. ASSERT(tp->t_ticket != NULL);
  98. ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
  99. (tp->t_flags & XFS_TRANS_RESERVE) |
  100. (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) |
  101. (tp->t_flags & XFS_TRANS_RES_FDBLKS);
  102. /* We gave our writer reference to the new transaction */
  103. tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
  104. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  105. ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
  106. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  107. tp->t_blk_res = tp->t_blk_res_used;
  108. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  109. tp->t_rtx_res = tp->t_rtx_res_used;
  110. xfs_trans_switch_context(tp, ntp);
  111. /* move deferred ops over to the new tp */
  112. xfs_defer_move(ntp, tp);
  113. xfs_trans_dup_dqinfo(tp, ntp);
  114. return ntp;
  115. }
  116. /*
  117. * This is called to reserve free disk blocks and log space for the
  118. * given transaction. This must be done before allocating any resources
  119. * within the transaction.
  120. *
  121. * This will return ENOSPC if there are not enough blocks available.
  122. * It will sleep waiting for available log space.
  123. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  124. * is used by long running transactions. If any one of the reservations
  125. * fails then they will all be backed out.
  126. *
  127. * This does not do quota reservations. That typically is done by the
  128. * caller afterwards.
  129. */
  130. static int
  131. xfs_trans_reserve(
  132. struct xfs_trans *tp,
  133. struct xfs_trans_res *resp,
  134. uint blocks,
  135. uint rtextents)
  136. {
  137. struct xfs_mount *mp = tp->t_mountp;
  138. int error = 0;
  139. bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  140. /*
  141. * Attempt to reserve the needed disk blocks by decrementing
  142. * the number needed from the number available. This will
  143. * fail if the count would go below zero.
  144. */
  145. if (blocks > 0) {
  146. error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
  147. if (error != 0)
  148. return -ENOSPC;
  149. tp->t_blk_res += blocks;
  150. }
  151. /*
  152. * Reserve the log space needed for this transaction.
  153. */
  154. if (resp->tr_logres > 0) {
  155. bool permanent = false;
  156. ASSERT(tp->t_log_res == 0 ||
  157. tp->t_log_res == resp->tr_logres);
  158. ASSERT(tp->t_log_count == 0 ||
  159. tp->t_log_count == resp->tr_logcount);
  160. if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
  161. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  162. permanent = true;
  163. } else {
  164. ASSERT(tp->t_ticket == NULL);
  165. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  166. }
  167. if (tp->t_ticket != NULL) {
  168. ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
  169. error = xfs_log_regrant(mp, tp->t_ticket);
  170. } else {
  171. error = xfs_log_reserve(mp, resp->tr_logres,
  172. resp->tr_logcount,
  173. &tp->t_ticket, permanent);
  174. }
  175. if (error)
  176. goto undo_blocks;
  177. tp->t_log_res = resp->tr_logres;
  178. tp->t_log_count = resp->tr_logcount;
  179. }
  180. /*
  181. * Attempt to reserve the needed realtime extents by decrementing
  182. * the number needed from the number available. This will
  183. * fail if the count would go below zero.
  184. */
  185. if (rtextents > 0) {
  186. error = xfs_mod_frextents(mp, -((int64_t)rtextents));
  187. if (error) {
  188. error = -ENOSPC;
  189. goto undo_log;
  190. }
  191. tp->t_rtx_res += rtextents;
  192. }
  193. return 0;
  194. /*
  195. * Error cases jump to one of these labels to undo any
  196. * reservations which have already been performed.
  197. */
  198. undo_log:
  199. if (resp->tr_logres > 0) {
  200. xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
  201. tp->t_ticket = NULL;
  202. tp->t_log_res = 0;
  203. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  204. }
  205. undo_blocks:
  206. if (blocks > 0) {
  207. xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
  208. tp->t_blk_res = 0;
  209. }
  210. return error;
  211. }
  212. int
  213. xfs_trans_alloc(
  214. struct xfs_mount *mp,
  215. struct xfs_trans_res *resp,
  216. uint blocks,
  217. uint rtextents,
  218. uint flags,
  219. struct xfs_trans **tpp)
  220. {
  221. struct xfs_trans *tp;
  222. bool want_retry = true;
  223. int error;
  224. /*
  225. * Allocate the handle before we do our freeze accounting and setting up
  226. * GFP_NOFS allocation context so that we avoid lockdep false positives
  227. * by doing GFP_KERNEL allocations inside sb_start_intwrite().
  228. */
  229. retry:
  230. tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
  231. if (!(flags & XFS_TRANS_NO_WRITECOUNT))
  232. sb_start_intwrite(mp->m_super);
  233. xfs_trans_set_context(tp);
  234. /*
  235. * Zero-reservation ("empty") transactions can't modify anything, so
  236. * they're allowed to run while we're frozen.
  237. */
  238. WARN_ON(resp->tr_logres > 0 &&
  239. mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
  240. ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) ||
  241. xfs_has_lazysbcount(mp));
  242. tp->t_magic = XFS_TRANS_HEADER_MAGIC;
  243. tp->t_flags = flags;
  244. tp->t_mountp = mp;
  245. INIT_LIST_HEAD(&tp->t_items);
  246. INIT_LIST_HEAD(&tp->t_busy);
  247. INIT_LIST_HEAD(&tp->t_dfops);
  248. tp->t_firstblock = NULLFSBLOCK;
  249. error = xfs_trans_reserve(tp, resp, blocks, rtextents);
  250. if (error == -ENOSPC && want_retry) {
  251. xfs_trans_cancel(tp);
  252. /*
  253. * We weren't able to reserve enough space for the transaction.
  254. * Flush the other speculative space allocations to free space.
  255. * Do not perform a synchronous scan because callers can hold
  256. * other locks.
  257. */
  258. xfs_blockgc_flush_all(mp);
  259. want_retry = false;
  260. goto retry;
  261. }
  262. if (error) {
  263. xfs_trans_cancel(tp);
  264. return error;
  265. }
  266. trace_xfs_trans_alloc(tp, _RET_IP_);
  267. *tpp = tp;
  268. return 0;
  269. }
  270. /*
  271. * Create an empty transaction with no reservation. This is a defensive
  272. * mechanism for routines that query metadata without actually modifying them --
  273. * if the metadata being queried is somehow cross-linked (think a btree block
  274. * pointer that points higher in the tree), we risk deadlock. However, blocks
  275. * grabbed as part of a transaction can be re-grabbed. The verifiers will
  276. * notice the corrupt block and the operation will fail back to userspace
  277. * without deadlocking.
  278. *
  279. * Note the zero-length reservation; this transaction MUST be cancelled without
  280. * any dirty data.
  281. *
  282. * Callers should obtain freeze protection to avoid a conflict with fs freezing
  283. * where we can be grabbing buffers at the same time that freeze is trying to
  284. * drain the buffer LRU list.
  285. */
  286. int
  287. xfs_trans_alloc_empty(
  288. struct xfs_mount *mp,
  289. struct xfs_trans **tpp)
  290. {
  291. struct xfs_trans_res resv = {0};
  292. return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
  293. }
  294. /*
  295. * Record the indicated change to the given field for application
  296. * to the file system's superblock when the transaction commits.
  297. * For now, just store the change in the transaction structure.
  298. *
  299. * Mark the transaction structure to indicate that the superblock
  300. * needs to be updated before committing.
  301. *
  302. * Because we may not be keeping track of allocated/free inodes and
  303. * used filesystem blocks in the superblock, we do not mark the
  304. * superblock dirty in this transaction if we modify these fields.
  305. * We still need to update the transaction deltas so that they get
  306. * applied to the incore superblock, but we don't want them to
  307. * cause the superblock to get locked and logged if these are the
  308. * only fields in the superblock that the transaction modifies.
  309. */
  310. void
  311. xfs_trans_mod_sb(
  312. xfs_trans_t *tp,
  313. uint field,
  314. int64_t delta)
  315. {
  316. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  317. xfs_mount_t *mp = tp->t_mountp;
  318. switch (field) {
  319. case XFS_TRANS_SB_ICOUNT:
  320. tp->t_icount_delta += delta;
  321. if (xfs_has_lazysbcount(mp))
  322. flags &= ~XFS_TRANS_SB_DIRTY;
  323. break;
  324. case XFS_TRANS_SB_IFREE:
  325. tp->t_ifree_delta += delta;
  326. if (xfs_has_lazysbcount(mp))
  327. flags &= ~XFS_TRANS_SB_DIRTY;
  328. break;
  329. case XFS_TRANS_SB_FDBLOCKS:
  330. /*
  331. * Track the number of blocks allocated in the transaction.
  332. * Make sure it does not exceed the number reserved. If so,
  333. * shutdown as this can lead to accounting inconsistency.
  334. */
  335. if (delta < 0) {
  336. tp->t_blk_res_used += (uint)-delta;
  337. if (tp->t_blk_res_used > tp->t_blk_res)
  338. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  339. } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) {
  340. int64_t blkres_delta;
  341. /*
  342. * Return freed blocks directly to the reservation
  343. * instead of the global pool, being careful not to
  344. * overflow the trans counter. This is used to preserve
  345. * reservation across chains of transaction rolls that
  346. * repeatedly free and allocate blocks.
  347. */
  348. blkres_delta = min_t(int64_t, delta,
  349. UINT_MAX - tp->t_blk_res);
  350. tp->t_blk_res += blkres_delta;
  351. delta -= blkres_delta;
  352. }
  353. tp->t_fdblocks_delta += delta;
  354. if (xfs_has_lazysbcount(mp))
  355. flags &= ~XFS_TRANS_SB_DIRTY;
  356. break;
  357. case XFS_TRANS_SB_RES_FDBLOCKS:
  358. /*
  359. * The allocation has already been applied to the
  360. * in-core superblock's counter. This should only
  361. * be applied to the on-disk superblock.
  362. */
  363. tp->t_res_fdblocks_delta += delta;
  364. if (xfs_has_lazysbcount(mp))
  365. flags &= ~XFS_TRANS_SB_DIRTY;
  366. break;
  367. case XFS_TRANS_SB_FREXTENTS:
  368. /*
  369. * Track the number of blocks allocated in the
  370. * transaction. Make sure it does not exceed the
  371. * number reserved.
  372. */
  373. if (delta < 0) {
  374. tp->t_rtx_res_used += (uint)-delta;
  375. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  376. }
  377. tp->t_frextents_delta += delta;
  378. break;
  379. case XFS_TRANS_SB_RES_FREXTENTS:
  380. /*
  381. * The allocation has already been applied to the
  382. * in-core superblock's counter. This should only
  383. * be applied to the on-disk superblock.
  384. */
  385. ASSERT(delta < 0);
  386. tp->t_res_frextents_delta += delta;
  387. break;
  388. case XFS_TRANS_SB_DBLOCKS:
  389. tp->t_dblocks_delta += delta;
  390. break;
  391. case XFS_TRANS_SB_AGCOUNT:
  392. ASSERT(delta > 0);
  393. tp->t_agcount_delta += delta;
  394. break;
  395. case XFS_TRANS_SB_IMAXPCT:
  396. tp->t_imaxpct_delta += delta;
  397. break;
  398. case XFS_TRANS_SB_REXTSIZE:
  399. tp->t_rextsize_delta += delta;
  400. break;
  401. case XFS_TRANS_SB_RBMBLOCKS:
  402. tp->t_rbmblocks_delta += delta;
  403. break;
  404. case XFS_TRANS_SB_RBLOCKS:
  405. tp->t_rblocks_delta += delta;
  406. break;
  407. case XFS_TRANS_SB_REXTENTS:
  408. tp->t_rextents_delta += delta;
  409. break;
  410. case XFS_TRANS_SB_REXTSLOG:
  411. tp->t_rextslog_delta += delta;
  412. break;
  413. default:
  414. ASSERT(0);
  415. return;
  416. }
  417. tp->t_flags |= flags;
  418. }
  419. /*
  420. * xfs_trans_apply_sb_deltas() is called from the commit code
  421. * to bring the superblock buffer into the current transaction
  422. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  423. *
  424. * For now we just look at each field allowed to change and change
  425. * it if necessary.
  426. */
  427. STATIC void
  428. xfs_trans_apply_sb_deltas(
  429. xfs_trans_t *tp)
  430. {
  431. struct xfs_dsb *sbp;
  432. struct xfs_buf *bp;
  433. int whole = 0;
  434. bp = xfs_trans_getsb(tp);
  435. sbp = bp->b_addr;
  436. /*
  437. * Only update the superblock counters if we are logging them
  438. */
  439. if (!xfs_has_lazysbcount((tp->t_mountp))) {
  440. if (tp->t_icount_delta)
  441. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  442. if (tp->t_ifree_delta)
  443. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  444. if (tp->t_fdblocks_delta)
  445. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  446. if (tp->t_res_fdblocks_delta)
  447. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  448. }
  449. /*
  450. * Updating frextents requires careful handling because it does not
  451. * behave like the lazysb counters because we cannot rely on log
  452. * recovery in older kenels to recompute the value from the rtbitmap.
  453. * This means that the ondisk frextents must be consistent with the
  454. * rtbitmap.
  455. *
  456. * Therefore, log the frextents change to the ondisk superblock and
  457. * update the incore superblock so that future calls to xfs_log_sb
  458. * write the correct value ondisk.
  459. *
  460. * Don't touch m_frextents because it includes incore reservations,
  461. * and those are handled by the unreserve function.
  462. */
  463. if (tp->t_frextents_delta || tp->t_res_frextents_delta) {
  464. struct xfs_mount *mp = tp->t_mountp;
  465. int64_t rtxdelta;
  466. rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta;
  467. spin_lock(&mp->m_sb_lock);
  468. be64_add_cpu(&sbp->sb_frextents, rtxdelta);
  469. mp->m_sb.sb_frextents += rtxdelta;
  470. spin_unlock(&mp->m_sb_lock);
  471. }
  472. if (tp->t_dblocks_delta) {
  473. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  474. whole = 1;
  475. }
  476. if (tp->t_agcount_delta) {
  477. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  478. whole = 1;
  479. }
  480. if (tp->t_imaxpct_delta) {
  481. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  482. whole = 1;
  483. }
  484. if (tp->t_rextsize_delta) {
  485. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  486. whole = 1;
  487. }
  488. if (tp->t_rbmblocks_delta) {
  489. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  490. whole = 1;
  491. }
  492. if (tp->t_rblocks_delta) {
  493. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  494. whole = 1;
  495. }
  496. if (tp->t_rextents_delta) {
  497. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  498. whole = 1;
  499. }
  500. if (tp->t_rextslog_delta) {
  501. sbp->sb_rextslog += tp->t_rextslog_delta;
  502. whole = 1;
  503. }
  504. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
  505. if (whole)
  506. /*
  507. * Log the whole thing, the fields are noncontiguous.
  508. */
  509. xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
  510. else
  511. /*
  512. * Since all the modifiable fields are contiguous, we
  513. * can get away with this.
  514. */
  515. xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount),
  516. offsetof(struct xfs_dsb, sb_frextents) +
  517. sizeof(sbp->sb_frextents) - 1);
  518. }
  519. /*
  520. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and
  521. * apply superblock counter changes to the in-core superblock. The
  522. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  523. * applied to the in-core superblock. The idea is that that has already been
  524. * done.
  525. *
  526. * If we are not logging superblock counters, then the inode allocated/free and
  527. * used block counts are not updated in the on disk superblock. In this case,
  528. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  529. * still need to update the incore superblock with the changes.
  530. *
  531. * Deltas for the inode count are +/-64, hence we use a large batch size of 128
  532. * so we don't need to take the counter lock on every update.
  533. */
  534. #define XFS_ICOUNT_BATCH 128
  535. void
  536. xfs_trans_unreserve_and_mod_sb(
  537. struct xfs_trans *tp)
  538. {
  539. struct xfs_mount *mp = tp->t_mountp;
  540. bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  541. int64_t blkdelta = 0;
  542. int64_t rtxdelta = 0;
  543. int64_t idelta = 0;
  544. int64_t ifreedelta = 0;
  545. int error;
  546. /* calculate deltas */
  547. if (tp->t_blk_res > 0)
  548. blkdelta = tp->t_blk_res;
  549. if ((tp->t_fdblocks_delta != 0) &&
  550. (xfs_has_lazysbcount(mp) ||
  551. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  552. blkdelta += tp->t_fdblocks_delta;
  553. if (tp->t_rtx_res > 0)
  554. rtxdelta = tp->t_rtx_res;
  555. if ((tp->t_frextents_delta != 0) &&
  556. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  557. rtxdelta += tp->t_frextents_delta;
  558. if (xfs_has_lazysbcount(mp) ||
  559. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  560. idelta = tp->t_icount_delta;
  561. ifreedelta = tp->t_ifree_delta;
  562. }
  563. /* apply the per-cpu counters */
  564. if (blkdelta) {
  565. error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
  566. ASSERT(!error);
  567. }
  568. if (idelta)
  569. percpu_counter_add_batch(&mp->m_icount, idelta,
  570. XFS_ICOUNT_BATCH);
  571. if (ifreedelta)
  572. percpu_counter_add(&mp->m_ifree, ifreedelta);
  573. if (rtxdelta) {
  574. error = xfs_mod_frextents(mp, rtxdelta);
  575. ASSERT(!error);
  576. }
  577. if (!(tp->t_flags & XFS_TRANS_SB_DIRTY))
  578. return;
  579. /* apply remaining deltas */
  580. spin_lock(&mp->m_sb_lock);
  581. mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta;
  582. mp->m_sb.sb_icount += idelta;
  583. mp->m_sb.sb_ifree += ifreedelta;
  584. /*
  585. * Do not touch sb_frextents here because we are dealing with incore
  586. * reservation. sb_frextents is not part of the lazy sb counters so it
  587. * must be consistent with the ondisk rtbitmap and must never include
  588. * incore reservations.
  589. */
  590. mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
  591. mp->m_sb.sb_agcount += tp->t_agcount_delta;
  592. mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta;
  593. mp->m_sb.sb_rextsize += tp->t_rextsize_delta;
  594. mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta;
  595. mp->m_sb.sb_rblocks += tp->t_rblocks_delta;
  596. mp->m_sb.sb_rextents += tp->t_rextents_delta;
  597. mp->m_sb.sb_rextslog += tp->t_rextslog_delta;
  598. spin_unlock(&mp->m_sb_lock);
  599. /*
  600. * Debug checks outside of the spinlock so they don't lock up the
  601. * machine if they fail.
  602. */
  603. ASSERT(mp->m_sb.sb_imax_pct >= 0);
  604. ASSERT(mp->m_sb.sb_rextslog >= 0);
  605. return;
  606. }
  607. /* Add the given log item to the transaction's list of log items. */
  608. void
  609. xfs_trans_add_item(
  610. struct xfs_trans *tp,
  611. struct xfs_log_item *lip)
  612. {
  613. ASSERT(lip->li_log == tp->t_mountp->m_log);
  614. ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
  615. ASSERT(list_empty(&lip->li_trans));
  616. ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
  617. list_add_tail(&lip->li_trans, &tp->t_items);
  618. trace_xfs_trans_add_item(tp, _RET_IP_);
  619. }
  620. /*
  621. * Unlink the log item from the transaction. the log item is no longer
  622. * considered dirty in this transaction, as the linked transaction has
  623. * finished, either by abort or commit completion.
  624. */
  625. void
  626. xfs_trans_del_item(
  627. struct xfs_log_item *lip)
  628. {
  629. clear_bit(XFS_LI_DIRTY, &lip->li_flags);
  630. list_del_init(&lip->li_trans);
  631. }
  632. /* Detach and unlock all of the items in a transaction */
  633. static void
  634. xfs_trans_free_items(
  635. struct xfs_trans *tp,
  636. bool abort)
  637. {
  638. struct xfs_log_item *lip, *next;
  639. trace_xfs_trans_free_items(tp, _RET_IP_);
  640. list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
  641. xfs_trans_del_item(lip);
  642. if (abort)
  643. set_bit(XFS_LI_ABORTED, &lip->li_flags);
  644. if (lip->li_ops->iop_release)
  645. lip->li_ops->iop_release(lip);
  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->ail_lock);
  658. /* xfs_trans_ail_update_bulk drops ailp->ail_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. if (lip->li_ops->iop_unpin)
  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_committed and iop_committing, 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 list_head *lv_chain,
  690. xfs_lsn_t commit_lsn,
  691. bool 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->ail_lock);
  699. xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
  700. spin_unlock(&ailp->ail_lock);
  701. /* unpin all the log items */
  702. list_for_each_entry(lv, lv_chain, lv_list) {
  703. struct xfs_log_item *lip = lv->lv_item;
  704. xfs_lsn_t item_lsn;
  705. if (aborted)
  706. set_bit(XFS_LI_ABORTED, &lip->li_flags);
  707. if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) {
  708. lip->li_ops->iop_release(lip);
  709. continue;
  710. }
  711. if (lip->li_ops->iop_committed)
  712. item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
  713. else
  714. item_lsn = commit_lsn;
  715. /* item_lsn of -1 means the item needs no further processing */
  716. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  717. continue;
  718. /*
  719. * if we are aborting the operation, no point in inserting the
  720. * object into the AIL as we are in a shutdown situation.
  721. */
  722. if (aborted) {
  723. ASSERT(xlog_is_shutdown(ailp->ail_log));
  724. if (lip->li_ops->iop_unpin)
  725. lip->li_ops->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->ail_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->ail_lock);
  741. if (lip->li_ops->iop_unpin)
  742. lip->li_ops->iop_unpin(lip, 0);
  743. continue;
  744. }
  745. /* Item is a candidate for bulk AIL insert. */
  746. log_items[i++] = lv->lv_item;
  747. if (i >= LOG_ITEM_BATCH_SIZE) {
  748. xfs_log_item_batch_insert(ailp, &cur, log_items,
  749. LOG_ITEM_BATCH_SIZE, commit_lsn);
  750. i = 0;
  751. }
  752. }
  753. /* make sure we insert the remainder! */
  754. if (i)
  755. xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
  756. spin_lock(&ailp->ail_lock);
  757. xfs_trans_ail_cursor_done(&cur);
  758. spin_unlock(&ailp->ail_lock);
  759. }
  760. /*
  761. * Sort transaction items prior to running precommit operations. This will
  762. * attempt to order the items such that they will always be locked in the same
  763. * order. Items that have no sort function are moved to the end of the list
  764. * and so are locked last.
  765. *
  766. * This may need refinement as different types of objects add sort functions.
  767. *
  768. * Function is more complex than it needs to be because we are comparing 64 bit
  769. * values and the function only returns 32 bit values.
  770. */
  771. static int
  772. xfs_trans_precommit_sort(
  773. void *unused_arg,
  774. const struct list_head *a,
  775. const struct list_head *b)
  776. {
  777. struct xfs_log_item *lia = container_of(a,
  778. struct xfs_log_item, li_trans);
  779. struct xfs_log_item *lib = container_of(b,
  780. struct xfs_log_item, li_trans);
  781. int64_t diff;
  782. /*
  783. * If both items are non-sortable, leave them alone. If only one is
  784. * sortable, move the non-sortable item towards the end of the list.
  785. */
  786. if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort)
  787. return 0;
  788. if (!lia->li_ops->iop_sort)
  789. return 1;
  790. if (!lib->li_ops->iop_sort)
  791. return -1;
  792. diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib);
  793. if (diff < 0)
  794. return -1;
  795. if (diff > 0)
  796. return 1;
  797. return 0;
  798. }
  799. /*
  800. * Run transaction precommit functions.
  801. *
  802. * If there is an error in any of the callouts, then stop immediately and
  803. * trigger a shutdown to abort the transaction. There is no recovery possible
  804. * from errors at this point as the transaction is dirty....
  805. */
  806. static int
  807. xfs_trans_run_precommits(
  808. struct xfs_trans *tp)
  809. {
  810. struct xfs_mount *mp = tp->t_mountp;
  811. struct xfs_log_item *lip, *n;
  812. int error = 0;
  813. /*
  814. * Sort the item list to avoid ABBA deadlocks with other transactions
  815. * running precommit operations that lock multiple shared items such as
  816. * inode cluster buffers.
  817. */
  818. list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort);
  819. /*
  820. * Precommit operations can remove the log item from the transaction
  821. * if the log item exists purely to delay modifications until they
  822. * can be ordered against other operations. Hence we have to use
  823. * list_for_each_entry_safe() here.
  824. */
  825. list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
  826. if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
  827. continue;
  828. if (lip->li_ops->iop_precommit) {
  829. error = lip->li_ops->iop_precommit(tp, lip);
  830. if (error)
  831. break;
  832. }
  833. }
  834. if (error)
  835. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  836. return error;
  837. }
  838. /*
  839. * Commit the given transaction to the log.
  840. *
  841. * XFS disk error handling mechanism is not based on a typical
  842. * transaction abort mechanism. Logically after the filesystem
  843. * gets marked 'SHUTDOWN', we can't let any new transactions
  844. * be durable - ie. committed to disk - because some metadata might
  845. * be inconsistent. In such cases, this returns an error, and the
  846. * caller may assume that all locked objects joined to the transaction
  847. * have already been unlocked as if the commit had succeeded.
  848. * Do not reference the transaction structure after this call.
  849. */
  850. static int
  851. __xfs_trans_commit(
  852. struct xfs_trans *tp,
  853. bool regrant)
  854. {
  855. struct xfs_mount *mp = tp->t_mountp;
  856. struct xlog *log = mp->m_log;
  857. xfs_csn_t commit_seq = 0;
  858. int error = 0;
  859. int sync = tp->t_flags & XFS_TRANS_SYNC;
  860. trace_xfs_trans_commit(tp, _RET_IP_);
  861. error = xfs_trans_run_precommits(tp);
  862. if (error) {
  863. if (tp->t_flags & XFS_TRANS_PERM_LOG_RES)
  864. xfs_defer_cancel(tp);
  865. goto out_unreserve;
  866. }
  867. /*
  868. * Finish deferred items on final commit. Only permanent transactions
  869. * should ever have deferred ops.
  870. */
  871. WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
  872. !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  873. if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
  874. error = xfs_defer_finish_noroll(&tp);
  875. if (error)
  876. goto out_unreserve;
  877. }
  878. /*
  879. * If there is nothing to be logged by the transaction,
  880. * then unlock all of the items associated with the
  881. * transaction and free the transaction structure.
  882. * Also make sure to return any reserved blocks to
  883. * the free pool.
  884. */
  885. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  886. goto out_unreserve;
  887. /*
  888. * We must check against log shutdown here because we cannot abort log
  889. * items and leave them dirty, inconsistent and unpinned in memory while
  890. * the log is active. This leaves them open to being written back to
  891. * disk, and that will lead to on-disk corruption.
  892. */
  893. if (xlog_is_shutdown(log)) {
  894. error = -EIO;
  895. goto out_unreserve;
  896. }
  897. ASSERT(tp->t_ticket != NULL);
  898. /*
  899. * If we need to update the superblock, then do it now.
  900. */
  901. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  902. xfs_trans_apply_sb_deltas(tp);
  903. xfs_trans_apply_dquot_deltas(tp);
  904. xlog_cil_commit(log, tp, &commit_seq, regrant);
  905. xfs_trans_free(tp);
  906. /*
  907. * If the transaction needs to be synchronous, then force the
  908. * log out now and wait for it.
  909. */
  910. if (sync) {
  911. error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL);
  912. XFS_STATS_INC(mp, xs_trans_sync);
  913. } else {
  914. XFS_STATS_INC(mp, xs_trans_async);
  915. }
  916. return error;
  917. out_unreserve:
  918. xfs_trans_unreserve_and_mod_sb(tp);
  919. /*
  920. * It is indeed possible for the transaction to be not dirty but
  921. * the dqinfo portion to be. All that means is that we have some
  922. * (non-persistent) quota reservations that need to be unreserved.
  923. */
  924. xfs_trans_unreserve_and_mod_dquots(tp);
  925. if (tp->t_ticket) {
  926. if (regrant && !xlog_is_shutdown(log))
  927. xfs_log_ticket_regrant(log, tp->t_ticket);
  928. else
  929. xfs_log_ticket_ungrant(log, tp->t_ticket);
  930. tp->t_ticket = NULL;
  931. }
  932. xfs_trans_free_items(tp, !!error);
  933. xfs_trans_free(tp);
  934. XFS_STATS_INC(mp, xs_trans_empty);
  935. return error;
  936. }
  937. int
  938. xfs_trans_commit(
  939. struct xfs_trans *tp)
  940. {
  941. return __xfs_trans_commit(tp, false);
  942. }
  943. /*
  944. * Unlock all of the transaction's items and free the transaction. If the
  945. * transaction is dirty, we must shut down the filesystem because there is no
  946. * way to restore them to their previous state.
  947. *
  948. * If the transaction has made a log reservation, make sure to release it as
  949. * well.
  950. *
  951. * This is a high level function (equivalent to xfs_trans_commit()) and so can
  952. * be called after the transaction has effectively been aborted due to the mount
  953. * being shut down. However, if the mount has not been shut down and the
  954. * transaction is dirty we will shut the mount down and, in doing so, that
  955. * guarantees that the log is shut down, too. Hence we don't need to be as
  956. * careful with shutdown state and dirty items here as we need to be in
  957. * xfs_trans_commit().
  958. */
  959. void
  960. xfs_trans_cancel(
  961. struct xfs_trans *tp)
  962. {
  963. struct xfs_mount *mp = tp->t_mountp;
  964. struct xlog *log = mp->m_log;
  965. bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
  966. trace_xfs_trans_cancel(tp, _RET_IP_);
  967. /*
  968. * It's never valid to cancel a transaction with deferred ops attached,
  969. * because the transaction is effectively dirty. Complain about this
  970. * loudly before freeing the in-memory defer items.
  971. */
  972. if (!list_empty(&tp->t_dfops)) {
  973. ASSERT(xfs_is_shutdown(mp) || list_empty(&tp->t_dfops));
  974. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  975. dirty = true;
  976. xfs_defer_cancel(tp);
  977. }
  978. /*
  979. * See if the caller is relying on us to shut down the filesystem. We
  980. * only want an error report if there isn't already a shutdown in
  981. * progress, so we only need to check against the mount shutdown state
  982. * here.
  983. */
  984. if (dirty && !xfs_is_shutdown(mp)) {
  985. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  986. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  987. }
  988. #ifdef DEBUG
  989. /* Log items need to be consistent until the log is shut down. */
  990. if (!dirty && !xlog_is_shutdown(log)) {
  991. struct xfs_log_item *lip;
  992. list_for_each_entry(lip, &tp->t_items, li_trans)
  993. ASSERT(!xlog_item_is_intent_done(lip));
  994. }
  995. #endif
  996. xfs_trans_unreserve_and_mod_sb(tp);
  997. xfs_trans_unreserve_and_mod_dquots(tp);
  998. if (tp->t_ticket) {
  999. xfs_log_ticket_ungrant(log, tp->t_ticket);
  1000. tp->t_ticket = NULL;
  1001. }
  1002. xfs_trans_free_items(tp, dirty);
  1003. xfs_trans_free(tp);
  1004. }
  1005. /*
  1006. * Roll from one trans in the sequence of PERMANENT transactions to
  1007. * the next: permanent transactions are only flushed out when
  1008. * committed with xfs_trans_commit(), but we still want as soon
  1009. * as possible to let chunks of it go to the log. So we commit the
  1010. * chunk we've been working on and get a new transaction to continue.
  1011. */
  1012. int
  1013. xfs_trans_roll(
  1014. struct xfs_trans **tpp)
  1015. {
  1016. struct xfs_trans *trans = *tpp;
  1017. struct xfs_trans_res tres;
  1018. int error;
  1019. trace_xfs_trans_roll(trans, _RET_IP_);
  1020. /*
  1021. * Copy the critical parameters from one trans to the next.
  1022. */
  1023. tres.tr_logres = trans->t_log_res;
  1024. tres.tr_logcount = trans->t_log_count;
  1025. *tpp = xfs_trans_dup(trans);
  1026. /*
  1027. * Commit the current transaction.
  1028. * If this commit failed, then it'd just unlock those items that
  1029. * are not marked ihold. That also means that a filesystem shutdown
  1030. * is in progress. The caller takes the responsibility to cancel
  1031. * the duplicate transaction that gets returned.
  1032. */
  1033. error = __xfs_trans_commit(trans, true);
  1034. if (error)
  1035. return error;
  1036. /*
  1037. * Reserve space in the log for the next transaction.
  1038. * This also pushes items in the "AIL", the list of logged items,
  1039. * out to disk if they are taking up space at the tail of the log
  1040. * that we want to use. This requires that either nothing be locked
  1041. * across this call, or that anything that is locked be logged in
  1042. * the prior and the next transactions.
  1043. */
  1044. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  1045. return xfs_trans_reserve(*tpp, &tres, 0, 0);
  1046. }
  1047. /*
  1048. * Allocate an transaction, lock and join the inode to it, and reserve quota.
  1049. *
  1050. * The caller must ensure that the on-disk dquots attached to this inode have
  1051. * already been allocated and initialized. The caller is responsible for
  1052. * releasing ILOCK_EXCL if a new transaction is returned.
  1053. */
  1054. int
  1055. xfs_trans_alloc_inode(
  1056. struct xfs_inode *ip,
  1057. struct xfs_trans_res *resv,
  1058. unsigned int dblocks,
  1059. unsigned int rblocks,
  1060. bool force,
  1061. struct xfs_trans **tpp)
  1062. {
  1063. struct xfs_trans *tp;
  1064. struct xfs_mount *mp = ip->i_mount;
  1065. bool retried = false;
  1066. int error;
  1067. retry:
  1068. error = xfs_trans_alloc(mp, resv, dblocks,
  1069. rblocks / mp->m_sb.sb_rextsize,
  1070. force ? XFS_TRANS_RESERVE : 0, &tp);
  1071. if (error)
  1072. return error;
  1073. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1074. xfs_trans_ijoin(tp, ip, 0);
  1075. error = xfs_qm_dqattach_locked(ip, false);
  1076. if (error) {
  1077. /* Caller should have allocated the dquots! */
  1078. ASSERT(error != -ENOENT);
  1079. goto out_cancel;
  1080. }
  1081. error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force);
  1082. if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
  1083. xfs_trans_cancel(tp);
  1084. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1085. xfs_blockgc_free_quota(ip, 0);
  1086. retried = true;
  1087. goto retry;
  1088. }
  1089. if (error)
  1090. goto out_cancel;
  1091. *tpp = tp;
  1092. return 0;
  1093. out_cancel:
  1094. xfs_trans_cancel(tp);
  1095. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1096. return error;
  1097. }
  1098. /*
  1099. * Allocate an transaction in preparation for inode creation by reserving quota
  1100. * against the given dquots. Callers are not required to hold any inode locks.
  1101. */
  1102. int
  1103. xfs_trans_alloc_icreate(
  1104. struct xfs_mount *mp,
  1105. struct xfs_trans_res *resv,
  1106. struct xfs_dquot *udqp,
  1107. struct xfs_dquot *gdqp,
  1108. struct xfs_dquot *pdqp,
  1109. unsigned int dblocks,
  1110. struct xfs_trans **tpp)
  1111. {
  1112. struct xfs_trans *tp;
  1113. bool retried = false;
  1114. int error;
  1115. retry:
  1116. error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
  1117. if (error)
  1118. return error;
  1119. error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks);
  1120. if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
  1121. xfs_trans_cancel(tp);
  1122. xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
  1123. retried = true;
  1124. goto retry;
  1125. }
  1126. if (error) {
  1127. xfs_trans_cancel(tp);
  1128. return error;
  1129. }
  1130. *tpp = tp;
  1131. return 0;
  1132. }
  1133. /*
  1134. * Allocate an transaction, lock and join the inode to it, and reserve quota
  1135. * in preparation for inode attribute changes that include uid, gid, or prid
  1136. * changes.
  1137. *
  1138. * The caller must ensure that the on-disk dquots attached to this inode have
  1139. * already been allocated and initialized. The ILOCK will be dropped when the
  1140. * transaction is committed or cancelled.
  1141. */
  1142. int
  1143. xfs_trans_alloc_ichange(
  1144. struct xfs_inode *ip,
  1145. struct xfs_dquot *new_udqp,
  1146. struct xfs_dquot *new_gdqp,
  1147. struct xfs_dquot *new_pdqp,
  1148. bool force,
  1149. struct xfs_trans **tpp)
  1150. {
  1151. struct xfs_trans *tp;
  1152. struct xfs_mount *mp = ip->i_mount;
  1153. struct xfs_dquot *udqp;
  1154. struct xfs_dquot *gdqp;
  1155. struct xfs_dquot *pdqp;
  1156. bool retried = false;
  1157. int error;
  1158. retry:
  1159. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  1160. if (error)
  1161. return error;
  1162. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1163. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1164. error = xfs_qm_dqattach_locked(ip, false);
  1165. if (error) {
  1166. /* Caller should have allocated the dquots! */
  1167. ASSERT(error != -ENOENT);
  1168. goto out_cancel;
  1169. }
  1170. /*
  1171. * For each quota type, skip quota reservations if the inode's dquots
  1172. * now match the ones that came from the caller, or the caller didn't
  1173. * pass one in. The inode's dquots can change if we drop the ILOCK to
  1174. * perform a blockgc scan, so we must preserve the caller's arguments.
  1175. */
  1176. udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL;
  1177. gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL;
  1178. pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL;
  1179. if (udqp || gdqp || pdqp) {
  1180. unsigned int qflags = XFS_QMOPT_RES_REGBLKS;
  1181. if (force)
  1182. qflags |= XFS_QMOPT_FORCE_RES;
  1183. /*
  1184. * Reserve enough quota to handle blocks on disk and reserved
  1185. * for a delayed allocation. We'll actually transfer the
  1186. * delalloc reservation between dquots at chown time, even
  1187. * though that part is only semi-transactional.
  1188. */
  1189. error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp,
  1190. pdqp, ip->i_nblocks + ip->i_delayed_blks,
  1191. 1, qflags);
  1192. if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
  1193. xfs_trans_cancel(tp);
  1194. xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
  1195. retried = true;
  1196. goto retry;
  1197. }
  1198. if (error)
  1199. goto out_cancel;
  1200. }
  1201. *tpp = tp;
  1202. return 0;
  1203. out_cancel:
  1204. xfs_trans_cancel(tp);
  1205. return error;
  1206. }
  1207. /*
  1208. * Allocate an transaction, lock and join the directory and child inodes to it,
  1209. * and reserve quota for a directory update. If there isn't sufficient space,
  1210. * @dblocks will be set to zero for a reservationless directory update and
  1211. * @nospace_error will be set to a negative errno describing the space
  1212. * constraint we hit.
  1213. *
  1214. * The caller must ensure that the on-disk dquots attached to this inode have
  1215. * already been allocated and initialized. The ILOCKs will be dropped when the
  1216. * transaction is committed or cancelled.
  1217. */
  1218. int
  1219. xfs_trans_alloc_dir(
  1220. struct xfs_inode *dp,
  1221. struct xfs_trans_res *resv,
  1222. struct xfs_inode *ip,
  1223. unsigned int *dblocks,
  1224. struct xfs_trans **tpp,
  1225. int *nospace_error)
  1226. {
  1227. struct xfs_trans *tp;
  1228. struct xfs_mount *mp = ip->i_mount;
  1229. unsigned int resblks;
  1230. bool retried = false;
  1231. int error;
  1232. retry:
  1233. *nospace_error = 0;
  1234. resblks = *dblocks;
  1235. error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
  1236. if (error == -ENOSPC) {
  1237. *nospace_error = error;
  1238. resblks = 0;
  1239. error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
  1240. }
  1241. if (error)
  1242. return error;
  1243. xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
  1244. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1245. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1246. error = xfs_qm_dqattach_locked(dp, false);
  1247. if (error) {
  1248. /* Caller should have allocated the dquots! */
  1249. ASSERT(error != -ENOENT);
  1250. goto out_cancel;
  1251. }
  1252. error = xfs_qm_dqattach_locked(ip, false);
  1253. if (error) {
  1254. /* Caller should have allocated the dquots! */
  1255. ASSERT(error != -ENOENT);
  1256. goto out_cancel;
  1257. }
  1258. if (resblks == 0)
  1259. goto done;
  1260. error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false);
  1261. if (error == -EDQUOT || error == -ENOSPC) {
  1262. if (!retried) {
  1263. xfs_trans_cancel(tp);
  1264. xfs_blockgc_free_quota(dp, 0);
  1265. retried = true;
  1266. goto retry;
  1267. }
  1268. *nospace_error = error;
  1269. resblks = 0;
  1270. error = 0;
  1271. }
  1272. if (error)
  1273. goto out_cancel;
  1274. done:
  1275. *tpp = tp;
  1276. *dblocks = resblks;
  1277. return 0;
  1278. out_cancel:
  1279. xfs_trans_cancel(tp);
  1280. return error;
  1281. }