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