blob: 76f9a02bc36be06a36003a16d3268f5710e5225c [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/*
Zhi Yong Wuab297432013-12-18 08:22:41 +0800207 * For removing an inode from unlinked list at first, we can modify:
208 * the agi hash list and counters: sector size
209 * the on disk inode before ours in the agi hash list: inode cluster size
210 */
211STATIC uint
212xfs_calc_iunlink_remove_reservation(
213 struct xfs_mount *mp)
214{
215 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
216 MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
217 (__uint16_t)XFS_INODE_CLUSTER_SIZE(mp));
218}
219
220/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000221 * For creating a link to an inode:
222 * the parent directory inode: inode size
223 * the linked inode: inode size
224 * the directory btree could split: (max depth + v2) * dir block size
225 * the directory bmap btree could join or split: (max depth + v2) * blocksize
226 * And the bmap_finish transaction can free some bmap blocks giving:
227 * the agf for the ag in which the blocks live: sector size
228 * the agfl for the ag in which the blocks live: sector size
229 * the superblock for the free block count: sector size
230 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
231 */
232STATIC uint
233xfs_calc_link_reservation(
234 struct xfs_mount *mp)
235{
236 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800237 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000238 MAX((xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000239 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
240 XFS_FSB_TO_B(mp, 1))),
241 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
242 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
243 XFS_FSB_TO_B(mp, 1))));
244}
245
246/*
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800247 * For adding an inode to unlinked list we can modify:
248 * the agi hash list: sector size
249 * the unlinked inode: inode size
250 */
251STATIC uint
252xfs_calc_iunlink_add_reservation(xfs_mount_t *mp)
253{
254 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
255 xfs_calc_inode_res(mp, 1);
256}
257
258/*
Dave Chinner7fd36c42013-08-12 20:49:32 +1000259 * For removing a directory entry we can modify:
260 * the parent directory inode: inode size
261 * the removed inode: inode size
262 * the directory btree could join: (max depth + v2) * dir block size
263 * the directory bmap btree could join or split: (max depth + v2) * blocksize
264 * And the bmap_finish transaction can free the dir and bmap blocks giving:
265 * the agf for the ag in which the blocks live: 2 * sector size
266 * the agfl for the ag in which the blocks live: 2 * sector size
267 * the superblock for the free block count: sector size
268 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
269 */
270STATIC uint
271xfs_calc_remove_reservation(
272 struct xfs_mount *mp)
273{
274 return XFS_DQUOT_LOGRES(mp) +
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800275 xfs_calc_iunlink_add_reservation(mp) +
276 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000277 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
278 XFS_FSB_TO_B(mp, 1))),
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800279 (xfs_calc_buf_res(4, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000280 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
281 XFS_FSB_TO_B(mp, 1))));
282}
283
284/*
285 * For create, break it in to the two cases that the transaction
286 * covers. We start with the modify case - allocation done by modification
287 * of the state of existing inodes - and the allocation case.
288 */
289
290/*
291 * For create we can modify:
292 * the parent directory inode: inode size
293 * the new inode: inode size
294 * the inode btree entry: block size
295 * the superblock for the nlink flag: sector size
296 * the directory btree: (max depth + v2) * dir block size
297 * the directory inode's bmap btree: (max depth + v2) * block size
298 */
299STATIC uint
300xfs_calc_create_resv_modify(
301 struct xfs_mount *mp)
302{
Dave Chinner23956702013-08-28 16:10:35 +1000303 return xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000304 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
305 (uint)XFS_FSB_TO_B(mp, 1) +
306 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp), XFS_FSB_TO_B(mp, 1));
307}
308
309/*
310 * For create we can allocate some inodes giving:
311 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
312 * the superblock for the nlink flag: sector size
313 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
314 * the inode btree: max depth * blocksize
315 * the allocation btrees: 2 trees * (max depth - 1) * block size
316 */
317STATIC uint
318xfs_calc_create_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(XFS_IALLOC_BLOCKS(mp), XFS_FSB_TO_B(mp, 1)) +
324 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
325 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
326 XFS_FSB_TO_B(mp, 1));
327}
328
329STATIC uint
330__xfs_calc_create_reservation(
331 struct xfs_mount *mp)
332{
333 return XFS_DQUOT_LOGRES(mp) +
334 MAX(xfs_calc_create_resv_alloc(mp),
335 xfs_calc_create_resv_modify(mp));
336}
337
338/*
339 * For icreate we can allocate some inodes giving:
340 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
341 * the superblock for the nlink flag: sector size
342 * the inode btree: max depth * blocksize
343 * the allocation btrees: 2 trees * (max depth - 1) * block size
344 */
345STATIC uint
346xfs_calc_icreate_resv_alloc(
347 struct xfs_mount *mp)
348{
349 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
350 mp->m_sb.sb_sectsize +
351 xfs_calc_buf_res(mp->m_in_maxlevels, XFS_FSB_TO_B(mp, 1)) +
352 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
353 XFS_FSB_TO_B(mp, 1));
354}
355
356STATIC uint
357xfs_calc_icreate_reservation(xfs_mount_t *mp)
358{
359 return XFS_DQUOT_LOGRES(mp) +
360 MAX(xfs_calc_icreate_resv_alloc(mp),
361 xfs_calc_create_resv_modify(mp));
362}
363
364STATIC uint
365xfs_calc_create_reservation(
366 struct xfs_mount *mp)
367{
368 if (xfs_sb_version_hascrc(&mp->m_sb))
369 return xfs_calc_icreate_reservation(mp);
370 return __xfs_calc_create_reservation(mp);
371
372}
373
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800374STATIC uint
375xfs_calc_create_tmpfile_reservation(
376 struct xfs_mount *mp)
377{
378 uint res = XFS_DQUOT_LOGRES(mp);
379
380 if (xfs_sb_version_hascrc(&mp->m_sb))
381 res += xfs_calc_icreate_resv_alloc(mp);
382 else
383 res += xfs_calc_create_resv_alloc(mp);
384
385 return res + xfs_calc_iunlink_add_reservation(mp);
386}
387
Dave Chinner7fd36c42013-08-12 20:49:32 +1000388/*
389 * Making a new directory is the same as creating a new file.
390 */
391STATIC uint
392xfs_calc_mkdir_reservation(
393 struct xfs_mount *mp)
394{
395 return xfs_calc_create_reservation(mp);
396}
397
398
399/*
400 * Making a new symplink is the same as creating a new file, but
401 * with the added blocks for remote symlink data which can be up to 1kB in
402 * length (MAXPATHLEN).
403 */
404STATIC uint
405xfs_calc_symlink_reservation(
406 struct xfs_mount *mp)
407{
408 return xfs_calc_create_reservation(mp) +
409 xfs_calc_buf_res(1, MAXPATHLEN);
410}
411
412/*
413 * In freeing an inode we can modify:
414 * the inode being freed: inode size
415 * the super block free inode counter: sector size
416 * the agi hash list and counters: sector size
417 * the inode btree entry: block size
418 * the on disk inode before ours in the agi hash list: inode cluster size
419 * the inode btree: max depth * blocksize
420 * the allocation btrees: 2 trees * (max depth - 1) * block size
421 */
422STATIC uint
423xfs_calc_ifree_reservation(
424 struct xfs_mount *mp)
425{
426 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000427 xfs_calc_inode_res(mp, 1) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800428 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000429 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
Zhi Yong Wuab297432013-12-18 08:22:41 +0800430 xfs_calc_iunlink_remove_reservation(mp) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000431 xfs_calc_buf_res(1, 0) +
432 xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
433 mp->m_in_maxlevels, 0) +
434 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
435 XFS_FSB_TO_B(mp, 1));
436}
437
438/*
439 * When only changing the inode we log the inode and possibly the superblock
440 * We also add a bit of slop for the transaction stuff.
441 */
442STATIC uint
443xfs_calc_ichange_reservation(
444 struct xfs_mount *mp)
445{
446 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000447 xfs_calc_inode_res(mp, 1) +
448 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000449
450}
451
452/*
453 * Growing the data section of the filesystem.
454 * superblock
455 * agi and agf
456 * allocation btrees
457 */
458STATIC uint
459xfs_calc_growdata_reservation(
460 struct xfs_mount *mp)
461{
462 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
463 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
464 XFS_FSB_TO_B(mp, 1));
465}
466
467/*
468 * Growing the rt section of the filesystem.
469 * In the first set of transactions (ALLOC) we allocate space to the
470 * bitmap or summary files.
471 * superblock: sector size
472 * agf of the ag from which the extent is allocated: sector size
473 * bmap btree for bitmap/summary inode: max depth * blocksize
474 * bitmap/summary inode: inode size
475 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
476 */
477STATIC uint
478xfs_calc_growrtalloc_reservation(
479 struct xfs_mount *mp)
480{
481 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
482 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
483 XFS_FSB_TO_B(mp, 1)) +
Dave Chinner23956702013-08-28 16:10:35 +1000484 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000485 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
486 XFS_FSB_TO_B(mp, 1));
487}
488
489/*
490 * Growing the rt section of the filesystem.
491 * In the second set of transactions (ZERO) we zero the new metadata blocks.
492 * one bitmap/summary block: blocksize
493 */
494STATIC uint
495xfs_calc_growrtzero_reservation(
496 struct xfs_mount *mp)
497{
498 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
499}
500
501/*
502 * Growing the rt section of the filesystem.
503 * In the third set of transactions (FREE) we update metadata without
504 * allocating any new blocks.
505 * superblock: sector size
506 * bitmap inode: inode size
507 * summary inode: inode size
508 * one bitmap block: blocksize
509 * summary blocks: new summary size
510 */
511STATIC uint
512xfs_calc_growrtfree_reservation(
513 struct xfs_mount *mp)
514{
515 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
Dave Chinner23956702013-08-28 16:10:35 +1000516 xfs_calc_inode_res(mp, 2) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000517 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
518 xfs_calc_buf_res(1, mp->m_rsumsize);
519}
520
521/*
522 * Logging the inode modification timestamp on a synchronous write.
523 * inode
524 */
525STATIC uint
526xfs_calc_swrite_reservation(
527 struct xfs_mount *mp)
528{
Dave Chinner23956702013-08-28 16:10:35 +1000529 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000530}
531
532/*
533 * Logging the inode mode bits when writing a setuid/setgid file
534 * inode
535 */
536STATIC uint
Dave Chinner23956702013-08-28 16:10:35 +1000537xfs_calc_writeid_reservation(
538 struct xfs_mount *mp)
Dave Chinner7fd36c42013-08-12 20:49:32 +1000539{
Dave Chinner23956702013-08-28 16:10:35 +1000540 return xfs_calc_inode_res(mp, 1);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000541}
542
543/*
544 * Converting the inode from non-attributed to attributed.
545 * the inode being converted: inode size
546 * agf block and superblock (for block allocation)
547 * the new block (directory sized)
548 * bmap blocks for the new directory block
549 * allocation btrees
550 */
551STATIC uint
552xfs_calc_addafork_reservation(
553 struct xfs_mount *mp)
554{
555 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000556 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000557 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
558 xfs_calc_buf_res(1, mp->m_dirblksize) +
559 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
560 XFS_FSB_TO_B(mp, 1)) +
561 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
562 XFS_FSB_TO_B(mp, 1));
563}
564
565/*
566 * Removing the attribute fork of a file
567 * the inode being truncated: inode size
568 * the inode's bmap btree: max depth * block size
569 * And the bmap_finish transaction can free the blocks and bmap blocks:
570 * the agf for each of the ags: 4 * sector size
571 * the agfl for each of the ags: 4 * sector size
572 * the super block to reflect the freed blocks: sector size
573 * worst case split in allocation btrees per extent assuming 4 extents:
574 * 4 exts * 2 trees * (2 * max depth - 1) * block size
575 */
576STATIC uint
577xfs_calc_attrinval_reservation(
578 struct xfs_mount *mp)
579{
Dave Chinner23956702013-08-28 16:10:35 +1000580 return MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000581 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
582 XFS_FSB_TO_B(mp, 1))),
583 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
584 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
585 XFS_FSB_TO_B(mp, 1))));
586}
587
588/*
589 * Setting an attribute at mount time.
590 * the inode getting the attribute
591 * the superblock for allocations
592 * the agfs extents are allocated from
593 * the attribute btree * max depth
594 * the inode allocation btree
595 * Since attribute transaction space is dependent on the size of the attribute,
596 * the calculation is done partially at mount time and partially at runtime(see
597 * below).
598 */
599STATIC uint
600xfs_calc_attrsetm_reservation(
601 struct xfs_mount *mp)
602{
603 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000604 xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000605 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
606 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
607}
608
609/*
610 * Setting an attribute at runtime, transaction space unit per block.
611 * the superblock for allocations: sector size
612 * the inode bmap btree could join or split: max depth * block size
613 * Since the runtime attribute transaction space is dependent on the total
614 * blocks needed for the 1st bmap, here we calculate out the space unit for
615 * one block so that the caller could figure out the total space according
Jie Liu3d3c8b52013-08-12 20:49:59 +1000616 * to the attibute extent length in blocks by:
617 * ext * M_RES(mp)->tr_attrsetrt.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000618 */
619STATIC uint
620xfs_calc_attrsetrt_reservation(
621 struct xfs_mount *mp)
622{
623 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
624 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
625 XFS_FSB_TO_B(mp, 1));
626}
627
628/*
629 * Removing an attribute.
630 * the inode: inode size
631 * the attribute btree could join: max depth * block size
632 * the inode bmap btree could join or split: max depth * block size
633 * And the bmap_finish transaction can free the attr blocks freed giving:
634 * the agf for the ag in which the blocks live: 2 * sector size
635 * the agfl for the ag in which the blocks live: 2 * sector size
636 * the superblock for the free block count: sector size
637 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
638 */
639STATIC uint
640xfs_calc_attrrm_reservation(
641 struct xfs_mount *mp)
642{
643 return XFS_DQUOT_LOGRES(mp) +
Dave Chinner23956702013-08-28 16:10:35 +1000644 MAX((xfs_calc_inode_res(mp, 1) +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000645 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
646 XFS_FSB_TO_B(mp, 1)) +
647 (uint)XFS_FSB_TO_B(mp,
648 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
649 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
650 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
651 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
652 XFS_FSB_TO_B(mp, 1))));
653}
654
655/*
656 * Clearing a bad agino number in an agi hash bucket.
657 */
658STATIC uint
659xfs_calc_clear_agi_bucket_reservation(
660 struct xfs_mount *mp)
661{
662 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
663}
664
665/*
666 * Clearing the quotaflags in the superblock.
667 * the super block for changing quota flags: sector size
668 */
669STATIC uint
670xfs_calc_qm_sbchange_reservation(
671 struct xfs_mount *mp)
672{
673 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
674}
675
676/*
677 * Adjusting quota limits.
678 * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
679 */
680STATIC uint
681xfs_calc_qm_setqlim_reservation(
682 struct xfs_mount *mp)
683{
684 return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
685}
686
687/*
688 * Allocating quota on disk if needed.
Jie Liu3d3c8b52013-08-12 20:49:59 +1000689 * the write transaction log space: M_RES(mp)->tr_write.tr_logres
Dave Chinner7fd36c42013-08-12 20:49:32 +1000690 * the unit of quota allocation: one system block size
691 */
692STATIC uint
693xfs_calc_qm_dqalloc_reservation(
694 struct xfs_mount *mp)
695{
Dave Chinner23956702013-08-28 16:10:35 +1000696 ASSERT(M_RES(mp)->tr_write.tr_logres);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000697 return M_RES(mp)->tr_write.tr_logres +
Dave Chinner7fd36c42013-08-12 20:49:32 +1000698 xfs_calc_buf_res(1,
699 XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
700}
701
702/*
703 * Turning off quotas.
704 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
705 * the superblock for the quota flags: sector size
706 */
707STATIC uint
708xfs_calc_qm_quotaoff_reservation(
709 struct xfs_mount *mp)
710{
711 return sizeof(struct xfs_qoff_logitem) * 2 +
712 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
713}
714
715/*
716 * End of turning off quotas.
717 * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
718 */
719STATIC uint
720xfs_calc_qm_quotaoff_end_reservation(
721 struct xfs_mount *mp)
722{
723 return sizeof(struct xfs_qoff_logitem) * 2;
724}
725
726/*
727 * Syncing the incore super block changes to disk.
728 * the super block to reflect the changes: sector size
729 */
730STATIC uint
731xfs_calc_sb_reservation(
732 struct xfs_mount *mp)
733{
734 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
735}
736
737void
738xfs_trans_resv_calc(
739 struct xfs_mount *mp,
740 struct xfs_trans_resv *resp)
741{
Jie Liu0eadd102013-08-12 20:49:56 +1000742 /*
743 * The following transactions are logged in physical format and
744 * require a permanent reservation on space.
745 */
746 resp->tr_write.tr_logres = xfs_calc_write_reservation(mp);
747 resp->tr_write.tr_logcount = XFS_WRITE_LOG_COUNT;
748 resp->tr_write.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
749
750 resp->tr_itruncate.tr_logres = xfs_calc_itruncate_reservation(mp);
751 resp->tr_itruncate.tr_logcount = XFS_ITRUNCATE_LOG_COUNT;
752 resp->tr_itruncate.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
753
754 resp->tr_rename.tr_logres = xfs_calc_rename_reservation(mp);
755 resp->tr_rename.tr_logcount = XFS_RENAME_LOG_COUNT;
756 resp->tr_rename.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
757
758 resp->tr_link.tr_logres = xfs_calc_link_reservation(mp);
759 resp->tr_link.tr_logcount = XFS_LINK_LOG_COUNT;
760 resp->tr_link.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
761
762 resp->tr_remove.tr_logres = xfs_calc_remove_reservation(mp);
763 resp->tr_remove.tr_logcount = XFS_REMOVE_LOG_COUNT;
764 resp->tr_remove.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
765
766 resp->tr_symlink.tr_logres = xfs_calc_symlink_reservation(mp);
767 resp->tr_symlink.tr_logcount = XFS_SYMLINK_LOG_COUNT;
768 resp->tr_symlink.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
769
770 resp->tr_create.tr_logres = xfs_calc_create_reservation(mp);
771 resp->tr_create.tr_logcount = XFS_CREATE_LOG_COUNT;
772 resp->tr_create.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
773
Zhi Yong Wu99b64362013-12-18 08:22:40 +0800774 resp->tr_create_tmpfile.tr_logres =
775 xfs_calc_create_tmpfile_reservation(mp);
776 resp->tr_create_tmpfile.tr_logcount = XFS_CREATE_TMPFILE_LOG_COUNT;
777 resp->tr_create_tmpfile.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
778
Jie Liu0eadd102013-08-12 20:49:56 +1000779 resp->tr_mkdir.tr_logres = xfs_calc_mkdir_reservation(mp);
780 resp->tr_mkdir.tr_logcount = XFS_MKDIR_LOG_COUNT;
781 resp->tr_mkdir.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
782
783 resp->tr_ifree.tr_logres = xfs_calc_ifree_reservation(mp);
784 resp->tr_ifree.tr_logcount = XFS_INACTIVE_LOG_COUNT;
785 resp->tr_ifree.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
786
787 resp->tr_addafork.tr_logres = xfs_calc_addafork_reservation(mp);
788 resp->tr_addafork.tr_logcount = XFS_ADDAFORK_LOG_COUNT;
789 resp->tr_addafork.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
790
791 resp->tr_attrinval.tr_logres = xfs_calc_attrinval_reservation(mp);
792 resp->tr_attrinval.tr_logcount = XFS_ATTRINVAL_LOG_COUNT;
793 resp->tr_attrinval.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
794
795 resp->tr_attrsetm.tr_logres = xfs_calc_attrsetm_reservation(mp);
796 resp->tr_attrsetm.tr_logcount = XFS_ATTRSET_LOG_COUNT;
797 resp->tr_attrsetm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
798
799 resp->tr_attrrm.tr_logres = xfs_calc_attrrm_reservation(mp);
800 resp->tr_attrrm.tr_logcount = XFS_ATTRRM_LOG_COUNT;
801 resp->tr_attrrm.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
802
803 resp->tr_growrtalloc.tr_logres = xfs_calc_growrtalloc_reservation(mp);
804 resp->tr_growrtalloc.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
805 resp->tr_growrtalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
806
807 resp->tr_qm_dqalloc.tr_logres = xfs_calc_qm_dqalloc_reservation(mp);
808 resp->tr_qm_dqalloc.tr_logcount = XFS_WRITE_LOG_COUNT;
809 resp->tr_qm_dqalloc.tr_logflags |= XFS_TRANS_PERM_LOG_RES;
810
811 /*
812 * The following transactions are logged in logical format with
813 * a default log count.
814 */
815 resp->tr_qm_sbchange.tr_logres = xfs_calc_qm_sbchange_reservation(mp);
816 resp->tr_qm_sbchange.tr_logcount = XFS_DEFAULT_LOG_COUNT;
817
818 resp->tr_qm_setqlim.tr_logres = xfs_calc_qm_setqlim_reservation(mp);
819 resp->tr_qm_setqlim.tr_logcount = XFS_DEFAULT_LOG_COUNT;
820
821 resp->tr_qm_quotaoff.tr_logres = xfs_calc_qm_quotaoff_reservation(mp);
822 resp->tr_qm_quotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
823
824 resp->tr_qm_equotaoff.tr_logres =
825 xfs_calc_qm_quotaoff_end_reservation(mp);
826 resp->tr_qm_equotaoff.tr_logcount = XFS_DEFAULT_LOG_COUNT;
827
828 resp->tr_sb.tr_logres = xfs_calc_sb_reservation(mp);
829 resp->tr_sb.tr_logcount = XFS_DEFAULT_LOG_COUNT;
830
831 /* The following transaction are logged in logical format */
832 resp->tr_ichange.tr_logres = xfs_calc_ichange_reservation(mp);
833 resp->tr_growdata.tr_logres = xfs_calc_growdata_reservation(mp);
834 resp->tr_swrite.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu20996c92013-08-12 20:49:57 +1000835 resp->tr_fsyncts.tr_logres = xfs_calc_swrite_reservation(mp);
Jie Liu0eadd102013-08-12 20:49:56 +1000836 resp->tr_writeid.tr_logres = xfs_calc_writeid_reservation(mp);
837 resp->tr_attrsetrt.tr_logres = xfs_calc_attrsetrt_reservation(mp);
838 resp->tr_clearagi.tr_logres = xfs_calc_clear_agi_bucket_reservation(mp);
839 resp->tr_growrtzero.tr_logres = xfs_calc_growrtzero_reservation(mp);
840 resp->tr_growrtfree.tr_logres = xfs_calc_growrtfree_reservation(mp);
Dave Chinner7fd36c42013-08-12 20:49:32 +1000841}