xfs_trans_resv.c 25 KB

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