blob: 084bd3a13184ac5eebfde1fa8bdc74c52a94255c [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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
51STATIC void xfs_trans_committed(xfs_trans_t *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052STATIC void xfs_trans_free(xfs_trans_t *);
53
Nathan Scott8f794052006-03-14 13:32:41 +110054kmem_zone_t *xfs_trans_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56
57/*
Nathan Scott8f794052006-03-14 13:32:41 +110058 * Reservation functions here avoid a huge stack in xfs_trans_init
59 * due to register overflow from temporaries in the calculations.
60 */
61
62STATIC uint
63xfs_calc_write_reservation(xfs_mount_t *mp)
64{
65 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
66}
67
68STATIC uint
69xfs_calc_itruncate_reservation(xfs_mount_t *mp)
70{
71 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
72}
73
74STATIC uint
75xfs_calc_rename_reservation(xfs_mount_t *mp)
76{
77 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
78}
79
80STATIC uint
81xfs_calc_link_reservation(xfs_mount_t *mp)
82{
Nathan Scott2ddd5922006-03-17 17:25:46 +110083 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
Nathan Scott8f794052006-03-14 13:32:41 +110084}
85
86STATIC uint
87xfs_calc_remove_reservation(xfs_mount_t *mp)
88{
89 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
90}
91
92STATIC uint
93xfs_calc_symlink_reservation(xfs_mount_t *mp)
94{
95 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
96}
97
98STATIC uint
99xfs_calc_create_reservation(xfs_mount_t *mp)
100{
101 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
102}
103
104STATIC uint
105xfs_calc_mkdir_reservation(xfs_mount_t *mp)
106{
107 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
108}
109
110STATIC uint
111xfs_calc_ifree_reservation(xfs_mount_t *mp)
112{
113 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
114}
115
116STATIC uint
117xfs_calc_ichange_reservation(xfs_mount_t *mp)
118{
119 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
120}
121
122STATIC uint
123xfs_calc_growdata_reservation(xfs_mount_t *mp)
124{
125 return XFS_CALC_GROWDATA_LOG_RES(mp);
126}
127
128STATIC uint
129xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
130{
131 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
132}
133
134STATIC uint
135xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
136{
137 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
138}
139
140STATIC uint
141xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
142{
143 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
144}
145
146STATIC uint
147xfs_calc_swrite_reservation(xfs_mount_t *mp)
148{
149 return XFS_CALC_SWRITE_LOG_RES(mp);
150}
151
152STATIC uint
153xfs_calc_writeid_reservation(xfs_mount_t *mp)
154{
155 return XFS_CALC_WRITEID_LOG_RES(mp);
156}
157
158STATIC uint
159xfs_calc_addafork_reservation(xfs_mount_t *mp)
160{
161 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
162}
163
164STATIC uint
165xfs_calc_attrinval_reservation(xfs_mount_t *mp)
166{
167 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
168}
169
170STATIC uint
171xfs_calc_attrset_reservation(xfs_mount_t *mp)
172{
173 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
174}
175
176STATIC uint
177xfs_calc_attrrm_reservation(xfs_mount_t *mp)
178{
179 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
180}
181
182STATIC uint
183xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
184{
185 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
186}
187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * Initialize the precomputed transaction reservation values
190 * in the mount structure.
191 */
192void
193xfs_trans_init(
194 xfs_mount_t *mp)
195{
196 xfs_trans_reservations_t *resp;
197
198 resp = &(mp->m_reservations);
Nathan Scott8f794052006-03-14 13:32:41 +1100199 resp->tr_write = xfs_calc_write_reservation(mp);
200 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
201 resp->tr_rename = xfs_calc_rename_reservation(mp);
202 resp->tr_link = xfs_calc_link_reservation(mp);
203 resp->tr_remove = xfs_calc_remove_reservation(mp);
204 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
205 resp->tr_create = xfs_calc_create_reservation(mp);
206 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
207 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
208 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
209 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
210 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
211 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
212 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
213 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
214 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
215 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
216 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
217 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
218 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
219 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/*
223 * This routine is called to allocate a transaction structure.
224 * The type parameter indicates the type of the transaction. These
225 * are enumerated in xfs_trans.h.
226 *
227 * Dynamically allocate the transaction structure from the transaction
228 * zone, initialize it, and return it to the caller.
229 */
230xfs_trans_t *
231xfs_trans_alloc(
232 xfs_mount_t *mp,
233 uint type)
234{
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000235 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000236 return _xfs_trans_alloc(mp, type, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239xfs_trans_t *
240_xfs_trans_alloc(
241 xfs_mount_t *mp,
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000242 uint type,
243 uint memflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 xfs_trans_t *tp;
246
Nathan Scott34327e12006-06-09 17:11:55 +1000247 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000249 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 tp->t_magic = XFS_TRANS_MAGIC;
251 tp->t_type = type;
252 tp->t_mountp = mp;
253 tp->t_items_free = XFS_LIC_NUM_SLOTS;
254 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000255 xfs_lic_init(&(tp->t_items));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 XFS_LBC_INIT(&(tp->t_busy));
Nathan Scott34327e12006-06-09 17:11:55 +1000257 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
261 * This is called to create a new transaction which will share the
262 * permanent log reservation of the given transaction. The remaining
263 * unused block and rt extent reservations are also inherited. This
264 * implies that the original transaction is no longer allowed to allocate
265 * blocks. Locks and log items, however, are no inherited. They must
266 * be added to the new transaction explicitly.
267 */
268xfs_trans_t *
269xfs_trans_dup(
270 xfs_trans_t *tp)
271{
272 xfs_trans_t *ntp;
273
274 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
275
276 /*
277 * Initialize the new transaction structure.
278 */
279 ntp->t_magic = XFS_TRANS_MAGIC;
280 ntp->t_type = tp->t_type;
281 ntp->t_mountp = tp->t_mountp;
282 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
283 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000284 xfs_lic_init(&(ntp->t_items));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 XFS_LBC_INIT(&(ntp->t_busy));
286
287 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100291 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
293 tp->t_blk_res = tp->t_blk_res_used;
294 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
295 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000296 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Christoph Hellwig7d095252009-06-08 15:33:32 +0200298 xfs_trans_dup_dqinfo(tp, ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 atomic_inc(&tp->t_mountp->m_active_trans);
301 return ntp;
302}
303
304/*
305 * This is called to reserve free disk blocks and log space for the
306 * given transaction. This must be done before allocating any resources
307 * within the transaction.
308 *
309 * This will return ENOSPC if there are not enough blocks available.
310 * It will sleep waiting for available log space.
311 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
312 * is used by long running transactions. If any one of the reservations
313 * fails then they will all be backed out.
314 *
315 * This does not do quota reservations. That typically is done by the
316 * caller afterwards.
317 */
318int
319xfs_trans_reserve(
320 xfs_trans_t *tp,
321 uint blocks,
322 uint logspace,
323 uint rtextents,
324 uint flags,
325 uint logcount)
326{
327 int log_flags;
Nathan Scott59c1b082006-06-09 14:59:13 +1000328 int error = 0;
329 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000332 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /*
335 * Attempt to reserve the needed disk blocks by decrementing
336 * the number needed from the number available. This will
337 * fail if the count would go below zero.
338 */
339 if (blocks > 0) {
340 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100341 -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000343 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return (XFS_ERROR(ENOSPC));
345 }
346 tp->t_blk_res += blocks;
347 }
348
349 /*
350 * Reserve the log space needed for this transaction.
351 */
352 if (logspace > 0) {
353 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
354 ASSERT((tp->t_log_count == 0) ||
355 (tp->t_log_count == logcount));
356 if (flags & XFS_TRANS_PERM_LOG_RES) {
357 log_flags = XFS_LOG_PERM_RESERV;
358 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
359 } else {
360 ASSERT(tp->t_ticket == NULL);
361 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
362 log_flags = 0;
363 }
364
365 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
366 &tp->t_ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000367 XFS_TRANSACTION, log_flags, tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (error) {
369 goto undo_blocks;
370 }
371 tp->t_log_res = logspace;
372 tp->t_log_count = logcount;
373 }
374
375 /*
376 * Attempt to reserve the needed realtime extents by decrementing
377 * the number needed from the number available. This will
378 * fail if the count would go below zero.
379 */
380 if (rtextents > 0) {
381 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100382 -((int64_t)rtextents), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (error) {
384 error = XFS_ERROR(ENOSPC);
385 goto undo_log;
386 }
387 tp->t_rtx_res += rtextents;
388 }
389
390 return 0;
391
392 /*
393 * Error cases jump to one of these labels to undo any
394 * reservations which have already been performed.
395 */
396undo_log:
397 if (logspace > 0) {
398 if (flags & XFS_TRANS_PERM_LOG_RES) {
399 log_flags = XFS_LOG_REL_PERM_RESERV;
400 } else {
401 log_flags = 0;
402 }
403 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
404 tp->t_ticket = NULL;
405 tp->t_log_res = 0;
406 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
407 }
408
409undo_blocks:
410 if (blocks > 0) {
411 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100412 (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 tp->t_blk_res = 0;
414 }
415
Nathan Scott59c1b082006-06-09 14:59:13 +1000416 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Nathan Scott59c1b082006-06-09 14:59:13 +1000418 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421
422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * Record the indicated change to the given field for application
424 * to the file system's superblock when the transaction commits.
425 * For now, just store the change in the transaction structure.
426 *
427 * Mark the transaction structure to indicate that the superblock
428 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000429 *
430 * Because we may not be keeping track of allocated/free inodes and
431 * used filesystem blocks in the superblock, we do not mark the
432 * superblock dirty in this transaction if we modify these fields.
433 * We still need to update the transaction deltas so that they get
434 * applied to the incore superblock, but we don't want them to
435 * cause the superblock to get locked and logged if these are the
436 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
438void
439xfs_trans_mod_sb(
440 xfs_trans_t *tp,
441 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100442 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
David Chinner92821e22007-05-24 15:26:31 +1000444 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
445 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 switch (field) {
448 case XFS_TRANS_SB_ICOUNT:
449 tp->t_icount_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000450 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
451 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 case XFS_TRANS_SB_IFREE:
454 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000455 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
456 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 case XFS_TRANS_SB_FDBLOCKS:
459 /*
460 * Track the number of blocks allocated in the
461 * transaction. Make sure it does not exceed the
462 * number reserved.
463 */
464 if (delta < 0) {
465 tp->t_blk_res_used += (uint)-delta;
466 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
467 }
468 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000469 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
470 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 case XFS_TRANS_SB_RES_FDBLOCKS:
473 /*
474 * The allocation has already been applied to the
475 * in-core superblock's counter. This should only
476 * be applied to the on-disk superblock.
477 */
478 ASSERT(delta < 0);
479 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000480 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
481 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 case XFS_TRANS_SB_FREXTENTS:
484 /*
485 * Track the number of blocks allocated in the
486 * transaction. Make sure it does not exceed the
487 * number reserved.
488 */
489 if (delta < 0) {
490 tp->t_rtx_res_used += (uint)-delta;
491 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
492 }
493 tp->t_frextents_delta += delta;
494 break;
495 case XFS_TRANS_SB_RES_FREXTENTS:
496 /*
497 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000498 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * be applied to the on-disk superblock.
500 */
501 ASSERT(delta < 0);
502 tp->t_res_frextents_delta += delta;
503 break;
504 case XFS_TRANS_SB_DBLOCKS:
505 ASSERT(delta > 0);
506 tp->t_dblocks_delta += delta;
507 break;
508 case XFS_TRANS_SB_AGCOUNT:
509 ASSERT(delta > 0);
510 tp->t_agcount_delta += delta;
511 break;
512 case XFS_TRANS_SB_IMAXPCT:
513 tp->t_imaxpct_delta += delta;
514 break;
515 case XFS_TRANS_SB_REXTSIZE:
516 tp->t_rextsize_delta += delta;
517 break;
518 case XFS_TRANS_SB_RBMBLOCKS:
519 tp->t_rbmblocks_delta += delta;
520 break;
521 case XFS_TRANS_SB_RBLOCKS:
522 tp->t_rblocks_delta += delta;
523 break;
524 case XFS_TRANS_SB_REXTENTS:
525 tp->t_rextents_delta += delta;
526 break;
527 case XFS_TRANS_SB_REXTSLOG:
528 tp->t_rextslog_delta += delta;
529 break;
530 default:
531 ASSERT(0);
532 return;
533 }
534
David Chinner210c6f12007-05-24 15:26:51 +1000535 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/*
539 * xfs_trans_apply_sb_deltas() is called from the commit code
540 * to bring the superblock buffer into the current transaction
541 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
542 *
543 * For now we just look at each field allowed to change and change
544 * it if necessary.
545 */
546STATIC void
547xfs_trans_apply_sb_deltas(
548 xfs_trans_t *tp)
549{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000550 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 xfs_buf_t *bp;
552 int whole = 0;
553
554 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
555 sbp = XFS_BUF_TO_SBP(bp);
556
557 /*
558 * Check that superblock mods match the mods made to AGF counters.
559 */
560 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
561 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
562 tp->t_ag_btree_delta));
563
David Chinner92821e22007-05-24 15:26:31 +1000564 /*
565 * Only update the superblock counters if we are logging them
566 */
567 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000568 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800569 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000570 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800571 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000572 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800573 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000574 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800575 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000578 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800579 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000580 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800581 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000582
583 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800584 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 whole = 1;
586 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000587 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800588 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 whole = 1;
590 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000591 if (tp->t_imaxpct_delta) {
592 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 whole = 1;
594 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000595 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800596 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 whole = 1;
598 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000599 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800600 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 whole = 1;
602 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000603 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800604 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 whole = 1;
606 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000607 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800608 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 whole = 1;
610 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000611 if (tp->t_rextslog_delta) {
612 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 whole = 1;
614 }
615
616 if (whole)
617 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000618 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000620 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 else
622 /*
623 * Since all the modifiable fields are contiguous, we
624 * can get away with this.
625 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000626 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
627 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 sizeof(sbp->sb_frextents) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/*
David Chinner45c34142007-06-18 16:49:44 +1000632 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
633 * and apply superblock counter changes to the in-core superblock. The
634 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
635 * applied to the in-core superblock. The idea is that that has already been
636 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 *
638 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
David Chinner45c34142007-06-18 16:49:44 +1000639 * However, we have to ensure that we only modify each superblock field only
640 * once because the application of the delta values may not be atomic. That can
641 * lead to ENOSPC races occurring if we have two separate modifcations of the
642 * free space counter to put back the entire reservation and then take away
643 * what we used.
644 *
645 * If we are not logging superblock counters, then the inode allocated/free and
646 * used block counts are not updated in the on disk superblock. In this case,
647 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
648 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000650STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651xfs_trans_unreserve_and_mod_sb(
652 xfs_trans_t *tp)
653{
654 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
655 xfs_mod_sb_t *msbp;
David Chinner92821e22007-05-24 15:26:31 +1000656 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* REFERENCED */
658 int error;
659 int rsvd;
David Chinner45c34142007-06-18 16:49:44 +1000660 int64_t blkdelta = 0;
661 int64_t rtxdelta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 msbp = msb;
664 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
665
David Chinner45c34142007-06-18 16:49:44 +1000666 /* calculate free blocks delta */
667 if (tp->t_blk_res > 0)
668 blkdelta = tp->t_blk_res;
669
670 if ((tp->t_fdblocks_delta != 0) &&
671 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
672 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
673 blkdelta += tp->t_fdblocks_delta;
674
675 if (blkdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 msbp->msb_field = XFS_SBS_FDBLOCKS;
David Chinner45c34142007-06-18 16:49:44 +1000677 msbp->msb_delta = blkdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 msbp++;
679 }
680
David Chinner45c34142007-06-18 16:49:44 +1000681 /* calculate free realtime extents delta */
682 if (tp->t_rtx_res > 0)
683 rtxdelta = tp->t_rtx_res;
684
685 if ((tp->t_frextents_delta != 0) &&
686 (tp->t_flags & XFS_TRANS_SB_DIRTY))
687 rtxdelta += tp->t_frextents_delta;
688
689 if (rtxdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 msbp->msb_field = XFS_SBS_FREXTENTS;
David Chinner45c34142007-06-18 16:49:44 +1000691 msbp->msb_delta = rtxdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 msbp++;
693 }
694
David Chinner45c34142007-06-18 16:49:44 +1000695 /* apply remaining deltas */
696
David Chinner92821e22007-05-24 15:26:31 +1000697 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
698 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (tp->t_icount_delta != 0) {
700 msbp->msb_field = XFS_SBS_ICOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100701 msbp->msb_delta = tp->t_icount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 msbp++;
703 }
704 if (tp->t_ifree_delta != 0) {
705 msbp->msb_field = XFS_SBS_IFREE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100706 msbp->msb_delta = tp->t_ifree_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 msbp++;
708 }
David Chinner92821e22007-05-24 15:26:31 +1000709 }
710
711 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (tp->t_dblocks_delta != 0) {
713 msbp->msb_field = XFS_SBS_DBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100714 msbp->msb_delta = tp->t_dblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 msbp++;
716 }
717 if (tp->t_agcount_delta != 0) {
718 msbp->msb_field = XFS_SBS_AGCOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100719 msbp->msb_delta = tp->t_agcount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 msbp++;
721 }
722 if (tp->t_imaxpct_delta != 0) {
723 msbp->msb_field = XFS_SBS_IMAX_PCT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100724 msbp->msb_delta = tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 msbp++;
726 }
727 if (tp->t_rextsize_delta != 0) {
728 msbp->msb_field = XFS_SBS_REXTSIZE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100729 msbp->msb_delta = tp->t_rextsize_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 msbp++;
731 }
732 if (tp->t_rbmblocks_delta != 0) {
733 msbp->msb_field = XFS_SBS_RBMBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100734 msbp->msb_delta = tp->t_rbmblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 msbp++;
736 }
737 if (tp->t_rblocks_delta != 0) {
738 msbp->msb_field = XFS_SBS_RBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100739 msbp->msb_delta = tp->t_rblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 msbp++;
741 }
742 if (tp->t_rextents_delta != 0) {
743 msbp->msb_field = XFS_SBS_REXTENTS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100744 msbp->msb_delta = tp->t_rextents_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 msbp++;
746 }
747 if (tp->t_rextslog_delta != 0) {
748 msbp->msb_field = XFS_SBS_REXTSLOG;
David Chinner20f4ebf2007-02-10 18:36:10 +1100749 msbp->msb_delta = tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 msbp++;
751 }
752 }
753
754 /*
755 * If we need to change anything, do it.
756 */
757 if (msbp > msb) {
758 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
759 (uint)(msbp - msb), rsvd);
760 ASSERT(error == 0);
761 }
762}
763
Dave Chinner09243782010-03-08 11:28:28 +1100764/*
765 * Total up the number of log iovecs needed to commit this
766 * transaction. The transaction itself needs one for the
767 * transaction header. Ask each dirty item in turn how many
768 * it needs to get the total.
769 */
770static uint
771xfs_trans_count_vecs(
772 xfs_trans_t *tp)
773{
774 int nvecs;
775 xfs_log_item_desc_t *lidp;
776
777 nvecs = 1;
778 lidp = xfs_trans_first_item(tp);
779 ASSERT(lidp != NULL);
780
781 /* In the non-debug case we need to start bailing out if we
782 * didn't find a log_item here, return zero and let trans_commit
783 * deal with it.
784 */
785 if (lidp == NULL)
786 return 0;
787
788 while (lidp != NULL) {
789 /*
790 * Skip items which aren't dirty in this transaction.
791 */
792 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
793 lidp = xfs_trans_next_item(tp, lidp);
794 continue;
795 }
796 lidp->lid_size = IOP_SIZE(lidp->lid_item);
797 nvecs += lidp->lid_size;
798 lidp = xfs_trans_next_item(tp, lidp);
799 }
800
801 return nvecs;
802}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804/*
Dave Chinner09243782010-03-08 11:28:28 +1100805 * Fill in the vector with pointers to data to be logged
806 * by this transaction. The transaction header takes
807 * the first vector, and then each dirty item takes the
808 * number of vectors it indicated it needed in xfs_trans_count_vecs().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
Dave Chinner09243782010-03-08 11:28:28 +1100810 * As each item fills in the entries it needs, also pin the item
811 * so that it cannot be flushed out until the log write completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Dave Chinner09243782010-03-08 11:28:28 +1100813static void
814xfs_trans_fill_vecs(
815 struct xfs_trans *tp,
816 struct xfs_log_iovec *log_vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Dave Chinner09243782010-03-08 11:28:28 +1100818 xfs_log_item_desc_t *lidp;
819 struct xfs_log_iovec *vecp;
820 uint nitems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /*
Dave Chinner09243782010-03-08 11:28:28 +1100823 * Skip over the entry for the transaction header, we'll
824 * fill that in at the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Dave Chinner09243782010-03-08 11:28:28 +1100826 vecp = log_vector + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Dave Chinner09243782010-03-08 11:28:28 +1100828 nitems = 0;
829 lidp = xfs_trans_first_item(tp);
830 ASSERT(lidp);
831 while (lidp) {
832 /* Skip items which aren't dirty in this transaction. */
833 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
834 lidp = xfs_trans_next_item(tp, lidp);
835 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Dave Chinner09243782010-03-08 11:28:28 +1100837
838 /*
839 * The item may be marked dirty but not log anything. This can
840 * be used to get called when a transaction is committed.
841 */
842 if (lidp->lid_size)
843 nitems++;
844 IOP_FORMAT(lidp->lid_item, vecp);
845 vecp += lidp->lid_size;
846 IOP_PIN(lidp->lid_item);
847 lidp = xfs_trans_next_item(tp, lidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /*
Dave Chinner09243782010-03-08 11:28:28 +1100851 * Now that we've counted the number of items in this transaction, fill
852 * in the transaction header. Note that the transaction header does not
853 * have a log item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
Dave Chinner09243782010-03-08 11:28:28 +1100855 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
856 tp->t_header.th_type = tp->t_type;
857 tp->t_header.th_num_items = nitems;
858 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
859 log_vector->i_len = sizeof(xfs_trans_header_t);
860 log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
861}
862
863/*
864 * Format the transaction direct to the iclog. This isolates the physical
865 * transaction commit operation from the logical operation and hence allows
866 * other methods to be introduced without affecting the existing commit path.
867 */
868static int
869xfs_trans_commit_iclog(
870 struct xfs_mount *mp,
871 struct xfs_trans *tp,
872 xfs_lsn_t *commit_lsn,
873 int flags)
874{
875 int shutdown;
876 int error;
877 int log_flags = 0;
878 struct xlog_in_core *commit_iclog;
879#define XFS_TRANS_LOGVEC_COUNT 16
880 struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
881 struct xfs_log_iovec *log_vector;
882 uint nvec;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /*
886 * Ask each log item how many log_vector entries it will
887 * need so we can figure out how many to allocate.
888 * Try to avoid the kmem_alloc() call in the common case
889 * by using a vector from the stack when it fits.
890 */
891 nvec = xfs_trans_count_vecs(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (nvec == 0) {
Dave Chinner09243782010-03-08 11:28:28 +1100893 return ENOMEM; /* triggers a shutdown! */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100894 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 log_vector = log_vector_fast;
896 } else {
897 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
898 sizeof(xfs_log_iovec_t),
899 KM_SLEEP);
900 }
901
902 /*
903 * Fill in the log_vector and pin the logged items, and
904 * then write the transaction to the log.
905 */
906 xfs_trans_fill_vecs(tp, log_vector);
907
Dave Chinner09243782010-03-08 11:28:28 +1100908 if (flags & XFS_TRANS_RELEASE_LOG_RES)
909 log_flags = XFS_LOG_REL_PERM_RESERV;
910
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100911 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100914 * The transaction is committed incore here, and can go out to disk
915 * at any time after this call. However, all the items associated
916 * with the transaction are still locked and pinned in memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Dave Chinner09243782010-03-08 11:28:28 +1100918 *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Dave Chinner09243782010-03-08 11:28:28 +1100920 tp->t_commit_lsn = *commit_lsn;
921 if (nvec > XFS_TRANS_LOGVEC_COUNT)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000922 kmem_free(log_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /*
925 * If we got a log write error. Unpin the logitems that we
926 * had pinned, clean up, free trans structure, and return error.
927 */
Dave Chinner09243782010-03-08 11:28:28 +1100928 if (error || *commit_lsn == -1) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000929 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
931 return XFS_ERROR(EIO);
932 }
933
934 /*
935 * Once the transaction has committed, unused
936 * reservations need to be released and changes to
937 * the superblock need to be reflected in the in-core
938 * version. Do that now.
939 */
940 xfs_trans_unreserve_and_mod_sb(tp);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * Tell the LM to call the transaction completion routine
944 * when the log write with LSN commit_lsn completes (e.g.
945 * when the transaction commit really hits the on-disk log).
946 * After this call we cannot reference tp, because the call
947 * can happen at any time and the call will free the transaction
948 * structure pointed to by tp. The only case where we call
949 * the completion routine (xfs_trans_committed) directly is
950 * if the log is turned off on a debug kernel or we're
951 * running in simulation mode (the log is explicitly turned
952 * off).
953 */
954 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
955 tp->t_logcb.cb_arg = tp;
956
957 /*
958 * We need to pass the iclog buffer which was used for the
959 * transaction commit record into this function, and attach
960 * the callback to it. The callback must be attached before
961 * the items are unlocked to avoid racing with other threads
962 * waiting for an item to unlock.
963 */
964 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
965
966 /*
967 * Mark this thread as no longer being in a transaction
968 */
Nathan Scott59c1b082006-06-09 14:59:13 +1000969 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /*
972 * Once all the items of the transaction have been copied
973 * to the in core log and the callback is attached, the
974 * items can be unlocked.
975 *
976 * This will free descriptors pointing to items which were
977 * not logged since there is nothing more to do with them.
978 * For items which were logged, we will keep pointers to them
979 * so they can be unpinned after the transaction commits to disk.
980 * This will also stamp each modified meta-data item with
981 * the commit lsn of this transaction for dependency tracking
982 * purposes.
983 */
Dave Chinner09243782010-03-08 11:28:28 +1100984 xfs_trans_unlock_items(tp, *commit_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /*
987 * If we detected a log error earlier, finish committing
988 * the transaction now (unpin log items, etc).
989 *
990 * Order is critical here, to avoid using the transaction
991 * pointer after its been freed (by xfs_trans_committed
992 * either here now, or as a callback). We cannot do this
993 * step inside xfs_log_notify as was done earlier because
994 * of this issue.
995 */
996 if (shutdown)
997 xfs_trans_committed(tp, XFS_LI_ABORTED);
998
999 /*
1000 * Now that the xfs_trans_committed callback has been attached,
1001 * and the items are released we can finally allow the iclog to
1002 * go to disk.
1003 */
Dave Chinner09243782010-03-08 11:28:28 +11001004 return xfs_log_release_iclog(mp, commit_iclog);
1005}
1006
1007
1008/*
1009 * xfs_trans_commit
1010 *
1011 * Commit the given transaction to the log a/synchronously.
1012 *
1013 * XFS disk error handling mechanism is not based on a typical
1014 * transaction abort mechanism. Logically after the filesystem
1015 * gets marked 'SHUTDOWN', we can't let any new transactions
1016 * be durable - ie. committed to disk - because some metadata might
1017 * be inconsistent. In such cases, this returns an error, and the
1018 * caller may assume that all locked objects joined to the transaction
1019 * have already been unlocked as if the commit had succeeded.
1020 * Do not reference the transaction structure after this call.
1021 */
Dave Chinner09243782010-03-08 11:28:28 +11001022int
1023_xfs_trans_commit(
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001024 struct xfs_trans *tp,
1025 uint flags,
1026 int *log_flushed)
Dave Chinner09243782010-03-08 11:28:28 +11001027{
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001028 struct xfs_mount *mp = tp->t_mountp;
Dave Chinner09243782010-03-08 11:28:28 +11001029 xfs_lsn_t commit_lsn = -1;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001030 int error = 0;
Dave Chinner09243782010-03-08 11:28:28 +11001031 int log_flags = 0;
1032 int sync = tp->t_flags & XFS_TRANS_SYNC;
Dave Chinner09243782010-03-08 11:28:28 +11001033
1034 /*
1035 * Determine whether this commit is releasing a permanent
1036 * log reservation or not.
1037 */
1038 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1039 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1040 log_flags = XFS_LOG_REL_PERM_RESERV;
1041 }
1042
1043 /*
1044 * If there is nothing to be logged by the transaction,
1045 * then unlock all of the items associated with the
1046 * transaction and free the transaction structure.
1047 * Also make sure to return any reserved blocks to
1048 * the free pool.
1049 */
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001050 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1051 goto out_unreserve;
1052
1053 if (XFS_FORCED_SHUTDOWN(mp)) {
1054 error = XFS_ERROR(EIO);
1055 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001056 }
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001057
Dave Chinner09243782010-03-08 11:28:28 +11001058 ASSERT(tp->t_ticket != NULL);
1059
1060 /*
1061 * If we need to update the superblock, then do it now.
1062 */
1063 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1064 xfs_trans_apply_sb_deltas(tp);
1065 xfs_trans_apply_dquot_deltas(tp);
1066
1067 error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
1068 if (error == ENOMEM) {
1069 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001070 error = XFS_ERROR(EIO);
1071 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /*
1075 * If the transaction needs to be synchronous, then force the
1076 * log out now and wait for it.
1077 */
1078 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001079 if (!error) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001080 error = _xfs_log_force_lsn(mp, commit_lsn,
1081 XFS_LOG_SYNC, log_flushed);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 XFS_STATS_INC(xs_trans_sync);
1084 } else {
1085 XFS_STATS_INC(xs_trans_async);
1086 }
1087
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001088 return error;
1089
1090out_unreserve:
1091 xfs_trans_unreserve_and_mod_sb(tp);
1092
1093 /*
1094 * It is indeed possible for the transaction to be not dirty but
1095 * the dqinfo portion to be. All that means is that we have some
1096 * (non-persistent) quota reservations that need to be unreserved.
1097 */
1098 xfs_trans_unreserve_and_mod_dquots(tp);
1099 if (tp->t_ticket) {
1100 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1101 if (commit_lsn == -1 && !error)
1102 error = XFS_ERROR(EIO);
1103 }
1104 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1105 xfs_trans_free_items(tp, error ? XFS_TRANS_ABORT : 0);
1106 xfs_trans_free_busy(tp);
1107 xfs_trans_free(tp);
1108
1109 XFS_STATS_INC(xs_trans_empty);
1110 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
1114 * Called from the trans_commit code when we notice that
1115 * the filesystem is in the middle of a forced shutdown.
1116 */
1117STATIC void
1118xfs_trans_uncommit(
1119 xfs_trans_t *tp,
1120 uint flags)
1121{
1122 xfs_log_item_desc_t *lidp;
1123
1124 for (lidp = xfs_trans_first_item(tp);
1125 lidp != NULL;
1126 lidp = xfs_trans_next_item(tp, lidp)) {
1127 /*
1128 * Unpin all but those that aren't dirty.
1129 */
1130 if (lidp->lid_flags & XFS_LID_DIRTY)
1131 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1132 }
1133
1134 xfs_trans_unreserve_and_mod_sb(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001135 xfs_trans_unreserve_and_mod_dquots(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 xfs_trans_free_items(tp, flags);
1138 xfs_trans_free_busy(tp);
1139 xfs_trans_free(tp);
1140}
1141
1142/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * Unlock all of the transaction's items and free the transaction.
1144 * The transaction must not have modified any of its items, because
1145 * there is no way to restore them to their previous state.
1146 *
1147 * If the transaction has made a log reservation, make sure to release
1148 * it as well.
1149 */
1150void
1151xfs_trans_cancel(
1152 xfs_trans_t *tp,
1153 int flags)
1154{
1155 int log_flags;
1156#ifdef DEBUG
1157 xfs_log_item_chunk_t *licp;
1158 xfs_log_item_desc_t *lidp;
1159 xfs_log_item_t *lip;
1160 int i;
1161#endif
Ryan Hankins0733af22006-01-11 15:36:44 +11001162 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /*
1165 * See if the caller is being too lazy to figure out if
1166 * the transaction really needs an abort.
1167 */
1168 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1169 flags &= ~XFS_TRANS_ABORT;
1170 /*
1171 * See if the caller is relying on us to shut down the
1172 * filesystem. This happens in paths where we detect
1173 * corruption and decide to give up.
1174 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001175 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001176 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001177 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179#ifdef DEBUG
1180 if (!(flags & XFS_TRANS_ABORT)) {
1181 licp = &(tp->t_items);
1182 while (licp != NULL) {
1183 lidp = licp->lic_descs;
1184 for (i = 0; i < licp->lic_unused; i++, lidp++) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10001185 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 continue;
1187 }
1188
1189 lip = lidp->lid_item;
Ryan Hankins0733af22006-01-11 15:36:44 +11001190 if (!XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ASSERT(!(lip->li_type == XFS_LI_EFD));
1192 }
1193 licp = licp->lic_next;
1194 }
1195 }
1196#endif
1197 xfs_trans_unreserve_and_mod_sb(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001198 xfs_trans_unreserve_and_mod_dquots(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 if (tp->t_ticket) {
1201 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1202 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1203 log_flags = XFS_LOG_REL_PERM_RESERV;
1204 } else {
1205 log_flags = 0;
1206 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001207 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
1210 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001211 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 xfs_trans_free_items(tp, flags);
1214 xfs_trans_free_busy(tp);
1215 xfs_trans_free(tp);
1216}
1217
1218
1219/*
1220 * Free the transaction structure. If there is more clean up
1221 * to do when the structure is freed, add it here.
1222 */
1223STATIC void
1224xfs_trans_free(
1225 xfs_trans_t *tp)
1226{
1227 atomic_dec(&tp->t_mountp->m_active_trans);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001228 xfs_trans_free_dqinfo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 kmem_zone_free(xfs_trans_zone, tp);
1230}
1231
Niv Sardi322ff6b2008-08-13 16:05:49 +10001232/*
1233 * Roll from one trans in the sequence of PERMANENT transactions to
1234 * the next: permanent transactions are only flushed out when
1235 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1236 * as possible to let chunks of it go to the log. So we commit the
1237 * chunk we've been working on and get a new transaction to continue.
1238 */
1239int
1240xfs_trans_roll(
1241 struct xfs_trans **tpp,
1242 struct xfs_inode *dp)
1243{
1244 struct xfs_trans *trans;
1245 unsigned int logres, count;
1246 int error;
1247
1248 /*
1249 * Ensure that the inode is always logged.
1250 */
1251 trans = *tpp;
1252 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1253
1254 /*
1255 * Copy the critical parameters from one trans to the next.
1256 */
1257 logres = trans->t_log_res;
1258 count = trans->t_log_count;
1259 *tpp = xfs_trans_dup(trans);
1260
1261 /*
1262 * Commit the current transaction.
1263 * If this commit failed, then it'd just unlock those items that
1264 * are not marked ihold. That also means that a filesystem shutdown
1265 * is in progress. The caller takes the responsibility to cancel
1266 * the duplicate transaction that gets returned.
1267 */
1268 error = xfs_trans_commit(trans, 0);
1269 if (error)
1270 return (error);
1271
1272 trans = *tpp;
1273
1274 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001275 * transaction commit worked ok so we can drop the extra ticket
1276 * reference that we gained in xfs_trans_dup()
1277 */
1278 xfs_log_ticket_put(trans->t_ticket);
1279
1280
1281 /*
Niv Sardi322ff6b2008-08-13 16:05:49 +10001282 * Reserve space in the log for th next transaction.
1283 * This also pushes items in the "AIL", the list of logged items,
1284 * out to disk if they are taking up space at the tail of the log
1285 * that we want to use. This requires that either nothing be locked
1286 * across this call, or that anything that is locked be logged in
1287 * the prior and the next transactions.
1288 */
1289 error = xfs_trans_reserve(trans, 0, logres, 0,
1290 XFS_TRANS_PERM_LOG_RES, count);
1291 /*
1292 * Ensure that the inode is in the new transaction and locked.
1293 */
1294 if (error)
1295 return error;
1296
1297 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1298 xfs_trans_ihold(trans, dp);
1299 return 0;
1300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302/*
Dave Chinner8e646a52010-03-08 15:06:22 +11001303 * The committed item processing consists of calling the committed routine of
1304 * each logged item, updating the item's position in the AIL if necessary, and
1305 * unpinning each item. If the committed routine returns -1, then do nothing
1306 * further with the item because it may have been freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 *
Dave Chinner8e646a52010-03-08 15:06:22 +11001308 * Since items are unlocked when they are copied to the incore log, it is
1309 * possible for two transactions to be completing and manipulating the same
1310 * item simultaneously. The AIL lock will protect the lsn field of each item.
1311 * The value of this field can never go backwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 *
Dave Chinner8e646a52010-03-08 15:06:22 +11001313 * We unpin the items after repositioning them in the AIL, because otherwise
1314 * they could be immediately flushed and we'd have to race with the flusher
1315 * trying to pull the item from the AIL as we add it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
Dave Chinner8e646a52010-03-08 15:06:22 +11001317static void
1318xfs_trans_item_committed(
1319 xfs_log_item_t *lip,
1320 xfs_lsn_t commit_lsn,
1321 int aborted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Dave Chinner8e646a52010-03-08 15:06:22 +11001323 xfs_lsn_t item_lsn;
1324 struct xfs_ail *ailp;
1325
1326 if (aborted)
1327 lip->li_flags |= XFS_LI_ABORTED;
1328
1329 /*
1330 * Send in the ABORTED flag to the COMMITTED routine so that it knows
1331 * whether the transaction was aborted or not.
1332 */
1333 item_lsn = IOP_COMMITTED(lip, commit_lsn);
1334
1335 /*
1336 * If the committed routine returns -1, item has been freed.
1337 */
1338 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
1339 return;
1340
1341 /*
1342 * If the returned lsn is greater than what it contained before, update
1343 * the location of the item in the AIL. If it is not, then do nothing.
1344 * Items can never move backwards in the AIL.
1345 *
1346 * While the new lsn should usually be greater, it is possible that a
1347 * later transaction completing simultaneously with an earlier one
1348 * using the same item could complete first with a higher lsn. This
1349 * would cause the earlier transaction to fail the test below.
1350 */
1351 ailp = lip->li_ailp;
1352 spin_lock(&ailp->xa_lock);
1353 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1354 /*
1355 * This will set the item's lsn to item_lsn and update the
1356 * position of the item in the AIL.
1357 *
1358 * xfs_trans_ail_update() drops the AIL lock.
1359 */
1360 xfs_trans_ail_update(ailp, lip, item_lsn);
1361 } else {
1362 spin_unlock(&ailp->xa_lock);
1363 }
1364
1365 /*
1366 * Now that we've repositioned the item in the AIL, unpin it so it can
1367 * be flushed. Pass information about buffer stale state down from the
1368 * log item flags, if anyone else stales the buffer we do not want to
1369 * pay any attention to it.
1370 */
1371 IOP_UNPIN(lip);
1372}
1373
1374/* Clear all the per-AG busy list items listed in this transaction */
1375static void
1376xfs_trans_clear_busy_extents(
1377 struct xfs_trans *tp)
1378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 xfs_log_busy_chunk_t *lbcp;
1380 xfs_log_busy_slot_t *lbsp;
1381 int i;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 lbcp = &tp->t_busy;
1384 while (lbcp != NULL) {
1385 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1386 if (!XFS_LBC_ISFREE(lbcp, i)) {
1387 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1388 lbsp->lbc_idx);
1389 }
1390 }
1391 lbcp = lbcp->lbc_next;
1392 }
1393 xfs_trans_free_busy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396/*
Dave Chinner8e646a52010-03-08 15:06:22 +11001397 * This is typically called by the LM when a transaction has been fully
1398 * committed to disk. It needs to unpin the items which have
1399 * been logged by the transaction and update their positions
1400 * in the AIL if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 *
Dave Chinner8e646a52010-03-08 15:06:22 +11001402 * This also gets called when the transactions didn't get written out
1403 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
1405STATIC void
Dave Chinner8e646a52010-03-08 15:06:22 +11001406xfs_trans_committed(
1407 xfs_trans_t *tp,
1408 int abortflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 xfs_log_item_desc_t *lidp;
Dave Chinner8e646a52010-03-08 15:06:22 +11001411 xfs_log_item_chunk_t *licp;
1412 xfs_log_item_chunk_t *next_licp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Dave Chinner8e646a52010-03-08 15:06:22 +11001414 /*
1415 * Call the transaction's completion callback if there
1416 * is one.
1417 */
1418 if (tp->t_callback != NULL) {
1419 tp->t_callback(tp, tp->t_callarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Dave Chinner8e646a52010-03-08 15:06:22 +11001421
1422 for (lidp = xfs_trans_first_item(tp);
1423 lidp != NULL;
1424 lidp = xfs_trans_next_item(tp, lidp)) {
1425 xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
1426 }
1427
1428 /* free the item chunks, ignoring the embedded chunk */
1429 licp = tp->t_items.lic_next;
1430 while (licp != NULL) {
1431 next_licp = licp->lic_next;
1432 kmem_free(licp);
1433 licp = next_licp;
1434 }
1435
1436 xfs_trans_clear_busy_extents(tp);
1437 xfs_trans_free(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}