xref: /linux/fs/xfs/xfs_trans.c (revision 9006fb91cfdf22812923f0536c7531c429c1aeab)
11da177e4SLinus Torvalds /*
27b718769SNathan Scott  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3e98c414fSChristoph Hellwig  * Copyright (C) 2010 Red Hat, Inc.
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  *
67b718769SNathan Scott  * This program is free software; you can redistribute it and/or
77b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
81da177e4SLinus Torvalds  * published by the Free Software Foundation.
91da177e4SLinus Torvalds  *
107b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
117b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
127b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137b718769SNathan Scott  * GNU General Public License for more details.
141da177e4SLinus Torvalds  *
157b718769SNathan Scott  * You should have received a copy of the GNU General Public License
167b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
177b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds #include "xfs.h"
20a844f451SNathan Scott #include "xfs_fs.h"
211da177e4SLinus Torvalds #include "xfs_types.h"
22a844f451SNathan Scott #include "xfs_bit.h"
231da177e4SLinus Torvalds #include "xfs_log.h"
24a844f451SNathan Scott #include "xfs_inum.h"
251da177e4SLinus Torvalds #include "xfs_trans.h"
261da177e4SLinus Torvalds #include "xfs_sb.h"
271da177e4SLinus Torvalds #include "xfs_ag.h"
281da177e4SLinus Torvalds #include "xfs_mount.h"
291da177e4SLinus Torvalds #include "xfs_error.h"
30a844f451SNathan Scott #include "xfs_da_btree.h"
311da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
32a844f451SNathan Scott #include "xfs_alloc_btree.h"
331da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
34a844f451SNathan Scott #include "xfs_dinode.h"
35a844f451SNathan Scott #include "xfs_inode.h"
361da177e4SLinus Torvalds #include "xfs_btree.h"
371da177e4SLinus Torvalds #include "xfs_ialloc.h"
381da177e4SLinus Torvalds #include "xfs_alloc.h"
391da177e4SLinus Torvalds #include "xfs_bmap.h"
401da177e4SLinus Torvalds #include "xfs_quota.h"
41a844f451SNathan Scott #include "xfs_trans_priv.h"
421da177e4SLinus Torvalds #include "xfs_trans_space.h"
43322ff6b8SNiv Sardi #include "xfs_inode_item.h"
44ed3b4d6cSDave Chinner #include "xfs_trace.h"
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds kmem_zone_t	*xfs_trans_zone;
47e98c414fSChristoph Hellwig kmem_zone_t	*xfs_log_item_desc_zone;
481da177e4SLinus Torvalds 
49025101dcSChristoph Hellwig 
501da177e4SLinus Torvalds /*
51025101dcSChristoph Hellwig  * Various log reservation values.
52025101dcSChristoph Hellwig  *
53025101dcSChristoph Hellwig  * These are based on the size of the file system block because that is what
54025101dcSChristoph Hellwig  * most transactions manipulate.  Each adds in an additional 128 bytes per
55025101dcSChristoph Hellwig  * item logged to try to account for the overhead of the transaction mechanism.
56025101dcSChristoph Hellwig  *
57025101dcSChristoph Hellwig  * Note:  Most of the reservations underestimate the number of allocation
58025101dcSChristoph Hellwig  * groups into which they could free extents in the xfs_bmap_finish() call.
59025101dcSChristoph Hellwig  * This is because the number in the worst case is quite high and quite
60025101dcSChristoph Hellwig  * unusual.  In order to fix this we need to change xfs_bmap_finish() to free
61025101dcSChristoph Hellwig  * extents in only a single AG at a time.  This will require changes to the
62025101dcSChristoph Hellwig  * EFI code as well, however, so that the EFI for the extents not freed is
63025101dcSChristoph Hellwig  * logged again in each transaction.  See SGI PV #261917.
64025101dcSChristoph Hellwig  *
65025101dcSChristoph Hellwig  * Reservation functions here avoid a huge stack in xfs_trans_init due to
66025101dcSChristoph Hellwig  * register overflow from temporaries in the calculations.
67025101dcSChristoph Hellwig  */
68025101dcSChristoph Hellwig 
69025101dcSChristoph Hellwig 
70025101dcSChristoph Hellwig /*
71025101dcSChristoph Hellwig  * In a write transaction we can allocate a maximum of 2
72025101dcSChristoph Hellwig  * extents.  This gives:
73025101dcSChristoph Hellwig  *    the inode getting the new extents: inode size
74025101dcSChristoph Hellwig  *    the inode's bmap btree: max depth * block size
75025101dcSChristoph Hellwig  *    the agfs of the ags from which the extents are allocated: 2 * sector
76025101dcSChristoph Hellwig  *    the superblock free block counter: sector size
77025101dcSChristoph Hellwig  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
78025101dcSChristoph Hellwig  * And the bmap_finish transaction can free bmap blocks in a join:
79025101dcSChristoph Hellwig  *    the agfs of the ags containing the blocks: 2 * sector size
80025101dcSChristoph Hellwig  *    the agfls of the ags containing the blocks: 2 * sector size
81025101dcSChristoph Hellwig  *    the super block free block counter: sector size
82025101dcSChristoph Hellwig  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
838f794055SNathan Scott  */
848f794055SNathan Scott STATIC uint
85025101dcSChristoph Hellwig xfs_calc_write_reservation(
86025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
878f794055SNathan Scott {
88025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
89025101dcSChristoph Hellwig 		MAX((mp->m_sb.sb_inodesize +
90025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
91025101dcSChristoph Hellwig 		     2 * mp->m_sb.sb_sectsize +
92025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
93025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 2) +
94025101dcSChristoph Hellwig 		     128 * (4 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
95025101dcSChristoph Hellwig 			    XFS_ALLOCFREE_LOG_COUNT(mp, 2))),
96025101dcSChristoph Hellwig 		    (2 * mp->m_sb.sb_sectsize +
97025101dcSChristoph Hellwig 		     2 * mp->m_sb.sb_sectsize +
98025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
99025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 2) +
100025101dcSChristoph Hellwig 		     128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
1018f794055SNathan Scott }
1028f794055SNathan Scott 
103025101dcSChristoph Hellwig /*
104025101dcSChristoph Hellwig  * In truncating a file we free up to two extents at once.  We can modify:
105025101dcSChristoph Hellwig  *    the inode being truncated: inode size
106025101dcSChristoph Hellwig  *    the inode's bmap btree: (max depth + 1) * block size
107025101dcSChristoph Hellwig  * And the bmap_finish transaction can free the blocks and bmap blocks:
108025101dcSChristoph Hellwig  *    the agf for each of the ags: 4 * sector size
109025101dcSChristoph Hellwig  *    the agfl for each of the ags: 4 * sector size
110025101dcSChristoph Hellwig  *    the super block to reflect the freed blocks: sector size
111025101dcSChristoph Hellwig  *    worst case split in allocation btrees per extent assuming 4 extents:
112025101dcSChristoph Hellwig  *		4 exts * 2 trees * (2 * max depth - 1) * block size
113025101dcSChristoph Hellwig  *    the inode btree: max depth * blocksize
114025101dcSChristoph Hellwig  *    the allocation btrees: 2 trees * (max depth - 1) * block size
115025101dcSChristoph Hellwig  */
1168f794055SNathan Scott STATIC uint
117025101dcSChristoph Hellwig xfs_calc_itruncate_reservation(
118025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
1198f794055SNathan Scott {
120025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
121025101dcSChristoph Hellwig 		MAX((mp->m_sb.sb_inodesize +
122025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1) +
123025101dcSChristoph Hellwig 		     128 * (2 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
124025101dcSChristoph Hellwig 		    (4 * mp->m_sb.sb_sectsize +
125025101dcSChristoph Hellwig 		     4 * mp->m_sb.sb_sectsize +
126025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
127025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 4) +
128025101dcSChristoph Hellwig 		     128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4)) +
129025101dcSChristoph Hellwig 		     128 * 5 +
130025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 1) +
131025101dcSChristoph Hellwig 		     128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
132025101dcSChristoph Hellwig 			    XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
1338f794055SNathan Scott }
1348f794055SNathan Scott 
135025101dcSChristoph Hellwig /*
136025101dcSChristoph Hellwig  * In renaming a files we can modify:
137025101dcSChristoph Hellwig  *    the four inodes involved: 4 * inode size
138025101dcSChristoph Hellwig  *    the two directory btrees: 2 * (max depth + v2) * dir block size
139025101dcSChristoph Hellwig  *    the two directory bmap btrees: 2 * max depth * block size
140025101dcSChristoph Hellwig  * And the bmap_finish transaction can free dir and bmap blocks (two sets
141025101dcSChristoph Hellwig  *	of bmap blocks) giving:
142025101dcSChristoph Hellwig  *    the agf for the ags in which the blocks live: 3 * sector size
143025101dcSChristoph Hellwig  *    the agfl for the ags in which the blocks live: 3 * sector size
144025101dcSChristoph Hellwig  *    the superblock for the free block count: sector size
145025101dcSChristoph Hellwig  *    the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
146025101dcSChristoph Hellwig  */
1478f794055SNathan Scott STATIC uint
148025101dcSChristoph Hellwig xfs_calc_rename_reservation(
149025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
1508f794055SNathan Scott {
151025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
152025101dcSChristoph Hellwig 		MAX((4 * mp->m_sb.sb_inodesize +
153025101dcSChristoph Hellwig 		     2 * XFS_DIROP_LOG_RES(mp) +
154025101dcSChristoph Hellwig 		     128 * (4 + 2 * XFS_DIROP_LOG_COUNT(mp))),
155025101dcSChristoph Hellwig 		    (3 * mp->m_sb.sb_sectsize +
156025101dcSChristoph Hellwig 		     3 * mp->m_sb.sb_sectsize +
157025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
158025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 3) +
159025101dcSChristoph Hellwig 		     128 * (7 + XFS_ALLOCFREE_LOG_COUNT(mp, 3))));
1608f794055SNathan Scott }
1618f794055SNathan Scott 
162025101dcSChristoph Hellwig /*
163025101dcSChristoph Hellwig  * For creating a link to an inode:
164025101dcSChristoph Hellwig  *    the parent directory inode: inode size
165025101dcSChristoph Hellwig  *    the linked inode: inode size
166025101dcSChristoph Hellwig  *    the directory btree could split: (max depth + v2) * dir block size
167025101dcSChristoph Hellwig  *    the directory bmap btree could join or split: (max depth + v2) * blocksize
168025101dcSChristoph Hellwig  * And the bmap_finish transaction can free some bmap blocks giving:
169025101dcSChristoph Hellwig  *    the agf for the ag in which the blocks live: sector size
170025101dcSChristoph Hellwig  *    the agfl for the ag in which the blocks live: sector size
171025101dcSChristoph Hellwig  *    the superblock for the free block count: sector size
172025101dcSChristoph Hellwig  *    the allocation btrees: 2 trees * (2 * max depth - 1) * block size
173025101dcSChristoph Hellwig  */
1748f794055SNathan Scott STATIC uint
175025101dcSChristoph Hellwig xfs_calc_link_reservation(
176025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
1778f794055SNathan Scott {
178025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
179025101dcSChristoph Hellwig 		MAX((mp->m_sb.sb_inodesize +
180025101dcSChristoph Hellwig 		     mp->m_sb.sb_inodesize +
181025101dcSChristoph Hellwig 		     XFS_DIROP_LOG_RES(mp) +
182025101dcSChristoph Hellwig 		     128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
183025101dcSChristoph Hellwig 		    (mp->m_sb.sb_sectsize +
184025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
185025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
186025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 1) +
187025101dcSChristoph Hellwig 		     128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
1888f794055SNathan Scott }
1898f794055SNathan Scott 
190025101dcSChristoph Hellwig /*
191025101dcSChristoph Hellwig  * For removing a directory entry we can modify:
192025101dcSChristoph Hellwig  *    the parent directory inode: inode size
193025101dcSChristoph Hellwig  *    the removed inode: inode size
194025101dcSChristoph Hellwig  *    the directory btree could join: (max depth + v2) * dir block size
195025101dcSChristoph Hellwig  *    the directory bmap btree could join or split: (max depth + v2) * blocksize
196025101dcSChristoph Hellwig  * And the bmap_finish transaction can free the dir and bmap blocks giving:
197025101dcSChristoph Hellwig  *    the agf for the ag in which the blocks live: 2 * sector size
198025101dcSChristoph Hellwig  *    the agfl for the ag in which the blocks live: 2 * sector size
199025101dcSChristoph Hellwig  *    the superblock for the free block count: sector size
200025101dcSChristoph Hellwig  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
201025101dcSChristoph Hellwig  */
2028f794055SNathan Scott STATIC uint
203025101dcSChristoph Hellwig xfs_calc_remove_reservation(
204025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
2058f794055SNathan Scott {
206025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
207025101dcSChristoph Hellwig 		MAX((mp->m_sb.sb_inodesize +
208025101dcSChristoph Hellwig 		     mp->m_sb.sb_inodesize +
209025101dcSChristoph Hellwig 		     XFS_DIROP_LOG_RES(mp) +
210025101dcSChristoph Hellwig 		     128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
211025101dcSChristoph Hellwig 		    (2 * mp->m_sb.sb_sectsize +
212025101dcSChristoph Hellwig 		     2 * mp->m_sb.sb_sectsize +
213025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
214025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 2) +
215025101dcSChristoph Hellwig 		     128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
2168f794055SNathan Scott }
2178f794055SNathan Scott 
218025101dcSChristoph Hellwig /*
219025101dcSChristoph Hellwig  * For symlink we can modify:
220025101dcSChristoph Hellwig  *    the parent directory inode: inode size
221025101dcSChristoph Hellwig  *    the new inode: inode size
222025101dcSChristoph Hellwig  *    the inode btree entry: 1 block
223025101dcSChristoph Hellwig  *    the directory btree: (max depth + v2) * dir block size
224025101dcSChristoph Hellwig  *    the directory inode's bmap btree: (max depth + v2) * block size
225025101dcSChristoph Hellwig  *    the blocks for the symlink: 1 kB
226025101dcSChristoph Hellwig  * Or in the first xact we allocate some inodes giving:
227025101dcSChristoph Hellwig  *    the agi and agf of the ag getting the new inodes: 2 * sectorsize
228025101dcSChristoph Hellwig  *    the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
229025101dcSChristoph Hellwig  *    the inode btree: max depth * blocksize
230025101dcSChristoph Hellwig  *    the allocation btrees: 2 trees * (2 * max depth - 1) * block size
231025101dcSChristoph Hellwig  */
2328f794055SNathan Scott STATIC uint
233025101dcSChristoph Hellwig xfs_calc_symlink_reservation(
234025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
2358f794055SNathan Scott {
236025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
237025101dcSChristoph Hellwig 		MAX((mp->m_sb.sb_inodesize +
238025101dcSChristoph Hellwig 		     mp->m_sb.sb_inodesize +
239025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, 1) +
240025101dcSChristoph Hellwig 		     XFS_DIROP_LOG_RES(mp) +
241025101dcSChristoph Hellwig 		     1024 +
242025101dcSChristoph Hellwig 		     128 * (4 + XFS_DIROP_LOG_COUNT(mp))),
243025101dcSChristoph Hellwig 		    (2 * mp->m_sb.sb_sectsize +
244025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
245025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
246025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 1) +
247025101dcSChristoph Hellwig 		     128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
248025101dcSChristoph Hellwig 			    XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
2498f794055SNathan Scott }
2508f794055SNathan Scott 
251025101dcSChristoph Hellwig /*
252025101dcSChristoph Hellwig  * For create we can modify:
253025101dcSChristoph Hellwig  *    the parent directory inode: inode size
254025101dcSChristoph Hellwig  *    the new inode: inode size
255025101dcSChristoph Hellwig  *    the inode btree entry: block size
256025101dcSChristoph Hellwig  *    the superblock for the nlink flag: sector size
257025101dcSChristoph Hellwig  *    the directory btree: (max depth + v2) * dir block size
258025101dcSChristoph Hellwig  *    the directory inode's bmap btree: (max depth + v2) * block size
259025101dcSChristoph Hellwig  * Or in the first xact we allocate some inodes giving:
260025101dcSChristoph Hellwig  *    the agi and agf of the ag getting the new inodes: 2 * sectorsize
261025101dcSChristoph Hellwig  *    the superblock for the nlink flag: sector size
262025101dcSChristoph Hellwig  *    the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
263025101dcSChristoph Hellwig  *    the inode btree: max depth * blocksize
264025101dcSChristoph Hellwig  *    the allocation btrees: 2 trees * (max depth - 1) * block size
265025101dcSChristoph Hellwig  */
2668f794055SNathan Scott STATIC uint
267025101dcSChristoph Hellwig xfs_calc_create_reservation(
268025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
2698f794055SNathan Scott {
270025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
271025101dcSChristoph Hellwig 		MAX((mp->m_sb.sb_inodesize +
272025101dcSChristoph Hellwig 		     mp->m_sb.sb_inodesize +
273025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
274025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, 1) +
275025101dcSChristoph Hellwig 		     XFS_DIROP_LOG_RES(mp) +
276025101dcSChristoph Hellwig 		     128 * (3 + XFS_DIROP_LOG_COUNT(mp))),
277025101dcSChristoph Hellwig 		    (3 * mp->m_sb.sb_sectsize +
278025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
279025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
280025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 1) +
281025101dcSChristoph Hellwig 		     128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
282025101dcSChristoph Hellwig 			    XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
2838f794055SNathan Scott }
2848f794055SNathan Scott 
285025101dcSChristoph Hellwig /*
286025101dcSChristoph Hellwig  * Making a new directory is the same as creating a new file.
287025101dcSChristoph Hellwig  */
2888f794055SNathan Scott STATIC uint
289025101dcSChristoph Hellwig xfs_calc_mkdir_reservation(
290025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
2918f794055SNathan Scott {
292025101dcSChristoph Hellwig 	return xfs_calc_create_reservation(mp);
2938f794055SNathan Scott }
2948f794055SNathan Scott 
295025101dcSChristoph Hellwig /*
296025101dcSChristoph Hellwig  * In freeing an inode we can modify:
297025101dcSChristoph Hellwig  *    the inode being freed: inode size
298025101dcSChristoph Hellwig  *    the super block free inode counter: sector size
299025101dcSChristoph Hellwig  *    the agi hash list and counters: sector size
300025101dcSChristoph Hellwig  *    the inode btree entry: block size
301025101dcSChristoph Hellwig  *    the on disk inode before ours in the agi hash list: inode cluster size
302025101dcSChristoph Hellwig  *    the inode btree: max depth * blocksize
303025101dcSChristoph Hellwig  *    the allocation btrees: 2 trees * (max depth - 1) * block size
304025101dcSChristoph Hellwig  */
3058f794055SNathan Scott STATIC uint
306025101dcSChristoph Hellwig xfs_calc_ifree_reservation(
307025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
3088f794055SNathan Scott {
309025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
310025101dcSChristoph Hellwig 		mp->m_sb.sb_inodesize +
311025101dcSChristoph Hellwig 		mp->m_sb.sb_sectsize +
312025101dcSChristoph Hellwig 		mp->m_sb.sb_sectsize +
313025101dcSChristoph Hellwig 		XFS_FSB_TO_B(mp, 1) +
314025101dcSChristoph Hellwig 		MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
315025101dcSChristoph Hellwig 		    XFS_INODE_CLUSTER_SIZE(mp)) +
316025101dcSChristoph Hellwig 		128 * 5 +
317025101dcSChristoph Hellwig 		XFS_ALLOCFREE_LOG_RES(mp, 1) +
318025101dcSChristoph Hellwig 		128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
319025101dcSChristoph Hellwig 		       XFS_ALLOCFREE_LOG_COUNT(mp, 1));
3208f794055SNathan Scott }
3218f794055SNathan Scott 
322025101dcSChristoph Hellwig /*
323025101dcSChristoph Hellwig  * When only changing the inode we log the inode and possibly the superblock
324025101dcSChristoph Hellwig  * We also add a bit of slop for the transaction stuff.
325025101dcSChristoph Hellwig  */
3268f794055SNathan Scott STATIC uint
327025101dcSChristoph Hellwig xfs_calc_ichange_reservation(
328025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
3298f794055SNathan Scott {
330025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
331025101dcSChristoph Hellwig 		mp->m_sb.sb_inodesize +
332025101dcSChristoph Hellwig 		mp->m_sb.sb_sectsize +
333025101dcSChristoph Hellwig 		512;
334025101dcSChristoph Hellwig 
3358f794055SNathan Scott }
3368f794055SNathan Scott 
337025101dcSChristoph Hellwig /*
338025101dcSChristoph Hellwig  * Growing the data section of the filesystem.
339025101dcSChristoph Hellwig  *	superblock
340025101dcSChristoph Hellwig  *	agi and agf
341025101dcSChristoph Hellwig  *	allocation btrees
342025101dcSChristoph Hellwig  */
3438f794055SNathan Scott STATIC uint
344025101dcSChristoph Hellwig xfs_calc_growdata_reservation(
345025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
3468f794055SNathan Scott {
347025101dcSChristoph Hellwig 	return mp->m_sb.sb_sectsize * 3 +
348025101dcSChristoph Hellwig 		XFS_ALLOCFREE_LOG_RES(mp, 1) +
349025101dcSChristoph Hellwig 		128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1));
3508f794055SNathan Scott }
3518f794055SNathan Scott 
352025101dcSChristoph Hellwig /*
353025101dcSChristoph Hellwig  * Growing the rt section of the filesystem.
354025101dcSChristoph Hellwig  * In the first set of transactions (ALLOC) we allocate space to the
355025101dcSChristoph Hellwig  * bitmap or summary files.
356025101dcSChristoph Hellwig  *	superblock: sector size
357025101dcSChristoph Hellwig  *	agf of the ag from which the extent is allocated: sector size
358025101dcSChristoph Hellwig  *	bmap btree for bitmap/summary inode: max depth * blocksize
359025101dcSChristoph Hellwig  *	bitmap/summary inode: inode size
360025101dcSChristoph Hellwig  *	allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
361025101dcSChristoph Hellwig  */
3628f794055SNathan Scott STATIC uint
363025101dcSChristoph Hellwig xfs_calc_growrtalloc_reservation(
364025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
3658f794055SNathan Scott {
366025101dcSChristoph Hellwig 	return 2 * mp->m_sb.sb_sectsize +
367025101dcSChristoph Hellwig 		XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
368025101dcSChristoph Hellwig 		mp->m_sb.sb_inodesize +
369025101dcSChristoph Hellwig 		XFS_ALLOCFREE_LOG_RES(mp, 1) +
370025101dcSChristoph Hellwig 		128 * (3 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
371025101dcSChristoph Hellwig 		       XFS_ALLOCFREE_LOG_COUNT(mp, 1));
3728f794055SNathan Scott }
3738f794055SNathan Scott 
374025101dcSChristoph Hellwig /*
375025101dcSChristoph Hellwig  * Growing the rt section of the filesystem.
376025101dcSChristoph Hellwig  * In the second set of transactions (ZERO) we zero the new metadata blocks.
377025101dcSChristoph Hellwig  *	one bitmap/summary block: blocksize
378025101dcSChristoph Hellwig  */
3798f794055SNathan Scott STATIC uint
380025101dcSChristoph Hellwig xfs_calc_growrtzero_reservation(
381025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
3828f794055SNathan Scott {
383025101dcSChristoph Hellwig 	return mp->m_sb.sb_blocksize + 128;
3848f794055SNathan Scott }
3858f794055SNathan Scott 
386025101dcSChristoph Hellwig /*
387025101dcSChristoph Hellwig  * Growing the rt section of the filesystem.
388025101dcSChristoph Hellwig  * In the third set of transactions (FREE) we update metadata without
389025101dcSChristoph Hellwig  * allocating any new blocks.
390025101dcSChristoph Hellwig  *	superblock: sector size
391025101dcSChristoph Hellwig  *	bitmap inode: inode size
392025101dcSChristoph Hellwig  *	summary inode: inode size
393025101dcSChristoph Hellwig  *	one bitmap block: blocksize
394025101dcSChristoph Hellwig  *	summary blocks: new summary size
395025101dcSChristoph Hellwig  */
3968f794055SNathan Scott STATIC uint
397025101dcSChristoph Hellwig xfs_calc_growrtfree_reservation(
398025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
3998f794055SNathan Scott {
400025101dcSChristoph Hellwig 	return mp->m_sb.sb_sectsize +
401025101dcSChristoph Hellwig 		2 * mp->m_sb.sb_inodesize +
402025101dcSChristoph Hellwig 		mp->m_sb.sb_blocksize +
403025101dcSChristoph Hellwig 		mp->m_rsumsize +
404025101dcSChristoph Hellwig 		128 * 5;
4058f794055SNathan Scott }
4068f794055SNathan Scott 
407025101dcSChristoph Hellwig /*
408025101dcSChristoph Hellwig  * Logging the inode modification timestamp on a synchronous write.
409025101dcSChristoph Hellwig  *	inode
410025101dcSChristoph Hellwig  */
4118f794055SNathan Scott STATIC uint
412025101dcSChristoph Hellwig xfs_calc_swrite_reservation(
413025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
4148f794055SNathan Scott {
415025101dcSChristoph Hellwig 	return mp->m_sb.sb_inodesize + 128;
4168f794055SNathan Scott }
4178f794055SNathan Scott 
418025101dcSChristoph Hellwig /*
419025101dcSChristoph Hellwig  * Logging the inode mode bits when writing a setuid/setgid file
420025101dcSChristoph Hellwig  *	inode
421025101dcSChristoph Hellwig  */
4228f794055SNathan Scott STATIC uint
4238f794055SNathan Scott xfs_calc_writeid_reservation(xfs_mount_t *mp)
4248f794055SNathan Scott {
425025101dcSChristoph Hellwig 	return mp->m_sb.sb_inodesize + 128;
4268f794055SNathan Scott }
4278f794055SNathan Scott 
428025101dcSChristoph Hellwig /*
429025101dcSChristoph Hellwig  * Converting the inode from non-attributed to attributed.
430025101dcSChristoph Hellwig  *	the inode being converted: inode size
431025101dcSChristoph Hellwig  *	agf block and superblock (for block allocation)
432025101dcSChristoph Hellwig  *	the new block (directory sized)
433025101dcSChristoph Hellwig  *	bmap blocks for the new directory block
434025101dcSChristoph Hellwig  *	allocation btrees
435025101dcSChristoph Hellwig  */
4368f794055SNathan Scott STATIC uint
437025101dcSChristoph Hellwig xfs_calc_addafork_reservation(
438025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
4398f794055SNathan Scott {
440025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
441025101dcSChristoph Hellwig 		mp->m_sb.sb_inodesize +
442025101dcSChristoph Hellwig 		mp->m_sb.sb_sectsize * 2 +
443025101dcSChristoph Hellwig 		mp->m_dirblksize +
444025101dcSChristoph Hellwig 		XFS_FSB_TO_B(mp, XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1) +
445025101dcSChristoph Hellwig 		XFS_ALLOCFREE_LOG_RES(mp, 1) +
446025101dcSChristoph Hellwig 		128 * (4 + XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1 +
447025101dcSChristoph Hellwig 		       XFS_ALLOCFREE_LOG_COUNT(mp, 1));
4488f794055SNathan Scott }
4498f794055SNathan Scott 
450025101dcSChristoph Hellwig /*
451025101dcSChristoph Hellwig  * Removing the attribute fork of a file
452025101dcSChristoph Hellwig  *    the inode being truncated: inode size
453025101dcSChristoph Hellwig  *    the inode's bmap btree: max depth * block size
454025101dcSChristoph Hellwig  * And the bmap_finish transaction can free the blocks and bmap blocks:
455025101dcSChristoph Hellwig  *    the agf for each of the ags: 4 * sector size
456025101dcSChristoph Hellwig  *    the agfl for each of the ags: 4 * sector size
457025101dcSChristoph Hellwig  *    the super block to reflect the freed blocks: sector size
458025101dcSChristoph Hellwig  *    worst case split in allocation btrees per extent assuming 4 extents:
459025101dcSChristoph Hellwig  *		4 exts * 2 trees * (2 * max depth - 1) * block size
460025101dcSChristoph Hellwig  */
4618f794055SNathan Scott STATIC uint
462025101dcSChristoph Hellwig xfs_calc_attrinval_reservation(
463025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
4648f794055SNathan Scott {
465025101dcSChristoph Hellwig 	return MAX((mp->m_sb.sb_inodesize +
466025101dcSChristoph Hellwig 		    XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
467025101dcSChristoph Hellwig 		    128 * (1 + XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK))),
468025101dcSChristoph Hellwig 		   (4 * mp->m_sb.sb_sectsize +
469025101dcSChristoph Hellwig 		    4 * mp->m_sb.sb_sectsize +
470025101dcSChristoph Hellwig 		    mp->m_sb.sb_sectsize +
471025101dcSChristoph Hellwig 		    XFS_ALLOCFREE_LOG_RES(mp, 4) +
472025101dcSChristoph Hellwig 		    128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4))));
4738f794055SNathan Scott }
4748f794055SNathan Scott 
475025101dcSChristoph Hellwig /*
476025101dcSChristoph Hellwig  * Setting an attribute.
477025101dcSChristoph Hellwig  *	the inode getting the attribute
478025101dcSChristoph Hellwig  *	the superblock for allocations
479025101dcSChristoph Hellwig  *	the agfs extents are allocated from
480025101dcSChristoph Hellwig  *	the attribute btree * max depth
481025101dcSChristoph Hellwig  *	the inode allocation btree
482025101dcSChristoph Hellwig  * Since attribute transaction space is dependent on the size of the attribute,
483025101dcSChristoph Hellwig  * the calculation is done partially at mount time and partially at runtime.
484025101dcSChristoph Hellwig  */
4858f794055SNathan Scott STATIC uint
486025101dcSChristoph Hellwig xfs_calc_attrset_reservation(
487025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
4888f794055SNathan Scott {
489025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
490025101dcSChristoph Hellwig 		mp->m_sb.sb_inodesize +
491025101dcSChristoph Hellwig 		mp->m_sb.sb_sectsize +
492025101dcSChristoph Hellwig 		XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
493025101dcSChristoph Hellwig 		128 * (2 + XFS_DA_NODE_MAXDEPTH);
4948f794055SNathan Scott }
4958f794055SNathan Scott 
496025101dcSChristoph Hellwig /*
497025101dcSChristoph Hellwig  * Removing an attribute.
498025101dcSChristoph Hellwig  *    the inode: inode size
499025101dcSChristoph Hellwig  *    the attribute btree could join: max depth * block size
500025101dcSChristoph Hellwig  *    the inode bmap btree could join or split: max depth * block size
501025101dcSChristoph Hellwig  * And the bmap_finish transaction can free the attr blocks freed giving:
502025101dcSChristoph Hellwig  *    the agf for the ag in which the blocks live: 2 * sector size
503025101dcSChristoph Hellwig  *    the agfl for the ag in which the blocks live: 2 * sector size
504025101dcSChristoph Hellwig  *    the superblock for the free block count: sector size
505025101dcSChristoph Hellwig  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
506025101dcSChristoph Hellwig  */
5078f794055SNathan Scott STATIC uint
508025101dcSChristoph Hellwig xfs_calc_attrrm_reservation(
509025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
5108f794055SNathan Scott {
511025101dcSChristoph Hellwig 	return XFS_DQUOT_LOGRES(mp) +
512025101dcSChristoph Hellwig 		MAX((mp->m_sb.sb_inodesize +
513025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
514025101dcSChristoph Hellwig 		     XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
515025101dcSChristoph Hellwig 		     128 * (1 + XFS_DA_NODE_MAXDEPTH +
516025101dcSChristoph Hellwig 			    XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
517025101dcSChristoph Hellwig 		    (2 * mp->m_sb.sb_sectsize +
518025101dcSChristoph Hellwig 		     2 * mp->m_sb.sb_sectsize +
519025101dcSChristoph Hellwig 		     mp->m_sb.sb_sectsize +
520025101dcSChristoph Hellwig 		     XFS_ALLOCFREE_LOG_RES(mp, 2) +
521025101dcSChristoph Hellwig 		     128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
5228f794055SNathan Scott }
5238f794055SNathan Scott 
524025101dcSChristoph Hellwig /*
525025101dcSChristoph Hellwig  * Clearing a bad agino number in an agi hash bucket.
526025101dcSChristoph Hellwig  */
5278f794055SNathan Scott STATIC uint
528025101dcSChristoph Hellwig xfs_calc_clear_agi_bucket_reservation(
529025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
5308f794055SNathan Scott {
531025101dcSChristoph Hellwig 	return mp->m_sb.sb_sectsize + 128;
5328f794055SNathan Scott }
5338f794055SNathan Scott 
5348f794055SNathan Scott /*
5351da177e4SLinus Torvalds  * Initialize the precomputed transaction reservation values
5361da177e4SLinus Torvalds  * in the mount structure.
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds void
5391da177e4SLinus Torvalds xfs_trans_init(
540025101dcSChristoph Hellwig 	struct xfs_mount	*mp)
5411da177e4SLinus Torvalds {
542025101dcSChristoph Hellwig 	struct xfs_trans_reservations *resp = &mp->m_reservations;
5431da177e4SLinus Torvalds 
5448f794055SNathan Scott 	resp->tr_write = xfs_calc_write_reservation(mp);
5458f794055SNathan Scott 	resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
5468f794055SNathan Scott 	resp->tr_rename = xfs_calc_rename_reservation(mp);
5478f794055SNathan Scott 	resp->tr_link = xfs_calc_link_reservation(mp);
5488f794055SNathan Scott 	resp->tr_remove = xfs_calc_remove_reservation(mp);
5498f794055SNathan Scott 	resp->tr_symlink = xfs_calc_symlink_reservation(mp);
5508f794055SNathan Scott 	resp->tr_create = xfs_calc_create_reservation(mp);
5518f794055SNathan Scott 	resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
5528f794055SNathan Scott 	resp->tr_ifree = xfs_calc_ifree_reservation(mp);
5538f794055SNathan Scott 	resp->tr_ichange = xfs_calc_ichange_reservation(mp);
5548f794055SNathan Scott 	resp->tr_growdata = xfs_calc_growdata_reservation(mp);
5558f794055SNathan Scott 	resp->tr_swrite = xfs_calc_swrite_reservation(mp);
5568f794055SNathan Scott 	resp->tr_writeid = xfs_calc_writeid_reservation(mp);
5578f794055SNathan Scott 	resp->tr_addafork = xfs_calc_addafork_reservation(mp);
5588f794055SNathan Scott 	resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
5598f794055SNathan Scott 	resp->tr_attrset = xfs_calc_attrset_reservation(mp);
5608f794055SNathan Scott 	resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
5618f794055SNathan Scott 	resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
5628f794055SNathan Scott 	resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
5638f794055SNathan Scott 	resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
5648f794055SNathan Scott 	resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds /*
5681da177e4SLinus Torvalds  * This routine is called to allocate a transaction structure.
5691da177e4SLinus Torvalds  * The type parameter indicates the type of the transaction.  These
5701da177e4SLinus Torvalds  * are enumerated in xfs_trans.h.
571b2ce3974SAlex Elder  *
572b2ce3974SAlex Elder  * Dynamically allocate the transaction structure from the transaction
573b2ce3974SAlex Elder  * zone, initialize it, and return it to the caller.
5741da177e4SLinus Torvalds  */
575b2ce3974SAlex Elder xfs_trans_t *
576b2ce3974SAlex Elder xfs_trans_alloc(
577b2ce3974SAlex Elder 	xfs_mount_t	*mp,
578b2ce3974SAlex Elder 	uint		type)
5791da177e4SLinus Torvalds {
580b2ce3974SAlex Elder 	xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
581b2ce3974SAlex Elder 	return _xfs_trans_alloc(mp, type, KM_SLEEP);
582b2ce3974SAlex Elder }
583b2ce3974SAlex Elder 
584b2ce3974SAlex Elder xfs_trans_t *
585b2ce3974SAlex Elder _xfs_trans_alloc(
586b2ce3974SAlex Elder 	xfs_mount_t	*mp,
587b2ce3974SAlex Elder 	uint		type,
588b2ce3974SAlex Elder 	uint		memflags)
589b2ce3974SAlex Elder {
590b2ce3974SAlex Elder 	xfs_trans_t	*tp;
5911da177e4SLinus Torvalds 
59234327e13SNathan Scott 	atomic_inc(&mp->m_active_trans);
5931da177e4SLinus Torvalds 
59480641dc6SChristoph Hellwig 	tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
5951da177e4SLinus Torvalds 	tp->t_magic = XFS_TRANS_MAGIC;
5961da177e4SLinus Torvalds 	tp->t_type = type;
5971da177e4SLinus Torvalds 	tp->t_mountp = mp;
598e98c414fSChristoph Hellwig 	INIT_LIST_HEAD(&tp->t_items);
599ed3b4d6cSDave Chinner 	INIT_LIST_HEAD(&tp->t_busy);
60034327e13SNathan Scott 	return tp;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds /*
604b1c1b5b6SDave Chinner  * Free the transaction structure.  If there is more clean up
605b1c1b5b6SDave Chinner  * to do when the structure is freed, add it here.
606b1c1b5b6SDave Chinner  */
607b1c1b5b6SDave Chinner STATIC void
608b1c1b5b6SDave Chinner xfs_trans_free(
609ed3b4d6cSDave Chinner 	struct xfs_trans	*tp)
610b1c1b5b6SDave Chinner {
6118a072a4dSChristoph Hellwig 	xfs_alloc_busy_sort(&tp->t_busy);
612e84661aaSChristoph Hellwig 	xfs_alloc_busy_clear(tp->t_mountp, &tp->t_busy, false);
613ed3b4d6cSDave Chinner 
614b1c1b5b6SDave Chinner 	atomic_dec(&tp->t_mountp->m_active_trans);
615b1c1b5b6SDave Chinner 	xfs_trans_free_dqinfo(tp);
616b1c1b5b6SDave Chinner 	kmem_zone_free(xfs_trans_zone, tp);
617b1c1b5b6SDave Chinner }
618b1c1b5b6SDave Chinner 
619b1c1b5b6SDave Chinner /*
6201da177e4SLinus Torvalds  * This is called to create a new transaction which will share the
6211da177e4SLinus Torvalds  * permanent log reservation of the given transaction.  The remaining
6221da177e4SLinus Torvalds  * unused block and rt extent reservations are also inherited.  This
6231da177e4SLinus Torvalds  * implies that the original transaction is no longer allowed to allocate
6241da177e4SLinus Torvalds  * blocks.  Locks and log items, however, are no inherited.  They must
6251da177e4SLinus Torvalds  * be added to the new transaction explicitly.
6261da177e4SLinus Torvalds  */
6271da177e4SLinus Torvalds xfs_trans_t *
6281da177e4SLinus Torvalds xfs_trans_dup(
6291da177e4SLinus Torvalds 	xfs_trans_t	*tp)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	xfs_trans_t	*ntp;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	/*
6361da177e4SLinus Torvalds 	 * Initialize the new transaction structure.
6371da177e4SLinus Torvalds 	 */
6381da177e4SLinus Torvalds 	ntp->t_magic = XFS_TRANS_MAGIC;
6391da177e4SLinus Torvalds 	ntp->t_type = tp->t_type;
6401da177e4SLinus Torvalds 	ntp->t_mountp = tp->t_mountp;
641e98c414fSChristoph Hellwig 	INIT_LIST_HEAD(&ntp->t_items);
642ed3b4d6cSDave Chinner 	INIT_LIST_HEAD(&ntp->t_busy);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
6451da177e4SLinus Torvalds 	ASSERT(tp->t_ticket != NULL);
646cfcbbbd0SNathan Scott 
6471da177e4SLinus Torvalds 	ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
648cc09c0dcSDave Chinner 	ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
6491da177e4SLinus Torvalds 	ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
6501da177e4SLinus Torvalds 	tp->t_blk_res = tp->t_blk_res_used;
6511da177e4SLinus Torvalds 	ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
6521da177e4SLinus Torvalds 	tp->t_rtx_res = tp->t_rtx_res_used;
65359c1b082SNathan Scott 	ntp->t_pflags = tp->t_pflags;
6541da177e4SLinus Torvalds 
6557d095257SChristoph Hellwig 	xfs_trans_dup_dqinfo(tp, ntp);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	atomic_inc(&tp->t_mountp->m_active_trans);
6581da177e4SLinus Torvalds 	return ntp;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /*
6621da177e4SLinus Torvalds  * This is called to reserve free disk blocks and log space for the
6631da177e4SLinus Torvalds  * given transaction.  This must be done before allocating any resources
6641da177e4SLinus Torvalds  * within the transaction.
6651da177e4SLinus Torvalds  *
6661da177e4SLinus Torvalds  * This will return ENOSPC if there are not enough blocks available.
6671da177e4SLinus Torvalds  * It will sleep waiting for available log space.
6681da177e4SLinus Torvalds  * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
6691da177e4SLinus Torvalds  * is used by long running transactions.  If any one of the reservations
6701da177e4SLinus Torvalds  * fails then they will all be backed out.
6711da177e4SLinus Torvalds  *
6721da177e4SLinus Torvalds  * This does not do quota reservations. That typically is done by the
6731da177e4SLinus Torvalds  * caller afterwards.
6741da177e4SLinus Torvalds  */
6751da177e4SLinus Torvalds int
6761da177e4SLinus Torvalds xfs_trans_reserve(
6771da177e4SLinus Torvalds 	xfs_trans_t	*tp,
6781da177e4SLinus Torvalds 	uint		blocks,
6791da177e4SLinus Torvalds 	uint		logspace,
6801da177e4SLinus Torvalds 	uint		rtextents,
6811da177e4SLinus Torvalds 	uint		flags,
6821da177e4SLinus Torvalds 	uint		logcount)
6831da177e4SLinus Torvalds {
68459c1b082SNathan Scott 	int		error = 0;
68559c1b082SNathan Scott 	int		rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	/* Mark this thread as being in a transaction */
68859c1b082SNathan Scott 	current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	/*
6911da177e4SLinus Torvalds 	 * Attempt to reserve the needed disk blocks by decrementing
6921da177e4SLinus Torvalds 	 * the number needed from the number available.  This will
6931da177e4SLinus Torvalds 	 * fail if the count would go below zero.
6941da177e4SLinus Torvalds 	 */
6951da177e4SLinus Torvalds 	if (blocks > 0) {
69696540c78SChristoph Hellwig 		error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
69720f4ebf2SDavid Chinner 					  -((int64_t)blocks), rsvd);
6981da177e4SLinus Torvalds 		if (error != 0) {
69959c1b082SNathan Scott 			current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
7001da177e4SLinus Torvalds 			return (XFS_ERROR(ENOSPC));
7011da177e4SLinus Torvalds 		}
7021da177e4SLinus Torvalds 		tp->t_blk_res += blocks;
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	/*
7061da177e4SLinus Torvalds 	 * Reserve the log space needed for this transaction.
7071da177e4SLinus Torvalds 	 */
7081da177e4SLinus Torvalds 	if (logspace > 0) {
709*9006fb91SChristoph Hellwig 		bool	permanent = false;
710*9006fb91SChristoph Hellwig 
711*9006fb91SChristoph Hellwig 		ASSERT(tp->t_log_res == 0 || tp->t_log_res == logspace);
712*9006fb91SChristoph Hellwig 		ASSERT(tp->t_log_count == 0 || tp->t_log_count == logcount);
713*9006fb91SChristoph Hellwig 
7141da177e4SLinus Torvalds 		if (flags & XFS_TRANS_PERM_LOG_RES) {
7151da177e4SLinus Torvalds 			tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
716*9006fb91SChristoph Hellwig 			permanent = true;
7171da177e4SLinus Torvalds 		} else {
7181da177e4SLinus Torvalds 			ASSERT(tp->t_ticket == NULL);
7191da177e4SLinus Torvalds 			ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
7201da177e4SLinus Torvalds 		}
7211da177e4SLinus Torvalds 
722*9006fb91SChristoph Hellwig 		if (tp->t_ticket != NULL) {
723*9006fb91SChristoph Hellwig 			ASSERT(flags & XFS_TRANS_PERM_LOG_RES);
724*9006fb91SChristoph Hellwig 			error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
725*9006fb91SChristoph Hellwig 		} else {
726*9006fb91SChristoph Hellwig 			error = xfs_log_reserve(tp->t_mountp, logspace,
727*9006fb91SChristoph Hellwig 						logcount, &tp->t_ticket,
728*9006fb91SChristoph Hellwig 						XFS_TRANSACTION, permanent,
729*9006fb91SChristoph Hellwig 						tp->t_type);
7301da177e4SLinus Torvalds 		}
731*9006fb91SChristoph Hellwig 
732*9006fb91SChristoph Hellwig 		if (error)
733*9006fb91SChristoph Hellwig 			goto undo_blocks;
734*9006fb91SChristoph Hellwig 
7351da177e4SLinus Torvalds 		tp->t_log_res = logspace;
7361da177e4SLinus Torvalds 		tp->t_log_count = logcount;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	/*
7401da177e4SLinus Torvalds 	 * Attempt to reserve the needed realtime extents by decrementing
7411da177e4SLinus Torvalds 	 * the number needed from the number available.  This will
7421da177e4SLinus Torvalds 	 * fail if the count would go below zero.
7431da177e4SLinus Torvalds 	 */
7441da177e4SLinus Torvalds 	if (rtextents > 0) {
7451da177e4SLinus Torvalds 		error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
74620f4ebf2SDavid Chinner 					  -((int64_t)rtextents), rsvd);
7471da177e4SLinus Torvalds 		if (error) {
7481da177e4SLinus Torvalds 			error = XFS_ERROR(ENOSPC);
7491da177e4SLinus Torvalds 			goto undo_log;
7501da177e4SLinus Torvalds 		}
7511da177e4SLinus Torvalds 		tp->t_rtx_res += rtextents;
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	return 0;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	/*
7571da177e4SLinus Torvalds 	 * Error cases jump to one of these labels to undo any
7581da177e4SLinus Torvalds 	 * reservations which have already been performed.
7591da177e4SLinus Torvalds 	 */
7601da177e4SLinus Torvalds undo_log:
7611da177e4SLinus Torvalds 	if (logspace > 0) {
762*9006fb91SChristoph Hellwig 		int		log_flags;
763*9006fb91SChristoph Hellwig 
7641da177e4SLinus Torvalds 		if (flags & XFS_TRANS_PERM_LOG_RES) {
7651da177e4SLinus Torvalds 			log_flags = XFS_LOG_REL_PERM_RESERV;
7661da177e4SLinus Torvalds 		} else {
7671da177e4SLinus Torvalds 			log_flags = 0;
7681da177e4SLinus Torvalds 		}
7691da177e4SLinus Torvalds 		xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
7701da177e4SLinus Torvalds 		tp->t_ticket = NULL;
7711da177e4SLinus Torvalds 		tp->t_log_res = 0;
7721da177e4SLinus Torvalds 		tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds undo_blocks:
7761da177e4SLinus Torvalds 	if (blocks > 0) {
77796540c78SChristoph Hellwig 		xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
77820f4ebf2SDavid Chinner 					 (int64_t)blocks, rsvd);
7791da177e4SLinus Torvalds 		tp->t_blk_res = 0;
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 
78259c1b082SNathan Scott 	current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
7831da177e4SLinus Torvalds 
78459c1b082SNathan Scott 	return error;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds /*
7881da177e4SLinus Torvalds  * Record the indicated change to the given field for application
7891da177e4SLinus Torvalds  * to the file system's superblock when the transaction commits.
7901da177e4SLinus Torvalds  * For now, just store the change in the transaction structure.
7911da177e4SLinus Torvalds  *
7921da177e4SLinus Torvalds  * Mark the transaction structure to indicate that the superblock
7931da177e4SLinus Torvalds  * needs to be updated before committing.
79492821e2bSDavid Chinner  *
79592821e2bSDavid Chinner  * Because we may not be keeping track of allocated/free inodes and
79692821e2bSDavid Chinner  * used filesystem blocks in the superblock, we do not mark the
79792821e2bSDavid Chinner  * superblock dirty in this transaction if we modify these fields.
79892821e2bSDavid Chinner  * We still need to update the transaction deltas so that they get
79992821e2bSDavid Chinner  * applied to the incore superblock, but we don't want them to
80092821e2bSDavid Chinner  * cause the superblock to get locked and logged if these are the
80192821e2bSDavid Chinner  * only fields in the superblock that the transaction modifies.
8021da177e4SLinus Torvalds  */
8031da177e4SLinus Torvalds void
8041da177e4SLinus Torvalds xfs_trans_mod_sb(
8051da177e4SLinus Torvalds 	xfs_trans_t	*tp,
8061da177e4SLinus Torvalds 	uint		field,
80720f4ebf2SDavid Chinner 	int64_t		delta)
8081da177e4SLinus Torvalds {
80992821e2bSDavid Chinner 	uint32_t	flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
81092821e2bSDavid Chinner 	xfs_mount_t	*mp = tp->t_mountp;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	switch (field) {
8131da177e4SLinus Torvalds 	case XFS_TRANS_SB_ICOUNT:
8141da177e4SLinus Torvalds 		tp->t_icount_delta += delta;
81592821e2bSDavid Chinner 		if (xfs_sb_version_haslazysbcount(&mp->m_sb))
81692821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
8171da177e4SLinus Torvalds 		break;
8181da177e4SLinus Torvalds 	case XFS_TRANS_SB_IFREE:
8191da177e4SLinus Torvalds 		tp->t_ifree_delta += delta;
82092821e2bSDavid Chinner 		if (xfs_sb_version_haslazysbcount(&mp->m_sb))
82192821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
8221da177e4SLinus Torvalds 		break;
8231da177e4SLinus Torvalds 	case XFS_TRANS_SB_FDBLOCKS:
8241da177e4SLinus Torvalds 		/*
8251da177e4SLinus Torvalds 		 * Track the number of blocks allocated in the
8261da177e4SLinus Torvalds 		 * transaction.  Make sure it does not exceed the
8271da177e4SLinus Torvalds 		 * number reserved.
8281da177e4SLinus Torvalds 		 */
8291da177e4SLinus Torvalds 		if (delta < 0) {
8301da177e4SLinus Torvalds 			tp->t_blk_res_used += (uint)-delta;
8311da177e4SLinus Torvalds 			ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
8321da177e4SLinus Torvalds 		}
8331da177e4SLinus Torvalds 		tp->t_fdblocks_delta += delta;
83492821e2bSDavid Chinner 		if (xfs_sb_version_haslazysbcount(&mp->m_sb))
83592821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
8361da177e4SLinus Torvalds 		break;
8371da177e4SLinus Torvalds 	case XFS_TRANS_SB_RES_FDBLOCKS:
8381da177e4SLinus Torvalds 		/*
8391da177e4SLinus Torvalds 		 * The allocation has already been applied to the
8401da177e4SLinus Torvalds 		 * in-core superblock's counter.  This should only
8411da177e4SLinus Torvalds 		 * be applied to the on-disk superblock.
8421da177e4SLinus Torvalds 		 */
8431da177e4SLinus Torvalds 		ASSERT(delta < 0);
8441da177e4SLinus Torvalds 		tp->t_res_fdblocks_delta += delta;
84592821e2bSDavid Chinner 		if (xfs_sb_version_haslazysbcount(&mp->m_sb))
84692821e2bSDavid Chinner 			flags &= ~XFS_TRANS_SB_DIRTY;
8471da177e4SLinus Torvalds 		break;
8481da177e4SLinus Torvalds 	case XFS_TRANS_SB_FREXTENTS:
8491da177e4SLinus Torvalds 		/*
8501da177e4SLinus Torvalds 		 * Track the number of blocks allocated in the
8511da177e4SLinus Torvalds 		 * transaction.  Make sure it does not exceed the
8521da177e4SLinus Torvalds 		 * number reserved.
8531da177e4SLinus Torvalds 		 */
8541da177e4SLinus Torvalds 		if (delta < 0) {
8551da177e4SLinus Torvalds 			tp->t_rtx_res_used += (uint)-delta;
8561da177e4SLinus Torvalds 			ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
8571da177e4SLinus Torvalds 		}
8581da177e4SLinus Torvalds 		tp->t_frextents_delta += delta;
8591da177e4SLinus Torvalds 		break;
8601da177e4SLinus Torvalds 	case XFS_TRANS_SB_RES_FREXTENTS:
8611da177e4SLinus Torvalds 		/*
8621da177e4SLinus Torvalds 		 * The allocation has already been applied to the
863c41564b5SNathan Scott 		 * in-core superblock's counter.  This should only
8641da177e4SLinus Torvalds 		 * be applied to the on-disk superblock.
8651da177e4SLinus Torvalds 		 */
8661da177e4SLinus Torvalds 		ASSERT(delta < 0);
8671da177e4SLinus Torvalds 		tp->t_res_frextents_delta += delta;
8681da177e4SLinus Torvalds 		break;
8691da177e4SLinus Torvalds 	case XFS_TRANS_SB_DBLOCKS:
8701da177e4SLinus Torvalds 		ASSERT(delta > 0);
8711da177e4SLinus Torvalds 		tp->t_dblocks_delta += delta;
8721da177e4SLinus Torvalds 		break;
8731da177e4SLinus Torvalds 	case XFS_TRANS_SB_AGCOUNT:
8741da177e4SLinus Torvalds 		ASSERT(delta > 0);
8751da177e4SLinus Torvalds 		tp->t_agcount_delta += delta;
8761da177e4SLinus Torvalds 		break;
8771da177e4SLinus Torvalds 	case XFS_TRANS_SB_IMAXPCT:
8781da177e4SLinus Torvalds 		tp->t_imaxpct_delta += delta;
8791da177e4SLinus Torvalds 		break;
8801da177e4SLinus Torvalds 	case XFS_TRANS_SB_REXTSIZE:
8811da177e4SLinus Torvalds 		tp->t_rextsize_delta += delta;
8821da177e4SLinus Torvalds 		break;
8831da177e4SLinus Torvalds 	case XFS_TRANS_SB_RBMBLOCKS:
8841da177e4SLinus Torvalds 		tp->t_rbmblocks_delta += delta;
8851da177e4SLinus Torvalds 		break;
8861da177e4SLinus Torvalds 	case XFS_TRANS_SB_RBLOCKS:
8871da177e4SLinus Torvalds 		tp->t_rblocks_delta += delta;
8881da177e4SLinus Torvalds 		break;
8891da177e4SLinus Torvalds 	case XFS_TRANS_SB_REXTENTS:
8901da177e4SLinus Torvalds 		tp->t_rextents_delta += delta;
8911da177e4SLinus Torvalds 		break;
8921da177e4SLinus Torvalds 	case XFS_TRANS_SB_REXTSLOG:
8931da177e4SLinus Torvalds 		tp->t_rextslog_delta += delta;
8941da177e4SLinus Torvalds 		break;
8951da177e4SLinus Torvalds 	default:
8961da177e4SLinus Torvalds 		ASSERT(0);
8971da177e4SLinus Torvalds 		return;
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 
900210c6f1cSDavid Chinner 	tp->t_flags |= flags;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds /*
9041da177e4SLinus Torvalds  * xfs_trans_apply_sb_deltas() is called from the commit code
9051da177e4SLinus Torvalds  * to bring the superblock buffer into the current transaction
9061da177e4SLinus Torvalds  * and modify it as requested by earlier calls to xfs_trans_mod_sb().
9071da177e4SLinus Torvalds  *
9081da177e4SLinus Torvalds  * For now we just look at each field allowed to change and change
9091da177e4SLinus Torvalds  * it if necessary.
9101da177e4SLinus Torvalds  */
9111da177e4SLinus Torvalds STATIC void
9121da177e4SLinus Torvalds xfs_trans_apply_sb_deltas(
9131da177e4SLinus Torvalds 	xfs_trans_t	*tp)
9141da177e4SLinus Torvalds {
9152bdf7cd0SChristoph Hellwig 	xfs_dsb_t	*sbp;
9161da177e4SLinus Torvalds 	xfs_buf_t	*bp;
9171da177e4SLinus Torvalds 	int		whole = 0;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
9201da177e4SLinus Torvalds 	sbp = XFS_BUF_TO_SBP(bp);
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	/*
9231da177e4SLinus Torvalds 	 * Check that superblock mods match the mods made to AGF counters.
9241da177e4SLinus Torvalds 	 */
9251da177e4SLinus Torvalds 	ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
9261da177e4SLinus Torvalds 	       (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
9271da177e4SLinus Torvalds 		tp->t_ag_btree_delta));
9281da177e4SLinus Torvalds 
92992821e2bSDavid Chinner 	/*
93092821e2bSDavid Chinner 	 * Only update the superblock counters if we are logging them
93192821e2bSDavid Chinner 	 */
93292821e2bSDavid Chinner 	if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
9332bdf7cd0SChristoph Hellwig 		if (tp->t_icount_delta)
934413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
9352bdf7cd0SChristoph Hellwig 		if (tp->t_ifree_delta)
936413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
9372bdf7cd0SChristoph Hellwig 		if (tp->t_fdblocks_delta)
938413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
9392bdf7cd0SChristoph Hellwig 		if (tp->t_res_fdblocks_delta)
940413d57c9SMarcin Slusarz 			be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
9432bdf7cd0SChristoph Hellwig 	if (tp->t_frextents_delta)
944413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
9452bdf7cd0SChristoph Hellwig 	if (tp->t_res_frextents_delta)
946413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
9471da177e4SLinus Torvalds 
9482bdf7cd0SChristoph Hellwig 	if (tp->t_dblocks_delta) {
949413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
9501da177e4SLinus Torvalds 		whole = 1;
9511da177e4SLinus Torvalds 	}
9522bdf7cd0SChristoph Hellwig 	if (tp->t_agcount_delta) {
953413d57c9SMarcin Slusarz 		be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
9541da177e4SLinus Torvalds 		whole = 1;
9551da177e4SLinus Torvalds 	}
9562bdf7cd0SChristoph Hellwig 	if (tp->t_imaxpct_delta) {
9572bdf7cd0SChristoph Hellwig 		sbp->sb_imax_pct += tp->t_imaxpct_delta;
9581da177e4SLinus Torvalds 		whole = 1;
9591da177e4SLinus Torvalds 	}
9602bdf7cd0SChristoph Hellwig 	if (tp->t_rextsize_delta) {
961413d57c9SMarcin Slusarz 		be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
9621da177e4SLinus Torvalds 		whole = 1;
9631da177e4SLinus Torvalds 	}
9642bdf7cd0SChristoph Hellwig 	if (tp->t_rbmblocks_delta) {
965413d57c9SMarcin Slusarz 		be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
9661da177e4SLinus Torvalds 		whole = 1;
9671da177e4SLinus Torvalds 	}
9682bdf7cd0SChristoph Hellwig 	if (tp->t_rblocks_delta) {
969413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
9701da177e4SLinus Torvalds 		whole = 1;
9711da177e4SLinus Torvalds 	}
9722bdf7cd0SChristoph Hellwig 	if (tp->t_rextents_delta) {
973413d57c9SMarcin Slusarz 		be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
9741da177e4SLinus Torvalds 		whole = 1;
9751da177e4SLinus Torvalds 	}
9762bdf7cd0SChristoph Hellwig 	if (tp->t_rextslog_delta) {
9772bdf7cd0SChristoph Hellwig 		sbp->sb_rextslog += tp->t_rextslog_delta;
9781da177e4SLinus Torvalds 		whole = 1;
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	if (whole)
9821da177e4SLinus Torvalds 		/*
983c41564b5SNathan Scott 		 * Log the whole thing, the fields are noncontiguous.
9841da177e4SLinus Torvalds 		 */
9852bdf7cd0SChristoph Hellwig 		xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
9861da177e4SLinus Torvalds 	else
9871da177e4SLinus Torvalds 		/*
9881da177e4SLinus Torvalds 		 * Since all the modifiable fields are contiguous, we
9891da177e4SLinus Torvalds 		 * can get away with this.
9901da177e4SLinus Torvalds 		 */
9912bdf7cd0SChristoph Hellwig 		xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
9922bdf7cd0SChristoph Hellwig 				  offsetof(xfs_dsb_t, sb_frextents) +
9931da177e4SLinus Torvalds 				  sizeof(sbp->sb_frextents) - 1);
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds /*
99745c34141SDavid Chinner  * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
99845c34141SDavid Chinner  * and apply superblock counter changes to the in-core superblock.  The
99945c34141SDavid Chinner  * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
100045c34141SDavid Chinner  * applied to the in-core superblock.  The idea is that that has already been
100145c34141SDavid Chinner  * done.
10021da177e4SLinus Torvalds  *
10031da177e4SLinus Torvalds  * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
100445c34141SDavid Chinner  * However, we have to ensure that we only modify each superblock field only
100545c34141SDavid Chinner  * once because the application of the delta values may not be atomic. That can
100645c34141SDavid Chinner  * lead to ENOSPC races occurring if we have two separate modifcations of the
100745c34141SDavid Chinner  * free space counter to put back the entire reservation and then take away
100845c34141SDavid Chinner  * what we used.
100945c34141SDavid Chinner  *
101045c34141SDavid Chinner  * If we are not logging superblock counters, then the inode allocated/free and
101145c34141SDavid Chinner  * used block counts are not updated in the on disk superblock. In this case,
101245c34141SDavid Chinner  * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
101345c34141SDavid Chinner  * still need to update the incore superblock with the changes.
10141da177e4SLinus Torvalds  */
101571e330b5SDave Chinner void
10161da177e4SLinus Torvalds xfs_trans_unreserve_and_mod_sb(
10171da177e4SLinus Torvalds 	xfs_trans_t	*tp)
10181da177e4SLinus Torvalds {
10191b040712SChristoph Hellwig 	xfs_mod_sb_t	msb[9];	/* If you add cases, add entries */
10201da177e4SLinus Torvalds 	xfs_mod_sb_t	*msbp;
102192821e2bSDavid Chinner 	xfs_mount_t	*mp = tp->t_mountp;
10221da177e4SLinus Torvalds 	/* REFERENCED */
10231da177e4SLinus Torvalds 	int		error;
10241da177e4SLinus Torvalds 	int		rsvd;
102545c34141SDavid Chinner 	int64_t		blkdelta = 0;
102645c34141SDavid Chinner 	int64_t		rtxdelta = 0;
10271b040712SChristoph Hellwig 	int64_t		idelta = 0;
10281b040712SChristoph Hellwig 	int64_t		ifreedelta = 0;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	msbp = msb;
10311da177e4SLinus Torvalds 	rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
10321da177e4SLinus Torvalds 
10331b040712SChristoph Hellwig 	/* calculate deltas */
103445c34141SDavid Chinner 	if (tp->t_blk_res > 0)
103545c34141SDavid Chinner 		blkdelta = tp->t_blk_res;
103645c34141SDavid Chinner 	if ((tp->t_fdblocks_delta != 0) &&
103745c34141SDavid Chinner 	    (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
103845c34141SDavid Chinner 	     (tp->t_flags & XFS_TRANS_SB_DIRTY)))
103945c34141SDavid Chinner 	        blkdelta += tp->t_fdblocks_delta;
104045c34141SDavid Chinner 
104145c34141SDavid Chinner 	if (tp->t_rtx_res > 0)
104245c34141SDavid Chinner 		rtxdelta = tp->t_rtx_res;
104345c34141SDavid Chinner 	if ((tp->t_frextents_delta != 0) &&
104445c34141SDavid Chinner 	    (tp->t_flags & XFS_TRANS_SB_DIRTY))
104545c34141SDavid Chinner 		rtxdelta += tp->t_frextents_delta;
104645c34141SDavid Chinner 
10471b040712SChristoph Hellwig 	if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
10481b040712SChristoph Hellwig 	     (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
10491b040712SChristoph Hellwig 		idelta = tp->t_icount_delta;
10501b040712SChristoph Hellwig 		ifreedelta = tp->t_ifree_delta;
10511b040712SChristoph Hellwig 	}
10521b040712SChristoph Hellwig 
10531b040712SChristoph Hellwig 	/* apply the per-cpu counters */
10541b040712SChristoph Hellwig 	if (blkdelta) {
10551b040712SChristoph Hellwig 		error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
10561b040712SChristoph Hellwig 						 blkdelta, rsvd);
10571b040712SChristoph Hellwig 		if (error)
10581b040712SChristoph Hellwig 			goto out;
10591b040712SChristoph Hellwig 	}
10601b040712SChristoph Hellwig 
10611b040712SChristoph Hellwig 	if (idelta) {
10621b040712SChristoph Hellwig 		error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
10631b040712SChristoph Hellwig 						 idelta, rsvd);
10641b040712SChristoph Hellwig 		if (error)
10651b040712SChristoph Hellwig 			goto out_undo_fdblocks;
10661b040712SChristoph Hellwig 	}
10671b040712SChristoph Hellwig 
10681b040712SChristoph Hellwig 	if (ifreedelta) {
10691b040712SChristoph Hellwig 		error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
10701b040712SChristoph Hellwig 						 ifreedelta, rsvd);
10711b040712SChristoph Hellwig 		if (error)
10721b040712SChristoph Hellwig 			goto out_undo_icount;
10731b040712SChristoph Hellwig 	}
10741b040712SChristoph Hellwig 
10751b040712SChristoph Hellwig 	/* apply remaining deltas */
107645c34141SDavid Chinner 	if (rtxdelta != 0) {
10771da177e4SLinus Torvalds 		msbp->msb_field = XFS_SBS_FREXTENTS;
107845c34141SDavid Chinner 		msbp->msb_delta = rtxdelta;
10791da177e4SLinus Torvalds 		msbp++;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
108292821e2bSDavid Chinner 	if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
10831da177e4SLinus Torvalds 		if (tp->t_dblocks_delta != 0) {
10841da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_DBLOCKS;
108520f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_dblocks_delta;
10861da177e4SLinus Torvalds 			msbp++;
10871da177e4SLinus Torvalds 		}
10881da177e4SLinus Torvalds 		if (tp->t_agcount_delta != 0) {
10891da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_AGCOUNT;
109020f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_agcount_delta;
10911da177e4SLinus Torvalds 			msbp++;
10921da177e4SLinus Torvalds 		}
10931da177e4SLinus Torvalds 		if (tp->t_imaxpct_delta != 0) {
10941da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_IMAX_PCT;
109520f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_imaxpct_delta;
10961da177e4SLinus Torvalds 			msbp++;
10971da177e4SLinus Torvalds 		}
10981da177e4SLinus Torvalds 		if (tp->t_rextsize_delta != 0) {
10991da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_REXTSIZE;
110020f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_rextsize_delta;
11011da177e4SLinus Torvalds 			msbp++;
11021da177e4SLinus Torvalds 		}
11031da177e4SLinus Torvalds 		if (tp->t_rbmblocks_delta != 0) {
11041da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_RBMBLOCKS;
110520f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_rbmblocks_delta;
11061da177e4SLinus Torvalds 			msbp++;
11071da177e4SLinus Torvalds 		}
11081da177e4SLinus Torvalds 		if (tp->t_rblocks_delta != 0) {
11091da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_RBLOCKS;
111020f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_rblocks_delta;
11111da177e4SLinus Torvalds 			msbp++;
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 		if (tp->t_rextents_delta != 0) {
11141da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_REXTENTS;
111520f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_rextents_delta;
11161da177e4SLinus Torvalds 			msbp++;
11171da177e4SLinus Torvalds 		}
11181da177e4SLinus Torvalds 		if (tp->t_rextslog_delta != 0) {
11191da177e4SLinus Torvalds 			msbp->msb_field = XFS_SBS_REXTSLOG;
112020f4ebf2SDavid Chinner 			msbp->msb_delta = tp->t_rextslog_delta;
11211da177e4SLinus Torvalds 			msbp++;
11221da177e4SLinus Torvalds 		}
11231da177e4SLinus Torvalds 	}
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	/*
11261da177e4SLinus Torvalds 	 * If we need to change anything, do it.
11271da177e4SLinus Torvalds 	 */
11281da177e4SLinus Torvalds 	if (msbp > msb) {
11291da177e4SLinus Torvalds 		error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
11301da177e4SLinus Torvalds 			(uint)(msbp - msb), rsvd);
11311b040712SChristoph Hellwig 		if (error)
11321b040712SChristoph Hellwig 			goto out_undo_ifreecount;
11331da177e4SLinus Torvalds 	}
11341b040712SChristoph Hellwig 
11351b040712SChristoph Hellwig 	return;
11361b040712SChristoph Hellwig 
11371b040712SChristoph Hellwig out_undo_ifreecount:
11381b040712SChristoph Hellwig 	if (ifreedelta)
11391b040712SChristoph Hellwig 		xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
11401b040712SChristoph Hellwig out_undo_icount:
11411b040712SChristoph Hellwig 	if (idelta)
11421b040712SChristoph Hellwig 		xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
11431b040712SChristoph Hellwig out_undo_fdblocks:
11441b040712SChristoph Hellwig 	if (blkdelta)
11451b040712SChristoph Hellwig 		xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
11461b040712SChristoph Hellwig out:
11471884bd83SJesper Juhl 	ASSERT(error == 0);
11481b040712SChristoph Hellwig 	return;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds /*
1152e98c414fSChristoph Hellwig  * Add the given log item to the transaction's list of log items.
1153e98c414fSChristoph Hellwig  *
1154e98c414fSChristoph Hellwig  * The log item will now point to its new descriptor with its li_desc field.
1155e98c414fSChristoph Hellwig  */
1156e98c414fSChristoph Hellwig void
1157e98c414fSChristoph Hellwig xfs_trans_add_item(
1158e98c414fSChristoph Hellwig 	struct xfs_trans	*tp,
1159e98c414fSChristoph Hellwig 	struct xfs_log_item	*lip)
1160e98c414fSChristoph Hellwig {
1161e98c414fSChristoph Hellwig 	struct xfs_log_item_desc *lidp;
1162e98c414fSChristoph Hellwig 
1163f65020a8SJesper Juhl 	ASSERT(lip->li_mountp == tp->t_mountp);
1164f65020a8SJesper Juhl 	ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
1165e98c414fSChristoph Hellwig 
116643869706SDave Chinner 	lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
1167e98c414fSChristoph Hellwig 
1168e98c414fSChristoph Hellwig 	lidp->lid_item = lip;
1169e98c414fSChristoph Hellwig 	lidp->lid_flags = 0;
1170e98c414fSChristoph Hellwig 	list_add_tail(&lidp->lid_trans, &tp->t_items);
1171e98c414fSChristoph Hellwig 
1172e98c414fSChristoph Hellwig 	lip->li_desc = lidp;
1173e98c414fSChristoph Hellwig }
1174e98c414fSChristoph Hellwig 
1175e98c414fSChristoph Hellwig STATIC void
1176e98c414fSChristoph Hellwig xfs_trans_free_item_desc(
1177e98c414fSChristoph Hellwig 	struct xfs_log_item_desc *lidp)
1178e98c414fSChristoph Hellwig {
1179e98c414fSChristoph Hellwig 	list_del_init(&lidp->lid_trans);
1180e98c414fSChristoph Hellwig 	kmem_zone_free(xfs_log_item_desc_zone, lidp);
1181e98c414fSChristoph Hellwig }
1182e98c414fSChristoph Hellwig 
1183e98c414fSChristoph Hellwig /*
1184e98c414fSChristoph Hellwig  * Unlink and free the given descriptor.
1185e98c414fSChristoph Hellwig  */
1186e98c414fSChristoph Hellwig void
1187e98c414fSChristoph Hellwig xfs_trans_del_item(
1188e98c414fSChristoph Hellwig 	struct xfs_log_item	*lip)
1189e98c414fSChristoph Hellwig {
1190e98c414fSChristoph Hellwig 	xfs_trans_free_item_desc(lip->li_desc);
1191e98c414fSChristoph Hellwig 	lip->li_desc = NULL;
1192e98c414fSChristoph Hellwig }
1193e98c414fSChristoph Hellwig 
1194e98c414fSChristoph Hellwig /*
1195e98c414fSChristoph Hellwig  * Unlock all of the items of a transaction and free all the descriptors
1196e98c414fSChristoph Hellwig  * of that transaction.
1197e98c414fSChristoph Hellwig  */
1198d17c701cSDave Chinner void
1199e98c414fSChristoph Hellwig xfs_trans_free_items(
1200e98c414fSChristoph Hellwig 	struct xfs_trans	*tp,
1201e98c414fSChristoph Hellwig 	xfs_lsn_t		commit_lsn,
1202e98c414fSChristoph Hellwig 	int			flags)
1203e98c414fSChristoph Hellwig {
1204e98c414fSChristoph Hellwig 	struct xfs_log_item_desc *lidp, *next;
1205e98c414fSChristoph Hellwig 
1206e98c414fSChristoph Hellwig 	list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
1207e98c414fSChristoph Hellwig 		struct xfs_log_item	*lip = lidp->lid_item;
1208e98c414fSChristoph Hellwig 
1209e98c414fSChristoph Hellwig 		lip->li_desc = NULL;
1210e98c414fSChristoph Hellwig 
1211e98c414fSChristoph Hellwig 		if (commit_lsn != NULLCOMMITLSN)
1212e98c414fSChristoph Hellwig 			IOP_COMMITTING(lip, commit_lsn);
1213e98c414fSChristoph Hellwig 		if (flags & XFS_TRANS_ABORT)
1214e98c414fSChristoph Hellwig 			lip->li_flags |= XFS_LI_ABORTED;
1215e98c414fSChristoph Hellwig 		IOP_UNLOCK(lip);
1216e98c414fSChristoph Hellwig 
1217e98c414fSChristoph Hellwig 		xfs_trans_free_item_desc(lidp);
1218e98c414fSChristoph Hellwig 	}
1219e98c414fSChristoph Hellwig }
1220e98c414fSChristoph Hellwig 
12210e57f6a3SDave Chinner static inline void
12220e57f6a3SDave Chinner xfs_log_item_batch_insert(
12230e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
12241d8c95a3SDave Chinner 	struct xfs_ail_cursor	*cur,
12250e57f6a3SDave Chinner 	struct xfs_log_item	**log_items,
12260e57f6a3SDave Chinner 	int			nr_items,
12270e57f6a3SDave Chinner 	xfs_lsn_t		commit_lsn)
12280e57f6a3SDave Chinner {
12290e57f6a3SDave Chinner 	int	i;
12300e57f6a3SDave Chinner 
12310e57f6a3SDave Chinner 	spin_lock(&ailp->xa_lock);
12320e57f6a3SDave Chinner 	/* xfs_trans_ail_update_bulk drops ailp->xa_lock */
12331d8c95a3SDave Chinner 	xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
12340e57f6a3SDave Chinner 
12350e57f6a3SDave Chinner 	for (i = 0; i < nr_items; i++)
12360e57f6a3SDave Chinner 		IOP_UNPIN(log_items[i], 0);
12370e57f6a3SDave Chinner }
12380e57f6a3SDave Chinner 
12390e57f6a3SDave Chinner /*
12400e57f6a3SDave Chinner  * Bulk operation version of xfs_trans_committed that takes a log vector of
12410e57f6a3SDave Chinner  * items to insert into the AIL. This uses bulk AIL insertion techniques to
12420e57f6a3SDave Chinner  * minimise lock traffic.
1243e34a314cSDave Chinner  *
1244e34a314cSDave Chinner  * If we are called with the aborted flag set, it is because a log write during
1245e34a314cSDave Chinner  * a CIL checkpoint commit has failed. In this case, all the items in the
1246e34a314cSDave Chinner  * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
1247e34a314cSDave Chinner  * means that checkpoint commit abort handling is treated exactly the same
1248e34a314cSDave Chinner  * as an iclog write error even though we haven't started any IO yet. Hence in
1249e34a314cSDave Chinner  * this case all we need to do is IOP_COMMITTED processing, followed by an
1250e34a314cSDave Chinner  * IOP_UNPIN(aborted) call.
12511d8c95a3SDave Chinner  *
12521d8c95a3SDave Chinner  * The AIL cursor is used to optimise the insert process. If commit_lsn is not
12531d8c95a3SDave Chinner  * at the end of the AIL, the insert cursor avoids the need to walk
12541d8c95a3SDave Chinner  * the AIL to find the insertion point on every xfs_log_item_batch_insert()
12551d8c95a3SDave Chinner  * call. This saves a lot of needless list walking and is a net win, even
12561d8c95a3SDave Chinner  * though it slightly increases that amount of AIL lock traffic to set it up
12571d8c95a3SDave Chinner  * and tear it down.
12580e57f6a3SDave Chinner  */
12590e57f6a3SDave Chinner void
12600e57f6a3SDave Chinner xfs_trans_committed_bulk(
12610e57f6a3SDave Chinner 	struct xfs_ail		*ailp,
12620e57f6a3SDave Chinner 	struct xfs_log_vec	*log_vector,
12630e57f6a3SDave Chinner 	xfs_lsn_t		commit_lsn,
12640e57f6a3SDave Chinner 	int			aborted)
12650e57f6a3SDave Chinner {
12660e57f6a3SDave Chinner #define LOG_ITEM_BATCH_SIZE	32
12670e57f6a3SDave Chinner 	struct xfs_log_item	*log_items[LOG_ITEM_BATCH_SIZE];
12680e57f6a3SDave Chinner 	struct xfs_log_vec	*lv;
12691d8c95a3SDave Chinner 	struct xfs_ail_cursor	cur;
12700e57f6a3SDave Chinner 	int			i = 0;
12710e57f6a3SDave Chinner 
12721d8c95a3SDave Chinner 	spin_lock(&ailp->xa_lock);
12731d8c95a3SDave Chinner 	xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
12741d8c95a3SDave Chinner 	spin_unlock(&ailp->xa_lock);
12751d8c95a3SDave Chinner 
12760e57f6a3SDave Chinner 	/* unpin all the log items */
12770e57f6a3SDave Chinner 	for (lv = log_vector; lv; lv = lv->lv_next ) {
12780e57f6a3SDave Chinner 		struct xfs_log_item	*lip = lv->lv_item;
12790e57f6a3SDave Chinner 		xfs_lsn_t		item_lsn;
12800e57f6a3SDave Chinner 
12810e57f6a3SDave Chinner 		if (aborted)
12820e57f6a3SDave Chinner 			lip->li_flags |= XFS_LI_ABORTED;
12830e57f6a3SDave Chinner 		item_lsn = IOP_COMMITTED(lip, commit_lsn);
12840e57f6a3SDave Chinner 
12851316d4daSDave Chinner 		/* item_lsn of -1 means the item needs no further processing */
12860e57f6a3SDave Chinner 		if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
12870e57f6a3SDave Chinner 			continue;
12880e57f6a3SDave Chinner 
1289e34a314cSDave Chinner 		/*
1290e34a314cSDave Chinner 		 * if we are aborting the operation, no point in inserting the
1291e34a314cSDave Chinner 		 * object into the AIL as we are in a shutdown situation.
1292e34a314cSDave Chinner 		 */
1293e34a314cSDave Chinner 		if (aborted) {
1294e34a314cSDave Chinner 			ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
1295e34a314cSDave Chinner 			IOP_UNPIN(lip, 1);
1296e34a314cSDave Chinner 			continue;
1297e34a314cSDave Chinner 		}
1298e34a314cSDave Chinner 
12990e57f6a3SDave Chinner 		if (item_lsn != commit_lsn) {
13000e57f6a3SDave Chinner 
13010e57f6a3SDave Chinner 			/*
13020e57f6a3SDave Chinner 			 * Not a bulk update option due to unusual item_lsn.
13030e57f6a3SDave Chinner 			 * Push into AIL immediately, rechecking the lsn once
13041d8c95a3SDave Chinner 			 * we have the ail lock. Then unpin the item. This does
13051d8c95a3SDave Chinner 			 * not affect the AIL cursor the bulk insert path is
13061d8c95a3SDave Chinner 			 * using.
13070e57f6a3SDave Chinner 			 */
13080e57f6a3SDave Chinner 			spin_lock(&ailp->xa_lock);
13090e57f6a3SDave Chinner 			if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
13100e57f6a3SDave Chinner 				xfs_trans_ail_update(ailp, lip, item_lsn);
13110e57f6a3SDave Chinner 			else
13120e57f6a3SDave Chinner 				spin_unlock(&ailp->xa_lock);
13130e57f6a3SDave Chinner 			IOP_UNPIN(lip, 0);
13140e57f6a3SDave Chinner 			continue;
13150e57f6a3SDave Chinner 		}
13160e57f6a3SDave Chinner 
13170e57f6a3SDave Chinner 		/* Item is a candidate for bulk AIL insert.  */
13180e57f6a3SDave Chinner 		log_items[i++] = lv->lv_item;
13190e57f6a3SDave Chinner 		if (i >= LOG_ITEM_BATCH_SIZE) {
13201d8c95a3SDave Chinner 			xfs_log_item_batch_insert(ailp, &cur, log_items,
13210e57f6a3SDave Chinner 					LOG_ITEM_BATCH_SIZE, commit_lsn);
13220e57f6a3SDave Chinner 			i = 0;
13230e57f6a3SDave Chinner 		}
13240e57f6a3SDave Chinner 	}
13250e57f6a3SDave Chinner 
13260e57f6a3SDave Chinner 	/* make sure we insert the remainder! */
13270e57f6a3SDave Chinner 	if (i)
13281d8c95a3SDave Chinner 		xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
13291d8c95a3SDave Chinner 
13301d8c95a3SDave Chinner 	spin_lock(&ailp->xa_lock);
13311d8c95a3SDave Chinner 	xfs_trans_ail_cursor_done(ailp, &cur);
13321d8c95a3SDave Chinner 	spin_unlock(&ailp->xa_lock);
13330e57f6a3SDave Chinner }
13340e57f6a3SDave Chinner 
1335b1c1b5b6SDave Chinner /*
1336b1037058SChristoph Hellwig  * Commit the given transaction to the log.
13370924378aSDave Chinner  *
13380924378aSDave Chinner  * XFS disk error handling mechanism is not based on a typical
13390924378aSDave Chinner  * transaction abort mechanism. Logically after the filesystem
13400924378aSDave Chinner  * gets marked 'SHUTDOWN', we can't let any new transactions
13410924378aSDave Chinner  * be durable - ie. committed to disk - because some metadata might
13420924378aSDave Chinner  * be inconsistent. In such cases, this returns an error, and the
13430924378aSDave Chinner  * caller may assume that all locked objects joined to the transaction
13440924378aSDave Chinner  * have already been unlocked as if the commit had succeeded.
13450924378aSDave Chinner  * Do not reference the transaction structure after this call.
13460924378aSDave Chinner  */
13470924378aSDave Chinner int
1348b1037058SChristoph Hellwig xfs_trans_commit(
1349a3ccd2caSChristoph Hellwig 	struct xfs_trans	*tp,
1350b1037058SChristoph Hellwig 	uint			flags)
13510924378aSDave Chinner {
1352a3ccd2caSChristoph Hellwig 	struct xfs_mount	*mp = tp->t_mountp;
13530924378aSDave Chinner 	xfs_lsn_t		commit_lsn = -1;
1354a3ccd2caSChristoph Hellwig 	int			error = 0;
13550924378aSDave Chinner 	int			log_flags = 0;
13560924378aSDave Chinner 	int			sync = tp->t_flags & XFS_TRANS_SYNC;
13570924378aSDave Chinner 
13580924378aSDave Chinner 	/*
13590924378aSDave Chinner 	 * Determine whether this commit is releasing a permanent
13600924378aSDave Chinner 	 * log reservation or not.
13610924378aSDave Chinner 	 */
13620924378aSDave Chinner 	if (flags & XFS_TRANS_RELEASE_LOG_RES) {
13630924378aSDave Chinner 		ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
13640924378aSDave Chinner 		log_flags = XFS_LOG_REL_PERM_RESERV;
13650924378aSDave Chinner 	}
13660924378aSDave Chinner 
13670924378aSDave Chinner 	/*
13680924378aSDave Chinner 	 * If there is nothing to be logged by the transaction,
13690924378aSDave Chinner 	 * then unlock all of the items associated with the
13700924378aSDave Chinner 	 * transaction and free the transaction structure.
13710924378aSDave Chinner 	 * Also make sure to return any reserved blocks to
13720924378aSDave Chinner 	 * the free pool.
13730924378aSDave Chinner 	 */
1374a3ccd2caSChristoph Hellwig 	if (!(tp->t_flags & XFS_TRANS_DIRTY))
1375a3ccd2caSChristoph Hellwig 		goto out_unreserve;
1376a3ccd2caSChristoph Hellwig 
1377a3ccd2caSChristoph Hellwig 	if (XFS_FORCED_SHUTDOWN(mp)) {
1378a3ccd2caSChristoph Hellwig 		error = XFS_ERROR(EIO);
1379a3ccd2caSChristoph Hellwig 		goto out_unreserve;
13800924378aSDave Chinner 	}
1381a3ccd2caSChristoph Hellwig 
13820924378aSDave Chinner 	ASSERT(tp->t_ticket != NULL);
13830924378aSDave Chinner 
13840924378aSDave Chinner 	/*
13850924378aSDave Chinner 	 * If we need to update the superblock, then do it now.
13860924378aSDave Chinner 	 */
13870924378aSDave Chinner 	if (tp->t_flags & XFS_TRANS_SB_DIRTY)
13880924378aSDave Chinner 		xfs_trans_apply_sb_deltas(tp);
13890924378aSDave Chinner 	xfs_trans_apply_dquot_deltas(tp);
13900924378aSDave Chinner 
13910244b960SChristoph Hellwig 	error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
13920924378aSDave Chinner 	if (error == ENOMEM) {
13930924378aSDave Chinner 		xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1394a3ccd2caSChristoph Hellwig 		error = XFS_ERROR(EIO);
1395a3ccd2caSChristoph Hellwig 		goto out_unreserve;
13960924378aSDave Chinner 	}
13971da177e4SLinus Torvalds 
13980244b960SChristoph Hellwig 	current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
13990244b960SChristoph Hellwig 	xfs_trans_free(tp);
14000244b960SChristoph Hellwig 
14011da177e4SLinus Torvalds 	/*
14021da177e4SLinus Torvalds 	 * If the transaction needs to be synchronous, then force the
14031da177e4SLinus Torvalds 	 * log out now and wait for it.
14041da177e4SLinus Torvalds 	 */
14051da177e4SLinus Torvalds 	if (sync) {
1406f538d4daSChristoph Hellwig 		if (!error) {
1407a14a348bSChristoph Hellwig 			error = _xfs_log_force_lsn(mp, commit_lsn,
1408b1037058SChristoph Hellwig 				      XFS_LOG_SYNC, NULL);
1409f538d4daSChristoph Hellwig 		}
14101da177e4SLinus Torvalds 		XFS_STATS_INC(xs_trans_sync);
14111da177e4SLinus Torvalds 	} else {
14121da177e4SLinus Torvalds 		XFS_STATS_INC(xs_trans_async);
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 
1415a3ccd2caSChristoph Hellwig 	return error;
1416a3ccd2caSChristoph Hellwig 
1417a3ccd2caSChristoph Hellwig out_unreserve:
1418a3ccd2caSChristoph Hellwig 	xfs_trans_unreserve_and_mod_sb(tp);
1419a3ccd2caSChristoph Hellwig 
1420a3ccd2caSChristoph Hellwig 	/*
1421a3ccd2caSChristoph Hellwig 	 * It is indeed possible for the transaction to be not dirty but
1422a3ccd2caSChristoph Hellwig 	 * the dqinfo portion to be.  All that means is that we have some
1423a3ccd2caSChristoph Hellwig 	 * (non-persistent) quota reservations that need to be unreserved.
1424a3ccd2caSChristoph Hellwig 	 */
1425a3ccd2caSChristoph Hellwig 	xfs_trans_unreserve_and_mod_dquots(tp);
1426a3ccd2caSChristoph Hellwig 	if (tp->t_ticket) {
1427a3ccd2caSChristoph Hellwig 		commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1428a3ccd2caSChristoph Hellwig 		if (commit_lsn == -1 && !error)
1429a3ccd2caSChristoph Hellwig 			error = XFS_ERROR(EIO);
1430a3ccd2caSChristoph Hellwig 	}
1431a3ccd2caSChristoph Hellwig 	current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
143271e330b5SDave Chinner 	xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
1433a3ccd2caSChristoph Hellwig 	xfs_trans_free(tp);
1434a3ccd2caSChristoph Hellwig 
1435a3ccd2caSChristoph Hellwig 	XFS_STATS_INC(xs_trans_empty);
1436a3ccd2caSChristoph Hellwig 	return error;
14371da177e4SLinus Torvalds }
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds /*
14401da177e4SLinus Torvalds  * Unlock all of the transaction's items and free the transaction.
14411da177e4SLinus Torvalds  * The transaction must not have modified any of its items, because
14421da177e4SLinus Torvalds  * there is no way to restore them to their previous state.
14431da177e4SLinus Torvalds  *
14441da177e4SLinus Torvalds  * If the transaction has made a log reservation, make sure to release
14451da177e4SLinus Torvalds  * it as well.
14461da177e4SLinus Torvalds  */
14471da177e4SLinus Torvalds void
14481da177e4SLinus Torvalds xfs_trans_cancel(
14491da177e4SLinus Torvalds 	xfs_trans_t		*tp,
14501da177e4SLinus Torvalds 	int			flags)
14511da177e4SLinus Torvalds {
14521da177e4SLinus Torvalds 	int			log_flags;
14530733af21SRyan Hankins 	xfs_mount_t		*mp = tp->t_mountp;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	/*
14561da177e4SLinus Torvalds 	 * See if the caller is being too lazy to figure out if
14571da177e4SLinus Torvalds 	 * the transaction really needs an abort.
14581da177e4SLinus Torvalds 	 */
14591da177e4SLinus Torvalds 	if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
14601da177e4SLinus Torvalds 		flags &= ~XFS_TRANS_ABORT;
14611da177e4SLinus Torvalds 	/*
14621da177e4SLinus Torvalds 	 * See if the caller is relying on us to shut down the
14631da177e4SLinus Torvalds 	 * filesystem.  This happens in paths where we detect
14641da177e4SLinus Torvalds 	 * corruption and decide to give up.
14651da177e4SLinus Torvalds 	 */
146660a204f0SNathan Scott 	if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
14670733af21SRyan Hankins 		XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
14687d04a335SNathan Scott 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
146960a204f0SNathan Scott 	}
14701da177e4SLinus Torvalds #ifdef DEBUG
1471e98c414fSChristoph Hellwig 	if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
1472e98c414fSChristoph Hellwig 		struct xfs_log_item_desc *lidp;
14731da177e4SLinus Torvalds 
1474e98c414fSChristoph Hellwig 		list_for_each_entry(lidp, &tp->t_items, lid_trans)
1475e98c414fSChristoph Hellwig 			ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
14761da177e4SLinus Torvalds 	}
14771da177e4SLinus Torvalds #endif
14781da177e4SLinus Torvalds 	xfs_trans_unreserve_and_mod_sb(tp);
14797d095257SChristoph Hellwig 	xfs_trans_unreserve_and_mod_dquots(tp);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	if (tp->t_ticket) {
14821da177e4SLinus Torvalds 		if (flags & XFS_TRANS_RELEASE_LOG_RES) {
14831da177e4SLinus Torvalds 			ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
14841da177e4SLinus Torvalds 			log_flags = XFS_LOG_REL_PERM_RESERV;
14851da177e4SLinus Torvalds 		} else {
14861da177e4SLinus Torvalds 			log_flags = 0;
14871da177e4SLinus Torvalds 		}
14880733af21SRyan Hankins 		xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	/* mark this thread as no longer being in a transaction */
149259c1b082SNathan Scott 	current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
14931da177e4SLinus Torvalds 
149471e330b5SDave Chinner 	xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
14951da177e4SLinus Torvalds 	xfs_trans_free(tp);
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
1498322ff6b8SNiv Sardi /*
1499322ff6b8SNiv Sardi  * Roll from one trans in the sequence of PERMANENT transactions to
1500322ff6b8SNiv Sardi  * the next: permanent transactions are only flushed out when
1501322ff6b8SNiv Sardi  * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1502322ff6b8SNiv Sardi  * as possible to let chunks of it go to the log. So we commit the
1503322ff6b8SNiv Sardi  * chunk we've been working on and get a new transaction to continue.
1504322ff6b8SNiv Sardi  */
1505322ff6b8SNiv Sardi int
1506322ff6b8SNiv Sardi xfs_trans_roll(
1507322ff6b8SNiv Sardi 	struct xfs_trans	**tpp,
1508322ff6b8SNiv Sardi 	struct xfs_inode	*dp)
1509322ff6b8SNiv Sardi {
1510322ff6b8SNiv Sardi 	struct xfs_trans	*trans;
1511322ff6b8SNiv Sardi 	unsigned int		logres, count;
1512322ff6b8SNiv Sardi 	int			error;
1513322ff6b8SNiv Sardi 
1514322ff6b8SNiv Sardi 	/*
1515322ff6b8SNiv Sardi 	 * Ensure that the inode is always logged.
1516322ff6b8SNiv Sardi 	 */
1517322ff6b8SNiv Sardi 	trans = *tpp;
1518322ff6b8SNiv Sardi 	xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1519322ff6b8SNiv Sardi 
1520322ff6b8SNiv Sardi 	/*
1521322ff6b8SNiv Sardi 	 * Copy the critical parameters from one trans to the next.
1522322ff6b8SNiv Sardi 	 */
1523322ff6b8SNiv Sardi 	logres = trans->t_log_res;
1524322ff6b8SNiv Sardi 	count = trans->t_log_count;
1525322ff6b8SNiv Sardi 	*tpp = xfs_trans_dup(trans);
1526322ff6b8SNiv Sardi 
1527322ff6b8SNiv Sardi 	/*
1528322ff6b8SNiv Sardi 	 * Commit the current transaction.
1529322ff6b8SNiv Sardi 	 * If this commit failed, then it'd just unlock those items that
1530322ff6b8SNiv Sardi 	 * are not marked ihold. That also means that a filesystem shutdown
1531322ff6b8SNiv Sardi 	 * is in progress. The caller takes the responsibility to cancel
1532322ff6b8SNiv Sardi 	 * the duplicate transaction that gets returned.
1533322ff6b8SNiv Sardi 	 */
1534322ff6b8SNiv Sardi 	error = xfs_trans_commit(trans, 0);
1535322ff6b8SNiv Sardi 	if (error)
1536322ff6b8SNiv Sardi 		return (error);
1537322ff6b8SNiv Sardi 
1538322ff6b8SNiv Sardi 	trans = *tpp;
1539322ff6b8SNiv Sardi 
1540322ff6b8SNiv Sardi 	/*
1541cc09c0dcSDave Chinner 	 * transaction commit worked ok so we can drop the extra ticket
1542cc09c0dcSDave Chinner 	 * reference that we gained in xfs_trans_dup()
1543cc09c0dcSDave Chinner 	 */
1544cc09c0dcSDave Chinner 	xfs_log_ticket_put(trans->t_ticket);
1545cc09c0dcSDave Chinner 
1546cc09c0dcSDave Chinner 
1547cc09c0dcSDave Chinner 	/*
1548322ff6b8SNiv Sardi 	 * Reserve space in the log for th next transaction.
1549322ff6b8SNiv Sardi 	 * This also pushes items in the "AIL", the list of logged items,
1550322ff6b8SNiv Sardi 	 * out to disk if they are taking up space at the tail of the log
1551322ff6b8SNiv Sardi 	 * that we want to use.  This requires that either nothing be locked
1552322ff6b8SNiv Sardi 	 * across this call, or that anything that is locked be logged in
1553322ff6b8SNiv Sardi 	 * the prior and the next transactions.
1554322ff6b8SNiv Sardi 	 */
1555322ff6b8SNiv Sardi 	error = xfs_trans_reserve(trans, 0, logres, 0,
1556322ff6b8SNiv Sardi 				  XFS_TRANS_PERM_LOG_RES, count);
1557322ff6b8SNiv Sardi 	/*
1558322ff6b8SNiv Sardi 	 *  Ensure that the inode is in the new transaction and locked.
1559322ff6b8SNiv Sardi 	 */
1560322ff6b8SNiv Sardi 	if (error)
1561322ff6b8SNiv Sardi 		return error;
1562322ff6b8SNiv Sardi 
1563ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(trans, dp, 0);
1564322ff6b8SNiv Sardi 	return 0;
1565322ff6b8SNiv Sardi }
1566