Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / fs / xfs / xfs_trans.c
blob3db0e22007759c076b0ed561270203af3dedd026
1 /*
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
34 #include "xfs_macros.h"
35 #include "xfs_types.h"
36 #include "xfs_inum.h"
37 #include "xfs_log.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_ag.h"
41 #include "xfs_dir.h"
42 #include "xfs_dir2.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_error.h"
46 #include "xfs_trans_priv.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_bmap_btree.h"
49 #include "xfs_ialloc_btree.h"
50 #include "xfs_btree.h"
51 #include "xfs_ialloc.h"
52 #include "xfs_alloc.h"
53 #include "xfs_attr_sf.h"
54 #include "xfs_dir_sf.h"
55 #include "xfs_dir2_sf.h"
56 #include "xfs_dinode.h"
57 #include "xfs_inode.h"
58 #include "xfs_bmap.h"
59 #include "xfs_da_btree.h"
60 #include "xfs_quota.h"
61 #include "xfs_trans_space.h"
64 STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *);
65 STATIC uint xfs_trans_count_vecs(xfs_trans_t *);
66 STATIC void xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *);
67 STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
68 STATIC void xfs_trans_committed(xfs_trans_t *, int);
69 STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
70 STATIC void xfs_trans_free(xfs_trans_t *);
72 kmem_zone_t *xfs_trans_zone;
76 * Initialize the precomputed transaction reservation values
77 * in the mount structure.
79 void
80 xfs_trans_init(
81 xfs_mount_t *mp)
83 xfs_trans_reservations_t *resp;
85 resp = &(mp->m_reservations);
86 resp->tr_write =
87 (uint)(XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
88 resp->tr_itruncate =
89 (uint)(XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
90 resp->tr_rename =
91 (uint)(XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
92 resp->tr_link = (uint)XFS_CALC_LINK_LOG_RES(mp);
93 resp->tr_remove =
94 (uint)(XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
95 resp->tr_symlink =
96 (uint)(XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
97 resp->tr_create =
98 (uint)(XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
99 resp->tr_mkdir =
100 (uint)(XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
101 resp->tr_ifree =
102 (uint)(XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
103 resp->tr_ichange =
104 (uint)(XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
105 resp->tr_growdata = (uint)XFS_CALC_GROWDATA_LOG_RES(mp);
106 resp->tr_swrite = (uint)XFS_CALC_SWRITE_LOG_RES(mp);
107 resp->tr_writeid = (uint)XFS_CALC_WRITEID_LOG_RES(mp);
108 resp->tr_addafork =
109 (uint)(XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
110 resp->tr_attrinval = (uint)XFS_CALC_ATTRINVAL_LOG_RES(mp);
111 resp->tr_attrset =
112 (uint)(XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
113 resp->tr_attrrm =
114 (uint)(XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
115 resp->tr_clearagi = (uint)XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
116 resp->tr_growrtalloc = (uint)XFS_CALC_GROWRTALLOC_LOG_RES(mp);
117 resp->tr_growrtzero = (uint)XFS_CALC_GROWRTZERO_LOG_RES(mp);
118 resp->tr_growrtfree = (uint)XFS_CALC_GROWRTFREE_LOG_RES(mp);
122 * This routine is called to allocate a transaction structure.
123 * The type parameter indicates the type of the transaction. These
124 * are enumerated in xfs_trans.h.
126 * Dynamically allocate the transaction structure from the transaction
127 * zone, initialize it, and return it to the caller.
129 xfs_trans_t *
130 xfs_trans_alloc(
131 xfs_mount_t *mp,
132 uint type)
134 fs_check_frozen(XFS_MTOVFS(mp), SB_FREEZE_TRANS);
135 atomic_inc(&mp->m_active_trans);
137 return (_xfs_trans_alloc(mp, type));
141 xfs_trans_t *
142 _xfs_trans_alloc(
143 xfs_mount_t *mp,
144 uint type)
146 xfs_trans_t *tp;
148 ASSERT(xfs_trans_zone != NULL);
149 tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
152 * Initialize the transaction structure.
154 tp->t_magic = XFS_TRANS_MAGIC;
155 tp->t_type = type;
156 tp->t_mountp = mp;
157 tp->t_items_free = XFS_LIC_NUM_SLOTS;
158 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
159 XFS_LIC_INIT(&(tp->t_items));
160 XFS_LBC_INIT(&(tp->t_busy));
162 return (tp);
166 * This is called to create a new transaction which will share the
167 * permanent log reservation of the given transaction. The remaining
168 * unused block and rt extent reservations are also inherited. This
169 * implies that the original transaction is no longer allowed to allocate
170 * blocks. Locks and log items, however, are no inherited. They must
171 * be added to the new transaction explicitly.
173 xfs_trans_t *
174 xfs_trans_dup(
175 xfs_trans_t *tp)
177 xfs_trans_t *ntp;
179 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
182 * Initialize the new transaction structure.
184 ntp->t_magic = XFS_TRANS_MAGIC;
185 ntp->t_type = tp->t_type;
186 ntp->t_mountp = tp->t_mountp;
187 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
188 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
189 XFS_LIC_INIT(&(ntp->t_items));
190 XFS_LBC_INIT(&(ntp->t_busy));
192 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
194 #if defined(XLOG_NOLOG) || defined(DEBUG)
195 ASSERT(!xlog_debug || tp->t_ticket != NULL);
196 #else
197 ASSERT(tp->t_ticket != NULL);
198 #endif
199 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
200 ntp->t_ticket = tp->t_ticket;
201 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
202 tp->t_blk_res = tp->t_blk_res_used;
203 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
204 tp->t_rtx_res = tp->t_rtx_res_used;
205 PFLAGS_DUP(&tp->t_pflags, &ntp->t_pflags);
207 XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp);
209 atomic_inc(&tp->t_mountp->m_active_trans);
210 return ntp;
214 * This is called to reserve free disk blocks and log space for the
215 * given transaction. This must be done before allocating any resources
216 * within the transaction.
218 * This will return ENOSPC if there are not enough blocks available.
219 * It will sleep waiting for available log space.
220 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
221 * is used by long running transactions. If any one of the reservations
222 * fails then they will all be backed out.
224 * This does not do quota reservations. That typically is done by the
225 * caller afterwards.
228 xfs_trans_reserve(
229 xfs_trans_t *tp,
230 uint blocks,
231 uint logspace,
232 uint rtextents,
233 uint flags,
234 uint logcount)
236 int log_flags;
237 int error;
238 int rsvd;
240 error = 0;
241 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
243 /* Mark this thread as being in a transaction */
244 PFLAGS_SET_FSTRANS(&tp->t_pflags);
247 * Attempt to reserve the needed disk blocks by decrementing
248 * the number needed from the number available. This will
249 * fail if the count would go below zero.
251 if (blocks > 0) {
252 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
253 -blocks, rsvd);
254 if (error != 0) {
255 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
256 return (XFS_ERROR(ENOSPC));
258 tp->t_blk_res += blocks;
262 * Reserve the log space needed for this transaction.
264 if (logspace > 0) {
265 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
266 ASSERT((tp->t_log_count == 0) ||
267 (tp->t_log_count == logcount));
268 if (flags & XFS_TRANS_PERM_LOG_RES) {
269 log_flags = XFS_LOG_PERM_RESERV;
270 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
271 } else {
272 ASSERT(tp->t_ticket == NULL);
273 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
274 log_flags = 0;
277 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
278 &tp->t_ticket,
279 XFS_TRANSACTION, log_flags);
280 if (error) {
281 goto undo_blocks;
283 tp->t_log_res = logspace;
284 tp->t_log_count = logcount;
288 * Attempt to reserve the needed realtime extents by decrementing
289 * the number needed from the number available. This will
290 * fail if the count would go below zero.
292 if (rtextents > 0) {
293 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
294 -rtextents, rsvd);
295 if (error) {
296 error = XFS_ERROR(ENOSPC);
297 goto undo_log;
299 tp->t_rtx_res += rtextents;
302 return 0;
305 * Error cases jump to one of these labels to undo any
306 * reservations which have already been performed.
308 undo_log:
309 if (logspace > 0) {
310 if (flags & XFS_TRANS_PERM_LOG_RES) {
311 log_flags = XFS_LOG_REL_PERM_RESERV;
312 } else {
313 log_flags = 0;
315 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
316 tp->t_ticket = NULL;
317 tp->t_log_res = 0;
318 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
321 undo_blocks:
322 if (blocks > 0) {
323 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
324 blocks, rsvd);
325 tp->t_blk_res = 0;
328 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
330 return (error);
335 * This is called to set the a callback to be called when the given
336 * transaction is committed to disk. The transaction pointer and the
337 * argument pointer will be passed to the callback routine.
339 * Only one callback can be associated with any single transaction.
341 void
342 xfs_trans_callback(
343 xfs_trans_t *tp,
344 xfs_trans_callback_t callback,
345 void *arg)
347 ASSERT(tp->t_callback == NULL);
348 tp->t_callback = callback;
349 tp->t_callarg = arg;
354 * Record the indicated change to the given field for application
355 * to the file system's superblock when the transaction commits.
356 * For now, just store the change in the transaction structure.
358 * Mark the transaction structure to indicate that the superblock
359 * needs to be updated before committing.
361 void
362 xfs_trans_mod_sb(
363 xfs_trans_t *tp,
364 uint field,
365 long delta)
368 switch (field) {
369 case XFS_TRANS_SB_ICOUNT:
370 tp->t_icount_delta += delta;
371 break;
372 case XFS_TRANS_SB_IFREE:
373 tp->t_ifree_delta += delta;
374 break;
375 case XFS_TRANS_SB_FDBLOCKS:
377 * Track the number of blocks allocated in the
378 * transaction. Make sure it does not exceed the
379 * number reserved.
381 if (delta < 0) {
382 tp->t_blk_res_used += (uint)-delta;
383 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
385 tp->t_fdblocks_delta += delta;
386 break;
387 case XFS_TRANS_SB_RES_FDBLOCKS:
389 * The allocation has already been applied to the
390 * in-core superblock's counter. This should only
391 * be applied to the on-disk superblock.
393 ASSERT(delta < 0);
394 tp->t_res_fdblocks_delta += delta;
395 break;
396 case XFS_TRANS_SB_FREXTENTS:
398 * Track the number of blocks allocated in the
399 * transaction. Make sure it does not exceed the
400 * number reserved.
402 if (delta < 0) {
403 tp->t_rtx_res_used += (uint)-delta;
404 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
406 tp->t_frextents_delta += delta;
407 break;
408 case XFS_TRANS_SB_RES_FREXTENTS:
410 * The allocation has already been applied to the
411 * in-core superblocks's counter. This should only
412 * be applied to the on-disk superblock.
414 ASSERT(delta < 0);
415 tp->t_res_frextents_delta += delta;
416 break;
417 case XFS_TRANS_SB_DBLOCKS:
418 ASSERT(delta > 0);
419 tp->t_dblocks_delta += delta;
420 break;
421 case XFS_TRANS_SB_AGCOUNT:
422 ASSERT(delta > 0);
423 tp->t_agcount_delta += delta;
424 break;
425 case XFS_TRANS_SB_IMAXPCT:
426 tp->t_imaxpct_delta += delta;
427 break;
428 case XFS_TRANS_SB_REXTSIZE:
429 tp->t_rextsize_delta += delta;
430 break;
431 case XFS_TRANS_SB_RBMBLOCKS:
432 tp->t_rbmblocks_delta += delta;
433 break;
434 case XFS_TRANS_SB_RBLOCKS:
435 tp->t_rblocks_delta += delta;
436 break;
437 case XFS_TRANS_SB_REXTENTS:
438 tp->t_rextents_delta += delta;
439 break;
440 case XFS_TRANS_SB_REXTSLOG:
441 tp->t_rextslog_delta += delta;
442 break;
443 default:
444 ASSERT(0);
445 return;
448 tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY);
452 * xfs_trans_apply_sb_deltas() is called from the commit code
453 * to bring the superblock buffer into the current transaction
454 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
456 * For now we just look at each field allowed to change and change
457 * it if necessary.
459 STATIC void
460 xfs_trans_apply_sb_deltas(
461 xfs_trans_t *tp)
463 xfs_sb_t *sbp;
464 xfs_buf_t *bp;
465 int whole = 0;
467 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
468 sbp = XFS_BUF_TO_SBP(bp);
471 * Check that superblock mods match the mods made to AGF counters.
473 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
474 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
475 tp->t_ag_btree_delta));
477 if (tp->t_icount_delta != 0) {
478 INT_MOD(sbp->sb_icount, ARCH_CONVERT, tp->t_icount_delta);
480 if (tp->t_ifree_delta != 0) {
481 INT_MOD(sbp->sb_ifree, ARCH_CONVERT, tp->t_ifree_delta);
484 if (tp->t_fdblocks_delta != 0) {
485 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_fdblocks_delta);
487 if (tp->t_res_fdblocks_delta != 0) {
488 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_res_fdblocks_delta);
491 if (tp->t_frextents_delta != 0) {
492 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_frextents_delta);
494 if (tp->t_res_frextents_delta != 0) {
495 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_res_frextents_delta);
497 if (tp->t_dblocks_delta != 0) {
498 INT_MOD(sbp->sb_dblocks, ARCH_CONVERT, tp->t_dblocks_delta);
499 whole = 1;
501 if (tp->t_agcount_delta != 0) {
502 INT_MOD(sbp->sb_agcount, ARCH_CONVERT, tp->t_agcount_delta);
503 whole = 1;
505 if (tp->t_imaxpct_delta != 0) {
506 INT_MOD(sbp->sb_imax_pct, ARCH_CONVERT, tp->t_imaxpct_delta);
507 whole = 1;
509 if (tp->t_rextsize_delta != 0) {
510 INT_MOD(sbp->sb_rextsize, ARCH_CONVERT, tp->t_rextsize_delta);
511 whole = 1;
513 if (tp->t_rbmblocks_delta != 0) {
514 INT_MOD(sbp->sb_rbmblocks, ARCH_CONVERT, tp->t_rbmblocks_delta);
515 whole = 1;
517 if (tp->t_rblocks_delta != 0) {
518 INT_MOD(sbp->sb_rblocks, ARCH_CONVERT, tp->t_rblocks_delta);
519 whole = 1;
521 if (tp->t_rextents_delta != 0) {
522 INT_MOD(sbp->sb_rextents, ARCH_CONVERT, tp->t_rextents_delta);
523 whole = 1;
525 if (tp->t_rextslog_delta != 0) {
526 INT_MOD(sbp->sb_rextslog, ARCH_CONVERT, tp->t_rextslog_delta);
527 whole = 1;
530 if (whole)
532 * Log the whole thing, the fields are discontiguous.
534 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_sb_t) - 1);
535 else
537 * Since all the modifiable fields are contiguous, we
538 * can get away with this.
540 xfs_trans_log_buf(tp, bp, offsetof(xfs_sb_t, sb_icount),
541 offsetof(xfs_sb_t, sb_frextents) +
542 sizeof(sbp->sb_frextents) - 1);
544 XFS_MTOVFS(tp->t_mountp)->vfs_super->s_dirt = 1;
548 * xfs_trans_unreserve_and_mod_sb() is called to release unused
549 * reservations and apply superblock counter changes to the in-core
550 * superblock.
552 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
554 void
555 xfs_trans_unreserve_and_mod_sb(
556 xfs_trans_t *tp)
558 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
559 xfs_mod_sb_t *msbp;
560 /* REFERENCED */
561 int error;
562 int rsvd;
564 msbp = msb;
565 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
568 * Release any reserved blocks. Any that were allocated
569 * will be taken back again by fdblocks_delta below.
571 if (tp->t_blk_res > 0) {
572 msbp->msb_field = XFS_SBS_FDBLOCKS;
573 msbp->msb_delta = tp->t_blk_res;
574 msbp++;
578 * Release any reserved real time extents . Any that were
579 * allocated will be taken back again by frextents_delta below.
581 if (tp->t_rtx_res > 0) {
582 msbp->msb_field = XFS_SBS_FREXTENTS;
583 msbp->msb_delta = tp->t_rtx_res;
584 msbp++;
588 * Apply any superblock modifications to the in-core version.
589 * The t_res_fdblocks_delta and t_res_frextents_delta fields are
590 * explicity NOT applied to the in-core superblock.
591 * The idea is that that has already been done.
593 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
594 if (tp->t_icount_delta != 0) {
595 msbp->msb_field = XFS_SBS_ICOUNT;
596 msbp->msb_delta = (int)tp->t_icount_delta;
597 msbp++;
599 if (tp->t_ifree_delta != 0) {
600 msbp->msb_field = XFS_SBS_IFREE;
601 msbp->msb_delta = (int)tp->t_ifree_delta;
602 msbp++;
604 if (tp->t_fdblocks_delta != 0) {
605 msbp->msb_field = XFS_SBS_FDBLOCKS;
606 msbp->msb_delta = (int)tp->t_fdblocks_delta;
607 msbp++;
609 if (tp->t_frextents_delta != 0) {
610 msbp->msb_field = XFS_SBS_FREXTENTS;
611 msbp->msb_delta = (int)tp->t_frextents_delta;
612 msbp++;
614 if (tp->t_dblocks_delta != 0) {
615 msbp->msb_field = XFS_SBS_DBLOCKS;
616 msbp->msb_delta = (int)tp->t_dblocks_delta;
617 msbp++;
619 if (tp->t_agcount_delta != 0) {
620 msbp->msb_field = XFS_SBS_AGCOUNT;
621 msbp->msb_delta = (int)tp->t_agcount_delta;
622 msbp++;
624 if (tp->t_imaxpct_delta != 0) {
625 msbp->msb_field = XFS_SBS_IMAX_PCT;
626 msbp->msb_delta = (int)tp->t_imaxpct_delta;
627 msbp++;
629 if (tp->t_rextsize_delta != 0) {
630 msbp->msb_field = XFS_SBS_REXTSIZE;
631 msbp->msb_delta = (int)tp->t_rextsize_delta;
632 msbp++;
634 if (tp->t_rbmblocks_delta != 0) {
635 msbp->msb_field = XFS_SBS_RBMBLOCKS;
636 msbp->msb_delta = (int)tp->t_rbmblocks_delta;
637 msbp++;
639 if (tp->t_rblocks_delta != 0) {
640 msbp->msb_field = XFS_SBS_RBLOCKS;
641 msbp->msb_delta = (int)tp->t_rblocks_delta;
642 msbp++;
644 if (tp->t_rextents_delta != 0) {
645 msbp->msb_field = XFS_SBS_REXTENTS;
646 msbp->msb_delta = (int)tp->t_rextents_delta;
647 msbp++;
649 if (tp->t_rextslog_delta != 0) {
650 msbp->msb_field = XFS_SBS_REXTSLOG;
651 msbp->msb_delta = (int)tp->t_rextslog_delta;
652 msbp++;
657 * If we need to change anything, do it.
659 if (msbp > msb) {
660 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
661 (uint)(msbp - msb), rsvd);
662 ASSERT(error == 0);
668 * xfs_trans_commit
670 * Commit the given transaction to the log a/synchronously.
672 * XFS disk error handling mechanism is not based on a typical
673 * transaction abort mechanism. Logically after the filesystem
674 * gets marked 'SHUTDOWN', we can't let any new transactions
675 * be durable - ie. committed to disk - because some metadata might
676 * be inconsistent. In such cases, this returns an error, and the
677 * caller may assume that all locked objects joined to the transaction
678 * have already been unlocked as if the commit had succeeded.
679 * Do not reference the transaction structure after this call.
681 /*ARGSUSED*/
683 xfs_trans_commit(
684 xfs_trans_t *tp,
685 uint flags,
686 xfs_lsn_t *commit_lsn_p)
688 xfs_log_iovec_t *log_vector;
689 int nvec;
690 xfs_mount_t *mp;
691 xfs_lsn_t commit_lsn;
692 /* REFERENCED */
693 int error;
694 int log_flags;
695 int sync;
696 #define XFS_TRANS_LOGVEC_COUNT 16
697 xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
698 #if defined(XLOG_NOLOG) || defined(DEBUG)
699 static xfs_lsn_t trans_lsn = 1;
700 #endif
701 void *commit_iclog;
702 int shutdown;
704 commit_lsn = -1;
707 * Determine whether this commit is releasing a permanent
708 * log reservation or not.
710 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
711 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
712 log_flags = XFS_LOG_REL_PERM_RESERV;
713 } else {
714 log_flags = 0;
716 mp = tp->t_mountp;
719 * If there is nothing to be logged by the transaction,
720 * then unlock all of the items associated with the
721 * transaction and free the transaction structure.
722 * Also make sure to return any reserved blocks to
723 * the free pool.
725 shut_us_down:
726 shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
727 if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
728 xfs_trans_unreserve_and_mod_sb(tp);
730 * It is indeed possible for the transaction to be
731 * not dirty but the dqinfo portion to be. All that
732 * means is that we have some (non-persistent) quota
733 * reservations that need to be unreserved.
735 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
736 if (tp->t_ticket) {
737 commit_lsn = xfs_log_done(mp, tp->t_ticket,
738 NULL, log_flags);
739 if (commit_lsn == -1 && !shutdown)
740 shutdown = XFS_ERROR(EIO);
742 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
743 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
744 xfs_trans_free_busy(tp);
745 xfs_trans_free(tp);
746 XFS_STATS_INC(xs_trans_empty);
747 if (commit_lsn_p)
748 *commit_lsn_p = commit_lsn;
749 return (shutdown);
751 #if defined(XLOG_NOLOG) || defined(DEBUG)
752 ASSERT(!xlog_debug || tp->t_ticket != NULL);
753 #else
754 ASSERT(tp->t_ticket != NULL);
755 #endif
758 * If we need to update the superblock, then do it now.
760 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
761 xfs_trans_apply_sb_deltas(tp);
763 XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp);
766 * Ask each log item how many log_vector entries it will
767 * need so we can figure out how many to allocate.
768 * Try to avoid the kmem_alloc() call in the common case
769 * by using a vector from the stack when it fits.
771 nvec = xfs_trans_count_vecs(tp);
773 if (nvec == 0) {
774 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
775 goto shut_us_down;
779 if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
780 log_vector = log_vector_fast;
781 } else {
782 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
783 sizeof(xfs_log_iovec_t),
784 KM_SLEEP);
788 * Fill in the log_vector and pin the logged items, and
789 * then write the transaction to the log.
791 xfs_trans_fill_vecs(tp, log_vector);
794 * Ignore errors here. xfs_log_done would do the right thing.
795 * We need to put the ticket, etc. away.
797 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket,
798 &(tp->t_lsn));
800 #if defined(XLOG_NOLOG) || defined(DEBUG)
801 if (xlog_debug) {
802 commit_lsn = xfs_log_done(mp, tp->t_ticket,
803 &commit_iclog, log_flags);
804 } else {
805 commit_lsn = 0;
806 tp->t_lsn = trans_lsn++;
808 #else
810 * This is the regular case. At this point (after the call finishes),
811 * the transaction is committed incore and could go out to disk at
812 * any time. However, all the items associated with the transaction
813 * are still locked and pinned in memory.
815 commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
816 #endif
818 tp->t_commit_lsn = commit_lsn;
819 if (nvec > XFS_TRANS_LOGVEC_COUNT) {
820 kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t));
823 if (commit_lsn_p)
824 *commit_lsn_p = commit_lsn;
827 * If we got a log write error. Unpin the logitems that we
828 * had pinned, clean up, free trans structure, and return error.
830 if (error || commit_lsn == -1) {
831 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
832 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
833 return XFS_ERROR(EIO);
837 * Once the transaction has committed, unused
838 * reservations need to be released and changes to
839 * the superblock need to be reflected in the in-core
840 * version. Do that now.
842 xfs_trans_unreserve_and_mod_sb(tp);
844 sync = tp->t_flags & XFS_TRANS_SYNC;
847 * Tell the LM to call the transaction completion routine
848 * when the log write with LSN commit_lsn completes (e.g.
849 * when the transaction commit really hits the on-disk log).
850 * After this call we cannot reference tp, because the call
851 * can happen at any time and the call will free the transaction
852 * structure pointed to by tp. The only case where we call
853 * the completion routine (xfs_trans_committed) directly is
854 * if the log is turned off on a debug kernel or we're
855 * running in simulation mode (the log is explicitly turned
856 * off).
858 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
859 tp->t_logcb.cb_arg = tp;
862 * We need to pass the iclog buffer which was used for the
863 * transaction commit record into this function, and attach
864 * the callback to it. The callback must be attached before
865 * the items are unlocked to avoid racing with other threads
866 * waiting for an item to unlock.
868 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
871 * Mark this thread as no longer being in a transaction
873 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
876 * Once all the items of the transaction have been copied
877 * to the in core log and the callback is attached, the
878 * items can be unlocked.
880 * This will free descriptors pointing to items which were
881 * not logged since there is nothing more to do with them.
882 * For items which were logged, we will keep pointers to them
883 * so they can be unpinned after the transaction commits to disk.
884 * This will also stamp each modified meta-data item with
885 * the commit lsn of this transaction for dependency tracking
886 * purposes.
888 xfs_trans_unlock_items(tp, commit_lsn);
891 * If we detected a log error earlier, finish committing
892 * the transaction now (unpin log items, etc).
894 * Order is critical here, to avoid using the transaction
895 * pointer after its been freed (by xfs_trans_committed
896 * either here now, or as a callback). We cannot do this
897 * step inside xfs_log_notify as was done earlier because
898 * of this issue.
900 if (shutdown)
901 xfs_trans_committed(tp, XFS_LI_ABORTED);
904 * Now that the xfs_trans_committed callback has been attached,
905 * and the items are released we can finally allow the iclog to
906 * go to disk.
908 error = xfs_log_release_iclog(mp, commit_iclog);
911 * If the transaction needs to be synchronous, then force the
912 * log out now and wait for it.
914 if (sync) {
915 if (!error)
916 error = xfs_log_force(mp, commit_lsn,
917 XFS_LOG_FORCE | XFS_LOG_SYNC);
918 XFS_STATS_INC(xs_trans_sync);
919 } else {
920 XFS_STATS_INC(xs_trans_async);
923 return (error);
928 * Total up the number of log iovecs needed to commit this
929 * transaction. The transaction itself needs one for the
930 * transaction header. Ask each dirty item in turn how many
931 * it needs to get the total.
933 STATIC uint
934 xfs_trans_count_vecs(
935 xfs_trans_t *tp)
937 int nvecs;
938 xfs_log_item_desc_t *lidp;
940 nvecs = 1;
941 lidp = xfs_trans_first_item(tp);
942 ASSERT(lidp != NULL);
944 /* In the non-debug case we need to start bailing out if we
945 * didn't find a log_item here, return zero and let trans_commit
946 * deal with it.
948 if (lidp == NULL)
949 return 0;
951 while (lidp != NULL) {
953 * Skip items which aren't dirty in this transaction.
955 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
956 lidp = xfs_trans_next_item(tp, lidp);
957 continue;
959 lidp->lid_size = IOP_SIZE(lidp->lid_item);
960 nvecs += lidp->lid_size;
961 lidp = xfs_trans_next_item(tp, lidp);
964 return nvecs;
968 * Called from the trans_commit code when we notice that
969 * the filesystem is in the middle of a forced shutdown.
971 STATIC void
972 xfs_trans_uncommit(
973 xfs_trans_t *tp,
974 uint flags)
976 xfs_log_item_desc_t *lidp;
978 for (lidp = xfs_trans_first_item(tp);
979 lidp != NULL;
980 lidp = xfs_trans_next_item(tp, lidp)) {
982 * Unpin all but those that aren't dirty.
984 if (lidp->lid_flags & XFS_LID_DIRTY)
985 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
988 xfs_trans_unreserve_and_mod_sb(tp);
989 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
991 xfs_trans_free_items(tp, flags);
992 xfs_trans_free_busy(tp);
993 xfs_trans_free(tp);
997 * Fill in the vector with pointers to data to be logged
998 * by this transaction. The transaction header takes
999 * the first vector, and then each dirty item takes the
1000 * number of vectors it indicated it needed in xfs_trans_count_vecs().
1002 * As each item fills in the entries it needs, also pin the item
1003 * so that it cannot be flushed out until the log write completes.
1005 STATIC void
1006 xfs_trans_fill_vecs(
1007 xfs_trans_t *tp,
1008 xfs_log_iovec_t *log_vector)
1010 xfs_log_item_desc_t *lidp;
1011 xfs_log_iovec_t *vecp;
1012 uint nitems;
1015 * Skip over the entry for the transaction header, we'll
1016 * fill that in at the end.
1018 vecp = log_vector + 1; /* pointer arithmetic */
1020 nitems = 0;
1021 lidp = xfs_trans_first_item(tp);
1022 ASSERT(lidp != NULL);
1023 while (lidp != NULL) {
1025 * Skip items which aren't dirty in this transaction.
1027 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1028 lidp = xfs_trans_next_item(tp, lidp);
1029 continue;
1032 * The item may be marked dirty but not log anything.
1033 * This can be used to get called when a transaction
1034 * is committed.
1036 if (lidp->lid_size) {
1037 nitems++;
1039 IOP_FORMAT(lidp->lid_item, vecp);
1040 vecp += lidp->lid_size; /* pointer arithmetic */
1041 IOP_PIN(lidp->lid_item);
1042 lidp = xfs_trans_next_item(tp, lidp);
1046 * Now that we've counted the number of items in this
1047 * transaction, fill in the transaction header.
1049 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1050 tp->t_header.th_type = tp->t_type;
1051 tp->t_header.th_num_items = nitems;
1052 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1053 log_vector->i_len = sizeof(xfs_trans_header_t);
1058 * Unlock all of the transaction's items and free the transaction.
1059 * The transaction must not have modified any of its items, because
1060 * there is no way to restore them to their previous state.
1062 * If the transaction has made a log reservation, make sure to release
1063 * it as well.
1065 void
1066 xfs_trans_cancel(
1067 xfs_trans_t *tp,
1068 int flags)
1070 int log_flags;
1071 #ifdef DEBUG
1072 xfs_log_item_chunk_t *licp;
1073 xfs_log_item_desc_t *lidp;
1074 xfs_log_item_t *lip;
1075 int i;
1076 #endif
1079 * See if the caller is being too lazy to figure out if
1080 * the transaction really needs an abort.
1082 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1083 flags &= ~XFS_TRANS_ABORT;
1085 * See if the caller is relying on us to shut down the
1086 * filesystem. This happens in paths where we detect
1087 * corruption and decide to give up.
1089 if ((tp->t_flags & XFS_TRANS_DIRTY) &&
1090 !XFS_FORCED_SHUTDOWN(tp->t_mountp))
1091 xfs_force_shutdown(tp->t_mountp, XFS_CORRUPT_INCORE);
1092 #ifdef DEBUG
1093 if (!(flags & XFS_TRANS_ABORT)) {
1094 licp = &(tp->t_items);
1095 while (licp != NULL) {
1096 lidp = licp->lic_descs;
1097 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1098 if (XFS_LIC_ISFREE(licp, i)) {
1099 continue;
1102 lip = lidp->lid_item;
1103 if (!XFS_FORCED_SHUTDOWN(tp->t_mountp))
1104 ASSERT(!(lip->li_type == XFS_LI_EFD));
1106 licp = licp->lic_next;
1109 #endif
1110 xfs_trans_unreserve_and_mod_sb(tp);
1111 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
1113 if (tp->t_ticket) {
1114 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1115 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1116 log_flags = XFS_LOG_REL_PERM_RESERV;
1117 } else {
1118 log_flags = 0;
1120 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
1123 /* mark this thread as no longer being in a transaction */
1124 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
1126 xfs_trans_free_items(tp, flags);
1127 xfs_trans_free_busy(tp);
1128 xfs_trans_free(tp);
1133 * Free the transaction structure. If there is more clean up
1134 * to do when the structure is freed, add it here.
1136 STATIC void
1137 xfs_trans_free(
1138 xfs_trans_t *tp)
1140 atomic_dec(&tp->t_mountp->m_active_trans);
1141 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
1142 kmem_zone_free(xfs_trans_zone, tp);
1147 * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
1149 * This is typically called by the LM when a transaction has been fully
1150 * committed to disk. It needs to unpin the items which have
1151 * been logged by the transaction and update their positions
1152 * in the AIL if necessary.
1153 * This also gets called when the transactions didn't get written out
1154 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1156 * Call xfs_trans_chunk_committed() to process the items in
1157 * each chunk.
1159 STATIC void
1160 xfs_trans_committed(
1161 xfs_trans_t *tp,
1162 int abortflag)
1164 xfs_log_item_chunk_t *licp;
1165 xfs_log_item_chunk_t *next_licp;
1166 xfs_log_busy_chunk_t *lbcp;
1167 xfs_log_busy_slot_t *lbsp;
1168 int i;
1171 * Call the transaction's completion callback if there
1172 * is one.
1174 if (tp->t_callback != NULL) {
1175 tp->t_callback(tp, tp->t_callarg);
1179 * Special case the chunk embedded in the transaction.
1181 licp = &(tp->t_items);
1182 if (!(XFS_LIC_ARE_ALL_FREE(licp))) {
1183 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1187 * Process the items in each chunk in turn.
1189 licp = licp->lic_next;
1190 while (licp != NULL) {
1191 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
1192 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1193 next_licp = licp->lic_next;
1194 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
1195 licp = next_licp;
1199 * Clear all the per-AG busy list items listed in this transaction
1201 lbcp = &tp->t_busy;
1202 while (lbcp != NULL) {
1203 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1204 if (!XFS_LBC_ISFREE(lbcp, i)) {
1205 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1206 lbsp->lbc_idx);
1209 lbcp = lbcp->lbc_next;
1211 xfs_trans_free_busy(tp);
1214 * That's it for the transaction structure. Free it.
1216 xfs_trans_free(tp);
1220 * This is called to perform the commit processing for each
1221 * item described by the given chunk.
1223 * The commit processing consists of unlocking items which were
1224 * held locked with the SYNC_UNLOCK attribute, calling the committed
1225 * routine of each logged item, updating the item's position in the AIL
1226 * if necessary, and unpinning each item. If the committed routine
1227 * returns -1, then do nothing further with the item because it
1228 * may have been freed.
1230 * Since items are unlocked when they are copied to the incore
1231 * log, it is possible for two transactions to be completing
1232 * and manipulating the same item simultaneously. The AIL lock
1233 * will protect the lsn field of each item. The value of this
1234 * field can never go backwards.
1236 * We unpin the items after repositioning them in the AIL, because
1237 * otherwise they could be immediately flushed and we'd have to race
1238 * with the flusher trying to pull the item from the AIL as we add it.
1240 STATIC void
1241 xfs_trans_chunk_committed(
1242 xfs_log_item_chunk_t *licp,
1243 xfs_lsn_t lsn,
1244 int aborted)
1246 xfs_log_item_desc_t *lidp;
1247 xfs_log_item_t *lip;
1248 xfs_lsn_t item_lsn;
1249 struct xfs_mount *mp;
1250 int i;
1251 SPLDECL(s);
1253 lidp = licp->lic_descs;
1254 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1255 if (XFS_LIC_ISFREE(licp, i)) {
1256 continue;
1259 lip = lidp->lid_item;
1260 if (aborted)
1261 lip->li_flags |= XFS_LI_ABORTED;
1264 * Send in the ABORTED flag to the COMMITTED routine
1265 * so that it knows whether the transaction was aborted
1266 * or not.
1268 item_lsn = IOP_COMMITTED(lip, lsn);
1271 * If the committed routine returns -1, make
1272 * no more references to the item.
1274 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
1275 continue;
1279 * If the returned lsn is greater than what it
1280 * contained before, update the location of the
1281 * item in the AIL. If it is not, then do nothing.
1282 * Items can never move backwards in the AIL.
1284 * While the new lsn should usually be greater, it
1285 * is possible that a later transaction completing
1286 * simultaneously with an earlier one using the
1287 * same item could complete first with a higher lsn.
1288 * This would cause the earlier transaction to fail
1289 * the test below.
1291 mp = lip->li_mountp;
1292 AIL_LOCK(mp,s);
1293 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1295 * This will set the item's lsn to item_lsn
1296 * and update the position of the item in
1297 * the AIL.
1299 * xfs_trans_update_ail() drops the AIL lock.
1301 xfs_trans_update_ail(mp, lip, item_lsn, s);
1302 } else {
1303 AIL_UNLOCK(mp, s);
1307 * Now that we've repositioned the item in the AIL,
1308 * unpin it so it can be flushed. Pass information
1309 * about buffer stale state down from the log item
1310 * flags, if anyone else stales the buffer we do not
1311 * want to pay any attention to it.
1313 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);