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