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