xfs: add a helper to figure out the space log reservation per item
[deliverable/linux.git] / fs / xfs / xfs_trans.c
CommitLineData
1da177e4 1/*
7b718769 2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
e98c414f 3 * Copyright (C) 2010 Red Hat, Inc.
7b718769 4 * All Rights Reserved.
1da177e4 5 *
7b718769
NS
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
1da177e4
LT
8 * published by the Free Software Foundation.
9 *
7b718769
NS
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.
1da177e4 14 *
7b718769
NS
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
1da177e4 18 */
1da177e4 19#include "xfs.h"
a844f451 20#include "xfs_fs.h"
1da177e4 21#include "xfs_types.h"
1da177e4
LT
22#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_mount.h"
27#include "xfs_error.h"
a844f451 28#include "xfs_da_btree.h"
1da177e4 29#include "xfs_bmap_btree.h"
a844f451 30#include "xfs_alloc_btree.h"
1da177e4 31#include "xfs_ialloc_btree.h"
1da177e4
LT
32#include "xfs_dinode.h"
33#include "xfs_inode.h"
a844f451
NS
34#include "xfs_btree.h"
35#include "xfs_ialloc.h"
36#include "xfs_alloc.h"
efc27b52 37#include "xfs_extent_busy.h"
1da177e4 38#include "xfs_bmap.h"
1da177e4 39#include "xfs_quota.h"
4f3b5783 40#include "xfs_qm.h"
a844f451 41#include "xfs_trans_priv.h"
1da177e4 42#include "xfs_trans_space.h"
322ff6b8 43#include "xfs_inode_item.h"
4f3b5783
JL
44#include "xfs_log_priv.h"
45#include "xfs_buf_item.h"
ed3b4d6c 46#include "xfs_trace.h"
1da177e4 47
8f794055 48kmem_zone_t *xfs_trans_zone;
e98c414f 49kmem_zone_t *xfs_log_item_desc_zone;
1da177e4 50
4f3b5783
JL
51/*
52 * A buffer has a format structure overhead in the log in addition
53 * to the data, so we need to take this into account when reserving
54 * space in a transaction for a buffer. Round the space required up
55 * to a multiple of 128 bytes so that we don't change the historical
56 * reservation that has been used for this overhead.
57 */
58STATIC uint
59xfs_buf_log_overhead(void)
60{
61 return round_up(sizeof(struct xlog_op_header) +
62 sizeof(struct xfs_buf_log_format), 128);
63}
64
65/*
66 * Calculate out transaction log reservation per item in bytes.
67 *
68 * The nbufs argument is used to indicate the number of items that
69 * will be changed in a transaction. size is used to tell how many
70 * bytes should be reserved per item.
71 */
72STATIC uint
73xfs_calc_buf_res(
74 uint nbufs,
75 uint size)
76{
77 return nbufs * (size + xfs_buf_log_overhead());
78}
025101dc 79
8f794055 80/*
025101dc
CH
81 * Various log reservation values.
82 *
83 * These are based on the size of the file system block because that is what
84 * most transactions manipulate. Each adds in an additional 128 bytes per
85 * item logged to try to account for the overhead of the transaction mechanism.
86 *
87 * Note: Most of the reservations underestimate the number of allocation
88 * groups into which they could free extents in the xfs_bmap_finish() call.
89 * This is because the number in the worst case is quite high and quite
90 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
91 * extents in only a single AG at a time. This will require changes to the
92 * EFI code as well, however, so that the EFI for the extents not freed is
93 * logged again in each transaction. See SGI PV #261917.
94 *
95 * Reservation functions here avoid a huge stack in xfs_trans_init due to
96 * register overflow from temporaries in the calculations.
97 */
98
99
100/*
101 * In a write transaction we can allocate a maximum of 2
102 * extents. This gives:
103 * the inode getting the new extents: inode size
104 * the inode's bmap btree: max depth * block size
105 * the agfs of the ags from which the extents are allocated: 2 * sector
106 * the superblock free block counter: sector size
107 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
108 * And the bmap_finish transaction can free bmap blocks in a join:
109 * the agfs of the ags containing the blocks: 2 * sector size
110 * the agfls of the ags containing the blocks: 2 * sector size
111 * the super block free block counter: sector size
112 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
8f794055 113 */
8f794055 114STATIC uint
025101dc
CH
115xfs_calc_write_reservation(
116 struct xfs_mount *mp)
8f794055 117{
025101dc
CH
118 return XFS_DQUOT_LOGRES(mp) +
119 MAX((mp->m_sb.sb_inodesize +
120 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
121 2 * mp->m_sb.sb_sectsize +
122 mp->m_sb.sb_sectsize +
123 XFS_ALLOCFREE_LOG_RES(mp, 2) +
124 128 * (4 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
125 XFS_ALLOCFREE_LOG_COUNT(mp, 2))),
126 (2 * mp->m_sb.sb_sectsize +
127 2 * mp->m_sb.sb_sectsize +
128 mp->m_sb.sb_sectsize +
129 XFS_ALLOCFREE_LOG_RES(mp, 2) +
130 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
8f794055
NS
131}
132
025101dc
CH
133/*
134 * In truncating a file we free up to two extents at once. We can modify:
135 * the inode being truncated: inode size
136 * the inode's bmap btree: (max depth + 1) * block size
137 * And the bmap_finish transaction can free the blocks and bmap blocks:
138 * the agf for each of the ags: 4 * sector size
139 * the agfl for each of the ags: 4 * sector size
140 * the super block to reflect the freed blocks: sector size
141 * worst case split in allocation btrees per extent assuming 4 extents:
142 * 4 exts * 2 trees * (2 * max depth - 1) * block size
143 * the inode btree: max depth * blocksize
144 * the allocation btrees: 2 trees * (max depth - 1) * block size
145 */
8f794055 146STATIC uint
025101dc
CH
147xfs_calc_itruncate_reservation(
148 struct xfs_mount *mp)
8f794055 149{
025101dc
CH
150 return XFS_DQUOT_LOGRES(mp) +
151 MAX((mp->m_sb.sb_inodesize +
152 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1) +
153 128 * (2 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
154 (4 * mp->m_sb.sb_sectsize +
155 4 * mp->m_sb.sb_sectsize +
156 mp->m_sb.sb_sectsize +
157 XFS_ALLOCFREE_LOG_RES(mp, 4) +
158 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4)) +
159 128 * 5 +
160 XFS_ALLOCFREE_LOG_RES(mp, 1) +
161 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
162 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
163}
164
025101dc
CH
165/*
166 * In renaming a files we can modify:
167 * the four inodes involved: 4 * inode size
168 * the two directory btrees: 2 * (max depth + v2) * dir block size
169 * the two directory bmap btrees: 2 * max depth * block size
170 * And the bmap_finish transaction can free dir and bmap blocks (two sets
171 * of bmap blocks) giving:
172 * the agf for the ags in which the blocks live: 3 * sector size
173 * the agfl for the ags in which the blocks live: 3 * sector size
174 * the superblock for the free block count: sector size
175 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
176 */
8f794055 177STATIC uint
025101dc
CH
178xfs_calc_rename_reservation(
179 struct xfs_mount *mp)
8f794055 180{
025101dc
CH
181 return XFS_DQUOT_LOGRES(mp) +
182 MAX((4 * mp->m_sb.sb_inodesize +
183 2 * XFS_DIROP_LOG_RES(mp) +
184 128 * (4 + 2 * XFS_DIROP_LOG_COUNT(mp))),
185 (3 * mp->m_sb.sb_sectsize +
186 3 * mp->m_sb.sb_sectsize +
187 mp->m_sb.sb_sectsize +
188 XFS_ALLOCFREE_LOG_RES(mp, 3) +
189 128 * (7 + XFS_ALLOCFREE_LOG_COUNT(mp, 3))));
8f794055
NS
190}
191
025101dc
CH
192/*
193 * For creating a link to an inode:
194 * the parent directory inode: inode size
195 * the linked inode: inode size
196 * the directory btree could split: (max depth + v2) * dir block size
197 * the directory bmap btree could join or split: (max depth + v2) * blocksize
198 * And the bmap_finish transaction can free some bmap blocks giving:
199 * the agf for the ag in which the blocks live: sector size
200 * the agfl for the ag in which the blocks live: sector size
201 * the superblock for the free block count: sector size
202 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
203 */
8f794055 204STATIC uint
025101dc
CH
205xfs_calc_link_reservation(
206 struct xfs_mount *mp)
8f794055 207{
025101dc
CH
208 return XFS_DQUOT_LOGRES(mp) +
209 MAX((mp->m_sb.sb_inodesize +
210 mp->m_sb.sb_inodesize +
211 XFS_DIROP_LOG_RES(mp) +
212 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
213 (mp->m_sb.sb_sectsize +
214 mp->m_sb.sb_sectsize +
215 mp->m_sb.sb_sectsize +
216 XFS_ALLOCFREE_LOG_RES(mp, 1) +
217 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
218}
219
025101dc
CH
220/*
221 * For removing a directory entry we can modify:
222 * the parent directory inode: inode size
223 * the removed inode: inode size
224 * the directory btree could join: (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 the dir and bmap blocks giving:
227 * the agf for the ag in which the blocks live: 2 * sector size
228 * the agfl for the ag in which the blocks live: 2 * sector size
229 * the superblock for the free block count: sector size
230 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
231 */
8f794055 232STATIC uint
025101dc
CH
233xfs_calc_remove_reservation(
234 struct xfs_mount *mp)
8f794055 235{
025101dc
CH
236 return XFS_DQUOT_LOGRES(mp) +
237 MAX((mp->m_sb.sb_inodesize +
238 mp->m_sb.sb_inodesize +
239 XFS_DIROP_LOG_RES(mp) +
240 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
241 (2 * mp->m_sb.sb_sectsize +
242 2 * mp->m_sb.sb_sectsize +
243 mp->m_sb.sb_sectsize +
244 XFS_ALLOCFREE_LOG_RES(mp, 2) +
245 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
8f794055
NS
246}
247
025101dc
CH
248/*
249 * For symlink we can modify:
250 * the parent directory inode: inode size
251 * the new inode: inode size
252 * the inode btree entry: 1 block
253 * the directory btree: (max depth + v2) * dir block size
254 * the directory inode's bmap btree: (max depth + v2) * block size
255 * the blocks for the symlink: 1 kB
256 * Or in the first xact we allocate some inodes giving:
257 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
258 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
259 * the inode btree: max depth * blocksize
260 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
261 */
8f794055 262STATIC uint
025101dc
CH
263xfs_calc_symlink_reservation(
264 struct xfs_mount *mp)
8f794055 265{
025101dc
CH
266 return XFS_DQUOT_LOGRES(mp) +
267 MAX((mp->m_sb.sb_inodesize +
268 mp->m_sb.sb_inodesize +
269 XFS_FSB_TO_B(mp, 1) +
270 XFS_DIROP_LOG_RES(mp) +
271 1024 +
272 128 * (4 + XFS_DIROP_LOG_COUNT(mp))),
273 (2 * mp->m_sb.sb_sectsize +
274 XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
275 XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
276 XFS_ALLOCFREE_LOG_RES(mp, 1) +
277 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
278 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
279}
280
025101dc
CH
281/*
282 * For create we can modify:
283 * the parent directory inode: inode size
284 * the new inode: inode size
285 * the inode btree entry: block size
286 * the superblock for the nlink flag: sector size
287 * the directory btree: (max depth + v2) * dir block size
288 * the directory inode's bmap btree: (max depth + v2) * block size
289 * Or in the first xact we allocate some inodes giving:
290 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
291 * the superblock for the nlink flag: sector size
292 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
293 * the inode btree: max depth * blocksize
294 * the allocation btrees: 2 trees * (max depth - 1) * block size
295 */
8f794055 296STATIC uint
025101dc
CH
297xfs_calc_create_reservation(
298 struct xfs_mount *mp)
8f794055 299{
025101dc
CH
300 return XFS_DQUOT_LOGRES(mp) +
301 MAX((mp->m_sb.sb_inodesize +
302 mp->m_sb.sb_inodesize +
303 mp->m_sb.sb_sectsize +
304 XFS_FSB_TO_B(mp, 1) +
305 XFS_DIROP_LOG_RES(mp) +
306 128 * (3 + XFS_DIROP_LOG_COUNT(mp))),
307 (3 * mp->m_sb.sb_sectsize +
308 XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
309 XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
310 XFS_ALLOCFREE_LOG_RES(mp, 1) +
311 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
312 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
313}
314
025101dc
CH
315/*
316 * Making a new directory is the same as creating a new file.
317 */
8f794055 318STATIC uint
025101dc
CH
319xfs_calc_mkdir_reservation(
320 struct xfs_mount *mp)
8f794055 321{
025101dc 322 return xfs_calc_create_reservation(mp);
8f794055
NS
323}
324
025101dc
CH
325/*
326 * In freeing an inode we can modify:
327 * the inode being freed: inode size
328 * the super block free inode counter: sector size
329 * the agi hash list and counters: sector size
330 * the inode btree entry: block size
331 * the on disk inode before ours in the agi hash list: inode cluster size
332 * the inode btree: max depth * blocksize
333 * the allocation btrees: 2 trees * (max depth - 1) * block size
334 */
8f794055 335STATIC uint
025101dc
CH
336xfs_calc_ifree_reservation(
337 struct xfs_mount *mp)
8f794055 338{
025101dc
CH
339 return XFS_DQUOT_LOGRES(mp) +
340 mp->m_sb.sb_inodesize +
341 mp->m_sb.sb_sectsize +
342 mp->m_sb.sb_sectsize +
343 XFS_FSB_TO_B(mp, 1) +
344 MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
345 XFS_INODE_CLUSTER_SIZE(mp)) +
346 128 * 5 +
347 XFS_ALLOCFREE_LOG_RES(mp, 1) +
348 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
349 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
350}
351
025101dc
CH
352/*
353 * When only changing the inode we log the inode and possibly the superblock
354 * We also add a bit of slop for the transaction stuff.
355 */
8f794055 356STATIC uint
025101dc
CH
357xfs_calc_ichange_reservation(
358 struct xfs_mount *mp)
8f794055 359{
025101dc
CH
360 return XFS_DQUOT_LOGRES(mp) +
361 mp->m_sb.sb_inodesize +
362 mp->m_sb.sb_sectsize +
363 512;
364
8f794055
NS
365}
366
025101dc
CH
367/*
368 * Growing the data section of the filesystem.
369 * superblock
370 * agi and agf
371 * allocation btrees
372 */
8f794055 373STATIC uint
025101dc
CH
374xfs_calc_growdata_reservation(
375 struct xfs_mount *mp)
8f794055 376{
025101dc
CH
377 return mp->m_sb.sb_sectsize * 3 +
378 XFS_ALLOCFREE_LOG_RES(mp, 1) +
379 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
380}
381
025101dc
CH
382/*
383 * Growing the rt section of the filesystem.
384 * In the first set of transactions (ALLOC) we allocate space to the
385 * bitmap or summary files.
386 * superblock: sector size
387 * agf of the ag from which the extent is allocated: sector size
388 * bmap btree for bitmap/summary inode: max depth * blocksize
389 * bitmap/summary inode: inode size
390 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
391 */
8f794055 392STATIC uint
025101dc
CH
393xfs_calc_growrtalloc_reservation(
394 struct xfs_mount *mp)
8f794055 395{
025101dc
CH
396 return 2 * mp->m_sb.sb_sectsize +
397 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
398 mp->m_sb.sb_inodesize +
399 XFS_ALLOCFREE_LOG_RES(mp, 1) +
400 128 * (3 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
401 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
402}
403
025101dc
CH
404/*
405 * Growing the rt section of the filesystem.
406 * In the second set of transactions (ZERO) we zero the new metadata blocks.
407 * one bitmap/summary block: blocksize
408 */
8f794055 409STATIC uint
025101dc
CH
410xfs_calc_growrtzero_reservation(
411 struct xfs_mount *mp)
8f794055 412{
025101dc 413 return mp->m_sb.sb_blocksize + 128;
8f794055
NS
414}
415
025101dc
CH
416/*
417 * Growing the rt section of the filesystem.
418 * In the third set of transactions (FREE) we update metadata without
419 * allocating any new blocks.
420 * superblock: sector size
421 * bitmap inode: inode size
422 * summary inode: inode size
423 * one bitmap block: blocksize
424 * summary blocks: new summary size
425 */
8f794055 426STATIC uint
025101dc
CH
427xfs_calc_growrtfree_reservation(
428 struct xfs_mount *mp)
8f794055 429{
025101dc
CH
430 return mp->m_sb.sb_sectsize +
431 2 * mp->m_sb.sb_inodesize +
432 mp->m_sb.sb_blocksize +
433 mp->m_rsumsize +
434 128 * 5;
8f794055
NS
435}
436
025101dc
CH
437/*
438 * Logging the inode modification timestamp on a synchronous write.
439 * inode
440 */
8f794055 441STATIC uint
025101dc
CH
442xfs_calc_swrite_reservation(
443 struct xfs_mount *mp)
8f794055 444{
025101dc 445 return mp->m_sb.sb_inodesize + 128;
8f794055
NS
446}
447
025101dc
CH
448/*
449 * Logging the inode mode bits when writing a setuid/setgid file
450 * inode
451 */
8f794055
NS
452STATIC uint
453xfs_calc_writeid_reservation(xfs_mount_t *mp)
454{
025101dc 455 return mp->m_sb.sb_inodesize + 128;
8f794055
NS
456}
457
025101dc
CH
458/*
459 * Converting the inode from non-attributed to attributed.
460 * the inode being converted: inode size
461 * agf block and superblock (for block allocation)
462 * the new block (directory sized)
463 * bmap blocks for the new directory block
464 * allocation btrees
465 */
8f794055 466STATIC uint
025101dc
CH
467xfs_calc_addafork_reservation(
468 struct xfs_mount *mp)
8f794055 469{
025101dc
CH
470 return XFS_DQUOT_LOGRES(mp) +
471 mp->m_sb.sb_inodesize +
472 mp->m_sb.sb_sectsize * 2 +
473 mp->m_dirblksize +
474 XFS_FSB_TO_B(mp, XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1) +
475 XFS_ALLOCFREE_LOG_RES(mp, 1) +
476 128 * (4 + XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1 +
477 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
478}
479
025101dc
CH
480/*
481 * Removing the attribute fork of a file
482 * the inode being truncated: inode size
483 * the inode's bmap btree: max depth * block size
484 * And the bmap_finish transaction can free the blocks and bmap blocks:
485 * the agf for each of the ags: 4 * sector size
486 * the agfl for each of the ags: 4 * sector size
487 * the super block to reflect the freed blocks: sector size
488 * worst case split in allocation btrees per extent assuming 4 extents:
489 * 4 exts * 2 trees * (2 * max depth - 1) * block size
490 */
8f794055 491STATIC uint
025101dc
CH
492xfs_calc_attrinval_reservation(
493 struct xfs_mount *mp)
8f794055 494{
025101dc
CH
495 return MAX((mp->m_sb.sb_inodesize +
496 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
497 128 * (1 + XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK))),
498 (4 * mp->m_sb.sb_sectsize +
499 4 * mp->m_sb.sb_sectsize +
500 mp->m_sb.sb_sectsize +
501 XFS_ALLOCFREE_LOG_RES(mp, 4) +
502 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4))));
8f794055
NS
503}
504
025101dc
CH
505/*
506 * Setting an attribute.
507 * the inode getting the attribute
508 * the superblock for allocations
509 * the agfs extents are allocated from
510 * the attribute btree * max depth
511 * the inode allocation btree
512 * Since attribute transaction space is dependent on the size of the attribute,
513 * the calculation is done partially at mount time and partially at runtime.
514 */
8f794055 515STATIC uint
025101dc
CH
516xfs_calc_attrset_reservation(
517 struct xfs_mount *mp)
8f794055 518{
025101dc
CH
519 return XFS_DQUOT_LOGRES(mp) +
520 mp->m_sb.sb_inodesize +
521 mp->m_sb.sb_sectsize +
522 XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
523 128 * (2 + XFS_DA_NODE_MAXDEPTH);
8f794055
NS
524}
525
025101dc
CH
526/*
527 * Removing an attribute.
528 * the inode: inode size
529 * the attribute btree could join: max depth * block size
530 * the inode bmap btree could join or split: max depth * block size
531 * And the bmap_finish transaction can free the attr blocks freed giving:
532 * the agf for the ag in which the blocks live: 2 * sector size
533 * the agfl for the ag in which the blocks live: 2 * sector size
534 * the superblock for the free block count: sector size
535 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
536 */
8f794055 537STATIC uint
025101dc
CH
538xfs_calc_attrrm_reservation(
539 struct xfs_mount *mp)
8f794055 540{
025101dc
CH
541 return XFS_DQUOT_LOGRES(mp) +
542 MAX((mp->m_sb.sb_inodesize +
543 XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
544 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
545 128 * (1 + XFS_DA_NODE_MAXDEPTH +
546 XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
547 (2 * mp->m_sb.sb_sectsize +
548 2 * mp->m_sb.sb_sectsize +
549 mp->m_sb.sb_sectsize +
550 XFS_ALLOCFREE_LOG_RES(mp, 2) +
551 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
8f794055
NS
552}
553
025101dc
CH
554/*
555 * Clearing a bad agino number in an agi hash bucket.
556 */
8f794055 557STATIC uint
025101dc
CH
558xfs_calc_clear_agi_bucket_reservation(
559 struct xfs_mount *mp)
8f794055 560{
025101dc 561 return mp->m_sb.sb_sectsize + 128;
8f794055
NS
562}
563
1da177e4
LT
564/*
565 * Initialize the precomputed transaction reservation values
566 * in the mount structure.
567 */
568void
569xfs_trans_init(
025101dc 570 struct xfs_mount *mp)
1da177e4 571{
025101dc 572 struct xfs_trans_reservations *resp = &mp->m_reservations;
1da177e4 573
8f794055
NS
574 resp->tr_write = xfs_calc_write_reservation(mp);
575 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
576 resp->tr_rename = xfs_calc_rename_reservation(mp);
577 resp->tr_link = xfs_calc_link_reservation(mp);
578 resp->tr_remove = xfs_calc_remove_reservation(mp);
579 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
580 resp->tr_create = xfs_calc_create_reservation(mp);
581 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
582 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
583 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
584 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
585 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
586 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
587 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
588 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
589 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
590 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
591 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
592 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
593 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
594 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
1da177e4
LT
595}
596
597/*
598 * This routine is called to allocate a transaction structure.
599 * The type parameter indicates the type of the transaction. These
600 * are enumerated in xfs_trans.h.
b2ce3974
AE
601 *
602 * Dynamically allocate the transaction structure from the transaction
603 * zone, initialize it, and return it to the caller.
1da177e4 604 */
b2ce3974
AE
605xfs_trans_t *
606xfs_trans_alloc(
607 xfs_mount_t *mp,
608 uint type)
609{
d9457dc0
JK
610 xfs_trans_t *tp;
611
612 sb_start_intwrite(mp->m_super);
613 tp = _xfs_trans_alloc(mp, type, KM_SLEEP);
614 tp->t_flags |= XFS_TRANS_FREEZE_PROT;
615 return tp;
b2ce3974
AE
616}
617
618xfs_trans_t *
1da177e4 619_xfs_trans_alloc(
b2ce3974
AE
620 xfs_mount_t *mp,
621 uint type,
77ba7877 622 xfs_km_flags_t memflags)
1da177e4 623{
b2ce3974 624 xfs_trans_t *tp;
1da177e4 625
d9457dc0 626 WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
34327e13 627 atomic_inc(&mp->m_active_trans);
1da177e4 628
80641dc6 629 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
1da177e4
LT
630 tp->t_magic = XFS_TRANS_MAGIC;
631 tp->t_type = type;
632 tp->t_mountp = mp;
e98c414f 633 INIT_LIST_HEAD(&tp->t_items);
ed3b4d6c 634 INIT_LIST_HEAD(&tp->t_busy);
34327e13 635 return tp;
1da177e4
LT
636}
637
b1c1b5b6
DC
638/*
639 * Free the transaction structure. If there is more clean up
640 * to do when the structure is freed, add it here.
641 */
642STATIC void
643xfs_trans_free(
ed3b4d6c 644 struct xfs_trans *tp)
b1c1b5b6 645{
4ecbfe63
DC
646 xfs_extent_busy_sort(&tp->t_busy);
647 xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
ed3b4d6c 648
b1c1b5b6 649 atomic_dec(&tp->t_mountp->m_active_trans);
d9457dc0
JK
650 if (tp->t_flags & XFS_TRANS_FREEZE_PROT)
651 sb_end_intwrite(tp->t_mountp->m_super);
b1c1b5b6
DC
652 xfs_trans_free_dqinfo(tp);
653 kmem_zone_free(xfs_trans_zone, tp);
654}
655
1da177e4
LT
656/*
657 * This is called to create a new transaction which will share the
658 * permanent log reservation of the given transaction. The remaining
659 * unused block and rt extent reservations are also inherited. This
660 * implies that the original transaction is no longer allowed to allocate
661 * blocks. Locks and log items, however, are no inherited. They must
662 * be added to the new transaction explicitly.
663 */
664xfs_trans_t *
665xfs_trans_dup(
666 xfs_trans_t *tp)
667{
668 xfs_trans_t *ntp;
669
670 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
671
672 /*
673 * Initialize the new transaction structure.
674 */
675 ntp->t_magic = XFS_TRANS_MAGIC;
676 ntp->t_type = tp->t_type;
677 ntp->t_mountp = tp->t_mountp;
e98c414f 678 INIT_LIST_HEAD(&ntp->t_items);
ed3b4d6c 679 INIT_LIST_HEAD(&ntp->t_busy);
1da177e4
LT
680
681 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1da177e4 682 ASSERT(tp->t_ticket != NULL);
cfcbbbd0 683
d9457dc0
JK
684 ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
685 (tp->t_flags & XFS_TRANS_RESERVE) |
686 (tp->t_flags & XFS_TRANS_FREEZE_PROT);
687 /* We gave our writer reference to the new transaction */
688 tp->t_flags &= ~XFS_TRANS_FREEZE_PROT;
cc09c0dc 689 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
1da177e4
LT
690 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
691 tp->t_blk_res = tp->t_blk_res_used;
692 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
693 tp->t_rtx_res = tp->t_rtx_res_used;
59c1b082 694 ntp->t_pflags = tp->t_pflags;
1da177e4 695
7d095257 696 xfs_trans_dup_dqinfo(tp, ntp);
1da177e4
LT
697
698 atomic_inc(&tp->t_mountp->m_active_trans);
699 return ntp;
700}
701
702/*
703 * This is called to reserve free disk blocks and log space for the
704 * given transaction. This must be done before allocating any resources
705 * within the transaction.
706 *
707 * This will return ENOSPC if there are not enough blocks available.
708 * It will sleep waiting for available log space.
709 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
710 * is used by long running transactions. If any one of the reservations
711 * fails then they will all be backed out.
712 *
713 * This does not do quota reservations. That typically is done by the
714 * caller afterwards.
715 */
716int
717xfs_trans_reserve(
718 xfs_trans_t *tp,
719 uint blocks,
720 uint logspace,
721 uint rtextents,
722 uint flags,
723 uint logcount)
724{
59c1b082
NS
725 int error = 0;
726 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1da177e4
LT
727
728 /* Mark this thread as being in a transaction */
59c1b082 729 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4
LT
730
731 /*
732 * Attempt to reserve the needed disk blocks by decrementing
733 * the number needed from the number available. This will
734 * fail if the count would go below zero.
735 */
736 if (blocks > 0) {
96540c78 737 error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
20f4ebf2 738 -((int64_t)blocks), rsvd);
1da177e4 739 if (error != 0) {
59c1b082 740 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4
LT
741 return (XFS_ERROR(ENOSPC));
742 }
743 tp->t_blk_res += blocks;
744 }
745
746 /*
747 * Reserve the log space needed for this transaction.
748 */
749 if (logspace > 0) {
9006fb91
CH
750 bool permanent = false;
751
752 ASSERT(tp->t_log_res == 0 || tp->t_log_res == logspace);
753 ASSERT(tp->t_log_count == 0 || tp->t_log_count == logcount);
754
1da177e4 755 if (flags & XFS_TRANS_PERM_LOG_RES) {
1da177e4 756 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
9006fb91 757 permanent = true;
1da177e4
LT
758 } else {
759 ASSERT(tp->t_ticket == NULL);
760 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
1da177e4
LT
761 }
762
9006fb91
CH
763 if (tp->t_ticket != NULL) {
764 ASSERT(flags & XFS_TRANS_PERM_LOG_RES);
765 error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
766 } else {
767 error = xfs_log_reserve(tp->t_mountp, logspace,
768 logcount, &tp->t_ticket,
769 XFS_TRANSACTION, permanent,
770 tp->t_type);
1da177e4 771 }
9006fb91
CH
772
773 if (error)
774 goto undo_blocks;
775
1da177e4
LT
776 tp->t_log_res = logspace;
777 tp->t_log_count = logcount;
778 }
779
780 /*
781 * Attempt to reserve the needed realtime extents by decrementing
782 * the number needed from the number available. This will
783 * fail if the count would go below zero.
784 */
785 if (rtextents > 0) {
786 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
20f4ebf2 787 -((int64_t)rtextents), rsvd);
1da177e4
LT
788 if (error) {
789 error = XFS_ERROR(ENOSPC);
790 goto undo_log;
791 }
792 tp->t_rtx_res += rtextents;
793 }
794
795 return 0;
796
797 /*
798 * Error cases jump to one of these labels to undo any
799 * reservations which have already been performed.
800 */
801undo_log:
802 if (logspace > 0) {
9006fb91
CH
803 int log_flags;
804
1da177e4
LT
805 if (flags & XFS_TRANS_PERM_LOG_RES) {
806 log_flags = XFS_LOG_REL_PERM_RESERV;
807 } else {
808 log_flags = 0;
809 }
810 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
811 tp->t_ticket = NULL;
812 tp->t_log_res = 0;
813 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
814 }
815
816undo_blocks:
817 if (blocks > 0) {
96540c78 818 xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
20f4ebf2 819 (int64_t)blocks, rsvd);
1da177e4
LT
820 tp->t_blk_res = 0;
821 }
822
59c1b082 823 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4 824
59c1b082 825 return error;
1da177e4
LT
826}
827
1da177e4
LT
828/*
829 * Record the indicated change to the given field for application
830 * to the file system's superblock when the transaction commits.
831 * For now, just store the change in the transaction structure.
832 *
833 * Mark the transaction structure to indicate that the superblock
834 * needs to be updated before committing.
92821e2b
DC
835 *
836 * Because we may not be keeping track of allocated/free inodes and
837 * used filesystem blocks in the superblock, we do not mark the
838 * superblock dirty in this transaction if we modify these fields.
839 * We still need to update the transaction deltas so that they get
840 * applied to the incore superblock, but we don't want them to
841 * cause the superblock to get locked and logged if these are the
842 * only fields in the superblock that the transaction modifies.
1da177e4
LT
843 */
844void
845xfs_trans_mod_sb(
846 xfs_trans_t *tp,
847 uint field,
20f4ebf2 848 int64_t delta)
1da177e4 849{
92821e2b
DC
850 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
851 xfs_mount_t *mp = tp->t_mountp;
1da177e4
LT
852
853 switch (field) {
854 case XFS_TRANS_SB_ICOUNT:
855 tp->t_icount_delta += delta;
92821e2b
DC
856 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
857 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
858 break;
859 case XFS_TRANS_SB_IFREE:
860 tp->t_ifree_delta += delta;
92821e2b
DC
861 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
862 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
863 break;
864 case XFS_TRANS_SB_FDBLOCKS:
865 /*
866 * Track the number of blocks allocated in the
867 * transaction. Make sure it does not exceed the
868 * number reserved.
869 */
870 if (delta < 0) {
871 tp->t_blk_res_used += (uint)-delta;
872 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
873 }
874 tp->t_fdblocks_delta += delta;
92821e2b
DC
875 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
876 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
877 break;
878 case XFS_TRANS_SB_RES_FDBLOCKS:
879 /*
880 * The allocation has already been applied to the
881 * in-core superblock's counter. This should only
882 * be applied to the on-disk superblock.
883 */
884 ASSERT(delta < 0);
885 tp->t_res_fdblocks_delta += delta;
92821e2b
DC
886 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
887 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
888 break;
889 case XFS_TRANS_SB_FREXTENTS:
890 /*
891 * Track the number of blocks allocated in the
892 * transaction. Make sure it does not exceed the
893 * number reserved.
894 */
895 if (delta < 0) {
896 tp->t_rtx_res_used += (uint)-delta;
897 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
898 }
899 tp->t_frextents_delta += delta;
900 break;
901 case XFS_TRANS_SB_RES_FREXTENTS:
902 /*
903 * The allocation has already been applied to the
c41564b5 904 * in-core superblock's counter. This should only
1da177e4
LT
905 * be applied to the on-disk superblock.
906 */
907 ASSERT(delta < 0);
908 tp->t_res_frextents_delta += delta;
909 break;
910 case XFS_TRANS_SB_DBLOCKS:
911 ASSERT(delta > 0);
912 tp->t_dblocks_delta += delta;
913 break;
914 case XFS_TRANS_SB_AGCOUNT:
915 ASSERT(delta > 0);
916 tp->t_agcount_delta += delta;
917 break;
918 case XFS_TRANS_SB_IMAXPCT:
919 tp->t_imaxpct_delta += delta;
920 break;
921 case XFS_TRANS_SB_REXTSIZE:
922 tp->t_rextsize_delta += delta;
923 break;
924 case XFS_TRANS_SB_RBMBLOCKS:
925 tp->t_rbmblocks_delta += delta;
926 break;
927 case XFS_TRANS_SB_RBLOCKS:
928 tp->t_rblocks_delta += delta;
929 break;
930 case XFS_TRANS_SB_REXTENTS:
931 tp->t_rextents_delta += delta;
932 break;
933 case XFS_TRANS_SB_REXTSLOG:
934 tp->t_rextslog_delta += delta;
935 break;
936 default:
937 ASSERT(0);
938 return;
939 }
940
210c6f1c 941 tp->t_flags |= flags;
1da177e4
LT
942}
943
944/*
945 * xfs_trans_apply_sb_deltas() is called from the commit code
946 * to bring the superblock buffer into the current transaction
947 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
948 *
949 * For now we just look at each field allowed to change and change
950 * it if necessary.
951 */
952STATIC void
953xfs_trans_apply_sb_deltas(
954 xfs_trans_t *tp)
955{
2bdf7cd0 956 xfs_dsb_t *sbp;
1da177e4
LT
957 xfs_buf_t *bp;
958 int whole = 0;
959
960 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
961 sbp = XFS_BUF_TO_SBP(bp);
962
963 /*
964 * Check that superblock mods match the mods made to AGF counters.
965 */
966 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
967 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
968 tp->t_ag_btree_delta));
969
92821e2b
DC
970 /*
971 * Only update the superblock counters if we are logging them
972 */
973 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
2bdf7cd0 974 if (tp->t_icount_delta)
413d57c9 975 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
2bdf7cd0 976 if (tp->t_ifree_delta)
413d57c9 977 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
2bdf7cd0 978 if (tp->t_fdblocks_delta)
413d57c9 979 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
2bdf7cd0 980 if (tp->t_res_fdblocks_delta)
413d57c9 981 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
1da177e4
LT
982 }
983
2bdf7cd0 984 if (tp->t_frextents_delta)
413d57c9 985 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
2bdf7cd0 986 if (tp->t_res_frextents_delta)
413d57c9 987 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
2bdf7cd0
CH
988
989 if (tp->t_dblocks_delta) {
413d57c9 990 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
1da177e4
LT
991 whole = 1;
992 }
2bdf7cd0 993 if (tp->t_agcount_delta) {
413d57c9 994 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
1da177e4
LT
995 whole = 1;
996 }
2bdf7cd0
CH
997 if (tp->t_imaxpct_delta) {
998 sbp->sb_imax_pct += tp->t_imaxpct_delta;
1da177e4
LT
999 whole = 1;
1000 }
2bdf7cd0 1001 if (tp->t_rextsize_delta) {
413d57c9 1002 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
1da177e4
LT
1003 whole = 1;
1004 }
2bdf7cd0 1005 if (tp->t_rbmblocks_delta) {
413d57c9 1006 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
1da177e4
LT
1007 whole = 1;
1008 }
2bdf7cd0 1009 if (tp->t_rblocks_delta) {
413d57c9 1010 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
1da177e4
LT
1011 whole = 1;
1012 }
2bdf7cd0 1013 if (tp->t_rextents_delta) {
413d57c9 1014 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
1da177e4
LT
1015 whole = 1;
1016 }
2bdf7cd0
CH
1017 if (tp->t_rextslog_delta) {
1018 sbp->sb_rextslog += tp->t_rextslog_delta;
1da177e4
LT
1019 whole = 1;
1020 }
1021
1022 if (whole)
1023 /*
c41564b5 1024 * Log the whole thing, the fields are noncontiguous.
1da177e4 1025 */
2bdf7cd0 1026 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
1da177e4
LT
1027 else
1028 /*
1029 * Since all the modifiable fields are contiguous, we
1030 * can get away with this.
1031 */
2bdf7cd0
CH
1032 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
1033 offsetof(xfs_dsb_t, sb_frextents) +
1da177e4 1034 sizeof(sbp->sb_frextents) - 1);
1da177e4
LT
1035}
1036
1037/*
45c34141
DC
1038 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
1039 * and apply superblock counter changes to the in-core superblock. The
1040 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
1041 * applied to the in-core superblock. The idea is that that has already been
1042 * done.
1da177e4
LT
1043 *
1044 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
45c34141
DC
1045 * However, we have to ensure that we only modify each superblock field only
1046 * once because the application of the delta values may not be atomic. That can
1047 * lead to ENOSPC races occurring if we have two separate modifcations of the
1048 * free space counter to put back the entire reservation and then take away
1049 * what we used.
1050 *
1051 * If we are not logging superblock counters, then the inode allocated/free and
1052 * used block counts are not updated in the on disk superblock. In this case,
1053 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
1054 * still need to update the incore superblock with the changes.
1da177e4 1055 */
71e330b5 1056void
1da177e4
LT
1057xfs_trans_unreserve_and_mod_sb(
1058 xfs_trans_t *tp)
1059{
1b040712 1060 xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
1da177e4 1061 xfs_mod_sb_t *msbp;
92821e2b 1062 xfs_mount_t *mp = tp->t_mountp;
1da177e4
LT
1063 /* REFERENCED */
1064 int error;
1065 int rsvd;
45c34141
DC
1066 int64_t blkdelta = 0;
1067 int64_t rtxdelta = 0;
1b040712
CH
1068 int64_t idelta = 0;
1069 int64_t ifreedelta = 0;
1da177e4
LT
1070
1071 msbp = msb;
1072 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1073
1b040712 1074 /* calculate deltas */
45c34141
DC
1075 if (tp->t_blk_res > 0)
1076 blkdelta = tp->t_blk_res;
45c34141
DC
1077 if ((tp->t_fdblocks_delta != 0) &&
1078 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1079 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
1080 blkdelta += tp->t_fdblocks_delta;
1081
45c34141
DC
1082 if (tp->t_rtx_res > 0)
1083 rtxdelta = tp->t_rtx_res;
45c34141
DC
1084 if ((tp->t_frextents_delta != 0) &&
1085 (tp->t_flags & XFS_TRANS_SB_DIRTY))
1086 rtxdelta += tp->t_frextents_delta;
1087
1b040712
CH
1088 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1089 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
1090 idelta = tp->t_icount_delta;
1091 ifreedelta = tp->t_ifree_delta;
1092 }
1093
1094 /* apply the per-cpu counters */
1095 if (blkdelta) {
1096 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
1097 blkdelta, rsvd);
1098 if (error)
1099 goto out;
1100 }
1101
1102 if (idelta) {
1103 error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
1104 idelta, rsvd);
1105 if (error)
1106 goto out_undo_fdblocks;
1107 }
1108
1109 if (ifreedelta) {
1110 error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
1111 ifreedelta, rsvd);
1112 if (error)
1113 goto out_undo_icount;
1114 }
1115
1116 /* apply remaining deltas */
45c34141 1117 if (rtxdelta != 0) {
1da177e4 1118 msbp->msb_field = XFS_SBS_FREXTENTS;
45c34141 1119 msbp->msb_delta = rtxdelta;
1da177e4
LT
1120 msbp++;
1121 }
1122
92821e2b 1123 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
1da177e4
LT
1124 if (tp->t_dblocks_delta != 0) {
1125 msbp->msb_field = XFS_SBS_DBLOCKS;
20f4ebf2 1126 msbp->msb_delta = tp->t_dblocks_delta;
1da177e4
LT
1127 msbp++;
1128 }
1129 if (tp->t_agcount_delta != 0) {
1130 msbp->msb_field = XFS_SBS_AGCOUNT;
20f4ebf2 1131 msbp->msb_delta = tp->t_agcount_delta;
1da177e4
LT
1132 msbp++;
1133 }
1134 if (tp->t_imaxpct_delta != 0) {
1135 msbp->msb_field = XFS_SBS_IMAX_PCT;
20f4ebf2 1136 msbp->msb_delta = tp->t_imaxpct_delta;
1da177e4
LT
1137 msbp++;
1138 }
1139 if (tp->t_rextsize_delta != 0) {
1140 msbp->msb_field = XFS_SBS_REXTSIZE;
20f4ebf2 1141 msbp->msb_delta = tp->t_rextsize_delta;
1da177e4
LT
1142 msbp++;
1143 }
1144 if (tp->t_rbmblocks_delta != 0) {
1145 msbp->msb_field = XFS_SBS_RBMBLOCKS;
20f4ebf2 1146 msbp->msb_delta = tp->t_rbmblocks_delta;
1da177e4
LT
1147 msbp++;
1148 }
1149 if (tp->t_rblocks_delta != 0) {
1150 msbp->msb_field = XFS_SBS_RBLOCKS;
20f4ebf2 1151 msbp->msb_delta = tp->t_rblocks_delta;
1da177e4
LT
1152 msbp++;
1153 }
1154 if (tp->t_rextents_delta != 0) {
1155 msbp->msb_field = XFS_SBS_REXTENTS;
20f4ebf2 1156 msbp->msb_delta = tp->t_rextents_delta;
1da177e4
LT
1157 msbp++;
1158 }
1159 if (tp->t_rextslog_delta != 0) {
1160 msbp->msb_field = XFS_SBS_REXTSLOG;
20f4ebf2 1161 msbp->msb_delta = tp->t_rextslog_delta;
1da177e4
LT
1162 msbp++;
1163 }
1164 }
1165
1166 /*
1167 * If we need to change anything, do it.
1168 */
1169 if (msbp > msb) {
1170 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
1171 (uint)(msbp - msb), rsvd);
1b040712
CH
1172 if (error)
1173 goto out_undo_ifreecount;
1da177e4 1174 }
1b040712
CH
1175
1176 return;
1177
1178out_undo_ifreecount:
1179 if (ifreedelta)
1180 xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
1181out_undo_icount:
1182 if (idelta)
1183 xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
1184out_undo_fdblocks:
1185 if (blkdelta)
1186 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
1187out:
1884bd83 1188 ASSERT(error == 0);
1b040712 1189 return;
1da177e4
LT
1190}
1191
e98c414f
CH
1192/*
1193 * Add the given log item to the transaction's list of log items.
1194 *
1195 * The log item will now point to its new descriptor with its li_desc field.
1196 */
1197void
1198xfs_trans_add_item(
1199 struct xfs_trans *tp,
1200 struct xfs_log_item *lip)
1201{
1202 struct xfs_log_item_desc *lidp;
1203
f65020a8
JJ
1204 ASSERT(lip->li_mountp == tp->t_mountp);
1205 ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
e98c414f 1206
43869706 1207 lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
e98c414f
CH
1208
1209 lidp->lid_item = lip;
1210 lidp->lid_flags = 0;
e98c414f
CH
1211 list_add_tail(&lidp->lid_trans, &tp->t_items);
1212
1213 lip->li_desc = lidp;
1214}
1215
1216STATIC void
1217xfs_trans_free_item_desc(
1218 struct xfs_log_item_desc *lidp)
1219{
1220 list_del_init(&lidp->lid_trans);
1221 kmem_zone_free(xfs_log_item_desc_zone, lidp);
1222}
1223
1224/*
1225 * Unlink and free the given descriptor.
1226 */
1227void
1228xfs_trans_del_item(
1229 struct xfs_log_item *lip)
1230{
1231 xfs_trans_free_item_desc(lip->li_desc);
1232 lip->li_desc = NULL;
1233}
1234
1235/*
1236 * Unlock all of the items of a transaction and free all the descriptors
1237 * of that transaction.
1238 */
d17c701c 1239void
e98c414f
CH
1240xfs_trans_free_items(
1241 struct xfs_trans *tp,
1242 xfs_lsn_t commit_lsn,
1243 int flags)
1244{
1245 struct xfs_log_item_desc *lidp, *next;
1246
1247 list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
1248 struct xfs_log_item *lip = lidp->lid_item;
1249
1250 lip->li_desc = NULL;
1251
1252 if (commit_lsn != NULLCOMMITLSN)
1253 IOP_COMMITTING(lip, commit_lsn);
1254 if (flags & XFS_TRANS_ABORT)
1255 lip->li_flags |= XFS_LI_ABORTED;
1256 IOP_UNLOCK(lip);
1257
1258 xfs_trans_free_item_desc(lidp);
1259 }
1260}
1261
0e57f6a3
DC
1262static inline void
1263xfs_log_item_batch_insert(
1264 struct xfs_ail *ailp,
1d8c95a3 1265 struct xfs_ail_cursor *cur,
0e57f6a3
DC
1266 struct xfs_log_item **log_items,
1267 int nr_items,
1268 xfs_lsn_t commit_lsn)
1269{
1270 int i;
1271
1272 spin_lock(&ailp->xa_lock);
1273 /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
1d8c95a3 1274 xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
0e57f6a3
DC
1275
1276 for (i = 0; i < nr_items; i++)
1277 IOP_UNPIN(log_items[i], 0);
1278}
1279
1280/*
1281 * Bulk operation version of xfs_trans_committed that takes a log vector of
1282 * items to insert into the AIL. This uses bulk AIL insertion techniques to
1283 * minimise lock traffic.
e34a314c
DC
1284 *
1285 * If we are called with the aborted flag set, it is because a log write during
1286 * a CIL checkpoint commit has failed. In this case, all the items in the
1287 * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
1288 * means that checkpoint commit abort handling is treated exactly the same
1289 * as an iclog write error even though we haven't started any IO yet. Hence in
1290 * this case all we need to do is IOP_COMMITTED processing, followed by an
1291 * IOP_UNPIN(aborted) call.
1d8c95a3
DC
1292 *
1293 * The AIL cursor is used to optimise the insert process. If commit_lsn is not
1294 * at the end of the AIL, the insert cursor avoids the need to walk
1295 * the AIL to find the insertion point on every xfs_log_item_batch_insert()
1296 * call. This saves a lot of needless list walking and is a net win, even
1297 * though it slightly increases that amount of AIL lock traffic to set it up
1298 * and tear it down.
0e57f6a3
DC
1299 */
1300void
1301xfs_trans_committed_bulk(
1302 struct xfs_ail *ailp,
1303 struct xfs_log_vec *log_vector,
1304 xfs_lsn_t commit_lsn,
1305 int aborted)
1306{
1307#define LOG_ITEM_BATCH_SIZE 32
1308 struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
1309 struct xfs_log_vec *lv;
1d8c95a3 1310 struct xfs_ail_cursor cur;
0e57f6a3
DC
1311 int i = 0;
1312
1d8c95a3
DC
1313 spin_lock(&ailp->xa_lock);
1314 xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
1315 spin_unlock(&ailp->xa_lock);
1316
0e57f6a3
DC
1317 /* unpin all the log items */
1318 for (lv = log_vector; lv; lv = lv->lv_next ) {
1319 struct xfs_log_item *lip = lv->lv_item;
1320 xfs_lsn_t item_lsn;
1321
1322 if (aborted)
1323 lip->li_flags |= XFS_LI_ABORTED;
1324 item_lsn = IOP_COMMITTED(lip, commit_lsn);
1325
1316d4da 1326 /* item_lsn of -1 means the item needs no further processing */
0e57f6a3
DC
1327 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
1328 continue;
1329
e34a314c
DC
1330 /*
1331 * if we are aborting the operation, no point in inserting the
1332 * object into the AIL as we are in a shutdown situation.
1333 */
1334 if (aborted) {
1335 ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
1336 IOP_UNPIN(lip, 1);
1337 continue;
1338 }
1339
0e57f6a3
DC
1340 if (item_lsn != commit_lsn) {
1341
1342 /*
1343 * Not a bulk update option due to unusual item_lsn.
1344 * Push into AIL immediately, rechecking the lsn once
1d8c95a3
DC
1345 * we have the ail lock. Then unpin the item. This does
1346 * not affect the AIL cursor the bulk insert path is
1347 * using.
0e57f6a3
DC
1348 */
1349 spin_lock(&ailp->xa_lock);
1350 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
1351 xfs_trans_ail_update(ailp, lip, item_lsn);
1352 else
1353 spin_unlock(&ailp->xa_lock);
1354 IOP_UNPIN(lip, 0);
1355 continue;
1356 }
1357
1358 /* Item is a candidate for bulk AIL insert. */
1359 log_items[i++] = lv->lv_item;
1360 if (i >= LOG_ITEM_BATCH_SIZE) {
1d8c95a3 1361 xfs_log_item_batch_insert(ailp, &cur, log_items,
0e57f6a3
DC
1362 LOG_ITEM_BATCH_SIZE, commit_lsn);
1363 i = 0;
1364 }
1365 }
1366
1367 /* make sure we insert the remainder! */
1368 if (i)
1d8c95a3
DC
1369 xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
1370
1371 spin_lock(&ailp->xa_lock);
1372 xfs_trans_ail_cursor_done(ailp, &cur);
1373 spin_unlock(&ailp->xa_lock);
0e57f6a3
DC
1374}
1375
0924378a 1376/*
b1037058 1377 * Commit the given transaction to the log.
0924378a
DC
1378 *
1379 * XFS disk error handling mechanism is not based on a typical
1380 * transaction abort mechanism. Logically after the filesystem
1381 * gets marked 'SHUTDOWN', we can't let any new transactions
1382 * be durable - ie. committed to disk - because some metadata might
1383 * be inconsistent. In such cases, this returns an error, and the
1384 * caller may assume that all locked objects joined to the transaction
1385 * have already been unlocked as if the commit had succeeded.
1386 * Do not reference the transaction structure after this call.
1387 */
0924378a 1388int
b1037058 1389xfs_trans_commit(
a3ccd2ca 1390 struct xfs_trans *tp,
b1037058 1391 uint flags)
0924378a 1392{
a3ccd2ca 1393 struct xfs_mount *mp = tp->t_mountp;
0924378a 1394 xfs_lsn_t commit_lsn = -1;
a3ccd2ca 1395 int error = 0;
0924378a
DC
1396 int log_flags = 0;
1397 int sync = tp->t_flags & XFS_TRANS_SYNC;
0924378a
DC
1398
1399 /*
1400 * Determine whether this commit is releasing a permanent
1401 * log reservation or not.
1402 */
1403 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1404 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1405 log_flags = XFS_LOG_REL_PERM_RESERV;
1406 }
1407
1408 /*
1409 * If there is nothing to be logged by the transaction,
1410 * then unlock all of the items associated with the
1411 * transaction and free the transaction structure.
1412 * Also make sure to return any reserved blocks to
1413 * the free pool.
1414 */
a3ccd2ca
CH
1415 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1416 goto out_unreserve;
1417
1418 if (XFS_FORCED_SHUTDOWN(mp)) {
1419 error = XFS_ERROR(EIO);
1420 goto out_unreserve;
0924378a 1421 }
a3ccd2ca 1422
0924378a
DC
1423 ASSERT(tp->t_ticket != NULL);
1424
1425 /*
1426 * If we need to update the superblock, then do it now.
1427 */
1428 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1429 xfs_trans_apply_sb_deltas(tp);
1430 xfs_trans_apply_dquot_deltas(tp);
1431
0244b960 1432 error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
0924378a
DC
1433 if (error == ENOMEM) {
1434 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
a3ccd2ca
CH
1435 error = XFS_ERROR(EIO);
1436 goto out_unreserve;
0924378a 1437 }
1da177e4 1438
0244b960
CH
1439 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1440 xfs_trans_free(tp);
1441
1da177e4
LT
1442 /*
1443 * If the transaction needs to be synchronous, then force the
1444 * log out now and wait for it.
1445 */
1446 if (sync) {
f538d4da 1447 if (!error) {
a14a348b 1448 error = _xfs_log_force_lsn(mp, commit_lsn,
b1037058 1449 XFS_LOG_SYNC, NULL);
f538d4da 1450 }
1da177e4
LT
1451 XFS_STATS_INC(xs_trans_sync);
1452 } else {
1453 XFS_STATS_INC(xs_trans_async);
1454 }
1455
a3ccd2ca
CH
1456 return error;
1457
1458out_unreserve:
1459 xfs_trans_unreserve_and_mod_sb(tp);
1460
1461 /*
1462 * It is indeed possible for the transaction to be not dirty but
1463 * the dqinfo portion to be. All that means is that we have some
1464 * (non-persistent) quota reservations that need to be unreserved.
1465 */
1466 xfs_trans_unreserve_and_mod_dquots(tp);
1467 if (tp->t_ticket) {
1468 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1469 if (commit_lsn == -1 && !error)
1470 error = XFS_ERROR(EIO);
1471 }
1472 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
71e330b5 1473 xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
a3ccd2ca
CH
1474 xfs_trans_free(tp);
1475
1476 XFS_STATS_INC(xs_trans_empty);
1477 return error;
1da177e4
LT
1478}
1479
1da177e4
LT
1480/*
1481 * Unlock all of the transaction's items and free the transaction.
1482 * The transaction must not have modified any of its items, because
1483 * there is no way to restore them to their previous state.
1484 *
1485 * If the transaction has made a log reservation, make sure to release
1486 * it as well.
1487 */
1488void
1489xfs_trans_cancel(
1490 xfs_trans_t *tp,
1491 int flags)
1492{
1493 int log_flags;
0733af21 1494 xfs_mount_t *mp = tp->t_mountp;
1da177e4
LT
1495
1496 /*
1497 * See if the caller is being too lazy to figure out if
1498 * the transaction really needs an abort.
1499 */
1500 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1501 flags &= ~XFS_TRANS_ABORT;
1502 /*
1503 * See if the caller is relying on us to shut down the
1504 * filesystem. This happens in paths where we detect
1505 * corruption and decide to give up.
1506 */
60a204f0 1507 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
0733af21 1508 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
7d04a335 1509 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
60a204f0 1510 }
1da177e4 1511#ifdef DEBUG
e98c414f
CH
1512 if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
1513 struct xfs_log_item_desc *lidp;
1514
1515 list_for_each_entry(lidp, &tp->t_items, lid_trans)
1516 ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
1da177e4
LT
1517 }
1518#endif
1519 xfs_trans_unreserve_and_mod_sb(tp);
7d095257 1520 xfs_trans_unreserve_and_mod_dquots(tp);
1da177e4
LT
1521
1522 if (tp->t_ticket) {
1523 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1524 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1525 log_flags = XFS_LOG_REL_PERM_RESERV;
1526 } else {
1527 log_flags = 0;
1528 }
0733af21 1529 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1da177e4
LT
1530 }
1531
1532 /* mark this thread as no longer being in a transaction */
59c1b082 1533 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4 1534
71e330b5 1535 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
1da177e4
LT
1536 xfs_trans_free(tp);
1537}
1538
322ff6b8
NS
1539/*
1540 * Roll from one trans in the sequence of PERMANENT transactions to
1541 * the next: permanent transactions are only flushed out when
1542 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1543 * as possible to let chunks of it go to the log. So we commit the
1544 * chunk we've been working on and get a new transaction to continue.
1545 */
1546int
1547xfs_trans_roll(
1548 struct xfs_trans **tpp,
1549 struct xfs_inode *dp)
1550{
1551 struct xfs_trans *trans;
1552 unsigned int logres, count;
1553 int error;
1554
1555 /*
1556 * Ensure that the inode is always logged.
1557 */
1558 trans = *tpp;
1559 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1560
1561 /*
1562 * Copy the critical parameters from one trans to the next.
1563 */
1564 logres = trans->t_log_res;
1565 count = trans->t_log_count;
1566 *tpp = xfs_trans_dup(trans);
1567
1568 /*
1569 * Commit the current transaction.
1570 * If this commit failed, then it'd just unlock those items that
1571 * are not marked ihold. That also means that a filesystem shutdown
1572 * is in progress. The caller takes the responsibility to cancel
1573 * the duplicate transaction that gets returned.
1574 */
1575 error = xfs_trans_commit(trans, 0);
1576 if (error)
1577 return (error);
1578
1579 trans = *tpp;
1580
cc09c0dc
DC
1581 /*
1582 * transaction commit worked ok so we can drop the extra ticket
1583 * reference that we gained in xfs_trans_dup()
1584 */
1585 xfs_log_ticket_put(trans->t_ticket);
1586
1587
322ff6b8
NS
1588 /*
1589 * Reserve space in the log for th next transaction.
1590 * This also pushes items in the "AIL", the list of logged items,
1591 * out to disk if they are taking up space at the tail of the log
1592 * that we want to use. This requires that either nothing be locked
1593 * across this call, or that anything that is locked be logged in
1594 * the prior and the next transactions.
1595 */
1596 error = xfs_trans_reserve(trans, 0, logres, 0,
1597 XFS_TRANS_PERM_LOG_RES, count);
1598 /*
1599 * Ensure that the inode is in the new transaction and locked.
1600 */
1601 if (error)
1602 return error;
1603
ddc3415a 1604 xfs_trans_ijoin(trans, dp, 0);
322ff6b8
NS
1605 return 0;
1606}
This page took 0.636629 seconds and 5 git commands to generate.