blob: be578ecb4af2d756dc09538f5cc955df019ef70f [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
Nathan Scott8f794052006-03-14 13:32:41 +110048kmem_zone_t *xfs_trans_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
Nathan Scott8f794052006-03-14 13:32:41 +110051 * Reservation functions here avoid a huge stack in xfs_trans_init
52 * due to register overflow from temporaries in the calculations.
53 */
Nathan Scott8f794052006-03-14 13:32:41 +110054STATIC uint
55xfs_calc_write_reservation(xfs_mount_t *mp)
56{
57 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
58}
59
60STATIC uint
61xfs_calc_itruncate_reservation(xfs_mount_t *mp)
62{
63 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
64}
65
66STATIC uint
67xfs_calc_rename_reservation(xfs_mount_t *mp)
68{
69 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
70}
71
72STATIC uint
73xfs_calc_link_reservation(xfs_mount_t *mp)
74{
Nathan Scott2ddd5922006-03-17 17:25:46 +110075 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
Nathan Scott8f794052006-03-14 13:32:41 +110076}
77
78STATIC uint
79xfs_calc_remove_reservation(xfs_mount_t *mp)
80{
81 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
82}
83
84STATIC uint
85xfs_calc_symlink_reservation(xfs_mount_t *mp)
86{
87 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
88}
89
90STATIC uint
91xfs_calc_create_reservation(xfs_mount_t *mp)
92{
93 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
94}
95
96STATIC uint
97xfs_calc_mkdir_reservation(xfs_mount_t *mp)
98{
99 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
100}
101
102STATIC uint
103xfs_calc_ifree_reservation(xfs_mount_t *mp)
104{
105 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
106}
107
108STATIC uint
109xfs_calc_ichange_reservation(xfs_mount_t *mp)
110{
111 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
112}
113
114STATIC uint
115xfs_calc_growdata_reservation(xfs_mount_t *mp)
116{
117 return XFS_CALC_GROWDATA_LOG_RES(mp);
118}
119
120STATIC uint
121xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
122{
123 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
124}
125
126STATIC uint
127xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
128{
129 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
130}
131
132STATIC uint
133xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
134{
135 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
136}
137
138STATIC uint
139xfs_calc_swrite_reservation(xfs_mount_t *mp)
140{
141 return XFS_CALC_SWRITE_LOG_RES(mp);
142}
143
144STATIC uint
145xfs_calc_writeid_reservation(xfs_mount_t *mp)
146{
147 return XFS_CALC_WRITEID_LOG_RES(mp);
148}
149
150STATIC uint
151xfs_calc_addafork_reservation(xfs_mount_t *mp)
152{
153 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
154}
155
156STATIC uint
157xfs_calc_attrinval_reservation(xfs_mount_t *mp)
158{
159 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
160}
161
162STATIC uint
163xfs_calc_attrset_reservation(xfs_mount_t *mp)
164{
165 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
166}
167
168STATIC uint
169xfs_calc_attrrm_reservation(xfs_mount_t *mp)
170{
171 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
172}
173
174STATIC uint
175xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
176{
177 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
178}
179
180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Initialize the precomputed transaction reservation values
182 * in the mount structure.
183 */
184void
185xfs_trans_init(
186 xfs_mount_t *mp)
187{
188 xfs_trans_reservations_t *resp;
189
190 resp = &(mp->m_reservations);
Nathan Scott8f794052006-03-14 13:32:41 +1100191 resp->tr_write = xfs_calc_write_reservation(mp);
192 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
193 resp->tr_rename = xfs_calc_rename_reservation(mp);
194 resp->tr_link = xfs_calc_link_reservation(mp);
195 resp->tr_remove = xfs_calc_remove_reservation(mp);
196 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
197 resp->tr_create = xfs_calc_create_reservation(mp);
198 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
199 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
200 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
201 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
202 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
203 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
204 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
205 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
206 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
207 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
208 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
209 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
210 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
211 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/*
215 * This routine is called to allocate a transaction structure.
216 * The type parameter indicates the type of the transaction. These
217 * are enumerated in xfs_trans.h.
218 *
219 * Dynamically allocate the transaction structure from the transaction
220 * zone, initialize it, and return it to the caller.
221 */
222xfs_trans_t *
223xfs_trans_alloc(
224 xfs_mount_t *mp,
225 uint type)
226{
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000227 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000228 return _xfs_trans_alloc(mp, type, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231xfs_trans_t *
232_xfs_trans_alloc(
233 xfs_mount_t *mp,
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000234 uint type,
235 uint memflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 xfs_trans_t *tp;
238
Nathan Scott34327e12006-06-09 17:11:55 +1000239 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000241 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 tp->t_magic = XFS_TRANS_MAGIC;
243 tp->t_type = type;
244 tp->t_mountp = mp;
245 tp->t_items_free = XFS_LIC_NUM_SLOTS;
246 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000247 xfs_lic_init(&(tp->t_items));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 XFS_LBC_INIT(&(tp->t_busy));
Nathan Scott34327e12006-06-09 17:11:55 +1000249 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100253 * Free the transaction structure. If there is more clean up
254 * to do when the structure is freed, add it here.
255 */
256STATIC void
257xfs_trans_free(
258 xfs_trans_t *tp)
259{
260 atomic_dec(&tp->t_mountp->m_active_trans);
261 xfs_trans_free_dqinfo(tp);
262 kmem_zone_free(xfs_trans_zone, tp);
263}
264
265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * This is called to create a new transaction which will share the
267 * permanent log reservation of the given transaction. The remaining
268 * unused block and rt extent reservations are also inherited. This
269 * implies that the original transaction is no longer allowed to allocate
270 * blocks. Locks and log items, however, are no inherited. They must
271 * be added to the new transaction explicitly.
272 */
273xfs_trans_t *
274xfs_trans_dup(
275 xfs_trans_t *tp)
276{
277 xfs_trans_t *ntp;
278
279 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
280
281 /*
282 * Initialize the new transaction structure.
283 */
284 ntp->t_magic = XFS_TRANS_MAGIC;
285 ntp->t_type = tp->t_type;
286 ntp->t_mountp = tp->t_mountp;
287 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
288 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000289 xfs_lic_init(&(ntp->t_items));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 XFS_LBC_INIT(&(ntp->t_busy));
291
292 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100296 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
298 tp->t_blk_res = tp->t_blk_res_used;
299 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
300 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000301 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Christoph Hellwig7d095252009-06-08 15:33:32 +0200303 xfs_trans_dup_dqinfo(tp, ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 atomic_inc(&tp->t_mountp->m_active_trans);
306 return ntp;
307}
308
309/*
310 * This is called to reserve free disk blocks and log space for the
311 * given transaction. This must be done before allocating any resources
312 * within the transaction.
313 *
314 * This will return ENOSPC if there are not enough blocks available.
315 * It will sleep waiting for available log space.
316 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
317 * is used by long running transactions. If any one of the reservations
318 * fails then they will all be backed out.
319 *
320 * This does not do quota reservations. That typically is done by the
321 * caller afterwards.
322 */
323int
324xfs_trans_reserve(
325 xfs_trans_t *tp,
326 uint blocks,
327 uint logspace,
328 uint rtextents,
329 uint flags,
330 uint logcount)
331{
332 int log_flags;
Nathan Scott59c1b082006-06-09 14:59:13 +1000333 int error = 0;
334 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000337 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /*
340 * Attempt to reserve the needed disk blocks by decrementing
341 * the number needed from the number available. This will
342 * fail if the count would go below zero.
343 */
344 if (blocks > 0) {
345 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100346 -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000348 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return (XFS_ERROR(ENOSPC));
350 }
351 tp->t_blk_res += blocks;
352 }
353
354 /*
355 * Reserve the log space needed for this transaction.
356 */
357 if (logspace > 0) {
358 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
359 ASSERT((tp->t_log_count == 0) ||
360 (tp->t_log_count == logcount));
361 if (flags & XFS_TRANS_PERM_LOG_RES) {
362 log_flags = XFS_LOG_PERM_RESERV;
363 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
364 } else {
365 ASSERT(tp->t_ticket == NULL);
366 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
367 log_flags = 0;
368 }
369
370 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
371 &tp->t_ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000372 XFS_TRANSACTION, log_flags, tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (error) {
374 goto undo_blocks;
375 }
376 tp->t_log_res = logspace;
377 tp->t_log_count = logcount;
378 }
379
380 /*
381 * Attempt to reserve the needed realtime extents by decrementing
382 * the number needed from the number available. This will
383 * fail if the count would go below zero.
384 */
385 if (rtextents > 0) {
386 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100387 -((int64_t)rtextents), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (error) {
389 error = XFS_ERROR(ENOSPC);
390 goto undo_log;
391 }
392 tp->t_rtx_res += rtextents;
393 }
394
395 return 0;
396
397 /*
398 * Error cases jump to one of these labels to undo any
399 * reservations which have already been performed.
400 */
401undo_log:
402 if (logspace > 0) {
403 if (flags & XFS_TRANS_PERM_LOG_RES) {
404 log_flags = XFS_LOG_REL_PERM_RESERV;
405 } else {
406 log_flags = 0;
407 }
408 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
409 tp->t_ticket = NULL;
410 tp->t_log_res = 0;
411 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
412 }
413
414undo_blocks:
415 if (blocks > 0) {
416 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100417 (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 tp->t_blk_res = 0;
419 }
420
Nathan Scott59c1b082006-06-09 14:59:13 +1000421 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Nathan Scott59c1b082006-06-09 14:59:13 +1000423 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426
427/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * Record the indicated change to the given field for application
429 * to the file system's superblock when the transaction commits.
430 * For now, just store the change in the transaction structure.
431 *
432 * Mark the transaction structure to indicate that the superblock
433 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000434 *
435 * Because we may not be keeping track of allocated/free inodes and
436 * used filesystem blocks in the superblock, we do not mark the
437 * superblock dirty in this transaction if we modify these fields.
438 * We still need to update the transaction deltas so that they get
439 * applied to the incore superblock, but we don't want them to
440 * cause the superblock to get locked and logged if these are the
441 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
443void
444xfs_trans_mod_sb(
445 xfs_trans_t *tp,
446 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100447 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
David Chinner92821e22007-05-24 15:26:31 +1000449 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
450 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 switch (field) {
453 case XFS_TRANS_SB_ICOUNT:
454 tp->t_icount_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_IFREE:
459 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000460 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
461 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
463 case XFS_TRANS_SB_FDBLOCKS:
464 /*
465 * Track the number of blocks allocated in the
466 * transaction. Make sure it does not exceed the
467 * number reserved.
468 */
469 if (delta < 0) {
470 tp->t_blk_res_used += (uint)-delta;
471 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
472 }
473 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000474 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
475 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case XFS_TRANS_SB_RES_FDBLOCKS:
478 /*
479 * The allocation has already been applied to the
480 * in-core superblock's counter. This should only
481 * be applied to the on-disk superblock.
482 */
483 ASSERT(delta < 0);
484 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000485 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
486 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 break;
488 case XFS_TRANS_SB_FREXTENTS:
489 /*
490 * Track the number of blocks allocated in the
491 * transaction. Make sure it does not exceed the
492 * number reserved.
493 */
494 if (delta < 0) {
495 tp->t_rtx_res_used += (uint)-delta;
496 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
497 }
498 tp->t_frextents_delta += delta;
499 break;
500 case XFS_TRANS_SB_RES_FREXTENTS:
501 /*
502 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000503 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * be applied to the on-disk superblock.
505 */
506 ASSERT(delta < 0);
507 tp->t_res_frextents_delta += delta;
508 break;
509 case XFS_TRANS_SB_DBLOCKS:
510 ASSERT(delta > 0);
511 tp->t_dblocks_delta += delta;
512 break;
513 case XFS_TRANS_SB_AGCOUNT:
514 ASSERT(delta > 0);
515 tp->t_agcount_delta += delta;
516 break;
517 case XFS_TRANS_SB_IMAXPCT:
518 tp->t_imaxpct_delta += delta;
519 break;
520 case XFS_TRANS_SB_REXTSIZE:
521 tp->t_rextsize_delta += delta;
522 break;
523 case XFS_TRANS_SB_RBMBLOCKS:
524 tp->t_rbmblocks_delta += delta;
525 break;
526 case XFS_TRANS_SB_RBLOCKS:
527 tp->t_rblocks_delta += delta;
528 break;
529 case XFS_TRANS_SB_REXTENTS:
530 tp->t_rextents_delta += delta;
531 break;
532 case XFS_TRANS_SB_REXTSLOG:
533 tp->t_rextslog_delta += delta;
534 break;
535 default:
536 ASSERT(0);
537 return;
538 }
539
David Chinner210c6f12007-05-24 15:26:51 +1000540 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/*
544 * xfs_trans_apply_sb_deltas() is called from the commit code
545 * to bring the superblock buffer into the current transaction
546 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
547 *
548 * For now we just look at each field allowed to change and change
549 * it if necessary.
550 */
551STATIC void
552xfs_trans_apply_sb_deltas(
553 xfs_trans_t *tp)
554{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000555 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xfs_buf_t *bp;
557 int whole = 0;
558
559 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
560 sbp = XFS_BUF_TO_SBP(bp);
561
562 /*
563 * Check that superblock mods match the mods made to AGF counters.
564 */
565 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
566 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
567 tp->t_ag_btree_delta));
568
David Chinner92821e22007-05-24 15:26:31 +1000569 /*
570 * Only update the superblock counters if we are logging them
571 */
572 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000573 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800574 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000575 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800576 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000577 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800578 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000579 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800580 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000583 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800584 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000585 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800586 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000587
588 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800589 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 whole = 1;
591 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000592 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800593 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 whole = 1;
595 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000596 if (tp->t_imaxpct_delta) {
597 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 whole = 1;
599 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000600 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800601 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 whole = 1;
603 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000604 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800605 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 whole = 1;
607 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000608 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800609 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 whole = 1;
611 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000612 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800613 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 whole = 1;
615 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000616 if (tp->t_rextslog_delta) {
617 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 whole = 1;
619 }
620
621 if (whole)
622 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000623 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000625 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 else
627 /*
628 * Since all the modifiable fields are contiguous, we
629 * can get away with this.
630 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000631 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
632 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 sizeof(sbp->sb_frextents) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636/*
David Chinner45c34142007-06-18 16:49:44 +1000637 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
638 * and apply superblock counter changes to the in-core superblock. The
639 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
640 * applied to the in-core superblock. The idea is that that has already been
641 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
David Chinner45c34142007-06-18 16:49:44 +1000644 * However, we have to ensure that we only modify each superblock field only
645 * once because the application of the delta values may not be atomic. That can
646 * lead to ENOSPC races occurring if we have two separate modifcations of the
647 * free space counter to put back the entire reservation and then take away
648 * what we used.
649 *
650 * If we are not logging superblock counters, then the inode allocated/free and
651 * used block counts are not updated in the on disk superblock. In this case,
652 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
653 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000655STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656xfs_trans_unreserve_and_mod_sb(
657 xfs_trans_t *tp)
658{
659 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
660 xfs_mod_sb_t *msbp;
David Chinner92821e22007-05-24 15:26:31 +1000661 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /* REFERENCED */
663 int error;
664 int rsvd;
David Chinner45c34142007-06-18 16:49:44 +1000665 int64_t blkdelta = 0;
666 int64_t rtxdelta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 msbp = msb;
669 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
670
David Chinner45c34142007-06-18 16:49:44 +1000671 /* calculate free blocks delta */
672 if (tp->t_blk_res > 0)
673 blkdelta = tp->t_blk_res;
674
675 if ((tp->t_fdblocks_delta != 0) &&
676 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
677 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
678 blkdelta += tp->t_fdblocks_delta;
679
680 if (blkdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 msbp->msb_field = XFS_SBS_FDBLOCKS;
David Chinner45c34142007-06-18 16:49:44 +1000682 msbp->msb_delta = blkdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 msbp++;
684 }
685
David Chinner45c34142007-06-18 16:49:44 +1000686 /* calculate free realtime extents delta */
687 if (tp->t_rtx_res > 0)
688 rtxdelta = tp->t_rtx_res;
689
690 if ((tp->t_frextents_delta != 0) &&
691 (tp->t_flags & XFS_TRANS_SB_DIRTY))
692 rtxdelta += tp->t_frextents_delta;
693
694 if (rtxdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 msbp->msb_field = XFS_SBS_FREXTENTS;
David Chinner45c34142007-06-18 16:49:44 +1000696 msbp->msb_delta = rtxdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 msbp++;
698 }
699
David Chinner45c34142007-06-18 16:49:44 +1000700 /* apply remaining deltas */
701
David Chinner92821e22007-05-24 15:26:31 +1000702 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
703 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (tp->t_icount_delta != 0) {
705 msbp->msb_field = XFS_SBS_ICOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100706 msbp->msb_delta = tp->t_icount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 msbp++;
708 }
709 if (tp->t_ifree_delta != 0) {
710 msbp->msb_field = XFS_SBS_IFREE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100711 msbp->msb_delta = tp->t_ifree_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 msbp++;
713 }
David Chinner92821e22007-05-24 15:26:31 +1000714 }
715
716 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (tp->t_dblocks_delta != 0) {
718 msbp->msb_field = XFS_SBS_DBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100719 msbp->msb_delta = tp->t_dblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 msbp++;
721 }
722 if (tp->t_agcount_delta != 0) {
723 msbp->msb_field = XFS_SBS_AGCOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100724 msbp->msb_delta = tp->t_agcount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 msbp++;
726 }
727 if (tp->t_imaxpct_delta != 0) {
728 msbp->msb_field = XFS_SBS_IMAX_PCT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100729 msbp->msb_delta = tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 msbp++;
731 }
732 if (tp->t_rextsize_delta != 0) {
733 msbp->msb_field = XFS_SBS_REXTSIZE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100734 msbp->msb_delta = tp->t_rextsize_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 msbp++;
736 }
737 if (tp->t_rbmblocks_delta != 0) {
738 msbp->msb_field = XFS_SBS_RBMBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100739 msbp->msb_delta = tp->t_rbmblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 msbp++;
741 }
742 if (tp->t_rblocks_delta != 0) {
743 msbp->msb_field = XFS_SBS_RBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100744 msbp->msb_delta = tp->t_rblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 msbp++;
746 }
747 if (tp->t_rextents_delta != 0) {
748 msbp->msb_field = XFS_SBS_REXTENTS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100749 msbp->msb_delta = tp->t_rextents_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 msbp++;
751 }
752 if (tp->t_rextslog_delta != 0) {
753 msbp->msb_field = XFS_SBS_REXTSLOG;
David Chinner20f4ebf2007-02-10 18:36:10 +1100754 msbp->msb_delta = tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 msbp++;
756 }
757 }
758
759 /*
760 * If we need to change anything, do it.
761 */
762 if (msbp > msb) {
763 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
764 (uint)(msbp - msb), rsvd);
765 ASSERT(error == 0);
766 }
767}
768
Dave Chinner09243782010-03-08 11:28:28 +1100769/*
770 * Total up the number of log iovecs needed to commit this
771 * transaction. The transaction itself needs one for the
772 * transaction header. Ask each dirty item in turn how many
773 * it needs to get the total.
774 */
775static uint
776xfs_trans_count_vecs(
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100777 struct xfs_trans *tp)
Dave Chinner09243782010-03-08 11:28:28 +1100778{
779 int nvecs;
780 xfs_log_item_desc_t *lidp;
781
782 nvecs = 1;
783 lidp = xfs_trans_first_item(tp);
784 ASSERT(lidp != NULL);
785
786 /* In the non-debug case we need to start bailing out if we
787 * didn't find a log_item here, return zero and let trans_commit
788 * deal with it.
789 */
790 if (lidp == NULL)
791 return 0;
792
793 while (lidp != NULL) {
794 /*
795 * Skip items which aren't dirty in this transaction.
796 */
797 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
798 lidp = xfs_trans_next_item(tp, lidp);
799 continue;
800 }
801 lidp->lid_size = IOP_SIZE(lidp->lid_item);
802 nvecs += lidp->lid_size;
803 lidp = xfs_trans_next_item(tp, lidp);
804 }
805
806 return nvecs;
807}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/*
Dave Chinner09243782010-03-08 11:28:28 +1100810 * Fill in the vector with pointers to data to be logged
811 * by this transaction. The transaction header takes
812 * the first vector, and then each dirty item takes the
813 * number of vectors it indicated it needed in xfs_trans_count_vecs().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
Dave Chinner09243782010-03-08 11:28:28 +1100815 * As each item fills in the entries it needs, also pin the item
816 * so that it cannot be flushed out until the log write completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Dave Chinner09243782010-03-08 11:28:28 +1100818static void
819xfs_trans_fill_vecs(
820 struct xfs_trans *tp,
821 struct xfs_log_iovec *log_vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Dave Chinner09243782010-03-08 11:28:28 +1100823 xfs_log_item_desc_t *lidp;
824 struct xfs_log_iovec *vecp;
825 uint nitems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /*
Dave Chinner09243782010-03-08 11:28:28 +1100828 * Skip over the entry for the transaction header, we'll
829 * fill that in at the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
Dave Chinner09243782010-03-08 11:28:28 +1100831 vecp = log_vector + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dave Chinner09243782010-03-08 11:28:28 +1100833 nitems = 0;
834 lidp = xfs_trans_first_item(tp);
835 ASSERT(lidp);
836 while (lidp) {
837 /* Skip items which aren't dirty in this transaction. */
838 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
839 lidp = xfs_trans_next_item(tp, lidp);
840 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Dave Chinner09243782010-03-08 11:28:28 +1100842
843 /*
844 * The item may be marked dirty but not log anything. This can
845 * be used to get called when a transaction is committed.
846 */
847 if (lidp->lid_size)
848 nitems++;
849 IOP_FORMAT(lidp->lid_item, vecp);
850 vecp += lidp->lid_size;
851 IOP_PIN(lidp->lid_item);
852 lidp = xfs_trans_next_item(tp, lidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /*
Dave Chinner09243782010-03-08 11:28:28 +1100856 * Now that we've counted the number of items in this transaction, fill
857 * in the transaction header. Note that the transaction header does not
858 * have a log item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Dave Chinner09243782010-03-08 11:28:28 +1100860 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
861 tp->t_header.th_type = tp->t_type;
862 tp->t_header.th_num_items = nitems;
863 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
864 log_vector->i_len = sizeof(xfs_trans_header_t);
865 log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
866}
867
868/*
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100869 * The committed item processing consists of calling the committed routine of
870 * each logged item, updating the item's position in the AIL if necessary, and
871 * unpinning each item. If the committed routine returns -1, then do nothing
872 * further with the item because it may have been freed.
873 *
874 * Since items are unlocked when they are copied to the incore log, it is
875 * possible for two transactions to be completing and manipulating the same
876 * item simultaneously. The AIL lock will protect the lsn field of each item.
877 * The value of this field can never go backwards.
878 *
879 * We unpin the items after repositioning them in the AIL, because otherwise
880 * they could be immediately flushed and we'd have to race with the flusher
881 * trying to pull the item from the AIL as we add it.
882 */
883static void
884xfs_trans_item_committed(
885 struct xfs_log_item *lip,
886 xfs_lsn_t commit_lsn,
887 int aborted)
888{
889 xfs_lsn_t item_lsn;
890 struct xfs_ail *ailp;
891
892 if (aborted)
893 lip->li_flags |= XFS_LI_ABORTED;
894 item_lsn = IOP_COMMITTED(lip, commit_lsn);
895
896 /* If the committed routine returns -1, item has been freed. */
897 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
898 return;
899
900 /*
901 * If the returned lsn is greater than what it contained before, update
902 * the location of the item in the AIL. If it is not, then do nothing.
903 * Items can never move backwards in the AIL.
904 *
905 * While the new lsn should usually be greater, it is possible that a
906 * later transaction completing simultaneously with an earlier one
907 * using the same item could complete first with a higher lsn. This
908 * would cause the earlier transaction to fail the test below.
909 */
910 ailp = lip->li_ailp;
911 spin_lock(&ailp->xa_lock);
912 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
913 /*
914 * This will set the item's lsn to item_lsn and update the
915 * position of the item in the AIL.
916 *
917 * xfs_trans_ail_update() drops the AIL lock.
918 */
919 xfs_trans_ail_update(ailp, lip, item_lsn);
920 } else {
921 spin_unlock(&ailp->xa_lock);
922 }
923
924 /*
925 * Now that we've repositioned the item in the AIL, unpin it so it can
926 * be flushed. Pass information about buffer stale state down from the
927 * log item flags, if anyone else stales the buffer we do not want to
928 * pay any attention to it.
929 */
930 IOP_UNPIN(lip);
931}
932
933/* Clear all the per-AG busy list items listed in this transaction */
934static void
935xfs_trans_clear_busy_extents(
936 struct xfs_trans *tp)
937{
938 xfs_log_busy_chunk_t *lbcp;
939 xfs_log_busy_slot_t *lbsp;
940 int i;
941
942 for (lbcp = &tp->t_busy; lbcp != NULL; lbcp = lbcp->lbc_next) {
943 i = 0;
944 for (lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
945 if (XFS_LBC_ISFREE(lbcp, i))
946 continue;
947 xfs_alloc_clear_busy(tp, lbsp->lbc_ag, lbsp->lbc_idx);
948 }
949 }
950 xfs_trans_free_busy(tp);
951}
952
953/*
954 * This is typically called by the LM when a transaction has been fully
955 * committed to disk. It needs to unpin the items which have
956 * been logged by the transaction and update their positions
957 * in the AIL if necessary.
958 *
959 * This also gets called when the transactions didn't get written out
960 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
961 */
962STATIC void
963xfs_trans_committed(
964 struct xfs_trans *tp,
965 int abortflag)
966{
967 xfs_log_item_desc_t *lidp;
968 xfs_log_item_chunk_t *licp;
969 xfs_log_item_chunk_t *next_licp;
970
971 /* Call the transaction's completion callback if there is one. */
972 if (tp->t_callback != NULL)
973 tp->t_callback(tp, tp->t_callarg);
974
975 for (lidp = xfs_trans_first_item(tp);
976 lidp != NULL;
977 lidp = xfs_trans_next_item(tp, lidp)) {
978 xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
979 }
980
981 /* free the item chunks, ignoring the embedded chunk */
982 for (licp = tp->t_items.lic_next; licp != NULL; licp = next_licp) {
983 next_licp = licp->lic_next;
984 kmem_free(licp);
985 }
986
987 xfs_trans_clear_busy_extents(tp);
988 xfs_trans_free(tp);
989}
990
991/*
992 * Called from the trans_commit code when we notice that
993 * the filesystem is in the middle of a forced shutdown.
994 */
995STATIC void
996xfs_trans_uncommit(
997 struct xfs_trans *tp,
998 uint flags)
999{
1000 xfs_log_item_desc_t *lidp;
1001
1002 for (lidp = xfs_trans_first_item(tp);
1003 lidp != NULL;
1004 lidp = xfs_trans_next_item(tp, lidp)) {
1005 /*
1006 * Unpin all but those that aren't dirty.
1007 */
1008 if (lidp->lid_flags & XFS_LID_DIRTY)
1009 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1010 }
1011
1012 xfs_trans_unreserve_and_mod_sb(tp);
1013 xfs_trans_unreserve_and_mod_dquots(tp);
1014
1015 xfs_trans_free_items(tp, flags);
1016 xfs_trans_free_busy(tp);
1017 xfs_trans_free(tp);
1018}
1019
1020/*
Dave Chinner09243782010-03-08 11:28:28 +11001021 * Format the transaction direct to the iclog. This isolates the physical
1022 * transaction commit operation from the logical operation and hence allows
1023 * other methods to be introduced without affecting the existing commit path.
1024 */
1025static int
1026xfs_trans_commit_iclog(
1027 struct xfs_mount *mp,
1028 struct xfs_trans *tp,
1029 xfs_lsn_t *commit_lsn,
1030 int flags)
1031{
1032 int shutdown;
1033 int error;
1034 int log_flags = 0;
1035 struct xlog_in_core *commit_iclog;
1036#define XFS_TRANS_LOGVEC_COUNT 16
1037 struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
1038 struct xfs_log_iovec *log_vector;
1039 uint nvec;
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /*
1043 * Ask each log item how many log_vector entries it will
1044 * need so we can figure out how many to allocate.
1045 * Try to avoid the kmem_alloc() call in the common case
1046 * by using a vector from the stack when it fits.
1047 */
1048 nvec = xfs_trans_count_vecs(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (nvec == 0) {
Dave Chinner09243782010-03-08 11:28:28 +11001050 return ENOMEM; /* triggers a shutdown! */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001051 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 log_vector = log_vector_fast;
1053 } else {
1054 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
1055 sizeof(xfs_log_iovec_t),
1056 KM_SLEEP);
1057 }
1058
1059 /*
1060 * Fill in the log_vector and pin the logged items, and
1061 * then write the transaction to the log.
1062 */
1063 xfs_trans_fill_vecs(tp, log_vector);
1064
Dave Chinner09243782010-03-08 11:28:28 +11001065 if (flags & XFS_TRANS_RELEASE_LOG_RES)
1066 log_flags = XFS_LOG_REL_PERM_RESERV;
1067
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001068 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001071 * The transaction is committed incore here, and can go out to disk
1072 * at any time after this call. However, all the items associated
1073 * with the transaction are still locked and pinned in memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Dave Chinner09243782010-03-08 11:28:28 +11001075 *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Dave Chinner09243782010-03-08 11:28:28 +11001077 tp->t_commit_lsn = *commit_lsn;
1078 if (nvec > XFS_TRANS_LOGVEC_COUNT)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001079 kmem_free(log_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * If we got a log write error. Unpin the logitems that we
1083 * had pinned, clean up, free trans structure, and return error.
1084 */
Dave Chinner09243782010-03-08 11:28:28 +11001085 if (error || *commit_lsn == -1) {
Nathan Scott59c1b082006-06-09 14:59:13 +10001086 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
1088 return XFS_ERROR(EIO);
1089 }
1090
1091 /*
1092 * Once the transaction has committed, unused
1093 * reservations need to be released and changes to
1094 * the superblock need to be reflected in the in-core
1095 * version. Do that now.
1096 */
1097 xfs_trans_unreserve_and_mod_sb(tp);
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /*
1100 * Tell the LM to call the transaction completion routine
1101 * when the log write with LSN commit_lsn completes (e.g.
1102 * when the transaction commit really hits the on-disk log).
1103 * After this call we cannot reference tp, because the call
1104 * can happen at any time and the call will free the transaction
1105 * structure pointed to by tp. The only case where we call
1106 * the completion routine (xfs_trans_committed) directly is
1107 * if the log is turned off on a debug kernel or we're
1108 * running in simulation mode (the log is explicitly turned
1109 * off).
1110 */
1111 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
1112 tp->t_logcb.cb_arg = tp;
1113
1114 /*
1115 * We need to pass the iclog buffer which was used for the
1116 * transaction commit record into this function, and attach
1117 * the callback to it. The callback must be attached before
1118 * the items are unlocked to avoid racing with other threads
1119 * waiting for an item to unlock.
1120 */
1121 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
1122
1123 /*
1124 * Mark this thread as no longer being in a transaction
1125 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001126 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
1129 * Once all the items of the transaction have been copied
1130 * to the in core log and the callback is attached, the
1131 * items can be unlocked.
1132 *
1133 * This will free descriptors pointing to items which were
1134 * not logged since there is nothing more to do with them.
1135 * For items which were logged, we will keep pointers to them
1136 * so they can be unpinned after the transaction commits to disk.
1137 * This will also stamp each modified meta-data item with
1138 * the commit lsn of this transaction for dependency tracking
1139 * purposes.
1140 */
Dave Chinner09243782010-03-08 11:28:28 +11001141 xfs_trans_unlock_items(tp, *commit_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /*
1144 * If we detected a log error earlier, finish committing
1145 * the transaction now (unpin log items, etc).
1146 *
1147 * Order is critical here, to avoid using the transaction
1148 * pointer after its been freed (by xfs_trans_committed
1149 * either here now, or as a callback). We cannot do this
1150 * step inside xfs_log_notify as was done earlier because
1151 * of this issue.
1152 */
1153 if (shutdown)
1154 xfs_trans_committed(tp, XFS_LI_ABORTED);
1155
1156 /*
1157 * Now that the xfs_trans_committed callback has been attached,
1158 * and the items are released we can finally allow the iclog to
1159 * go to disk.
1160 */
Dave Chinner09243782010-03-08 11:28:28 +11001161 return xfs_log_release_iclog(mp, commit_iclog);
1162}
1163
1164
1165/*
1166 * xfs_trans_commit
1167 *
1168 * Commit the given transaction to the log a/synchronously.
1169 *
1170 * XFS disk error handling mechanism is not based on a typical
1171 * transaction abort mechanism. Logically after the filesystem
1172 * gets marked 'SHUTDOWN', we can't let any new transactions
1173 * be durable - ie. committed to disk - because some metadata might
1174 * be inconsistent. In such cases, this returns an error, and the
1175 * caller may assume that all locked objects joined to the transaction
1176 * have already been unlocked as if the commit had succeeded.
1177 * Do not reference the transaction structure after this call.
1178 */
Dave Chinner09243782010-03-08 11:28:28 +11001179int
1180_xfs_trans_commit(
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001181 struct xfs_trans *tp,
1182 uint flags,
1183 int *log_flushed)
Dave Chinner09243782010-03-08 11:28:28 +11001184{
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001185 struct xfs_mount *mp = tp->t_mountp;
Dave Chinner09243782010-03-08 11:28:28 +11001186 xfs_lsn_t commit_lsn = -1;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001187 int error = 0;
Dave Chinner09243782010-03-08 11:28:28 +11001188 int log_flags = 0;
1189 int sync = tp->t_flags & XFS_TRANS_SYNC;
Dave Chinner09243782010-03-08 11:28:28 +11001190
1191 /*
1192 * Determine whether this commit is releasing a permanent
1193 * log reservation or not.
1194 */
1195 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1196 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1197 log_flags = XFS_LOG_REL_PERM_RESERV;
1198 }
1199
1200 /*
1201 * If there is nothing to be logged by the transaction,
1202 * then unlock all of the items associated with the
1203 * transaction and free the transaction structure.
1204 * Also make sure to return any reserved blocks to
1205 * the free pool.
1206 */
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001207 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1208 goto out_unreserve;
1209
1210 if (XFS_FORCED_SHUTDOWN(mp)) {
1211 error = XFS_ERROR(EIO);
1212 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001213 }
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001214
Dave Chinner09243782010-03-08 11:28:28 +11001215 ASSERT(tp->t_ticket != NULL);
1216
1217 /*
1218 * If we need to update the superblock, then do it now.
1219 */
1220 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1221 xfs_trans_apply_sb_deltas(tp);
1222 xfs_trans_apply_dquot_deltas(tp);
1223
1224 error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
1225 if (error == ENOMEM) {
1226 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001227 error = XFS_ERROR(EIO);
1228 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /*
1232 * If the transaction needs to be synchronous, then force the
1233 * log out now and wait for it.
1234 */
1235 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001236 if (!error) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001237 error = _xfs_log_force_lsn(mp, commit_lsn,
1238 XFS_LOG_SYNC, log_flushed);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 XFS_STATS_INC(xs_trans_sync);
1241 } else {
1242 XFS_STATS_INC(xs_trans_async);
1243 }
1244
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001245 return error;
1246
1247out_unreserve:
1248 xfs_trans_unreserve_and_mod_sb(tp);
1249
1250 /*
1251 * It is indeed possible for the transaction to be not dirty but
1252 * the dqinfo portion to be. All that means is that we have some
1253 * (non-persistent) quota reservations that need to be unreserved.
1254 */
1255 xfs_trans_unreserve_and_mod_dquots(tp);
1256 if (tp->t_ticket) {
1257 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1258 if (commit_lsn == -1 && !error)
1259 error = XFS_ERROR(EIO);
1260 }
1261 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1262 xfs_trans_free_items(tp, error ? XFS_TRANS_ABORT : 0);
1263 xfs_trans_free_busy(tp);
1264 xfs_trans_free(tp);
1265
1266 XFS_STATS_INC(xs_trans_empty);
1267 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * Unlock all of the transaction's items and free the transaction.
1272 * The transaction must not have modified any of its items, because
1273 * there is no way to restore them to their previous state.
1274 *
1275 * If the transaction has made a log reservation, make sure to release
1276 * it as well.
1277 */
1278void
1279xfs_trans_cancel(
1280 xfs_trans_t *tp,
1281 int flags)
1282{
1283 int log_flags;
1284#ifdef DEBUG
1285 xfs_log_item_chunk_t *licp;
1286 xfs_log_item_desc_t *lidp;
1287 xfs_log_item_t *lip;
1288 int i;
1289#endif
Ryan Hankins0733af22006-01-11 15:36:44 +11001290 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /*
1293 * See if the caller is being too lazy to figure out if
1294 * the transaction really needs an abort.
1295 */
1296 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1297 flags &= ~XFS_TRANS_ABORT;
1298 /*
1299 * See if the caller is relying on us to shut down the
1300 * filesystem. This happens in paths where we detect
1301 * corruption and decide to give up.
1302 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001303 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001304 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001305 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307#ifdef DEBUG
1308 if (!(flags & XFS_TRANS_ABORT)) {
1309 licp = &(tp->t_items);
1310 while (licp != NULL) {
1311 lidp = licp->lic_descs;
1312 for (i = 0; i < licp->lic_unused; i++, lidp++) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10001313 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 continue;
1315 }
1316
1317 lip = lidp->lid_item;
Ryan Hankins0733af22006-01-11 15:36:44 +11001318 if (!XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ASSERT(!(lip->li_type == XFS_LI_EFD));
1320 }
1321 licp = licp->lic_next;
1322 }
1323 }
1324#endif
1325 xfs_trans_unreserve_and_mod_sb(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001326 xfs_trans_unreserve_and_mod_dquots(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (tp->t_ticket) {
1329 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1330 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1331 log_flags = XFS_LOG_REL_PERM_RESERV;
1332 } else {
1333 log_flags = 0;
1334 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001335 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
1338 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001339 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 xfs_trans_free_items(tp, flags);
1342 xfs_trans_free_busy(tp);
1343 xfs_trans_free(tp);
1344}
1345
Niv Sardi322ff6b2008-08-13 16:05:49 +10001346/*
1347 * Roll from one trans in the sequence of PERMANENT transactions to
1348 * the next: permanent transactions are only flushed out when
1349 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1350 * as possible to let chunks of it go to the log. So we commit the
1351 * chunk we've been working on and get a new transaction to continue.
1352 */
1353int
1354xfs_trans_roll(
1355 struct xfs_trans **tpp,
1356 struct xfs_inode *dp)
1357{
1358 struct xfs_trans *trans;
1359 unsigned int logres, count;
1360 int error;
1361
1362 /*
1363 * Ensure that the inode is always logged.
1364 */
1365 trans = *tpp;
1366 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1367
1368 /*
1369 * Copy the critical parameters from one trans to the next.
1370 */
1371 logres = trans->t_log_res;
1372 count = trans->t_log_count;
1373 *tpp = xfs_trans_dup(trans);
1374
1375 /*
1376 * Commit the current transaction.
1377 * If this commit failed, then it'd just unlock those items that
1378 * are not marked ihold. That also means that a filesystem shutdown
1379 * is in progress. The caller takes the responsibility to cancel
1380 * the duplicate transaction that gets returned.
1381 */
1382 error = xfs_trans_commit(trans, 0);
1383 if (error)
1384 return (error);
1385
1386 trans = *tpp;
1387
1388 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001389 * transaction commit worked ok so we can drop the extra ticket
1390 * reference that we gained in xfs_trans_dup()
1391 */
1392 xfs_log_ticket_put(trans->t_ticket);
1393
1394
1395 /*
Niv Sardi322ff6b2008-08-13 16:05:49 +10001396 * Reserve space in the log for th next transaction.
1397 * This also pushes items in the "AIL", the list of logged items,
1398 * out to disk if they are taking up space at the tail of the log
1399 * that we want to use. This requires that either nothing be locked
1400 * across this call, or that anything that is locked be logged in
1401 * the prior and the next transactions.
1402 */
1403 error = xfs_trans_reserve(trans, 0, logres, 0,
1404 XFS_TRANS_PERM_LOG_RES, count);
1405 /*
1406 * Ensure that the inode is in the new transaction and locked.
1407 */
1408 if (error)
1409 return error;
1410
1411 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1412 xfs_trans_ihold(trans, dp);
1413 return 0;
1414}