]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_trans_resv.c
xfs: queue deferred rmap ops for cow staging extent alloc/free in the right order
[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: sector size
493 * the agi hash list and counters: sector size
494 * the inode btree entry: block size
495 * the on disk inode before ours in the agi hash list: inode cluster size
496 * the inode btree: max depth * blocksize
497 * the allocation btrees: 2 trees * (max depth - 1) * block size
498 * the finobt (record insertion, removal or modification)
499 */
500 STATIC uint
501 xfs_calc_ifree_reservation(
502 struct xfs_mount *mp)
503 {
504 return XFS_DQUOT_LOGRES(mp) +
505 xfs_calc_inode_res(mp, 1) +
506 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
507 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
508 xfs_calc_iunlink_remove_reservation(mp) +
509 xfs_calc_buf_res(1, 0) +
510 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
511 mp->m_in_maxlevels, 0) +
512 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
513 XFS_FSB_TO_B(mp, 1)) +
514 xfs_calc_finobt_res(mp, 0, 1);
515 }
516
517 /*
518 * When only changing the inode we log the inode and possibly the superblock
519 * We also add a bit of slop for the transaction stuff.
520 */
521 STATIC uint
522 xfs_calc_ichange_reservation(
523 struct xfs_mount *mp)
524 {
525 return XFS_DQUOT_LOGRES(mp) +
526 xfs_calc_inode_res(mp, 1) +
527 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
528
529 }
530
531 /*
532 * Growing the data section of the filesystem.
533 * superblock
534 * agi and agf
535 * allocation btrees
536 */
537 STATIC uint
538 xfs_calc_growdata_reservation(
539 struct xfs_mount *mp)
540 {
541 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
542 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
543 XFS_FSB_TO_B(mp, 1));
544 }
545
546 /*
547 * Growing the rt section of the filesystem.
548 * In the first set of transactions (ALLOC) we allocate space to the
549 * bitmap or summary files.
550 * superblock: sector size
551 * agf of the ag from which the extent is allocated: sector size
552 * bmap btree for bitmap/summary inode: max depth * blocksize
553 * bitmap/summary inode: inode size
554 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
555 */
556 STATIC uint
557 xfs_calc_growrtalloc_reservation(
558 struct xfs_mount *mp)
559 {
560 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
561 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
562 XFS_FSB_TO_B(mp, 1)) +
563 xfs_calc_inode_res(mp, 1) +
564 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
565 XFS_FSB_TO_B(mp, 1));
566 }
567
568 /*
569 * Growing the rt section of the filesystem.
570 * In the second set of transactions (ZERO) we zero the new metadata blocks.
571 * one bitmap/summary block: blocksize
572 */
573 STATIC uint
574 xfs_calc_growrtzero_reservation(
575 struct xfs_mount *mp)
576 {
577 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
578 }
579
580 /*
581 * Growing the rt section of the filesystem.
582 * In the third set of transactions (FREE) we update metadata without
583 * allocating any new blocks.
584 * superblock: sector size
585 * bitmap inode: inode size
586 * summary inode: inode size
587 * one bitmap block: blocksize
588 * summary blocks: new summary size
589 */
590 STATIC uint
591 xfs_calc_growrtfree_reservation(
592 struct xfs_mount *mp)
593 {
594 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
595 xfs_calc_inode_res(mp, 2) +
596 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
597 xfs_calc_buf_res(1, mp->m_rsumsize);
598 }
599
600 /*
601 * Logging the inode modification timestamp on a synchronous write.
602 * inode
603 */
604 STATIC uint
605 xfs_calc_swrite_reservation(
606 struct xfs_mount *mp)
607 {
608 return xfs_calc_inode_res(mp, 1);
609 }
610
611 /*
612 * Logging the inode mode bits when writing a setuid/setgid file
613 * inode
614 */
615 STATIC uint
616 xfs_calc_writeid_reservation(
617 struct xfs_mount *mp)
618 {
619 return xfs_calc_inode_res(mp, 1);
620 }
621
622 /*
623 * Converting the inode from non-attributed to attributed.
624 * the inode being converted: inode size
625 * agf block and superblock (for block allocation)
626 * the new block (directory sized)
627 * bmap blocks for the new directory block
628 * allocation btrees
629 */
630 STATIC uint
631 xfs_calc_addafork_reservation(
632 struct xfs_mount *mp)
633 {
634 return XFS_DQUOT_LOGRES(mp) +
635 xfs_calc_inode_res(mp, 1) +
636 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
637 xfs_calc_buf_res(1, mp->m_dir_geo->blksize) +
638 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
639 XFS_FSB_TO_B(mp, 1)) +
640 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 1),
641 XFS_FSB_TO_B(mp, 1));
642 }
643
644 /*
645 * Removing the attribute fork of a file
646 * the inode being truncated: inode size
647 * the inode's bmap btree: max depth * block size
648 * And the bmap_finish transaction can free the blocks and bmap blocks:
649 * the agf for each of the ags: 4 * sector size
650 * the agfl for each of the ags: 4 * sector size
651 * the super block to reflect the freed blocks: sector size
652 * worst case split in allocation btrees per extent assuming 4 extents:
653 * 4 exts * 2 trees * (2 * max depth - 1) * block size
654 */
655 STATIC uint
656 xfs_calc_attrinval_reservation(
657 struct xfs_mount *mp)
658 {
659 return MAX((xfs_calc_inode_res(mp, 1) +
660 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
661 XFS_FSB_TO_B(mp, 1))),
662 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
663 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 4),
664 XFS_FSB_TO_B(mp, 1))));
665 }
666
667 /*
668 * Setting an attribute at mount time.
669 * the inode getting the attribute
670 * the superblock for allocations
671 * the agfs extents are allocated from
672 * the attribute btree * max depth
673 * the inode allocation btree
674 * Since attribute transaction space is dependent on the size of the attribute,
675 * the calculation is done partially at mount time and partially at runtime(see
676 * below).
677 */
678 STATIC uint
679 xfs_calc_attrsetm_reservation(
680 struct xfs_mount *mp)
681 {
682 return XFS_DQUOT_LOGRES(mp) +
683 xfs_calc_inode_res(mp, 1) +
684 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
685 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
686 }
687
688 /*
689 * Setting an attribute at runtime, transaction space unit per block.
690 * the superblock for allocations: sector size
691 * the inode bmap btree could join or split: max depth * block size
692 * Since the runtime attribute transaction space is dependent on the total
693 * blocks needed for the 1st bmap, here we calculate out the space unit for
694 * one block so that the caller could figure out the total space according
695 * to the attibute extent length in blocks by:
696 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
697 */
698 STATIC uint
699 xfs_calc_attrsetrt_reservation(
700 struct xfs_mount *mp)
701 {
702 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
703 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
704 XFS_FSB_TO_B(mp, 1));
705 }
706
707 /*
708 * Removing an attribute.
709 * the inode: inode size
710 * the attribute btree could join: max depth * block size
711 * the inode bmap btree could join or split: max depth * block size
712 * And the bmap_finish transaction can free the attr blocks freed giving:
713 * the agf for the ag in which the blocks live: 2 * sector size
714 * the agfl for the ag in which the blocks live: 2 * sector size
715 * the superblock for the free block count: sector size
716 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
717 */
718 STATIC uint
719 xfs_calc_attrrm_reservation(
720 struct xfs_mount *mp)
721 {
722 return XFS_DQUOT_LOGRES(mp) +
723 MAX((xfs_calc_inode_res(mp, 1) +
724 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
725 XFS_FSB_TO_B(mp, 1)) +
726 (uint)XFS_FSB_TO_B(mp,
727 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
728 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
729 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
730 xfs_calc_buf_res(xfs_allocfree_log_count(mp, 2),
731 XFS_FSB_TO_B(mp, 1))));
732 }
733
734 /*
735 * Clearing a bad agino number in an agi hash bucket.
736 */
737 STATIC uint
738 xfs_calc_clear_agi_bucket_reservation(
739 struct xfs_mount *mp)
740 {
741 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
742 }
743
744 /*
745 * Adjusting quota limits.
746 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
747 */
748 STATIC uint
749 xfs_calc_qm_setqlim_reservation(
750 struct xfs_mount *mp)
751 {
752 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
753 }
754
755 /*
756 * Allocating quota on disk if needed.
757 * the write transaction log space for quota file extent allocation
758 * the unit of quota allocation: one system block size
759 */
760 STATIC uint
761 xfs_calc_qm_dqalloc_reservation(
762 struct xfs_mount *mp)
763 {
764 return xfs_calc_write_reservation(mp) +
765 xfs_calc_buf_res(1,
766 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
767 }
768
769 /*
770 * Turning off quotas.
771 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
772 * the superblock for the quota flags: sector size
773 */
774 STATIC uint
775 xfs_calc_qm_quotaoff_reservation(
776 struct xfs_mount *mp)
777 {
778 return sizeof(struct xfs_qoff_logitem) * 2 +
779 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
780 }
781
782 /*
783 * End of turning off quotas.
784 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
785 */
786 STATIC uint
787 xfs_calc_qm_quotaoff_end_reservation(
788 struct xfs_mount *mp)
789 {
790 return sizeof(struct xfs_qoff_logitem) * 2;
791 }
792
793 /*
794 * Syncing the incore super block changes to disk.
795 * the super block to reflect the changes: sector size
796 */
797 STATIC uint
798 xfs_calc_sb_reservation(
799 struct xfs_mount *mp)
800 {
801 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
802 }
803
804 void
805 xfs_trans_resv_calc(
806 struct xfs_mount *mp,
807 struct xfs_trans_resv *resp)
808 {
809 /*
810 * The following transactions are logged in physical format and
811 * require a permanent reservation on space.
812 */
813 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
814 if (xfs_sb_version_hasreflink(&mp->m_sb))
815 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
816 else
817 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
818 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
819
820 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
821 if (xfs_sb_version_hasreflink(&mp->m_sb))
822 resp->tr_itruncate.tr_logcount =
823 XFS_ITRUNCATE_LOG_COUNT_REFLINK;
824 else
825 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
826 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
827
828 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
829 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
830 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
831
832 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
833 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
834 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
835
836 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
837 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
838 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
839
840 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
841 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
842 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
843
844 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
845 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
846 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
847
848 resp->tr_create_tmpfile.tr_logres =
849 xfs_calc_create_tmpfile_reservation(mp);
850 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
851 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
852
853 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
854 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
855 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
856
857 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
858 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
859 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
860
861 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
862 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
863 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
864
865 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
866 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
867 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
868
869 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
870 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
871 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
872
873 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
874 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
875 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
876
877 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
878 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
879 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
880
881 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
882 if (xfs_sb_version_hasreflink(&mp->m_sb))
883 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT_REFLINK;
884 else
885 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
886 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
887
888 /*
889 * The following transactions are logged in logical format with
890 * a default log count.
891 */
892 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
893 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
894
895 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
896 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
897
898 resp->tr_qm_equotaoff.tr_logres =
899 xfs_calc_qm_quotaoff_end_reservation(mp);
900 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
901
902 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
903 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
904
905 /* The following transaction are logged in logical format */
906 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
907 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
908 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
909 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
910 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
911 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
912 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
913 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
914 }