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);
597 goto out_destroy_ail;
601 /* Normal transactions can now occur */
602 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
607 xfs_trans_ail_destroy(mp);
609 xlog_dealloc_log(mp->m_log);
615 * Finish the recovery of the file system. This is separate from
616 * the xfs_log_mount() call, because it depends on the code in
617 * xfs_mountfs() to read in the root and real-time bitmap inodes
618 * between calling xfs_log_mount() and here.
620 * mp - ubiquitous xfs mount point structure
623 xfs_log_mount_finish(xfs_mount_t *mp)
627 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
628 error = xlog_recover_finish(mp->m_log);
631 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
638 * Unmount processing for the log.
641 xfs_log_unmount(xfs_mount_t *mp)
645 error = xfs_log_unmount_write(mp);
646 xfs_log_unmount_dealloc(mp);
651 * Final log writes as part of unmount.
653 * Mark the filesystem clean as unmount happens. Note that during relocation
654 * this routine needs to be executed as part of source-bag while the
655 * deallocation must not be done until source-end.
659 * Unmount record used to have a string "Unmount filesystem--" in the
660 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
661 * We just write the magic number now since that particular field isn't
662 * currently architecture converted and "nUmount" is a bit foo.
663 * As far as I know, there weren't any dependencies on the old behaviour.
667 xfs_log_unmount_write(xfs_mount_t *mp)
669 xlog_t *log = mp->m_log;
670 xlog_in_core_t *iclog;
672 xlog_in_core_t *first_iclog;
674 xfs_log_iovec_t reg[1];
675 xfs_log_ticket_t tic = NULL;
679 /* the data section must be 32 bit size aligned */
683 __uint32_t pad2; /* may as well make it 64 bits */
684 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
687 * Don't write out unmount record on read-only mounts.
688 * Or, if we are doing a forced umount (typically because of IO errors).
690 if (mp->m_flags & XFS_MOUNT_RDONLY)
693 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
694 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
697 first_iclog = iclog = log->l_iclog;
699 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
700 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
701 ASSERT(iclog->ic_offset == 0);
703 iclog = iclog->ic_next;
704 } while (iclog != first_iclog);
706 if (! (XLOG_FORCED_SHUTDOWN(log))) {
707 reg[0].i_addr = (void*)&magic;
708 reg[0].i_len = sizeof(magic);
709 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
711 error = xfs_log_reserve(mp, 600, 1, &tic,
712 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
714 /* remove inited flag */
715 ((xlog_ticket_t *)tic)->t_flags = 0;
716 error = xlog_write(mp, reg, 1, tic, &lsn,
717 NULL, XLOG_UNMOUNT_TRANS);
719 * At this point, we're umounting anyway,
720 * so there's no point in transitioning log state
721 * to IOERROR. Just continue...
726 xfs_fs_cmn_err(CE_ALERT, mp,
727 "xfs_log_unmount: unmount record failed");
731 spin_lock(&log->l_icloglock);
732 iclog = log->l_iclog;
733 atomic_inc(&iclog->ic_refcnt);
734 xlog_state_want_sync(log, iclog);
735 spin_unlock(&log->l_icloglock);
736 error = xlog_state_release_iclog(log, iclog);
738 spin_lock(&log->l_icloglock);
739 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
740 iclog->ic_state == XLOG_STATE_DIRTY)) {
741 if (!XLOG_FORCED_SHUTDOWN(log)) {
742 sv_wait(&iclog->ic_force_wait, PMEM,
743 &log->l_icloglock, s);
745 spin_unlock(&log->l_icloglock);
748 spin_unlock(&log->l_icloglock);
751 xlog_trace_loggrant(log, tic, "unmount rec");
752 xlog_ungrant_log_space(log, tic);
753 xfs_log_ticket_put(tic);
757 * We're already in forced_shutdown mode, couldn't
758 * even attempt to write out the unmount transaction.
760 * Go through the motions of sync'ing and releasing
761 * the iclog, even though no I/O will actually happen,
762 * we need to wait for other log I/Os that may already
763 * be in progress. Do this as a separate section of
764 * code so we'll know if we ever get stuck here that
765 * we're in this odd situation of trying to unmount
766 * a file system that went into forced_shutdown as
767 * the result of an unmount..
769 spin_lock(&log->l_icloglock);
770 iclog = log->l_iclog;
771 atomic_inc(&iclog->ic_refcnt);
773 xlog_state_want_sync(log, iclog);
774 spin_unlock(&log->l_icloglock);
775 error = xlog_state_release_iclog(log, iclog);
777 spin_lock(&log->l_icloglock);
779 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
780 || iclog->ic_state == XLOG_STATE_DIRTY
781 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
783 sv_wait(&iclog->ic_force_wait, PMEM,
784 &log->l_icloglock, s);
786 spin_unlock(&log->l_icloglock);
791 } /* xfs_log_unmount_write */
794 * Deallocate log structures for unmount/relocation.
796 * We need to stop the aild from running before we destroy
797 * and deallocate the log as the aild references the log.
800 xfs_log_unmount_dealloc(xfs_mount_t *mp)
802 xfs_trans_ail_destroy(mp);
803 xlog_dealloc_log(mp->m_log);
807 * Write region vectors to log. The write happens using the space reservation
808 * of the ticket (tic). It is not a requirement that all writes for a given
809 * transaction occur with one call to xfs_log_write().
812 xfs_log_write(xfs_mount_t * mp,
813 xfs_log_iovec_t reg[],
815 xfs_log_ticket_t tic,
816 xfs_lsn_t *start_lsn)
819 xlog_t *log = mp->m_log;
821 if (XLOG_FORCED_SHUTDOWN(log))
822 return XFS_ERROR(EIO);
824 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
825 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
828 } /* xfs_log_write */
832 xfs_log_move_tail(xfs_mount_t *mp,
836 xlog_t *log = mp->m_log;
837 int need_bytes, free_bytes, cycle, bytes;
839 if (XLOG_FORCED_SHUTDOWN(log))
843 /* needed since sync_lsn is 64 bits */
844 spin_lock(&log->l_icloglock);
845 tail_lsn = log->l_last_sync_lsn;
846 spin_unlock(&log->l_icloglock);
849 spin_lock(&log->l_grant_lock);
851 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
855 log->l_tail_lsn = tail_lsn;
858 if ((tic = log->l_write_headq)) {
860 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
861 panic("Recovery problem");
863 cycle = log->l_grant_write_cycle;
864 bytes = log->l_grant_write_bytes;
865 free_bytes = xlog_space_left(log, cycle, bytes);
867 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
869 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
872 free_bytes -= tic->t_unit_res;
873 sv_signal(&tic->t_wait);
875 } while (tic != log->l_write_headq);
877 if ((tic = log->l_reserve_headq)) {
879 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
880 panic("Recovery problem");
882 cycle = log->l_grant_reserve_cycle;
883 bytes = log->l_grant_reserve_bytes;
884 free_bytes = xlog_space_left(log, cycle, bytes);
886 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
887 need_bytes = tic->t_unit_res*tic->t_cnt;
889 need_bytes = tic->t_unit_res;
890 if (free_bytes < need_bytes && tail_lsn != 1)
893 free_bytes -= need_bytes;
894 sv_signal(&tic->t_wait);
896 } while (tic != log->l_reserve_headq);
898 spin_unlock(&log->l_grant_lock);
899 } /* xfs_log_move_tail */
902 * Determine if we have a transaction that has gone to disk
903 * that needs to be covered. Log activity needs to be idle (no AIL and
904 * nothing in the iclogs). And, we need to be in the right state indicating
905 * something has gone out.
908 xfs_log_need_covered(xfs_mount_t *mp)
911 xlog_t *log = mp->m_log;
913 if (!xfs_fs_writable(mp))
916 spin_lock(&log->l_icloglock);
917 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
918 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
919 && !xfs_trans_ail_tail(log->l_ailp)
920 && xlog_iclogs_empty(log)) {
921 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
922 log->l_covered_state = XLOG_STATE_COVER_DONE;
924 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
925 log->l_covered_state = XLOG_STATE_COVER_DONE2;
929 spin_unlock(&log->l_icloglock);
933 /******************************************************************************
937 ******************************************************************************
940 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
941 * The log manager must keep track of the last LR which was committed
942 * to disk. The lsn of this LR will become the new tail_lsn whenever
943 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
944 * the situation where stuff could be written into the log but nothing
945 * was ever in the AIL when asked. Eventually, we panic since the
946 * tail hits the head.
948 * We may be holding the log iclog lock upon entering this routine.
951 xlog_assign_tail_lsn(xfs_mount_t *mp)
954 xlog_t *log = mp->m_log;
956 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
957 spin_lock(&log->l_grant_lock);
959 log->l_tail_lsn = tail_lsn;
961 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
963 spin_unlock(&log->l_grant_lock);
966 } /* xlog_assign_tail_lsn */
970 * Return the space in the log between the tail and the head. The head
971 * is passed in the cycle/bytes formal parms. In the special case where
972 * the reserve head has wrapped passed the tail, this calculation is no
973 * longer valid. In this case, just return 0 which means there is no space
974 * in the log. This works for all places where this function is called
975 * with the reserve head. Of course, if the write head were to ever
976 * wrap the tail, we should blow up. Rather than catch this case here,
977 * we depend on other ASSERTions in other parts of the code. XXXmiken
979 * This code also handles the case where the reservation head is behind
980 * the tail. The details of this case are described below, but the end
981 * result is that we return the size of the log as the amount of space left.
984 xlog_space_left(xlog_t *log, int cycle, int bytes)
990 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
991 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
992 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
993 free_bytes = log->l_logsize - (bytes - tail_bytes);
994 } else if ((tail_cycle + 1) < cycle) {
996 } else if (tail_cycle < cycle) {
997 ASSERT(tail_cycle == (cycle - 1));
998 free_bytes = tail_bytes - bytes;
1001 * The reservation head is behind the tail.
1002 * In this case we just want to return the size of the
1003 * log as the amount of space left.
1005 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
1006 "xlog_space_left: head behind tail\n"
1007 " tail_cycle = %d, tail_bytes = %d\n"
1008 " GH cycle = %d, GH bytes = %d",
1009 tail_cycle, tail_bytes, cycle, bytes);
1011 free_bytes = log->l_logsize;
1014 } /* xlog_space_left */
1018 * Log function which is called when an io completes.
1020 * The log manager needs its own routine, in order to control what
1021 * happens with the buffer after the write completes.
1024 xlog_iodone(xfs_buf_t *bp)
1026 xlog_in_core_t *iclog;
1030 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1031 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1032 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1037 * If the _XFS_BARRIER_FAILED flag was set by a lower
1038 * layer, it means the underlying device no longer supports
1039 * barrier I/O. Warn loudly and turn off barriers.
1041 if (bp->b_flags & _XFS_BARRIER_FAILED) {
1042 bp->b_flags &= ~_XFS_BARRIER_FAILED;
1043 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1044 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1045 "xlog_iodone: Barriers are no longer supported"
1046 " by device. Disabling barriers\n");
1047 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1051 * Race to shutdown the filesystem if we see an error.
1053 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1054 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1055 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1057 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
1059 * This flag will be propagated to the trans-committed
1060 * callback routines to let them know that the log-commit
1063 aborted = XFS_LI_ABORTED;
1064 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1065 aborted = XFS_LI_ABORTED;
1068 /* log I/O is always issued ASYNC */
1069 ASSERT(XFS_BUF_ISASYNC(bp));
1070 xlog_state_done_syncing(iclog, aborted);
1072 * do not reference the buffer (bp) here as we could race
1073 * with it being freed after writing the unmount record to the
1080 * The bdstrat callback function for log bufs. This gives us a central
1081 * place to trap bufs in case we get hit by a log I/O error and need to
1082 * shutdown. Actually, in practice, even when we didn't get a log error,
1083 * we transition the iclogs to IOERROR state *after* flushing all existing
1084 * iclogs to disk. This is because we don't want anymore new transactions to be
1085 * started or completed afterwards.
1088 xlog_bdstrat_cb(struct xfs_buf *bp)
1090 xlog_in_core_t *iclog;
1092 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1094 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1095 /* note for irix bstrat will need struct bdevsw passed
1096 * Fix the following macro if the code ever is merged
1102 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1103 XFS_BUF_ERROR(bp, EIO);
1106 return XFS_ERROR(EIO);
1112 * Return size of each in-core log record buffer.
1114 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
1116 * If the filesystem blocksize is too large, we may need to choose a
1117 * larger size since the directory code currently logs entire blocks.
1121 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1127 if (mp->m_logbufs <= 0)
1128 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1130 log->l_iclog_bufs = mp->m_logbufs;
1133 * Buffer size passed in from mount system call.
1135 if (mp->m_logbsize > 0) {
1136 size = log->l_iclog_size = mp->m_logbsize;
1137 log->l_iclog_size_log = 0;
1139 log->l_iclog_size_log++;
1143 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1144 /* # headers = size / 32K
1145 * one header holds cycles from 32K of data
1148 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1149 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1151 log->l_iclog_hsize = xhdrs << BBSHIFT;
1152 log->l_iclog_heads = xhdrs;
1154 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1155 log->l_iclog_hsize = BBSIZE;
1156 log->l_iclog_heads = 1;
1161 /* All machines use 32KB buffers by default. */
1162 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1163 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1165 /* the default log size is 16k or 32k which is one header sector */
1166 log->l_iclog_hsize = BBSIZE;
1167 log->l_iclog_heads = 1;
1170 /* are we being asked to make the sizes selected above visible? */
1171 if (mp->m_logbufs == 0)
1172 mp->m_logbufs = log->l_iclog_bufs;
1173 if (mp->m_logbsize == 0)
1174 mp->m_logbsize = log->l_iclog_size;
1175 } /* xlog_get_iclog_buffer_size */
1179 * This routine initializes some of the log structure for a given mount point.
1180 * Its primary purpose is to fill in enough, so recovery can occur. However,
1181 * some other stuff may be filled in too.
1184 xlog_alloc_log(xfs_mount_t *mp,
1185 xfs_buftarg_t *log_target,
1186 xfs_daddr_t blk_offset,
1190 xlog_rec_header_t *head;
1191 xlog_in_core_t **iclogp;
1192 xlog_in_core_t *iclog, *prev_iclog=NULL;
1197 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1202 log->l_targ = log_target;
1203 log->l_logsize = BBTOB(num_bblks);
1204 log->l_logBBstart = blk_offset;
1205 log->l_logBBsize = num_bblks;
1206 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1207 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1209 log->l_prev_block = -1;
1210 log->l_tail_lsn = xlog_assign_lsn(1, 0);
1211 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1212 log->l_last_sync_lsn = log->l_tail_lsn;
1213 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1214 log->l_grant_reserve_cycle = 1;
1215 log->l_grant_write_cycle = 1;
1217 if (xfs_sb_version_hassector(&mp->m_sb)) {
1218 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1219 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1220 /* for larger sector sizes, must have v2 or external log */
1221 ASSERT(log->l_sectbb_log == 0 ||
1222 log->l_logBBstart == 0 ||
1223 xfs_sb_version_haslogv2(&mp->m_sb));
1224 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1226 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1228 xlog_get_iclog_buffer_size(mp, log);
1230 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1233 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1234 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1235 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1236 ASSERT(XFS_BUF_ISBUSY(bp));
1237 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1240 spin_lock_init(&log->l_icloglock);
1241 spin_lock_init(&log->l_grant_lock);
1242 sv_init(&log->l_flush_wait, 0, "flush_wait");
1244 xlog_trace_loggrant_alloc(log);
1245 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1246 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1248 iclogp = &log->l_iclog;
1250 * The amount of memory to allocate for the iclog structure is
1251 * rather funky due to the way the structure is defined. It is
1252 * done this way so that we can use different sizes for machines
1253 * with different amounts of memory. See the definition of
1254 * xlog_in_core_t in xfs_log_priv.h for details.
1256 iclogsize = log->l_iclog_size;
1257 ASSERT(log->l_iclog_size >= 4096);
1258 for (i=0; i < log->l_iclog_bufs; i++) {
1259 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1261 goto out_free_iclog;
1264 iclog->ic_prev = prev_iclog;
1267 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1269 goto out_free_iclog;
1270 if (!XFS_BUF_CPSEMA(bp))
1272 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1273 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1274 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1276 iclog->ic_data = bp->b_addr;
1278 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1280 head = &iclog->ic_header;
1281 memset(head, 0, sizeof(xlog_rec_header_t));
1282 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1283 head->h_version = cpu_to_be32(
1284 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1285 head->h_size = cpu_to_be32(log->l_iclog_size);
1287 head->h_fmt = cpu_to_be32(XLOG_FMT);
1288 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1290 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1291 iclog->ic_state = XLOG_STATE_ACTIVE;
1292 iclog->ic_log = log;
1293 atomic_set(&iclog->ic_refcnt, 0);
1294 spin_lock_init(&iclog->ic_callback_lock);
1295 iclog->ic_callback_tail = &(iclog->ic_callback);
1296 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
1298 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1299 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1300 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1301 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
1303 xlog_trace_iclog_alloc(iclog);
1305 iclogp = &iclog->ic_next;
1307 *iclogp = log->l_iclog; /* complete ring */
1308 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1313 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1314 prev_iclog = iclog->ic_next;
1316 sv_destroy(&iclog->ic_force_wait);
1317 sv_destroy(&iclog->ic_write_wait);
1318 xfs_buf_free(iclog->ic_bp);
1319 xlog_trace_iclog_dealloc(iclog);
1323 spinlock_destroy(&log->l_icloglock);
1324 spinlock_destroy(&log->l_grant_lock);
1325 xlog_trace_loggrant_dealloc(log);
1326 xfs_buf_free(log->l_xbuf);
1330 } /* xlog_alloc_log */
1334 * Write out the commit record of a transaction associated with the given
1335 * ticket. Return the lsn of the commit record.
1338 xlog_commit_record(xfs_mount_t *mp,
1339 xlog_ticket_t *ticket,
1340 xlog_in_core_t **iclog,
1341 xfs_lsn_t *commitlsnp)
1344 xfs_log_iovec_t reg[1];
1346 reg[0].i_addr = NULL;
1348 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_COMMIT);
1350 ASSERT_ALWAYS(iclog);
1351 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1352 iclog, XLOG_COMMIT_TRANS))) {
1353 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1356 } /* xlog_commit_record */
1360 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1361 * log space. This code pushes on the lsn which would supposedly free up
1362 * the 25% which we want to leave free. We may need to adopt a policy which
1363 * pushes on an lsn which is further along in the log once we reach the high
1364 * water mark. In this manner, we would be creating a low water mark.
1367 xlog_grant_push_ail(xfs_mount_t *mp,
1370 xlog_t *log = mp->m_log; /* pointer to the log */
1371 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1372 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1373 int free_blocks; /* free blocks left to write to */
1374 int free_bytes; /* free bytes left to write to */
1375 int threshold_block; /* block in lsn we'd like to be at */
1376 int threshold_cycle; /* lsn cycle we'd like to be at */
1379 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1381 spin_lock(&log->l_grant_lock);
1382 free_bytes = xlog_space_left(log,
1383 log->l_grant_reserve_cycle,
1384 log->l_grant_reserve_bytes);
1385 tail_lsn = log->l_tail_lsn;
1386 free_blocks = BTOBBT(free_bytes);
1389 * Set the threshold for the minimum number of free blocks in the
1390 * log to the maximum of what the caller needs, one quarter of the
1391 * log, and 256 blocks.
1393 free_threshold = BTOBB(need_bytes);
1394 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1395 free_threshold = MAX(free_threshold, 256);
1396 if (free_blocks < free_threshold) {
1397 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1398 threshold_cycle = CYCLE_LSN(tail_lsn);
1399 if (threshold_block >= log->l_logBBsize) {
1400 threshold_block -= log->l_logBBsize;
1401 threshold_cycle += 1;
1403 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
1405 /* Don't pass in an lsn greater than the lsn of the last
1406 * log record known to be on disk.
1408 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1409 threshold_lsn = log->l_last_sync_lsn;
1411 spin_unlock(&log->l_grant_lock);
1414 * Get the transaction layer to kick the dirty buffers out to
1415 * disk asynchronously. No point in trying to do this if
1416 * the filesystem is shutting down.
1418 if (threshold_lsn &&
1419 !XLOG_FORCED_SHUTDOWN(log))
1420 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
1421 } /* xlog_grant_push_ail */
1425 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1426 * fashion. Previously, we should have moved the current iclog
1427 * ptr in the log to point to the next available iclog. This allows further
1428 * write to continue while this code syncs out an iclog ready to go.
1429 * Before an in-core log can be written out, the data section must be scanned
1430 * to save away the 1st word of each BBSIZE block into the header. We replace
1431 * it with the current cycle count. Each BBSIZE block is tagged with the
1432 * cycle count because there in an implicit assumption that drives will
1433 * guarantee that entire 512 byte blocks get written at once. In other words,
1434 * we can't have part of a 512 byte block written and part not written. By
1435 * tagging each block, we will know which blocks are valid when recovering
1436 * after an unclean shutdown.
1438 * This routine is single threaded on the iclog. No other thread can be in
1439 * this routine with the same iclog. Changing contents of iclog can there-
1440 * fore be done without grabbing the state machine lock. Updating the global
1441 * log will require grabbing the lock though.
1443 * The entire log manager uses a logical block numbering scheme. Only
1444 * log_sync (and then only bwrite()) know about the fact that the log may
1445 * not start with block zero on a given device. The log block start offset
1446 * is added immediately before calling bwrite().
1450 xlog_sync(xlog_t *log,
1451 xlog_in_core_t *iclog)
1453 xfs_caddr_t dptr; /* pointer to byte sized element */
1456 uint count; /* byte count of bwrite */
1457 uint count_init; /* initial count before roundup */
1458 int roundoff; /* roundoff to BB or stripe */
1459 int split = 0; /* split write into two regions */
1461 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
1463 XFS_STATS_INC(xs_log_writes);
1464 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1466 /* Add for LR header */
1467 count_init = log->l_iclog_hsize + iclog->ic_offset;
1469 /* Round out the log write size */
1470 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1471 /* we have a v2 stripe unit to use */
1472 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1474 count = BBTOB(BTOBB(count_init));
1476 roundoff = count - count_init;
1477 ASSERT(roundoff >= 0);
1478 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1479 roundoff < log->l_mp->m_sb.sb_logsunit)
1481 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1482 roundoff < BBTOB(1)));
1484 /* move grant heads by roundoff in sync */
1485 spin_lock(&log->l_grant_lock);
1486 xlog_grant_add_space(log, roundoff);
1487 spin_unlock(&log->l_grant_lock);
1489 /* put cycle number in every block */
1490 xlog_pack_data(log, iclog, roundoff);
1492 /* real byte length */
1494 iclog->ic_header.h_len =
1495 cpu_to_be32(iclog->ic_offset + roundoff);
1497 iclog->ic_header.h_len =
1498 cpu_to_be32(iclog->ic_offset);
1502 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1503 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1504 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
1506 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1508 /* Do we need to split this write into 2 parts? */
1509 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1510 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1511 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1512 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1514 iclog->ic_bwritecnt = 1;
1516 XFS_BUF_SET_COUNT(bp, count);
1517 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1518 XFS_BUF_ZEROFLAGS(bp);
1522 * Do an ordered write for the log block.
1523 * Its unnecessary to flush the first split block in the log wrap case.
1525 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
1526 XFS_BUF_ORDERED(bp);
1528 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1529 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1531 xlog_verify_iclog(log, iclog, count, B_TRUE);
1533 /* account for log which doesn't start at block #0 */
1534 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1536 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1541 if ((error = XFS_bwrite(bp))) {
1542 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1547 bp = iclog->ic_log->l_xbuf;
1548 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1550 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1551 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1552 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1553 (__psint_t)count), split);
1554 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1555 XFS_BUF_ZEROFLAGS(bp);
1558 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1559 XFS_BUF_ORDERED(bp);
1560 dptr = XFS_BUF_PTR(bp);
1562 * Bump the cycle numbers at the start of each block
1563 * since this part of the buffer is at the start of
1564 * a new cycle. Watch out for the header magic number
1567 for (i = 0; i < split; i += BBSIZE) {
1568 be32_add_cpu((__be32 *)dptr, 1);
1569 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
1570 be32_add_cpu((__be32 *)dptr, 1);
1574 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1575 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1577 /* account for internal log which doesn't start at block #0 */
1578 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1580 if ((error = XFS_bwrite(bp))) {
1581 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1582 bp, XFS_BUF_ADDR(bp));
1591 * Deallocate a log structure
1594 xlog_dealloc_log(xlog_t *log)
1596 xlog_in_core_t *iclog, *next_iclog;
1599 iclog = log->l_iclog;
1600 for (i=0; i<log->l_iclog_bufs; i++) {
1601 sv_destroy(&iclog->ic_force_wait);
1602 sv_destroy(&iclog->ic_write_wait);
1603 xfs_buf_free(iclog->ic_bp);
1604 xlog_trace_iclog_dealloc(iclog);
1605 next_iclog = iclog->ic_next;
1609 spinlock_destroy(&log->l_icloglock);
1610 spinlock_destroy(&log->l_grant_lock);
1612 xfs_buf_free(log->l_xbuf);
1613 xlog_trace_loggrant_dealloc(log);
1614 log->l_mp->m_log = NULL;
1616 } /* xlog_dealloc_log */
1619 * Update counters atomically now that memcpy is done.
1623 xlog_state_finish_copy(xlog_t *log,
1624 xlog_in_core_t *iclog,
1628 spin_lock(&log->l_icloglock);
1630 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1631 iclog->ic_offset += copy_bytes;
1633 spin_unlock(&log->l_icloglock);
1634 } /* xlog_state_finish_copy */
1640 * print out info relating to regions written which consume
1644 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1647 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1649 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1650 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1671 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1714 xfs_fs_cmn_err(CE_WARN, mp,
1715 "xfs_log_write: reservation summary:\n"
1716 " trans type = %s (%u)\n"
1717 " unit res = %d bytes\n"
1718 " current res = %d bytes\n"
1719 " total reg = %u bytes (o/flow = %u bytes)\n"
1720 " ophdrs = %u (ophdr space = %u bytes)\n"
1721 " ophdr + reg = %u bytes\n"
1722 " num regions = %u\n",
1723 ((ticket->t_trans_type <= 0 ||
1724 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1725 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1726 ticket->t_trans_type,
1729 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1730 ticket->t_res_num_ophdrs, ophdr_spc,
1731 ticket->t_res_arr_sum +
1732 ticket->t_res_o_flow + ophdr_spc,
1735 for (i = 0; i < ticket->t_res_num; i++) {
1736 uint r_type = ticket->t_res_arr[i].r_type;
1738 "region[%u]: %s - %u bytes\n",
1740 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1741 "bad-rtype" : res_type_str[r_type-1]),
1742 ticket->t_res_arr[i].r_len);
1747 * Write some region out to in-core log
1749 * This will be called when writing externally provided regions or when
1750 * writing out a commit record for a given transaction.
1752 * General algorithm:
1753 * 1. Find total length of this write. This may include adding to the
1754 * lengths passed in.
1755 * 2. Check whether we violate the tickets reservation.
1756 * 3. While writing to this iclog
1757 * A. Reserve as much space in this iclog as can get
1758 * B. If this is first write, save away start lsn
1759 * C. While writing this region:
1760 * 1. If first write of transaction, write start record
1761 * 2. Write log operation header (header per region)
1762 * 3. Find out if we can fit entire region into this iclog
1763 * 4. Potentially, verify destination memcpy ptr
1764 * 5. Memcpy (partial) region
1765 * 6. If partial copy, release iclog; otherwise, continue
1766 * copying more regions into current iclog
1767 * 4. Mark want sync bit (in simulation mode)
1768 * 5. Release iclog for potential flush to on-disk log.
1771 * 1. Panic if reservation is overrun. This should never happen since
1772 * reservation amounts are generated internal to the filesystem.
1774 * 1. Tickets are single threaded data structures.
1775 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1776 * syncing routine. When a single log_write region needs to span
1777 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1778 * on all log operation writes which don't contain the end of the
1779 * region. The XLOG_END_TRANS bit is used for the in-core log
1780 * operation which contains the end of the continued log_write region.
1781 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1782 * we don't really know exactly how much space will be used. As a result,
1783 * we don't update ic_offset until the end when we know exactly how many
1784 * bytes have been written out.
1787 xlog_write(xfs_mount_t * mp,
1788 xfs_log_iovec_t reg[],
1790 xfs_log_ticket_t tic,
1791 xfs_lsn_t *start_lsn,
1792 xlog_in_core_t **commit_iclog,
1795 xlog_t *log = mp->m_log;
1796 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1797 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
1798 xlog_op_header_t *logop_head; /* ptr to log operation header */
1799 __psint_t ptr; /* copy address into data region */
1800 int len; /* # xlog_write() bytes 2 still copy */
1801 int index; /* region index currently copying */
1802 int log_offset; /* offset (from 0) into data region */
1803 int start_rec_copy; /* # bytes to copy for start record */
1804 int partial_copy; /* did we split a region? */
1805 int partial_copy_len;/* # bytes copied if split region */
1806 int need_copy; /* # bytes need to memcpy this region */
1807 int copy_len; /* # bytes actually memcpy'ing */
1808 int copy_off; /* # bytes from entry start */
1809 int contwr; /* continued write of in-core log? */
1811 int record_cnt = 0, data_cnt = 0;
1813 partial_copy_len = partial_copy = 0;
1815 /* Calculate potential maximum space. Each region gets its own
1816 * xlog_op_header_t and may need to be double word aligned.
1819 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1820 len += sizeof(xlog_op_header_t);
1821 ticket->t_res_num_ophdrs++;
1824 for (index = 0; index < nentries; index++) {
1825 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1826 ticket->t_res_num_ophdrs++;
1827 len += reg[index].i_len;
1828 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
1830 contwr = *start_lsn = 0;
1832 if (ticket->t_curr_res < len) {
1833 xlog_print_tic_res(mp, ticket);
1836 "xfs_log_write: reservation ran out. Need to up reservation");
1838 /* Customer configurable panic */
1839 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1840 "xfs_log_write: reservation ran out. Need to up reservation");
1841 /* If we did not panic, shutdown the filesystem */
1842 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1845 ticket->t_curr_res -= len;
1847 for (index = 0; index < nentries; ) {
1848 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1849 &contwr, &log_offset)))
1852 ASSERT(log_offset <= iclog->ic_size - 1);
1853 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1855 /* start_lsn is the first lsn written to. That's all we need. */
1857 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
1859 /* This loop writes out as many regions as can fit in the amount
1860 * of space which was allocated by xlog_state_get_iclog_space().
1862 while (index < nentries) {
1863 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1864 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1867 /* If first write for transaction, insert start record.
1868 * We can't be trying to commit if we are inited. We can't
1869 * have any "partial_copy" if we are inited.
1871 if (ticket->t_flags & XLOG_TIC_INITED) {
1872 logop_head = (xlog_op_header_t *)ptr;
1873 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1874 logop_head->oh_clientid = ticket->t_clientid;
1875 logop_head->oh_len = 0;
1876 logop_head->oh_flags = XLOG_START_TRANS;
1877 logop_head->oh_res2 = 0;
1878 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1881 start_rec_copy = sizeof(xlog_op_header_t);
1882 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1885 /* Copy log operation header directly into data section */
1886 logop_head = (xlog_op_header_t *)ptr;
1887 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1888 logop_head->oh_clientid = ticket->t_clientid;
1889 logop_head->oh_res2 = 0;
1891 /* header copied directly */
1892 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1894 /* are we copying a commit or unmount record? */
1895 logop_head->oh_flags = flags;
1898 * We've seen logs corrupted with bad transaction client
1899 * ids. This makes sure that XFS doesn't generate them on.
1900 * Turn this into an EIO and shut down the filesystem.
1902 switch (logop_head->oh_clientid) {
1903 case XFS_TRANSACTION:
1908 xfs_fs_cmn_err(CE_WARN, mp,
1909 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1910 logop_head->oh_clientid, tic);
1911 return XFS_ERROR(EIO);
1914 /* Partial write last time? => (partial_copy != 0)
1915 * need_copy is the amount we'd like to copy if everything could
1916 * fit in the current memcpy.
1918 need_copy = reg[index].i_len - partial_copy_len;
1920 copy_off = partial_copy_len;
1921 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1922 copy_len = need_copy;
1923 logop_head->oh_len = cpu_to_be32(copy_len);
1925 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1926 partial_copy_len = partial_copy = 0;
1927 } else { /* partial write */
1928 copy_len = iclog->ic_size - log_offset;
1929 logop_head->oh_len = cpu_to_be32(copy_len);
1930 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1932 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1933 partial_copy_len += copy_len;
1935 len += sizeof(xlog_op_header_t); /* from splitting of region */
1936 /* account for new log op header */
1937 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1938 ticket->t_res_num_ophdrs++;
1940 xlog_verify_dest_ptr(log, ptr);
1943 ASSERT(copy_len >= 0);
1944 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1945 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1947 /* make copy_len total bytes copied, including headers */
1948 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1950 data_cnt += contwr ? copy_len : 0;
1951 if (partial_copy) { /* copied partial region */
1952 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1953 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1954 record_cnt = data_cnt = 0;
1955 if ((error = xlog_state_release_iclog(log, iclog)))
1957 break; /* don't increment index */
1958 } else { /* copied entire region */
1960 partial_copy_len = partial_copy = 0;
1962 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1963 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1964 record_cnt = data_cnt = 0;
1965 spin_lock(&log->l_icloglock);
1966 xlog_state_want_sync(log, iclog);
1967 spin_unlock(&log->l_icloglock);
1969 ASSERT(flags & XLOG_COMMIT_TRANS);
1970 *commit_iclog = iclog;
1971 } else if ((error = xlog_state_release_iclog(log, iclog)))
1973 if (index == nentries)
1974 return 0; /* we are done */
1978 } /* if (partial_copy) */
1979 } /* while (index < nentries) */
1980 } /* for (index = 0; index < nentries; ) */
1983 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1985 ASSERT(flags & XLOG_COMMIT_TRANS);
1986 *commit_iclog = iclog;
1989 return xlog_state_release_iclog(log, iclog);
1993 /*****************************************************************************
1995 * State Machine functions
1997 *****************************************************************************
2000 /* Clean iclogs starting from the head. This ordering must be
2001 * maintained, so an iclog doesn't become ACTIVE beyond one that
2002 * is SYNCING. This is also required to maintain the notion that we use
2003 * a ordered wait queue to hold off would be writers to the log when every
2004 * iclog is trying to sync to disk.
2006 * State Change: DIRTY -> ACTIVE
2009 xlog_state_clean_log(xlog_t *log)
2011 xlog_in_core_t *iclog;
2014 iclog = log->l_iclog;
2016 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2017 iclog->ic_state = XLOG_STATE_ACTIVE;
2018 iclog->ic_offset = 0;
2019 ASSERT(iclog->ic_callback == NULL);
2021 * If the number of ops in this iclog indicate it just
2022 * contains the dummy transaction, we can
2023 * change state into IDLE (the second time around).
2024 * Otherwise we should change the state into
2026 * We don't need to cover the dummy.
2029 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2034 * We have two dirty iclogs so start over
2035 * This could also be num of ops indicates
2036 * this is not the dummy going out.
2040 iclog->ic_header.h_num_logops = 0;
2041 memset(iclog->ic_header.h_cycle_data, 0,
2042 sizeof(iclog->ic_header.h_cycle_data));
2043 iclog->ic_header.h_lsn = 0;
2044 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2047 break; /* stop cleaning */
2048 iclog = iclog->ic_next;
2049 } while (iclog != log->l_iclog);
2051 /* log is locked when we are called */
2053 * Change state for the dummy log recording.
2054 * We usually go to NEED. But we go to NEED2 if the changed indicates
2055 * we are done writing the dummy record.
2056 * If we are done with the second dummy recored (DONE2), then
2060 switch (log->l_covered_state) {
2061 case XLOG_STATE_COVER_IDLE:
2062 case XLOG_STATE_COVER_NEED:
2063 case XLOG_STATE_COVER_NEED2:
2064 log->l_covered_state = XLOG_STATE_COVER_NEED;
2067 case XLOG_STATE_COVER_DONE:
2069 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2071 log->l_covered_state = XLOG_STATE_COVER_NEED;
2074 case XLOG_STATE_COVER_DONE2:
2076 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2078 log->l_covered_state = XLOG_STATE_COVER_NEED;
2085 } /* xlog_state_clean_log */
2088 xlog_get_lowest_lsn(
2091 xlog_in_core_t *lsn_log;
2092 xfs_lsn_t lowest_lsn, lsn;
2094 lsn_log = log->l_iclog;
2097 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2098 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
2099 if ((lsn && !lowest_lsn) ||
2100 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2104 lsn_log = lsn_log->ic_next;
2105 } while (lsn_log != log->l_iclog);
2111 xlog_state_do_callback(
2114 xlog_in_core_t *ciclog)
2116 xlog_in_core_t *iclog;
2117 xlog_in_core_t *first_iclog; /* used to know when we've
2118 * processed all iclogs once */
2119 xfs_log_callback_t *cb, *cb_next;
2121 xfs_lsn_t lowest_lsn;
2122 int ioerrors; /* counter: iclogs with errors */
2123 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2124 int funcdidcallbacks; /* flag: function did callbacks */
2125 int repeats; /* for issuing console warnings if
2126 * looping too many times */
2129 spin_lock(&log->l_icloglock);
2130 first_iclog = iclog = log->l_iclog;
2132 funcdidcallbacks = 0;
2137 * Scan all iclogs starting with the one pointed to by the
2138 * log. Reset this starting point each time the log is
2139 * unlocked (during callbacks).
2141 * Keep looping through iclogs until one full pass is made
2142 * without running any callbacks.
2144 first_iclog = log->l_iclog;
2145 iclog = log->l_iclog;
2146 loopdidcallbacks = 0;
2151 /* skip all iclogs in the ACTIVE & DIRTY states */
2152 if (iclog->ic_state &
2153 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2154 iclog = iclog->ic_next;
2159 * Between marking a filesystem SHUTDOWN and stopping
2160 * the log, we do flush all iclogs to disk (if there
2161 * wasn't a log I/O error). So, we do want things to
2162 * go smoothly in case of just a SHUTDOWN w/o a
2165 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2167 * Can only perform callbacks in order. Since
2168 * this iclog is not in the DONE_SYNC/
2169 * DO_CALLBACK state, we skip the rest and
2170 * just try to clean up. If we set our iclog
2171 * to DO_CALLBACK, we will not process it when
2172 * we retry since a previous iclog is in the
2173 * CALLBACK and the state cannot change since
2174 * we are holding the l_icloglock.
2176 if (!(iclog->ic_state &
2177 (XLOG_STATE_DONE_SYNC |
2178 XLOG_STATE_DO_CALLBACK))) {
2179 if (ciclog && (ciclog->ic_state ==
2180 XLOG_STATE_DONE_SYNC)) {
2181 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2186 * We now have an iclog that is in either the
2187 * DO_CALLBACK or DONE_SYNC states. The other
2188 * states (WANT_SYNC, SYNCING, or CALLBACK were
2189 * caught by the above if and are going to
2190 * clean (i.e. we aren't doing their callbacks)
2195 * We will do one more check here to see if we
2196 * have chased our tail around.
2199 lowest_lsn = xlog_get_lowest_lsn(log);
2201 XFS_LSN_CMP(lowest_lsn,
2202 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
2203 iclog = iclog->ic_next;
2204 continue; /* Leave this iclog for
2208 iclog->ic_state = XLOG_STATE_CALLBACK;
2210 spin_unlock(&log->l_icloglock);
2212 /* l_last_sync_lsn field protected by
2213 * l_grant_lock. Don't worry about iclog's lsn.
2214 * No one else can be here except us.
2216 spin_lock(&log->l_grant_lock);
2217 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2218 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2219 log->l_last_sync_lsn =
2220 be64_to_cpu(iclog->ic_header.h_lsn);
2221 spin_unlock(&log->l_grant_lock);
2224 spin_unlock(&log->l_icloglock);
2229 * Keep processing entries in the callback list until
2230 * we come around and it is empty. We need to
2231 * atomically see that the list is empty and change the
2232 * state to DIRTY so that we don't miss any more
2233 * callbacks being added.
2235 spin_lock(&iclog->ic_callback_lock);
2236 cb = iclog->ic_callback;
2238 iclog->ic_callback_tail = &(iclog->ic_callback);
2239 iclog->ic_callback = NULL;
2240 spin_unlock(&iclog->ic_callback_lock);
2242 /* perform callbacks in the order given */
2243 for (; cb; cb = cb_next) {
2244 cb_next = cb->cb_next;
2245 cb->cb_func(cb->cb_arg, aborted);
2247 spin_lock(&iclog->ic_callback_lock);
2248 cb = iclog->ic_callback;
2254 spin_lock(&log->l_icloglock);
2255 ASSERT(iclog->ic_callback == NULL);
2256 spin_unlock(&iclog->ic_callback_lock);
2257 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2258 iclog->ic_state = XLOG_STATE_DIRTY;
2261 * Transition from DIRTY to ACTIVE if applicable.
2262 * NOP if STATE_IOERROR.
2264 xlog_state_clean_log(log);
2266 /* wake up threads waiting in xfs_log_force() */
2267 sv_broadcast(&iclog->ic_force_wait);
2269 iclog = iclog->ic_next;
2270 } while (first_iclog != iclog);
2272 if (repeats > 5000) {
2273 flushcnt += repeats;
2275 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2276 "%s: possible infinite loop (%d iterations)",
2277 __func__, flushcnt);
2279 } while (!ioerrors && loopdidcallbacks);
2282 * make one last gasp attempt to see if iclogs are being left in
2286 if (funcdidcallbacks) {
2287 first_iclog = iclog = log->l_iclog;
2289 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2291 * Terminate the loop if iclogs are found in states
2292 * which will cause other threads to clean up iclogs.
2294 * SYNCING - i/o completion will go through logs
2295 * DONE_SYNC - interrupt thread should be waiting for
2297 * IOERROR - give up hope all ye who enter here
2299 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2300 iclog->ic_state == XLOG_STATE_SYNCING ||
2301 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2302 iclog->ic_state == XLOG_STATE_IOERROR )
2304 iclog = iclog->ic_next;
2305 } while (first_iclog != iclog);
2309 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2311 spin_unlock(&log->l_icloglock);
2314 sv_broadcast(&log->l_flush_wait);
2319 * Finish transitioning this iclog to the dirty state.
2321 * Make sure that we completely execute this routine only when this is
2322 * the last call to the iclog. There is a good chance that iclog flushes,
2323 * when we reach the end of the physical log, get turned into 2 separate
2324 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2325 * routine. By using the reference count bwritecnt, we guarantee that only
2326 * the second completion goes through.
2328 * Callbacks could take time, so they are done outside the scope of the
2329 * global state machine log lock.
2332 xlog_state_done_syncing(
2333 xlog_in_core_t *iclog,
2336 xlog_t *log = iclog->ic_log;
2338 spin_lock(&log->l_icloglock);
2340 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2341 iclog->ic_state == XLOG_STATE_IOERROR);
2342 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2343 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2347 * If we got an error, either on the first buffer, or in the case of
2348 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2349 * and none should ever be attempted to be written to disk
2352 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2353 if (--iclog->ic_bwritecnt == 1) {
2354 spin_unlock(&log->l_icloglock);
2357 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2361 * Someone could be sleeping prior to writing out the next
2362 * iclog buffer, we wake them all, one will get to do the
2363 * I/O, the others get to wait for the result.
2365 sv_broadcast(&iclog->ic_write_wait);
2366 spin_unlock(&log->l_icloglock);
2367 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2368 } /* xlog_state_done_syncing */
2372 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2373 * sleep. We wait on the flush queue on the head iclog as that should be
2374 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2375 * we will wait here and all new writes will sleep until a sync completes.
2377 * The in-core logs are used in a circular fashion. They are not used
2378 * out-of-order even when an iclog past the head is free.
2381 * * log_offset where xlog_write() can start writing into the in-core
2383 * * in-core log pointer to which xlog_write() should write.
2384 * * boolean indicating this is a continued write to an in-core log.
2385 * If this is the last write, then the in-core log's offset field
2386 * needs to be incremented, depending on the amount of data which
2390 xlog_state_get_iclog_space(xlog_t *log,
2392 xlog_in_core_t **iclogp,
2393 xlog_ticket_t *ticket,
2394 int *continued_write,
2398 xlog_rec_header_t *head;
2399 xlog_in_core_t *iclog;
2403 spin_lock(&log->l_icloglock);
2404 if (XLOG_FORCED_SHUTDOWN(log)) {
2405 spin_unlock(&log->l_icloglock);
2406 return XFS_ERROR(EIO);
2409 iclog = log->l_iclog;
2410 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2411 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2412 XFS_STATS_INC(xs_log_noiclogs);
2414 /* Wait for log writes to have flushed */
2415 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
2419 head = &iclog->ic_header;
2421 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
2422 log_offset = iclog->ic_offset;
2424 /* On the 1st write to an iclog, figure out lsn. This works
2425 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2426 * committing to. If the offset is set, that's how many blocks
2429 if (log_offset == 0) {
2430 ticket->t_curr_res -= log->l_iclog_hsize;
2431 xlog_tic_add_region(ticket,
2433 XLOG_REG_TYPE_LRHEADER);
2434 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2435 head->h_lsn = cpu_to_be64(
2436 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
2437 ASSERT(log->l_curr_block >= 0);
2440 /* If there is enough room to write everything, then do it. Otherwise,
2441 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2442 * bit is on, so this will get flushed out. Don't update ic_offset
2443 * until you know exactly how many bytes get copied. Therefore, wait
2444 * until later to update ic_offset.
2446 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2447 * can fit into remaining data section.
2449 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2450 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2453 * If I'm the only one writing to this iclog, sync it to disk.
2454 * We need to do an atomic compare and decrement here to avoid
2455 * racing with concurrent atomic_dec_and_lock() calls in
2456 * xlog_state_release_iclog() when there is more than one
2457 * reference to the iclog.
2459 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2460 /* we are the only one */
2461 spin_unlock(&log->l_icloglock);
2462 error = xlog_state_release_iclog(log, iclog);
2466 spin_unlock(&log->l_icloglock);
2471 /* Do we have enough room to write the full amount in the remainder
2472 * of this iclog? Or must we continue a write on the next iclog and
2473 * mark this iclog as completely taken? In the case where we switch
2474 * iclogs (to mark it taken), this particular iclog will release/sync
2475 * to disk in xlog_write().
2477 if (len <= iclog->ic_size - iclog->ic_offset) {
2478 *continued_write = 0;
2479 iclog->ic_offset += len;
2481 *continued_write = 1;
2482 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2486 ASSERT(iclog->ic_offset <= iclog->ic_size);
2487 spin_unlock(&log->l_icloglock);
2489 *logoffsetp = log_offset;
2491 } /* xlog_state_get_iclog_space */
2494 * Atomically get the log space required for a log ticket.
2496 * Once a ticket gets put onto the reserveq, it will only return after
2497 * the needed reservation is satisfied.
2500 xlog_grant_log_space(xlog_t *log,
2511 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2512 panic("grant Recovery problem");
2515 /* Is there space or do we need to sleep? */
2516 spin_lock(&log->l_grant_lock);
2517 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2519 /* something is already sleeping; insert new transaction at end */
2520 if (log->l_reserve_headq) {
2521 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2522 xlog_trace_loggrant(log, tic,
2523 "xlog_grant_log_space: sleep 1");
2525 * Gotta check this before going to sleep, while we're
2526 * holding the grant lock.
2528 if (XLOG_FORCED_SHUTDOWN(log))
2531 XFS_STATS_INC(xs_sleep_logspace);
2532 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2534 * If we got an error, and the filesystem is shutting down,
2535 * we'll catch it down below. So just continue...
2537 xlog_trace_loggrant(log, tic,
2538 "xlog_grant_log_space: wake 1");
2539 spin_lock(&log->l_grant_lock);
2541 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2542 need_bytes = tic->t_unit_res*tic->t_ocnt;
2544 need_bytes = tic->t_unit_res;
2547 if (XLOG_FORCED_SHUTDOWN(log))
2550 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2551 log->l_grant_reserve_bytes);
2552 if (free_bytes < need_bytes) {
2553 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2554 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2555 xlog_trace_loggrant(log, tic,
2556 "xlog_grant_log_space: sleep 2");
2557 XFS_STATS_INC(xs_sleep_logspace);
2558 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2560 if (XLOG_FORCED_SHUTDOWN(log)) {
2561 spin_lock(&log->l_grant_lock);
2565 xlog_trace_loggrant(log, tic,
2566 "xlog_grant_log_space: wake 2");
2567 xlog_grant_push_ail(log->l_mp, need_bytes);
2568 spin_lock(&log->l_grant_lock);
2570 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2571 xlog_del_ticketq(&log->l_reserve_headq, tic);
2573 /* we've got enough space */
2574 xlog_grant_add_space(log, need_bytes);
2576 tail_lsn = log->l_tail_lsn;
2578 * Check to make sure the grant write head didn't just over lap the
2579 * tail. If the cycles are the same, we can't be overlapping.
2580 * Otherwise, make sure that the cycles differ by exactly one and
2581 * check the byte count.
2583 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2584 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2585 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2588 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2589 xlog_verify_grant_head(log, 1);
2590 spin_unlock(&log->l_grant_lock);
2594 if (tic->t_flags & XLOG_TIC_IN_Q)
2595 xlog_del_ticketq(&log->l_reserve_headq, tic);
2596 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2598 * If we are failing, make sure the ticket doesn't have any
2599 * current reservations. We don't want to add this back when
2600 * the ticket/transaction gets cancelled.
2602 tic->t_curr_res = 0;
2603 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2604 spin_unlock(&log->l_grant_lock);
2605 return XFS_ERROR(EIO);
2606 } /* xlog_grant_log_space */
2610 * Replenish the byte reservation required by moving the grant write head.
2615 xlog_regrant_write_log_space(xlog_t *log,
2618 int free_bytes, need_bytes;
2619 xlog_ticket_t *ntic;
2624 tic->t_curr_res = tic->t_unit_res;
2625 xlog_tic_reset_res(tic);
2631 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2632 panic("regrant Recovery problem");
2635 spin_lock(&log->l_grant_lock);
2636 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2638 if (XLOG_FORCED_SHUTDOWN(log))
2641 /* If there are other waiters on the queue then give them a
2642 * chance at logspace before us. Wake up the first waiters,
2643 * if we do not wake up all the waiters then go to sleep waiting
2644 * for more free space, otherwise try to get some space for
2648 if ((ntic = log->l_write_headq)) {
2649 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2650 log->l_grant_write_bytes);
2652 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2654 if (free_bytes < ntic->t_unit_res)
2656 free_bytes -= ntic->t_unit_res;
2657 sv_signal(&ntic->t_wait);
2658 ntic = ntic->t_next;
2659 } while (ntic != log->l_write_headq);
2661 if (ntic != log->l_write_headq) {
2662 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2663 xlog_ins_ticketq(&log->l_write_headq, tic);
2665 xlog_trace_loggrant(log, tic,
2666 "xlog_regrant_write_log_space: sleep 1");
2667 XFS_STATS_INC(xs_sleep_logspace);
2668 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
2669 &log->l_grant_lock, s);
2671 /* If we're shutting down, this tic is already
2673 if (XLOG_FORCED_SHUTDOWN(log)) {
2674 spin_lock(&log->l_grant_lock);
2678 xlog_trace_loggrant(log, tic,
2679 "xlog_regrant_write_log_space: wake 1");
2680 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2681 spin_lock(&log->l_grant_lock);
2685 need_bytes = tic->t_unit_res;
2688 if (XLOG_FORCED_SHUTDOWN(log))
2691 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2692 log->l_grant_write_bytes);
2693 if (free_bytes < need_bytes) {
2694 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2695 xlog_ins_ticketq(&log->l_write_headq, tic);
2696 XFS_STATS_INC(xs_sleep_logspace);
2697 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2699 /* If we're shutting down, this tic is already off the queue */
2700 if (XLOG_FORCED_SHUTDOWN(log)) {
2701 spin_lock(&log->l_grant_lock);
2705 xlog_trace_loggrant(log, tic,
2706 "xlog_regrant_write_log_space: wake 2");
2707 xlog_grant_push_ail(log->l_mp, need_bytes);
2708 spin_lock(&log->l_grant_lock);
2710 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2711 xlog_del_ticketq(&log->l_write_headq, tic);
2713 /* we've got enough space */
2714 xlog_grant_add_space_write(log, need_bytes);
2716 tail_lsn = log->l_tail_lsn;
2717 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2718 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2719 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2723 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2724 xlog_verify_grant_head(log, 1);
2725 spin_unlock(&log->l_grant_lock);
2730 if (tic->t_flags & XLOG_TIC_IN_Q)
2731 xlog_del_ticketq(&log->l_reserve_headq, tic);
2732 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2734 * If we are failing, make sure the ticket doesn't have any
2735 * current reservations. We don't want to add this back when
2736 * the ticket/transaction gets cancelled.
2738 tic->t_curr_res = 0;
2739 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2740 spin_unlock(&log->l_grant_lock);
2741 return XFS_ERROR(EIO);
2742 } /* xlog_regrant_write_log_space */
2745 /* The first cnt-1 times through here we don't need to
2746 * move the grant write head because the permanent
2747 * reservation has reserved cnt times the unit amount.
2748 * Release part of current permanent unit reservation and
2749 * reset current reservation to be one units worth. Also
2750 * move grant reservation head forward.
2753 xlog_regrant_reserve_log_space(xlog_t *log,
2754 xlog_ticket_t *ticket)
2756 xlog_trace_loggrant(log, ticket,
2757 "xlog_regrant_reserve_log_space: enter");
2758 if (ticket->t_cnt > 0)
2761 spin_lock(&log->l_grant_lock);
2762 xlog_grant_sub_space(log, ticket->t_curr_res);
2763 ticket->t_curr_res = ticket->t_unit_res;
2764 xlog_tic_reset_res(ticket);
2765 xlog_trace_loggrant(log, ticket,
2766 "xlog_regrant_reserve_log_space: sub current res");
2767 xlog_verify_grant_head(log, 1);
2769 /* just return if we still have some of the pre-reserved space */
2770 if (ticket->t_cnt > 0) {
2771 spin_unlock(&log->l_grant_lock);
2775 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
2776 xlog_trace_loggrant(log, ticket,
2777 "xlog_regrant_reserve_log_space: exit");
2778 xlog_verify_grant_head(log, 0);
2779 spin_unlock(&log->l_grant_lock);
2780 ticket->t_curr_res = ticket->t_unit_res;
2781 xlog_tic_reset_res(ticket);
2782 } /* xlog_regrant_reserve_log_space */
2786 * Give back the space left from a reservation.
2788 * All the information we need to make a correct determination of space left
2789 * is present. For non-permanent reservations, things are quite easy. The
2790 * count should have been decremented to zero. We only need to deal with the
2791 * space remaining in the current reservation part of the ticket. If the
2792 * ticket contains a permanent reservation, there may be left over space which
2793 * needs to be released. A count of N means that N-1 refills of the current
2794 * reservation can be done before we need to ask for more space. The first
2795 * one goes to fill up the first current reservation. Once we run out of
2796 * space, the count will stay at zero and the only space remaining will be
2797 * in the current reservation field.
2800 xlog_ungrant_log_space(xlog_t *log,
2801 xlog_ticket_t *ticket)
2803 if (ticket->t_cnt > 0)
2806 spin_lock(&log->l_grant_lock);
2807 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2809 xlog_grant_sub_space(log, ticket->t_curr_res);
2811 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2813 /* If this is a permanent reservation ticket, we may be able to free
2814 * up more space based on the remaining count.
2816 if (ticket->t_cnt > 0) {
2817 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2818 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
2821 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2822 xlog_verify_grant_head(log, 1);
2823 spin_unlock(&log->l_grant_lock);
2824 xfs_log_move_tail(log->l_mp, 1);
2825 } /* xlog_ungrant_log_space */
2829 * Flush iclog to disk if this is the last reference to the given iclog and
2830 * the WANT_SYNC bit is set.
2832 * When this function is entered, the iclog is not necessarily in the
2833 * WANT_SYNC state. It may be sitting around waiting to get filled.
2838 xlog_state_release_iclog(
2840 xlog_in_core_t *iclog)
2842 int sync = 0; /* do we sync? */
2844 if (iclog->ic_state & XLOG_STATE_IOERROR)
2845 return XFS_ERROR(EIO);
2847 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2848 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2851 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2852 spin_unlock(&log->l_icloglock);
2853 return XFS_ERROR(EIO);
2855 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2856 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2858 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2859 /* update tail before writing to iclog */
2860 xlog_assign_tail_lsn(log->l_mp);
2862 iclog->ic_state = XLOG_STATE_SYNCING;
2863 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
2864 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2865 /* cycle incremented when incrementing curr_block */
2867 spin_unlock(&log->l_icloglock);
2870 * We let the log lock go, so it's possible that we hit a log I/O
2871 * error or some other SHUTDOWN condition that marks the iclog
2872 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2873 * this iclog has consistent data, so we ignore IOERROR
2874 * flags after this point.
2877 return xlog_sync(log, iclog);
2879 } /* xlog_state_release_iclog */
2883 * This routine will mark the current iclog in the ring as WANT_SYNC
2884 * and move the current iclog pointer to the next iclog in the ring.
2885 * When this routine is called from xlog_state_get_iclog_space(), the
2886 * exact size of the iclog has not yet been determined. All we know is
2887 * that every data block. We have run out of space in this log record.
2890 xlog_state_switch_iclogs(xlog_t *log,
2891 xlog_in_core_t *iclog,
2894 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2896 eventual_size = iclog->ic_offset;
2897 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2898 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
2899 log->l_prev_block = log->l_curr_block;
2900 log->l_prev_cycle = log->l_curr_cycle;
2902 /* roll log?: ic_offset changed later */
2903 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2905 /* Round up to next log-sunit */
2906 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
2907 log->l_mp->m_sb.sb_logsunit > 1) {
2908 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2909 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2912 if (log->l_curr_block >= log->l_logBBsize) {
2913 log->l_curr_cycle++;
2914 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2915 log->l_curr_cycle++;
2916 log->l_curr_block -= log->l_logBBsize;
2917 ASSERT(log->l_curr_block >= 0);
2919 ASSERT(iclog == log->l_iclog);
2920 log->l_iclog = iclog->ic_next;
2921 } /* xlog_state_switch_iclogs */
2925 * Write out all data in the in-core log as of this exact moment in time.
2927 * Data may be written to the in-core log during this call. However,
2928 * we don't guarantee this data will be written out. A change from past
2929 * implementation means this routine will *not* write out zero length LRs.
2931 * Basically, we try and perform an intelligent scan of the in-core logs.
2932 * If we determine there is no flushable data, we just return. There is no
2933 * flushable data if:
2935 * 1. the current iclog is active and has no data; the previous iclog
2936 * is in the active or dirty state.
2937 * 2. the current iclog is drity, and the previous iclog is in the
2938 * active or dirty state.
2942 * 1. the current iclog is not in the active nor dirty state.
2943 * 2. the current iclog dirty, and the previous iclog is not in the
2944 * active nor dirty state.
2945 * 3. the current iclog is active, and there is another thread writing
2946 * to this particular iclog.
2947 * 4. a) the current iclog is active and has no other writers
2948 * b) when we return from flushing out this iclog, it is still
2949 * not in the active nor dirty state.
2952 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2954 xlog_in_core_t *iclog;
2957 spin_lock(&log->l_icloglock);
2959 iclog = log->l_iclog;
2960 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2961 spin_unlock(&log->l_icloglock);
2962 return XFS_ERROR(EIO);
2965 /* If the head iclog is not active nor dirty, we just attach
2966 * ourselves to the head and go to sleep.
2968 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2969 iclog->ic_state == XLOG_STATE_DIRTY) {
2971 * If the head is dirty or (active and empty), then
2972 * we need to look at the previous iclog. If the previous
2973 * iclog is active or dirty we are done. There is nothing
2974 * to sync out. Otherwise, we attach ourselves to the
2975 * previous iclog and go to sleep.
2977 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2978 (atomic_read(&iclog->ic_refcnt) == 0
2979 && iclog->ic_offset == 0)) {
2980 iclog = iclog->ic_prev;
2981 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2982 iclog->ic_state == XLOG_STATE_DIRTY)
2987 if (atomic_read(&iclog->ic_refcnt) == 0) {
2988 /* We are the only one with access to this
2989 * iclog. Flush it out now. There should
2990 * be a roundoff of zero to show that someone
2991 * has already taken care of the roundoff from
2992 * the previous sync.
2994 atomic_inc(&iclog->ic_refcnt);
2995 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2996 xlog_state_switch_iclogs(log, iclog, 0);
2997 spin_unlock(&log->l_icloglock);
2999 if (xlog_state_release_iclog(log, iclog))
3000 return XFS_ERROR(EIO);
3002 spin_lock(&log->l_icloglock);
3003 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
3004 iclog->ic_state != XLOG_STATE_DIRTY)
3009 /* Someone else is writing to this iclog.
3010 * Use its call to flush out the data. However,
3011 * the other thread may not force out this LR,
3012 * so we mark it WANT_SYNC.
3014 xlog_state_switch_iclogs(log, iclog, 0);
3020 /* By the time we come around again, the iclog could've been filled
3021 * which would give it another lsn. If we have a new lsn, just
3022 * return because the relevant data has been flushed.
3025 if (flags & XFS_LOG_SYNC) {
3027 * We must check if we're shutting down here, before
3028 * we wait, while we're holding the l_icloglock.
3029 * Then we check again after waking up, in case our
3030 * sleep was disturbed by a bad news.
3032 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3033 spin_unlock(&log->l_icloglock);
3034 return XFS_ERROR(EIO);
3036 XFS_STATS_INC(xs_log_force_sleep);
3037 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
3039 * No need to grab the log lock here since we're
3040 * only deciding whether or not to return EIO
3041 * and the memory read should be atomic.
3043 if (iclog->ic_state & XLOG_STATE_IOERROR)
3044 return XFS_ERROR(EIO);
3050 spin_unlock(&log->l_icloglock);
3053 } /* xlog_state_sync_all */
3057 * Used by code which implements synchronous log forces.
3059 * Find in-core log with lsn.
3060 * If it is in the DIRTY state, just return.
3061 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3062 * state and go to sleep or return.
3063 * If it is in any other state, go to sleep or return.
3065 * If filesystem activity goes to zero, the iclog will get flushed only by
3069 xlog_state_sync(xlog_t *log,
3074 xlog_in_core_t *iclog;
3075 int already_slept = 0;
3078 spin_lock(&log->l_icloglock);
3079 iclog = log->l_iclog;
3081 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3082 spin_unlock(&log->l_icloglock);
3083 return XFS_ERROR(EIO);
3087 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3088 iclog = iclog->ic_next;
3092 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3093 spin_unlock(&log->l_icloglock);
3097 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3099 * We sleep here if we haven't already slept (e.g.
3100 * this is the first time we've looked at the correct
3101 * iclog buf) and the buffer before us is going to
3102 * be sync'ed. The reason for this is that if we
3103 * are doing sync transactions here, by waiting for
3104 * the previous I/O to complete, we can allow a few
3105 * more transactions into this iclog before we close
3108 * Otherwise, we mark the buffer WANT_SYNC, and bump
3109 * up the refcnt so we can release the log (which drops
3110 * the ref count). The state switch keeps new transaction
3111 * commits from using this buffer. When the current commits
3112 * finish writing into the buffer, the refcount will drop to
3113 * zero and the buffer will go out then.
3115 if (!already_slept &&
3116 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3117 XLOG_STATE_SYNCING))) {
3118 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3119 XFS_STATS_INC(xs_log_force_sleep);
3120 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
3121 &log->l_icloglock, s);
3126 atomic_inc(&iclog->ic_refcnt);
3127 xlog_state_switch_iclogs(log, iclog, 0);
3128 spin_unlock(&log->l_icloglock);
3129 if (xlog_state_release_iclog(log, iclog))
3130 return XFS_ERROR(EIO);
3132 spin_lock(&log->l_icloglock);
3136 if ((flags & XFS_LOG_SYNC) && /* sleep */
3137 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3140 * Don't wait on completion if we know that we've
3141 * gotten a log write error.
3143 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3144 spin_unlock(&log->l_icloglock);
3145 return XFS_ERROR(EIO);
3147 XFS_STATS_INC(xs_log_force_sleep);
3148 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3150 * No need to grab the log lock here since we're
3151 * only deciding whether or not to return EIO
3152 * and the memory read should be atomic.
3154 if (iclog->ic_state & XLOG_STATE_IOERROR)
3155 return XFS_ERROR(EIO);
3157 } else { /* just return */
3158 spin_unlock(&log->l_icloglock);
3162 } while (iclog != log->l_iclog);
3164 spin_unlock(&log->l_icloglock);
3166 } /* xlog_state_sync */
3170 * Called when we want to mark the current iclog as being ready to sync to
3174 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3176 ASSERT(spin_is_locked(&log->l_icloglock));
3178 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3179 xlog_state_switch_iclogs(log, iclog, 0);
3181 ASSERT(iclog->ic_state &
3182 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3187 /*****************************************************************************
3191 *****************************************************************************
3195 * Free a used ticket when it's refcount falls to zero.
3199 xlog_ticket_t *ticket)
3201 ASSERT(atomic_read(&ticket->t_ref) > 0);
3202 if (atomic_dec_and_test(&ticket->t_ref)) {
3203 sv_destroy(&ticket->t_wait);
3204 kmem_zone_free(xfs_log_ticket_zone, ticket);
3210 xlog_ticket_t *ticket)
3212 ASSERT(atomic_read(&ticket->t_ref) > 0);
3213 atomic_inc(&ticket->t_ref);
3218 * Allocate and initialise a new log ticket.
3220 STATIC xlog_ticket_t *
3221 xlog_ticket_alloc(xlog_t *log,
3230 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3235 * Permanent reservations have up to 'cnt'-1 active log operations
3236 * in the log. A unit in this case is the amount of space for one
3237 * of these log operations. Normal reservations have a cnt of 1
3238 * and their unit amount is the total amount of space required.
3240 * The following lines of code account for non-transaction data
3241 * which occupy space in the on-disk log.
3243 * Normal form of a transaction is:
3244 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3245 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3247 * We need to account for all the leadup data and trailer data
3248 * around the transaction data.
3249 * And then we need to account for the worst case in terms of using
3251 * The worst case will happen if:
3252 * - the placement of the transaction happens to be such that the
3253 * roundoff is at its maximum
3254 * - the transaction data is synced before the commit record is synced
3255 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3256 * Therefore the commit record is in its own Log Record.
3257 * This can happen as the commit record is called with its
3258 * own region to xlog_write().
3259 * This then means that in the worst case, roundoff can happen for
3260 * the commit-rec as well.
3261 * The commit-rec is smaller than padding in this scenario and so it is
3262 * not added separately.
3265 /* for trans header */
3266 unit_bytes += sizeof(xlog_op_header_t);
3267 unit_bytes += sizeof(xfs_trans_header_t);
3270 unit_bytes += sizeof(xlog_op_header_t);
3272 /* for LR headers */
3273 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3274 unit_bytes += log->l_iclog_hsize * num_headers;
3276 /* for commit-rec LR header - note: padding will subsume the ophdr */
3277 unit_bytes += log->l_iclog_hsize;
3279 /* for split-recs - ophdrs added when data split over LRs */
3280 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3282 /* for roundoff padding for transaction data and one for commit record */
3283 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
3284 log->l_mp->m_sb.sb_logsunit > 1) {
3285 /* log su roundoff */
3286 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3289 unit_bytes += 2*BBSIZE;
3292 atomic_set(&tic->t_ref, 1);
3293 tic->t_unit_res = unit_bytes;
3294 tic->t_curr_res = unit_bytes;
3297 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3298 tic->t_clientid = client;
3299 tic->t_flags = XLOG_TIC_INITED;
3300 tic->t_trans_type = 0;
3301 if (xflags & XFS_LOG_PERM_RESERV)
3302 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3303 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
3305 xlog_tic_reset_res(tic);
3311 /******************************************************************************
3313 * Log debug routines
3315 ******************************************************************************
3319 * Make sure that the destination ptr is within the valid data region of
3320 * one of the iclogs. This uses backup pointers stored in a different
3321 * part of the log in case we trash the log structure.
3324 xlog_verify_dest_ptr(xlog_t *log,
3330 for (i=0; i < log->l_iclog_bufs; i++) {
3331 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3332 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3336 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3337 } /* xlog_verify_dest_ptr */
3340 xlog_verify_grant_head(xlog_t *log, int equals)
3342 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3344 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3346 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3348 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3349 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3351 } /* xlog_verify_grant_head */
3353 /* check if it will fit */
3355 xlog_verify_tail_lsn(xlog_t *log,
3356 xlog_in_core_t *iclog,
3361 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3363 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3364 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3365 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3367 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3369 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3370 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3372 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3373 if (blocks < BTOBB(iclog->ic_offset) + 1)
3374 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3376 } /* xlog_verify_tail_lsn */
3379 * Perform a number of checks on the iclog before writing to disk.
3381 * 1. Make sure the iclogs are still circular
3382 * 2. Make sure we have a good magic number
3383 * 3. Make sure we don't have magic numbers in the data
3384 * 4. Check fields of each log operation header for:
3385 * A. Valid client identifier
3386 * B. tid ptr value falls in valid ptr space (user space code)
3387 * C. Length in log record header is correct according to the
3388 * individual operation headers within record.
3389 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3390 * log, check the preceding blocks of the physical log to make sure all
3391 * the cycle numbers agree with the current cycle number.
3394 xlog_verify_iclog(xlog_t *log,
3395 xlog_in_core_t *iclog,
3399 xlog_op_header_t *ophead;
3400 xlog_in_core_t *icptr;
3401 xlog_in_core_2_t *xhdr;
3403 xfs_caddr_t base_ptr;
3404 __psint_t field_offset;
3406 int len, i, j, k, op_len;
3409 /* check validity of iclog pointers */
3410 spin_lock(&log->l_icloglock);
3411 icptr = log->l_iclog;
3412 for (i=0; i < log->l_iclog_bufs; i++) {
3414 xlog_panic("xlog_verify_iclog: invalid ptr");
3415 icptr = icptr->ic_next;
3417 if (icptr != log->l_iclog)
3418 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3419 spin_unlock(&log->l_icloglock);
3421 /* check log magic numbers */
3422 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
3423 xlog_panic("xlog_verify_iclog: invalid magic num");
3425 ptr = (xfs_caddr_t) &iclog->ic_header;
3426 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
3428 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
3429 xlog_panic("xlog_verify_iclog: unexpected magic num");
3433 len = be32_to_cpu(iclog->ic_header.h_num_logops);
3434 ptr = iclog->ic_datap;
3436 ophead = (xlog_op_header_t *)ptr;
3437 xhdr = iclog->ic_data;
3438 for (i = 0; i < len; i++) {
3439 ophead = (xlog_op_header_t *)ptr;
3441 /* clientid is only 1 byte */
3442 field_offset = (__psint_t)
3443 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3444 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3445 clientid = ophead->oh_clientid;
3447 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3448 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3449 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3450 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3451 clientid = xlog_get_client_id(
3452 xhdr[j].hic_xheader.xh_cycle_data[k]);
3454 clientid = xlog_get_client_id(
3455 iclog->ic_header.h_cycle_data[idx]);
3458 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3459 cmn_err(CE_WARN, "xlog_verify_iclog: "
3460 "invalid clientid %d op 0x%p offset 0x%lx",
3461 clientid, ophead, (unsigned long)field_offset);
3464 field_offset = (__psint_t)
3465 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3466 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3467 op_len = be32_to_cpu(ophead->oh_len);
3469 idx = BTOBBT((__psint_t)&ophead->oh_len -
3470 (__psint_t)iclog->ic_datap);
3471 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3472 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3473 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3474 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
3476 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
3479 ptr += sizeof(xlog_op_header_t) + op_len;
3481 } /* xlog_verify_iclog */
3485 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3491 xlog_in_core_t *iclog, *ic;
3493 iclog = log->l_iclog;
3494 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3496 * Mark all the incore logs IOERROR.
3497 * From now on, no log flushes will result.
3501 ic->ic_state = XLOG_STATE_IOERROR;
3503 } while (ic != iclog);
3507 * Return non-zero, if state transition has already happened.
3513 * This is called from xfs_force_shutdown, when we're forcibly
3514 * shutting down the filesystem, typically because of an IO error.
3515 * Our main objectives here are to make sure that:
3516 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3517 * parties to find out, 'atomically'.
3518 * b. those who're sleeping on log reservations, pinned objects and
3519 * other resources get woken up, and be told the bad news.
3520 * c. nothing new gets queued up after (a) and (b) are done.
3521 * d. if !logerror, flush the iclogs to disk, then seal them off
3525 xfs_log_force_umount(
3526 struct xfs_mount *mp,
3537 * If this happens during log recovery, don't worry about
3538 * locking; the log isn't open for business yet.
3541 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3542 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3544 XFS_BUF_DONE(mp->m_sb_bp);
3549 * Somebody could've already done the hard work for us.
3550 * No need to get locks for this.
3552 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3553 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3558 * We must hold both the GRANT lock and the LOG lock,
3559 * before we mark the filesystem SHUTDOWN and wake
3560 * everybody up to tell the bad news.
3562 spin_lock(&log->l_icloglock);
3563 spin_lock(&log->l_grant_lock);
3564 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3566 XFS_BUF_DONE(mp->m_sb_bp);
3569 * This flag is sort of redundant because of the mount flag, but
3570 * it's good to maintain the separation between the log and the rest
3573 log->l_flags |= XLOG_IO_ERROR;
3576 * If we hit a log error, we want to mark all the iclogs IOERROR
3577 * while we're still holding the loglock.
3580 retval = xlog_state_ioerror(log);
3581 spin_unlock(&log->l_icloglock);
3584 * We don't want anybody waiting for log reservations
3585 * after this. That means we have to wake up everybody
3586 * queued up on reserve_headq as well as write_headq.
3587 * In addition, we make sure in xlog_{re}grant_log_space
3588 * that we don't enqueue anything once the SHUTDOWN flag
3589 * is set, and this action is protected by the GRANTLOCK.
3591 if ((tic = log->l_reserve_headq)) {
3593 sv_signal(&tic->t_wait);
3595 } while (tic != log->l_reserve_headq);
3598 if ((tic = log->l_write_headq)) {
3600 sv_signal(&tic->t_wait);
3602 } while (tic != log->l_write_headq);
3604 spin_unlock(&log->l_grant_lock);
3606 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3609 * Force the incore logs to disk before shutting the
3610 * log down completely.
3612 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3613 spin_lock(&log->l_icloglock);
3614 retval = xlog_state_ioerror(log);
3615 spin_unlock(&log->l_icloglock);
3618 * Wake up everybody waiting on xfs_log_force.
3619 * Callback all log item committed functions as if the
3620 * log writes were completed.
3622 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3624 #ifdef XFSERRORDEBUG
3626 xlog_in_core_t *iclog;
3628 spin_lock(&log->l_icloglock);
3629 iclog = log->l_iclog;
3631 ASSERT(iclog->ic_callback == 0);
3632 iclog = iclog->ic_next;
3633 } while (iclog != log->l_iclog);
3634 spin_unlock(&log->l_icloglock);
3637 /* return non-zero if log IOERROR transition had already happened */
3642 xlog_iclogs_empty(xlog_t *log)
3644 xlog_in_core_t *iclog;
3646 iclog = log->l_iclog;
3648 /* endianness does not matter here, zero is zero in
3651 if (iclog->ic_header.h_num_logops)
3653 iclog = iclog->ic_next;
3654 } while (iclog != log->l_iclog);