blob: bd3b4b7831b7de45aaff97605b489171af171113 [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/*
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800232 * For adding an inode to unlinked list we can modify:
233 * the agi hash list: sector size
234 * the unlinked inode: inode size
235 */
236STATIC uint
237xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
238{
239 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
240 xfs_calc_inode_res(mp, 1);
241}
242
243/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000244 * For removing a directory entry we can modify:
245 * the parent directory inode: inode size
246 * the removed inode: inode size
247 * the directory btree could join: (max depth + v2) * dir block size
248 * the directory bmap btree could join or split: (max depth + v2) * blocksize
249 * And the bmap_finish transaction can free the dir and bmap blocks giving:
250 * the agf for the ag in which the blocks live: 2 * sector size
251 * the agfl for the ag in which the blocks live: 2 * sector size
252 * the superblock for the free block count: sector size
253 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
254 */
255STATIC uint
256xfs_calc_remove_reservation(
257 struct xfs_mount *mp)
258{
259 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800260 xfs_calc_iunlink_add_reservation(mp) +
261 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000262 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
263 XFS_FSB_TO_B(mp, 1))),
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800264 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000265 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
266 XFS_FSB_TO_B(mp, 1))));
267}
268
269/*
270 * For create, break it in to the two cases that the transaction
271 * covers. We start with the modify case - allocation done by modification
272 * of the state of existing inodes - and the allocation case.
273 */
274
275/*
276 * For create we can modify:
277 * the parent directory inode: inode size
278 * the new inode: inode size
279 * the inode btree entry: block size
280 * the superblock for the nlink flag: sector size
281 * the directory btree: (max depth + v2) * dir block size
282 * the directory inode's bmap btree: (max depth + v2) * block size
283 */
284STATIC uint
285xfs_calc_create_resv_modify(
286 struct xfs_mount *mp)
287{
Dave Chinner23956702013-08-28 16:10:35 +1000288 return xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000289 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
290 (uint)XFS_FSB_TO_B(mp, 1) +
291 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1));
292}
293
294/*
295 * For create we can allocate some inodes giving:
296 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
297 * the superblock for the nlink flag: sector size
298 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
299 * the inode btree: max depth * blocksize
300 * the allocation btrees: 2 trees * (max depth - 1) * block size
301 */
302STATIC uint
303xfs_calc_create_resv_alloc(
304 struct xfs_mount *mp)
305{
306 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
307 mp->m_sb.sb_sectsize +
308 xfs_calc_buf_res(XFS_IALLOC_BLOCKS(mp), XFS_FSB_TO_B(mp, 1)) +
309 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
310 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
311 XFS_FSB_TO_B(mp, 1));
312}
313
314STATIC uint
315__xfs_calc_create_reservation(
316 struct xfs_mount *mp)
317{
318 return XFS_DQUOT_LOGRES(mp) +
319 MAX(xfs_calc_create_resv_alloc(mp),
320 xfs_calc_create_resv_modify(mp));
321}
322
323/*
324 * For icreate we can allocate some inodes giving:
325 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
326 * the superblock for the nlink flag: sector size
327 * the inode btree: max depth * blocksize
328 * the allocation btrees: 2 trees * (max depth - 1) * block size
329 */
330STATIC uint
331xfs_calc_icreate_resv_alloc(
332 struct xfs_mount *mp)
333{
334 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
335 mp->m_sb.sb_sectsize +
336 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
337 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
338 XFS_FSB_TO_B(mp, 1));
339}
340
341STATIC uint
342xfs_calc_icreate_reservation(xfs_mount_t *mp)
343{
344 return XFS_DQUOT_LOGRES(mp) +
345 MAX(xfs_calc_icreate_resv_alloc(mp),
346 xfs_calc_create_resv_modify(mp));
347}
348
349STATIC uint
350xfs_calc_create_reservation(
351 struct xfs_mount *mp)
352{
353 if (xfs_sb_version_hascrc(&mp->m_sb))
354 return xfs_calc_icreate_reservation(mp);
355 return __xfs_calc_create_reservation(mp);
356
357}
358
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800359STATIC uint
360xfs_calc_create_tmpfile_reservation(
361 struct xfs_mount *mp)
362{
363 uint res = XFS_DQUOT_LOGRES(mp);
364
365 if (xfs_sb_version_hascrc(&mp->m_sb))
366 res += xfs_calc_icreate_resv_alloc(mp);
367 else
368 res += xfs_calc_create_resv_alloc(mp);
369
370 return res + xfs_calc_iunlink_add_reservation(mp);
371}
372
Dave Chinner7fd36c42013-08-12 20:49:32 +1000373/*
374 * Making a new directory is the same as creating a new file.
375 */
376STATIC uint
377xfs_calc_mkdir_reservation(
378 struct xfs_mount *mp)
379{
380 return xfs_calc_create_reservation(mp);
381}
382
383
384/*
385 * Making a new symplink is the same as creating a new file, but
386 * with the added blocks for remote symlink data which can be up to 1kB in
387 * length (MAXPATHLEN).
388 */
389STATIC uint
390xfs_calc_symlink_reservation(
391 struct xfs_mount *mp)
392{
393 return xfs_calc_create_reservation(mp) +
394 xfs_calc_buf_res(1, MAXPATHLEN);
395}
396
397/*
398 * In freeing an inode we can modify:
399 * the inode being freed: inode size
400 * the super block free inode counter: sector size
401 * the agi hash list and counters: sector size
402 * the inode btree entry: block size
403 * the on disk inode before ours in the agi hash list: inode cluster size
404 * the inode btree: max depth * blocksize
405 * the allocation btrees: 2 trees * (max depth - 1) * block size
406 */
407STATIC uint
408xfs_calc_ifree_reservation(
409 struct xfs_mount *mp)
410{
411 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000412 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000413 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
414 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
Dave Chinner8f805872013-11-01 15:27:20 +1100415 max_t(uint, XFS_FSB_TO_B(mp, 1), XFS_INODE_CLUSTER_SIZE(mp)) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000416 xfs_calc_buf_res(1, 0) +
417 xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
418 mp->m_in_maxlevels, 0) +
419 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
420 XFS_FSB_TO_B(mp, 1));
421}
422
423/*
424 * When only changing the inode we log the inode and possibly the superblock
425 * We also add a bit of slop for the transaction stuff.
426 */
427STATIC uint
428xfs_calc_ichange_reservation(
429 struct xfs_mount *mp)
430{
431 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000432 xfs_calc_inode_res(mp, 1) +
433 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000434
435}
436
437/*
438 * Growing the data section of the filesystem.
439 * superblock
440 * agi and agf
441 * allocation btrees
442 */
443STATIC uint
444xfs_calc_growdata_reservation(
445 struct xfs_mount *mp)
446{
447 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
448 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
449 XFS_FSB_TO_B(mp, 1));
450}
451
452/*
453 * Growing the rt section of the filesystem.
454 * In the first set of transactions (ALLOC) we allocate space to the
455 * bitmap or summary files.
456 * superblock: sector size
457 * agf of the ag from which the extent is allocated: sector size
458 * bmap btree for bitmap/summary inode: max depth * blocksize
459 * bitmap/summary inode: inode size
460 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
461 */
462STATIC uint
463xfs_calc_growrtalloc_reservation(
464 struct xfs_mount *mp)
465{
466 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
467 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
468 XFS_FSB_TO_B(mp, 1)) +
Dave Chinner23956702013-08-28 16:10:35 +1000469 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000470 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
471 XFS_FSB_TO_B(mp, 1));
472}
473
474/*
475 * Growing the rt section of the filesystem.
476 * In the second set of transactions (ZERO) we zero the new metadata blocks.
477 * one bitmap/summary block: blocksize
478 */
479STATIC uint
480xfs_calc_growrtzero_reservation(
481 struct xfs_mount *mp)
482{
483 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
484}
485
486/*
487 * Growing the rt section of the filesystem.
488 * In the third set of transactions (FREE) we update metadata without
489 * allocating any new blocks.
490 * superblock: sector size
491 * bitmap inode: inode size
492 * summary inode: inode size
493 * one bitmap block: blocksize
494 * summary blocks: new summary size
495 */
496STATIC uint
497xfs_calc_growrtfree_reservation(
498 struct xfs_mount *mp)
499{
500 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner23956702013-08-28 16:10:35 +1000501 xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000502 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
503 xfs_calc_buf_res(1, mp->m_rsumsize);
504}
505
506/*
507 * Logging the inode modification timestamp on a synchronous write.
508 * inode
509 */
510STATIC uint
511xfs_calc_swrite_reservation(
512 struct xfs_mount *mp)
513{
Dave Chinner23956702013-08-28 16:10:35 +1000514 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000515}
516
517/*
518 * Logging the inode mode bits when writing a setuid/setgid file
519 * inode
520 */
521STATIC uint
Dave Chinner23956702013-08-28 16:10:35 +1000522xfs_calc_writeid_reservation(
523 struct xfs_mount *mp)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000524{
Dave Chinner23956702013-08-28 16:10:35 +1000525 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000526}
527
528/*
529 * Converting the inode from non-attributed to attributed.
530 * the inode being converted: inode size
531 * agf block and superblock (for block allocation)
532 * the new block (directory sized)
533 * bmap blocks for the new directory block
534 * allocation btrees
535 */
536STATIC uint
537xfs_calc_addafork_reservation(
538 struct xfs_mount *mp)
539{
540 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000541 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000542 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
543 xfs_calc_buf_res(1, mp->m_dirblksize) +
544 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
545 XFS_FSB_TO_B(mp, 1)) +
546 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
547 XFS_FSB_TO_B(mp, 1));
548}
549
550/*
551 * Removing the attribute fork of a file
552 * the inode being truncated: inode size
553 * the inode's bmap btree: max depth * block size
554 * And the bmap_finish transaction can free the blocks and bmap blocks:
555 * the agf for each of the ags: 4 * sector size
556 * the agfl for each of the ags: 4 * sector size
557 * the super block to reflect the freed blocks: sector size
558 * worst case split in allocation btrees per extent assuming 4 extents:
559 * 4 exts * 2 trees * (2 * max depth - 1) * block size
560 */
561STATIC uint
562xfs_calc_attrinval_reservation(
563 struct xfs_mount *mp)
564{
Dave Chinner23956702013-08-28 16:10:35 +1000565 return MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000566 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
567 XFS_FSB_TO_B(mp, 1))),
568 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
569 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
570 XFS_FSB_TO_B(mp, 1))));
571}
572
573/*
574 * Setting an attribute at mount time.
575 * the inode getting the attribute
576 * the superblock for allocations
577 * the agfs extents are allocated from
578 * the attribute btree * max depth
579 * the inode allocation btree
580 * Since attribute transaction space is dependent on the size of the attribute,
581 * the calculation is done partially at mount time and partially at runtime(see
582 * below).
583 */
584STATIC uint
585xfs_calc_attrsetm_reservation(
586 struct xfs_mount *mp)
587{
588 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000589 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000590 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
591 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
592}
593
594/*
595 * Setting an attribute at runtime, transaction space unit per block.
596 * the superblock for allocations: sector size
597 * the inode bmap btree could join or split: max depth * block size
598 * Since the runtime attribute transaction space is dependent on the total
599 * blocks needed for the 1st bmap, here we calculate out the space unit for
600 * one block so that the caller could figure out the total space according
Jie Liu3d3c8b52013-08-12 20:49:59 +1000601 * to the attibute extent length in blocks by:
602 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000603 */
604STATIC uint
605xfs_calc_attrsetrt_reservation(
606 struct xfs_mount *mp)
607{
608 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
609 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
610 XFS_FSB_TO_B(mp, 1));
611}
612
613/*
614 * Removing an attribute.
615 * the inode: inode size
616 * the attribute btree could join: max depth * block size
617 * the inode bmap btree could join or split: max depth * block size
618 * And the bmap_finish transaction can free the attr blocks freed giving:
619 * the agf for the ag in which the blocks live: 2 * sector size
620 * the agfl for the ag in which the blocks live: 2 * sector size
621 * the superblock for the free block count: sector size
622 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
623 */
624STATIC uint
625xfs_calc_attrrm_reservation(
626 struct xfs_mount *mp)
627{
628 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000629 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000630 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
631 XFS_FSB_TO_B(mp, 1)) +
632 (uint)XFS_FSB_TO_B(mp,
633 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
634 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
635 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
636 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
637 XFS_FSB_TO_B(mp, 1))));
638}
639
640/*
641 * Clearing a bad agino number in an agi hash bucket.
642 */
643STATIC uint
644xfs_calc_clear_agi_bucket_reservation(
645 struct xfs_mount *mp)
646{
647 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
648}
649
650/*
651 * Clearing the quotaflags in the superblock.
652 * the super block for changing quota flags: sector size
653 */
654STATIC uint
655xfs_calc_qm_sbchange_reservation(
656 struct xfs_mount *mp)
657{
658 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
659}
660
661/*
662 * Adjusting quota limits.
663 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
664 */
665STATIC uint
666xfs_calc_qm_setqlim_reservation(
667 struct xfs_mount *mp)
668{
669 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
670}
671
672/*
673 * Allocating quota on disk if needed.
Jie Liu3d3c8b52013-08-12 20:49:59 +1000674 * the write transaction log space: M_RES(mp)->tr_write.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000675 * the unit of quota allocation: one system block size
676 */
677STATIC uint
678xfs_calc_qm_dqalloc_reservation(
679 struct xfs_mount *mp)
680{
Dave Chinner23956702013-08-28 16:10:35 +1000681 ASSERT(M_RES(mp)->tr_write.tr_logres);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000682 return M_RES(mp)->tr_write.tr_logres +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000683 xfs_calc_buf_res(1,
684 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
685}
686
687/*
688 * Turning off quotas.
689 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
690 * the superblock for the quota flags: sector size
691 */
692STATIC uint
693xfs_calc_qm_quotaoff_reservation(
694 struct xfs_mount *mp)
695{
696 return sizeof(struct xfs_qoff_logitem) * 2 +
697 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
698}
699
700/*
701 * End of turning off quotas.
702 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
703 */
704STATIC uint
705xfs_calc_qm_quotaoff_end_reservation(
706 struct xfs_mount *mp)
707{
708 return sizeof(struct xfs_qoff_logitem) * 2;
709}
710
711/*
712 * Syncing the incore super block changes to disk.
713 * the super block to reflect the changes: sector size
714 */
715STATIC uint
716xfs_calc_sb_reservation(
717 struct xfs_mount *mp)
718{
719 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
720}
721
722void
723xfs_trans_resv_calc(
724 struct xfs_mount *mp,
725 struct xfs_trans_resv *resp)
726{
Jie Liu0eadd102013-08-12 20:49:56 +1000727 /*
728 * The following transactions are logged in physical format and
729 * require a permanent reservation on space.
730 */
731 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
732 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
733 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
734
735 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
736 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
737 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
738
739 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
740 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
741 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
742
743 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
744 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
745 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
746
747 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
748 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
749 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
750
751 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
752 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
753 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
754
755 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
756 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
757 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
758
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800759 resp->tr_create_tmpfile.tr_logres =
760 xfs_calc_create_tmpfile_reservation(mp);
761 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
762 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
763
Jie Liu0eadd102013-08-12 20:49:56 +1000764 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
765 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
766 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
767
768 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
769 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
770 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
771
772 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
773 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
774 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
775
776 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
777 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
778 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
779
780 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
781 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
782 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
783
784 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
785 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
786 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
787
788 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
789 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
790 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
791
792 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
793 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
794 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
795
796 /*
797 * The following transactions are logged in logical format with
798 * a default log count.
799 */
800 resp->tr_qm_sbchange.tr_logres = xfs_calc_qm_sbchange_reservation(mp);
801 resp->tr_qm_sbchange.tr_logcount = XFS_DEFAULT_LOG_COUNT;
802
803 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
804 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
805
806 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
807 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
808
809 resp->tr_qm_equotaoff.tr_logres =
810 xfs_calc_qm_quotaoff_end_reservation(mp);
811 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
812
813 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
814 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
815
816 /* The following transaction are logged in logical format */
817 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
818 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
819 resp->tr_swrite.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu20996c92013-08-12 20:49:57 +1000820 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu0eadd102013-08-12 20:49:56 +1000821 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
822 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
823 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
824 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
825 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000826}