xfs_trans_resv.c 29 KB

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