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