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