xfs_trans_resv.c 29 KB

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