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