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