xfs_trans_resv.c 26 KB

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