blob: 140386434aa3ec85d32528274e3add8d8be99cae [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"
46
47
48STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *);
49STATIC uint xfs_trans_count_vecs(xfs_trans_t *);
50STATIC void xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *);
51STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
52STATIC void xfs_trans_committed(xfs_trans_t *, int);
53STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
54STATIC void xfs_trans_free(xfs_trans_t *);
55
Nathan Scott8f794052006-03-14 13:32:41 +110056kmem_zone_t *xfs_trans_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58
59/*
Nathan Scott8f794052006-03-14 13:32:41 +110060 * Reservation functions here avoid a huge stack in xfs_trans_init
61 * due to register overflow from temporaries in the calculations.
62 */
63
64STATIC uint
65xfs_calc_write_reservation(xfs_mount_t *mp)
66{
67 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
68}
69
70STATIC uint
71xfs_calc_itruncate_reservation(xfs_mount_t *mp)
72{
73 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
74}
75
76STATIC uint
77xfs_calc_rename_reservation(xfs_mount_t *mp)
78{
79 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
80}
81
82STATIC uint
83xfs_calc_link_reservation(xfs_mount_t *mp)
84{
Nathan Scott2ddd5922006-03-17 17:25:46 +110085 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
Nathan Scott8f794052006-03-14 13:32:41 +110086}
87
88STATIC uint
89xfs_calc_remove_reservation(xfs_mount_t *mp)
90{
91 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
92}
93
94STATIC uint
95xfs_calc_symlink_reservation(xfs_mount_t *mp)
96{
97 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
98}
99
100STATIC uint
101xfs_calc_create_reservation(xfs_mount_t *mp)
102{
103 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
104}
105
106STATIC uint
107xfs_calc_mkdir_reservation(xfs_mount_t *mp)
108{
109 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
110}
111
112STATIC uint
113xfs_calc_ifree_reservation(xfs_mount_t *mp)
114{
115 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
116}
117
118STATIC uint
119xfs_calc_ichange_reservation(xfs_mount_t *mp)
120{
121 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
122}
123
124STATIC uint
125xfs_calc_growdata_reservation(xfs_mount_t *mp)
126{
127 return XFS_CALC_GROWDATA_LOG_RES(mp);
128}
129
130STATIC uint
131xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
132{
133 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
134}
135
136STATIC uint
137xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
138{
139 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
140}
141
142STATIC uint
143xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
144{
145 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
146}
147
148STATIC uint
149xfs_calc_swrite_reservation(xfs_mount_t *mp)
150{
151 return XFS_CALC_SWRITE_LOG_RES(mp);
152}
153
154STATIC uint
155xfs_calc_writeid_reservation(xfs_mount_t *mp)
156{
157 return XFS_CALC_WRITEID_LOG_RES(mp);
158}
159
160STATIC uint
161xfs_calc_addafork_reservation(xfs_mount_t *mp)
162{
163 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
164}
165
166STATIC uint
167xfs_calc_attrinval_reservation(xfs_mount_t *mp)
168{
169 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
170}
171
172STATIC uint
173xfs_calc_attrset_reservation(xfs_mount_t *mp)
174{
175 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
176}
177
178STATIC uint
179xfs_calc_attrrm_reservation(xfs_mount_t *mp)
180{
181 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
182}
183
184STATIC uint
185xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
186{
187 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
188}
189
190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Initialize the precomputed transaction reservation values
192 * in the mount structure.
193 */
194void
195xfs_trans_init(
196 xfs_mount_t *mp)
197{
198 xfs_trans_reservations_t *resp;
199
200 resp = &(mp->m_reservations);
Nathan Scott8f794052006-03-14 13:32:41 +1100201 resp->tr_write = xfs_calc_write_reservation(mp);
202 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
203 resp->tr_rename = xfs_calc_rename_reservation(mp);
204 resp->tr_link = xfs_calc_link_reservation(mp);
205 resp->tr_remove = xfs_calc_remove_reservation(mp);
206 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
207 resp->tr_create = xfs_calc_create_reservation(mp);
208 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
209 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
210 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
211 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
212 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
213 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
214 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
215 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
216 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
217 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
218 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
219 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
220 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
221 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
225 * This routine is called to allocate a transaction structure.
226 * The type parameter indicates the type of the transaction. These
227 * are enumerated in xfs_trans.h.
228 *
229 * Dynamically allocate the transaction structure from the transaction
230 * zone, initialize it, and return it to the caller.
231 */
232xfs_trans_t *
233xfs_trans_alloc(
234 xfs_mount_t *mp,
235 uint type)
236{
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000237 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
Nathan Scott34327e12006-06-09 17:11:55 +1000238 return _xfs_trans_alloc(mp, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241xfs_trans_t *
242_xfs_trans_alloc(
243 xfs_mount_t *mp,
244 uint type)
245{
246 xfs_trans_t *tp;
247
Nathan Scott34327e12006-06-09 17:11:55 +1000248 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Nathan Scott34327e12006-06-09 17:11:55 +1000250 tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 tp->t_magic = XFS_TRANS_MAGIC;
252 tp->t_type = type;
253 tp->t_mountp = mp;
254 tp->t_items_free = XFS_LIC_NUM_SLOTS;
255 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
256 XFS_LIC_INIT(&(tp->t_items));
257 XFS_LBC_INIT(&(tp->t_busy));
Nathan Scott34327e12006-06-09 17:11:55 +1000258 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/*
262 * This is called to create a new transaction which will share the
263 * permanent log reservation of the given transaction. The remaining
264 * unused block and rt extent reservations are also inherited. This
265 * implies that the original transaction is no longer allowed to allocate
266 * blocks. Locks and log items, however, are no inherited. They must
267 * be added to the new transaction explicitly.
268 */
269xfs_trans_t *
270xfs_trans_dup(
271 xfs_trans_t *tp)
272{
273 xfs_trans_t *ntp;
274
275 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
276
277 /*
278 * Initialize the new transaction structure.
279 */
280 ntp->t_magic = XFS_TRANS_MAGIC;
281 ntp->t_type = tp->t_type;
282 ntp->t_mountp = tp->t_mountp;
283 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
284 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
285 XFS_LIC_INIT(&(ntp->t_items));
286 XFS_LBC_INIT(&(ntp->t_busy));
287
288 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
292 ntp->t_ticket = tp->t_ticket;
293 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
294 tp->t_blk_res = tp->t_blk_res_used;
295 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
296 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000297 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp);
300
301 atomic_inc(&tp->t_mountp->m_active_trans);
302 return ntp;
303}
304
305/*
306 * This is called to reserve free disk blocks and log space for the
307 * given transaction. This must be done before allocating any resources
308 * within the transaction.
309 *
310 * This will return ENOSPC if there are not enough blocks available.
311 * It will sleep waiting for available log space.
312 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
313 * is used by long running transactions. If any one of the reservations
314 * fails then they will all be backed out.
315 *
316 * This does not do quota reservations. That typically is done by the
317 * caller afterwards.
318 */
319int
320xfs_trans_reserve(
321 xfs_trans_t *tp,
322 uint blocks,
323 uint logspace,
324 uint rtextents,
325 uint flags,
326 uint logcount)
327{
328 int log_flags;
Nathan Scott59c1b082006-06-09 14:59:13 +1000329 int error = 0;
330 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000333 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /*
336 * Attempt to reserve the needed disk blocks by decrementing
337 * the number needed from the number available. This will
338 * fail if the count would go below zero.
339 */
340 if (blocks > 0) {
341 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100342 -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000344 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return (XFS_ERROR(ENOSPC));
346 }
347 tp->t_blk_res += blocks;
348 }
349
350 /*
351 * Reserve the log space needed for this transaction.
352 */
353 if (logspace > 0) {
354 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
355 ASSERT((tp->t_log_count == 0) ||
356 (tp->t_log_count == logcount));
357 if (flags & XFS_TRANS_PERM_LOG_RES) {
358 log_flags = XFS_LOG_PERM_RESERV;
359 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
360 } else {
361 ASSERT(tp->t_ticket == NULL);
362 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
363 log_flags = 0;
364 }
365
366 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
367 &tp->t_ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000368 XFS_TRANSACTION, log_flags, tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (error) {
370 goto undo_blocks;
371 }
372 tp->t_log_res = logspace;
373 tp->t_log_count = logcount;
374 }
375
376 /*
377 * Attempt to reserve the needed realtime extents by decrementing
378 * the number needed from the number available. This will
379 * fail if the count would go below zero.
380 */
381 if (rtextents > 0) {
382 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100383 -((int64_t)rtextents), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (error) {
385 error = XFS_ERROR(ENOSPC);
386 goto undo_log;
387 }
388 tp->t_rtx_res += rtextents;
389 }
390
391 return 0;
392
393 /*
394 * Error cases jump to one of these labels to undo any
395 * reservations which have already been performed.
396 */
397undo_log:
398 if (logspace > 0) {
399 if (flags & XFS_TRANS_PERM_LOG_RES) {
400 log_flags = XFS_LOG_REL_PERM_RESERV;
401 } else {
402 log_flags = 0;
403 }
404 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
405 tp->t_ticket = NULL;
406 tp->t_log_res = 0;
407 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
408 }
409
410undo_blocks:
411 if (blocks > 0) {
412 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100413 (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 tp->t_blk_res = 0;
415 }
416
Nathan Scott59c1b082006-06-09 14:59:13 +1000417 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Nathan Scott59c1b082006-06-09 14:59:13 +1000419 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422
423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * Record the indicated change to the given field for application
425 * to the file system's superblock when the transaction commits.
426 * For now, just store the change in the transaction structure.
427 *
428 * Mark the transaction structure to indicate that the superblock
429 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000430 *
431 * Because we may not be keeping track of allocated/free inodes and
432 * used filesystem blocks in the superblock, we do not mark the
433 * superblock dirty in this transaction if we modify these fields.
434 * We still need to update the transaction deltas so that they get
435 * applied to the incore superblock, but we don't want them to
436 * cause the superblock to get locked and logged if these are the
437 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
439void
440xfs_trans_mod_sb(
441 xfs_trans_t *tp,
442 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100443 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
David Chinner92821e22007-05-24 15:26:31 +1000445 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
446 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 switch (field) {
449 case XFS_TRANS_SB_ICOUNT:
450 tp->t_icount_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000451 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
452 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454 case XFS_TRANS_SB_IFREE:
455 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000456 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
457 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
459 case XFS_TRANS_SB_FDBLOCKS:
460 /*
461 * Track the number of blocks allocated in the
462 * transaction. Make sure it does not exceed the
463 * number reserved.
464 */
465 if (delta < 0) {
466 tp->t_blk_res_used += (uint)-delta;
467 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
468 }
469 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000470 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
471 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case XFS_TRANS_SB_RES_FDBLOCKS:
474 /*
475 * The allocation has already been applied to the
476 * in-core superblock's counter. This should only
477 * be applied to the on-disk superblock.
478 */
479 ASSERT(delta < 0);
480 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000481 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
482 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case XFS_TRANS_SB_FREXTENTS:
485 /*
486 * Track the number of blocks allocated in the
487 * transaction. Make sure it does not exceed the
488 * number reserved.
489 */
490 if (delta < 0) {
491 tp->t_rtx_res_used += (uint)-delta;
492 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
493 }
494 tp->t_frextents_delta += delta;
495 break;
496 case XFS_TRANS_SB_RES_FREXTENTS:
497 /*
498 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000499 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * be applied to the on-disk superblock.
501 */
502 ASSERT(delta < 0);
503 tp->t_res_frextents_delta += delta;
504 break;
505 case XFS_TRANS_SB_DBLOCKS:
506 ASSERT(delta > 0);
507 tp->t_dblocks_delta += delta;
508 break;
509 case XFS_TRANS_SB_AGCOUNT:
510 ASSERT(delta > 0);
511 tp->t_agcount_delta += delta;
512 break;
513 case XFS_TRANS_SB_IMAXPCT:
514 tp->t_imaxpct_delta += delta;
515 break;
516 case XFS_TRANS_SB_REXTSIZE:
517 tp->t_rextsize_delta += delta;
518 break;
519 case XFS_TRANS_SB_RBMBLOCKS:
520 tp->t_rbmblocks_delta += delta;
521 break;
522 case XFS_TRANS_SB_RBLOCKS:
523 tp->t_rblocks_delta += delta;
524 break;
525 case XFS_TRANS_SB_REXTENTS:
526 tp->t_rextents_delta += delta;
527 break;
528 case XFS_TRANS_SB_REXTSLOG:
529 tp->t_rextslog_delta += delta;
530 break;
531 default:
532 ASSERT(0);
533 return;
534 }
535
David Chinner210c6f12007-05-24 15:26:51 +1000536 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/*
540 * xfs_trans_apply_sb_deltas() is called from the commit code
541 * to bring the superblock buffer into the current transaction
542 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
543 *
544 * For now we just look at each field allowed to change and change
545 * it if necessary.
546 */
547STATIC void
548xfs_trans_apply_sb_deltas(
549 xfs_trans_t *tp)
550{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000551 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 xfs_buf_t *bp;
553 int whole = 0;
554
555 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
556 sbp = XFS_BUF_TO_SBP(bp);
557
558 /*
559 * Check that superblock mods match the mods made to AGF counters.
560 */
561 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
562 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
563 tp->t_ag_btree_delta));
564
David Chinner92821e22007-05-24 15:26:31 +1000565 /*
566 * Only update the superblock counters if we are logging them
567 */
568 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000569 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800570 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000571 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800572 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000573 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800574 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000575 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800576 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000579 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800580 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000581 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800582 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000583
584 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800585 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 whole = 1;
587 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000588 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800589 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 whole = 1;
591 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000592 if (tp->t_imaxpct_delta) {
593 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 whole = 1;
595 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000596 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800597 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 whole = 1;
599 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000600 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800601 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 whole = 1;
603 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000604 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800605 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 whole = 1;
607 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000608 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800609 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 whole = 1;
611 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000612 if (tp->t_rextslog_delta) {
613 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 whole = 1;
615 }
616
617 if (whole)
618 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000619 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000621 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 else
623 /*
624 * Since all the modifiable fields are contiguous, we
625 * can get away with this.
626 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000627 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
628 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 sizeof(sbp->sb_frextents) - 1);
630
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000631 tp->t_mountp->m_super->s_dirt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/*
David Chinner45c34142007-06-18 16:49:44 +1000635 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
636 * and apply superblock counter changes to the in-core superblock. The
637 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
638 * applied to the in-core superblock. The idea is that that has already been
639 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *
641 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
David Chinner45c34142007-06-18 16:49:44 +1000642 * However, we have to ensure that we only modify each superblock field only
643 * once because the application of the delta values may not be atomic. That can
644 * lead to ENOSPC races occurring if we have two separate modifcations of the
645 * free space counter to put back the entire reservation and then take away
646 * what we used.
647 *
648 * If we are not logging superblock counters, then the inode allocated/free and
649 * used block counts are not updated in the on disk superblock. In this case,
650 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
651 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000653STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654xfs_trans_unreserve_and_mod_sb(
655 xfs_trans_t *tp)
656{
657 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
658 xfs_mod_sb_t *msbp;
David Chinner92821e22007-05-24 15:26:31 +1000659 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* REFERENCED */
661 int error;
662 int rsvd;
David Chinner45c34142007-06-18 16:49:44 +1000663 int64_t blkdelta = 0;
664 int64_t rtxdelta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 msbp = msb;
667 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
668
David Chinner45c34142007-06-18 16:49:44 +1000669 /* calculate free blocks delta */
670 if (tp->t_blk_res > 0)
671 blkdelta = tp->t_blk_res;
672
673 if ((tp->t_fdblocks_delta != 0) &&
674 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
675 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
676 blkdelta += tp->t_fdblocks_delta;
677
678 if (blkdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 msbp->msb_field = XFS_SBS_FDBLOCKS;
David Chinner45c34142007-06-18 16:49:44 +1000680 msbp->msb_delta = blkdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 msbp++;
682 }
683
David Chinner45c34142007-06-18 16:49:44 +1000684 /* calculate free realtime extents delta */
685 if (tp->t_rtx_res > 0)
686 rtxdelta = tp->t_rtx_res;
687
688 if ((tp->t_frextents_delta != 0) &&
689 (tp->t_flags & XFS_TRANS_SB_DIRTY))
690 rtxdelta += tp->t_frextents_delta;
691
692 if (rtxdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 msbp->msb_field = XFS_SBS_FREXTENTS;
David Chinner45c34142007-06-18 16:49:44 +1000694 msbp->msb_delta = rtxdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 msbp++;
696 }
697
David Chinner45c34142007-06-18 16:49:44 +1000698 /* apply remaining deltas */
699
David Chinner92821e22007-05-24 15:26:31 +1000700 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
701 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (tp->t_icount_delta != 0) {
703 msbp->msb_field = XFS_SBS_ICOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100704 msbp->msb_delta = tp->t_icount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 msbp++;
706 }
707 if (tp->t_ifree_delta != 0) {
708 msbp->msb_field = XFS_SBS_IFREE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100709 msbp->msb_delta = tp->t_ifree_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 msbp++;
711 }
David Chinner92821e22007-05-24 15:26:31 +1000712 }
713
714 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (tp->t_dblocks_delta != 0) {
716 msbp->msb_field = XFS_SBS_DBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100717 msbp->msb_delta = tp->t_dblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 msbp++;
719 }
720 if (tp->t_agcount_delta != 0) {
721 msbp->msb_field = XFS_SBS_AGCOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100722 msbp->msb_delta = tp->t_agcount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 msbp++;
724 }
725 if (tp->t_imaxpct_delta != 0) {
726 msbp->msb_field = XFS_SBS_IMAX_PCT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100727 msbp->msb_delta = tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 msbp++;
729 }
730 if (tp->t_rextsize_delta != 0) {
731 msbp->msb_field = XFS_SBS_REXTSIZE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100732 msbp->msb_delta = tp->t_rextsize_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 msbp++;
734 }
735 if (tp->t_rbmblocks_delta != 0) {
736 msbp->msb_field = XFS_SBS_RBMBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100737 msbp->msb_delta = tp->t_rbmblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 msbp++;
739 }
740 if (tp->t_rblocks_delta != 0) {
741 msbp->msb_field = XFS_SBS_RBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100742 msbp->msb_delta = tp->t_rblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 msbp++;
744 }
745 if (tp->t_rextents_delta != 0) {
746 msbp->msb_field = XFS_SBS_REXTENTS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100747 msbp->msb_delta = tp->t_rextents_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 msbp++;
749 }
750 if (tp->t_rextslog_delta != 0) {
751 msbp->msb_field = XFS_SBS_REXTSLOG;
David Chinner20f4ebf2007-02-10 18:36:10 +1100752 msbp->msb_delta = tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 msbp++;
754 }
755 }
756
757 /*
758 * If we need to change anything, do it.
759 */
760 if (msbp > msb) {
761 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
762 (uint)(msbp - msb), rsvd);
763 ASSERT(error == 0);
764 }
765}
766
767
768/*
769 * xfs_trans_commit
770 *
771 * Commit the given transaction to the log a/synchronously.
772 *
773 * XFS disk error handling mechanism is not based on a typical
774 * transaction abort mechanism. Logically after the filesystem
775 * gets marked 'SHUTDOWN', we can't let any new transactions
776 * be durable - ie. committed to disk - because some metadata might
777 * be inconsistent. In such cases, this returns an error, and the
778 * caller may assume that all locked objects joined to the transaction
779 * have already been unlocked as if the commit had succeeded.
780 * Do not reference the transaction structure after this call.
781 */
782 /*ARGSUSED*/
783int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100784_xfs_trans_commit(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 xfs_trans_t *tp,
786 uint flags,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100787 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 xfs_log_iovec_t *log_vector;
790 int nvec;
791 xfs_mount_t *mp;
792 xfs_lsn_t commit_lsn;
793 /* REFERENCED */
794 int error;
795 int log_flags;
796 int sync;
797#define XFS_TRANS_LOGVEC_COUNT 16
798 xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 void *commit_iclog;
800 int shutdown;
801
802 commit_lsn = -1;
803
804 /*
805 * Determine whether this commit is releasing a permanent
806 * log reservation or not.
807 */
808 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
809 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
810 log_flags = XFS_LOG_REL_PERM_RESERV;
811 } else {
812 log_flags = 0;
813 }
814 mp = tp->t_mountp;
815
816 /*
817 * If there is nothing to be logged by the transaction,
818 * then unlock all of the items associated with the
819 * transaction and free the transaction structure.
820 * Also make sure to return any reserved blocks to
821 * the free pool.
822 */
823shut_us_down:
824 shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
825 if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
826 xfs_trans_unreserve_and_mod_sb(tp);
827 /*
828 * It is indeed possible for the transaction to be
829 * not dirty but the dqinfo portion to be. All that
830 * means is that we have some (non-persistent) quota
831 * reservations that need to be unreserved.
832 */
833 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
834 if (tp->t_ticket) {
835 commit_lsn = xfs_log_done(mp, tp->t_ticket,
836 NULL, log_flags);
837 if (commit_lsn == -1 && !shutdown)
838 shutdown = XFS_ERROR(EIO);
839 }
Nathan Scott59c1b082006-06-09 14:59:13 +1000840 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
842 xfs_trans_free_busy(tp);
843 xfs_trans_free(tp);
844 XFS_STATS_INC(xs_trans_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return (shutdown);
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ASSERT(tp->t_ticket != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /*
850 * If we need to update the superblock, then do it now.
851 */
852 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
853 xfs_trans_apply_sb_deltas(tp);
854 }
855 XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp);
856
857 /*
858 * Ask each log item how many log_vector entries it will
859 * need so we can figure out how many to allocate.
860 * Try to avoid the kmem_alloc() call in the common case
861 * by using a vector from the stack when it fits.
862 */
863 nvec = xfs_trans_count_vecs(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (nvec == 0) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000865 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto shut_us_down;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100867 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 log_vector = log_vector_fast;
869 } else {
870 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
871 sizeof(xfs_log_iovec_t),
872 KM_SLEEP);
873 }
874
875 /*
876 * Fill in the log_vector and pin the logged items, and
877 * then write the transaction to the log.
878 */
879 xfs_trans_fill_vecs(tp, log_vector);
880
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100881 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100884 * The transaction is committed incore here, and can go out to disk
885 * at any time after this call. However, all the items associated
886 * with the transaction are still locked and pinned in memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
888 commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 tp->t_commit_lsn = commit_lsn;
891 if (nvec > XFS_TRANS_LOGVEC_COUNT) {
892 kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t));
893 }
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
896 * If we got a log write error. Unpin the logitems that we
897 * had pinned, clean up, free trans structure, and return error.
898 */
899 if (error || commit_lsn == -1) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000900 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
902 return XFS_ERROR(EIO);
903 }
904
905 /*
906 * Once the transaction has committed, unused
907 * reservations need to be released and changes to
908 * the superblock need to be reflected in the in-core
909 * version. Do that now.
910 */
911 xfs_trans_unreserve_and_mod_sb(tp);
912
913 sync = tp->t_flags & XFS_TRANS_SYNC;
914
915 /*
916 * Tell the LM to call the transaction completion routine
917 * when the log write with LSN commit_lsn completes (e.g.
918 * when the transaction commit really hits the on-disk log).
919 * After this call we cannot reference tp, because the call
920 * can happen at any time and the call will free the transaction
921 * structure pointed to by tp. The only case where we call
922 * the completion routine (xfs_trans_committed) directly is
923 * if the log is turned off on a debug kernel or we're
924 * running in simulation mode (the log is explicitly turned
925 * off).
926 */
927 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
928 tp->t_logcb.cb_arg = tp;
929
930 /*
931 * We need to pass the iclog buffer which was used for the
932 * transaction commit record into this function, and attach
933 * the callback to it. The callback must be attached before
934 * the items are unlocked to avoid racing with other threads
935 * waiting for an item to unlock.
936 */
937 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
938
939 /*
940 * Mark this thread as no longer being in a transaction
941 */
Nathan Scott59c1b082006-06-09 14:59:13 +1000942 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /*
945 * Once all the items of the transaction have been copied
946 * to the in core log and the callback is attached, the
947 * items can be unlocked.
948 *
949 * This will free descriptors pointing to items which were
950 * not logged since there is nothing more to do with them.
951 * For items which were logged, we will keep pointers to them
952 * so they can be unpinned after the transaction commits to disk.
953 * This will also stamp each modified meta-data item with
954 * the commit lsn of this transaction for dependency tracking
955 * purposes.
956 */
957 xfs_trans_unlock_items(tp, commit_lsn);
958
959 /*
960 * If we detected a log error earlier, finish committing
961 * the transaction now (unpin log items, etc).
962 *
963 * Order is critical here, to avoid using the transaction
964 * pointer after its been freed (by xfs_trans_committed
965 * either here now, or as a callback). We cannot do this
966 * step inside xfs_log_notify as was done earlier because
967 * of this issue.
968 */
969 if (shutdown)
970 xfs_trans_committed(tp, XFS_LI_ABORTED);
971
972 /*
973 * Now that the xfs_trans_committed callback has been attached,
974 * and the items are released we can finally allow the iclog to
975 * go to disk.
976 */
977 error = xfs_log_release_iclog(mp, commit_iclog);
978
979 /*
980 * If the transaction needs to be synchronous, then force the
981 * log out now and wait for it.
982 */
983 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100984 if (!error) {
985 error = _xfs_log_force(mp, commit_lsn,
986 XFS_LOG_FORCE | XFS_LOG_SYNC,
987 log_flushed);
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 XFS_STATS_INC(xs_trans_sync);
990 } else {
991 XFS_STATS_INC(xs_trans_async);
992 }
993
994 return (error);
995}
996
997
998/*
999 * Total up the number of log iovecs needed to commit this
1000 * transaction. The transaction itself needs one for the
1001 * transaction header. Ask each dirty item in turn how many
1002 * it needs to get the total.
1003 */
1004STATIC uint
1005xfs_trans_count_vecs(
1006 xfs_trans_t *tp)
1007{
1008 int nvecs;
1009 xfs_log_item_desc_t *lidp;
1010
1011 nvecs = 1;
1012 lidp = xfs_trans_first_item(tp);
1013 ASSERT(lidp != NULL);
1014
1015 /* In the non-debug case we need to start bailing out if we
1016 * didn't find a log_item here, return zero and let trans_commit
1017 * deal with it.
1018 */
1019 if (lidp == NULL)
1020 return 0;
1021
1022 while (lidp != NULL) {
1023 /*
1024 * Skip items which aren't dirty in this transaction.
1025 */
1026 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1027 lidp = xfs_trans_next_item(tp, lidp);
1028 continue;
1029 }
1030 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1031 nvecs += lidp->lid_size;
1032 lidp = xfs_trans_next_item(tp, lidp);
1033 }
1034
1035 return nvecs;
1036}
1037
1038/*
1039 * Called from the trans_commit code when we notice that
1040 * the filesystem is in the middle of a forced shutdown.
1041 */
1042STATIC void
1043xfs_trans_uncommit(
1044 xfs_trans_t *tp,
1045 uint flags)
1046{
1047 xfs_log_item_desc_t *lidp;
1048
1049 for (lidp = xfs_trans_first_item(tp);
1050 lidp != NULL;
1051 lidp = xfs_trans_next_item(tp, lidp)) {
1052 /*
1053 * Unpin all but those that aren't dirty.
1054 */
1055 if (lidp->lid_flags & XFS_LID_DIRTY)
1056 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1057 }
1058
1059 xfs_trans_unreserve_and_mod_sb(tp);
1060 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
1061
1062 xfs_trans_free_items(tp, flags);
1063 xfs_trans_free_busy(tp);
1064 xfs_trans_free(tp);
1065}
1066
1067/*
1068 * Fill in the vector with pointers to data to be logged
1069 * by this transaction. The transaction header takes
1070 * the first vector, and then each dirty item takes the
1071 * number of vectors it indicated it needed in xfs_trans_count_vecs().
1072 *
1073 * As each item fills in the entries it needs, also pin the item
1074 * so that it cannot be flushed out until the log write completes.
1075 */
1076STATIC void
1077xfs_trans_fill_vecs(
1078 xfs_trans_t *tp,
1079 xfs_log_iovec_t *log_vector)
1080{
1081 xfs_log_item_desc_t *lidp;
1082 xfs_log_iovec_t *vecp;
1083 uint nitems;
1084
1085 /*
1086 * Skip over the entry for the transaction header, we'll
1087 * fill that in at the end.
1088 */
1089 vecp = log_vector + 1; /* pointer arithmetic */
1090
1091 nitems = 0;
1092 lidp = xfs_trans_first_item(tp);
1093 ASSERT(lidp != NULL);
1094 while (lidp != NULL) {
1095 /*
1096 * Skip items which aren't dirty in this transaction.
1097 */
1098 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1099 lidp = xfs_trans_next_item(tp, lidp);
1100 continue;
1101 }
1102 /*
1103 * The item may be marked dirty but not log anything.
1104 * This can be used to get called when a transaction
1105 * is committed.
1106 */
1107 if (lidp->lid_size) {
1108 nitems++;
1109 }
1110 IOP_FORMAT(lidp->lid_item, vecp);
1111 vecp += lidp->lid_size; /* pointer arithmetic */
1112 IOP_PIN(lidp->lid_item);
1113 lidp = xfs_trans_next_item(tp, lidp);
1114 }
1115
1116 /*
1117 * Now that we've counted the number of items in this
1118 * transaction, fill in the transaction header.
1119 */
1120 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1121 tp->t_header.th_type = tp->t_type;
1122 tp->t_header.th_num_items = nitems;
1123 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1124 log_vector->i_len = sizeof(xfs_trans_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001125 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_TRANSHDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128
1129/*
1130 * Unlock all of the transaction's items and free the transaction.
1131 * The transaction must not have modified any of its items, because
1132 * there is no way to restore them to their previous state.
1133 *
1134 * If the transaction has made a log reservation, make sure to release
1135 * it as well.
1136 */
1137void
1138xfs_trans_cancel(
1139 xfs_trans_t *tp,
1140 int flags)
1141{
1142 int log_flags;
1143#ifdef DEBUG
1144 xfs_log_item_chunk_t *licp;
1145 xfs_log_item_desc_t *lidp;
1146 xfs_log_item_t *lip;
1147 int i;
1148#endif
Ryan Hankins0733af22006-01-11 15:36:44 +11001149 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /*
1152 * See if the caller is being too lazy to figure out if
1153 * the transaction really needs an abort.
1154 */
1155 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1156 flags &= ~XFS_TRANS_ABORT;
1157 /*
1158 * See if the caller is relying on us to shut down the
1159 * filesystem. This happens in paths where we detect
1160 * corruption and decide to give up.
1161 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001162 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001163 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001164 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166#ifdef DEBUG
1167 if (!(flags & XFS_TRANS_ABORT)) {
1168 licp = &(tp->t_items);
1169 while (licp != NULL) {
1170 lidp = licp->lic_descs;
1171 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1172 if (XFS_LIC_ISFREE(licp, i)) {
1173 continue;
1174 }
1175
1176 lip = lidp->lid_item;
Ryan Hankins0733af22006-01-11 15:36:44 +11001177 if (!XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 ASSERT(!(lip->li_type == XFS_LI_EFD));
1179 }
1180 licp = licp->lic_next;
1181 }
1182 }
1183#endif
1184 xfs_trans_unreserve_and_mod_sb(tp);
Ryan Hankins0733af22006-01-11 15:36:44 +11001185 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (tp->t_ticket) {
1188 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1189 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1190 log_flags = XFS_LOG_REL_PERM_RESERV;
1191 } else {
1192 log_flags = 0;
1193 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001194 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
1197 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001198 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 xfs_trans_free_items(tp, flags);
1201 xfs_trans_free_busy(tp);
1202 xfs_trans_free(tp);
1203}
1204
1205
1206/*
1207 * Free the transaction structure. If there is more clean up
1208 * to do when the structure is freed, add it here.
1209 */
1210STATIC void
1211xfs_trans_free(
1212 xfs_trans_t *tp)
1213{
1214 atomic_dec(&tp->t_mountp->m_active_trans);
1215 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
1216 kmem_zone_free(xfs_trans_zone, tp);
1217}
1218
1219
1220/*
1221 * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
1222 *
1223 * This is typically called by the LM when a transaction has been fully
1224 * committed to disk. It needs to unpin the items which have
1225 * been logged by the transaction and update their positions
1226 * in the AIL if necessary.
1227 * This also gets called when the transactions didn't get written out
1228 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1229 *
1230 * Call xfs_trans_chunk_committed() to process the items in
1231 * each chunk.
1232 */
1233STATIC void
1234xfs_trans_committed(
1235 xfs_trans_t *tp,
1236 int abortflag)
1237{
1238 xfs_log_item_chunk_t *licp;
1239 xfs_log_item_chunk_t *next_licp;
1240 xfs_log_busy_chunk_t *lbcp;
1241 xfs_log_busy_slot_t *lbsp;
1242 int i;
1243
1244 /*
1245 * Call the transaction's completion callback if there
1246 * is one.
1247 */
1248 if (tp->t_callback != NULL) {
1249 tp->t_callback(tp, tp->t_callarg);
1250 }
1251
1252 /*
1253 * Special case the chunk embedded in the transaction.
1254 */
1255 licp = &(tp->t_items);
1256 if (!(XFS_LIC_ARE_ALL_FREE(licp))) {
1257 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1258 }
1259
1260 /*
1261 * Process the items in each chunk in turn.
1262 */
1263 licp = licp->lic_next;
1264 while (licp != NULL) {
1265 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
1266 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1267 next_licp = licp->lic_next;
1268 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
1269 licp = next_licp;
1270 }
1271
1272 /*
1273 * Clear all the per-AG busy list items listed in this transaction
1274 */
1275 lbcp = &tp->t_busy;
1276 while (lbcp != NULL) {
1277 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1278 if (!XFS_LBC_ISFREE(lbcp, i)) {
1279 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1280 lbsp->lbc_idx);
1281 }
1282 }
1283 lbcp = lbcp->lbc_next;
1284 }
1285 xfs_trans_free_busy(tp);
1286
1287 /*
1288 * That's it for the transaction structure. Free it.
1289 */
1290 xfs_trans_free(tp);
1291}
1292
1293/*
1294 * This is called to perform the commit processing for each
1295 * item described by the given chunk.
1296 *
1297 * The commit processing consists of unlocking items which were
1298 * held locked with the SYNC_UNLOCK attribute, calling the committed
1299 * routine of each logged item, updating the item's position in the AIL
1300 * if necessary, and unpinning each item. If the committed routine
1301 * returns -1, then do nothing further with the item because it
1302 * may have been freed.
1303 *
1304 * Since items are unlocked when they are copied to the incore
1305 * log, it is possible for two transactions to be completing
1306 * and manipulating the same item simultaneously. The AIL lock
1307 * will protect the lsn field of each item. The value of this
1308 * field can never go backwards.
1309 *
1310 * We unpin the items after repositioning them in the AIL, because
1311 * otherwise they could be immediately flushed and we'd have to race
1312 * with the flusher trying to pull the item from the AIL as we add it.
1313 */
1314STATIC void
1315xfs_trans_chunk_committed(
1316 xfs_log_item_chunk_t *licp,
1317 xfs_lsn_t lsn,
1318 int aborted)
1319{
1320 xfs_log_item_desc_t *lidp;
1321 xfs_log_item_t *lip;
1322 xfs_lsn_t item_lsn;
1323 struct xfs_mount *mp;
1324 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 lidp = licp->lic_descs;
1327 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1328 if (XFS_LIC_ISFREE(licp, i)) {
1329 continue;
1330 }
1331
1332 lip = lidp->lid_item;
1333 if (aborted)
1334 lip->li_flags |= XFS_LI_ABORTED;
1335
1336 /*
1337 * Send in the ABORTED flag to the COMMITTED routine
1338 * so that it knows whether the transaction was aborted
1339 * or not.
1340 */
1341 item_lsn = IOP_COMMITTED(lip, lsn);
1342
1343 /*
1344 * If the committed routine returns -1, make
1345 * no more references to the item.
1346 */
1347 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
1348 continue;
1349 }
1350
1351 /*
1352 * If the returned lsn is greater than what it
1353 * contained before, update the location of the
1354 * item in the AIL. If it is not, then do nothing.
1355 * Items can never move backwards in the AIL.
1356 *
1357 * While the new lsn should usually be greater, it
1358 * is possible that a later transaction completing
1359 * simultaneously with an earlier one using the
1360 * same item could complete first with a higher lsn.
1361 * This would cause the earlier transaction to fail
1362 * the test below.
1363 */
1364 mp = lip->li_mountp;
Donald Douwsma287f3da2007-10-11 17:36:05 +10001365 spin_lock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1367 /*
1368 * This will set the item's lsn to item_lsn
1369 * and update the position of the item in
1370 * the AIL.
1371 *
1372 * xfs_trans_update_ail() drops the AIL lock.
1373 */
Donald Douwsma287f3da2007-10-11 17:36:05 +10001374 xfs_trans_update_ail(mp, lip, item_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 } else {
Donald Douwsma287f3da2007-10-11 17:36:05 +10001376 spin_unlock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
1379 /*
1380 * Now that we've repositioned the item in the AIL,
1381 * unpin it so it can be flushed. Pass information
1382 * about buffer stale state down from the log item
1383 * flags, if anyone else stales the buffer we do not
1384 * want to pay any attention to it.
1385 */
1386 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
1387 }
1388}