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,
100 STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
101 STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
103 /* local functions to manipulate grant head */
104 STATIC int xlog_grant_log_space(xlog_t *log,
105 xlog_ticket_t *xtic);
106 STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
108 STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
109 xlog_ticket_t *ticket);
110 STATIC int xlog_regrant_write_log_space(xlog_t *log,
111 xlog_ticket_t *ticket);
112 STATIC void xlog_ungrant_log_space(xlog_t *log,
113 xlog_ticket_t *ticket);
116 /* local ticket functions */
117 STATIC void xlog_state_ticket_alloc(xlog_t *log);
118 STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
123 STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
125 /* local debug functions */
126 #if defined(DEBUG) && !defined(XLOG_NOLOG)
127 STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
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_grant_head(a,b)
136 #define xlog_verify_iclog(a,b,c,d)
137 #define xlog_verify_tail_lsn(a,b,c)
140 STATIC int xlog_iclogs_empty(xlog_t *log);
143 int xlog_do_error = 0;
144 int xlog_req_num = 0;
145 int xlog_error_mod = 33;
148 #define XLOG_FORCED_SHUTDOWN(log) (log->l_flags & XLOG_IO_ERROR)
151 * 0 => disable log manager
152 * 1 => enable log manager
153 * 2 => enable log manager and log debugging
155 #if defined(XLOG_NOLOG) || defined(DEBUG)
157 xfs_buftarg_t *xlog_target;
160 #if defined(XFS_LOG_TRACE)
163 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
167 if (!log->l_grant_trace) {
168 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
169 if (!log->l_grant_trace)
172 /* ticket counts are 1 byte each */
173 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
175 ktrace_enter(log->l_grant_trace,
177 (void *)log->l_reserve_headq,
178 (void *)log->l_write_headq,
179 (void *)((unsigned long)log->l_grant_reserve_cycle),
180 (void *)((unsigned long)log->l_grant_reserve_bytes),
181 (void *)((unsigned long)log->l_grant_write_cycle),
182 (void *)((unsigned long)log->l_grant_write_bytes),
183 (void *)((unsigned long)log->l_curr_cycle),
184 (void *)((unsigned long)log->l_curr_block),
185 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
186 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
188 (void *)((unsigned long)tic->t_trans_type),
190 (void *)((unsigned long)tic->t_curr_res),
191 (void *)((unsigned long)tic->t_unit_res));
195 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
201 if (!iclog->ic_trace)
202 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
203 ktrace_enter(iclog->ic_trace,
204 (void *)((unsigned long)state),
205 (void *)((unsigned long)pid),
223 #define xlog_trace_loggrant(log,tic,string)
224 #define xlog_trace_iclog(iclog,state)
225 #endif /* XFS_LOG_TRACE */
230 * 1. currblock field gets updated at startup and after in-core logs
231 * marked as with WANT_SYNC.
235 * This routine is called when a user of a log manager ticket is done with
236 * the reservation. If the ticket was ever used, then a commit record for
237 * the associated transaction is written out as a log operation header with
238 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
239 * a given ticket. If the ticket was one with a permanent reservation, then
240 * a few operations are done differently. Permanent reservation tickets by
241 * default don't release the reservation. They just commit the current
242 * transaction with the belief that the reservation is still needed. A flag
243 * must be passed in before permanent reservations are actually released.
244 * When these type of tickets are not released, they need to be set into
245 * the inited state again. By doing this, a start record will be written
246 * out when the next write occurs.
249 xfs_log_done(xfs_mount_t *mp,
250 xfs_log_ticket_t xtic,
254 xlog_t *log = mp->m_log;
255 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
258 #if defined(DEBUG) || defined(XLOG_NOLOG)
259 if (!xlog_debug && xlog_target == log->l_targ)
263 if (XLOG_FORCED_SHUTDOWN(log) ||
265 * If nothing was ever written, don't write out commit record.
266 * If we get an error, just continue and give back the log ticket.
268 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
269 (xlog_commit_record(mp, ticket,
270 (xlog_in_core_t **)iclog, &lsn)))) {
271 lsn = (xfs_lsn_t) -1;
272 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
273 flags |= XFS_LOG_REL_PERM_RESERV;
278 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
279 (flags & XFS_LOG_REL_PERM_RESERV)) {
281 * Release ticket if not permanent reservation or a specifc
282 * request has been made to release a permanent reservation.
284 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
285 xlog_ungrant_log_space(log, ticket);
286 xlog_state_put_ticket(log, ticket);
288 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
289 xlog_regrant_reserve_log_space(log, ticket);
292 /* If this ticket was a permanent reservation and we aren't
293 * trying to release it, reset the inited flags; so next time
294 * we write, a start record will be written out.
296 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
297 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
298 ticket->t_flags |= XLOG_TIC_INITED;
305 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
306 * the force is done synchronously.
308 * Asynchronous forces are implemented by setting the WANT_SYNC
309 * bit in the appropriate in-core log and then returning.
311 * Synchronous forces are implemented with a semaphore. All callers
312 * to force a given lsn to disk will wait on a semaphore attached to the
313 * specific in-core log. When given in-core log finally completes its
314 * write to disk, that thread will wake up all threads waiting on the
324 xlog_t *log = mp->m_log;
328 log_flushed = &dummy;
330 #if defined(DEBUG) || defined(XLOG_NOLOG)
331 if (!xlog_debug && xlog_target == log->l_targ)
335 ASSERT(flags & XFS_LOG_FORCE);
337 XFS_STATS_INC(xs_log_force);
339 if (log->l_flags & XLOG_IO_ERROR)
340 return XFS_ERROR(EIO);
342 return xlog_state_sync_all(log, flags, log_flushed);
344 return xlog_state_sync(log, lsn, flags, log_flushed);
345 } /* xfs_log_force */
348 * Attaches a new iclog I/O completion callback routine during
349 * transaction commit. If the log is in error state, a non-zero
350 * return code is handed back and the caller is responsible for
351 * executing the callback at an appropriate time.
354 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
355 void *iclog_hndl, /* iclog to hang callback off */
356 xfs_log_callback_t *cb)
358 xlog_t *log = mp->m_log;
359 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
362 #if defined(DEBUG) || defined(XLOG_NOLOG)
363 if (!xlog_debug && xlog_target == log->l_targ)
368 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
370 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
371 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
373 *(iclog->ic_callback_tail) = cb;
374 iclog->ic_callback_tail = &(cb->cb_next);
376 LOG_UNLOCK(log, spl);
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,
414 xlog_t *log = mp->m_log;
415 xlog_ticket_t *internal_ticket;
418 #if defined(DEBUG) || defined(XLOG_NOLOG)
419 if (!xlog_debug && xlog_target == log->l_targ)
423 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
424 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
426 if (XLOG_FORCED_SHUTDOWN(log))
427 return XFS_ERROR(EIO);
429 XFS_STATS_INC(xs_try_logspace);
431 if (*ticket != NULL) {
432 ASSERT(flags & XFS_LOG_PERM_RESERV);
433 internal_ticket = (xlog_ticket_t *)*ticket;
434 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
435 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
436 retval = xlog_regrant_write_log_space(log, internal_ticket);
438 /* may sleep if need to allocate more tickets */
439 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
441 internal_ticket->t_trans_type = t_type;
442 *ticket = internal_ticket;
443 xlog_trace_loggrant(log, internal_ticket,
444 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
445 "xfs_log_reserve: create new ticket (permanent trans)" :
446 "xfs_log_reserve: create new ticket");
447 xlog_grant_push_ail(mp,
448 (internal_ticket->t_unit_res *
449 internal_ticket->t_cnt));
450 retval = xlog_grant_log_space(log, internal_ticket);
454 } /* xfs_log_reserve */
458 * Mount a log filesystem
460 * mp - ubiquitous xfs mount point structure
461 * log_target - buftarg of on-disk log device
462 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
463 * num_bblocks - Number of BBSIZE blocks in on-disk log
465 * Return error or zero.
468 xfs_log_mount(xfs_mount_t *mp,
469 xfs_buftarg_t *log_target,
470 xfs_daddr_t blk_offset,
473 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
474 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
477 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
479 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
482 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
484 #if defined(DEBUG) || defined(XLOG_NOLOG)
486 cmn_err(CE_NOTE, "log dev: %s", XFS_BUFTARG_NAME(log_target));
491 * skip log recovery on a norecovery mount. pretend it all
494 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
496 vfs_t *vfsp = XFS_MTOVFS(mp);
497 int readonly = (vfsp->vfs_flag & VFS_RDONLY);
500 vfsp->vfs_flag &= ~VFS_RDONLY;
502 error = xlog_recover(mp->m_log, readonly);
505 vfsp->vfs_flag |= VFS_RDONLY;
507 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
508 xlog_unalloc_log(mp->m_log);
513 /* Normal transactions can now occur */
514 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
516 /* End mounting message in xfs_log_mount_finish */
518 } /* xfs_log_mount */
521 * Finish the recovery of the file system. This is separate from
522 * the xfs_log_mount() call, because it depends on the code in
523 * xfs_mountfs() to read in the root and real-time bitmap inodes
524 * between calling xfs_log_mount() and here.
526 * mp - ubiquitous xfs mount point structure
529 xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
533 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
534 error = xlog_recover_finish(mp->m_log, mfsi_flags);
537 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
544 * Unmount processing for the log.
547 xfs_log_unmount(xfs_mount_t *mp)
551 error = xfs_log_unmount_write(mp);
552 xfs_log_unmount_dealloc(mp);
557 * Final log writes as part of unmount.
559 * Mark the filesystem clean as unmount happens. Note that during relocation
560 * this routine needs to be executed as part of source-bag while the
561 * deallocation must not be done until source-end.
565 * Unmount record used to have a string "Unmount filesystem--" in the
566 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
567 * We just write the magic number now since that particular field isn't
568 * currently architecture converted and "nUmount" is a bit foo.
569 * As far as I know, there weren't any dependencies on the old behaviour.
573 xfs_log_unmount_write(xfs_mount_t *mp)
575 xlog_t *log = mp->m_log;
576 xlog_in_core_t *iclog;
578 xlog_in_core_t *first_iclog;
580 xfs_log_iovec_t reg[1];
581 xfs_log_ticket_t tic = NULL;
586 /* the data section must be 32 bit size aligned */
590 __uint32_t pad2; /* may as well make it 64 bits */
591 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
593 #if defined(DEBUG) || defined(XLOG_NOLOG)
594 if (!xlog_debug && xlog_target == log->l_targ)
599 * Don't write out unmount record on read-only mounts.
600 * Or, if we are doing a forced umount (typically because of IO errors).
602 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
605 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
608 first_iclog = iclog = log->l_iclog;
610 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
611 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
612 ASSERT(iclog->ic_offset == 0);
614 iclog = iclog->ic_next;
615 } while (iclog != first_iclog);
617 if (! (XLOG_FORCED_SHUTDOWN(log))) {
618 reg[0].i_addr = (void*)&magic;
619 reg[0].i_len = sizeof(magic);
620 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
622 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0, 0);
624 /* remove inited flag */
625 ((xlog_ticket_t *)tic)->t_flags = 0;
626 error = xlog_write(mp, reg, 1, tic, &lsn,
627 NULL, XLOG_UNMOUNT_TRANS);
629 * At this point, we're umounting anyway,
630 * so there's no point in transitioning log state
631 * to IOERROR. Just continue...
636 xfs_fs_cmn_err(CE_ALERT, mp,
637 "xfs_log_unmount: unmount record failed");
642 iclog = log->l_iclog;
645 xlog_state_want_sync(log, iclog);
646 (void) xlog_state_release_iclog(log, iclog);
649 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
650 iclog->ic_state == XLOG_STATE_DIRTY)) {
651 if (!XLOG_FORCED_SHUTDOWN(log)) {
652 sv_wait(&iclog->ic_forcesema, PMEM,
653 &log->l_icloglock, s);
661 xlog_state_put_ticket(log, tic);
664 * We're already in forced_shutdown mode, couldn't
665 * even attempt to write out the unmount transaction.
667 * Go through the motions of sync'ing and releasing
668 * the iclog, even though no I/O will actually happen,
669 * we need to wait for other log I/O's that may already
670 * be in progress. Do this as a separate section of
671 * code so we'll know if we ever get stuck here that
672 * we're in this odd situation of trying to unmount
673 * a file system that went into forced_shutdown as
674 * the result of an unmount..
677 iclog = log->l_iclog;
681 xlog_state_want_sync(log, iclog);
682 (void) xlog_state_release_iclog(log, iclog);
686 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
687 || iclog->ic_state == XLOG_STATE_DIRTY
688 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
690 sv_wait(&iclog->ic_forcesema, PMEM,
691 &log->l_icloglock, s);
698 } /* xfs_log_unmount_write */
701 * Deallocate log structures for unmount/relocation.
704 xfs_log_unmount_dealloc(xfs_mount_t *mp)
706 xlog_unalloc_log(mp->m_log);
710 * Write region vectors to log. The write happens using the space reservation
711 * of the ticket (tic). It is not a requirement that all writes for a given
712 * transaction occur with one call to xfs_log_write().
715 xfs_log_write(xfs_mount_t * mp,
716 xfs_log_iovec_t reg[],
718 xfs_log_ticket_t tic,
719 xfs_lsn_t *start_lsn)
722 xlog_t *log = mp->m_log;
724 #if defined(DEBUG) || defined(XLOG_NOLOG)
725 if (!xlog_debug && xlog_target == log->l_targ) {
730 if (XLOG_FORCED_SHUTDOWN(log))
731 return XFS_ERROR(EIO);
733 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
734 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
737 } /* xfs_log_write */
741 xfs_log_move_tail(xfs_mount_t *mp,
745 xlog_t *log = mp->m_log;
746 int need_bytes, free_bytes, cycle, bytes;
749 #if defined(DEBUG) || defined(XLOG_NOLOG)
750 if (!xlog_debug && xlog_target == log->l_targ)
754 if (XLOG_FORCED_SHUTDOWN(log))
756 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
759 /* needed since sync_lsn is 64 bits */
761 tail_lsn = log->l_last_sync_lsn;
767 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
771 log->l_tail_lsn = tail_lsn;
774 if ((tic = log->l_write_headq)) {
776 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
777 panic("Recovery problem");
779 cycle = log->l_grant_write_cycle;
780 bytes = log->l_grant_write_bytes;
781 free_bytes = xlog_space_left(log, cycle, bytes);
783 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
785 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
788 free_bytes -= tic->t_unit_res;
789 sv_signal(&tic->t_sema);
791 } while (tic != log->l_write_headq);
793 if ((tic = log->l_reserve_headq)) {
795 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
796 panic("Recovery problem");
798 cycle = log->l_grant_reserve_cycle;
799 bytes = log->l_grant_reserve_bytes;
800 free_bytes = xlog_space_left(log, cycle, bytes);
802 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
803 need_bytes = tic->t_unit_res*tic->t_cnt;
805 need_bytes = tic->t_unit_res;
806 if (free_bytes < need_bytes && tail_lsn != 1)
809 free_bytes -= need_bytes;
810 sv_signal(&tic->t_sema);
812 } while (tic != log->l_reserve_headq);
814 GRANT_UNLOCK(log, s);
815 } /* xfs_log_move_tail */
818 * Determine if we have a transaction that has gone to disk
819 * that needs to be covered. Log activity needs to be idle (no AIL and
820 * nothing in the iclogs). And, we need to be in the right state indicating
821 * something has gone out.
824 xfs_log_need_covered(xfs_mount_t *mp)
828 xlog_t *log = mp->m_log;
829 vfs_t *vfsp = XFS_MTOVFS(mp);
831 if (fs_frozen(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
832 (vfsp->vfs_flag & VFS_RDONLY))
836 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
837 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
838 && !xfs_trans_first_ail(mp, &gen)
839 && xlog_iclogs_empty(log)) {
840 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
841 log->l_covered_state = XLOG_STATE_COVER_DONE;
843 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
844 log->l_covered_state = XLOG_STATE_COVER_DONE2;
852 /******************************************************************************
856 ******************************************************************************
859 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
860 * The log manager must keep track of the last LR which was committed
861 * to disk. The lsn of this LR will become the new tail_lsn whenever
862 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
863 * the situation where stuff could be written into the log but nothing
864 * was ever in the AIL when asked. Eventually, we panic since the
865 * tail hits the head.
867 * We may be holding the log iclog lock upon entering this routine.
870 xlog_assign_tail_lsn(xfs_mount_t *mp)
874 xlog_t *log = mp->m_log;
876 tail_lsn = xfs_trans_tail_ail(mp);
879 log->l_tail_lsn = tail_lsn;
881 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
883 GRANT_UNLOCK(log, s);
886 } /* xlog_assign_tail_lsn */
890 * Return the space in the log between the tail and the head. The head
891 * is passed in the cycle/bytes formal parms. In the special case where
892 * the reserve head has wrapped passed the tail, this calculation is no
893 * longer valid. In this case, just return 0 which means there is no space
894 * in the log. This works for all places where this function is called
895 * with the reserve head. Of course, if the write head were to ever
896 * wrap the tail, we should blow up. Rather than catch this case here,
897 * we depend on other ASSERTions in other parts of the code. XXXmiken
899 * This code also handles the case where the reservation head is behind
900 * the tail. The details of this case are described below, but the end
901 * result is that we return the size of the log as the amount of space left.
904 xlog_space_left(xlog_t *log, int cycle, int bytes)
910 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
911 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
912 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
913 free_bytes = log->l_logsize - (bytes - tail_bytes);
914 } else if ((tail_cycle + 1) < cycle) {
916 } else if (tail_cycle < cycle) {
917 ASSERT(tail_cycle == (cycle - 1));
918 free_bytes = tail_bytes - bytes;
921 * The reservation head is behind the tail.
922 * In this case we just want to return the size of the
923 * log as the amount of space left.
925 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
926 "xlog_space_left: head behind tail\n"
927 " tail_cycle = %d, tail_bytes = %d\n"
928 " GH cycle = %d, GH bytes = %d",
929 tail_cycle, tail_bytes, cycle, bytes);
931 free_bytes = log->l_logsize;
934 } /* xlog_space_left */
938 * Log function which is called when an io completes.
940 * The log manager needs its own routine, in order to control what
941 * happens with the buffer after the write completes.
944 xlog_iodone(xfs_buf_t *bp)
946 xlog_in_core_t *iclog;
950 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
951 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
952 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
956 * Some versions of cpp barf on the recursive definition of
957 * ic_log -> hic_fields.ic_log and expand ic_log twice when
958 * it is passed through two macros. Workaround broken cpp.
963 * Race to shutdown the filesystem if we see an error.
965 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
966 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
967 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
969 xfs_force_shutdown(l->l_mp, XFS_LOG_IO_ERROR);
971 * This flag will be propagated to the trans-committed
972 * callback routines to let them know that the log-commit
975 aborted = XFS_LI_ABORTED;
976 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
977 aborted = XFS_LI_ABORTED;
979 xlog_state_done_syncing(iclog, aborted);
980 if (!(XFS_BUF_ISASYNC(bp))) {
982 * Corresponding psema() will be done in bwrite(). If we don't
983 * vsema() here, panic.
985 XFS_BUF_V_IODONESEMA(bp);
990 * The bdstrat callback function for log bufs. This gives us a central
991 * place to trap bufs in case we get hit by a log I/O error and need to
992 * shutdown. Actually, in practice, even when we didn't get a log error,
993 * we transition the iclogs to IOERROR state *after* flushing all existing
994 * iclogs to disk. This is because we don't want anymore new transactions to be
995 * started or completed afterwards.
998 xlog_bdstrat_cb(struct xfs_buf *bp)
1000 xlog_in_core_t *iclog;
1002 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1004 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1005 /* note for irix bstrat will need struct bdevsw passed
1006 * Fix the following macro if the code ever is merged
1012 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1013 XFS_BUF_ERROR(bp, EIO);
1016 return (XFS_ERROR(EIO));
1022 * Return size of each in-core log record buffer.
1024 * Low memory machines only get 2 16KB buffers. We don't want to waste
1025 * memory here. However, all other machines get at least 2 32KB buffers.
1026 * The number is hard coded because we don't care about the minimum
1027 * memory size, just 32MB systems.
1029 * If the filesystem blocksize is too large, we may need to choose a
1030 * larger size since the directory code currently logs entire blocks.
1034 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1040 #if defined(DEBUG) || defined(XLOG_NOLOG)
1042 * When logbufs == 0, someone has disabled the log from the FSTAB
1043 * file. This is not a documented feature. We need to set xlog_debug
1044 * to zero (this deactivates the log) and set xlog_target to the
1045 * appropriate device. Only one filesystem may be affected as such
1046 * since this is just a performance hack to test what we might be able
1047 * to get if the log were not present.
1049 if (mp->m_logbufs == 0) {
1051 xlog_target = log->l_targ;
1052 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1057 * This is the normal path. If m_logbufs == -1, then the
1058 * admin has chosen to use the system defaults for logbuffers.
1060 if (mp->m_logbufs == -1) {
1061 if (xfs_physmem <= btoc(128*1024*1024)) {
1062 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1063 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1064 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1066 /* 256K with 32K bufs */
1067 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1070 log->l_iclog_bufs = mp->m_logbufs;
1072 #if defined(DEBUG) || defined(XLOG_NOLOG)
1073 /* We are reactivating a filesystem after it was inactive */
1074 if (log->l_targ == xlog_target) {
1082 * Buffer size passed in from mount system call.
1084 if (mp->m_logbsize != -1) {
1085 size = log->l_iclog_size = mp->m_logbsize;
1086 log->l_iclog_size_log = 0;
1088 log->l_iclog_size_log++;
1092 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1093 /* # headers = size / 32K
1094 * one header holds cycles from 32K of data
1097 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1098 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1100 log->l_iclog_hsize = xhdrs << BBSHIFT;
1101 log->l_iclog_heads = xhdrs;
1103 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1104 log->l_iclog_hsize = BBSIZE;
1105 log->l_iclog_heads = 1;
1111 * Special case machines that have less than 32MB of memory.
1112 * All machines with more memory use 32KB buffers.
1114 if (xfs_physmem <= btoc(32*1024*1024)) {
1115 /* Don't change; min configuration */
1116 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1117 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1119 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1120 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1123 /* the default log size is 16k or 32k which is one header sector */
1124 log->l_iclog_hsize = BBSIZE;
1125 log->l_iclog_heads = 1;
1128 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1129 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1131 if (mp->m_sb.sb_blocksize >= 16*1024) {
1132 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1133 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1134 if (mp->m_logbufs == -1) {
1135 switch (mp->m_sb.sb_blocksize) {
1136 case 16*1024: /* 16 KB */
1137 log->l_iclog_bufs = 3;
1139 case 32*1024: /* 32 KB */
1140 log->l_iclog_bufs = 4;
1142 case 64*1024: /* 64 KB */
1143 log->l_iclog_bufs = 8;
1146 xlog_panic("XFS: Invalid blocksize");
1151 } /* xlog_get_iclog_buffer_size */
1155 * This routine initializes some of the log structure for a given mount point.
1156 * Its primary purpose is to fill in enough, so recovery can occur. However,
1157 * some other stuff may be filled in too.
1160 xlog_alloc_log(xfs_mount_t *mp,
1161 xfs_buftarg_t *log_target,
1162 xfs_daddr_t blk_offset,
1166 xlog_rec_header_t *head;
1167 xlog_in_core_t **iclogp;
1168 xlog_in_core_t *iclog, *prev_iclog=NULL;
1173 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1176 log->l_targ = log_target;
1177 log->l_logsize = BBTOB(num_bblks);
1178 log->l_logBBstart = blk_offset;
1179 log->l_logBBsize = num_bblks;
1180 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1181 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1183 log->l_prev_block = -1;
1184 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1185 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1186 log->l_last_sync_lsn = log->l_tail_lsn;
1187 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1188 log->l_grant_reserve_cycle = 1;
1189 log->l_grant_write_cycle = 1;
1191 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1192 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1193 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1194 /* for larger sector sizes, must have v2 or external log */
1195 ASSERT(log->l_sectbb_log == 0 ||
1196 log->l_logBBstart == 0 ||
1197 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1198 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1200 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1202 xlog_get_iclog_buffer_size(mp, log);
1204 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1205 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1206 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1207 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1208 ASSERT(XFS_BUF_ISBUSY(bp));
1209 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1212 spinlock_init(&log->l_icloglock, "iclog");
1213 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1214 initnsema(&log->l_flushsema, 0, "ic-flush");
1215 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1217 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1218 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1220 iclogp = &log->l_iclog;
1222 * The amount of memory to allocate for the iclog structure is
1223 * rather funky due to the way the structure is defined. It is
1224 * done this way so that we can use different sizes for machines
1225 * with different amounts of memory. See the definition of
1226 * xlog_in_core_t in xfs_log_priv.h for details.
1228 iclogsize = log->l_iclog_size;
1229 ASSERT(log->l_iclog_size >= 4096);
1230 for (i=0; i < log->l_iclog_bufs; i++) {
1231 *iclogp = (xlog_in_core_t *)
1232 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1234 iclog->hic_data = (xlog_in_core_2_t *)
1235 kmem_zalloc(iclogsize, KM_SLEEP);
1237 iclog->ic_prev = prev_iclog;
1239 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1241 head = &iclog->ic_header;
1242 memset(head, 0, sizeof(xlog_rec_header_t));
1243 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1244 INT_SET(head->h_version, ARCH_CONVERT,
1245 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1246 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1248 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1249 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1251 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1252 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1253 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1254 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1257 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1258 iclog->ic_state = XLOG_STATE_ACTIVE;
1259 iclog->ic_log = log;
1260 iclog->ic_callback_tail = &(iclog->ic_callback);
1261 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1263 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1264 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1265 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1266 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1268 iclogp = &iclog->ic_next;
1270 *iclogp = log->l_iclog; /* complete ring */
1271 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1274 } /* xlog_alloc_log */
1278 * Write out the commit record of a transaction associated with the given
1279 * ticket. Return the lsn of the commit record.
1282 xlog_commit_record(xfs_mount_t *mp,
1283 xlog_ticket_t *ticket,
1284 xlog_in_core_t **iclog,
1285 xfs_lsn_t *commitlsnp)
1288 xfs_log_iovec_t reg[1];
1290 reg[0].i_addr = NULL;
1292 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_COMMIT);
1294 ASSERT_ALWAYS(iclog);
1295 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1296 iclog, XLOG_COMMIT_TRANS))) {
1297 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
1300 } /* xlog_commit_record */
1304 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1305 * log space. This code pushes on the lsn which would supposedly free up
1306 * the 25% which we want to leave free. We may need to adopt a policy which
1307 * pushes on an lsn which is further along in the log once we reach the high
1308 * water mark. In this manner, we would be creating a low water mark.
1311 xlog_grant_push_ail(xfs_mount_t *mp,
1314 xlog_t *log = mp->m_log; /* pointer to the log */
1315 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1316 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1317 int free_blocks; /* free blocks left to write to */
1318 int free_bytes; /* free bytes left to write to */
1319 int threshold_block; /* block in lsn we'd like to be at */
1320 int threshold_cycle; /* lsn cycle we'd like to be at */
1324 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1326 s = GRANT_LOCK(log);
1327 free_bytes = xlog_space_left(log,
1328 log->l_grant_reserve_cycle,
1329 log->l_grant_reserve_bytes);
1330 tail_lsn = log->l_tail_lsn;
1331 free_blocks = BTOBBT(free_bytes);
1334 * Set the threshold for the minimum number of free blocks in the
1335 * log to the maximum of what the caller needs, one quarter of the
1336 * log, and 256 blocks.
1338 free_threshold = BTOBB(need_bytes);
1339 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1340 free_threshold = MAX(free_threshold, 256);
1341 if (free_blocks < free_threshold) {
1342 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1343 threshold_cycle = CYCLE_LSN(tail_lsn);
1344 if (threshold_block >= log->l_logBBsize) {
1345 threshold_block -= log->l_logBBsize;
1346 threshold_cycle += 1;
1348 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1351 /* Don't pass in an lsn greater than the lsn of the last
1352 * log record known to be on disk.
1354 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1355 threshold_lsn = log->l_last_sync_lsn;
1357 GRANT_UNLOCK(log, s);
1360 * Get the transaction layer to kick the dirty buffers out to
1361 * disk asynchronously. No point in trying to do this if
1362 * the filesystem is shutting down.
1364 if (threshold_lsn &&
1365 !XLOG_FORCED_SHUTDOWN(log))
1366 xfs_trans_push_ail(mp, threshold_lsn);
1367 } /* xlog_grant_push_ail */
1371 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1372 * fashion. Previously, we should have moved the current iclog
1373 * ptr in the log to point to the next available iclog. This allows further
1374 * write to continue while this code syncs out an iclog ready to go.
1375 * Before an in-core log can be written out, the data section must be scanned
1376 * to save away the 1st word of each BBSIZE block into the header. We replace
1377 * it with the current cycle count. Each BBSIZE block is tagged with the
1378 * cycle count because there in an implicit assumption that drives will
1379 * guarantee that entire 512 byte blocks get written at once. In other words,
1380 * we can't have part of a 512 byte block written and part not written. By
1381 * tagging each block, we will know which blocks are valid when recovering
1382 * after an unclean shutdown.
1384 * This routine is single threaded on the iclog. No other thread can be in
1385 * this routine with the same iclog. Changing contents of iclog can there-
1386 * fore be done without grabbing the state machine lock. Updating the global
1387 * log will require grabbing the lock though.
1389 * The entire log manager uses a logical block numbering scheme. Only
1390 * log_sync (and then only bwrite()) know about the fact that the log may
1391 * not start with block zero on a given device. The log block start offset
1392 * is added immediately before calling bwrite().
1396 xlog_sync(xlog_t *log,
1397 xlog_in_core_t *iclog)
1399 xfs_caddr_t dptr; /* pointer to byte sized element */
1402 uint count; /* byte count of bwrite */
1403 uint count_init; /* initial count before roundup */
1404 int roundoff; /* roundoff to BB or stripe */
1405 int split = 0; /* split write into two regions */
1408 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1410 XFS_STATS_INC(xs_log_writes);
1411 ASSERT(iclog->ic_refcnt == 0);
1413 /* Add for LR header */
1414 count_init = log->l_iclog_hsize + iclog->ic_offset;
1416 /* Round out the log write size */
1417 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1418 /* we have a v2 stripe unit to use */
1419 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1421 count = BBTOB(BTOBB(count_init));
1423 roundoff = count - count_init;
1424 ASSERT(roundoff >= 0);
1425 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1426 roundoff < log->l_mp->m_sb.sb_logsunit)
1428 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1429 roundoff < BBTOB(1)));
1431 /* move grant heads by roundoff in sync */
1432 s = GRANT_LOCK(log);
1433 XLOG_GRANT_ADD_SPACE(log, roundoff, 'w');
1434 XLOG_GRANT_ADD_SPACE(log, roundoff, 'r');
1435 GRANT_UNLOCK(log, s);
1437 /* put cycle number in every block */
1438 xlog_pack_data(log, iclog, roundoff);
1440 /* real byte length */
1442 INT_SET(iclog->ic_header.h_len,
1444 iclog->ic_offset + roundoff);
1446 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1449 /* put ops count in correct order */
1450 ops = iclog->ic_header.h_num_logops;
1451 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1454 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1455 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1456 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1458 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1460 /* Do we need to split this write into 2 parts? */
1461 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1462 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1463 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1464 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1466 iclog->ic_bwritecnt = 1;
1468 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1469 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1473 * Do an ordered write for the log block.
1475 * It may not be needed to flush the first split block in the log wrap
1476 * case, but do it anyways to be safe -AK
1478 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1479 XFS_BUF_ORDERED(bp);
1481 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1482 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1484 xlog_verify_iclog(log, iclog, count, B_TRUE);
1486 /* account for log which doesn't start at block #0 */
1487 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1489 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1494 if ((error = XFS_bwrite(bp))) {
1495 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1500 bp = iclog->ic_log->l_xbuf;
1501 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1503 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1504 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1505 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1506 (__psint_t)count), split);
1507 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1510 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1511 XFS_BUF_ORDERED(bp);
1512 dptr = XFS_BUF_PTR(bp);
1514 * Bump the cycle numbers at the start of each block
1515 * since this part of the buffer is at the start of
1516 * a new cycle. Watch out for the header magic number
1519 for (i=0; i<split; i += BBSIZE) {
1520 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1521 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1522 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1526 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1527 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1529 /* account for internal log which does't start at block #0 */
1530 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1532 if ((error = XFS_bwrite(bp))) {
1533 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1534 bp, XFS_BUF_ADDR(bp));
1543 * Unallocate a log structure
1546 xlog_unalloc_log(xlog_t *log)
1548 xlog_in_core_t *iclog, *next_iclog;
1549 xlog_ticket_t *tic, *next_tic;
1553 iclog = log->l_iclog;
1554 for (i=0; i<log->l_iclog_bufs; i++) {
1555 sv_destroy(&iclog->ic_forcesema);
1556 sv_destroy(&iclog->ic_writesema);
1557 xfs_buf_free(iclog->ic_bp);
1558 #ifdef XFS_LOG_TRACE
1559 if (iclog->ic_trace != NULL) {
1560 ktrace_free(iclog->ic_trace);
1563 next_iclog = iclog->ic_next;
1564 kmem_free(iclog->hic_data, log->l_iclog_size);
1565 kmem_free(iclog, sizeof(xlog_in_core_t));
1568 freesema(&log->l_flushsema);
1569 spinlock_destroy(&log->l_icloglock);
1570 spinlock_destroy(&log->l_grant_lock);
1572 /* XXXsup take a look at this again. */
1573 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1574 !XLOG_FORCED_SHUTDOWN(log)) {
1575 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1576 "xlog_unalloc_log: (cnt: %d, total: %d)",
1577 log->l_ticket_cnt, log->l_ticket_tcnt);
1578 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1581 tic = log->l_unmount_free;
1583 next_tic = tic->t_next;
1584 kmem_free(tic, NBPP);
1588 xfs_buf_free(log->l_xbuf);
1589 #ifdef XFS_LOG_TRACE
1590 if (log->l_trace != NULL) {
1591 ktrace_free(log->l_trace);
1593 if (log->l_grant_trace != NULL) {
1594 ktrace_free(log->l_grant_trace);
1597 log->l_mp->m_log = NULL;
1598 kmem_free(log, sizeof(xlog_t));
1599 } /* xlog_unalloc_log */
1602 * Update counters atomically now that memcpy is done.
1606 xlog_state_finish_copy(xlog_t *log,
1607 xlog_in_core_t *iclog,
1615 iclog->ic_header.h_num_logops += record_cnt;
1616 iclog->ic_offset += copy_bytes;
1619 } /* xlog_state_finish_copy */
1625 * print out info relating to regions written which consume
1628 #if defined(XFS_LOG_RES_DEBUG)
1630 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1633 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1635 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1636 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1657 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1700 xfs_fs_cmn_err(CE_WARN, mp,
1701 "xfs_log_write: reservation summary:\n"
1702 " trans type = %s (%u)\n"
1703 " unit res = %d bytes\n"
1704 " current res = %d bytes\n"
1705 " total reg = %u bytes (o/flow = %u bytes)\n"
1706 " ophdrs = %u (ophdr space = %u bytes)\n"
1707 " ophdr + reg = %u bytes\n"
1708 " num regions = %u\n",
1709 ((ticket->t_trans_type <= 0 ||
1710 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1711 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1712 ticket->t_trans_type,
1715 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1716 ticket->t_res_num_ophdrs, ophdr_spc,
1717 ticket->t_res_arr_sum +
1718 ticket->t_res_o_flow + ophdr_spc,
1721 for (i = 0; i < ticket->t_res_num; i++) {
1722 uint r_type = ticket->t_res_arr[i].r_type;
1724 "region[%u]: %s - %u bytes\n",
1726 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1727 "bad-rtype" : res_type_str[r_type-1]),
1728 ticket->t_res_arr[i].r_len);
1732 #define xlog_print_tic_res(mp, ticket)
1736 * Write some region out to in-core log
1738 * This will be called when writing externally provided regions or when
1739 * writing out a commit record for a given transaction.
1741 * General algorithm:
1742 * 1. Find total length of this write. This may include adding to the
1743 * lengths passed in.
1744 * 2. Check whether we violate the tickets reservation.
1745 * 3. While writing to this iclog
1746 * A. Reserve as much space in this iclog as can get
1747 * B. If this is first write, save away start lsn
1748 * C. While writing this region:
1749 * 1. If first write of transaction, write start record
1750 * 2. Write log operation header (header per region)
1751 * 3. Find out if we can fit entire region into this iclog
1752 * 4. Potentially, verify destination memcpy ptr
1753 * 5. Memcpy (partial) region
1754 * 6. If partial copy, release iclog; otherwise, continue
1755 * copying more regions into current iclog
1756 * 4. Mark want sync bit (in simulation mode)
1757 * 5. Release iclog for potential flush to on-disk log.
1760 * 1. Panic if reservation is overrun. This should never happen since
1761 * reservation amounts are generated internal to the filesystem.
1763 * 1. Tickets are single threaded data structures.
1764 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1765 * syncing routine. When a single log_write region needs to span
1766 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1767 * on all log operation writes which don't contain the end of the
1768 * region. The XLOG_END_TRANS bit is used for the in-core log
1769 * operation which contains the end of the continued log_write region.
1770 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1771 * we don't really know exactly how much space will be used. As a result,
1772 * we don't update ic_offset until the end when we know exactly how many
1773 * bytes have been written out.
1776 xlog_write(xfs_mount_t * mp,
1777 xfs_log_iovec_t reg[],
1779 xfs_log_ticket_t tic,
1780 xfs_lsn_t *start_lsn,
1781 xlog_in_core_t **commit_iclog,
1784 xlog_t *log = mp->m_log;
1785 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1786 xlog_op_header_t *logop_head; /* ptr to log operation header */
1787 xlog_in_core_t *iclog; /* ptr to current in-core log */
1788 __psint_t ptr; /* copy address into data region */
1789 int len; /* # xlog_write() bytes 2 still copy */
1790 int index; /* region index currently copying */
1791 int log_offset; /* offset (from 0) into data region */
1792 int start_rec_copy; /* # bytes to copy for start record */
1793 int partial_copy; /* did we split a region? */
1794 int partial_copy_len;/* # bytes copied if split region */
1795 int need_copy; /* # bytes need to memcpy this region */
1796 int copy_len; /* # bytes actually memcpy'ing */
1797 int copy_off; /* # bytes from entry start */
1798 int contwr; /* continued write of in-core log? */
1800 int record_cnt = 0, data_cnt = 0;
1802 partial_copy_len = partial_copy = 0;
1804 /* Calculate potential maximum space. Each region gets its own
1805 * xlog_op_header_t and may need to be double word aligned.
1808 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1809 len += sizeof(xlog_op_header_t);
1810 XLOG_TIC_ADD_OPHDR(ticket);
1813 for (index = 0; index < nentries; index++) {
1814 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1815 XLOG_TIC_ADD_OPHDR(ticket);
1816 len += reg[index].i_len;
1817 XLOG_TIC_ADD_REGION(ticket, reg[index].i_len, reg[index].i_type);
1819 contwr = *start_lsn = 0;
1821 if (ticket->t_curr_res < len) {
1822 xlog_print_tic_res(mp, ticket);
1825 "xfs_log_write: reservation ran out. Need to up reservation");
1827 /* Customer configurable panic */
1828 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1829 "xfs_log_write: reservation ran out. Need to up reservation");
1830 /* If we did not panic, shutdown the filesystem */
1831 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1834 ticket->t_curr_res -= len;
1836 for (index = 0; index < nentries; ) {
1837 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1838 &contwr, &log_offset)))
1841 ASSERT(log_offset <= iclog->ic_size - 1);
1842 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1844 /* start_lsn is the first lsn written to. That's all we need. */
1846 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1848 /* This loop writes out as many regions as can fit in the amount
1849 * of space which was allocated by xlog_state_get_iclog_space().
1851 while (index < nentries) {
1852 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1853 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1856 /* If first write for transaction, insert start record.
1857 * We can't be trying to commit if we are inited. We can't
1858 * have any "partial_copy" if we are inited.
1860 if (ticket->t_flags & XLOG_TIC_INITED) {
1861 logop_head = (xlog_op_header_t *)ptr;
1862 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1863 logop_head->oh_clientid = ticket->t_clientid;
1864 logop_head->oh_len = 0;
1865 logop_head->oh_flags = XLOG_START_TRANS;
1866 logop_head->oh_res2 = 0;
1867 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1870 start_rec_copy = sizeof(xlog_op_header_t);
1871 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1874 /* Copy log operation header directly into data section */
1875 logop_head = (xlog_op_header_t *)ptr;
1876 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1877 logop_head->oh_clientid = ticket->t_clientid;
1878 logop_head->oh_res2 = 0;
1880 /* header copied directly */
1881 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1883 /* are we copying a commit or unmount record? */
1884 logop_head->oh_flags = flags;
1887 * We've seen logs corrupted with bad transaction client
1888 * ids. This makes sure that XFS doesn't generate them on.
1889 * Turn this into an EIO and shut down the filesystem.
1891 switch (logop_head->oh_clientid) {
1892 case XFS_TRANSACTION:
1897 xfs_fs_cmn_err(CE_WARN, mp,
1898 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1899 logop_head->oh_clientid, tic);
1900 return XFS_ERROR(EIO);
1903 /* Partial write last time? => (partial_copy != 0)
1904 * need_copy is the amount we'd like to copy if everything could
1905 * fit in the current memcpy.
1907 need_copy = reg[index].i_len - partial_copy_len;
1909 copy_off = partial_copy_len;
1910 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1911 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1913 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1914 partial_copy_len = partial_copy = 0;
1915 } else { /* partial write */
1916 copy_len = iclog->ic_size - log_offset;
1917 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1918 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1920 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1921 partial_copy_len += copy_len;
1923 len += sizeof(xlog_op_header_t); /* from splitting of region */
1924 /* account for new log op header */
1925 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1926 XLOG_TIC_ADD_OPHDR(ticket);
1928 xlog_verify_dest_ptr(log, ptr);
1931 ASSERT(copy_len >= 0);
1932 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1933 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1935 /* make copy_len total bytes copied, including headers */
1936 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1938 data_cnt += contwr ? copy_len : 0;
1939 if (partial_copy) { /* copied partial region */
1940 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1941 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1942 record_cnt = data_cnt = 0;
1943 if ((error = xlog_state_release_iclog(log, iclog)))
1945 break; /* don't increment index */
1946 } else { /* copied entire region */
1948 partial_copy_len = partial_copy = 0;
1950 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1951 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1952 record_cnt = data_cnt = 0;
1953 xlog_state_want_sync(log, iclog);
1955 ASSERT(flags & XLOG_COMMIT_TRANS);
1956 *commit_iclog = iclog;
1957 } else if ((error = xlog_state_release_iclog(log, iclog)))
1959 if (index == nentries)
1960 return 0; /* we are done */
1964 } /* if (partial_copy) */
1965 } /* while (index < nentries) */
1966 } /* for (index = 0; index < nentries; ) */
1969 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1971 ASSERT(flags & XLOG_COMMIT_TRANS);
1972 *commit_iclog = iclog;
1975 return (xlog_state_release_iclog(log, iclog));
1979 /*****************************************************************************
1981 * State Machine functions
1983 *****************************************************************************
1986 /* Clean iclogs starting from the head. This ordering must be
1987 * maintained, so an iclog doesn't become ACTIVE beyond one that
1988 * is SYNCING. This is also required to maintain the notion that we use
1989 * a counting semaphore to hold off would be writers to the log when every
1990 * iclog is trying to sync to disk.
1992 * State Change: DIRTY -> ACTIVE
1995 xlog_state_clean_log(xlog_t *log)
1997 xlog_in_core_t *iclog;
2000 iclog = log->l_iclog;
2002 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2003 iclog->ic_state = XLOG_STATE_ACTIVE;
2004 iclog->ic_offset = 0;
2005 iclog->ic_callback = NULL; /* don't need to free */
2007 * If the number of ops in this iclog indicate it just
2008 * contains the dummy transaction, we can
2009 * change state into IDLE (the second time around).
2010 * Otherwise we should change the state into
2012 * We don't need to cover the dummy.
2015 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
2019 * We have two dirty iclogs so start over
2020 * This could also be num of ops indicates
2021 * this is not the dummy going out.
2025 iclog->ic_header.h_num_logops = 0;
2026 memset(iclog->ic_header.h_cycle_data, 0,
2027 sizeof(iclog->ic_header.h_cycle_data));
2028 iclog->ic_header.h_lsn = 0;
2029 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2032 break; /* stop cleaning */
2033 iclog = iclog->ic_next;
2034 } while (iclog != log->l_iclog);
2036 /* log is locked when we are called */
2038 * Change state for the dummy log recording.
2039 * We usually go to NEED. But we go to NEED2 if the changed indicates
2040 * we are done writing the dummy record.
2041 * If we are done with the second dummy recored (DONE2), then
2045 switch (log->l_covered_state) {
2046 case XLOG_STATE_COVER_IDLE:
2047 case XLOG_STATE_COVER_NEED:
2048 case XLOG_STATE_COVER_NEED2:
2049 log->l_covered_state = XLOG_STATE_COVER_NEED;
2052 case XLOG_STATE_COVER_DONE:
2054 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2056 log->l_covered_state = XLOG_STATE_COVER_NEED;
2059 case XLOG_STATE_COVER_DONE2:
2061 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2063 log->l_covered_state = XLOG_STATE_COVER_NEED;
2070 } /* xlog_state_clean_log */
2073 xlog_get_lowest_lsn(
2076 xlog_in_core_t *lsn_log;
2077 xfs_lsn_t lowest_lsn, lsn;
2079 lsn_log = log->l_iclog;
2082 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2083 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
2084 if ((lsn && !lowest_lsn) ||
2085 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2089 lsn_log = lsn_log->ic_next;
2090 } while (lsn_log != log->l_iclog);
2096 xlog_state_do_callback(
2099 xlog_in_core_t *ciclog)
2101 xlog_in_core_t *iclog;
2102 xlog_in_core_t *first_iclog; /* used to know when we've
2103 * processed all iclogs once */
2104 xfs_log_callback_t *cb, *cb_next;
2106 xfs_lsn_t lowest_lsn;
2107 int ioerrors; /* counter: iclogs with errors */
2108 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2109 int funcdidcallbacks; /* flag: function did callbacks */
2110 int repeats; /* for issuing console warnings if
2111 * looping too many times */
2115 first_iclog = iclog = log->l_iclog;
2117 funcdidcallbacks = 0;
2122 * Scan all iclogs starting with the one pointed to by the
2123 * log. Reset this starting point each time the log is
2124 * unlocked (during callbacks).
2126 * Keep looping through iclogs until one full pass is made
2127 * without running any callbacks.
2129 first_iclog = log->l_iclog;
2130 iclog = log->l_iclog;
2131 loopdidcallbacks = 0;
2136 /* skip all iclogs in the ACTIVE & DIRTY states */
2137 if (iclog->ic_state &
2138 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2139 iclog = iclog->ic_next;
2144 * Between marking a filesystem SHUTDOWN and stopping
2145 * the log, we do flush all iclogs to disk (if there
2146 * wasn't a log I/O error). So, we do want things to
2147 * go smoothly in case of just a SHUTDOWN w/o a
2150 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2152 * Can only perform callbacks in order. Since
2153 * this iclog is not in the DONE_SYNC/
2154 * DO_CALLBACK state, we skip the rest and
2155 * just try to clean up. If we set our iclog
2156 * to DO_CALLBACK, we will not process it when
2157 * we retry since a previous iclog is in the
2158 * CALLBACK and the state cannot change since
2159 * we are holding the LOG_LOCK.
2161 if (!(iclog->ic_state &
2162 (XLOG_STATE_DONE_SYNC |
2163 XLOG_STATE_DO_CALLBACK))) {
2164 if (ciclog && (ciclog->ic_state ==
2165 XLOG_STATE_DONE_SYNC)) {
2166 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2171 * We now have an iclog that is in either the
2172 * DO_CALLBACK or DONE_SYNC states. The other
2173 * states (WANT_SYNC, SYNCING, or CALLBACK were
2174 * caught by the above if and are going to
2175 * clean (i.e. we aren't doing their callbacks)
2180 * We will do one more check here to see if we
2181 * have chased our tail around.
2184 lowest_lsn = xlog_get_lowest_lsn(log);
2188 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2190 iclog = iclog->ic_next;
2191 continue; /* Leave this iclog for
2195 iclog->ic_state = XLOG_STATE_CALLBACK;
2199 /* l_last_sync_lsn field protected by
2200 * GRANT_LOCK. Don't worry about iclog's lsn.
2201 * No one else can be here except us.
2203 s = GRANT_LOCK(log);
2205 log->l_last_sync_lsn,
2206 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2208 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2209 GRANT_UNLOCK(log, s);
2212 * Keep processing entries in the callback list
2213 * until we come around and it is empty. We
2214 * need to atomically see that the list is
2215 * empty and change the state to DIRTY so that
2216 * we don't miss any more callbacks being added.
2222 cb = iclog->ic_callback;
2225 iclog->ic_callback_tail = &(iclog->ic_callback);
2226 iclog->ic_callback = NULL;
2229 /* perform callbacks in the order given */
2230 for (; cb != 0; cb = cb_next) {
2231 cb_next = cb->cb_next;
2232 cb->cb_func(cb->cb_arg, aborted);
2235 cb = iclog->ic_callback;
2241 ASSERT(iclog->ic_callback == 0);
2242 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2243 iclog->ic_state = XLOG_STATE_DIRTY;
2246 * Transition from DIRTY to ACTIVE if applicable.
2247 * NOP if STATE_IOERROR.
2249 xlog_state_clean_log(log);
2251 /* wake up threads waiting in xfs_log_force() */
2252 sv_broadcast(&iclog->ic_forcesema);
2254 iclog = iclog->ic_next;
2255 } while (first_iclog != iclog);
2256 if (repeats && (repeats % 10) == 0) {
2257 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2258 "xlog_state_do_callback: looping %d", repeats);
2260 } while (!ioerrors && loopdidcallbacks);
2263 * make one last gasp attempt to see if iclogs are being left in
2267 if (funcdidcallbacks) {
2268 first_iclog = iclog = log->l_iclog;
2270 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2272 * Terminate the loop if iclogs are found in states
2273 * which will cause other threads to clean up iclogs.
2275 * SYNCING - i/o completion will go through logs
2276 * DONE_SYNC - interrupt thread should be waiting for
2278 * IOERROR - give up hope all ye who enter here
2280 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2281 iclog->ic_state == XLOG_STATE_SYNCING ||
2282 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2283 iclog->ic_state == XLOG_STATE_IOERROR )
2285 iclog = iclog->ic_next;
2286 } while (first_iclog != iclog);
2290 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2291 flushcnt = log->l_flushcnt;
2292 log->l_flushcnt = 0;
2296 vsema(&log->l_flushsema);
2297 } /* xlog_state_do_callback */
2301 * Finish transitioning this iclog to the dirty state.
2303 * Make sure that we completely execute this routine only when this is
2304 * the last call to the iclog. There is a good chance that iclog flushes,
2305 * when we reach the end of the physical log, get turned into 2 separate
2306 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2307 * routine. By using the reference count bwritecnt, we guarantee that only
2308 * the second completion goes through.
2310 * Callbacks could take time, so they are done outside the scope of the
2311 * global state machine log lock. Assume that the calls to cvsema won't
2312 * take a long time. At least we know it won't sleep.
2315 xlog_state_done_syncing(
2316 xlog_in_core_t *iclog,
2319 xlog_t *log = iclog->ic_log;
2324 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2325 iclog->ic_state == XLOG_STATE_IOERROR);
2326 ASSERT(iclog->ic_refcnt == 0);
2327 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2331 * If we got an error, either on the first buffer, or in the case of
2332 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2333 * and none should ever be attempted to be written to disk
2336 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2337 if (--iclog->ic_bwritecnt == 1) {
2341 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2345 * Someone could be sleeping prior to writing out the next
2346 * iclog buffer, we wake them all, one will get to do the
2347 * I/O, the others get to wait for the result.
2349 sv_broadcast(&iclog->ic_writesema);
2351 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2352 } /* xlog_state_done_syncing */
2356 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2357 * sleep. The flush semaphore is set to the number of in-core buffers and
2358 * decremented around disk syncing. Therefore, if all buffers are syncing,
2359 * this semaphore will cause new writes to sleep until a sync completes.
2360 * Otherwise, this code just does p() followed by v(). This approximates
2361 * a sleep/wakeup except we can't race.
2363 * The in-core logs are used in a circular fashion. They are not used
2364 * out-of-order even when an iclog past the head is free.
2367 * * log_offset where xlog_write() can start writing into the in-core
2369 * * in-core log pointer to which xlog_write() should write.
2370 * * boolean indicating this is a continued write to an in-core log.
2371 * If this is the last write, then the in-core log's offset field
2372 * needs to be incremented, depending on the amount of data which
2376 xlog_state_get_iclog_space(xlog_t *log,
2378 xlog_in_core_t **iclogp,
2379 xlog_ticket_t *ticket,
2380 int *continued_write,
2385 xlog_rec_header_t *head;
2386 xlog_in_core_t *iclog;
2391 if (XLOG_FORCED_SHUTDOWN(log)) {
2393 return XFS_ERROR(EIO);
2396 iclog = log->l_iclog;
2397 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2400 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2401 XFS_STATS_INC(xs_log_noiclogs);
2402 /* Ensure that log writes happen */
2403 psema(&log->l_flushsema, PINOD);
2406 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2407 head = &iclog->ic_header;
2409 iclog->ic_refcnt++; /* prevents sync */
2410 log_offset = iclog->ic_offset;
2412 /* On the 1st write to an iclog, figure out lsn. This works
2413 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2414 * committing to. If the offset is set, that's how many blocks
2417 if (log_offset == 0) {
2418 ticket->t_curr_res -= log->l_iclog_hsize;
2419 XLOG_TIC_ADD_REGION(ticket,
2421 XLOG_REG_TYPE_LRHEADER);
2422 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2423 ASSIGN_LSN(head->h_lsn, log);
2424 ASSERT(log->l_curr_block >= 0);
2427 /* If there is enough room to write everything, then do it. Otherwise,
2428 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2429 * bit is on, so this will get flushed out. Don't update ic_offset
2430 * until you know exactly how many bytes get copied. Therefore, wait
2431 * until later to update ic_offset.
2433 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2434 * can fit into remaining data section.
2436 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2437 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2439 /* If I'm the only one writing to this iclog, sync it to disk */
2440 if (iclog->ic_refcnt == 1) {
2442 if ((error = xlog_state_release_iclog(log, iclog)))
2451 /* Do we have enough room to write the full amount in the remainder
2452 * of this iclog? Or must we continue a write on the next iclog and
2453 * mark this iclog as completely taken? In the case where we switch
2454 * iclogs (to mark it taken), this particular iclog will release/sync
2455 * to disk in xlog_write().
2457 if (len <= iclog->ic_size - iclog->ic_offset) {
2458 *continued_write = 0;
2459 iclog->ic_offset += len;
2461 *continued_write = 1;
2462 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2466 ASSERT(iclog->ic_offset <= iclog->ic_size);
2469 *logoffsetp = log_offset;
2471 } /* xlog_state_get_iclog_space */
2474 * Atomically get the log space required for a log ticket.
2476 * Once a ticket gets put onto the reserveq, it will only return after
2477 * the needed reservation is satisfied.
2480 xlog_grant_log_space(xlog_t *log,
2492 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2493 panic("grant Recovery problem");
2496 /* Is there space or do we need to sleep? */
2497 s = GRANT_LOCK(log);
2498 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2500 /* something is already sleeping; insert new transaction at end */
2501 if (log->l_reserve_headq) {
2502 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2503 xlog_trace_loggrant(log, tic,
2504 "xlog_grant_log_space: sleep 1");
2506 * Gotta check this before going to sleep, while we're
2507 * holding the grant lock.
2509 if (XLOG_FORCED_SHUTDOWN(log))
2512 XFS_STATS_INC(xs_sleep_logspace);
2513 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2515 * If we got an error, and the filesystem is shutting down,
2516 * we'll catch it down below. So just continue...
2518 xlog_trace_loggrant(log, tic,
2519 "xlog_grant_log_space: wake 1");
2520 s = GRANT_LOCK(log);
2522 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2523 need_bytes = tic->t_unit_res*tic->t_ocnt;
2525 need_bytes = tic->t_unit_res;
2528 if (XLOG_FORCED_SHUTDOWN(log))
2531 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2532 log->l_grant_reserve_bytes);
2533 if (free_bytes < need_bytes) {
2534 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2535 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2536 xlog_trace_loggrant(log, tic,
2537 "xlog_grant_log_space: sleep 2");
2538 XFS_STATS_INC(xs_sleep_logspace);
2539 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2541 if (XLOG_FORCED_SHUTDOWN(log)) {
2542 s = GRANT_LOCK(log);
2546 xlog_trace_loggrant(log, tic,
2547 "xlog_grant_log_space: wake 2");
2548 xlog_grant_push_ail(log->l_mp, need_bytes);
2549 s = GRANT_LOCK(log);
2551 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2552 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2554 /* we've got enough space */
2555 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w');
2556 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'r');
2558 tail_lsn = log->l_tail_lsn;
2560 * Check to make sure the grant write head didn't just over lap the
2561 * tail. If the cycles are the same, we can't be overlapping.
2562 * Otherwise, make sure that the cycles differ by exactly one and
2563 * check the byte count.
2565 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2566 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2567 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2570 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2571 xlog_verify_grant_head(log, 1);
2572 GRANT_UNLOCK(log, s);
2576 if (tic->t_flags & XLOG_TIC_IN_Q)
2577 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2578 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2580 * If we are failing, make sure the ticket doesn't have any
2581 * current reservations. We don't want to add this back when
2582 * the ticket/transaction gets cancelled.
2584 tic->t_curr_res = 0;
2585 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2586 GRANT_UNLOCK(log, s);
2587 return XFS_ERROR(EIO);
2588 } /* xlog_grant_log_space */
2592 * Replenish the byte reservation required by moving the grant write head.
2597 xlog_regrant_write_log_space(xlog_t *log,
2601 int free_bytes, need_bytes;
2602 xlog_ticket_t *ntic;
2607 tic->t_curr_res = tic->t_unit_res;
2608 XLOG_TIC_RESET_RES(tic);
2614 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2615 panic("regrant Recovery problem");
2618 s = GRANT_LOCK(log);
2619 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2621 if (XLOG_FORCED_SHUTDOWN(log))
2624 /* If there are other waiters on the queue then give them a
2625 * chance at logspace before us. Wake up the first waiters,
2626 * if we do not wake up all the waiters then go to sleep waiting
2627 * for more free space, otherwise try to get some space for
2631 if ((ntic = log->l_write_headq)) {
2632 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2633 log->l_grant_write_bytes);
2635 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2637 if (free_bytes < ntic->t_unit_res)
2639 free_bytes -= ntic->t_unit_res;
2640 sv_signal(&ntic->t_sema);
2641 ntic = ntic->t_next;
2642 } while (ntic != log->l_write_headq);
2644 if (ntic != log->l_write_headq) {
2645 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2646 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2648 xlog_trace_loggrant(log, tic,
2649 "xlog_regrant_write_log_space: sleep 1");
2650 XFS_STATS_INC(xs_sleep_logspace);
2651 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2652 &log->l_grant_lock, s);
2654 /* If we're shutting down, this tic is already
2656 if (XLOG_FORCED_SHUTDOWN(log)) {
2657 s = GRANT_LOCK(log);
2661 xlog_trace_loggrant(log, tic,
2662 "xlog_regrant_write_log_space: wake 1");
2663 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2664 s = GRANT_LOCK(log);
2668 need_bytes = tic->t_unit_res;
2671 if (XLOG_FORCED_SHUTDOWN(log))
2674 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2675 log->l_grant_write_bytes);
2676 if (free_bytes < need_bytes) {
2677 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2678 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2679 XFS_STATS_INC(xs_sleep_logspace);
2680 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2682 /* If we're shutting down, this tic is already off the queue */
2683 if (XLOG_FORCED_SHUTDOWN(log)) {
2684 s = GRANT_LOCK(log);
2688 xlog_trace_loggrant(log, tic,
2689 "xlog_regrant_write_log_space: wake 2");
2690 xlog_grant_push_ail(log->l_mp, need_bytes);
2691 s = GRANT_LOCK(log);
2693 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2694 XLOG_DEL_TICKETQ(log->l_write_headq, tic);
2696 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w'); /* we've got enough space */
2698 tail_lsn = log->l_tail_lsn;
2699 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2700 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2701 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2705 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2706 xlog_verify_grant_head(log, 1);
2707 GRANT_UNLOCK(log, s);
2712 if (tic->t_flags & XLOG_TIC_IN_Q)
2713 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2714 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2716 * If we are failing, make sure the ticket doesn't have any
2717 * current reservations. We don't want to add this back when
2718 * the ticket/transaction gets cancelled.
2720 tic->t_curr_res = 0;
2721 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2722 GRANT_UNLOCK(log, s);
2723 return XFS_ERROR(EIO);
2724 } /* xlog_regrant_write_log_space */
2727 /* The first cnt-1 times through here we don't need to
2728 * move the grant write head because the permanent
2729 * reservation has reserved cnt times the unit amount.
2730 * Release part of current permanent unit reservation and
2731 * reset current reservation to be one units worth. Also
2732 * move grant reservation head forward.
2735 xlog_regrant_reserve_log_space(xlog_t *log,
2736 xlog_ticket_t *ticket)
2740 xlog_trace_loggrant(log, ticket,
2741 "xlog_regrant_reserve_log_space: enter");
2742 if (ticket->t_cnt > 0)
2745 s = GRANT_LOCK(log);
2746 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2747 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2748 ticket->t_curr_res = ticket->t_unit_res;
2749 XLOG_TIC_RESET_RES(ticket);
2750 xlog_trace_loggrant(log, ticket,
2751 "xlog_regrant_reserve_log_space: sub current res");
2752 xlog_verify_grant_head(log, 1);
2754 /* just return if we still have some of the pre-reserved space */
2755 if (ticket->t_cnt > 0) {
2756 GRANT_UNLOCK(log, s);
2760 XLOG_GRANT_ADD_SPACE(log, ticket->t_unit_res, 'r');
2761 xlog_trace_loggrant(log, ticket,
2762 "xlog_regrant_reserve_log_space: exit");
2763 xlog_verify_grant_head(log, 0);
2764 GRANT_UNLOCK(log, s);
2765 ticket->t_curr_res = ticket->t_unit_res;
2766 XLOG_TIC_RESET_RES(ticket);
2767 } /* xlog_regrant_reserve_log_space */
2771 * Give back the space left from a reservation.
2773 * All the information we need to make a correct determination of space left
2774 * is present. For non-permanent reservations, things are quite easy. The
2775 * count should have been decremented to zero. We only need to deal with the
2776 * space remaining in the current reservation part of the ticket. If the
2777 * ticket contains a permanent reservation, there may be left over space which
2778 * needs to be released. A count of N means that N-1 refills of the current
2779 * reservation can be done before we need to ask for more space. The first
2780 * one goes to fill up the first current reservation. Once we run out of
2781 * space, the count will stay at zero and the only space remaining will be
2782 * in the current reservation field.
2785 xlog_ungrant_log_space(xlog_t *log,
2786 xlog_ticket_t *ticket)
2790 if (ticket->t_cnt > 0)
2793 s = GRANT_LOCK(log);
2794 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2796 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2797 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2799 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2801 /* If this is a permanent reservation ticket, we may be able to free
2802 * up more space based on the remaining count.
2804 if (ticket->t_cnt > 0) {
2805 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2806 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'w');
2807 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'r');
2810 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2811 xlog_verify_grant_head(log, 1);
2812 GRANT_UNLOCK(log, s);
2813 xfs_log_move_tail(log->l_mp, 1);
2814 } /* xlog_ungrant_log_space */
2818 * Atomically put back used ticket.
2821 xlog_state_put_ticket(xlog_t *log,
2827 xlog_ticket_put(log, tic);
2829 } /* xlog_state_put_ticket */
2832 * Flush iclog to disk if this is the last reference to the given iclog and
2833 * the WANT_SYNC bit is set.
2835 * When this function is entered, the iclog is not necessarily in the
2836 * WANT_SYNC state. It may be sitting around waiting to get filled.
2841 xlog_state_release_iclog(xlog_t *log,
2842 xlog_in_core_t *iclog)
2845 int sync = 0; /* do we sync? */
2847 xlog_assign_tail_lsn(log->l_mp);
2851 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2853 return XFS_ERROR(EIO);
2856 ASSERT(iclog->ic_refcnt > 0);
2857 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2858 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2860 if (--iclog->ic_refcnt == 0 &&
2861 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2863 iclog->ic_state = XLOG_STATE_SYNCING;
2864 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2865 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2866 /* cycle incremented when incrementing curr_block */
2872 * We let the log lock go, so it's possible that we hit a log I/O
2873 * error or someother SHUTDOWN condition that marks the iclog
2874 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2875 * this iclog has consistent data, so we ignore IOERROR
2876 * flags after this point.
2879 return xlog_sync(log, iclog);
2883 } /* xlog_state_release_iclog */
2887 * This routine will mark the current iclog in the ring as WANT_SYNC
2888 * and move the current iclog pointer to the next iclog in the ring.
2889 * When this routine is called from xlog_state_get_iclog_space(), the
2890 * exact size of the iclog has not yet been determined. All we know is
2891 * that every data block. We have run out of space in this log record.
2894 xlog_state_switch_iclogs(xlog_t *log,
2895 xlog_in_core_t *iclog,
2898 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2900 eventual_size = iclog->ic_offset;
2901 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2902 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2903 log->l_prev_block = log->l_curr_block;
2904 log->l_prev_cycle = log->l_curr_cycle;
2906 /* roll log?: ic_offset changed later */
2907 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2909 /* Round up to next log-sunit */
2910 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2911 log->l_mp->m_sb.sb_logsunit > 1) {
2912 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2913 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2916 if (log->l_curr_block >= log->l_logBBsize) {
2917 log->l_curr_cycle++;
2918 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2919 log->l_curr_cycle++;
2920 log->l_curr_block -= log->l_logBBsize;
2921 ASSERT(log->l_curr_block >= 0);
2923 ASSERT(iclog == log->l_iclog);
2924 log->l_iclog = iclog->ic_next;
2925 } /* xlog_state_switch_iclogs */
2929 * Write out all data in the in-core log as of this exact moment in time.
2931 * Data may be written to the in-core log during this call. However,
2932 * we don't guarantee this data will be written out. A change from past
2933 * implementation means this routine will *not* write out zero length LRs.
2935 * Basically, we try and perform an intelligent scan of the in-core logs.
2936 * If we determine there is no flushable data, we just return. There is no
2937 * flushable data if:
2939 * 1. the current iclog is active and has no data; the previous iclog
2940 * is in the active or dirty state.
2941 * 2. the current iclog is drity, and the previous iclog is in the
2942 * active or dirty state.
2944 * We may sleep (call psema) if:
2946 * 1. the current iclog is not in the active nor dirty state.
2947 * 2. the current iclog dirty, and the previous iclog is not in the
2948 * active nor dirty state.
2949 * 3. the current iclog is active, and there is another thread writing
2950 * to this particular iclog.
2951 * 4. a) the current iclog is active and has no other writers
2952 * b) when we return from flushing out this iclog, it is still
2953 * not in the active nor dirty state.
2956 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2958 xlog_in_core_t *iclog;
2964 iclog = log->l_iclog;
2965 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2967 return XFS_ERROR(EIO);
2970 /* If the head iclog is not active nor dirty, we just attach
2971 * ourselves to the head and go to sleep.
2973 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2974 iclog->ic_state == XLOG_STATE_DIRTY) {
2976 * If the head is dirty or (active and empty), then
2977 * we need to look at the previous iclog. If the previous
2978 * iclog is active or dirty we are done. There is nothing
2979 * to sync out. Otherwise, we attach ourselves to the
2980 * previous iclog and go to sleep.
2982 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2983 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2984 iclog = iclog->ic_prev;
2985 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2986 iclog->ic_state == XLOG_STATE_DIRTY)
2991 if (iclog->ic_refcnt == 0) {
2992 /* We are the only one with access to this
2993 * iclog. Flush it out now. There should
2994 * be a roundoff of zero to show that someone
2995 * has already taken care of the roundoff from
2996 * the previous sync.
2999 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
3000 xlog_state_switch_iclogs(log, iclog, 0);
3003 if (xlog_state_release_iclog(log, iclog))
3004 return XFS_ERROR(EIO);
3007 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
3008 iclog->ic_state != XLOG_STATE_DIRTY)
3013 /* Someone else is writing to this iclog.
3014 * Use its call to flush out the data. However,
3015 * the other thread may not force out this LR,
3016 * so we mark it WANT_SYNC.
3018 xlog_state_switch_iclogs(log, iclog, 0);
3024 /* By the time we come around again, the iclog could've been filled
3025 * which would give it another lsn. If we have a new lsn, just
3026 * return because the relevant data has been flushed.
3029 if (flags & XFS_LOG_SYNC) {
3031 * We must check if we're shutting down here, before
3032 * we wait, while we're holding the LOG_LOCK.
3033 * Then we check again after waking up, in case our
3034 * sleep was disturbed by a bad news.
3036 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3038 return XFS_ERROR(EIO);
3040 XFS_STATS_INC(xs_log_force_sleep);
3041 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3043 * No need to grab the log lock here since we're
3044 * only deciding whether or not to return EIO
3045 * and the memory read should be atomic.
3047 if (iclog->ic_state & XLOG_STATE_IOERROR)
3048 return XFS_ERROR(EIO);
3057 } /* xlog_state_sync_all */
3061 * Used by code which implements synchronous log forces.
3063 * Find in-core log with lsn.
3064 * If it is in the DIRTY state, just return.
3065 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3066 * state and go to sleep or return.
3067 * If it is in any other state, go to sleep or return.
3069 * If filesystem activity goes to zero, the iclog will get flushed only by
3073 xlog_state_sync(xlog_t *log,
3078 xlog_in_core_t *iclog;
3079 int already_slept = 0;
3085 iclog = log->l_iclog;
3087 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3089 return XFS_ERROR(EIO);
3093 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
3094 iclog = iclog->ic_next;
3098 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3103 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3105 * We sleep here if we haven't already slept (e.g.
3106 * this is the first time we've looked at the correct
3107 * iclog buf) and the buffer before us is going to
3108 * be sync'ed. The reason for this is that if we
3109 * are doing sync transactions here, by waiting for
3110 * the previous I/O to complete, we can allow a few
3111 * more transactions into this iclog before we close
3114 * Otherwise, we mark the buffer WANT_SYNC, and bump
3115 * up the refcnt so we can release the log (which drops
3116 * the ref count). The state switch keeps new transaction
3117 * commits from using this buffer. When the current commits
3118 * finish writing into the buffer, the refcount will drop to
3119 * zero and the buffer will go out then.
3121 if (!already_slept &&
3122 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3123 XLOG_STATE_SYNCING))) {
3124 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3125 XFS_STATS_INC(xs_log_force_sleep);
3126 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3127 &log->l_icloglock, s);
3133 xlog_state_switch_iclogs(log, iclog, 0);
3135 if (xlog_state_release_iclog(log, iclog))
3136 return XFS_ERROR(EIO);
3142 if ((flags & XFS_LOG_SYNC) && /* sleep */
3143 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3146 * Don't wait on the forcesema if we know that we've
3147 * gotten a log write error.
3149 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3151 return XFS_ERROR(EIO);
3153 XFS_STATS_INC(xs_log_force_sleep);
3154 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3156 * No need to grab the log lock here since we're
3157 * only deciding whether or not to return EIO
3158 * and the memory read should be atomic.
3160 if (iclog->ic_state & XLOG_STATE_IOERROR)
3161 return XFS_ERROR(EIO);
3163 } else { /* just return */
3168 } while (iclog != log->l_iclog);
3172 } /* xlog_state_sync */
3176 * Called when we want to mark the current iclog as being ready to sync to
3180 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3186 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3187 xlog_state_switch_iclogs(log, iclog, 0);
3189 ASSERT(iclog->ic_state &
3190 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3194 } /* xlog_state_want_sync */
3198 /*****************************************************************************
3202 *****************************************************************************
3206 * Algorithm doesn't take into account page size. ;-(
3209 xlog_state_ticket_alloc(xlog_t *log)
3211 xlog_ticket_t *t_list;
3212 xlog_ticket_t *next;
3214 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3218 * The kmem_zalloc may sleep, so we shouldn't be holding the
3219 * global lock. XXXmiken: may want to use zone allocator.
3221 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3225 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3226 t_list = (xlog_ticket_t *)buf;
3227 t_list->t_next = log->l_unmount_free;
3228 log->l_unmount_free = t_list++;
3229 log->l_ticket_cnt++;
3230 log->l_ticket_tcnt++;
3232 /* Next ticket becomes first ticket attached to ticket free list */
3233 if (log->l_freelist != NULL) {
3234 ASSERT(log->l_tail != NULL);
3235 log->l_tail->t_next = t_list;
3237 log->l_freelist = t_list;
3239 log->l_ticket_cnt++;
3240 log->l_ticket_tcnt++;
3242 /* Cycle through rest of alloc'ed memory, building up free Q */
3243 for ( ; i > 0; i--) {
3245 t_list->t_next = next;
3247 log->l_ticket_cnt++;
3248 log->l_ticket_tcnt++;
3250 t_list->t_next = NULL;
3251 log->l_tail = t_list;
3253 } /* xlog_state_ticket_alloc */
3257 * Put ticket into free list
3259 * Assumption: log lock is held around this call.
3262 xlog_ticket_put(xlog_t *log,
3263 xlog_ticket_t *ticket)
3265 sv_destroy(&ticket->t_sema);
3268 * Don't think caching will make that much difference. It's
3269 * more important to make debug easier.
3272 /* real code will want to use LIFO for caching */
3273 ticket->t_next = log->l_freelist;
3274 log->l_freelist = ticket;
3275 /* no need to clear fields */
3277 /* When we debug, it is easier if tickets are cycled */
3278 ticket->t_next = NULL;
3279 if (log->l_tail != 0) {
3280 log->l_tail->t_next = ticket;
3282 ASSERT(log->l_freelist == 0);
3283 log->l_freelist = ticket;
3285 log->l_tail = ticket;
3287 log->l_ticket_cnt++;
3288 } /* xlog_ticket_put */
3292 * Grab ticket off freelist or allocation some more
3295 xlog_ticket_get(xlog_t *log,
3306 if (log->l_freelist == NULL)
3307 xlog_state_ticket_alloc(log); /* potentially sleep */
3310 if (log->l_freelist == NULL) {
3314 tic = log->l_freelist;
3315 log->l_freelist = tic->t_next;
3316 if (log->l_freelist == NULL)
3318 log->l_ticket_cnt--;
3322 * Permanent reservations have up to 'cnt'-1 active log operations
3323 * in the log. A unit in this case is the amount of space for one
3324 * of these log operations. Normal reservations have a cnt of 1
3325 * and their unit amount is the total amount of space required.
3327 * The following lines of code account for non-transaction data
3328 * which occupy space in the on-disk log.
3330 * Normal form of a transaction is:
3331 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3332 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3334 * We need to account for all the leadup data and trailer data
3335 * around the transaction data.
3336 * And then we need to account for the worst case in terms of using
3338 * The worst case will happen if:
3339 * - the placement of the transaction happens to be such that the
3340 * roundoff is at its maximum
3341 * - the transaction data is synced before the commit record is synced
3342 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3343 * Therefore the commit record is in its own Log Record.
3344 * This can happen as the commit record is called with its
3345 * own region to xlog_write().
3346 * This then means that in the worst case, roundoff can happen for
3347 * the commit-rec as well.
3348 * The commit-rec is smaller than padding in this scenario and so it is
3349 * not added separately.
3352 /* for trans header */
3353 unit_bytes += sizeof(xlog_op_header_t);
3354 unit_bytes += sizeof(xfs_trans_header_t);
3357 unit_bytes += sizeof(xlog_op_header_t);
3359 /* for LR headers */
3360 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3361 unit_bytes += log->l_iclog_hsize * num_headers;
3363 /* for commit-rec LR header - note: padding will subsume the ophdr */
3364 unit_bytes += log->l_iclog_hsize;
3366 /* for split-recs - ophdrs added when data split over LRs */
3367 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3369 /* for roundoff padding for transaction data and one for commit record */
3370 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3371 log->l_mp->m_sb.sb_logsunit > 1) {
3372 /* log su roundoff */
3373 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3376 unit_bytes += 2*BBSIZE;
3379 tic->t_unit_res = unit_bytes;
3380 tic->t_curr_res = unit_bytes;
3383 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3384 tic->t_clientid = client;
3385 tic->t_flags = XLOG_TIC_INITED;
3386 tic->t_trans_type = 0;
3387 if (xflags & XFS_LOG_PERM_RESERV)
3388 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3389 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3391 XLOG_TIC_RESET_RES(tic);
3394 } /* xlog_ticket_get */
3397 /******************************************************************************
3399 * Log debug routines
3401 ******************************************************************************
3403 #if defined(DEBUG) && !defined(XLOG_NOLOG)
3405 * Make sure that the destination ptr is within the valid data region of
3406 * one of the iclogs. This uses backup pointers stored in a different
3407 * part of the log in case we trash the log structure.
3410 xlog_verify_dest_ptr(xlog_t *log,
3416 for (i=0; i < log->l_iclog_bufs; i++) {
3417 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3418 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3422 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3423 } /* xlog_verify_dest_ptr */
3426 xlog_verify_grant_head(xlog_t *log, int equals)
3428 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3430 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3432 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3434 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3435 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3437 } /* xlog_verify_grant_head */
3439 /* check if it will fit */
3441 xlog_verify_tail_lsn(xlog_t *log,
3442 xlog_in_core_t *iclog,
3447 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3449 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3450 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3451 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3453 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3455 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3456 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3458 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3459 if (blocks < BTOBB(iclog->ic_offset) + 1)
3460 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3462 } /* xlog_verify_tail_lsn */
3465 * Perform a number of checks on the iclog before writing to disk.
3467 * 1. Make sure the iclogs are still circular
3468 * 2. Make sure we have a good magic number
3469 * 3. Make sure we don't have magic numbers in the data
3470 * 4. Check fields of each log operation header for:
3471 * A. Valid client identifier
3472 * B. tid ptr value falls in valid ptr space (user space code)
3473 * C. Length in log record header is correct according to the
3474 * individual operation headers within record.
3475 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3476 * log, check the preceding blocks of the physical log to make sure all
3477 * the cycle numbers agree with the current cycle number.
3480 xlog_verify_iclog(xlog_t *log,
3481 xlog_in_core_t *iclog,
3485 xlog_op_header_t *ophead;
3486 xlog_in_core_t *icptr;
3487 xlog_in_core_2_t *xhdr;
3489 xfs_caddr_t base_ptr;
3490 __psint_t field_offset;
3492 int len, i, j, k, op_len;
3496 /* check validity of iclog pointers */
3498 icptr = log->l_iclog;
3499 for (i=0; i < log->l_iclog_bufs; i++) {
3501 xlog_panic("xlog_verify_iclog: invalid ptr");
3502 icptr = icptr->ic_next;
3504 if (icptr != log->l_iclog)
3505 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3508 /* check log magic numbers */
3509 ptr = (xfs_caddr_t) &(iclog->ic_header);
3510 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3511 xlog_panic("xlog_verify_iclog: invalid magic num");
3513 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3515 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3516 xlog_panic("xlog_verify_iclog: unexpected magic num");
3520 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3521 ptr = iclog->ic_datap;
3523 ophead = (xlog_op_header_t *)ptr;
3524 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3525 for (i = 0; i < len; i++) {
3526 ophead = (xlog_op_header_t *)ptr;
3528 /* clientid is only 1 byte */
3529 field_offset = (__psint_t)
3530 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3531 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3532 clientid = ophead->oh_clientid;
3534 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3535 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3536 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3537 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3538 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3540 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3543 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3544 cmn_err(CE_WARN, "xlog_verify_iclog: "
3545 "invalid clientid %d op 0x%p offset 0x%lx",
3546 clientid, ophead, (unsigned long)field_offset);
3549 field_offset = (__psint_t)
3550 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3551 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3552 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3554 idx = BTOBBT((__psint_t)&ophead->oh_len -
3555 (__psint_t)iclog->ic_datap);
3556 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3557 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3558 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3559 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3561 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3564 ptr += sizeof(xlog_op_header_t) + op_len;
3566 } /* xlog_verify_iclog */
3567 #endif /* DEBUG && !XLOG_NOLOG */
3570 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3576 xlog_in_core_t *iclog, *ic;
3578 iclog = log->l_iclog;
3579 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3581 * Mark all the incore logs IOERROR.
3582 * From now on, no log flushes will result.
3586 ic->ic_state = XLOG_STATE_IOERROR;
3588 } while (ic != iclog);
3592 * Return non-zero, if state transition has already happened.
3598 * This is called from xfs_force_shutdown, when we're forcibly
3599 * shutting down the filesystem, typically because of an IO error.
3600 * Our main objectives here are to make sure that:
3601 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3602 * parties to find out, 'atomically'.
3603 * b. those who're sleeping on log reservations, pinned objects and
3604 * other resources get woken up, and be told the bad news.
3605 * c. nothing new gets queued up after (a) and (b) are done.
3606 * d. if !logerror, flush the iclogs to disk, then seal them off
3610 xfs_log_force_umount(
3611 struct xfs_mount *mp,
3624 * If this happens during log recovery, don't worry about
3625 * locking; the log isn't open for business yet.
3628 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3629 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3630 XFS_BUF_DONE(mp->m_sb_bp);
3635 * Somebody could've already done the hard work for us.
3636 * No need to get locks for this.
3638 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3639 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3644 * We must hold both the GRANT lock and the LOG lock,
3645 * before we mark the filesystem SHUTDOWN and wake
3646 * everybody up to tell the bad news.
3648 s = GRANT_LOCK(log);
3650 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3651 XFS_BUF_DONE(mp->m_sb_bp);
3653 * This flag is sort of redundant because of the mount flag, but
3654 * it's good to maintain the separation between the log and the rest
3657 log->l_flags |= XLOG_IO_ERROR;
3660 * If we hit a log error, we want to mark all the iclogs IOERROR
3661 * while we're still holding the loglock.
3664 retval = xlog_state_ioerror(log);
3665 LOG_UNLOCK(log, s2);
3668 * We don't want anybody waiting for log reservations
3669 * after this. That means we have to wake up everybody
3670 * queued up on reserve_headq as well as write_headq.
3671 * In addition, we make sure in xlog_{re}grant_log_space
3672 * that we don't enqueue anything once the SHUTDOWN flag
3673 * is set, and this action is protected by the GRANTLOCK.
3675 if ((tic = log->l_reserve_headq)) {
3677 sv_signal(&tic->t_sema);
3679 } while (tic != log->l_reserve_headq);
3682 if ((tic = log->l_write_headq)) {
3684 sv_signal(&tic->t_sema);
3686 } while (tic != log->l_write_headq);
3688 GRANT_UNLOCK(log, s);
3690 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3693 * Force the incore logs to disk before shutting the
3694 * log down completely.
3696 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3698 retval = xlog_state_ioerror(log);
3699 LOG_UNLOCK(log, s2);
3702 * Wake up everybody waiting on xfs_log_force.
3703 * Callback all log item committed functions as if the
3704 * log writes were completed.
3706 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3708 #ifdef XFSERRORDEBUG
3710 xlog_in_core_t *iclog;
3713 iclog = log->l_iclog;
3715 ASSERT(iclog->ic_callback == 0);
3716 iclog = iclog->ic_next;
3717 } while (iclog != log->l_iclog);
3721 /* return non-zero if log IOERROR transition had already happened */
3726 xlog_iclogs_empty(xlog_t *log)
3728 xlog_in_core_t *iclog;
3730 iclog = log->l_iclog;
3732 /* endianness does not matter here, zero is zero in
3735 if (iclog->ic_header.h_num_logops)
3737 iclog = iclog->ic_next;
3738 } while (iclog != log->l_iclog);