xfs_trans_resv.c 28 KB

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