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);
126 STATIC void xlog_verify_disk_cycle_no(xlog_t *log, xlog_in_core_t *iclog);
128 STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
129 STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
130 int count, boolean_t syncing);
131 STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
134 #define xlog_verify_dest_ptr(a,b)
135 #define xlog_verify_disk_cycle_no(a,b)
136 #define xlog_verify_grant_head(a,b)
137 #define xlog_verify_iclog(a,b,c,d)
138 #define xlog_verify_tail_lsn(a,b,c)
141 int xlog_iclogs_empty(xlog_t *log);
144 int xlog_do_error = 0;
145 int xlog_req_num = 0;
146 int xlog_error_mod = 33;
149 #define XLOG_FORCED_SHUTDOWN(log) (log->l_flags & XLOG_IO_ERROR)
152 * 0 => disable log manager
153 * 1 => enable log manager
154 * 2 => enable log manager and log debugging
156 #if defined(XLOG_NOLOG) || defined(DEBUG)
158 xfs_buftarg_t *xlog_target;
161 #if defined(XFS_LOG_TRACE)
164 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
166 if (! log->l_grant_trace) {
167 log->l_grant_trace = ktrace_alloc(1024, KM_NOSLEEP);
168 if (! log->l_grant_trace)
172 ktrace_enter(log->l_grant_trace,
174 (void *)log->l_reserve_headq,
175 (void *)log->l_write_headq,
176 (void *)((unsigned long)log->l_grant_reserve_cycle),
177 (void *)((unsigned long)log->l_grant_reserve_bytes),
178 (void *)((unsigned long)log->l_grant_write_cycle),
179 (void *)((unsigned long)log->l_grant_write_bytes),
180 (void *)((unsigned long)log->l_curr_cycle),
181 (void *)((unsigned long)log->l_curr_block),
182 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn, ARCH_NOCONVERT)),
183 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn, ARCH_NOCONVERT)),
185 (void *)((unsigned long)13),
186 (void *)((unsigned long)14),
187 (void *)((unsigned long)15),
188 (void *)((unsigned long)16));
192 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
198 if (!iclog->ic_trace)
199 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
200 ktrace_enter(iclog->ic_trace,
201 (void *)((unsigned long)state),
202 (void *)((unsigned long)pid),
220 #define xlog_trace_loggrant(log,tic,string)
221 #define xlog_trace_iclog(iclog,state)
222 #endif /* XFS_LOG_TRACE */
227 * 1. currblock field gets updated at startup and after in-core logs
228 * marked as with WANT_SYNC.
232 * This routine is called when a user of a log manager ticket is done with
233 * the reservation. If the ticket was ever used, then a commit record for
234 * the associated transaction is written out as a log operation header with
235 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
236 * a given ticket. If the ticket was one with a permanent reservation, then
237 * a few operations are done differently. Permanent reservation tickets by
238 * default don't release the reservation. They just commit the current
239 * transaction with the belief that the reservation is still needed. A flag
240 * must be passed in before permanent reservations are actually released.
241 * When these type of tickets are not released, they need to be set into
242 * the inited state again. By doing this, a start record will be written
243 * out when the next write occurs.
246 xfs_log_done(xfs_mount_t *mp,
247 xfs_log_ticket_t xtic,
251 xlog_t *log = mp->m_log;
252 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
255 #if defined(DEBUG) || defined(XLOG_NOLOG)
256 if (!xlog_debug && xlog_target == log->l_targ)
260 if (XLOG_FORCED_SHUTDOWN(log) ||
262 * If nothing was ever written, don't write out commit record.
263 * If we get an error, just continue and give back the log ticket.
265 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
266 (xlog_commit_record(mp, ticket,
267 (xlog_in_core_t **)iclog, &lsn)))) {
268 lsn = (xfs_lsn_t) -1;
269 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
270 flags |= XFS_LOG_REL_PERM_RESERV;
275 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
276 (flags & XFS_LOG_REL_PERM_RESERV)) {
278 * Release ticket if not permanent reservation or a specifc
279 * request has been made to release a permanent reservation.
281 xlog_ungrant_log_space(log, ticket);
282 xlog_state_put_ticket(log, ticket);
284 xlog_regrant_reserve_log_space(log, ticket);
287 /* If this ticket was a permanent reservation and we aren't
288 * trying to release it, reset the inited flags; so next time
289 * we write, a start record will be written out.
291 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
292 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
293 ticket->t_flags |= XLOG_TIC_INITED;
300 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
301 * the force is done synchronously.
303 * Asynchronous forces are implemented by setting the WANT_SYNC
304 * bit in the appropriate in-core log and then returning.
306 * Synchronous forces are implemented with a semaphore. All callers
307 * to force a given lsn to disk will wait on a semaphore attached to the
308 * specific in-core log. When given in-core log finally completes its
309 * write to disk, that thread will wake up all threads waiting on the
313 xfs_log_force(xfs_mount_t *mp,
318 xlog_t *log = mp->m_log;
320 #if defined(DEBUG) || defined(XLOG_NOLOG)
321 if (!xlog_debug && xlog_target == log->l_targ)
325 ASSERT(flags & XFS_LOG_FORCE);
327 XFS_STATS_INC(xs_log_force);
329 if ((log->l_flags & XLOG_IO_ERROR) == 0) {
331 rval = xlog_state_sync_all(log, flags);
333 rval = xlog_state_sync(log, lsn, flags);
335 rval = XFS_ERROR(EIO);
340 } /* xfs_log_force */
344 * This function will take a log sequence number and check to see if that
345 * lsn has been flushed to disk. If it has, then the callback function is
346 * called with the callback argument. If the relevant in-core log has not
347 * been synced to disk, we add the callback to the callback list of the
351 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
352 void *iclog_hndl, /* iclog to hang callback off */
353 xfs_log_callback_t *cb)
355 xlog_t *log = mp->m_log;
356 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
359 #if defined(DEBUG) || defined(XLOG_NOLOG)
360 if (!xlog_debug && xlog_target == log->l_targ)
365 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
367 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
368 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
370 *(iclog->ic_callback_tail) = cb;
371 iclog->ic_callback_tail = &(cb->cb_next);
373 LOG_UNLOCK(log, spl);
375 cb->cb_func(cb->cb_arg, abortflg);
378 } /* xfs_log_notify */
381 xfs_log_release_iclog(xfs_mount_t *mp,
384 xlog_t *log = mp->m_log;
385 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
387 if (xlog_state_release_iclog(log, iclog)) {
388 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
396 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
397 * to the reservation.
398 * 2. Potentially, push buffers at tail of log to disk.
400 * Each reservation is going to reserve extra space for a log record header.
401 * When writes happen to the on-disk log, we don't subtract the length of the
402 * log record header from any reservation. By wasting space in each
403 * reservation, we prevent over allocation problems.
406 xfs_log_reserve(xfs_mount_t *mp,
409 xfs_log_ticket_t *ticket,
413 xlog_t *log = mp->m_log;
414 xlog_ticket_t *internal_ticket;
417 #if defined(DEBUG) || defined(XLOG_NOLOG)
418 if (!xlog_debug && xlog_target == log->l_targ)
422 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
423 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
425 if (XLOG_FORCED_SHUTDOWN(log))
426 return XFS_ERROR(EIO);
428 XFS_STATS_INC(xs_try_logspace);
430 if (*ticket != NULL) {
431 ASSERT(flags & XFS_LOG_PERM_RESERV);
432 internal_ticket = (xlog_ticket_t *)*ticket;
433 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
434 retval = xlog_regrant_write_log_space(log, internal_ticket);
436 /* may sleep if need to allocate more tickets */
437 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
439 *ticket = internal_ticket;
440 xlog_grant_push_ail(mp,
441 (internal_ticket->t_unit_res *
442 internal_ticket->t_cnt));
443 retval = xlog_grant_log_space(log, internal_ticket);
447 } /* xfs_log_reserve */
451 * Mount a log filesystem
453 * mp - ubiquitous xfs mount point structure
454 * log_target - buftarg of on-disk log device
455 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
456 * num_bblocks - Number of BBSIZE blocks in on-disk log
458 * Return error or zero.
461 xfs_log_mount(xfs_mount_t *mp,
462 xfs_buftarg_t *log_target,
463 xfs_daddr_t blk_offset,
466 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
467 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
470 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
472 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
475 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
477 #if defined(DEBUG) || defined(XLOG_NOLOG)
479 cmn_err(CE_NOTE, "log dev: %s", XFS_BUFTARG_NAME(log_target));
484 * skip log recovery on a norecovery mount. pretend it all
487 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
489 vfs_t *vfsp = XFS_MTOVFS(mp);
490 int readonly = (vfsp->vfs_flag & VFS_RDONLY);
493 vfsp->vfs_flag &= ~VFS_RDONLY;
495 error = xlog_recover(mp->m_log, readonly);
498 vfsp->vfs_flag |= VFS_RDONLY;
500 cmn_err(CE_WARN, "XFS: log mount/recovery failed");
501 xlog_unalloc_log(mp->m_log);
506 /* Normal transactions can now occur */
507 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
509 /* End mounting message in xfs_log_mount_finish */
511 } /* xfs_log_mount */
514 * Finish the recovery of the file system. This is separate from
515 * the xfs_log_mount() call, because it depends on the code in
516 * xfs_mountfs() to read in the root and real-time bitmap inodes
517 * between calling xfs_log_mount() and here.
519 * mp - ubiquitous xfs mount point structure
522 xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
526 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
527 error = xlog_recover_finish(mp->m_log, mfsi_flags);
530 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
537 * Unmount processing for the log.
540 xfs_log_unmount(xfs_mount_t *mp)
544 error = xfs_log_unmount_write(mp);
545 xfs_log_unmount_dealloc(mp);
550 * Final log writes as part of unmount.
552 * Mark the filesystem clean as unmount happens. Note that during relocation
553 * this routine needs to be executed as part of source-bag while the
554 * deallocation must not be done until source-end.
558 * Unmount record used to have a string "Unmount filesystem--" in the
559 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
560 * We just write the magic number now since that particular field isn't
561 * currently architecture converted and "nUmount" is a bit foo.
562 * As far as I know, there weren't any dependencies on the old behaviour.
566 xfs_log_unmount_write(xfs_mount_t *mp)
568 xlog_t *log = mp->m_log;
569 xlog_in_core_t *iclog;
571 xlog_in_core_t *first_iclog;
573 xfs_log_iovec_t reg[1];
574 xfs_log_ticket_t tic = 0;
579 /* the data section must be 32 bit size aligned */
583 __uint32_t pad2; /* may as well make it 64 bits */
584 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
586 #if defined(DEBUG) || defined(XLOG_NOLOG)
587 if (!xlog_debug && xlog_target == log->l_targ)
592 * Don't write out unmount record on read-only mounts.
593 * Or, if we are doing a forced umount (typically because of IO errors).
595 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
598 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
601 first_iclog = iclog = log->l_iclog;
603 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
604 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
605 ASSERT(iclog->ic_offset == 0);
607 iclog = iclog->ic_next;
608 } while (iclog != first_iclog);
610 if (! (XLOG_FORCED_SHUTDOWN(log))) {
611 reg[0].i_addr = (void*)&magic;
612 reg[0].i_len = sizeof(magic);
614 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0);
616 /* remove inited flag */
617 ((xlog_ticket_t *)tic)->t_flags = 0;
618 error = xlog_write(mp, reg, 1, tic, &lsn,
619 NULL, XLOG_UNMOUNT_TRANS);
621 * At this point, we're umounting anyway,
622 * so there's no point in transitioning log state
623 * to IOERROR. Just continue...
628 xfs_fs_cmn_err(CE_ALERT, mp,
629 "xfs_log_unmount: unmount record failed");
634 iclog = log->l_iclog;
637 xlog_state_want_sync(log, iclog);
638 (void) xlog_state_release_iclog(log, iclog);
641 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
642 iclog->ic_state == XLOG_STATE_DIRTY)) {
643 if (!XLOG_FORCED_SHUTDOWN(log)) {
644 sv_wait(&iclog->ic_forcesema, PMEM,
645 &log->l_icloglock, s);
653 xlog_state_put_ticket(log, tic);
656 * We're already in forced_shutdown mode, couldn't
657 * even attempt to write out the unmount transaction.
659 * Go through the motions of sync'ing and releasing
660 * the iclog, even though no I/O will actually happen,
661 * we need to wait for other log I/O's that may already
662 * be in progress. Do this as a separate section of
663 * code so we'll know if we ever get stuck here that
664 * we're in this odd situation of trying to unmount
665 * a file system that went into forced_shutdown as
666 * the result of an unmount..
669 iclog = log->l_iclog;
673 xlog_state_want_sync(log, iclog);
674 (void) xlog_state_release_iclog(log, iclog);
678 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
679 || iclog->ic_state == XLOG_STATE_DIRTY
680 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
682 sv_wait(&iclog->ic_forcesema, PMEM,
683 &log->l_icloglock, s);
690 } /* xfs_log_unmount_write */
693 * Deallocate log structures for unmount/relocation.
696 xfs_log_unmount_dealloc(xfs_mount_t *mp)
698 xlog_unalloc_log(mp->m_log);
702 * Write region vectors to log. The write happens using the space reservation
703 * of the ticket (tic). It is not a requirement that all writes for a given
704 * transaction occur with one call to xfs_log_write().
707 xfs_log_write(xfs_mount_t * mp,
708 xfs_log_iovec_t reg[],
710 xfs_log_ticket_t tic,
711 xfs_lsn_t *start_lsn)
714 xlog_t *log = mp->m_log;
716 #if defined(DEBUG) || defined(XLOG_NOLOG)
717 if (!xlog_debug && xlog_target == log->l_targ) {
722 if (XLOG_FORCED_SHUTDOWN(log))
723 return XFS_ERROR(EIO);
725 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
726 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
729 } /* xfs_log_write */
733 xfs_log_move_tail(xfs_mount_t *mp,
737 xlog_t *log = mp->m_log;
738 int need_bytes, free_bytes, cycle, bytes;
741 #if defined(DEBUG) || defined(XLOG_NOLOG)
742 if (!xlog_debug && xlog_target == log->l_targ)
746 if (XLOG_FORCED_SHUTDOWN(log))
748 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
751 /* needed since sync_lsn is 64 bits */
753 tail_lsn = log->l_last_sync_lsn;
759 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
763 log->l_tail_lsn = tail_lsn;
766 if ((tic = log->l_write_headq)) {
768 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
769 panic("Recovery problem");
771 cycle = log->l_grant_write_cycle;
772 bytes = log->l_grant_write_bytes;
773 free_bytes = xlog_space_left(log, cycle, bytes);
775 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
777 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
780 free_bytes -= tic->t_unit_res;
781 sv_signal(&tic->t_sema);
783 } while (tic != log->l_write_headq);
785 if ((tic = log->l_reserve_headq)) {
787 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
788 panic("Recovery problem");
790 cycle = log->l_grant_reserve_cycle;
791 bytes = log->l_grant_reserve_bytes;
792 free_bytes = xlog_space_left(log, cycle, bytes);
794 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
795 need_bytes = tic->t_unit_res*tic->t_cnt;
797 need_bytes = tic->t_unit_res;
798 if (free_bytes < need_bytes && tail_lsn != 1)
801 free_bytes -= need_bytes;
802 sv_signal(&tic->t_sema);
804 } while (tic != log->l_reserve_headq);
806 GRANT_UNLOCK(log, s);
807 } /* xfs_log_move_tail */
810 * Determine if we have a transaction that has gone to disk
811 * that needs to be covered. Log activity needs to be idle (no AIL and
812 * nothing in the iclogs). And, we need to be in the right state indicating
813 * something has gone out.
816 xfs_log_need_covered(xfs_mount_t *mp)
820 xlog_t *log = mp->m_log;
821 vfs_t *vfsp = XFS_MTOVFS(mp);
823 if (vfsp->vfs_super->s_frozen || XFS_FORCED_SHUTDOWN(mp) ||
824 (vfsp->vfs_flag & VFS_RDONLY))
828 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
829 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
830 && !xfs_trans_first_ail(mp, &gen)
831 && xlog_iclogs_empty(log)) {
832 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
833 log->l_covered_state = XLOG_STATE_COVER_DONE;
835 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
836 log->l_covered_state = XLOG_STATE_COVER_DONE2;
844 /******************************************************************************
848 ******************************************************************************
851 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
852 * The log manager must keep track of the last LR which was committed
853 * to disk. The lsn of this LR will become the new tail_lsn whenever
854 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
855 * the situation where stuff could be written into the log but nothing
856 * was ever in the AIL when asked. Eventually, we panic since the
857 * tail hits the head.
859 * We may be holding the log iclog lock upon entering this routine.
862 xlog_assign_tail_lsn(xfs_mount_t *mp)
866 xlog_t *log = mp->m_log;
868 tail_lsn = xfs_trans_tail_ail(mp);
871 log->l_tail_lsn = tail_lsn;
873 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
875 GRANT_UNLOCK(log, s);
878 } /* xlog_assign_tail_lsn */
882 * Return the space in the log between the tail and the head. The head
883 * is passed in the cycle/bytes formal parms. In the special case where
884 * the reserve head has wrapped passed the tail, this calculation is no
885 * longer valid. In this case, just return 0 which means there is no space
886 * in the log. This works for all places where this function is called
887 * with the reserve head. Of course, if the write head were to ever
888 * wrap the tail, we should blow up. Rather than catch this case here,
889 * we depend on other ASSERTions in other parts of the code. XXXmiken
891 * This code also handles the case where the reservation head is behind
892 * the tail. The details of this case are described below, but the end
893 * result is that we return the size of the log as the amount of space left.
896 xlog_space_left(xlog_t *log, int cycle, int bytes)
902 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn, ARCH_NOCONVERT));
903 tail_cycle = CYCLE_LSN(log->l_tail_lsn, ARCH_NOCONVERT);
904 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
905 free_bytes = log->l_logsize - (bytes - tail_bytes);
906 } else if ((tail_cycle + 1) < cycle) {
908 } else if (tail_cycle < cycle) {
909 ASSERT(tail_cycle == (cycle - 1));
910 free_bytes = tail_bytes - bytes;
913 * The reservation head is behind the tail.
914 * This can only happen when the AIL is empty so the tail
915 * is equal to the head and the l_roundoff value in the
916 * log structure is taking up the difference between the
917 * reservation head and the tail. The bytes accounted for
918 * by the l_roundoff field are temporarily 'lost' to the
919 * reservation mechanism, but they are cleaned up when the
920 * log buffers that created them are reused. These lost
921 * bytes are what allow the reservation head to fall behind
922 * the tail in the case that the log is 'empty'.
923 * In this case we just want to return the size of the
924 * log as the amount of space left.
926 ASSERT((tail_cycle == (cycle + 1)) ||
927 ((bytes + log->l_roundoff) >= tail_bytes));
928 free_bytes = log->l_logsize;
931 } /* xlog_space_left */
935 * Log function which is called when an io completes.
937 * The log manager needs its own routine, in order to control what
938 * happens with the buffer after the write completes.
941 xlog_iodone(xfs_buf_t *bp)
943 xlog_in_core_t *iclog;
947 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
948 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
949 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
953 * Some versions of cpp barf on the recursive definition of
954 * ic_log -> hic_fields.ic_log and expand ic_log twice when
955 * it is passed through two macros. Workaround broken cpp.
960 * Race to shutdown the filesystem if we see an error.
962 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
963 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
964 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
966 xfs_force_shutdown(l->l_mp, XFS_LOG_IO_ERROR);
968 * This flag will be propagated to the trans-committed
969 * callback routines to let them know that the log-commit
972 aborted = XFS_LI_ABORTED;
973 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
974 aborted = XFS_LI_ABORTED;
976 xlog_state_done_syncing(iclog, aborted);
977 if (!(XFS_BUF_ISASYNC(bp))) {
979 * Corresponding psema() will be done in bwrite(). If we don't
980 * vsema() here, panic.
982 XFS_BUF_V_IODONESEMA(bp);
987 * The bdstrat callback function for log bufs. This gives us a central
988 * place to trap bufs in case we get hit by a log I/O error and need to
989 * shutdown. Actually, in practice, even when we didn't get a log error,
990 * we transition the iclogs to IOERROR state *after* flushing all existing
991 * iclogs to disk. This is because we don't want anymore new transactions to be
992 * started or completed afterwards.
995 xlog_bdstrat_cb(struct xfs_buf *bp)
997 xlog_in_core_t *iclog;
999 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1001 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1002 /* note for irix bstrat will need struct bdevsw passed
1003 * Fix the following macro if the code ever is merged
1009 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1010 XFS_BUF_ERROR(bp, EIO);
1013 return (XFS_ERROR(EIO));
1019 * Return size of each in-core log record buffer.
1021 * Low memory machines only get 2 16KB buffers. We don't want to waste
1022 * memory here. However, all other machines get at least 2 32KB buffers.
1023 * The number is hard coded because we don't care about the minimum
1024 * memory size, just 32MB systems.
1026 * If the filesystem blocksize is too large, we may need to choose a
1027 * larger size since the directory code currently logs entire blocks.
1031 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1037 #if defined(DEBUG) || defined(XLOG_NOLOG)
1039 * When logbufs == 0, someone has disabled the log from the FSTAB
1040 * file. This is not a documented feature. We need to set xlog_debug
1041 * to zero (this deactivates the log) and set xlog_target to the
1042 * appropriate device. Only one filesystem may be affected as such
1043 * since this is just a performance hack to test what we might be able
1044 * to get if the log were not present.
1046 if (mp->m_logbufs == 0) {
1048 xlog_target = log->l_targ;
1049 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1054 * This is the normal path. If m_logbufs == -1, then the
1055 * admin has chosen to use the system defaults for logbuffers.
1057 if (mp->m_logbufs == -1) {
1058 if (xfs_physmem <= btoc(128*1024*1024)) {
1059 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1060 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1061 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1063 /* 256K with 32K bufs */
1064 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1067 log->l_iclog_bufs = mp->m_logbufs;
1069 #if defined(DEBUG) || defined(XLOG_NOLOG)
1070 /* We are reactivating a filesystem after it was inactive */
1071 if (log->l_targ == xlog_target) {
1079 * Buffer size passed in from mount system call.
1081 if (mp->m_logbsize != -1) {
1082 size = log->l_iclog_size = mp->m_logbsize;
1083 log->l_iclog_size_log = 0;
1085 log->l_iclog_size_log++;
1089 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1090 /* # headers = size / 32K
1091 * one header holds cycles from 32K of data
1094 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1095 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1097 log->l_iclog_hsize = xhdrs << BBSHIFT;
1098 log->l_iclog_heads = xhdrs;
1100 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1101 log->l_iclog_hsize = BBSIZE;
1102 log->l_iclog_heads = 1;
1108 * Special case machines that have less than 32MB of memory.
1109 * All machines with more memory use 32KB buffers.
1111 if (xfs_physmem <= btoc(32*1024*1024)) {
1112 /* Don't change; min configuration */
1113 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1114 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1116 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1117 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1120 /* the default log size is 16k or 32k which is one header sector */
1121 log->l_iclog_hsize = BBSIZE;
1122 log->l_iclog_heads = 1;
1125 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1126 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1128 if (mp->m_sb.sb_blocksize >= 16*1024) {
1129 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1130 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1131 if (mp->m_logbufs == -1) {
1132 switch (mp->m_sb.sb_blocksize) {
1133 case 16*1024: /* 16 KB */
1134 log->l_iclog_bufs = 3;
1136 case 32*1024: /* 32 KB */
1137 log->l_iclog_bufs = 4;
1139 case 64*1024: /* 64 KB */
1140 log->l_iclog_bufs = 8;
1143 xlog_panic("XFS: Invalid blocksize");
1148 } /* xlog_get_iclog_buffer_size */
1152 * This routine initializes some of the log structure for a given mount point.
1153 * Its primary purpose is to fill in enough, so recovery can occur. However,
1154 * some other stuff may be filled in too.
1157 xlog_alloc_log(xfs_mount_t *mp,
1158 xfs_buftarg_t *log_target,
1159 xfs_daddr_t blk_offset,
1163 xlog_rec_header_t *head;
1164 xlog_in_core_t **iclogp;
1165 xlog_in_core_t *iclog, *prev_iclog=NULL;
1170 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1173 log->l_targ = log_target;
1174 log->l_logsize = BBTOB(num_bblks);
1175 log->l_logBBstart = blk_offset;
1176 log->l_logBBsize = num_bblks;
1177 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1178 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1180 log->l_prev_block = -1;
1181 ASSIGN_ANY_LSN(log->l_tail_lsn, 1, 0, ARCH_NOCONVERT);
1182 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1183 log->l_last_sync_lsn = log->l_tail_lsn;
1184 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1185 log->l_grant_reserve_cycle = 1;
1186 log->l_grant_write_cycle = 1;
1188 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1189 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1190 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1191 /* for larger sector sizes, must have v2 or external log */
1192 ASSERT(log->l_sectbb_log == 0 ||
1193 log->l_logBBstart == 0 ||
1194 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1195 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1197 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1199 xlog_get_iclog_buffer_size(mp, log);
1201 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1202 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1203 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1204 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1205 ASSERT(XFS_BUF_ISBUSY(bp));
1206 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1209 spinlock_init(&log->l_icloglock, "iclog");
1210 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1211 initnsema(&log->l_flushsema, 0, "ic-flush");
1212 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1214 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1215 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1217 iclogp = &log->l_iclog;
1219 * The amount of memory to allocate for the iclog structure is
1220 * rather funky due to the way the structure is defined. It is
1221 * done this way so that we can use different sizes for machines
1222 * with different amounts of memory. See the definition of
1223 * xlog_in_core_t in xfs_log_priv.h for details.
1225 iclogsize = log->l_iclog_size;
1226 ASSERT(log->l_iclog_size >= 4096);
1227 for (i=0; i < log->l_iclog_bufs; i++) {
1228 *iclogp = (xlog_in_core_t *)
1229 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1231 iclog->hic_data = (xlog_in_core_2_t *)
1232 kmem_zalloc(iclogsize, KM_SLEEP);
1234 iclog->ic_prev = prev_iclog;
1236 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1238 head = &iclog->ic_header;
1239 memset(head, 0, sizeof(xlog_rec_header_t));
1240 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1241 INT_SET(head->h_version, ARCH_CONVERT,
1242 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1243 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1245 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1246 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1248 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1249 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1250 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1251 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1254 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1255 iclog->ic_state = XLOG_STATE_ACTIVE;
1256 iclog->ic_log = log;
1257 iclog->ic_callback_tail = &(iclog->ic_callback);
1258 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1260 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1261 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1262 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1263 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1265 iclogp = &iclog->ic_next;
1267 *iclogp = log->l_iclog; /* complete ring */
1268 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1271 } /* xlog_alloc_log */
1275 * Write out the commit record of a transaction associated with the given
1276 * ticket. Return the lsn of the commit record.
1279 xlog_commit_record(xfs_mount_t *mp,
1280 xlog_ticket_t *ticket,
1281 xlog_in_core_t **iclog,
1282 xfs_lsn_t *commitlsnp)
1285 xfs_log_iovec_t reg[1];
1290 ASSERT_ALWAYS(iclog);
1291 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1292 iclog, XLOG_COMMIT_TRANS))) {
1293 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
1296 } /* xlog_commit_record */
1300 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1301 * log space. This code pushes on the lsn which would supposedly free up
1302 * the 25% which we want to leave free. We may need to adopt a policy which
1303 * pushes on an lsn which is further along in the log once we reach the high
1304 * water mark. In this manner, we would be creating a low water mark.
1307 xlog_grant_push_ail(xfs_mount_t *mp,
1310 xlog_t *log = mp->m_log; /* pointer to the log */
1311 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1312 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1313 int free_blocks; /* free blocks left to write to */
1314 int free_bytes; /* free bytes left to write to */
1315 int threshold_block; /* block in lsn we'd like to be at */
1316 int threshold_cycle; /* lsn cycle we'd like to be at */
1320 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1322 s = GRANT_LOCK(log);
1323 free_bytes = xlog_space_left(log,
1324 log->l_grant_reserve_cycle,
1325 log->l_grant_reserve_bytes);
1326 tail_lsn = log->l_tail_lsn;
1327 free_blocks = BTOBBT(free_bytes);
1330 * Set the threshold for the minimum number of free blocks in the
1331 * log to the maximum of what the caller needs, one quarter of the
1332 * log, and 256 blocks.
1334 free_threshold = BTOBB(need_bytes);
1335 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1336 free_threshold = MAX(free_threshold, 256);
1337 if (free_blocks < free_threshold) {
1338 threshold_block = BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) + free_threshold;
1339 threshold_cycle = CYCLE_LSN(tail_lsn, ARCH_NOCONVERT);
1340 if (threshold_block >= log->l_logBBsize) {
1341 threshold_block -= log->l_logBBsize;
1342 threshold_cycle += 1;
1344 ASSIGN_ANY_LSN(threshold_lsn, threshold_cycle,
1345 threshold_block, ARCH_NOCONVERT);
1347 /* Don't pass in an lsn greater than the lsn of the last
1348 * log record known to be on disk.
1350 if (XFS_LSN_CMP_ARCH(threshold_lsn, log->l_last_sync_lsn, ARCH_NOCONVERT) > 0)
1351 threshold_lsn = log->l_last_sync_lsn;
1353 GRANT_UNLOCK(log, s);
1356 * Get the transaction layer to kick the dirty buffers out to
1357 * disk asynchronously. No point in trying to do this if
1358 * the filesystem is shutting down.
1360 if (threshold_lsn &&
1361 !XLOG_FORCED_SHUTDOWN(log))
1362 xfs_trans_push_ail(mp, threshold_lsn);
1363 } /* xlog_grant_push_ail */
1367 * Flush out the in-core log (iclog) to the on-disk log in a synchronous or
1368 * asynchronous fashion. Previously, we should have moved the current iclog
1369 * ptr in the log to point to the next available iclog. This allows further
1370 * write to continue while this code syncs out an iclog ready to go.
1371 * Before an in-core log can be written out, the data section must be scanned
1372 * to save away the 1st word of each BBSIZE block into the header. We replace
1373 * it with the current cycle count. Each BBSIZE block is tagged with the
1374 * cycle count because there in an implicit assumption that drives will
1375 * guarantee that entire 512 byte blocks get written at once. In other words,
1376 * we can't have part of a 512 byte block written and part not written. By
1377 * tagging each block, we will know which blocks are valid when recovering
1378 * after an unclean shutdown.
1380 * This routine is single threaded on the iclog. No other thread can be in
1381 * this routine with the same iclog. Changing contents of iclog can there-
1382 * fore be done without grabbing the state machine lock. Updating the global
1383 * log will require grabbing the lock though.
1385 * The entire log manager uses a logical block numbering scheme. Only
1386 * log_sync (and then only bwrite()) know about the fact that the log may
1387 * not start with block zero on a given device. The log block start offset
1388 * is added immediately before calling bwrite().
1392 xlog_sync(xlog_t *log,
1393 xlog_in_core_t *iclog)
1395 xfs_caddr_t dptr; /* pointer to byte sized element */
1398 uint count; /* byte count of bwrite */
1399 uint count_init; /* initial count before roundup */
1400 int split = 0; /* split write into two regions */
1403 XFS_STATS_INC(xs_log_writes);
1404 ASSERT(iclog->ic_refcnt == 0);
1406 /* Add for LR header */
1407 count_init = log->l_iclog_hsize + iclog->ic_offset;
1409 /* Round out the log write size */
1410 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
1411 log->l_mp->m_sb.sb_logsunit > 1) {
1412 /* we have a v2 stripe unit to use */
1413 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1415 count = BBTOB(BTOBB(count_init));
1417 iclog->ic_roundoff = count - count_init;
1418 log->l_roundoff += iclog->ic_roundoff;
1420 xlog_pack_data(log, iclog); /* put cycle number in every block */
1422 /* real byte length */
1423 INT_SET(iclog->ic_header.h_len,
1425 iclog->ic_offset + iclog->ic_roundoff);
1427 /* put ops count in correct order */
1428 ops = iclog->ic_header.h_num_logops;
1429 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1432 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1433 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1434 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT));
1436 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1438 /* Do we need to split this write into 2 parts? */
1439 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1440 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1441 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1442 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1444 iclog->ic_bwritecnt = 1;
1446 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1447 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1451 * Do a disk write cache flush for the log block.
1452 * This is a bit of a sledgehammer, it would be better
1453 * to use a tag barrier here that just prevents reordering.
1454 * It may not be needed to flush the first split block in the log wrap
1455 * case, but do it anyways to be safe -AK
1457 if (!(log->l_mp->m_flags & XFS_MOUNT_NOLOGFLUSH))
1460 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1461 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1463 xlog_verify_iclog(log, iclog, count, B_TRUE);
1465 /* account for log which doesn't start at block #0 */
1466 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1468 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1473 if ((error = XFS_bwrite(bp))) {
1474 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1479 bp = iclog->ic_log->l_xbuf;
1480 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1482 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1483 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1484 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1485 (__psint_t)count), split);
1486 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1489 if (!(log->l_mp->m_flags & XFS_MOUNT_NOLOGFLUSH))
1491 dptr = XFS_BUF_PTR(bp);
1493 * Bump the cycle numbers at the start of each block
1494 * since this part of the buffer is at the start of
1495 * a new cycle. Watch out for the header magic number
1498 for (i=0; i<split; i += BBSIZE) {
1499 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1500 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1501 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1505 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1506 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1508 /* account for internal log which does't start at block #0 */
1509 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1511 if ((error = XFS_bwrite(bp))) {
1512 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1513 bp, XFS_BUF_ADDR(bp));
1522 * Unallocate a log structure
1525 xlog_unalloc_log(xlog_t *log)
1527 xlog_in_core_t *iclog, *next_iclog;
1528 xlog_ticket_t *tic, *next_tic;
1532 iclog = log->l_iclog;
1533 for (i=0; i<log->l_iclog_bufs; i++) {
1534 sv_destroy(&iclog->ic_forcesema);
1535 sv_destroy(&iclog->ic_writesema);
1536 xfs_buf_free(iclog->ic_bp);
1537 #ifdef XFS_LOG_TRACE
1538 if (iclog->ic_trace != NULL) {
1539 ktrace_free(iclog->ic_trace);
1542 next_iclog = iclog->ic_next;
1543 kmem_free(iclog->hic_data, log->l_iclog_size);
1544 kmem_free(iclog, sizeof(xlog_in_core_t));
1547 freesema(&log->l_flushsema);
1548 spinlock_destroy(&log->l_icloglock);
1549 spinlock_destroy(&log->l_grant_lock);
1551 /* XXXsup take a look at this again. */
1552 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1553 !XLOG_FORCED_SHUTDOWN(log)) {
1554 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1555 "xlog_unalloc_log: (cnt: %d, total: %d)",
1556 log->l_ticket_cnt, log->l_ticket_tcnt);
1557 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1560 tic = log->l_unmount_free;
1562 next_tic = tic->t_next;
1563 kmem_free(tic, NBPP);
1567 xfs_buf_free(log->l_xbuf);
1568 #ifdef XFS_LOG_TRACE
1569 if (log->l_trace != NULL) {
1570 ktrace_free(log->l_trace);
1572 if (log->l_grant_trace != NULL) {
1573 ktrace_free(log->l_grant_trace);
1576 log->l_mp->m_log = NULL;
1577 kmem_free(log, sizeof(xlog_t));
1578 } /* xlog_unalloc_log */
1581 * Update counters atomically now that memcpy is done.
1585 xlog_state_finish_copy(xlog_t *log,
1586 xlog_in_core_t *iclog,
1594 iclog->ic_header.h_num_logops += record_cnt;
1595 iclog->ic_offset += copy_bytes;
1598 } /* xlog_state_finish_copy */
1604 * Write some region out to in-core log
1606 * This will be called when writing externally provided regions or when
1607 * writing out a commit record for a given transaction.
1609 * General algorithm:
1610 * 1. Find total length of this write. This may include adding to the
1611 * lengths passed in.
1612 * 2. Check whether we violate the tickets reservation.
1613 * 3. While writing to this iclog
1614 * A. Reserve as much space in this iclog as can get
1615 * B. If this is first write, save away start lsn
1616 * C. While writing this region:
1617 * 1. If first write of transaction, write start record
1618 * 2. Write log operation header (header per region)
1619 * 3. Find out if we can fit entire region into this iclog
1620 * 4. Potentially, verify destination memcpy ptr
1621 * 5. Memcpy (partial) region
1622 * 6. If partial copy, release iclog; otherwise, continue
1623 * copying more regions into current iclog
1624 * 4. Mark want sync bit (in simulation mode)
1625 * 5. Release iclog for potential flush to on-disk log.
1628 * 1. Panic if reservation is overrun. This should never happen since
1629 * reservation amounts are generated internal to the filesystem.
1631 * 1. Tickets are single threaded data structures.
1632 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1633 * syncing routine. When a single log_write region needs to span
1634 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1635 * on all log operation writes which don't contain the end of the
1636 * region. The XLOG_END_TRANS bit is used for the in-core log
1637 * operation which contains the end of the continued log_write region.
1638 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1639 * we don't really know exactly how much space will be used. As a result,
1640 * we don't update ic_offset until the end when we know exactly how many
1641 * bytes have been written out.
1644 xlog_write(xfs_mount_t * mp,
1645 xfs_log_iovec_t reg[],
1647 xfs_log_ticket_t tic,
1648 xfs_lsn_t *start_lsn,
1649 xlog_in_core_t **commit_iclog,
1652 xlog_t *log = mp->m_log;
1653 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1654 xlog_op_header_t *logop_head; /* ptr to log operation header */
1655 xlog_in_core_t *iclog; /* ptr to current in-core log */
1656 __psint_t ptr; /* copy address into data region */
1657 int len; /* # xlog_write() bytes 2 still copy */
1658 int index; /* region index currently copying */
1659 int log_offset; /* offset (from 0) into data region */
1660 int start_rec_copy; /* # bytes to copy for start record */
1661 int partial_copy; /* did we split a region? */
1662 int partial_copy_len;/* # bytes copied if split region */
1663 int need_copy; /* # bytes need to memcpy this region */
1664 int copy_len; /* # bytes actually memcpy'ing */
1665 int copy_off; /* # bytes from entry start */
1666 int contwr; /* continued write of in-core log? */
1667 int firstwr = 0; /* first write of transaction */
1669 int record_cnt = 0, data_cnt = 0;
1671 partial_copy_len = partial_copy = 0;
1673 /* Calculate potential maximum space. Each region gets its own
1674 * xlog_op_header_t and may need to be double word aligned.
1677 if (ticket->t_flags & XLOG_TIC_INITED) /* acct for start rec of xact */
1678 len += sizeof(xlog_op_header_t);
1680 for (index = 0; index < nentries; index++) {
1681 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1682 len += reg[index].i_len;
1684 contwr = *start_lsn = 0;
1686 if (ticket->t_curr_res < len) {
1689 "xfs_log_write: reservation ran out. Need to up reservation");
1691 /* Customer configurable panic */
1692 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1693 "xfs_log_write: reservation ran out. Need to up reservation");
1694 /* If we did not panic, shutdown the filesystem */
1695 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1698 ticket->t_curr_res -= len;
1700 for (index = 0; index < nentries; ) {
1701 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1702 &contwr, &log_offset)))
1705 ASSERT(log_offset <= iclog->ic_size - 1);
1706 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1708 /* start_lsn is the first lsn written to. That's all we need. */
1710 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1712 /* This loop writes out as many regions as can fit in the amount
1713 * of space which was allocated by xlog_state_get_iclog_space().
1715 while (index < nentries) {
1716 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1717 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1720 /* If first write for transaction, insert start record.
1721 * We can't be trying to commit if we are inited. We can't
1722 * have any "partial_copy" if we are inited.
1724 if (ticket->t_flags & XLOG_TIC_INITED) {
1725 logop_head = (xlog_op_header_t *)ptr;
1726 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1727 logop_head->oh_clientid = ticket->t_clientid;
1728 INT_ZERO(logop_head->oh_len, ARCH_CONVERT);
1729 logop_head->oh_flags = XLOG_START_TRANS;
1730 INT_ZERO(logop_head->oh_res2, ARCH_CONVERT);
1731 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1732 firstwr = 1; /* increment log ops below */
1735 start_rec_copy = sizeof(xlog_op_header_t);
1736 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1739 /* Copy log operation header directly into data section */
1740 logop_head = (xlog_op_header_t *)ptr;
1741 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1742 logop_head->oh_clientid = ticket->t_clientid;
1743 INT_ZERO(logop_head->oh_res2, ARCH_CONVERT);
1745 /* header copied directly */
1746 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1748 /* are we copying a commit or unmount record? */
1749 logop_head->oh_flags = flags;
1752 * We've seen logs corrupted with bad transaction client
1753 * ids. This makes sure that XFS doesn't generate them on.
1754 * Turn this into an EIO and shut down the filesystem.
1756 switch (logop_head->oh_clientid) {
1757 case XFS_TRANSACTION:
1762 xfs_fs_cmn_err(CE_WARN, mp,
1763 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1764 logop_head->oh_clientid, tic);
1765 return XFS_ERROR(EIO);
1768 /* Partial write last time? => (partial_copy != 0)
1769 * need_copy is the amount we'd like to copy if everything could
1770 * fit in the current memcpy.
1772 need_copy = reg[index].i_len - partial_copy_len;
1774 copy_off = partial_copy_len;
1775 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1776 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1778 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1779 partial_copy_len = partial_copy = 0;
1780 } else { /* partial write */
1781 copy_len = iclog->ic_size - log_offset;
1782 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1783 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1785 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1786 partial_copy_len += copy_len;
1788 len += sizeof(xlog_op_header_t); /* from splitting of region */
1789 /* account for new log op header */
1790 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1792 xlog_verify_dest_ptr(log, ptr);
1795 ASSERT(copy_len >= 0);
1796 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1797 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1799 /* make copy_len total bytes copied, including headers */
1800 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1802 data_cnt += contwr ? copy_len : 0;
1804 if (partial_copy) { /* copied partial region */
1805 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1806 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1807 record_cnt = data_cnt = 0;
1808 if ((error = xlog_state_release_iclog(log, iclog)))
1810 break; /* don't increment index */
1811 } else { /* copied entire region */
1813 partial_copy_len = partial_copy = 0;
1815 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1816 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1817 record_cnt = data_cnt = 0;
1818 xlog_state_want_sync(log, iclog);
1820 ASSERT(flags & XLOG_COMMIT_TRANS);
1821 *commit_iclog = iclog;
1822 } else if ((error = xlog_state_release_iclog(log, iclog)))
1824 if (index == nentries)
1825 return 0; /* we are done */
1829 } /* if (partial_copy) */
1830 } /* while (index < nentries) */
1831 } /* for (index = 0; index < nentries; ) */
1834 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1836 ASSERT(flags & XLOG_COMMIT_TRANS);
1837 *commit_iclog = iclog;
1840 return (xlog_state_release_iclog(log, iclog));
1844 /*****************************************************************************
1846 * State Machine functions
1848 *****************************************************************************
1851 /* Clean iclogs starting from the head. This ordering must be
1852 * maintained, so an iclog doesn't become ACTIVE beyond one that
1853 * is SYNCING. This is also required to maintain the notion that we use
1854 * a counting semaphore to hold off would be writers to the log when every
1855 * iclog is trying to sync to disk.
1857 * State Change: DIRTY -> ACTIVE
1860 xlog_state_clean_log(xlog_t *log)
1862 xlog_in_core_t *iclog;
1865 iclog = log->l_iclog;
1867 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1868 iclog->ic_state = XLOG_STATE_ACTIVE;
1869 iclog->ic_offset = 0;
1870 iclog->ic_callback = 0; /* don't need to free */
1872 * If the number of ops in this iclog indicate it just
1873 * contains the dummy transaction, we can
1874 * change state into IDLE (the second time around).
1875 * Otherwise we should change the state into
1877 * We don't need to cover the dummy.
1880 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1884 * We have two dirty iclogs so start over
1885 * This could also be num of ops indicates
1886 * this is not the dummy going out.
1890 INT_ZERO(iclog->ic_header.h_num_logops, ARCH_CONVERT);
1891 memset(iclog->ic_header.h_cycle_data, 0,
1892 sizeof(iclog->ic_header.h_cycle_data));
1893 INT_ZERO(iclog->ic_header.h_lsn, ARCH_CONVERT);
1894 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1897 break; /* stop cleaning */
1898 iclog = iclog->ic_next;
1899 } while (iclog != log->l_iclog);
1901 /* log is locked when we are called */
1903 * Change state for the dummy log recording.
1904 * We usually go to NEED. But we go to NEED2 if the changed indicates
1905 * we are done writing the dummy record.
1906 * If we are done with the second dummy recored (DONE2), then
1910 switch (log->l_covered_state) {
1911 case XLOG_STATE_COVER_IDLE:
1912 case XLOG_STATE_COVER_NEED:
1913 case XLOG_STATE_COVER_NEED2:
1914 log->l_covered_state = XLOG_STATE_COVER_NEED;
1917 case XLOG_STATE_COVER_DONE:
1919 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1921 log->l_covered_state = XLOG_STATE_COVER_NEED;
1924 case XLOG_STATE_COVER_DONE2:
1926 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1928 log->l_covered_state = XLOG_STATE_COVER_NEED;
1935 } /* xlog_state_clean_log */
1938 xlog_get_lowest_lsn(
1941 xlog_in_core_t *lsn_log;
1942 xfs_lsn_t lowest_lsn, lsn;
1944 lsn_log = log->l_iclog;
1947 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
1948 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
1949 if ((lsn && !lowest_lsn) ||
1950 (XFS_LSN_CMP_ARCH(lsn, lowest_lsn, ARCH_NOCONVERT) < 0)) {
1954 lsn_log = lsn_log->ic_next;
1955 } while (lsn_log != log->l_iclog);
1961 xlog_state_do_callback(
1964 xlog_in_core_t *ciclog)
1966 xlog_in_core_t *iclog;
1967 xlog_in_core_t *first_iclog; /* used to know when we've
1968 * processed all iclogs once */
1969 xfs_log_callback_t *cb, *cb_next;
1971 xfs_lsn_t lowest_lsn;
1972 int ioerrors; /* counter: iclogs with errors */
1973 int loopdidcallbacks; /* flag: inner loop did callbacks*/
1974 int funcdidcallbacks; /* flag: function did callbacks */
1975 int repeats; /* for issuing console warnings if
1976 * looping too many times */
1980 first_iclog = iclog = log->l_iclog;
1982 funcdidcallbacks = 0;
1987 * Scan all iclogs starting with the one pointed to by the
1988 * log. Reset this starting point each time the log is
1989 * unlocked (during callbacks).
1991 * Keep looping through iclogs until one full pass is made
1992 * without running any callbacks.
1994 first_iclog = log->l_iclog;
1995 iclog = log->l_iclog;
1996 loopdidcallbacks = 0;
2001 /* skip all iclogs in the ACTIVE & DIRTY states */
2002 if (iclog->ic_state &
2003 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2004 iclog = iclog->ic_next;
2009 * Between marking a filesystem SHUTDOWN and stopping
2010 * the log, we do flush all iclogs to disk (if there
2011 * wasn't a log I/O error). So, we do want things to
2012 * go smoothly in case of just a SHUTDOWN w/o a
2015 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2017 * Can only perform callbacks in order. Since
2018 * this iclog is not in the DONE_SYNC/
2019 * DO_CALLBACK state, we skip the rest and
2020 * just try to clean up. If we set our iclog
2021 * to DO_CALLBACK, we will not process it when
2022 * we retry since a previous iclog is in the
2023 * CALLBACK and the state cannot change since
2024 * we are holding the LOG_LOCK.
2026 if (!(iclog->ic_state &
2027 (XLOG_STATE_DONE_SYNC |
2028 XLOG_STATE_DO_CALLBACK))) {
2029 if (ciclog && (ciclog->ic_state ==
2030 XLOG_STATE_DONE_SYNC)) {
2031 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2036 * We now have an iclog that is in either the
2037 * DO_CALLBACK or DONE_SYNC states. The other
2038 * states (WANT_SYNC, SYNCING, or CALLBACK were
2039 * caught by the above if and are going to
2040 * clean (i.e. we aren't doing their callbacks)
2045 * We will do one more check here to see if we
2046 * have chased our tail around.
2049 lowest_lsn = xlog_get_lowest_lsn(log);
2053 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT),
2056 iclog = iclog->ic_next;
2057 continue; /* Leave this iclog for
2061 iclog->ic_state = XLOG_STATE_CALLBACK;
2065 /* l_last_sync_lsn field protected by
2066 * GRANT_LOCK. Don't worry about iclog's lsn.
2067 * No one else can be here except us.
2069 s = GRANT_LOCK(log);
2070 ASSERT(XFS_LSN_CMP_ARCH(
2071 log->l_last_sync_lsn,
2072 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT),
2075 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2076 GRANT_UNLOCK(log, s);
2079 * Keep processing entries in the callback list
2080 * until we come around and it is empty. We
2081 * need to atomically see that the list is
2082 * empty and change the state to DIRTY so that
2083 * we don't miss any more callbacks being added.
2089 cb = iclog->ic_callback;
2092 iclog->ic_callback_tail = &(iclog->ic_callback);
2093 iclog->ic_callback = 0;
2096 /* perform callbacks in the order given */
2097 for (; cb != 0; cb = cb_next) {
2098 cb_next = cb->cb_next;
2099 cb->cb_func(cb->cb_arg, aborted);
2102 cb = iclog->ic_callback;
2108 ASSERT(iclog->ic_callback == 0);
2109 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2110 iclog->ic_state = XLOG_STATE_DIRTY;
2113 * Transition from DIRTY to ACTIVE if applicable.
2114 * NOP if STATE_IOERROR.
2116 xlog_state_clean_log(log);
2118 /* wake up threads waiting in xfs_log_force() */
2119 sv_broadcast(&iclog->ic_forcesema);
2121 iclog = iclog->ic_next;
2122 } while (first_iclog != iclog);
2123 if (repeats && (repeats % 10) == 0) {
2124 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2125 "xlog_state_do_callback: looping %d", repeats);
2127 } while (!ioerrors && loopdidcallbacks);
2130 * make one last gasp attempt to see if iclogs are being left in
2134 if (funcdidcallbacks) {
2135 first_iclog = iclog = log->l_iclog;
2137 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2139 * Terminate the loop if iclogs are found in states
2140 * which will cause other threads to clean up iclogs.
2142 * SYNCING - i/o completion will go through logs
2143 * DONE_SYNC - interrupt thread should be waiting for
2145 * IOERROR - give up hope all ye who enter here
2147 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2148 iclog->ic_state == XLOG_STATE_SYNCING ||
2149 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2150 iclog->ic_state == XLOG_STATE_IOERROR )
2152 iclog = iclog->ic_next;
2153 } while (first_iclog != iclog);
2157 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2158 flushcnt = log->l_flushcnt;
2159 log->l_flushcnt = 0;
2163 vsema(&log->l_flushsema);
2164 } /* xlog_state_do_callback */
2168 * Finish transitioning this iclog to the dirty state.
2170 * Make sure that we completely execute this routine only when this is
2171 * the last call to the iclog. There is a good chance that iclog flushes,
2172 * when we reach the end of the physical log, get turned into 2 separate
2173 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2174 * routine. By using the reference count bwritecnt, we guarantee that only
2175 * the second completion goes through.
2177 * Callbacks could take time, so they are done outside the scope of the
2178 * global state machine log lock. Assume that the calls to cvsema won't
2179 * take a long time. At least we know it won't sleep.
2182 xlog_state_done_syncing(
2183 xlog_in_core_t *iclog,
2186 xlog_t *log = iclog->ic_log;
2191 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2192 iclog->ic_state == XLOG_STATE_IOERROR);
2193 ASSERT(iclog->ic_refcnt == 0);
2194 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2198 * If we got an error, either on the first buffer, or in the case of
2199 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2200 * and none should ever be attempted to be written to disk
2203 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2204 if (--iclog->ic_bwritecnt == 1) {
2208 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2212 * Someone could be sleeping prior to writing out the next
2213 * iclog buffer, we wake them all, one will get to do the
2214 * I/O, the others get to wait for the result.
2216 sv_broadcast(&iclog->ic_writesema);
2218 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2219 } /* xlog_state_done_syncing */
2223 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2224 * sleep. The flush semaphore is set to the number of in-core buffers and
2225 * decremented around disk syncing. Therefore, if all buffers are syncing,
2226 * this semaphore will cause new writes to sleep until a sync completes.
2227 * Otherwise, this code just does p() followed by v(). This approximates
2228 * a sleep/wakeup except we can't race.
2230 * The in-core logs are used in a circular fashion. They are not used
2231 * out-of-order even when an iclog past the head is free.
2234 * * log_offset where xlog_write() can start writing into the in-core
2236 * * in-core log pointer to which xlog_write() should write.
2237 * * boolean indicating this is a continued write to an in-core log.
2238 * If this is the last write, then the in-core log's offset field
2239 * needs to be incremented, depending on the amount of data which
2243 xlog_state_get_iclog_space(xlog_t *log,
2245 xlog_in_core_t **iclogp,
2246 xlog_ticket_t *ticket,
2247 int *continued_write,
2252 xlog_rec_header_t *head;
2253 xlog_in_core_t *iclog;
2258 if (XLOG_FORCED_SHUTDOWN(log)) {
2260 return XFS_ERROR(EIO);
2263 iclog = log->l_iclog;
2264 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2267 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2268 XFS_STATS_INC(xs_log_noiclogs);
2269 /* Ensure that log writes happen */
2270 psema(&log->l_flushsema, PINOD);
2273 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2274 head = &iclog->ic_header;
2276 iclog->ic_refcnt++; /* prevents sync */
2277 log_offset = iclog->ic_offset;
2279 /* On the 1st write to an iclog, figure out lsn. This works
2280 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2281 * committing to. If the offset is set, that's how many blocks
2284 if (log_offset == 0) {
2285 ticket->t_curr_res -= log->l_iclog_hsize;
2286 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2287 ASSIGN_LSN(head->h_lsn, log, ARCH_CONVERT);
2288 ASSERT(log->l_curr_block >= 0);
2290 /* round off error from last write with this iclog */
2291 ticket->t_curr_res -= iclog->ic_roundoff;
2292 log->l_roundoff -= iclog->ic_roundoff;
2293 iclog->ic_roundoff = 0;
2296 /* If there is enough room to write everything, then do it. Otherwise,
2297 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2298 * bit is on, so this will get flushed out. Don't update ic_offset
2299 * until you know exactly how many bytes get copied. Therefore, wait
2300 * until later to update ic_offset.
2302 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2303 * can fit into remaining data section.
2305 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2306 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2308 /* If I'm the only one writing to this iclog, sync it to disk */
2309 if (iclog->ic_refcnt == 1) {
2311 if ((error = xlog_state_release_iclog(log, iclog)))
2320 /* Do we have enough room to write the full amount in the remainder
2321 * of this iclog? Or must we continue a write on the next iclog and
2322 * mark this iclog as completely taken? In the case where we switch
2323 * iclogs (to mark it taken), this particular iclog will release/sync
2324 * to disk in xlog_write().
2326 if (len <= iclog->ic_size - iclog->ic_offset) {
2327 *continued_write = 0;
2328 iclog->ic_offset += len;
2330 *continued_write = 1;
2331 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2335 ASSERT(iclog->ic_offset <= iclog->ic_size);
2338 *logoffsetp = log_offset;
2340 } /* xlog_state_get_iclog_space */
2343 * Atomically get the log space required for a log ticket.
2345 * Once a ticket gets put onto the reserveq, it will only return after
2346 * the needed reservation is satisfied.
2349 xlog_grant_log_space(xlog_t *log,
2361 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2362 panic("grant Recovery problem");
2365 /* Is there space or do we need to sleep? */
2366 s = GRANT_LOCK(log);
2367 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2369 /* something is already sleeping; insert new transaction at end */
2370 if (log->l_reserve_headq) {
2371 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2372 xlog_trace_loggrant(log, tic,
2373 "xlog_grant_log_space: sleep 1");
2375 * Gotta check this before going to sleep, while we're
2376 * holding the grant lock.
2378 if (XLOG_FORCED_SHUTDOWN(log))
2381 XFS_STATS_INC(xs_sleep_logspace);
2382 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2384 * If we got an error, and the filesystem is shutting down,
2385 * we'll catch it down below. So just continue...
2387 xlog_trace_loggrant(log, tic,
2388 "xlog_grant_log_space: wake 1");
2389 s = GRANT_LOCK(log);
2391 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2392 need_bytes = tic->t_unit_res*tic->t_ocnt;
2394 need_bytes = tic->t_unit_res;
2397 if (XLOG_FORCED_SHUTDOWN(log))
2400 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2401 log->l_grant_reserve_bytes);
2402 if (free_bytes < need_bytes) {
2403 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2404 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2405 xlog_trace_loggrant(log, tic,
2406 "xlog_grant_log_space: sleep 2");
2407 XFS_STATS_INC(xs_sleep_logspace);
2408 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2410 if (XLOG_FORCED_SHUTDOWN(log)) {
2411 s = GRANT_LOCK(log);
2415 xlog_trace_loggrant(log, tic,
2416 "xlog_grant_log_space: wake 2");
2417 xlog_grant_push_ail(log->l_mp, need_bytes);
2418 s = GRANT_LOCK(log);
2420 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2421 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2423 /* we've got enough space */
2424 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w');
2425 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'r');
2427 tail_lsn = log->l_tail_lsn;
2429 * Check to make sure the grant write head didn't just over lap the
2430 * tail. If the cycles are the same, we can't be overlapping.
2431 * Otherwise, make sure that the cycles differ by exactly one and
2432 * check the byte count.
2434 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) != log->l_grant_write_cycle) {
2435 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn, ARCH_NOCONVERT));
2436 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn, ARCH_NOCONVERT)));
2439 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2440 xlog_verify_grant_head(log, 1);
2441 GRANT_UNLOCK(log, s);
2445 if (tic->t_flags & XLOG_TIC_IN_Q)
2446 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2447 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2449 * If we are failing, make sure the ticket doesn't have any
2450 * current reservations. We don't want to add this back when
2451 * the ticket/transaction gets cancelled.
2453 tic->t_curr_res = 0;
2454 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2455 GRANT_UNLOCK(log, s);
2456 return XFS_ERROR(EIO);
2457 } /* xlog_grant_log_space */
2461 * Replenish the byte reservation required by moving the grant write head.
2466 xlog_regrant_write_log_space(xlog_t *log,
2470 int free_bytes, need_bytes;
2471 xlog_ticket_t *ntic;
2476 tic->t_curr_res = tic->t_unit_res;
2482 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2483 panic("regrant Recovery problem");
2486 s = GRANT_LOCK(log);
2487 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2489 if (XLOG_FORCED_SHUTDOWN(log))
2492 /* If there are other waiters on the queue then give them a
2493 * chance at logspace before us. Wake up the first waiters,
2494 * if we do not wake up all the waiters then go to sleep waiting
2495 * for more free space, otherwise try to get some space for
2499 if ((ntic = log->l_write_headq)) {
2500 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2501 log->l_grant_write_bytes);
2503 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2505 if (free_bytes < ntic->t_unit_res)
2507 free_bytes -= ntic->t_unit_res;
2508 sv_signal(&ntic->t_sema);
2509 ntic = ntic->t_next;
2510 } while (ntic != log->l_write_headq);
2512 if (ntic != log->l_write_headq) {
2513 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2514 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2516 xlog_trace_loggrant(log, tic,
2517 "xlog_regrant_write_log_space: sleep 1");
2518 XFS_STATS_INC(xs_sleep_logspace);
2519 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2520 &log->l_grant_lock, s);
2522 /* If we're shutting down, this tic is already
2524 if (XLOG_FORCED_SHUTDOWN(log)) {
2525 s = GRANT_LOCK(log);
2529 xlog_trace_loggrant(log, tic,
2530 "xlog_regrant_write_log_space: wake 1");
2531 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2532 s = GRANT_LOCK(log);
2536 need_bytes = tic->t_unit_res;
2539 if (XLOG_FORCED_SHUTDOWN(log))
2542 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2543 log->l_grant_write_bytes);
2544 if (free_bytes < need_bytes) {
2545 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2546 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2547 XFS_STATS_INC(xs_sleep_logspace);
2548 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2550 /* If we're shutting down, this tic is already off the queue */
2551 if (XLOG_FORCED_SHUTDOWN(log)) {
2552 s = GRANT_LOCK(log);
2556 xlog_trace_loggrant(log, tic,
2557 "xlog_regrant_write_log_space: wake 2");
2558 xlog_grant_push_ail(log->l_mp, need_bytes);
2559 s = GRANT_LOCK(log);
2561 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2562 XLOG_DEL_TICKETQ(log->l_write_headq, tic);
2564 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w'); /* we've got enough space */
2566 tail_lsn = log->l_tail_lsn;
2567 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) != log->l_grant_write_cycle) {
2568 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn, ARCH_NOCONVERT));
2569 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn, ARCH_NOCONVERT)));
2573 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2574 xlog_verify_grant_head(log, 1);
2575 GRANT_UNLOCK(log, s);
2580 if (tic->t_flags & XLOG_TIC_IN_Q)
2581 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2582 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2584 * If we are failing, make sure the ticket doesn't have any
2585 * current reservations. We don't want to add this back when
2586 * the ticket/transaction gets cancelled.
2588 tic->t_curr_res = 0;
2589 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2590 GRANT_UNLOCK(log, s);
2591 return XFS_ERROR(EIO);
2592 } /* xlog_regrant_write_log_space */
2595 /* The first cnt-1 times through here we don't need to
2596 * move the grant write head because the permanent
2597 * reservation has reserved cnt times the unit amount.
2598 * Release part of current permanent unit reservation and
2599 * reset current reservation to be one units worth. Also
2600 * move grant reservation head forward.
2603 xlog_regrant_reserve_log_space(xlog_t *log,
2604 xlog_ticket_t *ticket)
2608 xlog_trace_loggrant(log, ticket,
2609 "xlog_regrant_reserve_log_space: enter");
2610 if (ticket->t_cnt > 0)
2613 s = GRANT_LOCK(log);
2614 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2615 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2616 ticket->t_curr_res = ticket->t_unit_res;
2617 xlog_trace_loggrant(log, ticket,
2618 "xlog_regrant_reserve_log_space: sub current res");
2619 xlog_verify_grant_head(log, 1);
2621 /* just return if we still have some of the pre-reserved space */
2622 if (ticket->t_cnt > 0) {
2623 GRANT_UNLOCK(log, s);
2627 XLOG_GRANT_ADD_SPACE(log, ticket->t_unit_res, 'r');
2628 xlog_trace_loggrant(log, ticket,
2629 "xlog_regrant_reserve_log_space: exit");
2630 xlog_verify_grant_head(log, 0);
2631 GRANT_UNLOCK(log, s);
2632 ticket->t_curr_res = ticket->t_unit_res;
2633 } /* xlog_regrant_reserve_log_space */
2637 * Give back the space left from a reservation.
2639 * All the information we need to make a correct determination of space left
2640 * is present. For non-permanent reservations, things are quite easy. The
2641 * count should have been decremented to zero. We only need to deal with the
2642 * space remaining in the current reservation part of the ticket. If the
2643 * ticket contains a permanent reservation, there may be left over space which
2644 * needs to be released. A count of N means that N-1 refills of the current
2645 * reservation can be done before we need to ask for more space. The first
2646 * one goes to fill up the first current reservation. Once we run out of
2647 * space, the count will stay at zero and the only space remaining will be
2648 * in the current reservation field.
2651 xlog_ungrant_log_space(xlog_t *log,
2652 xlog_ticket_t *ticket)
2656 if (ticket->t_cnt > 0)
2659 s = GRANT_LOCK(log);
2660 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2662 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2663 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2665 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2667 /* If this is a permanent reservation ticket, we may be able to free
2668 * up more space based on the remaining count.
2670 if (ticket->t_cnt > 0) {
2671 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2672 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'w');
2673 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'r');
2676 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2677 xlog_verify_grant_head(log, 1);
2678 GRANT_UNLOCK(log, s);
2679 xfs_log_move_tail(log->l_mp, 1);
2680 } /* xlog_ungrant_log_space */
2684 * Atomically put back used ticket.
2687 xlog_state_put_ticket(xlog_t *log,
2693 xlog_ticket_put(log, tic);
2695 } /* xlog_state_put_ticket */
2698 * Flush iclog to disk if this is the last reference to the given iclog and
2699 * the WANT_SYNC bit is set.
2701 * When this function is entered, the iclog is not necessarily in the
2702 * WANT_SYNC state. It may be sitting around waiting to get filled.
2707 xlog_state_release_iclog(xlog_t *log,
2708 xlog_in_core_t *iclog)
2711 int sync = 0; /* do we sync? */
2713 xlog_assign_tail_lsn(log->l_mp);
2717 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2719 return XFS_ERROR(EIO);
2722 ASSERT(iclog->ic_refcnt > 0);
2723 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2724 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2726 if (--iclog->ic_refcnt == 0 &&
2727 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2729 iclog->ic_state = XLOG_STATE_SYNCING;
2730 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2731 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2732 /* cycle incremented when incrementing curr_block */
2738 * We let the log lock go, so it's possible that we hit a log I/O
2739 * error or someother SHUTDOWN condition that marks the iclog
2740 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2741 * this iclog has consistent data, so we ignore IOERROR
2742 * flags after this point.
2745 return xlog_sync(log, iclog);
2749 } /* xlog_state_release_iclog */
2753 * This routine will mark the current iclog in the ring as WANT_SYNC
2754 * and move the current iclog pointer to the next iclog in the ring.
2755 * When this routine is called from xlog_state_get_iclog_space(), the
2756 * exact size of the iclog has not yet been determined. All we know is
2757 * that every data block. We have run out of space in this log record.
2760 xlog_state_switch_iclogs(xlog_t *log,
2761 xlog_in_core_t *iclog,
2764 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2766 eventual_size = iclog->ic_offset;
2767 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2768 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2769 log->l_prev_block = log->l_curr_block;
2770 log->l_prev_cycle = log->l_curr_cycle;
2772 /* roll log?: ic_offset changed later */
2773 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2775 /* Round up to next log-sunit */
2776 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2777 log->l_mp->m_sb.sb_logsunit > 1) {
2778 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2779 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2782 if (log->l_curr_block >= log->l_logBBsize) {
2783 log->l_curr_cycle++;
2784 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2785 log->l_curr_cycle++;
2786 log->l_curr_block -= log->l_logBBsize;
2787 ASSERT(log->l_curr_block >= 0);
2789 ASSERT(iclog == log->l_iclog);
2790 log->l_iclog = iclog->ic_next;
2791 } /* xlog_state_switch_iclogs */
2795 * Write out all data in the in-core log as of this exact moment in time.
2797 * Data may be written to the in-core log during this call. However,
2798 * we don't guarantee this data will be written out. A change from past
2799 * implementation means this routine will *not* write out zero length LRs.
2801 * Basically, we try and perform an intelligent scan of the in-core logs.
2802 * If we determine there is no flushable data, we just return. There is no
2803 * flushable data if:
2805 * 1. the current iclog is active and has no data; the previous iclog
2806 * is in the active or dirty state.
2807 * 2. the current iclog is drity, and the previous iclog is in the
2808 * active or dirty state.
2810 * We may sleep (call psema) if:
2812 * 1. the current iclog is not in the active nor dirty state.
2813 * 2. the current iclog dirty, and the previous iclog is not in the
2814 * active nor dirty state.
2815 * 3. the current iclog is active, and there is another thread writing
2816 * to this particular iclog.
2817 * 4. a) the current iclog is active and has no other writers
2818 * b) when we return from flushing out this iclog, it is still
2819 * not in the active nor dirty state.
2822 xlog_state_sync_all(xlog_t *log, uint flags)
2824 xlog_in_core_t *iclog;
2830 iclog = log->l_iclog;
2831 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2833 return XFS_ERROR(EIO);
2836 /* If the head iclog is not active nor dirty, we just attach
2837 * ourselves to the head and go to sleep.
2839 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2840 iclog->ic_state == XLOG_STATE_DIRTY) {
2842 * If the head is dirty or (active and empty), then
2843 * we need to look at the previous iclog. If the previous
2844 * iclog is active or dirty we are done. There is nothing
2845 * to sync out. Otherwise, we attach ourselves to the
2846 * previous iclog and go to sleep.
2848 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2849 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2850 iclog = iclog->ic_prev;
2851 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2852 iclog->ic_state == XLOG_STATE_DIRTY)
2857 if (iclog->ic_refcnt == 0) {
2858 /* We are the only one with access to this
2859 * iclog. Flush it out now. There should
2860 * be a roundoff of zero to show that someone
2861 * has already taken care of the roundoff from
2862 * the previous sync.
2864 ASSERT(iclog->ic_roundoff == 0);
2866 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2867 xlog_state_switch_iclogs(log, iclog, 0);
2870 if (xlog_state_release_iclog(log, iclog))
2871 return XFS_ERROR(EIO);
2873 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2874 iclog->ic_state != XLOG_STATE_DIRTY)
2879 /* Someone else is writing to this iclog.
2880 * Use its call to flush out the data. However,
2881 * the other thread may not force out this LR,
2882 * so we mark it WANT_SYNC.
2884 xlog_state_switch_iclogs(log, iclog, 0);
2890 /* By the time we come around again, the iclog could've been filled
2891 * which would give it another lsn. If we have a new lsn, just
2892 * return because the relevant data has been flushed.
2895 if (flags & XFS_LOG_SYNC) {
2897 * We must check if we're shutting down here, before
2898 * we wait, while we're holding the LOG_LOCK.
2899 * Then we check again after waking up, in case our
2900 * sleep was disturbed by a bad news.
2902 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2904 return XFS_ERROR(EIO);
2906 XFS_STATS_INC(xs_log_force_sleep);
2907 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
2909 * No need to grab the log lock here since we're
2910 * only deciding whether or not to return EIO
2911 * and the memory read should be atomic.
2913 if (iclog->ic_state & XLOG_STATE_IOERROR)
2914 return XFS_ERROR(EIO);
2922 } /* xlog_state_sync_all */
2926 * Used by code which implements synchronous log forces.
2928 * Find in-core log with lsn.
2929 * If it is in the DIRTY state, just return.
2930 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2931 * state and go to sleep or return.
2932 * If it is in any other state, go to sleep or return.
2934 * If filesystem activity goes to zero, the iclog will get flushed only by
2938 xlog_state_sync(xlog_t *log,
2942 xlog_in_core_t *iclog;
2943 int already_slept = 0;
2949 iclog = log->l_iclog;
2951 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2953 return XFS_ERROR(EIO);
2957 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
2958 iclog = iclog->ic_next;
2962 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2967 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
2969 * We sleep here if we haven't already slept (e.g.
2970 * this is the first time we've looked at the correct
2971 * iclog buf) and the buffer before us is going to
2972 * be sync'ed. The reason for this is that if we
2973 * are doing sync transactions here, by waiting for
2974 * the previous I/O to complete, we can allow a few
2975 * more transactions into this iclog before we close
2978 * Otherwise, we mark the buffer WANT_SYNC, and bump
2979 * up the refcnt so we can release the log (which drops
2980 * the ref count). The state switch keeps new transaction
2981 * commits from using this buffer. When the current commits
2982 * finish writing into the buffer, the refcount will drop to
2983 * zero and the buffer will go out then.
2985 if (!already_slept &&
2986 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
2987 XLOG_STATE_SYNCING))) {
2988 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
2989 XFS_STATS_INC(xs_log_force_sleep);
2990 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
2991 &log->l_icloglock, s);
2996 xlog_state_switch_iclogs(log, iclog, 0);
2998 if (xlog_state_release_iclog(log, iclog))
2999 return XFS_ERROR(EIO);
3004 if ((flags & XFS_LOG_SYNC) && /* sleep */
3005 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3008 * Don't wait on the forcesema if we know that we've
3009 * gotten a log write error.
3011 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3013 return XFS_ERROR(EIO);
3015 XFS_STATS_INC(xs_log_force_sleep);
3016 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3018 * No need to grab the log lock here since we're
3019 * only deciding whether or not to return EIO
3020 * and the memory read should be atomic.
3022 if (iclog->ic_state & XLOG_STATE_IOERROR)
3023 return XFS_ERROR(EIO);
3024 } else { /* just return */
3029 } while (iclog != log->l_iclog);
3033 } /* xlog_state_sync */
3037 * Called when we want to mark the current iclog as being ready to sync to
3041 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3047 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3048 xlog_state_switch_iclogs(log, iclog, 0);
3050 ASSERT(iclog->ic_state &
3051 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3055 } /* xlog_state_want_sync */
3059 /*****************************************************************************
3063 *****************************************************************************
3067 * Algorithm doesn't take into account page size. ;-(
3070 xlog_state_ticket_alloc(xlog_t *log)
3072 xlog_ticket_t *t_list;
3073 xlog_ticket_t *next;
3075 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3079 * The kmem_zalloc may sleep, so we shouldn't be holding the
3080 * global lock. XXXmiken: may want to use zone allocator.
3082 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3086 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3087 t_list = (xlog_ticket_t *)buf;
3088 t_list->t_next = log->l_unmount_free;
3089 log->l_unmount_free = t_list++;
3090 log->l_ticket_cnt++;
3091 log->l_ticket_tcnt++;
3093 /* Next ticket becomes first ticket attached to ticket free list */
3094 if (log->l_freelist != NULL) {
3095 ASSERT(log->l_tail != NULL);
3096 log->l_tail->t_next = t_list;
3098 log->l_freelist = t_list;
3100 log->l_ticket_cnt++;
3101 log->l_ticket_tcnt++;
3103 /* Cycle through rest of alloc'ed memory, building up free Q */
3104 for ( ; i > 0; i--) {
3106 t_list->t_next = next;
3108 log->l_ticket_cnt++;
3109 log->l_ticket_tcnt++;
3112 log->l_tail = t_list;
3114 } /* xlog_state_ticket_alloc */
3118 * Put ticket into free list
3120 * Assumption: log lock is held around this call.
3123 xlog_ticket_put(xlog_t *log,
3124 xlog_ticket_t *ticket)
3126 sv_destroy(&ticket->t_sema);
3129 * Don't think caching will make that much difference. It's
3130 * more important to make debug easier.
3133 /* real code will want to use LIFO for caching */
3134 ticket->t_next = log->l_freelist;
3135 log->l_freelist = ticket;
3136 /* no need to clear fields */
3138 /* When we debug, it is easier if tickets are cycled */
3140 if (log->l_tail != 0) {
3141 log->l_tail->t_next = ticket;
3143 ASSERT(log->l_freelist == 0);
3144 log->l_freelist = ticket;
3146 log->l_tail = ticket;
3148 log->l_ticket_cnt++;
3149 } /* xlog_ticket_put */
3153 * Grab ticket off freelist or allocation some more
3156 xlog_ticket_get(xlog_t *log,
3167 if (log->l_freelist == NULL)
3168 xlog_state_ticket_alloc(log); /* potentially sleep */
3171 if (log->l_freelist == NULL) {
3175 tic = log->l_freelist;
3176 log->l_freelist = tic->t_next;
3177 if (log->l_freelist == NULL)
3179 log->l_ticket_cnt--;
3183 * Permanent reservations have up to 'cnt'-1 active log operations
3184 * in the log. A unit in this case is the amount of space for one
3185 * of these log operations. Normal reservations have a cnt of 1
3186 * and their unit amount is the total amount of space required.
3188 * The following lines of code account for non-transaction data
3189 * which occupy space in the on-disk log.
3193 unit_bytes += sizeof(xlog_op_header_t);
3196 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3197 log->l_mp->m_sb.sb_logsunit > 1) {
3198 /* log su roundoff */
3199 unit_bytes += log->l_mp->m_sb.sb_logsunit;
3202 unit_bytes += BBSIZE;
3205 /* for commit-rec */
3206 unit_bytes += sizeof(xlog_op_header_t);
3208 /* for LR headers */
3209 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3210 unit_bytes += log->l_iclog_hsize * num_headers;
3212 tic->t_unit_res = unit_bytes;
3213 tic->t_curr_res = unit_bytes;
3216 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3217 tic->t_clientid = client;
3218 tic->t_flags = XLOG_TIC_INITED;
3219 if (xflags & XFS_LOG_PERM_RESERV)
3220 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3221 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3224 } /* xlog_ticket_get */
3227 /******************************************************************************
3229 * Log debug routines
3231 ******************************************************************************
3233 #if defined(DEBUG) && !defined(XLOG_NOLOG)
3235 * Make sure that the destination ptr is within the valid data region of
3236 * one of the iclogs. This uses backup pointers stored in a different
3237 * part of the log in case we trash the log structure.
3240 xlog_verify_dest_ptr(xlog_t *log,
3246 for (i=0; i < log->l_iclog_bufs; i++) {
3247 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3248 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3252 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3253 } /* xlog_verify_dest_ptr */
3257 /* check split LR write */
3259 xlog_verify_disk_cycle_no(xlog_t *log,
3260 xlog_in_core_t *iclog)
3267 if (BLOCK_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT) < 10) {
3268 cycle_no = CYCLE_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT);
3269 bp = xlog_get_bp(log, 1);
3271 for (i = 0; i < BLOCK_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT); i++) {
3272 xlog_bread(log, i, 1, bp);
3273 ptr = xlog_align(log, i, 1, bp);
3274 if (GET_CYCLE(ptr, ARCH_CONVERT) != cycle_no)
3275 xlog_warn("XFS: xlog_verify_disk_cycle_no: bad cycle no");
3279 } /* xlog_verify_disk_cycle_no */
3283 xlog_verify_grant_head(xlog_t *log, int equals)
3285 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3287 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3289 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3291 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3292 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3294 } /* xlog_verify_grant_head */
3296 /* check if it will fit */
3298 xlog_verify_tail_lsn(xlog_t *log,
3299 xlog_in_core_t *iclog,
3304 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) == log->l_prev_cycle) {
3306 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn, ARCH_NOCONVERT));
3307 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3308 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3310 ASSERT(CYCLE_LSN(tail_lsn, ARCH_NOCONVERT)+1 == log->l_prev_cycle);
3312 if (BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) == log->l_prev_block)
3313 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3315 blocks = BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) - log->l_prev_block;
3316 if (blocks < BTOBB(iclog->ic_offset) + 1)
3317 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3319 } /* xlog_verify_tail_lsn */
3322 * Perform a number of checks on the iclog before writing to disk.
3324 * 1. Make sure the iclogs are still circular
3325 * 2. Make sure we have a good magic number
3326 * 3. Make sure we don't have magic numbers in the data
3327 * 4. Check fields of each log operation header for:
3328 * A. Valid client identifier
3329 * B. tid ptr value falls in valid ptr space (user space code)
3330 * C. Length in log record header is correct according to the
3331 * individual operation headers within record.
3332 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3333 * log, check the preceding blocks of the physical log to make sure all
3334 * the cycle numbers agree with the current cycle number.
3337 xlog_verify_iclog(xlog_t *log,
3338 xlog_in_core_t *iclog,
3342 xlog_op_header_t *ophead;
3343 xlog_in_core_t *icptr;
3344 xlog_in_core_2_t *xhdr;
3346 xfs_caddr_t base_ptr;
3347 __psint_t field_offset;
3349 int len, i, j, k, op_len;
3353 /* check validity of iclog pointers */
3355 icptr = log->l_iclog;
3356 for (i=0; i < log->l_iclog_bufs; i++) {
3358 xlog_panic("xlog_verify_iclog: invalid ptr");
3359 icptr = icptr->ic_next;
3361 if (icptr != log->l_iclog)
3362 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3365 /* check log magic numbers */
3366 ptr = (xfs_caddr_t) &(iclog->ic_header);
3367 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3368 xlog_panic("xlog_verify_iclog: invalid magic num");
3370 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3372 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3373 xlog_panic("xlog_verify_iclog: unexpected magic num");
3377 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3378 ptr = iclog->ic_datap;
3380 ophead = (xlog_op_header_t *)ptr;
3381 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3382 for (i = 0; i < len; i++) {
3383 ophead = (xlog_op_header_t *)ptr;
3385 /* clientid is only 1 byte */
3386 field_offset = (__psint_t)
3387 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3388 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3389 clientid = ophead->oh_clientid;
3391 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3392 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3393 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3394 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3395 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3397 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3400 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3401 cmn_err(CE_WARN, "xlog_verify_iclog: invalid clientid %d op 0x%p offset 0x%x", clientid, ophead, field_offset);
3404 field_offset = (__psint_t)
3405 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3406 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3407 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3409 idx = BTOBBT((__psint_t)&ophead->oh_len -
3410 (__psint_t)iclog->ic_datap);
3411 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3412 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3413 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3414 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3416 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3419 ptr += sizeof(xlog_op_header_t) + op_len;
3421 } /* xlog_verify_iclog */
3422 #endif /* DEBUG && !XLOG_NOLOG */
3425 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3431 xlog_in_core_t *iclog, *ic;
3433 iclog = log->l_iclog;
3434 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3436 * Mark all the incore logs IOERROR.
3437 * From now on, no log flushes will result.
3441 ic->ic_state = XLOG_STATE_IOERROR;
3443 } while (ic != iclog);
3447 * Return non-zero, if state transition has already happened.
3453 * This is called from xfs_force_shutdown, when we're forcibly
3454 * shutting down the filesystem, typically because of an IO error.
3455 * Our main objectives here are to make sure that:
3456 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3457 * parties to find out, 'atomically'.
3458 * b. those who're sleeping on log reservations, pinned objects and
3459 * other resources get woken up, and be told the bad news.
3460 * c. nothing new gets queued up after (a) and (b) are done.
3461 * d. if !logerror, flush the iclogs to disk, then seal them off
3465 xfs_log_force_umount(
3466 struct xfs_mount *mp,
3478 * If this happens during log recovery, don't worry about
3479 * locking; the log isn't open for business yet.
3482 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3483 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3484 XFS_BUF_DONE(mp->m_sb_bp);
3489 * Somebody could've already done the hard work for us.
3490 * No need to get locks for this.
3492 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3493 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3498 * We must hold both the GRANT lock and the LOG lock,
3499 * before we mark the filesystem SHUTDOWN and wake
3500 * everybody up to tell the bad news.
3502 s = GRANT_LOCK(log);
3504 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3505 XFS_BUF_DONE(mp->m_sb_bp);
3507 * This flag is sort of redundant because of the mount flag, but
3508 * it's good to maintain the separation between the log and the rest
3511 log->l_flags |= XLOG_IO_ERROR;
3514 * If we hit a log error, we want to mark all the iclogs IOERROR
3515 * while we're still holding the loglock.
3518 retval = xlog_state_ioerror(log);
3519 LOG_UNLOCK(log, s2);
3522 * We don't want anybody waiting for log reservations
3523 * after this. That means we have to wake up everybody
3524 * queued up on reserve_headq as well as write_headq.
3525 * In addition, we make sure in xlog_{re}grant_log_space
3526 * that we don't enqueue anything once the SHUTDOWN flag
3527 * is set, and this action is protected by the GRANTLOCK.
3529 if ((tic = log->l_reserve_headq)) {
3531 sv_signal(&tic->t_sema);
3533 } while (tic != log->l_reserve_headq);
3536 if ((tic = log->l_write_headq)) {
3538 sv_signal(&tic->t_sema);
3540 } while (tic != log->l_write_headq);
3542 GRANT_UNLOCK(log, s);
3544 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3547 * Force the incore logs to disk before shutting the
3548 * log down completely.
3550 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC);
3552 retval = xlog_state_ioerror(log);
3553 LOG_UNLOCK(log, s2);
3556 * Wake up everybody waiting on xfs_log_force.
3557 * Callback all log item committed functions as if the
3558 * log writes were completed.
3560 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3562 #ifdef XFSERRORDEBUG
3564 xlog_in_core_t *iclog;
3567 iclog = log->l_iclog;
3569 ASSERT(iclog->ic_callback == 0);
3570 iclog = iclog->ic_next;
3571 } while (iclog != log->l_iclog);
3575 /* return non-zero if log IOERROR transition had already happened */
3580 xlog_iclogs_empty(xlog_t *log)
3582 xlog_in_core_t *iclog;
3584 iclog = log->l_iclog;
3586 /* endianness does not matter here, zero is zero in
3589 if (iclog->ic_header.h_num_logops)
3591 iclog = iclog->ic_next;
3592 } while (iclog != log->l_iclog);