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