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