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"
48 kmem_zone_t *xfs_trans_zone;
51 * Reservation functions here avoid a huge stack in xfs_trans_init
52 * due to register overflow from temporaries in the calculations.
55 xfs_calc_write_reservation(xfs_mount_t *mp)
57 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
61 xfs_calc_itruncate_reservation(xfs_mount_t *mp)
63 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
67 xfs_calc_rename_reservation(xfs_mount_t *mp)
69 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
73 xfs_calc_link_reservation(xfs_mount_t *mp)
75 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
79 xfs_calc_remove_reservation(xfs_mount_t *mp)
81 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
85 xfs_calc_symlink_reservation(xfs_mount_t *mp)
87 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
91 xfs_calc_create_reservation(xfs_mount_t *mp)
93 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
97 xfs_calc_mkdir_reservation(xfs_mount_t *mp)
99 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
103 xfs_calc_ifree_reservation(xfs_mount_t *mp)
105 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
109 xfs_calc_ichange_reservation(xfs_mount_t *mp)
111 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
115 xfs_calc_growdata_reservation(xfs_mount_t *mp)
117 return XFS_CALC_GROWDATA_LOG_RES(mp);
121 xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
123 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
127 xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
129 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
133 xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
135 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
139 xfs_calc_swrite_reservation(xfs_mount_t *mp)
141 return XFS_CALC_SWRITE_LOG_RES(mp);
145 xfs_calc_writeid_reservation(xfs_mount_t *mp)
147 return XFS_CALC_WRITEID_LOG_RES(mp);
151 xfs_calc_addafork_reservation(xfs_mount_t *mp)
153 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
157 xfs_calc_attrinval_reservation(xfs_mount_t *mp)
159 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
163 xfs_calc_attrset_reservation(xfs_mount_t *mp)
165 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
169 xfs_calc_attrrm_reservation(xfs_mount_t *mp)
171 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
175 xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
177 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
181 * Initialize the precomputed transaction reservation values
182 * in the mount structure.
188 xfs_trans_reservations_t *resp;
190 resp = &(mp->m_reservations);
191 resp->tr_write = xfs_calc_write_reservation(mp);
192 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
193 resp->tr_rename = xfs_calc_rename_reservation(mp);
194 resp->tr_link = xfs_calc_link_reservation(mp);
195 resp->tr_remove = xfs_calc_remove_reservation(mp);
196 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
197 resp->tr_create = xfs_calc_create_reservation(mp);
198 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
199 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
200 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
201 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
202 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
203 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
204 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
205 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
206 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
207 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
208 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
209 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
210 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
211 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
215 * This routine is called to allocate a transaction structure.
216 * The type parameter indicates the type of the transaction. These
217 * are enumerated in xfs_trans.h.
219 * Dynamically allocate the transaction structure from the transaction
220 * zone, initialize it, and return it to the caller.
227 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
228 return _xfs_trans_alloc(mp, type, KM_SLEEP);
239 atomic_inc(&mp->m_active_trans);
241 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
242 tp->t_magic = XFS_TRANS_MAGIC;
245 tp->t_items_free = XFS_LIC_NUM_SLOTS;
246 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
247 xfs_lic_init(&(tp->t_items));
248 XFS_LBC_INIT(&(tp->t_busy));
253 * Free the transaction structure. If there is more clean up
254 * to do when the structure is freed, add it here.
260 atomic_dec(&tp->t_mountp->m_active_trans);
261 xfs_trans_free_dqinfo(tp);
262 kmem_zone_free(xfs_trans_zone, tp);
266 * This is called to create a new transaction which will share the
267 * permanent log reservation of the given transaction. The remaining
268 * unused block and rt extent reservations are also inherited. This
269 * implies that the original transaction is no longer allowed to allocate
270 * blocks. Locks and log items, however, are no inherited. They must
271 * be added to the new transaction explicitly.
279 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
282 * Initialize the new transaction structure.
284 ntp->t_magic = XFS_TRANS_MAGIC;
285 ntp->t_type = tp->t_type;
286 ntp->t_mountp = tp->t_mountp;
287 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
288 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
289 xfs_lic_init(&(ntp->t_items));
290 XFS_LBC_INIT(&(ntp->t_busy));
292 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
293 ASSERT(tp->t_ticket != NULL);
295 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
296 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
297 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
298 tp->t_blk_res = tp->t_blk_res_used;
299 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
300 tp->t_rtx_res = tp->t_rtx_res_used;
301 ntp->t_pflags = tp->t_pflags;
303 xfs_trans_dup_dqinfo(tp, ntp);
305 atomic_inc(&tp->t_mountp->m_active_trans);
310 * This is called to reserve free disk blocks and log space for the
311 * given transaction. This must be done before allocating any resources
312 * within the transaction.
314 * This will return ENOSPC if there are not enough blocks available.
315 * It will sleep waiting for available log space.
316 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
317 * is used by long running transactions. If any one of the reservations
318 * fails then they will all be backed out.
320 * This does not do quota reservations. That typically is done by the
334 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
336 /* Mark this thread as being in a transaction */
337 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
340 * Attempt to reserve the needed disk blocks by decrementing
341 * the number needed from the number available. This will
342 * fail if the count would go below zero.
345 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
346 -((int64_t)blocks), rsvd);
348 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
349 return (XFS_ERROR(ENOSPC));
351 tp->t_blk_res += blocks;
355 * Reserve the log space needed for this transaction.
358 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
359 ASSERT((tp->t_log_count == 0) ||
360 (tp->t_log_count == logcount));
361 if (flags & XFS_TRANS_PERM_LOG_RES) {
362 log_flags = XFS_LOG_PERM_RESERV;
363 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
365 ASSERT(tp->t_ticket == NULL);
366 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
370 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
372 XFS_TRANSACTION, log_flags, tp->t_type);
376 tp->t_log_res = logspace;
377 tp->t_log_count = logcount;
381 * Attempt to reserve the needed realtime extents by decrementing
382 * the number needed from the number available. This will
383 * fail if the count would go below zero.
386 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
387 -((int64_t)rtextents), rsvd);
389 error = XFS_ERROR(ENOSPC);
392 tp->t_rtx_res += rtextents;
398 * Error cases jump to one of these labels to undo any
399 * reservations which have already been performed.
403 if (flags & XFS_TRANS_PERM_LOG_RES) {
404 log_flags = XFS_LOG_REL_PERM_RESERV;
408 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
411 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
416 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
417 (int64_t)blocks, rsvd);
421 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
428 * Record the indicated change to the given field for application
429 * to the file system's superblock when the transaction commits.
430 * For now, just store the change in the transaction structure.
432 * Mark the transaction structure to indicate that the superblock
433 * needs to be updated before committing.
435 * Because we may not be keeping track of allocated/free inodes and
436 * used filesystem blocks in the superblock, we do not mark the
437 * superblock dirty in this transaction if we modify these fields.
438 * We still need to update the transaction deltas so that they get
439 * applied to the incore superblock, but we don't want them to
440 * cause the superblock to get locked and logged if these are the
441 * only fields in the superblock that the transaction modifies.
449 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
450 xfs_mount_t *mp = tp->t_mountp;
453 case XFS_TRANS_SB_ICOUNT:
454 tp->t_icount_delta += delta;
455 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
456 flags &= ~XFS_TRANS_SB_DIRTY;
458 case XFS_TRANS_SB_IFREE:
459 tp->t_ifree_delta += delta;
460 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
461 flags &= ~XFS_TRANS_SB_DIRTY;
463 case XFS_TRANS_SB_FDBLOCKS:
465 * Track the number of blocks allocated in the
466 * transaction. Make sure it does not exceed the
470 tp->t_blk_res_used += (uint)-delta;
471 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
473 tp->t_fdblocks_delta += delta;
474 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
475 flags &= ~XFS_TRANS_SB_DIRTY;
477 case XFS_TRANS_SB_RES_FDBLOCKS:
479 * The allocation has already been applied to the
480 * in-core superblock's counter. This should only
481 * be applied to the on-disk superblock.
484 tp->t_res_fdblocks_delta += delta;
485 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
486 flags &= ~XFS_TRANS_SB_DIRTY;
488 case XFS_TRANS_SB_FREXTENTS:
490 * Track the number of blocks allocated in the
491 * transaction. Make sure it does not exceed the
495 tp->t_rtx_res_used += (uint)-delta;
496 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
498 tp->t_frextents_delta += delta;
500 case XFS_TRANS_SB_RES_FREXTENTS:
502 * The allocation has already been applied to the
503 * in-core superblock's counter. This should only
504 * be applied to the on-disk superblock.
507 tp->t_res_frextents_delta += delta;
509 case XFS_TRANS_SB_DBLOCKS:
511 tp->t_dblocks_delta += delta;
513 case XFS_TRANS_SB_AGCOUNT:
515 tp->t_agcount_delta += delta;
517 case XFS_TRANS_SB_IMAXPCT:
518 tp->t_imaxpct_delta += delta;
520 case XFS_TRANS_SB_REXTSIZE:
521 tp->t_rextsize_delta += delta;
523 case XFS_TRANS_SB_RBMBLOCKS:
524 tp->t_rbmblocks_delta += delta;
526 case XFS_TRANS_SB_RBLOCKS:
527 tp->t_rblocks_delta += delta;
529 case XFS_TRANS_SB_REXTENTS:
530 tp->t_rextents_delta += delta;
532 case XFS_TRANS_SB_REXTSLOG:
533 tp->t_rextslog_delta += delta;
540 tp->t_flags |= flags;
544 * xfs_trans_apply_sb_deltas() is called from the commit code
545 * to bring the superblock buffer into the current transaction
546 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
548 * For now we just look at each field allowed to change and change
552 xfs_trans_apply_sb_deltas(
559 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
560 sbp = XFS_BUF_TO_SBP(bp);
563 * Check that superblock mods match the mods made to AGF counters.
565 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
566 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
567 tp->t_ag_btree_delta));
570 * Only update the superblock counters if we are logging them
572 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
573 if (tp->t_icount_delta)
574 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
575 if (tp->t_ifree_delta)
576 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
577 if (tp->t_fdblocks_delta)
578 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
579 if (tp->t_res_fdblocks_delta)
580 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
583 if (tp->t_frextents_delta)
584 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
585 if (tp->t_res_frextents_delta)
586 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
588 if (tp->t_dblocks_delta) {
589 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
592 if (tp->t_agcount_delta) {
593 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
596 if (tp->t_imaxpct_delta) {
597 sbp->sb_imax_pct += tp->t_imaxpct_delta;
600 if (tp->t_rextsize_delta) {
601 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
604 if (tp->t_rbmblocks_delta) {
605 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
608 if (tp->t_rblocks_delta) {
609 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
612 if (tp->t_rextents_delta) {
613 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
616 if (tp->t_rextslog_delta) {
617 sbp->sb_rextslog += tp->t_rextslog_delta;
623 * Log the whole thing, the fields are noncontiguous.
625 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
628 * Since all the modifiable fields are contiguous, we
629 * can get away with this.
631 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
632 offsetof(xfs_dsb_t, sb_frextents) +
633 sizeof(sbp->sb_frextents) - 1);
637 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
638 * and apply superblock counter changes to the in-core superblock. The
639 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
640 * applied to the in-core superblock. The idea is that that has already been
643 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
644 * However, we have to ensure that we only modify each superblock field only
645 * once because the application of the delta values may not be atomic. That can
646 * lead to ENOSPC races occurring if we have two separate modifcations of the
647 * free space counter to put back the entire reservation and then take away
650 * If we are not logging superblock counters, then the inode allocated/free and
651 * used block counts are not updated in the on disk superblock. In this case,
652 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
653 * still need to update the incore superblock with the changes.
656 xfs_trans_unreserve_and_mod_sb(
659 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
661 xfs_mount_t *mp = tp->t_mountp;
665 int64_t blkdelta = 0;
666 int64_t rtxdelta = 0;
669 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
671 /* calculate free blocks delta */
672 if (tp->t_blk_res > 0)
673 blkdelta = tp->t_blk_res;
675 if ((tp->t_fdblocks_delta != 0) &&
676 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
677 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
678 blkdelta += tp->t_fdblocks_delta;
681 msbp->msb_field = XFS_SBS_FDBLOCKS;
682 msbp->msb_delta = blkdelta;
686 /* calculate free realtime extents delta */
687 if (tp->t_rtx_res > 0)
688 rtxdelta = tp->t_rtx_res;
690 if ((tp->t_frextents_delta != 0) &&
691 (tp->t_flags & XFS_TRANS_SB_DIRTY))
692 rtxdelta += tp->t_frextents_delta;
695 msbp->msb_field = XFS_SBS_FREXTENTS;
696 msbp->msb_delta = rtxdelta;
700 /* apply remaining deltas */
702 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
703 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
704 if (tp->t_icount_delta != 0) {
705 msbp->msb_field = XFS_SBS_ICOUNT;
706 msbp->msb_delta = tp->t_icount_delta;
709 if (tp->t_ifree_delta != 0) {
710 msbp->msb_field = XFS_SBS_IFREE;
711 msbp->msb_delta = tp->t_ifree_delta;
716 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
717 if (tp->t_dblocks_delta != 0) {
718 msbp->msb_field = XFS_SBS_DBLOCKS;
719 msbp->msb_delta = tp->t_dblocks_delta;
722 if (tp->t_agcount_delta != 0) {
723 msbp->msb_field = XFS_SBS_AGCOUNT;
724 msbp->msb_delta = tp->t_agcount_delta;
727 if (tp->t_imaxpct_delta != 0) {
728 msbp->msb_field = XFS_SBS_IMAX_PCT;
729 msbp->msb_delta = tp->t_imaxpct_delta;
732 if (tp->t_rextsize_delta != 0) {
733 msbp->msb_field = XFS_SBS_REXTSIZE;
734 msbp->msb_delta = tp->t_rextsize_delta;
737 if (tp->t_rbmblocks_delta != 0) {
738 msbp->msb_field = XFS_SBS_RBMBLOCKS;
739 msbp->msb_delta = tp->t_rbmblocks_delta;
742 if (tp->t_rblocks_delta != 0) {
743 msbp->msb_field = XFS_SBS_RBLOCKS;
744 msbp->msb_delta = tp->t_rblocks_delta;
747 if (tp->t_rextents_delta != 0) {
748 msbp->msb_field = XFS_SBS_REXTENTS;
749 msbp->msb_delta = tp->t_rextents_delta;
752 if (tp->t_rextslog_delta != 0) {
753 msbp->msb_field = XFS_SBS_REXTSLOG;
754 msbp->msb_delta = tp->t_rextslog_delta;
760 * If we need to change anything, do it.
763 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
764 (uint)(msbp - msb), rsvd);
770 * Total up the number of log iovecs needed to commit this
771 * transaction. The transaction itself needs one for the
772 * transaction header. Ask each dirty item in turn how many
773 * it needs to get the total.
776 xfs_trans_count_vecs(
777 struct xfs_trans *tp)
780 xfs_log_item_desc_t *lidp;
783 lidp = xfs_trans_first_item(tp);
784 ASSERT(lidp != NULL);
786 /* In the non-debug case we need to start bailing out if we
787 * didn't find a log_item here, return zero and let trans_commit
793 while (lidp != NULL) {
795 * Skip items which aren't dirty in this transaction.
797 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
798 lidp = xfs_trans_next_item(tp, lidp);
801 lidp->lid_size = IOP_SIZE(lidp->lid_item);
802 nvecs += lidp->lid_size;
803 lidp = xfs_trans_next_item(tp, lidp);
810 * Fill in the vector with pointers to data to be logged
811 * by this transaction. The transaction header takes
812 * the first vector, and then each dirty item takes the
813 * number of vectors it indicated it needed in xfs_trans_count_vecs().
815 * As each item fills in the entries it needs, also pin the item
816 * so that it cannot be flushed out until the log write completes.
820 struct xfs_trans *tp,
821 struct xfs_log_iovec *log_vector)
823 xfs_log_item_desc_t *lidp;
824 struct xfs_log_iovec *vecp;
828 * Skip over the entry for the transaction header, we'll
829 * fill that in at the end.
831 vecp = log_vector + 1;
834 lidp = xfs_trans_first_item(tp);
837 /* Skip items which aren't dirty in this transaction. */
838 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
839 lidp = xfs_trans_next_item(tp, lidp);
844 * The item may be marked dirty but not log anything. This can
845 * be used to get called when a transaction is committed.
849 IOP_FORMAT(lidp->lid_item, vecp);
850 vecp += lidp->lid_size;
851 IOP_PIN(lidp->lid_item);
852 lidp = xfs_trans_next_item(tp, lidp);
856 * Now that we've counted the number of items in this transaction, fill
857 * in the transaction header. Note that the transaction header does not
860 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
861 tp->t_header.th_type = tp->t_type;
862 tp->t_header.th_num_items = nitems;
863 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
864 log_vector->i_len = sizeof(xfs_trans_header_t);
865 log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
869 * The committed item processing consists of calling the committed routine of
870 * each logged item, updating the item's position in the AIL if necessary, and
871 * unpinning each item. If the committed routine returns -1, then do nothing
872 * further with the item because it may have been freed.
874 * Since items are unlocked when they are copied to the incore log, it is
875 * possible for two transactions to be completing and manipulating the same
876 * item simultaneously. The AIL lock will protect the lsn field of each item.
877 * The value of this field can never go backwards.
879 * We unpin the items after repositioning them in the AIL, because otherwise
880 * they could be immediately flushed and we'd have to race with the flusher
881 * trying to pull the item from the AIL as we add it.
884 xfs_trans_item_committed(
885 struct xfs_log_item *lip,
886 xfs_lsn_t commit_lsn,
890 struct xfs_ail *ailp;
893 lip->li_flags |= XFS_LI_ABORTED;
894 item_lsn = IOP_COMMITTED(lip, commit_lsn);
896 /* If the committed routine returns -1, item has been freed. */
897 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
901 * If the returned lsn is greater than what it contained before, update
902 * the location of the item in the AIL. If it is not, then do nothing.
903 * Items can never move backwards in the AIL.
905 * While the new lsn should usually be greater, it is possible that a
906 * later transaction completing simultaneously with an earlier one
907 * using the same item could complete first with a higher lsn. This
908 * would cause the earlier transaction to fail the test below.
911 spin_lock(&ailp->xa_lock);
912 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
914 * This will set the item's lsn to item_lsn and update the
915 * position of the item in the AIL.
917 * xfs_trans_ail_update() drops the AIL lock.
919 xfs_trans_ail_update(ailp, lip, item_lsn);
921 spin_unlock(&ailp->xa_lock);
925 * Now that we've repositioned the item in the AIL, unpin it so it can
926 * be flushed. Pass information about buffer stale state down from the
927 * log item flags, if anyone else stales the buffer we do not want to
928 * pay any attention to it.
933 /* Clear all the per-AG busy list items listed in this transaction */
935 xfs_trans_clear_busy_extents(
936 struct xfs_trans *tp)
938 xfs_log_busy_chunk_t *lbcp;
939 xfs_log_busy_slot_t *lbsp;
942 for (lbcp = &tp->t_busy; lbcp != NULL; lbcp = lbcp->lbc_next) {
944 for (lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
945 if (XFS_LBC_ISFREE(lbcp, i))
947 xfs_alloc_clear_busy(tp, lbsp->lbc_ag, lbsp->lbc_idx);
950 xfs_trans_free_busy(tp);
954 * This is typically called by the LM when a transaction has been fully
955 * committed to disk. It needs to unpin the items which have
956 * been logged by the transaction and update their positions
957 * in the AIL if necessary.
959 * This also gets called when the transactions didn't get written out
960 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
964 struct xfs_trans *tp,
967 xfs_log_item_desc_t *lidp;
968 xfs_log_item_chunk_t *licp;
969 xfs_log_item_chunk_t *next_licp;
971 /* Call the transaction's completion callback if there is one. */
972 if (tp->t_callback != NULL)
973 tp->t_callback(tp, tp->t_callarg);
975 for (lidp = xfs_trans_first_item(tp);
977 lidp = xfs_trans_next_item(tp, lidp)) {
978 xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
981 /* free the item chunks, ignoring the embedded chunk */
982 for (licp = tp->t_items.lic_next; licp != NULL; licp = next_licp) {
983 next_licp = licp->lic_next;
987 xfs_trans_clear_busy_extents(tp);
992 * Called from the trans_commit code when we notice that
993 * the filesystem is in the middle of a forced shutdown.
997 struct xfs_trans *tp,
1000 xfs_log_item_desc_t *lidp;
1002 for (lidp = xfs_trans_first_item(tp);
1004 lidp = xfs_trans_next_item(tp, lidp)) {
1006 * Unpin all but those that aren't dirty.
1008 if (lidp->lid_flags & XFS_LID_DIRTY)
1009 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1012 xfs_trans_unreserve_and_mod_sb(tp);
1013 xfs_trans_unreserve_and_mod_dquots(tp);
1015 xfs_trans_free_items(tp, flags);
1016 xfs_trans_free_busy(tp);
1021 * Format the transaction direct to the iclog. This isolates the physical
1022 * transaction commit operation from the logical operation and hence allows
1023 * other methods to be introduced without affecting the existing commit path.
1026 xfs_trans_commit_iclog(
1027 struct xfs_mount *mp,
1028 struct xfs_trans *tp,
1029 xfs_lsn_t *commit_lsn,
1035 struct xlog_in_core *commit_iclog;
1036 #define XFS_TRANS_LOGVEC_COUNT 16
1037 struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
1038 struct xfs_log_iovec *log_vector;
1043 * Ask each log item how many log_vector entries it will
1044 * need so we can figure out how many to allocate.
1045 * Try to avoid the kmem_alloc() call in the common case
1046 * by using a vector from the stack when it fits.
1048 nvec = xfs_trans_count_vecs(tp);
1050 return ENOMEM; /* triggers a shutdown! */
1051 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
1052 log_vector = log_vector_fast;
1054 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
1055 sizeof(xfs_log_iovec_t),
1060 * Fill in the log_vector and pin the logged items, and
1061 * then write the transaction to the log.
1063 xfs_trans_fill_vecs(tp, log_vector);
1065 if (flags & XFS_TRANS_RELEASE_LOG_RES)
1066 log_flags = XFS_LOG_REL_PERM_RESERV;
1068 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
1071 * The transaction is committed incore here, and can go out to disk
1072 * at any time after this call. However, all the items associated
1073 * with the transaction are still locked and pinned in memory.
1075 *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
1077 tp->t_commit_lsn = *commit_lsn;
1078 if (nvec > XFS_TRANS_LOGVEC_COUNT)
1079 kmem_free(log_vector);
1082 * If we got a log write error. Unpin the logitems that we
1083 * had pinned, clean up, free trans structure, and return error.
1085 if (error || *commit_lsn == -1) {
1086 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1087 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
1088 return XFS_ERROR(EIO);
1092 * Once the transaction has committed, unused
1093 * reservations need to be released and changes to
1094 * the superblock need to be reflected in the in-core
1095 * version. Do that now.
1097 xfs_trans_unreserve_and_mod_sb(tp);
1100 * Tell the LM to call the transaction completion routine
1101 * when the log write with LSN commit_lsn completes (e.g.
1102 * when the transaction commit really hits the on-disk log).
1103 * After this call we cannot reference tp, because the call
1104 * can happen at any time and the call will free the transaction
1105 * structure pointed to by tp. The only case where we call
1106 * the completion routine (xfs_trans_committed) directly is
1107 * if the log is turned off on a debug kernel or we're
1108 * running in simulation mode (the log is explicitly turned
1111 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
1112 tp->t_logcb.cb_arg = tp;
1115 * We need to pass the iclog buffer which was used for the
1116 * transaction commit record into this function, and attach
1117 * the callback to it. The callback must be attached before
1118 * the items are unlocked to avoid racing with other threads
1119 * waiting for an item to unlock.
1121 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
1124 * Mark this thread as no longer being in a transaction
1126 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1129 * Once all the items of the transaction have been copied
1130 * to the in core log and the callback is attached, the
1131 * items can be unlocked.
1133 * This will free descriptors pointing to items which were
1134 * not logged since there is nothing more to do with them.
1135 * For items which were logged, we will keep pointers to them
1136 * so they can be unpinned after the transaction commits to disk.
1137 * This will also stamp each modified meta-data item with
1138 * the commit lsn of this transaction for dependency tracking
1141 xfs_trans_unlock_items(tp, *commit_lsn);
1144 * If we detected a log error earlier, finish committing
1145 * the transaction now (unpin log items, etc).
1147 * Order is critical here, to avoid using the transaction
1148 * pointer after its been freed (by xfs_trans_committed
1149 * either here now, or as a callback). We cannot do this
1150 * step inside xfs_log_notify as was done earlier because
1154 xfs_trans_committed(tp, XFS_LI_ABORTED);
1157 * Now that the xfs_trans_committed callback has been attached,
1158 * and the items are released we can finally allow the iclog to
1161 return xfs_log_release_iclog(mp, commit_iclog);
1168 * Commit the given transaction to the log a/synchronously.
1170 * XFS disk error handling mechanism is not based on a typical
1171 * transaction abort mechanism. Logically after the filesystem
1172 * gets marked 'SHUTDOWN', we can't let any new transactions
1173 * be durable - ie. committed to disk - because some metadata might
1174 * be inconsistent. In such cases, this returns an error, and the
1175 * caller may assume that all locked objects joined to the transaction
1176 * have already been unlocked as if the commit had succeeded.
1177 * Do not reference the transaction structure after this call.
1181 struct xfs_trans *tp,
1185 struct xfs_mount *mp = tp->t_mountp;
1186 xfs_lsn_t commit_lsn = -1;
1189 int sync = tp->t_flags & XFS_TRANS_SYNC;
1192 * Determine whether this commit is releasing a permanent
1193 * log reservation or not.
1195 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1196 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1197 log_flags = XFS_LOG_REL_PERM_RESERV;
1201 * If there is nothing to be logged by the transaction,
1202 * then unlock all of the items associated with the
1203 * transaction and free the transaction structure.
1204 * Also make sure to return any reserved blocks to
1207 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1210 if (XFS_FORCED_SHUTDOWN(mp)) {
1211 error = XFS_ERROR(EIO);
1215 ASSERT(tp->t_ticket != NULL);
1218 * If we need to update the superblock, then do it now.
1220 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1221 xfs_trans_apply_sb_deltas(tp);
1222 xfs_trans_apply_dquot_deltas(tp);
1224 error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
1225 if (error == ENOMEM) {
1226 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1227 error = XFS_ERROR(EIO);
1232 * If the transaction needs to be synchronous, then force the
1233 * log out now and wait for it.
1237 error = _xfs_log_force_lsn(mp, commit_lsn,
1238 XFS_LOG_SYNC, log_flushed);
1240 XFS_STATS_INC(xs_trans_sync);
1242 XFS_STATS_INC(xs_trans_async);
1248 xfs_trans_unreserve_and_mod_sb(tp);
1251 * It is indeed possible for the transaction to be not dirty but
1252 * the dqinfo portion to be. All that means is that we have some
1253 * (non-persistent) quota reservations that need to be unreserved.
1255 xfs_trans_unreserve_and_mod_dquots(tp);
1257 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1258 if (commit_lsn == -1 && !error)
1259 error = XFS_ERROR(EIO);
1261 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1262 xfs_trans_free_items(tp, error ? XFS_TRANS_ABORT : 0);
1263 xfs_trans_free_busy(tp);
1266 XFS_STATS_INC(xs_trans_empty);
1271 * Unlock all of the transaction's items and free the transaction.
1272 * The transaction must not have modified any of its items, because
1273 * there is no way to restore them to their previous state.
1275 * If the transaction has made a log reservation, make sure to release
1285 xfs_log_item_chunk_t *licp;
1286 xfs_log_item_desc_t *lidp;
1287 xfs_log_item_t *lip;
1290 xfs_mount_t *mp = tp->t_mountp;
1293 * See if the caller is being too lazy to figure out if
1294 * the transaction really needs an abort.
1296 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1297 flags &= ~XFS_TRANS_ABORT;
1299 * See if the caller is relying on us to shut down the
1300 * filesystem. This happens in paths where we detect
1301 * corruption and decide to give up.
1303 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
1304 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1305 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1308 if (!(flags & XFS_TRANS_ABORT)) {
1309 licp = &(tp->t_items);
1310 while (licp != NULL) {
1311 lidp = licp->lic_descs;
1312 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1313 if (xfs_lic_isfree(licp, i)) {
1317 lip = lidp->lid_item;
1318 if (!XFS_FORCED_SHUTDOWN(mp))
1319 ASSERT(!(lip->li_type == XFS_LI_EFD));
1321 licp = licp->lic_next;
1325 xfs_trans_unreserve_and_mod_sb(tp);
1326 xfs_trans_unreserve_and_mod_dquots(tp);
1329 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1330 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1331 log_flags = XFS_LOG_REL_PERM_RESERV;
1335 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1338 /* mark this thread as no longer being in a transaction */
1339 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1341 xfs_trans_free_items(tp, flags);
1342 xfs_trans_free_busy(tp);
1347 * Roll from one trans in the sequence of PERMANENT transactions to
1348 * the next: permanent transactions are only flushed out when
1349 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1350 * as possible to let chunks of it go to the log. So we commit the
1351 * chunk we've been working on and get a new transaction to continue.
1355 struct xfs_trans **tpp,
1356 struct xfs_inode *dp)
1358 struct xfs_trans *trans;
1359 unsigned int logres, count;
1363 * Ensure that the inode is always logged.
1366 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1369 * Copy the critical parameters from one trans to the next.
1371 logres = trans->t_log_res;
1372 count = trans->t_log_count;
1373 *tpp = xfs_trans_dup(trans);
1376 * Commit the current transaction.
1377 * If this commit failed, then it'd just unlock those items that
1378 * are not marked ihold. That also means that a filesystem shutdown
1379 * is in progress. The caller takes the responsibility to cancel
1380 * the duplicate transaction that gets returned.
1382 error = xfs_trans_commit(trans, 0);
1389 * transaction commit worked ok so we can drop the extra ticket
1390 * reference that we gained in xfs_trans_dup()
1392 xfs_log_ticket_put(trans->t_ticket);
1396 * Reserve space in the log for th next transaction.
1397 * This also pushes items in the "AIL", the list of logged items,
1398 * out to disk if they are taking up space at the tail of the log
1399 * that we want to use. This requires that either nothing be locked
1400 * across this call, or that anything that is locked be logged in
1401 * the prior and the next transactions.
1403 error = xfs_trans_reserve(trans, 0, logres, 0,
1404 XFS_TRANS_PERM_LOG_RES, count);
1406 * Ensure that the inode is in the new transaction and locked.
1411 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1412 xfs_trans_ihold(trans, dp);