Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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((__uint16_t)XFS_FSB_TO_B(mp, 1),
389                     XFS_INODE_CLUSTER_SIZE(mp)) +
390                 xfs_calc_buf_res(1, 0) +
391                 xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
392                                  mp->m_in_maxlevels, 0) +
393                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
394                                  XFS_FSB_TO_B(mp, 1));
395 }
396
397 /*
398  * When only changing the inode we log the inode and possibly the superblock
399  * We also add a bit of slop for the transaction stuff.
400  */
401 STATIC uint
402 xfs_calc_ichange_reservation(
403         struct xfs_mount        *mp)
404 {
405         return XFS_DQUOT_LOGRES(mp) +
406                 xfs_calc_inode_res(mp, 1) +
407                 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
408
409 }
410
411 /*
412  * Growing the data section of the filesystem.
413  *      superblock
414  *      agi and agf
415  *      allocation btrees
416  */
417 STATIC uint
418 xfs_calc_growdata_reservation(
419         struct xfs_mount        *mp)
420 {
421         return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
422                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
423                                  XFS_FSB_TO_B(mp, 1));
424 }
425
426 /*
427  * Growing the rt section of the filesystem.
428  * In the first set of transactions (ALLOC) we allocate space to the
429  * bitmap or summary files.
430  *      superblock: sector size
431  *      agf of the ag from which the extent is allocated: sector size
432  *      bmap btree for bitmap/summary inode: max depth * blocksize
433  *      bitmap/summary inode: inode size
434  *      allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
435  */
436 STATIC uint
437 xfs_calc_growrtalloc_reservation(
438         struct xfs_mount        *mp)
439 {
440         return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
441                 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
442                                  XFS_FSB_TO_B(mp, 1)) +
443                 xfs_calc_inode_res(mp, 1) +
444                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
445                                  XFS_FSB_TO_B(mp, 1));
446 }
447
448 /*
449  * Growing the rt section of the filesystem.
450  * In the second set of transactions (ZERO) we zero the new metadata blocks.
451  *      one bitmap/summary block: blocksize
452  */
453 STATIC uint
454 xfs_calc_growrtzero_reservation(
455         struct xfs_mount        *mp)
456 {
457         return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
458 }
459
460 /*
461  * Growing the rt section of the filesystem.
462  * In the third set of transactions (FREE) we update metadata without
463  * allocating any new blocks.
464  *      superblock: sector size
465  *      bitmap inode: inode size
466  *      summary inode: inode size
467  *      one bitmap block: blocksize
468  *      summary blocks: new summary size
469  */
470 STATIC uint
471 xfs_calc_growrtfree_reservation(
472         struct xfs_mount        *mp)
473 {
474         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
475                 xfs_calc_inode_res(mp, 2) +
476                 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
477                 xfs_calc_buf_res(1, mp->m_rsumsize);
478 }
479
480 /*
481  * Logging the inode modification timestamp on a synchronous write.
482  *      inode
483  */
484 STATIC uint
485 xfs_calc_swrite_reservation(
486         struct xfs_mount        *mp)
487 {
488         return xfs_calc_inode_res(mp, 1);
489 }
490
491 /*
492  * Logging the inode mode bits when writing a setuid/setgid file
493  *      inode
494  */
495 STATIC uint
496 xfs_calc_writeid_reservation(
497         struct xfs_mount        *mp)
498 {
499         return xfs_calc_inode_res(mp, 1);
500 }
501
502 /*
503  * Converting the inode from non-attributed to attributed.
504  *      the inode being converted: inode size
505  *      agf block and superblock (for block allocation)
506  *      the new block (directory sized)
507  *      bmap blocks for the new directory block
508  *      allocation btrees
509  */
510 STATIC uint
511 xfs_calc_addafork_reservation(
512         struct xfs_mount        *mp)
513 {
514         return XFS_DQUOT_LOGRES(mp) +
515                 xfs_calc_inode_res(mp, 1) +
516                 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
517                 xfs_calc_buf_res(1, mp->m_dirblksize) +
518                 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
519                                  XFS_FSB_TO_B(mp, 1)) +
520                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
521                                  XFS_FSB_TO_B(mp, 1));
522 }
523
524 /*
525  * Removing the attribute fork of a file
526  *    the inode being truncated: inode size
527  *    the inode's bmap btree: max depth * block size
528  * And the bmap_finish transaction can free the blocks and bmap blocks:
529  *    the agf for each of the ags: 4 * sector size
530  *    the agfl for each of the ags: 4 * sector size
531  *    the super block to reflect the freed blocks: sector size
532  *    worst case split in allocation btrees per extent assuming 4 extents:
533  *              4 exts * 2 trees * (2 * max depth - 1) * block size
534  */
535 STATIC uint
536 xfs_calc_attrinval_reservation(
537         struct xfs_mount        *mp)
538 {
539         return MAX((xfs_calc_inode_res(mp, 1) +
540                     xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
541                                      XFS_FSB_TO_B(mp, 1))),
542                    (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
543                     xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
544                                      XFS_FSB_TO_B(mp, 1))));
545 }
546
547 /*
548  * Setting an attribute at mount time.
549  *      the inode getting the attribute
550  *      the superblock for allocations
551  *      the agfs extents are allocated from
552  *      the attribute btree * max depth
553  *      the inode allocation btree
554  * Since attribute transaction space is dependent on the size of the attribute,
555  * the calculation is done partially at mount time and partially at runtime(see
556  * below).
557  */
558 STATIC uint
559 xfs_calc_attrsetm_reservation(
560         struct xfs_mount        *mp)
561 {
562         return XFS_DQUOT_LOGRES(mp) +
563                 xfs_calc_inode_res(mp, 1) +
564                 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
565                 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
566 }
567
568 /*
569  * Setting an attribute at runtime, transaction space unit per block.
570  *      the superblock for allocations: sector size
571  *      the inode bmap btree could join or split: max depth * block size
572  * Since the runtime attribute transaction space is dependent on the total
573  * blocks needed for the 1st bmap, here we calculate out the space unit for
574  * one block so that the caller could figure out the total space according
575  * to the attibute extent length in blocks by:
576  *      ext * M_RES(mp)->tr_attrsetrt.tr_logres
577  */
578 STATIC uint
579 xfs_calc_attrsetrt_reservation(
580         struct xfs_mount        *mp)
581 {
582         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
583                 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
584                                  XFS_FSB_TO_B(mp, 1));
585 }
586
587 /*
588  * Removing an attribute.
589  *    the inode: inode size
590  *    the attribute btree could join: max depth * block size
591  *    the inode bmap btree could join or split: max depth * block size
592  * And the bmap_finish transaction can free the attr blocks freed giving:
593  *    the agf for the ag in which the blocks live: 2 * sector size
594  *    the agfl for the ag in which the blocks live: 2 * sector size
595  *    the superblock for the free block count: sector size
596  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
597  */
598 STATIC uint
599 xfs_calc_attrrm_reservation(
600         struct xfs_mount        *mp)
601 {
602         return XFS_DQUOT_LOGRES(mp) +
603                 MAX((xfs_calc_inode_res(mp, 1) +
604                      xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
605                                       XFS_FSB_TO_B(mp, 1)) +
606                      (uint)XFS_FSB_TO_B(mp,
607                                         XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
608                      xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
609                     (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
610                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
611                                       XFS_FSB_TO_B(mp, 1))));
612 }
613
614 /*
615  * Clearing a bad agino number in an agi hash bucket.
616  */
617 STATIC uint
618 xfs_calc_clear_agi_bucket_reservation(
619         struct xfs_mount        *mp)
620 {
621         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
622 }
623
624 /*
625  * Clearing the quotaflags in the superblock.
626  *      the super block for changing quota flags: sector size
627  */
628 STATIC uint
629 xfs_calc_qm_sbchange_reservation(
630         struct xfs_mount        *mp)
631 {
632         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
633 }
634
635 /*
636  * Adjusting quota limits.
637  *    the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
638  */
639 STATIC uint
640 xfs_calc_qm_setqlim_reservation(
641         struct xfs_mount        *mp)
642 {
643         return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
644 }
645
646 /*
647  * Allocating quota on disk if needed.
648  *      the write transaction log space: M_RES(mp)->tr_write.tr_logres
649  *      the unit of quota allocation: one system block size
650  */
651 STATIC uint
652 xfs_calc_qm_dqalloc_reservation(
653         struct xfs_mount        *mp)
654 {
655         ASSERT(M_RES(mp)->tr_write.tr_logres);
656         return M_RES(mp)->tr_write.tr_logres +
657                 xfs_calc_buf_res(1,
658                         XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
659 }
660
661 /*
662  * Turning off quotas.
663  *    the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
664  *    the superblock for the quota flags: sector size
665  */
666 STATIC uint
667 xfs_calc_qm_quotaoff_reservation(
668         struct xfs_mount        *mp)
669 {
670         return sizeof(struct xfs_qoff_logitem) * 2 +
671                 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
672 }
673
674 /*
675  * End of turning off quotas.
676  *    the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
677  */
678 STATIC uint
679 xfs_calc_qm_quotaoff_end_reservation(
680         struct xfs_mount        *mp)
681 {
682         return sizeof(struct xfs_qoff_logitem) * 2;
683 }
684
685 /*
686  * Syncing the incore super block changes to disk.
687  *     the super block to reflect the changes: sector size
688  */
689 STATIC uint
690 xfs_calc_sb_reservation(
691         struct xfs_mount        *mp)
692 {
693         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
694 }
695
696 void
697 xfs_trans_resv_calc(
698         struct xfs_mount        *mp,
699         struct xfs_trans_resv   *resp)
700 {
701         /*
702          * The following transactions are logged in physical format and
703          * require a permanent reservation on space.
704          */
705         resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
706         resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
707         resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
708
709         resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
710         resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
711         resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
712
713         resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
714         resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
715         resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
716
717         resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
718         resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
719         resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
720
721         resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
722         resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
723         resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
724
725         resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
726         resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
727         resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
728
729         resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
730         resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
731         resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
732
733         resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
734         resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
735         resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
736
737         resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
738         resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
739         resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
740
741         resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
742         resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
743         resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
744
745         resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
746         resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
747         resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
748
749         resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
750         resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
751         resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
752
753         resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
754         resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
755         resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
756
757         resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
758         resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
759         resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
760
761         resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
762         resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
763         resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
764
765         /*
766          * The following transactions are logged in logical format with
767          * a default log count.
768          */
769         resp->tr_qm_sbchange.tr_logres = xfs_calc_qm_sbchange_reservation(mp);
770         resp->tr_qm_sbchange.tr_logcount = XFS_DEFAULT_LOG_COUNT;
771
772         resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
773         resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
774
775         resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
776         resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
777
778         resp->tr_qm_equotaoff.tr_logres =
779                 xfs_calc_qm_quotaoff_end_reservation(mp);
780         resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
781
782         resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
783         resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
784
785         /* The following transaction are logged in logical format */
786         resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
787         resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
788         resp->tr_swrite.tr_logres = xfs_calc_swrite_reservation(mp);
789         resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
790         resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
791         resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
792         resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
793         resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
794         resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
795 }