2 * Copyright (c) 2000-2004 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:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * High level interface routines for log manager
38 #include "xfs_macros.h"
39 #include "xfs_types.h"
44 #include "xfs_trans.h"
46 #include "xfs_dmapi.h"
47 #include "xfs_mount.h"
48 #include "xfs_error.h"
49 #include "xfs_log_priv.h"
50 #include "xfs_buf_item.h"
51 #include "xfs_alloc_btree.h"
52 #include "xfs_log_recover.h"
55 #include "xfs_trans_priv.h"
58 #define xlog_write_adv_cnt(ptr, len, off, bytes) \
63 /* Local miscellaneous function prototypes */
64 STATIC int xlog_bdstrat_cb(struct xfs_buf *);
65 STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
66 xlog_in_core_t **, xfs_lsn_t *);
67 STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
68 xfs_buftarg_t *log_target,
69 xfs_daddr_t blk_offset,
71 STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
72 STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
73 STATIC void xlog_unalloc_log(xlog_t *log);
74 STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
75 int nentries, xfs_log_ticket_t tic,
77 xlog_in_core_t **commit_iclog,
80 /* local state machine functions */
81 STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
82 STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
83 STATIC int xlog_state_get_iclog_space(xlog_t *log,
85 xlog_in_core_t **iclog,
86 xlog_ticket_t *ticket,
89 STATIC void xlog_state_put_ticket(xlog_t *log,
91 STATIC int xlog_state_release_iclog(xlog_t *log,
92 xlog_in_core_t *iclog);
93 STATIC void xlog_state_switch_iclogs(xlog_t *log,
94 xlog_in_core_t *iclog,
96 STATIC int xlog_state_sync(xlog_t *log, xfs_lsn_t lsn, uint flags);
97 STATIC int xlog_state_sync_all(xlog_t *log, uint flags);
98 STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
100 /* local functions to manipulate grant head */
101 STATIC int xlog_grant_log_space(xlog_t *log,
102 xlog_ticket_t *xtic);
103 STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
105 STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
106 xlog_ticket_t *ticket);
107 STATIC int xlog_regrant_write_log_space(xlog_t *log,
108 xlog_ticket_t *ticket);
109 STATIC void xlog_ungrant_log_space(xlog_t *log,
110 xlog_ticket_t *ticket);
113 /* local ticket functions */
114 STATIC void xlog_state_ticket_alloc(xlog_t *log);
115 STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
120 STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
122 /* local debug functions */
123 #if defined(DEBUG) && !defined(XLOG_NOLOG)
124 STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
125 STATIC void xlog_verify_disk_cycle_no(xlog_t *log, xlog_in_core_t *iclog);
126 STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
127 STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
128 int count, boolean_t syncing);
129 STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
132 #define xlog_verify_dest_ptr(a,b)
133 #define xlog_verify_disk_cycle_no(a,b)
134 #define xlog_verify_grant_head(a,b)
135 #define xlog_verify_iclog(a,b,c,d)
136 #define xlog_verify_tail_lsn(a,b,c)
139 int xlog_iclogs_empty(xlog_t *log);
142 int xlog_do_error = 0;
143 int xlog_req_num = 0;
144 int xlog_error_mod = 33;
147 #define XLOG_FORCED_SHUTDOWN(log) (log->l_flags & XLOG_IO_ERROR)
150 * 0 => disable log manager
151 * 1 => enable log manager
152 * 2 => enable log manager and log debugging
154 #if defined(XLOG_NOLOG) || defined(DEBUG)
156 xfs_buftarg_t *xlog_target;
159 #if defined(XFS_LOG_TRACE)
162 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
164 if (! log->l_grant_trace) {
165 log->l_grant_trace = ktrace_alloc(1024, KM_NOSLEEP);
166 if (! log->l_grant_trace)
170 ktrace_enter(log->l_grant_trace,
172 (void *)log->l_reserve_headq,
173 (void *)log->l_write_headq,
174 (void *)((unsigned long)log->l_grant_reserve_cycle),
175 (void *)((unsigned long)log->l_grant_reserve_bytes),
176 (void *)((unsigned long)log->l_grant_write_cycle),
177 (void *)((unsigned long)log->l_grant_write_bytes),
178 (void *)((unsigned long)log->l_curr_cycle),
179 (void *)((unsigned long)log->l_curr_block),
180 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn, ARCH_NOCONVERT)),
181 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn, ARCH_NOCONVERT)),
183 (void *)((unsigned long)13),
184 (void *)((unsigned long)14),
185 (void *)((unsigned long)15),
186 (void *)((unsigned long)16));
190 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
196 if (!iclog->ic_trace)
197 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
198 ktrace_enter(iclog->ic_trace,
199 (void *)((unsigned long)state),
200 (void *)((unsigned long)pid),
218 #define xlog_trace_loggrant(log,tic,string)
219 #define xlog_trace_iclog(iclog,state)
220 #endif /* XFS_LOG_TRACE */
225 * 1. currblock field gets updated at startup and after in-core logs
226 * marked as with WANT_SYNC.
230 * This routine is called when a user of a log manager ticket is done with
231 * the reservation. If the ticket was ever used, then a commit record for
232 * the associated transaction is written out as a log operation header with
233 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
234 * a given ticket. If the ticket was one with a permanent reservation, then
235 * a few operations are done differently. Permanent reservation tickets by
236 * default don't release the reservation. They just commit the current
237 * transaction with the belief that the reservation is still needed. A flag
238 * must be passed in before permanent reservations are actually released.
239 * When these type of tickets are not released, they need to be set into
240 * the inited state again. By doing this, a start record will be written
241 * out when the next write occurs.
244 xfs_log_done(xfs_mount_t *mp,
245 xfs_log_ticket_t xtic,
249 xlog_t *log = mp->m_log;
250 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
253 #if defined(DEBUG) || defined(XLOG_NOLOG)
254 if (!xlog_debug && xlog_target == log->l_targ)
258 if (XLOG_FORCED_SHUTDOWN(log) ||
260 * If nothing was ever written, don't write out commit record.
261 * If we get an error, just continue and give back the log ticket.
263 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
264 (xlog_commit_record(mp, ticket,
265 (xlog_in_core_t **)iclog, &lsn)))) {
266 lsn = (xfs_lsn_t) -1;
267 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
268 flags |= XFS_LOG_REL_PERM_RESERV;
273 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
274 (flags & XFS_LOG_REL_PERM_RESERV)) {
276 * Release ticket if not permanent reservation or a specifc
277 * request has been made to release a permanent reservation.
279 xlog_ungrant_log_space(log, ticket);
280 xlog_state_put_ticket(log, ticket);
282 xlog_regrant_reserve_log_space(log, ticket);
285 /* If this ticket was a permanent reservation and we aren't
286 * trying to release it, reset the inited flags; so next time
287 * we write, a start record will be written out.
289 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
290 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
291 ticket->t_flags |= XLOG_TIC_INITED;
298 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
299 * the force is done synchronously.
301 * Asynchronous forces are implemented by setting the WANT_SYNC
302 * bit in the appropriate in-core log and then returning.
304 * Synchronous forces are implemented with a semaphore. All callers
305 * to force a given lsn to disk will wait on a semaphore attached to the
306 * specific in-core log. When given in-core log finally completes its
307 * write to disk, that thread will wake up all threads waiting on the
311 xfs_log_force(xfs_mount_t *mp,
316 xlog_t *log = mp->m_log;
318 #if defined(DEBUG) || defined(XLOG_NOLOG)
319 if (!xlog_debug && xlog_target == log->l_targ)
323 ASSERT(flags & XFS_LOG_FORCE);
325 XFS_STATS_INC(xs_log_force);
327 if ((log->l_flags & XLOG_IO_ERROR) == 0) {
329 rval = xlog_state_sync_all(log, flags);
331 rval = xlog_state_sync(log, lsn, flags);
333 rval = XFS_ERROR(EIO);
338 } /* xfs_log_force */
341 * Attaches a new iclog I/O completion callback routine during
342 * transaction commit. If the log is in error state, a non-zero
343 * return code is handed back and the caller is responsible for
344 * executing the callback at an appropriate time.
347 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
348 void *iclog_hndl, /* iclog to hang callback off */
349 xfs_log_callback_t *cb)
351 xlog_t *log = mp->m_log;
352 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
355 #if defined(DEBUG) || defined(XLOG_NOLOG)
356 if (!xlog_debug && xlog_target == log->l_targ)
361 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
363 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
364 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
366 *(iclog->ic_callback_tail) = cb;
367 iclog->ic_callback_tail = &(cb->cb_next);
369 LOG_UNLOCK(log, spl);
371 } /* xfs_log_notify */
374 xfs_log_release_iclog(xfs_mount_t *mp,
377 xlog_t *log = mp->m_log;
378 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
380 if (xlog_state_release_iclog(log, iclog)) {
381 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
389 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
390 * to the reservation.
391 * 2. Potentially, push buffers at tail of log to disk.
393 * Each reservation is going to reserve extra space for a log record header.
394 * When writes happen to the on-disk log, we don't subtract the length of the
395 * log record header from any reservation. By wasting space in each
396 * reservation, we prevent over allocation problems.
399 xfs_log_reserve(xfs_mount_t *mp,
402 xfs_log_ticket_t *ticket,
406 xlog_t *log = mp->m_log;
407 xlog_ticket_t *internal_ticket;
410 #if defined(DEBUG) || defined(XLOG_NOLOG)
411 if (!xlog_debug && xlog_target == log->l_targ)
415 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
416 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
418 if (XLOG_FORCED_SHUTDOWN(log))
419 return XFS_ERROR(EIO);
421 XFS_STATS_INC(xs_try_logspace);
423 if (*ticket != NULL) {
424 ASSERT(flags & XFS_LOG_PERM_RESERV);
425 internal_ticket = (xlog_ticket_t *)*ticket;
426 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
427 retval = xlog_regrant_write_log_space(log, internal_ticket);
429 /* may sleep if need to allocate more tickets */
430 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
432 *ticket = internal_ticket;
433 xlog_grant_push_ail(mp,
434 (internal_ticket->t_unit_res *
435 internal_ticket->t_cnt));
436 retval = xlog_grant_log_space(log, internal_ticket);
440 } /* xfs_log_reserve */
444 * Mount a log filesystem
446 * mp - ubiquitous xfs mount point structure
447 * log_target - buftarg of on-disk log device
448 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
449 * num_bblocks - Number of BBSIZE blocks in on-disk log
451 * Return error or zero.
454 xfs_log_mount(xfs_mount_t *mp,
455 xfs_buftarg_t *log_target,
456 xfs_daddr_t blk_offset,
459 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
460 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
463 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
465 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
468 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
470 #if defined(DEBUG) || defined(XLOG_NOLOG)
472 cmn_err(CE_NOTE, "log dev: %s", XFS_BUFTARG_NAME(log_target));
477 * skip log recovery on a norecovery mount. pretend it all
480 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
482 vfs_t *vfsp = XFS_MTOVFS(mp);
483 int readonly = (vfsp->vfs_flag & VFS_RDONLY);
486 vfsp->vfs_flag &= ~VFS_RDONLY;
488 error = xlog_recover(mp->m_log, readonly);
491 vfsp->vfs_flag |= VFS_RDONLY;
493 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
494 xlog_unalloc_log(mp->m_log);
499 /* Normal transactions can now occur */
500 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
502 /* End mounting message in xfs_log_mount_finish */
504 } /* xfs_log_mount */
507 * Finish the recovery of the file system. This is separate from
508 * the xfs_log_mount() call, because it depends on the code in
509 * xfs_mountfs() to read in the root and real-time bitmap inodes
510 * between calling xfs_log_mount() and here.
512 * mp - ubiquitous xfs mount point structure
515 xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
519 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
520 error = xlog_recover_finish(mp->m_log, mfsi_flags);
523 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
530 * Unmount processing for the log.
533 xfs_log_unmount(xfs_mount_t *mp)
537 error = xfs_log_unmount_write(mp);
538 xfs_log_unmount_dealloc(mp);
543 * Final log writes as part of unmount.
545 * Mark the filesystem clean as unmount happens. Note that during relocation
546 * this routine needs to be executed as part of source-bag while the
547 * deallocation must not be done until source-end.
551 * Unmount record used to have a string "Unmount filesystem--" in the
552 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
553 * We just write the magic number now since that particular field isn't
554 * currently architecture converted and "nUmount" is a bit foo.
555 * As far as I know, there weren't any dependencies on the old behaviour.
559 xfs_log_unmount_write(xfs_mount_t *mp)
561 xlog_t *log = mp->m_log;
562 xlog_in_core_t *iclog;
564 xlog_in_core_t *first_iclog;
566 xfs_log_iovec_t reg[1];
567 xfs_log_ticket_t tic = 0;
572 /* the data section must be 32 bit size aligned */
576 __uint32_t pad2; /* may as well make it 64 bits */
577 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
579 #if defined(DEBUG) || defined(XLOG_NOLOG)
580 if (!xlog_debug && xlog_target == log->l_targ)
585 * Don't write out unmount record on read-only mounts.
586 * Or, if we are doing a forced umount (typically because of IO errors).
588 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
591 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
594 first_iclog = iclog = log->l_iclog;
596 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
597 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
598 ASSERT(iclog->ic_offset == 0);
600 iclog = iclog->ic_next;
601 } while (iclog != first_iclog);
603 if (! (XLOG_FORCED_SHUTDOWN(log))) {
604 reg[0].i_addr = (void*)&magic;
605 reg[0].i_len = sizeof(magic);
607 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0);
609 /* remove inited flag */
610 ((xlog_ticket_t *)tic)->t_flags = 0;
611 error = xlog_write(mp, reg, 1, tic, &lsn,
612 NULL, XLOG_UNMOUNT_TRANS);
614 * At this point, we're umounting anyway,
615 * so there's no point in transitioning log state
616 * to IOERROR. Just continue...
621 xfs_fs_cmn_err(CE_ALERT, mp,
622 "xfs_log_unmount: unmount record failed");
627 iclog = log->l_iclog;
630 xlog_state_want_sync(log, iclog);
631 (void) xlog_state_release_iclog(log, iclog);
634 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
635 iclog->ic_state == XLOG_STATE_DIRTY)) {
636 if (!XLOG_FORCED_SHUTDOWN(log)) {
637 sv_wait(&iclog->ic_forcesema, PMEM,
638 &log->l_icloglock, s);
646 xlog_state_put_ticket(log, tic);
649 * We're already in forced_shutdown mode, couldn't
650 * even attempt to write out the unmount transaction.
652 * Go through the motions of sync'ing and releasing
653 * the iclog, even though no I/O will actually happen,
654 * we need to wait for other log I/O's that may already
655 * be in progress. Do this as a separate section of
656 * code so we'll know if we ever get stuck here that
657 * we're in this odd situation of trying to unmount
658 * a file system that went into forced_shutdown as
659 * the result of an unmount..
662 iclog = log->l_iclog;
666 xlog_state_want_sync(log, iclog);
667 (void) xlog_state_release_iclog(log, iclog);
671 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
672 || iclog->ic_state == XLOG_STATE_DIRTY
673 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
675 sv_wait(&iclog->ic_forcesema, PMEM,
676 &log->l_icloglock, s);
683 } /* xfs_log_unmount_write */
686 * Deallocate log structures for unmount/relocation.
689 xfs_log_unmount_dealloc(xfs_mount_t *mp)
691 xlog_unalloc_log(mp->m_log);
695 * Write region vectors to log. The write happens using the space reservation
696 * of the ticket (tic). It is not a requirement that all writes for a given
697 * transaction occur with one call to xfs_log_write().
700 xfs_log_write(xfs_mount_t * mp,
701 xfs_log_iovec_t reg[],
703 xfs_log_ticket_t tic,
704 xfs_lsn_t *start_lsn)
707 xlog_t *log = mp->m_log;
709 #if defined(DEBUG) || defined(XLOG_NOLOG)
710 if (!xlog_debug && xlog_target == log->l_targ) {
715 if (XLOG_FORCED_SHUTDOWN(log))
716 return XFS_ERROR(EIO);
718 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
719 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
722 } /* xfs_log_write */
726 xfs_log_move_tail(xfs_mount_t *mp,
730 xlog_t *log = mp->m_log;
731 int need_bytes, free_bytes, cycle, bytes;
734 #if defined(DEBUG) || defined(XLOG_NOLOG)
735 if (!xlog_debug && xlog_target == log->l_targ)
739 if (XLOG_FORCED_SHUTDOWN(log))
741 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
744 /* needed since sync_lsn is 64 bits */
746 tail_lsn = log->l_last_sync_lsn;
752 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
756 log->l_tail_lsn = tail_lsn;
759 if ((tic = log->l_write_headq)) {
761 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
762 panic("Recovery problem");
764 cycle = log->l_grant_write_cycle;
765 bytes = log->l_grant_write_bytes;
766 free_bytes = xlog_space_left(log, cycle, bytes);
768 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
770 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
773 free_bytes -= tic->t_unit_res;
774 sv_signal(&tic->t_sema);
776 } while (tic != log->l_write_headq);
778 if ((tic = log->l_reserve_headq)) {
780 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
781 panic("Recovery problem");
783 cycle = log->l_grant_reserve_cycle;
784 bytes = log->l_grant_reserve_bytes;
785 free_bytes = xlog_space_left(log, cycle, bytes);
787 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
788 need_bytes = tic->t_unit_res*tic->t_cnt;
790 need_bytes = tic->t_unit_res;
791 if (free_bytes < need_bytes && tail_lsn != 1)
794 free_bytes -= need_bytes;
795 sv_signal(&tic->t_sema);
797 } while (tic != log->l_reserve_headq);
799 GRANT_UNLOCK(log, s);
800 } /* xfs_log_move_tail */
803 * Determine if we have a transaction that has gone to disk
804 * that needs to be covered. Log activity needs to be idle (no AIL and
805 * nothing in the iclogs). And, we need to be in the right state indicating
806 * something has gone out.
809 xfs_log_need_covered(xfs_mount_t *mp)
813 xlog_t *log = mp->m_log;
814 vfs_t *vfsp = XFS_MTOVFS(mp);
816 if (vfsp->vfs_super->s_frozen || XFS_FORCED_SHUTDOWN(mp) ||
817 (vfsp->vfs_flag & VFS_RDONLY))
821 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
822 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
823 && !xfs_trans_first_ail(mp, &gen)
824 && xlog_iclogs_empty(log)) {
825 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
826 log->l_covered_state = XLOG_STATE_COVER_DONE;
828 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
829 log->l_covered_state = XLOG_STATE_COVER_DONE2;
837 /******************************************************************************
841 ******************************************************************************
844 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
845 * The log manager must keep track of the last LR which was committed
846 * to disk. The lsn of this LR will become the new tail_lsn whenever
847 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
848 * the situation where stuff could be written into the log but nothing
849 * was ever in the AIL when asked. Eventually, we panic since the
850 * tail hits the head.
852 * We may be holding the log iclog lock upon entering this routine.
855 xlog_assign_tail_lsn(xfs_mount_t *mp)
859 xlog_t *log = mp->m_log;
861 tail_lsn = xfs_trans_tail_ail(mp);
864 log->l_tail_lsn = tail_lsn;
866 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
868 GRANT_UNLOCK(log, s);
871 } /* xlog_assign_tail_lsn */
875 * Return the space in the log between the tail and the head. The head
876 * is passed in the cycle/bytes formal parms. In the special case where
877 * the reserve head has wrapped passed the tail, this calculation is no
878 * longer valid. In this case, just return 0 which means there is no space
879 * in the log. This works for all places where this function is called
880 * with the reserve head. Of course, if the write head were to ever
881 * wrap the tail, we should blow up. Rather than catch this case here,
882 * we depend on other ASSERTions in other parts of the code. XXXmiken
884 * This code also handles the case where the reservation head is behind
885 * the tail. The details of this case are described below, but the end
886 * result is that we return the size of the log as the amount of space left.
889 xlog_space_left(xlog_t *log, int cycle, int bytes)
895 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn, ARCH_NOCONVERT));
896 tail_cycle = CYCLE_LSN(log->l_tail_lsn, ARCH_NOCONVERT);
897 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
898 free_bytes = log->l_logsize - (bytes - tail_bytes);
899 } else if ((tail_cycle + 1) < cycle) {
901 } else if (tail_cycle < cycle) {
902 ASSERT(tail_cycle == (cycle - 1));
903 free_bytes = tail_bytes - bytes;
906 * The reservation head is behind the tail.
907 * This can only happen when the AIL is empty so the tail
908 * is equal to the head and the l_roundoff value in the
909 * log structure is taking up the difference between the
910 * reservation head and the tail. The bytes accounted for
911 * by the l_roundoff field are temporarily 'lost' to the
912 * reservation mechanism, but they are cleaned up when the
913 * log buffers that created them are reused. These lost
914 * bytes are what allow the reservation head to fall behind
915 * the tail in the case that the log is 'empty'.
916 * In this case we just want to return the size of the
917 * log as the amount of space left.
919 ASSERT((tail_cycle == (cycle + 1)) ||
920 ((bytes + log->l_roundoff) >= tail_bytes));
921 free_bytes = log->l_logsize;
924 } /* xlog_space_left */
928 * Log function which is called when an io completes.
930 * The log manager needs its own routine, in order to control what
931 * happens with the buffer after the write completes.
934 xlog_iodone(xfs_buf_t *bp)
936 xlog_in_core_t *iclog;
940 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
941 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
942 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
946 * Some versions of cpp barf on the recursive definition of
947 * ic_log -> hic_fields.ic_log and expand ic_log twice when
948 * it is passed through two macros. Workaround broken cpp.
953 * Race to shutdown the filesystem if we see an error.
955 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
956 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
957 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
959 xfs_force_shutdown(l->l_mp, XFS_LOG_IO_ERROR);
961 * This flag will be propagated to the trans-committed
962 * callback routines to let them know that the log-commit
965 aborted = XFS_LI_ABORTED;
966 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
967 aborted = XFS_LI_ABORTED;
969 xlog_state_done_syncing(iclog, aborted);
970 if (!(XFS_BUF_ISASYNC(bp))) {
972 * Corresponding psema() will be done in bwrite(). If we don't
973 * vsema() here, panic.
975 XFS_BUF_V_IODONESEMA(bp);
980 * The bdstrat callback function for log bufs. This gives us a central
981 * place to trap bufs in case we get hit by a log I/O error and need to
982 * shutdown. Actually, in practice, even when we didn't get a log error,
983 * we transition the iclogs to IOERROR state *after* flushing all existing
984 * iclogs to disk. This is because we don't want anymore new transactions to be
985 * started or completed afterwards.
988 xlog_bdstrat_cb(struct xfs_buf *bp)
990 xlog_in_core_t *iclog;
992 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
994 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
995 /* note for irix bstrat will need struct bdevsw passed
996 * Fix the following macro if the code ever is merged
1002 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1003 XFS_BUF_ERROR(bp, EIO);
1006 return (XFS_ERROR(EIO));
1012 * Return size of each in-core log record buffer.
1014 * Low memory machines only get 2 16KB buffers. We don't want to waste
1015 * memory here. However, all other machines get at least 2 32KB buffers.
1016 * The number is hard coded because we don't care about the minimum
1017 * memory size, just 32MB systems.
1019 * If the filesystem blocksize is too large, we may need to choose a
1020 * larger size since the directory code currently logs entire blocks.
1024 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1030 #if defined(DEBUG) || defined(XLOG_NOLOG)
1032 * When logbufs == 0, someone has disabled the log from the FSTAB
1033 * file. This is not a documented feature. We need to set xlog_debug
1034 * to zero (this deactivates the log) and set xlog_target to the
1035 * appropriate device. Only one filesystem may be affected as such
1036 * since this is just a performance hack to test what we might be able
1037 * to get if the log were not present.
1039 if (mp->m_logbufs == 0) {
1041 xlog_target = log->l_targ;
1042 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1047 * This is the normal path. If m_logbufs == -1, then the
1048 * admin has chosen to use the system defaults for logbuffers.
1050 if (mp->m_logbufs == -1) {
1051 if (xfs_physmem <= btoc(128*1024*1024)) {
1052 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1053 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1054 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1056 /* 256K with 32K bufs */
1057 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1060 log->l_iclog_bufs = mp->m_logbufs;
1062 #if defined(DEBUG) || defined(XLOG_NOLOG)
1063 /* We are reactivating a filesystem after it was inactive */
1064 if (log->l_targ == xlog_target) {
1072 * Buffer size passed in from mount system call.
1074 if (mp->m_logbsize != -1) {
1075 size = log->l_iclog_size = mp->m_logbsize;
1076 log->l_iclog_size_log = 0;
1078 log->l_iclog_size_log++;
1082 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1083 /* # headers = size / 32K
1084 * one header holds cycles from 32K of data
1087 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1088 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1090 log->l_iclog_hsize = xhdrs << BBSHIFT;
1091 log->l_iclog_heads = xhdrs;
1093 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1094 log->l_iclog_hsize = BBSIZE;
1095 log->l_iclog_heads = 1;
1101 * Special case machines that have less than 32MB of memory.
1102 * All machines with more memory use 32KB buffers.
1104 if (xfs_physmem <= btoc(32*1024*1024)) {
1105 /* Don't change; min configuration */
1106 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1107 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1109 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1110 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1113 /* the default log size is 16k or 32k which is one header sector */
1114 log->l_iclog_hsize = BBSIZE;
1115 log->l_iclog_heads = 1;
1118 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1119 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1121 if (mp->m_sb.sb_blocksize >= 16*1024) {
1122 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1123 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1124 if (mp->m_logbufs == -1) {
1125 switch (mp->m_sb.sb_blocksize) {
1126 case 16*1024: /* 16 KB */
1127 log->l_iclog_bufs = 3;
1129 case 32*1024: /* 32 KB */
1130 log->l_iclog_bufs = 4;
1132 case 64*1024: /* 64 KB */
1133 log->l_iclog_bufs = 8;
1136 xlog_panic("XFS: Invalid blocksize");
1141 } /* xlog_get_iclog_buffer_size */
1145 * This routine initializes some of the log structure for a given mount point.
1146 * Its primary purpose is to fill in enough, so recovery can occur. However,
1147 * some other stuff may be filled in too.
1150 xlog_alloc_log(xfs_mount_t *mp,
1151 xfs_buftarg_t *log_target,
1152 xfs_daddr_t blk_offset,
1156 xlog_rec_header_t *head;
1157 xlog_in_core_t **iclogp;
1158 xlog_in_core_t *iclog, *prev_iclog=NULL;
1163 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1166 log->l_targ = log_target;
1167 log->l_logsize = BBTOB(num_bblks);
1168 log->l_logBBstart = blk_offset;
1169 log->l_logBBsize = num_bblks;
1170 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1171 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1173 log->l_prev_block = -1;
1174 ASSIGN_ANY_LSN(log->l_tail_lsn, 1, 0, ARCH_NOCONVERT);
1175 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1176 log->l_last_sync_lsn = log->l_tail_lsn;
1177 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1178 log->l_grant_reserve_cycle = 1;
1179 log->l_grant_write_cycle = 1;
1181 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1182 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1183 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1184 /* for larger sector sizes, must have v2 or external log */
1185 ASSERT(log->l_sectbb_log == 0 ||
1186 log->l_logBBstart == 0 ||
1187 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1188 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1190 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1192 xlog_get_iclog_buffer_size(mp, log);
1194 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1195 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1196 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1197 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1198 ASSERT(XFS_BUF_ISBUSY(bp));
1199 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1202 spinlock_init(&log->l_icloglock, "iclog");
1203 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1204 initnsema(&log->l_flushsema, 0, "ic-flush");
1205 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1207 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1208 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1210 iclogp = &log->l_iclog;
1212 * The amount of memory to allocate for the iclog structure is
1213 * rather funky due to the way the structure is defined. It is
1214 * done this way so that we can use different sizes for machines
1215 * with different amounts of memory. See the definition of
1216 * xlog_in_core_t in xfs_log_priv.h for details.
1218 iclogsize = log->l_iclog_size;
1219 ASSERT(log->l_iclog_size >= 4096);
1220 for (i=0; i < log->l_iclog_bufs; i++) {
1221 *iclogp = (xlog_in_core_t *)
1222 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1224 iclog->hic_data = (xlog_in_core_2_t *)
1225 kmem_zalloc(iclogsize, KM_SLEEP);
1227 iclog->ic_prev = prev_iclog;
1229 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1231 head = &iclog->ic_header;
1232 memset(head, 0, sizeof(xlog_rec_header_t));
1233 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1234 INT_SET(head->h_version, ARCH_CONVERT,
1235 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1236 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1238 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1239 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1241 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1242 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1243 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1244 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1247 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1248 iclog->ic_state = XLOG_STATE_ACTIVE;
1249 iclog->ic_log = log;
1250 iclog->ic_callback_tail = &(iclog->ic_callback);
1251 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1253 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1254 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1255 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1256 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1258 iclogp = &iclog->ic_next;
1260 *iclogp = log->l_iclog; /* complete ring */
1261 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1264 } /* xlog_alloc_log */
1268 * Write out the commit record of a transaction associated with the given
1269 * ticket. Return the lsn of the commit record.
1272 xlog_commit_record(xfs_mount_t *mp,
1273 xlog_ticket_t *ticket,
1274 xlog_in_core_t **iclog,
1275 xfs_lsn_t *commitlsnp)
1278 xfs_log_iovec_t reg[1];
1283 ASSERT_ALWAYS(iclog);
1284 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1285 iclog, XLOG_COMMIT_TRANS))) {
1286 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
1289 } /* xlog_commit_record */
1293 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1294 * log space. This code pushes on the lsn which would supposedly free up
1295 * the 25% which we want to leave free. We may need to adopt a policy which
1296 * pushes on an lsn which is further along in the log once we reach the high
1297 * water mark. In this manner, we would be creating a low water mark.
1300 xlog_grant_push_ail(xfs_mount_t *mp,
1303 xlog_t *log = mp->m_log; /* pointer to the log */
1304 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1305 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1306 int free_blocks; /* free blocks left to write to */
1307 int free_bytes; /* free bytes left to write to */
1308 int threshold_block; /* block in lsn we'd like to be at */
1309 int threshold_cycle; /* lsn cycle we'd like to be at */
1313 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1315 s = GRANT_LOCK(log);
1316 free_bytes = xlog_space_left(log,
1317 log->l_grant_reserve_cycle,
1318 log->l_grant_reserve_bytes);
1319 tail_lsn = log->l_tail_lsn;
1320 free_blocks = BTOBBT(free_bytes);
1323 * Set the threshold for the minimum number of free blocks in the
1324 * log to the maximum of what the caller needs, one quarter of the
1325 * log, and 256 blocks.
1327 free_threshold = BTOBB(need_bytes);
1328 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1329 free_threshold = MAX(free_threshold, 256);
1330 if (free_blocks < free_threshold) {
1331 threshold_block = BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) + free_threshold;
1332 threshold_cycle = CYCLE_LSN(tail_lsn, ARCH_NOCONVERT);
1333 if (threshold_block >= log->l_logBBsize) {
1334 threshold_block -= log->l_logBBsize;
1335 threshold_cycle += 1;
1337 ASSIGN_ANY_LSN(threshold_lsn, threshold_cycle,
1338 threshold_block, ARCH_NOCONVERT);
1340 /* Don't pass in an lsn greater than the lsn of the last
1341 * log record known to be on disk.
1343 if (XFS_LSN_CMP_ARCH(threshold_lsn, log->l_last_sync_lsn, ARCH_NOCONVERT) > 0)
1344 threshold_lsn = log->l_last_sync_lsn;
1346 GRANT_UNLOCK(log, s);
1349 * Get the transaction layer to kick the dirty buffers out to
1350 * disk asynchronously. No point in trying to do this if
1351 * the filesystem is shutting down.
1353 if (threshold_lsn &&
1354 !XLOG_FORCED_SHUTDOWN(log))
1355 xfs_trans_push_ail(mp, threshold_lsn);
1356 } /* xlog_grant_push_ail */
1360 * Flush out the in-core log (iclog) to the on-disk log in a synchronous or
1361 * asynchronous fashion. Previously, we should have moved the current iclog
1362 * ptr in the log to point to the next available iclog. This allows further
1363 * write to continue while this code syncs out an iclog ready to go.
1364 * Before an in-core log can be written out, the data section must be scanned
1365 * to save away the 1st word of each BBSIZE block into the header. We replace
1366 * it with the current cycle count. Each BBSIZE block is tagged with the
1367 * cycle count because there in an implicit assumption that drives will
1368 * guarantee that entire 512 byte blocks get written at once. In other words,
1369 * we can't have part of a 512 byte block written and part not written. By
1370 * tagging each block, we will know which blocks are valid when recovering
1371 * after an unclean shutdown.
1373 * This routine is single threaded on the iclog. No other thread can be in
1374 * this routine with the same iclog. Changing contents of iclog can there-
1375 * fore be done without grabbing the state machine lock. Updating the global
1376 * log will require grabbing the lock though.
1378 * The entire log manager uses a logical block numbering scheme. Only
1379 * log_sync (and then only bwrite()) know about the fact that the log may
1380 * not start with block zero on a given device. The log block start offset
1381 * is added immediately before calling bwrite().
1385 xlog_sync(xlog_t *log,
1386 xlog_in_core_t *iclog)
1388 xfs_caddr_t dptr; /* pointer to byte sized element */
1391 uint count; /* byte count of bwrite */
1392 uint count_init; /* initial count before roundup */
1393 int split = 0; /* split write into two regions */
1396 XFS_STATS_INC(xs_log_writes);
1397 ASSERT(iclog->ic_refcnt == 0);
1399 /* Add for LR header */
1400 count_init = log->l_iclog_hsize + iclog->ic_offset;
1402 /* Round out the log write size */
1403 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
1404 log->l_mp->m_sb.sb_logsunit > 1) {
1405 /* we have a v2 stripe unit to use */
1406 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1408 count = BBTOB(BTOBB(count_init));
1410 iclog->ic_roundoff = count - count_init;
1411 log->l_roundoff += iclog->ic_roundoff;
1413 xlog_pack_data(log, iclog); /* put cycle number in every block */
1415 /* real byte length */
1416 INT_SET(iclog->ic_header.h_len,
1418 iclog->ic_offset + iclog->ic_roundoff);
1420 /* put ops count in correct order */
1421 ops = iclog->ic_header.h_num_logops;
1422 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1425 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1426 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1427 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT));
1429 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1431 /* Do we need to split this write into 2 parts? */
1432 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1433 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1434 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1435 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1437 iclog->ic_bwritecnt = 1;
1439 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1440 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1444 * Do a disk write cache flush for the log block.
1445 * This is a bit of a sledgehammer, it would be better
1446 * to use a tag barrier here that just prevents reordering.
1447 * It may not be needed to flush the first split block in the log wrap
1448 * case, but do it anyways to be safe -AK
1450 if (!(log->l_mp->m_flags & XFS_MOUNT_NOLOGFLUSH))
1453 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1454 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1456 xlog_verify_iclog(log, iclog, count, B_TRUE);
1458 /* account for log which doesn't start at block #0 */
1459 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1461 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1466 if ((error = XFS_bwrite(bp))) {
1467 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1472 bp = iclog->ic_log->l_xbuf;
1473 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1475 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1476 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1477 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1478 (__psint_t)count), split);
1479 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1482 if (!(log->l_mp->m_flags & XFS_MOUNT_NOLOGFLUSH))
1484 dptr = XFS_BUF_PTR(bp);
1486 * Bump the cycle numbers at the start of each block
1487 * since this part of the buffer is at the start of
1488 * a new cycle. Watch out for the header magic number
1491 for (i=0; i<split; i += BBSIZE) {
1492 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1493 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1494 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1498 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1499 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1501 /* account for internal log which does't start at block #0 */
1502 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1504 if ((error = XFS_bwrite(bp))) {
1505 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1506 bp, XFS_BUF_ADDR(bp));
1515 * Unallocate a log structure
1518 xlog_unalloc_log(xlog_t *log)
1520 xlog_in_core_t *iclog, *next_iclog;
1521 xlog_ticket_t *tic, *next_tic;
1525 iclog = log->l_iclog;
1526 for (i=0; i<log->l_iclog_bufs; i++) {
1527 sv_destroy(&iclog->ic_forcesema);
1528 sv_destroy(&iclog->ic_writesema);
1529 xfs_buf_free(iclog->ic_bp);
1530 #ifdef XFS_LOG_TRACE
1531 if (iclog->ic_trace != NULL) {
1532 ktrace_free(iclog->ic_trace);
1535 next_iclog = iclog->ic_next;
1536 kmem_free(iclog->hic_data, log->l_iclog_size);
1537 kmem_free(iclog, sizeof(xlog_in_core_t));
1540 freesema(&log->l_flushsema);
1541 spinlock_destroy(&log->l_icloglock);
1542 spinlock_destroy(&log->l_grant_lock);
1544 /* XXXsup take a look at this again. */
1545 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1546 !XLOG_FORCED_SHUTDOWN(log)) {
1547 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1548 "xlog_unalloc_log: (cnt: %d, total: %d)",
1549 log->l_ticket_cnt, log->l_ticket_tcnt);
1550 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1553 tic = log->l_unmount_free;
1555 next_tic = tic->t_next;
1556 kmem_free(tic, NBPP);
1560 xfs_buf_free(log->l_xbuf);
1561 #ifdef XFS_LOG_TRACE
1562 if (log->l_trace != NULL) {
1563 ktrace_free(log->l_trace);
1565 if (log->l_grant_trace != NULL) {
1566 ktrace_free(log->l_grant_trace);
1569 log->l_mp->m_log = NULL;
1570 kmem_free(log, sizeof(xlog_t));
1571 } /* xlog_unalloc_log */
1574 * Update counters atomically now that memcpy is done.
1578 xlog_state_finish_copy(xlog_t *log,
1579 xlog_in_core_t *iclog,
1587 iclog->ic_header.h_num_logops += record_cnt;
1588 iclog->ic_offset += copy_bytes;
1591 } /* xlog_state_finish_copy */
1597 * Write some region out to in-core log
1599 * This will be called when writing externally provided regions or when
1600 * writing out a commit record for a given transaction.
1602 * General algorithm:
1603 * 1. Find total length of this write. This may include adding to the
1604 * lengths passed in.
1605 * 2. Check whether we violate the tickets reservation.
1606 * 3. While writing to this iclog
1607 * A. Reserve as much space in this iclog as can get
1608 * B. If this is first write, save away start lsn
1609 * C. While writing this region:
1610 * 1. If first write of transaction, write start record
1611 * 2. Write log operation header (header per region)
1612 * 3. Find out if we can fit entire region into this iclog
1613 * 4. Potentially, verify destination memcpy ptr
1614 * 5. Memcpy (partial) region
1615 * 6. If partial copy, release iclog; otherwise, continue
1616 * copying more regions into current iclog
1617 * 4. Mark want sync bit (in simulation mode)
1618 * 5. Release iclog for potential flush to on-disk log.
1621 * 1. Panic if reservation is overrun. This should never happen since
1622 * reservation amounts are generated internal to the filesystem.
1624 * 1. Tickets are single threaded data structures.
1625 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1626 * syncing routine. When a single log_write region needs to span
1627 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1628 * on all log operation writes which don't contain the end of the
1629 * region. The XLOG_END_TRANS bit is used for the in-core log
1630 * operation which contains the end of the continued log_write region.
1631 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1632 * we don't really know exactly how much space will be used. As a result,
1633 * we don't update ic_offset until the end when we know exactly how many
1634 * bytes have been written out.
1637 xlog_write(xfs_mount_t * mp,
1638 xfs_log_iovec_t reg[],
1640 xfs_log_ticket_t tic,
1641 xfs_lsn_t *start_lsn,
1642 xlog_in_core_t **commit_iclog,
1645 xlog_t *log = mp->m_log;
1646 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1647 xlog_op_header_t *logop_head; /* ptr to log operation header */
1648 xlog_in_core_t *iclog; /* ptr to current in-core log */
1649 __psint_t ptr; /* copy address into data region */
1650 int len; /* # xlog_write() bytes 2 still copy */
1651 int index; /* region index currently copying */
1652 int log_offset; /* offset (from 0) into data region */
1653 int start_rec_copy; /* # bytes to copy for start record */
1654 int partial_copy; /* did we split a region? */
1655 int partial_copy_len;/* # bytes copied if split region */
1656 int need_copy; /* # bytes need to memcpy this region */
1657 int copy_len; /* # bytes actually memcpy'ing */
1658 int copy_off; /* # bytes from entry start */
1659 int contwr; /* continued write of in-core log? */
1661 int record_cnt = 0, data_cnt = 0;
1663 partial_copy_len = partial_copy = 0;
1665 /* Calculate potential maximum space. Each region gets its own
1666 * xlog_op_header_t and may need to be double word aligned.
1669 if (ticket->t_flags & XLOG_TIC_INITED) /* acct for start rec of xact */
1670 len += sizeof(xlog_op_header_t);
1672 for (index = 0; index < nentries; index++) {
1673 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1674 len += reg[index].i_len;
1676 contwr = *start_lsn = 0;
1678 if (ticket->t_curr_res < len) {
1681 "xfs_log_write: reservation ran out. Need to up reservation");
1683 /* Customer configurable panic */
1684 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1685 "xfs_log_write: reservation ran out. Need to up reservation");
1686 /* If we did not panic, shutdown the filesystem */
1687 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1690 ticket->t_curr_res -= len;
1692 for (index = 0; index < nentries; ) {
1693 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1694 &contwr, &log_offset)))
1697 ASSERT(log_offset <= iclog->ic_size - 1);
1698 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1700 /* start_lsn is the first lsn written to. That's all we need. */
1702 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1704 /* This loop writes out as many regions as can fit in the amount
1705 * of space which was allocated by xlog_state_get_iclog_space().
1707 while (index < nentries) {
1708 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1709 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1712 /* If first write for transaction, insert start record.
1713 * We can't be trying to commit if we are inited. We can't
1714 * have any "partial_copy" if we are inited.
1716 if (ticket->t_flags & XLOG_TIC_INITED) {
1717 logop_head = (xlog_op_header_t *)ptr;
1718 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1719 logop_head->oh_clientid = ticket->t_clientid;
1720 INT_ZERO(logop_head->oh_len, ARCH_CONVERT);
1721 logop_head->oh_flags = XLOG_START_TRANS;
1722 INT_ZERO(logop_head->oh_res2, ARCH_CONVERT);
1723 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1726 start_rec_copy = sizeof(xlog_op_header_t);
1727 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1730 /* Copy log operation header directly into data section */
1731 logop_head = (xlog_op_header_t *)ptr;
1732 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1733 logop_head->oh_clientid = ticket->t_clientid;
1734 INT_ZERO(logop_head->oh_res2, ARCH_CONVERT);
1736 /* header copied directly */
1737 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1739 /* are we copying a commit or unmount record? */
1740 logop_head->oh_flags = flags;
1743 * We've seen logs corrupted with bad transaction client
1744 * ids. This makes sure that XFS doesn't generate them on.
1745 * Turn this into an EIO and shut down the filesystem.
1747 switch (logop_head->oh_clientid) {
1748 case XFS_TRANSACTION:
1753 xfs_fs_cmn_err(CE_WARN, mp,
1754 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1755 logop_head->oh_clientid, tic);
1756 return XFS_ERROR(EIO);
1759 /* Partial write last time? => (partial_copy != 0)
1760 * need_copy is the amount we'd like to copy if everything could
1761 * fit in the current memcpy.
1763 need_copy = reg[index].i_len - partial_copy_len;
1765 copy_off = partial_copy_len;
1766 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1767 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1769 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1770 partial_copy_len = partial_copy = 0;
1771 } else { /* partial write */
1772 copy_len = iclog->ic_size - log_offset;
1773 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1774 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1776 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1777 partial_copy_len += copy_len;
1779 len += sizeof(xlog_op_header_t); /* from splitting of region */
1780 /* account for new log op header */
1781 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1783 xlog_verify_dest_ptr(log, ptr);
1786 ASSERT(copy_len >= 0);
1787 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1788 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1790 /* make copy_len total bytes copied, including headers */
1791 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1793 data_cnt += contwr ? copy_len : 0;
1794 if (partial_copy) { /* copied partial region */
1795 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1796 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1797 record_cnt = data_cnt = 0;
1798 if ((error = xlog_state_release_iclog(log, iclog)))
1800 break; /* don't increment index */
1801 } else { /* copied entire region */
1803 partial_copy_len = partial_copy = 0;
1805 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1806 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1807 record_cnt = data_cnt = 0;
1808 xlog_state_want_sync(log, iclog);
1810 ASSERT(flags & XLOG_COMMIT_TRANS);
1811 *commit_iclog = iclog;
1812 } else if ((error = xlog_state_release_iclog(log, iclog)))
1814 if (index == nentries)
1815 return 0; /* we are done */
1819 } /* if (partial_copy) */
1820 } /* while (index < nentries) */
1821 } /* for (index = 0; index < nentries; ) */
1824 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1826 ASSERT(flags & XLOG_COMMIT_TRANS);
1827 *commit_iclog = iclog;
1830 return (xlog_state_release_iclog(log, iclog));
1834 /*****************************************************************************
1836 * State Machine functions
1838 *****************************************************************************
1841 /* Clean iclogs starting from the head. This ordering must be
1842 * maintained, so an iclog doesn't become ACTIVE beyond one that
1843 * is SYNCING. This is also required to maintain the notion that we use
1844 * a counting semaphore to hold off would be writers to the log when every
1845 * iclog is trying to sync to disk.
1847 * State Change: DIRTY -> ACTIVE
1850 xlog_state_clean_log(xlog_t *log)
1852 xlog_in_core_t *iclog;
1855 iclog = log->l_iclog;
1857 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1858 iclog->ic_state = XLOG_STATE_ACTIVE;
1859 iclog->ic_offset = 0;
1860 iclog->ic_callback = 0; /* don't need to free */
1862 * If the number of ops in this iclog indicate it just
1863 * contains the dummy transaction, we can
1864 * change state into IDLE (the second time around).
1865 * Otherwise we should change the state into
1867 * We don't need to cover the dummy.
1870 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1874 * We have two dirty iclogs so start over
1875 * This could also be num of ops indicates
1876 * this is not the dummy going out.
1880 INT_ZERO(iclog->ic_header.h_num_logops, ARCH_CONVERT);
1881 memset(iclog->ic_header.h_cycle_data, 0,
1882 sizeof(iclog->ic_header.h_cycle_data));
1883 INT_ZERO(iclog->ic_header.h_lsn, ARCH_CONVERT);
1884 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1887 break; /* stop cleaning */
1888 iclog = iclog->ic_next;
1889 } while (iclog != log->l_iclog);
1891 /* log is locked when we are called */
1893 * Change state for the dummy log recording.
1894 * We usually go to NEED. But we go to NEED2 if the changed indicates
1895 * we are done writing the dummy record.
1896 * If we are done with the second dummy recored (DONE2), then
1900 switch (log->l_covered_state) {
1901 case XLOG_STATE_COVER_IDLE:
1902 case XLOG_STATE_COVER_NEED:
1903 case XLOG_STATE_COVER_NEED2:
1904 log->l_covered_state = XLOG_STATE_COVER_NEED;
1907 case XLOG_STATE_COVER_DONE:
1909 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1911 log->l_covered_state = XLOG_STATE_COVER_NEED;
1914 case XLOG_STATE_COVER_DONE2:
1916 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1918 log->l_covered_state = XLOG_STATE_COVER_NEED;
1925 } /* xlog_state_clean_log */
1928 xlog_get_lowest_lsn(
1931 xlog_in_core_t *lsn_log;
1932 xfs_lsn_t lowest_lsn, lsn;
1934 lsn_log = log->l_iclog;
1937 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
1938 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
1939 if ((lsn && !lowest_lsn) ||
1940 (XFS_LSN_CMP_ARCH(lsn, lowest_lsn, ARCH_NOCONVERT) < 0)) {
1944 lsn_log = lsn_log->ic_next;
1945 } while (lsn_log != log->l_iclog);
1951 xlog_state_do_callback(
1954 xlog_in_core_t *ciclog)
1956 xlog_in_core_t *iclog;
1957 xlog_in_core_t *first_iclog; /* used to know when we've
1958 * processed all iclogs once */
1959 xfs_log_callback_t *cb, *cb_next;
1961 xfs_lsn_t lowest_lsn;
1962 int ioerrors; /* counter: iclogs with errors */
1963 int loopdidcallbacks; /* flag: inner loop did callbacks*/
1964 int funcdidcallbacks; /* flag: function did callbacks */
1965 int repeats; /* for issuing console warnings if
1966 * looping too many times */
1970 first_iclog = iclog = log->l_iclog;
1972 funcdidcallbacks = 0;
1977 * Scan all iclogs starting with the one pointed to by the
1978 * log. Reset this starting point each time the log is
1979 * unlocked (during callbacks).
1981 * Keep looping through iclogs until one full pass is made
1982 * without running any callbacks.
1984 first_iclog = log->l_iclog;
1985 iclog = log->l_iclog;
1986 loopdidcallbacks = 0;
1991 /* skip all iclogs in the ACTIVE & DIRTY states */
1992 if (iclog->ic_state &
1993 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
1994 iclog = iclog->ic_next;
1999 * Between marking a filesystem SHUTDOWN and stopping
2000 * the log, we do flush all iclogs to disk (if there
2001 * wasn't a log I/O error). So, we do want things to
2002 * go smoothly in case of just a SHUTDOWN w/o a
2005 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2007 * Can only perform callbacks in order. Since
2008 * this iclog is not in the DONE_SYNC/
2009 * DO_CALLBACK state, we skip the rest and
2010 * just try to clean up. If we set our iclog
2011 * to DO_CALLBACK, we will not process it when
2012 * we retry since a previous iclog is in the
2013 * CALLBACK and the state cannot change since
2014 * we are holding the LOG_LOCK.
2016 if (!(iclog->ic_state &
2017 (XLOG_STATE_DONE_SYNC |
2018 XLOG_STATE_DO_CALLBACK))) {
2019 if (ciclog && (ciclog->ic_state ==
2020 XLOG_STATE_DONE_SYNC)) {
2021 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2026 * We now have an iclog that is in either the
2027 * DO_CALLBACK or DONE_SYNC states. The other
2028 * states (WANT_SYNC, SYNCING, or CALLBACK were
2029 * caught by the above if and are going to
2030 * clean (i.e. we aren't doing their callbacks)
2035 * We will do one more check here to see if we
2036 * have chased our tail around.
2039 lowest_lsn = xlog_get_lowest_lsn(log);
2043 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT),
2046 iclog = iclog->ic_next;
2047 continue; /* Leave this iclog for
2051 iclog->ic_state = XLOG_STATE_CALLBACK;
2055 /* l_last_sync_lsn field protected by
2056 * GRANT_LOCK. Don't worry about iclog's lsn.
2057 * No one else can be here except us.
2059 s = GRANT_LOCK(log);
2060 ASSERT(XFS_LSN_CMP_ARCH(
2061 log->l_last_sync_lsn,
2062 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT),
2065 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2066 GRANT_UNLOCK(log, s);
2069 * Keep processing entries in the callback list
2070 * until we come around and it is empty. We
2071 * need to atomically see that the list is
2072 * empty and change the state to DIRTY so that
2073 * we don't miss any more callbacks being added.
2079 cb = iclog->ic_callback;
2082 iclog->ic_callback_tail = &(iclog->ic_callback);
2083 iclog->ic_callback = 0;
2086 /* perform callbacks in the order given */
2087 for (; cb != 0; cb = cb_next) {
2088 cb_next = cb->cb_next;
2089 cb->cb_func(cb->cb_arg, aborted);
2092 cb = iclog->ic_callback;
2098 ASSERT(iclog->ic_callback == 0);
2099 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2100 iclog->ic_state = XLOG_STATE_DIRTY;
2103 * Transition from DIRTY to ACTIVE if applicable.
2104 * NOP if STATE_IOERROR.
2106 xlog_state_clean_log(log);
2108 /* wake up threads waiting in xfs_log_force() */
2109 sv_broadcast(&iclog->ic_forcesema);
2111 iclog = iclog->ic_next;
2112 } while (first_iclog != iclog);
2113 if (repeats && (repeats % 10) == 0) {
2114 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2115 "xlog_state_do_callback: looping %d", repeats);
2117 } while (!ioerrors && loopdidcallbacks);
2120 * make one last gasp attempt to see if iclogs are being left in
2124 if (funcdidcallbacks) {
2125 first_iclog = iclog = log->l_iclog;
2127 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2129 * Terminate the loop if iclogs are found in states
2130 * which will cause other threads to clean up iclogs.
2132 * SYNCING - i/o completion will go through logs
2133 * DONE_SYNC - interrupt thread should be waiting for
2135 * IOERROR - give up hope all ye who enter here
2137 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2138 iclog->ic_state == XLOG_STATE_SYNCING ||
2139 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2140 iclog->ic_state == XLOG_STATE_IOERROR )
2142 iclog = iclog->ic_next;
2143 } while (first_iclog != iclog);
2147 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2148 flushcnt = log->l_flushcnt;
2149 log->l_flushcnt = 0;
2153 vsema(&log->l_flushsema);
2154 } /* xlog_state_do_callback */
2158 * Finish transitioning this iclog to the dirty state.
2160 * Make sure that we completely execute this routine only when this is
2161 * the last call to the iclog. There is a good chance that iclog flushes,
2162 * when we reach the end of the physical log, get turned into 2 separate
2163 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2164 * routine. By using the reference count bwritecnt, we guarantee that only
2165 * the second completion goes through.
2167 * Callbacks could take time, so they are done outside the scope of the
2168 * global state machine log lock. Assume that the calls to cvsema won't
2169 * take a long time. At least we know it won't sleep.
2172 xlog_state_done_syncing(
2173 xlog_in_core_t *iclog,
2176 xlog_t *log = iclog->ic_log;
2181 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2182 iclog->ic_state == XLOG_STATE_IOERROR);
2183 ASSERT(iclog->ic_refcnt == 0);
2184 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2188 * If we got an error, either on the first buffer, or in the case of
2189 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2190 * and none should ever be attempted to be written to disk
2193 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2194 if (--iclog->ic_bwritecnt == 1) {
2198 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2202 * Someone could be sleeping prior to writing out the next
2203 * iclog buffer, we wake them all, one will get to do the
2204 * I/O, the others get to wait for the result.
2206 sv_broadcast(&iclog->ic_writesema);
2208 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2209 } /* xlog_state_done_syncing */
2213 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2214 * sleep. The flush semaphore is set to the number of in-core buffers and
2215 * decremented around disk syncing. Therefore, if all buffers are syncing,
2216 * this semaphore will cause new writes to sleep until a sync completes.
2217 * Otherwise, this code just does p() followed by v(). This approximates
2218 * a sleep/wakeup except we can't race.
2220 * The in-core logs are used in a circular fashion. They are not used
2221 * out-of-order even when an iclog past the head is free.
2224 * * log_offset where xlog_write() can start writing into the in-core
2226 * * in-core log pointer to which xlog_write() should write.
2227 * * boolean indicating this is a continued write to an in-core log.
2228 * If this is the last write, then the in-core log's offset field
2229 * needs to be incremented, depending on the amount of data which
2233 xlog_state_get_iclog_space(xlog_t *log,
2235 xlog_in_core_t **iclogp,
2236 xlog_ticket_t *ticket,
2237 int *continued_write,
2242 xlog_rec_header_t *head;
2243 xlog_in_core_t *iclog;
2248 if (XLOG_FORCED_SHUTDOWN(log)) {
2250 return XFS_ERROR(EIO);
2253 iclog = log->l_iclog;
2254 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2257 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2258 XFS_STATS_INC(xs_log_noiclogs);
2259 /* Ensure that log writes happen */
2260 psema(&log->l_flushsema, PINOD);
2263 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2264 head = &iclog->ic_header;
2266 iclog->ic_refcnt++; /* prevents sync */
2267 log_offset = iclog->ic_offset;
2269 /* On the 1st write to an iclog, figure out lsn. This works
2270 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2271 * committing to. If the offset is set, that's how many blocks
2274 if (log_offset == 0) {
2275 ticket->t_curr_res -= log->l_iclog_hsize;
2276 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2277 ASSIGN_LSN(head->h_lsn, log, ARCH_CONVERT);
2278 ASSERT(log->l_curr_block >= 0);
2280 /* round off error from last write with this iclog */
2281 ticket->t_curr_res -= iclog->ic_roundoff;
2282 log->l_roundoff -= iclog->ic_roundoff;
2283 iclog->ic_roundoff = 0;
2286 /* If there is enough room to write everything, then do it. Otherwise,
2287 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2288 * bit is on, so this will get flushed out. Don't update ic_offset
2289 * until you know exactly how many bytes get copied. Therefore, wait
2290 * until later to update ic_offset.
2292 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2293 * can fit into remaining data section.
2295 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2296 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2298 /* If I'm the only one writing to this iclog, sync it to disk */
2299 if (iclog->ic_refcnt == 1) {
2301 if ((error = xlog_state_release_iclog(log, iclog)))
2310 /* Do we have enough room to write the full amount in the remainder
2311 * of this iclog? Or must we continue a write on the next iclog and
2312 * mark this iclog as completely taken? In the case where we switch
2313 * iclogs (to mark it taken), this particular iclog will release/sync
2314 * to disk in xlog_write().
2316 if (len <= iclog->ic_size - iclog->ic_offset) {
2317 *continued_write = 0;
2318 iclog->ic_offset += len;
2320 *continued_write = 1;
2321 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2325 ASSERT(iclog->ic_offset <= iclog->ic_size);
2328 *logoffsetp = log_offset;
2330 } /* xlog_state_get_iclog_space */
2333 * Atomically get the log space required for a log ticket.
2335 * Once a ticket gets put onto the reserveq, it will only return after
2336 * the needed reservation is satisfied.
2339 xlog_grant_log_space(xlog_t *log,
2351 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2352 panic("grant Recovery problem");
2355 /* Is there space or do we need to sleep? */
2356 s = GRANT_LOCK(log);
2357 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2359 /* something is already sleeping; insert new transaction at end */
2360 if (log->l_reserve_headq) {
2361 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2362 xlog_trace_loggrant(log, tic,
2363 "xlog_grant_log_space: sleep 1");
2365 * Gotta check this before going to sleep, while we're
2366 * holding the grant lock.
2368 if (XLOG_FORCED_SHUTDOWN(log))
2371 XFS_STATS_INC(xs_sleep_logspace);
2372 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2374 * If we got an error, and the filesystem is shutting down,
2375 * we'll catch it down below. So just continue...
2377 xlog_trace_loggrant(log, tic,
2378 "xlog_grant_log_space: wake 1");
2379 s = GRANT_LOCK(log);
2381 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2382 need_bytes = tic->t_unit_res*tic->t_ocnt;
2384 need_bytes = tic->t_unit_res;
2387 if (XLOG_FORCED_SHUTDOWN(log))
2390 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2391 log->l_grant_reserve_bytes);
2392 if (free_bytes < need_bytes) {
2393 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2394 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2395 xlog_trace_loggrant(log, tic,
2396 "xlog_grant_log_space: sleep 2");
2397 XFS_STATS_INC(xs_sleep_logspace);
2398 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2400 if (XLOG_FORCED_SHUTDOWN(log)) {
2401 s = GRANT_LOCK(log);
2405 xlog_trace_loggrant(log, tic,
2406 "xlog_grant_log_space: wake 2");
2407 xlog_grant_push_ail(log->l_mp, need_bytes);
2408 s = GRANT_LOCK(log);
2410 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2411 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2413 /* we've got enough space */
2414 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w');
2415 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'r');
2417 tail_lsn = log->l_tail_lsn;
2419 * Check to make sure the grant write head didn't just over lap the
2420 * tail. If the cycles are the same, we can't be overlapping.
2421 * Otherwise, make sure that the cycles differ by exactly one and
2422 * check the byte count.
2424 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) != log->l_grant_write_cycle) {
2425 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn, ARCH_NOCONVERT));
2426 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn, ARCH_NOCONVERT)));
2429 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2430 xlog_verify_grant_head(log, 1);
2431 GRANT_UNLOCK(log, s);
2435 if (tic->t_flags & XLOG_TIC_IN_Q)
2436 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2437 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2439 * If we are failing, make sure the ticket doesn't have any
2440 * current reservations. We don't want to add this back when
2441 * the ticket/transaction gets cancelled.
2443 tic->t_curr_res = 0;
2444 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2445 GRANT_UNLOCK(log, s);
2446 return XFS_ERROR(EIO);
2447 } /* xlog_grant_log_space */
2451 * Replenish the byte reservation required by moving the grant write head.
2456 xlog_regrant_write_log_space(xlog_t *log,
2460 int free_bytes, need_bytes;
2461 xlog_ticket_t *ntic;
2466 tic->t_curr_res = tic->t_unit_res;
2472 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2473 panic("regrant Recovery problem");
2476 s = GRANT_LOCK(log);
2477 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2479 if (XLOG_FORCED_SHUTDOWN(log))
2482 /* If there are other waiters on the queue then give them a
2483 * chance at logspace before us. Wake up the first waiters,
2484 * if we do not wake up all the waiters then go to sleep waiting
2485 * for more free space, otherwise try to get some space for
2489 if ((ntic = log->l_write_headq)) {
2490 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2491 log->l_grant_write_bytes);
2493 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2495 if (free_bytes < ntic->t_unit_res)
2497 free_bytes -= ntic->t_unit_res;
2498 sv_signal(&ntic->t_sema);
2499 ntic = ntic->t_next;
2500 } while (ntic != log->l_write_headq);
2502 if (ntic != log->l_write_headq) {
2503 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2504 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2506 xlog_trace_loggrant(log, tic,
2507 "xlog_regrant_write_log_space: sleep 1");
2508 XFS_STATS_INC(xs_sleep_logspace);
2509 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2510 &log->l_grant_lock, s);
2512 /* If we're shutting down, this tic is already
2514 if (XLOG_FORCED_SHUTDOWN(log)) {
2515 s = GRANT_LOCK(log);
2519 xlog_trace_loggrant(log, tic,
2520 "xlog_regrant_write_log_space: wake 1");
2521 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2522 s = GRANT_LOCK(log);
2526 need_bytes = tic->t_unit_res;
2529 if (XLOG_FORCED_SHUTDOWN(log))
2532 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2533 log->l_grant_write_bytes);
2534 if (free_bytes < need_bytes) {
2535 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2536 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2537 XFS_STATS_INC(xs_sleep_logspace);
2538 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2540 /* If we're shutting down, this tic is already off the queue */
2541 if (XLOG_FORCED_SHUTDOWN(log)) {
2542 s = GRANT_LOCK(log);
2546 xlog_trace_loggrant(log, tic,
2547 "xlog_regrant_write_log_space: wake 2");
2548 xlog_grant_push_ail(log->l_mp, need_bytes);
2549 s = GRANT_LOCK(log);
2551 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2552 XLOG_DEL_TICKETQ(log->l_write_headq, tic);
2554 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w'); /* we've got enough space */
2556 tail_lsn = log->l_tail_lsn;
2557 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) != log->l_grant_write_cycle) {
2558 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn, ARCH_NOCONVERT));
2559 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn, ARCH_NOCONVERT)));
2563 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2564 xlog_verify_grant_head(log, 1);
2565 GRANT_UNLOCK(log, s);
2570 if (tic->t_flags & XLOG_TIC_IN_Q)
2571 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2572 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2574 * If we are failing, make sure the ticket doesn't have any
2575 * current reservations. We don't want to add this back when
2576 * the ticket/transaction gets cancelled.
2578 tic->t_curr_res = 0;
2579 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2580 GRANT_UNLOCK(log, s);
2581 return XFS_ERROR(EIO);
2582 } /* xlog_regrant_write_log_space */
2585 /* The first cnt-1 times through here we don't need to
2586 * move the grant write head because the permanent
2587 * reservation has reserved cnt times the unit amount.
2588 * Release part of current permanent unit reservation and
2589 * reset current reservation to be one units worth. Also
2590 * move grant reservation head forward.
2593 xlog_regrant_reserve_log_space(xlog_t *log,
2594 xlog_ticket_t *ticket)
2598 xlog_trace_loggrant(log, ticket,
2599 "xlog_regrant_reserve_log_space: enter");
2600 if (ticket->t_cnt > 0)
2603 s = GRANT_LOCK(log);
2604 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2605 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2606 ticket->t_curr_res = ticket->t_unit_res;
2607 xlog_trace_loggrant(log, ticket,
2608 "xlog_regrant_reserve_log_space: sub current res");
2609 xlog_verify_grant_head(log, 1);
2611 /* just return if we still have some of the pre-reserved space */
2612 if (ticket->t_cnt > 0) {
2613 GRANT_UNLOCK(log, s);
2617 XLOG_GRANT_ADD_SPACE(log, ticket->t_unit_res, 'r');
2618 xlog_trace_loggrant(log, ticket,
2619 "xlog_regrant_reserve_log_space: exit");
2620 xlog_verify_grant_head(log, 0);
2621 GRANT_UNLOCK(log, s);
2622 ticket->t_curr_res = ticket->t_unit_res;
2623 } /* xlog_regrant_reserve_log_space */
2627 * Give back the space left from a reservation.
2629 * All the information we need to make a correct determination of space left
2630 * is present. For non-permanent reservations, things are quite easy. The
2631 * count should have been decremented to zero. We only need to deal with the
2632 * space remaining in the current reservation part of the ticket. If the
2633 * ticket contains a permanent reservation, there may be left over space which
2634 * needs to be released. A count of N means that N-1 refills of the current
2635 * reservation can be done before we need to ask for more space. The first
2636 * one goes to fill up the first current reservation. Once we run out of
2637 * space, the count will stay at zero and the only space remaining will be
2638 * in the current reservation field.
2641 xlog_ungrant_log_space(xlog_t *log,
2642 xlog_ticket_t *ticket)
2646 if (ticket->t_cnt > 0)
2649 s = GRANT_LOCK(log);
2650 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2652 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2653 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2655 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2657 /* If this is a permanent reservation ticket, we may be able to free
2658 * up more space based on the remaining count.
2660 if (ticket->t_cnt > 0) {
2661 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2662 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'w');
2663 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'r');
2666 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2667 xlog_verify_grant_head(log, 1);
2668 GRANT_UNLOCK(log, s);
2669 xfs_log_move_tail(log->l_mp, 1);
2670 } /* xlog_ungrant_log_space */
2674 * Atomically put back used ticket.
2677 xlog_state_put_ticket(xlog_t *log,
2683 xlog_ticket_put(log, tic);
2685 } /* xlog_state_put_ticket */
2688 * Flush iclog to disk if this is the last reference to the given iclog and
2689 * the WANT_SYNC bit is set.
2691 * When this function is entered, the iclog is not necessarily in the
2692 * WANT_SYNC state. It may be sitting around waiting to get filled.
2697 xlog_state_release_iclog(xlog_t *log,
2698 xlog_in_core_t *iclog)
2701 int sync = 0; /* do we sync? */
2703 xlog_assign_tail_lsn(log->l_mp);
2707 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2709 return XFS_ERROR(EIO);
2712 ASSERT(iclog->ic_refcnt > 0);
2713 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2714 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2716 if (--iclog->ic_refcnt == 0 &&
2717 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2719 iclog->ic_state = XLOG_STATE_SYNCING;
2720 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2721 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2722 /* cycle incremented when incrementing curr_block */
2728 * We let the log lock go, so it's possible that we hit a log I/O
2729 * error or someother SHUTDOWN condition that marks the iclog
2730 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2731 * this iclog has consistent data, so we ignore IOERROR
2732 * flags after this point.
2735 return xlog_sync(log, iclog);
2739 } /* xlog_state_release_iclog */
2743 * This routine will mark the current iclog in the ring as WANT_SYNC
2744 * and move the current iclog pointer to the next iclog in the ring.
2745 * When this routine is called from xlog_state_get_iclog_space(), the
2746 * exact size of the iclog has not yet been determined. All we know is
2747 * that every data block. We have run out of space in this log record.
2750 xlog_state_switch_iclogs(xlog_t *log,
2751 xlog_in_core_t *iclog,
2754 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2756 eventual_size = iclog->ic_offset;
2757 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2758 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2759 log->l_prev_block = log->l_curr_block;
2760 log->l_prev_cycle = log->l_curr_cycle;
2762 /* roll log?: ic_offset changed later */
2763 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2765 /* Round up to next log-sunit */
2766 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2767 log->l_mp->m_sb.sb_logsunit > 1) {
2768 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2769 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2772 if (log->l_curr_block >= log->l_logBBsize) {
2773 log->l_curr_cycle++;
2774 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2775 log->l_curr_cycle++;
2776 log->l_curr_block -= log->l_logBBsize;
2777 ASSERT(log->l_curr_block >= 0);
2779 ASSERT(iclog == log->l_iclog);
2780 log->l_iclog = iclog->ic_next;
2781 } /* xlog_state_switch_iclogs */
2785 * Write out all data in the in-core log as of this exact moment in time.
2787 * Data may be written to the in-core log during this call. However,
2788 * we don't guarantee this data will be written out. A change from past
2789 * implementation means this routine will *not* write out zero length LRs.
2791 * Basically, we try and perform an intelligent scan of the in-core logs.
2792 * If we determine there is no flushable data, we just return. There is no
2793 * flushable data if:
2795 * 1. the current iclog is active and has no data; the previous iclog
2796 * is in the active or dirty state.
2797 * 2. the current iclog is drity, and the previous iclog is in the
2798 * active or dirty state.
2800 * We may sleep (call psema) if:
2802 * 1. the current iclog is not in the active nor dirty state.
2803 * 2. the current iclog dirty, and the previous iclog is not in the
2804 * active nor dirty state.
2805 * 3. the current iclog is active, and there is another thread writing
2806 * to this particular iclog.
2807 * 4. a) the current iclog is active and has no other writers
2808 * b) when we return from flushing out this iclog, it is still
2809 * not in the active nor dirty state.
2812 xlog_state_sync_all(xlog_t *log, uint flags)
2814 xlog_in_core_t *iclog;
2820 iclog = log->l_iclog;
2821 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2823 return XFS_ERROR(EIO);
2826 /* If the head iclog is not active nor dirty, we just attach
2827 * ourselves to the head and go to sleep.
2829 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2830 iclog->ic_state == XLOG_STATE_DIRTY) {
2832 * If the head is dirty or (active and empty), then
2833 * we need to look at the previous iclog. If the previous
2834 * iclog is active or dirty we are done. There is nothing
2835 * to sync out. Otherwise, we attach ourselves to the
2836 * previous iclog and go to sleep.
2838 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2839 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2840 iclog = iclog->ic_prev;
2841 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2842 iclog->ic_state == XLOG_STATE_DIRTY)
2847 if (iclog->ic_refcnt == 0) {
2848 /* We are the only one with access to this
2849 * iclog. Flush it out now. There should
2850 * be a roundoff of zero to show that someone
2851 * has already taken care of the roundoff from
2852 * the previous sync.
2854 ASSERT(iclog->ic_roundoff == 0);
2856 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2857 xlog_state_switch_iclogs(log, iclog, 0);
2860 if (xlog_state_release_iclog(log, iclog))
2861 return XFS_ERROR(EIO);
2863 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2864 iclog->ic_state != XLOG_STATE_DIRTY)
2869 /* Someone else is writing to this iclog.
2870 * Use its call to flush out the data. However,
2871 * the other thread may not force out this LR,
2872 * so we mark it WANT_SYNC.
2874 xlog_state_switch_iclogs(log, iclog, 0);
2880 /* By the time we come around again, the iclog could've been filled
2881 * which would give it another lsn. If we have a new lsn, just
2882 * return because the relevant data has been flushed.
2885 if (flags & XFS_LOG_SYNC) {
2887 * We must check if we're shutting down here, before
2888 * we wait, while we're holding the LOG_LOCK.
2889 * Then we check again after waking up, in case our
2890 * sleep was disturbed by a bad news.
2892 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2894 return XFS_ERROR(EIO);
2896 XFS_STATS_INC(xs_log_force_sleep);
2897 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
2899 * No need to grab the log lock here since we're
2900 * only deciding whether or not to return EIO
2901 * and the memory read should be atomic.
2903 if (iclog->ic_state & XLOG_STATE_IOERROR)
2904 return XFS_ERROR(EIO);
2912 } /* xlog_state_sync_all */
2916 * Used by code which implements synchronous log forces.
2918 * Find in-core log with lsn.
2919 * If it is in the DIRTY state, just return.
2920 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2921 * state and go to sleep or return.
2922 * If it is in any other state, go to sleep or return.
2924 * If filesystem activity goes to zero, the iclog will get flushed only by
2928 xlog_state_sync(xlog_t *log,
2932 xlog_in_core_t *iclog;
2933 int already_slept = 0;
2939 iclog = log->l_iclog;
2941 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2943 return XFS_ERROR(EIO);
2947 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
2948 iclog = iclog->ic_next;
2952 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2957 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
2959 * We sleep here if we haven't already slept (e.g.
2960 * this is the first time we've looked at the correct
2961 * iclog buf) and the buffer before us is going to
2962 * be sync'ed. The reason for this is that if we
2963 * are doing sync transactions here, by waiting for
2964 * the previous I/O to complete, we can allow a few
2965 * more transactions into this iclog before we close
2968 * Otherwise, we mark the buffer WANT_SYNC, and bump
2969 * up the refcnt so we can release the log (which drops
2970 * the ref count). The state switch keeps new transaction
2971 * commits from using this buffer. When the current commits
2972 * finish writing into the buffer, the refcount will drop to
2973 * zero and the buffer will go out then.
2975 if (!already_slept &&
2976 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
2977 XLOG_STATE_SYNCING))) {
2978 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
2979 XFS_STATS_INC(xs_log_force_sleep);
2980 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
2981 &log->l_icloglock, s);
2986 xlog_state_switch_iclogs(log, iclog, 0);
2988 if (xlog_state_release_iclog(log, iclog))
2989 return XFS_ERROR(EIO);
2994 if ((flags & XFS_LOG_SYNC) && /* sleep */
2995 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
2998 * Don't wait on the forcesema if we know that we've
2999 * gotten a log write error.
3001 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3003 return XFS_ERROR(EIO);
3005 XFS_STATS_INC(xs_log_force_sleep);
3006 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3008 * No need to grab the log lock here since we're
3009 * only deciding whether or not to return EIO
3010 * and the memory read should be atomic.
3012 if (iclog->ic_state & XLOG_STATE_IOERROR)
3013 return XFS_ERROR(EIO);
3014 } else { /* just return */
3019 } while (iclog != log->l_iclog);
3023 } /* xlog_state_sync */
3027 * Called when we want to mark the current iclog as being ready to sync to
3031 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3037 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3038 xlog_state_switch_iclogs(log, iclog, 0);
3040 ASSERT(iclog->ic_state &
3041 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3045 } /* xlog_state_want_sync */
3049 /*****************************************************************************
3053 *****************************************************************************
3057 * Algorithm doesn't take into account page size. ;-(
3060 xlog_state_ticket_alloc(xlog_t *log)
3062 xlog_ticket_t *t_list;
3063 xlog_ticket_t *next;
3065 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3069 * The kmem_zalloc may sleep, so we shouldn't be holding the
3070 * global lock. XXXmiken: may want to use zone allocator.
3072 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3076 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3077 t_list = (xlog_ticket_t *)buf;
3078 t_list->t_next = log->l_unmount_free;
3079 log->l_unmount_free = t_list++;
3080 log->l_ticket_cnt++;
3081 log->l_ticket_tcnt++;
3083 /* Next ticket becomes first ticket attached to ticket free list */
3084 if (log->l_freelist != NULL) {
3085 ASSERT(log->l_tail != NULL);
3086 log->l_tail->t_next = t_list;
3088 log->l_freelist = t_list;
3090 log->l_ticket_cnt++;
3091 log->l_ticket_tcnt++;
3093 /* Cycle through rest of alloc'ed memory, building up free Q */
3094 for ( ; i > 0; i--) {
3096 t_list->t_next = next;
3098 log->l_ticket_cnt++;
3099 log->l_ticket_tcnt++;
3102 log->l_tail = t_list;
3104 } /* xlog_state_ticket_alloc */
3108 * Put ticket into free list
3110 * Assumption: log lock is held around this call.
3113 xlog_ticket_put(xlog_t *log,
3114 xlog_ticket_t *ticket)
3116 sv_destroy(&ticket->t_sema);
3119 * Don't think caching will make that much difference. It's
3120 * more important to make debug easier.
3123 /* real code will want to use LIFO for caching */
3124 ticket->t_next = log->l_freelist;
3125 log->l_freelist = ticket;
3126 /* no need to clear fields */
3128 /* When we debug, it is easier if tickets are cycled */
3130 if (log->l_tail != 0) {
3131 log->l_tail->t_next = ticket;
3133 ASSERT(log->l_freelist == 0);
3134 log->l_freelist = ticket;
3136 log->l_tail = ticket;
3138 log->l_ticket_cnt++;
3139 } /* xlog_ticket_put */
3143 * Grab ticket off freelist or allocation some more
3146 xlog_ticket_get(xlog_t *log,
3157 if (log->l_freelist == NULL)
3158 xlog_state_ticket_alloc(log); /* potentially sleep */
3161 if (log->l_freelist == NULL) {
3165 tic = log->l_freelist;
3166 log->l_freelist = tic->t_next;
3167 if (log->l_freelist == NULL)
3169 log->l_ticket_cnt--;
3173 * Permanent reservations have up to 'cnt'-1 active log operations
3174 * in the log. A unit in this case is the amount of space for one
3175 * of these log operations. Normal reservations have a cnt of 1
3176 * and their unit amount is the total amount of space required.
3178 * The following lines of code account for non-transaction data
3179 * which occupy space in the on-disk log.
3183 unit_bytes += sizeof(xlog_op_header_t);
3186 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3187 log->l_mp->m_sb.sb_logsunit > 1) {
3188 /* log su roundoff */
3189 unit_bytes += log->l_mp->m_sb.sb_logsunit;
3192 unit_bytes += BBSIZE;
3195 /* for commit-rec */
3196 unit_bytes += sizeof(xlog_op_header_t);
3198 /* for LR headers */
3199 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3200 unit_bytes += log->l_iclog_hsize * num_headers;
3202 tic->t_unit_res = unit_bytes;
3203 tic->t_curr_res = unit_bytes;
3206 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3207 tic->t_clientid = client;
3208 tic->t_flags = XLOG_TIC_INITED;
3209 if (xflags & XFS_LOG_PERM_RESERV)
3210 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3211 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3214 } /* xlog_ticket_get */
3217 /******************************************************************************
3219 * Log debug routines
3221 ******************************************************************************
3223 #if defined(DEBUG) && !defined(XLOG_NOLOG)
3225 * Make sure that the destination ptr is within the valid data region of
3226 * one of the iclogs. This uses backup pointers stored in a different
3227 * part of the log in case we trash the log structure.
3230 xlog_verify_dest_ptr(xlog_t *log,
3236 for (i=0; i < log->l_iclog_bufs; i++) {
3237 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3238 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3242 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3243 } /* xlog_verify_dest_ptr */
3247 /* check split LR write */
3249 xlog_verify_disk_cycle_no(xlog_t *log,
3250 xlog_in_core_t *iclog)
3257 if (BLOCK_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT) < 10) {
3258 cycle_no = CYCLE_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT);
3259 bp = xlog_get_bp(log, 1);
3261 for (i = 0; i < BLOCK_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT); i++) {
3262 xlog_bread(log, i, 1, bp);
3263 ptr = xlog_align(log, i, 1, bp);
3264 if (GET_CYCLE(ptr, ARCH_CONVERT) != cycle_no)
3265 xlog_warn("XFS: xlog_verify_disk_cycle_no: bad cycle no");
3269 } /* xlog_verify_disk_cycle_no */
3273 xlog_verify_grant_head(xlog_t *log, int equals)
3275 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3277 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3279 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3281 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3282 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3284 } /* xlog_verify_grant_head */
3286 /* check if it will fit */
3288 xlog_verify_tail_lsn(xlog_t *log,
3289 xlog_in_core_t *iclog,
3294 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) == log->l_prev_cycle) {
3296 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn, ARCH_NOCONVERT));
3297 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3298 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3300 ASSERT(CYCLE_LSN(tail_lsn, ARCH_NOCONVERT)+1 == log->l_prev_cycle);
3302 if (BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) == log->l_prev_block)
3303 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3305 blocks = BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) - log->l_prev_block;
3306 if (blocks < BTOBB(iclog->ic_offset) + 1)
3307 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3309 } /* xlog_verify_tail_lsn */
3312 * Perform a number of checks on the iclog before writing to disk.
3314 * 1. Make sure the iclogs are still circular
3315 * 2. Make sure we have a good magic number
3316 * 3. Make sure we don't have magic numbers in the data
3317 * 4. Check fields of each log operation header for:
3318 * A. Valid client identifier
3319 * B. tid ptr value falls in valid ptr space (user space code)
3320 * C. Length in log record header is correct according to the
3321 * individual operation headers within record.
3322 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3323 * log, check the preceding blocks of the physical log to make sure all
3324 * the cycle numbers agree with the current cycle number.
3327 xlog_verify_iclog(xlog_t *log,
3328 xlog_in_core_t *iclog,
3332 xlog_op_header_t *ophead;
3333 xlog_in_core_t *icptr;
3334 xlog_in_core_2_t *xhdr;
3336 xfs_caddr_t base_ptr;
3337 __psint_t field_offset;
3339 int len, i, j, k, op_len;
3343 /* check validity of iclog pointers */
3345 icptr = log->l_iclog;
3346 for (i=0; i < log->l_iclog_bufs; i++) {
3348 xlog_panic("xlog_verify_iclog: invalid ptr");
3349 icptr = icptr->ic_next;
3351 if (icptr != log->l_iclog)
3352 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3355 /* check log magic numbers */
3356 ptr = (xfs_caddr_t) &(iclog->ic_header);
3357 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3358 xlog_panic("xlog_verify_iclog: invalid magic num");
3360 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3362 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3363 xlog_panic("xlog_verify_iclog: unexpected magic num");
3367 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3368 ptr = iclog->ic_datap;
3370 ophead = (xlog_op_header_t *)ptr;
3371 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3372 for (i = 0; i < len; i++) {
3373 ophead = (xlog_op_header_t *)ptr;
3375 /* clientid is only 1 byte */
3376 field_offset = (__psint_t)
3377 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3378 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3379 clientid = ophead->oh_clientid;
3381 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3382 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3383 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3384 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3385 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3387 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3390 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3391 cmn_err(CE_WARN, "xlog_verify_iclog: invalid clientid %d op 0x%p offset 0x%x", clientid, ophead, field_offset);
3394 field_offset = (__psint_t)
3395 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3396 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3397 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3399 idx = BTOBBT((__psint_t)&ophead->oh_len -
3400 (__psint_t)iclog->ic_datap);
3401 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3402 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3403 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3404 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3406 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3409 ptr += sizeof(xlog_op_header_t) + op_len;
3411 } /* xlog_verify_iclog */
3412 #endif /* DEBUG && !XLOG_NOLOG */
3415 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3421 xlog_in_core_t *iclog, *ic;
3423 iclog = log->l_iclog;
3424 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3426 * Mark all the incore logs IOERROR.
3427 * From now on, no log flushes will result.
3431 ic->ic_state = XLOG_STATE_IOERROR;
3433 } while (ic != iclog);
3437 * Return non-zero, if state transition has already happened.
3443 * This is called from xfs_force_shutdown, when we're forcibly
3444 * shutting down the filesystem, typically because of an IO error.
3445 * Our main objectives here are to make sure that:
3446 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3447 * parties to find out, 'atomically'.
3448 * b. those who're sleeping on log reservations, pinned objects and
3449 * other resources get woken up, and be told the bad news.
3450 * c. nothing new gets queued up after (a) and (b) are done.
3451 * d. if !logerror, flush the iclogs to disk, then seal them off
3455 xfs_log_force_umount(
3456 struct xfs_mount *mp,
3468 * If this happens during log recovery, don't worry about
3469 * locking; the log isn't open for business yet.
3472 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3473 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3474 XFS_BUF_DONE(mp->m_sb_bp);
3479 * Somebody could've already done the hard work for us.
3480 * No need to get locks for this.
3482 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3483 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3488 * We must hold both the GRANT lock and the LOG lock,
3489 * before we mark the filesystem SHUTDOWN and wake
3490 * everybody up to tell the bad news.
3492 s = GRANT_LOCK(log);
3494 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3495 XFS_BUF_DONE(mp->m_sb_bp);
3497 * This flag is sort of redundant because of the mount flag, but
3498 * it's good to maintain the separation between the log and the rest
3501 log->l_flags |= XLOG_IO_ERROR;
3504 * If we hit a log error, we want to mark all the iclogs IOERROR
3505 * while we're still holding the loglock.
3508 retval = xlog_state_ioerror(log);
3509 LOG_UNLOCK(log, s2);
3512 * We don't want anybody waiting for log reservations
3513 * after this. That means we have to wake up everybody
3514 * queued up on reserve_headq as well as write_headq.
3515 * In addition, we make sure in xlog_{re}grant_log_space
3516 * that we don't enqueue anything once the SHUTDOWN flag
3517 * is set, and this action is protected by the GRANTLOCK.
3519 if ((tic = log->l_reserve_headq)) {
3521 sv_signal(&tic->t_sema);
3523 } while (tic != log->l_reserve_headq);
3526 if ((tic = log->l_write_headq)) {
3528 sv_signal(&tic->t_sema);
3530 } while (tic != log->l_write_headq);
3532 GRANT_UNLOCK(log, s);
3534 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3537 * Force the incore logs to disk before shutting the
3538 * log down completely.
3540 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC);
3542 retval = xlog_state_ioerror(log);
3543 LOG_UNLOCK(log, s2);
3546 * Wake up everybody waiting on xfs_log_force.
3547 * Callback all log item committed functions as if the
3548 * log writes were completed.
3550 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3552 #ifdef XFSERRORDEBUG
3554 xlog_in_core_t *iclog;
3557 iclog = log->l_iclog;
3559 ASSERT(iclog->ic_callback == 0);
3560 iclog = iclog->ic_next;
3561 } while (iclog != log->l_iclog);
3565 /* return non-zero if log IOERROR transition had already happened */
3570 xlog_iclogs_empty(xlog_t *log)
3572 xlog_in_core_t *iclog;
3574 iclog = log->l_iclog;
3576 /* endianness does not matter here, zero is zero in
3579 if (iclog->ic_header.h_num_logops)
3581 iclog = iclog->ic_next;
3582 } while (iclog != log->l_iclog);