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