Linux Audio

Check our new training course

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