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