xfs_trans_resv.c 27 KB

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