2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * 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.
14 * 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
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_error.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_alloc.h"
43 #include "xfs_quota.h"
44 #include "xfs_trans_priv.h"
45 #include "xfs_trans_space.h"
46 #include "xfs_inode_item.h"
49 STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *);
50 STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
51 STATIC void xfs_trans_committed(xfs_trans_t *, int);
52 STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
53 STATIC void xfs_trans_free(xfs_trans_t *);
55 kmem_zone_t *xfs_trans_zone;
59 * Reservation functions here avoid a huge stack in xfs_trans_init
60 * due to register overflow from temporaries in the calculations.
64 xfs_calc_write_reservation(xfs_mount_t *mp)
66 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
70 xfs_calc_itruncate_reservation(xfs_mount_t *mp)
72 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
76 xfs_calc_rename_reservation(xfs_mount_t *mp)
78 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
82 xfs_calc_link_reservation(xfs_mount_t *mp)
84 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
88 xfs_calc_remove_reservation(xfs_mount_t *mp)
90 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
94 xfs_calc_symlink_reservation(xfs_mount_t *mp)
96 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
100 xfs_calc_create_reservation(xfs_mount_t *mp)
102 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
106 xfs_calc_mkdir_reservation(xfs_mount_t *mp)
108 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
112 xfs_calc_ifree_reservation(xfs_mount_t *mp)
114 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
118 xfs_calc_ichange_reservation(xfs_mount_t *mp)
120 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
124 xfs_calc_growdata_reservation(xfs_mount_t *mp)
126 return XFS_CALC_GROWDATA_LOG_RES(mp);
130 xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
132 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
136 xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
138 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
142 xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
144 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
148 xfs_calc_swrite_reservation(xfs_mount_t *mp)
150 return XFS_CALC_SWRITE_LOG_RES(mp);
154 xfs_calc_writeid_reservation(xfs_mount_t *mp)
156 return XFS_CALC_WRITEID_LOG_RES(mp);
160 xfs_calc_addafork_reservation(xfs_mount_t *mp)
162 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
166 xfs_calc_attrinval_reservation(xfs_mount_t *mp)
168 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
172 xfs_calc_attrset_reservation(xfs_mount_t *mp)
174 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
178 xfs_calc_attrrm_reservation(xfs_mount_t *mp)
180 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
184 xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
186 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
190 * Initialize the precomputed transaction reservation values
191 * in the mount structure.
197 xfs_trans_reservations_t *resp;
199 resp = &(mp->m_reservations);
200 resp->tr_write = xfs_calc_write_reservation(mp);
201 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
202 resp->tr_rename = xfs_calc_rename_reservation(mp);
203 resp->tr_link = xfs_calc_link_reservation(mp);
204 resp->tr_remove = xfs_calc_remove_reservation(mp);
205 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
206 resp->tr_create = xfs_calc_create_reservation(mp);
207 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
208 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
209 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
210 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
211 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
212 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
213 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
214 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
215 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
216 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
217 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
218 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
219 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
220 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
224 * This routine is called to allocate a transaction structure.
225 * The type parameter indicates the type of the transaction. These
226 * are enumerated in xfs_trans.h.
228 * Dynamically allocate the transaction structure from the transaction
229 * zone, initialize it, and return it to the caller.
236 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
237 return _xfs_trans_alloc(mp, type, KM_SLEEP);
248 atomic_inc(&mp->m_active_trans);
250 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
251 tp->t_magic = XFS_TRANS_MAGIC;
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));
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.
275 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
278 * Initialize the new transaction structure.
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));
288 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
289 ASSERT(tp->t_ticket != NULL);
291 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
292 ntp->t_ticket = xfs_log_ticket_get(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;
297 ntp->t_pflags = tp->t_pflags;
299 xfs_trans_dup_dqinfo(tp, ntp);
301 atomic_inc(&tp->t_mountp->m_active_trans);
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.
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.
316 * This does not do quota reservations. That typically is done by the
330 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
332 /* Mark this thread as being in a transaction */
333 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
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.
341 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
342 -((int64_t)blocks), rsvd);
344 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
345 return (XFS_ERROR(ENOSPC));
347 tp->t_blk_res += blocks;
351 * Reserve the log space needed for this transaction.
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;
361 ASSERT(tp->t_ticket == NULL);
362 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
366 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
368 XFS_TRANSACTION, log_flags, tp->t_type);
372 tp->t_log_res = logspace;
373 tp->t_log_count = logcount;
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.
382 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
383 -((int64_t)rtextents), rsvd);
385 error = XFS_ERROR(ENOSPC);
388 tp->t_rtx_res += rtextents;
394 * Error cases jump to one of these labels to undo any
395 * reservations which have already been performed.
399 if (flags & XFS_TRANS_PERM_LOG_RES) {
400 log_flags = XFS_LOG_REL_PERM_RESERV;
404 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
407 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
412 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
413 (int64_t)blocks, rsvd);
417 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
424 * 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.
428 * Mark the transaction structure to indicate that the superblock
429 * needs to be updated before committing.
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.
445 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
446 xfs_mount_t *mp = tp->t_mountp;
449 case XFS_TRANS_SB_ICOUNT:
450 tp->t_icount_delta += delta;
451 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
452 flags &= ~XFS_TRANS_SB_DIRTY;
454 case XFS_TRANS_SB_IFREE:
455 tp->t_ifree_delta += delta;
456 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
457 flags &= ~XFS_TRANS_SB_DIRTY;
459 case XFS_TRANS_SB_FDBLOCKS:
461 * Track the number of blocks allocated in the
462 * transaction. Make sure it does not exceed the
466 tp->t_blk_res_used += (uint)-delta;
467 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
469 tp->t_fdblocks_delta += delta;
470 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
471 flags &= ~XFS_TRANS_SB_DIRTY;
473 case XFS_TRANS_SB_RES_FDBLOCKS:
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.
480 tp->t_res_fdblocks_delta += delta;
481 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
482 flags &= ~XFS_TRANS_SB_DIRTY;
484 case XFS_TRANS_SB_FREXTENTS:
486 * Track the number of blocks allocated in the
487 * transaction. Make sure it does not exceed the
491 tp->t_rtx_res_used += (uint)-delta;
492 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
494 tp->t_frextents_delta += delta;
496 case XFS_TRANS_SB_RES_FREXTENTS:
498 * The allocation has already been applied to the
499 * in-core superblock's counter. This should only
500 * be applied to the on-disk superblock.
503 tp->t_res_frextents_delta += delta;
505 case XFS_TRANS_SB_DBLOCKS:
507 tp->t_dblocks_delta += delta;
509 case XFS_TRANS_SB_AGCOUNT:
511 tp->t_agcount_delta += delta;
513 case XFS_TRANS_SB_IMAXPCT:
514 tp->t_imaxpct_delta += delta;
516 case XFS_TRANS_SB_REXTSIZE:
517 tp->t_rextsize_delta += delta;
519 case XFS_TRANS_SB_RBMBLOCKS:
520 tp->t_rbmblocks_delta += delta;
522 case XFS_TRANS_SB_RBLOCKS:
523 tp->t_rblocks_delta += delta;
525 case XFS_TRANS_SB_REXTENTS:
526 tp->t_rextents_delta += delta;
528 case XFS_TRANS_SB_REXTSLOG:
529 tp->t_rextslog_delta += delta;
536 tp->t_flags |= flags;
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().
544 * For now we just look at each field allowed to change and change
548 xfs_trans_apply_sb_deltas(
555 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
556 sbp = XFS_BUF_TO_SBP(bp);
559 * Check that superblock mods match the mods made to AGF counters.
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));
566 * Only update the superblock counters if we are logging them
568 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
569 if (tp->t_icount_delta)
570 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
571 if (tp->t_ifree_delta)
572 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
573 if (tp->t_fdblocks_delta)
574 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
575 if (tp->t_res_fdblocks_delta)
576 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
579 if (tp->t_frextents_delta)
580 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
581 if (tp->t_res_frextents_delta)
582 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
584 if (tp->t_dblocks_delta) {
585 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
588 if (tp->t_agcount_delta) {
589 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
592 if (tp->t_imaxpct_delta) {
593 sbp->sb_imax_pct += tp->t_imaxpct_delta;
596 if (tp->t_rextsize_delta) {
597 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
600 if (tp->t_rbmblocks_delta) {
601 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
604 if (tp->t_rblocks_delta) {
605 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
608 if (tp->t_rextents_delta) {
609 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
612 if (tp->t_rextslog_delta) {
613 sbp->sb_rextslog += tp->t_rextslog_delta;
619 * Log the whole thing, the fields are noncontiguous.
621 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
624 * Since all the modifiable fields are contiguous, we
625 * can get away with this.
627 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
628 offsetof(xfs_dsb_t, sb_frextents) +
629 sizeof(sbp->sb_frextents) - 1);
633 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
634 * and apply superblock counter changes to the in-core superblock. The
635 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
636 * applied to the in-core superblock. The idea is that that has already been
639 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
640 * However, we have to ensure that we only modify each superblock field only
641 * once because the application of the delta values may not be atomic. That can
642 * lead to ENOSPC races occurring if we have two separate modifcations of the
643 * free space counter to put back the entire reservation and then take away
646 * If we are not logging superblock counters, then the inode allocated/free and
647 * used block counts are not updated in the on disk superblock. In this case,
648 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
649 * still need to update the incore superblock with the changes.
652 xfs_trans_unreserve_and_mod_sb(
655 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
657 xfs_mount_t *mp = tp->t_mountp;
661 int64_t blkdelta = 0;
662 int64_t rtxdelta = 0;
665 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
667 /* calculate free blocks delta */
668 if (tp->t_blk_res > 0)
669 blkdelta = tp->t_blk_res;
671 if ((tp->t_fdblocks_delta != 0) &&
672 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
673 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
674 blkdelta += tp->t_fdblocks_delta;
677 msbp->msb_field = XFS_SBS_FDBLOCKS;
678 msbp->msb_delta = blkdelta;
682 /* calculate free realtime extents delta */
683 if (tp->t_rtx_res > 0)
684 rtxdelta = tp->t_rtx_res;
686 if ((tp->t_frextents_delta != 0) &&
687 (tp->t_flags & XFS_TRANS_SB_DIRTY))
688 rtxdelta += tp->t_frextents_delta;
691 msbp->msb_field = XFS_SBS_FREXTENTS;
692 msbp->msb_delta = rtxdelta;
696 /* apply remaining deltas */
698 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
699 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
700 if (tp->t_icount_delta != 0) {
701 msbp->msb_field = XFS_SBS_ICOUNT;
702 msbp->msb_delta = tp->t_icount_delta;
705 if (tp->t_ifree_delta != 0) {
706 msbp->msb_field = XFS_SBS_IFREE;
707 msbp->msb_delta = tp->t_ifree_delta;
712 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
713 if (tp->t_dblocks_delta != 0) {
714 msbp->msb_field = XFS_SBS_DBLOCKS;
715 msbp->msb_delta = tp->t_dblocks_delta;
718 if (tp->t_agcount_delta != 0) {
719 msbp->msb_field = XFS_SBS_AGCOUNT;
720 msbp->msb_delta = tp->t_agcount_delta;
723 if (tp->t_imaxpct_delta != 0) {
724 msbp->msb_field = XFS_SBS_IMAX_PCT;
725 msbp->msb_delta = tp->t_imaxpct_delta;
728 if (tp->t_rextsize_delta != 0) {
729 msbp->msb_field = XFS_SBS_REXTSIZE;
730 msbp->msb_delta = tp->t_rextsize_delta;
733 if (tp->t_rbmblocks_delta != 0) {
734 msbp->msb_field = XFS_SBS_RBMBLOCKS;
735 msbp->msb_delta = tp->t_rbmblocks_delta;
738 if (tp->t_rblocks_delta != 0) {
739 msbp->msb_field = XFS_SBS_RBLOCKS;
740 msbp->msb_delta = tp->t_rblocks_delta;
743 if (tp->t_rextents_delta != 0) {
744 msbp->msb_field = XFS_SBS_REXTENTS;
745 msbp->msb_delta = tp->t_rextents_delta;
748 if (tp->t_rextslog_delta != 0) {
749 msbp->msb_field = XFS_SBS_REXTSLOG;
750 msbp->msb_delta = tp->t_rextslog_delta;
756 * If we need to change anything, do it.
759 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
760 (uint)(msbp - msb), rsvd);
766 * Total up the number of log iovecs needed to commit this
767 * transaction. The transaction itself needs one for the
768 * transaction header. Ask each dirty item in turn how many
769 * it needs to get the total.
772 xfs_trans_count_vecs(
776 xfs_log_item_desc_t *lidp;
779 lidp = xfs_trans_first_item(tp);
780 ASSERT(lidp != NULL);
782 /* In the non-debug case we need to start bailing out if we
783 * didn't find a log_item here, return zero and let trans_commit
789 while (lidp != NULL) {
791 * Skip items which aren't dirty in this transaction.
793 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
794 lidp = xfs_trans_next_item(tp, lidp);
797 lidp->lid_size = IOP_SIZE(lidp->lid_item);
798 nvecs += lidp->lid_size;
799 lidp = xfs_trans_next_item(tp, lidp);
806 * Fill in the vector with pointers to data to be logged
807 * by this transaction. The transaction header takes
808 * the first vector, and then each dirty item takes the
809 * number of vectors it indicated it needed in xfs_trans_count_vecs().
811 * As each item fills in the entries it needs, also pin the item
812 * so that it cannot be flushed out until the log write completes.
816 struct xfs_trans *tp,
817 struct xfs_log_iovec *log_vector)
819 xfs_log_item_desc_t *lidp;
820 struct xfs_log_iovec *vecp;
824 * Skip over the entry for the transaction header, we'll
825 * fill that in at the end.
827 vecp = log_vector + 1;
830 lidp = xfs_trans_first_item(tp);
833 /* Skip items which aren't dirty in this transaction. */
834 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
835 lidp = xfs_trans_next_item(tp, lidp);
840 * The item may be marked dirty but not log anything. This can
841 * be used to get called when a transaction is committed.
845 IOP_FORMAT(lidp->lid_item, vecp);
846 vecp += lidp->lid_size;
847 IOP_PIN(lidp->lid_item);
848 lidp = xfs_trans_next_item(tp, lidp);
852 * Now that we've counted the number of items in this transaction, fill
853 * in the transaction header. Note that the transaction header does not
856 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
857 tp->t_header.th_type = tp->t_type;
858 tp->t_header.th_num_items = nitems;
859 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
860 log_vector->i_len = sizeof(xfs_trans_header_t);
861 log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
865 * Format the transaction direct to the iclog. This isolates the physical
866 * transaction commit operation from the logical operation and hence allows
867 * other methods to be introduced without affecting the existing commit path.
870 xfs_trans_commit_iclog(
871 struct xfs_mount *mp,
872 struct xfs_trans *tp,
873 xfs_lsn_t *commit_lsn,
879 struct xlog_in_core *commit_iclog;
880 #define XFS_TRANS_LOGVEC_COUNT 16
881 struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
882 struct xfs_log_iovec *log_vector;
887 * Ask each log item how many log_vector entries it will
888 * need so we can figure out how many to allocate.
889 * Try to avoid the kmem_alloc() call in the common case
890 * by using a vector from the stack when it fits.
892 nvec = xfs_trans_count_vecs(tp);
894 return ENOMEM; /* triggers a shutdown! */
895 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
896 log_vector = log_vector_fast;
898 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
899 sizeof(xfs_log_iovec_t),
904 * Fill in the log_vector and pin the logged items, and
905 * then write the transaction to the log.
907 xfs_trans_fill_vecs(tp, log_vector);
909 if (flags & XFS_TRANS_RELEASE_LOG_RES)
910 log_flags = XFS_LOG_REL_PERM_RESERV;
912 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
915 * The transaction is committed incore here, and can go out to disk
916 * at any time after this call. However, all the items associated
917 * with the transaction are still locked and pinned in memory.
919 *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
921 tp->t_commit_lsn = *commit_lsn;
922 if (nvec > XFS_TRANS_LOGVEC_COUNT)
923 kmem_free(log_vector);
926 * If we got a log write error. Unpin the logitems that we
927 * had pinned, clean up, free trans structure, and return error.
929 if (error || *commit_lsn == -1) {
930 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
931 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
932 return XFS_ERROR(EIO);
936 * Once the transaction has committed, unused
937 * reservations need to be released and changes to
938 * the superblock need to be reflected in the in-core
939 * version. Do that now.
941 xfs_trans_unreserve_and_mod_sb(tp);
944 * Tell the LM to call the transaction completion routine
945 * when the log write with LSN commit_lsn completes (e.g.
946 * when the transaction commit really hits the on-disk log).
947 * After this call we cannot reference tp, because the call
948 * can happen at any time and the call will free the transaction
949 * structure pointed to by tp. The only case where we call
950 * the completion routine (xfs_trans_committed) directly is
951 * if the log is turned off on a debug kernel or we're
952 * running in simulation mode (the log is explicitly turned
955 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
956 tp->t_logcb.cb_arg = tp;
959 * We need to pass the iclog buffer which was used for the
960 * transaction commit record into this function, and attach
961 * the callback to it. The callback must be attached before
962 * the items are unlocked to avoid racing with other threads
963 * waiting for an item to unlock.
965 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
968 * Mark this thread as no longer being in a transaction
970 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
973 * Once all the items of the transaction have been copied
974 * to the in core log and the callback is attached, the
975 * items can be unlocked.
977 * This will free descriptors pointing to items which were
978 * not logged since there is nothing more to do with them.
979 * For items which were logged, we will keep pointers to them
980 * so they can be unpinned after the transaction commits to disk.
981 * This will also stamp each modified meta-data item with
982 * the commit lsn of this transaction for dependency tracking
985 xfs_trans_unlock_items(tp, *commit_lsn);
988 * If we detected a log error earlier, finish committing
989 * the transaction now (unpin log items, etc).
991 * Order is critical here, to avoid using the transaction
992 * pointer after its been freed (by xfs_trans_committed
993 * either here now, or as a callback). We cannot do this
994 * step inside xfs_log_notify as was done earlier because
998 xfs_trans_committed(tp, XFS_LI_ABORTED);
1001 * Now that the xfs_trans_committed callback has been attached,
1002 * and the items are released we can finally allow the iclog to
1005 return xfs_log_release_iclog(mp, commit_iclog);
1012 * Commit the given transaction to the log a/synchronously.
1014 * XFS disk error handling mechanism is not based on a typical
1015 * transaction abort mechanism. Logically after the filesystem
1016 * gets marked 'SHUTDOWN', we can't let any new transactions
1017 * be durable - ie. committed to disk - because some metadata might
1018 * be inconsistent. In such cases, this returns an error, and the
1019 * caller may assume that all locked objects joined to the transaction
1020 * have already been unlocked as if the commit had succeeded.
1021 * Do not reference the transaction structure after this call.
1025 struct xfs_trans *tp,
1029 struct xfs_mount *mp = tp->t_mountp;
1030 xfs_lsn_t commit_lsn = -1;
1033 int sync = tp->t_flags & XFS_TRANS_SYNC;
1036 * Determine whether this commit is releasing a permanent
1037 * log reservation or not.
1039 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1040 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1041 log_flags = XFS_LOG_REL_PERM_RESERV;
1045 * If there is nothing to be logged by the transaction,
1046 * then unlock all of the items associated with the
1047 * transaction and free the transaction structure.
1048 * Also make sure to return any reserved blocks to
1051 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1054 if (XFS_FORCED_SHUTDOWN(mp)) {
1055 error = XFS_ERROR(EIO);
1059 ASSERT(tp->t_ticket != NULL);
1062 * If we need to update the superblock, then do it now.
1064 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1065 xfs_trans_apply_sb_deltas(tp);
1066 xfs_trans_apply_dquot_deltas(tp);
1068 error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
1069 if (error == ENOMEM) {
1070 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1071 error = XFS_ERROR(EIO);
1076 * If the transaction needs to be synchronous, then force the
1077 * log out now and wait for it.
1081 error = _xfs_log_force_lsn(mp, commit_lsn,
1082 XFS_LOG_SYNC, log_flushed);
1084 XFS_STATS_INC(xs_trans_sync);
1086 XFS_STATS_INC(xs_trans_async);
1092 xfs_trans_unreserve_and_mod_sb(tp);
1095 * It is indeed possible for the transaction to be not dirty but
1096 * the dqinfo portion to be. All that means is that we have some
1097 * (non-persistent) quota reservations that need to be unreserved.
1099 xfs_trans_unreserve_and_mod_dquots(tp);
1101 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1102 if (commit_lsn == -1 && !error)
1103 error = XFS_ERROR(EIO);
1105 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1106 xfs_trans_free_items(tp, error ? XFS_TRANS_ABORT : 0);
1107 xfs_trans_free_busy(tp);
1110 XFS_STATS_INC(xs_trans_empty);
1115 * Called from the trans_commit code when we notice that
1116 * the filesystem is in the middle of a forced shutdown.
1123 xfs_log_item_desc_t *lidp;
1125 for (lidp = xfs_trans_first_item(tp);
1127 lidp = xfs_trans_next_item(tp, lidp)) {
1129 * Unpin all but those that aren't dirty.
1131 if (lidp->lid_flags & XFS_LID_DIRTY)
1132 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1135 xfs_trans_unreserve_and_mod_sb(tp);
1136 xfs_trans_unreserve_and_mod_dquots(tp);
1138 xfs_trans_free_items(tp, flags);
1139 xfs_trans_free_busy(tp);
1144 * Unlock all of the transaction's items and free the transaction.
1145 * The transaction must not have modified any of its items, because
1146 * there is no way to restore them to their previous state.
1148 * If the transaction has made a log reservation, make sure to release
1158 xfs_log_item_chunk_t *licp;
1159 xfs_log_item_desc_t *lidp;
1160 xfs_log_item_t *lip;
1163 xfs_mount_t *mp = tp->t_mountp;
1166 * See if the caller is being too lazy to figure out if
1167 * the transaction really needs an abort.
1169 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1170 flags &= ~XFS_TRANS_ABORT;
1172 * See if the caller is relying on us to shut down the
1173 * filesystem. This happens in paths where we detect
1174 * corruption and decide to give up.
1176 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
1177 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1178 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1181 if (!(flags & XFS_TRANS_ABORT)) {
1182 licp = &(tp->t_items);
1183 while (licp != NULL) {
1184 lidp = licp->lic_descs;
1185 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1186 if (xfs_lic_isfree(licp, i)) {
1190 lip = lidp->lid_item;
1191 if (!XFS_FORCED_SHUTDOWN(mp))
1192 ASSERT(!(lip->li_type == XFS_LI_EFD));
1194 licp = licp->lic_next;
1198 xfs_trans_unreserve_and_mod_sb(tp);
1199 xfs_trans_unreserve_and_mod_dquots(tp);
1202 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1203 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1204 log_flags = XFS_LOG_REL_PERM_RESERV;
1208 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1211 /* mark this thread as no longer being in a transaction */
1212 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1214 xfs_trans_free_items(tp, flags);
1215 xfs_trans_free_busy(tp);
1221 * Free the transaction structure. If there is more clean up
1222 * to do when the structure is freed, add it here.
1228 atomic_dec(&tp->t_mountp->m_active_trans);
1229 xfs_trans_free_dqinfo(tp);
1230 kmem_zone_free(xfs_trans_zone, tp);
1234 * Roll from one trans in the sequence of PERMANENT transactions to
1235 * the next: permanent transactions are only flushed out when
1236 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1237 * as possible to let chunks of it go to the log. So we commit the
1238 * chunk we've been working on and get a new transaction to continue.
1242 struct xfs_trans **tpp,
1243 struct xfs_inode *dp)
1245 struct xfs_trans *trans;
1246 unsigned int logres, count;
1250 * Ensure that the inode is always logged.
1253 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1256 * Copy the critical parameters from one trans to the next.
1258 logres = trans->t_log_res;
1259 count = trans->t_log_count;
1260 *tpp = xfs_trans_dup(trans);
1263 * Commit the current transaction.
1264 * If this commit failed, then it'd just unlock those items that
1265 * are not marked ihold. That also means that a filesystem shutdown
1266 * is in progress. The caller takes the responsibility to cancel
1267 * the duplicate transaction that gets returned.
1269 error = xfs_trans_commit(trans, 0);
1276 * transaction commit worked ok so we can drop the extra ticket
1277 * reference that we gained in xfs_trans_dup()
1279 xfs_log_ticket_put(trans->t_ticket);
1283 * Reserve space in the log for th next transaction.
1284 * This also pushes items in the "AIL", the list of logged items,
1285 * out to disk if they are taking up space at the tail of the log
1286 * that we want to use. This requires that either nothing be locked
1287 * across this call, or that anything that is locked be logged in
1288 * the prior and the next transactions.
1290 error = xfs_trans_reserve(trans, 0, logres, 0,
1291 XFS_TRANS_PERM_LOG_RES, count);
1293 * Ensure that the inode is in the new transaction and locked.
1298 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1299 xfs_trans_ihold(trans, dp);
1304 * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
1306 * This is typically called by the LM when a transaction has been fully
1307 * committed to disk. It needs to unpin the items which have
1308 * been logged by the transaction and update their positions
1309 * in the AIL if necessary.
1310 * This also gets called when the transactions didn't get written out
1311 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1313 * Call xfs_trans_chunk_committed() to process the items in
1317 xfs_trans_committed(
1321 xfs_log_item_chunk_t *licp;
1322 xfs_log_item_chunk_t *next_licp;
1323 xfs_log_busy_chunk_t *lbcp;
1324 xfs_log_busy_slot_t *lbsp;
1328 * Call the transaction's completion callback if there
1331 if (tp->t_callback != NULL) {
1332 tp->t_callback(tp, tp->t_callarg);
1336 * Special case the chunk embedded in the transaction.
1338 licp = &(tp->t_items);
1339 if (!(xfs_lic_are_all_free(licp))) {
1340 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1344 * Process the items in each chunk in turn.
1346 licp = licp->lic_next;
1347 while (licp != NULL) {
1348 ASSERT(!xfs_lic_are_all_free(licp));
1349 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1350 next_licp = licp->lic_next;
1356 * Clear all the per-AG busy list items listed in this transaction
1359 while (lbcp != NULL) {
1360 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1361 if (!XFS_LBC_ISFREE(lbcp, i)) {
1362 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1366 lbcp = lbcp->lbc_next;
1368 xfs_trans_free_busy(tp);
1371 * That's it for the transaction structure. Free it.
1377 * This is called to perform the commit processing for each
1378 * item described by the given chunk.
1380 * The commit processing consists of unlocking items which were
1381 * held locked with the SYNC_UNLOCK attribute, calling the committed
1382 * routine of each logged item, updating the item's position in the AIL
1383 * if necessary, and unpinning each item. If the committed routine
1384 * returns -1, then do nothing further with the item because it
1385 * may have been freed.
1387 * Since items are unlocked when they are copied to the incore
1388 * log, it is possible for two transactions to be completing
1389 * and manipulating the same item simultaneously. The AIL lock
1390 * will protect the lsn field of each item. The value of this
1391 * field can never go backwards.
1393 * We unpin the items after repositioning them in the AIL, because
1394 * otherwise they could be immediately flushed and we'd have to race
1395 * with the flusher trying to pull the item from the AIL as we add it.
1398 xfs_trans_chunk_committed(
1399 xfs_log_item_chunk_t *licp,
1403 xfs_log_item_desc_t *lidp;
1404 xfs_log_item_t *lip;
1408 lidp = licp->lic_descs;
1409 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1410 struct xfs_ail *ailp;
1412 if (xfs_lic_isfree(licp, i)) {
1416 lip = lidp->lid_item;
1418 lip->li_flags |= XFS_LI_ABORTED;
1421 * Send in the ABORTED flag to the COMMITTED routine
1422 * so that it knows whether the transaction was aborted
1425 item_lsn = IOP_COMMITTED(lip, lsn);
1428 * If the committed routine returns -1, make
1429 * no more references to the item.
1431 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
1436 * If the returned lsn is greater than what it
1437 * contained before, update the location of the
1438 * item in the AIL. If it is not, then do nothing.
1439 * Items can never move backwards in the AIL.
1441 * While the new lsn should usually be greater, it
1442 * is possible that a later transaction completing
1443 * simultaneously with an earlier one using the
1444 * same item could complete first with a higher lsn.
1445 * This would cause the earlier transaction to fail
1448 ailp = lip->li_ailp;
1449 spin_lock(&ailp->xa_lock);
1450 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1452 * This will set the item's lsn to item_lsn
1453 * and update the position of the item in
1456 * xfs_trans_ail_update() drops the AIL lock.
1458 xfs_trans_ail_update(ailp, lip, item_lsn);
1460 spin_unlock(&ailp->xa_lock);
1464 * Now that we've repositioned the item in the AIL,
1465 * unpin it so it can be flushed. Pass information
1466 * about buffer stale state down from the log item
1467 * flags, if anyone else stales the buffer we do not
1468 * want to pay any attention to it.