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"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_error.h"
32 #include "xfs_log_priv.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_alloc_btree.h"
36 #include "xfs_ialloc_btree.h"
37 #include "xfs_log_recover.h"
38 #include "xfs_trans_priv.h"
39 #include "xfs_dir_sf.h"
40 #include "xfs_dir2_sf.h"
41 #include "xfs_attr_sf.h"
42 #include "xfs_dinode.h"
43 #include "xfs_inode.h"
47 #define xlog_write_adv_cnt(ptr, len, off, bytes) \
52 /* Local miscellaneous function prototypes */
53 STATIC int xlog_bdstrat_cb(struct xfs_buf *);
54 STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
55 xlog_in_core_t **, xfs_lsn_t *);
56 STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
57 xfs_buftarg_t *log_target,
58 xfs_daddr_t blk_offset,
60 STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
61 STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
62 STATIC void xlog_unalloc_log(xlog_t *log);
63 STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
64 int nentries, xfs_log_ticket_t tic,
66 xlog_in_core_t **commit_iclog,
69 /* local state machine functions */
70 STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
71 STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
72 STATIC int xlog_state_get_iclog_space(xlog_t *log,
74 xlog_in_core_t **iclog,
75 xlog_ticket_t *ticket,
78 STATIC void xlog_state_put_ticket(xlog_t *log,
80 STATIC int xlog_state_release_iclog(xlog_t *log,
81 xlog_in_core_t *iclog);
82 STATIC void xlog_state_switch_iclogs(xlog_t *log,
83 xlog_in_core_t *iclog,
85 STATIC int xlog_state_sync(xlog_t *log,
89 STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
90 STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
92 /* local functions to manipulate grant head */
93 STATIC int xlog_grant_log_space(xlog_t *log,
95 STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
97 STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
98 xlog_ticket_t *ticket);
99 STATIC int xlog_regrant_write_log_space(xlog_t *log,
100 xlog_ticket_t *ticket);
101 STATIC void xlog_ungrant_log_space(xlog_t *log,
102 xlog_ticket_t *ticket);
105 /* local ticket functions */
106 STATIC void xlog_state_ticket_alloc(xlog_t *log);
107 STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
112 STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
115 STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
116 STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
117 STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
118 int count, boolean_t syncing);
119 STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
122 #define xlog_verify_dest_ptr(a,b)
123 #define xlog_verify_grant_head(a,b)
124 #define xlog_verify_iclog(a,b,c,d)
125 #define xlog_verify_tail_lsn(a,b,c)
128 STATIC int xlog_iclogs_empty(xlog_t *log);
130 #if defined(XFS_LOG_TRACE)
132 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
136 if (!log->l_grant_trace) {
137 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
138 if (!log->l_grant_trace)
141 /* ticket counts are 1 byte each */
142 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
144 ktrace_enter(log->l_grant_trace,
146 (void *)log->l_reserve_headq,
147 (void *)log->l_write_headq,
148 (void *)((unsigned long)log->l_grant_reserve_cycle),
149 (void *)((unsigned long)log->l_grant_reserve_bytes),
150 (void *)((unsigned long)log->l_grant_write_cycle),
151 (void *)((unsigned long)log->l_grant_write_bytes),
152 (void *)((unsigned long)log->l_curr_cycle),
153 (void *)((unsigned long)log->l_curr_block),
154 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
155 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
157 (void *)((unsigned long)tic->t_trans_type),
159 (void *)((unsigned long)tic->t_curr_res),
160 (void *)((unsigned long)tic->t_unit_res));
164 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
166 if (!iclog->ic_trace)
167 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
168 ktrace_enter(iclog->ic_trace,
169 (void *)((unsigned long)state),
170 (void *)((unsigned long)current_pid()),
171 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
172 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
173 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
174 (void *)NULL, (void *)NULL);
177 #define xlog_trace_loggrant(log,tic,string)
178 #define xlog_trace_iclog(iclog,state)
179 #endif /* XFS_LOG_TRACE */
184 * 1. currblock field gets updated at startup and after in-core logs
185 * marked as with WANT_SYNC.
189 * This routine is called when a user of a log manager ticket is done with
190 * the reservation. If the ticket was ever used, then a commit record for
191 * the associated transaction is written out as a log operation header with
192 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
193 * a given ticket. If the ticket was one with a permanent reservation, then
194 * a few operations are done differently. Permanent reservation tickets by
195 * default don't release the reservation. They just commit the current
196 * transaction with the belief that the reservation is still needed. A flag
197 * must be passed in before permanent reservations are actually released.
198 * When these type of tickets are not released, they need to be set into
199 * the inited state again. By doing this, a start record will be written
200 * out when the next write occurs.
203 xfs_log_done(xfs_mount_t *mp,
204 xfs_log_ticket_t xtic,
208 xlog_t *log = mp->m_log;
209 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
212 if (XLOG_FORCED_SHUTDOWN(log) ||
214 * If nothing was ever written, don't write out commit record.
215 * If we get an error, just continue and give back the log ticket.
217 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
218 (xlog_commit_record(mp, ticket,
219 (xlog_in_core_t **)iclog, &lsn)))) {
220 lsn = (xfs_lsn_t) -1;
221 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
222 flags |= XFS_LOG_REL_PERM_RESERV;
227 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
228 (flags & XFS_LOG_REL_PERM_RESERV)) {
230 * Release ticket if not permanent reservation or a specifc
231 * request has been made to release a permanent reservation.
233 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
234 xlog_ungrant_log_space(log, ticket);
235 xlog_state_put_ticket(log, ticket);
237 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
238 xlog_regrant_reserve_log_space(log, ticket);
241 /* If this ticket was a permanent reservation and we aren't
242 * trying to release it, reset the inited flags; so next time
243 * we write, a start record will be written out.
245 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
246 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
247 ticket->t_flags |= XLOG_TIC_INITED;
254 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
255 * the force is done synchronously.
257 * Asynchronous forces are implemented by setting the WANT_SYNC
258 * bit in the appropriate in-core log and then returning.
260 * Synchronous forces are implemented with a semaphore. All callers
261 * to force a given lsn to disk will wait on a semaphore attached to the
262 * specific in-core log. When given in-core log finally completes its
263 * write to disk, that thread will wake up all threads waiting on the
273 xlog_t *log = mp->m_log;
277 log_flushed = &dummy;
279 ASSERT(flags & XFS_LOG_FORCE);
281 XFS_STATS_INC(xs_log_force);
283 if (log->l_flags & XLOG_IO_ERROR)
284 return XFS_ERROR(EIO);
286 return xlog_state_sync_all(log, flags, log_flushed);
288 return xlog_state_sync(log, lsn, flags, log_flushed);
289 } /* xfs_log_force */
292 * Attaches a new iclog I/O completion callback routine during
293 * transaction commit. If the log is in error state, a non-zero
294 * return code is handed back and the caller is responsible for
295 * executing the callback at an appropriate time.
298 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
299 void *iclog_hndl, /* iclog to hang callback off */
300 xfs_log_callback_t *cb)
302 xlog_t *log = mp->m_log;
303 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
308 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
310 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
311 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
313 *(iclog->ic_callback_tail) = cb;
314 iclog->ic_callback_tail = &(cb->cb_next);
316 LOG_UNLOCK(log, spl);
318 } /* xfs_log_notify */
321 xfs_log_release_iclog(xfs_mount_t *mp,
324 xlog_t *log = mp->m_log;
325 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
327 if (xlog_state_release_iclog(log, iclog)) {
328 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
336 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
337 * to the reservation.
338 * 2. Potentially, push buffers at tail of log to disk.
340 * Each reservation is going to reserve extra space for a log record header.
341 * When writes happen to the on-disk log, we don't subtract the length of the
342 * log record header from any reservation. By wasting space in each
343 * reservation, we prevent over allocation problems.
346 xfs_log_reserve(xfs_mount_t *mp,
349 xfs_log_ticket_t *ticket,
354 xlog_t *log = mp->m_log;
355 xlog_ticket_t *internal_ticket;
358 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
359 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
361 if (XLOG_FORCED_SHUTDOWN(log))
362 return XFS_ERROR(EIO);
364 XFS_STATS_INC(xs_try_logspace);
366 if (*ticket != NULL) {
367 ASSERT(flags & XFS_LOG_PERM_RESERV);
368 internal_ticket = (xlog_ticket_t *)*ticket;
369 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
370 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
371 retval = xlog_regrant_write_log_space(log, internal_ticket);
373 /* may sleep if need to allocate more tickets */
374 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
376 internal_ticket->t_trans_type = t_type;
377 *ticket = internal_ticket;
378 xlog_trace_loggrant(log, internal_ticket,
379 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
380 "xfs_log_reserve: create new ticket (permanent trans)" :
381 "xfs_log_reserve: create new ticket");
382 xlog_grant_push_ail(mp,
383 (internal_ticket->t_unit_res *
384 internal_ticket->t_cnt));
385 retval = xlog_grant_log_space(log, internal_ticket);
389 } /* xfs_log_reserve */
393 * Mount a log filesystem
395 * mp - ubiquitous xfs mount point structure
396 * log_target - buftarg of on-disk log device
397 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
398 * num_bblocks - Number of BBSIZE blocks in on-disk log
400 * Return error or zero.
403 xfs_log_mount(xfs_mount_t *mp,
404 xfs_buftarg_t *log_target,
405 xfs_daddr_t blk_offset,
408 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
409 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
412 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
414 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
417 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
420 * skip log recovery on a norecovery mount. pretend it all
423 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
425 vfs_t *vfsp = XFS_MTOVFS(mp);
426 int readonly = (vfsp->vfs_flag & VFS_RDONLY);
429 vfsp->vfs_flag &= ~VFS_RDONLY;
431 error = xlog_recover(mp->m_log, readonly);
434 vfsp->vfs_flag |= VFS_RDONLY;
436 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
437 xlog_unalloc_log(mp->m_log);
442 /* Normal transactions can now occur */
443 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
445 /* End mounting message in xfs_log_mount_finish */
447 } /* xfs_log_mount */
450 * Finish the recovery of the file system. This is separate from
451 * the xfs_log_mount() call, because it depends on the code in
452 * xfs_mountfs() to read in the root and real-time bitmap inodes
453 * between calling xfs_log_mount() and here.
455 * mp - ubiquitous xfs mount point structure
458 xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
462 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
463 error = xlog_recover_finish(mp->m_log, mfsi_flags);
466 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
473 * Unmount processing for the log.
476 xfs_log_unmount(xfs_mount_t *mp)
480 error = xfs_log_unmount_write(mp);
481 xfs_log_unmount_dealloc(mp);
486 * Final log writes as part of unmount.
488 * Mark the filesystem clean as unmount happens. Note that during relocation
489 * this routine needs to be executed as part of source-bag while the
490 * deallocation must not be done until source-end.
494 * Unmount record used to have a string "Unmount filesystem--" in the
495 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
496 * We just write the magic number now since that particular field isn't
497 * currently architecture converted and "nUmount" is a bit foo.
498 * As far as I know, there weren't any dependencies on the old behaviour.
502 xfs_log_unmount_write(xfs_mount_t *mp)
504 xlog_t *log = mp->m_log;
505 xlog_in_core_t *iclog;
507 xlog_in_core_t *first_iclog;
509 xfs_log_iovec_t reg[1];
510 xfs_log_ticket_t tic = NULL;
515 /* the data section must be 32 bit size aligned */
519 __uint32_t pad2; /* may as well make it 64 bits */
520 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
523 * Don't write out unmount record on read-only mounts.
524 * Or, if we are doing a forced umount (typically because of IO errors).
526 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
529 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
532 first_iclog = iclog = log->l_iclog;
534 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
535 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
536 ASSERT(iclog->ic_offset == 0);
538 iclog = iclog->ic_next;
539 } while (iclog != first_iclog);
541 if (! (XLOG_FORCED_SHUTDOWN(log))) {
542 reg[0].i_addr = (void*)&magic;
543 reg[0].i_len = sizeof(magic);
544 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
546 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0, 0);
548 /* remove inited flag */
549 ((xlog_ticket_t *)tic)->t_flags = 0;
550 error = xlog_write(mp, reg, 1, tic, &lsn,
551 NULL, XLOG_UNMOUNT_TRANS);
553 * At this point, we're umounting anyway,
554 * so there's no point in transitioning log state
555 * to IOERROR. Just continue...
560 xfs_fs_cmn_err(CE_ALERT, mp,
561 "xfs_log_unmount: unmount record failed");
566 iclog = log->l_iclog;
569 xlog_state_want_sync(log, iclog);
570 (void) xlog_state_release_iclog(log, iclog);
573 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
574 iclog->ic_state == XLOG_STATE_DIRTY)) {
575 if (!XLOG_FORCED_SHUTDOWN(log)) {
576 sv_wait(&iclog->ic_forcesema, PMEM,
577 &log->l_icloglock, s);
585 xlog_state_put_ticket(log, tic);
588 * We're already in forced_shutdown mode, couldn't
589 * even attempt to write out the unmount transaction.
591 * Go through the motions of sync'ing and releasing
592 * the iclog, even though no I/O will actually happen,
593 * we need to wait for other log I/O's that may already
594 * be in progress. Do this as a separate section of
595 * code so we'll know if we ever get stuck here that
596 * we're in this odd situation of trying to unmount
597 * a file system that went into forced_shutdown as
598 * the result of an unmount..
601 iclog = log->l_iclog;
605 xlog_state_want_sync(log, iclog);
606 (void) xlog_state_release_iclog(log, iclog);
610 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
611 || iclog->ic_state == XLOG_STATE_DIRTY
612 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
614 sv_wait(&iclog->ic_forcesema, PMEM,
615 &log->l_icloglock, s);
622 } /* xfs_log_unmount_write */
625 * Deallocate log structures for unmount/relocation.
628 xfs_log_unmount_dealloc(xfs_mount_t *mp)
630 xlog_unalloc_log(mp->m_log);
634 * Write region vectors to log. The write happens using the space reservation
635 * of the ticket (tic). It is not a requirement that all writes for a given
636 * transaction occur with one call to xfs_log_write().
639 xfs_log_write(xfs_mount_t * mp,
640 xfs_log_iovec_t reg[],
642 xfs_log_ticket_t tic,
643 xfs_lsn_t *start_lsn)
646 xlog_t *log = mp->m_log;
648 if (XLOG_FORCED_SHUTDOWN(log))
649 return XFS_ERROR(EIO);
651 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
652 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
655 } /* xfs_log_write */
659 xfs_log_move_tail(xfs_mount_t *mp,
663 xlog_t *log = mp->m_log;
664 int need_bytes, free_bytes, cycle, bytes;
667 if (XLOG_FORCED_SHUTDOWN(log))
669 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
672 /* needed since sync_lsn is 64 bits */
674 tail_lsn = log->l_last_sync_lsn;
680 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
684 log->l_tail_lsn = tail_lsn;
687 if ((tic = log->l_write_headq)) {
689 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
690 panic("Recovery problem");
692 cycle = log->l_grant_write_cycle;
693 bytes = log->l_grant_write_bytes;
694 free_bytes = xlog_space_left(log, cycle, bytes);
696 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
698 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
701 free_bytes -= tic->t_unit_res;
702 sv_signal(&tic->t_sema);
704 } while (tic != log->l_write_headq);
706 if ((tic = log->l_reserve_headq)) {
708 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
709 panic("Recovery problem");
711 cycle = log->l_grant_reserve_cycle;
712 bytes = log->l_grant_reserve_bytes;
713 free_bytes = xlog_space_left(log, cycle, bytes);
715 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
716 need_bytes = tic->t_unit_res*tic->t_cnt;
718 need_bytes = tic->t_unit_res;
719 if (free_bytes < need_bytes && tail_lsn != 1)
722 free_bytes -= need_bytes;
723 sv_signal(&tic->t_sema);
725 } while (tic != log->l_reserve_headq);
727 GRANT_UNLOCK(log, s);
728 } /* xfs_log_move_tail */
731 * Determine if we have a transaction that has gone to disk
732 * that needs to be covered. Log activity needs to be idle (no AIL and
733 * nothing in the iclogs). And, we need to be in the right state indicating
734 * something has gone out.
737 xfs_log_need_covered(xfs_mount_t *mp)
741 xlog_t *log = mp->m_log;
742 vfs_t *vfsp = XFS_MTOVFS(mp);
744 if (fs_frozen(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
745 (vfsp->vfs_flag & VFS_RDONLY))
749 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
750 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
751 && !xfs_trans_first_ail(mp, &gen)
752 && xlog_iclogs_empty(log)) {
753 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
754 log->l_covered_state = XLOG_STATE_COVER_DONE;
756 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
757 log->l_covered_state = XLOG_STATE_COVER_DONE2;
765 /******************************************************************************
769 ******************************************************************************
772 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
773 * The log manager must keep track of the last LR which was committed
774 * to disk. The lsn of this LR will become the new tail_lsn whenever
775 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
776 * the situation where stuff could be written into the log but nothing
777 * was ever in the AIL when asked. Eventually, we panic since the
778 * tail hits the head.
780 * We may be holding the log iclog lock upon entering this routine.
783 xlog_assign_tail_lsn(xfs_mount_t *mp)
787 xlog_t *log = mp->m_log;
789 tail_lsn = xfs_trans_tail_ail(mp);
792 log->l_tail_lsn = tail_lsn;
794 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
796 GRANT_UNLOCK(log, s);
799 } /* xlog_assign_tail_lsn */
803 * Return the space in the log between the tail and the head. The head
804 * is passed in the cycle/bytes formal parms. In the special case where
805 * the reserve head has wrapped passed the tail, this calculation is no
806 * longer valid. In this case, just return 0 which means there is no space
807 * in the log. This works for all places where this function is called
808 * with the reserve head. Of course, if the write head were to ever
809 * wrap the tail, we should blow up. Rather than catch this case here,
810 * we depend on other ASSERTions in other parts of the code. XXXmiken
812 * This code also handles the case where the reservation head is behind
813 * the tail. The details of this case are described below, but the end
814 * result is that we return the size of the log as the amount of space left.
817 xlog_space_left(xlog_t *log, int cycle, int bytes)
823 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
824 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
825 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
826 free_bytes = log->l_logsize - (bytes - tail_bytes);
827 } else if ((tail_cycle + 1) < cycle) {
829 } else if (tail_cycle < cycle) {
830 ASSERT(tail_cycle == (cycle - 1));
831 free_bytes = tail_bytes - bytes;
834 * The reservation head is behind the tail.
835 * In this case we just want to return the size of the
836 * log as the amount of space left.
838 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
839 "xlog_space_left: head behind tail\n"
840 " tail_cycle = %d, tail_bytes = %d\n"
841 " GH cycle = %d, GH bytes = %d",
842 tail_cycle, tail_bytes, cycle, bytes);
844 free_bytes = log->l_logsize;
847 } /* xlog_space_left */
851 * Log function which is called when an io completes.
853 * The log manager needs its own routine, in order to control what
854 * happens with the buffer after the write completes.
857 xlog_iodone(xfs_buf_t *bp)
859 xlog_in_core_t *iclog;
863 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
864 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
865 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
869 * Some versions of cpp barf on the recursive definition of
870 * ic_log -> hic_fields.ic_log and expand ic_log twice when
871 * it is passed through two macros. Workaround broken cpp.
876 * Race to shutdown the filesystem if we see an error.
878 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
879 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
880 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
882 xfs_force_shutdown(l->l_mp, XFS_LOG_IO_ERROR);
884 * This flag will be propagated to the trans-committed
885 * callback routines to let them know that the log-commit
888 aborted = XFS_LI_ABORTED;
889 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
890 aborted = XFS_LI_ABORTED;
892 xlog_state_done_syncing(iclog, aborted);
893 if (!(XFS_BUF_ISASYNC(bp))) {
895 * Corresponding psema() will be done in bwrite(). If we don't
896 * vsema() here, panic.
898 XFS_BUF_V_IODONESEMA(bp);
903 * The bdstrat callback function for log bufs. This gives us a central
904 * place to trap bufs in case we get hit by a log I/O error and need to
905 * shutdown. Actually, in practice, even when we didn't get a log error,
906 * we transition the iclogs to IOERROR state *after* flushing all existing
907 * iclogs to disk. This is because we don't want anymore new transactions to be
908 * started or completed afterwards.
911 xlog_bdstrat_cb(struct xfs_buf *bp)
913 xlog_in_core_t *iclog;
915 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
917 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
918 /* note for irix bstrat will need struct bdevsw passed
919 * Fix the following macro if the code ever is merged
925 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
926 XFS_BUF_ERROR(bp, EIO);
929 return (XFS_ERROR(EIO));
935 * Return size of each in-core log record buffer.
937 * Low memory machines only get 2 16KB buffers. We don't want to waste
938 * memory here. However, all other machines get at least 2 32KB buffers.
939 * The number is hard coded because we don't care about the minimum
940 * memory size, just 32MB systems.
942 * If the filesystem blocksize is too large, we may need to choose a
943 * larger size since the directory code currently logs entire blocks.
947 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
953 if (mp->m_logbufs <= 0) {
954 if (xfs_physmem <= btoc(128*1024*1024)) {
955 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
956 } else if (xfs_physmem <= btoc(400*1024*1024)) {
957 log->l_iclog_bufs = XLOG_MED_ICLOGS;
958 } else { /* 256K with 32K bufs */
959 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
962 log->l_iclog_bufs = mp->m_logbufs;
966 * Buffer size passed in from mount system call.
968 if (mp->m_logbsize > 0) {
969 size = log->l_iclog_size = mp->m_logbsize;
970 log->l_iclog_size_log = 0;
972 log->l_iclog_size_log++;
976 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
977 /* # headers = size / 32K
978 * one header holds cycles from 32K of data
981 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
982 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
984 log->l_iclog_hsize = xhdrs << BBSHIFT;
985 log->l_iclog_heads = xhdrs;
987 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
988 log->l_iclog_hsize = BBSIZE;
989 log->l_iclog_heads = 1;
995 * Special case machines that have less than 32MB of memory.
996 * All machines with more memory use 32KB buffers.
998 if (xfs_physmem <= btoc(32*1024*1024)) {
999 /* Don't change; min configuration */
1000 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1001 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1003 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1004 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1007 /* the default log size is 16k or 32k which is one header sector */
1008 log->l_iclog_hsize = BBSIZE;
1009 log->l_iclog_heads = 1;
1012 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1013 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1015 if (mp->m_sb.sb_blocksize >= 16*1024) {
1016 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1017 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1018 if (mp->m_logbufs <= 0) {
1019 switch (mp->m_sb.sb_blocksize) {
1020 case 16*1024: /* 16 KB */
1021 log->l_iclog_bufs = 3;
1023 case 32*1024: /* 32 KB */
1024 log->l_iclog_bufs = 4;
1026 case 64*1024: /* 64 KB */
1027 log->l_iclog_bufs = 8;
1030 xlog_panic("XFS: Invalid blocksize");
1036 done: /* are we being asked to make the sizes selected above visible? */
1037 if (mp->m_logbufs == 0)
1038 mp->m_logbufs = log->l_iclog_bufs;
1039 if (mp->m_logbsize == 0)
1040 mp->m_logbsize = log->l_iclog_size;
1041 } /* xlog_get_iclog_buffer_size */
1045 * This routine initializes some of the log structure for a given mount point.
1046 * Its primary purpose is to fill in enough, so recovery can occur. However,
1047 * some other stuff may be filled in too.
1050 xlog_alloc_log(xfs_mount_t *mp,
1051 xfs_buftarg_t *log_target,
1052 xfs_daddr_t blk_offset,
1056 xlog_rec_header_t *head;
1057 xlog_in_core_t **iclogp;
1058 xlog_in_core_t *iclog, *prev_iclog=NULL;
1063 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1066 log->l_targ = log_target;
1067 log->l_logsize = BBTOB(num_bblks);
1068 log->l_logBBstart = blk_offset;
1069 log->l_logBBsize = num_bblks;
1070 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1071 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1073 log->l_prev_block = -1;
1074 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1075 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1076 log->l_last_sync_lsn = log->l_tail_lsn;
1077 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1078 log->l_grant_reserve_cycle = 1;
1079 log->l_grant_write_cycle = 1;
1081 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1082 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1083 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1084 /* for larger sector sizes, must have v2 or external log */
1085 ASSERT(log->l_sectbb_log == 0 ||
1086 log->l_logBBstart == 0 ||
1087 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1088 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1090 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1092 xlog_get_iclog_buffer_size(mp, log);
1094 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1095 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1096 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1097 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1098 ASSERT(XFS_BUF_ISBUSY(bp));
1099 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1102 spinlock_init(&log->l_icloglock, "iclog");
1103 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1104 initnsema(&log->l_flushsema, 0, "ic-flush");
1105 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1107 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1108 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1110 iclogp = &log->l_iclog;
1112 * The amount of memory to allocate for the iclog structure is
1113 * rather funky due to the way the structure is defined. It is
1114 * done this way so that we can use different sizes for machines
1115 * with different amounts of memory. See the definition of
1116 * xlog_in_core_t in xfs_log_priv.h for details.
1118 iclogsize = log->l_iclog_size;
1119 ASSERT(log->l_iclog_size >= 4096);
1120 for (i=0; i < log->l_iclog_bufs; i++) {
1121 *iclogp = (xlog_in_core_t *)
1122 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1124 iclog->hic_data = (xlog_in_core_2_t *)
1125 kmem_zalloc(iclogsize, KM_SLEEP);
1127 iclog->ic_prev = prev_iclog;
1129 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1131 head = &iclog->ic_header;
1132 memset(head, 0, sizeof(xlog_rec_header_t));
1133 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1134 INT_SET(head->h_version, ARCH_CONVERT,
1135 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1136 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1138 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1139 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1141 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1142 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1143 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1144 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1147 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1148 iclog->ic_state = XLOG_STATE_ACTIVE;
1149 iclog->ic_log = log;
1150 iclog->ic_callback_tail = &(iclog->ic_callback);
1151 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1153 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1154 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1155 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1156 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1158 iclogp = &iclog->ic_next;
1160 *iclogp = log->l_iclog; /* complete ring */
1161 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1164 } /* xlog_alloc_log */
1168 * Write out the commit record of a transaction associated with the given
1169 * ticket. Return the lsn of the commit record.
1172 xlog_commit_record(xfs_mount_t *mp,
1173 xlog_ticket_t *ticket,
1174 xlog_in_core_t **iclog,
1175 xfs_lsn_t *commitlsnp)
1178 xfs_log_iovec_t reg[1];
1180 reg[0].i_addr = NULL;
1182 XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_COMMIT);
1184 ASSERT_ALWAYS(iclog);
1185 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1186 iclog, XLOG_COMMIT_TRANS))) {
1187 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
1190 } /* xlog_commit_record */
1194 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1195 * log space. This code pushes on the lsn which would supposedly free up
1196 * the 25% which we want to leave free. We may need to adopt a policy which
1197 * pushes on an lsn which is further along in the log once we reach the high
1198 * water mark. In this manner, we would be creating a low water mark.
1201 xlog_grant_push_ail(xfs_mount_t *mp,
1204 xlog_t *log = mp->m_log; /* pointer to the log */
1205 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1206 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1207 int free_blocks; /* free blocks left to write to */
1208 int free_bytes; /* free bytes left to write to */
1209 int threshold_block; /* block in lsn we'd like to be at */
1210 int threshold_cycle; /* lsn cycle we'd like to be at */
1214 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1216 s = GRANT_LOCK(log);
1217 free_bytes = xlog_space_left(log,
1218 log->l_grant_reserve_cycle,
1219 log->l_grant_reserve_bytes);
1220 tail_lsn = log->l_tail_lsn;
1221 free_blocks = BTOBBT(free_bytes);
1224 * Set the threshold for the minimum number of free blocks in the
1225 * log to the maximum of what the caller needs, one quarter of the
1226 * log, and 256 blocks.
1228 free_threshold = BTOBB(need_bytes);
1229 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1230 free_threshold = MAX(free_threshold, 256);
1231 if (free_blocks < free_threshold) {
1232 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1233 threshold_cycle = CYCLE_LSN(tail_lsn);
1234 if (threshold_block >= log->l_logBBsize) {
1235 threshold_block -= log->l_logBBsize;
1236 threshold_cycle += 1;
1238 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1241 /* Don't pass in an lsn greater than the lsn of the last
1242 * log record known to be on disk.
1244 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1245 threshold_lsn = log->l_last_sync_lsn;
1247 GRANT_UNLOCK(log, s);
1250 * Get the transaction layer to kick the dirty buffers out to
1251 * disk asynchronously. No point in trying to do this if
1252 * the filesystem is shutting down.
1254 if (threshold_lsn &&
1255 !XLOG_FORCED_SHUTDOWN(log))
1256 xfs_trans_push_ail(mp, threshold_lsn);
1257 } /* xlog_grant_push_ail */
1261 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1262 * fashion. Previously, we should have moved the current iclog
1263 * ptr in the log to point to the next available iclog. This allows further
1264 * write to continue while this code syncs out an iclog ready to go.
1265 * Before an in-core log can be written out, the data section must be scanned
1266 * to save away the 1st word of each BBSIZE block into the header. We replace
1267 * it with the current cycle count. Each BBSIZE block is tagged with the
1268 * cycle count because there in an implicit assumption that drives will
1269 * guarantee that entire 512 byte blocks get written at once. In other words,
1270 * we can't have part of a 512 byte block written and part not written. By
1271 * tagging each block, we will know which blocks are valid when recovering
1272 * after an unclean shutdown.
1274 * This routine is single threaded on the iclog. No other thread can be in
1275 * this routine with the same iclog. Changing contents of iclog can there-
1276 * fore be done without grabbing the state machine lock. Updating the global
1277 * log will require grabbing the lock though.
1279 * The entire log manager uses a logical block numbering scheme. Only
1280 * log_sync (and then only bwrite()) know about the fact that the log may
1281 * not start with block zero on a given device. The log block start offset
1282 * is added immediately before calling bwrite().
1286 xlog_sync(xlog_t *log,
1287 xlog_in_core_t *iclog)
1289 xfs_caddr_t dptr; /* pointer to byte sized element */
1292 uint count; /* byte count of bwrite */
1293 uint count_init; /* initial count before roundup */
1294 int roundoff; /* roundoff to BB or stripe */
1295 int split = 0; /* split write into two regions */
1298 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1300 XFS_STATS_INC(xs_log_writes);
1301 ASSERT(iclog->ic_refcnt == 0);
1303 /* Add for LR header */
1304 count_init = log->l_iclog_hsize + iclog->ic_offset;
1306 /* Round out the log write size */
1307 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1308 /* we have a v2 stripe unit to use */
1309 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1311 count = BBTOB(BTOBB(count_init));
1313 roundoff = count - count_init;
1314 ASSERT(roundoff >= 0);
1315 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1316 roundoff < log->l_mp->m_sb.sb_logsunit)
1318 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1319 roundoff < BBTOB(1)));
1321 /* move grant heads by roundoff in sync */
1322 s = GRANT_LOCK(log);
1323 XLOG_GRANT_ADD_SPACE(log, roundoff, 'w');
1324 XLOG_GRANT_ADD_SPACE(log, roundoff, 'r');
1325 GRANT_UNLOCK(log, s);
1327 /* put cycle number in every block */
1328 xlog_pack_data(log, iclog, roundoff);
1330 /* real byte length */
1332 INT_SET(iclog->ic_header.h_len,
1334 iclog->ic_offset + roundoff);
1336 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1339 /* put ops count in correct order */
1340 ops = iclog->ic_header.h_num_logops;
1341 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1344 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1345 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1346 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1348 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1350 /* Do we need to split this write into 2 parts? */
1351 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1352 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1353 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1354 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1356 iclog->ic_bwritecnt = 1;
1358 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1359 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1363 * Do an ordered write for the log block.
1365 * It may not be needed to flush the first split block in the log wrap
1366 * case, but do it anyways to be safe -AK
1368 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1369 XFS_BUF_ORDERED(bp);
1371 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1372 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1374 xlog_verify_iclog(log, iclog, count, B_TRUE);
1376 /* account for log which doesn't start at block #0 */
1377 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1379 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1384 if ((error = XFS_bwrite(bp))) {
1385 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1390 bp = iclog->ic_log->l_xbuf;
1391 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1393 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1394 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1395 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1396 (__psint_t)count), split);
1397 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1400 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1401 XFS_BUF_ORDERED(bp);
1402 dptr = XFS_BUF_PTR(bp);
1404 * Bump the cycle numbers at the start of each block
1405 * since this part of the buffer is at the start of
1406 * a new cycle. Watch out for the header magic number
1409 for (i=0; i<split; i += BBSIZE) {
1410 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1411 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1412 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1416 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1417 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1419 /* account for internal log which does't start at block #0 */
1420 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1422 if ((error = XFS_bwrite(bp))) {
1423 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1424 bp, XFS_BUF_ADDR(bp));
1433 * Unallocate a log structure
1436 xlog_unalloc_log(xlog_t *log)
1438 xlog_in_core_t *iclog, *next_iclog;
1439 xlog_ticket_t *tic, *next_tic;
1443 iclog = log->l_iclog;
1444 for (i=0; i<log->l_iclog_bufs; i++) {
1445 sv_destroy(&iclog->ic_forcesema);
1446 sv_destroy(&iclog->ic_writesema);
1447 xfs_buf_free(iclog->ic_bp);
1448 #ifdef XFS_LOG_TRACE
1449 if (iclog->ic_trace != NULL) {
1450 ktrace_free(iclog->ic_trace);
1453 next_iclog = iclog->ic_next;
1454 kmem_free(iclog->hic_data, log->l_iclog_size);
1455 kmem_free(iclog, sizeof(xlog_in_core_t));
1458 freesema(&log->l_flushsema);
1459 spinlock_destroy(&log->l_icloglock);
1460 spinlock_destroy(&log->l_grant_lock);
1462 /* XXXsup take a look at this again. */
1463 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1464 !XLOG_FORCED_SHUTDOWN(log)) {
1465 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1466 "xlog_unalloc_log: (cnt: %d, total: %d)",
1467 log->l_ticket_cnt, log->l_ticket_tcnt);
1468 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1471 tic = log->l_unmount_free;
1473 next_tic = tic->t_next;
1474 kmem_free(tic, NBPP);
1478 xfs_buf_free(log->l_xbuf);
1479 #ifdef XFS_LOG_TRACE
1480 if (log->l_trace != NULL) {
1481 ktrace_free(log->l_trace);
1483 if (log->l_grant_trace != NULL) {
1484 ktrace_free(log->l_grant_trace);
1487 log->l_mp->m_log = NULL;
1488 kmem_free(log, sizeof(xlog_t));
1489 } /* xlog_unalloc_log */
1492 * Update counters atomically now that memcpy is done.
1496 xlog_state_finish_copy(xlog_t *log,
1497 xlog_in_core_t *iclog,
1505 iclog->ic_header.h_num_logops += record_cnt;
1506 iclog->ic_offset += copy_bytes;
1509 } /* xlog_state_finish_copy */
1515 * print out info relating to regions written which consume
1518 #if defined(XFS_LOG_RES_DEBUG)
1520 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1523 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1525 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1526 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1547 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1590 xfs_fs_cmn_err(CE_WARN, mp,
1591 "xfs_log_write: reservation summary:\n"
1592 " trans type = %s (%u)\n"
1593 " unit res = %d bytes\n"
1594 " current res = %d bytes\n"
1595 " total reg = %u bytes (o/flow = %u bytes)\n"
1596 " ophdrs = %u (ophdr space = %u bytes)\n"
1597 " ophdr + reg = %u bytes\n"
1598 " num regions = %u\n",
1599 ((ticket->t_trans_type <= 0 ||
1600 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1601 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1602 ticket->t_trans_type,
1605 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1606 ticket->t_res_num_ophdrs, ophdr_spc,
1607 ticket->t_res_arr_sum +
1608 ticket->t_res_o_flow + ophdr_spc,
1611 for (i = 0; i < ticket->t_res_num; i++) {
1612 uint r_type = ticket->t_res_arr[i].r_type;
1614 "region[%u]: %s - %u bytes\n",
1616 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1617 "bad-rtype" : res_type_str[r_type-1]),
1618 ticket->t_res_arr[i].r_len);
1622 #define xlog_print_tic_res(mp, ticket)
1626 * Write some region out to in-core log
1628 * This will be called when writing externally provided regions or when
1629 * writing out a commit record for a given transaction.
1631 * General algorithm:
1632 * 1. Find total length of this write. This may include adding to the
1633 * lengths passed in.
1634 * 2. Check whether we violate the tickets reservation.
1635 * 3. While writing to this iclog
1636 * A. Reserve as much space in this iclog as can get
1637 * B. If this is first write, save away start lsn
1638 * C. While writing this region:
1639 * 1. If first write of transaction, write start record
1640 * 2. Write log operation header (header per region)
1641 * 3. Find out if we can fit entire region into this iclog
1642 * 4. Potentially, verify destination memcpy ptr
1643 * 5. Memcpy (partial) region
1644 * 6. If partial copy, release iclog; otherwise, continue
1645 * copying more regions into current iclog
1646 * 4. Mark want sync bit (in simulation mode)
1647 * 5. Release iclog for potential flush to on-disk log.
1650 * 1. Panic if reservation is overrun. This should never happen since
1651 * reservation amounts are generated internal to the filesystem.
1653 * 1. Tickets are single threaded data structures.
1654 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1655 * syncing routine. When a single log_write region needs to span
1656 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1657 * on all log operation writes which don't contain the end of the
1658 * region. The XLOG_END_TRANS bit is used for the in-core log
1659 * operation which contains the end of the continued log_write region.
1660 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1661 * we don't really know exactly how much space will be used. As a result,
1662 * we don't update ic_offset until the end when we know exactly how many
1663 * bytes have been written out.
1666 xlog_write(xfs_mount_t * mp,
1667 xfs_log_iovec_t reg[],
1669 xfs_log_ticket_t tic,
1670 xfs_lsn_t *start_lsn,
1671 xlog_in_core_t **commit_iclog,
1674 xlog_t *log = mp->m_log;
1675 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1676 xlog_op_header_t *logop_head; /* ptr to log operation header */
1677 xlog_in_core_t *iclog; /* ptr to current in-core log */
1678 __psint_t ptr; /* copy address into data region */
1679 int len; /* # xlog_write() bytes 2 still copy */
1680 int index; /* region index currently copying */
1681 int log_offset; /* offset (from 0) into data region */
1682 int start_rec_copy; /* # bytes to copy for start record */
1683 int partial_copy; /* did we split a region? */
1684 int partial_copy_len;/* # bytes copied if split region */
1685 int need_copy; /* # bytes need to memcpy this region */
1686 int copy_len; /* # bytes actually memcpy'ing */
1687 int copy_off; /* # bytes from entry start */
1688 int contwr; /* continued write of in-core log? */
1690 int record_cnt = 0, data_cnt = 0;
1692 partial_copy_len = partial_copy = 0;
1694 /* Calculate potential maximum space. Each region gets its own
1695 * xlog_op_header_t and may need to be double word aligned.
1698 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1699 len += sizeof(xlog_op_header_t);
1700 XLOG_TIC_ADD_OPHDR(ticket);
1703 for (index = 0; index < nentries; index++) {
1704 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1705 XLOG_TIC_ADD_OPHDR(ticket);
1706 len += reg[index].i_len;
1707 XLOG_TIC_ADD_REGION(ticket, reg[index].i_len, reg[index].i_type);
1709 contwr = *start_lsn = 0;
1711 if (ticket->t_curr_res < len) {
1712 xlog_print_tic_res(mp, ticket);
1715 "xfs_log_write: reservation ran out. Need to up reservation");
1717 /* Customer configurable panic */
1718 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1719 "xfs_log_write: reservation ran out. Need to up reservation");
1720 /* If we did not panic, shutdown the filesystem */
1721 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1724 ticket->t_curr_res -= len;
1726 for (index = 0; index < nentries; ) {
1727 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1728 &contwr, &log_offset)))
1731 ASSERT(log_offset <= iclog->ic_size - 1);
1732 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1734 /* start_lsn is the first lsn written to. That's all we need. */
1736 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1738 /* This loop writes out as many regions as can fit in the amount
1739 * of space which was allocated by xlog_state_get_iclog_space().
1741 while (index < nentries) {
1742 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1743 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1746 /* If first write for transaction, insert start record.
1747 * We can't be trying to commit if we are inited. We can't
1748 * have any "partial_copy" if we are inited.
1750 if (ticket->t_flags & XLOG_TIC_INITED) {
1751 logop_head = (xlog_op_header_t *)ptr;
1752 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1753 logop_head->oh_clientid = ticket->t_clientid;
1754 logop_head->oh_len = 0;
1755 logop_head->oh_flags = XLOG_START_TRANS;
1756 logop_head->oh_res2 = 0;
1757 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1760 start_rec_copy = sizeof(xlog_op_header_t);
1761 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1764 /* Copy log operation header directly into data section */
1765 logop_head = (xlog_op_header_t *)ptr;
1766 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1767 logop_head->oh_clientid = ticket->t_clientid;
1768 logop_head->oh_res2 = 0;
1770 /* header copied directly */
1771 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1773 /* are we copying a commit or unmount record? */
1774 logop_head->oh_flags = flags;
1777 * We've seen logs corrupted with bad transaction client
1778 * ids. This makes sure that XFS doesn't generate them on.
1779 * Turn this into an EIO and shut down the filesystem.
1781 switch (logop_head->oh_clientid) {
1782 case XFS_TRANSACTION:
1787 xfs_fs_cmn_err(CE_WARN, mp,
1788 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1789 logop_head->oh_clientid, tic);
1790 return XFS_ERROR(EIO);
1793 /* Partial write last time? => (partial_copy != 0)
1794 * need_copy is the amount we'd like to copy if everything could
1795 * fit in the current memcpy.
1797 need_copy = reg[index].i_len - partial_copy_len;
1799 copy_off = partial_copy_len;
1800 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1801 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1803 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1804 partial_copy_len = partial_copy = 0;
1805 } else { /* partial write */
1806 copy_len = iclog->ic_size - log_offset;
1807 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1808 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1810 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1811 partial_copy_len += copy_len;
1813 len += sizeof(xlog_op_header_t); /* from splitting of region */
1814 /* account for new log op header */
1815 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1816 XLOG_TIC_ADD_OPHDR(ticket);
1818 xlog_verify_dest_ptr(log, ptr);
1821 ASSERT(copy_len >= 0);
1822 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1823 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1825 /* make copy_len total bytes copied, including headers */
1826 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1828 data_cnt += contwr ? copy_len : 0;
1829 if (partial_copy) { /* copied partial region */
1830 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1831 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1832 record_cnt = data_cnt = 0;
1833 if ((error = xlog_state_release_iclog(log, iclog)))
1835 break; /* don't increment index */
1836 } else { /* copied entire region */
1838 partial_copy_len = partial_copy = 0;
1840 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1841 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1842 record_cnt = data_cnt = 0;
1843 xlog_state_want_sync(log, iclog);
1845 ASSERT(flags & XLOG_COMMIT_TRANS);
1846 *commit_iclog = iclog;
1847 } else if ((error = xlog_state_release_iclog(log, iclog)))
1849 if (index == nentries)
1850 return 0; /* we are done */
1854 } /* if (partial_copy) */
1855 } /* while (index < nentries) */
1856 } /* for (index = 0; index < nentries; ) */
1859 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1861 ASSERT(flags & XLOG_COMMIT_TRANS);
1862 *commit_iclog = iclog;
1865 return (xlog_state_release_iclog(log, iclog));
1869 /*****************************************************************************
1871 * State Machine functions
1873 *****************************************************************************
1876 /* Clean iclogs starting from the head. This ordering must be
1877 * maintained, so an iclog doesn't become ACTIVE beyond one that
1878 * is SYNCING. This is also required to maintain the notion that we use
1879 * a counting semaphore to hold off would be writers to the log when every
1880 * iclog is trying to sync to disk.
1882 * State Change: DIRTY -> ACTIVE
1885 xlog_state_clean_log(xlog_t *log)
1887 xlog_in_core_t *iclog;
1890 iclog = log->l_iclog;
1892 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1893 iclog->ic_state = XLOG_STATE_ACTIVE;
1894 iclog->ic_offset = 0;
1895 iclog->ic_callback = NULL; /* don't need to free */
1897 * If the number of ops in this iclog indicate it just
1898 * contains the dummy transaction, we can
1899 * change state into IDLE (the second time around).
1900 * Otherwise we should change the state into
1902 * We don't need to cover the dummy.
1905 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1909 * We have two dirty iclogs so start over
1910 * This could also be num of ops indicates
1911 * this is not the dummy going out.
1915 iclog->ic_header.h_num_logops = 0;
1916 memset(iclog->ic_header.h_cycle_data, 0,
1917 sizeof(iclog->ic_header.h_cycle_data));
1918 iclog->ic_header.h_lsn = 0;
1919 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1922 break; /* stop cleaning */
1923 iclog = iclog->ic_next;
1924 } while (iclog != log->l_iclog);
1926 /* log is locked when we are called */
1928 * Change state for the dummy log recording.
1929 * We usually go to NEED. But we go to NEED2 if the changed indicates
1930 * we are done writing the dummy record.
1931 * If we are done with the second dummy recored (DONE2), then
1935 switch (log->l_covered_state) {
1936 case XLOG_STATE_COVER_IDLE:
1937 case XLOG_STATE_COVER_NEED:
1938 case XLOG_STATE_COVER_NEED2:
1939 log->l_covered_state = XLOG_STATE_COVER_NEED;
1942 case XLOG_STATE_COVER_DONE:
1944 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1946 log->l_covered_state = XLOG_STATE_COVER_NEED;
1949 case XLOG_STATE_COVER_DONE2:
1951 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1953 log->l_covered_state = XLOG_STATE_COVER_NEED;
1960 } /* xlog_state_clean_log */
1963 xlog_get_lowest_lsn(
1966 xlog_in_core_t *lsn_log;
1967 xfs_lsn_t lowest_lsn, lsn;
1969 lsn_log = log->l_iclog;
1972 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
1973 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
1974 if ((lsn && !lowest_lsn) ||
1975 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
1979 lsn_log = lsn_log->ic_next;
1980 } while (lsn_log != log->l_iclog);
1986 xlog_state_do_callback(
1989 xlog_in_core_t *ciclog)
1991 xlog_in_core_t *iclog;
1992 xlog_in_core_t *first_iclog; /* used to know when we've
1993 * processed all iclogs once */
1994 xfs_log_callback_t *cb, *cb_next;
1996 xfs_lsn_t lowest_lsn;
1997 int ioerrors; /* counter: iclogs with errors */
1998 int loopdidcallbacks; /* flag: inner loop did callbacks*/
1999 int funcdidcallbacks; /* flag: function did callbacks */
2000 int repeats; /* for issuing console warnings if
2001 * looping too many times */
2005 first_iclog = iclog = log->l_iclog;
2007 funcdidcallbacks = 0;
2012 * Scan all iclogs starting with the one pointed to by the
2013 * log. Reset this starting point each time the log is
2014 * unlocked (during callbacks).
2016 * Keep looping through iclogs until one full pass is made
2017 * without running any callbacks.
2019 first_iclog = log->l_iclog;
2020 iclog = log->l_iclog;
2021 loopdidcallbacks = 0;
2026 /* skip all iclogs in the ACTIVE & DIRTY states */
2027 if (iclog->ic_state &
2028 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2029 iclog = iclog->ic_next;
2034 * Between marking a filesystem SHUTDOWN and stopping
2035 * the log, we do flush all iclogs to disk (if there
2036 * wasn't a log I/O error). So, we do want things to
2037 * go smoothly in case of just a SHUTDOWN w/o a
2040 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2042 * Can only perform callbacks in order. Since
2043 * this iclog is not in the DONE_SYNC/
2044 * DO_CALLBACK state, we skip the rest and
2045 * just try to clean up. If we set our iclog
2046 * to DO_CALLBACK, we will not process it when
2047 * we retry since a previous iclog is in the
2048 * CALLBACK and the state cannot change since
2049 * we are holding the LOG_LOCK.
2051 if (!(iclog->ic_state &
2052 (XLOG_STATE_DONE_SYNC |
2053 XLOG_STATE_DO_CALLBACK))) {
2054 if (ciclog && (ciclog->ic_state ==
2055 XLOG_STATE_DONE_SYNC)) {
2056 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2061 * We now have an iclog that is in either the
2062 * DO_CALLBACK or DONE_SYNC states. The other
2063 * states (WANT_SYNC, SYNCING, or CALLBACK were
2064 * caught by the above if and are going to
2065 * clean (i.e. we aren't doing their callbacks)
2070 * We will do one more check here to see if we
2071 * have chased our tail around.
2074 lowest_lsn = xlog_get_lowest_lsn(log);
2078 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2080 iclog = iclog->ic_next;
2081 continue; /* Leave this iclog for
2085 iclog->ic_state = XLOG_STATE_CALLBACK;
2089 /* l_last_sync_lsn field protected by
2090 * GRANT_LOCK. Don't worry about iclog's lsn.
2091 * No one else can be here except us.
2093 s = GRANT_LOCK(log);
2095 log->l_last_sync_lsn,
2096 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2098 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2099 GRANT_UNLOCK(log, s);
2102 * Keep processing entries in the callback list
2103 * until we come around and it is empty. We
2104 * need to atomically see that the list is
2105 * empty and change the state to DIRTY so that
2106 * we don't miss any more callbacks being added.
2112 cb = iclog->ic_callback;
2115 iclog->ic_callback_tail = &(iclog->ic_callback);
2116 iclog->ic_callback = NULL;
2119 /* perform callbacks in the order given */
2120 for (; cb != 0; cb = cb_next) {
2121 cb_next = cb->cb_next;
2122 cb->cb_func(cb->cb_arg, aborted);
2125 cb = iclog->ic_callback;
2131 ASSERT(iclog->ic_callback == 0);
2132 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2133 iclog->ic_state = XLOG_STATE_DIRTY;
2136 * Transition from DIRTY to ACTIVE if applicable.
2137 * NOP if STATE_IOERROR.
2139 xlog_state_clean_log(log);
2141 /* wake up threads waiting in xfs_log_force() */
2142 sv_broadcast(&iclog->ic_forcesema);
2144 iclog = iclog->ic_next;
2145 } while (first_iclog != iclog);
2146 if (repeats && (repeats % 10) == 0) {
2147 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2148 "xlog_state_do_callback: looping %d", repeats);
2150 } while (!ioerrors && loopdidcallbacks);
2153 * make one last gasp attempt to see if iclogs are being left in
2157 if (funcdidcallbacks) {
2158 first_iclog = iclog = log->l_iclog;
2160 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2162 * Terminate the loop if iclogs are found in states
2163 * which will cause other threads to clean up iclogs.
2165 * SYNCING - i/o completion will go through logs
2166 * DONE_SYNC - interrupt thread should be waiting for
2168 * IOERROR - give up hope all ye who enter here
2170 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2171 iclog->ic_state == XLOG_STATE_SYNCING ||
2172 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2173 iclog->ic_state == XLOG_STATE_IOERROR )
2175 iclog = iclog->ic_next;
2176 } while (first_iclog != iclog);
2180 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2181 flushcnt = log->l_flushcnt;
2182 log->l_flushcnt = 0;
2186 vsema(&log->l_flushsema);
2187 } /* xlog_state_do_callback */
2191 * Finish transitioning this iclog to the dirty state.
2193 * Make sure that we completely execute this routine only when this is
2194 * the last call to the iclog. There is a good chance that iclog flushes,
2195 * when we reach the end of the physical log, get turned into 2 separate
2196 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2197 * routine. By using the reference count bwritecnt, we guarantee that only
2198 * the second completion goes through.
2200 * Callbacks could take time, so they are done outside the scope of the
2201 * global state machine log lock. Assume that the calls to cvsema won't
2202 * take a long time. At least we know it won't sleep.
2205 xlog_state_done_syncing(
2206 xlog_in_core_t *iclog,
2209 xlog_t *log = iclog->ic_log;
2214 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2215 iclog->ic_state == XLOG_STATE_IOERROR);
2216 ASSERT(iclog->ic_refcnt == 0);
2217 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2221 * If we got an error, either on the first buffer, or in the case of
2222 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2223 * and none should ever be attempted to be written to disk
2226 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2227 if (--iclog->ic_bwritecnt == 1) {
2231 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2235 * Someone could be sleeping prior to writing out the next
2236 * iclog buffer, we wake them all, one will get to do the
2237 * I/O, the others get to wait for the result.
2239 sv_broadcast(&iclog->ic_writesema);
2241 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2242 } /* xlog_state_done_syncing */
2246 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2247 * sleep. The flush semaphore is set to the number of in-core buffers and
2248 * decremented around disk syncing. Therefore, if all buffers are syncing,
2249 * this semaphore will cause new writes to sleep until a sync completes.
2250 * Otherwise, this code just does p() followed by v(). This approximates
2251 * a sleep/wakeup except we can't race.
2253 * The in-core logs are used in a circular fashion. They are not used
2254 * out-of-order even when an iclog past the head is free.
2257 * * log_offset where xlog_write() can start writing into the in-core
2259 * * in-core log pointer to which xlog_write() should write.
2260 * * boolean indicating this is a continued write to an in-core log.
2261 * If this is the last write, then the in-core log's offset field
2262 * needs to be incremented, depending on the amount of data which
2266 xlog_state_get_iclog_space(xlog_t *log,
2268 xlog_in_core_t **iclogp,
2269 xlog_ticket_t *ticket,
2270 int *continued_write,
2275 xlog_rec_header_t *head;
2276 xlog_in_core_t *iclog;
2281 if (XLOG_FORCED_SHUTDOWN(log)) {
2283 return XFS_ERROR(EIO);
2286 iclog = log->l_iclog;
2287 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2290 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2291 XFS_STATS_INC(xs_log_noiclogs);
2292 /* Ensure that log writes happen */
2293 psema(&log->l_flushsema, PINOD);
2296 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2297 head = &iclog->ic_header;
2299 iclog->ic_refcnt++; /* prevents sync */
2300 log_offset = iclog->ic_offset;
2302 /* On the 1st write to an iclog, figure out lsn. This works
2303 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2304 * committing to. If the offset is set, that's how many blocks
2307 if (log_offset == 0) {
2308 ticket->t_curr_res -= log->l_iclog_hsize;
2309 XLOG_TIC_ADD_REGION(ticket,
2311 XLOG_REG_TYPE_LRHEADER);
2312 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2313 ASSIGN_LSN(head->h_lsn, log);
2314 ASSERT(log->l_curr_block >= 0);
2317 /* If there is enough room to write everything, then do it. Otherwise,
2318 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2319 * bit is on, so this will get flushed out. Don't update ic_offset
2320 * until you know exactly how many bytes get copied. Therefore, wait
2321 * until later to update ic_offset.
2323 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2324 * can fit into remaining data section.
2326 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2327 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2329 /* If I'm the only one writing to this iclog, sync it to disk */
2330 if (iclog->ic_refcnt == 1) {
2332 if ((error = xlog_state_release_iclog(log, iclog)))
2341 /* Do we have enough room to write the full amount in the remainder
2342 * of this iclog? Or must we continue a write on the next iclog and
2343 * mark this iclog as completely taken? In the case where we switch
2344 * iclogs (to mark it taken), this particular iclog will release/sync
2345 * to disk in xlog_write().
2347 if (len <= iclog->ic_size - iclog->ic_offset) {
2348 *continued_write = 0;
2349 iclog->ic_offset += len;
2351 *continued_write = 1;
2352 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2356 ASSERT(iclog->ic_offset <= iclog->ic_size);
2359 *logoffsetp = log_offset;
2361 } /* xlog_state_get_iclog_space */
2364 * Atomically get the log space required for a log ticket.
2366 * Once a ticket gets put onto the reserveq, it will only return after
2367 * the needed reservation is satisfied.
2370 xlog_grant_log_space(xlog_t *log,
2382 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2383 panic("grant Recovery problem");
2386 /* Is there space or do we need to sleep? */
2387 s = GRANT_LOCK(log);
2388 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2390 /* something is already sleeping; insert new transaction at end */
2391 if (log->l_reserve_headq) {
2392 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2393 xlog_trace_loggrant(log, tic,
2394 "xlog_grant_log_space: sleep 1");
2396 * Gotta check this before going to sleep, while we're
2397 * holding the grant lock.
2399 if (XLOG_FORCED_SHUTDOWN(log))
2402 XFS_STATS_INC(xs_sleep_logspace);
2403 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2405 * If we got an error, and the filesystem is shutting down,
2406 * we'll catch it down below. So just continue...
2408 xlog_trace_loggrant(log, tic,
2409 "xlog_grant_log_space: wake 1");
2410 s = GRANT_LOCK(log);
2412 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2413 need_bytes = tic->t_unit_res*tic->t_ocnt;
2415 need_bytes = tic->t_unit_res;
2418 if (XLOG_FORCED_SHUTDOWN(log))
2421 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2422 log->l_grant_reserve_bytes);
2423 if (free_bytes < need_bytes) {
2424 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2425 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2426 xlog_trace_loggrant(log, tic,
2427 "xlog_grant_log_space: sleep 2");
2428 XFS_STATS_INC(xs_sleep_logspace);
2429 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2431 if (XLOG_FORCED_SHUTDOWN(log)) {
2432 s = GRANT_LOCK(log);
2436 xlog_trace_loggrant(log, tic,
2437 "xlog_grant_log_space: wake 2");
2438 xlog_grant_push_ail(log->l_mp, need_bytes);
2439 s = GRANT_LOCK(log);
2441 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2442 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2444 /* we've got enough space */
2445 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w');
2446 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'r');
2448 tail_lsn = log->l_tail_lsn;
2450 * Check to make sure the grant write head didn't just over lap the
2451 * tail. If the cycles are the same, we can't be overlapping.
2452 * Otherwise, make sure that the cycles differ by exactly one and
2453 * check the byte count.
2455 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2456 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2457 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2460 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2461 xlog_verify_grant_head(log, 1);
2462 GRANT_UNLOCK(log, s);
2466 if (tic->t_flags & XLOG_TIC_IN_Q)
2467 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2468 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2470 * If we are failing, make sure the ticket doesn't have any
2471 * current reservations. We don't want to add this back when
2472 * the ticket/transaction gets cancelled.
2474 tic->t_curr_res = 0;
2475 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2476 GRANT_UNLOCK(log, s);
2477 return XFS_ERROR(EIO);
2478 } /* xlog_grant_log_space */
2482 * Replenish the byte reservation required by moving the grant write head.
2487 xlog_regrant_write_log_space(xlog_t *log,
2491 int free_bytes, need_bytes;
2492 xlog_ticket_t *ntic;
2497 tic->t_curr_res = tic->t_unit_res;
2498 XLOG_TIC_RESET_RES(tic);
2504 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2505 panic("regrant Recovery problem");
2508 s = GRANT_LOCK(log);
2509 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2511 if (XLOG_FORCED_SHUTDOWN(log))
2514 /* If there are other waiters on the queue then give them a
2515 * chance at logspace before us. Wake up the first waiters,
2516 * if we do not wake up all the waiters then go to sleep waiting
2517 * for more free space, otherwise try to get some space for
2521 if ((ntic = log->l_write_headq)) {
2522 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2523 log->l_grant_write_bytes);
2525 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2527 if (free_bytes < ntic->t_unit_res)
2529 free_bytes -= ntic->t_unit_res;
2530 sv_signal(&ntic->t_sema);
2531 ntic = ntic->t_next;
2532 } while (ntic != log->l_write_headq);
2534 if (ntic != log->l_write_headq) {
2535 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2536 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2538 xlog_trace_loggrant(log, tic,
2539 "xlog_regrant_write_log_space: sleep 1");
2540 XFS_STATS_INC(xs_sleep_logspace);
2541 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2542 &log->l_grant_lock, s);
2544 /* If we're shutting down, this tic is already
2546 if (XLOG_FORCED_SHUTDOWN(log)) {
2547 s = GRANT_LOCK(log);
2551 xlog_trace_loggrant(log, tic,
2552 "xlog_regrant_write_log_space: wake 1");
2553 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2554 s = GRANT_LOCK(log);
2558 need_bytes = tic->t_unit_res;
2561 if (XLOG_FORCED_SHUTDOWN(log))
2564 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2565 log->l_grant_write_bytes);
2566 if (free_bytes < need_bytes) {
2567 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2568 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2569 XFS_STATS_INC(xs_sleep_logspace);
2570 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2572 /* If we're shutting down, this tic is already off the queue */
2573 if (XLOG_FORCED_SHUTDOWN(log)) {
2574 s = GRANT_LOCK(log);
2578 xlog_trace_loggrant(log, tic,
2579 "xlog_regrant_write_log_space: wake 2");
2580 xlog_grant_push_ail(log->l_mp, need_bytes);
2581 s = GRANT_LOCK(log);
2583 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2584 XLOG_DEL_TICKETQ(log->l_write_headq, tic);
2586 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w'); /* we've got enough space */
2588 tail_lsn = log->l_tail_lsn;
2589 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2590 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2591 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2595 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2596 xlog_verify_grant_head(log, 1);
2597 GRANT_UNLOCK(log, s);
2602 if (tic->t_flags & XLOG_TIC_IN_Q)
2603 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2604 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2606 * If we are failing, make sure the ticket doesn't have any
2607 * current reservations. We don't want to add this back when
2608 * the ticket/transaction gets cancelled.
2610 tic->t_curr_res = 0;
2611 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2612 GRANT_UNLOCK(log, s);
2613 return XFS_ERROR(EIO);
2614 } /* xlog_regrant_write_log_space */
2617 /* The first cnt-1 times through here we don't need to
2618 * move the grant write head because the permanent
2619 * reservation has reserved cnt times the unit amount.
2620 * Release part of current permanent unit reservation and
2621 * reset current reservation to be one units worth. Also
2622 * move grant reservation head forward.
2625 xlog_regrant_reserve_log_space(xlog_t *log,
2626 xlog_ticket_t *ticket)
2630 xlog_trace_loggrant(log, ticket,
2631 "xlog_regrant_reserve_log_space: enter");
2632 if (ticket->t_cnt > 0)
2635 s = GRANT_LOCK(log);
2636 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2637 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2638 ticket->t_curr_res = ticket->t_unit_res;
2639 XLOG_TIC_RESET_RES(ticket);
2640 xlog_trace_loggrant(log, ticket,
2641 "xlog_regrant_reserve_log_space: sub current res");
2642 xlog_verify_grant_head(log, 1);
2644 /* just return if we still have some of the pre-reserved space */
2645 if (ticket->t_cnt > 0) {
2646 GRANT_UNLOCK(log, s);
2650 XLOG_GRANT_ADD_SPACE(log, ticket->t_unit_res, 'r');
2651 xlog_trace_loggrant(log, ticket,
2652 "xlog_regrant_reserve_log_space: exit");
2653 xlog_verify_grant_head(log, 0);
2654 GRANT_UNLOCK(log, s);
2655 ticket->t_curr_res = ticket->t_unit_res;
2656 XLOG_TIC_RESET_RES(ticket);
2657 } /* xlog_regrant_reserve_log_space */
2661 * Give back the space left from a reservation.
2663 * All the information we need to make a correct determination of space left
2664 * is present. For non-permanent reservations, things are quite easy. The
2665 * count should have been decremented to zero. We only need to deal with the
2666 * space remaining in the current reservation part of the ticket. If the
2667 * ticket contains a permanent reservation, there may be left over space which
2668 * needs to be released. A count of N means that N-1 refills of the current
2669 * reservation can be done before we need to ask for more space. The first
2670 * one goes to fill up the first current reservation. Once we run out of
2671 * space, the count will stay at zero and the only space remaining will be
2672 * in the current reservation field.
2675 xlog_ungrant_log_space(xlog_t *log,
2676 xlog_ticket_t *ticket)
2680 if (ticket->t_cnt > 0)
2683 s = GRANT_LOCK(log);
2684 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2686 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2687 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2689 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2691 /* If this is a permanent reservation ticket, we may be able to free
2692 * up more space based on the remaining count.
2694 if (ticket->t_cnt > 0) {
2695 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2696 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'w');
2697 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'r');
2700 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2701 xlog_verify_grant_head(log, 1);
2702 GRANT_UNLOCK(log, s);
2703 xfs_log_move_tail(log->l_mp, 1);
2704 } /* xlog_ungrant_log_space */
2708 * Atomically put back used ticket.
2711 xlog_state_put_ticket(xlog_t *log,
2717 xlog_ticket_put(log, tic);
2719 } /* xlog_state_put_ticket */
2722 * Flush iclog to disk if this is the last reference to the given iclog and
2723 * the WANT_SYNC bit is set.
2725 * When this function is entered, the iclog is not necessarily in the
2726 * WANT_SYNC state. It may be sitting around waiting to get filled.
2731 xlog_state_release_iclog(xlog_t *log,
2732 xlog_in_core_t *iclog)
2735 int sync = 0; /* do we sync? */
2737 xlog_assign_tail_lsn(log->l_mp);
2741 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2743 return XFS_ERROR(EIO);
2746 ASSERT(iclog->ic_refcnt > 0);
2747 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2748 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2750 if (--iclog->ic_refcnt == 0 &&
2751 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2753 iclog->ic_state = XLOG_STATE_SYNCING;
2754 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2755 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2756 /* cycle incremented when incrementing curr_block */
2762 * We let the log lock go, so it's possible that we hit a log I/O
2763 * error or someother SHUTDOWN condition that marks the iclog
2764 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2765 * this iclog has consistent data, so we ignore IOERROR
2766 * flags after this point.
2769 return xlog_sync(log, iclog);
2773 } /* xlog_state_release_iclog */
2777 * This routine will mark the current iclog in the ring as WANT_SYNC
2778 * and move the current iclog pointer to the next iclog in the ring.
2779 * When this routine is called from xlog_state_get_iclog_space(), the
2780 * exact size of the iclog has not yet been determined. All we know is
2781 * that every data block. We have run out of space in this log record.
2784 xlog_state_switch_iclogs(xlog_t *log,
2785 xlog_in_core_t *iclog,
2788 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2790 eventual_size = iclog->ic_offset;
2791 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2792 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2793 log->l_prev_block = log->l_curr_block;
2794 log->l_prev_cycle = log->l_curr_cycle;
2796 /* roll log?: ic_offset changed later */
2797 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2799 /* Round up to next log-sunit */
2800 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2801 log->l_mp->m_sb.sb_logsunit > 1) {
2802 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2803 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2806 if (log->l_curr_block >= log->l_logBBsize) {
2807 log->l_curr_cycle++;
2808 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2809 log->l_curr_cycle++;
2810 log->l_curr_block -= log->l_logBBsize;
2811 ASSERT(log->l_curr_block >= 0);
2813 ASSERT(iclog == log->l_iclog);
2814 log->l_iclog = iclog->ic_next;
2815 } /* xlog_state_switch_iclogs */
2819 * Write out all data in the in-core log as of this exact moment in time.
2821 * Data may be written to the in-core log during this call. However,
2822 * we don't guarantee this data will be written out. A change from past
2823 * implementation means this routine will *not* write out zero length LRs.
2825 * Basically, we try and perform an intelligent scan of the in-core logs.
2826 * If we determine there is no flushable data, we just return. There is no
2827 * flushable data if:
2829 * 1. the current iclog is active and has no data; the previous iclog
2830 * is in the active or dirty state.
2831 * 2. the current iclog is drity, and the previous iclog is in the
2832 * active or dirty state.
2834 * We may sleep (call psema) if:
2836 * 1. the current iclog is not in the active nor dirty state.
2837 * 2. the current iclog dirty, and the previous iclog is not in the
2838 * active nor dirty state.
2839 * 3. the current iclog is active, and there is another thread writing
2840 * to this particular iclog.
2841 * 4. a) the current iclog is active and has no other writers
2842 * b) when we return from flushing out this iclog, it is still
2843 * not in the active nor dirty state.
2846 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2848 xlog_in_core_t *iclog;
2854 iclog = log->l_iclog;
2855 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2857 return XFS_ERROR(EIO);
2860 /* If the head iclog is not active nor dirty, we just attach
2861 * ourselves to the head and go to sleep.
2863 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2864 iclog->ic_state == XLOG_STATE_DIRTY) {
2866 * If the head is dirty or (active and empty), then
2867 * we need to look at the previous iclog. If the previous
2868 * iclog is active or dirty we are done. There is nothing
2869 * to sync out. Otherwise, we attach ourselves to the
2870 * previous iclog and go to sleep.
2872 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2873 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2874 iclog = iclog->ic_prev;
2875 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2876 iclog->ic_state == XLOG_STATE_DIRTY)
2881 if (iclog->ic_refcnt == 0) {
2882 /* We are the only one with access to this
2883 * iclog. Flush it out now. There should
2884 * be a roundoff of zero to show that someone
2885 * has already taken care of the roundoff from
2886 * the previous sync.
2889 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2890 xlog_state_switch_iclogs(log, iclog, 0);
2893 if (xlog_state_release_iclog(log, iclog))
2894 return XFS_ERROR(EIO);
2897 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2898 iclog->ic_state != XLOG_STATE_DIRTY)
2903 /* Someone else is writing to this iclog.
2904 * Use its call to flush out the data. However,
2905 * the other thread may not force out this LR,
2906 * so we mark it WANT_SYNC.
2908 xlog_state_switch_iclogs(log, iclog, 0);
2914 /* By the time we come around again, the iclog could've been filled
2915 * which would give it another lsn. If we have a new lsn, just
2916 * return because the relevant data has been flushed.
2919 if (flags & XFS_LOG_SYNC) {
2921 * We must check if we're shutting down here, before
2922 * we wait, while we're holding the LOG_LOCK.
2923 * Then we check again after waking up, in case our
2924 * sleep was disturbed by a bad news.
2926 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2928 return XFS_ERROR(EIO);
2930 XFS_STATS_INC(xs_log_force_sleep);
2931 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
2933 * No need to grab the log lock here since we're
2934 * only deciding whether or not to return EIO
2935 * and the memory read should be atomic.
2937 if (iclog->ic_state & XLOG_STATE_IOERROR)
2938 return XFS_ERROR(EIO);
2947 } /* xlog_state_sync_all */
2951 * Used by code which implements synchronous log forces.
2953 * Find in-core log with lsn.
2954 * If it is in the DIRTY state, just return.
2955 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2956 * state and go to sleep or return.
2957 * If it is in any other state, go to sleep or return.
2959 * If filesystem activity goes to zero, the iclog will get flushed only by
2963 xlog_state_sync(xlog_t *log,
2968 xlog_in_core_t *iclog;
2969 int already_slept = 0;
2975 iclog = log->l_iclog;
2977 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2979 return XFS_ERROR(EIO);
2983 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
2984 iclog = iclog->ic_next;
2988 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2993 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
2995 * We sleep here if we haven't already slept (e.g.
2996 * this is the first time we've looked at the correct
2997 * iclog buf) and the buffer before us is going to
2998 * be sync'ed. The reason for this is that if we
2999 * are doing sync transactions here, by waiting for
3000 * the previous I/O to complete, we can allow a few
3001 * more transactions into this iclog before we close
3004 * Otherwise, we mark the buffer WANT_SYNC, and bump
3005 * up the refcnt so we can release the log (which drops
3006 * the ref count). The state switch keeps new transaction
3007 * commits from using this buffer. When the current commits
3008 * finish writing into the buffer, the refcount will drop to
3009 * zero and the buffer will go out then.
3011 if (!already_slept &&
3012 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3013 XLOG_STATE_SYNCING))) {
3014 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3015 XFS_STATS_INC(xs_log_force_sleep);
3016 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3017 &log->l_icloglock, s);
3023 xlog_state_switch_iclogs(log, iclog, 0);
3025 if (xlog_state_release_iclog(log, iclog))
3026 return XFS_ERROR(EIO);
3032 if ((flags & XFS_LOG_SYNC) && /* sleep */
3033 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3036 * Don't wait on the forcesema if we know that we've
3037 * gotten a log write error.
3039 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3041 return XFS_ERROR(EIO);
3043 XFS_STATS_INC(xs_log_force_sleep);
3044 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3046 * No need to grab the log lock here since we're
3047 * only deciding whether or not to return EIO
3048 * and the memory read should be atomic.
3050 if (iclog->ic_state & XLOG_STATE_IOERROR)
3051 return XFS_ERROR(EIO);
3053 } else { /* just return */
3058 } while (iclog != log->l_iclog);
3062 } /* xlog_state_sync */
3066 * Called when we want to mark the current iclog as being ready to sync to
3070 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3076 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3077 xlog_state_switch_iclogs(log, iclog, 0);
3079 ASSERT(iclog->ic_state &
3080 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3084 } /* xlog_state_want_sync */
3088 /*****************************************************************************
3092 *****************************************************************************
3096 * Algorithm doesn't take into account page size. ;-(
3099 xlog_state_ticket_alloc(xlog_t *log)
3101 xlog_ticket_t *t_list;
3102 xlog_ticket_t *next;
3104 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3108 * The kmem_zalloc may sleep, so we shouldn't be holding the
3109 * global lock. XXXmiken: may want to use zone allocator.
3111 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3115 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3116 t_list = (xlog_ticket_t *)buf;
3117 t_list->t_next = log->l_unmount_free;
3118 log->l_unmount_free = t_list++;
3119 log->l_ticket_cnt++;
3120 log->l_ticket_tcnt++;
3122 /* Next ticket becomes first ticket attached to ticket free list */
3123 if (log->l_freelist != NULL) {
3124 ASSERT(log->l_tail != NULL);
3125 log->l_tail->t_next = t_list;
3127 log->l_freelist = t_list;
3129 log->l_ticket_cnt++;
3130 log->l_ticket_tcnt++;
3132 /* Cycle through rest of alloc'ed memory, building up free Q */
3133 for ( ; i > 0; i--) {
3135 t_list->t_next = next;
3137 log->l_ticket_cnt++;
3138 log->l_ticket_tcnt++;
3140 t_list->t_next = NULL;
3141 log->l_tail = t_list;
3143 } /* xlog_state_ticket_alloc */
3147 * Put ticket into free list
3149 * Assumption: log lock is held around this call.
3152 xlog_ticket_put(xlog_t *log,
3153 xlog_ticket_t *ticket)
3155 sv_destroy(&ticket->t_sema);
3158 * Don't think caching will make that much difference. It's
3159 * more important to make debug easier.
3162 /* real code will want to use LIFO for caching */
3163 ticket->t_next = log->l_freelist;
3164 log->l_freelist = ticket;
3165 /* no need to clear fields */
3167 /* When we debug, it is easier if tickets are cycled */
3168 ticket->t_next = NULL;
3169 if (log->l_tail != 0) {
3170 log->l_tail->t_next = ticket;
3172 ASSERT(log->l_freelist == 0);
3173 log->l_freelist = ticket;
3175 log->l_tail = ticket;
3177 log->l_ticket_cnt++;
3178 } /* xlog_ticket_put */
3182 * Grab ticket off freelist or allocation some more
3185 xlog_ticket_get(xlog_t *log,
3196 if (log->l_freelist == NULL)
3197 xlog_state_ticket_alloc(log); /* potentially sleep */
3200 if (log->l_freelist == NULL) {
3204 tic = log->l_freelist;
3205 log->l_freelist = tic->t_next;
3206 if (log->l_freelist == NULL)
3208 log->l_ticket_cnt--;
3212 * Permanent reservations have up to 'cnt'-1 active log operations
3213 * in the log. A unit in this case is the amount of space for one
3214 * of these log operations. Normal reservations have a cnt of 1
3215 * and their unit amount is the total amount of space required.
3217 * The following lines of code account for non-transaction data
3218 * which occupy space in the on-disk log.
3220 * Normal form of a transaction is:
3221 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3222 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3224 * We need to account for all the leadup data and trailer data
3225 * around the transaction data.
3226 * And then we need to account for the worst case in terms of using
3228 * The worst case will happen if:
3229 * - the placement of the transaction happens to be such that the
3230 * roundoff is at its maximum
3231 * - the transaction data is synced before the commit record is synced
3232 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3233 * Therefore the commit record is in its own Log Record.
3234 * This can happen as the commit record is called with its
3235 * own region to xlog_write().
3236 * This then means that in the worst case, roundoff can happen for
3237 * the commit-rec as well.
3238 * The commit-rec is smaller than padding in this scenario and so it is
3239 * not added separately.
3242 /* for trans header */
3243 unit_bytes += sizeof(xlog_op_header_t);
3244 unit_bytes += sizeof(xfs_trans_header_t);
3247 unit_bytes += sizeof(xlog_op_header_t);
3249 /* for LR headers */
3250 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3251 unit_bytes += log->l_iclog_hsize * num_headers;
3253 /* for commit-rec LR header - note: padding will subsume the ophdr */
3254 unit_bytes += log->l_iclog_hsize;
3256 /* for split-recs - ophdrs added when data split over LRs */
3257 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3259 /* for roundoff padding for transaction data and one for commit record */
3260 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3261 log->l_mp->m_sb.sb_logsunit > 1) {
3262 /* log su roundoff */
3263 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3266 unit_bytes += 2*BBSIZE;
3269 tic->t_unit_res = unit_bytes;
3270 tic->t_curr_res = unit_bytes;
3273 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3274 tic->t_clientid = client;
3275 tic->t_flags = XLOG_TIC_INITED;
3276 tic->t_trans_type = 0;
3277 if (xflags & XFS_LOG_PERM_RESERV)
3278 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3279 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3281 XLOG_TIC_RESET_RES(tic);
3284 } /* xlog_ticket_get */
3287 /******************************************************************************
3289 * Log debug routines
3291 ******************************************************************************
3295 * Make sure that the destination ptr is within the valid data region of
3296 * one of the iclogs. This uses backup pointers stored in a different
3297 * part of the log in case we trash the log structure.
3300 xlog_verify_dest_ptr(xlog_t *log,
3306 for (i=0; i < log->l_iclog_bufs; i++) {
3307 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3308 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3312 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3313 } /* xlog_verify_dest_ptr */
3316 xlog_verify_grant_head(xlog_t *log, int equals)
3318 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3320 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3322 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3324 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3325 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3327 } /* xlog_verify_grant_head */
3329 /* check if it will fit */
3331 xlog_verify_tail_lsn(xlog_t *log,
3332 xlog_in_core_t *iclog,
3337 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3339 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3340 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3341 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3343 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3345 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3346 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3348 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3349 if (blocks < BTOBB(iclog->ic_offset) + 1)
3350 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3352 } /* xlog_verify_tail_lsn */
3355 * Perform a number of checks on the iclog before writing to disk.
3357 * 1. Make sure the iclogs are still circular
3358 * 2. Make sure we have a good magic number
3359 * 3. Make sure we don't have magic numbers in the data
3360 * 4. Check fields of each log operation header for:
3361 * A. Valid client identifier
3362 * B. tid ptr value falls in valid ptr space (user space code)
3363 * C. Length in log record header is correct according to the
3364 * individual operation headers within record.
3365 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3366 * log, check the preceding blocks of the physical log to make sure all
3367 * the cycle numbers agree with the current cycle number.
3370 xlog_verify_iclog(xlog_t *log,
3371 xlog_in_core_t *iclog,
3375 xlog_op_header_t *ophead;
3376 xlog_in_core_t *icptr;
3377 xlog_in_core_2_t *xhdr;
3379 xfs_caddr_t base_ptr;
3380 __psint_t field_offset;
3382 int len, i, j, k, op_len;
3386 /* check validity of iclog pointers */
3388 icptr = log->l_iclog;
3389 for (i=0; i < log->l_iclog_bufs; i++) {
3391 xlog_panic("xlog_verify_iclog: invalid ptr");
3392 icptr = icptr->ic_next;
3394 if (icptr != log->l_iclog)
3395 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3398 /* check log magic numbers */
3399 ptr = (xfs_caddr_t) &(iclog->ic_header);
3400 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3401 xlog_panic("xlog_verify_iclog: invalid magic num");
3403 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3405 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3406 xlog_panic("xlog_verify_iclog: unexpected magic num");
3410 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3411 ptr = iclog->ic_datap;
3413 ophead = (xlog_op_header_t *)ptr;
3414 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3415 for (i = 0; i < len; i++) {
3416 ophead = (xlog_op_header_t *)ptr;
3418 /* clientid is only 1 byte */
3419 field_offset = (__psint_t)
3420 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3421 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3422 clientid = ophead->oh_clientid;
3424 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3425 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3426 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3427 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3428 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3430 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3433 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3434 cmn_err(CE_WARN, "xlog_verify_iclog: "
3435 "invalid clientid %d op 0x%p offset 0x%lx",
3436 clientid, ophead, (unsigned long)field_offset);
3439 field_offset = (__psint_t)
3440 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3441 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3442 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3444 idx = BTOBBT((__psint_t)&ophead->oh_len -
3445 (__psint_t)iclog->ic_datap);
3446 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3447 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3448 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3449 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3451 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3454 ptr += sizeof(xlog_op_header_t) + op_len;
3456 } /* xlog_verify_iclog */
3460 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3466 xlog_in_core_t *iclog, *ic;
3468 iclog = log->l_iclog;
3469 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3471 * Mark all the incore logs IOERROR.
3472 * From now on, no log flushes will result.
3476 ic->ic_state = XLOG_STATE_IOERROR;
3478 } while (ic != iclog);
3482 * Return non-zero, if state transition has already happened.
3488 * This is called from xfs_force_shutdown, when we're forcibly
3489 * shutting down the filesystem, typically because of an IO error.
3490 * Our main objectives here are to make sure that:
3491 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3492 * parties to find out, 'atomically'.
3493 * b. those who're sleeping on log reservations, pinned objects and
3494 * other resources get woken up, and be told the bad news.
3495 * c. nothing new gets queued up after (a) and (b) are done.
3496 * d. if !logerror, flush the iclogs to disk, then seal them off
3500 xfs_log_force_umount(
3501 struct xfs_mount *mp,
3514 * If this happens during log recovery, don't worry about
3515 * locking; the log isn't open for business yet.
3518 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3519 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3520 XFS_BUF_DONE(mp->m_sb_bp);
3525 * Somebody could've already done the hard work for us.
3526 * No need to get locks for this.
3528 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3529 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3534 * We must hold both the GRANT lock and the LOG lock,
3535 * before we mark the filesystem SHUTDOWN and wake
3536 * everybody up to tell the bad news.
3538 s = GRANT_LOCK(log);
3540 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3541 XFS_BUF_DONE(mp->m_sb_bp);
3543 * This flag is sort of redundant because of the mount flag, but
3544 * it's good to maintain the separation between the log and the rest
3547 log->l_flags |= XLOG_IO_ERROR;
3550 * If we hit a log error, we want to mark all the iclogs IOERROR
3551 * while we're still holding the loglock.
3554 retval = xlog_state_ioerror(log);
3555 LOG_UNLOCK(log, s2);
3558 * We don't want anybody waiting for log reservations
3559 * after this. That means we have to wake up everybody
3560 * queued up on reserve_headq as well as write_headq.
3561 * In addition, we make sure in xlog_{re}grant_log_space
3562 * that we don't enqueue anything once the SHUTDOWN flag
3563 * is set, and this action is protected by the GRANTLOCK.
3565 if ((tic = log->l_reserve_headq)) {
3567 sv_signal(&tic->t_sema);
3569 } while (tic != log->l_reserve_headq);
3572 if ((tic = log->l_write_headq)) {
3574 sv_signal(&tic->t_sema);
3576 } while (tic != log->l_write_headq);
3578 GRANT_UNLOCK(log, s);
3580 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3583 * Force the incore logs to disk before shutting the
3584 * log down completely.
3586 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3588 retval = xlog_state_ioerror(log);
3589 LOG_UNLOCK(log, s2);
3592 * Wake up everybody waiting on xfs_log_force.
3593 * Callback all log item committed functions as if the
3594 * log writes were completed.
3596 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3598 #ifdef XFSERRORDEBUG
3600 xlog_in_core_t *iclog;
3603 iclog = log->l_iclog;
3605 ASSERT(iclog->ic_callback == 0);
3606 iclog = iclog->ic_next;
3607 } while (iclog != log->l_iclog);
3611 /* return non-zero if log IOERROR transition had already happened */
3616 xlog_iclogs_empty(xlog_t *log)
3618 xlog_in_core_t *iclog;
3620 iclog = log->l_iclog;
3622 /* endianness does not matter here, zero is zero in
3625 if (iclog->ic_header.h_num_logops)
3627 iclog = iclog->ic_next;
3628 } while (iclog != log->l_iclog);