HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_trans_resv.c
blob68cb1e7bf2bb1d38e34398ab6dff35be5bd81c18
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * Copyright (C) 2010 Red Hat, Inc.
4 * All Rights Reserved.
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.
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.
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
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"
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.
44 STATIC uint
45 xfs_buf_log_overhead(void)
47 return round_up(sizeof(struct xlog_op_header) +
48 sizeof(struct xfs_buf_log_format), 128);
52 * Calculate out transaction log reservation per item in bytes.
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.
58 STATIC uint
59 xfs_calc_buf_res(
60 uint nbufs,
61 uint size)
63 return nbufs * (size + xfs_buf_log_overhead());
67 * Logging inodes is really tricksy. They are logged in memory format,
68 * which means that what we write into the log doesn't directly translate into
69 * the amount of space they use on disk.
71 * Case in point - btree format forks in memory format use more space than the
72 * on-disk format. In memory, the buffer contains a normal btree block header so
73 * the btree code can treat it as though it is just another generic buffer.
74 * However, when we write it to the inode fork, we don't write all of this
75 * header as it isn't needed. e.g. the root is only ever in the inode, so
76 * there's no need for sibling pointers which would waste 16 bytes of space.
78 * Hence when we have an inode with a maximally sized btree format fork, then
79 * amount of information we actually log is greater than the size of the inode
80 * on disk. Hence we need an inode reservation function that calculates all this
81 * correctly. So, we log:
83 * - 4 log op headers for object
84 * - for the ilf, the inode core and 2 forks
85 * - inode log format object
86 * - the inode core
87 * - two inode forks containing bmap btree root blocks.
88 * - the btree data contained by both forks will fit into the inode size,
89 * hence when combined with the inode core above, we have a total of the
90 * actual inode size.
91 * - the BMBT headers need to be accounted separately, as they are
92 * additional to the records and pointers that fit inside the inode
93 * forks.
95 STATIC uint
96 xfs_calc_inode_res(
97 struct xfs_mount *mp,
98 uint ninodes)
100 return ninodes *
101 (4 * sizeof(struct xlog_op_header) +
102 sizeof(struct xfs_inode_log_format) +
103 mp->m_sb.sb_inodesize +
104 2 * XFS_BMBT_BLOCK_LEN(mp));
108 * The free inode btree is a conditional feature and the log reservation
109 * requirements differ slightly from that of the traditional inode allocation
110 * btree. The finobt tracks records for inode chunks with at least one free
111 * inode. A record can be removed from the tree for an inode allocation
112 * or free and thus the finobt reservation is unconditional across:
114 * - inode allocation
115 * - inode free
116 * - inode chunk allocation
118 * The 'modify' param indicates to include the record modification scenario. The
119 * 'alloc' param indicates to include the reservation for free space btree
120 * modifications on behalf of finobt modifications. This is required only for
121 * transactions that do not already account for free space btree modifications.
123 * the free inode btree: max depth * block size
124 * the allocation btrees: 2 trees * (max depth - 1) * block size
125 * the free inode btree entry: block size
127 STATIC uint
128 xfs_calc_finobt_res(
129 struct xfs_mount *mp,
130 int alloc,
131 int modify)
133 uint res;
135 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
136 return 0;
138 res = xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1));
139 if (alloc)
140 res += xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
141 XFS_FSB_TO_B(mp, 1));
142 if (modify)
143 res += (uint)XFS_FSB_TO_B(mp, 1);
145 return res;
149 * Various log reservation values.
151 * These are based on the size of the file system block because that is what
152 * most transactions manipulate. Each adds in an additional 128 bytes per
153 * item logged to try to account for the overhead of the transaction mechanism.
155 * Note: Most of the reservations underestimate the number of allocation
156 * groups into which they could free extents in the xfs_bmap_finish() call.
157 * This is because the number in the worst case is quite high and quite
158 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
159 * extents in only a single AG at a time. This will require changes to the
160 * EFI code as well, however, so that the EFI for the extents not freed is
161 * logged again in each transaction. See SGI PV #261917.
163 * Reservation functions here avoid a huge stack in xfs_trans_init due to
164 * register overflow from temporaries in the calculations.
169 * In a write transaction we can allocate a maximum of 2
170 * extents. This gives:
171 * the inode getting the new extents: inode size
172 * the inode's bmap btree: max depth * block size
173 * the agfs of the ags from which the extents are allocated: 2 * sector
174 * the superblock free block counter: sector size
175 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
176 * And the bmap_finish transaction can free bmap blocks in a join:
177 * the agfs of the ags containing the blocks: 2 * sector size
178 * the agfls of the ags containing the blocks: 2 * sector size
179 * the super block free block counter: sector size
180 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
182 STATIC uint
183 xfs_calc_write_reservation(
184 struct xfs_mount *mp)
186 return XFS_DQUOT_LOGRES(mp) +
187 MAX((xfs_calc_inode_res(mp, 1) +
188 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
189 XFS_FSB_TO_B(mp, 1)) +
190 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
191 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
192 XFS_FSB_TO_B(mp, 1))),
193 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
194 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
195 XFS_FSB_TO_B(mp, 1))));
199 * In truncating a file we free up to two extents at once. We can modify:
200 * the inode being truncated: inode size
201 * the inode's bmap btree: (max depth + 1) * block size
202 * And the bmap_finish transaction can free the blocks and bmap blocks:
203 * the agf for each of the ags: 4 * sector size
204 * the agfl for each of the ags: 4 * sector size
205 * the super block to reflect the freed blocks: sector size
206 * worst case split in allocation btrees per extent assuming 4 extents:
207 * 4 exts * 2 trees * (2 * max depth - 1) * block size
208 * the inode btree: max depth * blocksize
209 * the allocation btrees: 2 trees * (max depth - 1) * block size
211 STATIC uint
212 xfs_calc_itruncate_reservation(
213 struct xfs_mount *mp)
215 return XFS_DQUOT_LOGRES(mp) +
216 MAX((xfs_calc_inode_res(mp, 1) +
217 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
218 XFS_FSB_TO_B(mp, 1))),
219 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
220 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
221 XFS_FSB_TO_B(mp, 1)) +
222 xfs_calc_buf_res(5, 0) +
223 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
224 XFS_FSB_TO_B(mp, 1)) +
225 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
226 mp->m_in_maxlevels, 0)));
230 * In renaming a files we can modify:
231 * the four inodes involved: 4 * inode size
232 * the two directory btrees: 2 * (max depth + v2) * dir block size
233 * the two directory bmap btrees: 2 * max depth * block size
234 * And the bmap_finish transaction can free dir and bmap blocks (two sets
235 * of bmap blocks) giving:
236 * the agf for the ags in which the blocks live: 3 * sector size
237 * the agfl for the ags in which the blocks live: 3 * sector size
238 * the superblock for the free block count: sector size
239 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
241 STATIC uint
242 xfs_calc_rename_reservation(
243 struct xfs_mount *mp)
245 return XFS_DQUOT_LOGRES(mp) +
246 MAX((xfs_calc_inode_res(mp, 4) +
247 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
248 XFS_FSB_TO_B(mp, 1))),
249 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
250 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
251 XFS_FSB_TO_B(mp, 1))));
255 * For removing an inode from unlinked list at first, we can modify:
256 * the agi hash list and counters: sector size
257 * the on disk inode before ours in the agi hash list: inode cluster size
259 STATIC uint
260 xfs_calc_iunlink_remove_reservation(
261 struct xfs_mount *mp)
263 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
264 max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
268 * For creating a link to an inode:
269 * the parent directory inode: inode size
270 * the linked inode: inode size
271 * the directory btree could split: (max depth + v2) * dir block size
272 * the directory bmap btree could join or split: (max depth + v2) * blocksize
273 * And the bmap_finish transaction can free some bmap blocks giving:
274 * the agf for the ag in which the blocks live: sector size
275 * the agfl for the ag in which the blocks live: sector size
276 * the superblock for the free block count: sector size
277 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
279 STATIC uint
280 xfs_calc_link_reservation(
281 struct xfs_mount *mp)
283 return XFS_DQUOT_LOGRES(mp) +
284 xfs_calc_iunlink_remove_reservation(mp) +
285 MAX((xfs_calc_inode_res(mp, 2) +
286 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
287 XFS_FSB_TO_B(mp, 1))),
288 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
289 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
290 XFS_FSB_TO_B(mp, 1))));
294 * For adding an inode to unlinked list we can modify:
295 * the agi hash list: sector size
296 * the unlinked inode: inode size
298 STATIC uint
299 xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
301 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
302 xfs_calc_inode_res(mp, 1);
306 * For removing a directory entry we can modify:
307 * the parent directory inode: inode size
308 * the removed inode: inode size
309 * the directory btree could join: (max depth + v2) * dir block size
310 * the directory bmap btree could join or split: (max depth + v2) * blocksize
311 * And the bmap_finish transaction can free the dir and bmap blocks giving:
312 * the agf for the ag in which the blocks live: 2 * sector size
313 * the agfl for the ag in which the blocks live: 2 * sector size
314 * the superblock for the free block count: sector size
315 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
317 STATIC uint
318 xfs_calc_remove_reservation(
319 struct xfs_mount *mp)
321 return XFS_DQUOT_LOGRES(mp) +
322 xfs_calc_iunlink_add_reservation(mp) +
323 MAX((xfs_calc_inode_res(mp, 1) +
324 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
325 XFS_FSB_TO_B(mp, 1))),
326 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
327 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
328 XFS_FSB_TO_B(mp, 1))));
332 * For create, break it in to the two cases that the transaction
333 * covers. We start with the modify case - allocation done by modification
334 * of the state of existing inodes - and the allocation case.
338 * For create we can modify:
339 * the parent directory inode: inode size
340 * the new inode: inode size
341 * the inode btree entry: block size
342 * the superblock for the nlink flag: sector size
343 * the directory btree: (max depth + v2) * dir block size
344 * the directory inode's bmap btree: (max depth + v2) * block size
345 * the finobt (record modification and allocation btrees)
347 STATIC uint
348 xfs_calc_create_resv_modify(
349 struct xfs_mount *mp)
351 return xfs_calc_inode_res(mp, 2) +
352 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
353 (uint)XFS_FSB_TO_B(mp, 1) +
354 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1)) +
355 xfs_calc_finobt_res(mp, 1, 1);
359 * For create we can allocate some inodes giving:
360 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
361 * the superblock for the nlink flag: sector size
362 * the inode blocks allocated: mp->m_ialloc_blks * blocksize
363 * the inode btree: max depth * blocksize
364 * the allocation btrees: 2 trees * (max depth - 1) * block size
366 STATIC uint
367 xfs_calc_create_resv_alloc(
368 struct xfs_mount *mp)
370 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
371 mp->m_sb.sb_sectsize +
372 xfs_calc_buf_res(mp->m_ialloc_blks, XFS_FSB_TO_B(mp, 1)) +
373 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
374 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
375 XFS_FSB_TO_B(mp, 1));
378 STATIC uint
379 __xfs_calc_create_reservation(
380 struct xfs_mount *mp)
382 return XFS_DQUOT_LOGRES(mp) +
383 MAX(xfs_calc_create_resv_alloc(mp),
384 xfs_calc_create_resv_modify(mp));
388 * For icreate we can allocate some inodes giving:
389 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
390 * the superblock for the nlink flag: sector size
391 * the inode btree: max depth * blocksize
392 * the allocation btrees: 2 trees * (max depth - 1) * block size
393 * the finobt (record insertion)
395 STATIC uint
396 xfs_calc_icreate_resv_alloc(
397 struct xfs_mount *mp)
399 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
400 mp->m_sb.sb_sectsize +
401 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
402 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
403 XFS_FSB_TO_B(mp, 1)) +
404 xfs_calc_finobt_res(mp, 0, 0);
407 STATIC uint
408 xfs_calc_icreate_reservation(xfs_mount_t *mp)
410 return XFS_DQUOT_LOGRES(mp) +
411 MAX(xfs_calc_icreate_resv_alloc(mp),
412 xfs_calc_create_resv_modify(mp));
415 STATIC uint
416 xfs_calc_create_reservation(
417 struct xfs_mount *mp)
419 if (xfs_sb_version_hascrc(&mp->m_sb))
420 return xfs_calc_icreate_reservation(mp);
421 return __xfs_calc_create_reservation(mp);
425 STATIC uint
426 xfs_calc_create_tmpfile_reservation(
427 struct xfs_mount *mp)
429 uint res = XFS_DQUOT_LOGRES(mp);
431 if (xfs_sb_version_hascrc(&mp->m_sb))
432 res += xfs_calc_icreate_resv_alloc(mp);
433 else
434 res += xfs_calc_create_resv_alloc(mp);
436 return res + xfs_calc_iunlink_add_reservation(mp);
440 * Making a new directory is the same as creating a new file.
442 STATIC uint
443 xfs_calc_mkdir_reservation(
444 struct xfs_mount *mp)
446 return xfs_calc_create_reservation(mp);
451 * Making a new symplink is the same as creating a new file, but
452 * with the added blocks for remote symlink data which can be up to 1kB in
453 * length (MAXPATHLEN).
455 STATIC uint
456 xfs_calc_symlink_reservation(
457 struct xfs_mount *mp)
459 return xfs_calc_create_reservation(mp) +
460 xfs_calc_buf_res(1, MAXPATHLEN);
464 * In freeing an inode we can modify:
465 * the inode being freed: inode size
466 * the super block free inode counter: sector size
467 * the agi hash list and counters: sector size
468 * the inode btree entry: block size
469 * the on disk inode before ours in the agi hash list: inode cluster size
470 * the inode btree: max depth * blocksize
471 * the allocation btrees: 2 trees * (max depth - 1) * block size
472 * the finobt (record insertion, removal or modification)
474 STATIC uint
475 xfs_calc_ifree_reservation(
476 struct xfs_mount *mp)
478 return XFS_DQUOT_LOGRES(mp) +
479 xfs_calc_inode_res(mp, 1) +
480 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
481 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
482 xfs_calc_iunlink_remove_reservation(mp) +
483 xfs_calc_buf_res(1, 0) +
484 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
485 mp->m_in_maxlevels, 0) +
486 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
487 XFS_FSB_TO_B(mp, 1)) +
488 xfs_calc_finobt_res(mp, 0, 1);
492 * When only changing the inode we log the inode and possibly the superblock
493 * We also add a bit of slop for the transaction stuff.
495 STATIC uint
496 xfs_calc_ichange_reservation(
497 struct xfs_mount *mp)
499 return XFS_DQUOT_LOGRES(mp) +
500 xfs_calc_inode_res(mp, 1) +
501 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
506 * Growing the data section of the filesystem.
507 * superblock
508 * agi and agf
509 * allocation btrees
511 STATIC uint
512 xfs_calc_growdata_reservation(
513 struct xfs_mount *mp)
515 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
516 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
517 XFS_FSB_TO_B(mp, 1));
521 * Growing the rt section of the filesystem.
522 * In the first set of transactions (ALLOC) we allocate space to the
523 * bitmap or summary files.
524 * superblock: sector size
525 * agf of the ag from which the extent is allocated: sector size
526 * bmap btree for bitmap/summary inode: max depth * blocksize
527 * bitmap/summary inode: inode size
528 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
530 STATIC uint
531 xfs_calc_growrtalloc_reservation(
532 struct xfs_mount *mp)
534 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
535 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
536 XFS_FSB_TO_B(mp, 1)) +
537 xfs_calc_inode_res(mp, 1) +
538 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
539 XFS_FSB_TO_B(mp, 1));
543 * Growing the rt section of the filesystem.
544 * In the second set of transactions (ZERO) we zero the new metadata blocks.
545 * one bitmap/summary block: blocksize
547 STATIC uint
548 xfs_calc_growrtzero_reservation(
549 struct xfs_mount *mp)
551 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
555 * Growing the rt section of the filesystem.
556 * In the third set of transactions (FREE) we update metadata without
557 * allocating any new blocks.
558 * superblock: sector size
559 * bitmap inode: inode size
560 * summary inode: inode size
561 * one bitmap block: blocksize
562 * summary blocks: new summary size
564 STATIC uint
565 xfs_calc_growrtfree_reservation(
566 struct xfs_mount *mp)
568 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
569 xfs_calc_inode_res(mp, 2) +
570 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
571 xfs_calc_buf_res(1, mp->m_rsumsize);
575 * Logging the inode modification timestamp on a synchronous write.
576 * inode
578 STATIC uint
579 xfs_calc_swrite_reservation(
580 struct xfs_mount *mp)
582 return xfs_calc_inode_res(mp, 1);
586 * Logging the inode mode bits when writing a setuid/setgid file
587 * inode
589 STATIC uint
590 xfs_calc_writeid_reservation(
591 struct xfs_mount *mp)
593 return xfs_calc_inode_res(mp, 1);
597 * Converting the inode from non-attributed to attributed.
598 * the inode being converted: inode size
599 * agf block and superblock (for block allocation)
600 * the new block (directory sized)
601 * bmap blocks for the new directory block
602 * allocation btrees
604 STATIC uint
605 xfs_calc_addafork_reservation(
606 struct xfs_mount *mp)
608 return XFS_DQUOT_LOGRES(mp) +
609 xfs_calc_inode_res(mp, 1) +
610 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
611 xfs_calc_buf_res(1, mp->m_dir_geo->blksize) +
612 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
613 XFS_FSB_TO_B(mp, 1)) +
614 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
615 XFS_FSB_TO_B(mp, 1));
619 * Removing the attribute fork of a file
620 * the inode being truncated: inode size
621 * the inode's bmap btree: max depth * block size
622 * And the bmap_finish transaction can free the blocks and bmap blocks:
623 * the agf for each of the ags: 4 * sector size
624 * the agfl for each of the ags: 4 * sector size
625 * the super block to reflect the freed blocks: sector size
626 * worst case split in allocation btrees per extent assuming 4 extents:
627 * 4 exts * 2 trees * (2 * max depth - 1) * block size
629 STATIC uint
630 xfs_calc_attrinval_reservation(
631 struct xfs_mount *mp)
633 return MAX((xfs_calc_inode_res(mp, 1) +
634 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
635 XFS_FSB_TO_B(mp, 1))),
636 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
637 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
638 XFS_FSB_TO_B(mp, 1))));
642 * Setting an attribute at mount time.
643 * the inode getting the attribute
644 * the superblock for allocations
645 * the agfs extents are allocated from
646 * the attribute btree * max depth
647 * the inode allocation btree
648 * Since attribute transaction space is dependent on the size of the attribute,
649 * the calculation is done partially at mount time and partially at runtime(see
650 * below).
652 STATIC uint
653 xfs_calc_attrsetm_reservation(
654 struct xfs_mount *mp)
656 return XFS_DQUOT_LOGRES(mp) +
657 xfs_calc_inode_res(mp, 1) +
658 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
659 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
663 * Setting an attribute at runtime, transaction space unit per block.
664 * the superblock for allocations: sector size
665 * the inode bmap btree could join or split: max depth * block size
666 * Since the runtime attribute transaction space is dependent on the total
667 * blocks needed for the 1st bmap, here we calculate out the space unit for
668 * one block so that the caller could figure out the total space according
669 * to the attibute extent length in blocks by:
670 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
672 STATIC uint
673 xfs_calc_attrsetrt_reservation(
674 struct xfs_mount *mp)
676 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
677 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
678 XFS_FSB_TO_B(mp, 1));
682 * Removing an attribute.
683 * the inode: inode size
684 * the attribute btree could join: max depth * block size
685 * the inode bmap btree could join or split: max depth * block size
686 * And the bmap_finish transaction can free the attr blocks freed giving:
687 * the agf for the ag in which the blocks live: 2 * sector size
688 * the agfl for the ag in which the blocks live: 2 * sector size
689 * the superblock for the free block count: sector size
690 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
692 STATIC uint
693 xfs_calc_attrrm_reservation(
694 struct xfs_mount *mp)
696 return XFS_DQUOT_LOGRES(mp) +
697 MAX((xfs_calc_inode_res(mp, 1) +
698 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
699 XFS_FSB_TO_B(mp, 1)) +
700 (uint)XFS_FSB_TO_B(mp,
701 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
702 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
703 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
704 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
705 XFS_FSB_TO_B(mp, 1))));
709 * Clearing a bad agino number in an agi hash bucket.
711 STATIC uint
712 xfs_calc_clear_agi_bucket_reservation(
713 struct xfs_mount *mp)
715 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
719 * Adjusting quota limits.
720 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
722 STATIC uint
723 xfs_calc_qm_setqlim_reservation(
724 struct xfs_mount *mp)
726 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
730 * Allocating quota on disk if needed.
731 * the write transaction log space for quota file extent allocation
732 * the unit of quota allocation: one system block size
734 STATIC uint
735 xfs_calc_qm_dqalloc_reservation(
736 struct xfs_mount *mp)
738 return xfs_calc_write_reservation(mp) +
739 xfs_calc_buf_res(1,
740 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
744 * Turning off quotas.
745 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
746 * the superblock for the quota flags: sector size
748 STATIC uint
749 xfs_calc_qm_quotaoff_reservation(
750 struct xfs_mount *mp)
752 return sizeof(struct xfs_qoff_logitem) * 2 +
753 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
757 * End of turning off quotas.
758 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
760 STATIC uint
761 xfs_calc_qm_quotaoff_end_reservation(
762 struct xfs_mount *mp)
764 return sizeof(struct xfs_qoff_logitem) * 2;
768 * Syncing the incore super block changes to disk.
769 * the super block to reflect the changes: sector size
771 STATIC uint
772 xfs_calc_sb_reservation(
773 struct xfs_mount *mp)
775 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
778 void
779 xfs_trans_resv_calc(
780 struct xfs_mount *mp,
781 struct xfs_trans_resv *resp)
784 * The following transactions are logged in physical format and
785 * require a permanent reservation on space.
787 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
788 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
789 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
791 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
792 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
793 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
795 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
796 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
797 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
799 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
800 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
801 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
803 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
804 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
805 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
807 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
808 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
809 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
811 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
812 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
813 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
815 resp->tr_create_tmpfile.tr_logres =
816 xfs_calc_create_tmpfile_reservation(mp);
817 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
818 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
820 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
821 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
822 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
824 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
825 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
826 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
828 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
829 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
830 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
832 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
833 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
834 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
836 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
837 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
838 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
840 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
841 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
842 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
844 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
845 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
846 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
848 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
849 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
850 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
853 * The following transactions are logged in logical format with
854 * a default log count.
856 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
857 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
859 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
860 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
862 resp->tr_qm_equotaoff.tr_logres =
863 xfs_calc_qm_quotaoff_end_reservation(mp);
864 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
866 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
867 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
869 /* The following transaction are logged in logical format */
870 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
871 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
872 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
873 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
874 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
875 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
876 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
877 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);