blob: 6c601ea77a9eb9c06a1e441727179bf51c6c67e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
Christoph Hellwige98c4142010-06-23 18:11:15 +10003 * Copyright (C) 2010 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
27#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_dinode.h"
33#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_btree.h"
35#include "xfs_ialloc.h"
36#include "xfs_alloc.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000037#include "xfs_extent_busy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_quota.h"
Jeff Liu4f3b5782013-01-28 21:25:35 +080040#include "xfs_qm.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_trans_space.h"
Niv Sardi322ff6b2008-08-13 16:05:49 +100043#include "xfs_inode_item.h"
Jeff Liu4f3b5782013-01-28 21:25:35 +080044#include "xfs_log_priv.h"
45#include "xfs_buf_item.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100046#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nathan Scott8f794052006-03-14 13:32:41 +110048kmem_zone_t *xfs_trans_zone;
Christoph Hellwige98c4142010-06-23 18:11:15 +100049kmem_zone_t *xfs_log_item_desc_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jeff Liu4f3b5782013-01-28 21:25:35 +080051/*
52 * A buffer has a format structure overhead in the log in addition
53 * to the data, so we need to take this into account when reserving
54 * space in a transaction for a buffer. Round the space required up
55 * to a multiple of 128 bytes so that we don't change the historical
56 * reservation that has been used for this overhead.
57 */
58STATIC uint
59xfs_buf_log_overhead(void)
60{
61 return round_up(sizeof(struct xlog_op_header) +
62 sizeof(struct xfs_buf_log_format), 128);
63}
64
65/*
66 * Calculate out transaction log reservation per item in bytes.
67 *
68 * The nbufs argument is used to indicate the number of items that
69 * will be changed in a transaction. size is used to tell how many
70 * bytes should be reserved per item.
71 */
72STATIC uint
73xfs_calc_buf_res(
74 uint nbufs,
75 uint size)
76{
77 return nbufs * (size + xfs_buf_log_overhead());
78}
Christoph Hellwig025101d2010-05-04 13:53:48 +000079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Christoph Hellwig025101d2010-05-04 13:53:48 +000081 * Various log reservation values.
82 *
83 * These are based on the size of the file system block because that is what
84 * most transactions manipulate. Each adds in an additional 128 bytes per
85 * item logged to try to account for the overhead of the transaction mechanism.
86 *
87 * Note: Most of the reservations underestimate the number of allocation
88 * groups into which they could free extents in the xfs_bmap_finish() call.
89 * This is because the number in the worst case is quite high and quite
90 * unusual. In order to fix this we need to change xfs_bmap_finish() to free
91 * extents in only a single AG at a time. This will require changes to the
92 * EFI code as well, however, so that the EFI for the extents not freed is
93 * logged again in each transaction. See SGI PV #261917.
94 *
95 * Reservation functions here avoid a huge stack in xfs_trans_init due to
96 * register overflow from temporaries in the calculations.
97 */
98
99
100/*
101 * In a write transaction we can allocate a maximum of 2
102 * extents. This gives:
103 * the inode getting the new extents: inode size
104 * the inode's bmap btree: max depth * block size
105 * the agfs of the ags from which the extents are allocated: 2 * sector
106 * the superblock free block counter: sector size
107 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
108 * And the bmap_finish transaction can free bmap blocks in a join:
109 * the agfs of the ags containing the blocks: 2 * sector size
110 * the agfls of the ags containing the blocks: 2 * sector size
111 * the super block free block counter: sector size
112 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
Nathan Scott8f794052006-03-14 13:32:41 +1100113 */
Nathan Scott8f794052006-03-14 13:32:41 +1100114STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000115xfs_calc_write_reservation(
116 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100117{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000118 return XFS_DQUOT_LOGRES(mp) +
119 MAX((mp->m_sb.sb_inodesize +
120 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
121 2 * mp->m_sb.sb_sectsize +
122 mp->m_sb.sb_sectsize +
123 XFS_ALLOCFREE_LOG_RES(mp, 2) +
124 128 * (4 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
125 XFS_ALLOCFREE_LOG_COUNT(mp, 2))),
126 (2 * mp->m_sb.sb_sectsize +
127 2 * mp->m_sb.sb_sectsize +
128 mp->m_sb.sb_sectsize +
129 XFS_ALLOCFREE_LOG_RES(mp, 2) +
130 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
Nathan Scott8f794052006-03-14 13:32:41 +1100131}
132
Christoph Hellwig025101d2010-05-04 13:53:48 +0000133/*
134 * In truncating a file we free up to two extents at once. We can modify:
135 * the inode being truncated: inode size
136 * the inode's bmap btree: (max depth + 1) * block size
137 * And the bmap_finish transaction can free the blocks and bmap blocks:
138 * the agf for each of the ags: 4 * sector size
139 * the agfl for each of the ags: 4 * sector size
140 * the super block to reflect the freed blocks: sector size
141 * worst case split in allocation btrees per extent assuming 4 extents:
142 * 4 exts * 2 trees * (2 * max depth - 1) * block size
143 * the inode btree: max depth * blocksize
144 * the allocation btrees: 2 trees * (max depth - 1) * block size
145 */
Nathan Scott8f794052006-03-14 13:32:41 +1100146STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000147xfs_calc_itruncate_reservation(
148 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100149{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000150 return XFS_DQUOT_LOGRES(mp) +
151 MAX((mp->m_sb.sb_inodesize +
152 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1) +
153 128 * (2 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
154 (4 * mp->m_sb.sb_sectsize +
155 4 * mp->m_sb.sb_sectsize +
156 mp->m_sb.sb_sectsize +
157 XFS_ALLOCFREE_LOG_RES(mp, 4) +
158 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4)) +
159 128 * 5 +
160 XFS_ALLOCFREE_LOG_RES(mp, 1) +
161 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
162 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100163}
164
Christoph Hellwig025101d2010-05-04 13:53:48 +0000165/*
166 * In renaming a files we can modify:
167 * the four inodes involved: 4 * inode size
168 * the two directory btrees: 2 * (max depth + v2) * dir block size
169 * the two directory bmap btrees: 2 * max depth * block size
170 * And the bmap_finish transaction can free dir and bmap blocks (two sets
171 * of bmap blocks) giving:
172 * the agf for the ags in which the blocks live: 3 * sector size
173 * the agfl for the ags in which the blocks live: 3 * sector size
174 * the superblock for the free block count: sector size
175 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
176 */
Nathan Scott8f794052006-03-14 13:32:41 +1100177STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000178xfs_calc_rename_reservation(
179 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100180{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000181 return XFS_DQUOT_LOGRES(mp) +
182 MAX((4 * mp->m_sb.sb_inodesize +
183 2 * XFS_DIROP_LOG_RES(mp) +
184 128 * (4 + 2 * XFS_DIROP_LOG_COUNT(mp))),
185 (3 * mp->m_sb.sb_sectsize +
186 3 * mp->m_sb.sb_sectsize +
187 mp->m_sb.sb_sectsize +
188 XFS_ALLOCFREE_LOG_RES(mp, 3) +
189 128 * (7 + XFS_ALLOCFREE_LOG_COUNT(mp, 3))));
Nathan Scott8f794052006-03-14 13:32:41 +1100190}
191
Christoph Hellwig025101d2010-05-04 13:53:48 +0000192/*
193 * For creating a link to an inode:
194 * the parent directory inode: inode size
195 * the linked inode: inode size
196 * the directory btree could split: (max depth + v2) * dir block size
197 * the directory bmap btree could join or split: (max depth + v2) * blocksize
198 * And the bmap_finish transaction can free some bmap blocks giving:
199 * the agf for the ag in which the blocks live: sector size
200 * the agfl for the ag in which the blocks live: sector size
201 * the superblock for the free block count: sector size
202 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
203 */
Nathan Scott8f794052006-03-14 13:32:41 +1100204STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000205xfs_calc_link_reservation(
206 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100207{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000208 return XFS_DQUOT_LOGRES(mp) +
209 MAX((mp->m_sb.sb_inodesize +
210 mp->m_sb.sb_inodesize +
211 XFS_DIROP_LOG_RES(mp) +
212 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
213 (mp->m_sb.sb_sectsize +
214 mp->m_sb.sb_sectsize +
215 mp->m_sb.sb_sectsize +
216 XFS_ALLOCFREE_LOG_RES(mp, 1) +
217 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100218}
219
Christoph Hellwig025101d2010-05-04 13:53:48 +0000220/*
221 * For removing a directory entry we can modify:
222 * the parent directory inode: inode size
223 * the removed inode: inode size
224 * the directory btree could join: (max depth + v2) * dir block size
225 * the directory bmap btree could join or split: (max depth + v2) * blocksize
226 * And the bmap_finish transaction can free the dir and bmap blocks giving:
227 * the agf for the ag in which the blocks live: 2 * sector size
228 * the agfl for the ag in which the blocks live: 2 * sector size
229 * the superblock for the free block count: sector size
230 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
231 */
Nathan Scott8f794052006-03-14 13:32:41 +1100232STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000233xfs_calc_remove_reservation(
234 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100235{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000236 return XFS_DQUOT_LOGRES(mp) +
237 MAX((mp->m_sb.sb_inodesize +
238 mp->m_sb.sb_inodesize +
239 XFS_DIROP_LOG_RES(mp) +
240 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
241 (2 * mp->m_sb.sb_sectsize +
242 2 * mp->m_sb.sb_sectsize +
243 mp->m_sb.sb_sectsize +
244 XFS_ALLOCFREE_LOG_RES(mp, 2) +
245 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
Nathan Scott8f794052006-03-14 13:32:41 +1100246}
247
Christoph Hellwig025101d2010-05-04 13:53:48 +0000248/*
249 * For symlink we can modify:
250 * the parent directory inode: inode size
251 * the new inode: inode size
252 * the inode btree entry: 1 block
253 * the directory btree: (max depth + v2) * dir block size
254 * the directory inode's bmap btree: (max depth + v2) * block size
255 * the blocks for the symlink: 1 kB
256 * Or in the first xact we allocate some inodes giving:
257 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
258 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
259 * the inode btree: max depth * blocksize
260 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
261 */
Nathan Scott8f794052006-03-14 13:32:41 +1100262STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000263xfs_calc_symlink_reservation(
264 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100265{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000266 return XFS_DQUOT_LOGRES(mp) +
267 MAX((mp->m_sb.sb_inodesize +
268 mp->m_sb.sb_inodesize +
269 XFS_FSB_TO_B(mp, 1) +
270 XFS_DIROP_LOG_RES(mp) +
271 1024 +
272 128 * (4 + XFS_DIROP_LOG_COUNT(mp))),
273 (2 * mp->m_sb.sb_sectsize +
274 XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
275 XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
276 XFS_ALLOCFREE_LOG_RES(mp, 1) +
277 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
278 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100279}
280
Christoph Hellwig025101d2010-05-04 13:53:48 +0000281/*
282 * For create we can modify:
283 * the parent directory inode: inode size
284 * the new inode: inode size
285 * the inode btree entry: block size
286 * the superblock for the nlink flag: sector size
287 * the directory btree: (max depth + v2) * dir block size
288 * the directory inode's bmap btree: (max depth + v2) * block size
289 * Or in the first xact we allocate some inodes giving:
290 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
291 * the superblock for the nlink flag: sector size
292 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
293 * the inode btree: max depth * blocksize
294 * the allocation btrees: 2 trees * (max depth - 1) * block size
295 */
Nathan Scott8f794052006-03-14 13:32:41 +1100296STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000297xfs_calc_create_reservation(
298 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100299{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000300 return XFS_DQUOT_LOGRES(mp) +
301 MAX((mp->m_sb.sb_inodesize +
302 mp->m_sb.sb_inodesize +
303 mp->m_sb.sb_sectsize +
304 XFS_FSB_TO_B(mp, 1) +
305 XFS_DIROP_LOG_RES(mp) +
306 128 * (3 + XFS_DIROP_LOG_COUNT(mp))),
307 (3 * mp->m_sb.sb_sectsize +
308 XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
309 XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
310 XFS_ALLOCFREE_LOG_RES(mp, 1) +
311 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
312 XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100313}
314
Christoph Hellwig025101d2010-05-04 13:53:48 +0000315/*
316 * Making a new directory is the same as creating a new file.
317 */
Nathan Scott8f794052006-03-14 13:32:41 +1100318STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000319xfs_calc_mkdir_reservation(
320 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100321{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000322 return xfs_calc_create_reservation(mp);
Nathan Scott8f794052006-03-14 13:32:41 +1100323}
324
Christoph Hellwig025101d2010-05-04 13:53:48 +0000325/*
326 * In freeing an inode we can modify:
327 * the inode being freed: inode size
328 * the super block free inode counter: sector size
329 * the agi hash list and counters: sector size
330 * the inode btree entry: block size
331 * the on disk inode before ours in the agi hash list: inode cluster size
332 * the inode btree: max depth * blocksize
333 * the allocation btrees: 2 trees * (max depth - 1) * block size
334 */
Nathan Scott8f794052006-03-14 13:32:41 +1100335STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000336xfs_calc_ifree_reservation(
337 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100338{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000339 return XFS_DQUOT_LOGRES(mp) +
340 mp->m_sb.sb_inodesize +
341 mp->m_sb.sb_sectsize +
342 mp->m_sb.sb_sectsize +
343 XFS_FSB_TO_B(mp, 1) +
344 MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
345 XFS_INODE_CLUSTER_SIZE(mp)) +
346 128 * 5 +
347 XFS_ALLOCFREE_LOG_RES(mp, 1) +
348 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
349 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100350}
351
Christoph Hellwig025101d2010-05-04 13:53:48 +0000352/*
353 * When only changing the inode we log the inode and possibly the superblock
354 * We also add a bit of slop for the transaction stuff.
355 */
Nathan Scott8f794052006-03-14 13:32:41 +1100356STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000357xfs_calc_ichange_reservation(
358 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100359{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000360 return XFS_DQUOT_LOGRES(mp) +
361 mp->m_sb.sb_inodesize +
362 mp->m_sb.sb_sectsize +
363 512;
364
Nathan Scott8f794052006-03-14 13:32:41 +1100365}
366
Christoph Hellwig025101d2010-05-04 13:53:48 +0000367/*
368 * Growing the data section of the filesystem.
369 * superblock
370 * agi and agf
371 * allocation btrees
372 */
Nathan Scott8f794052006-03-14 13:32:41 +1100373STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000374xfs_calc_growdata_reservation(
375 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100376{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000377 return mp->m_sb.sb_sectsize * 3 +
378 XFS_ALLOCFREE_LOG_RES(mp, 1) +
379 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100380}
381
Christoph Hellwig025101d2010-05-04 13:53:48 +0000382/*
383 * Growing the rt section of the filesystem.
384 * In the first set of transactions (ALLOC) we allocate space to the
385 * bitmap or summary files.
386 * superblock: sector size
387 * agf of the ag from which the extent is allocated: sector size
388 * bmap btree for bitmap/summary inode: max depth * blocksize
389 * bitmap/summary inode: inode size
390 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
391 */
Nathan Scott8f794052006-03-14 13:32:41 +1100392STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000393xfs_calc_growrtalloc_reservation(
394 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100395{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000396 return 2 * mp->m_sb.sb_sectsize +
397 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
398 mp->m_sb.sb_inodesize +
399 XFS_ALLOCFREE_LOG_RES(mp, 1) +
400 128 * (3 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
401 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100402}
403
Christoph Hellwig025101d2010-05-04 13:53:48 +0000404/*
405 * Growing the rt section of the filesystem.
406 * In the second set of transactions (ZERO) we zero the new metadata blocks.
407 * one bitmap/summary block: blocksize
408 */
Nathan Scott8f794052006-03-14 13:32:41 +1100409STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000410xfs_calc_growrtzero_reservation(
411 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100412{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000413 return mp->m_sb.sb_blocksize + 128;
Nathan Scott8f794052006-03-14 13:32:41 +1100414}
415
Christoph Hellwig025101d2010-05-04 13:53:48 +0000416/*
417 * Growing the rt section of the filesystem.
418 * In the third set of transactions (FREE) we update metadata without
419 * allocating any new blocks.
420 * superblock: sector size
421 * bitmap inode: inode size
422 * summary inode: inode size
423 * one bitmap block: blocksize
424 * summary blocks: new summary size
425 */
Nathan Scott8f794052006-03-14 13:32:41 +1100426STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000427xfs_calc_growrtfree_reservation(
428 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100429{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000430 return mp->m_sb.sb_sectsize +
431 2 * mp->m_sb.sb_inodesize +
432 mp->m_sb.sb_blocksize +
433 mp->m_rsumsize +
434 128 * 5;
Nathan Scott8f794052006-03-14 13:32:41 +1100435}
436
Christoph Hellwig025101d2010-05-04 13:53:48 +0000437/*
438 * Logging the inode modification timestamp on a synchronous write.
439 * inode
440 */
Nathan Scott8f794052006-03-14 13:32:41 +1100441STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000442xfs_calc_swrite_reservation(
443 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100444{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000445 return mp->m_sb.sb_inodesize + 128;
Nathan Scott8f794052006-03-14 13:32:41 +1100446}
447
Christoph Hellwig025101d2010-05-04 13:53:48 +0000448/*
449 * Logging the inode mode bits when writing a setuid/setgid file
450 * inode
451 */
Nathan Scott8f794052006-03-14 13:32:41 +1100452STATIC uint
453xfs_calc_writeid_reservation(xfs_mount_t *mp)
454{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000455 return mp->m_sb.sb_inodesize + 128;
Nathan Scott8f794052006-03-14 13:32:41 +1100456}
457
Christoph Hellwig025101d2010-05-04 13:53:48 +0000458/*
459 * Converting the inode from non-attributed to attributed.
460 * the inode being converted: inode size
461 * agf block and superblock (for block allocation)
462 * the new block (directory sized)
463 * bmap blocks for the new directory block
464 * allocation btrees
465 */
Nathan Scott8f794052006-03-14 13:32:41 +1100466STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000467xfs_calc_addafork_reservation(
468 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100469{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000470 return XFS_DQUOT_LOGRES(mp) +
471 mp->m_sb.sb_inodesize +
472 mp->m_sb.sb_sectsize * 2 +
473 mp->m_dirblksize +
474 XFS_FSB_TO_B(mp, XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1) +
475 XFS_ALLOCFREE_LOG_RES(mp, 1) +
476 128 * (4 + XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1 +
477 XFS_ALLOCFREE_LOG_COUNT(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100478}
479
Christoph Hellwig025101d2010-05-04 13:53:48 +0000480/*
481 * Removing the attribute fork of a file
482 * the inode being truncated: inode size
483 * the inode's bmap btree: max depth * block size
484 * And the bmap_finish transaction can free the blocks and bmap blocks:
485 * the agf for each of the ags: 4 * sector size
486 * the agfl for each of the ags: 4 * sector size
487 * the super block to reflect the freed blocks: sector size
488 * worst case split in allocation btrees per extent assuming 4 extents:
489 * 4 exts * 2 trees * (2 * max depth - 1) * block size
490 */
Nathan Scott8f794052006-03-14 13:32:41 +1100491STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000492xfs_calc_attrinval_reservation(
493 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100494{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000495 return MAX((mp->m_sb.sb_inodesize +
496 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
497 128 * (1 + XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK))),
498 (4 * mp->m_sb.sb_sectsize +
499 4 * mp->m_sb.sb_sectsize +
500 mp->m_sb.sb_sectsize +
501 XFS_ALLOCFREE_LOG_RES(mp, 4) +
502 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4))));
Nathan Scott8f794052006-03-14 13:32:41 +1100503}
504
Christoph Hellwig025101d2010-05-04 13:53:48 +0000505/*
506 * Setting an attribute.
507 * the inode getting the attribute
508 * the superblock for allocations
509 * the agfs extents are allocated from
510 * the attribute btree * max depth
511 * the inode allocation btree
512 * Since attribute transaction space is dependent on the size of the attribute,
513 * the calculation is done partially at mount time and partially at runtime.
514 */
Nathan Scott8f794052006-03-14 13:32:41 +1100515STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000516xfs_calc_attrset_reservation(
517 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100518{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000519 return XFS_DQUOT_LOGRES(mp) +
520 mp->m_sb.sb_inodesize +
521 mp->m_sb.sb_sectsize +
522 XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
523 128 * (2 + XFS_DA_NODE_MAXDEPTH);
Nathan Scott8f794052006-03-14 13:32:41 +1100524}
525
Christoph Hellwig025101d2010-05-04 13:53:48 +0000526/*
527 * Removing an attribute.
528 * the inode: inode size
529 * the attribute btree could join: max depth * block size
530 * the inode bmap btree could join or split: max depth * block size
531 * And the bmap_finish transaction can free the attr blocks freed giving:
532 * the agf for the ag in which the blocks live: 2 * sector size
533 * the agfl for the ag in which the blocks live: 2 * sector size
534 * the superblock for the free block count: sector size
535 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
536 */
Nathan Scott8f794052006-03-14 13:32:41 +1100537STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000538xfs_calc_attrrm_reservation(
539 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100540{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000541 return XFS_DQUOT_LOGRES(mp) +
542 MAX((mp->m_sb.sb_inodesize +
543 XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
544 XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
545 128 * (1 + XFS_DA_NODE_MAXDEPTH +
546 XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
547 (2 * mp->m_sb.sb_sectsize +
548 2 * mp->m_sb.sb_sectsize +
549 mp->m_sb.sb_sectsize +
550 XFS_ALLOCFREE_LOG_RES(mp, 2) +
551 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
Nathan Scott8f794052006-03-14 13:32:41 +1100552}
553
Christoph Hellwig025101d2010-05-04 13:53:48 +0000554/*
555 * Clearing a bad agino number in an agi hash bucket.
556 */
Nathan Scott8f794052006-03-14 13:32:41 +1100557STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000558xfs_calc_clear_agi_bucket_reservation(
559 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100560{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000561 return mp->m_sb.sb_sectsize + 128;
Nathan Scott8f794052006-03-14 13:32:41 +1100562}
563
564/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * Initialize the precomputed transaction reservation values
566 * in the mount structure.
567 */
568void
569xfs_trans_init(
Christoph Hellwig025101d2010-05-04 13:53:48 +0000570 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000572 struct xfs_trans_reservations *resp = &mp->m_reservations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Nathan Scott8f794052006-03-14 13:32:41 +1100574 resp->tr_write = xfs_calc_write_reservation(mp);
575 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
576 resp->tr_rename = xfs_calc_rename_reservation(mp);
577 resp->tr_link = xfs_calc_link_reservation(mp);
578 resp->tr_remove = xfs_calc_remove_reservation(mp);
579 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
580 resp->tr_create = xfs_calc_create_reservation(mp);
581 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
582 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
583 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
584 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
585 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
586 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
587 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
588 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
589 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
590 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
591 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
592 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
593 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
594 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * This routine is called to allocate a transaction structure.
599 * The type parameter indicates the type of the transaction. These
600 * are enumerated in xfs_trans.h.
Alex Elderb2ce3972011-07-11 09:51:44 -0500601 *
602 * Dynamically allocate the transaction structure from the transaction
603 * zone, initialize it, and return it to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Alex Elderb2ce3972011-07-11 09:51:44 -0500605xfs_trans_t *
606xfs_trans_alloc(
607 xfs_mount_t *mp,
608 uint type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Jan Karad9457dc2012-06-12 16:20:39 +0200610 xfs_trans_t *tp;
611
612 sb_start_intwrite(mp->m_super);
613 tp = _xfs_trans_alloc(mp, type, KM_SLEEP);
614 tp->t_flags |= XFS_TRANS_FREEZE_PROT;
615 return tp;
Alex Elderb2ce3972011-07-11 09:51:44 -0500616}
617
618xfs_trans_t *
619_xfs_trans_alloc(
620 xfs_mount_t *mp,
621 uint type,
Al Viro77ba7872012-04-02 06:24:04 -0400622 xfs_km_flags_t memflags)
Alex Elderb2ce3972011-07-11 09:51:44 -0500623{
624 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Jan Karad9457dc2012-06-12 16:20:39 +0200626 WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
Nathan Scott34327e12006-06-09 17:11:55 +1000627 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000629 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 tp->t_magic = XFS_TRANS_MAGIC;
631 tp->t_type = type;
632 tp->t_mountp = mp;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000633 INIT_LIST_HEAD(&tp->t_items);
Dave Chinnered3b4d62010-05-21 12:07:08 +1000634 INIT_LIST_HEAD(&tp->t_busy);
Nathan Scott34327e12006-06-09 17:11:55 +1000635 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100639 * Free the transaction structure. If there is more clean up
640 * to do when the structure is freed, add it here.
641 */
642STATIC void
643xfs_trans_free(
Dave Chinnered3b4d62010-05-21 12:07:08 +1000644 struct xfs_trans *tp)
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100645{
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000646 xfs_extent_busy_sort(&tp->t_busy);
647 xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
Dave Chinnered3b4d62010-05-21 12:07:08 +1000648
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100649 atomic_dec(&tp->t_mountp->m_active_trans);
Jan Karad9457dc2012-06-12 16:20:39 +0200650 if (tp->t_flags & XFS_TRANS_FREEZE_PROT)
651 sb_end_intwrite(tp->t_mountp->m_super);
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100652 xfs_trans_free_dqinfo(tp);
653 kmem_zone_free(xfs_trans_zone, tp);
654}
655
656/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * This is called to create a new transaction which will share the
658 * permanent log reservation of the given transaction. The remaining
659 * unused block and rt extent reservations are also inherited. This
660 * implies that the original transaction is no longer allowed to allocate
661 * blocks. Locks and log items, however, are no inherited. They must
662 * be added to the new transaction explicitly.
663 */
664xfs_trans_t *
665xfs_trans_dup(
666 xfs_trans_t *tp)
667{
668 xfs_trans_t *ntp;
669
670 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
671
672 /*
673 * Initialize the new transaction structure.
674 */
675 ntp->t_magic = XFS_TRANS_MAGIC;
676 ntp->t_type = tp->t_type;
677 ntp->t_mountp = tp->t_mountp;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000678 INIT_LIST_HEAD(&ntp->t_items);
Dave Chinnered3b4d62010-05-21 12:07:08 +1000679 INIT_LIST_HEAD(&ntp->t_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100683
Jan Karad9457dc2012-06-12 16:20:39 +0200684 ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
685 (tp->t_flags & XFS_TRANS_RESERVE) |
686 (tp->t_flags & XFS_TRANS_FREEZE_PROT);
687 /* We gave our writer reference to the new transaction */
688 tp->t_flags &= ~XFS_TRANS_FREEZE_PROT;
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100689 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
691 tp->t_blk_res = tp->t_blk_res_used;
692 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
693 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000694 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Christoph Hellwig7d095252009-06-08 15:33:32 +0200696 xfs_trans_dup_dqinfo(tp, ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 atomic_inc(&tp->t_mountp->m_active_trans);
699 return ntp;
700}
701
702/*
703 * This is called to reserve free disk blocks and log space for the
704 * given transaction. This must be done before allocating any resources
705 * within the transaction.
706 *
707 * This will return ENOSPC if there are not enough blocks available.
708 * It will sleep waiting for available log space.
709 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
710 * is used by long running transactions. If any one of the reservations
711 * fails then they will all be backed out.
712 *
713 * This does not do quota reservations. That typically is done by the
714 * caller afterwards.
715 */
716int
717xfs_trans_reserve(
718 xfs_trans_t *tp,
719 uint blocks,
720 uint logspace,
721 uint rtextents,
722 uint flags,
723 uint logcount)
724{
Nathan Scott59c1b082006-06-09 14:59:13 +1000725 int error = 0;
726 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000729 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /*
732 * Attempt to reserve the needed disk blocks by decrementing
733 * the number needed from the number available. This will
734 * fail if the count would go below zero.
735 */
736 if (blocks > 0) {
Christoph Hellwig96540c72010-09-30 02:25:55 +0000737 error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100738 -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000740 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return (XFS_ERROR(ENOSPC));
742 }
743 tp->t_blk_res += blocks;
744 }
745
746 /*
747 * Reserve the log space needed for this transaction.
748 */
749 if (logspace > 0) {
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000750 bool permanent = false;
751
752 ASSERT(tp->t_log_res == 0 || tp->t_log_res == logspace);
753 ASSERT(tp->t_log_count == 0 || tp->t_log_count == logcount);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (flags & XFS_TRANS_PERM_LOG_RES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000757 permanent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 } else {
759 ASSERT(tp->t_ticket == NULL);
760 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000763 if (tp->t_ticket != NULL) {
764 ASSERT(flags & XFS_TRANS_PERM_LOG_RES);
765 error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
766 } else {
767 error = xfs_log_reserve(tp->t_mountp, logspace,
768 logcount, &tp->t_ticket,
769 XFS_TRANSACTION, permanent,
770 tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000772
773 if (error)
774 goto undo_blocks;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 tp->t_log_res = logspace;
777 tp->t_log_count = logcount;
778 }
779
780 /*
781 * Attempt to reserve the needed realtime extents by decrementing
782 * the number needed from the number available. This will
783 * fail if the count would go below zero.
784 */
785 if (rtextents > 0) {
786 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100787 -((int64_t)rtextents), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (error) {
789 error = XFS_ERROR(ENOSPC);
790 goto undo_log;
791 }
792 tp->t_rtx_res += rtextents;
793 }
794
795 return 0;
796
797 /*
798 * Error cases jump to one of these labels to undo any
799 * reservations which have already been performed.
800 */
801undo_log:
802 if (logspace > 0) {
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000803 int log_flags;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (flags & XFS_TRANS_PERM_LOG_RES) {
806 log_flags = XFS_LOG_REL_PERM_RESERV;
807 } else {
808 log_flags = 0;
809 }
810 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
811 tp->t_ticket = NULL;
812 tp->t_log_res = 0;
813 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
814 }
815
816undo_blocks:
817 if (blocks > 0) {
Christoph Hellwig96540c72010-09-30 02:25:55 +0000818 xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100819 (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 tp->t_blk_res = 0;
821 }
822
Nathan Scott59c1b082006-06-09 14:59:13 +1000823 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Nathan Scott59c1b082006-06-09 14:59:13 +1000825 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * Record the indicated change to the given field for application
830 * to the file system's superblock when the transaction commits.
831 * For now, just store the change in the transaction structure.
832 *
833 * Mark the transaction structure to indicate that the superblock
834 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000835 *
836 * Because we may not be keeping track of allocated/free inodes and
837 * used filesystem blocks in the superblock, we do not mark the
838 * superblock dirty in this transaction if we modify these fields.
839 * We still need to update the transaction deltas so that they get
840 * applied to the incore superblock, but we don't want them to
841 * cause the superblock to get locked and logged if these are the
842 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844void
845xfs_trans_mod_sb(
846 xfs_trans_t *tp,
847 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100848 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
David Chinner92821e22007-05-24 15:26:31 +1000850 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
851 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 switch (field) {
854 case XFS_TRANS_SB_ICOUNT:
855 tp->t_icount_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000856 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
857 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 case XFS_TRANS_SB_IFREE:
860 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000861 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
862 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
864 case XFS_TRANS_SB_FDBLOCKS:
865 /*
866 * Track the number of blocks allocated in the
867 * transaction. Make sure it does not exceed the
868 * number reserved.
869 */
870 if (delta < 0) {
871 tp->t_blk_res_used += (uint)-delta;
872 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
873 }
874 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000875 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
876 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 case XFS_TRANS_SB_RES_FDBLOCKS:
879 /*
880 * The allocation has already been applied to the
881 * in-core superblock's counter. This should only
882 * be applied to the on-disk superblock.
883 */
884 ASSERT(delta < 0);
885 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000886 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
887 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
889 case XFS_TRANS_SB_FREXTENTS:
890 /*
891 * Track the number of blocks allocated in the
892 * transaction. Make sure it does not exceed the
893 * number reserved.
894 */
895 if (delta < 0) {
896 tp->t_rtx_res_used += (uint)-delta;
897 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
898 }
899 tp->t_frextents_delta += delta;
900 break;
901 case XFS_TRANS_SB_RES_FREXTENTS:
902 /*
903 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000904 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * be applied to the on-disk superblock.
906 */
907 ASSERT(delta < 0);
908 tp->t_res_frextents_delta += delta;
909 break;
910 case XFS_TRANS_SB_DBLOCKS:
911 ASSERT(delta > 0);
912 tp->t_dblocks_delta += delta;
913 break;
914 case XFS_TRANS_SB_AGCOUNT:
915 ASSERT(delta > 0);
916 tp->t_agcount_delta += delta;
917 break;
918 case XFS_TRANS_SB_IMAXPCT:
919 tp->t_imaxpct_delta += delta;
920 break;
921 case XFS_TRANS_SB_REXTSIZE:
922 tp->t_rextsize_delta += delta;
923 break;
924 case XFS_TRANS_SB_RBMBLOCKS:
925 tp->t_rbmblocks_delta += delta;
926 break;
927 case XFS_TRANS_SB_RBLOCKS:
928 tp->t_rblocks_delta += delta;
929 break;
930 case XFS_TRANS_SB_REXTENTS:
931 tp->t_rextents_delta += delta;
932 break;
933 case XFS_TRANS_SB_REXTSLOG:
934 tp->t_rextslog_delta += delta;
935 break;
936 default:
937 ASSERT(0);
938 return;
939 }
940
David Chinner210c6f12007-05-24 15:26:51 +1000941 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
945 * xfs_trans_apply_sb_deltas() is called from the commit code
946 * to bring the superblock buffer into the current transaction
947 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
948 *
949 * For now we just look at each field allowed to change and change
950 * it if necessary.
951 */
952STATIC void
953xfs_trans_apply_sb_deltas(
954 xfs_trans_t *tp)
955{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000956 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 xfs_buf_t *bp;
958 int whole = 0;
959
960 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
961 sbp = XFS_BUF_TO_SBP(bp);
962
963 /*
964 * Check that superblock mods match the mods made to AGF counters.
965 */
966 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
967 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
968 tp->t_ag_btree_delta));
969
David Chinner92821e22007-05-24 15:26:31 +1000970 /*
971 * Only update the superblock counters if we are logging them
972 */
973 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000974 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800975 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000976 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800977 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000978 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800979 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000980 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800981 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000984 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800985 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000986 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800987 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000988
989 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800990 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 whole = 1;
992 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000993 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800994 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 whole = 1;
996 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000997 if (tp->t_imaxpct_delta) {
998 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 whole = 1;
1000 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001001 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001002 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 whole = 1;
1004 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001005 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001006 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 whole = 1;
1008 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001009 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001010 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 whole = 1;
1012 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001013 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001014 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 whole = 1;
1016 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001017 if (tp->t_rextslog_delta) {
1018 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 whole = 1;
1020 }
1021
1022 if (whole)
1023 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001024 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001026 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 else
1028 /*
1029 * Since all the modifiable fields are contiguous, we
1030 * can get away with this.
1031 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001032 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
1033 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 sizeof(sbp->sb_frextents) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/*
David Chinner45c34142007-06-18 16:49:44 +10001038 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
1039 * and apply superblock counter changes to the in-core superblock. The
1040 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
1041 * applied to the in-core superblock. The idea is that that has already been
1042 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 *
1044 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
David Chinner45c34142007-06-18 16:49:44 +10001045 * However, we have to ensure that we only modify each superblock field only
1046 * once because the application of the delta values may not be atomic. That can
1047 * lead to ENOSPC races occurring if we have two separate modifcations of the
1048 * free space counter to put back the entire reservation and then take away
1049 * what we used.
1050 *
1051 * If we are not logging superblock counters, then the inode allocated/free and
1052 * used block counts are not updated in the on disk superblock. In this case,
1053 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
1054 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001056void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057xfs_trans_unreserve_and_mod_sb(
1058 xfs_trans_t *tp)
1059{
Christoph Hellwig1b040712010-09-30 02:25:56 +00001060 xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 xfs_mod_sb_t *msbp;
David Chinner92821e22007-05-24 15:26:31 +10001062 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* REFERENCED */
1064 int error;
1065 int rsvd;
David Chinner45c34142007-06-18 16:49:44 +10001066 int64_t blkdelta = 0;
1067 int64_t rtxdelta = 0;
Christoph Hellwig1b040712010-09-30 02:25:56 +00001068 int64_t idelta = 0;
1069 int64_t ifreedelta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 msbp = msb;
1072 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1073
Christoph Hellwig1b040712010-09-30 02:25:56 +00001074 /* calculate deltas */
David Chinner45c34142007-06-18 16:49:44 +10001075 if (tp->t_blk_res > 0)
1076 blkdelta = tp->t_blk_res;
David Chinner45c34142007-06-18 16:49:44 +10001077 if ((tp->t_fdblocks_delta != 0) &&
1078 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1079 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
1080 blkdelta += tp->t_fdblocks_delta;
1081
David Chinner45c34142007-06-18 16:49:44 +10001082 if (tp->t_rtx_res > 0)
1083 rtxdelta = tp->t_rtx_res;
David Chinner45c34142007-06-18 16:49:44 +10001084 if ((tp->t_frextents_delta != 0) &&
1085 (tp->t_flags & XFS_TRANS_SB_DIRTY))
1086 rtxdelta += tp->t_frextents_delta;
1087
Christoph Hellwig1b040712010-09-30 02:25:56 +00001088 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1089 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
1090 idelta = tp->t_icount_delta;
1091 ifreedelta = tp->t_ifree_delta;
1092 }
1093
1094 /* apply the per-cpu counters */
1095 if (blkdelta) {
1096 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
1097 blkdelta, rsvd);
1098 if (error)
1099 goto out;
1100 }
1101
1102 if (idelta) {
1103 error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
1104 idelta, rsvd);
1105 if (error)
1106 goto out_undo_fdblocks;
1107 }
1108
1109 if (ifreedelta) {
1110 error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
1111 ifreedelta, rsvd);
1112 if (error)
1113 goto out_undo_icount;
1114 }
1115
1116 /* apply remaining deltas */
David Chinner45c34142007-06-18 16:49:44 +10001117 if (rtxdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 msbp->msb_field = XFS_SBS_FREXTENTS;
David Chinner45c34142007-06-18 16:49:44 +10001119 msbp->msb_delta = rtxdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 msbp++;
1121 }
1122
David Chinner92821e22007-05-24 15:26:31 +10001123 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (tp->t_dblocks_delta != 0) {
1125 msbp->msb_field = XFS_SBS_DBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001126 msbp->msb_delta = tp->t_dblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 msbp++;
1128 }
1129 if (tp->t_agcount_delta != 0) {
1130 msbp->msb_field = XFS_SBS_AGCOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +11001131 msbp->msb_delta = tp->t_agcount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 msbp++;
1133 }
1134 if (tp->t_imaxpct_delta != 0) {
1135 msbp->msb_field = XFS_SBS_IMAX_PCT;
David Chinner20f4ebf2007-02-10 18:36:10 +11001136 msbp->msb_delta = tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 msbp++;
1138 }
1139 if (tp->t_rextsize_delta != 0) {
1140 msbp->msb_field = XFS_SBS_REXTSIZE;
David Chinner20f4ebf2007-02-10 18:36:10 +11001141 msbp->msb_delta = tp->t_rextsize_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 msbp++;
1143 }
1144 if (tp->t_rbmblocks_delta != 0) {
1145 msbp->msb_field = XFS_SBS_RBMBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001146 msbp->msb_delta = tp->t_rbmblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 msbp++;
1148 }
1149 if (tp->t_rblocks_delta != 0) {
1150 msbp->msb_field = XFS_SBS_RBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001151 msbp->msb_delta = tp->t_rblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 msbp++;
1153 }
1154 if (tp->t_rextents_delta != 0) {
1155 msbp->msb_field = XFS_SBS_REXTENTS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001156 msbp->msb_delta = tp->t_rextents_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 msbp++;
1158 }
1159 if (tp->t_rextslog_delta != 0) {
1160 msbp->msb_field = XFS_SBS_REXTSLOG;
David Chinner20f4ebf2007-02-10 18:36:10 +11001161 msbp->msb_delta = tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 msbp++;
1163 }
1164 }
1165
1166 /*
1167 * If we need to change anything, do it.
1168 */
1169 if (msbp > msb) {
1170 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
1171 (uint)(msbp - msb), rsvd);
Christoph Hellwig1b040712010-09-30 02:25:56 +00001172 if (error)
1173 goto out_undo_ifreecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Christoph Hellwig1b040712010-09-30 02:25:56 +00001175
1176 return;
1177
1178out_undo_ifreecount:
1179 if (ifreedelta)
1180 xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
1181out_undo_icount:
1182 if (idelta)
1183 xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
1184out_undo_fdblocks:
1185 if (blkdelta)
1186 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
1187out:
Jesper Juhl1884bd82010-12-25 20:14:53 +00001188 ASSERT(error == 0);
Christoph Hellwig1b040712010-09-30 02:25:56 +00001189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Dave Chinner09243782010-03-08 11:28:28 +11001192/*
Christoph Hellwige98c4142010-06-23 18:11:15 +10001193 * Add the given log item to the transaction's list of log items.
1194 *
1195 * The log item will now point to its new descriptor with its li_desc field.
1196 */
1197void
1198xfs_trans_add_item(
1199 struct xfs_trans *tp,
1200 struct xfs_log_item *lip)
1201{
1202 struct xfs_log_item_desc *lidp;
1203
Jesper Juhlf65020a2012-02-13 20:51:05 +00001204 ASSERT(lip->li_mountp == tp->t_mountp);
1205 ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
Christoph Hellwige98c4142010-06-23 18:11:15 +10001206
Dave Chinner43869702010-07-20 17:53:44 +10001207 lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwige98c4142010-06-23 18:11:15 +10001208
1209 lidp->lid_item = lip;
1210 lidp->lid_flags = 0;
Christoph Hellwige98c4142010-06-23 18:11:15 +10001211 list_add_tail(&lidp->lid_trans, &tp->t_items);
1212
1213 lip->li_desc = lidp;
1214}
1215
1216STATIC void
1217xfs_trans_free_item_desc(
1218 struct xfs_log_item_desc *lidp)
1219{
1220 list_del_init(&lidp->lid_trans);
1221 kmem_zone_free(xfs_log_item_desc_zone, lidp);
1222}
1223
1224/*
1225 * Unlink and free the given descriptor.
1226 */
1227void
1228xfs_trans_del_item(
1229 struct xfs_log_item *lip)
1230{
1231 xfs_trans_free_item_desc(lip->li_desc);
1232 lip->li_desc = NULL;
1233}
1234
1235/*
1236 * Unlock all of the items of a transaction and free all the descriptors
1237 * of that transaction.
1238 */
Dave Chinnerd17c7012010-08-24 11:42:52 +10001239void
Christoph Hellwige98c4142010-06-23 18:11:15 +10001240xfs_trans_free_items(
1241 struct xfs_trans *tp,
1242 xfs_lsn_t commit_lsn,
1243 int flags)
1244{
1245 struct xfs_log_item_desc *lidp, *next;
1246
1247 list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
1248 struct xfs_log_item *lip = lidp->lid_item;
1249
1250 lip->li_desc = NULL;
1251
1252 if (commit_lsn != NULLCOMMITLSN)
1253 IOP_COMMITTING(lip, commit_lsn);
1254 if (flags & XFS_TRANS_ABORT)
1255 lip->li_flags |= XFS_LI_ABORTED;
1256 IOP_UNLOCK(lip);
1257
1258 xfs_trans_free_item_desc(lidp);
1259 }
1260}
1261
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001262static inline void
1263xfs_log_item_batch_insert(
1264 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001265 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001266 struct xfs_log_item **log_items,
1267 int nr_items,
1268 xfs_lsn_t commit_lsn)
1269{
1270 int i;
1271
1272 spin_lock(&ailp->xa_lock);
1273 /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001274 xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001275
1276 for (i = 0; i < nr_items; i++)
1277 IOP_UNPIN(log_items[i], 0);
1278}
1279
1280/*
1281 * Bulk operation version of xfs_trans_committed that takes a log vector of
1282 * items to insert into the AIL. This uses bulk AIL insertion techniques to
1283 * minimise lock traffic.
Dave Chinnere34a3142011-01-27 12:13:35 +11001284 *
1285 * If we are called with the aborted flag set, it is because a log write during
1286 * a CIL checkpoint commit has failed. In this case, all the items in the
1287 * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
1288 * means that checkpoint commit abort handling is treated exactly the same
1289 * as an iclog write error even though we haven't started any IO yet. Hence in
1290 * this case all we need to do is IOP_COMMITTED processing, followed by an
1291 * IOP_UNPIN(aborted) call.
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001292 *
1293 * The AIL cursor is used to optimise the insert process. If commit_lsn is not
1294 * at the end of the AIL, the insert cursor avoids the need to walk
1295 * the AIL to find the insertion point on every xfs_log_item_batch_insert()
1296 * call. This saves a lot of needless list walking and is a net win, even
1297 * though it slightly increases that amount of AIL lock traffic to set it up
1298 * and tear it down.
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001299 */
1300void
1301xfs_trans_committed_bulk(
1302 struct xfs_ail *ailp,
1303 struct xfs_log_vec *log_vector,
1304 xfs_lsn_t commit_lsn,
1305 int aborted)
1306{
1307#define LOG_ITEM_BATCH_SIZE 32
1308 struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
1309 struct xfs_log_vec *lv;
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001310 struct xfs_ail_cursor cur;
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001311 int i = 0;
1312
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001313 spin_lock(&ailp->xa_lock);
1314 xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
1315 spin_unlock(&ailp->xa_lock);
1316
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001317 /* unpin all the log items */
1318 for (lv = log_vector; lv; lv = lv->lv_next ) {
1319 struct xfs_log_item *lip = lv->lv_item;
1320 xfs_lsn_t item_lsn;
1321
1322 if (aborted)
1323 lip->li_flags |= XFS_LI_ABORTED;
1324 item_lsn = IOP_COMMITTED(lip, commit_lsn);
1325
Dave Chinner1316d4d2011-07-04 05:27:36 +00001326 /* item_lsn of -1 means the item needs no further processing */
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001327 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
1328 continue;
1329
Dave Chinnere34a3142011-01-27 12:13:35 +11001330 /*
1331 * if we are aborting the operation, no point in inserting the
1332 * object into the AIL as we are in a shutdown situation.
1333 */
1334 if (aborted) {
1335 ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
1336 IOP_UNPIN(lip, 1);
1337 continue;
1338 }
1339
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001340 if (item_lsn != commit_lsn) {
1341
1342 /*
1343 * Not a bulk update option due to unusual item_lsn.
1344 * Push into AIL immediately, rechecking the lsn once
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001345 * we have the ail lock. Then unpin the item. This does
1346 * not affect the AIL cursor the bulk insert path is
1347 * using.
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001348 */
1349 spin_lock(&ailp->xa_lock);
1350 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
1351 xfs_trans_ail_update(ailp, lip, item_lsn);
1352 else
1353 spin_unlock(&ailp->xa_lock);
1354 IOP_UNPIN(lip, 0);
1355 continue;
1356 }
1357
1358 /* Item is a candidate for bulk AIL insert. */
1359 log_items[i++] = lv->lv_item;
1360 if (i >= LOG_ITEM_BATCH_SIZE) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001361 xfs_log_item_batch_insert(ailp, &cur, log_items,
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001362 LOG_ITEM_BATCH_SIZE, commit_lsn);
1363 i = 0;
1364 }
1365 }
1366
1367 /* make sure we insert the remainder! */
1368 if (i)
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001369 xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
1370
1371 spin_lock(&ailp->xa_lock);
1372 xfs_trans_ail_cursor_done(ailp, &cur);
1373 spin_unlock(&ailp->xa_lock);
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001374}
1375
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +11001376/*
Christoph Hellwigb1037052011-09-19 14:55:51 +00001377 * Commit the given transaction to the log.
Dave Chinner09243782010-03-08 11:28:28 +11001378 *
1379 * XFS disk error handling mechanism is not based on a typical
1380 * transaction abort mechanism. Logically after the filesystem
1381 * gets marked 'SHUTDOWN', we can't let any new transactions
1382 * be durable - ie. committed to disk - because some metadata might
1383 * be inconsistent. In such cases, this returns an error, and the
1384 * caller may assume that all locked objects joined to the transaction
1385 * have already been unlocked as if the commit had succeeded.
1386 * Do not reference the transaction structure after this call.
1387 */
Dave Chinner09243782010-03-08 11:28:28 +11001388int
Christoph Hellwigb1037052011-09-19 14:55:51 +00001389xfs_trans_commit(
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001390 struct xfs_trans *tp,
Christoph Hellwigb1037052011-09-19 14:55:51 +00001391 uint flags)
Dave Chinner09243782010-03-08 11:28:28 +11001392{
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001393 struct xfs_mount *mp = tp->t_mountp;
Dave Chinner09243782010-03-08 11:28:28 +11001394 xfs_lsn_t commit_lsn = -1;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001395 int error = 0;
Dave Chinner09243782010-03-08 11:28:28 +11001396 int log_flags = 0;
1397 int sync = tp->t_flags & XFS_TRANS_SYNC;
Dave Chinner09243782010-03-08 11:28:28 +11001398
1399 /*
1400 * Determine whether this commit is releasing a permanent
1401 * log reservation or not.
1402 */
1403 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1404 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1405 log_flags = XFS_LOG_REL_PERM_RESERV;
1406 }
1407
1408 /*
1409 * If there is nothing to be logged by the transaction,
1410 * then unlock all of the items associated with the
1411 * transaction and free the transaction structure.
1412 * Also make sure to return any reserved blocks to
1413 * the free pool.
1414 */
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001415 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1416 goto out_unreserve;
1417
1418 if (XFS_FORCED_SHUTDOWN(mp)) {
1419 error = XFS_ERROR(EIO);
1420 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001421 }
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001422
Dave Chinner09243782010-03-08 11:28:28 +11001423 ASSERT(tp->t_ticket != NULL);
1424
1425 /*
1426 * If we need to update the superblock, then do it now.
1427 */
1428 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1429 xfs_trans_apply_sb_deltas(tp);
1430 xfs_trans_apply_dquot_deltas(tp);
1431
Christoph Hellwig0244b962011-12-06 21:58:08 +00001432 error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
Dave Chinner09243782010-03-08 11:28:28 +11001433 if (error == ENOMEM) {
1434 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001435 error = XFS_ERROR(EIO);
1436 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Christoph Hellwig0244b962011-12-06 21:58:08 +00001439 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1440 xfs_trans_free(tp);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /*
1443 * If the transaction needs to be synchronous, then force the
1444 * log out now and wait for it.
1445 */
1446 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001447 if (!error) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001448 error = _xfs_log_force_lsn(mp, commit_lsn,
Christoph Hellwigb1037052011-09-19 14:55:51 +00001449 XFS_LOG_SYNC, NULL);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 XFS_STATS_INC(xs_trans_sync);
1452 } else {
1453 XFS_STATS_INC(xs_trans_async);
1454 }
1455
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001456 return error;
1457
1458out_unreserve:
1459 xfs_trans_unreserve_and_mod_sb(tp);
1460
1461 /*
1462 * It is indeed possible for the transaction to be not dirty but
1463 * the dqinfo portion to be. All that means is that we have some
1464 * (non-persistent) quota reservations that need to be unreserved.
1465 */
1466 xfs_trans_unreserve_and_mod_dquots(tp);
1467 if (tp->t_ticket) {
1468 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1469 if (commit_lsn == -1 && !error)
1470 error = XFS_ERROR(EIO);
1471 }
1472 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Dave Chinner71e330b2010-05-21 14:37:18 +10001473 xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001474 xfs_trans_free(tp);
1475
1476 XFS_STATS_INC(xs_trans_empty);
1477 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * Unlock all of the transaction's items and free the transaction.
1482 * The transaction must not have modified any of its items, because
1483 * there is no way to restore them to their previous state.
1484 *
1485 * If the transaction has made a log reservation, make sure to release
1486 * it as well.
1487 */
1488void
1489xfs_trans_cancel(
1490 xfs_trans_t *tp,
1491 int flags)
1492{
1493 int log_flags;
Ryan Hankins0733af22006-01-11 15:36:44 +11001494 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /*
1497 * See if the caller is being too lazy to figure out if
1498 * the transaction really needs an abort.
1499 */
1500 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1501 flags &= ~XFS_TRANS_ABORT;
1502 /*
1503 * See if the caller is relying on us to shut down the
1504 * filesystem. This happens in paths where we detect
1505 * corruption and decide to give up.
1506 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001507 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001508 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001509 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511#ifdef DEBUG
Christoph Hellwige98c4142010-06-23 18:11:15 +10001512 if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
1513 struct xfs_log_item_desc *lidp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Christoph Hellwige98c4142010-06-23 18:11:15 +10001515 list_for_each_entry(lidp, &tp->t_items, lid_trans)
1516 ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518#endif
1519 xfs_trans_unreserve_and_mod_sb(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001520 xfs_trans_unreserve_and_mod_dquots(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (tp->t_ticket) {
1523 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1524 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1525 log_flags = XFS_LOG_REL_PERM_RESERV;
1526 } else {
1527 log_flags = 0;
1528 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001529 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531
1532 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001533 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Dave Chinner71e330b2010-05-21 14:37:18 +10001535 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 xfs_trans_free(tp);
1537}
1538
Niv Sardi322ff6b2008-08-13 16:05:49 +10001539/*
1540 * Roll from one trans in the sequence of PERMANENT transactions to
1541 * the next: permanent transactions are only flushed out when
1542 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1543 * as possible to let chunks of it go to the log. So we commit the
1544 * chunk we've been working on and get a new transaction to continue.
1545 */
1546int
1547xfs_trans_roll(
1548 struct xfs_trans **tpp,
1549 struct xfs_inode *dp)
1550{
1551 struct xfs_trans *trans;
1552 unsigned int logres, count;
1553 int error;
1554
1555 /*
1556 * Ensure that the inode is always logged.
1557 */
1558 trans = *tpp;
1559 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1560
1561 /*
1562 * Copy the critical parameters from one trans to the next.
1563 */
1564 logres = trans->t_log_res;
1565 count = trans->t_log_count;
1566 *tpp = xfs_trans_dup(trans);
1567
1568 /*
1569 * Commit the current transaction.
1570 * If this commit failed, then it'd just unlock those items that
1571 * are not marked ihold. That also means that a filesystem shutdown
1572 * is in progress. The caller takes the responsibility to cancel
1573 * the duplicate transaction that gets returned.
1574 */
1575 error = xfs_trans_commit(trans, 0);
1576 if (error)
1577 return (error);
1578
1579 trans = *tpp;
1580
1581 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001582 * transaction commit worked ok so we can drop the extra ticket
1583 * reference that we gained in xfs_trans_dup()
1584 */
1585 xfs_log_ticket_put(trans->t_ticket);
1586
1587
1588 /*
Niv Sardi322ff6b2008-08-13 16:05:49 +10001589 * Reserve space in the log for th next transaction.
1590 * This also pushes items in the "AIL", the list of logged items,
1591 * out to disk if they are taking up space at the tail of the log
1592 * that we want to use. This requires that either nothing be locked
1593 * across this call, or that anything that is locked be logged in
1594 * the prior and the next transactions.
1595 */
1596 error = xfs_trans_reserve(trans, 0, logres, 0,
1597 XFS_TRANS_PERM_LOG_RES, count);
1598 /*
1599 * Ensure that the inode is in the new transaction and locked.
1600 */
1601 if (error)
1602 return error;
1603
Christoph Hellwigddc34152011-09-19 15:00:54 +00001604 xfs_trans_ijoin(trans, dp, 0);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001605 return 0;
1606}