xfs_trans_resv.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. * Per-extent log reservation for the btree changes involved in freeing or
  188. * allocating a realtime extent. We have to be able to log as many rtbitmap
  189. * blocks as needed to mark inuse MAXEXTLEN blocks' worth of realtime extents,
  190. * as well as the realtime summary block.
  191. */
  192. unsigned int
  193. xfs_rtalloc_log_count(
  194. struct xfs_mount *mp,
  195. unsigned int num_ops)
  196. {
  197. unsigned int blksz = XFS_FSB_TO_B(mp, 1);
  198. unsigned int rtbmp_bytes;
  199. rtbmp_bytes = (MAXEXTLEN / mp->m_sb.sb_rextsize) / NBBY;
  200. return (howmany(rtbmp_bytes, blksz) + 1) * num_ops;
  201. }
  202. /*
  203. * Various log reservation values.
  204. *
  205. * These are based on the size of the file system block because that is what
  206. * most transactions manipulate. Each adds in an additional 128 bytes per
  207. * item logged to try to account for the overhead of the transaction mechanism.
  208. *
  209. * Note: Most of the reservations underestimate the number of allocation
  210. * groups into which they could free extents in the xfs_defer_finish() call.
  211. * This is because the number in the worst case is quite high and quite
  212. * unusual. In order to fix this we need to change xfs_defer_finish() to free
  213. * extents in only a single AG at a time. This will require changes to the
  214. * EFI code as well, however, so that the EFI for the extents not freed is
  215. * logged again in each transaction. See SGI PV #261917.
  216. *
  217. * Reservation functions here avoid a huge stack in xfs_trans_init due to
  218. * register overflow from temporaries in the calculations.
  219. */
  220. /*
  221. * In a write transaction we can allocate a maximum of 2
  222. * extents. This gives (t1):
  223. * the inode getting the new extents: inode size
  224. * the inode's bmap btree: max depth * block size
  225. * the agfs of the ags from which the extents are allocated: 2 * sector
  226. * the superblock free block counter: sector size
  227. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  228. * Or, if we're writing to a realtime file (t2):
  229. * the inode getting the new extents: inode size
  230. * the inode's bmap btree: max depth * block size
  231. * the agfs of the ags from which the extents are allocated: 2 * sector
  232. * the superblock free block counter: sector size
  233. * the realtime bitmap: ((MAXEXTLEN / rtextsize) / NBBY) bytes
  234. * the realtime summary: 1 block
  235. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  236. * And the bmap_finish transaction can free bmap blocks in a join (t3):
  237. * the agfs of the ags containing the blocks: 2 * sector size
  238. * the agfls of the ags containing the blocks: 2 * sector size
  239. * the super block free block counter: sector size
  240. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  241. */
  242. STATIC uint
  243. xfs_calc_write_reservation(
  244. struct xfs_mount *mp)
  245. {
  246. unsigned int t1, t2, t3;
  247. unsigned int blksz = XFS_FSB_TO_B(mp, 1);
  248. t1 = xfs_calc_inode_res(mp, 1) +
  249. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), blksz) +
  250. xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  251. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2), blksz);
  252. if (xfs_sb_version_hasrealtime(&mp->m_sb)) {
  253. t2 = xfs_calc_inode_res(mp, 1) +
  254. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
  255. blksz) +
  256. xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  257. xfs_calc_buf_res(xfs_rtalloc_log_count(mp, 1), blksz) +
  258. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1), blksz);
  259. } else {
  260. t2 = 0;
  261. }
  262. t3 = xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  263. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2), blksz);
  264. return XFS_DQUOT_LOGRES(mp) + max3(t1, t2, t3);
  265. }
  266. /*
  267. * In truncating a file we free up to two extents at once. We can modify (t1):
  268. * the inode being truncated: inode size
  269. * the inode's bmap btree: (max depth + 1) * block size
  270. * And the bmap_finish transaction can free the blocks and bmap blocks (t2):
  271. * the agf for each of the ags: 4 * sector size
  272. * the agfl for each of the ags: 4 * sector size
  273. * the super block to reflect the freed blocks: sector size
  274. * worst case split in allocation btrees per extent assuming 4 extents:
  275. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  276. * Or, if it's a realtime file (t3):
  277. * the agf for each of the ags: 2 * sector size
  278. * the agfl for each of the ags: 2 * sector size
  279. * the super block to reflect the freed blocks: sector size
  280. * the realtime bitmap: 2 exts * ((MAXEXTLEN / rtextsize) / NBBY) bytes
  281. * the realtime summary: 2 exts * 1 block
  282. * worst case split in allocation btrees per extent assuming 2 extents:
  283. * 2 exts * 2 trees * (2 * max depth - 1) * block size
  284. */
  285. STATIC uint
  286. xfs_calc_itruncate_reservation(
  287. struct xfs_mount *mp)
  288. {
  289. unsigned int t1, t2, t3;
  290. unsigned int blksz = XFS_FSB_TO_B(mp, 1);
  291. t1 = xfs_calc_inode_res(mp, 1) +
  292. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1, blksz);
  293. t2 = xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
  294. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4), blksz);
  295. if (xfs_sb_version_hasrealtime(&mp->m_sb)) {
  296. t3 = xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  297. xfs_calc_buf_res(xfs_rtalloc_log_count(mp, 2), blksz) +
  298. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2), blksz);
  299. } else {
  300. t3 = 0;
  301. }
  302. return XFS_DQUOT_LOGRES(mp) + max3(t1, t2, t3);
  303. }
  304. /*
  305. * In renaming a files we can modify:
  306. * the four inodes involved: 4 * inode size
  307. * the two directory btrees: 2 * (max depth + v2) * dir block size
  308. * the two directory bmap btrees: 2 * max depth * block size
  309. * And the bmap_finish transaction can free dir and bmap blocks (two sets
  310. * of bmap blocks) giving:
  311. * the agf for the ags in which the blocks live: 3 * sector size
  312. * the agfl for the ags in which the blocks live: 3 * sector size
  313. * the superblock for the free block count: sector size
  314. * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
  315. */
  316. STATIC uint
  317. xfs_calc_rename_reservation(
  318. struct xfs_mount *mp)
  319. {
  320. return XFS_DQUOT_LOGRES(mp) +
  321. max((xfs_calc_inode_res(mp, 4) +
  322. xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
  323. XFS_FSB_TO_B(mp, 1))),
  324. (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
  325. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 3),
  326. XFS_FSB_TO_B(mp, 1))));
  327. }
  328. /*
  329. * For removing an inode from unlinked list at first, we can modify:
  330. * the agi hash list and counters: sector size
  331. * the on disk inode before ours in the agi hash list: inode cluster size
  332. * the on disk inode in the agi hash list: inode cluster size
  333. */
  334. STATIC uint
  335. xfs_calc_iunlink_remove_reservation(
  336. struct xfs_mount *mp)
  337. {
  338. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  339. 2 * max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
  340. }
  341. /*
  342. * For creating a link to an inode:
  343. * the parent directory inode: inode size
  344. * the linked inode: inode size
  345. * the directory btree could split: (max depth + v2) * dir block size
  346. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  347. * And the bmap_finish transaction can free some bmap blocks giving:
  348. * the agf for the ag in which the blocks live: sector size
  349. * the agfl for the ag in which the blocks live: sector size
  350. * the superblock for the free block count: sector size
  351. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  352. */
  353. STATIC uint
  354. xfs_calc_link_reservation(
  355. struct xfs_mount *mp)
  356. {
  357. return XFS_DQUOT_LOGRES(mp) +
  358. xfs_calc_iunlink_remove_reservation(mp) +
  359. max((xfs_calc_inode_res(mp, 2) +
  360. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  361. XFS_FSB_TO_B(mp, 1))),
  362. (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  363. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  364. XFS_FSB_TO_B(mp, 1))));
  365. }
  366. /*
  367. * For adding an inode to unlinked list we can modify:
  368. * the agi hash list: sector size
  369. * the on disk inode: inode cluster size
  370. */
  371. STATIC uint
  372. xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
  373. {
  374. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  375. max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
  376. }
  377. /*
  378. * For removing a directory entry we can modify:
  379. * the parent directory inode: inode size
  380. * the removed inode: inode size
  381. * the directory btree could join: (max depth + v2) * dir block size
  382. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  383. * And the bmap_finish transaction can free the dir and bmap blocks giving:
  384. * the agf for the ag in which the blocks live: 2 * sector size
  385. * the agfl for the ag in which the blocks live: 2 * sector size
  386. * the superblock for the free block count: sector size
  387. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  388. */
  389. STATIC uint
  390. xfs_calc_remove_reservation(
  391. struct xfs_mount *mp)
  392. {
  393. return XFS_DQUOT_LOGRES(mp) +
  394. xfs_calc_iunlink_add_reservation(mp) +
  395. max((xfs_calc_inode_res(mp, 1) +
  396. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  397. XFS_FSB_TO_B(mp, 1))),
  398. (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
  399. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
  400. XFS_FSB_TO_B(mp, 1))));
  401. }
  402. /*
  403. * For create, break it in to the two cases that the transaction
  404. * covers. We start with the modify case - allocation done by modification
  405. * of the state of existing inodes - and the allocation case.
  406. */
  407. /*
  408. * For create we can modify:
  409. * the parent directory inode: inode size
  410. * the new inode: inode size
  411. * the inode btree entry: block size
  412. * the superblock for the nlink flag: sector size
  413. * the directory btree: (max depth + v2) * dir block size
  414. * the directory inode's bmap btree: (max depth + v2) * block size
  415. * the finobt (record modification and allocation btrees)
  416. */
  417. STATIC uint
  418. xfs_calc_create_resv_modify(
  419. struct xfs_mount *mp)
  420. {
  421. return xfs_calc_inode_res(mp, 2) +
  422. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  423. (uint)XFS_FSB_TO_B(mp, 1) +
  424. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1)) +
  425. xfs_calc_finobt_res(mp);
  426. }
  427. /*
  428. * For icreate we can allocate some inodes giving:
  429. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  430. * the superblock for the nlink flag: sector size
  431. * the inode chunk (allocation, optional init)
  432. * the inobt (record insertion)
  433. * the finobt (optional, record insertion)
  434. */
  435. STATIC uint
  436. xfs_calc_icreate_resv_alloc(
  437. struct xfs_mount *mp)
  438. {
  439. return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  440. mp->m_sb.sb_sectsize +
  441. xfs_calc_inode_chunk_res(mp, _ALLOC) +
  442. xfs_calc_inobt_res(mp) +
  443. xfs_calc_finobt_res(mp);
  444. }
  445. STATIC uint
  446. xfs_calc_icreate_reservation(xfs_mount_t *mp)
  447. {
  448. return XFS_DQUOT_LOGRES(mp) +
  449. max(xfs_calc_icreate_resv_alloc(mp),
  450. xfs_calc_create_resv_modify(mp));
  451. }
  452. STATIC uint
  453. xfs_calc_create_tmpfile_reservation(
  454. struct xfs_mount *mp)
  455. {
  456. uint res = XFS_DQUOT_LOGRES(mp);
  457. res += xfs_calc_icreate_resv_alloc(mp);
  458. return res + xfs_calc_iunlink_add_reservation(mp);
  459. }
  460. /*
  461. * Making a new directory is the same as creating a new file.
  462. */
  463. STATIC uint
  464. xfs_calc_mkdir_reservation(
  465. struct xfs_mount *mp)
  466. {
  467. return xfs_calc_icreate_reservation(mp);
  468. }
  469. /*
  470. * Making a new symplink is the same as creating a new file, but
  471. * with the added blocks for remote symlink data which can be up to 1kB in
  472. * length (XFS_SYMLINK_MAXLEN).
  473. */
  474. STATIC uint
  475. xfs_calc_symlink_reservation(
  476. struct xfs_mount *mp)
  477. {
  478. return xfs_calc_icreate_reservation(mp) +
  479. xfs_calc_buf_res(1, XFS_SYMLINK_MAXLEN);
  480. }
  481. /*
  482. * In freeing an inode we can modify:
  483. * the inode being freed: inode size
  484. * the super block free inode counter, AGF and AGFL: sector size
  485. * the on disk inode (agi unlinked list removal)
  486. * the inode chunk (invalidated, headers only)
  487. * the inode btree
  488. * the finobt (record insertion, removal or modification)
  489. *
  490. * Note that the inode chunk res. includes an allocfree res. for freeing of the
  491. * inode chunk. This is technically extraneous because the inode chunk free is
  492. * deferred (it occurs after a transaction roll). Include the extra reservation
  493. * anyways since we've had reports of ifree transaction overruns due to too many
  494. * agfl fixups during inode chunk frees.
  495. */
  496. STATIC uint
  497. xfs_calc_ifree_reservation(
  498. struct xfs_mount *mp)
  499. {
  500. return XFS_DQUOT_LOGRES(mp) +
  501. xfs_calc_inode_res(mp, 1) +
  502. xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  503. xfs_calc_iunlink_remove_reservation(mp) +
  504. xfs_calc_inode_chunk_res(mp, _FREE) +
  505. xfs_calc_inobt_res(mp) +
  506. xfs_calc_finobt_res(mp);
  507. }
  508. /*
  509. * When only changing the inode we log the inode and possibly the superblock
  510. * We also add a bit of slop for the transaction stuff.
  511. */
  512. STATIC uint
  513. xfs_calc_ichange_reservation(
  514. struct xfs_mount *mp)
  515. {
  516. return XFS_DQUOT_LOGRES(mp) +
  517. xfs_calc_inode_res(mp, 1) +
  518. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  519. }
  520. /*
  521. * Growing the data section of the filesystem.
  522. * superblock
  523. * agi and agf
  524. * allocation btrees
  525. */
  526. STATIC uint
  527. xfs_calc_growdata_reservation(
  528. struct xfs_mount *mp)
  529. {
  530. return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  531. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  532. XFS_FSB_TO_B(mp, 1));
  533. }
  534. /*
  535. * Growing the rt section of the filesystem.
  536. * In the first set of transactions (ALLOC) we allocate space to the
  537. * bitmap or summary files.
  538. * superblock: sector size
  539. * agf of the ag from which the extent is allocated: sector size
  540. * bmap btree for bitmap/summary inode: max depth * blocksize
  541. * bitmap/summary inode: inode size
  542. * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
  543. */
  544. STATIC uint
  545. xfs_calc_growrtalloc_reservation(
  546. struct xfs_mount *mp)
  547. {
  548. return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  549. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
  550. XFS_FSB_TO_B(mp, 1)) +
  551. xfs_calc_inode_res(mp, 1) +
  552. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  553. XFS_FSB_TO_B(mp, 1));
  554. }
  555. /*
  556. * Growing the rt section of the filesystem.
  557. * In the second set of transactions (ZERO) we zero the new metadata blocks.
  558. * one bitmap/summary block: blocksize
  559. */
  560. STATIC uint
  561. xfs_calc_growrtzero_reservation(
  562. struct xfs_mount *mp)
  563. {
  564. return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
  565. }
  566. /*
  567. * Growing the rt section of the filesystem.
  568. * In the third set of transactions (FREE) we update metadata without
  569. * allocating any new blocks.
  570. * superblock: sector size
  571. * bitmap inode: inode size
  572. * summary inode: inode size
  573. * one bitmap block: blocksize
  574. * summary blocks: new summary size
  575. */
  576. STATIC uint
  577. xfs_calc_growrtfree_reservation(
  578. struct xfs_mount *mp)
  579. {
  580. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  581. xfs_calc_inode_res(mp, 2) +
  582. xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
  583. xfs_calc_buf_res(1, mp->m_rsumsize);
  584. }
  585. /*
  586. * Logging the inode modification timestamp on a synchronous write.
  587. * inode
  588. */
  589. STATIC uint
  590. xfs_calc_swrite_reservation(
  591. struct xfs_mount *mp)
  592. {
  593. return xfs_calc_inode_res(mp, 1);
  594. }
  595. /*
  596. * Logging the inode mode bits when writing a setuid/setgid file
  597. * inode
  598. */
  599. STATIC uint
  600. xfs_calc_writeid_reservation(
  601. struct xfs_mount *mp)
  602. {
  603. return xfs_calc_inode_res(mp, 1);
  604. }
  605. /*
  606. * Converting the inode from non-attributed to attributed.
  607. * the inode being converted: inode size
  608. * agf block and superblock (for block allocation)
  609. * the new block (directory sized)
  610. * bmap blocks for the new directory block
  611. * allocation btrees
  612. */
  613. STATIC uint
  614. xfs_calc_addafork_reservation(
  615. struct xfs_mount *mp)
  616. {
  617. return XFS_DQUOT_LOGRES(mp) +
  618. xfs_calc_inode_res(mp, 1) +
  619. xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  620. xfs_calc_buf_res(1, mp->m_dir_geo->blksize) +
  621. xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
  622. XFS_FSB_TO_B(mp, 1)) +
  623. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
  624. XFS_FSB_TO_B(mp, 1));
  625. }
  626. /*
  627. * Removing the attribute fork of a file
  628. * the inode being truncated: inode size
  629. * the inode's bmap btree: max depth * block size
  630. * And the bmap_finish transaction can free the blocks and bmap blocks:
  631. * the agf for each of the ags: 4 * sector size
  632. * the agfl for each of the ags: 4 * sector size
  633. * the super block to reflect the freed blocks: sector size
  634. * worst case split in allocation btrees per extent assuming 4 extents:
  635. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  636. */
  637. STATIC uint
  638. xfs_calc_attrinval_reservation(
  639. struct xfs_mount *mp)
  640. {
  641. return max((xfs_calc_inode_res(mp, 1) +
  642. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
  643. XFS_FSB_TO_B(mp, 1))),
  644. (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
  645. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4),
  646. XFS_FSB_TO_B(mp, 1))));
  647. }
  648. /*
  649. * Setting an attribute at mount time.
  650. * the inode getting the attribute
  651. * the superblock for allocations
  652. * the agfs extents are allocated from
  653. * the attribute btree * max depth
  654. * the inode allocation btree
  655. * Since attribute transaction space is dependent on the size of the attribute,
  656. * the calculation is done partially at mount time and partially at runtime(see
  657. * below).
  658. */
  659. STATIC uint
  660. xfs_calc_attrsetm_reservation(
  661. struct xfs_mount *mp)
  662. {
  663. return XFS_DQUOT_LOGRES(mp) +
  664. xfs_calc_inode_res(mp, 1) +
  665. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  666. xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
  667. }
  668. /*
  669. * Setting an attribute at runtime, transaction space unit per block.
  670. * the superblock for allocations: sector size
  671. * the inode bmap btree could join or split: max depth * block size
  672. * Since the runtime attribute transaction space is dependent on the total
  673. * blocks needed for the 1st bmap, here we calculate out the space unit for
  674. * one block so that the caller could figure out the total space according
  675. * to the attibute extent length in blocks by:
  676. * ext * M_RES(mp)->tr_attrsetrt.tr_logres
  677. */
  678. STATIC uint
  679. xfs_calc_attrsetrt_reservation(
  680. struct xfs_mount *mp)
  681. {
  682. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  683. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
  684. XFS_FSB_TO_B(mp, 1));
  685. }
  686. /*
  687. * Removing an attribute.
  688. * the inode: inode size
  689. * the attribute btree could join: max depth * block size
  690. * the inode bmap btree could join or split: max depth * block size
  691. * And the bmap_finish transaction can free the attr blocks freed giving:
  692. * the agf for the ag in which the blocks live: 2 * sector size
  693. * the agfl for the ag in which the blocks live: 2 * sector size
  694. * the superblock for the free block count: sector size
  695. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  696. */
  697. STATIC uint
  698. xfs_calc_attrrm_reservation(
  699. struct xfs_mount *mp)
  700. {
  701. return XFS_DQUOT_LOGRES(mp) +
  702. max((xfs_calc_inode_res(mp, 1) +
  703. xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
  704. XFS_FSB_TO_B(mp, 1)) +
  705. (uint)XFS_FSB_TO_B(mp,
  706. XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
  707. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
  708. (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  709. xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
  710. XFS_FSB_TO_B(mp, 1))));
  711. }
  712. /*
  713. * Clearing a bad agino number in an agi hash bucket.
  714. */
  715. STATIC uint
  716. xfs_calc_clear_agi_bucket_reservation(
  717. struct xfs_mount *mp)
  718. {
  719. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  720. }
  721. /*
  722. * Adjusting quota limits.
  723. * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
  724. */
  725. STATIC uint
  726. xfs_calc_qm_setqlim_reservation(void)
  727. {
  728. return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
  729. }
  730. /*
  731. * Allocating quota on disk if needed.
  732. * the write transaction log space for quota file extent allocation
  733. * the unit of quota allocation: one system block size
  734. */
  735. STATIC uint
  736. xfs_calc_qm_dqalloc_reservation(
  737. struct xfs_mount *mp)
  738. {
  739. return xfs_calc_write_reservation(mp) +
  740. xfs_calc_buf_res(1,
  741. XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
  742. }
  743. /*
  744. * Turning off quotas.
  745. * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
  746. * the superblock for the quota flags: sector size
  747. */
  748. STATIC uint
  749. xfs_calc_qm_quotaoff_reservation(
  750. struct xfs_mount *mp)
  751. {
  752. return sizeof(struct xfs_qoff_logitem) * 2 +
  753. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  754. }
  755. /*
  756. * End of turning off quotas.
  757. * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
  758. */
  759. STATIC uint
  760. xfs_calc_qm_quotaoff_end_reservation(void)
  761. {
  762. return sizeof(struct xfs_qoff_logitem) * 2;
  763. }
  764. /*
  765. * Syncing the incore super block changes to disk.
  766. * the super block to reflect the changes: sector size
  767. */
  768. STATIC uint
  769. xfs_calc_sb_reservation(
  770. struct xfs_mount *mp)
  771. {
  772. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  773. }
  774. void
  775. xfs_trans_resv_calc(
  776. struct xfs_mount *mp,
  777. struct xfs_trans_resv *resp)
  778. {
  779. /*
  780. * The following transactions are logged in physical format and
  781. * require a permanent reservation on space.
  782. */
  783. resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
  784. if (xfs_sb_version_hasreflink(&mp->m_sb))
  785. resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
  786. else
  787. resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
  788. resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  789. resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
  790. if (xfs_sb_version_hasreflink(&mp->m_sb))
  791. resp->tr_itruncate.tr_logcount =
  792. XFS_ITRUNCATE_LOG_COUNT_REFLINK;
  793. else
  794. resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
  795. resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  796. resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
  797. resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
  798. resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  799. resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
  800. resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
  801. resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  802. resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
  803. resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
  804. resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  805. resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
  806. resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
  807. resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  808. resp->tr_create.tr_logres = xfs_calc_icreate_reservation(mp);
  809. resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
  810. resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  811. resp->tr_create_tmpfile.tr_logres =
  812. xfs_calc_create_tmpfile_reservation(mp);
  813. resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
  814. resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  815. resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
  816. resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
  817. resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  818. resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
  819. resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
  820. resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  821. resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
  822. resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
  823. resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  824. resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
  825. resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
  826. resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  827. resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
  828. resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
  829. resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  830. resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
  831. resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
  832. resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  833. resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
  834. resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
  835. resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  836. resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
  837. if (xfs_sb_version_hasreflink(&mp->m_sb))
  838. resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
  839. else
  840. resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
  841. resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
  842. /*
  843. * The following transactions are logged in logical format with
  844. * a default log count.
  845. */
  846. resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation();
  847. resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  848. resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
  849. resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  850. resp->tr_qm_equotaoff.tr_logres =
  851. xfs_calc_qm_quotaoff_end_reservation();
  852. resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  853. resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
  854. resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
  855. /* The following transaction are logged in logical format */
  856. resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
  857. resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
  858. resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
  859. resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
  860. resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
  861. resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
  862. resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
  863. resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
  864. }