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