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