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