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_SLEEP);
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);
341 /* If this ticket was a permanent reservation and we aren't
342 * trying to release it, reset the inited flags; so next time
343 * we write, a start record will be written out.
345 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
346 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
347 ticket->t_flags |= XLOG_TIC_INITED;
354 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
355 * the force is done synchronously.
357 * Asynchronous forces are implemented by setting the WANT_SYNC
358 * bit in the appropriate in-core log and then returning.
360 * Synchronous forces are implemented with a signal variable. All callers
361 * to force a given lsn to disk will wait on a the sv attached to the
362 * specific in-core log. When given in-core log finally completes its
363 * write to disk, that thread will wake up all threads waiting on the
373 xlog_t *log = mp->m_log;
377 log_flushed = &dummy;
379 ASSERT(flags & XFS_LOG_FORCE);
381 XFS_STATS_INC(xs_log_force);
383 if (log->l_flags & XLOG_IO_ERROR)
384 return XFS_ERROR(EIO);
386 return xlog_state_sync_all(log, flags, log_flushed);
388 return xlog_state_sync(log, lsn, flags, log_flushed);
389 } /* _xfs_log_force */
392 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
393 * about errors or whether the log was flushed or not. This is the normal
394 * interface to use when trying to unpin items or move the log forward.
403 error = _xfs_log_force(mp, lsn, flags, NULL);
405 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
406 "error %d returned.", error);
412 * Attaches a new iclog I/O completion callback routine during
413 * transaction commit. If the log is in error state, a non-zero
414 * return code is handed back and the caller is responsible for
415 * executing the callback at an appropriate time.
418 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
419 void *iclog_hndl, /* iclog to hang callback off */
420 xfs_log_callback_t *cb)
422 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
425 spin_lock(&iclog->ic_callback_lock);
426 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
428 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
429 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
431 *(iclog->ic_callback_tail) = cb;
432 iclog->ic_callback_tail = &(cb->cb_next);
434 spin_unlock(&iclog->ic_callback_lock);
436 } /* xfs_log_notify */
439 xfs_log_release_iclog(xfs_mount_t *mp,
442 xlog_t *log = mp->m_log;
443 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
445 if (xlog_state_release_iclog(log, iclog)) {
446 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
454 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
455 * to the reservation.
456 * 2. Potentially, push buffers at tail of log to disk.
458 * Each reservation is going to reserve extra space for a log record header.
459 * When writes happen to the on-disk log, we don't subtract the length of the
460 * log record header from any reservation. By wasting space in each
461 * reservation, we prevent over allocation problems.
464 xfs_log_reserve(xfs_mount_t *mp,
467 xfs_log_ticket_t *ticket,
472 xlog_t *log = mp->m_log;
473 xlog_ticket_t *internal_ticket;
476 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
477 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
479 if (XLOG_FORCED_SHUTDOWN(log))
480 return XFS_ERROR(EIO);
482 XFS_STATS_INC(xs_try_logspace);
484 if (*ticket != NULL) {
485 ASSERT(flags & XFS_LOG_PERM_RESERV);
486 internal_ticket = (xlog_ticket_t *)*ticket;
487 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
488 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
489 retval = xlog_regrant_write_log_space(log, internal_ticket);
491 /* may sleep if need to allocate more tickets */
492 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
494 if (!internal_ticket)
495 return XFS_ERROR(ENOMEM);
496 internal_ticket->t_trans_type = t_type;
497 *ticket = internal_ticket;
498 xlog_trace_loggrant(log, internal_ticket,
499 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
500 "xfs_log_reserve: create new ticket (permanent trans)" :
501 "xfs_log_reserve: create new ticket");
502 xlog_grant_push_ail(mp,
503 (internal_ticket->t_unit_res *
504 internal_ticket->t_cnt));
505 retval = xlog_grant_log_space(log, internal_ticket);
509 } /* xfs_log_reserve */
513 * Mount a log filesystem
515 * mp - ubiquitous xfs mount point structure
516 * log_target - buftarg of on-disk log device
517 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
518 * num_bblocks - Number of BBSIZE blocks in on-disk log
520 * Return error or zero.
525 xfs_buftarg_t *log_target,
526 xfs_daddr_t blk_offset,
531 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
532 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
535 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
537 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
540 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
543 * Initialize the AIL now we have a log.
545 spin_lock_init(&mp->m_ail_lock);
546 error = xfs_trans_ail_init(mp);
548 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
553 * skip log recovery on a norecovery mount. pretend it all
556 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
557 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
560 mp->m_flags &= ~XFS_MOUNT_RDONLY;
562 error = xlog_recover(mp->m_log);
565 mp->m_flags |= XFS_MOUNT_RDONLY;
567 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
572 /* Normal transactions can now occur */
573 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
575 /* End mounting message in xfs_log_mount_finish */
578 xfs_log_unmount_dealloc(mp);
580 } /* xfs_log_mount */
583 * Finish the recovery of the file system. This is separate from
584 * the xfs_log_mount() call, because it depends on the code in
585 * xfs_mountfs() to read in the root and real-time bitmap inodes
586 * between calling xfs_log_mount() and here.
588 * mp - ubiquitous xfs mount point structure
591 xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
595 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
596 error = xlog_recover_finish(mp->m_log, mfsi_flags);
599 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
606 * Unmount processing for the log.
609 xfs_log_unmount(xfs_mount_t *mp)
613 error = xfs_log_unmount_write(mp);
614 xfs_log_unmount_dealloc(mp);
619 * Final log writes as part of unmount.
621 * Mark the filesystem clean as unmount happens. Note that during relocation
622 * this routine needs to be executed as part of source-bag while the
623 * deallocation must not be done until source-end.
627 * Unmount record used to have a string "Unmount filesystem--" in the
628 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
629 * We just write the magic number now since that particular field isn't
630 * currently architecture converted and "nUmount" is a bit foo.
631 * As far as I know, there weren't any dependencies on the old behaviour.
635 xfs_log_unmount_write(xfs_mount_t *mp)
637 xlog_t *log = mp->m_log;
638 xlog_in_core_t *iclog;
640 xlog_in_core_t *first_iclog;
642 xfs_log_iovec_t reg[1];
643 xfs_log_ticket_t tic = NULL;
647 /* the data section must be 32 bit size aligned */
651 __uint32_t pad2; /* may as well make it 64 bits */
652 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
655 * Don't write out unmount record on read-only mounts.
656 * Or, if we are doing a forced umount (typically because of IO errors).
658 if (mp->m_flags & XFS_MOUNT_RDONLY)
661 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
662 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
665 first_iclog = iclog = log->l_iclog;
667 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
668 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
669 ASSERT(iclog->ic_offset == 0);
671 iclog = iclog->ic_next;
672 } while (iclog != first_iclog);
674 if (! (XLOG_FORCED_SHUTDOWN(log))) {
675 reg[0].i_addr = (void*)&magic;
676 reg[0].i_len = sizeof(magic);
677 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
679 error = xfs_log_reserve(mp, 600, 1, &tic,
680 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
682 /* remove inited flag */
683 ((xlog_ticket_t *)tic)->t_flags = 0;
684 error = xlog_write(mp, reg, 1, tic, &lsn,
685 NULL, XLOG_UNMOUNT_TRANS);
687 * At this point, we're umounting anyway,
688 * so there's no point in transitioning log state
689 * to IOERROR. Just continue...
694 xfs_fs_cmn_err(CE_ALERT, mp,
695 "xfs_log_unmount: unmount record failed");
699 spin_lock(&log->l_icloglock);
700 iclog = log->l_iclog;
701 atomic_inc(&iclog->ic_refcnt);
702 spin_unlock(&log->l_icloglock);
703 xlog_state_want_sync(log, iclog);
704 error = xlog_state_release_iclog(log, iclog);
706 spin_lock(&log->l_icloglock);
707 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
708 iclog->ic_state == XLOG_STATE_DIRTY)) {
709 if (!XLOG_FORCED_SHUTDOWN(log)) {
710 sv_wait(&iclog->ic_force_wait, PMEM,
711 &log->l_icloglock, s);
713 spin_unlock(&log->l_icloglock);
716 spin_unlock(&log->l_icloglock);
719 xlog_trace_loggrant(log, tic, "unmount rec");
720 xlog_ungrant_log_space(log, tic);
721 xlog_ticket_put(log, tic);
725 * We're already in forced_shutdown mode, couldn't
726 * even attempt to write out the unmount transaction.
728 * Go through the motions of sync'ing and releasing
729 * the iclog, even though no I/O will actually happen,
730 * we need to wait for other log I/Os that may already
731 * be in progress. Do this as a separate section of
732 * code so we'll know if we ever get stuck here that
733 * we're in this odd situation of trying to unmount
734 * a file system that went into forced_shutdown as
735 * the result of an unmount..
737 spin_lock(&log->l_icloglock);
738 iclog = log->l_iclog;
739 atomic_inc(&iclog->ic_refcnt);
740 spin_unlock(&log->l_icloglock);
742 xlog_state_want_sync(log, iclog);
743 error = xlog_state_release_iclog(log, iclog);
745 spin_lock(&log->l_icloglock);
747 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
748 || iclog->ic_state == XLOG_STATE_DIRTY
749 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
751 sv_wait(&iclog->ic_force_wait, PMEM,
752 &log->l_icloglock, s);
754 spin_unlock(&log->l_icloglock);
759 } /* xfs_log_unmount_write */
762 * Deallocate log structures for unmount/relocation.
764 * We need to stop the aild from running before we destroy
765 * and deallocate the log as the aild references the log.
768 xfs_log_unmount_dealloc(xfs_mount_t *mp)
770 xfs_trans_ail_destroy(mp);
771 xlog_dealloc_log(mp->m_log);
775 * Write region vectors to log. The write happens using the space reservation
776 * of the ticket (tic). It is not a requirement that all writes for a given
777 * transaction occur with one call to xfs_log_write().
780 xfs_log_write(xfs_mount_t * mp,
781 xfs_log_iovec_t reg[],
783 xfs_log_ticket_t tic,
784 xfs_lsn_t *start_lsn)
787 xlog_t *log = mp->m_log;
789 if (XLOG_FORCED_SHUTDOWN(log))
790 return XFS_ERROR(EIO);
792 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
793 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
796 } /* xfs_log_write */
800 xfs_log_move_tail(xfs_mount_t *mp,
804 xlog_t *log = mp->m_log;
805 int need_bytes, free_bytes, cycle, bytes;
807 if (XLOG_FORCED_SHUTDOWN(log))
811 /* needed since sync_lsn is 64 bits */
812 spin_lock(&log->l_icloglock);
813 tail_lsn = log->l_last_sync_lsn;
814 spin_unlock(&log->l_icloglock);
817 spin_lock(&log->l_grant_lock);
819 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
823 log->l_tail_lsn = tail_lsn;
826 if ((tic = log->l_write_headq)) {
828 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
829 panic("Recovery problem");
831 cycle = log->l_grant_write_cycle;
832 bytes = log->l_grant_write_bytes;
833 free_bytes = xlog_space_left(log, cycle, bytes);
835 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
837 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
840 free_bytes -= tic->t_unit_res;
841 sv_signal(&tic->t_wait);
843 } while (tic != log->l_write_headq);
845 if ((tic = log->l_reserve_headq)) {
847 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
848 panic("Recovery problem");
850 cycle = log->l_grant_reserve_cycle;
851 bytes = log->l_grant_reserve_bytes;
852 free_bytes = xlog_space_left(log, cycle, bytes);
854 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
855 need_bytes = tic->t_unit_res*tic->t_cnt;
857 need_bytes = tic->t_unit_res;
858 if (free_bytes < need_bytes && tail_lsn != 1)
861 free_bytes -= need_bytes;
862 sv_signal(&tic->t_wait);
864 } while (tic != log->l_reserve_headq);
866 spin_unlock(&log->l_grant_lock);
867 } /* xfs_log_move_tail */
870 * Determine if we have a transaction that has gone to disk
871 * that needs to be covered. Log activity needs to be idle (no AIL and
872 * nothing in the iclogs). And, we need to be in the right state indicating
873 * something has gone out.
876 xfs_log_need_covered(xfs_mount_t *mp)
879 xlog_t *log = mp->m_log;
881 if (!xfs_fs_writable(mp))
884 spin_lock(&log->l_icloglock);
885 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
886 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
887 && !xfs_trans_first_ail(mp, &gen)
888 && xlog_iclogs_empty(log)) {
889 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
890 log->l_covered_state = XLOG_STATE_COVER_DONE;
892 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
893 log->l_covered_state = XLOG_STATE_COVER_DONE2;
897 spin_unlock(&log->l_icloglock);
901 /******************************************************************************
905 ******************************************************************************
908 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
909 * The log manager must keep track of the last LR which was committed
910 * to disk. The lsn of this LR will become the new tail_lsn whenever
911 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
912 * the situation where stuff could be written into the log but nothing
913 * was ever in the AIL when asked. Eventually, we panic since the
914 * tail hits the head.
916 * We may be holding the log iclog lock upon entering this routine.
919 xlog_assign_tail_lsn(xfs_mount_t *mp)
922 xlog_t *log = mp->m_log;
924 tail_lsn = xfs_trans_tail_ail(mp);
925 spin_lock(&log->l_grant_lock);
927 log->l_tail_lsn = tail_lsn;
929 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
931 spin_unlock(&log->l_grant_lock);
934 } /* xlog_assign_tail_lsn */
938 * Return the space in the log between the tail and the head. The head
939 * is passed in the cycle/bytes formal parms. In the special case where
940 * the reserve head has wrapped passed the tail, this calculation is no
941 * longer valid. In this case, just return 0 which means there is no space
942 * in the log. This works for all places where this function is called
943 * with the reserve head. Of course, if the write head were to ever
944 * wrap the tail, we should blow up. Rather than catch this case here,
945 * we depend on other ASSERTions in other parts of the code. XXXmiken
947 * This code also handles the case where the reservation head is behind
948 * the tail. The details of this case are described below, but the end
949 * result is that we return the size of the log as the amount of space left.
952 xlog_space_left(xlog_t *log, int cycle, int bytes)
958 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
959 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
960 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
961 free_bytes = log->l_logsize - (bytes - tail_bytes);
962 } else if ((tail_cycle + 1) < cycle) {
964 } else if (tail_cycle < cycle) {
965 ASSERT(tail_cycle == (cycle - 1));
966 free_bytes = tail_bytes - bytes;
969 * The reservation head is behind the tail.
970 * In this case we just want to return the size of the
971 * log as the amount of space left.
973 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
974 "xlog_space_left: head behind tail\n"
975 " tail_cycle = %d, tail_bytes = %d\n"
976 " GH cycle = %d, GH bytes = %d",
977 tail_cycle, tail_bytes, cycle, bytes);
979 free_bytes = log->l_logsize;
982 } /* xlog_space_left */
986 * Log function which is called when an io completes.
988 * The log manager needs its own routine, in order to control what
989 * happens with the buffer after the write completes.
992 xlog_iodone(xfs_buf_t *bp)
994 xlog_in_core_t *iclog;
998 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
999 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1000 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1004 * Some versions of cpp barf on the recursive definition of
1005 * ic_log -> hic_fields.ic_log and expand ic_log twice when
1006 * it is passed through two macros. Workaround broken cpp.
1011 * If the ordered flag has been removed by a lower
1012 * layer, it means the underlyin device no longer supports
1013 * barrier I/O. Warn loudly and turn off barriers.
1015 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
1016 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1017 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1018 "xlog_iodone: Barriers are no longer supported"
1019 " by device. Disabling barriers\n");
1020 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1024 * Race to shutdown the filesystem if we see an error.
1026 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1027 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1028 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1030 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
1032 * This flag will be propagated to the trans-committed
1033 * callback routines to let them know that the log-commit
1036 aborted = XFS_LI_ABORTED;
1037 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1038 aborted = XFS_LI_ABORTED;
1041 /* log I/O is always issued ASYNC */
1042 ASSERT(XFS_BUF_ISASYNC(bp));
1043 xlog_state_done_syncing(iclog, aborted);
1045 * do not reference the buffer (bp) here as we could race
1046 * with it being freed after writing the unmount record to the
1053 * The bdstrat callback function for log bufs. This gives us a central
1054 * place to trap bufs in case we get hit by a log I/O error and need to
1055 * shutdown. Actually, in practice, even when we didn't get a log error,
1056 * we transition the iclogs to IOERROR state *after* flushing all existing
1057 * iclogs to disk. This is because we don't want anymore new transactions to be
1058 * started or completed afterwards.
1061 xlog_bdstrat_cb(struct xfs_buf *bp)
1063 xlog_in_core_t *iclog;
1065 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1067 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1068 /* note for irix bstrat will need struct bdevsw passed
1069 * Fix the following macro if the code ever is merged
1075 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1076 XFS_BUF_ERROR(bp, EIO);
1079 return XFS_ERROR(EIO);
1085 * Return size of each in-core log record buffer.
1087 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
1089 * If the filesystem blocksize is too large, we may need to choose a
1090 * larger size since the directory code currently logs entire blocks.
1094 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1100 if (mp->m_logbufs <= 0)
1101 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1103 log->l_iclog_bufs = mp->m_logbufs;
1106 * Buffer size passed in from mount system call.
1108 if (mp->m_logbsize > 0) {
1109 size = log->l_iclog_size = mp->m_logbsize;
1110 log->l_iclog_size_log = 0;
1112 log->l_iclog_size_log++;
1116 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1117 /* # headers = size / 32K
1118 * one header holds cycles from 32K of data
1121 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1122 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1124 log->l_iclog_hsize = xhdrs << BBSHIFT;
1125 log->l_iclog_heads = xhdrs;
1127 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1128 log->l_iclog_hsize = BBSIZE;
1129 log->l_iclog_heads = 1;
1134 /* All machines use 32KB buffers by default. */
1135 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1136 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1138 /* the default log size is 16k or 32k which is one header sector */
1139 log->l_iclog_hsize = BBSIZE;
1140 log->l_iclog_heads = 1;
1143 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1144 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1146 if (mp->m_sb.sb_blocksize >= 16*1024) {
1147 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1148 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1149 if (mp->m_logbufs <= 0) {
1150 switch (mp->m_sb.sb_blocksize) {
1151 case 16*1024: /* 16 KB */
1152 log->l_iclog_bufs = 3;
1154 case 32*1024: /* 32 KB */
1155 log->l_iclog_bufs = 4;
1157 case 64*1024: /* 64 KB */
1158 log->l_iclog_bufs = 8;
1161 xlog_panic("XFS: Invalid blocksize");
1167 done: /* are we being asked to make the sizes selected above visible? */
1168 if (mp->m_logbufs == 0)
1169 mp->m_logbufs = log->l_iclog_bufs;
1170 if (mp->m_logbsize == 0)
1171 mp->m_logbsize = log->l_iclog_size;
1172 } /* xlog_get_iclog_buffer_size */
1176 * This routine initializes some of the log structure for a given mount point.
1177 * Its primary purpose is to fill in enough, so recovery can occur. However,
1178 * some other stuff may be filled in too.
1181 xlog_alloc_log(xfs_mount_t *mp,
1182 xfs_buftarg_t *log_target,
1183 xfs_daddr_t blk_offset,
1187 xlog_rec_header_t *head;
1188 xlog_in_core_t **iclogp;
1189 xlog_in_core_t *iclog, *prev_iclog=NULL;
1194 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1197 log->l_targ = log_target;
1198 log->l_logsize = BBTOB(num_bblks);
1199 log->l_logBBstart = blk_offset;
1200 log->l_logBBsize = num_bblks;
1201 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1202 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1204 log->l_prev_block = -1;
1205 log->l_tail_lsn = xlog_assign_lsn(1, 0);
1206 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1207 log->l_last_sync_lsn = log->l_tail_lsn;
1208 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1209 log->l_grant_reserve_cycle = 1;
1210 log->l_grant_write_cycle = 1;
1212 if (xfs_sb_version_hassector(&mp->m_sb)) {
1213 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1214 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1215 /* for larger sector sizes, must have v2 or external log */
1216 ASSERT(log->l_sectbb_log == 0 ||
1217 log->l_logBBstart == 0 ||
1218 xfs_sb_version_haslogv2(&mp->m_sb));
1219 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1221 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1223 xlog_get_iclog_buffer_size(mp, log);
1225 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1226 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1227 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1228 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1229 ASSERT(XFS_BUF_ISBUSY(bp));
1230 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1233 spin_lock_init(&log->l_icloglock);
1234 spin_lock_init(&log->l_grant_lock);
1235 sv_init(&log->l_flush_wait, 0, "flush_wait");
1237 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1238 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1240 iclogp = &log->l_iclog;
1242 * The amount of memory to allocate for the iclog structure is
1243 * rather funky due to the way the structure is defined. It is
1244 * done this way so that we can use different sizes for machines
1245 * with different amounts of memory. See the definition of
1246 * xlog_in_core_t in xfs_log_priv.h for details.
1248 iclogsize = log->l_iclog_size;
1249 ASSERT(log->l_iclog_size >= 4096);
1250 for (i=0; i < log->l_iclog_bufs; i++) {
1251 *iclogp = (xlog_in_core_t *)
1252 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1254 iclog->ic_prev = prev_iclog;
1257 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1258 if (!XFS_BUF_CPSEMA(bp))
1260 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1261 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1262 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1264 iclog->hic_data = bp->b_addr;
1266 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1268 head = &iclog->ic_header;
1269 memset(head, 0, sizeof(xlog_rec_header_t));
1270 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1271 head->h_version = cpu_to_be32(
1272 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1273 head->h_size = cpu_to_be32(log->l_iclog_size);
1275 head->h_fmt = cpu_to_be32(XLOG_FMT);
1276 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1278 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1279 iclog->ic_state = XLOG_STATE_ACTIVE;
1280 iclog->ic_log = log;
1281 atomic_set(&iclog->ic_refcnt, 0);
1282 spin_lock_init(&iclog->ic_callback_lock);
1283 iclog->ic_callback_tail = &(iclog->ic_callback);
1284 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1286 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1287 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1288 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1289 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
1291 iclogp = &iclog->ic_next;
1293 *iclogp = log->l_iclog; /* complete ring */
1294 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1297 } /* xlog_alloc_log */
1301 * Write out the commit record of a transaction associated with the given
1302 * ticket. Return the lsn of the commit record.
1305 xlog_commit_record(xfs_mount_t *mp,
1306 xlog_ticket_t *ticket,
1307 xlog_in_core_t **iclog,
1308 xfs_lsn_t *commitlsnp)
1311 xfs_log_iovec_t reg[1];
1313 reg[0].i_addr = NULL;
1315 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_COMMIT);
1317 ASSERT_ALWAYS(iclog);
1318 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1319 iclog, XLOG_COMMIT_TRANS))) {
1320 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1323 } /* xlog_commit_record */
1327 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1328 * log space. This code pushes on the lsn which would supposedly free up
1329 * the 25% which we want to leave free. We may need to adopt a policy which
1330 * pushes on an lsn which is further along in the log once we reach the high
1331 * water mark. In this manner, we would be creating a low water mark.
1334 xlog_grant_push_ail(xfs_mount_t *mp,
1337 xlog_t *log = mp->m_log; /* pointer to the log */
1338 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1339 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1340 int free_blocks; /* free blocks left to write to */
1341 int free_bytes; /* free bytes left to write to */
1342 int threshold_block; /* block in lsn we'd like to be at */
1343 int threshold_cycle; /* lsn cycle we'd like to be at */
1346 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1348 spin_lock(&log->l_grant_lock);
1349 free_bytes = xlog_space_left(log,
1350 log->l_grant_reserve_cycle,
1351 log->l_grant_reserve_bytes);
1352 tail_lsn = log->l_tail_lsn;
1353 free_blocks = BTOBBT(free_bytes);
1356 * Set the threshold for the minimum number of free blocks in the
1357 * log to the maximum of what the caller needs, one quarter of the
1358 * log, and 256 blocks.
1360 free_threshold = BTOBB(need_bytes);
1361 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1362 free_threshold = MAX(free_threshold, 256);
1363 if (free_blocks < free_threshold) {
1364 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1365 threshold_cycle = CYCLE_LSN(tail_lsn);
1366 if (threshold_block >= log->l_logBBsize) {
1367 threshold_block -= log->l_logBBsize;
1368 threshold_cycle += 1;
1370 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
1372 /* Don't pass in an lsn greater than the lsn of the last
1373 * log record known to be on disk.
1375 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1376 threshold_lsn = log->l_last_sync_lsn;
1378 spin_unlock(&log->l_grant_lock);
1381 * Get the transaction layer to kick the dirty buffers out to
1382 * disk asynchronously. No point in trying to do this if
1383 * the filesystem is shutting down.
1385 if (threshold_lsn &&
1386 !XLOG_FORCED_SHUTDOWN(log))
1387 xfs_trans_push_ail(mp, threshold_lsn);
1388 } /* xlog_grant_push_ail */
1392 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1393 * fashion. Previously, we should have moved the current iclog
1394 * ptr in the log to point to the next available iclog. This allows further
1395 * write to continue while this code syncs out an iclog ready to go.
1396 * Before an in-core log can be written out, the data section must be scanned
1397 * to save away the 1st word of each BBSIZE block into the header. We replace
1398 * it with the current cycle count. Each BBSIZE block is tagged with the
1399 * cycle count because there in an implicit assumption that drives will
1400 * guarantee that entire 512 byte blocks get written at once. In other words,
1401 * we can't have part of a 512 byte block written and part not written. By
1402 * tagging each block, we will know which blocks are valid when recovering
1403 * after an unclean shutdown.
1405 * This routine is single threaded on the iclog. No other thread can be in
1406 * this routine with the same iclog. Changing contents of iclog can there-
1407 * fore be done without grabbing the state machine lock. Updating the global
1408 * log will require grabbing the lock though.
1410 * The entire log manager uses a logical block numbering scheme. Only
1411 * log_sync (and then only bwrite()) know about the fact that the log may
1412 * not start with block zero on a given device. The log block start offset
1413 * is added immediately before calling bwrite().
1417 xlog_sync(xlog_t *log,
1418 xlog_in_core_t *iclog)
1420 xfs_caddr_t dptr; /* pointer to byte sized element */
1423 uint count; /* byte count of bwrite */
1424 uint count_init; /* initial count before roundup */
1425 int roundoff; /* roundoff to BB or stripe */
1426 int split = 0; /* split write into two regions */
1428 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
1430 XFS_STATS_INC(xs_log_writes);
1431 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1433 /* Add for LR header */
1434 count_init = log->l_iclog_hsize + iclog->ic_offset;
1436 /* Round out the log write size */
1437 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1438 /* we have a v2 stripe unit to use */
1439 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1441 count = BBTOB(BTOBB(count_init));
1443 roundoff = count - count_init;
1444 ASSERT(roundoff >= 0);
1445 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1446 roundoff < log->l_mp->m_sb.sb_logsunit)
1448 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1449 roundoff < BBTOB(1)));
1451 /* move grant heads by roundoff in sync */
1452 spin_lock(&log->l_grant_lock);
1453 xlog_grant_add_space(log, roundoff);
1454 spin_unlock(&log->l_grant_lock);
1456 /* put cycle number in every block */
1457 xlog_pack_data(log, iclog, roundoff);
1459 /* real byte length */
1461 iclog->ic_header.h_len =
1462 cpu_to_be32(iclog->ic_offset + roundoff);
1464 iclog->ic_header.h_len =
1465 cpu_to_be32(iclog->ic_offset);
1469 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1470 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1471 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
1473 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1475 /* Do we need to split this write into 2 parts? */
1476 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1477 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1478 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1479 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1481 iclog->ic_bwritecnt = 1;
1483 XFS_BUF_SET_COUNT(bp, count);
1484 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1485 XFS_BUF_ZEROFLAGS(bp);
1489 * Do an ordered write for the log block.
1490 * Its unnecessary to flush the first split block in the log wrap case.
1492 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
1493 XFS_BUF_ORDERED(bp);
1495 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1496 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1498 xlog_verify_iclog(log, iclog, count, B_TRUE);
1500 /* account for log which doesn't start at block #0 */
1501 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1503 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1508 if ((error = XFS_bwrite(bp))) {
1509 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1514 bp = iclog->ic_log->l_xbuf;
1515 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1517 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1518 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1519 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1520 (__psint_t)count), split);
1521 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1522 XFS_BUF_ZEROFLAGS(bp);
1525 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1526 XFS_BUF_ORDERED(bp);
1527 dptr = XFS_BUF_PTR(bp);
1529 * Bump the cycle numbers at the start of each block
1530 * since this part of the buffer is at the start of
1531 * a new cycle. Watch out for the header magic number
1534 for (i = 0; i < split; i += BBSIZE) {
1535 be32_add_cpu((__be32 *)dptr, 1);
1536 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
1537 be32_add_cpu((__be32 *)dptr, 1);
1541 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1542 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1544 /* account for internal log which doesn't start at block #0 */
1545 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1547 if ((error = XFS_bwrite(bp))) {
1548 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1549 bp, XFS_BUF_ADDR(bp));
1558 * Deallocate a log structure
1561 xlog_dealloc_log(xlog_t *log)
1563 xlog_in_core_t *iclog, *next_iclog;
1566 iclog = log->l_iclog;
1567 for (i=0; i<log->l_iclog_bufs; i++) {
1568 sv_destroy(&iclog->ic_force_wait);
1569 sv_destroy(&iclog->ic_write_wait);
1570 xfs_buf_free(iclog->ic_bp);
1571 #ifdef XFS_LOG_TRACE
1572 if (iclog->ic_trace != NULL) {
1573 ktrace_free(iclog->ic_trace);
1576 next_iclog = iclog->ic_next;
1580 spinlock_destroy(&log->l_icloglock);
1581 spinlock_destroy(&log->l_grant_lock);
1583 xfs_buf_free(log->l_xbuf);
1584 #ifdef XFS_LOG_TRACE
1585 if (log->l_trace != NULL) {
1586 ktrace_free(log->l_trace);
1588 if (log->l_grant_trace != NULL) {
1589 ktrace_free(log->l_grant_trace);
1592 log->l_mp->m_log = NULL;
1594 } /* xlog_dealloc_log */
1597 * Update counters atomically now that memcpy is done.
1601 xlog_state_finish_copy(xlog_t *log,
1602 xlog_in_core_t *iclog,
1606 spin_lock(&log->l_icloglock);
1608 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1609 iclog->ic_offset += copy_bytes;
1611 spin_unlock(&log->l_icloglock);
1612 } /* xlog_state_finish_copy */
1618 * print out info relating to regions written which consume
1622 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1625 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1627 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1628 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1649 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1692 xfs_fs_cmn_err(CE_WARN, mp,
1693 "xfs_log_write: reservation summary:\n"
1694 " trans type = %s (%u)\n"
1695 " unit res = %d bytes\n"
1696 " current res = %d bytes\n"
1697 " total reg = %u bytes (o/flow = %u bytes)\n"
1698 " ophdrs = %u (ophdr space = %u bytes)\n"
1699 " ophdr + reg = %u bytes\n"
1700 " num regions = %u\n",
1701 ((ticket->t_trans_type <= 0 ||
1702 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1703 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1704 ticket->t_trans_type,
1707 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1708 ticket->t_res_num_ophdrs, ophdr_spc,
1709 ticket->t_res_arr_sum +
1710 ticket->t_res_o_flow + ophdr_spc,
1713 for (i = 0; i < ticket->t_res_num; i++) {
1714 uint r_type = ticket->t_res_arr[i].r_type;
1716 "region[%u]: %s - %u bytes\n",
1718 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1719 "bad-rtype" : res_type_str[r_type-1]),
1720 ticket->t_res_arr[i].r_len);
1725 * Write some region out to in-core log
1727 * This will be called when writing externally provided regions or when
1728 * writing out a commit record for a given transaction.
1730 * General algorithm:
1731 * 1. Find total length of this write. This may include adding to the
1732 * lengths passed in.
1733 * 2. Check whether we violate the tickets reservation.
1734 * 3. While writing to this iclog
1735 * A. Reserve as much space in this iclog as can get
1736 * B. If this is first write, save away start lsn
1737 * C. While writing this region:
1738 * 1. If first write of transaction, write start record
1739 * 2. Write log operation header (header per region)
1740 * 3. Find out if we can fit entire region into this iclog
1741 * 4. Potentially, verify destination memcpy ptr
1742 * 5. Memcpy (partial) region
1743 * 6. If partial copy, release iclog; otherwise, continue
1744 * copying more regions into current iclog
1745 * 4. Mark want sync bit (in simulation mode)
1746 * 5. Release iclog for potential flush to on-disk log.
1749 * 1. Panic if reservation is overrun. This should never happen since
1750 * reservation amounts are generated internal to the filesystem.
1752 * 1. Tickets are single threaded data structures.
1753 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1754 * syncing routine. When a single log_write region needs to span
1755 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1756 * on all log operation writes which don't contain the end of the
1757 * region. The XLOG_END_TRANS bit is used for the in-core log
1758 * operation which contains the end of the continued log_write region.
1759 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1760 * we don't really know exactly how much space will be used. As a result,
1761 * we don't update ic_offset until the end when we know exactly how many
1762 * bytes have been written out.
1765 xlog_write(xfs_mount_t * mp,
1766 xfs_log_iovec_t reg[],
1768 xfs_log_ticket_t tic,
1769 xfs_lsn_t *start_lsn,
1770 xlog_in_core_t **commit_iclog,
1773 xlog_t *log = mp->m_log;
1774 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1775 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
1776 xlog_op_header_t *logop_head; /* ptr to log operation header */
1777 __psint_t ptr; /* copy address into data region */
1778 int len; /* # xlog_write() bytes 2 still copy */
1779 int index; /* region index currently copying */
1780 int log_offset; /* offset (from 0) into data region */
1781 int start_rec_copy; /* # bytes to copy for start record */
1782 int partial_copy; /* did we split a region? */
1783 int partial_copy_len;/* # bytes copied if split region */
1784 int need_copy; /* # bytes need to memcpy this region */
1785 int copy_len; /* # bytes actually memcpy'ing */
1786 int copy_off; /* # bytes from entry start */
1787 int contwr; /* continued write of in-core log? */
1789 int record_cnt = 0, data_cnt = 0;
1791 partial_copy_len = partial_copy = 0;
1793 /* Calculate potential maximum space. Each region gets its own
1794 * xlog_op_header_t and may need to be double word aligned.
1797 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1798 len += sizeof(xlog_op_header_t);
1799 ticket->t_res_num_ophdrs++;
1802 for (index = 0; index < nentries; index++) {
1803 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1804 ticket->t_res_num_ophdrs++;
1805 len += reg[index].i_len;
1806 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
1808 contwr = *start_lsn = 0;
1810 if (ticket->t_curr_res < len) {
1811 xlog_print_tic_res(mp, ticket);
1814 "xfs_log_write: reservation ran out. Need to up reservation");
1816 /* Customer configurable panic */
1817 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1818 "xfs_log_write: reservation ran out. Need to up reservation");
1819 /* If we did not panic, shutdown the filesystem */
1820 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1823 ticket->t_curr_res -= len;
1825 for (index = 0; index < nentries; ) {
1826 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1827 &contwr, &log_offset)))
1830 ASSERT(log_offset <= iclog->ic_size - 1);
1831 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1833 /* start_lsn is the first lsn written to. That's all we need. */
1835 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
1837 /* This loop writes out as many regions as can fit in the amount
1838 * of space which was allocated by xlog_state_get_iclog_space().
1840 while (index < nentries) {
1841 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1842 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1845 /* If first write for transaction, insert start record.
1846 * We can't be trying to commit if we are inited. We can't
1847 * have any "partial_copy" if we are inited.
1849 if (ticket->t_flags & XLOG_TIC_INITED) {
1850 logop_head = (xlog_op_header_t *)ptr;
1851 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1852 logop_head->oh_clientid = ticket->t_clientid;
1853 logop_head->oh_len = 0;
1854 logop_head->oh_flags = XLOG_START_TRANS;
1855 logop_head->oh_res2 = 0;
1856 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1859 start_rec_copy = sizeof(xlog_op_header_t);
1860 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1863 /* Copy log operation header directly into data section */
1864 logop_head = (xlog_op_header_t *)ptr;
1865 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1866 logop_head->oh_clientid = ticket->t_clientid;
1867 logop_head->oh_res2 = 0;
1869 /* header copied directly */
1870 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1872 /* are we copying a commit or unmount record? */
1873 logop_head->oh_flags = flags;
1876 * We've seen logs corrupted with bad transaction client
1877 * ids. This makes sure that XFS doesn't generate them on.
1878 * Turn this into an EIO and shut down the filesystem.
1880 switch (logop_head->oh_clientid) {
1881 case XFS_TRANSACTION:
1886 xfs_fs_cmn_err(CE_WARN, mp,
1887 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1888 logop_head->oh_clientid, tic);
1889 return XFS_ERROR(EIO);
1892 /* Partial write last time? => (partial_copy != 0)
1893 * need_copy is the amount we'd like to copy if everything could
1894 * fit in the current memcpy.
1896 need_copy = reg[index].i_len - partial_copy_len;
1898 copy_off = partial_copy_len;
1899 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1900 copy_len = need_copy;
1901 logop_head->oh_len = cpu_to_be32(copy_len);
1903 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1904 partial_copy_len = partial_copy = 0;
1905 } else { /* partial write */
1906 copy_len = iclog->ic_size - log_offset;
1907 logop_head->oh_len = cpu_to_be32(copy_len);
1908 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1910 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1911 partial_copy_len += copy_len;
1913 len += sizeof(xlog_op_header_t); /* from splitting of region */
1914 /* account for new log op header */
1915 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1916 ticket->t_res_num_ophdrs++;
1918 xlog_verify_dest_ptr(log, ptr);
1921 ASSERT(copy_len >= 0);
1922 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1923 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1925 /* make copy_len total bytes copied, including headers */
1926 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1928 data_cnt += contwr ? copy_len : 0;
1929 if (partial_copy) { /* copied partial region */
1930 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1931 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1932 record_cnt = data_cnt = 0;
1933 if ((error = xlog_state_release_iclog(log, iclog)))
1935 break; /* don't increment index */
1936 } else { /* copied entire region */
1938 partial_copy_len = partial_copy = 0;
1940 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1941 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1942 record_cnt = data_cnt = 0;
1943 xlog_state_want_sync(log, iclog);
1945 ASSERT(flags & XLOG_COMMIT_TRANS);
1946 *commit_iclog = iclog;
1947 } else if ((error = xlog_state_release_iclog(log, iclog)))
1949 if (index == nentries)
1950 return 0; /* we are done */
1954 } /* if (partial_copy) */
1955 } /* while (index < nentries) */
1956 } /* for (index = 0; index < nentries; ) */
1959 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1961 ASSERT(flags & XLOG_COMMIT_TRANS);
1962 *commit_iclog = iclog;
1965 return xlog_state_release_iclog(log, iclog);
1969 /*****************************************************************************
1971 * State Machine functions
1973 *****************************************************************************
1976 /* Clean iclogs starting from the head. This ordering must be
1977 * maintained, so an iclog doesn't become ACTIVE beyond one that
1978 * is SYNCING. This is also required to maintain the notion that we use
1979 * a ordered wait queue to hold off would be writers to the log when every
1980 * iclog is trying to sync to disk.
1982 * State Change: DIRTY -> ACTIVE
1985 xlog_state_clean_log(xlog_t *log)
1987 xlog_in_core_t *iclog;
1990 iclog = log->l_iclog;
1992 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1993 iclog->ic_state = XLOG_STATE_ACTIVE;
1994 iclog->ic_offset = 0;
1995 ASSERT(iclog->ic_callback == NULL);
1997 * If the number of ops in this iclog indicate it just
1998 * contains the dummy transaction, we can
1999 * change state into IDLE (the second time around).
2000 * Otherwise we should change the state into
2002 * We don't need to cover the dummy.
2005 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2010 * We have two dirty iclogs so start over
2011 * This could also be num of ops indicates
2012 * this is not the dummy going out.
2016 iclog->ic_header.h_num_logops = 0;
2017 memset(iclog->ic_header.h_cycle_data, 0,
2018 sizeof(iclog->ic_header.h_cycle_data));
2019 iclog->ic_header.h_lsn = 0;
2020 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2023 break; /* stop cleaning */
2024 iclog = iclog->ic_next;
2025 } while (iclog != log->l_iclog);
2027 /* log is locked when we are called */
2029 * Change state for the dummy log recording.
2030 * We usually go to NEED. But we go to NEED2 if the changed indicates
2031 * we are done writing the dummy record.
2032 * If we are done with the second dummy recored (DONE2), then
2036 switch (log->l_covered_state) {
2037 case XLOG_STATE_COVER_IDLE:
2038 case XLOG_STATE_COVER_NEED:
2039 case XLOG_STATE_COVER_NEED2:
2040 log->l_covered_state = XLOG_STATE_COVER_NEED;
2043 case XLOG_STATE_COVER_DONE:
2045 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2047 log->l_covered_state = XLOG_STATE_COVER_NEED;
2050 case XLOG_STATE_COVER_DONE2:
2052 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2054 log->l_covered_state = XLOG_STATE_COVER_NEED;
2061 } /* xlog_state_clean_log */
2064 xlog_get_lowest_lsn(
2067 xlog_in_core_t *lsn_log;
2068 xfs_lsn_t lowest_lsn, lsn;
2070 lsn_log = log->l_iclog;
2073 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2074 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
2075 if ((lsn && !lowest_lsn) ||
2076 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2080 lsn_log = lsn_log->ic_next;
2081 } while (lsn_log != log->l_iclog);
2087 xlog_state_do_callback(
2090 xlog_in_core_t *ciclog)
2092 xlog_in_core_t *iclog;
2093 xlog_in_core_t *first_iclog; /* used to know when we've
2094 * processed all iclogs once */
2095 xfs_log_callback_t *cb, *cb_next;
2097 xfs_lsn_t lowest_lsn;
2098 int ioerrors; /* counter: iclogs with errors */
2099 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2100 int funcdidcallbacks; /* flag: function did callbacks */
2101 int repeats; /* for issuing console warnings if
2102 * looping too many times */
2105 spin_lock(&log->l_icloglock);
2106 first_iclog = iclog = log->l_iclog;
2108 funcdidcallbacks = 0;
2113 * Scan all iclogs starting with the one pointed to by the
2114 * log. Reset this starting point each time the log is
2115 * unlocked (during callbacks).
2117 * Keep looping through iclogs until one full pass is made
2118 * without running any callbacks.
2120 first_iclog = log->l_iclog;
2121 iclog = log->l_iclog;
2122 loopdidcallbacks = 0;
2127 /* skip all iclogs in the ACTIVE & DIRTY states */
2128 if (iclog->ic_state &
2129 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2130 iclog = iclog->ic_next;
2135 * Between marking a filesystem SHUTDOWN and stopping
2136 * the log, we do flush all iclogs to disk (if there
2137 * wasn't a log I/O error). So, we do want things to
2138 * go smoothly in case of just a SHUTDOWN w/o a
2141 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2143 * Can only perform callbacks in order. Since
2144 * this iclog is not in the DONE_SYNC/
2145 * DO_CALLBACK state, we skip the rest and
2146 * just try to clean up. If we set our iclog
2147 * to DO_CALLBACK, we will not process it when
2148 * we retry since a previous iclog is in the
2149 * CALLBACK and the state cannot change since
2150 * we are holding the l_icloglock.
2152 if (!(iclog->ic_state &
2153 (XLOG_STATE_DONE_SYNC |
2154 XLOG_STATE_DO_CALLBACK))) {
2155 if (ciclog && (ciclog->ic_state ==
2156 XLOG_STATE_DONE_SYNC)) {
2157 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2162 * We now have an iclog that is in either the
2163 * DO_CALLBACK or DONE_SYNC states. The other
2164 * states (WANT_SYNC, SYNCING, or CALLBACK were
2165 * caught by the above if and are going to
2166 * clean (i.e. we aren't doing their callbacks)
2171 * We will do one more check here to see if we
2172 * have chased our tail around.
2175 lowest_lsn = xlog_get_lowest_lsn(log);
2177 XFS_LSN_CMP(lowest_lsn,
2178 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
2179 iclog = iclog->ic_next;
2180 continue; /* Leave this iclog for
2184 iclog->ic_state = XLOG_STATE_CALLBACK;
2186 spin_unlock(&log->l_icloglock);
2188 /* l_last_sync_lsn field protected by
2189 * l_grant_lock. Don't worry about iclog's lsn.
2190 * No one else can be here except us.
2192 spin_lock(&log->l_grant_lock);
2193 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2194 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2195 log->l_last_sync_lsn =
2196 be64_to_cpu(iclog->ic_header.h_lsn);
2197 spin_unlock(&log->l_grant_lock);
2200 spin_unlock(&log->l_icloglock);
2205 * Keep processing entries in the callback list until
2206 * we come around and it is empty. We need to
2207 * atomically see that the list is empty and change the
2208 * state to DIRTY so that we don't miss any more
2209 * callbacks being added.
2211 spin_lock(&iclog->ic_callback_lock);
2212 cb = iclog->ic_callback;
2214 iclog->ic_callback_tail = &(iclog->ic_callback);
2215 iclog->ic_callback = NULL;
2216 spin_unlock(&iclog->ic_callback_lock);
2218 /* perform callbacks in the order given */
2219 for (; cb; cb = cb_next) {
2220 cb_next = cb->cb_next;
2221 cb->cb_func(cb->cb_arg, aborted);
2223 spin_lock(&iclog->ic_callback_lock);
2224 cb = iclog->ic_callback;
2230 spin_lock(&log->l_icloglock);
2231 ASSERT(iclog->ic_callback == NULL);
2232 spin_unlock(&iclog->ic_callback_lock);
2233 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2234 iclog->ic_state = XLOG_STATE_DIRTY;
2237 * Transition from DIRTY to ACTIVE if applicable.
2238 * NOP if STATE_IOERROR.
2240 xlog_state_clean_log(log);
2242 /* wake up threads waiting in xfs_log_force() */
2243 sv_broadcast(&iclog->ic_force_wait);
2245 iclog = iclog->ic_next;
2246 } while (first_iclog != iclog);
2248 if (repeats > 5000) {
2249 flushcnt += repeats;
2251 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2252 "%s: possible infinite loop (%d iterations)",
2253 __func__, flushcnt);
2255 } while (!ioerrors && loopdidcallbacks);
2258 * make one last gasp attempt to see if iclogs are being left in
2262 if (funcdidcallbacks) {
2263 first_iclog = iclog = log->l_iclog;
2265 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2267 * Terminate the loop if iclogs are found in states
2268 * which will cause other threads to clean up iclogs.
2270 * SYNCING - i/o completion will go through logs
2271 * DONE_SYNC - interrupt thread should be waiting for
2273 * IOERROR - give up hope all ye who enter here
2275 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2276 iclog->ic_state == XLOG_STATE_SYNCING ||
2277 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2278 iclog->ic_state == XLOG_STATE_IOERROR )
2280 iclog = iclog->ic_next;
2281 } while (first_iclog != iclog);
2285 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2287 spin_unlock(&log->l_icloglock);
2290 sv_broadcast(&log->l_flush_wait);
2295 * Finish transitioning this iclog to the dirty state.
2297 * Make sure that we completely execute this routine only when this is
2298 * the last call to the iclog. There is a good chance that iclog flushes,
2299 * when we reach the end of the physical log, get turned into 2 separate
2300 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2301 * routine. By using the reference count bwritecnt, we guarantee that only
2302 * the second completion goes through.
2304 * Callbacks could take time, so they are done outside the scope of the
2305 * global state machine log lock.
2308 xlog_state_done_syncing(
2309 xlog_in_core_t *iclog,
2312 xlog_t *log = iclog->ic_log;
2314 spin_lock(&log->l_icloglock);
2316 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2317 iclog->ic_state == XLOG_STATE_IOERROR);
2318 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2319 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2323 * If we got an error, either on the first buffer, or in the case of
2324 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2325 * and none should ever be attempted to be written to disk
2328 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2329 if (--iclog->ic_bwritecnt == 1) {
2330 spin_unlock(&log->l_icloglock);
2333 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2337 * Someone could be sleeping prior to writing out the next
2338 * iclog buffer, we wake them all, one will get to do the
2339 * I/O, the others get to wait for the result.
2341 sv_broadcast(&iclog->ic_write_wait);
2342 spin_unlock(&log->l_icloglock);
2343 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2344 } /* xlog_state_done_syncing */
2348 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2349 * sleep. We wait on the flush queue on the head iclog as that should be
2350 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2351 * we will wait here and all new writes will sleep until a sync completes.
2353 * The in-core logs are used in a circular fashion. They are not used
2354 * out-of-order even when an iclog past the head is free.
2357 * * log_offset where xlog_write() can start writing into the in-core
2359 * * in-core log pointer to which xlog_write() should write.
2360 * * boolean indicating this is a continued write to an in-core log.
2361 * If this is the last write, then the in-core log's offset field
2362 * needs to be incremented, depending on the amount of data which
2366 xlog_state_get_iclog_space(xlog_t *log,
2368 xlog_in_core_t **iclogp,
2369 xlog_ticket_t *ticket,
2370 int *continued_write,
2374 xlog_rec_header_t *head;
2375 xlog_in_core_t *iclog;
2379 spin_lock(&log->l_icloglock);
2380 if (XLOG_FORCED_SHUTDOWN(log)) {
2381 spin_unlock(&log->l_icloglock);
2382 return XFS_ERROR(EIO);
2385 iclog = log->l_iclog;
2386 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2387 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2388 XFS_STATS_INC(xs_log_noiclogs);
2390 /* Wait for log writes to have flushed */
2391 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
2395 head = &iclog->ic_header;
2397 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
2398 log_offset = iclog->ic_offset;
2400 /* On the 1st write to an iclog, figure out lsn. This works
2401 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2402 * committing to. If the offset is set, that's how many blocks
2405 if (log_offset == 0) {
2406 ticket->t_curr_res -= log->l_iclog_hsize;
2407 xlog_tic_add_region(ticket,
2409 XLOG_REG_TYPE_LRHEADER);
2410 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2411 head->h_lsn = cpu_to_be64(
2412 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
2413 ASSERT(log->l_curr_block >= 0);
2416 /* If there is enough room to write everything, then do it. Otherwise,
2417 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2418 * bit is on, so this will get flushed out. Don't update ic_offset
2419 * until you know exactly how many bytes get copied. Therefore, wait
2420 * until later to update ic_offset.
2422 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2423 * can fit into remaining data section.
2425 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2426 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2429 * If I'm the only one writing to this iclog, sync it to disk.
2430 * We need to do an atomic compare and decrement here to avoid
2431 * racing with concurrent atomic_dec_and_lock() calls in
2432 * xlog_state_release_iclog() when there is more than one
2433 * reference to the iclog.
2435 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2436 /* we are the only one */
2437 spin_unlock(&log->l_icloglock);
2438 error = xlog_state_release_iclog(log, iclog);
2442 spin_unlock(&log->l_icloglock);
2447 /* Do we have enough room to write the full amount in the remainder
2448 * of this iclog? Or must we continue a write on the next iclog and
2449 * mark this iclog as completely taken? In the case where we switch
2450 * iclogs (to mark it taken), this particular iclog will release/sync
2451 * to disk in xlog_write().
2453 if (len <= iclog->ic_size - iclog->ic_offset) {
2454 *continued_write = 0;
2455 iclog->ic_offset += len;
2457 *continued_write = 1;
2458 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2462 ASSERT(iclog->ic_offset <= iclog->ic_size);
2463 spin_unlock(&log->l_icloglock);
2465 *logoffsetp = log_offset;
2467 } /* xlog_state_get_iclog_space */
2470 * Atomically get the log space required for a log ticket.
2472 * Once a ticket gets put onto the reserveq, it will only return after
2473 * the needed reservation is satisfied.
2476 xlog_grant_log_space(xlog_t *log,
2487 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2488 panic("grant Recovery problem");
2491 /* Is there space or do we need to sleep? */
2492 spin_lock(&log->l_grant_lock);
2493 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2495 /* something is already sleeping; insert new transaction at end */
2496 if (log->l_reserve_headq) {
2497 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2498 xlog_trace_loggrant(log, tic,
2499 "xlog_grant_log_space: sleep 1");
2501 * Gotta check this before going to sleep, while we're
2502 * holding the grant lock.
2504 if (XLOG_FORCED_SHUTDOWN(log))
2507 XFS_STATS_INC(xs_sleep_logspace);
2508 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2510 * If we got an error, and the filesystem is shutting down,
2511 * we'll catch it down below. So just continue...
2513 xlog_trace_loggrant(log, tic,
2514 "xlog_grant_log_space: wake 1");
2515 spin_lock(&log->l_grant_lock);
2517 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2518 need_bytes = tic->t_unit_res*tic->t_ocnt;
2520 need_bytes = tic->t_unit_res;
2523 if (XLOG_FORCED_SHUTDOWN(log))
2526 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2527 log->l_grant_reserve_bytes);
2528 if (free_bytes < need_bytes) {
2529 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2530 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2531 xlog_trace_loggrant(log, tic,
2532 "xlog_grant_log_space: sleep 2");
2533 XFS_STATS_INC(xs_sleep_logspace);
2534 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2536 if (XLOG_FORCED_SHUTDOWN(log)) {
2537 spin_lock(&log->l_grant_lock);
2541 xlog_trace_loggrant(log, tic,
2542 "xlog_grant_log_space: wake 2");
2543 xlog_grant_push_ail(log->l_mp, need_bytes);
2544 spin_lock(&log->l_grant_lock);
2546 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2547 xlog_del_ticketq(&log->l_reserve_headq, tic);
2549 /* we've got enough space */
2550 xlog_grant_add_space(log, need_bytes);
2552 tail_lsn = log->l_tail_lsn;
2554 * Check to make sure the grant write head didn't just over lap the
2555 * tail. If the cycles are the same, we can't be overlapping.
2556 * Otherwise, make sure that the cycles differ by exactly one and
2557 * check the byte count.
2559 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2560 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2561 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2564 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2565 xlog_verify_grant_head(log, 1);
2566 spin_unlock(&log->l_grant_lock);
2570 if (tic->t_flags & XLOG_TIC_IN_Q)
2571 xlog_del_ticketq(&log->l_reserve_headq, tic);
2572 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2574 * If we are failing, make sure the ticket doesn't have any
2575 * current reservations. We don't want to add this back when
2576 * the ticket/transaction gets cancelled.
2578 tic->t_curr_res = 0;
2579 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2580 spin_unlock(&log->l_grant_lock);
2581 return XFS_ERROR(EIO);
2582 } /* xlog_grant_log_space */
2586 * Replenish the byte reservation required by moving the grant write head.
2591 xlog_regrant_write_log_space(xlog_t *log,
2594 int free_bytes, need_bytes;
2595 xlog_ticket_t *ntic;
2600 tic->t_curr_res = tic->t_unit_res;
2601 xlog_tic_reset_res(tic);
2607 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2608 panic("regrant Recovery problem");
2611 spin_lock(&log->l_grant_lock);
2612 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2614 if (XLOG_FORCED_SHUTDOWN(log))
2617 /* If there are other waiters on the queue then give them a
2618 * chance at logspace before us. Wake up the first waiters,
2619 * if we do not wake up all the waiters then go to sleep waiting
2620 * for more free space, otherwise try to get some space for
2624 if ((ntic = log->l_write_headq)) {
2625 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2626 log->l_grant_write_bytes);
2628 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2630 if (free_bytes < ntic->t_unit_res)
2632 free_bytes -= ntic->t_unit_res;
2633 sv_signal(&ntic->t_wait);
2634 ntic = ntic->t_next;
2635 } while (ntic != log->l_write_headq);
2637 if (ntic != log->l_write_headq) {
2638 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2639 xlog_ins_ticketq(&log->l_write_headq, tic);
2641 xlog_trace_loggrant(log, tic,
2642 "xlog_regrant_write_log_space: sleep 1");
2643 XFS_STATS_INC(xs_sleep_logspace);
2644 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
2645 &log->l_grant_lock, s);
2647 /* If we're shutting down, this tic is already
2649 if (XLOG_FORCED_SHUTDOWN(log)) {
2650 spin_lock(&log->l_grant_lock);
2654 xlog_trace_loggrant(log, tic,
2655 "xlog_regrant_write_log_space: wake 1");
2656 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2657 spin_lock(&log->l_grant_lock);
2661 need_bytes = tic->t_unit_res;
2664 if (XLOG_FORCED_SHUTDOWN(log))
2667 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2668 log->l_grant_write_bytes);
2669 if (free_bytes < need_bytes) {
2670 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2671 xlog_ins_ticketq(&log->l_write_headq, tic);
2672 XFS_STATS_INC(xs_sleep_logspace);
2673 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2675 /* If we're shutting down, this tic is already off the queue */
2676 if (XLOG_FORCED_SHUTDOWN(log)) {
2677 spin_lock(&log->l_grant_lock);
2681 xlog_trace_loggrant(log, tic,
2682 "xlog_regrant_write_log_space: wake 2");
2683 xlog_grant_push_ail(log->l_mp, need_bytes);
2684 spin_lock(&log->l_grant_lock);
2686 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2687 xlog_del_ticketq(&log->l_write_headq, tic);
2689 /* we've got enough space */
2690 xlog_grant_add_space_write(log, need_bytes);
2692 tail_lsn = log->l_tail_lsn;
2693 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2694 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2695 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2699 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2700 xlog_verify_grant_head(log, 1);
2701 spin_unlock(&log->l_grant_lock);
2706 if (tic->t_flags & XLOG_TIC_IN_Q)
2707 xlog_del_ticketq(&log->l_reserve_headq, tic);
2708 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2710 * If we are failing, make sure the ticket doesn't have any
2711 * current reservations. We don't want to add this back when
2712 * the ticket/transaction gets cancelled.
2714 tic->t_curr_res = 0;
2715 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2716 spin_unlock(&log->l_grant_lock);
2717 return XFS_ERROR(EIO);
2718 } /* xlog_regrant_write_log_space */
2721 /* The first cnt-1 times through here we don't need to
2722 * move the grant write head because the permanent
2723 * reservation has reserved cnt times the unit amount.
2724 * Release part of current permanent unit reservation and
2725 * reset current reservation to be one units worth. Also
2726 * move grant reservation head forward.
2729 xlog_regrant_reserve_log_space(xlog_t *log,
2730 xlog_ticket_t *ticket)
2732 xlog_trace_loggrant(log, ticket,
2733 "xlog_regrant_reserve_log_space: enter");
2734 if (ticket->t_cnt > 0)
2737 spin_lock(&log->l_grant_lock);
2738 xlog_grant_sub_space(log, ticket->t_curr_res);
2739 ticket->t_curr_res = ticket->t_unit_res;
2740 xlog_tic_reset_res(ticket);
2741 xlog_trace_loggrant(log, ticket,
2742 "xlog_regrant_reserve_log_space: sub current res");
2743 xlog_verify_grant_head(log, 1);
2745 /* just return if we still have some of the pre-reserved space */
2746 if (ticket->t_cnt > 0) {
2747 spin_unlock(&log->l_grant_lock);
2751 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
2752 xlog_trace_loggrant(log, ticket,
2753 "xlog_regrant_reserve_log_space: exit");
2754 xlog_verify_grant_head(log, 0);
2755 spin_unlock(&log->l_grant_lock);
2756 ticket->t_curr_res = ticket->t_unit_res;
2757 xlog_tic_reset_res(ticket);
2758 } /* xlog_regrant_reserve_log_space */
2762 * Give back the space left from a reservation.
2764 * All the information we need to make a correct determination of space left
2765 * is present. For non-permanent reservations, things are quite easy. The
2766 * count should have been decremented to zero. We only need to deal with the
2767 * space remaining in the current reservation part of the ticket. If the
2768 * ticket contains a permanent reservation, there may be left over space which
2769 * needs to be released. A count of N means that N-1 refills of the current
2770 * reservation can be done before we need to ask for more space. The first
2771 * one goes to fill up the first current reservation. Once we run out of
2772 * space, the count will stay at zero and the only space remaining will be
2773 * in the current reservation field.
2776 xlog_ungrant_log_space(xlog_t *log,
2777 xlog_ticket_t *ticket)
2779 if (ticket->t_cnt > 0)
2782 spin_lock(&log->l_grant_lock);
2783 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2785 xlog_grant_sub_space(log, ticket->t_curr_res);
2787 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2789 /* If this is a permanent reservation ticket, we may be able to free
2790 * up more space based on the remaining count.
2792 if (ticket->t_cnt > 0) {
2793 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2794 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
2797 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2798 xlog_verify_grant_head(log, 1);
2799 spin_unlock(&log->l_grant_lock);
2800 xfs_log_move_tail(log->l_mp, 1);
2801 } /* xlog_ungrant_log_space */
2805 * Flush iclog to disk if this is the last reference to the given iclog and
2806 * the WANT_SYNC bit is set.
2808 * When this function is entered, the iclog is not necessarily in the
2809 * WANT_SYNC state. It may be sitting around waiting to get filled.
2814 xlog_state_release_iclog(
2816 xlog_in_core_t *iclog)
2818 int sync = 0; /* do we sync? */
2820 if (iclog->ic_state & XLOG_STATE_IOERROR)
2821 return XFS_ERROR(EIO);
2823 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2824 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2827 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2828 spin_unlock(&log->l_icloglock);
2829 return XFS_ERROR(EIO);
2831 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2832 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2834 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2835 /* update tail before writing to iclog */
2836 xlog_assign_tail_lsn(log->l_mp);
2838 iclog->ic_state = XLOG_STATE_SYNCING;
2839 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
2840 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2841 /* cycle incremented when incrementing curr_block */
2843 spin_unlock(&log->l_icloglock);
2846 * We let the log lock go, so it's possible that we hit a log I/O
2847 * error or some other SHUTDOWN condition that marks the iclog
2848 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2849 * this iclog has consistent data, so we ignore IOERROR
2850 * flags after this point.
2853 return xlog_sync(log, iclog);
2855 } /* xlog_state_release_iclog */
2859 * This routine will mark the current iclog in the ring as WANT_SYNC
2860 * and move the current iclog pointer to the next iclog in the ring.
2861 * When this routine is called from xlog_state_get_iclog_space(), the
2862 * exact size of the iclog has not yet been determined. All we know is
2863 * that every data block. We have run out of space in this log record.
2866 xlog_state_switch_iclogs(xlog_t *log,
2867 xlog_in_core_t *iclog,
2870 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2872 eventual_size = iclog->ic_offset;
2873 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2874 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
2875 log->l_prev_block = log->l_curr_block;
2876 log->l_prev_cycle = log->l_curr_cycle;
2878 /* roll log?: ic_offset changed later */
2879 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2881 /* Round up to next log-sunit */
2882 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
2883 log->l_mp->m_sb.sb_logsunit > 1) {
2884 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2885 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2888 if (log->l_curr_block >= log->l_logBBsize) {
2889 log->l_curr_cycle++;
2890 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2891 log->l_curr_cycle++;
2892 log->l_curr_block -= log->l_logBBsize;
2893 ASSERT(log->l_curr_block >= 0);
2895 ASSERT(iclog == log->l_iclog);
2896 log->l_iclog = iclog->ic_next;
2897 } /* xlog_state_switch_iclogs */
2901 * Write out all data in the in-core log as of this exact moment in time.
2903 * Data may be written to the in-core log during this call. However,
2904 * we don't guarantee this data will be written out. A change from past
2905 * implementation means this routine will *not* write out zero length LRs.
2907 * Basically, we try and perform an intelligent scan of the in-core logs.
2908 * If we determine there is no flushable data, we just return. There is no
2909 * flushable data if:
2911 * 1. the current iclog is active and has no data; the previous iclog
2912 * is in the active or dirty state.
2913 * 2. the current iclog is drity, and the previous iclog is in the
2914 * active or dirty state.
2918 * 1. the current iclog is not in the active nor dirty state.
2919 * 2. the current iclog dirty, and the previous iclog is not in the
2920 * active nor dirty state.
2921 * 3. the current iclog is active, and there is another thread writing
2922 * to this particular iclog.
2923 * 4. a) the current iclog is active and has no other writers
2924 * b) when we return from flushing out this iclog, it is still
2925 * not in the active nor dirty state.
2928 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2930 xlog_in_core_t *iclog;
2933 spin_lock(&log->l_icloglock);
2935 iclog = log->l_iclog;
2936 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2937 spin_unlock(&log->l_icloglock);
2938 return XFS_ERROR(EIO);
2941 /* If the head iclog is not active nor dirty, we just attach
2942 * ourselves to the head and go to sleep.
2944 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2945 iclog->ic_state == XLOG_STATE_DIRTY) {
2947 * If the head is dirty or (active and empty), then
2948 * we need to look at the previous iclog. If the previous
2949 * iclog is active or dirty we are done. There is nothing
2950 * to sync out. Otherwise, we attach ourselves to the
2951 * previous iclog and go to sleep.
2953 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2954 (atomic_read(&iclog->ic_refcnt) == 0
2955 && iclog->ic_offset == 0)) {
2956 iclog = iclog->ic_prev;
2957 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2958 iclog->ic_state == XLOG_STATE_DIRTY)
2963 if (atomic_read(&iclog->ic_refcnt) == 0) {
2964 /* We are the only one with access to this
2965 * iclog. Flush it out now. There should
2966 * be a roundoff of zero to show that someone
2967 * has already taken care of the roundoff from
2968 * the previous sync.
2970 atomic_inc(&iclog->ic_refcnt);
2971 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2972 xlog_state_switch_iclogs(log, iclog, 0);
2973 spin_unlock(&log->l_icloglock);
2975 if (xlog_state_release_iclog(log, iclog))
2976 return XFS_ERROR(EIO);
2978 spin_lock(&log->l_icloglock);
2979 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
2980 iclog->ic_state != XLOG_STATE_DIRTY)
2985 /* Someone else is writing to this iclog.
2986 * Use its call to flush out the data. However,
2987 * the other thread may not force out this LR,
2988 * so we mark it WANT_SYNC.
2990 xlog_state_switch_iclogs(log, iclog, 0);
2996 /* By the time we come around again, the iclog could've been filled
2997 * which would give it another lsn. If we have a new lsn, just
2998 * return because the relevant data has been flushed.
3001 if (flags & XFS_LOG_SYNC) {
3003 * We must check if we're shutting down here, before
3004 * we wait, while we're holding the l_icloglock.
3005 * Then we check again after waking up, in case our
3006 * sleep was disturbed by a bad news.
3008 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3009 spin_unlock(&log->l_icloglock);
3010 return XFS_ERROR(EIO);
3012 XFS_STATS_INC(xs_log_force_sleep);
3013 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
3015 * No need to grab the log lock here since we're
3016 * only deciding whether or not to return EIO
3017 * and the memory read should be atomic.
3019 if (iclog->ic_state & XLOG_STATE_IOERROR)
3020 return XFS_ERROR(EIO);
3026 spin_unlock(&log->l_icloglock);
3029 } /* xlog_state_sync_all */
3033 * Used by code which implements synchronous log forces.
3035 * Find in-core log with lsn.
3036 * If it is in the DIRTY state, just return.
3037 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3038 * state and go to sleep or return.
3039 * If it is in any other state, go to sleep or return.
3041 * If filesystem activity goes to zero, the iclog will get flushed only by
3045 xlog_state_sync(xlog_t *log,
3050 xlog_in_core_t *iclog;
3051 int already_slept = 0;
3054 spin_lock(&log->l_icloglock);
3055 iclog = log->l_iclog;
3057 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3058 spin_unlock(&log->l_icloglock);
3059 return XFS_ERROR(EIO);
3063 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3064 iclog = iclog->ic_next;
3068 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3069 spin_unlock(&log->l_icloglock);
3073 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3075 * We sleep here if we haven't already slept (e.g.
3076 * this is the first time we've looked at the correct
3077 * iclog buf) and the buffer before us is going to
3078 * be sync'ed. The reason for this is that if we
3079 * are doing sync transactions here, by waiting for
3080 * the previous I/O to complete, we can allow a few
3081 * more transactions into this iclog before we close
3084 * Otherwise, we mark the buffer WANT_SYNC, and bump
3085 * up the refcnt so we can release the log (which drops
3086 * the ref count). The state switch keeps new transaction
3087 * commits from using this buffer. When the current commits
3088 * finish writing into the buffer, the refcount will drop to
3089 * zero and the buffer will go out then.
3091 if (!already_slept &&
3092 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3093 XLOG_STATE_SYNCING))) {
3094 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3095 XFS_STATS_INC(xs_log_force_sleep);
3096 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
3097 &log->l_icloglock, s);
3102 atomic_inc(&iclog->ic_refcnt);
3103 xlog_state_switch_iclogs(log, iclog, 0);
3104 spin_unlock(&log->l_icloglock);
3105 if (xlog_state_release_iclog(log, iclog))
3106 return XFS_ERROR(EIO);
3108 spin_lock(&log->l_icloglock);
3112 if ((flags & XFS_LOG_SYNC) && /* sleep */
3113 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3116 * Don't wait on completion if we know that we've
3117 * gotten a log write error.
3119 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3120 spin_unlock(&log->l_icloglock);
3121 return XFS_ERROR(EIO);
3123 XFS_STATS_INC(xs_log_force_sleep);
3124 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3126 * No need to grab the log lock here since we're
3127 * only deciding whether or not to return EIO
3128 * and the memory read should be atomic.
3130 if (iclog->ic_state & XLOG_STATE_IOERROR)
3131 return XFS_ERROR(EIO);
3133 } else { /* just return */
3134 spin_unlock(&log->l_icloglock);
3138 } while (iclog != log->l_iclog);
3140 spin_unlock(&log->l_icloglock);
3142 } /* xlog_state_sync */
3146 * Called when we want to mark the current iclog as being ready to sync to
3150 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3152 spin_lock(&log->l_icloglock);
3154 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3155 xlog_state_switch_iclogs(log, iclog, 0);
3157 ASSERT(iclog->ic_state &
3158 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3161 spin_unlock(&log->l_icloglock);
3162 } /* xlog_state_want_sync */
3166 /*****************************************************************************
3170 *****************************************************************************
3174 * Free a used ticket.
3177 xlog_ticket_put(xlog_t *log,
3178 xlog_ticket_t *ticket)
3180 sv_destroy(&ticket->t_wait);
3181 kmem_zone_free(xfs_log_ticket_zone, ticket);
3182 } /* xlog_ticket_put */
3186 * Allocate and initialise a new log ticket.
3188 STATIC xlog_ticket_t *
3189 xlog_ticket_get(xlog_t *log,
3198 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3203 * Permanent reservations have up to 'cnt'-1 active log operations
3204 * in the log. A unit in this case is the amount of space for one
3205 * of these log operations. Normal reservations have a cnt of 1
3206 * and their unit amount is the total amount of space required.
3208 * The following lines of code account for non-transaction data
3209 * which occupy space in the on-disk log.
3211 * Normal form of a transaction is:
3212 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3213 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3215 * We need to account for all the leadup data and trailer data
3216 * around the transaction data.
3217 * And then we need to account for the worst case in terms of using
3219 * The worst case will happen if:
3220 * - the placement of the transaction happens to be such that the
3221 * roundoff is at its maximum
3222 * - the transaction data is synced before the commit record is synced
3223 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3224 * Therefore the commit record is in its own Log Record.
3225 * This can happen as the commit record is called with its
3226 * own region to xlog_write().
3227 * This then means that in the worst case, roundoff can happen for
3228 * the commit-rec as well.
3229 * The commit-rec is smaller than padding in this scenario and so it is
3230 * not added separately.
3233 /* for trans header */
3234 unit_bytes += sizeof(xlog_op_header_t);
3235 unit_bytes += sizeof(xfs_trans_header_t);
3238 unit_bytes += sizeof(xlog_op_header_t);
3240 /* for LR headers */
3241 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3242 unit_bytes += log->l_iclog_hsize * num_headers;
3244 /* for commit-rec LR header - note: padding will subsume the ophdr */
3245 unit_bytes += log->l_iclog_hsize;
3247 /* for split-recs - ophdrs added when data split over LRs */
3248 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3250 /* for roundoff padding for transaction data and one for commit record */
3251 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
3252 log->l_mp->m_sb.sb_logsunit > 1) {
3253 /* log su roundoff */
3254 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3257 unit_bytes += 2*BBSIZE;
3260 tic->t_unit_res = unit_bytes;
3261 tic->t_curr_res = unit_bytes;
3264 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3265 tic->t_clientid = client;
3266 tic->t_flags = XLOG_TIC_INITED;
3267 tic->t_trans_type = 0;
3268 if (xflags & XFS_LOG_PERM_RESERV)
3269 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3270 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
3272 xlog_tic_reset_res(tic);
3275 } /* xlog_ticket_get */
3278 /******************************************************************************
3280 * Log debug routines
3282 ******************************************************************************
3286 * Make sure that the destination ptr is within the valid data region of
3287 * one of the iclogs. This uses backup pointers stored in a different
3288 * part of the log in case we trash the log structure.
3291 xlog_verify_dest_ptr(xlog_t *log,
3297 for (i=0; i < log->l_iclog_bufs; i++) {
3298 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3299 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3303 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3304 } /* xlog_verify_dest_ptr */
3307 xlog_verify_grant_head(xlog_t *log, int equals)
3309 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3311 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3313 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3315 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3316 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3318 } /* xlog_verify_grant_head */
3320 /* check if it will fit */
3322 xlog_verify_tail_lsn(xlog_t *log,
3323 xlog_in_core_t *iclog,
3328 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3330 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3331 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3332 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3334 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3336 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3337 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3339 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3340 if (blocks < BTOBB(iclog->ic_offset) + 1)
3341 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3343 } /* xlog_verify_tail_lsn */
3346 * Perform a number of checks on the iclog before writing to disk.
3348 * 1. Make sure the iclogs are still circular
3349 * 2. Make sure we have a good magic number
3350 * 3. Make sure we don't have magic numbers in the data
3351 * 4. Check fields of each log operation header for:
3352 * A. Valid client identifier
3353 * B. tid ptr value falls in valid ptr space (user space code)
3354 * C. Length in log record header is correct according to the
3355 * individual operation headers within record.
3356 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3357 * log, check the preceding blocks of the physical log to make sure all
3358 * the cycle numbers agree with the current cycle number.
3361 xlog_verify_iclog(xlog_t *log,
3362 xlog_in_core_t *iclog,
3366 xlog_op_header_t *ophead;
3367 xlog_in_core_t *icptr;
3368 xlog_in_core_2_t *xhdr;
3370 xfs_caddr_t base_ptr;
3371 __psint_t field_offset;
3373 int len, i, j, k, op_len;
3376 /* check validity of iclog pointers */
3377 spin_lock(&log->l_icloglock);
3378 icptr = log->l_iclog;
3379 for (i=0; i < log->l_iclog_bufs; i++) {
3381 xlog_panic("xlog_verify_iclog: invalid ptr");
3382 icptr = icptr->ic_next;
3384 if (icptr != log->l_iclog)
3385 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3386 spin_unlock(&log->l_icloglock);
3388 /* check log magic numbers */
3389 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
3390 xlog_panic("xlog_verify_iclog: invalid magic num");
3392 ptr = (xfs_caddr_t) &iclog->ic_header;
3393 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
3395 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
3396 xlog_panic("xlog_verify_iclog: unexpected magic num");
3400 len = be32_to_cpu(iclog->ic_header.h_num_logops);
3401 ptr = iclog->ic_datap;
3403 ophead = (xlog_op_header_t *)ptr;
3404 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3405 for (i = 0; i < len; i++) {
3406 ophead = (xlog_op_header_t *)ptr;
3408 /* clientid is only 1 byte */
3409 field_offset = (__psint_t)
3410 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3411 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3412 clientid = ophead->oh_clientid;
3414 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3415 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3416 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3417 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3418 clientid = xlog_get_client_id(
3419 xhdr[j].hic_xheader.xh_cycle_data[k]);
3421 clientid = xlog_get_client_id(
3422 iclog->ic_header.h_cycle_data[idx]);
3425 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3426 cmn_err(CE_WARN, "xlog_verify_iclog: "
3427 "invalid clientid %d op 0x%p offset 0x%lx",
3428 clientid, ophead, (unsigned long)field_offset);
3431 field_offset = (__psint_t)
3432 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3433 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3434 op_len = be32_to_cpu(ophead->oh_len);
3436 idx = BTOBBT((__psint_t)&ophead->oh_len -
3437 (__psint_t)iclog->ic_datap);
3438 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3439 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3440 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3441 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
3443 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
3446 ptr += sizeof(xlog_op_header_t) + op_len;
3448 } /* xlog_verify_iclog */
3452 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3458 xlog_in_core_t *iclog, *ic;
3460 iclog = log->l_iclog;
3461 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3463 * Mark all the incore logs IOERROR.
3464 * From now on, no log flushes will result.
3468 ic->ic_state = XLOG_STATE_IOERROR;
3470 } while (ic != iclog);
3474 * Return non-zero, if state transition has already happened.
3480 * This is called from xfs_force_shutdown, when we're forcibly
3481 * shutting down the filesystem, typically because of an IO error.
3482 * Our main objectives here are to make sure that:
3483 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3484 * parties to find out, 'atomically'.
3485 * b. those who're sleeping on log reservations, pinned objects and
3486 * other resources get woken up, and be told the bad news.
3487 * c. nothing new gets queued up after (a) and (b) are done.
3488 * d. if !logerror, flush the iclogs to disk, then seal them off
3492 xfs_log_force_umount(
3493 struct xfs_mount *mp,
3504 * If this happens during log recovery, don't worry about
3505 * locking; the log isn't open for business yet.
3508 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3509 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3510 XFS_BUF_DONE(mp->m_sb_bp);
3515 * Somebody could've already done the hard work for us.
3516 * No need to get locks for this.
3518 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3519 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3524 * We must hold both the GRANT lock and the LOG lock,
3525 * before we mark the filesystem SHUTDOWN and wake
3526 * everybody up to tell the bad news.
3528 spin_lock(&log->l_icloglock);
3529 spin_lock(&log->l_grant_lock);
3530 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3531 XFS_BUF_DONE(mp->m_sb_bp);
3533 * This flag is sort of redundant because of the mount flag, but
3534 * it's good to maintain the separation between the log and the rest
3537 log->l_flags |= XLOG_IO_ERROR;
3540 * If we hit a log error, we want to mark all the iclogs IOERROR
3541 * while we're still holding the loglock.
3544 retval = xlog_state_ioerror(log);
3545 spin_unlock(&log->l_icloglock);
3548 * We don't want anybody waiting for log reservations
3549 * after this. That means we have to wake up everybody
3550 * queued up on reserve_headq as well as write_headq.
3551 * In addition, we make sure in xlog_{re}grant_log_space
3552 * that we don't enqueue anything once the SHUTDOWN flag
3553 * is set, and this action is protected by the GRANTLOCK.
3555 if ((tic = log->l_reserve_headq)) {
3557 sv_signal(&tic->t_wait);
3559 } while (tic != log->l_reserve_headq);
3562 if ((tic = log->l_write_headq)) {
3564 sv_signal(&tic->t_wait);
3566 } while (tic != log->l_write_headq);
3568 spin_unlock(&log->l_grant_lock);
3570 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3573 * Force the incore logs to disk before shutting the
3574 * log down completely.
3576 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3577 spin_lock(&log->l_icloglock);
3578 retval = xlog_state_ioerror(log);
3579 spin_unlock(&log->l_icloglock);
3582 * Wake up everybody waiting on xfs_log_force.
3583 * Callback all log item committed functions as if the
3584 * log writes were completed.
3586 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3588 #ifdef XFSERRORDEBUG
3590 xlog_in_core_t *iclog;
3592 spin_lock(&log->l_icloglock);
3593 iclog = log->l_iclog;
3595 ASSERT(iclog->ic_callback == 0);
3596 iclog = iclog->ic_next;
3597 } while (iclog != log->l_iclog);
3598 spin_unlock(&log->l_icloglock);
3601 /* return non-zero if log IOERROR transition had already happened */
3606 xlog_iclogs_empty(xlog_t *log)
3608 xlog_in_core_t *iclog;
3610 iclog = log->l_iclog;
3612 /* endianness does not matter here, zero is zero in
3615 if (iclog->ic_header.h_num_logops)
3617 iclog = iclog->ic_next;
3618 } while (iclog != log->l_iclog);