]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/libxfs/xfs_trans_resv.c
xfs: convert to SPDX license tags
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / libxfs / xfs_trans_resv.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
7fd36c44
DC
2/*
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * Copyright (C) 2010 Red Hat, Inc.
5 * All Rights Reserved.
7fd36c44
DC
6 */
7#include "xfs.h"
8#include "xfs_fs.h"
70a9883c 9#include "xfs_shared.h"
239880ef
DC
10#include "xfs_format.h"
11#include "xfs_log_format.h"
7fd36c44 12#include "xfs_trans_resv.h"
7fd36c44 13#include "xfs_mount.h"
57062787 14#include "xfs_da_format.h"
d6cf1305 15#include "xfs_da_btree.h"
7fd36c44 16#include "xfs_inode.h"
a4fbe6ab 17#include "xfs_bmap_btree.h"
7fd36c44 18#include "xfs_ialloc.h"
7fd36c44 19#include "xfs_quota.h"
239880ef 20#include "xfs_trans.h"
7fd36c44
DC
21#include "xfs_qm.h"
22#include "xfs_trans_space.h"
23#include "xfs_trace.h"
24
57af33e4
BF
25#define _ALLOC true
26#define _FREE false
27
7fd36c44
DC
28/*
29 * A buffer has a format structure overhead in the log in addition
30 * to the data, so we need to take this into account when reserving
31 * space in a transaction for a buffer. Round the space required up
32 * to a multiple of 128 bytes so that we don't change the historical
33 * reservation that has been used for this overhead.
34 */
35STATIC uint
36xfs_buf_log_overhead(void)
37{
38 return round_up(sizeof(struct xlog_op_header) +
39 sizeof(struct xfs_buf_log_format), 128);
40}
41
42/*
43 * Calculate out transaction log reservation per item in bytes.
44 *
45 * The nbufs argument is used to indicate the number of items that
46 * will be changed in a transaction. size is used to tell how many
47 * bytes should be reserved per item.
48 */
49STATIC uint
50xfs_calc_buf_res(
51 uint nbufs,
52 uint size)
53{
54 return nbufs * (size + xfs_buf_log_overhead());
55}
56
fa30f03c
DW
57/*
58 * Per-extent log reservation for the btree changes involved in freeing or
59 * allocating an extent. In classic XFS there were two trees that will be
60 * modified (bnobt + cntbt). With rmap enabled, there are three trees
f310bd2e
DW
61 * (rmapbt). With reflink, there are four trees (refcountbt). The number of
62 * blocks reserved is based on the formula:
fa30f03c
DW
63 *
64 * num trees * ((2 blocks/level * max depth) - 1)
65 *
66 * Keep in mind that max depth is calculated separately for each type of tree.
67 */
1946b91c 68uint
fa30f03c
DW
69xfs_allocfree_log_count(
70 struct xfs_mount *mp,
71 uint num_ops)
72{
73 uint blocks;
74
75 blocks = num_ops * 2 * (2 * mp->m_ag_maxlevels - 1);
76 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
77 blocks += num_ops * (2 * mp->m_rmap_maxlevels - 1);
f310bd2e
DW
78 if (xfs_sb_version_hasreflink(&mp->m_sb))
79 blocks += num_ops * (2 * mp->m_refc_maxlevels - 1);
fa30f03c
DW
80
81 return blocks;
82}
83
23956703
DC
84/*
85 * Logging inodes is really tricksy. They are logged in memory format,
86 * which means that what we write into the log doesn't directly translate into
87 * the amount of space they use on disk.
88 *
89 * Case in point - btree format forks in memory format use more space than the
90 * on-disk format. In memory, the buffer contains a normal btree block header so
91 * the btree code can treat it as though it is just another generic buffer.
92 * However, when we write it to the inode fork, we don't write all of this
93 * header as it isn't needed. e.g. the root is only ever in the inode, so
94 * there's no need for sibling pointers which would waste 16 bytes of space.
95 *
96 * Hence when we have an inode with a maximally sized btree format fork, then
97 * amount of information we actually log is greater than the size of the inode
98 * on disk. Hence we need an inode reservation function that calculates all this
99 * correctly. So, we log:
100 *
fe4c224a
DC
101 * - 4 log op headers for object
102 * - for the ilf, the inode core and 2 forks
23956703 103 * - inode log format object
fe4c224a
DC
104 * - the inode core
105 * - two inode forks containing bmap btree root blocks.
106 * - the btree data contained by both forks will fit into the inode size,
107 * hence when combined with the inode core above, we have a total of the
108 * actual inode size.
109 * - the BMBT headers need to be accounted separately, as they are
110 * additional to the records and pointers that fit inside the inode
111 * forks.
23956703
DC
112 */
113STATIC uint
114xfs_calc_inode_res(
115 struct xfs_mount *mp,
116 uint ninodes)
117{
fe4c224a
DC
118 return ninodes *
119 (4 * sizeof(struct xlog_op_header) +
120 sizeof(struct xfs_inode_log_format) +
121 mp->m_sb.sb_inodesize +
122 2 * XFS_BMBT_BLOCK_LEN(mp));
23956703
DC
123}
124
9d43b180 125/*
f03c78f3
BF
126 * Inode btree record insertion/removal modifies the inode btree and free space
127 * btrees (since the inobt does not use the agfl). This requires the following
128 * reservation:
9d43b180 129 *
f03c78f3
BF
130 * the inode btree: max depth * blocksize
131 * the allocation btrees: 2 trees * (max depth - 1) * block size
9d43b180 132 *
f03c78f3
BF
133 * The caller must account for SB and AG header modifications, etc.
134 */
135STATIC uint
136xfs_calc_inobt_res(
137 struct xfs_mount *mp)
138{
139 return xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
140 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
141 XFS_FSB_TO_B(mp, 1));
142}
143
144/*
145 * The free inode btree is a conditional feature. The behavior differs slightly
146 * from that of the traditional inode btree in that the finobt tracks records
147 * for inode chunks with at least one free inode. A record can be removed from
148 * the tree during individual inode allocation. Therefore the finobt
149 * reservation is unconditional for both the inode chunk allocation and
150 * individual inode allocation (modify) cases.
9d43b180 151 *
f03c78f3
BF
152 * Behavior aside, the reservation for finobt modification is equivalent to the
153 * traditional inobt: cover a full finobt shape change plus block allocation.
9d43b180
BF
154 */
155STATIC uint
156xfs_calc_finobt_res(
f03c78f3 157 struct xfs_mount *mp)
9d43b180 158{
9d43b180
BF
159 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
160 return 0;
161
f03c78f3 162 return xfs_calc_inobt_res(mp);
9d43b180
BF
163}
164
57af33e4
BF
165/*
166 * Calculate the reservation required to allocate or free an inode chunk. This
167 * includes:
168 *
169 * the allocation btrees: 2 trees * (max depth - 1) * block size
170 * the inode chunk: m_ialloc_blks * N
171 *
172 * The size N of the inode chunk reservation depends on whether it is for
173 * allocation or free and which type of create transaction is in use. An inode
174 * chunk free always invalidates the buffers and only requires reservation for
175 * headers (N == 0). An inode chunk allocation requires a chunk sized
176 * reservation on v4 and older superblocks to initialize the chunk. No chunk
177 * reservation is required for allocation on v5 supers, which use ordered
178 * buffers to initialize.
179 */
180STATIC uint
181xfs_calc_inode_chunk_res(
182 struct xfs_mount *mp,
183 bool alloc)
184{
185 uint res, size = 0;
186
187 res = xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
188 XFS_FSB_TO_B(mp, 1));
189 if (alloc) {
190 /* icreate tx uses ordered buffers */
191 if (xfs_sb_version_hascrc(&mp->m_sb))
192 return res;
193 size = XFS_FSB_TO_B(mp, 1);
194 }
195
196 res += xfs_calc_buf_res(mp->m_ialloc_blks, size);
197 return res;
198}
199
7fd36c44
DC
200/*
201 * Various log reservation values.
202 *
203 * These are based on the size of the file system block because that is what
204 * most transactions manipulate. Each adds in an additional 128 bytes per
205 * item logged to try to account for the overhead of the transaction mechanism.
206 *
207 * Note: Most of the reservations underestimate the number of allocation
310a75a3 208 * groups into which they could free extents in the xfs_defer_finish() call.
7fd36c44 209 * This is because the number in the worst case is quite high and quite
310a75a3 210 * unusual. In order to fix this we need to change xfs_defer_finish() to free
7fd36c44
DC
211 * extents in only a single AG at a time. This will require changes to the
212 * EFI code as well, however, so that the EFI for the extents not freed is
213 * logged again in each transaction. See SGI PV #261917.
214 *
215 * Reservation functions here avoid a huge stack in xfs_trans_init due to
216 * register overflow from temporaries in the calculations.
217 */
218
219
220/*
221 * In a write transaction we can allocate a maximum of 2
222 * extents. This gives:
223 * the inode getting the new extents: inode size
224 * the inode's bmap btree: max depth * block size
225 * the agfs of the ags from which the extents are allocated: 2 * sector
226 * the superblock free block counter: sector size
227 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
228 * And the bmap_finish transaction can free bmap blocks in a join:
229 * the agfs of the ags containing the blocks: 2 * sector size
230 * the agfls of the ags containing the blocks: 2 * sector size
231 * the super block free block counter: sector size
232 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
233 */
234STATIC uint
235xfs_calc_write_reservation(
236 struct xfs_mount *mp)
237{
238 return XFS_DQUOT_LOGRES(mp) +
23956703 239 MAX((xfs_calc_inode_res(mp, 1) +
7fd36c44
DC
240 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
241 XFS_FSB_TO_B(mp, 1)) +
242 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
fa30f03c 243 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
7fd36c44
DC
244 XFS_FSB_TO_B(mp, 1))),
245 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
fa30f03c 246 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
7fd36c44
DC
247 XFS_FSB_TO_B(mp, 1))));
248}
249
250/*
251 * In truncating a file we free up to two extents at once. We can modify:
252 * the inode being truncated: inode size
253 * the inode's bmap btree: (max depth + 1) * block size
254 * And the bmap_finish transaction can free the blocks and bmap blocks:
255 * the agf for each of the ags: 4 * sector size
256 * the agfl for each of the ags: 4 * sector size
257 * the super block to reflect the freed blocks: sector size
258 * worst case split in allocation btrees per extent assuming 4 extents:
259 * 4 exts * 2 trees * (2 * max depth - 1) * block size
7fd36c44
DC
260 */
261STATIC uint
262xfs_calc_itruncate_reservation(
263 struct xfs_mount *mp)
264{
265 return XFS_DQUOT_LOGRES(mp) +
23956703 266 MAX((xfs_calc_inode_res(mp, 1) +
7fd36c44
DC
267 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
268 XFS_FSB_TO_B(mp, 1))),
269 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
fa30f03c 270 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4),
a606ebdb 271 XFS_FSB_TO_B(mp, 1))));
7fd36c44
DC
272}
273
274/*
275 * In renaming a files we can modify:
276 * the four inodes involved: 4 * inode size
277 * the two directory btrees: 2 * (max depth + v2) * dir block size
278 * the two directory bmap btrees: 2 * max depth * block size
279 * And the bmap_finish transaction can free dir and bmap blocks (two sets
280 * of bmap blocks) giving:
281 * the agf for the ags in which the blocks live: 3 * sector size
282 * the agfl for the ags in which the blocks live: 3 * sector size
283 * the superblock for the free block count: sector size
284 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
285 */
286STATIC uint
287xfs_calc_rename_reservation(
288 struct xfs_mount *mp)
289{
290 return XFS_DQUOT_LOGRES(mp) +
23956703 291 MAX((xfs_calc_inode_res(mp, 4) +
7fd36c44
DC
292 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
293 XFS_FSB_TO_B(mp, 1))),
294 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
fa30f03c 295 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 3),
7fd36c44
DC
296 XFS_FSB_TO_B(mp, 1))));
297}
298
ab297431
ZYW
299/*
300 * For removing an inode from unlinked list at first, we can modify:
301 * the agi hash list and counters: sector size
302 * the on disk inode before ours in the agi hash list: inode cluster size
e8341d9f 303 * the on disk inode in the agi hash list: inode cluster size
ab297431
ZYW
304 */
305STATIC uint
306xfs_calc_iunlink_remove_reservation(
307 struct xfs_mount *mp)
308{
309 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
e8341d9f 310 2 * max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
ab297431
ZYW
311}
312
7fd36c44
DC
313/*
314 * For creating a link to an inode:
315 * the parent directory inode: inode size
316 * the linked inode: inode size
317 * the directory btree could split: (max depth + v2) * dir block size
318 * the directory bmap btree could join or split: (max depth + v2) * blocksize
319 * And the bmap_finish transaction can free some bmap blocks giving:
320 * the agf for the ag in which the blocks live: sector size
321 * the agfl for the ag in which the blocks live: sector size
322 * the superblock for the free block count: sector size
323 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
324 */
325STATIC uint
326xfs_calc_link_reservation(
327 struct xfs_mount *mp)
328{
329 return XFS_DQUOT_LOGRES(mp) +
ab297431 330 xfs_calc_iunlink_remove_reservation(mp) +
23956703 331 MAX((xfs_calc_inode_res(mp, 2) +
7fd36c44
DC
332 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
333 XFS_FSB_TO_B(mp, 1))),
334 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
fa30f03c 335 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
7fd36c44
DC
336 XFS_FSB_TO_B(mp, 1))));
337}
338
99b6436b
ZYW
339/*
340 * For adding an inode to unlinked list we can modify:
341 * the agi hash list: sector size
e8341d9f 342 * the on disk inode: inode cluster size
99b6436b
ZYW
343 */
344STATIC uint
345xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
346{
347 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
e8341d9f 348 max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
99b6436b
ZYW
349}
350
7fd36c44
DC
351/*
352 * For removing a directory entry we can modify:
353 * the parent directory inode: inode size
354 * the removed inode: inode size
355 * the directory btree could join: (max depth + v2) * dir block size
356 * the directory bmap btree could join or split: (max depth + v2) * blocksize
357 * And the bmap_finish transaction can free the dir and bmap blocks giving:
358 * the agf for the ag in which the blocks live: 2 * sector size
359 * the agfl for the ag in which the blocks live: 2 * sector size
360 * the superblock for the free block count: sector size
361 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
362 */
363STATIC uint
364xfs_calc_remove_reservation(
365 struct xfs_mount *mp)
366{
367 return XFS_DQUOT_LOGRES(mp) +
99b6436b
ZYW
368 xfs_calc_iunlink_add_reservation(mp) +
369 MAX((xfs_calc_inode_res(mp, 1) +
7fd36c44
DC
370 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
371 XFS_FSB_TO_B(mp, 1))),
99b6436b 372 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
fa30f03c 373 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
7fd36c44
DC
374 XFS_FSB_TO_B(mp, 1))));
375}
376
377/*
378 * For create, break it in to the two cases that the transaction
379 * covers. We start with the modify case - allocation done by modification
380 * of the state of existing inodes - and the allocation case.
381 */
382
383/*
384 * For create we can modify:
385 * the parent directory inode: inode size
386 * the new inode: inode size
387 * the inode btree entry: block size
388 * the superblock for the nlink flag: sector size
389 * the directory btree: (max depth + v2) * dir block size
390 * the directory inode's bmap btree: (max depth + v2) * block size
9d43b180 391 * the finobt (record modification and allocation btrees)
7fd36c44
DC
392 */
393STATIC uint
394xfs_calc_create_resv_modify(
395 struct xfs_mount *mp)
396{
23956703 397 return xfs_calc_inode_res(mp, 2) +
7fd36c44
DC
398 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
399 (uint)XFS_FSB_TO_B(mp, 1) +
9d43b180 400 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1)) +
f03c78f3 401 xfs_calc_finobt_res(mp);
7fd36c44
DC
402}
403
7fd36c44
DC
404/*
405 * For icreate we can allocate some inodes giving:
406 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
407 * the superblock for the nlink flag: sector size
c017cb5d 408 * the inode chunk (allocation, optional init)
f03c78f3 409 * the inobt (record insertion)
c017cb5d 410 * the finobt (optional, record insertion)
7fd36c44
DC
411 */
412STATIC uint
413xfs_calc_icreate_resv_alloc(
414 struct xfs_mount *mp)
415{
416 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
417 mp->m_sb.sb_sectsize +
57af33e4 418 xfs_calc_inode_chunk_res(mp, _ALLOC) +
f03c78f3
BF
419 xfs_calc_inobt_res(mp) +
420 xfs_calc_finobt_res(mp);
7fd36c44
DC
421}
422
423STATIC uint
424xfs_calc_icreate_reservation(xfs_mount_t *mp)
425{
426 return XFS_DQUOT_LOGRES(mp) +
427 MAX(xfs_calc_icreate_resv_alloc(mp),
428 xfs_calc_create_resv_modify(mp));
429}
430
99b6436b
ZYW
431STATIC uint
432xfs_calc_create_tmpfile_reservation(
433 struct xfs_mount *mp)
434{
435 uint res = XFS_DQUOT_LOGRES(mp);
436
c017cb5d 437 res += xfs_calc_icreate_resv_alloc(mp);
99b6436b
ZYW
438 return res + xfs_calc_iunlink_add_reservation(mp);
439}
440
7fd36c44
DC
441/*
442 * Making a new directory is the same as creating a new file.
443 */
444STATIC uint
445xfs_calc_mkdir_reservation(
446 struct xfs_mount *mp)
447{
c017cb5d 448 return xfs_calc_icreate_reservation(mp);
7fd36c44
DC
449}
450
451
452/*
453 * Making a new symplink is the same as creating a new file, but
454 * with the added blocks for remote symlink data which can be up to 1kB in
6eb0b8df 455 * length (XFS_SYMLINK_MAXLEN).
7fd36c44
DC
456 */
457STATIC uint
458xfs_calc_symlink_reservation(
459 struct xfs_mount *mp)
460{
c017cb5d 461 return xfs_calc_icreate_reservation(mp) +
6eb0b8df 462 xfs_calc_buf_res(1, XFS_SYMLINK_MAXLEN);
7fd36c44
DC
463}
464
465/*
466 * In freeing an inode we can modify:
467 * the inode being freed: inode size
a6f48590
BF
468 * the super block free inode counter, AGF and AGFL: sector size
469 * the on disk inode (agi unlinked list removal)
57af33e4 470 * the inode chunk (invalidated, headers only)
f03c78f3 471 * the inode btree
9d43b180 472 * the finobt (record insertion, removal or modification)
f03c78f3 473 *
57af33e4
BF
474 * Note that the inode chunk res. includes an allocfree res. for freeing of the
475 * inode chunk. This is technically extraneous because the inode chunk free is
476 * deferred (it occurs after a transaction roll). Include the extra reservation
477 * anyways since we've had reports of ifree transaction overruns due to too many
478 * agfl fixups during inode chunk frees.
7fd36c44
DC
479 */
480STATIC uint
481xfs_calc_ifree_reservation(
482 struct xfs_mount *mp)
483{
484 return XFS_DQUOT_LOGRES(mp) +
23956703 485 xfs_calc_inode_res(mp, 1) +
a6f48590 486 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
ab297431 487 xfs_calc_iunlink_remove_reservation(mp) +
57af33e4 488 xfs_calc_inode_chunk_res(mp, _FREE) +
f03c78f3
BF
489 xfs_calc_inobt_res(mp) +
490 xfs_calc_finobt_res(mp);
7fd36c44
DC
491}
492
493/*
494 * When only changing the inode we log the inode and possibly the superblock
495 * We also add a bit of slop for the transaction stuff.
496 */
497STATIC uint
498xfs_calc_ichange_reservation(
499 struct xfs_mount *mp)
500{
501 return XFS_DQUOT_LOGRES(mp) +
23956703
DC
502 xfs_calc_inode_res(mp, 1) +
503 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
7fd36c44
DC
504
505}
506
507/*
508 * Growing the data section of the filesystem.
509 * superblock
510 * agi and agf
511 * allocation btrees
512 */
513STATIC uint
514xfs_calc_growdata_reservation(
515 struct xfs_mount *mp)
516{
517 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
fa30f03c 518 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
7fd36c44
DC
519 XFS_FSB_TO_B(mp, 1));
520}
521
522/*
523 * Growing the rt section of the filesystem.
524 * In the first set of transactions (ALLOC) we allocate space to the
525 * bitmap or summary files.
526 * superblock: sector size
527 * agf of the ag from which the extent is allocated: sector size
528 * bmap btree for bitmap/summary inode: max depth * blocksize
529 * bitmap/summary inode: inode size
530 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
531 */
532STATIC uint
533xfs_calc_growrtalloc_reservation(
534 struct xfs_mount *mp)
535{
536 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
537 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
538 XFS_FSB_TO_B(mp, 1)) +
23956703 539 xfs_calc_inode_res(mp, 1) +
fa30f03c 540 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
7fd36c44
DC
541 XFS_FSB_TO_B(mp, 1));
542}
543
544/*
545 * Growing the rt section of the filesystem.
546 * In the second set of transactions (ZERO) we zero the new metadata blocks.
547 * one bitmap/summary block: blocksize
548 */
549STATIC uint
550xfs_calc_growrtzero_reservation(
551 struct xfs_mount *mp)
552{
553 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
554}
555
556/*
557 * Growing the rt section of the filesystem.
558 * In the third set of transactions (FREE) we update metadata without
559 * allocating any new blocks.
560 * superblock: sector size
561 * bitmap inode: inode size
562 * summary inode: inode size
563 * one bitmap block: blocksize
564 * summary blocks: new summary size
565 */
566STATIC uint
567xfs_calc_growrtfree_reservation(
568 struct xfs_mount *mp)
569{
570 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
23956703 571 xfs_calc_inode_res(mp, 2) +
7fd36c44
DC
572 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
573 xfs_calc_buf_res(1, mp->m_rsumsize);
574}
575
576/*
577 * Logging the inode modification timestamp on a synchronous write.
578 * inode
579 */
580STATIC uint
581xfs_calc_swrite_reservation(
582 struct xfs_mount *mp)
583{
23956703 584 return xfs_calc_inode_res(mp, 1);
7fd36c44
DC
585}
586
587/*
588 * Logging the inode mode bits when writing a setuid/setgid file
589 * inode
590 */
591STATIC uint
23956703
DC
592xfs_calc_writeid_reservation(
593 struct xfs_mount *mp)
7fd36c44 594{
23956703 595 return xfs_calc_inode_res(mp, 1);
7fd36c44
DC
596}
597
598/*
599 * Converting the inode from non-attributed to attributed.
600 * the inode being converted: inode size
601 * agf block and superblock (for block allocation)
602 * the new block (directory sized)
603 * bmap blocks for the new directory block
604 * allocation btrees
605 */
606STATIC uint
607xfs_calc_addafork_reservation(
608 struct xfs_mount *mp)
609{
610 return XFS_DQUOT_LOGRES(mp) +
23956703 611 xfs_calc_inode_res(mp, 1) +
7fd36c44 612 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
8f66193c 613 xfs_calc_buf_res(1, mp->m_dir_geo->blksize) +
7fd36c44
DC
614 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
615 XFS_FSB_TO_B(mp, 1)) +
fa30f03c 616 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
7fd36c44
DC
617 XFS_FSB_TO_B(mp, 1));
618}
619
620/*
621 * Removing the attribute fork of a file
622 * the inode being truncated: inode size
623 * the inode's bmap btree: max depth * block size
624 * And the bmap_finish transaction can free the blocks and bmap blocks:
625 * the agf for each of the ags: 4 * sector size
626 * the agfl for each of the ags: 4 * sector size
627 * the super block to reflect the freed blocks: sector size
628 * worst case split in allocation btrees per extent assuming 4 extents:
629 * 4 exts * 2 trees * (2 * max depth - 1) * block size
630 */
631STATIC uint
632xfs_calc_attrinval_reservation(
633 struct xfs_mount *mp)
634{
23956703 635 return MAX((xfs_calc_inode_res(mp, 1) +
7fd36c44
DC
636 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
637 XFS_FSB_TO_B(mp, 1))),
638 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
fa30f03c 639 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4),
7fd36c44
DC
640 XFS_FSB_TO_B(mp, 1))));
641}
642
643/*
644 * Setting an attribute at mount time.
645 * the inode getting the attribute
646 * the superblock for allocations
647 * the agfs extents are allocated from
648 * the attribute btree * max depth
649 * the inode allocation btree
650 * Since attribute transaction space is dependent on the size of the attribute,
651 * the calculation is done partially at mount time and partially at runtime(see
652 * below).
653 */
654STATIC uint
655xfs_calc_attrsetm_reservation(
656 struct xfs_mount *mp)
657{
658 return XFS_DQUOT_LOGRES(mp) +
23956703 659 xfs_calc_inode_res(mp, 1) +
7fd36c44
DC
660 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
661 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
662}
663
664/*
665 * Setting an attribute at runtime, transaction space unit per block.
666 * the superblock for allocations: sector size
667 * the inode bmap btree could join or split: max depth * block size
668 * Since the runtime attribute transaction space is dependent on the total
669 * blocks needed for the 1st bmap, here we calculate out the space unit for
670 * one block so that the caller could figure out the total space according
3d3c8b52
JL
671 * to the attibute extent length in blocks by:
672 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
7fd36c44
DC
673 */
674STATIC uint
675xfs_calc_attrsetrt_reservation(
676 struct xfs_mount *mp)
677{
678 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
679 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
680 XFS_FSB_TO_B(mp, 1));
681}
682
683/*
684 * Removing an attribute.
685 * the inode: inode size
686 * the attribute btree could join: max depth * block size
687 * the inode bmap btree could join or split: max depth * block size
688 * And the bmap_finish transaction can free the attr blocks freed giving:
689 * the agf for the ag in which the blocks live: 2 * sector size
690 * the agfl for the ag in which the blocks live: 2 * sector size
691 * the superblock for the free block count: sector size
692 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
693 */
694STATIC uint
695xfs_calc_attrrm_reservation(
696 struct xfs_mount *mp)
697{
698 return XFS_DQUOT_LOGRES(mp) +
23956703 699 MAX((xfs_calc_inode_res(mp, 1) +
7fd36c44
DC
700 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
701 XFS_FSB_TO_B(mp, 1)) +
702 (uint)XFS_FSB_TO_B(mp,
703 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
704 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
705 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
fa30f03c 706 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
7fd36c44
DC
707 XFS_FSB_TO_B(mp, 1))));
708}
709
710/*
711 * Clearing a bad agino number in an agi hash bucket.
712 */
713STATIC uint
714xfs_calc_clear_agi_bucket_reservation(
715 struct xfs_mount *mp)
716{
717 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
718}
719
7fd36c44
DC
720/*
721 * Adjusting quota limits.
722 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
723 */
724STATIC uint
a1f69417 725xfs_calc_qm_setqlim_reservation(void)
7fd36c44
DC
726{
727 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
728}
729
730/*
731 * Allocating quota on disk if needed.
410b11a6 732 * the write transaction log space for quota file extent allocation
7fd36c44
DC
733 * the unit of quota allocation: one system block size
734 */
735STATIC uint
736xfs_calc_qm_dqalloc_reservation(
737 struct xfs_mount *mp)
738{
410b11a6 739 return xfs_calc_write_reservation(mp) +
7fd36c44
DC
740 xfs_calc_buf_res(1,
741 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
742}
743
744/*
745 * Turning off quotas.
746 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
747 * the superblock for the quota flags: sector size
748 */
749STATIC uint
750xfs_calc_qm_quotaoff_reservation(
751 struct xfs_mount *mp)
752{
753 return sizeof(struct xfs_qoff_logitem) * 2 +
754 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
755}
756
757/*
758 * End of turning off quotas.
759 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
760 */
761STATIC uint
a1f69417 762xfs_calc_qm_quotaoff_end_reservation(void)
7fd36c44
DC
763{
764 return sizeof(struct xfs_qoff_logitem) * 2;
765}
766
767/*
768 * Syncing the incore super block changes to disk.
769 * the super block to reflect the changes: sector size
770 */
771STATIC uint
772xfs_calc_sb_reservation(
773 struct xfs_mount *mp)
774{
775 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
776}
777
778void
779xfs_trans_resv_calc(
780 struct xfs_mount *mp,
781 struct xfs_trans_resv *resp)
782{
0eadd102
JL
783 /*
784 * The following transactions are logged in physical format and
785 * require a permanent reservation on space.
786 */
787 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
80de462e
DW
788 if (xfs_sb_version_hasreflink(&mp->m_sb))
789 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
790 else
791 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
0eadd102
JL
792 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
793
794 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
80de462e
DW
795 if (xfs_sb_version_hasreflink(&mp->m_sb))
796 resp->tr_itruncate.tr_logcount =
797 XFS_ITRUNCATE_LOG_COUNT_REFLINK;
798 else
799 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
0eadd102
JL
800 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
801
802 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
803 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
804 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
805
806 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
807 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
808 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
809
810 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
811 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
812 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
813
814 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
815 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
816 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
817
c017cb5d 818 resp->tr_create.tr_logres = xfs_calc_icreate_reservation(mp);
0eadd102
JL
819 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
820 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
821
99b6436b
ZYW
822 resp->tr_create_tmpfile.tr_logres =
823 xfs_calc_create_tmpfile_reservation(mp);
824 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
825 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
826
0eadd102
JL
827 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
828 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
829 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
830
831 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
832 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
833 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
834
835 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
836 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
837 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
838
839 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
840 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
841 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
842
843 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
844 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
845 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
846
847 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
848 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
849 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
850
851 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
852 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
853 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
854
855 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
80de462e
DW
856 if (xfs_sb_version_hasreflink(&mp->m_sb))
857 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
858 else
859 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
0eadd102
JL
860 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
861
862 /*
863 * The following transactions are logged in logical format with
864 * a default log count.
865 */
a1f69417 866 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation();
0eadd102
JL
867 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
868
869 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
870 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
871
872 resp->tr_qm_equotaoff.tr_logres =
a1f69417 873 xfs_calc_qm_quotaoff_end_reservation();
0eadd102
JL
874 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
875
876 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
877 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
878
879 /* The following transaction are logged in logical format */
880 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
881 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
20996c93 882 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
0eadd102
JL
883 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
884 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
885 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
886 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
887 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
7fd36c44 888}