2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_error.h"
31 #include "xfs_log_priv.h"
32 #include "xfs_buf_item.h"
33 #include "xfs_bmap_btree.h"
34 #include "xfs_alloc_btree.h"
35 #include "xfs_ialloc_btree.h"
36 #include "xfs_log_recover.h"
37 #include "xfs_trans_priv.h"
38 #include "xfs_dir2_sf.h"
39 #include "xfs_attr_sf.h"
40 #include "xfs_dinode.h"
41 #include "xfs_inode.h"
44 kmem_zone_t *xfs_log_ticket_zone;
46 #define xlog_write_adv_cnt(ptr, len, off, bytes) \
51 /* Local miscellaneous function prototypes */
52 STATIC int xlog_bdstrat_cb(struct xfs_buf *);
53 STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54 xlog_in_core_t **, xfs_lsn_t *);
55 STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
56 xfs_buftarg_t *log_target,
57 xfs_daddr_t blk_offset,
59 STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
60 STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
61 STATIC void xlog_dealloc_log(xlog_t *log);
62 STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
63 int nentries, xfs_log_ticket_t tic,
65 xlog_in_core_t **commit_iclog,
68 /* local state machine functions */
69 STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70 STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71 STATIC int xlog_state_get_iclog_space(xlog_t *log,
73 xlog_in_core_t **iclog,
74 xlog_ticket_t *ticket,
77 STATIC int xlog_state_release_iclog(xlog_t *log,
78 xlog_in_core_t *iclog);
79 STATIC void xlog_state_switch_iclogs(xlog_t *log,
80 xlog_in_core_t *iclog,
82 STATIC int xlog_state_sync(xlog_t *log,
86 STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
87 STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
89 /* local functions to manipulate grant head */
90 STATIC int xlog_grant_log_space(xlog_t *log,
92 STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
94 STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
95 xlog_ticket_t *ticket);
96 STATIC int xlog_regrant_write_log_space(xlog_t *log,
97 xlog_ticket_t *ticket);
98 STATIC void xlog_ungrant_log_space(xlog_t *log,
99 xlog_ticket_t *ticket);
102 /* local ticket functions */
103 STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
108 STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
111 STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
112 STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
113 STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
114 int count, boolean_t syncing);
115 STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
118 #define xlog_verify_dest_ptr(a,b)
119 #define xlog_verify_grant_head(a,b)
120 #define xlog_verify_iclog(a,b,c,d)
121 #define xlog_verify_tail_lsn(a,b,c)
124 STATIC int xlog_iclogs_empty(xlog_t *log);
126 #if defined(XFS_LOG_TRACE)
128 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
132 if (!log->l_grant_trace) {
133 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
134 if (!log->l_grant_trace)
137 /* ticket counts are 1 byte each */
138 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
140 ktrace_enter(log->l_grant_trace,
142 (void *)log->l_reserve_headq,
143 (void *)log->l_write_headq,
144 (void *)((unsigned long)log->l_grant_reserve_cycle),
145 (void *)((unsigned long)log->l_grant_reserve_bytes),
146 (void *)((unsigned long)log->l_grant_write_cycle),
147 (void *)((unsigned long)log->l_grant_write_bytes),
148 (void *)((unsigned long)log->l_curr_cycle),
149 (void *)((unsigned long)log->l_curr_block),
150 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
151 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
153 (void *)((unsigned long)tic->t_trans_type),
155 (void *)((unsigned long)tic->t_curr_res),
156 (void *)((unsigned long)tic->t_unit_res));
160 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
162 if (!iclog->ic_trace)
163 iclog->ic_trace = ktrace_alloc(256, KM_NOFS);
164 ktrace_enter(iclog->ic_trace,
165 (void *)((unsigned long)state),
166 (void *)((unsigned long)current_pid()),
167 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
168 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
169 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
170 (void *)NULL, (void *)NULL);
173 #define xlog_trace_loggrant(log,tic,string)
174 #define xlog_trace_iclog(iclog,state)
175 #endif /* XFS_LOG_TRACE */
179 xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
183 tic->t_prev = (*qp)->t_prev;
184 (*qp)->t_prev->t_next = tic;
187 tic->t_prev = tic->t_next = tic;
191 tic->t_flags |= XLOG_TIC_IN_Q;
195 xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
197 if (tic == tic->t_next) {
201 tic->t_next->t_prev = tic->t_prev;
202 tic->t_prev->t_next = tic->t_next;
205 tic->t_next = tic->t_prev = NULL;
206 tic->t_flags &= ~XLOG_TIC_IN_Q;
210 xlog_grant_sub_space(struct log *log, int bytes)
212 log->l_grant_write_bytes -= bytes;
213 if (log->l_grant_write_bytes < 0) {
214 log->l_grant_write_bytes += log->l_logsize;
215 log->l_grant_write_cycle--;
218 log->l_grant_reserve_bytes -= bytes;
219 if ((log)->l_grant_reserve_bytes < 0) {
220 log->l_grant_reserve_bytes += log->l_logsize;
221 log->l_grant_reserve_cycle--;
227 xlog_grant_add_space_write(struct log *log, int bytes)
229 int tmp = log->l_logsize - log->l_grant_write_bytes;
231 log->l_grant_write_bytes += bytes;
233 log->l_grant_write_cycle++;
234 log->l_grant_write_bytes = bytes - tmp;
239 xlog_grant_add_space_reserve(struct log *log, int bytes)
241 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
243 log->l_grant_reserve_bytes += bytes;
245 log->l_grant_reserve_cycle++;
246 log->l_grant_reserve_bytes = bytes - tmp;
251 xlog_grant_add_space(struct log *log, int bytes)
253 xlog_grant_add_space_write(log, bytes);
254 xlog_grant_add_space_reserve(log, bytes);
258 xlog_tic_reset_res(xlog_ticket_t *tic)
261 tic->t_res_arr_sum = 0;
262 tic->t_res_num_ophdrs = 0;
266 xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
268 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
269 /* add to overflow and start again */
270 tic->t_res_o_flow += tic->t_res_arr_sum;
272 tic->t_res_arr_sum = 0;
275 tic->t_res_arr[tic->t_res_num].r_len = len;
276 tic->t_res_arr[tic->t_res_num].r_type = type;
277 tic->t_res_arr_sum += len;
284 * 1. currblock field gets updated at startup and after in-core logs
285 * marked as with WANT_SYNC.
289 * This routine is called when a user of a log manager ticket is done with
290 * the reservation. If the ticket was ever used, then a commit record for
291 * the associated transaction is written out as a log operation header with
292 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
293 * a given ticket. If the ticket was one with a permanent reservation, then
294 * a few operations are done differently. Permanent reservation tickets by
295 * default don't release the reservation. They just commit the current
296 * transaction with the belief that the reservation is still needed. A flag
297 * must be passed in before permanent reservations are actually released.
298 * When these type of tickets are not released, they need to be set into
299 * the inited state again. By doing this, a start record will be written
300 * out when the next write occurs.
303 xfs_log_done(xfs_mount_t *mp,
304 xfs_log_ticket_t xtic,
308 xlog_t *log = mp->m_log;
309 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
312 if (XLOG_FORCED_SHUTDOWN(log) ||
314 * If nothing was ever written, don't write out commit record.
315 * If we get an error, just continue and give back the log ticket.
317 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
318 (xlog_commit_record(mp, ticket,
319 (xlog_in_core_t **)iclog, &lsn)))) {
320 lsn = (xfs_lsn_t) -1;
321 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
322 flags |= XFS_LOG_REL_PERM_RESERV;
327 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
328 (flags & XFS_LOG_REL_PERM_RESERV)) {
330 * Release ticket if not permanent reservation or a specific
331 * request has been made to release a permanent reservation.
333 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
334 xlog_ungrant_log_space(log, ticket);
335 xlog_ticket_put(log, ticket);
337 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
338 xlog_regrant_reserve_log_space(log, ticket);
339 /* If this ticket was a permanent reservation and we aren't
340 * trying to release it, reset the inited flags; so next time
341 * we write, a start record will be written out.
343 ticket->t_flags |= XLOG_TIC_INITED;
351 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
352 * the force is done synchronously.
354 * Asynchronous forces are implemented by setting the WANT_SYNC
355 * bit in the appropriate in-core log and then returning.
357 * Synchronous forces are implemented with a signal variable. All callers
358 * to force a given lsn to disk will wait on a the sv attached to the
359 * specific in-core log. When given in-core log finally completes its
360 * write to disk, that thread will wake up all threads waiting on the
370 xlog_t *log = mp->m_log;
374 log_flushed = &dummy;
376 ASSERT(flags & XFS_LOG_FORCE);
378 XFS_STATS_INC(xs_log_force);
380 if (log->l_flags & XLOG_IO_ERROR)
381 return XFS_ERROR(EIO);
383 return xlog_state_sync_all(log, flags, log_flushed);
385 return xlog_state_sync(log, lsn, flags, log_flushed);
386 } /* _xfs_log_force */
389 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
390 * about errors or whether the log was flushed or not. This is the normal
391 * interface to use when trying to unpin items or move the log forward.
400 error = _xfs_log_force(mp, lsn, flags, NULL);
402 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
403 "error %d returned.", error);
409 * Attaches a new iclog I/O completion callback routine during
410 * transaction commit. If the log is in error state, a non-zero
411 * return code is handed back and the caller is responsible for
412 * executing the callback at an appropriate time.
415 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
416 void *iclog_hndl, /* iclog to hang callback off */
417 xfs_log_callback_t *cb)
419 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
422 spin_lock(&iclog->ic_callback_lock);
423 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
425 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
426 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
428 *(iclog->ic_callback_tail) = cb;
429 iclog->ic_callback_tail = &(cb->cb_next);
431 spin_unlock(&iclog->ic_callback_lock);
433 } /* xfs_log_notify */
436 xfs_log_release_iclog(xfs_mount_t *mp,
439 xlog_t *log = mp->m_log;
440 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
442 if (xlog_state_release_iclog(log, iclog)) {
443 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
451 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
452 * to the reservation.
453 * 2. Potentially, push buffers at tail of log to disk.
455 * Each reservation is going to reserve extra space for a log record header.
456 * When writes happen to the on-disk log, we don't subtract the length of the
457 * log record header from any reservation. By wasting space in each
458 * reservation, we prevent over allocation problems.
461 xfs_log_reserve(xfs_mount_t *mp,
464 xfs_log_ticket_t *ticket,
469 xlog_t *log = mp->m_log;
470 xlog_ticket_t *internal_ticket;
473 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
474 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
476 if (XLOG_FORCED_SHUTDOWN(log))
477 return XFS_ERROR(EIO);
479 XFS_STATS_INC(xs_try_logspace);
481 if (*ticket != NULL) {
482 ASSERT(flags & XFS_LOG_PERM_RESERV);
483 internal_ticket = (xlog_ticket_t *)*ticket;
484 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
485 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
486 retval = xlog_regrant_write_log_space(log, internal_ticket);
488 /* may sleep if need to allocate more tickets */
489 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
491 if (!internal_ticket)
492 return XFS_ERROR(ENOMEM);
493 internal_ticket->t_trans_type = t_type;
494 *ticket = internal_ticket;
495 xlog_trace_loggrant(log, internal_ticket,
496 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
497 "xfs_log_reserve: create new ticket (permanent trans)" :
498 "xfs_log_reserve: create new ticket");
499 xlog_grant_push_ail(mp,
500 (internal_ticket->t_unit_res *
501 internal_ticket->t_cnt));
502 retval = xlog_grant_log_space(log, internal_ticket);
506 } /* xfs_log_reserve */
510 * Mount a log filesystem
512 * mp - ubiquitous xfs mount point structure
513 * log_target - buftarg of on-disk log device
514 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
515 * num_bblocks - Number of BBSIZE blocks in on-disk log
517 * Return error or zero.
522 xfs_buftarg_t *log_target,
523 xfs_daddr_t blk_offset,
528 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
529 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
532 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
534 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
537 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
540 * Initialize the AIL now we have a log.
542 spin_lock_init(&mp->m_ail_lock);
543 error = xfs_trans_ail_init(mp);
545 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
550 * skip log recovery on a norecovery mount. pretend it all
553 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
554 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
557 mp->m_flags &= ~XFS_MOUNT_RDONLY;
559 error = xlog_recover(mp->m_log);
562 mp->m_flags |= XFS_MOUNT_RDONLY;
564 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
569 /* Normal transactions can now occur */
570 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
572 /* End mounting message in xfs_log_mount_finish */
575 xfs_log_unmount_dealloc(mp);
577 } /* xfs_log_mount */
580 * Finish the recovery of the file system. This is separate from
581 * the xfs_log_mount() call, because it depends on the code in
582 * xfs_mountfs() to read in the root and real-time bitmap inodes
583 * between calling xfs_log_mount() and here.
585 * mp - ubiquitous xfs mount point structure
588 xfs_log_mount_finish(xfs_mount_t *mp)
592 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
593 error = xlog_recover_finish(mp->m_log);
596 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
603 * Unmount processing for the log.
606 xfs_log_unmount(xfs_mount_t *mp)
610 error = xfs_log_unmount_write(mp);
611 xfs_log_unmount_dealloc(mp);
616 * Final log writes as part of unmount.
618 * Mark the filesystem clean as unmount happens. Note that during relocation
619 * this routine needs to be executed as part of source-bag while the
620 * deallocation must not be done until source-end.
624 * Unmount record used to have a string "Unmount filesystem--" in the
625 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
626 * We just write the magic number now since that particular field isn't
627 * currently architecture converted and "nUmount" is a bit foo.
628 * As far as I know, there weren't any dependencies on the old behaviour.
632 xfs_log_unmount_write(xfs_mount_t *mp)
634 xlog_t *log = mp->m_log;
635 xlog_in_core_t *iclog;
637 xlog_in_core_t *first_iclog;
639 xfs_log_iovec_t reg[1];
640 xfs_log_ticket_t tic = NULL;
644 /* the data section must be 32 bit size aligned */
648 __uint32_t pad2; /* may as well make it 64 bits */
649 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
652 * Don't write out unmount record on read-only mounts.
653 * Or, if we are doing a forced umount (typically because of IO errors).
655 if (mp->m_flags & XFS_MOUNT_RDONLY)
658 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
659 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
662 first_iclog = iclog = log->l_iclog;
664 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
665 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
666 ASSERT(iclog->ic_offset == 0);
668 iclog = iclog->ic_next;
669 } while (iclog != first_iclog);
671 if (! (XLOG_FORCED_SHUTDOWN(log))) {
672 reg[0].i_addr = (void*)&magic;
673 reg[0].i_len = sizeof(magic);
674 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
676 error = xfs_log_reserve(mp, 600, 1, &tic,
677 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
679 /* remove inited flag */
680 ((xlog_ticket_t *)tic)->t_flags = 0;
681 error = xlog_write(mp, reg, 1, tic, &lsn,
682 NULL, XLOG_UNMOUNT_TRANS);
684 * At this point, we're umounting anyway,
685 * so there's no point in transitioning log state
686 * to IOERROR. Just continue...
691 xfs_fs_cmn_err(CE_ALERT, mp,
692 "xfs_log_unmount: unmount record failed");
696 spin_lock(&log->l_icloglock);
697 iclog = log->l_iclog;
698 atomic_inc(&iclog->ic_refcnt);
699 spin_unlock(&log->l_icloglock);
700 xlog_state_want_sync(log, iclog);
701 error = xlog_state_release_iclog(log, iclog);
703 spin_lock(&log->l_icloglock);
704 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
705 iclog->ic_state == XLOG_STATE_DIRTY)) {
706 if (!XLOG_FORCED_SHUTDOWN(log)) {
707 sv_wait(&iclog->ic_force_wait, PMEM,
708 &log->l_icloglock, s);
710 spin_unlock(&log->l_icloglock);
713 spin_unlock(&log->l_icloglock);
716 xlog_trace_loggrant(log, tic, "unmount rec");
717 xlog_ungrant_log_space(log, tic);
718 xlog_ticket_put(log, tic);
722 * We're already in forced_shutdown mode, couldn't
723 * even attempt to write out the unmount transaction.
725 * Go through the motions of sync'ing and releasing
726 * the iclog, even though no I/O will actually happen,
727 * we need to wait for other log I/Os that may already
728 * be in progress. Do this as a separate section of
729 * code so we'll know if we ever get stuck here that
730 * we're in this odd situation of trying to unmount
731 * a file system that went into forced_shutdown as
732 * the result of an unmount..
734 spin_lock(&log->l_icloglock);
735 iclog = log->l_iclog;
736 atomic_inc(&iclog->ic_refcnt);
737 spin_unlock(&log->l_icloglock);
739 xlog_state_want_sync(log, iclog);
740 error = xlog_state_release_iclog(log, iclog);
742 spin_lock(&log->l_icloglock);
744 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
745 || iclog->ic_state == XLOG_STATE_DIRTY
746 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
748 sv_wait(&iclog->ic_force_wait, PMEM,
749 &log->l_icloglock, s);
751 spin_unlock(&log->l_icloglock);
756 } /* xfs_log_unmount_write */
759 * Deallocate log structures for unmount/relocation.
761 * We need to stop the aild from running before we destroy
762 * and deallocate the log as the aild references the log.
765 xfs_log_unmount_dealloc(xfs_mount_t *mp)
767 xfs_trans_ail_destroy(mp);
768 xlog_dealloc_log(mp->m_log);
772 * Write region vectors to log. The write happens using the space reservation
773 * of the ticket (tic). It is not a requirement that all writes for a given
774 * transaction occur with one call to xfs_log_write().
777 xfs_log_write(xfs_mount_t * mp,
778 xfs_log_iovec_t reg[],
780 xfs_log_ticket_t tic,
781 xfs_lsn_t *start_lsn)
784 xlog_t *log = mp->m_log;
786 if (XLOG_FORCED_SHUTDOWN(log))
787 return XFS_ERROR(EIO);
789 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
790 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
793 } /* xfs_log_write */
797 xfs_log_move_tail(xfs_mount_t *mp,
801 xlog_t *log = mp->m_log;
802 int need_bytes, free_bytes, cycle, bytes;
804 if (XLOG_FORCED_SHUTDOWN(log))
808 /* needed since sync_lsn is 64 bits */
809 spin_lock(&log->l_icloglock);
810 tail_lsn = log->l_last_sync_lsn;
811 spin_unlock(&log->l_icloglock);
814 spin_lock(&log->l_grant_lock);
816 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
820 log->l_tail_lsn = tail_lsn;
823 if ((tic = log->l_write_headq)) {
825 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
826 panic("Recovery problem");
828 cycle = log->l_grant_write_cycle;
829 bytes = log->l_grant_write_bytes;
830 free_bytes = xlog_space_left(log, cycle, bytes);
832 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
834 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
837 free_bytes -= tic->t_unit_res;
838 sv_signal(&tic->t_wait);
840 } while (tic != log->l_write_headq);
842 if ((tic = log->l_reserve_headq)) {
844 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
845 panic("Recovery problem");
847 cycle = log->l_grant_reserve_cycle;
848 bytes = log->l_grant_reserve_bytes;
849 free_bytes = xlog_space_left(log, cycle, bytes);
851 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
852 need_bytes = tic->t_unit_res*tic->t_cnt;
854 need_bytes = tic->t_unit_res;
855 if (free_bytes < need_bytes && tail_lsn != 1)
858 free_bytes -= need_bytes;
859 sv_signal(&tic->t_wait);
861 } while (tic != log->l_reserve_headq);
863 spin_unlock(&log->l_grant_lock);
864 } /* xfs_log_move_tail */
867 * Determine if we have a transaction that has gone to disk
868 * that needs to be covered. Log activity needs to be idle (no AIL and
869 * nothing in the iclogs). And, we need to be in the right state indicating
870 * something has gone out.
873 xfs_log_need_covered(xfs_mount_t *mp)
876 xlog_t *log = mp->m_log;
878 if (!xfs_fs_writable(mp))
881 spin_lock(&log->l_icloglock);
882 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
883 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
884 && !xfs_trans_first_ail(mp, &gen)
885 && xlog_iclogs_empty(log)) {
886 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
887 log->l_covered_state = XLOG_STATE_COVER_DONE;
889 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
890 log->l_covered_state = XLOG_STATE_COVER_DONE2;
894 spin_unlock(&log->l_icloglock);
898 /******************************************************************************
902 ******************************************************************************
905 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
906 * The log manager must keep track of the last LR which was committed
907 * to disk. The lsn of this LR will become the new tail_lsn whenever
908 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
909 * the situation where stuff could be written into the log but nothing
910 * was ever in the AIL when asked. Eventually, we panic since the
911 * tail hits the head.
913 * We may be holding the log iclog lock upon entering this routine.
916 xlog_assign_tail_lsn(xfs_mount_t *mp)
919 xlog_t *log = mp->m_log;
921 tail_lsn = xfs_trans_tail_ail(mp);
922 spin_lock(&log->l_grant_lock);
924 log->l_tail_lsn = tail_lsn;
926 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
928 spin_unlock(&log->l_grant_lock);
931 } /* xlog_assign_tail_lsn */
935 * Return the space in the log between the tail and the head. The head
936 * is passed in the cycle/bytes formal parms. In the special case where
937 * the reserve head has wrapped passed the tail, this calculation is no
938 * longer valid. In this case, just return 0 which means there is no space
939 * in the log. This works for all places where this function is called
940 * with the reserve head. Of course, if the write head were to ever
941 * wrap the tail, we should blow up. Rather than catch this case here,
942 * we depend on other ASSERTions in other parts of the code. XXXmiken
944 * This code also handles the case where the reservation head is behind
945 * the tail. The details of this case are described below, but the end
946 * result is that we return the size of the log as the amount of space left.
949 xlog_space_left(xlog_t *log, int cycle, int bytes)
955 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
956 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
957 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
958 free_bytes = log->l_logsize - (bytes - tail_bytes);
959 } else if ((tail_cycle + 1) < cycle) {
961 } else if (tail_cycle < cycle) {
962 ASSERT(tail_cycle == (cycle - 1));
963 free_bytes = tail_bytes - bytes;
966 * The reservation head is behind the tail.
967 * In this case we just want to return the size of the
968 * log as the amount of space left.
970 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
971 "xlog_space_left: head behind tail\n"
972 " tail_cycle = %d, tail_bytes = %d\n"
973 " GH cycle = %d, GH bytes = %d",
974 tail_cycle, tail_bytes, cycle, bytes);
976 free_bytes = log->l_logsize;
979 } /* xlog_space_left */
983 * Log function which is called when an io completes.
985 * The log manager needs its own routine, in order to control what
986 * happens with the buffer after the write completes.
989 xlog_iodone(xfs_buf_t *bp)
991 xlog_in_core_t *iclog;
995 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
996 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
997 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1001 * Some versions of cpp barf on the recursive definition of
1002 * ic_log -> hic_fields.ic_log and expand ic_log twice when
1003 * it is passed through two macros. Workaround broken cpp.
1008 * If the ordered flag has been removed by a lower
1009 * layer, it means the underlyin device no longer supports
1010 * barrier I/O. Warn loudly and turn off barriers.
1012 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
1013 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1014 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1015 "xlog_iodone: Barriers are no longer supported"
1016 " by device. Disabling barriers\n");
1017 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1021 * Race to shutdown the filesystem if we see an error.
1023 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1024 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1025 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1027 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
1029 * This flag will be propagated to the trans-committed
1030 * callback routines to let them know that the log-commit
1033 aborted = XFS_LI_ABORTED;
1034 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1035 aborted = XFS_LI_ABORTED;
1038 /* log I/O is always issued ASYNC */
1039 ASSERT(XFS_BUF_ISASYNC(bp));
1040 xlog_state_done_syncing(iclog, aborted);
1042 * do not reference the buffer (bp) here as we could race
1043 * with it being freed after writing the unmount record to the
1050 * The bdstrat callback function for log bufs. This gives us a central
1051 * place to trap bufs in case we get hit by a log I/O error and need to
1052 * shutdown. Actually, in practice, even when we didn't get a log error,
1053 * we transition the iclogs to IOERROR state *after* flushing all existing
1054 * iclogs to disk. This is because we don't want anymore new transactions to be
1055 * started or completed afterwards.
1058 xlog_bdstrat_cb(struct xfs_buf *bp)
1060 xlog_in_core_t *iclog;
1062 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1064 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1065 /* note for irix bstrat will need struct bdevsw passed
1066 * Fix the following macro if the code ever is merged
1072 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1073 XFS_BUF_ERROR(bp, EIO);
1076 return XFS_ERROR(EIO);
1082 * Return size of each in-core log record buffer.
1084 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
1086 * If the filesystem blocksize is too large, we may need to choose a
1087 * larger size since the directory code currently logs entire blocks.
1091 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1097 if (mp->m_logbufs <= 0)
1098 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1100 log->l_iclog_bufs = mp->m_logbufs;
1103 * Buffer size passed in from mount system call.
1105 if (mp->m_logbsize > 0) {
1106 size = log->l_iclog_size = mp->m_logbsize;
1107 log->l_iclog_size_log = 0;
1109 log->l_iclog_size_log++;
1113 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1114 /* # headers = size / 32K
1115 * one header holds cycles from 32K of data
1118 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1119 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1121 log->l_iclog_hsize = xhdrs << BBSHIFT;
1122 log->l_iclog_heads = xhdrs;
1124 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1125 log->l_iclog_hsize = BBSIZE;
1126 log->l_iclog_heads = 1;
1131 /* All machines use 32KB buffers by default. */
1132 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1133 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1135 /* the default log size is 16k or 32k which is one header sector */
1136 log->l_iclog_hsize = BBSIZE;
1137 log->l_iclog_heads = 1;
1140 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1141 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1143 if (mp->m_sb.sb_blocksize >= 16*1024) {
1144 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1145 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1146 if (mp->m_logbufs <= 0) {
1147 switch (mp->m_sb.sb_blocksize) {
1148 case 16*1024: /* 16 KB */
1149 log->l_iclog_bufs = 3;
1151 case 32*1024: /* 32 KB */
1152 log->l_iclog_bufs = 4;
1154 case 64*1024: /* 64 KB */
1155 log->l_iclog_bufs = 8;
1158 xlog_panic("XFS: Invalid blocksize");
1164 done: /* are we being asked to make the sizes selected above visible? */
1165 if (mp->m_logbufs == 0)
1166 mp->m_logbufs = log->l_iclog_bufs;
1167 if (mp->m_logbsize == 0)
1168 mp->m_logbsize = log->l_iclog_size;
1169 } /* xlog_get_iclog_buffer_size */
1173 * This routine initializes some of the log structure for a given mount point.
1174 * Its primary purpose is to fill in enough, so recovery can occur. However,
1175 * some other stuff may be filled in too.
1178 xlog_alloc_log(xfs_mount_t *mp,
1179 xfs_buftarg_t *log_target,
1180 xfs_daddr_t blk_offset,
1184 xlog_rec_header_t *head;
1185 xlog_in_core_t **iclogp;
1186 xlog_in_core_t *iclog, *prev_iclog=NULL;
1191 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1194 log->l_targ = log_target;
1195 log->l_logsize = BBTOB(num_bblks);
1196 log->l_logBBstart = blk_offset;
1197 log->l_logBBsize = num_bblks;
1198 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1199 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1201 log->l_prev_block = -1;
1202 log->l_tail_lsn = xlog_assign_lsn(1, 0);
1203 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1204 log->l_last_sync_lsn = log->l_tail_lsn;
1205 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1206 log->l_grant_reserve_cycle = 1;
1207 log->l_grant_write_cycle = 1;
1209 if (xfs_sb_version_hassector(&mp->m_sb)) {
1210 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1211 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1212 /* for larger sector sizes, must have v2 or external log */
1213 ASSERT(log->l_sectbb_log == 0 ||
1214 log->l_logBBstart == 0 ||
1215 xfs_sb_version_haslogv2(&mp->m_sb));
1216 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1218 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1220 xlog_get_iclog_buffer_size(mp, log);
1222 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1223 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1224 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1225 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1226 ASSERT(XFS_BUF_ISBUSY(bp));
1227 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1230 spin_lock_init(&log->l_icloglock);
1231 spin_lock_init(&log->l_grant_lock);
1232 sv_init(&log->l_flush_wait, 0, "flush_wait");
1234 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1235 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1237 iclogp = &log->l_iclog;
1239 * The amount of memory to allocate for the iclog structure is
1240 * rather funky due to the way the structure is defined. It is
1241 * done this way so that we can use different sizes for machines
1242 * with different amounts of memory. See the definition of
1243 * xlog_in_core_t in xfs_log_priv.h for details.
1245 iclogsize = log->l_iclog_size;
1246 ASSERT(log->l_iclog_size >= 4096);
1247 for (i=0; i < log->l_iclog_bufs; i++) {
1248 *iclogp = (xlog_in_core_t *)
1249 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1251 iclog->ic_prev = prev_iclog;
1254 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1255 if (!XFS_BUF_CPSEMA(bp))
1257 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1258 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1259 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1261 iclog->hic_data = bp->b_addr;
1263 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1265 head = &iclog->ic_header;
1266 memset(head, 0, sizeof(xlog_rec_header_t));
1267 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1268 head->h_version = cpu_to_be32(
1269 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1270 head->h_size = cpu_to_be32(log->l_iclog_size);
1272 head->h_fmt = cpu_to_be32(XLOG_FMT);
1273 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1275 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1276 iclog->ic_state = XLOG_STATE_ACTIVE;
1277 iclog->ic_log = log;
1278 atomic_set(&iclog->ic_refcnt, 0);
1279 spin_lock_init(&iclog->ic_callback_lock);
1280 iclog->ic_callback_tail = &(iclog->ic_callback);
1281 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1283 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1284 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1285 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1286 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
1288 iclogp = &iclog->ic_next;
1290 *iclogp = log->l_iclog; /* complete ring */
1291 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1294 } /* xlog_alloc_log */
1298 * Write out the commit record of a transaction associated with the given
1299 * ticket. Return the lsn of the commit record.
1302 xlog_commit_record(xfs_mount_t *mp,
1303 xlog_ticket_t *ticket,
1304 xlog_in_core_t **iclog,
1305 xfs_lsn_t *commitlsnp)
1308 xfs_log_iovec_t reg[1];
1310 reg[0].i_addr = NULL;
1312 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_COMMIT);
1314 ASSERT_ALWAYS(iclog);
1315 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1316 iclog, XLOG_COMMIT_TRANS))) {
1317 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1320 } /* xlog_commit_record */
1324 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1325 * log space. This code pushes on the lsn which would supposedly free up
1326 * the 25% which we want to leave free. We may need to adopt a policy which
1327 * pushes on an lsn which is further along in the log once we reach the high
1328 * water mark. In this manner, we would be creating a low water mark.
1331 xlog_grant_push_ail(xfs_mount_t *mp,
1334 xlog_t *log = mp->m_log; /* pointer to the log */
1335 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1336 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1337 int free_blocks; /* free blocks left to write to */
1338 int free_bytes; /* free bytes left to write to */
1339 int threshold_block; /* block in lsn we'd like to be at */
1340 int threshold_cycle; /* lsn cycle we'd like to be at */
1343 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1345 spin_lock(&log->l_grant_lock);
1346 free_bytes = xlog_space_left(log,
1347 log->l_grant_reserve_cycle,
1348 log->l_grant_reserve_bytes);
1349 tail_lsn = log->l_tail_lsn;
1350 free_blocks = BTOBBT(free_bytes);
1353 * Set the threshold for the minimum number of free blocks in the
1354 * log to the maximum of what the caller needs, one quarter of the
1355 * log, and 256 blocks.
1357 free_threshold = BTOBB(need_bytes);
1358 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1359 free_threshold = MAX(free_threshold, 256);
1360 if (free_blocks < free_threshold) {
1361 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1362 threshold_cycle = CYCLE_LSN(tail_lsn);
1363 if (threshold_block >= log->l_logBBsize) {
1364 threshold_block -= log->l_logBBsize;
1365 threshold_cycle += 1;
1367 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
1369 /* Don't pass in an lsn greater than the lsn of the last
1370 * log record known to be on disk.
1372 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1373 threshold_lsn = log->l_last_sync_lsn;
1375 spin_unlock(&log->l_grant_lock);
1378 * Get the transaction layer to kick the dirty buffers out to
1379 * disk asynchronously. No point in trying to do this if
1380 * the filesystem is shutting down.
1382 if (threshold_lsn &&
1383 !XLOG_FORCED_SHUTDOWN(log))
1384 xfs_trans_push_ail(mp, threshold_lsn);
1385 } /* xlog_grant_push_ail */
1389 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1390 * fashion. Previously, we should have moved the current iclog
1391 * ptr in the log to point to the next available iclog. This allows further
1392 * write to continue while this code syncs out an iclog ready to go.
1393 * Before an in-core log can be written out, the data section must be scanned
1394 * to save away the 1st word of each BBSIZE block into the header. We replace
1395 * it with the current cycle count. Each BBSIZE block is tagged with the
1396 * cycle count because there in an implicit assumption that drives will
1397 * guarantee that entire 512 byte blocks get written at once. In other words,
1398 * we can't have part of a 512 byte block written and part not written. By
1399 * tagging each block, we will know which blocks are valid when recovering
1400 * after an unclean shutdown.
1402 * This routine is single threaded on the iclog. No other thread can be in
1403 * this routine with the same iclog. Changing contents of iclog can there-
1404 * fore be done without grabbing the state machine lock. Updating the global
1405 * log will require grabbing the lock though.
1407 * The entire log manager uses a logical block numbering scheme. Only
1408 * log_sync (and then only bwrite()) know about the fact that the log may
1409 * not start with block zero on a given device. The log block start offset
1410 * is added immediately before calling bwrite().
1414 xlog_sync(xlog_t *log,
1415 xlog_in_core_t *iclog)
1417 xfs_caddr_t dptr; /* pointer to byte sized element */
1420 uint count; /* byte count of bwrite */
1421 uint count_init; /* initial count before roundup */
1422 int roundoff; /* roundoff to BB or stripe */
1423 int split = 0; /* split write into two regions */
1425 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
1427 XFS_STATS_INC(xs_log_writes);
1428 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1430 /* Add for LR header */
1431 count_init = log->l_iclog_hsize + iclog->ic_offset;
1433 /* Round out the log write size */
1434 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1435 /* we have a v2 stripe unit to use */
1436 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1438 count = BBTOB(BTOBB(count_init));
1440 roundoff = count - count_init;
1441 ASSERT(roundoff >= 0);
1442 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1443 roundoff < log->l_mp->m_sb.sb_logsunit)
1445 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1446 roundoff < BBTOB(1)));
1448 /* move grant heads by roundoff in sync */
1449 spin_lock(&log->l_grant_lock);
1450 xlog_grant_add_space(log, roundoff);
1451 spin_unlock(&log->l_grant_lock);
1453 /* put cycle number in every block */
1454 xlog_pack_data(log, iclog, roundoff);
1456 /* real byte length */
1458 iclog->ic_header.h_len =
1459 cpu_to_be32(iclog->ic_offset + roundoff);
1461 iclog->ic_header.h_len =
1462 cpu_to_be32(iclog->ic_offset);
1466 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1467 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1468 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
1470 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1472 /* Do we need to split this write into 2 parts? */
1473 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1474 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1475 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1476 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1478 iclog->ic_bwritecnt = 1;
1480 XFS_BUF_SET_COUNT(bp, count);
1481 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1482 XFS_BUF_ZEROFLAGS(bp);
1486 * Do an ordered write for the log block.
1487 * Its unnecessary to flush the first split block in the log wrap case.
1489 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
1490 XFS_BUF_ORDERED(bp);
1492 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1493 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1495 xlog_verify_iclog(log, iclog, count, B_TRUE);
1497 /* account for log which doesn't start at block #0 */
1498 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1500 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1505 if ((error = XFS_bwrite(bp))) {
1506 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1511 bp = iclog->ic_log->l_xbuf;
1512 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1514 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1515 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1516 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1517 (__psint_t)count), split);
1518 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1519 XFS_BUF_ZEROFLAGS(bp);
1522 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1523 XFS_BUF_ORDERED(bp);
1524 dptr = XFS_BUF_PTR(bp);
1526 * Bump the cycle numbers at the start of each block
1527 * since this part of the buffer is at the start of
1528 * a new cycle. Watch out for the header magic number
1531 for (i = 0; i < split; i += BBSIZE) {
1532 be32_add_cpu((__be32 *)dptr, 1);
1533 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
1534 be32_add_cpu((__be32 *)dptr, 1);
1538 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1539 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1541 /* account for internal log which doesn't start at block #0 */
1542 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1544 if ((error = XFS_bwrite(bp))) {
1545 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1546 bp, XFS_BUF_ADDR(bp));
1555 * Deallocate a log structure
1558 xlog_dealloc_log(xlog_t *log)
1560 xlog_in_core_t *iclog, *next_iclog;
1563 iclog = log->l_iclog;
1564 for (i=0; i<log->l_iclog_bufs; i++) {
1565 sv_destroy(&iclog->ic_force_wait);
1566 sv_destroy(&iclog->ic_write_wait);
1567 xfs_buf_free(iclog->ic_bp);
1568 #ifdef XFS_LOG_TRACE
1569 if (iclog->ic_trace != NULL) {
1570 ktrace_free(iclog->ic_trace);
1573 next_iclog = iclog->ic_next;
1577 spinlock_destroy(&log->l_icloglock);
1578 spinlock_destroy(&log->l_grant_lock);
1580 xfs_buf_free(log->l_xbuf);
1581 #ifdef XFS_LOG_TRACE
1582 if (log->l_trace != NULL) {
1583 ktrace_free(log->l_trace);
1585 if (log->l_grant_trace != NULL) {
1586 ktrace_free(log->l_grant_trace);
1589 log->l_mp->m_log = NULL;
1591 } /* xlog_dealloc_log */
1594 * Update counters atomically now that memcpy is done.
1598 xlog_state_finish_copy(xlog_t *log,
1599 xlog_in_core_t *iclog,
1603 spin_lock(&log->l_icloglock);
1605 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1606 iclog->ic_offset += copy_bytes;
1608 spin_unlock(&log->l_icloglock);
1609 } /* xlog_state_finish_copy */
1615 * print out info relating to regions written which consume
1619 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1622 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1624 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1625 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1646 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1689 xfs_fs_cmn_err(CE_WARN, mp,
1690 "xfs_log_write: reservation summary:\n"
1691 " trans type = %s (%u)\n"
1692 " unit res = %d bytes\n"
1693 " current res = %d bytes\n"
1694 " total reg = %u bytes (o/flow = %u bytes)\n"
1695 " ophdrs = %u (ophdr space = %u bytes)\n"
1696 " ophdr + reg = %u bytes\n"
1697 " num regions = %u\n",
1698 ((ticket->t_trans_type <= 0 ||
1699 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1700 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1701 ticket->t_trans_type,
1704 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1705 ticket->t_res_num_ophdrs, ophdr_spc,
1706 ticket->t_res_arr_sum +
1707 ticket->t_res_o_flow + ophdr_spc,
1710 for (i = 0; i < ticket->t_res_num; i++) {
1711 uint r_type = ticket->t_res_arr[i].r_type;
1713 "region[%u]: %s - %u bytes\n",
1715 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1716 "bad-rtype" : res_type_str[r_type-1]),
1717 ticket->t_res_arr[i].r_len);
1722 * Write some region out to in-core log
1724 * This will be called when writing externally provided regions or when
1725 * writing out a commit record for a given transaction.
1727 * General algorithm:
1728 * 1. Find total length of this write. This may include adding to the
1729 * lengths passed in.
1730 * 2. Check whether we violate the tickets reservation.
1731 * 3. While writing to this iclog
1732 * A. Reserve as much space in this iclog as can get
1733 * B. If this is first write, save away start lsn
1734 * C. While writing this region:
1735 * 1. If first write of transaction, write start record
1736 * 2. Write log operation header (header per region)
1737 * 3. Find out if we can fit entire region into this iclog
1738 * 4. Potentially, verify destination memcpy ptr
1739 * 5. Memcpy (partial) region
1740 * 6. If partial copy, release iclog; otherwise, continue
1741 * copying more regions into current iclog
1742 * 4. Mark want sync bit (in simulation mode)
1743 * 5. Release iclog for potential flush to on-disk log.
1746 * 1. Panic if reservation is overrun. This should never happen since
1747 * reservation amounts are generated internal to the filesystem.
1749 * 1. Tickets are single threaded data structures.
1750 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1751 * syncing routine. When a single log_write region needs to span
1752 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1753 * on all log operation writes which don't contain the end of the
1754 * region. The XLOG_END_TRANS bit is used for the in-core log
1755 * operation which contains the end of the continued log_write region.
1756 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1757 * we don't really know exactly how much space will be used. As a result,
1758 * we don't update ic_offset until the end when we know exactly how many
1759 * bytes have been written out.
1762 xlog_write(xfs_mount_t * mp,
1763 xfs_log_iovec_t reg[],
1765 xfs_log_ticket_t tic,
1766 xfs_lsn_t *start_lsn,
1767 xlog_in_core_t **commit_iclog,
1770 xlog_t *log = mp->m_log;
1771 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1772 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
1773 xlog_op_header_t *logop_head; /* ptr to log operation header */
1774 __psint_t ptr; /* copy address into data region */
1775 int len; /* # xlog_write() bytes 2 still copy */
1776 int index; /* region index currently copying */
1777 int log_offset; /* offset (from 0) into data region */
1778 int start_rec_copy; /* # bytes to copy for start record */
1779 int partial_copy; /* did we split a region? */
1780 int partial_copy_len;/* # bytes copied if split region */
1781 int need_copy; /* # bytes need to memcpy this region */
1782 int copy_len; /* # bytes actually memcpy'ing */
1783 int copy_off; /* # bytes from entry start */
1784 int contwr; /* continued write of in-core log? */
1786 int record_cnt = 0, data_cnt = 0;
1788 partial_copy_len = partial_copy = 0;
1790 /* Calculate potential maximum space. Each region gets its own
1791 * xlog_op_header_t and may need to be double word aligned.
1794 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1795 len += sizeof(xlog_op_header_t);
1796 ticket->t_res_num_ophdrs++;
1799 for (index = 0; index < nentries; index++) {
1800 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1801 ticket->t_res_num_ophdrs++;
1802 len += reg[index].i_len;
1803 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
1805 contwr = *start_lsn = 0;
1807 if (ticket->t_curr_res < len) {
1808 xlog_print_tic_res(mp, ticket);
1811 "xfs_log_write: reservation ran out. Need to up reservation");
1813 /* Customer configurable panic */
1814 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1815 "xfs_log_write: reservation ran out. Need to up reservation");
1816 /* If we did not panic, shutdown the filesystem */
1817 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1820 ticket->t_curr_res -= len;
1822 for (index = 0; index < nentries; ) {
1823 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1824 &contwr, &log_offset)))
1827 ASSERT(log_offset <= iclog->ic_size - 1);
1828 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1830 /* start_lsn is the first lsn written to. That's all we need. */
1832 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
1834 /* This loop writes out as many regions as can fit in the amount
1835 * of space which was allocated by xlog_state_get_iclog_space().
1837 while (index < nentries) {
1838 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1839 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1842 /* If first write for transaction, insert start record.
1843 * We can't be trying to commit if we are inited. We can't
1844 * have any "partial_copy" if we are inited.
1846 if (ticket->t_flags & XLOG_TIC_INITED) {
1847 logop_head = (xlog_op_header_t *)ptr;
1848 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1849 logop_head->oh_clientid = ticket->t_clientid;
1850 logop_head->oh_len = 0;
1851 logop_head->oh_flags = XLOG_START_TRANS;
1852 logop_head->oh_res2 = 0;
1853 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1856 start_rec_copy = sizeof(xlog_op_header_t);
1857 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1860 /* Copy log operation header directly into data section */
1861 logop_head = (xlog_op_header_t *)ptr;
1862 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1863 logop_head->oh_clientid = ticket->t_clientid;
1864 logop_head->oh_res2 = 0;
1866 /* header copied directly */
1867 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1869 /* are we copying a commit or unmount record? */
1870 logop_head->oh_flags = flags;
1873 * We've seen logs corrupted with bad transaction client
1874 * ids. This makes sure that XFS doesn't generate them on.
1875 * Turn this into an EIO and shut down the filesystem.
1877 switch (logop_head->oh_clientid) {
1878 case XFS_TRANSACTION:
1883 xfs_fs_cmn_err(CE_WARN, mp,
1884 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1885 logop_head->oh_clientid, tic);
1886 return XFS_ERROR(EIO);
1889 /* Partial write last time? => (partial_copy != 0)
1890 * need_copy is the amount we'd like to copy if everything could
1891 * fit in the current memcpy.
1893 need_copy = reg[index].i_len - partial_copy_len;
1895 copy_off = partial_copy_len;
1896 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1897 copy_len = need_copy;
1898 logop_head->oh_len = cpu_to_be32(copy_len);
1900 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1901 partial_copy_len = partial_copy = 0;
1902 } else { /* partial write */
1903 copy_len = iclog->ic_size - log_offset;
1904 logop_head->oh_len = cpu_to_be32(copy_len);
1905 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1907 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1908 partial_copy_len += copy_len;
1910 len += sizeof(xlog_op_header_t); /* from splitting of region */
1911 /* account for new log op header */
1912 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1913 ticket->t_res_num_ophdrs++;
1915 xlog_verify_dest_ptr(log, ptr);
1918 ASSERT(copy_len >= 0);
1919 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1920 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1922 /* make copy_len total bytes copied, including headers */
1923 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1925 data_cnt += contwr ? copy_len : 0;
1926 if (partial_copy) { /* copied partial region */
1927 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1928 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1929 record_cnt = data_cnt = 0;
1930 if ((error = xlog_state_release_iclog(log, iclog)))
1932 break; /* don't increment index */
1933 } else { /* copied entire region */
1935 partial_copy_len = partial_copy = 0;
1937 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1938 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1939 record_cnt = data_cnt = 0;
1940 xlog_state_want_sync(log, iclog);
1942 ASSERT(flags & XLOG_COMMIT_TRANS);
1943 *commit_iclog = iclog;
1944 } else if ((error = xlog_state_release_iclog(log, iclog)))
1946 if (index == nentries)
1947 return 0; /* we are done */
1951 } /* if (partial_copy) */
1952 } /* while (index < nentries) */
1953 } /* for (index = 0; index < nentries; ) */
1956 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1958 ASSERT(flags & XLOG_COMMIT_TRANS);
1959 *commit_iclog = iclog;
1962 return xlog_state_release_iclog(log, iclog);
1966 /*****************************************************************************
1968 * State Machine functions
1970 *****************************************************************************
1973 /* Clean iclogs starting from the head. This ordering must be
1974 * maintained, so an iclog doesn't become ACTIVE beyond one that
1975 * is SYNCING. This is also required to maintain the notion that we use
1976 * a ordered wait queue to hold off would be writers to the log when every
1977 * iclog is trying to sync to disk.
1979 * State Change: DIRTY -> ACTIVE
1982 xlog_state_clean_log(xlog_t *log)
1984 xlog_in_core_t *iclog;
1987 iclog = log->l_iclog;
1989 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1990 iclog->ic_state = XLOG_STATE_ACTIVE;
1991 iclog->ic_offset = 0;
1992 ASSERT(iclog->ic_callback == NULL);
1994 * If the number of ops in this iclog indicate it just
1995 * contains the dummy transaction, we can
1996 * change state into IDLE (the second time around).
1997 * Otherwise we should change the state into
1999 * We don't need to cover the dummy.
2002 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2007 * We have two dirty iclogs so start over
2008 * This could also be num of ops indicates
2009 * this is not the dummy going out.
2013 iclog->ic_header.h_num_logops = 0;
2014 memset(iclog->ic_header.h_cycle_data, 0,
2015 sizeof(iclog->ic_header.h_cycle_data));
2016 iclog->ic_header.h_lsn = 0;
2017 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2020 break; /* stop cleaning */
2021 iclog = iclog->ic_next;
2022 } while (iclog != log->l_iclog);
2024 /* log is locked when we are called */
2026 * Change state for the dummy log recording.
2027 * We usually go to NEED. But we go to NEED2 if the changed indicates
2028 * we are done writing the dummy record.
2029 * If we are done with the second dummy recored (DONE2), then
2033 switch (log->l_covered_state) {
2034 case XLOG_STATE_COVER_IDLE:
2035 case XLOG_STATE_COVER_NEED:
2036 case XLOG_STATE_COVER_NEED2:
2037 log->l_covered_state = XLOG_STATE_COVER_NEED;
2040 case XLOG_STATE_COVER_DONE:
2042 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2044 log->l_covered_state = XLOG_STATE_COVER_NEED;
2047 case XLOG_STATE_COVER_DONE2:
2049 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2051 log->l_covered_state = XLOG_STATE_COVER_NEED;
2058 } /* xlog_state_clean_log */
2061 xlog_get_lowest_lsn(
2064 xlog_in_core_t *lsn_log;
2065 xfs_lsn_t lowest_lsn, lsn;
2067 lsn_log = log->l_iclog;
2070 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2071 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
2072 if ((lsn && !lowest_lsn) ||
2073 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2077 lsn_log = lsn_log->ic_next;
2078 } while (lsn_log != log->l_iclog);
2084 xlog_state_do_callback(
2087 xlog_in_core_t *ciclog)
2089 xlog_in_core_t *iclog;
2090 xlog_in_core_t *first_iclog; /* used to know when we've
2091 * processed all iclogs once */
2092 xfs_log_callback_t *cb, *cb_next;
2094 xfs_lsn_t lowest_lsn;
2095 int ioerrors; /* counter: iclogs with errors */
2096 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2097 int funcdidcallbacks; /* flag: function did callbacks */
2098 int repeats; /* for issuing console warnings if
2099 * looping too many times */
2102 spin_lock(&log->l_icloglock);
2103 first_iclog = iclog = log->l_iclog;
2105 funcdidcallbacks = 0;
2110 * Scan all iclogs starting with the one pointed to by the
2111 * log. Reset this starting point each time the log is
2112 * unlocked (during callbacks).
2114 * Keep looping through iclogs until one full pass is made
2115 * without running any callbacks.
2117 first_iclog = log->l_iclog;
2118 iclog = log->l_iclog;
2119 loopdidcallbacks = 0;
2124 /* skip all iclogs in the ACTIVE & DIRTY states */
2125 if (iclog->ic_state &
2126 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2127 iclog = iclog->ic_next;
2132 * Between marking a filesystem SHUTDOWN and stopping
2133 * the log, we do flush all iclogs to disk (if there
2134 * wasn't a log I/O error). So, we do want things to
2135 * go smoothly in case of just a SHUTDOWN w/o a
2138 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2140 * Can only perform callbacks in order. Since
2141 * this iclog is not in the DONE_SYNC/
2142 * DO_CALLBACK state, we skip the rest and
2143 * just try to clean up. If we set our iclog
2144 * to DO_CALLBACK, we will not process it when
2145 * we retry since a previous iclog is in the
2146 * CALLBACK and the state cannot change since
2147 * we are holding the l_icloglock.
2149 if (!(iclog->ic_state &
2150 (XLOG_STATE_DONE_SYNC |
2151 XLOG_STATE_DO_CALLBACK))) {
2152 if (ciclog && (ciclog->ic_state ==
2153 XLOG_STATE_DONE_SYNC)) {
2154 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2159 * We now have an iclog that is in either the
2160 * DO_CALLBACK or DONE_SYNC states. The other
2161 * states (WANT_SYNC, SYNCING, or CALLBACK were
2162 * caught by the above if and are going to
2163 * clean (i.e. we aren't doing their callbacks)
2168 * We will do one more check here to see if we
2169 * have chased our tail around.
2172 lowest_lsn = xlog_get_lowest_lsn(log);
2174 XFS_LSN_CMP(lowest_lsn,
2175 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
2176 iclog = iclog->ic_next;
2177 continue; /* Leave this iclog for
2181 iclog->ic_state = XLOG_STATE_CALLBACK;
2183 spin_unlock(&log->l_icloglock);
2185 /* l_last_sync_lsn field protected by
2186 * l_grant_lock. Don't worry about iclog's lsn.
2187 * No one else can be here except us.
2189 spin_lock(&log->l_grant_lock);
2190 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2191 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2192 log->l_last_sync_lsn =
2193 be64_to_cpu(iclog->ic_header.h_lsn);
2194 spin_unlock(&log->l_grant_lock);
2197 spin_unlock(&log->l_icloglock);
2202 * Keep processing entries in the callback list until
2203 * we come around and it is empty. We need to
2204 * atomically see that the list is empty and change the
2205 * state to DIRTY so that we don't miss any more
2206 * callbacks being added.
2208 spin_lock(&iclog->ic_callback_lock);
2209 cb = iclog->ic_callback;
2211 iclog->ic_callback_tail = &(iclog->ic_callback);
2212 iclog->ic_callback = NULL;
2213 spin_unlock(&iclog->ic_callback_lock);
2215 /* perform callbacks in the order given */
2216 for (; cb; cb = cb_next) {
2217 cb_next = cb->cb_next;
2218 cb->cb_func(cb->cb_arg, aborted);
2220 spin_lock(&iclog->ic_callback_lock);
2221 cb = iclog->ic_callback;
2227 spin_lock(&log->l_icloglock);
2228 ASSERT(iclog->ic_callback == NULL);
2229 spin_unlock(&iclog->ic_callback_lock);
2230 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2231 iclog->ic_state = XLOG_STATE_DIRTY;
2234 * Transition from DIRTY to ACTIVE if applicable.
2235 * NOP if STATE_IOERROR.
2237 xlog_state_clean_log(log);
2239 /* wake up threads waiting in xfs_log_force() */
2240 sv_broadcast(&iclog->ic_force_wait);
2242 iclog = iclog->ic_next;
2243 } while (first_iclog != iclog);
2245 if (repeats > 5000) {
2246 flushcnt += repeats;
2248 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2249 "%s: possible infinite loop (%d iterations)",
2250 __func__, flushcnt);
2252 } while (!ioerrors && loopdidcallbacks);
2255 * make one last gasp attempt to see if iclogs are being left in
2259 if (funcdidcallbacks) {
2260 first_iclog = iclog = log->l_iclog;
2262 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2264 * Terminate the loop if iclogs are found in states
2265 * which will cause other threads to clean up iclogs.
2267 * SYNCING - i/o completion will go through logs
2268 * DONE_SYNC - interrupt thread should be waiting for
2270 * IOERROR - give up hope all ye who enter here
2272 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2273 iclog->ic_state == XLOG_STATE_SYNCING ||
2274 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2275 iclog->ic_state == XLOG_STATE_IOERROR )
2277 iclog = iclog->ic_next;
2278 } while (first_iclog != iclog);
2282 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2284 spin_unlock(&log->l_icloglock);
2287 sv_broadcast(&log->l_flush_wait);
2292 * Finish transitioning this iclog to the dirty state.
2294 * Make sure that we completely execute this routine only when this is
2295 * the last call to the iclog. There is a good chance that iclog flushes,
2296 * when we reach the end of the physical log, get turned into 2 separate
2297 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2298 * routine. By using the reference count bwritecnt, we guarantee that only
2299 * the second completion goes through.
2301 * Callbacks could take time, so they are done outside the scope of the
2302 * global state machine log lock.
2305 xlog_state_done_syncing(
2306 xlog_in_core_t *iclog,
2309 xlog_t *log = iclog->ic_log;
2311 spin_lock(&log->l_icloglock);
2313 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2314 iclog->ic_state == XLOG_STATE_IOERROR);
2315 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2316 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2320 * If we got an error, either on the first buffer, or in the case of
2321 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2322 * and none should ever be attempted to be written to disk
2325 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2326 if (--iclog->ic_bwritecnt == 1) {
2327 spin_unlock(&log->l_icloglock);
2330 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2334 * Someone could be sleeping prior to writing out the next
2335 * iclog buffer, we wake them all, one will get to do the
2336 * I/O, the others get to wait for the result.
2338 sv_broadcast(&iclog->ic_write_wait);
2339 spin_unlock(&log->l_icloglock);
2340 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2341 } /* xlog_state_done_syncing */
2345 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2346 * sleep. We wait on the flush queue on the head iclog as that should be
2347 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2348 * we will wait here and all new writes will sleep until a sync completes.
2350 * The in-core logs are used in a circular fashion. They are not used
2351 * out-of-order even when an iclog past the head is free.
2354 * * log_offset where xlog_write() can start writing into the in-core
2356 * * in-core log pointer to which xlog_write() should write.
2357 * * boolean indicating this is a continued write to an in-core log.
2358 * If this is the last write, then the in-core log's offset field
2359 * needs to be incremented, depending on the amount of data which
2363 xlog_state_get_iclog_space(xlog_t *log,
2365 xlog_in_core_t **iclogp,
2366 xlog_ticket_t *ticket,
2367 int *continued_write,
2371 xlog_rec_header_t *head;
2372 xlog_in_core_t *iclog;
2376 spin_lock(&log->l_icloglock);
2377 if (XLOG_FORCED_SHUTDOWN(log)) {
2378 spin_unlock(&log->l_icloglock);
2379 return XFS_ERROR(EIO);
2382 iclog = log->l_iclog;
2383 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2384 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2385 XFS_STATS_INC(xs_log_noiclogs);
2387 /* Wait for log writes to have flushed */
2388 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
2392 head = &iclog->ic_header;
2394 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
2395 log_offset = iclog->ic_offset;
2397 /* On the 1st write to an iclog, figure out lsn. This works
2398 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2399 * committing to. If the offset is set, that's how many blocks
2402 if (log_offset == 0) {
2403 ticket->t_curr_res -= log->l_iclog_hsize;
2404 xlog_tic_add_region(ticket,
2406 XLOG_REG_TYPE_LRHEADER);
2407 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2408 head->h_lsn = cpu_to_be64(
2409 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
2410 ASSERT(log->l_curr_block >= 0);
2413 /* If there is enough room to write everything, then do it. Otherwise,
2414 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2415 * bit is on, so this will get flushed out. Don't update ic_offset
2416 * until you know exactly how many bytes get copied. Therefore, wait
2417 * until later to update ic_offset.
2419 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2420 * can fit into remaining data section.
2422 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2423 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2426 * If I'm the only one writing to this iclog, sync it to disk.
2427 * We need to do an atomic compare and decrement here to avoid
2428 * racing with concurrent atomic_dec_and_lock() calls in
2429 * xlog_state_release_iclog() when there is more than one
2430 * reference to the iclog.
2432 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2433 /* we are the only one */
2434 spin_unlock(&log->l_icloglock);
2435 error = xlog_state_release_iclog(log, iclog);
2439 spin_unlock(&log->l_icloglock);
2444 /* Do we have enough room to write the full amount in the remainder
2445 * of this iclog? Or must we continue a write on the next iclog and
2446 * mark this iclog as completely taken? In the case where we switch
2447 * iclogs (to mark it taken), this particular iclog will release/sync
2448 * to disk in xlog_write().
2450 if (len <= iclog->ic_size - iclog->ic_offset) {
2451 *continued_write = 0;
2452 iclog->ic_offset += len;
2454 *continued_write = 1;
2455 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2459 ASSERT(iclog->ic_offset <= iclog->ic_size);
2460 spin_unlock(&log->l_icloglock);
2462 *logoffsetp = log_offset;
2464 } /* xlog_state_get_iclog_space */
2467 * Atomically get the log space required for a log ticket.
2469 * Once a ticket gets put onto the reserveq, it will only return after
2470 * the needed reservation is satisfied.
2473 xlog_grant_log_space(xlog_t *log,
2484 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2485 panic("grant Recovery problem");
2488 /* Is there space or do we need to sleep? */
2489 spin_lock(&log->l_grant_lock);
2490 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2492 /* something is already sleeping; insert new transaction at end */
2493 if (log->l_reserve_headq) {
2494 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2495 xlog_trace_loggrant(log, tic,
2496 "xlog_grant_log_space: sleep 1");
2498 * Gotta check this before going to sleep, while we're
2499 * holding the grant lock.
2501 if (XLOG_FORCED_SHUTDOWN(log))
2504 XFS_STATS_INC(xs_sleep_logspace);
2505 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2507 * If we got an error, and the filesystem is shutting down,
2508 * we'll catch it down below. So just continue...
2510 xlog_trace_loggrant(log, tic,
2511 "xlog_grant_log_space: wake 1");
2512 spin_lock(&log->l_grant_lock);
2514 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2515 need_bytes = tic->t_unit_res*tic->t_ocnt;
2517 need_bytes = tic->t_unit_res;
2520 if (XLOG_FORCED_SHUTDOWN(log))
2523 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2524 log->l_grant_reserve_bytes);
2525 if (free_bytes < need_bytes) {
2526 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2527 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2528 xlog_trace_loggrant(log, tic,
2529 "xlog_grant_log_space: sleep 2");
2530 XFS_STATS_INC(xs_sleep_logspace);
2531 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2533 if (XLOG_FORCED_SHUTDOWN(log)) {
2534 spin_lock(&log->l_grant_lock);
2538 xlog_trace_loggrant(log, tic,
2539 "xlog_grant_log_space: wake 2");
2540 xlog_grant_push_ail(log->l_mp, need_bytes);
2541 spin_lock(&log->l_grant_lock);
2543 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2544 xlog_del_ticketq(&log->l_reserve_headq, tic);
2546 /* we've got enough space */
2547 xlog_grant_add_space(log, need_bytes);
2549 tail_lsn = log->l_tail_lsn;
2551 * Check to make sure the grant write head didn't just over lap the
2552 * tail. If the cycles are the same, we can't be overlapping.
2553 * Otherwise, make sure that the cycles differ by exactly one and
2554 * check the byte count.
2556 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2557 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2558 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2561 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2562 xlog_verify_grant_head(log, 1);
2563 spin_unlock(&log->l_grant_lock);
2567 if (tic->t_flags & XLOG_TIC_IN_Q)
2568 xlog_del_ticketq(&log->l_reserve_headq, tic);
2569 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2571 * If we are failing, make sure the ticket doesn't have any
2572 * current reservations. We don't want to add this back when
2573 * the ticket/transaction gets cancelled.
2575 tic->t_curr_res = 0;
2576 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2577 spin_unlock(&log->l_grant_lock);
2578 return XFS_ERROR(EIO);
2579 } /* xlog_grant_log_space */
2583 * Replenish the byte reservation required by moving the grant write head.
2588 xlog_regrant_write_log_space(xlog_t *log,
2591 int free_bytes, need_bytes;
2592 xlog_ticket_t *ntic;
2597 tic->t_curr_res = tic->t_unit_res;
2598 xlog_tic_reset_res(tic);
2604 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2605 panic("regrant Recovery problem");
2608 spin_lock(&log->l_grant_lock);
2609 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2611 if (XLOG_FORCED_SHUTDOWN(log))
2614 /* If there are other waiters on the queue then give them a
2615 * chance at logspace before us. Wake up the first waiters,
2616 * if we do not wake up all the waiters then go to sleep waiting
2617 * for more free space, otherwise try to get some space for
2621 if ((ntic = log->l_write_headq)) {
2622 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2623 log->l_grant_write_bytes);
2625 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2627 if (free_bytes < ntic->t_unit_res)
2629 free_bytes -= ntic->t_unit_res;
2630 sv_signal(&ntic->t_wait);
2631 ntic = ntic->t_next;
2632 } while (ntic != log->l_write_headq);
2634 if (ntic != log->l_write_headq) {
2635 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2636 xlog_ins_ticketq(&log->l_write_headq, tic);
2638 xlog_trace_loggrant(log, tic,
2639 "xlog_regrant_write_log_space: sleep 1");
2640 XFS_STATS_INC(xs_sleep_logspace);
2641 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
2642 &log->l_grant_lock, s);
2644 /* If we're shutting down, this tic is already
2646 if (XLOG_FORCED_SHUTDOWN(log)) {
2647 spin_lock(&log->l_grant_lock);
2651 xlog_trace_loggrant(log, tic,
2652 "xlog_regrant_write_log_space: wake 1");
2653 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2654 spin_lock(&log->l_grant_lock);
2658 need_bytes = tic->t_unit_res;
2661 if (XLOG_FORCED_SHUTDOWN(log))
2664 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2665 log->l_grant_write_bytes);
2666 if (free_bytes < need_bytes) {
2667 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2668 xlog_ins_ticketq(&log->l_write_headq, tic);
2669 XFS_STATS_INC(xs_sleep_logspace);
2670 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2672 /* If we're shutting down, this tic is already off the queue */
2673 if (XLOG_FORCED_SHUTDOWN(log)) {
2674 spin_lock(&log->l_grant_lock);
2678 xlog_trace_loggrant(log, tic,
2679 "xlog_regrant_write_log_space: wake 2");
2680 xlog_grant_push_ail(log->l_mp, need_bytes);
2681 spin_lock(&log->l_grant_lock);
2683 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2684 xlog_del_ticketq(&log->l_write_headq, tic);
2686 /* we've got enough space */
2687 xlog_grant_add_space_write(log, need_bytes);
2689 tail_lsn = log->l_tail_lsn;
2690 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2691 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2692 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2696 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2697 xlog_verify_grant_head(log, 1);
2698 spin_unlock(&log->l_grant_lock);
2703 if (tic->t_flags & XLOG_TIC_IN_Q)
2704 xlog_del_ticketq(&log->l_reserve_headq, tic);
2705 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2707 * If we are failing, make sure the ticket doesn't have any
2708 * current reservations. We don't want to add this back when
2709 * the ticket/transaction gets cancelled.
2711 tic->t_curr_res = 0;
2712 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2713 spin_unlock(&log->l_grant_lock);
2714 return XFS_ERROR(EIO);
2715 } /* xlog_regrant_write_log_space */
2718 /* The first cnt-1 times through here we don't need to
2719 * move the grant write head because the permanent
2720 * reservation has reserved cnt times the unit amount.
2721 * Release part of current permanent unit reservation and
2722 * reset current reservation to be one units worth. Also
2723 * move grant reservation head forward.
2726 xlog_regrant_reserve_log_space(xlog_t *log,
2727 xlog_ticket_t *ticket)
2729 xlog_trace_loggrant(log, ticket,
2730 "xlog_regrant_reserve_log_space: enter");
2731 if (ticket->t_cnt > 0)
2734 spin_lock(&log->l_grant_lock);
2735 xlog_grant_sub_space(log, ticket->t_curr_res);
2736 ticket->t_curr_res = ticket->t_unit_res;
2737 xlog_tic_reset_res(ticket);
2738 xlog_trace_loggrant(log, ticket,
2739 "xlog_regrant_reserve_log_space: sub current res");
2740 xlog_verify_grant_head(log, 1);
2742 /* just return if we still have some of the pre-reserved space */
2743 if (ticket->t_cnt > 0) {
2744 spin_unlock(&log->l_grant_lock);
2748 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
2749 xlog_trace_loggrant(log, ticket,
2750 "xlog_regrant_reserve_log_space: exit");
2751 xlog_verify_grant_head(log, 0);
2752 spin_unlock(&log->l_grant_lock);
2753 ticket->t_curr_res = ticket->t_unit_res;
2754 xlog_tic_reset_res(ticket);
2755 } /* xlog_regrant_reserve_log_space */
2759 * Give back the space left from a reservation.
2761 * All the information we need to make a correct determination of space left
2762 * is present. For non-permanent reservations, things are quite easy. The
2763 * count should have been decremented to zero. We only need to deal with the
2764 * space remaining in the current reservation part of the ticket. If the
2765 * ticket contains a permanent reservation, there may be left over space which
2766 * needs to be released. A count of N means that N-1 refills of the current
2767 * reservation can be done before we need to ask for more space. The first
2768 * one goes to fill up the first current reservation. Once we run out of
2769 * space, the count will stay at zero and the only space remaining will be
2770 * in the current reservation field.
2773 xlog_ungrant_log_space(xlog_t *log,
2774 xlog_ticket_t *ticket)
2776 if (ticket->t_cnt > 0)
2779 spin_lock(&log->l_grant_lock);
2780 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2782 xlog_grant_sub_space(log, ticket->t_curr_res);
2784 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2786 /* If this is a permanent reservation ticket, we may be able to free
2787 * up more space based on the remaining count.
2789 if (ticket->t_cnt > 0) {
2790 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2791 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
2794 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2795 xlog_verify_grant_head(log, 1);
2796 spin_unlock(&log->l_grant_lock);
2797 xfs_log_move_tail(log->l_mp, 1);
2798 } /* xlog_ungrant_log_space */
2802 * Flush iclog to disk if this is the last reference to the given iclog and
2803 * the WANT_SYNC bit is set.
2805 * When this function is entered, the iclog is not necessarily in the
2806 * WANT_SYNC state. It may be sitting around waiting to get filled.
2811 xlog_state_release_iclog(
2813 xlog_in_core_t *iclog)
2815 int sync = 0; /* do we sync? */
2817 if (iclog->ic_state & XLOG_STATE_IOERROR)
2818 return XFS_ERROR(EIO);
2820 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2821 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2824 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2825 spin_unlock(&log->l_icloglock);
2826 return XFS_ERROR(EIO);
2828 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2829 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2831 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2832 /* update tail before writing to iclog */
2833 xlog_assign_tail_lsn(log->l_mp);
2835 iclog->ic_state = XLOG_STATE_SYNCING;
2836 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
2837 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2838 /* cycle incremented when incrementing curr_block */
2840 spin_unlock(&log->l_icloglock);
2843 * We let the log lock go, so it's possible that we hit a log I/O
2844 * error or some other SHUTDOWN condition that marks the iclog
2845 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2846 * this iclog has consistent data, so we ignore IOERROR
2847 * flags after this point.
2850 return xlog_sync(log, iclog);
2852 } /* xlog_state_release_iclog */
2856 * This routine will mark the current iclog in the ring as WANT_SYNC
2857 * and move the current iclog pointer to the next iclog in the ring.
2858 * When this routine is called from xlog_state_get_iclog_space(), the
2859 * exact size of the iclog has not yet been determined. All we know is
2860 * that every data block. We have run out of space in this log record.
2863 xlog_state_switch_iclogs(xlog_t *log,
2864 xlog_in_core_t *iclog,
2867 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2869 eventual_size = iclog->ic_offset;
2870 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2871 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
2872 log->l_prev_block = log->l_curr_block;
2873 log->l_prev_cycle = log->l_curr_cycle;
2875 /* roll log?: ic_offset changed later */
2876 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2878 /* Round up to next log-sunit */
2879 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
2880 log->l_mp->m_sb.sb_logsunit > 1) {
2881 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2882 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2885 if (log->l_curr_block >= log->l_logBBsize) {
2886 log->l_curr_cycle++;
2887 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2888 log->l_curr_cycle++;
2889 log->l_curr_block -= log->l_logBBsize;
2890 ASSERT(log->l_curr_block >= 0);
2892 ASSERT(iclog == log->l_iclog);
2893 log->l_iclog = iclog->ic_next;
2894 } /* xlog_state_switch_iclogs */
2898 * Write out all data in the in-core log as of this exact moment in time.
2900 * Data may be written to the in-core log during this call. However,
2901 * we don't guarantee this data will be written out. A change from past
2902 * implementation means this routine will *not* write out zero length LRs.
2904 * Basically, we try and perform an intelligent scan of the in-core logs.
2905 * If we determine there is no flushable data, we just return. There is no
2906 * flushable data if:
2908 * 1. the current iclog is active and has no data; the previous iclog
2909 * is in the active or dirty state.
2910 * 2. the current iclog is drity, and the previous iclog is in the
2911 * active or dirty state.
2915 * 1. the current iclog is not in the active nor dirty state.
2916 * 2. the current iclog dirty, and the previous iclog is not in the
2917 * active nor dirty state.
2918 * 3. the current iclog is active, and there is another thread writing
2919 * to this particular iclog.
2920 * 4. a) the current iclog is active and has no other writers
2921 * b) when we return from flushing out this iclog, it is still
2922 * not in the active nor dirty state.
2925 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2927 xlog_in_core_t *iclog;
2930 spin_lock(&log->l_icloglock);
2932 iclog = log->l_iclog;
2933 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2934 spin_unlock(&log->l_icloglock);
2935 return XFS_ERROR(EIO);
2938 /* If the head iclog is not active nor dirty, we just attach
2939 * ourselves to the head and go to sleep.
2941 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2942 iclog->ic_state == XLOG_STATE_DIRTY) {
2944 * If the head is dirty or (active and empty), then
2945 * we need to look at the previous iclog. If the previous
2946 * iclog is active or dirty we are done. There is nothing
2947 * to sync out. Otherwise, we attach ourselves to the
2948 * previous iclog and go to sleep.
2950 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2951 (atomic_read(&iclog->ic_refcnt) == 0
2952 && iclog->ic_offset == 0)) {
2953 iclog = iclog->ic_prev;
2954 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2955 iclog->ic_state == XLOG_STATE_DIRTY)
2960 if (atomic_read(&iclog->ic_refcnt) == 0) {
2961 /* We are the only one with access to this
2962 * iclog. Flush it out now. There should
2963 * be a roundoff of zero to show that someone
2964 * has already taken care of the roundoff from
2965 * the previous sync.
2967 atomic_inc(&iclog->ic_refcnt);
2968 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2969 xlog_state_switch_iclogs(log, iclog, 0);
2970 spin_unlock(&log->l_icloglock);
2972 if (xlog_state_release_iclog(log, iclog))
2973 return XFS_ERROR(EIO);
2975 spin_lock(&log->l_icloglock);
2976 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
2977 iclog->ic_state != XLOG_STATE_DIRTY)
2982 /* Someone else is writing to this iclog.
2983 * Use its call to flush out the data. However,
2984 * the other thread may not force out this LR,
2985 * so we mark it WANT_SYNC.
2987 xlog_state_switch_iclogs(log, iclog, 0);
2993 /* By the time we come around again, the iclog could've been filled
2994 * which would give it another lsn. If we have a new lsn, just
2995 * return because the relevant data has been flushed.
2998 if (flags & XFS_LOG_SYNC) {
3000 * We must check if we're shutting down here, before
3001 * we wait, while we're holding the l_icloglock.
3002 * Then we check again after waking up, in case our
3003 * sleep was disturbed by a bad news.
3005 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3006 spin_unlock(&log->l_icloglock);
3007 return XFS_ERROR(EIO);
3009 XFS_STATS_INC(xs_log_force_sleep);
3010 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
3012 * No need to grab the log lock here since we're
3013 * only deciding whether or not to return EIO
3014 * and the memory read should be atomic.
3016 if (iclog->ic_state & XLOG_STATE_IOERROR)
3017 return XFS_ERROR(EIO);
3023 spin_unlock(&log->l_icloglock);
3026 } /* xlog_state_sync_all */
3030 * Used by code which implements synchronous log forces.
3032 * Find in-core log with lsn.
3033 * If it is in the DIRTY state, just return.
3034 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3035 * state and go to sleep or return.
3036 * If it is in any other state, go to sleep or return.
3038 * If filesystem activity goes to zero, the iclog will get flushed only by
3042 xlog_state_sync(xlog_t *log,
3047 xlog_in_core_t *iclog;
3048 int already_slept = 0;
3051 spin_lock(&log->l_icloglock);
3052 iclog = log->l_iclog;
3054 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3055 spin_unlock(&log->l_icloglock);
3056 return XFS_ERROR(EIO);
3060 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3061 iclog = iclog->ic_next;
3065 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3066 spin_unlock(&log->l_icloglock);
3070 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3072 * We sleep here if we haven't already slept (e.g.
3073 * this is the first time we've looked at the correct
3074 * iclog buf) and the buffer before us is going to
3075 * be sync'ed. The reason for this is that if we
3076 * are doing sync transactions here, by waiting for
3077 * the previous I/O to complete, we can allow a few
3078 * more transactions into this iclog before we close
3081 * Otherwise, we mark the buffer WANT_SYNC, and bump
3082 * up the refcnt so we can release the log (which drops
3083 * the ref count). The state switch keeps new transaction
3084 * commits from using this buffer. When the current commits
3085 * finish writing into the buffer, the refcount will drop to
3086 * zero and the buffer will go out then.
3088 if (!already_slept &&
3089 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3090 XLOG_STATE_SYNCING))) {
3091 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3092 XFS_STATS_INC(xs_log_force_sleep);
3093 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
3094 &log->l_icloglock, s);
3099 atomic_inc(&iclog->ic_refcnt);
3100 xlog_state_switch_iclogs(log, iclog, 0);
3101 spin_unlock(&log->l_icloglock);
3102 if (xlog_state_release_iclog(log, iclog))
3103 return XFS_ERROR(EIO);
3105 spin_lock(&log->l_icloglock);
3109 if ((flags & XFS_LOG_SYNC) && /* sleep */
3110 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3113 * Don't wait on completion if we know that we've
3114 * gotten a log write error.
3116 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3117 spin_unlock(&log->l_icloglock);
3118 return XFS_ERROR(EIO);
3120 XFS_STATS_INC(xs_log_force_sleep);
3121 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3123 * No need to grab the log lock here since we're
3124 * only deciding whether or not to return EIO
3125 * and the memory read should be atomic.
3127 if (iclog->ic_state & XLOG_STATE_IOERROR)
3128 return XFS_ERROR(EIO);
3130 } else { /* just return */
3131 spin_unlock(&log->l_icloglock);
3135 } while (iclog != log->l_iclog);
3137 spin_unlock(&log->l_icloglock);
3139 } /* xlog_state_sync */
3143 * Called when we want to mark the current iclog as being ready to sync to
3147 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3149 spin_lock(&log->l_icloglock);
3151 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3152 xlog_state_switch_iclogs(log, iclog, 0);
3154 ASSERT(iclog->ic_state &
3155 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3158 spin_unlock(&log->l_icloglock);
3159 } /* xlog_state_want_sync */
3163 /*****************************************************************************
3167 *****************************************************************************
3171 * Free a used ticket.
3174 xlog_ticket_put(xlog_t *log,
3175 xlog_ticket_t *ticket)
3177 sv_destroy(&ticket->t_wait);
3178 kmem_zone_free(xfs_log_ticket_zone, ticket);
3179 } /* xlog_ticket_put */
3183 * Allocate and initialise a new log ticket.
3185 STATIC xlog_ticket_t *
3186 xlog_ticket_get(xlog_t *log,
3195 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3200 * Permanent reservations have up to 'cnt'-1 active log operations
3201 * in the log. A unit in this case is the amount of space for one
3202 * of these log operations. Normal reservations have a cnt of 1
3203 * and their unit amount is the total amount of space required.
3205 * The following lines of code account for non-transaction data
3206 * which occupy space in the on-disk log.
3208 * Normal form of a transaction is:
3209 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3210 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3212 * We need to account for all the leadup data and trailer data
3213 * around the transaction data.
3214 * And then we need to account for the worst case in terms of using
3216 * The worst case will happen if:
3217 * - the placement of the transaction happens to be such that the
3218 * roundoff is at its maximum
3219 * - the transaction data is synced before the commit record is synced
3220 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3221 * Therefore the commit record is in its own Log Record.
3222 * This can happen as the commit record is called with its
3223 * own region to xlog_write().
3224 * This then means that in the worst case, roundoff can happen for
3225 * the commit-rec as well.
3226 * The commit-rec is smaller than padding in this scenario and so it is
3227 * not added separately.
3230 /* for trans header */
3231 unit_bytes += sizeof(xlog_op_header_t);
3232 unit_bytes += sizeof(xfs_trans_header_t);
3235 unit_bytes += sizeof(xlog_op_header_t);
3237 /* for LR headers */
3238 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3239 unit_bytes += log->l_iclog_hsize * num_headers;
3241 /* for commit-rec LR header - note: padding will subsume the ophdr */
3242 unit_bytes += log->l_iclog_hsize;
3244 /* for split-recs - ophdrs added when data split over LRs */
3245 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3247 /* for roundoff padding for transaction data and one for commit record */
3248 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
3249 log->l_mp->m_sb.sb_logsunit > 1) {
3250 /* log su roundoff */
3251 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3254 unit_bytes += 2*BBSIZE;
3257 tic->t_unit_res = unit_bytes;
3258 tic->t_curr_res = unit_bytes;
3261 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3262 tic->t_clientid = client;
3263 tic->t_flags = XLOG_TIC_INITED;
3264 tic->t_trans_type = 0;
3265 if (xflags & XFS_LOG_PERM_RESERV)
3266 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3267 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
3269 xlog_tic_reset_res(tic);
3272 } /* xlog_ticket_get */
3275 /******************************************************************************
3277 * Log debug routines
3279 ******************************************************************************
3283 * Make sure that the destination ptr is within the valid data region of
3284 * one of the iclogs. This uses backup pointers stored in a different
3285 * part of the log in case we trash the log structure.
3288 xlog_verify_dest_ptr(xlog_t *log,
3294 for (i=0; i < log->l_iclog_bufs; i++) {
3295 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3296 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3300 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3301 } /* xlog_verify_dest_ptr */
3304 xlog_verify_grant_head(xlog_t *log, int equals)
3306 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3308 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3310 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3312 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3313 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3315 } /* xlog_verify_grant_head */
3317 /* check if it will fit */
3319 xlog_verify_tail_lsn(xlog_t *log,
3320 xlog_in_core_t *iclog,
3325 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3327 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3328 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3329 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3331 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3333 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3334 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3336 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3337 if (blocks < BTOBB(iclog->ic_offset) + 1)
3338 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3340 } /* xlog_verify_tail_lsn */
3343 * Perform a number of checks on the iclog before writing to disk.
3345 * 1. Make sure the iclogs are still circular
3346 * 2. Make sure we have a good magic number
3347 * 3. Make sure we don't have magic numbers in the data
3348 * 4. Check fields of each log operation header for:
3349 * A. Valid client identifier
3350 * B. tid ptr value falls in valid ptr space (user space code)
3351 * C. Length in log record header is correct according to the
3352 * individual operation headers within record.
3353 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3354 * log, check the preceding blocks of the physical log to make sure all
3355 * the cycle numbers agree with the current cycle number.
3358 xlog_verify_iclog(xlog_t *log,
3359 xlog_in_core_t *iclog,
3363 xlog_op_header_t *ophead;
3364 xlog_in_core_t *icptr;
3365 xlog_in_core_2_t *xhdr;
3367 xfs_caddr_t base_ptr;
3368 __psint_t field_offset;
3370 int len, i, j, k, op_len;
3373 /* check validity of iclog pointers */
3374 spin_lock(&log->l_icloglock);
3375 icptr = log->l_iclog;
3376 for (i=0; i < log->l_iclog_bufs; i++) {
3378 xlog_panic("xlog_verify_iclog: invalid ptr");
3379 icptr = icptr->ic_next;
3381 if (icptr != log->l_iclog)
3382 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3383 spin_unlock(&log->l_icloglock);
3385 /* check log magic numbers */
3386 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
3387 xlog_panic("xlog_verify_iclog: invalid magic num");
3389 ptr = (xfs_caddr_t) &iclog->ic_header;
3390 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
3392 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
3393 xlog_panic("xlog_verify_iclog: unexpected magic num");
3397 len = be32_to_cpu(iclog->ic_header.h_num_logops);
3398 ptr = iclog->ic_datap;
3400 ophead = (xlog_op_header_t *)ptr;
3401 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3402 for (i = 0; i < len; i++) {
3403 ophead = (xlog_op_header_t *)ptr;
3405 /* clientid is only 1 byte */
3406 field_offset = (__psint_t)
3407 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3408 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3409 clientid = ophead->oh_clientid;
3411 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3412 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3413 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3414 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3415 clientid = xlog_get_client_id(
3416 xhdr[j].hic_xheader.xh_cycle_data[k]);
3418 clientid = xlog_get_client_id(
3419 iclog->ic_header.h_cycle_data[idx]);
3422 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3423 cmn_err(CE_WARN, "xlog_verify_iclog: "
3424 "invalid clientid %d op 0x%p offset 0x%lx",
3425 clientid, ophead, (unsigned long)field_offset);
3428 field_offset = (__psint_t)
3429 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3430 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3431 op_len = be32_to_cpu(ophead->oh_len);
3433 idx = BTOBBT((__psint_t)&ophead->oh_len -
3434 (__psint_t)iclog->ic_datap);
3435 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3436 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3437 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3438 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
3440 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
3443 ptr += sizeof(xlog_op_header_t) + op_len;
3445 } /* xlog_verify_iclog */
3449 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3455 xlog_in_core_t *iclog, *ic;
3457 iclog = log->l_iclog;
3458 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3460 * Mark all the incore logs IOERROR.
3461 * From now on, no log flushes will result.
3465 ic->ic_state = XLOG_STATE_IOERROR;
3467 } while (ic != iclog);
3471 * Return non-zero, if state transition has already happened.
3477 * This is called from xfs_force_shutdown, when we're forcibly
3478 * shutting down the filesystem, typically because of an IO error.
3479 * Our main objectives here are to make sure that:
3480 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3481 * parties to find out, 'atomically'.
3482 * b. those who're sleeping on log reservations, pinned objects and
3483 * other resources get woken up, and be told the bad news.
3484 * c. nothing new gets queued up after (a) and (b) are done.
3485 * d. if !logerror, flush the iclogs to disk, then seal them off
3489 xfs_log_force_umount(
3490 struct xfs_mount *mp,
3501 * If this happens during log recovery, don't worry about
3502 * locking; the log isn't open for business yet.
3505 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3506 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3507 XFS_BUF_DONE(mp->m_sb_bp);
3512 * Somebody could've already done the hard work for us.
3513 * No need to get locks for this.
3515 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3516 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3521 * We must hold both the GRANT lock and the LOG lock,
3522 * before we mark the filesystem SHUTDOWN and wake
3523 * everybody up to tell the bad news.
3525 spin_lock(&log->l_icloglock);
3526 spin_lock(&log->l_grant_lock);
3527 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3528 XFS_BUF_DONE(mp->m_sb_bp);
3530 * This flag is sort of redundant because of the mount flag, but
3531 * it's good to maintain the separation between the log and the rest
3534 log->l_flags |= XLOG_IO_ERROR;
3537 * If we hit a log error, we want to mark all the iclogs IOERROR
3538 * while we're still holding the loglock.
3541 retval = xlog_state_ioerror(log);
3542 spin_unlock(&log->l_icloglock);
3545 * We don't want anybody waiting for log reservations
3546 * after this. That means we have to wake up everybody
3547 * queued up on reserve_headq as well as write_headq.
3548 * In addition, we make sure in xlog_{re}grant_log_space
3549 * that we don't enqueue anything once the SHUTDOWN flag
3550 * is set, and this action is protected by the GRANTLOCK.
3552 if ((tic = log->l_reserve_headq)) {
3554 sv_signal(&tic->t_wait);
3556 } while (tic != log->l_reserve_headq);
3559 if ((tic = log->l_write_headq)) {
3561 sv_signal(&tic->t_wait);
3563 } while (tic != log->l_write_headq);
3565 spin_unlock(&log->l_grant_lock);
3567 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3570 * Force the incore logs to disk before shutting the
3571 * log down completely.
3573 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3574 spin_lock(&log->l_icloglock);
3575 retval = xlog_state_ioerror(log);
3576 spin_unlock(&log->l_icloglock);
3579 * Wake up everybody waiting on xfs_log_force.
3580 * Callback all log item committed functions as if the
3581 * log writes were completed.
3583 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3585 #ifdef XFSERRORDEBUG
3587 xlog_in_core_t *iclog;
3589 spin_lock(&log->l_icloglock);
3590 iclog = log->l_iclog;
3592 ASSERT(iclog->ic_callback == 0);
3593 iclog = iclog->ic_next;
3594 } while (iclog != log->l_iclog);
3595 spin_unlock(&log->l_icloglock);
3598 /* return non-zero if log IOERROR transition had already happened */
3603 xlog_iclogs_empty(xlog_t *log)
3605 xlog_in_core_t *iclog;
3607 iclog = log->l_iclog;
3609 /* endianness does not matter here, zero is zero in
3612 if (iclog->ic_header.h_num_logops)
3614 iclog = iclog->ic_next;
3615 } while (iclog != log->l_iclog);