xfs_trans.c 37 KB

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