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_alloc(xlog_t *log,
110 STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
111 STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
112 STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
113 int count, boolean_t syncing);
114 STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
117 #define xlog_verify_dest_ptr(a,b)
118 #define xlog_verify_grant_head(a,b)
119 #define xlog_verify_iclog(a,b,c,d)
120 #define xlog_verify_tail_lsn(a,b,c)
123 STATIC int xlog_iclogs_empty(xlog_t *log);
125 #if defined(XFS_LOG_TRACE)
127 #define XLOG_TRACE_LOGGRANT_SIZE 2048
128 #define XLOG_TRACE_ICLOG_SIZE 256
131 xlog_trace_loggrant_alloc(xlog_t *log)
133 log->l_grant_trace = ktrace_alloc(XLOG_TRACE_LOGGRANT_SIZE, KM_NOFS);
137 xlog_trace_loggrant_dealloc(xlog_t *log)
139 ktrace_free(log->l_grant_trace);
143 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
147 /* ticket counts are 1 byte each */
148 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
150 ktrace_enter(log->l_grant_trace,
152 (void *)log->l_reserve_headq,
153 (void *)log->l_write_headq,
154 (void *)((unsigned long)log->l_grant_reserve_cycle),
155 (void *)((unsigned long)log->l_grant_reserve_bytes),
156 (void *)((unsigned long)log->l_grant_write_cycle),
157 (void *)((unsigned long)log->l_grant_write_bytes),
158 (void *)((unsigned long)log->l_curr_cycle),
159 (void *)((unsigned long)log->l_curr_block),
160 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
161 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
163 (void *)((unsigned long)tic->t_trans_type),
165 (void *)((unsigned long)tic->t_curr_res),
166 (void *)((unsigned long)tic->t_unit_res));
170 xlog_trace_iclog_alloc(xlog_in_core_t *iclog)
172 iclog->ic_trace = ktrace_alloc(XLOG_TRACE_ICLOG_SIZE, KM_NOFS);
176 xlog_trace_iclog_dealloc(xlog_in_core_t *iclog)
178 ktrace_free(iclog->ic_trace);
182 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
184 ktrace_enter(iclog->ic_trace,
185 (void *)((unsigned long)state),
186 (void *)((unsigned long)current_pid()),
187 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
188 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
189 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
190 (void *)NULL, (void *)NULL);
194 #define xlog_trace_loggrant_alloc(log)
195 #define xlog_trace_loggrant_dealloc(log)
196 #define xlog_trace_loggrant(log,tic,string)
198 #define xlog_trace_iclog_alloc(iclog)
199 #define xlog_trace_iclog_dealloc(iclog)
200 #define xlog_trace_iclog(iclog,state)
202 #endif /* XFS_LOG_TRACE */
206 xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
210 tic->t_prev = (*qp)->t_prev;
211 (*qp)->t_prev->t_next = tic;
214 tic->t_prev = tic->t_next = tic;
218 tic->t_flags |= XLOG_TIC_IN_Q;
222 xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
224 if (tic == tic->t_next) {
228 tic->t_next->t_prev = tic->t_prev;
229 tic->t_prev->t_next = tic->t_next;
232 tic->t_next = tic->t_prev = NULL;
233 tic->t_flags &= ~XLOG_TIC_IN_Q;
237 xlog_grant_sub_space(struct log *log, int bytes)
239 log->l_grant_write_bytes -= bytes;
240 if (log->l_grant_write_bytes < 0) {
241 log->l_grant_write_bytes += log->l_logsize;
242 log->l_grant_write_cycle--;
245 log->l_grant_reserve_bytes -= bytes;
246 if ((log)->l_grant_reserve_bytes < 0) {
247 log->l_grant_reserve_bytes += log->l_logsize;
248 log->l_grant_reserve_cycle--;
254 xlog_grant_add_space_write(struct log *log, int bytes)
256 int tmp = log->l_logsize - log->l_grant_write_bytes;
258 log->l_grant_write_bytes += bytes;
260 log->l_grant_write_cycle++;
261 log->l_grant_write_bytes = bytes - tmp;
266 xlog_grant_add_space_reserve(struct log *log, int bytes)
268 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
270 log->l_grant_reserve_bytes += bytes;
272 log->l_grant_reserve_cycle++;
273 log->l_grant_reserve_bytes = bytes - tmp;
278 xlog_grant_add_space(struct log *log, int bytes)
280 xlog_grant_add_space_write(log, bytes);
281 xlog_grant_add_space_reserve(log, bytes);
285 xlog_tic_reset_res(xlog_ticket_t *tic)
288 tic->t_res_arr_sum = 0;
289 tic->t_res_num_ophdrs = 0;
293 xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
295 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
296 /* add to overflow and start again */
297 tic->t_res_o_flow += tic->t_res_arr_sum;
299 tic->t_res_arr_sum = 0;
302 tic->t_res_arr[tic->t_res_num].r_len = len;
303 tic->t_res_arr[tic->t_res_num].r_type = type;
304 tic->t_res_arr_sum += len;
311 * 1. currblock field gets updated at startup and after in-core logs
312 * marked as with WANT_SYNC.
316 * This routine is called when a user of a log manager ticket is done with
317 * the reservation. If the ticket was ever used, then a commit record for
318 * the associated transaction is written out as a log operation header with
319 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
320 * a given ticket. If the ticket was one with a permanent reservation, then
321 * a few operations are done differently. Permanent reservation tickets by
322 * default don't release the reservation. They just commit the current
323 * transaction with the belief that the reservation is still needed. A flag
324 * must be passed in before permanent reservations are actually released.
325 * When these type of tickets are not released, they need to be set into
326 * the inited state again. By doing this, a start record will be written
327 * out when the next write occurs.
330 xfs_log_done(xfs_mount_t *mp,
331 xfs_log_ticket_t xtic,
335 xlog_t *log = mp->m_log;
336 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
339 if (XLOG_FORCED_SHUTDOWN(log) ||
341 * If nothing was ever written, don't write out commit record.
342 * If we get an error, just continue and give back the log ticket.
344 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
345 (xlog_commit_record(mp, ticket,
346 (xlog_in_core_t **)iclog, &lsn)))) {
347 lsn = (xfs_lsn_t) -1;
348 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
349 flags |= XFS_LOG_REL_PERM_RESERV;
354 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
355 (flags & XFS_LOG_REL_PERM_RESERV)) {
357 * Release ticket if not permanent reservation or a specific
358 * request has been made to release a permanent reservation.
360 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
361 xlog_ungrant_log_space(log, ticket);
362 xfs_log_ticket_put(ticket);
364 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
365 xlog_regrant_reserve_log_space(log, ticket);
366 /* If this ticket was a permanent reservation and we aren't
367 * trying to release it, reset the inited flags; so next time
368 * we write, a start record will be written out.
370 ticket->t_flags |= XLOG_TIC_INITED;
378 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
379 * the force is done synchronously.
381 * Asynchronous forces are implemented by setting the WANT_SYNC
382 * bit in the appropriate in-core log and then returning.
384 * Synchronous forces are implemented with a signal variable. All callers
385 * to force a given lsn to disk will wait on a the sv attached to the
386 * specific in-core log. When given in-core log finally completes its
387 * write to disk, that thread will wake up all threads waiting on the
397 xlog_t *log = mp->m_log;
401 log_flushed = &dummy;
403 ASSERT(flags & XFS_LOG_FORCE);
405 XFS_STATS_INC(xs_log_force);
407 if (log->l_flags & XLOG_IO_ERROR)
408 return XFS_ERROR(EIO);
410 return xlog_state_sync_all(log, flags, log_flushed);
412 return xlog_state_sync(log, lsn, flags, log_flushed);
413 } /* _xfs_log_force */
416 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
417 * about errors or whether the log was flushed or not. This is the normal
418 * interface to use when trying to unpin items or move the log forward.
427 error = _xfs_log_force(mp, lsn, flags, NULL);
429 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
430 "error %d returned.", error);
436 * Attaches a new iclog I/O completion callback routine during
437 * transaction commit. If the log is in error state, a non-zero
438 * return code is handed back and the caller is responsible for
439 * executing the callback at an appropriate time.
442 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
443 void *iclog_hndl, /* iclog to hang callback off */
444 xfs_log_callback_t *cb)
446 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
449 spin_lock(&iclog->ic_callback_lock);
450 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
452 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
453 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
455 *(iclog->ic_callback_tail) = cb;
456 iclog->ic_callback_tail = &(cb->cb_next);
458 spin_unlock(&iclog->ic_callback_lock);
460 } /* xfs_log_notify */
463 xfs_log_release_iclog(xfs_mount_t *mp,
466 xlog_t *log = mp->m_log;
467 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
469 if (xlog_state_release_iclog(log, iclog)) {
470 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
478 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
479 * to the reservation.
480 * 2. Potentially, push buffers at tail of log to disk.
482 * Each reservation is going to reserve extra space for a log record header.
483 * When writes happen to the on-disk log, we don't subtract the length of the
484 * log record header from any reservation. By wasting space in each
485 * reservation, we prevent over allocation problems.
488 xfs_log_reserve(xfs_mount_t *mp,
491 xfs_log_ticket_t *ticket,
496 xlog_t *log = mp->m_log;
497 xlog_ticket_t *internal_ticket;
500 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
501 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
503 if (XLOG_FORCED_SHUTDOWN(log))
504 return XFS_ERROR(EIO);
506 XFS_STATS_INC(xs_try_logspace);
508 if (*ticket != NULL) {
509 ASSERT(flags & XFS_LOG_PERM_RESERV);
510 internal_ticket = (xlog_ticket_t *)*ticket;
511 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
512 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
513 retval = xlog_regrant_write_log_space(log, internal_ticket);
515 /* may sleep if need to allocate more tickets */
516 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
518 if (!internal_ticket)
519 return XFS_ERROR(ENOMEM);
520 internal_ticket->t_trans_type = t_type;
521 *ticket = internal_ticket;
522 xlog_trace_loggrant(log, internal_ticket,
523 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
524 "xfs_log_reserve: create new ticket (permanent trans)" :
525 "xfs_log_reserve: create new ticket");
526 xlog_grant_push_ail(mp,
527 (internal_ticket->t_unit_res *
528 internal_ticket->t_cnt));
529 retval = xlog_grant_log_space(log, internal_ticket);
533 } /* xfs_log_reserve */
537 * Mount a log filesystem
539 * mp - ubiquitous xfs mount point structure
540 * log_target - buftarg of on-disk log device
541 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
542 * num_bblocks - Number of BBSIZE blocks in on-disk log
544 * Return error or zero.
549 xfs_buftarg_t *log_target,
550 xfs_daddr_t blk_offset,
555 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
556 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
559 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
561 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
564 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
566 cmn_err(CE_WARN, "XFS: Log allocation failed: No memory!");
572 * Initialize the AIL now we have a log.
574 error = xfs_trans_ail_init(mp);
576 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
579 mp->m_log->l_ailp = mp->m_ail;
582 * skip log recovery on a norecovery mount. pretend it all
585 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
586 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
589 mp->m_flags &= ~XFS_MOUNT_RDONLY;
591 error = xlog_recover(mp->m_log);
594 mp->m_flags |= XFS_MOUNT_RDONLY;
596 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
601 /* Normal transactions can now occur */
602 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
604 /* End mounting message in xfs_log_mount_finish */
607 xfs_log_unmount_dealloc(mp);
610 } /* xfs_log_mount */
613 * Finish the recovery of the file system. This is separate from
614 * the xfs_log_mount() call, because it depends on the code in
615 * xfs_mountfs() to read in the root and real-time bitmap inodes
616 * between calling xfs_log_mount() and here.
618 * mp - ubiquitous xfs mount point structure
621 xfs_log_mount_finish(xfs_mount_t *mp)
625 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
626 error = xlog_recover_finish(mp->m_log);
629 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
636 * Unmount processing for the log.
639 xfs_log_unmount(xfs_mount_t *mp)
643 error = xfs_log_unmount_write(mp);
644 xfs_log_unmount_dealloc(mp);
649 * Final log writes as part of unmount.
651 * Mark the filesystem clean as unmount happens. Note that during relocation
652 * this routine needs to be executed as part of source-bag while the
653 * deallocation must not be done until source-end.
657 * Unmount record used to have a string "Unmount filesystem--" in the
658 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
659 * We just write the magic number now since that particular field isn't
660 * currently architecture converted and "nUmount" is a bit foo.
661 * As far as I know, there weren't any dependencies on the old behaviour.
665 xfs_log_unmount_write(xfs_mount_t *mp)
667 xlog_t *log = mp->m_log;
668 xlog_in_core_t *iclog;
670 xlog_in_core_t *first_iclog;
672 xfs_log_iovec_t reg[1];
673 xfs_log_ticket_t tic = NULL;
677 /* the data section must be 32 bit size aligned */
681 __uint32_t pad2; /* may as well make it 64 bits */
682 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
685 * Don't write out unmount record on read-only mounts.
686 * Or, if we are doing a forced umount (typically because of IO errors).
688 if (mp->m_flags & XFS_MOUNT_RDONLY)
691 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
692 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
695 first_iclog = iclog = log->l_iclog;
697 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
698 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
699 ASSERT(iclog->ic_offset == 0);
701 iclog = iclog->ic_next;
702 } while (iclog != first_iclog);
704 if (! (XLOG_FORCED_SHUTDOWN(log))) {
705 reg[0].i_addr = (void*)&magic;
706 reg[0].i_len = sizeof(magic);
707 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
709 error = xfs_log_reserve(mp, 600, 1, &tic,
710 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
712 /* remove inited flag */
713 ((xlog_ticket_t *)tic)->t_flags = 0;
714 error = xlog_write(mp, reg, 1, tic, &lsn,
715 NULL, XLOG_UNMOUNT_TRANS);
717 * At this point, we're umounting anyway,
718 * so there's no point in transitioning log state
719 * to IOERROR. Just continue...
724 xfs_fs_cmn_err(CE_ALERT, mp,
725 "xfs_log_unmount: unmount record failed");
729 spin_lock(&log->l_icloglock);
730 iclog = log->l_iclog;
731 atomic_inc(&iclog->ic_refcnt);
732 xlog_state_want_sync(log, iclog);
733 spin_unlock(&log->l_icloglock);
734 error = xlog_state_release_iclog(log, iclog);
736 spin_lock(&log->l_icloglock);
737 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
738 iclog->ic_state == XLOG_STATE_DIRTY)) {
739 if (!XLOG_FORCED_SHUTDOWN(log)) {
740 sv_wait(&iclog->ic_force_wait, PMEM,
741 &log->l_icloglock, s);
743 spin_unlock(&log->l_icloglock);
746 spin_unlock(&log->l_icloglock);
749 xlog_trace_loggrant(log, tic, "unmount rec");
750 xlog_ungrant_log_space(log, tic);
751 xfs_log_ticket_put(tic);
755 * We're already in forced_shutdown mode, couldn't
756 * even attempt to write out the unmount transaction.
758 * Go through the motions of sync'ing and releasing
759 * the iclog, even though no I/O will actually happen,
760 * we need to wait for other log I/Os that may already
761 * be in progress. Do this as a separate section of
762 * code so we'll know if we ever get stuck here that
763 * we're in this odd situation of trying to unmount
764 * a file system that went into forced_shutdown as
765 * the result of an unmount..
767 spin_lock(&log->l_icloglock);
768 iclog = log->l_iclog;
769 atomic_inc(&iclog->ic_refcnt);
771 xlog_state_want_sync(log, iclog);
772 spin_unlock(&log->l_icloglock);
773 error = xlog_state_release_iclog(log, iclog);
775 spin_lock(&log->l_icloglock);
777 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
778 || iclog->ic_state == XLOG_STATE_DIRTY
779 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
781 sv_wait(&iclog->ic_force_wait, PMEM,
782 &log->l_icloglock, s);
784 spin_unlock(&log->l_icloglock);
789 } /* xfs_log_unmount_write */
792 * Deallocate log structures for unmount/relocation.
794 * We need to stop the aild from running before we destroy
795 * and deallocate the log as the aild references the log.
798 xfs_log_unmount_dealloc(xfs_mount_t *mp)
800 xfs_trans_ail_destroy(mp);
801 xlog_dealloc_log(mp->m_log);
805 * Write region vectors to log. The write happens using the space reservation
806 * of the ticket (tic). It is not a requirement that all writes for a given
807 * transaction occur with one call to xfs_log_write().
810 xfs_log_write(xfs_mount_t * mp,
811 xfs_log_iovec_t reg[],
813 xfs_log_ticket_t tic,
814 xfs_lsn_t *start_lsn)
817 xlog_t *log = mp->m_log;
819 if (XLOG_FORCED_SHUTDOWN(log))
820 return XFS_ERROR(EIO);
822 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
823 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
826 } /* xfs_log_write */
830 xfs_log_move_tail(xfs_mount_t *mp,
834 xlog_t *log = mp->m_log;
835 int need_bytes, free_bytes, cycle, bytes;
837 if (XLOG_FORCED_SHUTDOWN(log))
841 /* needed since sync_lsn is 64 bits */
842 spin_lock(&log->l_icloglock);
843 tail_lsn = log->l_last_sync_lsn;
844 spin_unlock(&log->l_icloglock);
847 spin_lock(&log->l_grant_lock);
849 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
853 log->l_tail_lsn = tail_lsn;
856 if ((tic = log->l_write_headq)) {
858 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
859 panic("Recovery problem");
861 cycle = log->l_grant_write_cycle;
862 bytes = log->l_grant_write_bytes;
863 free_bytes = xlog_space_left(log, cycle, bytes);
865 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
867 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
870 free_bytes -= tic->t_unit_res;
871 sv_signal(&tic->t_wait);
873 } while (tic != log->l_write_headq);
875 if ((tic = log->l_reserve_headq)) {
877 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
878 panic("Recovery problem");
880 cycle = log->l_grant_reserve_cycle;
881 bytes = log->l_grant_reserve_bytes;
882 free_bytes = xlog_space_left(log, cycle, bytes);
884 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
885 need_bytes = tic->t_unit_res*tic->t_cnt;
887 need_bytes = tic->t_unit_res;
888 if (free_bytes < need_bytes && tail_lsn != 1)
891 free_bytes -= need_bytes;
892 sv_signal(&tic->t_wait);
894 } while (tic != log->l_reserve_headq);
896 spin_unlock(&log->l_grant_lock);
897 } /* xfs_log_move_tail */
900 * Determine if we have a transaction that has gone to disk
901 * that needs to be covered. Log activity needs to be idle (no AIL and
902 * nothing in the iclogs). And, we need to be in the right state indicating
903 * something has gone out.
906 xfs_log_need_covered(xfs_mount_t *mp)
909 xlog_t *log = mp->m_log;
911 if (!xfs_fs_writable(mp))
914 spin_lock(&log->l_icloglock);
915 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
916 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
917 && !xfs_trans_ail_tail(log->l_ailp)
918 && xlog_iclogs_empty(log)) {
919 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
920 log->l_covered_state = XLOG_STATE_COVER_DONE;
922 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
923 log->l_covered_state = XLOG_STATE_COVER_DONE2;
927 spin_unlock(&log->l_icloglock);
931 /******************************************************************************
935 ******************************************************************************
938 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
939 * The log manager must keep track of the last LR which was committed
940 * to disk. The lsn of this LR will become the new tail_lsn whenever
941 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
942 * the situation where stuff could be written into the log but nothing
943 * was ever in the AIL when asked. Eventually, we panic since the
944 * tail hits the head.
946 * We may be holding the log iclog lock upon entering this routine.
949 xlog_assign_tail_lsn(xfs_mount_t *mp)
952 xlog_t *log = mp->m_log;
954 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
955 spin_lock(&log->l_grant_lock);
957 log->l_tail_lsn = tail_lsn;
959 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
961 spin_unlock(&log->l_grant_lock);
964 } /* xlog_assign_tail_lsn */
968 * Return the space in the log between the tail and the head. The head
969 * is passed in the cycle/bytes formal parms. In the special case where
970 * the reserve head has wrapped passed the tail, this calculation is no
971 * longer valid. In this case, just return 0 which means there is no space
972 * in the log. This works for all places where this function is called
973 * with the reserve head. Of course, if the write head were to ever
974 * wrap the tail, we should blow up. Rather than catch this case here,
975 * we depend on other ASSERTions in other parts of the code. XXXmiken
977 * This code also handles the case where the reservation head is behind
978 * the tail. The details of this case are described below, but the end
979 * result is that we return the size of the log as the amount of space left.
982 xlog_space_left(xlog_t *log, int cycle, int bytes)
988 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
989 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
990 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
991 free_bytes = log->l_logsize - (bytes - tail_bytes);
992 } else if ((tail_cycle + 1) < cycle) {
994 } else if (tail_cycle < cycle) {
995 ASSERT(tail_cycle == (cycle - 1));
996 free_bytes = tail_bytes - bytes;
999 * The reservation head is behind the tail.
1000 * In this case we just want to return the size of the
1001 * log as the amount of space left.
1003 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
1004 "xlog_space_left: head behind tail\n"
1005 " tail_cycle = %d, tail_bytes = %d\n"
1006 " GH cycle = %d, GH bytes = %d",
1007 tail_cycle, tail_bytes, cycle, bytes);
1009 free_bytes = log->l_logsize;
1012 } /* xlog_space_left */
1016 * Log function which is called when an io completes.
1018 * The log manager needs its own routine, in order to control what
1019 * happens with the buffer after the write completes.
1022 xlog_iodone(xfs_buf_t *bp)
1024 xlog_in_core_t *iclog;
1028 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1029 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1030 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1035 * If the _XFS_BARRIER_FAILED flag was set by a lower
1036 * layer, it means the underlying device no longer supports
1037 * barrier I/O. Warn loudly and turn off barriers.
1039 if (bp->b_flags & _XFS_BARRIER_FAILED) {
1040 bp->b_flags &= ~_XFS_BARRIER_FAILED;
1041 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1042 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1043 "xlog_iodone: Barriers are no longer supported"
1044 " by device. Disabling barriers\n");
1045 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1049 * Race to shutdown the filesystem if we see an error.
1051 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1052 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1053 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1055 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
1057 * This flag will be propagated to the trans-committed
1058 * callback routines to let them know that the log-commit
1061 aborted = XFS_LI_ABORTED;
1062 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1063 aborted = XFS_LI_ABORTED;
1066 /* log I/O is always issued ASYNC */
1067 ASSERT(XFS_BUF_ISASYNC(bp));
1068 xlog_state_done_syncing(iclog, aborted);
1070 * do not reference the buffer (bp) here as we could race
1071 * with it being freed after writing the unmount record to the
1078 * The bdstrat callback function for log bufs. This gives us a central
1079 * place to trap bufs in case we get hit by a log I/O error and need to
1080 * shutdown. Actually, in practice, even when we didn't get a log error,
1081 * we transition the iclogs to IOERROR state *after* flushing all existing
1082 * iclogs to disk. This is because we don't want anymore new transactions to be
1083 * started or completed afterwards.
1086 xlog_bdstrat_cb(struct xfs_buf *bp)
1088 xlog_in_core_t *iclog;
1090 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1092 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1093 /* note for irix bstrat will need struct bdevsw passed
1094 * Fix the following macro if the code ever is merged
1100 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1101 XFS_BUF_ERROR(bp, EIO);
1104 return XFS_ERROR(EIO);
1110 * Return size of each in-core log record buffer.
1112 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
1114 * If the filesystem blocksize is too large, we may need to choose a
1115 * larger size since the directory code currently logs entire blocks.
1119 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1125 if (mp->m_logbufs <= 0)
1126 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1128 log->l_iclog_bufs = mp->m_logbufs;
1131 * Buffer size passed in from mount system call.
1133 if (mp->m_logbsize > 0) {
1134 size = log->l_iclog_size = mp->m_logbsize;
1135 log->l_iclog_size_log = 0;
1137 log->l_iclog_size_log++;
1141 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1142 /* # headers = size / 32K
1143 * one header holds cycles from 32K of data
1146 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1147 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1149 log->l_iclog_hsize = xhdrs << BBSHIFT;
1150 log->l_iclog_heads = xhdrs;
1152 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1153 log->l_iclog_hsize = BBSIZE;
1154 log->l_iclog_heads = 1;
1159 /* All machines use 32KB buffers by default. */
1160 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1161 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1163 /* the default log size is 16k or 32k which is one header sector */
1164 log->l_iclog_hsize = BBSIZE;
1165 log->l_iclog_heads = 1;
1168 /* are we being asked to make the sizes selected above visible? */
1169 if (mp->m_logbufs == 0)
1170 mp->m_logbufs = log->l_iclog_bufs;
1171 if (mp->m_logbsize == 0)
1172 mp->m_logbsize = log->l_iclog_size;
1173 } /* xlog_get_iclog_buffer_size */
1177 * This routine initializes some of the log structure for a given mount point.
1178 * Its primary purpose is to fill in enough, so recovery can occur. However,
1179 * some other stuff may be filled in too.
1182 xlog_alloc_log(xfs_mount_t *mp,
1183 xfs_buftarg_t *log_target,
1184 xfs_daddr_t blk_offset,
1188 xlog_rec_header_t *head;
1189 xlog_in_core_t **iclogp;
1190 xlog_in_core_t *iclog, *prev_iclog=NULL;
1195 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1200 log->l_targ = log_target;
1201 log->l_logsize = BBTOB(num_bblks);
1202 log->l_logBBstart = blk_offset;
1203 log->l_logBBsize = num_bblks;
1204 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1205 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1207 log->l_prev_block = -1;
1208 log->l_tail_lsn = xlog_assign_lsn(1, 0);
1209 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1210 log->l_last_sync_lsn = log->l_tail_lsn;
1211 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1212 log->l_grant_reserve_cycle = 1;
1213 log->l_grant_write_cycle = 1;
1215 if (xfs_sb_version_hassector(&mp->m_sb)) {
1216 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1217 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1218 /* for larger sector sizes, must have v2 or external log */
1219 ASSERT(log->l_sectbb_log == 0 ||
1220 log->l_logBBstart == 0 ||
1221 xfs_sb_version_haslogv2(&mp->m_sb));
1222 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1224 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1226 xlog_get_iclog_buffer_size(mp, log);
1228 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1231 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1232 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1233 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1234 ASSERT(XFS_BUF_ISBUSY(bp));
1235 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1238 spin_lock_init(&log->l_icloglock);
1239 spin_lock_init(&log->l_grant_lock);
1240 sv_init(&log->l_flush_wait, 0, "flush_wait");
1242 xlog_trace_loggrant_alloc(log);
1243 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1244 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1246 iclogp = &log->l_iclog;
1248 * The amount of memory to allocate for the iclog structure is
1249 * rather funky due to the way the structure is defined. It is
1250 * done this way so that we can use different sizes for machines
1251 * with different amounts of memory. See the definition of
1252 * xlog_in_core_t in xfs_log_priv.h for details.
1254 iclogsize = log->l_iclog_size;
1255 ASSERT(log->l_iclog_size >= 4096);
1256 for (i=0; i < log->l_iclog_bufs; i++) {
1257 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1259 goto out_free_iclog;
1262 iclog->ic_prev = prev_iclog;
1265 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1267 goto out_free_iclog;
1268 if (!XFS_BUF_CPSEMA(bp))
1270 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1271 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1272 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1274 iclog->ic_data = bp->b_addr;
1276 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1278 head = &iclog->ic_header;
1279 memset(head, 0, sizeof(xlog_rec_header_t));
1280 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1281 head->h_version = cpu_to_be32(
1282 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1283 head->h_size = cpu_to_be32(log->l_iclog_size);
1285 head->h_fmt = cpu_to_be32(XLOG_FMT);
1286 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1288 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1289 iclog->ic_state = XLOG_STATE_ACTIVE;
1290 iclog->ic_log = log;
1291 atomic_set(&iclog->ic_refcnt, 0);
1292 spin_lock_init(&iclog->ic_callback_lock);
1293 iclog->ic_callback_tail = &(iclog->ic_callback);
1294 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
1296 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1297 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1298 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1299 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
1301 xlog_trace_iclog_alloc(iclog);
1303 iclogp = &iclog->ic_next;
1305 *iclogp = log->l_iclog; /* complete ring */
1306 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1311 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1312 prev_iclog = iclog->ic_next;
1314 sv_destroy(&iclog->ic_force_wait);
1315 sv_destroy(&iclog->ic_write_wait);
1316 xfs_buf_free(iclog->ic_bp);
1317 xlog_trace_iclog_dealloc(iclog);
1321 spinlock_destroy(&log->l_icloglock);
1322 spinlock_destroy(&log->l_grant_lock);
1323 xlog_trace_loggrant_dealloc(log);
1324 xfs_buf_free(log->l_xbuf);
1328 } /* xlog_alloc_log */
1332 * Write out the commit record of a transaction associated with the given
1333 * ticket. Return the lsn of the commit record.
1336 xlog_commit_record(xfs_mount_t *mp,
1337 xlog_ticket_t *ticket,
1338 xlog_in_core_t **iclog,
1339 xfs_lsn_t *commitlsnp)
1342 xfs_log_iovec_t reg[1];
1344 reg[0].i_addr = NULL;
1346 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_COMMIT);
1348 ASSERT_ALWAYS(iclog);
1349 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1350 iclog, XLOG_COMMIT_TRANS))) {
1351 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1354 } /* xlog_commit_record */
1358 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1359 * log space. This code pushes on the lsn which would supposedly free up
1360 * the 25% which we want to leave free. We may need to adopt a policy which
1361 * pushes on an lsn which is further along in the log once we reach the high
1362 * water mark. In this manner, we would be creating a low water mark.
1365 xlog_grant_push_ail(xfs_mount_t *mp,
1368 xlog_t *log = mp->m_log; /* pointer to the log */
1369 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1370 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1371 int free_blocks; /* free blocks left to write to */
1372 int free_bytes; /* free bytes left to write to */
1373 int threshold_block; /* block in lsn we'd like to be at */
1374 int threshold_cycle; /* lsn cycle we'd like to be at */
1377 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1379 spin_lock(&log->l_grant_lock);
1380 free_bytes = xlog_space_left(log,
1381 log->l_grant_reserve_cycle,
1382 log->l_grant_reserve_bytes);
1383 tail_lsn = log->l_tail_lsn;
1384 free_blocks = BTOBBT(free_bytes);
1387 * Set the threshold for the minimum number of free blocks in the
1388 * log to the maximum of what the caller needs, one quarter of the
1389 * log, and 256 blocks.
1391 free_threshold = BTOBB(need_bytes);
1392 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1393 free_threshold = MAX(free_threshold, 256);
1394 if (free_blocks < free_threshold) {
1395 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1396 threshold_cycle = CYCLE_LSN(tail_lsn);
1397 if (threshold_block >= log->l_logBBsize) {
1398 threshold_block -= log->l_logBBsize;
1399 threshold_cycle += 1;
1401 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
1403 /* Don't pass in an lsn greater than the lsn of the last
1404 * log record known to be on disk.
1406 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1407 threshold_lsn = log->l_last_sync_lsn;
1409 spin_unlock(&log->l_grant_lock);
1412 * Get the transaction layer to kick the dirty buffers out to
1413 * disk asynchronously. No point in trying to do this if
1414 * the filesystem is shutting down.
1416 if (threshold_lsn &&
1417 !XLOG_FORCED_SHUTDOWN(log))
1418 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
1419 } /* xlog_grant_push_ail */
1423 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1424 * fashion. Previously, we should have moved the current iclog
1425 * ptr in the log to point to the next available iclog. This allows further
1426 * write to continue while this code syncs out an iclog ready to go.
1427 * Before an in-core log can be written out, the data section must be scanned
1428 * to save away the 1st word of each BBSIZE block into the header. We replace
1429 * it with the current cycle count. Each BBSIZE block is tagged with the
1430 * cycle count because there in an implicit assumption that drives will
1431 * guarantee that entire 512 byte blocks get written at once. In other words,
1432 * we can't have part of a 512 byte block written and part not written. By
1433 * tagging each block, we will know which blocks are valid when recovering
1434 * after an unclean shutdown.
1436 * This routine is single threaded on the iclog. No other thread can be in
1437 * this routine with the same iclog. Changing contents of iclog can there-
1438 * fore be done without grabbing the state machine lock. Updating the global
1439 * log will require grabbing the lock though.
1441 * The entire log manager uses a logical block numbering scheme. Only
1442 * log_sync (and then only bwrite()) know about the fact that the log may
1443 * not start with block zero on a given device. The log block start offset
1444 * is added immediately before calling bwrite().
1448 xlog_sync(xlog_t *log,
1449 xlog_in_core_t *iclog)
1451 xfs_caddr_t dptr; /* pointer to byte sized element */
1454 uint count; /* byte count of bwrite */
1455 uint count_init; /* initial count before roundup */
1456 int roundoff; /* roundoff to BB or stripe */
1457 int split = 0; /* split write into two regions */
1459 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
1461 XFS_STATS_INC(xs_log_writes);
1462 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1464 /* Add for LR header */
1465 count_init = log->l_iclog_hsize + iclog->ic_offset;
1467 /* Round out the log write size */
1468 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1469 /* we have a v2 stripe unit to use */
1470 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1472 count = BBTOB(BTOBB(count_init));
1474 roundoff = count - count_init;
1475 ASSERT(roundoff >= 0);
1476 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1477 roundoff < log->l_mp->m_sb.sb_logsunit)
1479 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1480 roundoff < BBTOB(1)));
1482 /* move grant heads by roundoff in sync */
1483 spin_lock(&log->l_grant_lock);
1484 xlog_grant_add_space(log, roundoff);
1485 spin_unlock(&log->l_grant_lock);
1487 /* put cycle number in every block */
1488 xlog_pack_data(log, iclog, roundoff);
1490 /* real byte length */
1492 iclog->ic_header.h_len =
1493 cpu_to_be32(iclog->ic_offset + roundoff);
1495 iclog->ic_header.h_len =
1496 cpu_to_be32(iclog->ic_offset);
1500 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1501 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1502 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
1504 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1506 /* Do we need to split this write into 2 parts? */
1507 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1508 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1509 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1510 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1512 iclog->ic_bwritecnt = 1;
1514 XFS_BUF_SET_COUNT(bp, count);
1515 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1516 XFS_BUF_ZEROFLAGS(bp);
1520 * Do an ordered write for the log block.
1521 * Its unnecessary to flush the first split block in the log wrap case.
1523 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
1524 XFS_BUF_ORDERED(bp);
1526 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1527 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1529 xlog_verify_iclog(log, iclog, count, B_TRUE);
1531 /* account for log which doesn't start at block #0 */
1532 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1534 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1539 if ((error = XFS_bwrite(bp))) {
1540 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1545 bp = iclog->ic_log->l_xbuf;
1546 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1548 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1549 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1550 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1551 (__psint_t)count), split);
1552 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1553 XFS_BUF_ZEROFLAGS(bp);
1556 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1557 XFS_BUF_ORDERED(bp);
1558 dptr = XFS_BUF_PTR(bp);
1560 * Bump the cycle numbers at the start of each block
1561 * since this part of the buffer is at the start of
1562 * a new cycle. Watch out for the header magic number
1565 for (i = 0; i < split; i += BBSIZE) {
1566 be32_add_cpu((__be32 *)dptr, 1);
1567 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
1568 be32_add_cpu((__be32 *)dptr, 1);
1572 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1573 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1575 /* account for internal log which doesn't start at block #0 */
1576 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1578 if ((error = XFS_bwrite(bp))) {
1579 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1580 bp, XFS_BUF_ADDR(bp));
1589 * Deallocate a log structure
1592 xlog_dealloc_log(xlog_t *log)
1594 xlog_in_core_t *iclog, *next_iclog;
1597 iclog = log->l_iclog;
1598 for (i=0; i<log->l_iclog_bufs; i++) {
1599 sv_destroy(&iclog->ic_force_wait);
1600 sv_destroy(&iclog->ic_write_wait);
1601 xfs_buf_free(iclog->ic_bp);
1602 xlog_trace_iclog_dealloc(iclog);
1603 next_iclog = iclog->ic_next;
1607 spinlock_destroy(&log->l_icloglock);
1608 spinlock_destroy(&log->l_grant_lock);
1610 xfs_buf_free(log->l_xbuf);
1611 xlog_trace_loggrant_dealloc(log);
1612 log->l_mp->m_log = NULL;
1614 } /* xlog_dealloc_log */
1617 * Update counters atomically now that memcpy is done.
1621 xlog_state_finish_copy(xlog_t *log,
1622 xlog_in_core_t *iclog,
1626 spin_lock(&log->l_icloglock);
1628 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1629 iclog->ic_offset += copy_bytes;
1631 spin_unlock(&log->l_icloglock);
1632 } /* xlog_state_finish_copy */
1638 * print out info relating to regions written which consume
1642 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1645 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1647 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1648 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1669 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1712 xfs_fs_cmn_err(CE_WARN, mp,
1713 "xfs_log_write: reservation summary:\n"
1714 " trans type = %s (%u)\n"
1715 " unit res = %d bytes\n"
1716 " current res = %d bytes\n"
1717 " total reg = %u bytes (o/flow = %u bytes)\n"
1718 " ophdrs = %u (ophdr space = %u bytes)\n"
1719 " ophdr + reg = %u bytes\n"
1720 " num regions = %u\n",
1721 ((ticket->t_trans_type <= 0 ||
1722 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1723 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1724 ticket->t_trans_type,
1727 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1728 ticket->t_res_num_ophdrs, ophdr_spc,
1729 ticket->t_res_arr_sum +
1730 ticket->t_res_o_flow + ophdr_spc,
1733 for (i = 0; i < ticket->t_res_num; i++) {
1734 uint r_type = ticket->t_res_arr[i].r_type;
1736 "region[%u]: %s - %u bytes\n",
1738 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1739 "bad-rtype" : res_type_str[r_type-1]),
1740 ticket->t_res_arr[i].r_len);
1745 * Write some region out to in-core log
1747 * This will be called when writing externally provided regions or when
1748 * writing out a commit record for a given transaction.
1750 * General algorithm:
1751 * 1. Find total length of this write. This may include adding to the
1752 * lengths passed in.
1753 * 2. Check whether we violate the tickets reservation.
1754 * 3. While writing to this iclog
1755 * A. Reserve as much space in this iclog as can get
1756 * B. If this is first write, save away start lsn
1757 * C. While writing this region:
1758 * 1. If first write of transaction, write start record
1759 * 2. Write log operation header (header per region)
1760 * 3. Find out if we can fit entire region into this iclog
1761 * 4. Potentially, verify destination memcpy ptr
1762 * 5. Memcpy (partial) region
1763 * 6. If partial copy, release iclog; otherwise, continue
1764 * copying more regions into current iclog
1765 * 4. Mark want sync bit (in simulation mode)
1766 * 5. Release iclog for potential flush to on-disk log.
1769 * 1. Panic if reservation is overrun. This should never happen since
1770 * reservation amounts are generated internal to the filesystem.
1772 * 1. Tickets are single threaded data structures.
1773 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1774 * syncing routine. When a single log_write region needs to span
1775 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1776 * on all log operation writes which don't contain the end of the
1777 * region. The XLOG_END_TRANS bit is used for the in-core log
1778 * operation which contains the end of the continued log_write region.
1779 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1780 * we don't really know exactly how much space will be used. As a result,
1781 * we don't update ic_offset until the end when we know exactly how many
1782 * bytes have been written out.
1785 xlog_write(xfs_mount_t * mp,
1786 xfs_log_iovec_t reg[],
1788 xfs_log_ticket_t tic,
1789 xfs_lsn_t *start_lsn,
1790 xlog_in_core_t **commit_iclog,
1793 xlog_t *log = mp->m_log;
1794 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1795 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
1796 xlog_op_header_t *logop_head; /* ptr to log operation header */
1797 __psint_t ptr; /* copy address into data region */
1798 int len; /* # xlog_write() bytes 2 still copy */
1799 int index; /* region index currently copying */
1800 int log_offset; /* offset (from 0) into data region */
1801 int start_rec_copy; /* # bytes to copy for start record */
1802 int partial_copy; /* did we split a region? */
1803 int partial_copy_len;/* # bytes copied if split region */
1804 int need_copy; /* # bytes need to memcpy this region */
1805 int copy_len; /* # bytes actually memcpy'ing */
1806 int copy_off; /* # bytes from entry start */
1807 int contwr; /* continued write of in-core log? */
1809 int record_cnt = 0, data_cnt = 0;
1811 partial_copy_len = partial_copy = 0;
1813 /* Calculate potential maximum space. Each region gets its own
1814 * xlog_op_header_t and may need to be double word aligned.
1817 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1818 len += sizeof(xlog_op_header_t);
1819 ticket->t_res_num_ophdrs++;
1822 for (index = 0; index < nentries; index++) {
1823 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1824 ticket->t_res_num_ophdrs++;
1825 len += reg[index].i_len;
1826 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
1828 contwr = *start_lsn = 0;
1830 if (ticket->t_curr_res < len) {
1831 xlog_print_tic_res(mp, ticket);
1834 "xfs_log_write: reservation ran out. Need to up reservation");
1836 /* Customer configurable panic */
1837 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1838 "xfs_log_write: reservation ran out. Need to up reservation");
1839 /* If we did not panic, shutdown the filesystem */
1840 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1843 ticket->t_curr_res -= len;
1845 for (index = 0; index < nentries; ) {
1846 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1847 &contwr, &log_offset)))
1850 ASSERT(log_offset <= iclog->ic_size - 1);
1851 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1853 /* start_lsn is the first lsn written to. That's all we need. */
1855 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
1857 /* This loop writes out as many regions as can fit in the amount
1858 * of space which was allocated by xlog_state_get_iclog_space().
1860 while (index < nentries) {
1861 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1862 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1865 /* If first write for transaction, insert start record.
1866 * We can't be trying to commit if we are inited. We can't
1867 * have any "partial_copy" if we are inited.
1869 if (ticket->t_flags & XLOG_TIC_INITED) {
1870 logop_head = (xlog_op_header_t *)ptr;
1871 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1872 logop_head->oh_clientid = ticket->t_clientid;
1873 logop_head->oh_len = 0;
1874 logop_head->oh_flags = XLOG_START_TRANS;
1875 logop_head->oh_res2 = 0;
1876 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1879 start_rec_copy = sizeof(xlog_op_header_t);
1880 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1883 /* Copy log operation header directly into data section */
1884 logop_head = (xlog_op_header_t *)ptr;
1885 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1886 logop_head->oh_clientid = ticket->t_clientid;
1887 logop_head->oh_res2 = 0;
1889 /* header copied directly */
1890 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1892 /* are we copying a commit or unmount record? */
1893 logop_head->oh_flags = flags;
1896 * We've seen logs corrupted with bad transaction client
1897 * ids. This makes sure that XFS doesn't generate them on.
1898 * Turn this into an EIO and shut down the filesystem.
1900 switch (logop_head->oh_clientid) {
1901 case XFS_TRANSACTION:
1906 xfs_fs_cmn_err(CE_WARN, mp,
1907 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1908 logop_head->oh_clientid, tic);
1909 return XFS_ERROR(EIO);
1912 /* Partial write last time? => (partial_copy != 0)
1913 * need_copy is the amount we'd like to copy if everything could
1914 * fit in the current memcpy.
1916 need_copy = reg[index].i_len - partial_copy_len;
1918 copy_off = partial_copy_len;
1919 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1920 copy_len = need_copy;
1921 logop_head->oh_len = cpu_to_be32(copy_len);
1923 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1924 partial_copy_len = partial_copy = 0;
1925 } else { /* partial write */
1926 copy_len = iclog->ic_size - log_offset;
1927 logop_head->oh_len = cpu_to_be32(copy_len);
1928 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1930 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1931 partial_copy_len += copy_len;
1933 len += sizeof(xlog_op_header_t); /* from splitting of region */
1934 /* account for new log op header */
1935 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1936 ticket->t_res_num_ophdrs++;
1938 xlog_verify_dest_ptr(log, ptr);
1941 ASSERT(copy_len >= 0);
1942 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1943 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1945 /* make copy_len total bytes copied, including headers */
1946 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1948 data_cnt += contwr ? copy_len : 0;
1949 if (partial_copy) { /* copied partial region */
1950 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1951 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1952 record_cnt = data_cnt = 0;
1953 if ((error = xlog_state_release_iclog(log, iclog)))
1955 break; /* don't increment index */
1956 } else { /* copied entire region */
1958 partial_copy_len = partial_copy = 0;
1960 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1961 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1962 record_cnt = data_cnt = 0;
1963 spin_lock(&log->l_icloglock);
1964 xlog_state_want_sync(log, iclog);
1965 spin_unlock(&log->l_icloglock);
1967 ASSERT(flags & XLOG_COMMIT_TRANS);
1968 *commit_iclog = iclog;
1969 } else if ((error = xlog_state_release_iclog(log, iclog)))
1971 if (index == nentries)
1972 return 0; /* we are done */
1976 } /* if (partial_copy) */
1977 } /* while (index < nentries) */
1978 } /* for (index = 0; index < nentries; ) */
1981 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1983 ASSERT(flags & XLOG_COMMIT_TRANS);
1984 *commit_iclog = iclog;
1987 return xlog_state_release_iclog(log, iclog);
1991 /*****************************************************************************
1993 * State Machine functions
1995 *****************************************************************************
1998 /* Clean iclogs starting from the head. This ordering must be
1999 * maintained, so an iclog doesn't become ACTIVE beyond one that
2000 * is SYNCING. This is also required to maintain the notion that we use
2001 * a ordered wait queue to hold off would be writers to the log when every
2002 * iclog is trying to sync to disk.
2004 * State Change: DIRTY -> ACTIVE
2007 xlog_state_clean_log(xlog_t *log)
2009 xlog_in_core_t *iclog;
2012 iclog = log->l_iclog;
2014 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2015 iclog->ic_state = XLOG_STATE_ACTIVE;
2016 iclog->ic_offset = 0;
2017 ASSERT(iclog->ic_callback == NULL);
2019 * If the number of ops in this iclog indicate it just
2020 * contains the dummy transaction, we can
2021 * change state into IDLE (the second time around).
2022 * Otherwise we should change the state into
2024 * We don't need to cover the dummy.
2027 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2032 * We have two dirty iclogs so start over
2033 * This could also be num of ops indicates
2034 * this is not the dummy going out.
2038 iclog->ic_header.h_num_logops = 0;
2039 memset(iclog->ic_header.h_cycle_data, 0,
2040 sizeof(iclog->ic_header.h_cycle_data));
2041 iclog->ic_header.h_lsn = 0;
2042 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2045 break; /* stop cleaning */
2046 iclog = iclog->ic_next;
2047 } while (iclog != log->l_iclog);
2049 /* log is locked when we are called */
2051 * Change state for the dummy log recording.
2052 * We usually go to NEED. But we go to NEED2 if the changed indicates
2053 * we are done writing the dummy record.
2054 * If we are done with the second dummy recored (DONE2), then
2058 switch (log->l_covered_state) {
2059 case XLOG_STATE_COVER_IDLE:
2060 case XLOG_STATE_COVER_NEED:
2061 case XLOG_STATE_COVER_NEED2:
2062 log->l_covered_state = XLOG_STATE_COVER_NEED;
2065 case XLOG_STATE_COVER_DONE:
2067 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2069 log->l_covered_state = XLOG_STATE_COVER_NEED;
2072 case XLOG_STATE_COVER_DONE2:
2074 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2076 log->l_covered_state = XLOG_STATE_COVER_NEED;
2083 } /* xlog_state_clean_log */
2086 xlog_get_lowest_lsn(
2089 xlog_in_core_t *lsn_log;
2090 xfs_lsn_t lowest_lsn, lsn;
2092 lsn_log = log->l_iclog;
2095 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2096 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
2097 if ((lsn && !lowest_lsn) ||
2098 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2102 lsn_log = lsn_log->ic_next;
2103 } while (lsn_log != log->l_iclog);
2109 xlog_state_do_callback(
2112 xlog_in_core_t *ciclog)
2114 xlog_in_core_t *iclog;
2115 xlog_in_core_t *first_iclog; /* used to know when we've
2116 * processed all iclogs once */
2117 xfs_log_callback_t *cb, *cb_next;
2119 xfs_lsn_t lowest_lsn;
2120 int ioerrors; /* counter: iclogs with errors */
2121 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2122 int funcdidcallbacks; /* flag: function did callbacks */
2123 int repeats; /* for issuing console warnings if
2124 * looping too many times */
2127 spin_lock(&log->l_icloglock);
2128 first_iclog = iclog = log->l_iclog;
2130 funcdidcallbacks = 0;
2135 * Scan all iclogs starting with the one pointed to by the
2136 * log. Reset this starting point each time the log is
2137 * unlocked (during callbacks).
2139 * Keep looping through iclogs until one full pass is made
2140 * without running any callbacks.
2142 first_iclog = log->l_iclog;
2143 iclog = log->l_iclog;
2144 loopdidcallbacks = 0;
2149 /* skip all iclogs in the ACTIVE & DIRTY states */
2150 if (iclog->ic_state &
2151 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2152 iclog = iclog->ic_next;
2157 * Between marking a filesystem SHUTDOWN and stopping
2158 * the log, we do flush all iclogs to disk (if there
2159 * wasn't a log I/O error). So, we do want things to
2160 * go smoothly in case of just a SHUTDOWN w/o a
2163 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2165 * Can only perform callbacks in order. Since
2166 * this iclog is not in the DONE_SYNC/
2167 * DO_CALLBACK state, we skip the rest and
2168 * just try to clean up. If we set our iclog
2169 * to DO_CALLBACK, we will not process it when
2170 * we retry since a previous iclog is in the
2171 * CALLBACK and the state cannot change since
2172 * we are holding the l_icloglock.
2174 if (!(iclog->ic_state &
2175 (XLOG_STATE_DONE_SYNC |
2176 XLOG_STATE_DO_CALLBACK))) {
2177 if (ciclog && (ciclog->ic_state ==
2178 XLOG_STATE_DONE_SYNC)) {
2179 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2184 * We now have an iclog that is in either the
2185 * DO_CALLBACK or DONE_SYNC states. The other
2186 * states (WANT_SYNC, SYNCING, or CALLBACK were
2187 * caught by the above if and are going to
2188 * clean (i.e. we aren't doing their callbacks)
2193 * We will do one more check here to see if we
2194 * have chased our tail around.
2197 lowest_lsn = xlog_get_lowest_lsn(log);
2199 XFS_LSN_CMP(lowest_lsn,
2200 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
2201 iclog = iclog->ic_next;
2202 continue; /* Leave this iclog for
2206 iclog->ic_state = XLOG_STATE_CALLBACK;
2208 spin_unlock(&log->l_icloglock);
2210 /* l_last_sync_lsn field protected by
2211 * l_grant_lock. Don't worry about iclog's lsn.
2212 * No one else can be here except us.
2214 spin_lock(&log->l_grant_lock);
2215 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2216 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2217 log->l_last_sync_lsn =
2218 be64_to_cpu(iclog->ic_header.h_lsn);
2219 spin_unlock(&log->l_grant_lock);
2222 spin_unlock(&log->l_icloglock);
2227 * Keep processing entries in the callback list until
2228 * we come around and it is empty. We need to
2229 * atomically see that the list is empty and change the
2230 * state to DIRTY so that we don't miss any more
2231 * callbacks being added.
2233 spin_lock(&iclog->ic_callback_lock);
2234 cb = iclog->ic_callback;
2236 iclog->ic_callback_tail = &(iclog->ic_callback);
2237 iclog->ic_callback = NULL;
2238 spin_unlock(&iclog->ic_callback_lock);
2240 /* perform callbacks in the order given */
2241 for (; cb; cb = cb_next) {
2242 cb_next = cb->cb_next;
2243 cb->cb_func(cb->cb_arg, aborted);
2245 spin_lock(&iclog->ic_callback_lock);
2246 cb = iclog->ic_callback;
2252 spin_lock(&log->l_icloglock);
2253 ASSERT(iclog->ic_callback == NULL);
2254 spin_unlock(&iclog->ic_callback_lock);
2255 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2256 iclog->ic_state = XLOG_STATE_DIRTY;
2259 * Transition from DIRTY to ACTIVE if applicable.
2260 * NOP if STATE_IOERROR.
2262 xlog_state_clean_log(log);
2264 /* wake up threads waiting in xfs_log_force() */
2265 sv_broadcast(&iclog->ic_force_wait);
2267 iclog = iclog->ic_next;
2268 } while (first_iclog != iclog);
2270 if (repeats > 5000) {
2271 flushcnt += repeats;
2273 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2274 "%s: possible infinite loop (%d iterations)",
2275 __func__, flushcnt);
2277 } while (!ioerrors && loopdidcallbacks);
2280 * make one last gasp attempt to see if iclogs are being left in
2284 if (funcdidcallbacks) {
2285 first_iclog = iclog = log->l_iclog;
2287 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2289 * Terminate the loop if iclogs are found in states
2290 * which will cause other threads to clean up iclogs.
2292 * SYNCING - i/o completion will go through logs
2293 * DONE_SYNC - interrupt thread should be waiting for
2295 * IOERROR - give up hope all ye who enter here
2297 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2298 iclog->ic_state == XLOG_STATE_SYNCING ||
2299 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2300 iclog->ic_state == XLOG_STATE_IOERROR )
2302 iclog = iclog->ic_next;
2303 } while (first_iclog != iclog);
2307 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2309 spin_unlock(&log->l_icloglock);
2312 sv_broadcast(&log->l_flush_wait);
2317 * Finish transitioning this iclog to the dirty state.
2319 * Make sure that we completely execute this routine only when this is
2320 * the last call to the iclog. There is a good chance that iclog flushes,
2321 * when we reach the end of the physical log, get turned into 2 separate
2322 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2323 * routine. By using the reference count bwritecnt, we guarantee that only
2324 * the second completion goes through.
2326 * Callbacks could take time, so they are done outside the scope of the
2327 * global state machine log lock.
2330 xlog_state_done_syncing(
2331 xlog_in_core_t *iclog,
2334 xlog_t *log = iclog->ic_log;
2336 spin_lock(&log->l_icloglock);
2338 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2339 iclog->ic_state == XLOG_STATE_IOERROR);
2340 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2341 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2345 * If we got an error, either on the first buffer, or in the case of
2346 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2347 * and none should ever be attempted to be written to disk
2350 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2351 if (--iclog->ic_bwritecnt == 1) {
2352 spin_unlock(&log->l_icloglock);
2355 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2359 * Someone could be sleeping prior to writing out the next
2360 * iclog buffer, we wake them all, one will get to do the
2361 * I/O, the others get to wait for the result.
2363 sv_broadcast(&iclog->ic_write_wait);
2364 spin_unlock(&log->l_icloglock);
2365 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2366 } /* xlog_state_done_syncing */
2370 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2371 * sleep. We wait on the flush queue on the head iclog as that should be
2372 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2373 * we will wait here and all new writes will sleep until a sync completes.
2375 * The in-core logs are used in a circular fashion. They are not used
2376 * out-of-order even when an iclog past the head is free.
2379 * * log_offset where xlog_write() can start writing into the in-core
2381 * * in-core log pointer to which xlog_write() should write.
2382 * * boolean indicating this is a continued write to an in-core log.
2383 * If this is the last write, then the in-core log's offset field
2384 * needs to be incremented, depending on the amount of data which
2388 xlog_state_get_iclog_space(xlog_t *log,
2390 xlog_in_core_t **iclogp,
2391 xlog_ticket_t *ticket,
2392 int *continued_write,
2396 xlog_rec_header_t *head;
2397 xlog_in_core_t *iclog;
2401 spin_lock(&log->l_icloglock);
2402 if (XLOG_FORCED_SHUTDOWN(log)) {
2403 spin_unlock(&log->l_icloglock);
2404 return XFS_ERROR(EIO);
2407 iclog = log->l_iclog;
2408 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2409 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2410 XFS_STATS_INC(xs_log_noiclogs);
2412 /* Wait for log writes to have flushed */
2413 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
2417 head = &iclog->ic_header;
2419 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
2420 log_offset = iclog->ic_offset;
2422 /* On the 1st write to an iclog, figure out lsn. This works
2423 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2424 * committing to. If the offset is set, that's how many blocks
2427 if (log_offset == 0) {
2428 ticket->t_curr_res -= log->l_iclog_hsize;
2429 xlog_tic_add_region(ticket,
2431 XLOG_REG_TYPE_LRHEADER);
2432 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2433 head->h_lsn = cpu_to_be64(
2434 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
2435 ASSERT(log->l_curr_block >= 0);
2438 /* If there is enough room to write everything, then do it. Otherwise,
2439 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2440 * bit is on, so this will get flushed out. Don't update ic_offset
2441 * until you know exactly how many bytes get copied. Therefore, wait
2442 * until later to update ic_offset.
2444 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2445 * can fit into remaining data section.
2447 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2448 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2451 * If I'm the only one writing to this iclog, sync it to disk.
2452 * We need to do an atomic compare and decrement here to avoid
2453 * racing with concurrent atomic_dec_and_lock() calls in
2454 * xlog_state_release_iclog() when there is more than one
2455 * reference to the iclog.
2457 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2458 /* we are the only one */
2459 spin_unlock(&log->l_icloglock);
2460 error = xlog_state_release_iclog(log, iclog);
2464 spin_unlock(&log->l_icloglock);
2469 /* Do we have enough room to write the full amount in the remainder
2470 * of this iclog? Or must we continue a write on the next iclog and
2471 * mark this iclog as completely taken? In the case where we switch
2472 * iclogs (to mark it taken), this particular iclog will release/sync
2473 * to disk in xlog_write().
2475 if (len <= iclog->ic_size - iclog->ic_offset) {
2476 *continued_write = 0;
2477 iclog->ic_offset += len;
2479 *continued_write = 1;
2480 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2484 ASSERT(iclog->ic_offset <= iclog->ic_size);
2485 spin_unlock(&log->l_icloglock);
2487 *logoffsetp = log_offset;
2489 } /* xlog_state_get_iclog_space */
2492 * Atomically get the log space required for a log ticket.
2494 * Once a ticket gets put onto the reserveq, it will only return after
2495 * the needed reservation is satisfied.
2498 xlog_grant_log_space(xlog_t *log,
2509 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2510 panic("grant Recovery problem");
2513 /* Is there space or do we need to sleep? */
2514 spin_lock(&log->l_grant_lock);
2515 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2517 /* something is already sleeping; insert new transaction at end */
2518 if (log->l_reserve_headq) {
2519 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2520 xlog_trace_loggrant(log, tic,
2521 "xlog_grant_log_space: sleep 1");
2523 * Gotta check this before going to sleep, while we're
2524 * holding the grant lock.
2526 if (XLOG_FORCED_SHUTDOWN(log))
2529 XFS_STATS_INC(xs_sleep_logspace);
2530 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2532 * If we got an error, and the filesystem is shutting down,
2533 * we'll catch it down below. So just continue...
2535 xlog_trace_loggrant(log, tic,
2536 "xlog_grant_log_space: wake 1");
2537 spin_lock(&log->l_grant_lock);
2539 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2540 need_bytes = tic->t_unit_res*tic->t_ocnt;
2542 need_bytes = tic->t_unit_res;
2545 if (XLOG_FORCED_SHUTDOWN(log))
2548 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2549 log->l_grant_reserve_bytes);
2550 if (free_bytes < need_bytes) {
2551 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2552 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2553 xlog_trace_loggrant(log, tic,
2554 "xlog_grant_log_space: sleep 2");
2555 XFS_STATS_INC(xs_sleep_logspace);
2556 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2558 if (XLOG_FORCED_SHUTDOWN(log)) {
2559 spin_lock(&log->l_grant_lock);
2563 xlog_trace_loggrant(log, tic,
2564 "xlog_grant_log_space: wake 2");
2565 xlog_grant_push_ail(log->l_mp, need_bytes);
2566 spin_lock(&log->l_grant_lock);
2568 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2569 xlog_del_ticketq(&log->l_reserve_headq, tic);
2571 /* we've got enough space */
2572 xlog_grant_add_space(log, need_bytes);
2574 tail_lsn = log->l_tail_lsn;
2576 * Check to make sure the grant write head didn't just over lap the
2577 * tail. If the cycles are the same, we can't be overlapping.
2578 * Otherwise, make sure that the cycles differ by exactly one and
2579 * check the byte count.
2581 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2582 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2583 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2586 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2587 xlog_verify_grant_head(log, 1);
2588 spin_unlock(&log->l_grant_lock);
2592 if (tic->t_flags & XLOG_TIC_IN_Q)
2593 xlog_del_ticketq(&log->l_reserve_headq, tic);
2594 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2596 * If we are failing, make sure the ticket doesn't have any
2597 * current reservations. We don't want to add this back when
2598 * the ticket/transaction gets cancelled.
2600 tic->t_curr_res = 0;
2601 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2602 spin_unlock(&log->l_grant_lock);
2603 return XFS_ERROR(EIO);
2604 } /* xlog_grant_log_space */
2608 * Replenish the byte reservation required by moving the grant write head.
2613 xlog_regrant_write_log_space(xlog_t *log,
2616 int free_bytes, need_bytes;
2617 xlog_ticket_t *ntic;
2622 tic->t_curr_res = tic->t_unit_res;
2623 xlog_tic_reset_res(tic);
2629 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2630 panic("regrant Recovery problem");
2633 spin_lock(&log->l_grant_lock);
2634 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2636 if (XLOG_FORCED_SHUTDOWN(log))
2639 /* If there are other waiters on the queue then give them a
2640 * chance at logspace before us. Wake up the first waiters,
2641 * if we do not wake up all the waiters then go to sleep waiting
2642 * for more free space, otherwise try to get some space for
2646 if ((ntic = log->l_write_headq)) {
2647 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2648 log->l_grant_write_bytes);
2650 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2652 if (free_bytes < ntic->t_unit_res)
2654 free_bytes -= ntic->t_unit_res;
2655 sv_signal(&ntic->t_wait);
2656 ntic = ntic->t_next;
2657 } while (ntic != log->l_write_headq);
2659 if (ntic != log->l_write_headq) {
2660 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2661 xlog_ins_ticketq(&log->l_write_headq, tic);
2663 xlog_trace_loggrant(log, tic,
2664 "xlog_regrant_write_log_space: sleep 1");
2665 XFS_STATS_INC(xs_sleep_logspace);
2666 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
2667 &log->l_grant_lock, s);
2669 /* If we're shutting down, this tic is already
2671 if (XLOG_FORCED_SHUTDOWN(log)) {
2672 spin_lock(&log->l_grant_lock);
2676 xlog_trace_loggrant(log, tic,
2677 "xlog_regrant_write_log_space: wake 1");
2678 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2679 spin_lock(&log->l_grant_lock);
2683 need_bytes = tic->t_unit_res;
2686 if (XLOG_FORCED_SHUTDOWN(log))
2689 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2690 log->l_grant_write_bytes);
2691 if (free_bytes < need_bytes) {
2692 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2693 xlog_ins_ticketq(&log->l_write_headq, tic);
2694 XFS_STATS_INC(xs_sleep_logspace);
2695 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2697 /* If we're shutting down, this tic is already off the queue */
2698 if (XLOG_FORCED_SHUTDOWN(log)) {
2699 spin_lock(&log->l_grant_lock);
2703 xlog_trace_loggrant(log, tic,
2704 "xlog_regrant_write_log_space: wake 2");
2705 xlog_grant_push_ail(log->l_mp, need_bytes);
2706 spin_lock(&log->l_grant_lock);
2708 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2709 xlog_del_ticketq(&log->l_write_headq, tic);
2711 /* we've got enough space */
2712 xlog_grant_add_space_write(log, need_bytes);
2714 tail_lsn = log->l_tail_lsn;
2715 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2716 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2717 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2721 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2722 xlog_verify_grant_head(log, 1);
2723 spin_unlock(&log->l_grant_lock);
2728 if (tic->t_flags & XLOG_TIC_IN_Q)
2729 xlog_del_ticketq(&log->l_reserve_headq, tic);
2730 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2732 * If we are failing, make sure the ticket doesn't have any
2733 * current reservations. We don't want to add this back when
2734 * the ticket/transaction gets cancelled.
2736 tic->t_curr_res = 0;
2737 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2738 spin_unlock(&log->l_grant_lock);
2739 return XFS_ERROR(EIO);
2740 } /* xlog_regrant_write_log_space */
2743 /* The first cnt-1 times through here we don't need to
2744 * move the grant write head because the permanent
2745 * reservation has reserved cnt times the unit amount.
2746 * Release part of current permanent unit reservation and
2747 * reset current reservation to be one units worth. Also
2748 * move grant reservation head forward.
2751 xlog_regrant_reserve_log_space(xlog_t *log,
2752 xlog_ticket_t *ticket)
2754 xlog_trace_loggrant(log, ticket,
2755 "xlog_regrant_reserve_log_space: enter");
2756 if (ticket->t_cnt > 0)
2759 spin_lock(&log->l_grant_lock);
2760 xlog_grant_sub_space(log, ticket->t_curr_res);
2761 ticket->t_curr_res = ticket->t_unit_res;
2762 xlog_tic_reset_res(ticket);
2763 xlog_trace_loggrant(log, ticket,
2764 "xlog_regrant_reserve_log_space: sub current res");
2765 xlog_verify_grant_head(log, 1);
2767 /* just return if we still have some of the pre-reserved space */
2768 if (ticket->t_cnt > 0) {
2769 spin_unlock(&log->l_grant_lock);
2773 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
2774 xlog_trace_loggrant(log, ticket,
2775 "xlog_regrant_reserve_log_space: exit");
2776 xlog_verify_grant_head(log, 0);
2777 spin_unlock(&log->l_grant_lock);
2778 ticket->t_curr_res = ticket->t_unit_res;
2779 xlog_tic_reset_res(ticket);
2780 } /* xlog_regrant_reserve_log_space */
2784 * Give back the space left from a reservation.
2786 * All the information we need to make a correct determination of space left
2787 * is present. For non-permanent reservations, things are quite easy. The
2788 * count should have been decremented to zero. We only need to deal with the
2789 * space remaining in the current reservation part of the ticket. If the
2790 * ticket contains a permanent reservation, there may be left over space which
2791 * needs to be released. A count of N means that N-1 refills of the current
2792 * reservation can be done before we need to ask for more space. The first
2793 * one goes to fill up the first current reservation. Once we run out of
2794 * space, the count will stay at zero and the only space remaining will be
2795 * in the current reservation field.
2798 xlog_ungrant_log_space(xlog_t *log,
2799 xlog_ticket_t *ticket)
2801 if (ticket->t_cnt > 0)
2804 spin_lock(&log->l_grant_lock);
2805 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2807 xlog_grant_sub_space(log, ticket->t_curr_res);
2809 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2811 /* If this is a permanent reservation ticket, we may be able to free
2812 * up more space based on the remaining count.
2814 if (ticket->t_cnt > 0) {
2815 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2816 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
2819 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2820 xlog_verify_grant_head(log, 1);
2821 spin_unlock(&log->l_grant_lock);
2822 xfs_log_move_tail(log->l_mp, 1);
2823 } /* xlog_ungrant_log_space */
2827 * Flush iclog to disk if this is the last reference to the given iclog and
2828 * the WANT_SYNC bit is set.
2830 * When this function is entered, the iclog is not necessarily in the
2831 * WANT_SYNC state. It may be sitting around waiting to get filled.
2836 xlog_state_release_iclog(
2838 xlog_in_core_t *iclog)
2840 int sync = 0; /* do we sync? */
2842 if (iclog->ic_state & XLOG_STATE_IOERROR)
2843 return XFS_ERROR(EIO);
2845 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2846 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2849 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2850 spin_unlock(&log->l_icloglock);
2851 return XFS_ERROR(EIO);
2853 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2854 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2856 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2857 /* update tail before writing to iclog */
2858 xlog_assign_tail_lsn(log->l_mp);
2860 iclog->ic_state = XLOG_STATE_SYNCING;
2861 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
2862 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2863 /* cycle incremented when incrementing curr_block */
2865 spin_unlock(&log->l_icloglock);
2868 * We let the log lock go, so it's possible that we hit a log I/O
2869 * error or some other SHUTDOWN condition that marks the iclog
2870 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2871 * this iclog has consistent data, so we ignore IOERROR
2872 * flags after this point.
2875 return xlog_sync(log, iclog);
2877 } /* xlog_state_release_iclog */
2881 * This routine will mark the current iclog in the ring as WANT_SYNC
2882 * and move the current iclog pointer to the next iclog in the ring.
2883 * When this routine is called from xlog_state_get_iclog_space(), the
2884 * exact size of the iclog has not yet been determined. All we know is
2885 * that every data block. We have run out of space in this log record.
2888 xlog_state_switch_iclogs(xlog_t *log,
2889 xlog_in_core_t *iclog,
2892 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2894 eventual_size = iclog->ic_offset;
2895 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2896 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
2897 log->l_prev_block = log->l_curr_block;
2898 log->l_prev_cycle = log->l_curr_cycle;
2900 /* roll log?: ic_offset changed later */
2901 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2903 /* Round up to next log-sunit */
2904 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
2905 log->l_mp->m_sb.sb_logsunit > 1) {
2906 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2907 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2910 if (log->l_curr_block >= log->l_logBBsize) {
2911 log->l_curr_cycle++;
2912 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2913 log->l_curr_cycle++;
2914 log->l_curr_block -= log->l_logBBsize;
2915 ASSERT(log->l_curr_block >= 0);
2917 ASSERT(iclog == log->l_iclog);
2918 log->l_iclog = iclog->ic_next;
2919 } /* xlog_state_switch_iclogs */
2923 * Write out all data in the in-core log as of this exact moment in time.
2925 * Data may be written to the in-core log during this call. However,
2926 * we don't guarantee this data will be written out. A change from past
2927 * implementation means this routine will *not* write out zero length LRs.
2929 * Basically, we try and perform an intelligent scan of the in-core logs.
2930 * If we determine there is no flushable data, we just return. There is no
2931 * flushable data if:
2933 * 1. the current iclog is active and has no data; the previous iclog
2934 * is in the active or dirty state.
2935 * 2. the current iclog is drity, and the previous iclog is in the
2936 * active or dirty state.
2940 * 1. the current iclog is not in the active nor dirty state.
2941 * 2. the current iclog dirty, and the previous iclog is not in the
2942 * active nor dirty state.
2943 * 3. the current iclog is active, and there is another thread writing
2944 * to this particular iclog.
2945 * 4. a) the current iclog is active and has no other writers
2946 * b) when we return from flushing out this iclog, it is still
2947 * not in the active nor dirty state.
2950 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2952 xlog_in_core_t *iclog;
2955 spin_lock(&log->l_icloglock);
2957 iclog = log->l_iclog;
2958 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2959 spin_unlock(&log->l_icloglock);
2960 return XFS_ERROR(EIO);
2963 /* If the head iclog is not active nor dirty, we just attach
2964 * ourselves to the head and go to sleep.
2966 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2967 iclog->ic_state == XLOG_STATE_DIRTY) {
2969 * If the head is dirty or (active and empty), then
2970 * we need to look at the previous iclog. If the previous
2971 * iclog is active or dirty we are done. There is nothing
2972 * to sync out. Otherwise, we attach ourselves to the
2973 * previous iclog and go to sleep.
2975 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2976 (atomic_read(&iclog->ic_refcnt) == 0
2977 && iclog->ic_offset == 0)) {
2978 iclog = iclog->ic_prev;
2979 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2980 iclog->ic_state == XLOG_STATE_DIRTY)
2985 if (atomic_read(&iclog->ic_refcnt) == 0) {
2986 /* We are the only one with access to this
2987 * iclog. Flush it out now. There should
2988 * be a roundoff of zero to show that someone
2989 * has already taken care of the roundoff from
2990 * the previous sync.
2992 atomic_inc(&iclog->ic_refcnt);
2993 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2994 xlog_state_switch_iclogs(log, iclog, 0);
2995 spin_unlock(&log->l_icloglock);
2997 if (xlog_state_release_iclog(log, iclog))
2998 return XFS_ERROR(EIO);
3000 spin_lock(&log->l_icloglock);
3001 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
3002 iclog->ic_state != XLOG_STATE_DIRTY)
3007 /* Someone else is writing to this iclog.
3008 * Use its call to flush out the data. However,
3009 * the other thread may not force out this LR,
3010 * so we mark it WANT_SYNC.
3012 xlog_state_switch_iclogs(log, iclog, 0);
3018 /* By the time we come around again, the iclog could've been filled
3019 * which would give it another lsn. If we have a new lsn, just
3020 * return because the relevant data has been flushed.
3023 if (flags & XFS_LOG_SYNC) {
3025 * We must check if we're shutting down here, before
3026 * we wait, while we're holding the l_icloglock.
3027 * Then we check again after waking up, in case our
3028 * sleep was disturbed by a bad news.
3030 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3031 spin_unlock(&log->l_icloglock);
3032 return XFS_ERROR(EIO);
3034 XFS_STATS_INC(xs_log_force_sleep);
3035 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
3037 * No need to grab the log lock here since we're
3038 * only deciding whether or not to return EIO
3039 * and the memory read should be atomic.
3041 if (iclog->ic_state & XLOG_STATE_IOERROR)
3042 return XFS_ERROR(EIO);
3048 spin_unlock(&log->l_icloglock);
3051 } /* xlog_state_sync_all */
3055 * Used by code which implements synchronous log forces.
3057 * Find in-core log with lsn.
3058 * If it is in the DIRTY state, just return.
3059 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3060 * state and go to sleep or return.
3061 * If it is in any other state, go to sleep or return.
3063 * If filesystem activity goes to zero, the iclog will get flushed only by
3067 xlog_state_sync(xlog_t *log,
3072 xlog_in_core_t *iclog;
3073 int already_slept = 0;
3076 spin_lock(&log->l_icloglock);
3077 iclog = log->l_iclog;
3079 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3080 spin_unlock(&log->l_icloglock);
3081 return XFS_ERROR(EIO);
3085 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3086 iclog = iclog->ic_next;
3090 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3091 spin_unlock(&log->l_icloglock);
3095 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3097 * We sleep here if we haven't already slept (e.g.
3098 * this is the first time we've looked at the correct
3099 * iclog buf) and the buffer before us is going to
3100 * be sync'ed. The reason for this is that if we
3101 * are doing sync transactions here, by waiting for
3102 * the previous I/O to complete, we can allow a few
3103 * more transactions into this iclog before we close
3106 * Otherwise, we mark the buffer WANT_SYNC, and bump
3107 * up the refcnt so we can release the log (which drops
3108 * the ref count). The state switch keeps new transaction
3109 * commits from using this buffer. When the current commits
3110 * finish writing into the buffer, the refcount will drop to
3111 * zero and the buffer will go out then.
3113 if (!already_slept &&
3114 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3115 XLOG_STATE_SYNCING))) {
3116 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3117 XFS_STATS_INC(xs_log_force_sleep);
3118 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
3119 &log->l_icloglock, s);
3124 atomic_inc(&iclog->ic_refcnt);
3125 xlog_state_switch_iclogs(log, iclog, 0);
3126 spin_unlock(&log->l_icloglock);
3127 if (xlog_state_release_iclog(log, iclog))
3128 return XFS_ERROR(EIO);
3130 spin_lock(&log->l_icloglock);
3134 if ((flags & XFS_LOG_SYNC) && /* sleep */
3135 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3138 * Don't wait on completion if we know that we've
3139 * gotten a log write error.
3141 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3142 spin_unlock(&log->l_icloglock);
3143 return XFS_ERROR(EIO);
3145 XFS_STATS_INC(xs_log_force_sleep);
3146 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3148 * No need to grab the log lock here since we're
3149 * only deciding whether or not to return EIO
3150 * and the memory read should be atomic.
3152 if (iclog->ic_state & XLOG_STATE_IOERROR)
3153 return XFS_ERROR(EIO);
3155 } else { /* just return */
3156 spin_unlock(&log->l_icloglock);
3160 } while (iclog != log->l_iclog);
3162 spin_unlock(&log->l_icloglock);
3164 } /* xlog_state_sync */
3168 * Called when we want to mark the current iclog as being ready to sync to
3172 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3174 ASSERT(spin_is_locked(&log->l_icloglock));
3176 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3177 xlog_state_switch_iclogs(log, iclog, 0);
3179 ASSERT(iclog->ic_state &
3180 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3185 /*****************************************************************************
3189 *****************************************************************************
3193 * Free a used ticket when it's refcount falls to zero.
3197 xlog_ticket_t *ticket)
3199 ASSERT(atomic_read(&ticket->t_ref) > 0);
3200 if (atomic_dec_and_test(&ticket->t_ref)) {
3201 sv_destroy(&ticket->t_wait);
3202 kmem_zone_free(xfs_log_ticket_zone, ticket);
3208 xlog_ticket_t *ticket)
3210 ASSERT(atomic_read(&ticket->t_ref) > 0);
3211 atomic_inc(&ticket->t_ref);
3216 * Allocate and initialise a new log ticket.
3218 STATIC xlog_ticket_t *
3219 xlog_ticket_alloc(xlog_t *log,
3228 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3233 * Permanent reservations have up to 'cnt'-1 active log operations
3234 * in the log. A unit in this case is the amount of space for one
3235 * of these log operations. Normal reservations have a cnt of 1
3236 * and their unit amount is the total amount of space required.
3238 * The following lines of code account for non-transaction data
3239 * which occupy space in the on-disk log.
3241 * Normal form of a transaction is:
3242 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3243 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3245 * We need to account for all the leadup data and trailer data
3246 * around the transaction data.
3247 * And then we need to account for the worst case in terms of using
3249 * The worst case will happen if:
3250 * - the placement of the transaction happens to be such that the
3251 * roundoff is at its maximum
3252 * - the transaction data is synced before the commit record is synced
3253 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3254 * Therefore the commit record is in its own Log Record.
3255 * This can happen as the commit record is called with its
3256 * own region to xlog_write().
3257 * This then means that in the worst case, roundoff can happen for
3258 * the commit-rec as well.
3259 * The commit-rec is smaller than padding in this scenario and so it is
3260 * not added separately.
3263 /* for trans header */
3264 unit_bytes += sizeof(xlog_op_header_t);
3265 unit_bytes += sizeof(xfs_trans_header_t);
3268 unit_bytes += sizeof(xlog_op_header_t);
3270 /* for LR headers */
3271 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3272 unit_bytes += log->l_iclog_hsize * num_headers;
3274 /* for commit-rec LR header - note: padding will subsume the ophdr */
3275 unit_bytes += log->l_iclog_hsize;
3277 /* for split-recs - ophdrs added when data split over LRs */
3278 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3280 /* for roundoff padding for transaction data and one for commit record */
3281 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
3282 log->l_mp->m_sb.sb_logsunit > 1) {
3283 /* log su roundoff */
3284 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3287 unit_bytes += 2*BBSIZE;
3290 atomic_set(&tic->t_ref, 1);
3291 tic->t_unit_res = unit_bytes;
3292 tic->t_curr_res = unit_bytes;
3295 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3296 tic->t_clientid = client;
3297 tic->t_flags = XLOG_TIC_INITED;
3298 tic->t_trans_type = 0;
3299 if (xflags & XFS_LOG_PERM_RESERV)
3300 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3301 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
3303 xlog_tic_reset_res(tic);
3309 /******************************************************************************
3311 * Log debug routines
3313 ******************************************************************************
3317 * Make sure that the destination ptr is within the valid data region of
3318 * one of the iclogs. This uses backup pointers stored in a different
3319 * part of the log in case we trash the log structure.
3322 xlog_verify_dest_ptr(xlog_t *log,
3328 for (i=0; i < log->l_iclog_bufs; i++) {
3329 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3330 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3334 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3335 } /* xlog_verify_dest_ptr */
3338 xlog_verify_grant_head(xlog_t *log, int equals)
3340 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3342 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3344 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3346 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3347 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3349 } /* xlog_verify_grant_head */
3351 /* check if it will fit */
3353 xlog_verify_tail_lsn(xlog_t *log,
3354 xlog_in_core_t *iclog,
3359 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3361 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3362 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3363 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3365 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3367 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3368 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3370 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3371 if (blocks < BTOBB(iclog->ic_offset) + 1)
3372 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3374 } /* xlog_verify_tail_lsn */
3377 * Perform a number of checks on the iclog before writing to disk.
3379 * 1. Make sure the iclogs are still circular
3380 * 2. Make sure we have a good magic number
3381 * 3. Make sure we don't have magic numbers in the data
3382 * 4. Check fields of each log operation header for:
3383 * A. Valid client identifier
3384 * B. tid ptr value falls in valid ptr space (user space code)
3385 * C. Length in log record header is correct according to the
3386 * individual operation headers within record.
3387 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3388 * log, check the preceding blocks of the physical log to make sure all
3389 * the cycle numbers agree with the current cycle number.
3392 xlog_verify_iclog(xlog_t *log,
3393 xlog_in_core_t *iclog,
3397 xlog_op_header_t *ophead;
3398 xlog_in_core_t *icptr;
3399 xlog_in_core_2_t *xhdr;
3401 xfs_caddr_t base_ptr;
3402 __psint_t field_offset;
3404 int len, i, j, k, op_len;
3407 /* check validity of iclog pointers */
3408 spin_lock(&log->l_icloglock);
3409 icptr = log->l_iclog;
3410 for (i=0; i < log->l_iclog_bufs; i++) {
3412 xlog_panic("xlog_verify_iclog: invalid ptr");
3413 icptr = icptr->ic_next;
3415 if (icptr != log->l_iclog)
3416 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3417 spin_unlock(&log->l_icloglock);
3419 /* check log magic numbers */
3420 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
3421 xlog_panic("xlog_verify_iclog: invalid magic num");
3423 ptr = (xfs_caddr_t) &iclog->ic_header;
3424 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
3426 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
3427 xlog_panic("xlog_verify_iclog: unexpected magic num");
3431 len = be32_to_cpu(iclog->ic_header.h_num_logops);
3432 ptr = iclog->ic_datap;
3434 ophead = (xlog_op_header_t *)ptr;
3435 xhdr = iclog->ic_data;
3436 for (i = 0; i < len; i++) {
3437 ophead = (xlog_op_header_t *)ptr;
3439 /* clientid is only 1 byte */
3440 field_offset = (__psint_t)
3441 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3442 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3443 clientid = ophead->oh_clientid;
3445 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3446 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3447 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3448 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3449 clientid = xlog_get_client_id(
3450 xhdr[j].hic_xheader.xh_cycle_data[k]);
3452 clientid = xlog_get_client_id(
3453 iclog->ic_header.h_cycle_data[idx]);
3456 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3457 cmn_err(CE_WARN, "xlog_verify_iclog: "
3458 "invalid clientid %d op 0x%p offset 0x%lx",
3459 clientid, ophead, (unsigned long)field_offset);
3462 field_offset = (__psint_t)
3463 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3464 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3465 op_len = be32_to_cpu(ophead->oh_len);
3467 idx = BTOBBT((__psint_t)&ophead->oh_len -
3468 (__psint_t)iclog->ic_datap);
3469 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3470 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3471 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3472 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
3474 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
3477 ptr += sizeof(xlog_op_header_t) + op_len;
3479 } /* xlog_verify_iclog */
3483 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3489 xlog_in_core_t *iclog, *ic;
3491 iclog = log->l_iclog;
3492 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3494 * Mark all the incore logs IOERROR.
3495 * From now on, no log flushes will result.
3499 ic->ic_state = XLOG_STATE_IOERROR;
3501 } while (ic != iclog);
3505 * Return non-zero, if state transition has already happened.
3511 * This is called from xfs_force_shutdown, when we're forcibly
3512 * shutting down the filesystem, typically because of an IO error.
3513 * Our main objectives here are to make sure that:
3514 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3515 * parties to find out, 'atomically'.
3516 * b. those who're sleeping on log reservations, pinned objects and
3517 * other resources get woken up, and be told the bad news.
3518 * c. nothing new gets queued up after (a) and (b) are done.
3519 * d. if !logerror, flush the iclogs to disk, then seal them off
3523 xfs_log_force_umount(
3524 struct xfs_mount *mp,
3535 * If this happens during log recovery, don't worry about
3536 * locking; the log isn't open for business yet.
3539 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3540 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3542 XFS_BUF_DONE(mp->m_sb_bp);
3547 * Somebody could've already done the hard work for us.
3548 * No need to get locks for this.
3550 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3551 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3556 * We must hold both the GRANT lock and the LOG lock,
3557 * before we mark the filesystem SHUTDOWN and wake
3558 * everybody up to tell the bad news.
3560 spin_lock(&log->l_icloglock);
3561 spin_lock(&log->l_grant_lock);
3562 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3564 XFS_BUF_DONE(mp->m_sb_bp);
3567 * This flag is sort of redundant because of the mount flag, but
3568 * it's good to maintain the separation between the log and the rest
3571 log->l_flags |= XLOG_IO_ERROR;
3574 * If we hit a log error, we want to mark all the iclogs IOERROR
3575 * while we're still holding the loglock.
3578 retval = xlog_state_ioerror(log);
3579 spin_unlock(&log->l_icloglock);
3582 * We don't want anybody waiting for log reservations
3583 * after this. That means we have to wake up everybody
3584 * queued up on reserve_headq as well as write_headq.
3585 * In addition, we make sure in xlog_{re}grant_log_space
3586 * that we don't enqueue anything once the SHUTDOWN flag
3587 * is set, and this action is protected by the GRANTLOCK.
3589 if ((tic = log->l_reserve_headq)) {
3591 sv_signal(&tic->t_wait);
3593 } while (tic != log->l_reserve_headq);
3596 if ((tic = log->l_write_headq)) {
3598 sv_signal(&tic->t_wait);
3600 } while (tic != log->l_write_headq);
3602 spin_unlock(&log->l_grant_lock);
3604 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3607 * Force the incore logs to disk before shutting the
3608 * log down completely.
3610 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3611 spin_lock(&log->l_icloglock);
3612 retval = xlog_state_ioerror(log);
3613 spin_unlock(&log->l_icloglock);
3616 * Wake up everybody waiting on xfs_log_force.
3617 * Callback all log item committed functions as if the
3618 * log writes were completed.
3620 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3622 #ifdef XFSERRORDEBUG
3624 xlog_in_core_t *iclog;
3626 spin_lock(&log->l_icloglock);
3627 iclog = log->l_iclog;
3629 ASSERT(iclog->ic_callback == 0);
3630 iclog = iclog->ic_next;
3631 } while (iclog != log->l_iclog);
3632 spin_unlock(&log->l_icloglock);
3635 /* return non-zero if log IOERROR transition had already happened */
3640 xlog_iclogs_empty(xlog_t *log)
3642 xlog_in_core_t *iclog;
3644 iclog = log->l_iclog;
3646 /* endianness does not matter here, zero is zero in
3649 if (iclog->ic_header.h_num_logops)
3651 iclog = iclog->ic_next;
3652 } while (iclog != log->l_iclog);