xfs: remove unneeded #include statements
[deliverable/linux.git] / fs / xfs / xfs_trans.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_mount.h"
28#include "xfs_error.h"
a844f451 29#include "xfs_da_btree.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
1da177e4
LT
33#include "xfs_dinode.h"
34#include "xfs_inode.h"
a844f451
NS
35#include "xfs_btree.h"
36#include "xfs_ialloc.h"
37#include "xfs_alloc.h"
1da177e4 38#include "xfs_bmap.h"
1da177e4 39#include "xfs_quota.h"
a844f451 40#include "xfs_trans_priv.h"
1da177e4 41#include "xfs_trans_space.h"
322ff6b8 42#include "xfs_inode_item.h"
ed3b4d6c 43#include "xfs_trace.h"
1da177e4 44
8f794055 45kmem_zone_t *xfs_trans_zone;
1da177e4 46
025101dc 47
8f794055 48/*
025101dc
CH
49 * Various log reservation values.
50 *
51 * These are based on the size of the file system block because that is what
52 * most transactions manipulate. Each adds in an additional 128 bytes per
53 * item logged to try to account for the overhead of the transaction mechanism.
54 *
55 * Note: Most of the reservations underestimate the number of allocation
56 * groups into which they could free extents in the xfs_bmap_finish() call.
57 * This is because the number in the worst case is quite high and quite
58 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
59 * extents in only a single AG at a time. This will require changes to the
60 * EFI code as well, however, so that the EFI for the extents not freed is
61 * logged again in each transaction. See SGI PV #261917.
62 *
63 * Reservation functions here avoid a huge stack in xfs_trans_init due to
64 * register overflow from temporaries in the calculations.
65 */
66
67
68/*
69 * In a write transaction we can allocate a maximum of 2
70 * extents. This gives:
71 * the inode getting the new extents: inode size
72 * the inode's bmap btree: max depth * block size
73 * the agfs of the ags from which the extents are allocated: 2 * sector
74 * the superblock free block counter: sector size
75 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
76 * And the bmap_finish transaction can free bmap blocks in a join:
77 * the agfs of the ags containing the blocks: 2 * sector size
78 * the agfls of the ags containing the blocks: 2 * sector size
79 * the super block free block counter: sector size
80 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
8f794055 81 */
8f794055 82STATIC uint
025101dc
CH
83xfs_calc_write_reservation(
84 struct xfs_mount *mp)
8f794055 85{
025101dc
CH
86 return XFS_DQUOT_LOGRES(mp) +
87 MAX((mp->m_sb.sb_inodesize +
88 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
89 2 * mp->m_sb.sb_sectsize +
90 mp->m_sb.sb_sectsize +
91 XFS_ALLOCFREE_LOG_RES(mp, 2) +
92 128 * (4 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
93 XFS_ALLOCFREE_LOG_COUNT(mp, 2))),
94 (2 * mp->m_sb.sb_sectsize +
95 2 * mp->m_sb.sb_sectsize +
96 mp->m_sb.sb_sectsize +
97 XFS_ALLOCFREE_LOG_RES(mp, 2) +
98 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
8f794055
NS
99}
100
025101dc
CH
101/*
102 * In truncating a file we free up to two extents at once. We can modify:
103 * the inode being truncated: inode size
104 * the inode's bmap btree: (max depth + 1) * block size
105 * And the bmap_finish transaction can free the blocks and bmap blocks:
106 * the agf for each of the ags: 4 * sector size
107 * the agfl for each of the ags: 4 * sector size
108 * the super block to reflect the freed blocks: sector size
109 * worst case split in allocation btrees per extent assuming 4 extents:
110 * 4 exts * 2 trees * (2 * max depth - 1) * block size
111 * the inode btree: max depth * blocksize
112 * the allocation btrees: 2 trees * (max depth - 1) * block size
113 */
8f794055 114STATIC uint
025101dc
CH
115xfs_calc_itruncate_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) + 1) +
121 128 * (2 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
122 (4 * mp->m_sb.sb_sectsize +
123 4 * mp->m_sb.sb_sectsize +
124 mp->m_sb.sb_sectsize +
125 XFS_ALLOCFREE_LOG_RES(mp, 4) +
126 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4)) +
127 128 * 5 +
128 XFS_ALLOCFREE_LOG_RES(mp, 1) +
129 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
130 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
131}
132
025101dc
CH
133/*
134 * In renaming a files we can modify:
135 * the four inodes involved: 4 * inode size
136 * the two directory btrees: 2 * (max depth + v2) * dir block size
137 * the two directory bmap btrees: 2 * max depth * block size
138 * And the bmap_finish transaction can free dir and bmap blocks (two sets
139 * of bmap blocks) giving:
140 * the agf for the ags in which the blocks live: 3 * sector size
141 * the agfl for the ags in which the blocks live: 3 * sector size
142 * the superblock for the free block count: sector size
143 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
144 */
8f794055 145STATIC uint
025101dc
CH
146xfs_calc_rename_reservation(
147 struct xfs_mount *mp)
8f794055 148{
025101dc
CH
149 return XFS_DQUOT_LOGRES(mp) +
150 MAX((4 * mp->m_sb.sb_inodesize +
151 2 * XFS_DIROP_LOG_RES(mp) +
152 128 * (4 + 2 * XFS_DIROP_LOG_COUNT(mp))),
153 (3 * mp->m_sb.sb_sectsize +
154 3 * mp->m_sb.sb_sectsize +
155 mp->m_sb.sb_sectsize +
156 XFS_ALLOCFREE_LOG_RES(mp, 3) +
157 128 * (7 + XFS_ALLOCFREE_LOG_COUNT(mp, 3))));
8f794055
NS
158}
159
025101dc
CH
160/*
161 * For creating a link to an inode:
162 * the parent directory inode: inode size
163 * the linked inode: inode size
164 * the directory btree could split: (max depth + v2) * dir block size
165 * the directory bmap btree could join or split: (max depth + v2) * blocksize
166 * And the bmap_finish transaction can free some bmap blocks giving:
167 * the agf for the ag in which the blocks live: sector size
168 * the agfl for the ag in which the blocks live: sector size
169 * the superblock for the free block count: sector size
170 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
171 */
8f794055 172STATIC uint
025101dc
CH
173xfs_calc_link_reservation(
174 struct xfs_mount *mp)
8f794055 175{
025101dc
CH
176 return XFS_DQUOT_LOGRES(mp) +
177 MAX((mp->m_sb.sb_inodesize +
178 mp->m_sb.sb_inodesize +
179 XFS_DIROP_LOG_RES(mp) +
180 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
181 (mp->m_sb.sb_sectsize +
182 mp->m_sb.sb_sectsize +
183 mp->m_sb.sb_sectsize +
184 XFS_ALLOCFREE_LOG_RES(mp, 1) +
185 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
186}
187
025101dc
CH
188/*
189 * For removing a directory entry we can modify:
190 * the parent directory inode: inode size
191 * the removed inode: inode size
192 * the directory btree could join: (max depth + v2) * dir block size
193 * the directory bmap btree could join or split: (max depth + v2) * blocksize
194 * And the bmap_finish transaction can free the dir and bmap blocks giving:
195 * the agf for the ag in which the blocks live: 2 * sector size
196 * the agfl for the ag in which the blocks live: 2 * sector size
197 * the superblock for the free block count: sector size
198 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
199 */
8f794055 200STATIC uint
025101dc
CH
201xfs_calc_remove_reservation(
202 struct xfs_mount *mp)
8f794055 203{
025101dc
CH
204 return XFS_DQUOT_LOGRES(mp) +
205 MAX((mp->m_sb.sb_inodesize +
206 mp->m_sb.sb_inodesize +
207 XFS_DIROP_LOG_RES(mp) +
208 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
209 (2 * mp->m_sb.sb_sectsize +
210 2 * mp->m_sb.sb_sectsize +
211 mp->m_sb.sb_sectsize +
212 XFS_ALLOCFREE_LOG_RES(mp, 2) +
213 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
8f794055
NS
214}
215
025101dc
CH
216/*
217 * For symlink we can modify:
218 * the parent directory inode: inode size
219 * the new inode: inode size
220 * the inode btree entry: 1 block
221 * the directory btree: (max depth + v2) * dir block size
222 * the directory inode's bmap btree: (max depth + v2) * block size
223 * the blocks for the symlink: 1 kB
224 * Or in the first xact we allocate some inodes giving:
225 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
226 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
227 * the inode btree: max depth * blocksize
228 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
229 */
8f794055 230STATIC uint
025101dc
CH
231xfs_calc_symlink_reservation(
232 struct xfs_mount *mp)
8f794055 233{
025101dc
CH
234 return XFS_DQUOT_LOGRES(mp) +
235 MAX((mp->m_sb.sb_inodesize +
236 mp->m_sb.sb_inodesize +
237 XFS_FSB_TO_B(mp, 1) +
238 XFS_DIROP_LOG_RES(mp) +
239 1024 +
240 128 * (4 + XFS_DIROP_LOG_COUNT(mp))),
241 (2 * mp->m_sb.sb_sectsize +
242 XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
243 XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
244 XFS_ALLOCFREE_LOG_RES(mp, 1) +
245 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
246 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
247}
248
025101dc
CH
249/*
250 * For create we can modify:
251 * the parent directory inode: inode size
252 * the new inode: inode size
253 * the inode btree entry: block size
254 * the superblock for the nlink flag: sector size
255 * the directory btree: (max depth + v2) * dir block size
256 * the directory inode's bmap btree: (max depth + v2) * block size
257 * Or in the first xact we allocate some inodes giving:
258 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
259 * the superblock for the nlink flag: sector size
260 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
261 * the inode btree: max depth * blocksize
262 * the allocation btrees: 2 trees * (max depth - 1) * block size
263 */
8f794055 264STATIC uint
025101dc
CH
265xfs_calc_create_reservation(
266 struct xfs_mount *mp)
8f794055 267{
025101dc
CH
268 return XFS_DQUOT_LOGRES(mp) +
269 MAX((mp->m_sb.sb_inodesize +
270 mp->m_sb.sb_inodesize +
271 mp->m_sb.sb_sectsize +
272 XFS_FSB_TO_B(mp, 1) +
273 XFS_DIROP_LOG_RES(mp) +
274 128 * (3 + XFS_DIROP_LOG_COUNT(mp))),
275 (3 * mp->m_sb.sb_sectsize +
276 XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
277 XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
278 XFS_ALLOCFREE_LOG_RES(mp, 1) +
279 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
280 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
8f794055
NS
281}
282
025101dc
CH
283/*
284 * Making a new directory is the same as creating a new file.
285 */
8f794055 286STATIC uint
025101dc
CH
287xfs_calc_mkdir_reservation(
288 struct xfs_mount *mp)
8f794055 289{
025101dc 290 return xfs_calc_create_reservation(mp);
8f794055
NS
291}
292
025101dc
CH
293/*
294 * In freeing an inode we can modify:
295 * the inode being freed: inode size
296 * the super block free inode counter: sector size
297 * the agi hash list and counters: sector size
298 * the inode btree entry: block size
299 * the on disk inode before ours in the agi hash list: inode cluster size
300 * the inode btree: max depth * blocksize
301 * the allocation btrees: 2 trees * (max depth - 1) * block size
302 */
8f794055 303STATIC uint
025101dc
CH
304xfs_calc_ifree_reservation(
305 struct xfs_mount *mp)
8f794055 306{
025101dc
CH
307 return XFS_DQUOT_LOGRES(mp) +
308 mp->m_sb.sb_inodesize +
309 mp->m_sb.sb_sectsize +
310 mp->m_sb.sb_sectsize +
311 XFS_FSB_TO_B(mp, 1) +
312 MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
313 XFS_INODE_CLUSTER_SIZE(mp)) +
314 128 * 5 +
315 XFS_ALLOCFREE_LOG_RES(mp, 1) +
316 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
317 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
318}
319
025101dc
CH
320/*
321 * When only changing the inode we log the inode and possibly the superblock
322 * We also add a bit of slop for the transaction stuff.
323 */
8f794055 324STATIC uint
025101dc
CH
325xfs_calc_ichange_reservation(
326 struct xfs_mount *mp)
8f794055 327{
025101dc
CH
328 return XFS_DQUOT_LOGRES(mp) +
329 mp->m_sb.sb_inodesize +
330 mp->m_sb.sb_sectsize +
331 512;
332
8f794055
NS
333}
334
025101dc
CH
335/*
336 * Growing the data section of the filesystem.
337 * superblock
338 * agi and agf
339 * allocation btrees
340 */
8f794055 341STATIC uint
025101dc
CH
342xfs_calc_growdata_reservation(
343 struct xfs_mount *mp)
8f794055 344{
025101dc
CH
345 return mp->m_sb.sb_sectsize * 3 +
346 XFS_ALLOCFREE_LOG_RES(mp, 1) +
347 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
348}
349
025101dc
CH
350/*
351 * Growing the rt section of the filesystem.
352 * In the first set of transactions (ALLOC) we allocate space to the
353 * bitmap or summary files.
354 * superblock: sector size
355 * agf of the ag from which the extent is allocated: sector size
356 * bmap btree for bitmap/summary inode: max depth * blocksize
357 * bitmap/summary inode: inode size
358 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
359 */
8f794055 360STATIC uint
025101dc
CH
361xfs_calc_growrtalloc_reservation(
362 struct xfs_mount *mp)
8f794055 363{
025101dc
CH
364 return 2 * mp->m_sb.sb_sectsize +
365 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
366 mp->m_sb.sb_inodesize +
367 XFS_ALLOCFREE_LOG_RES(mp, 1) +
368 128 * (3 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
369 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
370}
371
025101dc
CH
372/*
373 * Growing the rt section of the filesystem.
374 * In the second set of transactions (ZERO) we zero the new metadata blocks.
375 * one bitmap/summary block: blocksize
376 */
8f794055 377STATIC uint
025101dc
CH
378xfs_calc_growrtzero_reservation(
379 struct xfs_mount *mp)
8f794055 380{
025101dc 381 return mp->m_sb.sb_blocksize + 128;
8f794055
NS
382}
383
025101dc
CH
384/*
385 * Growing the rt section of the filesystem.
386 * In the third set of transactions (FREE) we update metadata without
387 * allocating any new blocks.
388 * superblock: sector size
389 * bitmap inode: inode size
390 * summary inode: inode size
391 * one bitmap block: blocksize
392 * summary blocks: new summary size
393 */
8f794055 394STATIC uint
025101dc
CH
395xfs_calc_growrtfree_reservation(
396 struct xfs_mount *mp)
8f794055 397{
025101dc
CH
398 return mp->m_sb.sb_sectsize +
399 2 * mp->m_sb.sb_inodesize +
400 mp->m_sb.sb_blocksize +
401 mp->m_rsumsize +
402 128 * 5;
8f794055
NS
403}
404
025101dc
CH
405/*
406 * Logging the inode modification timestamp on a synchronous write.
407 * inode
408 */
8f794055 409STATIC uint
025101dc
CH
410xfs_calc_swrite_reservation(
411 struct xfs_mount *mp)
8f794055 412{
025101dc 413 return mp->m_sb.sb_inodesize + 128;
8f794055
NS
414}
415
025101dc
CH
416/*
417 * Logging the inode mode bits when writing a setuid/setgid file
418 * inode
419 */
8f794055
NS
420STATIC uint
421xfs_calc_writeid_reservation(xfs_mount_t *mp)
422{
025101dc 423 return mp->m_sb.sb_inodesize + 128;
8f794055
NS
424}
425
025101dc
CH
426/*
427 * Converting the inode from non-attributed to attributed.
428 * the inode being converted: inode size
429 * agf block and superblock (for block allocation)
430 * the new block (directory sized)
431 * bmap blocks for the new directory block
432 * allocation btrees
433 */
8f794055 434STATIC uint
025101dc
CH
435xfs_calc_addafork_reservation(
436 struct xfs_mount *mp)
8f794055 437{
025101dc
CH
438 return XFS_DQUOT_LOGRES(mp) +
439 mp->m_sb.sb_inodesize +
440 mp->m_sb.sb_sectsize * 2 +
441 mp->m_dirblksize +
442 XFS_FSB_TO_B(mp, XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1) +
443 XFS_ALLOCFREE_LOG_RES(mp, 1) +
444 128 * (4 + XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1 +
445 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
8f794055
NS
446}
447
025101dc
CH
448/*
449 * Removing the attribute fork of a file
450 * the inode being truncated: inode size
451 * the inode's bmap btree: max depth * block size
452 * And the bmap_finish transaction can free the blocks and bmap blocks:
453 * the agf for each of the ags: 4 * sector size
454 * the agfl for each of the ags: 4 * sector size
455 * the super block to reflect the freed blocks: sector size
456 * worst case split in allocation btrees per extent assuming 4 extents:
457 * 4 exts * 2 trees * (2 * max depth - 1) * block size
458 */
8f794055 459STATIC uint
025101dc
CH
460xfs_calc_attrinval_reservation(
461 struct xfs_mount *mp)
8f794055 462{
025101dc
CH
463 return MAX((mp->m_sb.sb_inodesize +
464 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
465 128 * (1 + XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK))),
466 (4 * mp->m_sb.sb_sectsize +
467 4 * mp->m_sb.sb_sectsize +
468 mp->m_sb.sb_sectsize +
469 XFS_ALLOCFREE_LOG_RES(mp, 4) +
470 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4))));
8f794055
NS
471}
472
025101dc
CH
473/*
474 * Setting an attribute.
475 * the inode getting the attribute
476 * the superblock for allocations
477 * the agfs extents are allocated from
478 * the attribute btree * max depth
479 * the inode allocation btree
480 * Since attribute transaction space is dependent on the size of the attribute,
481 * the calculation is done partially at mount time and partially at runtime.
482 */
8f794055 483STATIC uint
025101dc
CH
484xfs_calc_attrset_reservation(
485 struct xfs_mount *mp)
8f794055 486{
025101dc
CH
487 return XFS_DQUOT_LOGRES(mp) +
488 mp->m_sb.sb_inodesize +
489 mp->m_sb.sb_sectsize +
490 XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
491 128 * (2 + XFS_DA_NODE_MAXDEPTH);
8f794055
NS
492}
493
025101dc
CH
494/*
495 * Removing an attribute.
496 * the inode: inode size
497 * the attribute btree could join: max depth * block size
498 * the inode bmap btree could join or split: max depth * block size
499 * And the bmap_finish transaction can free the attr blocks freed giving:
500 * the agf for the ag in which the blocks live: 2 * sector size
501 * the agfl for the ag in which the blocks live: 2 * sector size
502 * the superblock for the free block count: sector size
503 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
504 */
8f794055 505STATIC uint
025101dc
CH
506xfs_calc_attrrm_reservation(
507 struct xfs_mount *mp)
8f794055 508{
025101dc
CH
509 return XFS_DQUOT_LOGRES(mp) +
510 MAX((mp->m_sb.sb_inodesize +
511 XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
512 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
513 128 * (1 + XFS_DA_NODE_MAXDEPTH +
514 XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
515 (2 * mp->m_sb.sb_sectsize +
516 2 * mp->m_sb.sb_sectsize +
517 mp->m_sb.sb_sectsize +
518 XFS_ALLOCFREE_LOG_RES(mp, 2) +
519 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
8f794055
NS
520}
521
025101dc
CH
522/*
523 * Clearing a bad agino number in an agi hash bucket.
524 */
8f794055 525STATIC uint
025101dc
CH
526xfs_calc_clear_agi_bucket_reservation(
527 struct xfs_mount *mp)
8f794055 528{
025101dc 529 return mp->m_sb.sb_sectsize + 128;
8f794055
NS
530}
531
1da177e4
LT
532/*
533 * Initialize the precomputed transaction reservation values
534 * in the mount structure.
535 */
536void
537xfs_trans_init(
025101dc 538 struct xfs_mount *mp)
1da177e4 539{
025101dc 540 struct xfs_trans_reservations *resp = &mp->m_reservations;
1da177e4 541
8f794055
NS
542 resp->tr_write = xfs_calc_write_reservation(mp);
543 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
544 resp->tr_rename = xfs_calc_rename_reservation(mp);
545 resp->tr_link = xfs_calc_link_reservation(mp);
546 resp->tr_remove = xfs_calc_remove_reservation(mp);
547 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
548 resp->tr_create = xfs_calc_create_reservation(mp);
549 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
550 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
551 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
552 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
553 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
554 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
555 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
556 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
557 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
558 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
559 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
560 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
561 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
562 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
1da177e4
LT
563}
564
565/*
566 * This routine is called to allocate a transaction structure.
567 * The type parameter indicates the type of the transaction. These
568 * are enumerated in xfs_trans.h.
569 *
570 * Dynamically allocate the transaction structure from the transaction
571 * zone, initialize it, and return it to the caller.
572 */
573xfs_trans_t *
574xfs_trans_alloc(
575 xfs_mount_t *mp,
576 uint type)
577{
b267ce99 578 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
80641dc6 579 return _xfs_trans_alloc(mp, type, KM_SLEEP);
1da177e4
LT
580}
581
582xfs_trans_t *
583_xfs_trans_alloc(
584 xfs_mount_t *mp,
80641dc6
CH
585 uint type,
586 uint memflags)
1da177e4
LT
587{
588 xfs_trans_t *tp;
589
34327e13 590 atomic_inc(&mp->m_active_trans);
1da177e4 591
80641dc6 592 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
1da177e4
LT
593 tp->t_magic = XFS_TRANS_MAGIC;
594 tp->t_type = type;
595 tp->t_mountp = mp;
596 tp->t_items_free = XFS_LIC_NUM_SLOTS;
39dab9d7 597 xfs_lic_init(&(tp->t_items));
ed3b4d6c 598 INIT_LIST_HEAD(&tp->t_busy);
34327e13 599 return tp;
1da177e4
LT
600}
601
b1c1b5b6
DC
602/*
603 * Free the transaction structure. If there is more clean up
604 * to do when the structure is freed, add it here.
605 */
606STATIC void
607xfs_trans_free(
ed3b4d6c 608 struct xfs_trans *tp)
b1c1b5b6 609{
ed3b4d6c
DC
610 struct xfs_busy_extent *busyp, *n;
611
612 list_for_each_entry_safe(busyp, n, &tp->t_busy, list)
613 xfs_alloc_busy_clear(tp->t_mountp, busyp);
614
b1c1b5b6
DC
615 atomic_dec(&tp->t_mountp->m_active_trans);
616 xfs_trans_free_dqinfo(tp);
617 kmem_zone_free(xfs_trans_zone, tp);
618}
619
1da177e4
LT
620/*
621 * This is called to create a new transaction which will share the
622 * permanent log reservation of the given transaction. The remaining
623 * unused block and rt extent reservations are also inherited. This
624 * implies that the original transaction is no longer allowed to allocate
625 * blocks. Locks and log items, however, are no inherited. They must
626 * be added to the new transaction explicitly.
627 */
628xfs_trans_t *
629xfs_trans_dup(
630 xfs_trans_t *tp)
631{
632 xfs_trans_t *ntp;
633
634 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
635
636 /*
637 * Initialize the new transaction structure.
638 */
639 ntp->t_magic = XFS_TRANS_MAGIC;
640 ntp->t_type = tp->t_type;
641 ntp->t_mountp = tp->t_mountp;
642 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
39dab9d7 643 xfs_lic_init(&(ntp->t_items));
ed3b4d6c 644 INIT_LIST_HEAD(&ntp->t_busy);
1da177e4
LT
645
646 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1da177e4 647 ASSERT(tp->t_ticket != NULL);
cfcbbbd0 648
1da177e4 649 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
cc09c0dc 650 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
1da177e4
LT
651 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
652 tp->t_blk_res = tp->t_blk_res_used;
653 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
654 tp->t_rtx_res = tp->t_rtx_res_used;
59c1b082 655 ntp->t_pflags = tp->t_pflags;
1da177e4 656
7d095257 657 xfs_trans_dup_dqinfo(tp, ntp);
1da177e4
LT
658
659 atomic_inc(&tp->t_mountp->m_active_trans);
660 return ntp;
661}
662
663/*
664 * This is called to reserve free disk blocks and log space for the
665 * given transaction. This must be done before allocating any resources
666 * within the transaction.
667 *
668 * This will return ENOSPC if there are not enough blocks available.
669 * It will sleep waiting for available log space.
670 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
671 * is used by long running transactions. If any one of the reservations
672 * fails then they will all be backed out.
673 *
674 * This does not do quota reservations. That typically is done by the
675 * caller afterwards.
676 */
677int
678xfs_trans_reserve(
679 xfs_trans_t *tp,
680 uint blocks,
681 uint logspace,
682 uint rtextents,
683 uint flags,
684 uint logcount)
685{
686 int log_flags;
59c1b082
NS
687 int error = 0;
688 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1da177e4
LT
689
690 /* Mark this thread as being in a transaction */
59c1b082 691 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4
LT
692
693 /*
694 * Attempt to reserve the needed disk blocks by decrementing
695 * the number needed from the number available. This will
696 * fail if the count would go below zero.
697 */
698 if (blocks > 0) {
699 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
20f4ebf2 700 -((int64_t)blocks), rsvd);
1da177e4 701 if (error != 0) {
59c1b082 702 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4
LT
703 return (XFS_ERROR(ENOSPC));
704 }
705 tp->t_blk_res += blocks;
706 }
707
708 /*
709 * Reserve the log space needed for this transaction.
710 */
711 if (logspace > 0) {
712 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
713 ASSERT((tp->t_log_count == 0) ||
714 (tp->t_log_count == logcount));
715 if (flags & XFS_TRANS_PERM_LOG_RES) {
716 log_flags = XFS_LOG_PERM_RESERV;
717 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
718 } else {
719 ASSERT(tp->t_ticket == NULL);
720 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
721 log_flags = 0;
722 }
723
724 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
725 &tp->t_ticket,
7e9c6396 726 XFS_TRANSACTION, log_flags, tp->t_type);
1da177e4
LT
727 if (error) {
728 goto undo_blocks;
729 }
730 tp->t_log_res = logspace;
731 tp->t_log_count = logcount;
732 }
733
734 /*
735 * Attempt to reserve the needed realtime extents by decrementing
736 * the number needed from the number available. This will
737 * fail if the count would go below zero.
738 */
739 if (rtextents > 0) {
740 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
20f4ebf2 741 -((int64_t)rtextents), rsvd);
1da177e4
LT
742 if (error) {
743 error = XFS_ERROR(ENOSPC);
744 goto undo_log;
745 }
746 tp->t_rtx_res += rtextents;
747 }
748
749 return 0;
750
751 /*
752 * Error cases jump to one of these labels to undo any
753 * reservations which have already been performed.
754 */
755undo_log:
756 if (logspace > 0) {
757 if (flags & XFS_TRANS_PERM_LOG_RES) {
758 log_flags = XFS_LOG_REL_PERM_RESERV;
759 } else {
760 log_flags = 0;
761 }
762 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
763 tp->t_ticket = NULL;
764 tp->t_log_res = 0;
765 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
766 }
767
768undo_blocks:
769 if (blocks > 0) {
770 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
20f4ebf2 771 (int64_t)blocks, rsvd);
1da177e4
LT
772 tp->t_blk_res = 0;
773 }
774
59c1b082 775 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4 776
59c1b082 777 return error;
1da177e4
LT
778}
779
1da177e4
LT
780/*
781 * Record the indicated change to the given field for application
782 * to the file system's superblock when the transaction commits.
783 * For now, just store the change in the transaction structure.
784 *
785 * Mark the transaction structure to indicate that the superblock
786 * needs to be updated before committing.
92821e2b
DC
787 *
788 * Because we may not be keeping track of allocated/free inodes and
789 * used filesystem blocks in the superblock, we do not mark the
790 * superblock dirty in this transaction if we modify these fields.
791 * We still need to update the transaction deltas so that they get
792 * applied to the incore superblock, but we don't want them to
793 * cause the superblock to get locked and logged if these are the
794 * only fields in the superblock that the transaction modifies.
1da177e4
LT
795 */
796void
797xfs_trans_mod_sb(
798 xfs_trans_t *tp,
799 uint field,
20f4ebf2 800 int64_t delta)
1da177e4 801{
92821e2b
DC
802 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
803 xfs_mount_t *mp = tp->t_mountp;
1da177e4
LT
804
805 switch (field) {
806 case XFS_TRANS_SB_ICOUNT:
807 tp->t_icount_delta += delta;
92821e2b
DC
808 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
809 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
810 break;
811 case XFS_TRANS_SB_IFREE:
812 tp->t_ifree_delta += delta;
92821e2b
DC
813 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
814 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
815 break;
816 case XFS_TRANS_SB_FDBLOCKS:
817 /*
818 * Track the number of blocks allocated in the
819 * transaction. Make sure it does not exceed the
820 * number reserved.
821 */
822 if (delta < 0) {
823 tp->t_blk_res_used += (uint)-delta;
824 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
825 }
826 tp->t_fdblocks_delta += delta;
92821e2b
DC
827 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
828 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
829 break;
830 case XFS_TRANS_SB_RES_FDBLOCKS:
831 /*
832 * The allocation has already been applied to the
833 * in-core superblock's counter. This should only
834 * be applied to the on-disk superblock.
835 */
836 ASSERT(delta < 0);
837 tp->t_res_fdblocks_delta += delta;
92821e2b
DC
838 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
839 flags &= ~XFS_TRANS_SB_DIRTY;
1da177e4
LT
840 break;
841 case XFS_TRANS_SB_FREXTENTS:
842 /*
843 * Track the number of blocks allocated in the
844 * transaction. Make sure it does not exceed the
845 * number reserved.
846 */
847 if (delta < 0) {
848 tp->t_rtx_res_used += (uint)-delta;
849 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
850 }
851 tp->t_frextents_delta += delta;
852 break;
853 case XFS_TRANS_SB_RES_FREXTENTS:
854 /*
855 * The allocation has already been applied to the
c41564b5 856 * in-core superblock's counter. This should only
1da177e4
LT
857 * be applied to the on-disk superblock.
858 */
859 ASSERT(delta < 0);
860 tp->t_res_frextents_delta += delta;
861 break;
862 case XFS_TRANS_SB_DBLOCKS:
863 ASSERT(delta > 0);
864 tp->t_dblocks_delta += delta;
865 break;
866 case XFS_TRANS_SB_AGCOUNT:
867 ASSERT(delta > 0);
868 tp->t_agcount_delta += delta;
869 break;
870 case XFS_TRANS_SB_IMAXPCT:
871 tp->t_imaxpct_delta += delta;
872 break;
873 case XFS_TRANS_SB_REXTSIZE:
874 tp->t_rextsize_delta += delta;
875 break;
876 case XFS_TRANS_SB_RBMBLOCKS:
877 tp->t_rbmblocks_delta += delta;
878 break;
879 case XFS_TRANS_SB_RBLOCKS:
880 tp->t_rblocks_delta += delta;
881 break;
882 case XFS_TRANS_SB_REXTENTS:
883 tp->t_rextents_delta += delta;
884 break;
885 case XFS_TRANS_SB_REXTSLOG:
886 tp->t_rextslog_delta += delta;
887 break;
888 default:
889 ASSERT(0);
890 return;
891 }
892
210c6f1c 893 tp->t_flags |= flags;
1da177e4
LT
894}
895
896/*
897 * xfs_trans_apply_sb_deltas() is called from the commit code
898 * to bring the superblock buffer into the current transaction
899 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
900 *
901 * For now we just look at each field allowed to change and change
902 * it if necessary.
903 */
904STATIC void
905xfs_trans_apply_sb_deltas(
906 xfs_trans_t *tp)
907{
2bdf7cd0 908 xfs_dsb_t *sbp;
1da177e4
LT
909 xfs_buf_t *bp;
910 int whole = 0;
911
912 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
913 sbp = XFS_BUF_TO_SBP(bp);
914
915 /*
916 * Check that superblock mods match the mods made to AGF counters.
917 */
918 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
919 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
920 tp->t_ag_btree_delta));
921
92821e2b
DC
922 /*
923 * Only update the superblock counters if we are logging them
924 */
925 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
2bdf7cd0 926 if (tp->t_icount_delta)
413d57c9 927 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
2bdf7cd0 928 if (tp->t_ifree_delta)
413d57c9 929 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
2bdf7cd0 930 if (tp->t_fdblocks_delta)
413d57c9 931 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
2bdf7cd0 932 if (tp->t_res_fdblocks_delta)
413d57c9 933 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
1da177e4
LT
934 }
935
2bdf7cd0 936 if (tp->t_frextents_delta)
413d57c9 937 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
2bdf7cd0 938 if (tp->t_res_frextents_delta)
413d57c9 939 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
2bdf7cd0
CH
940
941 if (tp->t_dblocks_delta) {
413d57c9 942 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
1da177e4
LT
943 whole = 1;
944 }
2bdf7cd0 945 if (tp->t_agcount_delta) {
413d57c9 946 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
1da177e4
LT
947 whole = 1;
948 }
2bdf7cd0
CH
949 if (tp->t_imaxpct_delta) {
950 sbp->sb_imax_pct += tp->t_imaxpct_delta;
1da177e4
LT
951 whole = 1;
952 }
2bdf7cd0 953 if (tp->t_rextsize_delta) {
413d57c9 954 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
1da177e4
LT
955 whole = 1;
956 }
2bdf7cd0 957 if (tp->t_rbmblocks_delta) {
413d57c9 958 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
1da177e4
LT
959 whole = 1;
960 }
2bdf7cd0 961 if (tp->t_rblocks_delta) {
413d57c9 962 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
1da177e4
LT
963 whole = 1;
964 }
2bdf7cd0 965 if (tp->t_rextents_delta) {
413d57c9 966 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
1da177e4
LT
967 whole = 1;
968 }
2bdf7cd0
CH
969 if (tp->t_rextslog_delta) {
970 sbp->sb_rextslog += tp->t_rextslog_delta;
1da177e4
LT
971 whole = 1;
972 }
973
974 if (whole)
975 /*
c41564b5 976 * Log the whole thing, the fields are noncontiguous.
1da177e4 977 */
2bdf7cd0 978 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
1da177e4
LT
979 else
980 /*
981 * Since all the modifiable fields are contiguous, we
982 * can get away with this.
983 */
2bdf7cd0
CH
984 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
985 offsetof(xfs_dsb_t, sb_frextents) +
1da177e4 986 sizeof(sbp->sb_frextents) - 1);
1da177e4
LT
987}
988
989/*
45c34141
DC
990 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
991 * and apply superblock counter changes to the in-core superblock. The
992 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
993 * applied to the in-core superblock. The idea is that that has already been
994 * done.
1da177e4
LT
995 *
996 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
45c34141
DC
997 * However, we have to ensure that we only modify each superblock field only
998 * once because the application of the delta values may not be atomic. That can
999 * lead to ENOSPC races occurring if we have two separate modifcations of the
1000 * free space counter to put back the entire reservation and then take away
1001 * what we used.
1002 *
1003 * If we are not logging superblock counters, then the inode allocated/free and
1004 * used block counts are not updated in the on disk superblock. In this case,
1005 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
1006 * still need to update the incore superblock with the changes.
1da177e4 1007 */
71e330b5 1008void
1da177e4
LT
1009xfs_trans_unreserve_and_mod_sb(
1010 xfs_trans_t *tp)
1011{
1012 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
1013 xfs_mod_sb_t *msbp;
92821e2b 1014 xfs_mount_t *mp = tp->t_mountp;
1da177e4
LT
1015 /* REFERENCED */
1016 int error;
1017 int rsvd;
45c34141
DC
1018 int64_t blkdelta = 0;
1019 int64_t rtxdelta = 0;
1da177e4
LT
1020
1021 msbp = msb;
1022 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1023
45c34141
DC
1024 /* calculate free blocks delta */
1025 if (tp->t_blk_res > 0)
1026 blkdelta = tp->t_blk_res;
1027
1028 if ((tp->t_fdblocks_delta != 0) &&
1029 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1030 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
1031 blkdelta += tp->t_fdblocks_delta;
1032
1033 if (blkdelta != 0) {
1da177e4 1034 msbp->msb_field = XFS_SBS_FDBLOCKS;
45c34141 1035 msbp->msb_delta = blkdelta;
1da177e4
LT
1036 msbp++;
1037 }
1038
45c34141
DC
1039 /* calculate free realtime extents delta */
1040 if (tp->t_rtx_res > 0)
1041 rtxdelta = tp->t_rtx_res;
1042
1043 if ((tp->t_frextents_delta != 0) &&
1044 (tp->t_flags & XFS_TRANS_SB_DIRTY))
1045 rtxdelta += tp->t_frextents_delta;
1046
1047 if (rtxdelta != 0) {
1da177e4 1048 msbp->msb_field = XFS_SBS_FREXTENTS;
45c34141 1049 msbp->msb_delta = rtxdelta;
1da177e4
LT
1050 msbp++;
1051 }
1052
45c34141
DC
1053 /* apply remaining deltas */
1054
92821e2b
DC
1055 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1056 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
1da177e4
LT
1057 if (tp->t_icount_delta != 0) {
1058 msbp->msb_field = XFS_SBS_ICOUNT;
20f4ebf2 1059 msbp->msb_delta = tp->t_icount_delta;
1da177e4
LT
1060 msbp++;
1061 }
1062 if (tp->t_ifree_delta != 0) {
1063 msbp->msb_field = XFS_SBS_IFREE;
20f4ebf2 1064 msbp->msb_delta = tp->t_ifree_delta;
1da177e4
LT
1065 msbp++;
1066 }
92821e2b
DC
1067 }
1068
1069 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
1da177e4
LT
1070 if (tp->t_dblocks_delta != 0) {
1071 msbp->msb_field = XFS_SBS_DBLOCKS;
20f4ebf2 1072 msbp->msb_delta = tp->t_dblocks_delta;
1da177e4
LT
1073 msbp++;
1074 }
1075 if (tp->t_agcount_delta != 0) {
1076 msbp->msb_field = XFS_SBS_AGCOUNT;
20f4ebf2 1077 msbp->msb_delta = tp->t_agcount_delta;
1da177e4
LT
1078 msbp++;
1079 }
1080 if (tp->t_imaxpct_delta != 0) {
1081 msbp->msb_field = XFS_SBS_IMAX_PCT;
20f4ebf2 1082 msbp->msb_delta = tp->t_imaxpct_delta;
1da177e4
LT
1083 msbp++;
1084 }
1085 if (tp->t_rextsize_delta != 0) {
1086 msbp->msb_field = XFS_SBS_REXTSIZE;
20f4ebf2 1087 msbp->msb_delta = tp->t_rextsize_delta;
1da177e4
LT
1088 msbp++;
1089 }
1090 if (tp->t_rbmblocks_delta != 0) {
1091 msbp->msb_field = XFS_SBS_RBMBLOCKS;
20f4ebf2 1092 msbp->msb_delta = tp->t_rbmblocks_delta;
1da177e4
LT
1093 msbp++;
1094 }
1095 if (tp->t_rblocks_delta != 0) {
1096 msbp->msb_field = XFS_SBS_RBLOCKS;
20f4ebf2 1097 msbp->msb_delta = tp->t_rblocks_delta;
1da177e4
LT
1098 msbp++;
1099 }
1100 if (tp->t_rextents_delta != 0) {
1101 msbp->msb_field = XFS_SBS_REXTENTS;
20f4ebf2 1102 msbp->msb_delta = tp->t_rextents_delta;
1da177e4
LT
1103 msbp++;
1104 }
1105 if (tp->t_rextslog_delta != 0) {
1106 msbp->msb_field = XFS_SBS_REXTSLOG;
20f4ebf2 1107 msbp->msb_delta = tp->t_rextslog_delta;
1da177e4
LT
1108 msbp++;
1109 }
1110 }
1111
1112 /*
1113 * If we need to change anything, do it.
1114 */
1115 if (msbp > msb) {
1116 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
1117 (uint)(msbp - msb), rsvd);
1118 ASSERT(error == 0);
1119 }
1120}
1121
1da177e4 1122/*
0924378a
DC
1123 * Total up the number of log iovecs needed to commit this
1124 * transaction. The transaction itself needs one for the
1125 * transaction header. Ask each dirty item in turn how many
1126 * it needs to get the total.
1da177e4 1127 */
0924378a
DC
1128static uint
1129xfs_trans_count_vecs(
b1c1b5b6 1130 struct xfs_trans *tp)
1da177e4 1131{
0924378a
DC
1132 int nvecs;
1133 xfs_log_item_desc_t *lidp;
1da177e4 1134
0924378a
DC
1135 nvecs = 1;
1136 lidp = xfs_trans_first_item(tp);
1137 ASSERT(lidp != NULL);
1da177e4 1138
0924378a
DC
1139 /* In the non-debug case we need to start bailing out if we
1140 * didn't find a log_item here, return zero and let trans_commit
1141 * deal with it.
1da177e4 1142 */
0924378a
DC
1143 if (lidp == NULL)
1144 return 0;
1145
1146 while (lidp != NULL) {
1147 /*
1148 * Skip items which aren't dirty in this transaction.
1149 */
1150 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1151 lidp = xfs_trans_next_item(tp, lidp);
1152 continue;
1153 }
1154 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1155 nvecs += lidp->lid_size;
1156 lidp = xfs_trans_next_item(tp, lidp);
1da177e4 1157 }
0924378a
DC
1158
1159 return nvecs;
1160}
1161
1162/*
1163 * Fill in the vector with pointers to data to be logged
1164 * by this transaction. The transaction header takes
1165 * the first vector, and then each dirty item takes the
1166 * number of vectors it indicated it needed in xfs_trans_count_vecs().
1167 *
1168 * As each item fills in the entries it needs, also pin the item
1169 * so that it cannot be flushed out until the log write completes.
1170 */
1171static void
1172xfs_trans_fill_vecs(
1173 struct xfs_trans *tp,
1174 struct xfs_log_iovec *log_vector)
1175{
1176 xfs_log_item_desc_t *lidp;
1177 struct xfs_log_iovec *vecp;
1178 uint nitems;
1da177e4
LT
1179
1180 /*
0924378a
DC
1181 * Skip over the entry for the transaction header, we'll
1182 * fill that in at the end.
1da177e4 1183 */
0924378a
DC
1184 vecp = log_vector + 1;
1185
1186 nitems = 0;
1187 lidp = xfs_trans_first_item(tp);
1188 ASSERT(lidp);
1189 while (lidp) {
1190 /* Skip items which aren't dirty in this transaction. */
1191 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1192 lidp = xfs_trans_next_item(tp, lidp);
1193 continue;
1194 }
1195
1da177e4 1196 /*
0924378a
DC
1197 * The item may be marked dirty but not log anything. This can
1198 * be used to get called when a transaction is committed.
1da177e4 1199 */
0924378a
DC
1200 if (lidp->lid_size)
1201 nitems++;
1202 IOP_FORMAT(lidp->lid_item, vecp);
1203 vecp += lidp->lid_size;
1204 IOP_PIN(lidp->lid_item);
1205 lidp = xfs_trans_next_item(tp, lidp);
1da177e4 1206 }
1da177e4
LT
1207
1208 /*
0924378a
DC
1209 * Now that we've counted the number of items in this transaction, fill
1210 * in the transaction header. Note that the transaction header does not
1211 * have a log item.
1da177e4 1212 */
0924378a
DC
1213 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1214 tp->t_header.th_type = tp->t_type;
1215 tp->t_header.th_num_items = nitems;
1216 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1217 log_vector->i_len = sizeof(xfs_trans_header_t);
1218 log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
1219}
1220
b1c1b5b6
DC
1221/*
1222 * The committed item processing consists of calling the committed routine of
1223 * each logged item, updating the item's position in the AIL if necessary, and
1224 * unpinning each item. If the committed routine returns -1, then do nothing
1225 * further with the item because it may have been freed.
1226 *
1227 * Since items are unlocked when they are copied to the incore log, it is
1228 * possible for two transactions to be completing and manipulating the same
1229 * item simultaneously. The AIL lock will protect the lsn field of each item.
1230 * The value of this field can never go backwards.
1231 *
1232 * We unpin the items after repositioning them in the AIL, because otherwise
1233 * they could be immediately flushed and we'd have to race with the flusher
1234 * trying to pull the item from the AIL as we add it.
1235 */
71e330b5 1236void
b1c1b5b6
DC
1237xfs_trans_item_committed(
1238 struct xfs_log_item *lip,
1239 xfs_lsn_t commit_lsn,
1240 int aborted)
1241{
1242 xfs_lsn_t item_lsn;
1243 struct xfs_ail *ailp;
1244
1245 if (aborted)
1246 lip->li_flags |= XFS_LI_ABORTED;
1247 item_lsn = IOP_COMMITTED(lip, commit_lsn);
1248
1249 /* If the committed routine returns -1, item has been freed. */
1250 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
1251 return;
1252
1253 /*
1254 * If the returned lsn is greater than what it contained before, update
1255 * the location of the item in the AIL. If it is not, then do nothing.
1256 * Items can never move backwards in the AIL.
1257 *
1258 * While the new lsn should usually be greater, it is possible that a
1259 * later transaction completing simultaneously with an earlier one
1260 * using the same item could complete first with a higher lsn. This
1261 * would cause the earlier transaction to fail the test below.
1262 */
1263 ailp = lip->li_ailp;
1264 spin_lock(&ailp->xa_lock);
1265 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1266 /*
1267 * This will set the item's lsn to item_lsn and update the
1268 * position of the item in the AIL.
1269 *
1270 * xfs_trans_ail_update() drops the AIL lock.
1271 */
1272 xfs_trans_ail_update(ailp, lip, item_lsn);
1273 } else {
1274 spin_unlock(&ailp->xa_lock);
1275 }
1276
1277 /*
1278 * Now that we've repositioned the item in the AIL, unpin it so it can
1279 * be flushed. Pass information about buffer stale state down from the
1280 * log item flags, if anyone else stales the buffer we do not want to
1281 * pay any attention to it.
1282 */
1283 IOP_UNPIN(lip);
1284}
1285
b1c1b5b6
DC
1286/*
1287 * This is typically called by the LM when a transaction has been fully
1288 * committed to disk. It needs to unpin the items which have
1289 * been logged by the transaction and update their positions
1290 * in the AIL if necessary.
1291 *
1292 * This also gets called when the transactions didn't get written out
1293 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1294 */
1295STATIC void
1296xfs_trans_committed(
1297 struct xfs_trans *tp,
1298 int abortflag)
1299{
1300 xfs_log_item_desc_t *lidp;
1301 xfs_log_item_chunk_t *licp;
1302 xfs_log_item_chunk_t *next_licp;
1303
1304 /* Call the transaction's completion callback if there is one. */
1305 if (tp->t_callback != NULL)
1306 tp->t_callback(tp, tp->t_callarg);
1307
1308 for (lidp = xfs_trans_first_item(tp);
1309 lidp != NULL;
1310 lidp = xfs_trans_next_item(tp, lidp)) {
1311 xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
1312 }
1313
1314 /* free the item chunks, ignoring the embedded chunk */
1315 for (licp = tp->t_items.lic_next; licp != NULL; licp = next_licp) {
1316 next_licp = licp->lic_next;
1317 kmem_free(licp);
1318 }
1319
b1c1b5b6
DC
1320 xfs_trans_free(tp);
1321}
1322
1323/*
1324 * Called from the trans_commit code when we notice that
1325 * the filesystem is in the middle of a forced shutdown.
1326 */
1327STATIC void
1328xfs_trans_uncommit(
1329 struct xfs_trans *tp,
1330 uint flags)
1331{
1332 xfs_log_item_desc_t *lidp;
1333
1334 for (lidp = xfs_trans_first_item(tp);
1335 lidp != NULL;
1336 lidp = xfs_trans_next_item(tp, lidp)) {
1337 /*
1338 * Unpin all but those that aren't dirty.
1339 */
1340 if (lidp->lid_flags & XFS_LID_DIRTY)
1341 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1342 }
1343
1344 xfs_trans_unreserve_and_mod_sb(tp);
1345 xfs_trans_unreserve_and_mod_dquots(tp);
1346
71e330b5 1347 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
b1c1b5b6
DC
1348 xfs_trans_free(tp);
1349}
1350
0924378a
DC
1351/*
1352 * Format the transaction direct to the iclog. This isolates the physical
1353 * transaction commit operation from the logical operation and hence allows
1354 * other methods to be introduced without affecting the existing commit path.
1355 */
1356static int
1357xfs_trans_commit_iclog(
1358 struct xfs_mount *mp,
1359 struct xfs_trans *tp,
1360 xfs_lsn_t *commit_lsn,
1361 int flags)
1362{
1363 int shutdown;
1364 int error;
1365 int log_flags = 0;
1366 struct xlog_in_core *commit_iclog;
1367#define XFS_TRANS_LOGVEC_COUNT 16
1368 struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
1369 struct xfs_log_iovec *log_vector;
1370 uint nvec;
1371
1da177e4
LT
1372
1373 /*
1374 * Ask each log item how many log_vector entries it will
1375 * need so we can figure out how many to allocate.
1376 * Try to avoid the kmem_alloc() call in the common case
1377 * by using a vector from the stack when it fits.
1378 */
1379 nvec = xfs_trans_count_vecs(tp);
1da177e4 1380 if (nvec == 0) {
0924378a 1381 return ENOMEM; /* triggers a shutdown! */
cfcbbbd0 1382 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
1da177e4
LT
1383 log_vector = log_vector_fast;
1384 } else {
1385 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
1386 sizeof(xfs_log_iovec_t),
1387 KM_SLEEP);
1388 }
1389
1390 /*
1391 * Fill in the log_vector and pin the logged items, and
1392 * then write the transaction to the log.
1393 */
1394 xfs_trans_fill_vecs(tp, log_vector);
1395
0924378a
DC
1396 if (flags & XFS_TRANS_RELEASE_LOG_RES)
1397 log_flags = XFS_LOG_REL_PERM_RESERV;
1398
cfcbbbd0 1399 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
1da177e4 1400
1da177e4 1401 /*
cfcbbbd0
NS
1402 * The transaction is committed incore here, and can go out to disk
1403 * at any time after this call. However, all the items associated
1404 * with the transaction are still locked and pinned in memory.
1da177e4 1405 */
0924378a 1406 *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
1da177e4 1407
0924378a 1408 tp->t_commit_lsn = *commit_lsn;
ed3b4d6c
DC
1409 trace_xfs_trans_commit_lsn(tp);
1410
0924378a 1411 if (nvec > XFS_TRANS_LOGVEC_COUNT)
f0e2d93c 1412 kmem_free(log_vector);
1da177e4 1413
1da177e4
LT
1414 /*
1415 * If we got a log write error. Unpin the logitems that we
1416 * had pinned, clean up, free trans structure, and return error.
1417 */
0924378a 1418 if (error || *commit_lsn == -1) {
59c1b082 1419 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4
LT
1420 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
1421 return XFS_ERROR(EIO);
1422 }
1423
1424 /*
1425 * Once the transaction has committed, unused
1426 * reservations need to be released and changes to
1427 * the superblock need to be reflected in the in-core
1428 * version. Do that now.
1429 */
1430 xfs_trans_unreserve_and_mod_sb(tp);
1431
1da177e4
LT
1432 /*
1433 * Tell the LM to call the transaction completion routine
1434 * when the log write with LSN commit_lsn completes (e.g.
1435 * when the transaction commit really hits the on-disk log).
1436 * After this call we cannot reference tp, because the call
1437 * can happen at any time and the call will free the transaction
1438 * structure pointed to by tp. The only case where we call
1439 * the completion routine (xfs_trans_committed) directly is
1440 * if the log is turned off on a debug kernel or we're
1441 * running in simulation mode (the log is explicitly turned
1442 * off).
1443 */
1444 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
1445 tp->t_logcb.cb_arg = tp;
1446
1447 /*
1448 * We need to pass the iclog buffer which was used for the
1449 * transaction commit record into this function, and attach
1450 * the callback to it. The callback must be attached before
1451 * the items are unlocked to avoid racing with other threads
1452 * waiting for an item to unlock.
1453 */
1454 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
1455
1456 /*
1457 * Mark this thread as no longer being in a transaction
1458 */
59c1b082 1459 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4
LT
1460
1461 /*
1462 * Once all the items of the transaction have been copied
1463 * to the in core log and the callback is attached, the
1464 * items can be unlocked.
1465 *
1466 * This will free descriptors pointing to items which were
1467 * not logged since there is nothing more to do with them.
1468 * For items which were logged, we will keep pointers to them
1469 * so they can be unpinned after the transaction commits to disk.
1470 * This will also stamp each modified meta-data item with
1471 * the commit lsn of this transaction for dependency tracking
1472 * purposes.
1473 */
0924378a 1474 xfs_trans_unlock_items(tp, *commit_lsn);
1da177e4
LT
1475
1476 /*
1477 * If we detected a log error earlier, finish committing
1478 * the transaction now (unpin log items, etc).
1479 *
1480 * Order is critical here, to avoid using the transaction
1481 * pointer after its been freed (by xfs_trans_committed
1482 * either here now, or as a callback). We cannot do this
1483 * step inside xfs_log_notify as was done earlier because
1484 * of this issue.
1485 */
1486 if (shutdown)
1487 xfs_trans_committed(tp, XFS_LI_ABORTED);
1488
1489 /*
1490 * Now that the xfs_trans_committed callback has been attached,
1491 * and the items are released we can finally allow the iclog to
1492 * go to disk.
1493 */
0924378a
DC
1494 return xfs_log_release_iclog(mp, commit_iclog);
1495}
1496
71e330b5
DC
1497/*
1498 * Walk the log items and allocate log vector structures for
1499 * each item large enough to fit all the vectors they require.
1500 * Note that this format differs from the old log vector format in
1501 * that there is no transaction header in these log vectors.
1502 */
1503STATIC struct xfs_log_vec *
1504xfs_trans_alloc_log_vecs(
1505 xfs_trans_t *tp)
1506{
1507 xfs_log_item_desc_t *lidp;
1508 struct xfs_log_vec *lv = NULL;
1509 struct xfs_log_vec *ret_lv = NULL;
1510
1511 lidp = xfs_trans_first_item(tp);
1512
1513 /* Bail out if we didn't find a log item. */
1514 if (!lidp) {
1515 ASSERT(0);
1516 return NULL;
1517 }
1518
1519 while (lidp != NULL) {
1520 struct xfs_log_vec *new_lv;
1521
1522 /* Skip items which aren't dirty in this transaction. */
1523 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1524 lidp = xfs_trans_next_item(tp, lidp);
1525 continue;
1526 }
1527
1528 /* Skip items that do not have any vectors for writing */
1529 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1530 if (!lidp->lid_size) {
1531 lidp = xfs_trans_next_item(tp, lidp);
1532 continue;
1533 }
1534
1535 new_lv = kmem_zalloc(sizeof(*new_lv) +
1536 lidp->lid_size * sizeof(struct xfs_log_iovec),
1537 KM_SLEEP);
1538
1539 /* The allocated iovec region lies beyond the log vector. */
1540 new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
1541 new_lv->lv_niovecs = lidp->lid_size;
1542 new_lv->lv_item = lidp->lid_item;
1543 if (!ret_lv)
1544 ret_lv = new_lv;
1545 else
1546 lv->lv_next = new_lv;
1547 lv = new_lv;
1548 lidp = xfs_trans_next_item(tp, lidp);
1549 }
1550
1551 return ret_lv;
1552}
1553
1554static int
1555xfs_trans_commit_cil(
1556 struct xfs_mount *mp,
1557 struct xfs_trans *tp,
1558 xfs_lsn_t *commit_lsn,
1559 int flags)
1560{
1561 struct xfs_log_vec *log_vector;
1562 int error;
1563
1564 /*
1565 * Get each log item to allocate a vector structure for
1566 * the log item to to pass to the log write code. The
1567 * CIL commit code will format the vector and save it away.
1568 */
1569 log_vector = xfs_trans_alloc_log_vecs(tp);
1570 if (!log_vector)
1571 return ENOMEM;
1572
1573 error = xfs_log_commit_cil(mp, tp, log_vector, commit_lsn, flags);
1574 if (error)
1575 return error;
1576
1577 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1578
1579 /* xfs_trans_free_items() unlocks them first */
1580 xfs_trans_free_items(tp, *commit_lsn, 0);
1581 xfs_trans_free(tp);
1582 return 0;
1583}
0924378a
DC
1584
1585/*
1586 * xfs_trans_commit
1587 *
1588 * Commit the given transaction to the log a/synchronously.
1589 *
1590 * XFS disk error handling mechanism is not based on a typical
1591 * transaction abort mechanism. Logically after the filesystem
1592 * gets marked 'SHUTDOWN', we can't let any new transactions
1593 * be durable - ie. committed to disk - because some metadata might
1594 * be inconsistent. In such cases, this returns an error, and the
1595 * caller may assume that all locked objects joined to the transaction
1596 * have already been unlocked as if the commit had succeeded.
1597 * Do not reference the transaction structure after this call.
1598 */
0924378a
DC
1599int
1600_xfs_trans_commit(
a3ccd2ca
CH
1601 struct xfs_trans *tp,
1602 uint flags,
1603 int *log_flushed)
0924378a 1604{
a3ccd2ca 1605 struct xfs_mount *mp = tp->t_mountp;
0924378a 1606 xfs_lsn_t commit_lsn = -1;
a3ccd2ca 1607 int error = 0;
0924378a
DC
1608 int log_flags = 0;
1609 int sync = tp->t_flags & XFS_TRANS_SYNC;
0924378a
DC
1610
1611 /*
1612 * Determine whether this commit is releasing a permanent
1613 * log reservation or not.
1614 */
1615 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1616 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1617 log_flags = XFS_LOG_REL_PERM_RESERV;
1618 }
1619
1620 /*
1621 * If there is nothing to be logged by the transaction,
1622 * then unlock all of the items associated with the
1623 * transaction and free the transaction structure.
1624 * Also make sure to return any reserved blocks to
1625 * the free pool.
1626 */
a3ccd2ca
CH
1627 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1628 goto out_unreserve;
1629
1630 if (XFS_FORCED_SHUTDOWN(mp)) {
1631 error = XFS_ERROR(EIO);
1632 goto out_unreserve;
0924378a 1633 }
a3ccd2ca 1634
0924378a
DC
1635 ASSERT(tp->t_ticket != NULL);
1636
1637 /*
1638 * If we need to update the superblock, then do it now.
1639 */
1640 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1641 xfs_trans_apply_sb_deltas(tp);
1642 xfs_trans_apply_dquot_deltas(tp);
1643
71e330b5
DC
1644 if (mp->m_flags & XFS_MOUNT_DELAYLOG)
1645 error = xfs_trans_commit_cil(mp, tp, &commit_lsn, flags);
1646 else
1647 error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
1648
0924378a
DC
1649 if (error == ENOMEM) {
1650 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
a3ccd2ca
CH
1651 error = XFS_ERROR(EIO);
1652 goto out_unreserve;
0924378a 1653 }
1da177e4
LT
1654
1655 /*
1656 * If the transaction needs to be synchronous, then force the
1657 * log out now and wait for it.
1658 */
1659 if (sync) {
f538d4da 1660 if (!error) {
a14a348b
CH
1661 error = _xfs_log_force_lsn(mp, commit_lsn,
1662 XFS_LOG_SYNC, log_flushed);
f538d4da 1663 }
1da177e4
LT
1664 XFS_STATS_INC(xs_trans_sync);
1665 } else {
1666 XFS_STATS_INC(xs_trans_async);
1667 }
1668
a3ccd2ca
CH
1669 return error;
1670
1671out_unreserve:
1672 xfs_trans_unreserve_and_mod_sb(tp);
1673
1674 /*
1675 * It is indeed possible for the transaction to be not dirty but
1676 * the dqinfo portion to be. All that means is that we have some
1677 * (non-persistent) quota reservations that need to be unreserved.
1678 */
1679 xfs_trans_unreserve_and_mod_dquots(tp);
1680 if (tp->t_ticket) {
1681 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1682 if (commit_lsn == -1 && !error)
1683 error = XFS_ERROR(EIO);
1684 }
1685 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
71e330b5 1686 xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
a3ccd2ca
CH
1687 xfs_trans_free(tp);
1688
1689 XFS_STATS_INC(xs_trans_empty);
1690 return error;
1da177e4
LT
1691}
1692
1da177e4
LT
1693/*
1694 * Unlock all of the transaction's items and free the transaction.
1695 * The transaction must not have modified any of its items, because
1696 * there is no way to restore them to their previous state.
1697 *
1698 * If the transaction has made a log reservation, make sure to release
1699 * it as well.
1700 */
1701void
1702xfs_trans_cancel(
1703 xfs_trans_t *tp,
1704 int flags)
1705{
1706 int log_flags;
1707#ifdef DEBUG
1708 xfs_log_item_chunk_t *licp;
1709 xfs_log_item_desc_t *lidp;
1710 xfs_log_item_t *lip;
1711 int i;
1712#endif
0733af21 1713 xfs_mount_t *mp = tp->t_mountp;
1da177e4
LT
1714
1715 /*
1716 * See if the caller is being too lazy to figure out if
1717 * the transaction really needs an abort.
1718 */
1719 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1720 flags &= ~XFS_TRANS_ABORT;
1721 /*
1722 * See if the caller is relying on us to shut down the
1723 * filesystem. This happens in paths where we detect
1724 * corruption and decide to give up.
1725 */
60a204f0 1726 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
0733af21 1727 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
7d04a335 1728 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
60a204f0 1729 }
1da177e4
LT
1730#ifdef DEBUG
1731 if (!(flags & XFS_TRANS_ABORT)) {
1732 licp = &(tp->t_items);
1733 while (licp != NULL) {
1734 lidp = licp->lic_descs;
1735 for (i = 0; i < licp->lic_unused; i++, lidp++) {
39dab9d7 1736 if (xfs_lic_isfree(licp, i)) {
1da177e4
LT
1737 continue;
1738 }
1739
1740 lip = lidp->lid_item;
0733af21 1741 if (!XFS_FORCED_SHUTDOWN(mp))
1da177e4
LT
1742 ASSERT(!(lip->li_type == XFS_LI_EFD));
1743 }
1744 licp = licp->lic_next;
1745 }
1746 }
1747#endif
1748 xfs_trans_unreserve_and_mod_sb(tp);
7d095257 1749 xfs_trans_unreserve_and_mod_dquots(tp);
1da177e4
LT
1750
1751 if (tp->t_ticket) {
1752 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1753 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1754 log_flags = XFS_LOG_REL_PERM_RESERV;
1755 } else {
1756 log_flags = 0;
1757 }
0733af21 1758 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1da177e4
LT
1759 }
1760
1761 /* mark this thread as no longer being in a transaction */
59c1b082 1762 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1da177e4 1763
71e330b5 1764 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
1da177e4
LT
1765 xfs_trans_free(tp);
1766}
1767
322ff6b8
NS
1768/*
1769 * Roll from one trans in the sequence of PERMANENT transactions to
1770 * the next: permanent transactions are only flushed out when
1771 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1772 * as possible to let chunks of it go to the log. So we commit the
1773 * chunk we've been working on and get a new transaction to continue.
1774 */
1775int
1776xfs_trans_roll(
1777 struct xfs_trans **tpp,
1778 struct xfs_inode *dp)
1779{
1780 struct xfs_trans *trans;
1781 unsigned int logres, count;
1782 int error;
1783
1784 /*
1785 * Ensure that the inode is always logged.
1786 */
1787 trans = *tpp;
1788 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1789
1790 /*
1791 * Copy the critical parameters from one trans to the next.
1792 */
1793 logres = trans->t_log_res;
1794 count = trans->t_log_count;
1795 *tpp = xfs_trans_dup(trans);
1796
1797 /*
1798 * Commit the current transaction.
1799 * If this commit failed, then it'd just unlock those items that
1800 * are not marked ihold. That also means that a filesystem shutdown
1801 * is in progress. The caller takes the responsibility to cancel
1802 * the duplicate transaction that gets returned.
1803 */
1804 error = xfs_trans_commit(trans, 0);
1805 if (error)
1806 return (error);
1807
1808 trans = *tpp;
1809
cc09c0dc
DC
1810 /*
1811 * transaction commit worked ok so we can drop the extra ticket
1812 * reference that we gained in xfs_trans_dup()
1813 */
1814 xfs_log_ticket_put(trans->t_ticket);
1815
1816
322ff6b8
NS
1817 /*
1818 * Reserve space in the log for th next transaction.
1819 * This also pushes items in the "AIL", the list of logged items,
1820 * out to disk if they are taking up space at the tail of the log
1821 * that we want to use. This requires that either nothing be locked
1822 * across this call, or that anything that is locked be logged in
1823 * the prior and the next transactions.
1824 */
1825 error = xfs_trans_reserve(trans, 0, logres, 0,
1826 XFS_TRANS_PERM_LOG_RES, count);
1827 /*
1828 * Ensure that the inode is in the new transaction and locked.
1829 */
1830 if (error)
1831 return error;
1832
1833 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1834 xfs_trans_ihold(trans, dp);
1835 return 0;
1836}
This page took 1.062354 seconds and 5 git commands to generate.