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