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