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