blob: 6ba62c0e24d1f69f027f8295de3ca9dfa2a82dfb [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) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600119 MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
120 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
121 XFS_FSB_TO_B(mp, 1)) +
122 xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
123 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
124 XFS_FSB_TO_B(mp, 1))),
125 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
126 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
127 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100128}
129
Christoph Hellwig025101d2010-05-04 13:53:48 +0000130/*
131 * In truncating a file we free up to two extents at once. We can modify:
132 * the inode being truncated: inode size
133 * the inode's bmap btree: (max depth + 1) * block size
134 * And the bmap_finish transaction can free the blocks and bmap blocks:
135 * the agf for each of the ags: 4 * sector size
136 * the agfl for each of the ags: 4 * sector size
137 * the super block to reflect the freed blocks: sector size
138 * worst case split in allocation btrees per extent assuming 4 extents:
139 * 4 exts * 2 trees * (2 * max depth - 1) * block size
140 * the inode btree: max depth * blocksize
141 * the allocation btrees: 2 trees * (max depth - 1) * block size
142 */
Nathan Scott8f794052006-03-14 13:32:41 +1100143STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000144xfs_calc_itruncate_reservation(
145 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100146{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000147 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600148 MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
149 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
150 XFS_FSB_TO_B(mp, 1))),
151 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
152 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
153 XFS_FSB_TO_B(mp, 1)) +
154 xfs_calc_buf_res(5, 0) +
155 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
156 XFS_FSB_TO_B(mp, 1)) +
157 xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
158 mp->m_in_maxlevels, 0)));
Nathan Scott8f794052006-03-14 13:32:41 +1100159}
160
Christoph Hellwig025101d2010-05-04 13:53:48 +0000161/*
162 * In renaming a files we can modify:
163 * the four inodes involved: 4 * inode size
164 * the two directory btrees: 2 * (max depth + v2) * dir block size
165 * the two directory bmap btrees: 2 * max depth * block size
166 * And the bmap_finish transaction can free dir and bmap blocks (two sets
167 * of bmap blocks) giving:
168 * the agf for the ags in which the blocks live: 3 * sector size
169 * the agfl for the ags in which the blocks live: 3 * sector size
170 * the superblock for the free block count: sector size
171 * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
172 */
Nathan Scott8f794052006-03-14 13:32:41 +1100173STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000174xfs_calc_rename_reservation(
175 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100176{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000177 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600178 MAX((xfs_calc_buf_res(4, mp->m_sb.sb_inodesize) +
179 xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
180 XFS_FSB_TO_B(mp, 1))),
181 (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
182 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
183 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100184}
185
Christoph Hellwig025101d2010-05-04 13:53:48 +0000186/*
187 * For creating a link to an inode:
188 * the parent directory inode: inode size
189 * the linked inode: inode size
190 * the directory btree could split: (max depth + v2) * dir block size
191 * the directory bmap btree could join or split: (max depth + v2) * blocksize
192 * And the bmap_finish transaction can free some bmap blocks giving:
193 * the agf for the ag in which the blocks live: sector size
194 * the agfl for the ag in which the blocks live: sector size
195 * the superblock for the free block count: sector size
196 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
197 */
Nathan Scott8f794052006-03-14 13:32:41 +1100198STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000199xfs_calc_link_reservation(
200 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100201{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000202 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600203 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
204 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
205 XFS_FSB_TO_B(mp, 1))),
206 (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
207 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
208 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100209}
210
Christoph Hellwig025101d2010-05-04 13:53:48 +0000211/*
212 * For removing a directory entry we can modify:
213 * the parent directory inode: inode size
214 * the removed inode: inode size
215 * the directory btree could join: (max depth + v2) * dir block size
216 * the directory bmap btree could join or split: (max depth + v2) * blocksize
217 * And the bmap_finish transaction can free the dir and bmap blocks giving:
218 * the agf for the ag in which the blocks live: 2 * sector size
219 * the agfl for the ag in which the blocks live: 2 * sector size
220 * the superblock for the free block count: sector size
221 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
222 */
Nathan Scott8f794052006-03-14 13:32:41 +1100223STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000224xfs_calc_remove_reservation(
225 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100226{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000227 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600228 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
229 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
230 XFS_FSB_TO_B(mp, 1))),
231 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
232 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
233 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100234}
235
Christoph Hellwig025101d2010-05-04 13:53:48 +0000236/*
237 * For symlink we can modify:
238 * the parent directory inode: inode size
239 * the new inode: inode size
240 * the inode btree entry: 1 block
241 * the directory btree: (max depth + v2) * dir block size
242 * the directory inode's bmap btree: (max depth + v2) * block size
243 * the blocks for the symlink: 1 kB
244 * Or in the first xact we allocate some inodes giving:
245 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
246 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
247 * the inode btree: max depth * blocksize
248 * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
249 */
Nathan Scott8f794052006-03-14 13:32:41 +1100250STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000251xfs_calc_symlink_reservation(
252 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100253{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000254 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600255 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
256 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
257 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
258 XFS_FSB_TO_B(mp, 1)) +
259 xfs_calc_buf_res(1, 1024)),
260 (xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
261 xfs_calc_buf_res(XFS_IALLOC_BLOCKS(mp),
262 XFS_FSB_TO_B(mp, 1)) +
263 xfs_calc_buf_res(mp->m_in_maxlevels,
264 XFS_FSB_TO_B(mp, 1)) +
265 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
266 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100267}
268
Christoph Hellwig025101d2010-05-04 13:53:48 +0000269/*
270 * For create we can modify:
271 * the parent directory inode: inode size
272 * the new inode: inode size
273 * the inode btree entry: block size
274 * the superblock for the nlink flag: sector size
275 * the directory btree: (max depth + v2) * dir block size
276 * the directory inode's bmap btree: (max depth + v2) * block size
277 * Or in the first xact we allocate some inodes giving:
278 * the agi and agf of the ag getting the new inodes: 2 * sectorsize
279 * the superblock for the nlink flag: sector size
280 * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
281 * the inode btree: max depth * blocksize
282 * the allocation btrees: 2 trees * (max depth - 1) * block size
283 */
Nathan Scott8f794052006-03-14 13:32:41 +1100284STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000285xfs_calc_create_reservation(
286 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100287{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000288 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600289 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
290 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
291 (uint)XFS_FSB_TO_B(mp, 1) +
292 xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
293 XFS_FSB_TO_B(mp, 1))),
294 (xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
Christoph Hellwig025101d2010-05-04 13:53:48 +0000295 mp->m_sb.sb_sectsize +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600296 xfs_calc_buf_res(XFS_IALLOC_BLOCKS(mp),
297 XFS_FSB_TO_B(mp, 1)) +
298 xfs_calc_buf_res(mp->m_in_maxlevels,
299 XFS_FSB_TO_B(mp, 1)) +
300 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
301 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100302}
303
Christoph Hellwig025101d2010-05-04 13:53:48 +0000304/*
305 * Making a new directory is the same as creating a new file.
306 */
Nathan Scott8f794052006-03-14 13:32:41 +1100307STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000308xfs_calc_mkdir_reservation(
309 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100310{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000311 return xfs_calc_create_reservation(mp);
Nathan Scott8f794052006-03-14 13:32:41 +1100312}
313
Christoph Hellwig025101d2010-05-04 13:53:48 +0000314/*
315 * In freeing an inode we can modify:
316 * the inode being freed: inode size
317 * the super block free inode counter: sector size
318 * the agi hash list and counters: sector size
319 * the inode btree entry: block size
320 * the on disk inode before ours in the agi hash list: inode cluster size
321 * the inode btree: max depth * blocksize
322 * the allocation btrees: 2 trees * (max depth - 1) * block size
323 */
Nathan Scott8f794052006-03-14 13:32:41 +1100324STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000325xfs_calc_ifree_reservation(
326 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100327{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000328 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600329 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
330 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
331 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
Christoph Hellwig025101d2010-05-04 13:53:48 +0000332 MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
333 XFS_INODE_CLUSTER_SIZE(mp)) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600334 xfs_calc_buf_res(1, 0) +
335 xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
336 mp->m_in_maxlevels, 0) +
337 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
338 XFS_FSB_TO_B(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100339}
340
Christoph Hellwig025101d2010-05-04 13:53:48 +0000341/*
342 * When only changing the inode we log the inode and possibly the superblock
343 * We also add a bit of slop for the transaction stuff.
344 */
Nathan Scott8f794052006-03-14 13:32:41 +1100345STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000346xfs_calc_ichange_reservation(
347 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100348{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000349 return XFS_DQUOT_LOGRES(mp) +
350 mp->m_sb.sb_inodesize +
351 mp->m_sb.sb_sectsize +
352 512;
353
Nathan Scott8f794052006-03-14 13:32:41 +1100354}
355
Christoph Hellwig025101d2010-05-04 13:53:48 +0000356/*
357 * Growing the data section of the filesystem.
358 * superblock
359 * agi and agf
360 * allocation btrees
361 */
Nathan Scott8f794052006-03-14 13:32:41 +1100362STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000363xfs_calc_growdata_reservation(
364 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100365{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600366 return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
367 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
368 XFS_FSB_TO_B(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100369}
370
Christoph Hellwig025101d2010-05-04 13:53:48 +0000371/*
372 * Growing the rt section of the filesystem.
373 * In the first set of transactions (ALLOC) we allocate space to the
374 * bitmap or summary files.
375 * superblock: sector size
376 * agf of the ag from which the extent is allocated: sector size
377 * bmap btree for bitmap/summary inode: max depth * blocksize
378 * bitmap/summary inode: inode size
379 * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
380 */
Nathan Scott8f794052006-03-14 13:32:41 +1100381STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000382xfs_calc_growrtalloc_reservation(
383 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100384{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600385 return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
386 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
387 XFS_FSB_TO_B(mp, 1)) +
388 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
389 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
390 XFS_FSB_TO_B(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100391}
392
Christoph Hellwig025101d2010-05-04 13:53:48 +0000393/*
394 * Growing the rt section of the filesystem.
395 * In the second set of transactions (ZERO) we zero the new metadata blocks.
396 * one bitmap/summary block: blocksize
397 */
Nathan Scott8f794052006-03-14 13:32:41 +1100398STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000399xfs_calc_growrtzero_reservation(
400 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100401{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600402 return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
Nathan Scott8f794052006-03-14 13:32:41 +1100403}
404
Christoph Hellwig025101d2010-05-04 13:53:48 +0000405/*
406 * Growing the rt section of the filesystem.
407 * In the third set of transactions (FREE) we update metadata without
408 * allocating any new blocks.
409 * superblock: sector size
410 * bitmap inode: inode size
411 * summary inode: inode size
412 * one bitmap block: blocksize
413 * summary blocks: new summary size
414 */
Nathan Scott8f794052006-03-14 13:32:41 +1100415STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000416xfs_calc_growrtfree_reservation(
417 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100418{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600419 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
420 xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
421 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
422 xfs_calc_buf_res(1, mp->m_rsumsize);
Nathan Scott8f794052006-03-14 13:32:41 +1100423}
424
Christoph Hellwig025101d2010-05-04 13:53:48 +0000425/*
426 * Logging the inode modification timestamp on a synchronous write.
427 * inode
428 */
Nathan Scott8f794052006-03-14 13:32:41 +1100429STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000430xfs_calc_swrite_reservation(
431 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100432{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600433 return xfs_calc_buf_res(1, mp->m_sb.sb_inodesize);
Nathan Scott8f794052006-03-14 13:32:41 +1100434}
435
Christoph Hellwig025101d2010-05-04 13:53:48 +0000436/*
437 * Logging the inode mode bits when writing a setuid/setgid file
438 * inode
439 */
Nathan Scott8f794052006-03-14 13:32:41 +1100440STATIC uint
441xfs_calc_writeid_reservation(xfs_mount_t *mp)
442{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600443 return xfs_calc_buf_res(1, mp->m_sb.sb_inodesize);
Nathan Scott8f794052006-03-14 13:32:41 +1100444}
445
Christoph Hellwig025101d2010-05-04 13:53:48 +0000446/*
447 * Converting the inode from non-attributed to attributed.
448 * the inode being converted: inode size
449 * agf block and superblock (for block allocation)
450 * the new block (directory sized)
451 * bmap blocks for the new directory block
452 * allocation btrees
453 */
Nathan Scott8f794052006-03-14 13:32:41 +1100454STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000455xfs_calc_addafork_reservation(
456 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100457{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000458 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600459 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
460 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
461 xfs_calc_buf_res(1, mp->m_dirblksize) +
462 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
463 XFS_FSB_TO_B(mp, 1)) +
464 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
465 XFS_FSB_TO_B(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100466}
467
Christoph Hellwig025101d2010-05-04 13:53:48 +0000468/*
469 * Removing the attribute fork of a file
470 * the inode being truncated: inode size
471 * the inode's bmap btree: max depth * block size
472 * And the bmap_finish transaction can free the blocks and bmap blocks:
473 * the agf for each of the ags: 4 * sector size
474 * the agfl for each of the ags: 4 * sector size
475 * the super block to reflect the freed blocks: sector size
476 * worst case split in allocation btrees per extent assuming 4 extents:
477 * 4 exts * 2 trees * (2 * max depth - 1) * block size
478 */
Nathan Scott8f794052006-03-14 13:32:41 +1100479STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000480xfs_calc_attrinval_reservation(
481 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100482{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600483 return MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
484 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
485 XFS_FSB_TO_B(mp, 1))),
486 (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
487 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
488 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100489}
490
Christoph Hellwig025101d2010-05-04 13:53:48 +0000491/*
492 * Setting an attribute.
493 * the inode getting the attribute
494 * the superblock for allocations
495 * the agfs extents are allocated from
496 * the attribute btree * max depth
497 * the inode allocation btree
498 * Since attribute transaction space is dependent on the size of the attribute,
499 * the calculation is done partially at mount time and partially at runtime.
500 */
Nathan Scott8f794052006-03-14 13:32:41 +1100501STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000502xfs_calc_attrset_reservation(
503 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100504{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000505 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600506 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
507 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
508 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
Nathan Scott8f794052006-03-14 13:32:41 +1100509}
510
Christoph Hellwig025101d2010-05-04 13:53:48 +0000511/*
512 * Removing an attribute.
513 * the inode: inode size
514 * the attribute btree could join: max depth * block size
515 * the inode bmap btree could join or split: max depth * block size
516 * And the bmap_finish transaction can free the attr blocks freed giving:
517 * the agf for the ag in which the blocks live: 2 * sector size
518 * the agfl for the ag in which the blocks live: 2 * sector size
519 * the superblock for the free block count: sector size
520 * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
521 */
Nathan Scott8f794052006-03-14 13:32:41 +1100522STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000523xfs_calc_attrrm_reservation(
524 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100525{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000526 return XFS_DQUOT_LOGRES(mp) +
Jeff Liu5b292ae2013-02-01 14:39:29 -0600527 MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
528 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
529 XFS_FSB_TO_B(mp, 1)) +
530 (uint)XFS_FSB_TO_B(mp,
531 XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
532 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
533 (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
534 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
535 XFS_FSB_TO_B(mp, 1))));
Nathan Scott8f794052006-03-14 13:32:41 +1100536}
537
Christoph Hellwig025101d2010-05-04 13:53:48 +0000538/*
539 * Clearing a bad agino number in an agi hash bucket.
540 */
Nathan Scott8f794052006-03-14 13:32:41 +1100541STATIC uint
Christoph Hellwig025101d2010-05-04 13:53:48 +0000542xfs_calc_clear_agi_bucket_reservation(
543 struct xfs_mount *mp)
Nathan Scott8f794052006-03-14 13:32:41 +1100544{
Jeff Liu5b292ae2013-02-01 14:39:29 -0600545 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
Nathan Scott8f794052006-03-14 13:32:41 +1100546}
547
548/*
Jeff Liub0c10b982013-01-28 21:26:16 +0800549 * Clearing the quotaflags in the superblock.
550 * the super block for changing quota flags: sector size
551 */
552STATIC uint
553xfs_calc_qm_sbchange_reservation(
554 struct xfs_mount *mp)
555{
556 return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
557}
558
559/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * Initialize the precomputed transaction reservation values
561 * in the mount structure.
562 */
563void
564xfs_trans_init(
Christoph Hellwig025101d2010-05-04 13:53:48 +0000565 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Christoph Hellwig025101d2010-05-04 13:53:48 +0000567 struct xfs_trans_reservations *resp = &mp->m_reservations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Nathan Scott8f794052006-03-14 13:32:41 +1100569 resp->tr_write = xfs_calc_write_reservation(mp);
570 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
571 resp->tr_rename = xfs_calc_rename_reservation(mp);
572 resp->tr_link = xfs_calc_link_reservation(mp);
573 resp->tr_remove = xfs_calc_remove_reservation(mp);
574 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
575 resp->tr_create = xfs_calc_create_reservation(mp);
576 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
577 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
578 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
579 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
580 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
581 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
582 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
583 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
584 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
585 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
586 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
587 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
588 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
589 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
Jeff Liub0c10b982013-01-28 21:26:16 +0800590 resp->tr_qm_sbchange = xfs_calc_qm_sbchange_reservation(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/*
594 * This routine is called to allocate a transaction structure.
595 * The type parameter indicates the type of the transaction. These
596 * are enumerated in xfs_trans.h.
Alex Elderb2ce3972011-07-11 09:51:44 -0500597 *
598 * Dynamically allocate the transaction structure from the transaction
599 * zone, initialize it, and return it to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Alex Elderb2ce3972011-07-11 09:51:44 -0500601xfs_trans_t *
602xfs_trans_alloc(
603 xfs_mount_t *mp,
604 uint type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jan Karad9457dc2012-06-12 16:20:39 +0200606 xfs_trans_t *tp;
607
608 sb_start_intwrite(mp->m_super);
609 tp = _xfs_trans_alloc(mp, type, KM_SLEEP);
610 tp->t_flags |= XFS_TRANS_FREEZE_PROT;
611 return tp;
Alex Elderb2ce3972011-07-11 09:51:44 -0500612}
613
614xfs_trans_t *
615_xfs_trans_alloc(
616 xfs_mount_t *mp,
617 uint type,
Al Viro77ba7872012-04-02 06:24:04 -0400618 xfs_km_flags_t memflags)
Alex Elderb2ce3972011-07-11 09:51:44 -0500619{
620 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Jan Karad9457dc2012-06-12 16:20:39 +0200622 WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
Nathan Scott34327e12006-06-09 17:11:55 +1000623 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000625 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 tp->t_magic = XFS_TRANS_MAGIC;
627 tp->t_type = type;
628 tp->t_mountp = mp;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000629 INIT_LIST_HEAD(&tp->t_items);
Dave Chinnered3b4d62010-05-21 12:07:08 +1000630 INIT_LIST_HEAD(&tp->t_busy);
Nathan Scott34327e12006-06-09 17:11:55 +1000631 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/*
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100635 * Free the transaction structure. If there is more clean up
636 * to do when the structure is freed, add it here.
637 */
638STATIC void
639xfs_trans_free(
Dave Chinnered3b4d62010-05-21 12:07:08 +1000640 struct xfs_trans *tp)
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100641{
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000642 xfs_extent_busy_sort(&tp->t_busy);
643 xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
Dave Chinnered3b4d62010-05-21 12:07:08 +1000644
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100645 atomic_dec(&tp->t_mountp->m_active_trans);
Jan Karad9457dc2012-06-12 16:20:39 +0200646 if (tp->t_flags & XFS_TRANS_FREEZE_PROT)
647 sb_end_intwrite(tp->t_mountp->m_super);
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100648 xfs_trans_free_dqinfo(tp);
649 kmem_zone_free(xfs_trans_zone, tp);
650}
651
652/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * This is called to create a new transaction which will share the
654 * permanent log reservation of the given transaction. The remaining
655 * unused block and rt extent reservations are also inherited. This
656 * implies that the original transaction is no longer allowed to allocate
657 * blocks. Locks and log items, however, are no inherited. They must
658 * be added to the new transaction explicitly.
659 */
660xfs_trans_t *
661xfs_trans_dup(
662 xfs_trans_t *tp)
663{
664 xfs_trans_t *ntp;
665
666 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
667
668 /*
669 * Initialize the new transaction structure.
670 */
671 ntp->t_magic = XFS_TRANS_MAGIC;
672 ntp->t_type = tp->t_type;
673 ntp->t_mountp = tp->t_mountp;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000674 INIT_LIST_HEAD(&ntp->t_items);
Dave Chinnered3b4d62010-05-21 12:07:08 +1000675 INIT_LIST_HEAD(&ntp->t_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100679
Jan Karad9457dc2012-06-12 16:20:39 +0200680 ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
681 (tp->t_flags & XFS_TRANS_RESERVE) |
682 (tp->t_flags & XFS_TRANS_FREEZE_PROT);
683 /* We gave our writer reference to the new transaction */
684 tp->t_flags &= ~XFS_TRANS_FREEZE_PROT;
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100685 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
687 tp->t_blk_res = tp->t_blk_res_used;
688 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
689 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000690 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Christoph Hellwig7d095252009-06-08 15:33:32 +0200692 xfs_trans_dup_dqinfo(tp, ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 atomic_inc(&tp->t_mountp->m_active_trans);
695 return ntp;
696}
697
698/*
699 * This is called to reserve free disk blocks and log space for the
700 * given transaction. This must be done before allocating any resources
701 * within the transaction.
702 *
703 * This will return ENOSPC if there are not enough blocks available.
704 * It will sleep waiting for available log space.
705 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
706 * is used by long running transactions. If any one of the reservations
707 * fails then they will all be backed out.
708 *
709 * This does not do quota reservations. That typically is done by the
710 * caller afterwards.
711 */
712int
713xfs_trans_reserve(
714 xfs_trans_t *tp,
715 uint blocks,
716 uint logspace,
717 uint rtextents,
718 uint flags,
719 uint logcount)
720{
Nathan Scott59c1b082006-06-09 14:59:13 +1000721 int error = 0;
722 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000725 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /*
728 * Attempt to reserve the needed disk blocks by decrementing
729 * the number needed from the number available. This will
730 * fail if the count would go below zero.
731 */
732 if (blocks > 0) {
Christoph Hellwig96540c72010-09-30 02:25:55 +0000733 error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100734 -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000736 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return (XFS_ERROR(ENOSPC));
738 }
739 tp->t_blk_res += blocks;
740 }
741
742 /*
743 * Reserve the log space needed for this transaction.
744 */
745 if (logspace > 0) {
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000746 bool permanent = false;
747
748 ASSERT(tp->t_log_res == 0 || tp->t_log_res == logspace);
749 ASSERT(tp->t_log_count == 0 || tp->t_log_count == logcount);
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (flags & XFS_TRANS_PERM_LOG_RES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000753 permanent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 } else {
755 ASSERT(tp->t_ticket == NULL);
756 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000759 if (tp->t_ticket != NULL) {
760 ASSERT(flags & XFS_TRANS_PERM_LOG_RES);
761 error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
762 } else {
763 error = xfs_log_reserve(tp->t_mountp, logspace,
764 logcount, &tp->t_ticket,
765 XFS_TRANSACTION, permanent,
766 tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000768
769 if (error)
770 goto undo_blocks;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 tp->t_log_res = logspace;
773 tp->t_log_count = logcount;
774 }
775
776 /*
777 * Attempt to reserve the needed realtime extents by decrementing
778 * the number needed from the number available. This will
779 * fail if the count would go below zero.
780 */
781 if (rtextents > 0) {
782 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100783 -((int64_t)rtextents), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (error) {
785 error = XFS_ERROR(ENOSPC);
786 goto undo_log;
787 }
788 tp->t_rtx_res += rtextents;
789 }
790
791 return 0;
792
793 /*
794 * Error cases jump to one of these labels to undo any
795 * reservations which have already been performed.
796 */
797undo_log:
798 if (logspace > 0) {
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000799 int log_flags;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (flags & XFS_TRANS_PERM_LOG_RES) {
802 log_flags = XFS_LOG_REL_PERM_RESERV;
803 } else {
804 log_flags = 0;
805 }
806 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
807 tp->t_ticket = NULL;
808 tp->t_log_res = 0;
809 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
810 }
811
812undo_blocks:
813 if (blocks > 0) {
Christoph Hellwig96540c72010-09-30 02:25:55 +0000814 xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100815 (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 tp->t_blk_res = 0;
817 }
818
Nathan Scott59c1b082006-06-09 14:59:13 +1000819 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Nathan Scott59c1b082006-06-09 14:59:13 +1000821 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * Record the indicated change to the given field for application
826 * to the file system's superblock when the transaction commits.
827 * For now, just store the change in the transaction structure.
828 *
829 * Mark the transaction structure to indicate that the superblock
830 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000831 *
832 * Because we may not be keeping track of allocated/free inodes and
833 * used filesystem blocks in the superblock, we do not mark the
834 * superblock dirty in this transaction if we modify these fields.
835 * We still need to update the transaction deltas so that they get
836 * applied to the incore superblock, but we don't want them to
837 * cause the superblock to get locked and logged if these are the
838 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
840void
841xfs_trans_mod_sb(
842 xfs_trans_t *tp,
843 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100844 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
David Chinner92821e22007-05-24 15:26:31 +1000846 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
847 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 switch (field) {
850 case XFS_TRANS_SB_ICOUNT:
851 tp->t_icount_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000852 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
853 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 break;
855 case XFS_TRANS_SB_IFREE:
856 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000857 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
858 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 case XFS_TRANS_SB_FDBLOCKS:
861 /*
862 * Track the number of blocks allocated in the
863 * transaction. Make sure it does not exceed the
864 * number reserved.
865 */
866 if (delta < 0) {
867 tp->t_blk_res_used += (uint)-delta;
868 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
869 }
870 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000871 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
872 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 break;
874 case XFS_TRANS_SB_RES_FDBLOCKS:
875 /*
876 * The allocation has already been applied to the
877 * in-core superblock's counter. This should only
878 * be applied to the on-disk superblock.
879 */
880 ASSERT(delta < 0);
881 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000882 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
883 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 break;
885 case XFS_TRANS_SB_FREXTENTS:
886 /*
887 * Track the number of blocks allocated in the
888 * transaction. Make sure it does not exceed the
889 * number reserved.
890 */
891 if (delta < 0) {
892 tp->t_rtx_res_used += (uint)-delta;
893 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
894 }
895 tp->t_frextents_delta += delta;
896 break;
897 case XFS_TRANS_SB_RES_FREXTENTS:
898 /*
899 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000900 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * be applied to the on-disk superblock.
902 */
903 ASSERT(delta < 0);
904 tp->t_res_frextents_delta += delta;
905 break;
906 case XFS_TRANS_SB_DBLOCKS:
907 ASSERT(delta > 0);
908 tp->t_dblocks_delta += delta;
909 break;
910 case XFS_TRANS_SB_AGCOUNT:
911 ASSERT(delta > 0);
912 tp->t_agcount_delta += delta;
913 break;
914 case XFS_TRANS_SB_IMAXPCT:
915 tp->t_imaxpct_delta += delta;
916 break;
917 case XFS_TRANS_SB_REXTSIZE:
918 tp->t_rextsize_delta += delta;
919 break;
920 case XFS_TRANS_SB_RBMBLOCKS:
921 tp->t_rbmblocks_delta += delta;
922 break;
923 case XFS_TRANS_SB_RBLOCKS:
924 tp->t_rblocks_delta += delta;
925 break;
926 case XFS_TRANS_SB_REXTENTS:
927 tp->t_rextents_delta += delta;
928 break;
929 case XFS_TRANS_SB_REXTSLOG:
930 tp->t_rextslog_delta += delta;
931 break;
932 default:
933 ASSERT(0);
934 return;
935 }
936
David Chinner210c6f12007-05-24 15:26:51 +1000937 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/*
941 * xfs_trans_apply_sb_deltas() is called from the commit code
942 * to bring the superblock buffer into the current transaction
943 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
944 *
945 * For now we just look at each field allowed to change and change
946 * it if necessary.
947 */
948STATIC void
949xfs_trans_apply_sb_deltas(
950 xfs_trans_t *tp)
951{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000952 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 xfs_buf_t *bp;
954 int whole = 0;
955
956 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
957 sbp = XFS_BUF_TO_SBP(bp);
958
959 /*
960 * Check that superblock mods match the mods made to AGF counters.
961 */
962 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
963 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
964 tp->t_ag_btree_delta));
965
David Chinner92821e22007-05-24 15:26:31 +1000966 /*
967 * Only update the superblock counters if we are logging them
968 */
969 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000970 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800971 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000972 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800973 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000974 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800975 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000976 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800977 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000980 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800981 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000982 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800983 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000984
985 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800986 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 whole = 1;
988 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000989 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800990 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 whole = 1;
992 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000993 if (tp->t_imaxpct_delta) {
994 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 whole = 1;
996 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000997 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800998 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 whole = 1;
1000 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001001 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001002 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 whole = 1;
1004 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001005 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001006 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 whole = 1;
1008 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001009 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001010 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 whole = 1;
1012 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001013 if (tp->t_rextslog_delta) {
1014 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 whole = 1;
1016 }
1017
1018 if (whole)
1019 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001020 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001022 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 else
1024 /*
1025 * Since all the modifiable fields are contiguous, we
1026 * can get away with this.
1027 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001028 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
1029 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 sizeof(sbp->sb_frextents) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033/*
David Chinner45c34142007-06-18 16:49:44 +10001034 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
1035 * and apply superblock counter changes to the in-core superblock. The
1036 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
1037 * applied to the in-core superblock. The idea is that that has already been
1038 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 *
1040 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
David Chinner45c34142007-06-18 16:49:44 +10001041 * However, we have to ensure that we only modify each superblock field only
1042 * once because the application of the delta values may not be atomic. That can
1043 * lead to ENOSPC races occurring if we have two separate modifcations of the
1044 * free space counter to put back the entire reservation and then take away
1045 * what we used.
1046 *
1047 * If we are not logging superblock counters, then the inode allocated/free and
1048 * used block counts are not updated in the on disk superblock. In this case,
1049 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
1050 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001052void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053xfs_trans_unreserve_and_mod_sb(
1054 xfs_trans_t *tp)
1055{
Christoph Hellwig1b040712010-09-30 02:25:56 +00001056 xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 xfs_mod_sb_t *msbp;
David Chinner92821e22007-05-24 15:26:31 +10001058 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* REFERENCED */
1060 int error;
1061 int rsvd;
David Chinner45c34142007-06-18 16:49:44 +10001062 int64_t blkdelta = 0;
1063 int64_t rtxdelta = 0;
Christoph Hellwig1b040712010-09-30 02:25:56 +00001064 int64_t idelta = 0;
1065 int64_t ifreedelta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 msbp = msb;
1068 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1069
Christoph Hellwig1b040712010-09-30 02:25:56 +00001070 /* calculate deltas */
David Chinner45c34142007-06-18 16:49:44 +10001071 if (tp->t_blk_res > 0)
1072 blkdelta = tp->t_blk_res;
David Chinner45c34142007-06-18 16:49:44 +10001073 if ((tp->t_fdblocks_delta != 0) &&
1074 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1075 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
1076 blkdelta += tp->t_fdblocks_delta;
1077
David Chinner45c34142007-06-18 16:49:44 +10001078 if (tp->t_rtx_res > 0)
1079 rtxdelta = tp->t_rtx_res;
David Chinner45c34142007-06-18 16:49:44 +10001080 if ((tp->t_frextents_delta != 0) &&
1081 (tp->t_flags & XFS_TRANS_SB_DIRTY))
1082 rtxdelta += tp->t_frextents_delta;
1083
Christoph Hellwig1b040712010-09-30 02:25:56 +00001084 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1085 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
1086 idelta = tp->t_icount_delta;
1087 ifreedelta = tp->t_ifree_delta;
1088 }
1089
1090 /* apply the per-cpu counters */
1091 if (blkdelta) {
1092 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
1093 blkdelta, rsvd);
1094 if (error)
1095 goto out;
1096 }
1097
1098 if (idelta) {
1099 error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
1100 idelta, rsvd);
1101 if (error)
1102 goto out_undo_fdblocks;
1103 }
1104
1105 if (ifreedelta) {
1106 error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
1107 ifreedelta, rsvd);
1108 if (error)
1109 goto out_undo_icount;
1110 }
1111
1112 /* apply remaining deltas */
David Chinner45c34142007-06-18 16:49:44 +10001113 if (rtxdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 msbp->msb_field = XFS_SBS_FREXTENTS;
David Chinner45c34142007-06-18 16:49:44 +10001115 msbp->msb_delta = rtxdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 msbp++;
1117 }
1118
David Chinner92821e22007-05-24 15:26:31 +10001119 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (tp->t_dblocks_delta != 0) {
1121 msbp->msb_field = XFS_SBS_DBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001122 msbp->msb_delta = tp->t_dblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 msbp++;
1124 }
1125 if (tp->t_agcount_delta != 0) {
1126 msbp->msb_field = XFS_SBS_AGCOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +11001127 msbp->msb_delta = tp->t_agcount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 msbp++;
1129 }
1130 if (tp->t_imaxpct_delta != 0) {
1131 msbp->msb_field = XFS_SBS_IMAX_PCT;
David Chinner20f4ebf2007-02-10 18:36:10 +11001132 msbp->msb_delta = tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 msbp++;
1134 }
1135 if (tp->t_rextsize_delta != 0) {
1136 msbp->msb_field = XFS_SBS_REXTSIZE;
David Chinner20f4ebf2007-02-10 18:36:10 +11001137 msbp->msb_delta = tp->t_rextsize_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 msbp++;
1139 }
1140 if (tp->t_rbmblocks_delta != 0) {
1141 msbp->msb_field = XFS_SBS_RBMBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001142 msbp->msb_delta = tp->t_rbmblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 msbp++;
1144 }
1145 if (tp->t_rblocks_delta != 0) {
1146 msbp->msb_field = XFS_SBS_RBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001147 msbp->msb_delta = tp->t_rblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 msbp++;
1149 }
1150 if (tp->t_rextents_delta != 0) {
1151 msbp->msb_field = XFS_SBS_REXTENTS;
David Chinner20f4ebf2007-02-10 18:36:10 +11001152 msbp->msb_delta = tp->t_rextents_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 msbp++;
1154 }
1155 if (tp->t_rextslog_delta != 0) {
1156 msbp->msb_field = XFS_SBS_REXTSLOG;
David Chinner20f4ebf2007-02-10 18:36:10 +11001157 msbp->msb_delta = tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 msbp++;
1159 }
1160 }
1161
1162 /*
1163 * If we need to change anything, do it.
1164 */
1165 if (msbp > msb) {
1166 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
1167 (uint)(msbp - msb), rsvd);
Christoph Hellwig1b040712010-09-30 02:25:56 +00001168 if (error)
1169 goto out_undo_ifreecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Christoph Hellwig1b040712010-09-30 02:25:56 +00001171
1172 return;
1173
1174out_undo_ifreecount:
1175 if (ifreedelta)
1176 xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
1177out_undo_icount:
1178 if (idelta)
1179 xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
1180out_undo_fdblocks:
1181 if (blkdelta)
1182 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
1183out:
Jesper Juhl1884bd82010-12-25 20:14:53 +00001184 ASSERT(error == 0);
Christoph Hellwig1b040712010-09-30 02:25:56 +00001185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
Dave Chinner09243782010-03-08 11:28:28 +11001188/*
Christoph Hellwige98c4142010-06-23 18:11:15 +10001189 * Add the given log item to the transaction's list of log items.
1190 *
1191 * The log item will now point to its new descriptor with its li_desc field.
1192 */
1193void
1194xfs_trans_add_item(
1195 struct xfs_trans *tp,
1196 struct xfs_log_item *lip)
1197{
1198 struct xfs_log_item_desc *lidp;
1199
Jesper Juhlf65020a2012-02-13 20:51:05 +00001200 ASSERT(lip->li_mountp == tp->t_mountp);
1201 ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
Christoph Hellwige98c4142010-06-23 18:11:15 +10001202
Dave Chinner43869702010-07-20 17:53:44 +10001203 lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwige98c4142010-06-23 18:11:15 +10001204
1205 lidp->lid_item = lip;
1206 lidp->lid_flags = 0;
Christoph Hellwige98c4142010-06-23 18:11:15 +10001207 list_add_tail(&lidp->lid_trans, &tp->t_items);
1208
1209 lip->li_desc = lidp;
1210}
1211
1212STATIC void
1213xfs_trans_free_item_desc(
1214 struct xfs_log_item_desc *lidp)
1215{
1216 list_del_init(&lidp->lid_trans);
1217 kmem_zone_free(xfs_log_item_desc_zone, lidp);
1218}
1219
1220/*
1221 * Unlink and free the given descriptor.
1222 */
1223void
1224xfs_trans_del_item(
1225 struct xfs_log_item *lip)
1226{
1227 xfs_trans_free_item_desc(lip->li_desc);
1228 lip->li_desc = NULL;
1229}
1230
1231/*
1232 * Unlock all of the items of a transaction and free all the descriptors
1233 * of that transaction.
1234 */
Dave Chinnerd17c7012010-08-24 11:42:52 +10001235void
Christoph Hellwige98c4142010-06-23 18:11:15 +10001236xfs_trans_free_items(
1237 struct xfs_trans *tp,
1238 xfs_lsn_t commit_lsn,
1239 int flags)
1240{
1241 struct xfs_log_item_desc *lidp, *next;
1242
1243 list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
1244 struct xfs_log_item *lip = lidp->lid_item;
1245
1246 lip->li_desc = NULL;
1247
1248 if (commit_lsn != NULLCOMMITLSN)
1249 IOP_COMMITTING(lip, commit_lsn);
1250 if (flags & XFS_TRANS_ABORT)
1251 lip->li_flags |= XFS_LI_ABORTED;
1252 IOP_UNLOCK(lip);
1253
1254 xfs_trans_free_item_desc(lidp);
1255 }
1256}
1257
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001258static inline void
1259xfs_log_item_batch_insert(
1260 struct xfs_ail *ailp,
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001261 struct xfs_ail_cursor *cur,
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001262 struct xfs_log_item **log_items,
1263 int nr_items,
1264 xfs_lsn_t commit_lsn)
1265{
1266 int i;
1267
1268 spin_lock(&ailp->xa_lock);
1269 /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001270 xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001271
1272 for (i = 0; i < nr_items; i++)
1273 IOP_UNPIN(log_items[i], 0);
1274}
1275
1276/*
1277 * Bulk operation version of xfs_trans_committed that takes a log vector of
1278 * items to insert into the AIL. This uses bulk AIL insertion techniques to
1279 * minimise lock traffic.
Dave Chinnere34a3142011-01-27 12:13:35 +11001280 *
1281 * If we are called with the aborted flag set, it is because a log write during
1282 * a CIL checkpoint commit has failed. In this case, all the items in the
1283 * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
1284 * means that checkpoint commit abort handling is treated exactly the same
1285 * as an iclog write error even though we haven't started any IO yet. Hence in
1286 * this case all we need to do is IOP_COMMITTED processing, followed by an
1287 * IOP_UNPIN(aborted) call.
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001288 *
1289 * The AIL cursor is used to optimise the insert process. If commit_lsn is not
1290 * at the end of the AIL, the insert cursor avoids the need to walk
1291 * the AIL to find the insertion point on every xfs_log_item_batch_insert()
1292 * call. This saves a lot of needless list walking and is a net win, even
1293 * though it slightly increases that amount of AIL lock traffic to set it up
1294 * and tear it down.
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001295 */
1296void
1297xfs_trans_committed_bulk(
1298 struct xfs_ail *ailp,
1299 struct xfs_log_vec *log_vector,
1300 xfs_lsn_t commit_lsn,
1301 int aborted)
1302{
1303#define LOG_ITEM_BATCH_SIZE 32
1304 struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
1305 struct xfs_log_vec *lv;
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001306 struct xfs_ail_cursor cur;
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001307 int i = 0;
1308
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001309 spin_lock(&ailp->xa_lock);
1310 xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
1311 spin_unlock(&ailp->xa_lock);
1312
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001313 /* unpin all the log items */
1314 for (lv = log_vector; lv; lv = lv->lv_next ) {
1315 struct xfs_log_item *lip = lv->lv_item;
1316 xfs_lsn_t item_lsn;
1317
1318 if (aborted)
1319 lip->li_flags |= XFS_LI_ABORTED;
1320 item_lsn = IOP_COMMITTED(lip, commit_lsn);
1321
Dave Chinner1316d4d2011-07-04 05:27:36 +00001322 /* item_lsn of -1 means the item needs no further processing */
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001323 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
1324 continue;
1325
Dave Chinnere34a3142011-01-27 12:13:35 +11001326 /*
1327 * if we are aborting the operation, no point in inserting the
1328 * object into the AIL as we are in a shutdown situation.
1329 */
1330 if (aborted) {
1331 ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
1332 IOP_UNPIN(lip, 1);
1333 continue;
1334 }
1335
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001336 if (item_lsn != commit_lsn) {
1337
1338 /*
1339 * Not a bulk update option due to unusual item_lsn.
1340 * Push into AIL immediately, rechecking the lsn once
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001341 * we have the ail lock. Then unpin the item. This does
1342 * not affect the AIL cursor the bulk insert path is
1343 * using.
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001344 */
1345 spin_lock(&ailp->xa_lock);
1346 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
1347 xfs_trans_ail_update(ailp, lip, item_lsn);
1348 else
1349 spin_unlock(&ailp->xa_lock);
1350 IOP_UNPIN(lip, 0);
1351 continue;
1352 }
1353
1354 /* Item is a candidate for bulk AIL insert. */
1355 log_items[i++] = lv->lv_item;
1356 if (i >= LOG_ITEM_BATCH_SIZE) {
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001357 xfs_log_item_batch_insert(ailp, &cur, log_items,
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001358 LOG_ITEM_BATCH_SIZE, commit_lsn);
1359 i = 0;
1360 }
1361 }
1362
1363 /* make sure we insert the remainder! */
1364 if (i)
Dave Chinner1d8c95a2011-07-18 03:40:16 +00001365 xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
1366
1367 spin_lock(&ailp->xa_lock);
1368 xfs_trans_ail_cursor_done(ailp, &cur);
1369 spin_unlock(&ailp->xa_lock);
Dave Chinner0e57f6a2010-12-20 12:02:19 +11001370}
1371
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +11001372/*
Christoph Hellwigb1037052011-09-19 14:55:51 +00001373 * Commit the given transaction to the log.
Dave Chinner09243782010-03-08 11:28:28 +11001374 *
1375 * XFS disk error handling mechanism is not based on a typical
1376 * transaction abort mechanism. Logically after the filesystem
1377 * gets marked 'SHUTDOWN', we can't let any new transactions
1378 * be durable - ie. committed to disk - because some metadata might
1379 * be inconsistent. In such cases, this returns an error, and the
1380 * caller may assume that all locked objects joined to the transaction
1381 * have already been unlocked as if the commit had succeeded.
1382 * Do not reference the transaction structure after this call.
1383 */
Dave Chinner09243782010-03-08 11:28:28 +11001384int
Christoph Hellwigb1037052011-09-19 14:55:51 +00001385xfs_trans_commit(
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001386 struct xfs_trans *tp,
Christoph Hellwigb1037052011-09-19 14:55:51 +00001387 uint flags)
Dave Chinner09243782010-03-08 11:28:28 +11001388{
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001389 struct xfs_mount *mp = tp->t_mountp;
Dave Chinner09243782010-03-08 11:28:28 +11001390 xfs_lsn_t commit_lsn = -1;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001391 int error = 0;
Dave Chinner09243782010-03-08 11:28:28 +11001392 int log_flags = 0;
1393 int sync = tp->t_flags & XFS_TRANS_SYNC;
Dave Chinner09243782010-03-08 11:28:28 +11001394
1395 /*
1396 * Determine whether this commit is releasing a permanent
1397 * log reservation or not.
1398 */
1399 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1400 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1401 log_flags = XFS_LOG_REL_PERM_RESERV;
1402 }
1403
1404 /*
1405 * If there is nothing to be logged by the transaction,
1406 * then unlock all of the items associated with the
1407 * transaction and free the transaction structure.
1408 * Also make sure to return any reserved blocks to
1409 * the free pool.
1410 */
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001411 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1412 goto out_unreserve;
1413
1414 if (XFS_FORCED_SHUTDOWN(mp)) {
1415 error = XFS_ERROR(EIO);
1416 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001417 }
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001418
Dave Chinner09243782010-03-08 11:28:28 +11001419 ASSERT(tp->t_ticket != NULL);
1420
1421 /*
1422 * If we need to update the superblock, then do it now.
1423 */
1424 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1425 xfs_trans_apply_sb_deltas(tp);
1426 xfs_trans_apply_dquot_deltas(tp);
1427
Christoph Hellwig0244b962011-12-06 21:58:08 +00001428 error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
Dave Chinner09243782010-03-08 11:28:28 +11001429 if (error == ENOMEM) {
1430 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001431 error = XFS_ERROR(EIO);
1432 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Christoph Hellwig0244b962011-12-06 21:58:08 +00001435 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1436 xfs_trans_free(tp);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /*
1439 * If the transaction needs to be synchronous, then force the
1440 * log out now and wait for it.
1441 */
1442 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001443 if (!error) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001444 error = _xfs_log_force_lsn(mp, commit_lsn,
Christoph Hellwigb1037052011-09-19 14:55:51 +00001445 XFS_LOG_SYNC, NULL);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 XFS_STATS_INC(xs_trans_sync);
1448 } else {
1449 XFS_STATS_INC(xs_trans_async);
1450 }
1451
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001452 return error;
1453
1454out_unreserve:
1455 xfs_trans_unreserve_and_mod_sb(tp);
1456
1457 /*
1458 * It is indeed possible for the transaction to be not dirty but
1459 * the dqinfo portion to be. All that means is that we have some
1460 * (non-persistent) quota reservations that need to be unreserved.
1461 */
1462 xfs_trans_unreserve_and_mod_dquots(tp);
1463 if (tp->t_ticket) {
1464 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1465 if (commit_lsn == -1 && !error)
1466 error = XFS_ERROR(EIO);
1467 }
1468 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Dave Chinner71e330b2010-05-21 14:37:18 +10001469 xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001470 xfs_trans_free(tp);
1471
1472 XFS_STATS_INC(xs_trans_empty);
1473 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * Unlock all of the transaction's items and free the transaction.
1478 * The transaction must not have modified any of its items, because
1479 * there is no way to restore them to their previous state.
1480 *
1481 * If the transaction has made a log reservation, make sure to release
1482 * it as well.
1483 */
1484void
1485xfs_trans_cancel(
1486 xfs_trans_t *tp,
1487 int flags)
1488{
1489 int log_flags;
Ryan Hankins0733af22006-01-11 15:36:44 +11001490 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /*
1493 * See if the caller is being too lazy to figure out if
1494 * the transaction really needs an abort.
1495 */
1496 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1497 flags &= ~XFS_TRANS_ABORT;
1498 /*
1499 * See if the caller is relying on us to shut down the
1500 * filesystem. This happens in paths where we detect
1501 * corruption and decide to give up.
1502 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001503 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001504 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001505 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507#ifdef DEBUG
Christoph Hellwige98c4142010-06-23 18:11:15 +10001508 if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
1509 struct xfs_log_item_desc *lidp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Christoph Hellwige98c4142010-06-23 18:11:15 +10001511 list_for_each_entry(lidp, &tp->t_items, lid_trans)
1512 ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514#endif
1515 xfs_trans_unreserve_and_mod_sb(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001516 xfs_trans_unreserve_and_mod_dquots(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 if (tp->t_ticket) {
1519 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1520 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1521 log_flags = XFS_LOG_REL_PERM_RESERV;
1522 } else {
1523 log_flags = 0;
1524 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001525 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
1528 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001529 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Dave Chinner71e330b2010-05-21 14:37:18 +10001531 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 xfs_trans_free(tp);
1533}
1534
Niv Sardi322ff6b2008-08-13 16:05:49 +10001535/*
1536 * Roll from one trans in the sequence of PERMANENT transactions to
1537 * the next: permanent transactions are only flushed out when
1538 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1539 * as possible to let chunks of it go to the log. So we commit the
1540 * chunk we've been working on and get a new transaction to continue.
1541 */
1542int
1543xfs_trans_roll(
1544 struct xfs_trans **tpp,
1545 struct xfs_inode *dp)
1546{
1547 struct xfs_trans *trans;
1548 unsigned int logres, count;
1549 int error;
1550
1551 /*
1552 * Ensure that the inode is always logged.
1553 */
1554 trans = *tpp;
1555 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1556
1557 /*
1558 * Copy the critical parameters from one trans to the next.
1559 */
1560 logres = trans->t_log_res;
1561 count = trans->t_log_count;
1562 *tpp = xfs_trans_dup(trans);
1563
1564 /*
1565 * Commit the current transaction.
1566 * If this commit failed, then it'd just unlock those items that
1567 * are not marked ihold. That also means that a filesystem shutdown
1568 * is in progress. The caller takes the responsibility to cancel
1569 * the duplicate transaction that gets returned.
1570 */
1571 error = xfs_trans_commit(trans, 0);
1572 if (error)
1573 return (error);
1574
1575 trans = *tpp;
1576
1577 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001578 * transaction commit worked ok so we can drop the extra ticket
1579 * reference that we gained in xfs_trans_dup()
1580 */
1581 xfs_log_ticket_put(trans->t_ticket);
1582
1583
1584 /*
Niv Sardi322ff6b2008-08-13 16:05:49 +10001585 * Reserve space in the log for th next transaction.
1586 * This also pushes items in the "AIL", the list of logged items,
1587 * out to disk if they are taking up space at the tail of the log
1588 * that we want to use. This requires that either nothing be locked
1589 * across this call, or that anything that is locked be logged in
1590 * the prior and the next transactions.
1591 */
1592 error = xfs_trans_reserve(trans, 0, logres, 0,
1593 XFS_TRANS_PERM_LOG_RES, count);
1594 /*
1595 * Ensure that the inode is in the new transaction and locked.
1596 */
1597 if (error)
1598 return error;
1599
Christoph Hellwigddc34152011-09-19 15:00:54 +00001600 xfs_trans_ijoin(trans, dp, 0);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001601 return 0;
1602}