blob: 52b6c3e3203e167200750e7766870692dbb7d2bc [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 Chinner70a98832013-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/*
Brian Foster9d43b182014-04-24 16:00:52 +1000109 * The free inode btree is a conditional feature and the log reservation
110 * requirements differ slightly from that of the traditional inode allocation
111 * btree. The finobt tracks records for inode chunks with at least one free
112 * inode. A record can be removed from the tree for an inode allocation
113 * or free and thus the finobt reservation is unconditional across:
114 *
115 * - inode allocation
116 * - inode free
117 * - inode chunk allocation
118 *
119 * The 'modify' param indicates to include the record modification scenario. The
120 * 'alloc' param indicates to include the reservation for free space btree
121 * modifications on behalf of finobt modifications. This is required only for
122 * transactions that do not already account for free space btree modifications.
123 *
124 * the free inode btree: max depth * block size
125 * the allocation btrees: 2 trees * (max depth - 1) * block size
126 * the free inode btree entry: block size
127 */
128STATIC uint
129xfs_calc_finobt_res(
130 struct xfs_mount *mp,
131 int alloc,
132 int modify)
133{
134 uint res;
135
136 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
137 return 0;
138
139 res = xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1));
140 if (alloc)
141 res += xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
142 XFS_FSB_TO_B(mp, 1));
143 if (modify)
144 res += (uint)XFS_FSB_TO_B(mp, 1);
145
146 return res;
147}
148
149/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000150 * Various log reservation values.
151 *
152 * These are based on the size of the file system block because that is what
153 * most transactions manipulate. Each adds in an additional 128 bytes per
154 * item logged to try to account for the overhead of the transaction mechanism.
155 *
156 * Note: Most of the reservations underestimate the number of allocation
157 * groups into which they could free extents in the xfs_bmap_finish() call.
158 * This is because the number in the worst case is quite high and quite
159 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
160 * extents in only a single AG at a time. This will require changes to the
161 * EFI code as well, however, so that the EFI for the extents not freed is
162 * logged again in each transaction. See SGI PV #261917.
163 *
164 * Reservation functions here avoid a huge stack in xfs_trans_init due to
165 * register overflow from temporaries in the calculations.
166 */
167
168
169/*
170 * In a write transaction we can allocate a maximum of 2
171 * extents. This gives:
172 * the inode getting the new extents: inode size
173 * the inode's bmap btree: max depth * block size
174 * the agfs of the ags from which the extents are allocated: 2 * sector
175 * the superblock free block counter: sector size
176 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
177 * And the bmap_finish transaction can free bmap blocks in a join:
178 * the agfs of the ags containing the blocks: 2 * sector size
179 * the agfls of the ags containing the blocks: 2 * sector size
180 * the super block free block counter: sector size
181 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
182 */
183STATIC uint
184xfs_calc_write_reservation(
185 struct xfs_mount *mp)
186{
187 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000188 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000189 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
190 XFS_FSB_TO_B(mp, 1)) +
191 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
192 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
193 XFS_FSB_TO_B(mp, 1))),
194 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
195 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
196 XFS_FSB_TO_B(mp, 1))));
197}
198
199/*
200 * In truncating a file we free up to two extents at once. We can modify:
201 * the inode being truncated: inode size
202 * the inode's bmap btree: (max depth + 1) * block size
203 * And the bmap_finish transaction can free the blocks and bmap blocks:
204 * the agf for each of the ags: 4 * sector size
205 * the agfl for each of the ags: 4 * sector size
206 * the super block to reflect the freed blocks: sector size
207 * worst case split in allocation btrees per extent assuming 4 extents:
208 * 4 exts * 2 trees * (2 * max depth - 1) * block size
209 * the inode btree: max depth * blocksize
210 * the allocation btrees: 2 trees * (max depth - 1) * block size
211 */
212STATIC uint
213xfs_calc_itruncate_reservation(
214 struct xfs_mount *mp)
215{
216 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000217 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000218 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
219 XFS_FSB_TO_B(mp, 1))),
220 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
221 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
222 XFS_FSB_TO_B(mp, 1)) +
223 xfs_calc_buf_res(5, 0) +
224 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
225 XFS_FSB_TO_B(mp, 1)) +
Jie Liu126cd102013-12-13 15:51:48 +1100226 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000227 mp->m_in_maxlevels, 0)));
228}
229
230/*
231 * In renaming a files we can modify:
232 * the four inodes involved: 4 * inode size
233 * the two directory btrees: 2 * (max depth + v2) * dir block size
234 * the two directory bmap btrees: 2 * max depth * block size
235 * And the bmap_finish transaction can free dir and bmap blocks (two sets
236 * of bmap blocks) giving:
237 * the agf for the ags in which the blocks live: 3 * sector size
238 * the agfl for the ags in which the blocks live: 3 * sector size
239 * the superblock for the free block count: sector size
240 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
241 */
242STATIC uint
243xfs_calc_rename_reservation(
244 struct xfs_mount *mp)
245{
246 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000247 MAX((xfs_calc_inode_res(mp, 4) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000248 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
249 XFS_FSB_TO_B(mp, 1))),
250 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
251 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
252 XFS_FSB_TO_B(mp, 1))));
253}
254
255/*
Zhi Yong Wuab297432013-12-18 08:22:41 +0800256 * For removing an inode from unlinked list at first, we can modify:
257 * the agi hash list and counters: sector size
258 * the on disk inode before ours in the agi hash list: inode cluster size
259 */
260STATIC uint
261xfs_calc_iunlink_remove_reservation(
262 struct xfs_mount *mp)
263{
264 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinnerfe986f92014-03-13 19:14:43 +1100265 max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
Zhi Yong Wuab297432013-12-18 08:22:41 +0800266}
267
268/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000269 * For creating a link to an inode:
270 * the parent directory inode: inode size
271 * the linked inode: inode size
272 * the directory btree could split: (max depth + v2) * dir block size
273 * the directory bmap btree could join or split: (max depth + v2) * blocksize
274 * And the bmap_finish transaction can free some bmap blocks giving:
275 * the agf for the ag in which the blocks live: sector size
276 * the agfl for the ag in which the blocks live: sector size
277 * the superblock for the free block count: sector size
278 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
279 */
280STATIC uint
281xfs_calc_link_reservation(
282 struct xfs_mount *mp)
283{
284 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800285 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000286 MAX((xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000287 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
288 XFS_FSB_TO_B(mp, 1))),
289 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
290 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
291 XFS_FSB_TO_B(mp, 1))));
292}
293
294/*
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800295 * For adding an inode to unlinked list we can modify:
296 * the agi hash list: sector size
297 * the unlinked inode: inode size
298 */
299STATIC uint
300xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
301{
302 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
303 xfs_calc_inode_res(mp, 1);
304}
305
306/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000307 * For removing a directory entry we can modify:
308 * the parent directory inode: inode size
309 * the removed inode: inode size
310 * the directory btree could join: (max depth + v2) * dir block size
311 * the directory bmap btree could join or split: (max depth + v2) * blocksize
312 * And the bmap_finish transaction can free the dir and bmap blocks giving:
313 * the agf for the ag in which the blocks live: 2 * sector size
314 * the agfl for the ag in which the blocks live: 2 * sector size
315 * the superblock for the free block count: sector size
316 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
317 */
318STATIC uint
319xfs_calc_remove_reservation(
320 struct xfs_mount *mp)
321{
322 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800323 xfs_calc_iunlink_add_reservation(mp) +
324 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000325 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
326 XFS_FSB_TO_B(mp, 1))),
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800327 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000328 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
329 XFS_FSB_TO_B(mp, 1))));
330}
331
332/*
333 * For create, break it in to the two cases that the transaction
334 * covers. We start with the modify case - allocation done by modification
335 * of the state of existing inodes - and the allocation case.
336 */
337
338/*
339 * For create we can modify:
340 * the parent directory inode: inode size
341 * the new inode: inode size
342 * the inode btree entry: block size
343 * the superblock for the nlink flag: sector size
344 * the directory btree: (max depth + v2) * dir block size
345 * the directory inode's bmap btree: (max depth + v2) * block size
Brian Foster9d43b182014-04-24 16:00:52 +1000346 * the finobt (record modification and allocation btrees)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000347 */
348STATIC uint
349xfs_calc_create_resv_modify(
350 struct xfs_mount *mp)
351{
Dave Chinner23956702013-08-28 16:10:35 +1000352 return xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000353 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
354 (uint)XFS_FSB_TO_B(mp, 1) +
Brian Foster9d43b182014-04-24 16:00:52 +1000355 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1)) +
356 xfs_calc_finobt_res(mp, 1, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000357}
358
359/*
360 * For create we can allocate some inodes giving:
361 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
362 * the superblock for the nlink flag: sector size
Jie Liu126cd102013-12-13 15:51:48 +1100363 * the inode blocks allocated: mp->m_ialloc_blks * blocksize
Dave Chinner7fd36c42013-08-12 20:49:32 +1000364 * the inode btree: max depth * blocksize
365 * the allocation btrees: 2 trees * (max depth - 1) * block size
366 */
367STATIC uint
368xfs_calc_create_resv_alloc(
369 struct xfs_mount *mp)
370{
371 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
372 mp->m_sb.sb_sectsize +
Jie Liu126cd102013-12-13 15:51:48 +1100373 xfs_calc_buf_res(mp->m_ialloc_blks, XFS_FSB_TO_B(mp, 1)) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000374 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
375 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
376 XFS_FSB_TO_B(mp, 1));
377}
378
379STATIC uint
380__xfs_calc_create_reservation(
381 struct xfs_mount *mp)
382{
383 return XFS_DQUOT_LOGRES(mp) +
384 MAX(xfs_calc_create_resv_alloc(mp),
385 xfs_calc_create_resv_modify(mp));
386}
387
388/*
389 * For icreate we can allocate some inodes giving:
390 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
391 * the superblock for the nlink flag: sector size
392 * the inode btree: max depth * blocksize
393 * the allocation btrees: 2 trees * (max depth - 1) * block size
Brian Foster9d43b182014-04-24 16:00:52 +1000394 * the finobt (record insertion)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000395 */
396STATIC uint
397xfs_calc_icreate_resv_alloc(
398 struct xfs_mount *mp)
399{
400 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
401 mp->m_sb.sb_sectsize +
402 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
403 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
Brian Foster9d43b182014-04-24 16:00:52 +1000404 XFS_FSB_TO_B(mp, 1)) +
405 xfs_calc_finobt_res(mp, 0, 0);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000406}
407
408STATIC uint
409xfs_calc_icreate_reservation(xfs_mount_t *mp)
410{
411 return XFS_DQUOT_LOGRES(mp) +
412 MAX(xfs_calc_icreate_resv_alloc(mp),
413 xfs_calc_create_resv_modify(mp));
414}
415
416STATIC uint
417xfs_calc_create_reservation(
418 struct xfs_mount *mp)
419{
420 if (xfs_sb_version_hascrc(&mp->m_sb))
421 return xfs_calc_icreate_reservation(mp);
422 return __xfs_calc_create_reservation(mp);
423
424}
425
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800426STATIC uint
427xfs_calc_create_tmpfile_reservation(
428 struct xfs_mount *mp)
429{
430 uint res = XFS_DQUOT_LOGRES(mp);
431
432 if (xfs_sb_version_hascrc(&mp->m_sb))
433 res += xfs_calc_icreate_resv_alloc(mp);
434 else
435 res += xfs_calc_create_resv_alloc(mp);
436
437 return res + xfs_calc_iunlink_add_reservation(mp);
438}
439
Dave Chinner7fd36c42013-08-12 20:49:32 +1000440/*
441 * Making a new directory is the same as creating a new file.
442 */
443STATIC uint
444xfs_calc_mkdir_reservation(
445 struct xfs_mount *mp)
446{
447 return xfs_calc_create_reservation(mp);
448}
449
450
451/*
452 * Making a new symplink is the same as creating a new file, but
453 * with the added blocks for remote symlink data which can be up to 1kB in
454 * length (MAXPATHLEN).
455 */
456STATIC uint
457xfs_calc_symlink_reservation(
458 struct xfs_mount *mp)
459{
460 return xfs_calc_create_reservation(mp) +
461 xfs_calc_buf_res(1, MAXPATHLEN);
462}
463
464/*
465 * In freeing an inode we can modify:
466 * the inode being freed: inode size
467 * the super block free inode counter: sector size
468 * the agi hash list and counters: sector size
469 * the inode btree entry: block size
470 * the on disk inode before ours in the agi hash list: inode cluster size
471 * the inode btree: max depth * blocksize
472 * the allocation btrees: 2 trees * (max depth - 1) * block size
Brian Foster9d43b182014-04-24 16:00:52 +1000473 * the finobt (record insertion, removal or modification)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000474 */
475STATIC uint
476xfs_calc_ifree_reservation(
477 struct xfs_mount *mp)
478{
479 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000480 xfs_calc_inode_res(mp, 1) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800481 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000482 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800483 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000484 xfs_calc_buf_res(1, 0) +
Jie Liu126cd102013-12-13 15:51:48 +1100485 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000486 mp->m_in_maxlevels, 0) +
487 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
Brian Foster9d43b182014-04-24 16:00:52 +1000488 XFS_FSB_TO_B(mp, 1)) +
489 xfs_calc_finobt_res(mp, 0, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000490}
491
492/*
493 * When only changing the inode we log the inode and possibly the superblock
494 * We also add a bit of slop for the transaction stuff.
495 */
496STATIC uint
497xfs_calc_ichange_reservation(
498 struct xfs_mount *mp)
499{
500 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000501 xfs_calc_inode_res(mp, 1) +
502 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000503
504}
505
506/*
507 * Growing the data section of the filesystem.
508 * superblock
509 * agi and agf
510 * allocation btrees
511 */
512STATIC uint
513xfs_calc_growdata_reservation(
514 struct xfs_mount *mp)
515{
516 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
517 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
518 XFS_FSB_TO_B(mp, 1));
519}
520
521/*
522 * Growing the rt section of the filesystem.
523 * In the first set of transactions (ALLOC) we allocate space to the
524 * bitmap or summary files.
525 * superblock: sector size
526 * agf of the ag from which the extent is allocated: sector size
527 * bmap btree for bitmap/summary inode: max depth * blocksize
528 * bitmap/summary inode: inode size
529 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
530 */
531STATIC uint
532xfs_calc_growrtalloc_reservation(
533 struct xfs_mount *mp)
534{
535 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
536 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
537 XFS_FSB_TO_B(mp, 1)) +
Dave Chinner23956702013-08-28 16:10:35 +1000538 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000539 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
540 XFS_FSB_TO_B(mp, 1));
541}
542
543/*
544 * Growing the rt section of the filesystem.
545 * In the second set of transactions (ZERO) we zero the new metadata blocks.
546 * one bitmap/summary block: blocksize
547 */
548STATIC uint
549xfs_calc_growrtzero_reservation(
550 struct xfs_mount *mp)
551{
552 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
553}
554
555/*
556 * Growing the rt section of the filesystem.
557 * In the third set of transactions (FREE) we update metadata without
558 * allocating any new blocks.
559 * superblock: sector size
560 * bitmap inode: inode size
561 * summary inode: inode size
562 * one bitmap block: blocksize
563 * summary blocks: new summary size
564 */
565STATIC uint
566xfs_calc_growrtfree_reservation(
567 struct xfs_mount *mp)
568{
569 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner23956702013-08-28 16:10:35 +1000570 xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000571 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
572 xfs_calc_buf_res(1, mp->m_rsumsize);
573}
574
575/*
576 * Logging the inode modification timestamp on a synchronous write.
577 * inode
578 */
579STATIC uint
580xfs_calc_swrite_reservation(
581 struct xfs_mount *mp)
582{
Dave Chinner23956702013-08-28 16:10:35 +1000583 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000584}
585
586/*
587 * Logging the inode mode bits when writing a setuid/setgid file
588 * inode
589 */
590STATIC uint
Dave Chinner23956702013-08-28 16:10:35 +1000591xfs_calc_writeid_reservation(
592 struct xfs_mount *mp)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000593{
Dave Chinner23956702013-08-28 16:10:35 +1000594 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000595}
596
597/*
598 * Converting the inode from non-attributed to attributed.
599 * the inode being converted: inode size
600 * agf block and superblock (for block allocation)
601 * the new block (directory sized)
602 * bmap blocks for the new directory block
603 * allocation btrees
604 */
605STATIC uint
606xfs_calc_addafork_reservation(
607 struct xfs_mount *mp)
608{
609 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000610 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000611 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
612 xfs_calc_buf_res(1, mp->m_dirblksize) +
613 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
614 XFS_FSB_TO_B(mp, 1)) +
615 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
616 XFS_FSB_TO_B(mp, 1));
617}
618
619/*
620 * Removing the attribute fork of a file
621 * the inode being truncated: inode size
622 * the inode's bmap btree: max depth * block size
623 * And the bmap_finish transaction can free the blocks and bmap blocks:
624 * the agf for each of the ags: 4 * sector size
625 * the agfl for each of the ags: 4 * sector size
626 * the super block to reflect the freed blocks: sector size
627 * worst case split in allocation btrees per extent assuming 4 extents:
628 * 4 exts * 2 trees * (2 * max depth - 1) * block size
629 */
630STATIC uint
631xfs_calc_attrinval_reservation(
632 struct xfs_mount *mp)
633{
Dave Chinner23956702013-08-28 16:10:35 +1000634 return MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000635 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
636 XFS_FSB_TO_B(mp, 1))),
637 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
638 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
639 XFS_FSB_TO_B(mp, 1))));
640}
641
642/*
643 * Setting an attribute at mount time.
644 * the inode getting the attribute
645 * the superblock for allocations
646 * the agfs extents are allocated from
647 * the attribute btree * max depth
648 * the inode allocation btree
649 * Since attribute transaction space is dependent on the size of the attribute,
650 * the calculation is done partially at mount time and partially at runtime(see
651 * below).
652 */
653STATIC uint
654xfs_calc_attrsetm_reservation(
655 struct xfs_mount *mp)
656{
657 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000658 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000659 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
660 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
661}
662
663/*
664 * Setting an attribute at runtime, transaction space unit per block.
665 * the superblock for allocations: sector size
666 * the inode bmap btree could join or split: max depth * block size
667 * Since the runtime attribute transaction space is dependent on the total
668 * blocks needed for the 1st bmap, here we calculate out the space unit for
669 * one block so that the caller could figure out the total space according
Jie Liu3d3c8b52013-08-12 20:49:59 +1000670 * to the attibute extent length in blocks by:
671 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000672 */
673STATIC uint
674xfs_calc_attrsetrt_reservation(
675 struct xfs_mount *mp)
676{
677 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
678 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
679 XFS_FSB_TO_B(mp, 1));
680}
681
682/*
683 * Removing an attribute.
684 * the inode: inode size
685 * the attribute btree could join: max depth * block size
686 * the inode bmap btree could join or split: max depth * block size
687 * And the bmap_finish transaction can free the attr blocks freed giving:
688 * the agf for the ag in which the blocks live: 2 * sector size
689 * the agfl for the ag in which the blocks live: 2 * sector size
690 * the superblock for the free block count: sector size
691 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
692 */
693STATIC uint
694xfs_calc_attrrm_reservation(
695 struct xfs_mount *mp)
696{
697 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000698 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000699 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
700 XFS_FSB_TO_B(mp, 1)) +
701 (uint)XFS_FSB_TO_B(mp,
702 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
703 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
704 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
705 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
706 XFS_FSB_TO_B(mp, 1))));
707}
708
709/*
710 * Clearing a bad agino number in an agi hash bucket.
711 */
712STATIC uint
713xfs_calc_clear_agi_bucket_reservation(
714 struct xfs_mount *mp)
715{
716 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
717}
718
719/*
720 * Clearing the quotaflags in the superblock.
721 * the super block for changing quota flags: sector size
722 */
723STATIC uint
724xfs_calc_qm_sbchange_reservation(
725 struct xfs_mount *mp)
726{
727 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
728}
729
730/*
731 * Adjusting quota limits.
732 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
733 */
734STATIC uint
735xfs_calc_qm_setqlim_reservation(
736 struct xfs_mount *mp)
737{
738 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
739}
740
741/*
742 * Allocating quota on disk if needed.
Brian Foster410b11a2014-02-07 14:55:54 +1100743 * the write transaction log space for quota file extent allocation
Dave Chinner7fd36c42013-08-12 20:49:32 +1000744 * the unit of quota allocation: one system block size
745 */
746STATIC uint
747xfs_calc_qm_dqalloc_reservation(
748 struct xfs_mount *mp)
749{
Brian Foster410b11a2014-02-07 14:55:54 +1100750 return xfs_calc_write_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000751 xfs_calc_buf_res(1,
752 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
753}
754
755/*
756 * Turning off quotas.
757 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
758 * the superblock for the quota flags: sector size
759 */
760STATIC uint
761xfs_calc_qm_quotaoff_reservation(
762 struct xfs_mount *mp)
763{
764 return sizeof(struct xfs_qoff_logitem) * 2 +
765 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
766}
767
768/*
769 * End of turning off quotas.
770 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
771 */
772STATIC uint
773xfs_calc_qm_quotaoff_end_reservation(
774 struct xfs_mount *mp)
775{
776 return sizeof(struct xfs_qoff_logitem) * 2;
777}
778
779/*
780 * Syncing the incore super block changes to disk.
781 * the super block to reflect the changes: sector size
782 */
783STATIC uint
784xfs_calc_sb_reservation(
785 struct xfs_mount *mp)
786{
787 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
788}
789
790void
791xfs_trans_resv_calc(
792 struct xfs_mount *mp,
793 struct xfs_trans_resv *resp)
794{
Jie Liu0eadd102013-08-12 20:49:56 +1000795 /*
796 * The following transactions are logged in physical format and
797 * require a permanent reservation on space.
798 */
799 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
800 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
801 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
802
803 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
804 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
805 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
806
807 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
808 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
809 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
810
811 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
812 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
813 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
814
815 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
816 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
817 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
818
819 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
820 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
821 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
822
823 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
824 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
825 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
826
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800827 resp->tr_create_tmpfile.tr_logres =
828 xfs_calc_create_tmpfile_reservation(mp);
829 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
830 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
831
Jie Liu0eadd102013-08-12 20:49:56 +1000832 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
833 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
834 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
835
836 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
837 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
838 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
839
840 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
841 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
842 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
843
844 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
845 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
846 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
847
848 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
849 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
850 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
851
852 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
853 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
854 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
855
856 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
857 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
858 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
859
860 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
861 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
862 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
863
864 /*
865 * The following transactions are logged in logical format with
866 * a default log count.
867 */
868 resp->tr_qm_sbchange.tr_logres = xfs_calc_qm_sbchange_reservation(mp);
869 resp->tr_qm_sbchange.tr_logcount = XFS_DEFAULT_LOG_COUNT;
870
871 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
872 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
873
874 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
875 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
876
877 resp->tr_qm_equotaoff.tr_logres =
878 xfs_calc_qm_quotaoff_end_reservation(mp);
879 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
880
881 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
882 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
883
884 /* The following transaction are logged in logical format */
885 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
886 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
Jie Liu20996c92013-08-12 20:49:57 +1000887 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu0eadd102013-08-12 20:49:56 +1000888 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
889 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
890 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
891 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
892 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000893}