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