]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_trans_resv.c
xfs: include inobt buffers in ifree tx log reservation
[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). With reflink, there are four trees (refcountbt). The number of
70 * blocks reserved is based on the formula:
71 *
72 * num trees * ((2 blocks/level * max depth) - 1)
73 *
74 * Keep in mind that max depth is calculated separately for each type of tree.
75 */
76 uint
77 xfs_allocfree_log_count(
78 struct xfs_mount *mp,
79 uint num_ops)
80 {
81 uint blocks;
82
83 blocks = num_ops * 2 * (2 * mp->m_ag_maxlevels - 1);
84 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
85 blocks += num_ops * (2 * mp->m_rmap_maxlevels - 1);
86 if (xfs_sb_version_hasreflink(&mp->m_sb))
87 blocks += num_ops * (2 * mp->m_refc_maxlevels - 1);
88
89 return blocks;
90 }
91
92 /*
93 * Logging inodes is really tricksy. They are logged in memory format,
94 * which means that what we write into the log doesn't directly translate into
95 * the amount of space they use on disk.
96 *
97 * Case in point - btree format forks in memory format use more space than the
98 * on-disk format. In memory, the buffer contains a normal btree block header so
99 * the btree code can treat it as though it is just another generic buffer.
100 * However, when we write it to the inode fork, we don't write all of this
101 * header as it isn't needed. e.g. the root is only ever in the inode, so
102 * there's no need for sibling pointers which would waste 16 bytes of space.
103 *
104 * Hence when we have an inode with a maximally sized btree format fork, then
105 * amount of information we actually log is greater than the size of the inode
106 * on disk. Hence we need an inode reservation function that calculates all this
107 * correctly. So, we log:
108 *
109 * - 4 log op headers for object
110 * - for the ilf, the inode core and 2 forks
111 * - inode log format object
112 * - the inode core
113 * - two inode forks containing bmap btree root blocks.
114 * - the btree data contained by both forks will fit into the inode size,
115 * hence when combined with the inode core above, we have a total of the
116 * actual inode size.
117 * - the BMBT headers need to be accounted separately, as they are
118 * additional to the records and pointers that fit inside the inode
119 * forks.
120 */
121 STATIC uint
122 xfs_calc_inode_res(
123 struct xfs_mount *mp,
124 uint ninodes)
125 {
126 return ninodes *
127 (4 * sizeof(struct xlog_op_header) +
128 sizeof(struct xfs_inode_log_format) +
129 mp->m_sb.sb_inodesize +
130 2 * XFS_BMBT_BLOCK_LEN(mp));
131 }
132
133 /*
134 * The free inode btree is a conditional feature and the log reservation
135 * requirements differ slightly from that of the traditional inode allocation
136 * btree. The finobt tracks records for inode chunks with at least one free
137 * inode. A record can be removed from the tree for an inode allocation
138 * or free and thus the finobt reservation is unconditional across:
139 *
140 * - inode allocation
141 * - inode free
142 * - inode chunk allocation
143 *
144 * The 'modify' param indicates to include the record modification scenario. The
145 * 'alloc' param indicates to include the reservation for free space btree
146 * modifications on behalf of finobt modifications. This is required only for
147 * transactions that do not already account for free space btree modifications.
148 *
149 * the free inode btree: max depth * block size
150 * the allocation btrees: 2 trees * (max depth - 1) * block size
151 * the free inode btree entry: block size
152 */
153 STATIC uint
154 xfs_calc_finobt_res(
155 struct xfs_mount *mp,
156 int alloc,
157 int modify)
158 {
159 uint res;
160
161 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
162 return 0;
163
164 res = xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1));
165 if (alloc)
166 res += xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
167 XFS_FSB_TO_B(mp, 1));
168 if (modify)
169 res += (uint)XFS_FSB_TO_B(mp, 1);
170
171 return res;
172 }
173
174 /*
175 * Various log reservation values.
176 *
177 * These are based on the size of the file system block because that is what
178 * most transactions manipulate. Each adds in an additional 128 bytes per
179 * item logged to try to account for the overhead of the transaction mechanism.
180 *
181 * Note: Most of the reservations underestimate the number of allocation
182 * groups into which they could free extents in the xfs_defer_finish() call.
183 * This is because the number in the worst case is quite high and quite
184 * unusual. In order to fix this we need to change xfs_defer_finish() to free
185 * extents in only a single AG at a time. This will require changes to the
186 * EFI code as well, however, so that the EFI for the extents not freed is
187 * logged again in each transaction. See SGI PV #261917.
188 *
189 * Reservation functions here avoid a huge stack in xfs_trans_init due to
190 * register overflow from temporaries in the calculations.
191 */
192
193
194 /*
195 * In a write transaction we can allocate a maximum of 2
196 * extents. This gives:
197 * the inode getting the new extents: inode size
198 * the inode's bmap btree: max depth * block size
199 * the agfs of the ags from which the extents are allocated: 2 * sector
200 * the superblock free block counter: sector size
201 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
202 * And the bmap_finish transaction can free bmap blocks in a join:
203 * the agfs of the ags containing the blocks: 2 * sector size
204 * the agfls of the ags containing the blocks: 2 * sector size
205 * the super block free block counter: sector size
206 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
207 */
208 STATIC uint
209 xfs_calc_write_reservation(
210 struct xfs_mount *mp)
211 {
212 return XFS_DQUOT_LOGRES(mp) +
213 MAX((xfs_calc_inode_res(mp, 1) +
214 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
215 XFS_FSB_TO_B(mp, 1)) +
216 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
217 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
218 XFS_FSB_TO_B(mp, 1))),
219 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
220 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
221 XFS_FSB_TO_B(mp, 1))));
222 }
223
224 /*
225 * In truncating a file we free up to two extents at once. We can modify:
226 * the inode being truncated: inode size
227 * the inode's bmap btree: (max depth + 1) * block size
228 * And the bmap_finish transaction can free the blocks and bmap blocks:
229 * the agf for each of the ags: 4 * sector size
230 * the agfl for each of the ags: 4 * sector size
231 * the super block to reflect the freed blocks: sector size
232 * worst case split in allocation btrees per extent assuming 4 extents:
233 * 4 exts * 2 trees * (2 * max depth - 1) * block size
234 * the inode btree: max depth * blocksize
235 * the allocation btrees: 2 trees * (max depth - 1) * block size
236 */
237 STATIC uint
238 xfs_calc_itruncate_reservation(
239 struct xfs_mount *mp)
240 {
241 return XFS_DQUOT_LOGRES(mp) +
242 MAX((xfs_calc_inode_res(mp, 1) +
243 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
244 XFS_FSB_TO_B(mp, 1))),
245 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
246 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4),
247 XFS_FSB_TO_B(mp, 1)) +
248 xfs_calc_buf_res(5, 0) +
249 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
250 XFS_FSB_TO_B(mp, 1)) +
251 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
252 mp->m_in_maxlevels, 0)));
253 }
254
255 /*
256 * In renaming a files we can modify:
257 * the four inodes involved: 4 * inode size
258 * the two directory btrees: 2 * (max depth + v2) * dir block size
259 * the two directory bmap btrees: 2 * max depth * block size
260 * And the bmap_finish transaction can free dir and bmap blocks (two sets
261 * of bmap blocks) giving:
262 * the agf for the ags in which the blocks live: 3 * sector size
263 * the agfl for the ags in which the blocks live: 3 * sector size
264 * the superblock for the free block count: sector size
265 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
266 */
267 STATIC uint
268 xfs_calc_rename_reservation(
269 struct xfs_mount *mp)
270 {
271 return XFS_DQUOT_LOGRES(mp) +
272 MAX((xfs_calc_inode_res(mp, 4) +
273 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
274 XFS_FSB_TO_B(mp, 1))),
275 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
276 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 3),
277 XFS_FSB_TO_B(mp, 1))));
278 }
279
280 /*
281 * For removing an inode from unlinked list at first, we can modify:
282 * the agi hash list and counters: sector size
283 * the on disk inode before ours in the agi hash list: inode cluster size
284 */
285 STATIC uint
286 xfs_calc_iunlink_remove_reservation(
287 struct xfs_mount *mp)
288 {
289 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
290 max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
291 }
292
293 /*
294 * For creating a link to an inode:
295 * the parent directory inode: inode size
296 * the linked inode: inode size
297 * the directory btree could split: (max depth + v2) * dir block size
298 * the directory bmap btree could join or split: (max depth + v2) * blocksize
299 * And the bmap_finish transaction can free some bmap blocks giving:
300 * the agf for the ag in which the blocks live: sector size
301 * the agfl for the ag in which the blocks live: sector size
302 * the superblock for the free block count: sector size
303 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
304 */
305 STATIC uint
306 xfs_calc_link_reservation(
307 struct xfs_mount *mp)
308 {
309 return XFS_DQUOT_LOGRES(mp) +
310 xfs_calc_iunlink_remove_reservation(mp) +
311 MAX((xfs_calc_inode_res(mp, 2) +
312 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
313 XFS_FSB_TO_B(mp, 1))),
314 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
315 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
316 XFS_FSB_TO_B(mp, 1))));
317 }
318
319 /*
320 * For adding an inode to unlinked list we can modify:
321 * the agi hash list: sector size
322 * the unlinked inode: inode size
323 */
324 STATIC uint
325 xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
326 {
327 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
328 xfs_calc_inode_res(mp, 1);
329 }
330
331 /*
332 * For removing a directory entry we can modify:
333 * the parent directory inode: inode size
334 * the removed inode: inode size
335 * the directory btree could join: (max depth + v2) * dir block size
336 * the directory bmap btree could join or split: (max depth + v2) * blocksize
337 * And the bmap_finish transaction can free the dir and bmap blocks giving:
338 * the agf for the ag in which the blocks live: 2 * sector size
339 * the agfl for the ag in which the blocks live: 2 * sector size
340 * the superblock for the free block count: sector size
341 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
342 */
343 STATIC uint
344 xfs_calc_remove_reservation(
345 struct xfs_mount *mp)
346 {
347 return XFS_DQUOT_LOGRES(mp) +
348 xfs_calc_iunlink_add_reservation(mp) +
349 MAX((xfs_calc_inode_res(mp, 1) +
350 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
351 XFS_FSB_TO_B(mp, 1))),
352 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
353 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
354 XFS_FSB_TO_B(mp, 1))));
355 }
356
357 /*
358 * For create, break it in to the two cases that the transaction
359 * covers. We start with the modify case - allocation done by modification
360 * of the state of existing inodes - and the allocation case.
361 */
362
363 /*
364 * For create we can modify:
365 * the parent directory inode: inode size
366 * the new inode: inode size
367 * the inode btree entry: block size
368 * the superblock for the nlink flag: sector size
369 * the directory btree: (max depth + v2) * dir block size
370 * the directory inode's bmap btree: (max depth + v2) * block size
371 * the finobt (record modification and allocation btrees)
372 */
373 STATIC uint
374 xfs_calc_create_resv_modify(
375 struct xfs_mount *mp)
376 {
377 return xfs_calc_inode_res(mp, 2) +
378 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
379 (uint)XFS_FSB_TO_B(mp, 1) +
380 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1)) +
381 xfs_calc_finobt_res(mp, 1, 1);
382 }
383
384 /*
385 * For create we can allocate some inodes giving:
386 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
387 * the superblock for the nlink flag: sector size
388 * the inode blocks allocated: mp->m_ialloc_blks * blocksize
389 * the inode btree: max depth * blocksize
390 * the allocation btrees: 2 trees * (max depth - 1) * block size
391 */
392 STATIC uint
393 xfs_calc_create_resv_alloc(
394 struct xfs_mount *mp)
395 {
396 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
397 mp->m_sb.sb_sectsize +
398 xfs_calc_buf_res(mp->m_ialloc_blks, XFS_FSB_TO_B(mp, 1)) +
399 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
400 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
401 XFS_FSB_TO_B(mp, 1));
402 }
403
404 STATIC uint
405 __xfs_calc_create_reservation(
406 struct xfs_mount *mp)
407 {
408 return XFS_DQUOT_LOGRES(mp) +
409 MAX(xfs_calc_create_resv_alloc(mp),
410 xfs_calc_create_resv_modify(mp));
411 }
412
413 /*
414 * For icreate we can allocate some inodes giving:
415 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
416 * the superblock for the nlink flag: sector size
417 * the inode btree: max depth * blocksize
418 * the allocation btrees: 2 trees * (max depth - 1) * block size
419 * the finobt (record insertion)
420 */
421 STATIC uint
422 xfs_calc_icreate_resv_alloc(
423 struct xfs_mount *mp)
424 {
425 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
426 mp->m_sb.sb_sectsize +
427 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
428 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
429 XFS_FSB_TO_B(mp, 1)) +
430 xfs_calc_finobt_res(mp, 0, 0);
431 }
432
433 STATIC uint
434 xfs_calc_icreate_reservation(xfs_mount_t *mp)
435 {
436 return XFS_DQUOT_LOGRES(mp) +
437 MAX(xfs_calc_icreate_resv_alloc(mp),
438 xfs_calc_create_resv_modify(mp));
439 }
440
441 STATIC uint
442 xfs_calc_create_reservation(
443 struct xfs_mount *mp)
444 {
445 if (xfs_sb_version_hascrc(&mp->m_sb))
446 return xfs_calc_icreate_reservation(mp);
447 return __xfs_calc_create_reservation(mp);
448
449 }
450
451 STATIC uint
452 xfs_calc_create_tmpfile_reservation(
453 struct xfs_mount *mp)
454 {
455 uint res = XFS_DQUOT_LOGRES(mp);
456
457 if (xfs_sb_version_hascrc(&mp->m_sb))
458 res += xfs_calc_icreate_resv_alloc(mp);
459 else
460 res += xfs_calc_create_resv_alloc(mp);
461
462 return res + xfs_calc_iunlink_add_reservation(mp);
463 }
464
465 /*
466 * Making a new directory is the same as creating a new file.
467 */
468 STATIC uint
469 xfs_calc_mkdir_reservation(
470 struct xfs_mount *mp)
471 {
472 return xfs_calc_create_reservation(mp);
473 }
474
475
476 /*
477 * Making a new symplink is the same as creating a new file, but
478 * with the added blocks for remote symlink data which can be up to 1kB in
479 * length (XFS_SYMLINK_MAXLEN).
480 */
481 STATIC uint
482 xfs_calc_symlink_reservation(
483 struct xfs_mount *mp)
484 {
485 return xfs_calc_create_reservation(mp) +
486 xfs_calc_buf_res(1, XFS_SYMLINK_MAXLEN);
487 }
488
489 /*
490 * In freeing an inode we can modify:
491 * the inode being freed: inode size
492 * the super block free inode counter, AGF and AGFL: sector size
493 * the on disk inode (agi unlinked list removal)
494 * the inode chunk is marked stale (headers only)
495 * the inode btree: max depth * blocksize
496 * the allocation btrees: 2 trees * (max depth - 1) * block size
497 * the finobt (record insertion, removal or modification)
498 */
499 STATIC uint
500 xfs_calc_ifree_reservation(
501 struct xfs_mount *mp)
502 {
503 return XFS_DQUOT_LOGRES(mp) +
504 xfs_calc_inode_res(mp, 1) +
505 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
506 xfs_calc_iunlink_remove_reservation(mp) +
507 xfs_calc_buf_res(mp->m_ialloc_blks, 0) +
508 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
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 if (xfs_sb_version_hasreflink(&mp->m_sb))
812 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
813 else
814 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
815 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
816
817 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
818 if (xfs_sb_version_hasreflink(&mp->m_sb))
819 resp->tr_itruncate.tr_logcount =
820 XFS_ITRUNCATE_LOG_COUNT_REFLINK;
821 else
822 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
823 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
824
825 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
826 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
827 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
828
829 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
830 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
831 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
832
833 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
834 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
835 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
836
837 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
838 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
839 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
840
841 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
842 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
843 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
844
845 resp->tr_create_tmpfile.tr_logres =
846 xfs_calc_create_tmpfile_reservation(mp);
847 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
848 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
849
850 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
851 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
852 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
853
854 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
855 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
856 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
857
858 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
859 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
860 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
861
862 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
863 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
864 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
865
866 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
867 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
868 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
869
870 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
871 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
872 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
873
874 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
875 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
876 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
877
878 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
879 if (xfs_sb_version_hasreflink(&mp->m_sb))
880 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
881 else
882 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
883 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
884
885 /*
886 * The following transactions are logged in logical format with
887 * a default log count.
888 */
889 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
890 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
891
892 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
893 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
894
895 resp->tr_qm_equotaoff.tr_logres =
896 xfs_calc_qm_quotaoff_end_reservation(mp);
897 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
898
899 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
900 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
901
902 /* The following transaction are logged in logical format */
903 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
904 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
905 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
906 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
907 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
908 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
909 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
910 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
911 }