blob: f2bda7c76b8a6de3f8e28c345fb9a84738ac322f [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 Chinnerd6cf1302014-06-06 15:14:11 +100029#include "xfs_da_btree.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100030#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_bmap_btree.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100032#include "xfs_ialloc.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100033#include "xfs_quota.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100035#include "xfs_qm.h"
36#include "xfs_trans_space.h"
37#include "xfs_trace.h"
38
39/*
40 * A buffer has a format structure overhead in the log in addition
41 * to the data, so we need to take this into account when reserving
42 * space in a transaction for a buffer. Round the space required up
43 * to a multiple of 128 bytes so that we don't change the historical
44 * reservation that has been used for this overhead.
45 */
46STATIC uint
47xfs_buf_log_overhead(void)
48{
49 return round_up(sizeof(struct xlog_op_header) +
50 sizeof(struct xfs_buf_log_format), 128);
51}
52
53/*
54 * Calculate out transaction log reservation per item in bytes.
55 *
56 * The nbufs argument is used to indicate the number of items that
57 * will be changed in a transaction. size is used to tell how many
58 * bytes should be reserved per item.
59 */
60STATIC uint
61xfs_calc_buf_res(
62 uint nbufs,
63 uint size)
64{
65 return nbufs * (size + xfs_buf_log_overhead());
66}
67
68/*
Dave Chinner23956702013-08-28 16:10:35 +100069 * Logging inodes is really tricksy. They are logged in memory format,
70 * which means that what we write into the log doesn't directly translate into
71 * the amount of space they use on disk.
72 *
73 * Case in point - btree format forks in memory format use more space than the
74 * on-disk format. In memory, the buffer contains a normal btree block header so
75 * the btree code can treat it as though it is just another generic buffer.
76 * However, when we write it to the inode fork, we don't write all of this
77 * header as it isn't needed. e.g. the root is only ever in the inode, so
78 * there's no need for sibling pointers which would waste 16 bytes of space.
79 *
80 * Hence when we have an inode with a maximally sized btree format fork, then
81 * amount of information we actually log is greater than the size of the inode
82 * on disk. Hence we need an inode reservation function that calculates all this
83 * correctly. So, we log:
84 *
Dave Chinnerfe4c2242014-03-07 16:19:14 +110085 * - 4 log op headers for object
86 * - for the ilf, the inode core and 2 forks
Dave Chinner23956702013-08-28 16:10:35 +100087 * - inode log format object
Dave Chinnerfe4c2242014-03-07 16:19:14 +110088 * - the inode core
89 * - two inode forks containing bmap btree root blocks.
90 * - the btree data contained by both forks will fit into the inode size,
91 * hence when combined with the inode core above, we have a total of the
92 * actual inode size.
93 * - the BMBT headers need to be accounted separately, as they are
94 * additional to the records and pointers that fit inside the inode
95 * forks.
Dave Chinner23956702013-08-28 16:10:35 +100096 */
97STATIC uint
98xfs_calc_inode_res(
99 struct xfs_mount *mp,
100 uint ninodes)
101{
Dave Chinnerfe4c2242014-03-07 16:19:14 +1100102 return ninodes *
103 (4 * sizeof(struct xlog_op_header) +
104 sizeof(struct xfs_inode_log_format) +
105 mp->m_sb.sb_inodesize +
106 2 * XFS_BMBT_BLOCK_LEN(mp));
Dave Chinner23956702013-08-28 16:10:35 +1000107}
108
109/*
Brian Foster9d43b182014-04-24 16:00:52 +1000110 * The free inode btree is a conditional feature and the log reservation
111 * requirements differ slightly from that of the traditional inode allocation
112 * btree. The finobt tracks records for inode chunks with at least one free
113 * inode. A record can be removed from the tree for an inode allocation
114 * or free and thus the finobt reservation is unconditional across:
115 *
116 * - inode allocation
117 * - inode free
118 * - inode chunk allocation
119 *
120 * The 'modify' param indicates to include the record modification scenario. The
121 * 'alloc' param indicates to include the reservation for free space btree
122 * modifications on behalf of finobt modifications. This is required only for
123 * transactions that do not already account for free space btree modifications.
124 *
125 * the free inode btree: max depth * block size
126 * the allocation btrees: 2 trees * (max depth - 1) * block size
127 * the free inode btree entry: block size
128 */
129STATIC uint
130xfs_calc_finobt_res(
131 struct xfs_mount *mp,
132 int alloc,
133 int modify)
134{
135 uint res;
136
137 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
138 return 0;
139
140 res = xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1));
141 if (alloc)
142 res += xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
143 XFS_FSB_TO_B(mp, 1));
144 if (modify)
145 res += (uint)XFS_FSB_TO_B(mp, 1);
146
147 return res;
148}
149
150/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000151 * Various log reservation values.
152 *
153 * These are based on the size of the file system block because that is what
154 * most transactions manipulate. Each adds in an additional 128 bytes per
155 * item logged to try to account for the overhead of the transaction mechanism.
156 *
157 * Note: Most of the reservations underestimate the number of allocation
158 * groups into which they could free extents in the xfs_bmap_finish() call.
159 * This is because the number in the worst case is quite high and quite
160 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
161 * extents in only a single AG at a time. This will require changes to the
162 * EFI code as well, however, so that the EFI for the extents not freed is
163 * logged again in each transaction. See SGI PV #261917.
164 *
165 * Reservation functions here avoid a huge stack in xfs_trans_init due to
166 * register overflow from temporaries in the calculations.
167 */
168
169
170/*
171 * In a write transaction we can allocate a maximum of 2
172 * extents. This gives:
173 * the inode getting the new extents: inode size
174 * the inode's bmap btree: max depth * block size
175 * the agfs of the ags from which the extents are allocated: 2 * sector
176 * the superblock free block counter: sector size
177 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
178 * And the bmap_finish transaction can free bmap blocks in a join:
179 * the agfs of the ags containing the blocks: 2 * sector size
180 * the agfls of the ags containing the blocks: 2 * sector size
181 * the super block free block counter: sector size
182 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
183 */
184STATIC uint
185xfs_calc_write_reservation(
186 struct xfs_mount *mp)
187{
188 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000189 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000190 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
191 XFS_FSB_TO_B(mp, 1)) +
192 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
193 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
194 XFS_FSB_TO_B(mp, 1))),
195 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
196 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
197 XFS_FSB_TO_B(mp, 1))));
198}
199
200/*
201 * In truncating a file we free up to two extents at once. We can modify:
202 * the inode being truncated: inode size
203 * the inode's bmap btree: (max depth + 1) * block size
204 * And the bmap_finish transaction can free the blocks and bmap blocks:
205 * the agf for each of the ags: 4 * sector size
206 * the agfl for each of the ags: 4 * sector size
207 * the super block to reflect the freed blocks: sector size
208 * worst case split in allocation btrees per extent assuming 4 extents:
209 * 4 exts * 2 trees * (2 * max depth - 1) * block size
210 * the inode btree: max depth * blocksize
211 * the allocation btrees: 2 trees * (max depth - 1) * block size
212 */
213STATIC uint
214xfs_calc_itruncate_reservation(
215 struct xfs_mount *mp)
216{
217 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000218 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000219 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
220 XFS_FSB_TO_B(mp, 1))),
221 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
222 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
223 XFS_FSB_TO_B(mp, 1)) +
224 xfs_calc_buf_res(5, 0) +
225 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
226 XFS_FSB_TO_B(mp, 1)) +
Jie Liu126cd102013-12-13 15:51:48 +1100227 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000228 mp->m_in_maxlevels, 0)));
229}
230
231/*
232 * In renaming a files we can modify:
233 * the four inodes involved: 4 * inode size
234 * the two directory btrees: 2 * (max depth + v2) * dir block size
235 * the two directory bmap btrees: 2 * max depth * block size
236 * And the bmap_finish transaction can free dir and bmap blocks (two sets
237 * of bmap blocks) giving:
238 * the agf for the ags in which the blocks live: 3 * sector size
239 * the agfl for the ags in which the blocks live: 3 * sector size
240 * the superblock for the free block count: sector size
241 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
242 */
243STATIC uint
244xfs_calc_rename_reservation(
245 struct xfs_mount *mp)
246{
247 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000248 MAX((xfs_calc_inode_res(mp, 4) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000249 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
250 XFS_FSB_TO_B(mp, 1))),
251 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
252 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
253 XFS_FSB_TO_B(mp, 1))));
254}
255
256/*
Zhi Yong Wuab297432013-12-18 08:22:41 +0800257 * For removing an inode from unlinked list at first, we can modify:
258 * the agi hash list and counters: sector size
259 * the on disk inode before ours in the agi hash list: inode cluster size
260 */
261STATIC uint
262xfs_calc_iunlink_remove_reservation(
263 struct xfs_mount *mp)
264{
265 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinnerfe986f92014-03-13 19:14:43 +1100266 max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size);
Zhi Yong Wuab297432013-12-18 08:22:41 +0800267}
268
269/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000270 * For creating a link to an inode:
271 * the parent directory inode: inode size
272 * the linked inode: inode size
273 * the directory btree could split: (max depth + v2) * dir block size
274 * the directory bmap btree could join or split: (max depth + v2) * blocksize
275 * And the bmap_finish transaction can free some bmap blocks giving:
276 * the agf for the ag in which the blocks live: sector size
277 * the agfl for the ag in which the blocks live: sector size
278 * the superblock for the free block count: sector size
279 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
280 */
281STATIC uint
282xfs_calc_link_reservation(
283 struct xfs_mount *mp)
284{
285 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800286 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000287 MAX((xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000288 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
289 XFS_FSB_TO_B(mp, 1))),
290 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
291 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
292 XFS_FSB_TO_B(mp, 1))));
293}
294
295/*
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800296 * For adding an inode to unlinked list we can modify:
297 * the agi hash list: sector size
298 * the unlinked inode: inode size
299 */
300STATIC uint
301xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
302{
303 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
304 xfs_calc_inode_res(mp, 1);
305}
306
307/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000308 * For removing a directory entry we can modify:
309 * the parent directory inode: inode size
310 * the removed inode: inode size
311 * the directory btree could join: (max depth + v2) * dir block size
312 * the directory bmap btree could join or split: (max depth + v2) * blocksize
313 * And the bmap_finish transaction can free the dir and bmap blocks giving:
314 * the agf for the ag in which the blocks live: 2 * sector size
315 * the agfl for the ag in which the blocks live: 2 * sector size
316 * the superblock for the free block count: sector size
317 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
318 */
319STATIC uint
320xfs_calc_remove_reservation(
321 struct xfs_mount *mp)
322{
323 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800324 xfs_calc_iunlink_add_reservation(mp) +
325 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000326 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
327 XFS_FSB_TO_B(mp, 1))),
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800328 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000329 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
330 XFS_FSB_TO_B(mp, 1))));
331}
332
333/*
334 * For create, break it in to the two cases that the transaction
335 * covers. We start with the modify case - allocation done by modification
336 * of the state of existing inodes - and the allocation case.
337 */
338
339/*
340 * For create we can modify:
341 * the parent directory inode: inode size
342 * the new inode: inode size
343 * the inode btree entry: block size
344 * the superblock for the nlink flag: sector size
345 * the directory btree: (max depth + v2) * dir block size
346 * the directory inode's bmap btree: (max depth + v2) * block size
Brian Foster9d43b182014-04-24 16:00:52 +1000347 * the finobt (record modification and allocation btrees)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000348 */
349STATIC uint
350xfs_calc_create_resv_modify(
351 struct xfs_mount *mp)
352{
Dave Chinner23956702013-08-28 16:10:35 +1000353 return xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000354 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
355 (uint)XFS_FSB_TO_B(mp, 1) +
Brian Foster9d43b182014-04-24 16:00:52 +1000356 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1)) +
357 xfs_calc_finobt_res(mp, 1, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000358}
359
360/*
361 * For create we can allocate some inodes giving:
362 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
363 * the superblock for the nlink flag: sector size
Jie Liu126cd102013-12-13 15:51:48 +1100364 * the inode blocks allocated: mp->m_ialloc_blks * blocksize
Dave Chinner7fd36c42013-08-12 20:49:32 +1000365 * the inode btree: max depth * blocksize
366 * the allocation btrees: 2 trees * (max depth - 1) * block size
367 */
368STATIC uint
369xfs_calc_create_resv_alloc(
370 struct xfs_mount *mp)
371{
372 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
373 mp->m_sb.sb_sectsize +
Jie Liu126cd102013-12-13 15:51:48 +1100374 xfs_calc_buf_res(mp->m_ialloc_blks, XFS_FSB_TO_B(mp, 1)) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000375 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
376 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
377 XFS_FSB_TO_B(mp, 1));
378}
379
380STATIC uint
381__xfs_calc_create_reservation(
382 struct xfs_mount *mp)
383{
384 return XFS_DQUOT_LOGRES(mp) +
385 MAX(xfs_calc_create_resv_alloc(mp),
386 xfs_calc_create_resv_modify(mp));
387}
388
389/*
390 * For icreate we can allocate some inodes giving:
391 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
392 * the superblock for the nlink flag: sector size
393 * the inode btree: max depth * blocksize
394 * the allocation btrees: 2 trees * (max depth - 1) * block size
Brian Foster9d43b182014-04-24 16:00:52 +1000395 * the finobt (record insertion)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000396 */
397STATIC uint
398xfs_calc_icreate_resv_alloc(
399 struct xfs_mount *mp)
400{
401 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
402 mp->m_sb.sb_sectsize +
403 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
404 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
Brian Foster9d43b182014-04-24 16:00:52 +1000405 XFS_FSB_TO_B(mp, 1)) +
406 xfs_calc_finobt_res(mp, 0, 0);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000407}
408
409STATIC uint
410xfs_calc_icreate_reservation(xfs_mount_t *mp)
411{
412 return XFS_DQUOT_LOGRES(mp) +
413 MAX(xfs_calc_icreate_resv_alloc(mp),
414 xfs_calc_create_resv_modify(mp));
415}
416
417STATIC uint
418xfs_calc_create_reservation(
419 struct xfs_mount *mp)
420{
421 if (xfs_sb_version_hascrc(&mp->m_sb))
422 return xfs_calc_icreate_reservation(mp);
423 return __xfs_calc_create_reservation(mp);
424
425}
426
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800427STATIC uint
428xfs_calc_create_tmpfile_reservation(
429 struct xfs_mount *mp)
430{
431 uint res = XFS_DQUOT_LOGRES(mp);
432
433 if (xfs_sb_version_hascrc(&mp->m_sb))
434 res += xfs_calc_icreate_resv_alloc(mp);
435 else
436 res += xfs_calc_create_resv_alloc(mp);
437
438 return res + xfs_calc_iunlink_add_reservation(mp);
439}
440
Dave Chinner7fd36c42013-08-12 20:49:32 +1000441/*
442 * Making a new directory is the same as creating a new file.
443 */
444STATIC uint
445xfs_calc_mkdir_reservation(
446 struct xfs_mount *mp)
447{
448 return xfs_calc_create_reservation(mp);
449}
450
451
452/*
453 * Making a new symplink is the same as creating a new file, but
454 * with the added blocks for remote symlink data which can be up to 1kB in
455 * length (MAXPATHLEN).
456 */
457STATIC uint
458xfs_calc_symlink_reservation(
459 struct xfs_mount *mp)
460{
461 return xfs_calc_create_reservation(mp) +
462 xfs_calc_buf_res(1, MAXPATHLEN);
463}
464
465/*
466 * In freeing an inode we can modify:
467 * the inode being freed: inode size
468 * the super block free inode counter: sector size
469 * the agi hash list and counters: sector size
470 * the inode btree entry: block size
471 * the on disk inode before ours in the agi hash list: inode cluster size
472 * the inode btree: max depth * blocksize
473 * the allocation btrees: 2 trees * (max depth - 1) * block size
Brian Foster9d43b182014-04-24 16:00:52 +1000474 * the finobt (record insertion, removal or modification)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000475 */
476STATIC uint
477xfs_calc_ifree_reservation(
478 struct xfs_mount *mp)
479{
480 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000481 xfs_calc_inode_res(mp, 1) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800482 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000483 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800484 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000485 xfs_calc_buf_res(1, 0) +
Jie Liu126cd102013-12-13 15:51:48 +1100486 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000487 mp->m_in_maxlevels, 0) +
488 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
Brian Foster9d43b182014-04-24 16:00:52 +1000489 XFS_FSB_TO_B(mp, 1)) +
490 xfs_calc_finobt_res(mp, 0, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000491}
492
493/*
494 * When only changing the inode we log the inode and possibly the superblock
495 * We also add a bit of slop for the transaction stuff.
496 */
497STATIC uint
498xfs_calc_ichange_reservation(
499 struct xfs_mount *mp)
500{
501 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000502 xfs_calc_inode_res(mp, 1) +
503 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000504
505}
506
507/*
508 * Growing the data section of the filesystem.
509 * superblock
510 * agi and agf
511 * allocation btrees
512 */
513STATIC uint
514xfs_calc_growdata_reservation(
515 struct xfs_mount *mp)
516{
517 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
518 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
519 XFS_FSB_TO_B(mp, 1));
520}
521
522/*
523 * Growing the rt section of the filesystem.
524 * In the first set of transactions (ALLOC) we allocate space to the
525 * bitmap or summary files.
526 * superblock: sector size
527 * agf of the ag from which the extent is allocated: sector size
528 * bmap btree for bitmap/summary inode: max depth * blocksize
529 * bitmap/summary inode: inode size
530 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
531 */
532STATIC uint
533xfs_calc_growrtalloc_reservation(
534 struct xfs_mount *mp)
535{
536 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
537 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
538 XFS_FSB_TO_B(mp, 1)) +
Dave Chinner23956702013-08-28 16:10:35 +1000539 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000540 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
541 XFS_FSB_TO_B(mp, 1));
542}
543
544/*
545 * Growing the rt section of the filesystem.
546 * In the second set of transactions (ZERO) we zero the new metadata blocks.
547 * one bitmap/summary block: blocksize
548 */
549STATIC uint
550xfs_calc_growrtzero_reservation(
551 struct xfs_mount *mp)
552{
553 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
554}
555
556/*
557 * Growing the rt section of the filesystem.
558 * In the third set of transactions (FREE) we update metadata without
559 * allocating any new blocks.
560 * superblock: sector size
561 * bitmap inode: inode size
562 * summary inode: inode size
563 * one bitmap block: blocksize
564 * summary blocks: new summary size
565 */
566STATIC uint
567xfs_calc_growrtfree_reservation(
568 struct xfs_mount *mp)
569{
570 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner23956702013-08-28 16:10:35 +1000571 xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000572 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
573 xfs_calc_buf_res(1, mp->m_rsumsize);
574}
575
576/*
577 * Logging the inode modification timestamp on a synchronous write.
578 * inode
579 */
580STATIC uint
581xfs_calc_swrite_reservation(
582 struct xfs_mount *mp)
583{
Dave Chinner23956702013-08-28 16:10:35 +1000584 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000585}
586
587/*
588 * Logging the inode mode bits when writing a setuid/setgid file
589 * inode
590 */
591STATIC uint
Dave Chinner23956702013-08-28 16:10:35 +1000592xfs_calc_writeid_reservation(
593 struct xfs_mount *mp)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000594{
Dave Chinner23956702013-08-28 16:10:35 +1000595 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000596}
597
598/*
599 * Converting the inode from non-attributed to attributed.
600 * the inode being converted: inode size
601 * agf block and superblock (for block allocation)
602 * the new block (directory sized)
603 * bmap blocks for the new directory block
604 * allocation btrees
605 */
606STATIC uint
607xfs_calc_addafork_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(2, mp->m_sb.sb_sectsize) +
Dave Chinner8f661932014-06-06 15:15:59 +1000613 xfs_calc_buf_res(1, mp->m_dir_geo->blksize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000614 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
615 XFS_FSB_TO_B(mp, 1)) +
616 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
617 XFS_FSB_TO_B(mp, 1));
618}
619
620/*
621 * Removing the attribute fork of a file
622 * the inode being truncated: inode size
623 * the inode's bmap btree: max depth * block size
624 * And the bmap_finish transaction can free the blocks and bmap blocks:
625 * the agf for each of the ags: 4 * sector size
626 * the agfl for each of the ags: 4 * sector size
627 * the super block to reflect the freed blocks: sector size
628 * worst case split in allocation btrees per extent assuming 4 extents:
629 * 4 exts * 2 trees * (2 * max depth - 1) * block size
630 */
631STATIC uint
632xfs_calc_attrinval_reservation(
633 struct xfs_mount *mp)
634{
Dave Chinner23956702013-08-28 16:10:35 +1000635 return MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000636 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
637 XFS_FSB_TO_B(mp, 1))),
638 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
639 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
640 XFS_FSB_TO_B(mp, 1))));
641}
642
643/*
644 * Setting an attribute at mount time.
645 * the inode getting the attribute
646 * the superblock for allocations
647 * the agfs extents are allocated from
648 * the attribute btree * max depth
649 * the inode allocation btree
650 * Since attribute transaction space is dependent on the size of the attribute,
651 * the calculation is done partially at mount time and partially at runtime(see
652 * below).
653 */
654STATIC uint
655xfs_calc_attrsetm_reservation(
656 struct xfs_mount *mp)
657{
658 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000659 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000660 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
661 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
662}
663
664/*
665 * Setting an attribute at runtime, transaction space unit per block.
666 * the superblock for allocations: sector size
667 * the inode bmap btree could join or split: max depth * block size
668 * Since the runtime attribute transaction space is dependent on the total
669 * blocks needed for the 1st bmap, here we calculate out the space unit for
670 * one block so that the caller could figure out the total space according
Jie Liu3d3c8b52013-08-12 20:49:59 +1000671 * to the attibute extent length in blocks by:
672 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000673 */
674STATIC uint
675xfs_calc_attrsetrt_reservation(
676 struct xfs_mount *mp)
677{
678 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
679 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
680 XFS_FSB_TO_B(mp, 1));
681}
682
683/*
684 * Removing an attribute.
685 * the inode: inode size
686 * the attribute btree could join: max depth * block size
687 * the inode bmap btree could join or split: max depth * block size
688 * And the bmap_finish transaction can free the attr blocks freed giving:
689 * the agf for the ag in which the blocks live: 2 * sector size
690 * the agfl for the ag in which the blocks live: 2 * sector size
691 * the superblock for the free block count: sector size
692 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
693 */
694STATIC uint
695xfs_calc_attrrm_reservation(
696 struct xfs_mount *mp)
697{
698 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000699 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000700 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
701 XFS_FSB_TO_B(mp, 1)) +
702 (uint)XFS_FSB_TO_B(mp,
703 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
704 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
705 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
706 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
707 XFS_FSB_TO_B(mp, 1))));
708}
709
710/*
711 * Clearing a bad agino number in an agi hash bucket.
712 */
713STATIC uint
714xfs_calc_clear_agi_bucket_reservation(
715 struct xfs_mount *mp)
716{
717 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
718}
719
720/*
721 * Clearing the quotaflags in the superblock.
722 * the super block for changing quota flags: sector size
723 */
724STATIC uint
725xfs_calc_qm_sbchange_reservation(
726 struct xfs_mount *mp)
727{
728 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
729}
730
731/*
732 * Adjusting quota limits.
733 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
734 */
735STATIC uint
736xfs_calc_qm_setqlim_reservation(
737 struct xfs_mount *mp)
738{
739 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
740}
741
742/*
743 * Allocating quota on disk if needed.
Brian Foster410b11a2014-02-07 14:55:54 +1100744 * the write transaction log space for quota file extent allocation
Dave Chinner7fd36c42013-08-12 20:49:32 +1000745 * the unit of quota allocation: one system block size
746 */
747STATIC uint
748xfs_calc_qm_dqalloc_reservation(
749 struct xfs_mount *mp)
750{
Brian Foster410b11a2014-02-07 14:55:54 +1100751 return xfs_calc_write_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000752 xfs_calc_buf_res(1,
753 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
754}
755
756/*
757 * Turning off quotas.
758 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
759 * the superblock for the quota flags: sector size
760 */
761STATIC uint
762xfs_calc_qm_quotaoff_reservation(
763 struct xfs_mount *mp)
764{
765 return sizeof(struct xfs_qoff_logitem) * 2 +
766 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
767}
768
769/*
770 * End of turning off quotas.
771 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
772 */
773STATIC uint
774xfs_calc_qm_quotaoff_end_reservation(
775 struct xfs_mount *mp)
776{
777 return sizeof(struct xfs_qoff_logitem) * 2;
778}
779
780/*
781 * Syncing the incore super block changes to disk.
782 * the super block to reflect the changes: sector size
783 */
784STATIC uint
785xfs_calc_sb_reservation(
786 struct xfs_mount *mp)
787{
788 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
789}
790
791void
792xfs_trans_resv_calc(
793 struct xfs_mount *mp,
794 struct xfs_trans_resv *resp)
795{
Jie Liu0eadd102013-08-12 20:49:56 +1000796 /*
797 * The following transactions are logged in physical format and
798 * require a permanent reservation on space.
799 */
800 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
801 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
802 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
803
804 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
805 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
806 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
807
808 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
809 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
810 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
811
812 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
813 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
814 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
815
816 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
817 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
818 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
819
820 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
821 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
822 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
823
824 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
825 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
826 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
827
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800828 resp->tr_create_tmpfile.tr_logres =
829 xfs_calc_create_tmpfile_reservation(mp);
830 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
831 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
832
Jie Liu0eadd102013-08-12 20:49:56 +1000833 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
834 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
835 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
836
837 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
838 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
839 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
840
841 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
842 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
843 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
844
845 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
846 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
847 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
848
849 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
850 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
851 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
852
853 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
854 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
855 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
856
857 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
858 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
859 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
860
861 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
862 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
863 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
864
865 /*
866 * The following transactions are logged in logical format with
867 * a default log count.
868 */
869 resp->tr_qm_sbchange.tr_logres = xfs_calc_qm_sbchange_reservation(mp);
870 resp->tr_qm_sbchange.tr_logcount = XFS_DEFAULT_LOG_COUNT;
871
872 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
873 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
874
875 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
876 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
877
878 resp->tr_qm_equotaoff.tr_logres =
879 xfs_calc_qm_quotaoff_end_reservation(mp);
880 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
881
882 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
883 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
884
885 /* The following transaction are logged in logical format */
886 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
887 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
Jie Liu20996c92013-08-12 20:49:57 +1000888 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu0eadd102013-08-12 20:49:56 +1000889 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
890 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
891 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
892 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
893 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000894}