blob: ae368165244d49458bd4db2cc4688d196f2e2098 [file] [log] [blame]
Dave Chinner7fd36c42013-08-12 20:49:32 +10001/*
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"
Dave Chinner70a9883c2013-10-23 10:36:05 +110021#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_format.h"
23#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100024#include "xfs_trans_resv.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100025#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100029#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110030#include "xfs_bmap_btree.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100031#include "xfs_ialloc.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100032#include "xfs_quota.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100034#include "xfs_qm.h"
35#include "xfs_trans_space.h"
36#include "xfs_trace.h"
37
38/*
39 * A buffer has a format structure overhead in the log in addition
40 * to the data, so we need to take this into account when reserving
41 * space in a transaction for a buffer. Round the space required up
42 * to a multiple of 128 bytes so that we don't change the historical
43 * reservation that has been used for this overhead.
44 */
45STATIC uint
46xfs_buf_log_overhead(void)
47{
48 return round_up(sizeof(struct xlog_op_header) +
49 sizeof(struct xfs_buf_log_format), 128);
50}
51
52/*
53 * Calculate out transaction log reservation per item in bytes.
54 *
55 * The nbufs argument is used to indicate the number of items that
56 * will be changed in a transaction. size is used to tell how many
57 * bytes should be reserved per item.
58 */
59STATIC uint
60xfs_calc_buf_res(
61 uint nbufs,
62 uint size)
63{
64 return nbufs * (size + xfs_buf_log_overhead());
65}
66
67/*
Dave Chinner23956702013-08-28 16:10:35 +100068 * Logging inodes is really tricksy. They are logged in memory format,
69 * which means that what we write into the log doesn't directly translate into
70 * the amount of space they use on disk.
71 *
72 * Case in point - btree format forks in memory format use more space than the
73 * on-disk format. In memory, the buffer contains a normal btree block header so
74 * the btree code can treat it as though it is just another generic buffer.
75 * However, when we write it to the inode fork, we don't write all of this
76 * header as it isn't needed. e.g. the root is only ever in the inode, so
77 * there's no need for sibling pointers which would waste 16 bytes of space.
78 *
79 * Hence when we have an inode with a maximally sized btree format fork, then
80 * amount of information we actually log is greater than the size of the inode
81 * on disk. Hence we need an inode reservation function that calculates all this
82 * correctly. So, we log:
83 *
Dave Chinnerfe4c2242014-03-07 16:19:14 +110084 * - 4 log op headers for object
85 * - for the ilf, the inode core and 2 forks
Dave Chinner23956702013-08-28 16:10:35 +100086 * - inode log format object
Dave Chinnerfe4c2242014-03-07 16:19:14 +110087 * - the inode core
88 * - two inode forks containing bmap btree root blocks.
89 * - the btree data contained by both forks will fit into the inode size,
90 * hence when combined with the inode core above, we have a total of the
91 * actual inode size.
92 * - the BMBT headers need to be accounted separately, as they are
93 * additional to the records and pointers that fit inside the inode
94 * forks.
Dave Chinner23956702013-08-28 16:10:35 +100095 */
96STATIC uint
97xfs_calc_inode_res(
98 struct xfs_mount *mp,
99 uint ninodes)
100{
Dave Chinnerfe4c2242014-03-07 16:19:14 +1100101 return ninodes *
102 (4 * sizeof(struct xlog_op_header) +
103 sizeof(struct xfs_inode_log_format) +
104 mp->m_sb.sb_inodesize +
105 2 * XFS_BMBT_BLOCK_LEN(mp));
Dave Chinner23956702013-08-28 16:10:35 +1000106}
107
108/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000109 * Various log reservation values.
110 *
111 * These are based on the size of the file system block because that is what
112 * most transactions manipulate. Each adds in an additional 128 bytes per
113 * item logged to try to account for the overhead of the transaction mechanism.
114 *
115 * Note: Most of the reservations underestimate the number of allocation
116 * groups into which they could free extents in the xfs_bmap_finish() call.
117 * This is because the number in the worst case is quite high and quite
118 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
119 * extents in only a single AG at a time. This will require changes to the
120 * EFI code as well, however, so that the EFI for the extents not freed is
121 * logged again in each transaction. See SGI PV #261917.
122 *
123 * Reservation functions here avoid a huge stack in xfs_trans_init due to
124 * register overflow from temporaries in the calculations.
125 */
126
127
128/*
129 * In a write transaction we can allocate a maximum of 2
130 * extents. This gives:
131 * the inode getting the new extents: inode size
132 * the inode's bmap btree: max depth * block size
133 * the agfs of the ags from which the extents are allocated: 2 * sector
134 * the superblock free block counter: sector size
135 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
136 * And the bmap_finish transaction can free bmap blocks in a join:
137 * the agfs of the ags containing the blocks: 2 * sector size
138 * the agfls of the ags containing the blocks: 2 * sector size
139 * the super block free block counter: sector size
140 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
141 */
142STATIC uint
143xfs_calc_write_reservation(
144 struct xfs_mount *mp)
145{
146 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000147 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000148 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
149 XFS_FSB_TO_B(mp, 1)) +
150 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
151 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
152 XFS_FSB_TO_B(mp, 1))),
153 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
154 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
155 XFS_FSB_TO_B(mp, 1))));
156}
157
158/*
159 * In truncating a file we free up to two extents at once. We can modify:
160 * the inode being truncated: inode size
161 * the inode's bmap btree: (max depth + 1) * block size
162 * And the bmap_finish transaction can free the blocks and bmap blocks:
163 * the agf for each of the ags: 4 * sector size
164 * the agfl for each of the ags: 4 * sector size
165 * the super block to reflect the freed blocks: sector size
166 * worst case split in allocation btrees per extent assuming 4 extents:
167 * 4 exts * 2 trees * (2 * max depth - 1) * block size
168 * the inode btree: max depth * blocksize
169 * the allocation btrees: 2 trees * (max depth - 1) * block size
170 */
171STATIC uint
172xfs_calc_itruncate_reservation(
173 struct xfs_mount *mp)
174{
175 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000176 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000177 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
178 XFS_FSB_TO_B(mp, 1))),
179 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
180 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
181 XFS_FSB_TO_B(mp, 1)) +
182 xfs_calc_buf_res(5, 0) +
183 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
184 XFS_FSB_TO_B(mp, 1)) +
Jie Liu126cd102013-12-13 15:51:48 +1100185 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000186 mp->m_in_maxlevels, 0)));
187}
188
189/*
190 * In renaming a files we can modify:
191 * the four inodes involved: 4 * inode size
192 * the two directory btrees: 2 * (max depth + v2) * dir block size
193 * the two directory bmap btrees: 2 * max depth * block size
194 * And the bmap_finish transaction can free dir and bmap blocks (two sets
195 * of bmap blocks) giving:
196 * the agf for the ags in which the blocks live: 3 * sector size
197 * the agfl for the ags in which the blocks live: 3 * sector size
198 * the superblock for the free block count: sector size
199 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
200 */
201STATIC uint
202xfs_calc_rename_reservation(
203 struct xfs_mount *mp)
204{
205 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000206 MAX((xfs_calc_inode_res(mp, 4) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000207 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
208 XFS_FSB_TO_B(mp, 1))),
209 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
210 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
211 XFS_FSB_TO_B(mp, 1))));
212}
213
214/*
Zhi Yong Wuab297432013-12-18 08:22:41 +0800215 * For removing an inode from unlinked list at first, we can modify:
216 * the agi hash list and counters: sector size
217 * the on disk inode before ours in the agi hash list: inode cluster size
218 */
219STATIC uint
220xfs_calc_iunlink_remove_reservation(
221 struct xfs_mount *mp)
222{
223 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinnerfe986f92014-03-13 19:14:43 +1100224 max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
Zhi Yong Wuab297432013-12-18 08:22:41 +0800225}
226
227/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000228 * For creating a link to an inode:
229 * the parent directory inode: inode size
230 * the linked inode: inode size
231 * the directory btree could split: (max depth + v2) * dir block size
232 * the directory bmap btree could join or split: (max depth + v2) * blocksize
233 * And the bmap_finish transaction can free some bmap blocks giving:
234 * the agf for the ag in which the blocks live: sector size
235 * the agfl for the ag in which the blocks live: sector size
236 * the superblock for the free block count: sector size
237 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
238 */
239STATIC uint
240xfs_calc_link_reservation(
241 struct xfs_mount *mp)
242{
243 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800244 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000245 MAX((xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000246 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
247 XFS_FSB_TO_B(mp, 1))),
248 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
249 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
250 XFS_FSB_TO_B(mp, 1))));
251}
252
253/*
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800254 * For adding an inode to unlinked list we can modify:
255 * the agi hash list: sector size
256 * the unlinked inode: inode size
257 */
258STATIC uint
259xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
260{
261 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
262 xfs_calc_inode_res(mp, 1);
263}
264
265/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000266 * For removing a directory entry we can modify:
267 * the parent directory inode: inode size
268 * the removed inode: inode size
269 * the directory btree could join: (max depth + v2) * dir block size
270 * the directory bmap btree could join or split: (max depth + v2) * blocksize
271 * And the bmap_finish transaction can free the dir and bmap blocks giving:
272 * the agf for the ag in which the blocks live: 2 * sector size
273 * the agfl for the ag in which the blocks live: 2 * sector size
274 * the superblock for the free block count: sector size
275 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
276 */
277STATIC uint
278xfs_calc_remove_reservation(
279 struct xfs_mount *mp)
280{
281 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800282 xfs_calc_iunlink_add_reservation(mp) +
283 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000284 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
285 XFS_FSB_TO_B(mp, 1))),
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800286 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000287 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
288 XFS_FSB_TO_B(mp, 1))));
289}
290
291/*
292 * For create, break it in to the two cases that the transaction
293 * covers. We start with the modify case - allocation done by modification
294 * of the state of existing inodes - and the allocation case.
295 */
296
297/*
298 * For create we can modify:
299 * the parent directory inode: inode size
300 * the new inode: inode size
301 * the inode btree entry: block size
302 * the superblock for the nlink flag: sector size
303 * the directory btree: (max depth + v2) * dir block size
304 * the directory inode's bmap btree: (max depth + v2) * block size
305 */
306STATIC uint
307xfs_calc_create_resv_modify(
308 struct xfs_mount *mp)
309{
Dave Chinner23956702013-08-28 16:10:35 +1000310 return xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000311 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
312 (uint)XFS_FSB_TO_B(mp, 1) +
313 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1));
314}
315
316/*
317 * For create we can allocate some inodes giving:
318 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
319 * the superblock for the nlink flag: sector size
Jie Liu126cd102013-12-13 15:51:48 +1100320 * the inode blocks allocated: mp->m_ialloc_blks * blocksize
Dave Chinner7fd36c42013-08-12 20:49:32 +1000321 * the inode btree: max depth * blocksize
322 * the allocation btrees: 2 trees * (max depth - 1) * block size
323 */
324STATIC uint
325xfs_calc_create_resv_alloc(
326 struct xfs_mount *mp)
327{
328 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
329 mp->m_sb.sb_sectsize +
Jie Liu126cd102013-12-13 15:51:48 +1100330 xfs_calc_buf_res(mp->m_ialloc_blks, XFS_FSB_TO_B(mp, 1)) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000331 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
332 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
333 XFS_FSB_TO_B(mp, 1));
334}
335
336STATIC uint
337__xfs_calc_create_reservation(
338 struct xfs_mount *mp)
339{
340 return XFS_DQUOT_LOGRES(mp) +
341 MAX(xfs_calc_create_resv_alloc(mp),
342 xfs_calc_create_resv_modify(mp));
343}
344
345/*
346 * For icreate we can allocate some inodes giving:
347 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
348 * the superblock for the nlink flag: sector size
349 * the inode btree: max depth * blocksize
350 * the allocation btrees: 2 trees * (max depth - 1) * block size
351 */
352STATIC uint
353xfs_calc_icreate_resv_alloc(
354 struct xfs_mount *mp)
355{
356 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
357 mp->m_sb.sb_sectsize +
358 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
359 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
360 XFS_FSB_TO_B(mp, 1));
361}
362
363STATIC uint
364xfs_calc_icreate_reservation(xfs_mount_t *mp)
365{
366 return XFS_DQUOT_LOGRES(mp) +
367 MAX(xfs_calc_icreate_resv_alloc(mp),
368 xfs_calc_create_resv_modify(mp));
369}
370
371STATIC uint
372xfs_calc_create_reservation(
373 struct xfs_mount *mp)
374{
375 if (xfs_sb_version_hascrc(&mp->m_sb))
376 return xfs_calc_icreate_reservation(mp);
377 return __xfs_calc_create_reservation(mp);
378
379}
380
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800381STATIC uint
382xfs_calc_create_tmpfile_reservation(
383 struct xfs_mount *mp)
384{
385 uint res = XFS_DQUOT_LOGRES(mp);
386
387 if (xfs_sb_version_hascrc(&mp->m_sb))
388 res += xfs_calc_icreate_resv_alloc(mp);
389 else
390 res += xfs_calc_create_resv_alloc(mp);
391
392 return res + xfs_calc_iunlink_add_reservation(mp);
393}
394
Dave Chinner7fd36c42013-08-12 20:49:32 +1000395/*
396 * Making a new directory is the same as creating a new file.
397 */
398STATIC uint
399xfs_calc_mkdir_reservation(
400 struct xfs_mount *mp)
401{
402 return xfs_calc_create_reservation(mp);
403}
404
405
406/*
407 * Making a new symplink is the same as creating a new file, but
408 * with the added blocks for remote symlink data which can be up to 1kB in
409 * length (MAXPATHLEN).
410 */
411STATIC uint
412xfs_calc_symlink_reservation(
413 struct xfs_mount *mp)
414{
415 return xfs_calc_create_reservation(mp) +
416 xfs_calc_buf_res(1, MAXPATHLEN);
417}
418
419/*
420 * In freeing an inode we can modify:
421 * the inode being freed: inode size
422 * the super block free inode counter: sector size
423 * the agi hash list and counters: sector size
424 * the inode btree entry: block size
425 * the on disk inode before ours in the agi hash list: inode cluster size
426 * the inode btree: max depth * blocksize
427 * the allocation btrees: 2 trees * (max depth - 1) * block size
428 */
429STATIC uint
430xfs_calc_ifree_reservation(
431 struct xfs_mount *mp)
432{
433 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000434 xfs_calc_inode_res(mp, 1) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800435 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000436 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800437 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000438 xfs_calc_buf_res(1, 0) +
Jie Liu126cd102013-12-13 15:51:48 +1100439 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000440 mp->m_in_maxlevels, 0) +
441 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
442 XFS_FSB_TO_B(mp, 1));
443}
444
445/*
446 * When only changing the inode we log the inode and possibly the superblock
447 * We also add a bit of slop for the transaction stuff.
448 */
449STATIC uint
450xfs_calc_ichange_reservation(
451 struct xfs_mount *mp)
452{
453 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000454 xfs_calc_inode_res(mp, 1) +
455 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000456
457}
458
459/*
460 * Growing the data section of the filesystem.
461 * superblock
462 * agi and agf
463 * allocation btrees
464 */
465STATIC uint
466xfs_calc_growdata_reservation(
467 struct xfs_mount *mp)
468{
469 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
470 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
471 XFS_FSB_TO_B(mp, 1));
472}
473
474/*
475 * Growing the rt section of the filesystem.
476 * In the first set of transactions (ALLOC) we allocate space to the
477 * bitmap or summary files.
478 * superblock: sector size
479 * agf of the ag from which the extent is allocated: sector size
480 * bmap btree for bitmap/summary inode: max depth * blocksize
481 * bitmap/summary inode: inode size
482 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
483 */
484STATIC uint
485xfs_calc_growrtalloc_reservation(
486 struct xfs_mount *mp)
487{
488 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
489 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
490 XFS_FSB_TO_B(mp, 1)) +
Dave Chinner23956702013-08-28 16:10:35 +1000491 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000492 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
493 XFS_FSB_TO_B(mp, 1));
494}
495
496/*
497 * Growing the rt section of the filesystem.
498 * In the second set of transactions (ZERO) we zero the new metadata blocks.
499 * one bitmap/summary block: blocksize
500 */
501STATIC uint
502xfs_calc_growrtzero_reservation(
503 struct xfs_mount *mp)
504{
505 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
506}
507
508/*
509 * Growing the rt section of the filesystem.
510 * In the third set of transactions (FREE) we update metadata without
511 * allocating any new blocks.
512 * superblock: sector size
513 * bitmap inode: inode size
514 * summary inode: inode size
515 * one bitmap block: blocksize
516 * summary blocks: new summary size
517 */
518STATIC uint
519xfs_calc_growrtfree_reservation(
520 struct xfs_mount *mp)
521{
522 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner23956702013-08-28 16:10:35 +1000523 xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000524 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
525 xfs_calc_buf_res(1, mp->m_rsumsize);
526}
527
528/*
529 * Logging the inode modification timestamp on a synchronous write.
530 * inode
531 */
532STATIC uint
533xfs_calc_swrite_reservation(
534 struct xfs_mount *mp)
535{
Dave Chinner23956702013-08-28 16:10:35 +1000536 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000537}
538
539/*
540 * Logging the inode mode bits when writing a setuid/setgid file
541 * inode
542 */
543STATIC uint
Dave Chinner23956702013-08-28 16:10:35 +1000544xfs_calc_writeid_reservation(
545 struct xfs_mount *mp)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000546{
Dave Chinner23956702013-08-28 16:10:35 +1000547 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000548}
549
550/*
551 * Converting the inode from non-attributed to attributed.
552 * the inode being converted: inode size
553 * agf block and superblock (for block allocation)
554 * the new block (directory sized)
555 * bmap blocks for the new directory block
556 * allocation btrees
557 */
558STATIC uint
559xfs_calc_addafork_reservation(
560 struct xfs_mount *mp)
561{
562 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000563 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000564 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
565 xfs_calc_buf_res(1, mp->m_dirblksize) +
566 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
567 XFS_FSB_TO_B(mp, 1)) +
568 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
569 XFS_FSB_TO_B(mp, 1));
570}
571
572/*
573 * Removing the attribute fork of a file
574 * the inode being truncated: inode size
575 * the inode's bmap btree: max depth * block size
576 * And the bmap_finish transaction can free the blocks and bmap blocks:
577 * the agf for each of the ags: 4 * sector size
578 * the agfl for each of the ags: 4 * sector size
579 * the super block to reflect the freed blocks: sector size
580 * worst case split in allocation btrees per extent assuming 4 extents:
581 * 4 exts * 2 trees * (2 * max depth - 1) * block size
582 */
583STATIC uint
584xfs_calc_attrinval_reservation(
585 struct xfs_mount *mp)
586{
Dave Chinner23956702013-08-28 16:10:35 +1000587 return MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000588 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
589 XFS_FSB_TO_B(mp, 1))),
590 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
591 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
592 XFS_FSB_TO_B(mp, 1))));
593}
594
595/*
596 * Setting an attribute at mount time.
597 * the inode getting the attribute
598 * the superblock for allocations
599 * the agfs extents are allocated from
600 * the attribute btree * max depth
601 * the inode allocation btree
602 * Since attribute transaction space is dependent on the size of the attribute,
603 * the calculation is done partially at mount time and partially at runtime(see
604 * below).
605 */
606STATIC uint
607xfs_calc_attrsetm_reservation(
608 struct xfs_mount *mp)
609{
610 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000611 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000612 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
613 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
614}
615
616/*
617 * Setting an attribute at runtime, transaction space unit per block.
618 * the superblock for allocations: sector size
619 * the inode bmap btree could join or split: max depth * block size
620 * Since the runtime attribute transaction space is dependent on the total
621 * blocks needed for the 1st bmap, here we calculate out the space unit for
622 * one block so that the caller could figure out the total space according
Jie Liu3d3c8b52013-08-12 20:49:59 +1000623 * to the attibute extent length in blocks by:
624 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000625 */
626STATIC uint
627xfs_calc_attrsetrt_reservation(
628 struct xfs_mount *mp)
629{
630 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
631 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
632 XFS_FSB_TO_B(mp, 1));
633}
634
635/*
636 * Removing an attribute.
637 * the inode: inode size
638 * the attribute btree could join: max depth * block size
639 * the inode bmap btree could join or split: max depth * block size
640 * And the bmap_finish transaction can free the attr blocks freed giving:
641 * the agf for the ag in which the blocks live: 2 * sector size
642 * the agfl for the ag in which the blocks live: 2 * sector size
643 * the superblock for the free block count: sector size
644 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
645 */
646STATIC uint
647xfs_calc_attrrm_reservation(
648 struct xfs_mount *mp)
649{
650 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000651 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000652 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
653 XFS_FSB_TO_B(mp, 1)) +
654 (uint)XFS_FSB_TO_B(mp,
655 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
656 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
657 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
658 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
659 XFS_FSB_TO_B(mp, 1))));
660}
661
662/*
663 * Clearing a bad agino number in an agi hash bucket.
664 */
665STATIC uint
666xfs_calc_clear_agi_bucket_reservation(
667 struct xfs_mount *mp)
668{
669 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
670}
671
672/*
673 * Clearing the quotaflags in the superblock.
674 * the super block for changing quota flags: sector size
675 */
676STATIC uint
677xfs_calc_qm_sbchange_reservation(
678 struct xfs_mount *mp)
679{
680 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
681}
682
683/*
684 * Adjusting quota limits.
685 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
686 */
687STATIC uint
688xfs_calc_qm_setqlim_reservation(
689 struct xfs_mount *mp)
690{
691 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
692}
693
694/*
695 * Allocating quota on disk if needed.
Brian Foster410b11a2014-02-07 14:55:54 +1100696 * the write transaction log space for quota file extent allocation
Dave Chinner7fd36c42013-08-12 20:49:32 +1000697 * the unit of quota allocation: one system block size
698 */
699STATIC uint
700xfs_calc_qm_dqalloc_reservation(
701 struct xfs_mount *mp)
702{
Brian Foster410b11a2014-02-07 14:55:54 +1100703 return xfs_calc_write_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000704 xfs_calc_buf_res(1,
705 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
706}
707
708/*
709 * Turning off quotas.
710 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
711 * the superblock for the quota flags: sector size
712 */
713STATIC uint
714xfs_calc_qm_quotaoff_reservation(
715 struct xfs_mount *mp)
716{
717 return sizeof(struct xfs_qoff_logitem) * 2 +
718 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
719}
720
721/*
722 * End of turning off quotas.
723 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
724 */
725STATIC uint
726xfs_calc_qm_quotaoff_end_reservation(
727 struct xfs_mount *mp)
728{
729 return sizeof(struct xfs_qoff_logitem) * 2;
730}
731
732/*
733 * Syncing the incore super block changes to disk.
734 * the super block to reflect the changes: sector size
735 */
736STATIC uint
737xfs_calc_sb_reservation(
738 struct xfs_mount *mp)
739{
740 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
741}
742
743void
744xfs_trans_resv_calc(
745 struct xfs_mount *mp,
746 struct xfs_trans_resv *resp)
747{
Jie Liu0eadd102013-08-12 20:49:56 +1000748 /*
749 * The following transactions are logged in physical format and
750 * require a permanent reservation on space.
751 */
752 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
753 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
754 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
755
756 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
757 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
758 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
759
760 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
761 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
762 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
763
764 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
765 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
766 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
767
768 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
769 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
770 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
771
772 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
773 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
774 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
775
776 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
777 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
778 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
779
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800780 resp->tr_create_tmpfile.tr_logres =
781 xfs_calc_create_tmpfile_reservation(mp);
782 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
783 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
784
Jie Liu0eadd102013-08-12 20:49:56 +1000785 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
786 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
787 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
788
789 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
790 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
791 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
792
793 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
794 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
795 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
796
797 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
798 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
799 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
800
801 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
802 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
803 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
804
805 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
806 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
807 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
808
809 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
810 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
811 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
812
813 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
814 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
815 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
816
817 /*
818 * The following transactions are logged in logical format with
819 * a default log count.
820 */
821 resp->tr_qm_sbchange.tr_logres = xfs_calc_qm_sbchange_reservation(mp);
822 resp->tr_qm_sbchange.tr_logcount = XFS_DEFAULT_LOG_COUNT;
823
824 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
825 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
826
827 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
828 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
829
830 resp->tr_qm_equotaoff.tr_logres =
831 xfs_calc_qm_quotaoff_end_reservation(mp);
832 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
833
834 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
835 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
836
837 /* The following transaction are logged in logical format */
838 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
839 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
Jie Liu20996c92013-08-12 20:49:57 +1000840 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu0eadd102013-08-12 20:49:56 +1000841 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
842 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
843 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
844 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
845 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000846}