blob: d2c8e4a6ee2afdc4a4ea9c625a24fa0c718aee23 [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/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000109 * Various log reservation values.
110 *
111 * These are based on the size of the file system block because that is what
112 * most transactions manipulate. Each adds in an additional 128 bytes per
113 * item logged to try to account for the overhead of the transaction mechanism.
114 *
115 * Note: Most of the reservations underestimate the number of allocation
116 * groups into which they could free extents in the xfs_bmap_finish() call.
117 * This is because the number in the worst case is quite high and quite
118 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
119 * extents in only a single AG at a time. This will require changes to the
120 * EFI code as well, however, so that the EFI for the extents not freed is
121 * logged again in each transaction. See SGI PV #261917.
122 *
123 * Reservation functions here avoid a huge stack in xfs_trans_init due to
124 * register overflow from temporaries in the calculations.
125 */
126
127
128/*
129 * In a write transaction we can allocate a maximum of 2
130 * extents. This gives:
131 * the inode getting the new extents: inode size
132 * the inode's bmap btree: max depth * block size
133 * the agfs of the ags from which the extents are allocated: 2 * sector
134 * the superblock free block counter: sector size
135 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
136 * And the bmap_finish transaction can free bmap blocks in a join:
137 * the agfs of the ags containing the blocks: 2 * sector size
138 * the agfls of the ags containing the blocks: 2 * sector size
139 * the super block free block counter: sector size
140 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
141 */
142STATIC uint
143xfs_calc_write_reservation(
144 struct xfs_mount *mp)
145{
146 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000147 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000148 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
149 XFS_FSB_TO_B(mp, 1)) +
150 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
151 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
152 XFS_FSB_TO_B(mp, 1))),
153 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
154 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
155 XFS_FSB_TO_B(mp, 1))));
156}
157
158/*
159 * In truncating a file we free up to two extents at once. We can modify:
160 * the inode being truncated: inode size
161 * the inode's bmap btree: (max depth + 1) * block size
162 * And the bmap_finish transaction can free the blocks and bmap blocks:
163 * the agf for each of the ags: 4 * sector size
164 * the agfl for each of the ags: 4 * sector size
165 * the super block to reflect the freed blocks: sector size
166 * worst case split in allocation btrees per extent assuming 4 extents:
167 * 4 exts * 2 trees * (2 * max depth - 1) * block size
168 * the inode btree: max depth * blocksize
169 * the allocation btrees: 2 trees * (max depth - 1) * block size
170 */
171STATIC uint
172xfs_calc_itruncate_reservation(
173 struct xfs_mount *mp)
174{
175 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000176 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000177 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
178 XFS_FSB_TO_B(mp, 1))),
179 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
180 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
181 XFS_FSB_TO_B(mp, 1)) +
182 xfs_calc_buf_res(5, 0) +
183 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
184 XFS_FSB_TO_B(mp, 1)) +
Jie Liu126cd102013-12-13 15:51:48 +1100185 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000186 mp->m_in_maxlevels, 0)));
187}
188
189/*
190 * In renaming a files we can modify:
191 * the four inodes involved: 4 * inode size
192 * the two directory btrees: 2 * (max depth + v2) * dir block size
193 * the two directory bmap btrees: 2 * max depth * block size
194 * And the bmap_finish transaction can free dir and bmap blocks (two sets
195 * of bmap blocks) giving:
196 * the agf for the ags in which the blocks live: 3 * sector size
197 * the agfl for the ags in which the blocks live: 3 * sector size
198 * the superblock for the free block count: sector size
199 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
200 */
201STATIC uint
202xfs_calc_rename_reservation(
203 struct xfs_mount *mp)
204{
205 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000206 MAX((xfs_calc_inode_res(mp, 4) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000207 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
208 XFS_FSB_TO_B(mp, 1))),
209 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
210 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
211 XFS_FSB_TO_B(mp, 1))));
212}
213
214/*
215 * For creating a link to an inode:
216 * the parent directory inode: inode size
217 * the linked inode: inode size
218 * the directory btree could split: (max depth + v2) * dir block size
219 * the directory bmap btree could join or split: (max depth + v2) * blocksize
220 * And the bmap_finish transaction can free some bmap blocks giving:
221 * the agf for the ag in which the blocks live: sector size
222 * the agfl for the ag in which the blocks live: sector size
223 * the superblock for the free block count: sector size
224 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
225 */
226STATIC uint
227xfs_calc_link_reservation(
228 struct xfs_mount *mp)
229{
230 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000231 MAX((xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000232 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
233 XFS_FSB_TO_B(mp, 1))),
234 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
235 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
236 XFS_FSB_TO_B(mp, 1))));
237}
238
239/*
240 * For removing a directory entry we can modify:
241 * the parent directory inode: inode size
242 * the removed inode: inode size
243 * the directory btree could join: (max depth + v2) * dir block size
244 * the directory bmap btree could join or split: (max depth + v2) * blocksize
245 * And the bmap_finish transaction can free the dir and bmap blocks giving:
246 * the agf for the ag in which the blocks live: 2 * sector size
247 * the agfl for the ag in which the blocks live: 2 * sector size
248 * the superblock for the free block count: sector size
249 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
250 */
251STATIC uint
252xfs_calc_remove_reservation(
253 struct xfs_mount *mp)
254{
255 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000256 MAX((xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000257 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
258 XFS_FSB_TO_B(mp, 1))),
259 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
260 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
261 XFS_FSB_TO_B(mp, 1))));
262}
263
264/*
265 * For create, break it in to the two cases that the transaction
266 * covers. We start with the modify case - allocation done by modification
267 * of the state of existing inodes - and the allocation case.
268 */
269
270/*
271 * For create we can modify:
272 * the parent directory inode: inode size
273 * the new inode: inode size
274 * the inode btree entry: block size
275 * the superblock for the nlink flag: sector size
276 * the directory btree: (max depth + v2) * dir block size
277 * the directory inode's bmap btree: (max depth + v2) * block size
278 */
279STATIC uint
280xfs_calc_create_resv_modify(
281 struct xfs_mount *mp)
282{
Dave Chinner23956702013-08-28 16:10:35 +1000283 return xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000284 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
285 (uint)XFS_FSB_TO_B(mp, 1) +
286 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1));
287}
288
289/*
290 * For create we can allocate some inodes giving:
291 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
292 * the superblock for the nlink flag: sector size
Jie Liu126cd102013-12-13 15:51:48 +1100293 * the inode blocks allocated: mp->m_ialloc_blks * blocksize
Dave Chinner7fd36c42013-08-12 20:49:32 +1000294 * the inode btree: max depth * blocksize
295 * the allocation btrees: 2 trees * (max depth - 1) * block size
296 */
297STATIC uint
298xfs_calc_create_resv_alloc(
299 struct xfs_mount *mp)
300{
301 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
302 mp->m_sb.sb_sectsize +
Jie Liu126cd102013-12-13 15:51:48 +1100303 xfs_calc_buf_res(mp->m_ialloc_blks, XFS_FSB_TO_B(mp, 1)) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000304 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
305 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
306 XFS_FSB_TO_B(mp, 1));
307}
308
309STATIC uint
310__xfs_calc_create_reservation(
311 struct xfs_mount *mp)
312{
313 return XFS_DQUOT_LOGRES(mp) +
314 MAX(xfs_calc_create_resv_alloc(mp),
315 xfs_calc_create_resv_modify(mp));
316}
317
318/*
319 * For icreate we can allocate some inodes giving:
320 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
321 * the superblock for the nlink flag: sector size
322 * the inode btree: max depth * blocksize
323 * the allocation btrees: 2 trees * (max depth - 1) * block size
324 */
325STATIC uint
326xfs_calc_icreate_resv_alloc(
327 struct xfs_mount *mp)
328{
329 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
330 mp->m_sb.sb_sectsize +
331 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
332 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
333 XFS_FSB_TO_B(mp, 1));
334}
335
336STATIC uint
337xfs_calc_icreate_reservation(xfs_mount_t *mp)
338{
339 return XFS_DQUOT_LOGRES(mp) +
340 MAX(xfs_calc_icreate_resv_alloc(mp),
341 xfs_calc_create_resv_modify(mp));
342}
343
344STATIC uint
345xfs_calc_create_reservation(
346 struct xfs_mount *mp)
347{
348 if (xfs_sb_version_hascrc(&mp->m_sb))
349 return xfs_calc_icreate_reservation(mp);
350 return __xfs_calc_create_reservation(mp);
351
352}
353
354/*
355 * Making a new directory is the same as creating a new file.
356 */
357STATIC uint
358xfs_calc_mkdir_reservation(
359 struct xfs_mount *mp)
360{
361 return xfs_calc_create_reservation(mp);
362}
363
364
365/*
366 * Making a new symplink is the same as creating a new file, but
367 * with the added blocks for remote symlink data which can be up to 1kB in
368 * length (MAXPATHLEN).
369 */
370STATIC uint
371xfs_calc_symlink_reservation(
372 struct xfs_mount *mp)
373{
374 return xfs_calc_create_reservation(mp) +
375 xfs_calc_buf_res(1, MAXPATHLEN);
376}
377
378/*
379 * In freeing an inode we can modify:
380 * the inode being freed: inode size
381 * the super block free inode counter: sector size
382 * the agi hash list and counters: sector size
383 * the inode btree entry: block size
384 * the on disk inode before ours in the agi hash list: inode cluster size
385 * the inode btree: max depth * blocksize
386 * the allocation btrees: 2 trees * (max depth - 1) * block size
387 */
388STATIC uint
389xfs_calc_ifree_reservation(
390 struct xfs_mount *mp)
391{
392 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000393 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000394 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
395 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
Jie Liu0f49efd2013-12-13 15:51:48 +1100396 max_t(uint, XFS_FSB_TO_B(mp, 1), mp->m_inode_cluster_size) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000397 xfs_calc_buf_res(1, 0) +
Jie Liu126cd102013-12-13 15:51:48 +1100398 xfs_calc_buf_res(2 + mp->m_ialloc_blks +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000399 mp->m_in_maxlevels, 0) +
400 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
401 XFS_FSB_TO_B(mp, 1));
402}
403
404/*
405 * When only changing the inode we log the inode and possibly the superblock
406 * We also add a bit of slop for the transaction stuff.
407 */
408STATIC uint
409xfs_calc_ichange_reservation(
410 struct xfs_mount *mp)
411{
412 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000413 xfs_calc_inode_res(mp, 1) +
414 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000415
416}
417
418/*
419 * Growing the data section of the filesystem.
420 * superblock
421 * agi and agf
422 * allocation btrees
423 */
424STATIC uint
425xfs_calc_growdata_reservation(
426 struct xfs_mount *mp)
427{
428 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
429 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
430 XFS_FSB_TO_B(mp, 1));
431}
432
433/*
434 * Growing the rt section of the filesystem.
435 * In the first set of transactions (ALLOC) we allocate space to the
436 * bitmap or summary files.
437 * superblock: sector size
438 * agf of the ag from which the extent is allocated: sector size
439 * bmap btree for bitmap/summary inode: max depth * blocksize
440 * bitmap/summary inode: inode size
441 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
442 */
443STATIC uint
444xfs_calc_growrtalloc_reservation(
445 struct xfs_mount *mp)
446{
447 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
448 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
449 XFS_FSB_TO_B(mp, 1)) +
Dave Chinner23956702013-08-28 16:10:35 +1000450 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000451 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
452 XFS_FSB_TO_B(mp, 1));
453}
454
455/*
456 * Growing the rt section of the filesystem.
457 * In the second set of transactions (ZERO) we zero the new metadata blocks.
458 * one bitmap/summary block: blocksize
459 */
460STATIC uint
461xfs_calc_growrtzero_reservation(
462 struct xfs_mount *mp)
463{
464 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
465}
466
467/*
468 * Growing the rt section of the filesystem.
469 * In the third set of transactions (FREE) we update metadata without
470 * allocating any new blocks.
471 * superblock: sector size
472 * bitmap inode: inode size
473 * summary inode: inode size
474 * one bitmap block: blocksize
475 * summary blocks: new summary size
476 */
477STATIC uint
478xfs_calc_growrtfree_reservation(
479 struct xfs_mount *mp)
480{
481 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner23956702013-08-28 16:10:35 +1000482 xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000483 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
484 xfs_calc_buf_res(1, mp->m_rsumsize);
485}
486
487/*
488 * Logging the inode modification timestamp on a synchronous write.
489 * inode
490 */
491STATIC uint
492xfs_calc_swrite_reservation(
493 struct xfs_mount *mp)
494{
Dave Chinner23956702013-08-28 16:10:35 +1000495 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000496}
497
498/*
499 * Logging the inode mode bits when writing a setuid/setgid file
500 * inode
501 */
502STATIC uint
Dave Chinner23956702013-08-28 16:10:35 +1000503xfs_calc_writeid_reservation(
504 struct xfs_mount *mp)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000505{
Dave Chinner23956702013-08-28 16:10:35 +1000506 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000507}
508
509/*
510 * Converting the inode from non-attributed to attributed.
511 * the inode being converted: inode size
512 * agf block and superblock (for block allocation)
513 * the new block (directory sized)
514 * bmap blocks for the new directory block
515 * allocation btrees
516 */
517STATIC uint
518xfs_calc_addafork_reservation(
519 struct xfs_mount *mp)
520{
521 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000522 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000523 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
524 xfs_calc_buf_res(1, mp->m_dirblksize) +
525 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
526 XFS_FSB_TO_B(mp, 1)) +
527 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
528 XFS_FSB_TO_B(mp, 1));
529}
530
531/*
532 * Removing the attribute fork of a file
533 * the inode being truncated: inode size
534 * the inode's bmap btree: max depth * block size
535 * And the bmap_finish transaction can free the blocks and bmap blocks:
536 * the agf for each of the ags: 4 * sector size
537 * the agfl for each of the ags: 4 * sector size
538 * the super block to reflect the freed blocks: sector size
539 * worst case split in allocation btrees per extent assuming 4 extents:
540 * 4 exts * 2 trees * (2 * max depth - 1) * block size
541 */
542STATIC uint
543xfs_calc_attrinval_reservation(
544 struct xfs_mount *mp)
545{
Dave Chinner23956702013-08-28 16:10:35 +1000546 return MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000547 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
548 XFS_FSB_TO_B(mp, 1))),
549 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
550 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
551 XFS_FSB_TO_B(mp, 1))));
552}
553
554/*
555 * Setting an attribute at mount time.
556 * the inode getting the attribute
557 * the superblock for allocations
558 * the agfs extents are allocated from
559 * the attribute btree * max depth
560 * the inode allocation btree
561 * Since attribute transaction space is dependent on the size of the attribute,
562 * the calculation is done partially at mount time and partially at runtime(see
563 * below).
564 */
565STATIC uint
566xfs_calc_attrsetm_reservation(
567 struct xfs_mount *mp)
568{
569 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000570 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000571 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
572 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
573}
574
575/*
576 * Setting an attribute at runtime, transaction space unit per block.
577 * the superblock for allocations: sector size
578 * the inode bmap btree could join or split: max depth * block size
579 * Since the runtime attribute transaction space is dependent on the total
580 * blocks needed for the 1st bmap, here we calculate out the space unit for
581 * one block so that the caller could figure out the total space according
Jie Liu3d3c8b52013-08-12 20:49:59 +1000582 * to the attibute extent length in blocks by:
583 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000584 */
585STATIC uint
586xfs_calc_attrsetrt_reservation(
587 struct xfs_mount *mp)
588{
589 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
590 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
591 XFS_FSB_TO_B(mp, 1));
592}
593
594/*
595 * Removing an attribute.
596 * the inode: inode size
597 * the attribute btree could join: max depth * block size
598 * the inode bmap btree could join or split: max depth * block size
599 * And the bmap_finish transaction can free the attr blocks freed giving:
600 * the agf for the ag in which the blocks live: 2 * sector size
601 * the agfl for the ag in which the blocks live: 2 * sector size
602 * the superblock for the free block count: sector size
603 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
604 */
605STATIC uint
606xfs_calc_attrrm_reservation(
607 struct xfs_mount *mp)
608{
609 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000610 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000611 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
612 XFS_FSB_TO_B(mp, 1)) +
613 (uint)XFS_FSB_TO_B(mp,
614 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
615 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
616 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
617 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
618 XFS_FSB_TO_B(mp, 1))));
619}
620
621/*
622 * Clearing a bad agino number in an agi hash bucket.
623 */
624STATIC uint
625xfs_calc_clear_agi_bucket_reservation(
626 struct xfs_mount *mp)
627{
628 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
629}
630
631/*
632 * Clearing the quotaflags in the superblock.
633 * the super block for changing quota flags: sector size
634 */
635STATIC uint
636xfs_calc_qm_sbchange_reservation(
637 struct xfs_mount *mp)
638{
639 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
640}
641
642/*
643 * Adjusting quota limits.
644 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
645 */
646STATIC uint
647xfs_calc_qm_setqlim_reservation(
648 struct xfs_mount *mp)
649{
650 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
651}
652
653/*
654 * Allocating quota on disk if needed.
Brian Foster410b11a2014-02-07 14:55:54 +1100655 * the write transaction log space for quota file extent allocation
Dave Chinner7fd36c42013-08-12 20:49:32 +1000656 * the unit of quota allocation: one system block size
657 */
658STATIC uint
659xfs_calc_qm_dqalloc_reservation(
660 struct xfs_mount *mp)
661{
Brian Foster410b11a2014-02-07 14:55:54 +1100662 return xfs_calc_write_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000663 xfs_calc_buf_res(1,
664 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
665}
666
667/*
668 * Turning off quotas.
669 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
670 * the superblock for the quota flags: sector size
671 */
672STATIC uint
673xfs_calc_qm_quotaoff_reservation(
674 struct xfs_mount *mp)
675{
676 return sizeof(struct xfs_qoff_logitem) * 2 +
677 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
678}
679
680/*
681 * End of turning off quotas.
682 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
683 */
684STATIC uint
685xfs_calc_qm_quotaoff_end_reservation(
686 struct xfs_mount *mp)
687{
688 return sizeof(struct xfs_qoff_logitem) * 2;
689}
690
691/*
692 * Syncing the incore super block changes to disk.
693 * the super block to reflect the changes: sector size
694 */
695STATIC uint
696xfs_calc_sb_reservation(
697 struct xfs_mount *mp)
698{
699 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
700}
701
702void
703xfs_trans_resv_calc(
704 struct xfs_mount *mp,
705 struct xfs_trans_resv *resp)
706{
Jie Liu0eadd102013-08-12 20:49:56 +1000707 /*
708 * The following transactions are logged in physical format and
709 * require a permanent reservation on space.
710 */
711 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
712 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
713 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
714
715 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
716 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
717 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
718
719 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
720 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
721 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
722
723 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
724 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
725 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
726
727 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
728 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
729 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
730
731 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
732 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
733 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
734
735 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
736 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
737 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
738
739 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
740 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
741 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
742
743 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
744 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
745 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
746
747 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
748 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
749 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
750
751 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
752 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
753 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
754
755 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
756 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
757 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
758
759 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
760 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
761 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
762
763 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
764 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
765 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
766
767 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
768 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
769 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
770
771 /*
772 * The following transactions are logged in logical format with
773 * a default log count.
774 */
775 resp->tr_qm_sbchange.tr_logres = xfs_calc_qm_sbchange_reservation(mp);
776 resp->tr_qm_sbchange.tr_logcount = XFS_DEFAULT_LOG_COUNT;
777
778 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
779 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
780
781 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
782 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
783
784 resp->tr_qm_equotaoff.tr_logres =
785 xfs_calc_qm_quotaoff_end_reservation(mp);
786 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
787
788 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
789 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
790
791 /* The following transaction are logged in logical format */
792 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
793 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
Jie Liu20996c92013-08-12 20:49:57 +1000794 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu0eadd102013-08-12 20:49:56 +1000795 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
796 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
797 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
798 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
799 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000800}