xfs_trans_resv.c 33 KB

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