xfs_trans_resv.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_bmap_btree.h"
  18. #include "xfs_ialloc.h"
  19. #include "xfs_quota.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_qm.h"
  22. #include "xfs_trans_space.h"
  23. #include "xfs_trace.h"
  24. #define _ALLOC true
  25. #define _FREE false
  26. /*
  27. * A buffer has a format structure overhead in the log in addition
  28. * to the data, so we need to take this into account when reserving
  29. * space in a transaction for a buffer. Round the space required up
  30. * to a multiple of 128 bytes so that we don't change the historical
  31. * reservation that has been used for this overhead.
  32. */
  33. STATIC uint
  34. xfs_buf_log_overhead(void)
  35. {
  36. return round_up(sizeof(struct xlog_op_header) +
  37. sizeof(struct xfs_buf_log_format), 128);
  38. }
  39. /*
  40. * Calculate out transaction log reservation per item in bytes.
  41. *
  42. * The nbufs argument is used to indicate the number of items that
  43. * will be changed in a transaction. size is used to tell how many
  44. * bytes should be reserved per item.
  45. */
  46. STATIC uint
  47. xfs_calc_buf_res(
  48. uint nbufs,
  49. uint size)
  50. {
  51. return nbufs * (size + xfs_buf_log_overhead());
  52. }
  53. /*
  54. * Per-extent log reservation for the btree changes involved in freeing or
  55. * allocating an extent. In classic XFS there were two trees that will be
  56. * modified (bnobt + cntbt). With rmap enabled, there are three trees
  57. * (rmapbt). With reflink, there are four trees (refcountbt). The number of
  58. * blocks reserved is based on the formula:
  59. *
  60. * num trees * ((2 blocks/level * max depth) - 1)
  61. *
  62. * Keep in mind that max depth is calculated separately for each type of tree.
  63. */
  64. uint
  65. xfs_allocfree_log_count(
  66. struct xfs_mount *mp,
  67. uint num_ops)
  68. {
  69. uint blocks;
  70. blocks = num_ops * 2 * (2 * mp->m_ag_maxlevels - 1);
  71. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  72. blocks += num_ops * (2 * mp->m_rmap_maxlevels - 1);
  73. if (xfs_sb_version_hasreflink(&mp->m_sb))
  74. blocks += num_ops * (2 * mp->m_refc_maxlevels - 1);
  75. return blocks;
  76. }
  77. /*
  78. * Logging inodes is really tricksy. They are logged in memory format,
  79. * which means that what we write into the log doesn't directly translate into
  80. * the amount of space they use on disk.
  81. *
  82. * Case in point - btree format forks in memory format use more space than the
  83. * on-disk format. In memory, the buffer contains a normal btree block header so
  84. * the btree code can treat it as though it is just another generic buffer.
  85. * However, when we write it to the inode fork, we don't write all of this
  86. * header as it isn't needed. e.g. the root is only ever in the inode, so
  87. * there's no need for sibling pointers which would waste 16 bytes of space.
  88. *
  89. * Hence when we have an inode with a maximally sized btree format fork, then
  90. * amount of information we actually log is greater than the size of the inode
  91. * on disk. Hence we need an inode reservation function that calculates all this
  92. * correctly. So, we log:
  93. *
  94. * - 4 log op headers for object
  95. * - for the ilf, the inode core and 2 forks
  96. * - inode log format object
  97. * - the inode core
  98. * - two inode forks containing bmap btree root blocks.
  99. * - the btree data contained by both forks will fit into the inode size,
  100. * hence when combined with the inode core above, we have a total of the
  101. * actual inode size.
  102. * - the BMBT headers need to be accounted separately, as they are
  103. * additional to the records and pointers that fit inside the inode
  104. * forks.
  105. */
  106. STATIC uint
  107. xfs_calc_inode_res(
  108. struct xfs_mount *mp,
  109. uint ninodes)
  110. {
  111. return ninodes *
  112. (4 * sizeof(struct xlog_op_header) +
  113. sizeof(struct xfs_inode_log_format) +
  114. mp->m_sb.sb_inodesize +
  115. 2 * XFS_BMBT_BLOCK_LEN(mp));
  116. }
  117. /*
  118. * Inode btree record insertion/removal modifies the inode btree and free space
  119. * btrees (since the inobt does not use the agfl). This requires the following
  120. * reservation:
  121. *
  122. * the inode btree: max depth * blocksize
  123. * the allocation btrees: 2 trees * (max depth - 1) * block size
  124. *
  125. * The caller must account for SB and AG header modifications, etc.
  126. */
  127. STATIC uint
  128. xfs_calc_inobt_res(
  129. struct xfs_mount *mp)
  130. {
  131. return xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
  132. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  133. XFS_FSB_TO_B(mp, 1));
  134. }
  135. /*
  136. * The free inode btree is a conditional feature. The behavior differs slightly
  137. * from that of the traditional inode btree in that the finobt tracks records
  138. * for inode chunks with at least one free inode. A record can be removed from
  139. * the tree during individual inode allocation. Therefore the finobt
  140. * reservation is unconditional for both the inode chunk allocation and
  141. * individual inode allocation (modify) cases.
  142. *
  143. * Behavior aside, the reservation for finobt modification is equivalent to the
  144. * traditional inobt: cover a full finobt shape change plus block allocation.
  145. */
  146. STATIC uint
  147. xfs_calc_finobt_res(
  148. struct xfs_mount *mp)
  149. {
  150. if (!xfs_sb_version_hasfinobt(&mp->m_sb))
  151. return 0;
  152. return xfs_calc_inobt_res(mp);
  153. }
  154. /*
  155. * Calculate the reservation required to allocate or free an inode chunk. This
  156. * includes:
  157. *
  158. * the allocation btrees: 2 trees * (max depth - 1) * block size
  159. * the inode chunk: m_ialloc_blks * N
  160. *
  161. * The size N of the inode chunk reservation depends on whether it is for
  162. * allocation or free and which type of create transaction is in use. An inode
  163. * chunk free always invalidates the buffers and only requires reservation for
  164. * headers (N == 0). An inode chunk allocation requires a chunk sized
  165. * reservation on v4 and older superblocks to initialize the chunk. No chunk
  166. * reservation is required for allocation on v5 supers, which use ordered
  167. * buffers to initialize.
  168. */
  169. STATIC uint
  170. xfs_calc_inode_chunk_res(
  171. struct xfs_mount *mp,
  172. bool alloc)
  173. {
  174. uint res, size = 0;
  175. res = xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  176. XFS_FSB_TO_B(mp, 1));
  177. if (alloc) {
  178. /* icreate tx uses ordered buffers */
  179. if (xfs_sb_version_hascrc(&mp->m_sb))
  180. return res;
  181. size = XFS_FSB_TO_B(mp, 1);
  182. }
  183. res += xfs_calc_buf_res(mp->m_ialloc_blks, size);
  184. return res;
  185. }
  186. /*
  187. * Various log reservation values.
  188. *
  189. * These are based on the size of the file system block because that is what
  190. * most transactions manipulate. Each adds in an additional 128 bytes per
  191. * item logged to try to account for the overhead of the transaction mechanism.
  192. *
  193. * Note: Most of the reservations underestimate the number of allocation
  194. * groups into which they could free extents in the xfs_defer_finish() call.
  195. * This is because the number in the worst case is quite high and quite
  196. * unusual. In order to fix this we need to change xfs_defer_finish() to free
  197. * extents in only a single AG at a time. This will require changes to the
  198. * EFI code as well, however, so that the EFI for the extents not freed is
  199. * logged again in each transaction. See SGI PV #261917.
  200. *
  201. * Reservation functions here avoid a huge stack in xfs_trans_init due to
  202. * register overflow from temporaries in the calculations.
  203. */
  204. /*
  205. * In a write transaction we can allocate a maximum of 2
  206. * extents. This gives:
  207. * the inode getting the new extents: inode size
  208. * the inode's bmap btree: max depth * block size
  209. * the agfs of the ags from which the extents are allocated: 2 * sector
  210. * the superblock free block counter: sector size
  211. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  212. * And the bmap_finish transaction can free bmap blocks in a join:
  213. * the agfs of the ags containing the blocks: 2 * sector size
  214. * the agfls of the ags containing the blocks: 2 * sector size
  215. * the super block free block counter: sector size
  216. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  217. */
  218. STATIC uint
  219. xfs_calc_write_reservation(
  220. struct xfs_mount *mp)
  221. {
  222. return XFS_DQUOT_LOGRES(mp) +
  223. max((xfs_calc_inode_res(mp, 1) +
  224. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
  225. XFS_FSB_TO_B(mp, 1)) +
  226. xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  227. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
  228. XFS_FSB_TO_B(mp, 1))),
  229. (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  230. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
  231. XFS_FSB_TO_B(mp, 1))));
  232. }
  233. /*
  234. * In truncating a file we free up to two extents at once. We can modify:
  235. * the inode being truncated: inode size
  236. * the inode's bmap btree: (max depth + 1) * block size
  237. * And the bmap_finish transaction can free the blocks and bmap blocks:
  238. * the agf for each of the ags: 4 * sector size
  239. * the agfl for each of the ags: 4 * sector size
  240. * the super block to reflect the freed blocks: sector size
  241. * worst case split in allocation btrees per extent assuming 4 extents:
  242. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  243. */
  244. STATIC uint
  245. xfs_calc_itruncate_reservation(
  246. struct xfs_mount *mp)
  247. {
  248. return XFS_DQUOT_LOGRES(mp) +
  249. max((xfs_calc_inode_res(mp, 1) +
  250. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
  251. XFS_FSB_TO_B(mp, 1))),
  252. (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
  253. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4),
  254. XFS_FSB_TO_B(mp, 1))));
  255. }
  256. /*
  257. * In renaming a files we can modify:
  258. * the four inodes involved: 4 * inode size
  259. * the two directory btrees: 2 * (max depth + v2) * dir block size
  260. * the two directory bmap btrees: 2 * max depth * block size
  261. * And the bmap_finish transaction can free dir and bmap blocks (two sets
  262. * of bmap blocks) giving:
  263. * the agf for the ags in which the blocks live: 3 * sector size
  264. * the agfl for the ags in which the blocks live: 3 * sector size
  265. * the superblock for the free block count: sector size
  266. * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
  267. */
  268. STATIC uint
  269. xfs_calc_rename_reservation(
  270. struct xfs_mount *mp)
  271. {
  272. return XFS_DQUOT_LOGRES(mp) +
  273. max((xfs_calc_inode_res(mp, 4) +
  274. xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
  275. XFS_FSB_TO_B(mp, 1))),
  276. (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
  277. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 3),
  278. XFS_FSB_TO_B(mp, 1))));
  279. }
  280. /*
  281. * For removing an inode from unlinked list at first, we can modify:
  282. * the agi hash list and counters: sector size
  283. * the on disk inode before ours in the agi hash list: inode cluster size
  284. * the on disk inode in the agi hash list: inode cluster size
  285. */
  286. STATIC uint
  287. xfs_calc_iunlink_remove_reservation(
  288. struct xfs_mount *mp)
  289. {
  290. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  291. 2 * max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
  292. }
  293. /*
  294. * For creating a link to an inode:
  295. * the parent directory inode: inode size
  296. * the linked inode: inode size
  297. * the directory btree could split: (max depth + v2) * dir block size
  298. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  299. * And the bmap_finish transaction can free some bmap blocks giving:
  300. * the agf for the ag in which the blocks live: sector size
  301. * the agfl for the ag in which the blocks live: sector size
  302. * the superblock for the free block count: sector size
  303. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  304. */
  305. STATIC uint
  306. xfs_calc_link_reservation(
  307. struct xfs_mount *mp)
  308. {
  309. return XFS_DQUOT_LOGRES(mp) +
  310. xfs_calc_iunlink_remove_reservation(mp) +
  311. max((xfs_calc_inode_res(mp, 2) +
  312. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  313. XFS_FSB_TO_B(mp, 1))),
  314. (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  315. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  316. XFS_FSB_TO_B(mp, 1))));
  317. }
  318. /*
  319. * For adding an inode to unlinked list we can modify:
  320. * the agi hash list: sector size
  321. * the on disk inode: inode cluster size
  322. */
  323. STATIC uint
  324. xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
  325. {
  326. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  327. max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
  328. }
  329. /*
  330. * For removing a directory entry we can modify:
  331. * the parent directory inode: inode size
  332. * the removed inode: inode size
  333. * the directory btree could join: (max depth + v2) * dir block size
  334. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  335. * And the bmap_finish transaction can free the dir and bmap blocks giving:
  336. * the agf for the ag in which the blocks live: 2 * sector size
  337. * the agfl for the ag in which the blocks live: 2 * sector size
  338. * the superblock for the free block count: sector size
  339. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  340. */
  341. STATIC uint
  342. xfs_calc_remove_reservation(
  343. struct xfs_mount *mp)
  344. {
  345. return XFS_DQUOT_LOGRES(mp) +
  346. xfs_calc_iunlink_add_reservation(mp) +
  347. max((xfs_calc_inode_res(mp, 1) +
  348. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  349. XFS_FSB_TO_B(mp, 1))),
  350. (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
  351. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
  352. XFS_FSB_TO_B(mp, 1))));
  353. }
  354. /*
  355. * For create, break it in to the two cases that the transaction
  356. * covers. We start with the modify case - allocation done by modification
  357. * of the state of existing inodes - and the allocation case.
  358. */
  359. /*
  360. * For create we can modify:
  361. * the parent directory inode: inode size
  362. * the new inode: inode size
  363. * the inode btree entry: block size
  364. * the superblock for the nlink flag: sector size
  365. * the directory btree: (max depth + v2) * dir block size
  366. * the directory inode's bmap btree: (max depth + v2) * block size
  367. * the finobt (record modification and allocation btrees)
  368. */
  369. STATIC uint
  370. xfs_calc_create_resv_modify(
  371. struct xfs_mount *mp)
  372. {
  373. return xfs_calc_inode_res(mp, 2) +
  374. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  375. (uint)XFS_FSB_TO_B(mp, 1) +
  376. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1)) +
  377. xfs_calc_finobt_res(mp);
  378. }
  379. /*
  380. * For icreate we can allocate some inodes giving:
  381. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  382. * the superblock for the nlink flag: sector size
  383. * the inode chunk (allocation, optional init)
  384. * the inobt (record insertion)
  385. * the finobt (optional, record insertion)
  386. */
  387. STATIC uint
  388. xfs_calc_icreate_resv_alloc(
  389. struct xfs_mount *mp)
  390. {
  391. return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  392. mp->m_sb.sb_sectsize +
  393. xfs_calc_inode_chunk_res(mp, _ALLOC) +
  394. xfs_calc_inobt_res(mp) +
  395. xfs_calc_finobt_res(mp);
  396. }
  397. STATIC uint
  398. xfs_calc_icreate_reservation(xfs_mount_t *mp)
  399. {
  400. return XFS_DQUOT_LOGRES(mp) +
  401. max(xfs_calc_icreate_resv_alloc(mp),
  402. xfs_calc_create_resv_modify(mp));
  403. }
  404. STATIC uint
  405. xfs_calc_create_tmpfile_reservation(
  406. struct xfs_mount *mp)
  407. {
  408. uint res = XFS_DQUOT_LOGRES(mp);
  409. res += xfs_calc_icreate_resv_alloc(mp);
  410. return res + xfs_calc_iunlink_add_reservation(mp);
  411. }
  412. /*
  413. * Making a new directory is the same as creating a new file.
  414. */
  415. STATIC uint
  416. xfs_calc_mkdir_reservation(
  417. struct xfs_mount *mp)
  418. {
  419. return xfs_calc_icreate_reservation(mp);
  420. }
  421. /*
  422. * Making a new symplink is the same as creating a new file, but
  423. * with the added blocks for remote symlink data which can be up to 1kB in
  424. * length (XFS_SYMLINK_MAXLEN).
  425. */
  426. STATIC uint
  427. xfs_calc_symlink_reservation(
  428. struct xfs_mount *mp)
  429. {
  430. return xfs_calc_icreate_reservation(mp) +
  431. xfs_calc_buf_res(1, XFS_SYMLINK_MAXLEN);
  432. }
  433. /*
  434. * In freeing an inode we can modify:
  435. * the inode being freed: inode size
  436. * the super block free inode counter, AGF and AGFL: sector size
  437. * the on disk inode (agi unlinked list removal)
  438. * the inode chunk (invalidated, headers only)
  439. * the inode btree
  440. * the finobt (record insertion, removal or modification)
  441. *
  442. * Note that the inode chunk res. includes an allocfree res. for freeing of the
  443. * inode chunk. This is technically extraneous because the inode chunk free is
  444. * deferred (it occurs after a transaction roll). Include the extra reservation
  445. * anyways since we've had reports of ifree transaction overruns due to too many
  446. * agfl fixups during inode chunk frees.
  447. */
  448. STATIC uint
  449. xfs_calc_ifree_reservation(
  450. struct xfs_mount *mp)
  451. {
  452. return XFS_DQUOT_LOGRES(mp) +
  453. xfs_calc_inode_res(mp, 1) +
  454. xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  455. xfs_calc_iunlink_remove_reservation(mp) +
  456. xfs_calc_inode_chunk_res(mp, _FREE) +
  457. xfs_calc_inobt_res(mp) +
  458. xfs_calc_finobt_res(mp);
  459. }
  460. /*
  461. * When only changing the inode we log the inode and possibly the superblock
  462. * We also add a bit of slop for the transaction stuff.
  463. */
  464. STATIC uint
  465. xfs_calc_ichange_reservation(
  466. struct xfs_mount *mp)
  467. {
  468. return XFS_DQUOT_LOGRES(mp) +
  469. xfs_calc_inode_res(mp, 1) +
  470. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  471. }
  472. /*
  473. * Growing the data section of the filesystem.
  474. * superblock
  475. * agi and agf
  476. * allocation btrees
  477. */
  478. STATIC uint
  479. xfs_calc_growdata_reservation(
  480. struct xfs_mount *mp)
  481. {
  482. return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  483. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  484. XFS_FSB_TO_B(mp, 1));
  485. }
  486. /*
  487. * Growing the rt section of the filesystem.
  488. * In the first set of transactions (ALLOC) we allocate space to the
  489. * bitmap or summary files.
  490. * superblock: sector size
  491. * agf of the ag from which the extent is allocated: sector size
  492. * bmap btree for bitmap/summary inode: max depth * blocksize
  493. * bitmap/summary inode: inode size
  494. * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
  495. */
  496. STATIC uint
  497. xfs_calc_growrtalloc_reservation(
  498. struct xfs_mount *mp)
  499. {
  500. return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  501. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
  502. XFS_FSB_TO_B(mp, 1)) +
  503. xfs_calc_inode_res(mp, 1) +
  504. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  505. XFS_FSB_TO_B(mp, 1));
  506. }
  507. /*
  508. * Growing the rt section of the filesystem.
  509. * In the second set of transactions (ZERO) we zero the new metadata blocks.
  510. * one bitmap/summary block: blocksize
  511. */
  512. STATIC uint
  513. xfs_calc_growrtzero_reservation(
  514. struct xfs_mount *mp)
  515. {
  516. return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
  517. }
  518. /*
  519. * Growing the rt section of the filesystem.
  520. * In the third set of transactions (FREE) we update metadata without
  521. * allocating any new blocks.
  522. * superblock: sector size
  523. * bitmap inode: inode size
  524. * summary inode: inode size
  525. * one bitmap block: blocksize
  526. * summary blocks: new summary size
  527. */
  528. STATIC uint
  529. xfs_calc_growrtfree_reservation(
  530. struct xfs_mount *mp)
  531. {
  532. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  533. xfs_calc_inode_res(mp, 2) +
  534. xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
  535. xfs_calc_buf_res(1, mp->m_rsumsize);
  536. }
  537. /*
  538. * Logging the inode modification timestamp on a synchronous write.
  539. * inode
  540. */
  541. STATIC uint
  542. xfs_calc_swrite_reservation(
  543. struct xfs_mount *mp)
  544. {
  545. return xfs_calc_inode_res(mp, 1);
  546. }
  547. /*
  548. * Logging the inode mode bits when writing a setuid/setgid file
  549. * inode
  550. */
  551. STATIC uint
  552. xfs_calc_writeid_reservation(
  553. struct xfs_mount *mp)
  554. {
  555. return xfs_calc_inode_res(mp, 1);
  556. }
  557. /*
  558. * Converting the inode from non-attributed to attributed.
  559. * the inode being converted: inode size
  560. * agf block and superblock (for block allocation)
  561. * the new block (directory sized)
  562. * bmap blocks for the new directory block
  563. * allocation btrees
  564. */
  565. STATIC uint
  566. xfs_calc_addafork_reservation(
  567. struct xfs_mount *mp)
  568. {
  569. return XFS_DQUOT_LOGRES(mp) +
  570. xfs_calc_inode_res(mp, 1) +
  571. xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  572. xfs_calc_buf_res(1, mp->m_dir_geo->blksize) +
  573. xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
  574. XFS_FSB_TO_B(mp, 1)) +
  575. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  576. XFS_FSB_TO_B(mp, 1));
  577. }
  578. /*
  579. * Removing the attribute fork of a file
  580. * the inode being truncated: inode size
  581. * the inode's bmap btree: max depth * block size
  582. * And the bmap_finish transaction can free the blocks and bmap blocks:
  583. * the agf for each of the ags: 4 * sector size
  584. * the agfl for each of the ags: 4 * sector size
  585. * the super block to reflect the freed blocks: sector size
  586. * worst case split in allocation btrees per extent assuming 4 extents:
  587. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  588. */
  589. STATIC uint
  590. xfs_calc_attrinval_reservation(
  591. struct xfs_mount *mp)
  592. {
  593. return max((xfs_calc_inode_res(mp, 1) +
  594. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
  595. XFS_FSB_TO_B(mp, 1))),
  596. (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
  597. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4),
  598. XFS_FSB_TO_B(mp, 1))));
  599. }
  600. /*
  601. * Setting an attribute at mount time.
  602. * the inode getting the attribute
  603. * the superblock for allocations
  604. * the agfs extents are allocated from
  605. * the attribute btree * max depth
  606. * the inode allocation btree
  607. * Since attribute transaction space is dependent on the size of the attribute,
  608. * the calculation is done partially at mount time and partially at runtime(see
  609. * below).
  610. */
  611. STATIC uint
  612. xfs_calc_attrsetm_reservation(
  613. struct xfs_mount *mp)
  614. {
  615. return XFS_DQUOT_LOGRES(mp) +
  616. xfs_calc_inode_res(mp, 1) +
  617. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  618. xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
  619. }
  620. /*
  621. * Setting an attribute at runtime, transaction space unit per block.
  622. * the superblock for allocations: sector size
  623. * the inode bmap btree could join or split: max depth * block size
  624. * Since the runtime attribute transaction space is dependent on the total
  625. * blocks needed for the 1st bmap, here we calculate out the space unit for
  626. * one block so that the caller could figure out the total space according
  627. * to the attibute extent length in blocks by:
  628. * ext * M_RES(mp)->tr_attrsetrt.tr_logres
  629. */
  630. STATIC uint
  631. xfs_calc_attrsetrt_reservation(
  632. struct xfs_mount *mp)
  633. {
  634. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  635. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
  636. XFS_FSB_TO_B(mp, 1));
  637. }
  638. /*
  639. * Removing an attribute.
  640. * the inode: inode size
  641. * the attribute btree could join: max depth * block size
  642. * the inode bmap btree could join or split: max depth * block size
  643. * And the bmap_finish transaction can free the attr blocks freed giving:
  644. * the agf for the ag in which the blocks live: 2 * sector size
  645. * the agfl for the ag in which the blocks live: 2 * sector size
  646. * the superblock for the free block count: sector size
  647. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  648. */
  649. STATIC uint
  650. xfs_calc_attrrm_reservation(
  651. struct xfs_mount *mp)
  652. {
  653. return XFS_DQUOT_LOGRES(mp) +
  654. max((xfs_calc_inode_res(mp, 1) +
  655. xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
  656. XFS_FSB_TO_B(mp, 1)) +
  657. (uint)XFS_FSB_TO_B(mp,
  658. XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
  659. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
  660. (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  661. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
  662. XFS_FSB_TO_B(mp, 1))));
  663. }
  664. /*
  665. * Clearing a bad agino number in an agi hash bucket.
  666. */
  667. STATIC uint
  668. xfs_calc_clear_agi_bucket_reservation(
  669. struct xfs_mount *mp)
  670. {
  671. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  672. }
  673. /*
  674. * Adjusting quota limits.
  675. * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
  676. */
  677. STATIC uint
  678. xfs_calc_qm_setqlim_reservation(void)
  679. {
  680. return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
  681. }
  682. /*
  683. * Allocating quota on disk if needed.
  684. * the write transaction log space for quota file extent allocation
  685. * the unit of quota allocation: one system block size
  686. */
  687. STATIC uint
  688. xfs_calc_qm_dqalloc_reservation(
  689. struct xfs_mount *mp)
  690. {
  691. return xfs_calc_write_reservation(mp) +
  692. xfs_calc_buf_res(1,
  693. XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
  694. }
  695. /*
  696. * Turning off quotas.
  697. * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
  698. * the superblock for the quota flags: sector size
  699. */
  700. STATIC uint
  701. xfs_calc_qm_quotaoff_reservation(
  702. struct xfs_mount *mp)
  703. {
  704. return sizeof(struct xfs_qoff_logitem) * 2 +
  705. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  706. }
  707. /*
  708. * End of turning off quotas.
  709. * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
  710. */
  711. STATIC uint
  712. xfs_calc_qm_quotaoff_end_reservation(void)
  713. {
  714. return sizeof(struct xfs_qoff_logitem) * 2;
  715. }
  716. /*
  717. * Syncing the incore super block changes to disk.
  718. * the super block to reflect the changes: sector size
  719. */
  720. STATIC uint
  721. xfs_calc_sb_reservation(
  722. struct xfs_mount *mp)
  723. {
  724. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  725. }
  726. void
  727. xfs_trans_resv_calc(
  728. struct xfs_mount *mp,
  729. struct xfs_trans_resv *resp)
  730. {
  731. /*
  732. * The following transactions are logged in physical format and
  733. * require a permanent reservation on space.
  734. */
  735. resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
  736. if (xfs_sb_version_hasreflink(&mp->m_sb))
  737. resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
  738. else
  739. resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
  740. resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  741. resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
  742. if (xfs_sb_version_hasreflink(&mp->m_sb))
  743. resp->tr_itruncate.tr_logcount =
  744. XFS_ITRUNCATE_LOG_COUNT_REFLINK;
  745. else
  746. resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
  747. resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  748. resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
  749. resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
  750. resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  751. resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
  752. resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
  753. resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  754. resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
  755. resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
  756. resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  757. resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
  758. resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
  759. resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  760. resp->tr_create.tr_logres = xfs_calc_icreate_reservation(mp);
  761. resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
  762. resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  763. resp->tr_create_tmpfile.tr_logres =
  764. xfs_calc_create_tmpfile_reservation(mp);
  765. resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
  766. resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  767. resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
  768. resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
  769. resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  770. resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
  771. resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
  772. resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  773. resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
  774. resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
  775. resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  776. resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
  777. resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
  778. resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  779. resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
  780. resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
  781. resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  782. resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
  783. resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
  784. resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  785. resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
  786. resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
  787. resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  788. resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
  789. if (xfs_sb_version_hasreflink(&mp->m_sb))
  790. resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
  791. else
  792. resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
  793. resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  794. /*
  795. * The following transactions are logged in logical format with
  796. * a default log count.
  797. */
  798. resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation();
  799. resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  800. resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
  801. resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  802. resp->tr_qm_equotaoff.tr_logres =
  803. xfs_calc_qm_quotaoff_end_reservation();
  804. resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  805. resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
  806. resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  807. /* The following transaction are logged in logical format */
  808. resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
  809. resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
  810. resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
  811. resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
  812. resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
  813. resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
  814. resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
  815. resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
  816. }