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, "logdev: %s", mp->m_logname ?
487 mp->m_logname : "internal");
492 * skip log recovery on a norecovery mount. pretend it all
495 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
497 vfs_t *vfsp = XFS_MTOVFS(mp);
498 int readonly = (vfsp->vfs_flag & VFS_RDONLY);
501 vfsp->vfs_flag &= ~VFS_RDONLY;
503 error = xlog_recover(mp->m_log, readonly);
506 vfsp->vfs_flag |= VFS_RDONLY;
508 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
509 xlog_unalloc_log(mp->m_log);
514 /* Normal transactions can now occur */
515 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
517 /* End mounting message in xfs_log_mount_finish */
519 } /* xfs_log_mount */
522 * Finish the recovery of the file system. This is separate from
523 * the xfs_log_mount() call, because it depends on the code in
524 * xfs_mountfs() to read in the root and real-time bitmap inodes
525 * between calling xfs_log_mount() and here.
527 * mp - ubiquitous xfs mount point structure
530 xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
534 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
535 error = xlog_recover_finish(mp->m_log, mfsi_flags);
538 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
545 * Unmount processing for the log.
548 xfs_log_unmount(xfs_mount_t *mp)
552 error = xfs_log_unmount_write(mp);
553 xfs_log_unmount_dealloc(mp);
558 * Final log writes as part of unmount.
560 * Mark the filesystem clean as unmount happens. Note that during relocation
561 * this routine needs to be executed as part of source-bag while the
562 * deallocation must not be done until source-end.
566 * Unmount record used to have a string "Unmount filesystem--" in the
567 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
568 * We just write the magic number now since that particular field isn't
569 * currently architecture converted and "nUmount" is a bit foo.
570 * As far as I know, there weren't any dependencies on the old behaviour.
574 xfs_log_unmount_write(xfs_mount_t *mp)
576 xlog_t *log = mp->m_log;
577 xlog_in_core_t *iclog;
579 xlog_in_core_t *first_iclog;
581 xfs_log_iovec_t reg[1];
582 xfs_log_ticket_t tic = NULL;
587 /* the data section must be 32 bit size aligned */
591 __uint32_t pad2; /* may as well make it 64 bits */
592 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
594 #if defined(DEBUG) || defined(XLOG_NOLOG)
595 if (!xlog_debug && xlog_target == log->l_targ)
600 * Don't write out unmount record on read-only mounts.
601 * Or, if we are doing a forced umount (typically because of IO errors).
603 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
606 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
609 first_iclog = iclog = log->l_iclog;
611 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
612 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
613 ASSERT(iclog->ic_offset == 0);
615 iclog = iclog->ic_next;
616 } while (iclog != first_iclog);
618 if (! (XLOG_FORCED_SHUTDOWN(log))) {
619 reg[0].i_addr = (void*)&magic;
620 reg[0].i_len = sizeof(magic);
621 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
623 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0, 0);
625 /* remove inited flag */
626 ((xlog_ticket_t *)tic)->t_flags = 0;
627 error = xlog_write(mp, reg, 1, tic, &lsn,
628 NULL, XLOG_UNMOUNT_TRANS);
630 * At this point, we're umounting anyway,
631 * so there's no point in transitioning log state
632 * to IOERROR. Just continue...
637 xfs_fs_cmn_err(CE_ALERT, mp,
638 "xfs_log_unmount: unmount record failed");
643 iclog = log->l_iclog;
646 xlog_state_want_sync(log, iclog);
647 (void) xlog_state_release_iclog(log, iclog);
650 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
651 iclog->ic_state == XLOG_STATE_DIRTY)) {
652 if (!XLOG_FORCED_SHUTDOWN(log)) {
653 sv_wait(&iclog->ic_forcesema, PMEM,
654 &log->l_icloglock, s);
662 xlog_state_put_ticket(log, tic);
665 * We're already in forced_shutdown mode, couldn't
666 * even attempt to write out the unmount transaction.
668 * Go through the motions of sync'ing and releasing
669 * the iclog, even though no I/O will actually happen,
670 * we need to wait for other log I/O's that may already
671 * be in progress. Do this as a separate section of
672 * code so we'll know if we ever get stuck here that
673 * we're in this odd situation of trying to unmount
674 * a file system that went into forced_shutdown as
675 * the result of an unmount..
678 iclog = log->l_iclog;
682 xlog_state_want_sync(log, iclog);
683 (void) xlog_state_release_iclog(log, iclog);
687 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
688 || iclog->ic_state == XLOG_STATE_DIRTY
689 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
691 sv_wait(&iclog->ic_forcesema, PMEM,
692 &log->l_icloglock, s);
699 } /* xfs_log_unmount_write */
702 * Deallocate log structures for unmount/relocation.
705 xfs_log_unmount_dealloc(xfs_mount_t *mp)
707 xlog_unalloc_log(mp->m_log);
711 * Write region vectors to log. The write happens using the space reservation
712 * of the ticket (tic). It is not a requirement that all writes for a given
713 * transaction occur with one call to xfs_log_write().
716 xfs_log_write(xfs_mount_t * mp,
717 xfs_log_iovec_t reg[],
719 xfs_log_ticket_t tic,
720 xfs_lsn_t *start_lsn)
723 xlog_t *log = mp->m_log;
725 #if defined(DEBUG) || defined(XLOG_NOLOG)
726 if (!xlog_debug && xlog_target == log->l_targ) {
731 if (XLOG_FORCED_SHUTDOWN(log))
732 return XFS_ERROR(EIO);
734 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
735 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
738 } /* xfs_log_write */
742 xfs_log_move_tail(xfs_mount_t *mp,
746 xlog_t *log = mp->m_log;
747 int need_bytes, free_bytes, cycle, bytes;
750 #if defined(DEBUG) || defined(XLOG_NOLOG)
751 if (!xlog_debug && xlog_target == log->l_targ)
755 if (XLOG_FORCED_SHUTDOWN(log))
757 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
760 /* needed since sync_lsn is 64 bits */
762 tail_lsn = log->l_last_sync_lsn;
768 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
772 log->l_tail_lsn = tail_lsn;
775 if ((tic = log->l_write_headq)) {
777 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
778 panic("Recovery problem");
780 cycle = log->l_grant_write_cycle;
781 bytes = log->l_grant_write_bytes;
782 free_bytes = xlog_space_left(log, cycle, bytes);
784 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
786 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
789 free_bytes -= tic->t_unit_res;
790 sv_signal(&tic->t_sema);
792 } while (tic != log->l_write_headq);
794 if ((tic = log->l_reserve_headq)) {
796 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
797 panic("Recovery problem");
799 cycle = log->l_grant_reserve_cycle;
800 bytes = log->l_grant_reserve_bytes;
801 free_bytes = xlog_space_left(log, cycle, bytes);
803 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
804 need_bytes = tic->t_unit_res*tic->t_cnt;
806 need_bytes = tic->t_unit_res;
807 if (free_bytes < need_bytes && tail_lsn != 1)
810 free_bytes -= need_bytes;
811 sv_signal(&tic->t_sema);
813 } while (tic != log->l_reserve_headq);
815 GRANT_UNLOCK(log, s);
816 } /* xfs_log_move_tail */
819 * Determine if we have a transaction that has gone to disk
820 * that needs to be covered. Log activity needs to be idle (no AIL and
821 * nothing in the iclogs). And, we need to be in the right state indicating
822 * something has gone out.
825 xfs_log_need_covered(xfs_mount_t *mp)
829 xlog_t *log = mp->m_log;
830 vfs_t *vfsp = XFS_MTOVFS(mp);
832 if (fs_frozen(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
833 (vfsp->vfs_flag & VFS_RDONLY))
837 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
838 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
839 && !xfs_trans_first_ail(mp, &gen)
840 && xlog_iclogs_empty(log)) {
841 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
842 log->l_covered_state = XLOG_STATE_COVER_DONE;
844 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
845 log->l_covered_state = XLOG_STATE_COVER_DONE2;
853 /******************************************************************************
857 ******************************************************************************
860 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
861 * The log manager must keep track of the last LR which was committed
862 * to disk. The lsn of this LR will become the new tail_lsn whenever
863 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
864 * the situation where stuff could be written into the log but nothing
865 * was ever in the AIL when asked. Eventually, we panic since the
866 * tail hits the head.
868 * We may be holding the log iclog lock upon entering this routine.
871 xlog_assign_tail_lsn(xfs_mount_t *mp)
875 xlog_t *log = mp->m_log;
877 tail_lsn = xfs_trans_tail_ail(mp);
880 log->l_tail_lsn = tail_lsn;
882 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
884 GRANT_UNLOCK(log, s);
887 } /* xlog_assign_tail_lsn */
891 * Return the space in the log between the tail and the head. The head
892 * is passed in the cycle/bytes formal parms. In the special case where
893 * the reserve head has wrapped passed the tail, this calculation is no
894 * longer valid. In this case, just return 0 which means there is no space
895 * in the log. This works for all places where this function is called
896 * with the reserve head. Of course, if the write head were to ever
897 * wrap the tail, we should blow up. Rather than catch this case here,
898 * we depend on other ASSERTions in other parts of the code. XXXmiken
900 * This code also handles the case where the reservation head is behind
901 * the tail. The details of this case are described below, but the end
902 * result is that we return the size of the log as the amount of space left.
905 xlog_space_left(xlog_t *log, int cycle, int bytes)
911 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
912 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
913 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
914 free_bytes = log->l_logsize - (bytes - tail_bytes);
915 } else if ((tail_cycle + 1) < cycle) {
917 } else if (tail_cycle < cycle) {
918 ASSERT(tail_cycle == (cycle - 1));
919 free_bytes = tail_bytes - bytes;
922 * The reservation head is behind the tail.
923 * In this case we just want to return the size of the
924 * log as the amount of space left.
926 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
927 "xlog_space_left: head behind tail\n"
928 " tail_cycle = %d, tail_bytes = %d\n"
929 " GH cycle = %d, GH bytes = %d",
930 tail_cycle, tail_bytes, cycle, bytes);
932 free_bytes = log->l_logsize;
935 } /* xlog_space_left */
939 * Log function which is called when an io completes.
941 * The log manager needs its own routine, in order to control what
942 * happens with the buffer after the write completes.
945 xlog_iodone(xfs_buf_t *bp)
947 xlog_in_core_t *iclog;
951 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
952 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
953 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
957 * Some versions of cpp barf on the recursive definition of
958 * ic_log -> hic_fields.ic_log and expand ic_log twice when
959 * it is passed through two macros. Workaround broken cpp.
964 * Race to shutdown the filesystem if we see an error.
966 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
967 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
968 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
970 xfs_force_shutdown(l->l_mp, XFS_LOG_IO_ERROR);
972 * This flag will be propagated to the trans-committed
973 * callback routines to let them know that the log-commit
976 aborted = XFS_LI_ABORTED;
977 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
978 aborted = XFS_LI_ABORTED;
980 xlog_state_done_syncing(iclog, aborted);
981 if (!(XFS_BUF_ISASYNC(bp))) {
983 * Corresponding psema() will be done in bwrite(). If we don't
984 * vsema() here, panic.
986 XFS_BUF_V_IODONESEMA(bp);
991 * The bdstrat callback function for log bufs. This gives us a central
992 * place to trap bufs in case we get hit by a log I/O error and need to
993 * shutdown. Actually, in practice, even when we didn't get a log error,
994 * we transition the iclogs to IOERROR state *after* flushing all existing
995 * iclogs to disk. This is because we don't want anymore new transactions to be
996 * started or completed afterwards.
999 xlog_bdstrat_cb(struct xfs_buf *bp)
1001 xlog_in_core_t *iclog;
1003 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1005 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1006 /* note for irix bstrat will need struct bdevsw passed
1007 * Fix the following macro if the code ever is merged
1013 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1014 XFS_BUF_ERROR(bp, EIO);
1017 return (XFS_ERROR(EIO));
1023 * Return size of each in-core log record buffer.
1025 * Low memory machines only get 2 16KB buffers. We don't want to waste
1026 * memory here. However, all other machines get at least 2 32KB buffers.
1027 * The number is hard coded because we don't care about the minimum
1028 * memory size, just 32MB systems.
1030 * If the filesystem blocksize is too large, we may need to choose a
1031 * larger size since the directory code currently logs entire blocks.
1035 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1041 #if defined(DEBUG) || defined(XLOG_NOLOG)
1043 * When logbufs == 0, someone has disabled the log from the FSTAB
1044 * file. This is not a documented feature. We need to set xlog_debug
1045 * to zero (this deactivates the log) and set xlog_target to the
1046 * appropriate device. Only one filesystem may be affected as such
1047 * since this is just a performance hack to test what we might be able
1048 * to get if the log were not present.
1050 if (mp->m_logbufs == 0) {
1052 xlog_target = log->l_targ;
1053 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1058 * This is the normal path. If m_logbufs == -1, then the
1059 * admin has chosen to use the system defaults for logbuffers.
1061 if (mp->m_logbufs == -1) {
1062 if (xfs_physmem <= btoc(128*1024*1024)) {
1063 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1064 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1065 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1067 /* 256K with 32K bufs */
1068 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1071 log->l_iclog_bufs = mp->m_logbufs;
1073 #if defined(DEBUG) || defined(XLOG_NOLOG)
1074 /* We are reactivating a filesystem after it was inactive */
1075 if (log->l_targ == xlog_target) {
1083 * Buffer size passed in from mount system call.
1085 if (mp->m_logbsize != -1) {
1086 size = log->l_iclog_size = mp->m_logbsize;
1087 log->l_iclog_size_log = 0;
1089 log->l_iclog_size_log++;
1093 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1094 /* # headers = size / 32K
1095 * one header holds cycles from 32K of data
1098 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1099 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1101 log->l_iclog_hsize = xhdrs << BBSHIFT;
1102 log->l_iclog_heads = xhdrs;
1104 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1105 log->l_iclog_hsize = BBSIZE;
1106 log->l_iclog_heads = 1;
1112 * Special case machines that have less than 32MB of memory.
1113 * All machines with more memory use 32KB buffers.
1115 if (xfs_physmem <= btoc(32*1024*1024)) {
1116 /* Don't change; min configuration */
1117 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1118 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1120 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1121 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1124 /* the default log size is 16k or 32k which is one header sector */
1125 log->l_iclog_hsize = BBSIZE;
1126 log->l_iclog_heads = 1;
1129 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1130 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1132 if (mp->m_sb.sb_blocksize >= 16*1024) {
1133 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1134 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1135 if (mp->m_logbufs == -1) {
1136 switch (mp->m_sb.sb_blocksize) {
1137 case 16*1024: /* 16 KB */
1138 log->l_iclog_bufs = 3;
1140 case 32*1024: /* 32 KB */
1141 log->l_iclog_bufs = 4;
1143 case 64*1024: /* 64 KB */
1144 log->l_iclog_bufs = 8;
1147 xlog_panic("XFS: Invalid blocksize");
1152 } /* xlog_get_iclog_buffer_size */
1156 * This routine initializes some of the log structure for a given mount point.
1157 * Its primary purpose is to fill in enough, so recovery can occur. However,
1158 * some other stuff may be filled in too.
1161 xlog_alloc_log(xfs_mount_t *mp,
1162 xfs_buftarg_t *log_target,
1163 xfs_daddr_t blk_offset,
1167 xlog_rec_header_t *head;
1168 xlog_in_core_t **iclogp;
1169 xlog_in_core_t *iclog, *prev_iclog=NULL;
1174 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1177 log->l_targ = log_target;
1178 log->l_logsize = BBTOB(num_bblks);
1179 log->l_logBBstart = blk_offset;
1180 log->l_logBBsize = num_bblks;
1181 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1182 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1184 log->l_prev_block = -1;
1185 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1186 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1187 log->l_last_sync_lsn = log->l_tail_lsn;
1188 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1189 log->l_grant_reserve_cycle = 1;
1190 log->l_grant_write_cycle = 1;
1192 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1193 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1194 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1195 /* for larger sector sizes, must have v2 or external log */
1196 ASSERT(log->l_sectbb_log == 0 ||
1197 log->l_logBBstart == 0 ||
1198 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1199 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1201 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1203 xlog_get_iclog_buffer_size(mp, log);
1205 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1206 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1207 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1208 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1209 ASSERT(XFS_BUF_ISBUSY(bp));
1210 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1213 spinlock_init(&log->l_icloglock, "iclog");
1214 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1215 initnsema(&log->l_flushsema, 0, "ic-flush");
1216 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1218 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1219 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1221 iclogp = &log->l_iclog;
1223 * The amount of memory to allocate for the iclog structure is
1224 * rather funky due to the way the structure is defined. It is
1225 * done this way so that we can use different sizes for machines
1226 * with different amounts of memory. See the definition of
1227 * xlog_in_core_t in xfs_log_priv.h for details.
1229 iclogsize = log->l_iclog_size;
1230 ASSERT(log->l_iclog_size >= 4096);
1231 for (i=0; i < log->l_iclog_bufs; i++) {
1232 *iclogp = (xlog_in_core_t *)
1233 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1235 iclog->hic_data = (xlog_in_core_2_t *)
1236 kmem_zalloc(iclogsize, KM_SLEEP);
1238 iclog->ic_prev = prev_iclog;
1240 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1242 head = &iclog->ic_header;
1243 memset(head, 0, sizeof(xlog_rec_header_t));
1244 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1245 INT_SET(head->h_version, ARCH_CONVERT,
1246 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1247 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1249 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1250 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1252 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1253 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1254 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1255 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1258 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1259 iclog->ic_state = XLOG_STATE_ACTIVE;
1260 iclog->ic_log = log;
1261 iclog->ic_callback_tail = &(iclog->ic_callback);
1262 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1264 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1265 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1266 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1267 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1269 iclogp = &iclog->ic_next;
1271 *iclogp = log->l_iclog; /* complete ring */
1272 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1275 } /* xlog_alloc_log */
1279 * Write out the commit record of a transaction associated with the given
1280 * ticket. Return the lsn of the commit record.
1283 xlog_commit_record(xfs_mount_t *mp,
1284 xlog_ticket_t *ticket,
1285 xlog_in_core_t **iclog,
1286 xfs_lsn_t *commitlsnp)
1289 xfs_log_iovec_t reg[1];
1291 reg[0].i_addr = NULL;
1293 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_COMMIT);
1295 ASSERT_ALWAYS(iclog);
1296 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1297 iclog, XLOG_COMMIT_TRANS))) {
1298 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
1301 } /* xlog_commit_record */
1305 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1306 * log space. This code pushes on the lsn which would supposedly free up
1307 * the 25% which we want to leave free. We may need to adopt a policy which
1308 * pushes on an lsn which is further along in the log once we reach the high
1309 * water mark. In this manner, we would be creating a low water mark.
1312 xlog_grant_push_ail(xfs_mount_t *mp,
1315 xlog_t *log = mp->m_log; /* pointer to the log */
1316 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1317 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1318 int free_blocks; /* free blocks left to write to */
1319 int free_bytes; /* free bytes left to write to */
1320 int threshold_block; /* block in lsn we'd like to be at */
1321 int threshold_cycle; /* lsn cycle we'd like to be at */
1325 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1327 s = GRANT_LOCK(log);
1328 free_bytes = xlog_space_left(log,
1329 log->l_grant_reserve_cycle,
1330 log->l_grant_reserve_bytes);
1331 tail_lsn = log->l_tail_lsn;
1332 free_blocks = BTOBBT(free_bytes);
1335 * Set the threshold for the minimum number of free blocks in the
1336 * log to the maximum of what the caller needs, one quarter of the
1337 * log, and 256 blocks.
1339 free_threshold = BTOBB(need_bytes);
1340 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1341 free_threshold = MAX(free_threshold, 256);
1342 if (free_blocks < free_threshold) {
1343 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1344 threshold_cycle = CYCLE_LSN(tail_lsn);
1345 if (threshold_block >= log->l_logBBsize) {
1346 threshold_block -= log->l_logBBsize;
1347 threshold_cycle += 1;
1349 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1352 /* Don't pass in an lsn greater than the lsn of the last
1353 * log record known to be on disk.
1355 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1356 threshold_lsn = log->l_last_sync_lsn;
1358 GRANT_UNLOCK(log, s);
1361 * Get the transaction layer to kick the dirty buffers out to
1362 * disk asynchronously. No point in trying to do this if
1363 * the filesystem is shutting down.
1365 if (threshold_lsn &&
1366 !XLOG_FORCED_SHUTDOWN(log))
1367 xfs_trans_push_ail(mp, threshold_lsn);
1368 } /* xlog_grant_push_ail */
1372 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1373 * fashion. Previously, we should have moved the current iclog
1374 * ptr in the log to point to the next available iclog. This allows further
1375 * write to continue while this code syncs out an iclog ready to go.
1376 * Before an in-core log can be written out, the data section must be scanned
1377 * to save away the 1st word of each BBSIZE block into the header. We replace
1378 * it with the current cycle count. Each BBSIZE block is tagged with the
1379 * cycle count because there in an implicit assumption that drives will
1380 * guarantee that entire 512 byte blocks get written at once. In other words,
1381 * we can't have part of a 512 byte block written and part not written. By
1382 * tagging each block, we will know which blocks are valid when recovering
1383 * after an unclean shutdown.
1385 * This routine is single threaded on the iclog. No other thread can be in
1386 * this routine with the same iclog. Changing contents of iclog can there-
1387 * fore be done without grabbing the state machine lock. Updating the global
1388 * log will require grabbing the lock though.
1390 * The entire log manager uses a logical block numbering scheme. Only
1391 * log_sync (and then only bwrite()) know about the fact that the log may
1392 * not start with block zero on a given device. The log block start offset
1393 * is added immediately before calling bwrite().
1397 xlog_sync(xlog_t *log,
1398 xlog_in_core_t *iclog)
1400 xfs_caddr_t dptr; /* pointer to byte sized element */
1403 uint count; /* byte count of bwrite */
1404 uint count_init; /* initial count before roundup */
1405 int roundoff; /* roundoff to BB or stripe */
1406 int split = 0; /* split write into two regions */
1409 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1411 XFS_STATS_INC(xs_log_writes);
1412 ASSERT(iclog->ic_refcnt == 0);
1414 /* Add for LR header */
1415 count_init = log->l_iclog_hsize + iclog->ic_offset;
1417 /* Round out the log write size */
1418 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1419 /* we have a v2 stripe unit to use */
1420 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1422 count = BBTOB(BTOBB(count_init));
1424 roundoff = count - count_init;
1425 ASSERT(roundoff >= 0);
1426 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1427 roundoff < log->l_mp->m_sb.sb_logsunit)
1429 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1430 roundoff < BBTOB(1)));
1432 /* move grant heads by roundoff in sync */
1433 s = GRANT_LOCK(log);
1434 XLOG_GRANT_ADD_SPACE(log, roundoff, 'w');
1435 XLOG_GRANT_ADD_SPACE(log, roundoff, 'r');
1436 GRANT_UNLOCK(log, s);
1438 /* put cycle number in every block */
1439 xlog_pack_data(log, iclog, roundoff);
1441 /* real byte length */
1443 INT_SET(iclog->ic_header.h_len,
1445 iclog->ic_offset + roundoff);
1447 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1450 /* put ops count in correct order */
1451 ops = iclog->ic_header.h_num_logops;
1452 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1455 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1456 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1457 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1459 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1461 /* Do we need to split this write into 2 parts? */
1462 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1463 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1464 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1465 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1467 iclog->ic_bwritecnt = 1;
1469 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1470 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1474 * Do an ordered write for the log block.
1476 * It may not be needed to flush the first split block in the log wrap
1477 * case, but do it anyways to be safe -AK
1479 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1480 XFS_BUF_ORDERED(bp);
1482 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1483 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1485 xlog_verify_iclog(log, iclog, count, B_TRUE);
1487 /* account for log which doesn't start at block #0 */
1488 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1490 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1495 if ((error = XFS_bwrite(bp))) {
1496 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1501 bp = iclog->ic_log->l_xbuf;
1502 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1504 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1505 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1506 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1507 (__psint_t)count), split);
1508 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1511 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1512 XFS_BUF_ORDERED(bp);
1513 dptr = XFS_BUF_PTR(bp);
1515 * Bump the cycle numbers at the start of each block
1516 * since this part of the buffer is at the start of
1517 * a new cycle. Watch out for the header magic number
1520 for (i=0; i<split; i += BBSIZE) {
1521 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1522 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1523 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1527 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1528 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1530 /* account for internal log which does't start at block #0 */
1531 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1533 if ((error = XFS_bwrite(bp))) {
1534 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1535 bp, XFS_BUF_ADDR(bp));
1544 * Unallocate a log structure
1547 xlog_unalloc_log(xlog_t *log)
1549 xlog_in_core_t *iclog, *next_iclog;
1550 xlog_ticket_t *tic, *next_tic;
1554 iclog = log->l_iclog;
1555 for (i=0; i<log->l_iclog_bufs; i++) {
1556 sv_destroy(&iclog->ic_forcesema);
1557 sv_destroy(&iclog->ic_writesema);
1558 xfs_buf_free(iclog->ic_bp);
1559 #ifdef XFS_LOG_TRACE
1560 if (iclog->ic_trace != NULL) {
1561 ktrace_free(iclog->ic_trace);
1564 next_iclog = iclog->ic_next;
1565 kmem_free(iclog->hic_data, log->l_iclog_size);
1566 kmem_free(iclog, sizeof(xlog_in_core_t));
1569 freesema(&log->l_flushsema);
1570 spinlock_destroy(&log->l_icloglock);
1571 spinlock_destroy(&log->l_grant_lock);
1573 /* XXXsup take a look at this again. */
1574 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1575 !XLOG_FORCED_SHUTDOWN(log)) {
1576 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1577 "xlog_unalloc_log: (cnt: %d, total: %d)",
1578 log->l_ticket_cnt, log->l_ticket_tcnt);
1579 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1582 tic = log->l_unmount_free;
1584 next_tic = tic->t_next;
1585 kmem_free(tic, NBPP);
1589 xfs_buf_free(log->l_xbuf);
1590 #ifdef XFS_LOG_TRACE
1591 if (log->l_trace != NULL) {
1592 ktrace_free(log->l_trace);
1594 if (log->l_grant_trace != NULL) {
1595 ktrace_free(log->l_grant_trace);
1598 log->l_mp->m_log = NULL;
1599 kmem_free(log, sizeof(xlog_t));
1600 } /* xlog_unalloc_log */
1603 * Update counters atomically now that memcpy is done.
1607 xlog_state_finish_copy(xlog_t *log,
1608 xlog_in_core_t *iclog,
1616 iclog->ic_header.h_num_logops += record_cnt;
1617 iclog->ic_offset += copy_bytes;
1620 } /* xlog_state_finish_copy */
1626 * print out info relating to regions written which consume
1629 #if defined(XFS_LOG_RES_DEBUG)
1631 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1634 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1636 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1637 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1658 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1701 xfs_fs_cmn_err(CE_WARN, mp,
1702 "xfs_log_write: reservation summary:\n"
1703 " trans type = %s (%u)\n"
1704 " unit res = %d bytes\n"
1705 " current res = %d bytes\n"
1706 " total reg = %u bytes (o/flow = %u bytes)\n"
1707 " ophdrs = %u (ophdr space = %u bytes)\n"
1708 " ophdr + reg = %u bytes\n"
1709 " num regions = %u\n",
1710 ((ticket->t_trans_type <= 0 ||
1711 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1712 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1713 ticket->t_trans_type,
1716 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1717 ticket->t_res_num_ophdrs, ophdr_spc,
1718 ticket->t_res_arr_sum +
1719 ticket->t_res_o_flow + ophdr_spc,
1722 for (i = 0; i < ticket->t_res_num; i++) {
1723 uint r_type = ticket->t_res_arr[i].r_type;
1725 "region[%u]: %s - %u bytes\n",
1727 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1728 "bad-rtype" : res_type_str[r_type-1]),
1729 ticket->t_res_arr[i].r_len);
1733 #define xlog_print_tic_res(mp, ticket)
1737 * Write some region out to in-core log
1739 * This will be called when writing externally provided regions or when
1740 * writing out a commit record for a given transaction.
1742 * General algorithm:
1743 * 1. Find total length of this write. This may include adding to the
1744 * lengths passed in.
1745 * 2. Check whether we violate the tickets reservation.
1746 * 3. While writing to this iclog
1747 * A. Reserve as much space in this iclog as can get
1748 * B. If this is first write, save away start lsn
1749 * C. While writing this region:
1750 * 1. If first write of transaction, write start record
1751 * 2. Write log operation header (header per region)
1752 * 3. Find out if we can fit entire region into this iclog
1753 * 4. Potentially, verify destination memcpy ptr
1754 * 5. Memcpy (partial) region
1755 * 6. If partial copy, release iclog; otherwise, continue
1756 * copying more regions into current iclog
1757 * 4. Mark want sync bit (in simulation mode)
1758 * 5. Release iclog for potential flush to on-disk log.
1761 * 1. Panic if reservation is overrun. This should never happen since
1762 * reservation amounts are generated internal to the filesystem.
1764 * 1. Tickets are single threaded data structures.
1765 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1766 * syncing routine. When a single log_write region needs to span
1767 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1768 * on all log operation writes which don't contain the end of the
1769 * region. The XLOG_END_TRANS bit is used for the in-core log
1770 * operation which contains the end of the continued log_write region.
1771 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1772 * we don't really know exactly how much space will be used. As a result,
1773 * we don't update ic_offset until the end when we know exactly how many
1774 * bytes have been written out.
1777 xlog_write(xfs_mount_t * mp,
1778 xfs_log_iovec_t reg[],
1780 xfs_log_ticket_t tic,
1781 xfs_lsn_t *start_lsn,
1782 xlog_in_core_t **commit_iclog,
1785 xlog_t *log = mp->m_log;
1786 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1787 xlog_op_header_t *logop_head; /* ptr to log operation header */
1788 xlog_in_core_t *iclog; /* ptr to current in-core log */
1789 __psint_t ptr; /* copy address into data region */
1790 int len; /* # xlog_write() bytes 2 still copy */
1791 int index; /* region index currently copying */
1792 int log_offset; /* offset (from 0) into data region */
1793 int start_rec_copy; /* # bytes to copy for start record */
1794 int partial_copy; /* did we split a region? */
1795 int partial_copy_len;/* # bytes copied if split region */
1796 int need_copy; /* # bytes need to memcpy this region */
1797 int copy_len; /* # bytes actually memcpy'ing */
1798 int copy_off; /* # bytes from entry start */
1799 int contwr; /* continued write of in-core log? */
1801 int record_cnt = 0, data_cnt = 0;
1803 partial_copy_len = partial_copy = 0;
1805 /* Calculate potential maximum space. Each region gets its own
1806 * xlog_op_header_t and may need to be double word aligned.
1809 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1810 len += sizeof(xlog_op_header_t);
1811 XLOG_TIC_ADD_OPHDR(ticket);
1814 for (index = 0; index < nentries; index++) {
1815 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1816 XLOG_TIC_ADD_OPHDR(ticket);
1817 len += reg[index].i_len;
1818 XLOG_TIC_ADD_REGION(ticket, reg[index].i_len, reg[index].i_type);
1820 contwr = *start_lsn = 0;
1822 if (ticket->t_curr_res < len) {
1823 xlog_print_tic_res(mp, ticket);
1826 "xfs_log_write: reservation ran out. Need to up reservation");
1828 /* Customer configurable panic */
1829 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1830 "xfs_log_write: reservation ran out. Need to up reservation");
1831 /* If we did not panic, shutdown the filesystem */
1832 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1835 ticket->t_curr_res -= len;
1837 for (index = 0; index < nentries; ) {
1838 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1839 &contwr, &log_offset)))
1842 ASSERT(log_offset <= iclog->ic_size - 1);
1843 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1845 /* start_lsn is the first lsn written to. That's all we need. */
1847 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1849 /* This loop writes out as many regions as can fit in the amount
1850 * of space which was allocated by xlog_state_get_iclog_space().
1852 while (index < nentries) {
1853 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1854 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1857 /* If first write for transaction, insert start record.
1858 * We can't be trying to commit if we are inited. We can't
1859 * have any "partial_copy" if we are inited.
1861 if (ticket->t_flags & XLOG_TIC_INITED) {
1862 logop_head = (xlog_op_header_t *)ptr;
1863 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1864 logop_head->oh_clientid = ticket->t_clientid;
1865 logop_head->oh_len = 0;
1866 logop_head->oh_flags = XLOG_START_TRANS;
1867 logop_head->oh_res2 = 0;
1868 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1871 start_rec_copy = sizeof(xlog_op_header_t);
1872 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1875 /* Copy log operation header directly into data section */
1876 logop_head = (xlog_op_header_t *)ptr;
1877 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1878 logop_head->oh_clientid = ticket->t_clientid;
1879 logop_head->oh_res2 = 0;
1881 /* header copied directly */
1882 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1884 /* are we copying a commit or unmount record? */
1885 logop_head->oh_flags = flags;
1888 * We've seen logs corrupted with bad transaction client
1889 * ids. This makes sure that XFS doesn't generate them on.
1890 * Turn this into an EIO and shut down the filesystem.
1892 switch (logop_head->oh_clientid) {
1893 case XFS_TRANSACTION:
1898 xfs_fs_cmn_err(CE_WARN, mp,
1899 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1900 logop_head->oh_clientid, tic);
1901 return XFS_ERROR(EIO);
1904 /* Partial write last time? => (partial_copy != 0)
1905 * need_copy is the amount we'd like to copy if everything could
1906 * fit in the current memcpy.
1908 need_copy = reg[index].i_len - partial_copy_len;
1910 copy_off = partial_copy_len;
1911 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1912 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1914 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1915 partial_copy_len = partial_copy = 0;
1916 } else { /* partial write */
1917 copy_len = iclog->ic_size - log_offset;
1918 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1919 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1921 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1922 partial_copy_len += copy_len;
1924 len += sizeof(xlog_op_header_t); /* from splitting of region */
1925 /* account for new log op header */
1926 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1927 XLOG_TIC_ADD_OPHDR(ticket);
1929 xlog_verify_dest_ptr(log, ptr);
1932 ASSERT(copy_len >= 0);
1933 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1934 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1936 /* make copy_len total bytes copied, including headers */
1937 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1939 data_cnt += contwr ? copy_len : 0;
1940 if (partial_copy) { /* copied partial region */
1941 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1942 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1943 record_cnt = data_cnt = 0;
1944 if ((error = xlog_state_release_iclog(log, iclog)))
1946 break; /* don't increment index */
1947 } else { /* copied entire region */
1949 partial_copy_len = partial_copy = 0;
1951 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1952 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1953 record_cnt = data_cnt = 0;
1954 xlog_state_want_sync(log, iclog);
1956 ASSERT(flags & XLOG_COMMIT_TRANS);
1957 *commit_iclog = iclog;
1958 } else if ((error = xlog_state_release_iclog(log, iclog)))
1960 if (index == nentries)
1961 return 0; /* we are done */
1965 } /* if (partial_copy) */
1966 } /* while (index < nentries) */
1967 } /* for (index = 0; index < nentries; ) */
1970 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1972 ASSERT(flags & XLOG_COMMIT_TRANS);
1973 *commit_iclog = iclog;
1976 return (xlog_state_release_iclog(log, iclog));
1980 /*****************************************************************************
1982 * State Machine functions
1984 *****************************************************************************
1987 /* Clean iclogs starting from the head. This ordering must be
1988 * maintained, so an iclog doesn't become ACTIVE beyond one that
1989 * is SYNCING. This is also required to maintain the notion that we use
1990 * a counting semaphore to hold off would be writers to the log when every
1991 * iclog is trying to sync to disk.
1993 * State Change: DIRTY -> ACTIVE
1996 xlog_state_clean_log(xlog_t *log)
1998 xlog_in_core_t *iclog;
2001 iclog = log->l_iclog;
2003 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2004 iclog->ic_state = XLOG_STATE_ACTIVE;
2005 iclog->ic_offset = 0;
2006 iclog->ic_callback = NULL; /* don't need to free */
2008 * If the number of ops in this iclog indicate it just
2009 * contains the dummy transaction, we can
2010 * change state into IDLE (the second time around).
2011 * Otherwise we should change the state into
2013 * We don't need to cover the dummy.
2016 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
2020 * We have two dirty iclogs so start over
2021 * This could also be num of ops indicates
2022 * this is not the dummy going out.
2026 iclog->ic_header.h_num_logops = 0;
2027 memset(iclog->ic_header.h_cycle_data, 0,
2028 sizeof(iclog->ic_header.h_cycle_data));
2029 iclog->ic_header.h_lsn = 0;
2030 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2033 break; /* stop cleaning */
2034 iclog = iclog->ic_next;
2035 } while (iclog != log->l_iclog);
2037 /* log is locked when we are called */
2039 * Change state for the dummy log recording.
2040 * We usually go to NEED. But we go to NEED2 if the changed indicates
2041 * we are done writing the dummy record.
2042 * If we are done with the second dummy recored (DONE2), then
2046 switch (log->l_covered_state) {
2047 case XLOG_STATE_COVER_IDLE:
2048 case XLOG_STATE_COVER_NEED:
2049 case XLOG_STATE_COVER_NEED2:
2050 log->l_covered_state = XLOG_STATE_COVER_NEED;
2053 case XLOG_STATE_COVER_DONE:
2055 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2057 log->l_covered_state = XLOG_STATE_COVER_NEED;
2060 case XLOG_STATE_COVER_DONE2:
2062 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2064 log->l_covered_state = XLOG_STATE_COVER_NEED;
2071 } /* xlog_state_clean_log */
2074 xlog_get_lowest_lsn(
2077 xlog_in_core_t *lsn_log;
2078 xfs_lsn_t lowest_lsn, lsn;
2080 lsn_log = log->l_iclog;
2083 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2084 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
2085 if ((lsn && !lowest_lsn) ||
2086 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2090 lsn_log = lsn_log->ic_next;
2091 } while (lsn_log != log->l_iclog);
2097 xlog_state_do_callback(
2100 xlog_in_core_t *ciclog)
2102 xlog_in_core_t *iclog;
2103 xlog_in_core_t *first_iclog; /* used to know when we've
2104 * processed all iclogs once */
2105 xfs_log_callback_t *cb, *cb_next;
2107 xfs_lsn_t lowest_lsn;
2108 int ioerrors; /* counter: iclogs with errors */
2109 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2110 int funcdidcallbacks; /* flag: function did callbacks */
2111 int repeats; /* for issuing console warnings if
2112 * looping too many times */
2116 first_iclog = iclog = log->l_iclog;
2118 funcdidcallbacks = 0;
2123 * Scan all iclogs starting with the one pointed to by the
2124 * log. Reset this starting point each time the log is
2125 * unlocked (during callbacks).
2127 * Keep looping through iclogs until one full pass is made
2128 * without running any callbacks.
2130 first_iclog = log->l_iclog;
2131 iclog = log->l_iclog;
2132 loopdidcallbacks = 0;
2137 /* skip all iclogs in the ACTIVE & DIRTY states */
2138 if (iclog->ic_state &
2139 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2140 iclog = iclog->ic_next;
2145 * Between marking a filesystem SHUTDOWN and stopping
2146 * the log, we do flush all iclogs to disk (if there
2147 * wasn't a log I/O error). So, we do want things to
2148 * go smoothly in case of just a SHUTDOWN w/o a
2151 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2153 * Can only perform callbacks in order. Since
2154 * this iclog is not in the DONE_SYNC/
2155 * DO_CALLBACK state, we skip the rest and
2156 * just try to clean up. If we set our iclog
2157 * to DO_CALLBACK, we will not process it when
2158 * we retry since a previous iclog is in the
2159 * CALLBACK and the state cannot change since
2160 * we are holding the LOG_LOCK.
2162 if (!(iclog->ic_state &
2163 (XLOG_STATE_DONE_SYNC |
2164 XLOG_STATE_DO_CALLBACK))) {
2165 if (ciclog && (ciclog->ic_state ==
2166 XLOG_STATE_DONE_SYNC)) {
2167 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2172 * We now have an iclog that is in either the
2173 * DO_CALLBACK or DONE_SYNC states. The other
2174 * states (WANT_SYNC, SYNCING, or CALLBACK were
2175 * caught by the above if and are going to
2176 * clean (i.e. we aren't doing their callbacks)
2181 * We will do one more check here to see if we
2182 * have chased our tail around.
2185 lowest_lsn = xlog_get_lowest_lsn(log);
2189 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2191 iclog = iclog->ic_next;
2192 continue; /* Leave this iclog for
2196 iclog->ic_state = XLOG_STATE_CALLBACK;
2200 /* l_last_sync_lsn field protected by
2201 * GRANT_LOCK. Don't worry about iclog's lsn.
2202 * No one else can be here except us.
2204 s = GRANT_LOCK(log);
2206 log->l_last_sync_lsn,
2207 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2209 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2210 GRANT_UNLOCK(log, s);
2213 * Keep processing entries in the callback list
2214 * until we come around and it is empty. We
2215 * need to atomically see that the list is
2216 * empty and change the state to DIRTY so that
2217 * we don't miss any more callbacks being added.
2223 cb = iclog->ic_callback;
2226 iclog->ic_callback_tail = &(iclog->ic_callback);
2227 iclog->ic_callback = NULL;
2230 /* perform callbacks in the order given */
2231 for (; cb != 0; cb = cb_next) {
2232 cb_next = cb->cb_next;
2233 cb->cb_func(cb->cb_arg, aborted);
2236 cb = iclog->ic_callback;
2242 ASSERT(iclog->ic_callback == 0);
2243 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2244 iclog->ic_state = XLOG_STATE_DIRTY;
2247 * Transition from DIRTY to ACTIVE if applicable.
2248 * NOP if STATE_IOERROR.
2250 xlog_state_clean_log(log);
2252 /* wake up threads waiting in xfs_log_force() */
2253 sv_broadcast(&iclog->ic_forcesema);
2255 iclog = iclog->ic_next;
2256 } while (first_iclog != iclog);
2257 if (repeats && (repeats % 10) == 0) {
2258 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2259 "xlog_state_do_callback: looping %d", repeats);
2261 } while (!ioerrors && loopdidcallbacks);
2264 * make one last gasp attempt to see if iclogs are being left in
2268 if (funcdidcallbacks) {
2269 first_iclog = iclog = log->l_iclog;
2271 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2273 * Terminate the loop if iclogs are found in states
2274 * which will cause other threads to clean up iclogs.
2276 * SYNCING - i/o completion will go through logs
2277 * DONE_SYNC - interrupt thread should be waiting for
2279 * IOERROR - give up hope all ye who enter here
2281 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2282 iclog->ic_state == XLOG_STATE_SYNCING ||
2283 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2284 iclog->ic_state == XLOG_STATE_IOERROR )
2286 iclog = iclog->ic_next;
2287 } while (first_iclog != iclog);
2291 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2292 flushcnt = log->l_flushcnt;
2293 log->l_flushcnt = 0;
2297 vsema(&log->l_flushsema);
2298 } /* xlog_state_do_callback */
2302 * Finish transitioning this iclog to the dirty state.
2304 * Make sure that we completely execute this routine only when this is
2305 * the last call to the iclog. There is a good chance that iclog flushes,
2306 * when we reach the end of the physical log, get turned into 2 separate
2307 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2308 * routine. By using the reference count bwritecnt, we guarantee that only
2309 * the second completion goes through.
2311 * Callbacks could take time, so they are done outside the scope of the
2312 * global state machine log lock. Assume that the calls to cvsema won't
2313 * take a long time. At least we know it won't sleep.
2316 xlog_state_done_syncing(
2317 xlog_in_core_t *iclog,
2320 xlog_t *log = iclog->ic_log;
2325 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2326 iclog->ic_state == XLOG_STATE_IOERROR);
2327 ASSERT(iclog->ic_refcnt == 0);
2328 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2332 * If we got an error, either on the first buffer, or in the case of
2333 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2334 * and none should ever be attempted to be written to disk
2337 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2338 if (--iclog->ic_bwritecnt == 1) {
2342 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2346 * Someone could be sleeping prior to writing out the next
2347 * iclog buffer, we wake them all, one will get to do the
2348 * I/O, the others get to wait for the result.
2350 sv_broadcast(&iclog->ic_writesema);
2352 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2353 } /* xlog_state_done_syncing */
2357 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2358 * sleep. The flush semaphore is set to the number of in-core buffers and
2359 * decremented around disk syncing. Therefore, if all buffers are syncing,
2360 * this semaphore will cause new writes to sleep until a sync completes.
2361 * Otherwise, this code just does p() followed by v(). This approximates
2362 * a sleep/wakeup except we can't race.
2364 * The in-core logs are used in a circular fashion. They are not used
2365 * out-of-order even when an iclog past the head is free.
2368 * * log_offset where xlog_write() can start writing into the in-core
2370 * * in-core log pointer to which xlog_write() should write.
2371 * * boolean indicating this is a continued write to an in-core log.
2372 * If this is the last write, then the in-core log's offset field
2373 * needs to be incremented, depending on the amount of data which
2377 xlog_state_get_iclog_space(xlog_t *log,
2379 xlog_in_core_t **iclogp,
2380 xlog_ticket_t *ticket,
2381 int *continued_write,
2386 xlog_rec_header_t *head;
2387 xlog_in_core_t *iclog;
2392 if (XLOG_FORCED_SHUTDOWN(log)) {
2394 return XFS_ERROR(EIO);
2397 iclog = log->l_iclog;
2398 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2401 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2402 XFS_STATS_INC(xs_log_noiclogs);
2403 /* Ensure that log writes happen */
2404 psema(&log->l_flushsema, PINOD);
2407 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2408 head = &iclog->ic_header;
2410 iclog->ic_refcnt++; /* prevents sync */
2411 log_offset = iclog->ic_offset;
2413 /* On the 1st write to an iclog, figure out lsn. This works
2414 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2415 * committing to. If the offset is set, that's how many blocks
2418 if (log_offset == 0) {
2419 ticket->t_curr_res -= log->l_iclog_hsize;
2420 XLOG_TIC_ADD_REGION(ticket,
2422 XLOG_REG_TYPE_LRHEADER);
2423 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2424 ASSIGN_LSN(head->h_lsn, log);
2425 ASSERT(log->l_curr_block >= 0);
2428 /* If there is enough room to write everything, then do it. Otherwise,
2429 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2430 * bit is on, so this will get flushed out. Don't update ic_offset
2431 * until you know exactly how many bytes get copied. Therefore, wait
2432 * until later to update ic_offset.
2434 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2435 * can fit into remaining data section.
2437 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2438 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2440 /* If I'm the only one writing to this iclog, sync it to disk */
2441 if (iclog->ic_refcnt == 1) {
2443 if ((error = xlog_state_release_iclog(log, iclog)))
2452 /* Do we have enough room to write the full amount in the remainder
2453 * of this iclog? Or must we continue a write on the next iclog and
2454 * mark this iclog as completely taken? In the case where we switch
2455 * iclogs (to mark it taken), this particular iclog will release/sync
2456 * to disk in xlog_write().
2458 if (len <= iclog->ic_size - iclog->ic_offset) {
2459 *continued_write = 0;
2460 iclog->ic_offset += len;
2462 *continued_write = 1;
2463 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2467 ASSERT(iclog->ic_offset <= iclog->ic_size);
2470 *logoffsetp = log_offset;
2472 } /* xlog_state_get_iclog_space */
2475 * Atomically get the log space required for a log ticket.
2477 * Once a ticket gets put onto the reserveq, it will only return after
2478 * the needed reservation is satisfied.
2481 xlog_grant_log_space(xlog_t *log,
2493 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2494 panic("grant Recovery problem");
2497 /* Is there space or do we need to sleep? */
2498 s = GRANT_LOCK(log);
2499 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2501 /* something is already sleeping; insert new transaction at end */
2502 if (log->l_reserve_headq) {
2503 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2504 xlog_trace_loggrant(log, tic,
2505 "xlog_grant_log_space: sleep 1");
2507 * Gotta check this before going to sleep, while we're
2508 * holding the grant lock.
2510 if (XLOG_FORCED_SHUTDOWN(log))
2513 XFS_STATS_INC(xs_sleep_logspace);
2514 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2516 * If we got an error, and the filesystem is shutting down,
2517 * we'll catch it down below. So just continue...
2519 xlog_trace_loggrant(log, tic,
2520 "xlog_grant_log_space: wake 1");
2521 s = GRANT_LOCK(log);
2523 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2524 need_bytes = tic->t_unit_res*tic->t_ocnt;
2526 need_bytes = tic->t_unit_res;
2529 if (XLOG_FORCED_SHUTDOWN(log))
2532 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2533 log->l_grant_reserve_bytes);
2534 if (free_bytes < need_bytes) {
2535 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2536 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2537 xlog_trace_loggrant(log, tic,
2538 "xlog_grant_log_space: sleep 2");
2539 XFS_STATS_INC(xs_sleep_logspace);
2540 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2542 if (XLOG_FORCED_SHUTDOWN(log)) {
2543 s = GRANT_LOCK(log);
2547 xlog_trace_loggrant(log, tic,
2548 "xlog_grant_log_space: wake 2");
2549 xlog_grant_push_ail(log->l_mp, need_bytes);
2550 s = GRANT_LOCK(log);
2552 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2553 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2555 /* we've got enough space */
2556 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w');
2557 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'r');
2559 tail_lsn = log->l_tail_lsn;
2561 * Check to make sure the grant write head didn't just over lap the
2562 * tail. If the cycles are the same, we can't be overlapping.
2563 * Otherwise, make sure that the cycles differ by exactly one and
2564 * check the byte count.
2566 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2567 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2568 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2571 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2572 xlog_verify_grant_head(log, 1);
2573 GRANT_UNLOCK(log, s);
2577 if (tic->t_flags & XLOG_TIC_IN_Q)
2578 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2579 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2581 * If we are failing, make sure the ticket doesn't have any
2582 * current reservations. We don't want to add this back when
2583 * the ticket/transaction gets cancelled.
2585 tic->t_curr_res = 0;
2586 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2587 GRANT_UNLOCK(log, s);
2588 return XFS_ERROR(EIO);
2589 } /* xlog_grant_log_space */
2593 * Replenish the byte reservation required by moving the grant write head.
2598 xlog_regrant_write_log_space(xlog_t *log,
2602 int free_bytes, need_bytes;
2603 xlog_ticket_t *ntic;
2608 tic->t_curr_res = tic->t_unit_res;
2609 XLOG_TIC_RESET_RES(tic);
2615 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2616 panic("regrant Recovery problem");
2619 s = GRANT_LOCK(log);
2620 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2622 if (XLOG_FORCED_SHUTDOWN(log))
2625 /* If there are other waiters on the queue then give them a
2626 * chance at logspace before us. Wake up the first waiters,
2627 * if we do not wake up all the waiters then go to sleep waiting
2628 * for more free space, otherwise try to get some space for
2632 if ((ntic = log->l_write_headq)) {
2633 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2634 log->l_grant_write_bytes);
2636 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2638 if (free_bytes < ntic->t_unit_res)
2640 free_bytes -= ntic->t_unit_res;
2641 sv_signal(&ntic->t_sema);
2642 ntic = ntic->t_next;
2643 } while (ntic != log->l_write_headq);
2645 if (ntic != log->l_write_headq) {
2646 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2647 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2649 xlog_trace_loggrant(log, tic,
2650 "xlog_regrant_write_log_space: sleep 1");
2651 XFS_STATS_INC(xs_sleep_logspace);
2652 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2653 &log->l_grant_lock, s);
2655 /* If we're shutting down, this tic is already
2657 if (XLOG_FORCED_SHUTDOWN(log)) {
2658 s = GRANT_LOCK(log);
2662 xlog_trace_loggrant(log, tic,
2663 "xlog_regrant_write_log_space: wake 1");
2664 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2665 s = GRANT_LOCK(log);
2669 need_bytes = tic->t_unit_res;
2672 if (XLOG_FORCED_SHUTDOWN(log))
2675 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2676 log->l_grant_write_bytes);
2677 if (free_bytes < need_bytes) {
2678 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2679 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2680 XFS_STATS_INC(xs_sleep_logspace);
2681 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2683 /* If we're shutting down, this tic is already off the queue */
2684 if (XLOG_FORCED_SHUTDOWN(log)) {
2685 s = GRANT_LOCK(log);
2689 xlog_trace_loggrant(log, tic,
2690 "xlog_regrant_write_log_space: wake 2");
2691 xlog_grant_push_ail(log->l_mp, need_bytes);
2692 s = GRANT_LOCK(log);
2694 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2695 XLOG_DEL_TICKETQ(log->l_write_headq, tic);
2697 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w'); /* we've got enough space */
2699 tail_lsn = log->l_tail_lsn;
2700 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2701 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2702 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2706 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2707 xlog_verify_grant_head(log, 1);
2708 GRANT_UNLOCK(log, s);
2713 if (tic->t_flags & XLOG_TIC_IN_Q)
2714 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2715 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2717 * If we are failing, make sure the ticket doesn't have any
2718 * current reservations. We don't want to add this back when
2719 * the ticket/transaction gets cancelled.
2721 tic->t_curr_res = 0;
2722 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2723 GRANT_UNLOCK(log, s);
2724 return XFS_ERROR(EIO);
2725 } /* xlog_regrant_write_log_space */
2728 /* The first cnt-1 times through here we don't need to
2729 * move the grant write head because the permanent
2730 * reservation has reserved cnt times the unit amount.
2731 * Release part of current permanent unit reservation and
2732 * reset current reservation to be one units worth. Also
2733 * move grant reservation head forward.
2736 xlog_regrant_reserve_log_space(xlog_t *log,
2737 xlog_ticket_t *ticket)
2741 xlog_trace_loggrant(log, ticket,
2742 "xlog_regrant_reserve_log_space: enter");
2743 if (ticket->t_cnt > 0)
2746 s = GRANT_LOCK(log);
2747 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2748 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2749 ticket->t_curr_res = ticket->t_unit_res;
2750 XLOG_TIC_RESET_RES(ticket);
2751 xlog_trace_loggrant(log, ticket,
2752 "xlog_regrant_reserve_log_space: sub current res");
2753 xlog_verify_grant_head(log, 1);
2755 /* just return if we still have some of the pre-reserved space */
2756 if (ticket->t_cnt > 0) {
2757 GRANT_UNLOCK(log, s);
2761 XLOG_GRANT_ADD_SPACE(log, ticket->t_unit_res, 'r');
2762 xlog_trace_loggrant(log, ticket,
2763 "xlog_regrant_reserve_log_space: exit");
2764 xlog_verify_grant_head(log, 0);
2765 GRANT_UNLOCK(log, s);
2766 ticket->t_curr_res = ticket->t_unit_res;
2767 XLOG_TIC_RESET_RES(ticket);
2768 } /* xlog_regrant_reserve_log_space */
2772 * Give back the space left from a reservation.
2774 * All the information we need to make a correct determination of space left
2775 * is present. For non-permanent reservations, things are quite easy. The
2776 * count should have been decremented to zero. We only need to deal with the
2777 * space remaining in the current reservation part of the ticket. If the
2778 * ticket contains a permanent reservation, there may be left over space which
2779 * needs to be released. A count of N means that N-1 refills of the current
2780 * reservation can be done before we need to ask for more space. The first
2781 * one goes to fill up the first current reservation. Once we run out of
2782 * space, the count will stay at zero and the only space remaining will be
2783 * in the current reservation field.
2786 xlog_ungrant_log_space(xlog_t *log,
2787 xlog_ticket_t *ticket)
2791 if (ticket->t_cnt > 0)
2794 s = GRANT_LOCK(log);
2795 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2797 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2798 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2800 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2802 /* If this is a permanent reservation ticket, we may be able to free
2803 * up more space based on the remaining count.
2805 if (ticket->t_cnt > 0) {
2806 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2807 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'w');
2808 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'r');
2811 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2812 xlog_verify_grant_head(log, 1);
2813 GRANT_UNLOCK(log, s);
2814 xfs_log_move_tail(log->l_mp, 1);
2815 } /* xlog_ungrant_log_space */
2819 * Atomically put back used ticket.
2822 xlog_state_put_ticket(xlog_t *log,
2828 xlog_ticket_put(log, tic);
2830 } /* xlog_state_put_ticket */
2833 * Flush iclog to disk if this is the last reference to the given iclog and
2834 * the WANT_SYNC bit is set.
2836 * When this function is entered, the iclog is not necessarily in the
2837 * WANT_SYNC state. It may be sitting around waiting to get filled.
2842 xlog_state_release_iclog(xlog_t *log,
2843 xlog_in_core_t *iclog)
2846 int sync = 0; /* do we sync? */
2848 xlog_assign_tail_lsn(log->l_mp);
2852 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2854 return XFS_ERROR(EIO);
2857 ASSERT(iclog->ic_refcnt > 0);
2858 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2859 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2861 if (--iclog->ic_refcnt == 0 &&
2862 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2864 iclog->ic_state = XLOG_STATE_SYNCING;
2865 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2866 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2867 /* cycle incremented when incrementing curr_block */
2873 * We let the log lock go, so it's possible that we hit a log I/O
2874 * error or someother SHUTDOWN condition that marks the iclog
2875 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2876 * this iclog has consistent data, so we ignore IOERROR
2877 * flags after this point.
2880 return xlog_sync(log, iclog);
2884 } /* xlog_state_release_iclog */
2888 * This routine will mark the current iclog in the ring as WANT_SYNC
2889 * and move the current iclog pointer to the next iclog in the ring.
2890 * When this routine is called from xlog_state_get_iclog_space(), the
2891 * exact size of the iclog has not yet been determined. All we know is
2892 * that every data block. We have run out of space in this log record.
2895 xlog_state_switch_iclogs(xlog_t *log,
2896 xlog_in_core_t *iclog,
2899 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2901 eventual_size = iclog->ic_offset;
2902 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2903 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2904 log->l_prev_block = log->l_curr_block;
2905 log->l_prev_cycle = log->l_curr_cycle;
2907 /* roll log?: ic_offset changed later */
2908 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2910 /* Round up to next log-sunit */
2911 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2912 log->l_mp->m_sb.sb_logsunit > 1) {
2913 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2914 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2917 if (log->l_curr_block >= log->l_logBBsize) {
2918 log->l_curr_cycle++;
2919 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2920 log->l_curr_cycle++;
2921 log->l_curr_block -= log->l_logBBsize;
2922 ASSERT(log->l_curr_block >= 0);
2924 ASSERT(iclog == log->l_iclog);
2925 log->l_iclog = iclog->ic_next;
2926 } /* xlog_state_switch_iclogs */
2930 * Write out all data in the in-core log as of this exact moment in time.
2932 * Data may be written to the in-core log during this call. However,
2933 * we don't guarantee this data will be written out. A change from past
2934 * implementation means this routine will *not* write out zero length LRs.
2936 * Basically, we try and perform an intelligent scan of the in-core logs.
2937 * If we determine there is no flushable data, we just return. There is no
2938 * flushable data if:
2940 * 1. the current iclog is active and has no data; the previous iclog
2941 * is in the active or dirty state.
2942 * 2. the current iclog is drity, and the previous iclog is in the
2943 * active or dirty state.
2945 * We may sleep (call psema) if:
2947 * 1. the current iclog is not in the active nor dirty state.
2948 * 2. the current iclog dirty, and the previous iclog is not in the
2949 * active nor dirty state.
2950 * 3. the current iclog is active, and there is another thread writing
2951 * to this particular iclog.
2952 * 4. a) the current iclog is active and has no other writers
2953 * b) when we return from flushing out this iclog, it is still
2954 * not in the active nor dirty state.
2957 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2959 xlog_in_core_t *iclog;
2965 iclog = log->l_iclog;
2966 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2968 return XFS_ERROR(EIO);
2971 /* If the head iclog is not active nor dirty, we just attach
2972 * ourselves to the head and go to sleep.
2974 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2975 iclog->ic_state == XLOG_STATE_DIRTY) {
2977 * If the head is dirty or (active and empty), then
2978 * we need to look at the previous iclog. If the previous
2979 * iclog is active or dirty we are done. There is nothing
2980 * to sync out. Otherwise, we attach ourselves to the
2981 * previous iclog and go to sleep.
2983 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2984 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2985 iclog = iclog->ic_prev;
2986 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2987 iclog->ic_state == XLOG_STATE_DIRTY)
2992 if (iclog->ic_refcnt == 0) {
2993 /* We are the only one with access to this
2994 * iclog. Flush it out now. There should
2995 * be a roundoff of zero to show that someone
2996 * has already taken care of the roundoff from
2997 * the previous sync.
3000 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
3001 xlog_state_switch_iclogs(log, iclog, 0);
3004 if (xlog_state_release_iclog(log, iclog))
3005 return XFS_ERROR(EIO);
3008 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
3009 iclog->ic_state != XLOG_STATE_DIRTY)
3014 /* Someone else is writing to this iclog.
3015 * Use its call to flush out the data. However,
3016 * the other thread may not force out this LR,
3017 * so we mark it WANT_SYNC.
3019 xlog_state_switch_iclogs(log, iclog, 0);
3025 /* By the time we come around again, the iclog could've been filled
3026 * which would give it another lsn. If we have a new lsn, just
3027 * return because the relevant data has been flushed.
3030 if (flags & XFS_LOG_SYNC) {
3032 * We must check if we're shutting down here, before
3033 * we wait, while we're holding the LOG_LOCK.
3034 * Then we check again after waking up, in case our
3035 * sleep was disturbed by a bad news.
3037 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3039 return XFS_ERROR(EIO);
3041 XFS_STATS_INC(xs_log_force_sleep);
3042 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3044 * No need to grab the log lock here since we're
3045 * only deciding whether or not to return EIO
3046 * and the memory read should be atomic.
3048 if (iclog->ic_state & XLOG_STATE_IOERROR)
3049 return XFS_ERROR(EIO);
3058 } /* xlog_state_sync_all */
3062 * Used by code which implements synchronous log forces.
3064 * Find in-core log with lsn.
3065 * If it is in the DIRTY state, just return.
3066 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3067 * state and go to sleep or return.
3068 * If it is in any other state, go to sleep or return.
3070 * If filesystem activity goes to zero, the iclog will get flushed only by
3074 xlog_state_sync(xlog_t *log,
3079 xlog_in_core_t *iclog;
3080 int already_slept = 0;
3086 iclog = log->l_iclog;
3088 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3090 return XFS_ERROR(EIO);
3094 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
3095 iclog = iclog->ic_next;
3099 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3104 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3106 * We sleep here if we haven't already slept (e.g.
3107 * this is the first time we've looked at the correct
3108 * iclog buf) and the buffer before us is going to
3109 * be sync'ed. The reason for this is that if we
3110 * are doing sync transactions here, by waiting for
3111 * the previous I/O to complete, we can allow a few
3112 * more transactions into this iclog before we close
3115 * Otherwise, we mark the buffer WANT_SYNC, and bump
3116 * up the refcnt so we can release the log (which drops
3117 * the ref count). The state switch keeps new transaction
3118 * commits from using this buffer. When the current commits
3119 * finish writing into the buffer, the refcount will drop to
3120 * zero and the buffer will go out then.
3122 if (!already_slept &&
3123 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3124 XLOG_STATE_SYNCING))) {
3125 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3126 XFS_STATS_INC(xs_log_force_sleep);
3127 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3128 &log->l_icloglock, s);
3134 xlog_state_switch_iclogs(log, iclog, 0);
3136 if (xlog_state_release_iclog(log, iclog))
3137 return XFS_ERROR(EIO);
3143 if ((flags & XFS_LOG_SYNC) && /* sleep */
3144 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3147 * Don't wait on the forcesema if we know that we've
3148 * gotten a log write error.
3150 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3152 return XFS_ERROR(EIO);
3154 XFS_STATS_INC(xs_log_force_sleep);
3155 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3157 * No need to grab the log lock here since we're
3158 * only deciding whether or not to return EIO
3159 * and the memory read should be atomic.
3161 if (iclog->ic_state & XLOG_STATE_IOERROR)
3162 return XFS_ERROR(EIO);
3164 } else { /* just return */
3169 } while (iclog != log->l_iclog);
3173 } /* xlog_state_sync */
3177 * Called when we want to mark the current iclog as being ready to sync to
3181 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3187 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3188 xlog_state_switch_iclogs(log, iclog, 0);
3190 ASSERT(iclog->ic_state &
3191 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3195 } /* xlog_state_want_sync */
3199 /*****************************************************************************
3203 *****************************************************************************
3207 * Algorithm doesn't take into account page size. ;-(
3210 xlog_state_ticket_alloc(xlog_t *log)
3212 xlog_ticket_t *t_list;
3213 xlog_ticket_t *next;
3215 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3219 * The kmem_zalloc may sleep, so we shouldn't be holding the
3220 * global lock. XXXmiken: may want to use zone allocator.
3222 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3226 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3227 t_list = (xlog_ticket_t *)buf;
3228 t_list->t_next = log->l_unmount_free;
3229 log->l_unmount_free = t_list++;
3230 log->l_ticket_cnt++;
3231 log->l_ticket_tcnt++;
3233 /* Next ticket becomes first ticket attached to ticket free list */
3234 if (log->l_freelist != NULL) {
3235 ASSERT(log->l_tail != NULL);
3236 log->l_tail->t_next = t_list;
3238 log->l_freelist = t_list;
3240 log->l_ticket_cnt++;
3241 log->l_ticket_tcnt++;
3243 /* Cycle through rest of alloc'ed memory, building up free Q */
3244 for ( ; i > 0; i--) {
3246 t_list->t_next = next;
3248 log->l_ticket_cnt++;
3249 log->l_ticket_tcnt++;
3251 t_list->t_next = NULL;
3252 log->l_tail = t_list;
3254 } /* xlog_state_ticket_alloc */
3258 * Put ticket into free list
3260 * Assumption: log lock is held around this call.
3263 xlog_ticket_put(xlog_t *log,
3264 xlog_ticket_t *ticket)
3266 sv_destroy(&ticket->t_sema);
3269 * Don't think caching will make that much difference. It's
3270 * more important to make debug easier.
3273 /* real code will want to use LIFO for caching */
3274 ticket->t_next = log->l_freelist;
3275 log->l_freelist = ticket;
3276 /* no need to clear fields */
3278 /* When we debug, it is easier if tickets are cycled */
3279 ticket->t_next = NULL;
3280 if (log->l_tail != 0) {
3281 log->l_tail->t_next = ticket;
3283 ASSERT(log->l_freelist == 0);
3284 log->l_freelist = ticket;
3286 log->l_tail = ticket;
3288 log->l_ticket_cnt++;
3289 } /* xlog_ticket_put */
3293 * Grab ticket off freelist or allocation some more
3296 xlog_ticket_get(xlog_t *log,
3307 if (log->l_freelist == NULL)
3308 xlog_state_ticket_alloc(log); /* potentially sleep */
3311 if (log->l_freelist == NULL) {
3315 tic = log->l_freelist;
3316 log->l_freelist = tic->t_next;
3317 if (log->l_freelist == NULL)
3319 log->l_ticket_cnt--;
3323 * Permanent reservations have up to 'cnt'-1 active log operations
3324 * in the log. A unit in this case is the amount of space for one
3325 * of these log operations. Normal reservations have a cnt of 1
3326 * and their unit amount is the total amount of space required.
3328 * The following lines of code account for non-transaction data
3329 * which occupy space in the on-disk log.
3331 * Normal form of a transaction is:
3332 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3333 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3335 * We need to account for all the leadup data and trailer data
3336 * around the transaction data.
3337 * And then we need to account for the worst case in terms of using
3339 * The worst case will happen if:
3340 * - the placement of the transaction happens to be such that the
3341 * roundoff is at its maximum
3342 * - the transaction data is synced before the commit record is synced
3343 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3344 * Therefore the commit record is in its own Log Record.
3345 * This can happen as the commit record is called with its
3346 * own region to xlog_write().
3347 * This then means that in the worst case, roundoff can happen for
3348 * the commit-rec as well.
3349 * The commit-rec is smaller than padding in this scenario and so it is
3350 * not added separately.
3353 /* for trans header */
3354 unit_bytes += sizeof(xlog_op_header_t);
3355 unit_bytes += sizeof(xfs_trans_header_t);
3358 unit_bytes += sizeof(xlog_op_header_t);
3360 /* for LR headers */
3361 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3362 unit_bytes += log->l_iclog_hsize * num_headers;
3364 /* for commit-rec LR header - note: padding will subsume the ophdr */
3365 unit_bytes += log->l_iclog_hsize;
3367 /* for split-recs - ophdrs added when data split over LRs */
3368 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3370 /* for roundoff padding for transaction data and one for commit record */
3371 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3372 log->l_mp->m_sb.sb_logsunit > 1) {
3373 /* log su roundoff */
3374 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3377 unit_bytes += 2*BBSIZE;
3380 tic->t_unit_res = unit_bytes;
3381 tic->t_curr_res = unit_bytes;
3384 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3385 tic->t_clientid = client;
3386 tic->t_flags = XLOG_TIC_INITED;
3387 tic->t_trans_type = 0;
3388 if (xflags & XFS_LOG_PERM_RESERV)
3389 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3390 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3392 XLOG_TIC_RESET_RES(tic);
3395 } /* xlog_ticket_get */
3398 /******************************************************************************
3400 * Log debug routines
3402 ******************************************************************************
3404 #if defined(DEBUG) && !defined(XLOG_NOLOG)
3406 * Make sure that the destination ptr is within the valid data region of
3407 * one of the iclogs. This uses backup pointers stored in a different
3408 * part of the log in case we trash the log structure.
3411 xlog_verify_dest_ptr(xlog_t *log,
3417 for (i=0; i < log->l_iclog_bufs; i++) {
3418 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3419 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3423 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3424 } /* xlog_verify_dest_ptr */
3427 xlog_verify_grant_head(xlog_t *log, int equals)
3429 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3431 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3433 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3435 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3436 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3438 } /* xlog_verify_grant_head */
3440 /* check if it will fit */
3442 xlog_verify_tail_lsn(xlog_t *log,
3443 xlog_in_core_t *iclog,
3448 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3450 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3451 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3452 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3454 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3456 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3457 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3459 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3460 if (blocks < BTOBB(iclog->ic_offset) + 1)
3461 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3463 } /* xlog_verify_tail_lsn */
3466 * Perform a number of checks on the iclog before writing to disk.
3468 * 1. Make sure the iclogs are still circular
3469 * 2. Make sure we have a good magic number
3470 * 3. Make sure we don't have magic numbers in the data
3471 * 4. Check fields of each log operation header for:
3472 * A. Valid client identifier
3473 * B. tid ptr value falls in valid ptr space (user space code)
3474 * C. Length in log record header is correct according to the
3475 * individual operation headers within record.
3476 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3477 * log, check the preceding blocks of the physical log to make sure all
3478 * the cycle numbers agree with the current cycle number.
3481 xlog_verify_iclog(xlog_t *log,
3482 xlog_in_core_t *iclog,
3486 xlog_op_header_t *ophead;
3487 xlog_in_core_t *icptr;
3488 xlog_in_core_2_t *xhdr;
3490 xfs_caddr_t base_ptr;
3491 __psint_t field_offset;
3493 int len, i, j, k, op_len;
3497 /* check validity of iclog pointers */
3499 icptr = log->l_iclog;
3500 for (i=0; i < log->l_iclog_bufs; i++) {
3502 xlog_panic("xlog_verify_iclog: invalid ptr");
3503 icptr = icptr->ic_next;
3505 if (icptr != log->l_iclog)
3506 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3509 /* check log magic numbers */
3510 ptr = (xfs_caddr_t) &(iclog->ic_header);
3511 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3512 xlog_panic("xlog_verify_iclog: invalid magic num");
3514 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3516 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3517 xlog_panic("xlog_verify_iclog: unexpected magic num");
3521 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3522 ptr = iclog->ic_datap;
3524 ophead = (xlog_op_header_t *)ptr;
3525 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3526 for (i = 0; i < len; i++) {
3527 ophead = (xlog_op_header_t *)ptr;
3529 /* clientid is only 1 byte */
3530 field_offset = (__psint_t)
3531 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3532 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3533 clientid = ophead->oh_clientid;
3535 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3536 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3537 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3538 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3539 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3541 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3544 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3545 cmn_err(CE_WARN, "xlog_verify_iclog: "
3546 "invalid clientid %d op 0x%p offset 0x%lx",
3547 clientid, ophead, (unsigned long)field_offset);
3550 field_offset = (__psint_t)
3551 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3552 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3553 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3555 idx = BTOBBT((__psint_t)&ophead->oh_len -
3556 (__psint_t)iclog->ic_datap);
3557 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3558 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3559 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3560 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3562 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3565 ptr += sizeof(xlog_op_header_t) + op_len;
3567 } /* xlog_verify_iclog */
3568 #endif /* DEBUG && !XLOG_NOLOG */
3571 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3577 xlog_in_core_t *iclog, *ic;
3579 iclog = log->l_iclog;
3580 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3582 * Mark all the incore logs IOERROR.
3583 * From now on, no log flushes will result.
3587 ic->ic_state = XLOG_STATE_IOERROR;
3589 } while (ic != iclog);
3593 * Return non-zero, if state transition has already happened.
3599 * This is called from xfs_force_shutdown, when we're forcibly
3600 * shutting down the filesystem, typically because of an IO error.
3601 * Our main objectives here are to make sure that:
3602 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3603 * parties to find out, 'atomically'.
3604 * b. those who're sleeping on log reservations, pinned objects and
3605 * other resources get woken up, and be told the bad news.
3606 * c. nothing new gets queued up after (a) and (b) are done.
3607 * d. if !logerror, flush the iclogs to disk, then seal them off
3611 xfs_log_force_umount(
3612 struct xfs_mount *mp,
3625 * If this happens during log recovery, don't worry about
3626 * locking; the log isn't open for business yet.
3629 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3630 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3631 XFS_BUF_DONE(mp->m_sb_bp);
3636 * Somebody could've already done the hard work for us.
3637 * No need to get locks for this.
3639 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3640 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3645 * We must hold both the GRANT lock and the LOG lock,
3646 * before we mark the filesystem SHUTDOWN and wake
3647 * everybody up to tell the bad news.
3649 s = GRANT_LOCK(log);
3651 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3652 XFS_BUF_DONE(mp->m_sb_bp);
3654 * This flag is sort of redundant because of the mount flag, but
3655 * it's good to maintain the separation between the log and the rest
3658 log->l_flags |= XLOG_IO_ERROR;
3661 * If we hit a log error, we want to mark all the iclogs IOERROR
3662 * while we're still holding the loglock.
3665 retval = xlog_state_ioerror(log);
3666 LOG_UNLOCK(log, s2);
3669 * We don't want anybody waiting for log reservations
3670 * after this. That means we have to wake up everybody
3671 * queued up on reserve_headq as well as write_headq.
3672 * In addition, we make sure in xlog_{re}grant_log_space
3673 * that we don't enqueue anything once the SHUTDOWN flag
3674 * is set, and this action is protected by the GRANTLOCK.
3676 if ((tic = log->l_reserve_headq)) {
3678 sv_signal(&tic->t_sema);
3680 } while (tic != log->l_reserve_headq);
3683 if ((tic = log->l_write_headq)) {
3685 sv_signal(&tic->t_sema);
3687 } while (tic != log->l_write_headq);
3689 GRANT_UNLOCK(log, s);
3691 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3694 * Force the incore logs to disk before shutting the
3695 * log down completely.
3697 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3699 retval = xlog_state_ioerror(log);
3700 LOG_UNLOCK(log, s2);
3703 * Wake up everybody waiting on xfs_log_force.
3704 * Callback all log item committed functions as if the
3705 * log writes were completed.
3707 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3709 #ifdef XFSERRORDEBUG
3711 xlog_in_core_t *iclog;
3714 iclog = log->l_iclog;
3716 ASSERT(iclog->ic_callback == 0);
3717 iclog = iclog->ic_next;
3718 } while (iclog != log->l_iclog);
3722 /* return non-zero if log IOERROR transition had already happened */
3727 xlog_iclogs_empty(xlog_t *log)
3729 xlog_in_core_t *iclog;
3731 iclog = log->l_iclog;
3733 /* endianness does not matter here, zero is zero in
3736 if (iclog->ic_header.h_num_logops)
3738 iclog = iclog->ic_next;
3739 } while (iclog != log->l_iclog);