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