xfs_trans_resv.c 22 KB

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