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