blob: 4e1c22a23be5fb2496b20600767de4ee1a8822ce [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.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_dir2_sf.h"
36#include "xfs_attr_sf.h"
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
41#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_quota.h"
Nathan Scotta844f452005-11-02 14:38:42 +110044#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_trans_space.h"
Niv Sardi322ff6b2008-08-13 16:05:49 +100046#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48
49STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *);
50STATIC uint xfs_trans_count_vecs(xfs_trans_t *);
51STATIC void xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *);
52STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
53STATIC void xfs_trans_committed(xfs_trans_t *, int);
54STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
55STATIC void xfs_trans_free(xfs_trans_t *);
56
Nathan Scott8f794052006-03-14 13:32:41 +110057kmem_zone_t *xfs_trans_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59
60/*
Nathan Scott8f794052006-03-14 13:32:41 +110061 * Reservation functions here avoid a huge stack in xfs_trans_init
62 * due to register overflow from temporaries in the calculations.
63 */
64
65STATIC uint
66xfs_calc_write_reservation(xfs_mount_t *mp)
67{
68 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
69}
70
71STATIC uint
72xfs_calc_itruncate_reservation(xfs_mount_t *mp)
73{
74 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
75}
76
77STATIC uint
78xfs_calc_rename_reservation(xfs_mount_t *mp)
79{
80 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
81}
82
83STATIC uint
84xfs_calc_link_reservation(xfs_mount_t *mp)
85{
Nathan Scott2ddd5922006-03-17 17:25:46 +110086 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
Nathan Scott8f794052006-03-14 13:32:41 +110087}
88
89STATIC uint
90xfs_calc_remove_reservation(xfs_mount_t *mp)
91{
92 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
93}
94
95STATIC uint
96xfs_calc_symlink_reservation(xfs_mount_t *mp)
97{
98 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
99}
100
101STATIC uint
102xfs_calc_create_reservation(xfs_mount_t *mp)
103{
104 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
105}
106
107STATIC uint
108xfs_calc_mkdir_reservation(xfs_mount_t *mp)
109{
110 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
111}
112
113STATIC uint
114xfs_calc_ifree_reservation(xfs_mount_t *mp)
115{
116 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
117}
118
119STATIC uint
120xfs_calc_ichange_reservation(xfs_mount_t *mp)
121{
122 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
123}
124
125STATIC uint
126xfs_calc_growdata_reservation(xfs_mount_t *mp)
127{
128 return XFS_CALC_GROWDATA_LOG_RES(mp);
129}
130
131STATIC uint
132xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
133{
134 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
135}
136
137STATIC uint
138xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
139{
140 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
141}
142
143STATIC uint
144xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
145{
146 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
147}
148
149STATIC uint
150xfs_calc_swrite_reservation(xfs_mount_t *mp)
151{
152 return XFS_CALC_SWRITE_LOG_RES(mp);
153}
154
155STATIC uint
156xfs_calc_writeid_reservation(xfs_mount_t *mp)
157{
158 return XFS_CALC_WRITEID_LOG_RES(mp);
159}
160
161STATIC uint
162xfs_calc_addafork_reservation(xfs_mount_t *mp)
163{
164 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
165}
166
167STATIC uint
168xfs_calc_attrinval_reservation(xfs_mount_t *mp)
169{
170 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
171}
172
173STATIC uint
174xfs_calc_attrset_reservation(xfs_mount_t *mp)
175{
176 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
177}
178
179STATIC uint
180xfs_calc_attrrm_reservation(xfs_mount_t *mp)
181{
182 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
183}
184
185STATIC uint
186xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
187{
188 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
189}
190
191/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * Initialize the precomputed transaction reservation values
193 * in the mount structure.
194 */
195void
196xfs_trans_init(
197 xfs_mount_t *mp)
198{
199 xfs_trans_reservations_t *resp;
200
201 resp = &(mp->m_reservations);
Nathan Scott8f794052006-03-14 13:32:41 +1100202 resp->tr_write = xfs_calc_write_reservation(mp);
203 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
204 resp->tr_rename = xfs_calc_rename_reservation(mp);
205 resp->tr_link = xfs_calc_link_reservation(mp);
206 resp->tr_remove = xfs_calc_remove_reservation(mp);
207 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
208 resp->tr_create = xfs_calc_create_reservation(mp);
209 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
210 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
211 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
212 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
213 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
214 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
215 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
216 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
217 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
218 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
219 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
220 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
221 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
222 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/*
226 * This routine is called to allocate a transaction structure.
227 * The type parameter indicates the type of the transaction. These
228 * are enumerated in xfs_trans.h.
229 *
230 * Dynamically allocate the transaction structure from the transaction
231 * zone, initialize it, and return it to the caller.
232 */
233xfs_trans_t *
234xfs_trans_alloc(
235 xfs_mount_t *mp,
236 uint type)
237{
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000238 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
Nathan Scott34327e12006-06-09 17:11:55 +1000239 return _xfs_trans_alloc(mp, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242xfs_trans_t *
243_xfs_trans_alloc(
244 xfs_mount_t *mp,
245 uint type)
246{
247 xfs_trans_t *tp;
248
Nathan Scott34327e12006-06-09 17:11:55 +1000249 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Nathan Scott34327e12006-06-09 17:11:55 +1000251 tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 tp->t_magic = XFS_TRANS_MAGIC;
253 tp->t_type = type;
254 tp->t_mountp = mp;
255 tp->t_items_free = XFS_LIC_NUM_SLOTS;
256 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000257 xfs_lic_init(&(tp->t_items));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 XFS_LBC_INIT(&(tp->t_busy));
Nathan Scott34327e12006-06-09 17:11:55 +1000259 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * This is called to create a new transaction which will share the
264 * permanent log reservation of the given transaction. The remaining
265 * unused block and rt extent reservations are also inherited. This
266 * implies that the original transaction is no longer allowed to allocate
267 * blocks. Locks and log items, however, are no inherited. They must
268 * be added to the new transaction explicitly.
269 */
270xfs_trans_t *
271xfs_trans_dup(
272 xfs_trans_t *tp)
273{
274 xfs_trans_t *ntp;
275
276 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
277
278 /*
279 * Initialize the new transaction structure.
280 */
281 ntp->t_magic = XFS_TRANS_MAGIC;
282 ntp->t_type = tp->t_type;
283 ntp->t_mountp = tp->t_mountp;
284 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
285 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000286 xfs_lic_init(&(ntp->t_items));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 XFS_LBC_INIT(&(ntp->t_busy));
288
289 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
293 ntp->t_ticket = tp->t_ticket;
294 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
295 tp->t_blk_res = tp->t_blk_res_used;
296 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
297 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000298 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp);
301
302 atomic_inc(&tp->t_mountp->m_active_trans);
303 return ntp;
304}
305
306/*
307 * This is called to reserve free disk blocks and log space for the
308 * given transaction. This must be done before allocating any resources
309 * within the transaction.
310 *
311 * This will return ENOSPC if there are not enough blocks available.
312 * It will sleep waiting for available log space.
313 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
314 * is used by long running transactions. If any one of the reservations
315 * fails then they will all be backed out.
316 *
317 * This does not do quota reservations. That typically is done by the
318 * caller afterwards.
319 */
320int
321xfs_trans_reserve(
322 xfs_trans_t *tp,
323 uint blocks,
324 uint logspace,
325 uint rtextents,
326 uint flags,
327 uint logcount)
328{
329 int log_flags;
Nathan Scott59c1b082006-06-09 14:59:13 +1000330 int error = 0;
331 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000334 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /*
337 * Attempt to reserve the needed disk blocks by decrementing
338 * the number needed from the number available. This will
339 * fail if the count would go below zero.
340 */
341 if (blocks > 0) {
342 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100343 -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000345 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return (XFS_ERROR(ENOSPC));
347 }
348 tp->t_blk_res += blocks;
349 }
350
351 /*
352 * Reserve the log space needed for this transaction.
353 */
354 if (logspace > 0) {
355 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
356 ASSERT((tp->t_log_count == 0) ||
357 (tp->t_log_count == logcount));
358 if (flags & XFS_TRANS_PERM_LOG_RES) {
359 log_flags = XFS_LOG_PERM_RESERV;
360 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
361 } else {
362 ASSERT(tp->t_ticket == NULL);
363 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
364 log_flags = 0;
365 }
366
367 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
368 &tp->t_ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000369 XFS_TRANSACTION, log_flags, tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (error) {
371 goto undo_blocks;
372 }
373 tp->t_log_res = logspace;
374 tp->t_log_count = logcount;
375 }
376
377 /*
378 * Attempt to reserve the needed realtime extents by decrementing
379 * the number needed from the number available. This will
380 * fail if the count would go below zero.
381 */
382 if (rtextents > 0) {
383 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100384 -((int64_t)rtextents), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (error) {
386 error = XFS_ERROR(ENOSPC);
387 goto undo_log;
388 }
389 tp->t_rtx_res += rtextents;
390 }
391
392 return 0;
393
394 /*
395 * Error cases jump to one of these labels to undo any
396 * reservations which have already been performed.
397 */
398undo_log:
399 if (logspace > 0) {
400 if (flags & XFS_TRANS_PERM_LOG_RES) {
401 log_flags = XFS_LOG_REL_PERM_RESERV;
402 } else {
403 log_flags = 0;
404 }
405 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
406 tp->t_ticket = NULL;
407 tp->t_log_res = 0;
408 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
409 }
410
411undo_blocks:
412 if (blocks > 0) {
413 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100414 (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 tp->t_blk_res = 0;
416 }
417
Nathan Scott59c1b082006-06-09 14:59:13 +1000418 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Nathan Scott59c1b082006-06-09 14:59:13 +1000420 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423
424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * Record the indicated change to the given field for application
426 * to the file system's superblock when the transaction commits.
427 * For now, just store the change in the transaction structure.
428 *
429 * Mark the transaction structure to indicate that the superblock
430 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000431 *
432 * Because we may not be keeping track of allocated/free inodes and
433 * used filesystem blocks in the superblock, we do not mark the
434 * superblock dirty in this transaction if we modify these fields.
435 * We still need to update the transaction deltas so that they get
436 * applied to the incore superblock, but we don't want them to
437 * cause the superblock to get locked and logged if these are the
438 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440void
441xfs_trans_mod_sb(
442 xfs_trans_t *tp,
443 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100444 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
David Chinner92821e22007-05-24 15:26:31 +1000446 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
447 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 switch (field) {
450 case XFS_TRANS_SB_ICOUNT:
451 tp->t_icount_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000452 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
453 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 case XFS_TRANS_SB_IFREE:
456 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000457 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
458 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 case XFS_TRANS_SB_FDBLOCKS:
461 /*
462 * Track the number of blocks allocated in the
463 * transaction. Make sure it does not exceed the
464 * number reserved.
465 */
466 if (delta < 0) {
467 tp->t_blk_res_used += (uint)-delta;
468 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
469 }
470 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000471 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
472 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 case XFS_TRANS_SB_RES_FDBLOCKS:
475 /*
476 * The allocation has already been applied to the
477 * in-core superblock's counter. This should only
478 * be applied to the on-disk superblock.
479 */
480 ASSERT(delta < 0);
481 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000482 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
483 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case XFS_TRANS_SB_FREXTENTS:
486 /*
487 * Track the number of blocks allocated in the
488 * transaction. Make sure it does not exceed the
489 * number reserved.
490 */
491 if (delta < 0) {
492 tp->t_rtx_res_used += (uint)-delta;
493 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
494 }
495 tp->t_frextents_delta += delta;
496 break;
497 case XFS_TRANS_SB_RES_FREXTENTS:
498 /*
499 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000500 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * be applied to the on-disk superblock.
502 */
503 ASSERT(delta < 0);
504 tp->t_res_frextents_delta += delta;
505 break;
506 case XFS_TRANS_SB_DBLOCKS:
507 ASSERT(delta > 0);
508 tp->t_dblocks_delta += delta;
509 break;
510 case XFS_TRANS_SB_AGCOUNT:
511 ASSERT(delta > 0);
512 tp->t_agcount_delta += delta;
513 break;
514 case XFS_TRANS_SB_IMAXPCT:
515 tp->t_imaxpct_delta += delta;
516 break;
517 case XFS_TRANS_SB_REXTSIZE:
518 tp->t_rextsize_delta += delta;
519 break;
520 case XFS_TRANS_SB_RBMBLOCKS:
521 tp->t_rbmblocks_delta += delta;
522 break;
523 case XFS_TRANS_SB_RBLOCKS:
524 tp->t_rblocks_delta += delta;
525 break;
526 case XFS_TRANS_SB_REXTENTS:
527 tp->t_rextents_delta += delta;
528 break;
529 case XFS_TRANS_SB_REXTSLOG:
530 tp->t_rextslog_delta += delta;
531 break;
532 default:
533 ASSERT(0);
534 return;
535 }
536
David Chinner210c6f12007-05-24 15:26:51 +1000537 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
541 * xfs_trans_apply_sb_deltas() is called from the commit code
542 * to bring the superblock buffer into the current transaction
543 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
544 *
545 * For now we just look at each field allowed to change and change
546 * it if necessary.
547 */
548STATIC void
549xfs_trans_apply_sb_deltas(
550 xfs_trans_t *tp)
551{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000552 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 xfs_buf_t *bp;
554 int whole = 0;
555
556 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
557 sbp = XFS_BUF_TO_SBP(bp);
558
559 /*
560 * Check that superblock mods match the mods made to AGF counters.
561 */
562 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
563 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
564 tp->t_ag_btree_delta));
565
David Chinner92821e22007-05-24 15:26:31 +1000566 /*
567 * Only update the superblock counters if we are logging them
568 */
569 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000570 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800571 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000572 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800573 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000574 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800575 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000576 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800577 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000580 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800581 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000582 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800583 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000584
585 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800586 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 whole = 1;
588 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000589 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800590 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 whole = 1;
592 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000593 if (tp->t_imaxpct_delta) {
594 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 whole = 1;
596 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000597 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800598 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 whole = 1;
600 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000601 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800602 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 whole = 1;
604 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000605 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800606 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 whole = 1;
608 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000609 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800610 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 whole = 1;
612 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000613 if (tp->t_rextslog_delta) {
614 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 whole = 1;
616 }
617
618 if (whole)
619 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000620 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000622 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else
624 /*
625 * Since all the modifiable fields are contiguous, we
626 * can get away with this.
627 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000628 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
629 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 sizeof(sbp->sb_frextents) - 1);
631
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000632 tp->t_mountp->m_super->s_dirt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/*
David Chinner45c34142007-06-18 16:49:44 +1000636 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
637 * and apply superblock counter changes to the in-core superblock. The
638 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
639 * applied to the in-core superblock. The idea is that that has already been
640 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 *
642 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
David Chinner45c34142007-06-18 16:49:44 +1000643 * However, we have to ensure that we only modify each superblock field only
644 * once because the application of the delta values may not be atomic. That can
645 * lead to ENOSPC races occurring if we have two separate modifcations of the
646 * free space counter to put back the entire reservation and then take away
647 * what we used.
648 *
649 * If we are not logging superblock counters, then the inode allocated/free and
650 * used block counts are not updated in the on disk superblock. In this case,
651 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
652 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000654STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655xfs_trans_unreserve_and_mod_sb(
656 xfs_trans_t *tp)
657{
658 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
659 xfs_mod_sb_t *msbp;
David Chinner92821e22007-05-24 15:26:31 +1000660 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* REFERENCED */
662 int error;
663 int rsvd;
David Chinner45c34142007-06-18 16:49:44 +1000664 int64_t blkdelta = 0;
665 int64_t rtxdelta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 msbp = msb;
668 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
669
David Chinner45c34142007-06-18 16:49:44 +1000670 /* calculate free blocks delta */
671 if (tp->t_blk_res > 0)
672 blkdelta = tp->t_blk_res;
673
674 if ((tp->t_fdblocks_delta != 0) &&
675 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
676 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
677 blkdelta += tp->t_fdblocks_delta;
678
679 if (blkdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 msbp->msb_field = XFS_SBS_FDBLOCKS;
David Chinner45c34142007-06-18 16:49:44 +1000681 msbp->msb_delta = blkdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 msbp++;
683 }
684
David Chinner45c34142007-06-18 16:49:44 +1000685 /* calculate free realtime extents delta */
686 if (tp->t_rtx_res > 0)
687 rtxdelta = tp->t_rtx_res;
688
689 if ((tp->t_frextents_delta != 0) &&
690 (tp->t_flags & XFS_TRANS_SB_DIRTY))
691 rtxdelta += tp->t_frextents_delta;
692
693 if (rtxdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 msbp->msb_field = XFS_SBS_FREXTENTS;
David Chinner45c34142007-06-18 16:49:44 +1000695 msbp->msb_delta = rtxdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 msbp++;
697 }
698
David Chinner45c34142007-06-18 16:49:44 +1000699 /* apply remaining deltas */
700
David Chinner92821e22007-05-24 15:26:31 +1000701 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
702 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (tp->t_icount_delta != 0) {
704 msbp->msb_field = XFS_SBS_ICOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100705 msbp->msb_delta = tp->t_icount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 msbp++;
707 }
708 if (tp->t_ifree_delta != 0) {
709 msbp->msb_field = XFS_SBS_IFREE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100710 msbp->msb_delta = tp->t_ifree_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 msbp++;
712 }
David Chinner92821e22007-05-24 15:26:31 +1000713 }
714
715 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (tp->t_dblocks_delta != 0) {
717 msbp->msb_field = XFS_SBS_DBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100718 msbp->msb_delta = tp->t_dblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 msbp++;
720 }
721 if (tp->t_agcount_delta != 0) {
722 msbp->msb_field = XFS_SBS_AGCOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100723 msbp->msb_delta = tp->t_agcount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 msbp++;
725 }
726 if (tp->t_imaxpct_delta != 0) {
727 msbp->msb_field = XFS_SBS_IMAX_PCT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100728 msbp->msb_delta = tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 msbp++;
730 }
731 if (tp->t_rextsize_delta != 0) {
732 msbp->msb_field = XFS_SBS_REXTSIZE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100733 msbp->msb_delta = tp->t_rextsize_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 msbp++;
735 }
736 if (tp->t_rbmblocks_delta != 0) {
737 msbp->msb_field = XFS_SBS_RBMBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100738 msbp->msb_delta = tp->t_rbmblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 msbp++;
740 }
741 if (tp->t_rblocks_delta != 0) {
742 msbp->msb_field = XFS_SBS_RBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100743 msbp->msb_delta = tp->t_rblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 msbp++;
745 }
746 if (tp->t_rextents_delta != 0) {
747 msbp->msb_field = XFS_SBS_REXTENTS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100748 msbp->msb_delta = tp->t_rextents_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 msbp++;
750 }
751 if (tp->t_rextslog_delta != 0) {
752 msbp->msb_field = XFS_SBS_REXTSLOG;
David Chinner20f4ebf2007-02-10 18:36:10 +1100753 msbp->msb_delta = tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 msbp++;
755 }
756 }
757
758 /*
759 * If we need to change anything, do it.
760 */
761 if (msbp > msb) {
762 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
763 (uint)(msbp - msb), rsvd);
764 ASSERT(error == 0);
765 }
766}
767
768
769/*
770 * xfs_trans_commit
771 *
772 * Commit the given transaction to the log a/synchronously.
773 *
774 * XFS disk error handling mechanism is not based on a typical
775 * transaction abort mechanism. Logically after the filesystem
776 * gets marked 'SHUTDOWN', we can't let any new transactions
777 * be durable - ie. committed to disk - because some metadata might
778 * be inconsistent. In such cases, this returns an error, and the
779 * caller may assume that all locked objects joined to the transaction
780 * have already been unlocked as if the commit had succeeded.
781 * Do not reference the transaction structure after this call.
782 */
783 /*ARGSUSED*/
784int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100785_xfs_trans_commit(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 xfs_trans_t *tp,
787 uint flags,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100788 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 xfs_log_iovec_t *log_vector;
791 int nvec;
792 xfs_mount_t *mp;
793 xfs_lsn_t commit_lsn;
794 /* REFERENCED */
795 int error;
796 int log_flags;
797 int sync;
798#define XFS_TRANS_LOGVEC_COUNT 16
799 xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 void *commit_iclog;
801 int shutdown;
802
803 commit_lsn = -1;
804
805 /*
806 * Determine whether this commit is releasing a permanent
807 * log reservation or not.
808 */
809 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
810 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
811 log_flags = XFS_LOG_REL_PERM_RESERV;
812 } else {
813 log_flags = 0;
814 }
815 mp = tp->t_mountp;
816
817 /*
818 * If there is nothing to be logged by the transaction,
819 * then unlock all of the items associated with the
820 * transaction and free the transaction structure.
821 * Also make sure to return any reserved blocks to
822 * the free pool.
823 */
824shut_us_down:
825 shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
826 if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
827 xfs_trans_unreserve_and_mod_sb(tp);
828 /*
829 * It is indeed possible for the transaction to be
830 * not dirty but the dqinfo portion to be. All that
831 * means is that we have some (non-persistent) quota
832 * reservations that need to be unreserved.
833 */
834 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
835 if (tp->t_ticket) {
836 commit_lsn = xfs_log_done(mp, tp->t_ticket,
837 NULL, log_flags);
838 if (commit_lsn == -1 && !shutdown)
839 shutdown = XFS_ERROR(EIO);
840 }
Nathan Scott59c1b082006-06-09 14:59:13 +1000841 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
843 xfs_trans_free_busy(tp);
844 xfs_trans_free(tp);
845 XFS_STATS_INC(xs_trans_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return (shutdown);
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ASSERT(tp->t_ticket != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /*
851 * If we need to update the superblock, then do it now.
852 */
853 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
854 xfs_trans_apply_sb_deltas(tp);
855 }
856 XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp);
857
858 /*
859 * Ask each log item how many log_vector entries it will
860 * need so we can figure out how many to allocate.
861 * Try to avoid the kmem_alloc() call in the common case
862 * by using a vector from the stack when it fits.
863 */
864 nvec = xfs_trans_count_vecs(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (nvec == 0) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000866 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto shut_us_down;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100868 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 log_vector = log_vector_fast;
870 } else {
871 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
872 sizeof(xfs_log_iovec_t),
873 KM_SLEEP);
874 }
875
876 /*
877 * Fill in the log_vector and pin the logged items, and
878 * then write the transaction to the log.
879 */
880 xfs_trans_fill_vecs(tp, log_vector);
881
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100882 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /*
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100885 * The transaction is committed incore here, and can go out to disk
886 * at any time after this call. However, all the items associated
887 * with the transaction are still locked and pinned in memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
889 commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 tp->t_commit_lsn = commit_lsn;
892 if (nvec > XFS_TRANS_LOGVEC_COUNT) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000893 kmem_free(log_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
897 * If we got a log write error. Unpin the logitems that we
898 * had pinned, clean up, free trans structure, and return error.
899 */
900 if (error || commit_lsn == -1) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000901 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
903 return XFS_ERROR(EIO);
904 }
905
906 /*
907 * Once the transaction has committed, unused
908 * reservations need to be released and changes to
909 * the superblock need to be reflected in the in-core
910 * version. Do that now.
911 */
912 xfs_trans_unreserve_and_mod_sb(tp);
913
914 sync = tp->t_flags & XFS_TRANS_SYNC;
915
916 /*
917 * Tell the LM to call the transaction completion routine
918 * when the log write with LSN commit_lsn completes (e.g.
919 * when the transaction commit really hits the on-disk log).
920 * After this call we cannot reference tp, because the call
921 * can happen at any time and the call will free the transaction
922 * structure pointed to by tp. The only case where we call
923 * the completion routine (xfs_trans_committed) directly is
924 * if the log is turned off on a debug kernel or we're
925 * running in simulation mode (the log is explicitly turned
926 * off).
927 */
928 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
929 tp->t_logcb.cb_arg = tp;
930
931 /*
932 * We need to pass the iclog buffer which was used for the
933 * transaction commit record into this function, and attach
934 * the callback to it. The callback must be attached before
935 * the items are unlocked to avoid racing with other threads
936 * waiting for an item to unlock.
937 */
938 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
939
940 /*
941 * Mark this thread as no longer being in a transaction
942 */
Nathan Scott59c1b082006-06-09 14:59:13 +1000943 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /*
946 * Once all the items of the transaction have been copied
947 * to the in core log and the callback is attached, the
948 * items can be unlocked.
949 *
950 * This will free descriptors pointing to items which were
951 * not logged since there is nothing more to do with them.
952 * For items which were logged, we will keep pointers to them
953 * so they can be unpinned after the transaction commits to disk.
954 * This will also stamp each modified meta-data item with
955 * the commit lsn of this transaction for dependency tracking
956 * purposes.
957 */
958 xfs_trans_unlock_items(tp, commit_lsn);
959
960 /*
961 * If we detected a log error earlier, finish committing
962 * the transaction now (unpin log items, etc).
963 *
964 * Order is critical here, to avoid using the transaction
965 * pointer after its been freed (by xfs_trans_committed
966 * either here now, or as a callback). We cannot do this
967 * step inside xfs_log_notify as was done earlier because
968 * of this issue.
969 */
970 if (shutdown)
971 xfs_trans_committed(tp, XFS_LI_ABORTED);
972
973 /*
974 * Now that the xfs_trans_committed callback has been attached,
975 * and the items are released we can finally allow the iclog to
976 * go to disk.
977 */
978 error = xfs_log_release_iclog(mp, commit_iclog);
979
980 /*
981 * If the transaction needs to be synchronous, then force the
982 * log out now and wait for it.
983 */
984 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100985 if (!error) {
986 error = _xfs_log_force(mp, commit_lsn,
987 XFS_LOG_FORCE | XFS_LOG_SYNC,
988 log_flushed);
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 XFS_STATS_INC(xs_trans_sync);
991 } else {
992 XFS_STATS_INC(xs_trans_async);
993 }
994
995 return (error);
996}
997
998
999/*
1000 * Total up the number of log iovecs needed to commit this
1001 * transaction. The transaction itself needs one for the
1002 * transaction header. Ask each dirty item in turn how many
1003 * it needs to get the total.
1004 */
1005STATIC uint
1006xfs_trans_count_vecs(
1007 xfs_trans_t *tp)
1008{
1009 int nvecs;
1010 xfs_log_item_desc_t *lidp;
1011
1012 nvecs = 1;
1013 lidp = xfs_trans_first_item(tp);
1014 ASSERT(lidp != NULL);
1015
1016 /* In the non-debug case we need to start bailing out if we
1017 * didn't find a log_item here, return zero and let trans_commit
1018 * deal with it.
1019 */
1020 if (lidp == NULL)
1021 return 0;
1022
1023 while (lidp != NULL) {
1024 /*
1025 * Skip items which aren't dirty in this transaction.
1026 */
1027 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1028 lidp = xfs_trans_next_item(tp, lidp);
1029 continue;
1030 }
1031 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1032 nvecs += lidp->lid_size;
1033 lidp = xfs_trans_next_item(tp, lidp);
1034 }
1035
1036 return nvecs;
1037}
1038
1039/*
1040 * Called from the trans_commit code when we notice that
1041 * the filesystem is in the middle of a forced shutdown.
1042 */
1043STATIC void
1044xfs_trans_uncommit(
1045 xfs_trans_t *tp,
1046 uint flags)
1047{
1048 xfs_log_item_desc_t *lidp;
1049
1050 for (lidp = xfs_trans_first_item(tp);
1051 lidp != NULL;
1052 lidp = xfs_trans_next_item(tp, lidp)) {
1053 /*
1054 * Unpin all but those that aren't dirty.
1055 */
1056 if (lidp->lid_flags & XFS_LID_DIRTY)
1057 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1058 }
1059
1060 xfs_trans_unreserve_and_mod_sb(tp);
1061 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
1062
1063 xfs_trans_free_items(tp, flags);
1064 xfs_trans_free_busy(tp);
1065 xfs_trans_free(tp);
1066}
1067
1068/*
1069 * Fill in the vector with pointers to data to be logged
1070 * by this transaction. The transaction header takes
1071 * the first vector, and then each dirty item takes the
1072 * number of vectors it indicated it needed in xfs_trans_count_vecs().
1073 *
1074 * As each item fills in the entries it needs, also pin the item
1075 * so that it cannot be flushed out until the log write completes.
1076 */
1077STATIC void
1078xfs_trans_fill_vecs(
1079 xfs_trans_t *tp,
1080 xfs_log_iovec_t *log_vector)
1081{
1082 xfs_log_item_desc_t *lidp;
1083 xfs_log_iovec_t *vecp;
1084 uint nitems;
1085
1086 /*
1087 * Skip over the entry for the transaction header, we'll
1088 * fill that in at the end.
1089 */
1090 vecp = log_vector + 1; /* pointer arithmetic */
1091
1092 nitems = 0;
1093 lidp = xfs_trans_first_item(tp);
1094 ASSERT(lidp != NULL);
1095 while (lidp != NULL) {
1096 /*
1097 * Skip items which aren't dirty in this transaction.
1098 */
1099 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1100 lidp = xfs_trans_next_item(tp, lidp);
1101 continue;
1102 }
1103 /*
1104 * The item may be marked dirty but not log anything.
1105 * This can be used to get called when a transaction
1106 * is committed.
1107 */
1108 if (lidp->lid_size) {
1109 nitems++;
1110 }
1111 IOP_FORMAT(lidp->lid_item, vecp);
1112 vecp += lidp->lid_size; /* pointer arithmetic */
1113 IOP_PIN(lidp->lid_item);
1114 lidp = xfs_trans_next_item(tp, lidp);
1115 }
1116
1117 /*
1118 * Now that we've counted the number of items in this
1119 * transaction, fill in the transaction header.
1120 */
1121 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1122 tp->t_header.th_type = tp->t_type;
1123 tp->t_header.th_num_items = nitems;
1124 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1125 log_vector->i_len = sizeof(xfs_trans_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001126 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_TRANSHDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129
1130/*
1131 * Unlock all of the transaction's items and free the transaction.
1132 * The transaction must not have modified any of its items, because
1133 * there is no way to restore them to their previous state.
1134 *
1135 * If the transaction has made a log reservation, make sure to release
1136 * it as well.
1137 */
1138void
1139xfs_trans_cancel(
1140 xfs_trans_t *tp,
1141 int flags)
1142{
1143 int log_flags;
1144#ifdef DEBUG
1145 xfs_log_item_chunk_t *licp;
1146 xfs_log_item_desc_t *lidp;
1147 xfs_log_item_t *lip;
1148 int i;
1149#endif
Ryan Hankins0733af22006-01-11 15:36:44 +11001150 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /*
1153 * See if the caller is being too lazy to figure out if
1154 * the transaction really needs an abort.
1155 */
1156 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1157 flags &= ~XFS_TRANS_ABORT;
1158 /*
1159 * See if the caller is relying on us to shut down the
1160 * filesystem. This happens in paths where we detect
1161 * corruption and decide to give up.
1162 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001163 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001164 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001165 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167#ifdef DEBUG
1168 if (!(flags & XFS_TRANS_ABORT)) {
1169 licp = &(tp->t_items);
1170 while (licp != NULL) {
1171 lidp = licp->lic_descs;
1172 for (i = 0; i < licp->lic_unused; i++, lidp++) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10001173 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 continue;
1175 }
1176
1177 lip = lidp->lid_item;
Ryan Hankins0733af22006-01-11 15:36:44 +11001178 if (!XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 ASSERT(!(lip->li_type == XFS_LI_EFD));
1180 }
1181 licp = licp->lic_next;
1182 }
1183 }
1184#endif
1185 xfs_trans_unreserve_and_mod_sb(tp);
Ryan Hankins0733af22006-01-11 15:36:44 +11001186 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (tp->t_ticket) {
1189 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1190 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1191 log_flags = XFS_LOG_REL_PERM_RESERV;
1192 } else {
1193 log_flags = 0;
1194 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001195 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
1198 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001199 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 xfs_trans_free_items(tp, flags);
1202 xfs_trans_free_busy(tp);
1203 xfs_trans_free(tp);
1204}
1205
1206
1207/*
1208 * Free the transaction structure. If there is more clean up
1209 * to do when the structure is freed, add it here.
1210 */
1211STATIC void
1212xfs_trans_free(
1213 xfs_trans_t *tp)
1214{
1215 atomic_dec(&tp->t_mountp->m_active_trans);
1216 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
1217 kmem_zone_free(xfs_trans_zone, tp);
1218}
1219
Niv Sardi322ff6b2008-08-13 16:05:49 +10001220/*
1221 * Roll from one trans in the sequence of PERMANENT transactions to
1222 * the next: permanent transactions are only flushed out when
1223 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1224 * as possible to let chunks of it go to the log. So we commit the
1225 * chunk we've been working on and get a new transaction to continue.
1226 */
1227int
1228xfs_trans_roll(
1229 struct xfs_trans **tpp,
1230 struct xfs_inode *dp)
1231{
1232 struct xfs_trans *trans;
1233 unsigned int logres, count;
1234 int error;
1235
1236 /*
1237 * Ensure that the inode is always logged.
1238 */
1239 trans = *tpp;
1240 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1241
1242 /*
1243 * Copy the critical parameters from one trans to the next.
1244 */
1245 logres = trans->t_log_res;
1246 count = trans->t_log_count;
1247 *tpp = xfs_trans_dup(trans);
1248
1249 /*
1250 * Commit the current transaction.
1251 * If this commit failed, then it'd just unlock those items that
1252 * are not marked ihold. That also means that a filesystem shutdown
1253 * is in progress. The caller takes the responsibility to cancel
1254 * the duplicate transaction that gets returned.
1255 */
1256 error = xfs_trans_commit(trans, 0);
1257 if (error)
1258 return (error);
1259
1260 trans = *tpp;
1261
1262 /*
1263 * Reserve space in the log for th next transaction.
1264 * This also pushes items in the "AIL", the list of logged items,
1265 * out to disk if they are taking up space at the tail of the log
1266 * that we want to use. This requires that either nothing be locked
1267 * across this call, or that anything that is locked be logged in
1268 * the prior and the next transactions.
1269 */
1270 error = xfs_trans_reserve(trans, 0, logres, 0,
1271 XFS_TRANS_PERM_LOG_RES, count);
1272 /*
1273 * Ensure that the inode is in the new transaction and locked.
1274 */
1275 if (error)
1276 return error;
1277
1278 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1279 xfs_trans_ihold(trans, dp);
1280 return 0;
1281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283/*
1284 * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
1285 *
1286 * This is typically called by the LM when a transaction has been fully
1287 * committed to disk. It needs to unpin the items which have
1288 * been logged by the transaction and update their positions
1289 * in the AIL if necessary.
1290 * This also gets called when the transactions didn't get written out
1291 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1292 *
1293 * Call xfs_trans_chunk_committed() to process the items in
1294 * each chunk.
1295 */
1296STATIC void
1297xfs_trans_committed(
1298 xfs_trans_t *tp,
1299 int abortflag)
1300{
1301 xfs_log_item_chunk_t *licp;
1302 xfs_log_item_chunk_t *next_licp;
1303 xfs_log_busy_chunk_t *lbcp;
1304 xfs_log_busy_slot_t *lbsp;
1305 int i;
1306
1307 /*
1308 * Call the transaction's completion callback if there
1309 * is one.
1310 */
1311 if (tp->t_callback != NULL) {
1312 tp->t_callback(tp, tp->t_callarg);
1313 }
1314
1315 /*
1316 * Special case the chunk embedded in the transaction.
1317 */
1318 licp = &(tp->t_items);
Eric Sandeen39dab9d2008-08-13 16:10:52 +10001319 if (!(xfs_lic_are_all_free(licp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1321 }
1322
1323 /*
1324 * Process the items in each chunk in turn.
1325 */
1326 licp = licp->lic_next;
1327 while (licp != NULL) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10001328 ASSERT(!xfs_lic_are_all_free(licp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1330 next_licp = licp->lic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001331 kmem_free(licp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 licp = next_licp;
1333 }
1334
1335 /*
1336 * Clear all the per-AG busy list items listed in this transaction
1337 */
1338 lbcp = &tp->t_busy;
1339 while (lbcp != NULL) {
1340 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1341 if (!XFS_LBC_ISFREE(lbcp, i)) {
1342 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1343 lbsp->lbc_idx);
1344 }
1345 }
1346 lbcp = lbcp->lbc_next;
1347 }
1348 xfs_trans_free_busy(tp);
1349
1350 /*
1351 * That's it for the transaction structure. Free it.
1352 */
1353 xfs_trans_free(tp);
1354}
1355
1356/*
1357 * This is called to perform the commit processing for each
1358 * item described by the given chunk.
1359 *
1360 * The commit processing consists of unlocking items which were
1361 * held locked with the SYNC_UNLOCK attribute, calling the committed
1362 * routine of each logged item, updating the item's position in the AIL
1363 * if necessary, and unpinning each item. If the committed routine
1364 * returns -1, then do nothing further with the item because it
1365 * may have been freed.
1366 *
1367 * Since items are unlocked when they are copied to the incore
1368 * log, it is possible for two transactions to be completing
1369 * and manipulating the same item simultaneously. The AIL lock
1370 * will protect the lsn field of each item. The value of this
1371 * field can never go backwards.
1372 *
1373 * We unpin the items after repositioning them in the AIL, because
1374 * otherwise they could be immediately flushed and we'd have to race
1375 * with the flusher trying to pull the item from the AIL as we add it.
1376 */
1377STATIC void
1378xfs_trans_chunk_committed(
1379 xfs_log_item_chunk_t *licp,
1380 xfs_lsn_t lsn,
1381 int aborted)
1382{
1383 xfs_log_item_desc_t *lidp;
1384 xfs_log_item_t *lip;
1385 xfs_lsn_t item_lsn;
1386 struct xfs_mount *mp;
1387 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 lidp = licp->lic_descs;
1390 for (i = 0; i < licp->lic_unused; i++, lidp++) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10001391 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 continue;
1393 }
1394
1395 lip = lidp->lid_item;
1396 if (aborted)
1397 lip->li_flags |= XFS_LI_ABORTED;
1398
1399 /*
1400 * Send in the ABORTED flag to the COMMITTED routine
1401 * so that it knows whether the transaction was aborted
1402 * or not.
1403 */
1404 item_lsn = IOP_COMMITTED(lip, lsn);
1405
1406 /*
1407 * If the committed routine returns -1, make
1408 * no more references to the item.
1409 */
1410 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
1411 continue;
1412 }
1413
1414 /*
1415 * If the returned lsn is greater than what it
1416 * contained before, update the location of the
1417 * item in the AIL. If it is not, then do nothing.
1418 * Items can never move backwards in the AIL.
1419 *
1420 * While the new lsn should usually be greater, it
1421 * is possible that a later transaction completing
1422 * simultaneously with an earlier one using the
1423 * same item could complete first with a higher lsn.
1424 * This would cause the earlier transaction to fail
1425 * the test below.
1426 */
1427 mp = lip->li_mountp;
Donald Douwsma287f3da2007-10-11 17:36:05 +10001428 spin_lock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1430 /*
1431 * This will set the item's lsn to item_lsn
1432 * and update the position of the item in
1433 * the AIL.
1434 *
1435 * xfs_trans_update_ail() drops the AIL lock.
1436 */
Donald Douwsma287f3da2007-10-11 17:36:05 +10001437 xfs_trans_update_ail(mp, lip, item_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 } else {
Donald Douwsma287f3da2007-10-11 17:36:05 +10001439 spin_unlock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
1442 /*
1443 * Now that we've repositioned the item in the AIL,
1444 * unpin it so it can be flushed. Pass information
1445 * about buffer stale state down from the log item
1446 * flags, if anyone else stales the buffer we do not
1447 * want to pay any attention to it.
1448 */
1449 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
1450 }
1451}