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"
27 #include "xfs_mount.h"
28 #include "xfs_error.h"
29 #include "xfs_log_priv.h"
30 #include "xfs_buf_item.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_log_recover.h"
35 #include "xfs_trans_priv.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
39 #include "xfs_trace.h"
41 kmem_zone_t
*xfs_log_ticket_zone
;
43 /* Local miscellaneous function prototypes */
44 STATIC
int xlog_commit_record(struct log
*log
, struct xlog_ticket
*ticket
,
45 xlog_in_core_t
**, xfs_lsn_t
*);
46 STATIC xlog_t
* xlog_alloc_log(xfs_mount_t
*mp
,
47 xfs_buftarg_t
*log_target
,
48 xfs_daddr_t blk_offset
,
50 STATIC
int xlog_space_left(struct log
*log
, atomic64_t
*head
);
51 STATIC
int xlog_sync(xlog_t
*log
, xlog_in_core_t
*iclog
);
52 STATIC
void xlog_dealloc_log(xlog_t
*log
);
54 /* local state machine functions */
55 STATIC
void xlog_state_done_syncing(xlog_in_core_t
*iclog
, int);
56 STATIC
void xlog_state_do_callback(xlog_t
*log
,int aborted
, xlog_in_core_t
*iclog
);
57 STATIC
int xlog_state_get_iclog_space(xlog_t
*log
,
59 xlog_in_core_t
**iclog
,
60 xlog_ticket_t
*ticket
,
63 STATIC
int xlog_state_release_iclog(xlog_t
*log
,
64 xlog_in_core_t
*iclog
);
65 STATIC
void xlog_state_switch_iclogs(xlog_t
*log
,
66 xlog_in_core_t
*iclog
,
68 STATIC
void xlog_state_want_sync(xlog_t
*log
, xlog_in_core_t
*iclog
);
70 /* local functions to manipulate grant head */
71 STATIC
int xlog_grant_log_space(xlog_t
*log
,
73 STATIC
void xlog_grant_push_ail(struct log
*log
,
75 STATIC
void xlog_regrant_reserve_log_space(xlog_t
*log
,
76 xlog_ticket_t
*ticket
);
77 STATIC
int xlog_regrant_write_log_space(xlog_t
*log
,
78 xlog_ticket_t
*ticket
);
79 STATIC
void xlog_ungrant_log_space(xlog_t
*log
,
80 xlog_ticket_t
*ticket
);
83 STATIC
void xlog_verify_dest_ptr(xlog_t
*log
, char *ptr
);
84 STATIC
void xlog_verify_grant_tail(struct log
*log
);
85 STATIC
void xlog_verify_iclog(xlog_t
*log
, xlog_in_core_t
*iclog
,
86 int count
, boolean_t syncing
);
87 STATIC
void xlog_verify_tail_lsn(xlog_t
*log
, xlog_in_core_t
*iclog
,
90 #define xlog_verify_dest_ptr(a,b)
91 #define xlog_verify_grant_tail(a)
92 #define xlog_verify_iclog(a,b,c,d)
93 #define xlog_verify_tail_lsn(a,b,c)
96 STATIC
int xlog_iclogs_empty(xlog_t
*log
);
104 int64_t head_val
= atomic64_read(head
);
110 xlog_crack_grant_head_val(head_val
, &cycle
, &space
);
114 space
+= log
->l_logsize
;
119 new = xlog_assign_grant_head_val(cycle
, space
);
120 head_val
= atomic64_cmpxchg(head
, old
, new);
121 } while (head_val
!= old
);
125 xlog_grant_add_space(
130 int64_t head_val
= atomic64_read(head
);
137 xlog_crack_grant_head_val(head_val
, &cycle
, &space
);
139 tmp
= log
->l_logsize
- space
;
148 new = xlog_assign_grant_head_val(cycle
, space
);
149 head_val
= atomic64_cmpxchg(head
, old
, new);
150 } while (head_val
!= old
);
158 struct xlog_ticket
*tic
;
161 list_for_each_entry(tic
, &log
->l_reserveq
, t_queue
) {
162 if (tic
->t_flags
& XLOG_TIC_PERM_RESERV
)
163 need_bytes
= tic
->t_unit_res
* tic
->t_cnt
;
165 need_bytes
= tic
->t_unit_res
;
167 if (*free_bytes
< need_bytes
)
169 *free_bytes
-= need_bytes
;
171 trace_xfs_log_grant_wake_up(log
, tic
);
172 wake_up_process(tic
->t_task
);
183 struct xlog_ticket
*tic
;
186 list_for_each_entry(tic
, &log
->l_writeq
, t_queue
) {
187 ASSERT(tic
->t_flags
& XLOG_TIC_PERM_RESERV
);
189 need_bytes
= tic
->t_unit_res
;
191 if (*free_bytes
< need_bytes
)
193 *free_bytes
-= need_bytes
;
195 trace_xfs_log_regrant_write_wake_up(log
, tic
);
196 wake_up_process(tic
->t_task
);
205 struct xlog_ticket
*tic
,
208 list_add_tail(&tic
->t_queue
, &log
->l_reserveq
);
211 if (XLOG_FORCED_SHUTDOWN(log
))
213 xlog_grant_push_ail(log
, need_bytes
);
215 __set_current_state(TASK_UNINTERRUPTIBLE
);
216 spin_unlock(&log
->l_grant_reserve_lock
);
218 XFS_STATS_INC(xs_sleep_logspace
);
220 trace_xfs_log_grant_sleep(log
, tic
);
222 trace_xfs_log_grant_wake(log
, tic
);
224 spin_lock(&log
->l_grant_reserve_lock
);
225 if (XLOG_FORCED_SHUTDOWN(log
))
227 } while (xlog_space_left(log
, &log
->l_grant_reserve_head
) < need_bytes
);
229 list_del_init(&tic
->t_queue
);
232 list_del_init(&tic
->t_queue
);
233 return XFS_ERROR(EIO
);
239 struct xlog_ticket
*tic
,
242 list_add_tail(&tic
->t_queue
, &log
->l_writeq
);
245 if (XLOG_FORCED_SHUTDOWN(log
))
247 xlog_grant_push_ail(log
, need_bytes
);
249 __set_current_state(TASK_UNINTERRUPTIBLE
);
250 spin_unlock(&log
->l_grant_write_lock
);
252 XFS_STATS_INC(xs_sleep_logspace
);
254 trace_xfs_log_regrant_write_sleep(log
, tic
);
256 trace_xfs_log_regrant_write_wake(log
, tic
);
258 spin_lock(&log
->l_grant_write_lock
);
259 if (XLOG_FORCED_SHUTDOWN(log
))
261 } while (xlog_space_left(log
, &log
->l_grant_write_head
) < need_bytes
);
263 list_del_init(&tic
->t_queue
);
266 list_del_init(&tic
->t_queue
);
267 return XFS_ERROR(EIO
);
271 xlog_tic_reset_res(xlog_ticket_t
*tic
)
274 tic
->t_res_arr_sum
= 0;
275 tic
->t_res_num_ophdrs
= 0;
279 xlog_tic_add_region(xlog_ticket_t
*tic
, uint len
, uint type
)
281 if (tic
->t_res_num
== XLOG_TIC_LEN_MAX
) {
282 /* add to overflow and start again */
283 tic
->t_res_o_flow
+= tic
->t_res_arr_sum
;
285 tic
->t_res_arr_sum
= 0;
288 tic
->t_res_arr
[tic
->t_res_num
].r_len
= len
;
289 tic
->t_res_arr
[tic
->t_res_num
].r_type
= type
;
290 tic
->t_res_arr_sum
+= len
;
297 * 1. currblock field gets updated at startup and after in-core logs
298 * marked as with WANT_SYNC.
302 * This routine is called when a user of a log manager ticket is done with
303 * the reservation. If the ticket was ever used, then a commit record for
304 * the associated transaction is written out as a log operation header with
305 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
306 * a given ticket. If the ticket was one with a permanent reservation, then
307 * a few operations are done differently. Permanent reservation tickets by
308 * default don't release the reservation. They just commit the current
309 * transaction with the belief that the reservation is still needed. A flag
310 * must be passed in before permanent reservations are actually released.
311 * When these type of tickets are not released, they need to be set into
312 * the inited state again. By doing this, a start record will be written
313 * out when the next write occurs.
317 struct xfs_mount
*mp
,
318 struct xlog_ticket
*ticket
,
319 struct xlog_in_core
**iclog
,
322 struct log
*log
= mp
->m_log
;
325 if (XLOG_FORCED_SHUTDOWN(log
) ||
327 * If nothing was ever written, don't write out commit record.
328 * If we get an error, just continue and give back the log ticket.
330 (((ticket
->t_flags
& XLOG_TIC_INITED
) == 0) &&
331 (xlog_commit_record(log
, ticket
, iclog
, &lsn
)))) {
332 lsn
= (xfs_lsn_t
) -1;
333 if (ticket
->t_flags
& XLOG_TIC_PERM_RESERV
) {
334 flags
|= XFS_LOG_REL_PERM_RESERV
;
339 if ((ticket
->t_flags
& XLOG_TIC_PERM_RESERV
) == 0 ||
340 (flags
& XFS_LOG_REL_PERM_RESERV
)) {
341 trace_xfs_log_done_nonperm(log
, ticket
);
344 * Release ticket if not permanent reservation or a specific
345 * request has been made to release a permanent reservation.
347 xlog_ungrant_log_space(log
, ticket
);
348 xfs_log_ticket_put(ticket
);
350 trace_xfs_log_done_perm(log
, ticket
);
352 xlog_regrant_reserve_log_space(log
, ticket
);
353 /* If this ticket was a permanent reservation and we aren't
354 * trying to release it, reset the inited flags; so next time
355 * we write, a start record will be written out.
357 ticket
->t_flags
|= XLOG_TIC_INITED
;
364 * Attaches a new iclog I/O completion callback routine during
365 * transaction commit. If the log is in error state, a non-zero
366 * return code is handed back and the caller is responsible for
367 * executing the callback at an appropriate time.
371 struct xfs_mount
*mp
,
372 struct xlog_in_core
*iclog
,
373 xfs_log_callback_t
*cb
)
377 spin_lock(&iclog
->ic_callback_lock
);
378 abortflg
= (iclog
->ic_state
& XLOG_STATE_IOERROR
);
380 ASSERT_ALWAYS((iclog
->ic_state
== XLOG_STATE_ACTIVE
) ||
381 (iclog
->ic_state
== XLOG_STATE_WANT_SYNC
));
383 *(iclog
->ic_callback_tail
) = cb
;
384 iclog
->ic_callback_tail
= &(cb
->cb_next
);
386 spin_unlock(&iclog
->ic_callback_lock
);
391 xfs_log_release_iclog(
392 struct xfs_mount
*mp
,
393 struct xlog_in_core
*iclog
)
395 if (xlog_state_release_iclog(mp
->m_log
, iclog
)) {
396 xfs_force_shutdown(mp
, SHUTDOWN_LOG_IO_ERROR
);
404 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
405 * to the reservation.
406 * 2. Potentially, push buffers at tail of log to disk.
408 * Each reservation is going to reserve extra space for a log record header.
409 * When writes happen to the on-disk log, we don't subtract the length of the
410 * log record header from any reservation. By wasting space in each
411 * reservation, we prevent over allocation problems.
415 struct xfs_mount
*mp
,
418 struct xlog_ticket
**ticket
,
423 struct log
*log
= mp
->m_log
;
424 struct xlog_ticket
*internal_ticket
;
427 ASSERT(client
== XFS_TRANSACTION
|| client
== XFS_LOG
);
429 if (XLOG_FORCED_SHUTDOWN(log
))
430 return XFS_ERROR(EIO
);
432 XFS_STATS_INC(xs_try_logspace
);
435 if (*ticket
!= NULL
) {
436 ASSERT(flags
& XFS_LOG_PERM_RESERV
);
437 internal_ticket
= *ticket
;
440 * this is a new transaction on the ticket, so we need to
441 * change the transaction ID so that the next transaction has a
442 * different TID in the log. Just add one to the existing tid
443 * so that we can see chains of rolling transactions in the log
446 internal_ticket
->t_tid
++;
448 trace_xfs_log_reserve(log
, internal_ticket
);
450 xlog_grant_push_ail(log
, internal_ticket
->t_unit_res
);
451 retval
= xlog_regrant_write_log_space(log
, internal_ticket
);
453 /* may sleep if need to allocate more tickets */
454 internal_ticket
= xlog_ticket_alloc(log
, unit_bytes
, cnt
,
456 KM_SLEEP
|KM_MAYFAIL
);
457 if (!internal_ticket
)
458 return XFS_ERROR(ENOMEM
);
459 internal_ticket
->t_trans_type
= t_type
;
460 *ticket
= internal_ticket
;
462 trace_xfs_log_reserve(log
, internal_ticket
);
464 xlog_grant_push_ail(log
,
465 (internal_ticket
->t_unit_res
*
466 internal_ticket
->t_cnt
));
467 retval
= xlog_grant_log_space(log
, internal_ticket
);
470 if (unlikely(retval
)) {
472 * If we are failing, make sure the ticket doesn't have any
473 * current reservations. We don't want to add this back
474 * when the ticket/ transaction gets cancelled.
476 internal_ticket
->t_curr_res
= 0;
477 /* ungrant will give back unit_res * t_cnt. */
478 internal_ticket
->t_cnt
= 0;
486 * Mount a log filesystem
488 * mp - ubiquitous xfs mount point structure
489 * log_target - buftarg of on-disk log device
490 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
491 * num_bblocks - Number of BBSIZE blocks in on-disk log
493 * Return error or zero.
498 xfs_buftarg_t
*log_target
,
499 xfs_daddr_t blk_offset
,
504 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
))
505 xfs_notice(mp
, "Mounting Filesystem");
508 "Mounting filesystem in no-recovery mode. Filesystem will be inconsistent.");
509 ASSERT(mp
->m_flags
& XFS_MOUNT_RDONLY
);
512 mp
->m_log
= xlog_alloc_log(mp
, log_target
, blk_offset
, num_bblks
);
513 if (IS_ERR(mp
->m_log
)) {
514 error
= -PTR_ERR(mp
->m_log
);
519 * Initialize the AIL now we have a log.
521 error
= xfs_trans_ail_init(mp
);
523 xfs_warn(mp
, "AIL initialisation failed: error %d", error
);
526 mp
->m_log
->l_ailp
= mp
->m_ail
;
529 * skip log recovery on a norecovery mount. pretend it all
532 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
)) {
533 int readonly
= (mp
->m_flags
& XFS_MOUNT_RDONLY
);
536 mp
->m_flags
&= ~XFS_MOUNT_RDONLY
;
538 error
= xlog_recover(mp
->m_log
);
541 mp
->m_flags
|= XFS_MOUNT_RDONLY
;
543 xfs_warn(mp
, "log mount/recovery failed: error %d",
545 goto out_destroy_ail
;
549 /* Normal transactions can now occur */
550 mp
->m_log
->l_flags
&= ~XLOG_ACTIVE_RECOVERY
;
553 * Now the log has been fully initialised and we know were our
554 * space grant counters are, we can initialise the permanent ticket
555 * needed for delayed logging to work.
557 xlog_cil_init_post_recovery(mp
->m_log
);
562 xfs_trans_ail_destroy(mp
);
564 xlog_dealloc_log(mp
->m_log
);
570 * Finish the recovery of the file system. This is separate from
571 * the xfs_log_mount() call, because it depends on the code in
572 * xfs_mountfs() to read in the root and real-time bitmap inodes
573 * between calling xfs_log_mount() and here.
575 * mp - ubiquitous xfs mount point structure
578 xfs_log_mount_finish(xfs_mount_t
*mp
)
582 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
))
583 error
= xlog_recover_finish(mp
->m_log
);
586 ASSERT(mp
->m_flags
& XFS_MOUNT_RDONLY
);
593 * Final log writes as part of unmount.
595 * Mark the filesystem clean as unmount happens. Note that during relocation
596 * this routine needs to be executed as part of source-bag while the
597 * deallocation must not be done until source-end.
601 * Unmount record used to have a string "Unmount filesystem--" in the
602 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
603 * We just write the magic number now since that particular field isn't
604 * currently architecture converted and "nUmount" is a bit foo.
605 * As far as I know, there weren't any dependencies on the old behaviour.
609 xfs_log_unmount_write(xfs_mount_t
*mp
)
611 xlog_t
*log
= mp
->m_log
;
612 xlog_in_core_t
*iclog
;
614 xlog_in_core_t
*first_iclog
;
616 xlog_ticket_t
*tic
= NULL
;
621 * Don't write out unmount record on read-only mounts.
622 * Or, if we are doing a forced umount (typically because of IO errors).
624 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
627 error
= _xfs_log_force(mp
, XFS_LOG_SYNC
, NULL
);
628 ASSERT(error
|| !(XLOG_FORCED_SHUTDOWN(log
)));
631 first_iclog
= iclog
= log
->l_iclog
;
633 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
634 ASSERT(iclog
->ic_state
& XLOG_STATE_ACTIVE
);
635 ASSERT(iclog
->ic_offset
== 0);
637 iclog
= iclog
->ic_next
;
638 } while (iclog
!= first_iclog
);
640 if (! (XLOG_FORCED_SHUTDOWN(log
))) {
641 error
= xfs_log_reserve(mp
, 600, 1, &tic
,
642 XFS_LOG
, 0, XLOG_UNMOUNT_REC_TYPE
);
644 /* the data section must be 32 bit size aligned */
648 __uint32_t pad2
; /* may as well make it 64 bits */
650 .magic
= XLOG_UNMOUNT_TYPE
,
652 struct xfs_log_iovec reg
= {
654 .i_len
= sizeof(magic
),
655 .i_type
= XLOG_REG_TYPE_UNMOUNT
,
657 struct xfs_log_vec vec
= {
662 /* remove inited flag */
664 error
= xlog_write(log
, &vec
, tic
, &lsn
,
665 NULL
, XLOG_UNMOUNT_TRANS
);
667 * At this point, we're umounting anyway,
668 * so there's no point in transitioning log state
669 * to IOERROR. Just continue...
674 xfs_alert(mp
, "%s: unmount record failed", __func__
);
677 spin_lock(&log
->l_icloglock
);
678 iclog
= log
->l_iclog
;
679 atomic_inc(&iclog
->ic_refcnt
);
680 xlog_state_want_sync(log
, iclog
);
681 spin_unlock(&log
->l_icloglock
);
682 error
= xlog_state_release_iclog(log
, iclog
);
684 spin_lock(&log
->l_icloglock
);
685 if (!(iclog
->ic_state
== XLOG_STATE_ACTIVE
||
686 iclog
->ic_state
== XLOG_STATE_DIRTY
)) {
687 if (!XLOG_FORCED_SHUTDOWN(log
)) {
688 xlog_wait(&iclog
->ic_force_wait
,
691 spin_unlock(&log
->l_icloglock
);
694 spin_unlock(&log
->l_icloglock
);
697 trace_xfs_log_umount_write(log
, tic
);
698 xlog_ungrant_log_space(log
, tic
);
699 xfs_log_ticket_put(tic
);
703 * We're already in forced_shutdown mode, couldn't
704 * even attempt to write out the unmount transaction.
706 * Go through the motions of sync'ing and releasing
707 * the iclog, even though no I/O will actually happen,
708 * we need to wait for other log I/Os that may already
709 * be in progress. Do this as a separate section of
710 * code so we'll know if we ever get stuck here that
711 * we're in this odd situation of trying to unmount
712 * a file system that went into forced_shutdown as
713 * the result of an unmount..
715 spin_lock(&log
->l_icloglock
);
716 iclog
= log
->l_iclog
;
717 atomic_inc(&iclog
->ic_refcnt
);
719 xlog_state_want_sync(log
, iclog
);
720 spin_unlock(&log
->l_icloglock
);
721 error
= xlog_state_release_iclog(log
, iclog
);
723 spin_lock(&log
->l_icloglock
);
725 if ( ! ( iclog
->ic_state
== XLOG_STATE_ACTIVE
726 || iclog
->ic_state
== XLOG_STATE_DIRTY
727 || iclog
->ic_state
== XLOG_STATE_IOERROR
) ) {
729 xlog_wait(&iclog
->ic_force_wait
,
732 spin_unlock(&log
->l_icloglock
);
737 } /* xfs_log_unmount_write */
740 * Deallocate log structures for unmount/relocation.
742 * We need to stop the aild from running before we destroy
743 * and deallocate the log as the aild references the log.
746 xfs_log_unmount(xfs_mount_t
*mp
)
748 xfs_trans_ail_destroy(mp
);
749 xlog_dealloc_log(mp
->m_log
);
754 struct xfs_mount
*mp
,
755 struct xfs_log_item
*item
,
757 const struct xfs_item_ops
*ops
)
759 item
->li_mountp
= mp
;
760 item
->li_ailp
= mp
->m_ail
;
761 item
->li_type
= type
;
765 INIT_LIST_HEAD(&item
->li_ail
);
766 INIT_LIST_HEAD(&item
->li_cil
);
770 * Wake up processes waiting for log space after we have moved the log tail.
774 struct xfs_mount
*mp
)
776 struct log
*log
= mp
->m_log
;
779 if (XLOG_FORCED_SHUTDOWN(log
))
782 if (!list_empty_careful(&log
->l_writeq
)) {
783 ASSERT(!(log
->l_flags
& XLOG_ACTIVE_RECOVERY
));
785 spin_lock(&log
->l_grant_write_lock
);
786 free_bytes
= xlog_space_left(log
, &log
->l_grant_write_head
);
787 xlog_writeq_wake(log
, &free_bytes
);
788 spin_unlock(&log
->l_grant_write_lock
);
791 if (!list_empty_careful(&log
->l_reserveq
)) {
792 ASSERT(!(log
->l_flags
& XLOG_ACTIVE_RECOVERY
));
794 spin_lock(&log
->l_grant_reserve_lock
);
795 free_bytes
= xlog_space_left(log
, &log
->l_grant_reserve_head
);
796 xlog_reserveq_wake(log
, &free_bytes
);
797 spin_unlock(&log
->l_grant_reserve_lock
);
802 * Determine if we have a transaction that has gone to disk
803 * that needs to be covered. To begin the transition to the idle state
804 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
805 * If we are then in a state where covering is needed, the caller is informed
806 * that dummy transactions are required to move the log into the idle state.
808 * Because this is called as part of the sync process, we should also indicate
809 * that dummy transactions should be issued in anything but the covered or
810 * idle states. This ensures that the log tail is accurately reflected in
811 * the log at the end of the sync, hence if a crash occurrs avoids replay
812 * of transactions where the metadata is already on disk.
815 xfs_log_need_covered(xfs_mount_t
*mp
)
818 xlog_t
*log
= mp
->m_log
;
820 if (!xfs_fs_writable(mp
))
823 spin_lock(&log
->l_icloglock
);
824 switch (log
->l_covered_state
) {
825 case XLOG_STATE_COVER_DONE
:
826 case XLOG_STATE_COVER_DONE2
:
827 case XLOG_STATE_COVER_IDLE
:
829 case XLOG_STATE_COVER_NEED
:
830 case XLOG_STATE_COVER_NEED2
:
831 if (!xfs_ail_min_lsn(log
->l_ailp
) &&
832 xlog_iclogs_empty(log
)) {
833 if (log
->l_covered_state
== XLOG_STATE_COVER_NEED
)
834 log
->l_covered_state
= XLOG_STATE_COVER_DONE
;
836 log
->l_covered_state
= XLOG_STATE_COVER_DONE2
;
843 spin_unlock(&log
->l_icloglock
);
848 * We may be holding the log iclog lock upon entering this routine.
851 xlog_assign_tail_lsn(
852 struct xfs_mount
*mp
)
855 struct log
*log
= mp
->m_log
;
858 * To make sure we always have a valid LSN for the log tail we keep
859 * track of the last LSN which was committed in log->l_last_sync_lsn,
860 * and use that when the AIL was empty and xfs_ail_min_lsn returns 0.
862 * If the AIL has been emptied we also need to wake any process
863 * waiting for this condition.
865 tail_lsn
= xfs_ail_min_lsn(mp
->m_ail
);
867 tail_lsn
= atomic64_read(&log
->l_last_sync_lsn
);
868 atomic64_set(&log
->l_tail_lsn
, tail_lsn
);
873 * Return the space in the log between the tail and the head. The head
874 * is passed in the cycle/bytes formal parms. In the special case where
875 * the reserve head has wrapped passed the tail, this calculation is no
876 * longer valid. In this case, just return 0 which means there is no space
877 * in the log. This works for all places where this function is called
878 * with the reserve head. Of course, if the write head were to ever
879 * wrap the tail, we should blow up. Rather than catch this case here,
880 * we depend on other ASSERTions in other parts of the code. XXXmiken
882 * This code also handles the case where the reservation head is behind
883 * the tail. The details of this case are described below, but the end
884 * result is that we return the size of the log as the amount of space left.
897 xlog_crack_grant_head(head
, &head_cycle
, &head_bytes
);
898 xlog_crack_atomic_lsn(&log
->l_tail_lsn
, &tail_cycle
, &tail_bytes
);
899 tail_bytes
= BBTOB(tail_bytes
);
900 if (tail_cycle
== head_cycle
&& head_bytes
>= tail_bytes
)
901 free_bytes
= log
->l_logsize
- (head_bytes
- tail_bytes
);
902 else if (tail_cycle
+ 1 < head_cycle
)
904 else if (tail_cycle
< head_cycle
) {
905 ASSERT(tail_cycle
== (head_cycle
- 1));
906 free_bytes
= tail_bytes
- head_bytes
;
909 * The reservation head is behind the tail.
910 * In this case we just want to return the size of the
911 * log as the amount of space left.
914 "xlog_space_left: head behind tail\n"
915 " tail_cycle = %d, tail_bytes = %d\n"
916 " GH cycle = %d, GH bytes = %d",
917 tail_cycle
, tail_bytes
, head_cycle
, head_bytes
);
919 free_bytes
= log
->l_logsize
;
926 * Log function which is called when an io completes.
928 * The log manager needs its own routine, in order to control what
929 * happens with the buffer after the write completes.
932 xlog_iodone(xfs_buf_t
*bp
)
934 xlog_in_core_t
*iclog
= bp
->b_fspriv
;
935 xlog_t
*l
= iclog
->ic_log
;
939 * Race to shutdown the filesystem if we see an error.
941 if (XFS_TEST_ERROR((xfs_buf_geterror(bp
)), l
->l_mp
,
942 XFS_ERRTAG_IODONE_IOERR
, XFS_RANDOM_IODONE_IOERR
)) {
943 xfs_buf_ioerror_alert(bp
, __func__
);
945 xfs_force_shutdown(l
->l_mp
, SHUTDOWN_LOG_IO_ERROR
);
947 * This flag will be propagated to the trans-committed
948 * callback routines to let them know that the log-commit
951 aborted
= XFS_LI_ABORTED
;
952 } else if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
953 aborted
= XFS_LI_ABORTED
;
956 /* log I/O is always issued ASYNC */
957 ASSERT(XFS_BUF_ISASYNC(bp
));
958 xlog_state_done_syncing(iclog
, aborted
);
960 * do not reference the buffer (bp) here as we could race
961 * with it being freed after writing the unmount record to the
968 * Return size of each in-core log record buffer.
970 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
972 * If the filesystem blocksize is too large, we may need to choose a
973 * larger size since the directory code currently logs entire blocks.
977 xlog_get_iclog_buffer_size(xfs_mount_t
*mp
,
983 if (mp
->m_logbufs
<= 0)
984 log
->l_iclog_bufs
= XLOG_MAX_ICLOGS
;
986 log
->l_iclog_bufs
= mp
->m_logbufs
;
989 * Buffer size passed in from mount system call.
991 if (mp
->m_logbsize
> 0) {
992 size
= log
->l_iclog_size
= mp
->m_logbsize
;
993 log
->l_iclog_size_log
= 0;
995 log
->l_iclog_size_log
++;
999 if (xfs_sb_version_haslogv2(&mp
->m_sb
)) {
1000 /* # headers = size / 32k
1001 * one header holds cycles from 32k of data
1004 xhdrs
= mp
->m_logbsize
/ XLOG_HEADER_CYCLE_SIZE
;
1005 if (mp
->m_logbsize
% XLOG_HEADER_CYCLE_SIZE
)
1007 log
->l_iclog_hsize
= xhdrs
<< BBSHIFT
;
1008 log
->l_iclog_heads
= xhdrs
;
1010 ASSERT(mp
->m_logbsize
<= XLOG_BIG_RECORD_BSIZE
);
1011 log
->l_iclog_hsize
= BBSIZE
;
1012 log
->l_iclog_heads
= 1;
1017 /* All machines use 32kB buffers by default. */
1018 log
->l_iclog_size
= XLOG_BIG_RECORD_BSIZE
;
1019 log
->l_iclog_size_log
= XLOG_BIG_RECORD_BSHIFT
;
1021 /* the default log size is 16k or 32k which is one header sector */
1022 log
->l_iclog_hsize
= BBSIZE
;
1023 log
->l_iclog_heads
= 1;
1026 /* are we being asked to make the sizes selected above visible? */
1027 if (mp
->m_logbufs
== 0)
1028 mp
->m_logbufs
= log
->l_iclog_bufs
;
1029 if (mp
->m_logbsize
== 0)
1030 mp
->m_logbsize
= log
->l_iclog_size
;
1031 } /* xlog_get_iclog_buffer_size */
1035 * This routine initializes some of the log structure for a given mount point.
1036 * Its primary purpose is to fill in enough, so recovery can occur. However,
1037 * some other stuff may be filled in too.
1040 xlog_alloc_log(xfs_mount_t
*mp
,
1041 xfs_buftarg_t
*log_target
,
1042 xfs_daddr_t blk_offset
,
1046 xlog_rec_header_t
*head
;
1047 xlog_in_core_t
**iclogp
;
1048 xlog_in_core_t
*iclog
, *prev_iclog
=NULL
;
1054 log
= kmem_zalloc(sizeof(xlog_t
), KM_MAYFAIL
);
1056 xfs_warn(mp
, "Log allocation failed: No memory!");
1061 log
->l_targ
= log_target
;
1062 log
->l_logsize
= BBTOB(num_bblks
);
1063 log
->l_logBBstart
= blk_offset
;
1064 log
->l_logBBsize
= num_bblks
;
1065 log
->l_covered_state
= XLOG_STATE_COVER_IDLE
;
1066 log
->l_flags
|= XLOG_ACTIVE_RECOVERY
;
1068 log
->l_prev_block
= -1;
1069 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1070 xlog_assign_atomic_lsn(&log
->l_tail_lsn
, 1, 0);
1071 xlog_assign_atomic_lsn(&log
->l_last_sync_lsn
, 1, 0);
1072 log
->l_curr_cycle
= 1; /* 0 is bad since this is initial value */
1073 xlog_assign_grant_head(&log
->l_grant_reserve_head
, 1, 0);
1074 xlog_assign_grant_head(&log
->l_grant_write_head
, 1, 0);
1075 INIT_LIST_HEAD(&log
->l_reserveq
);
1076 INIT_LIST_HEAD(&log
->l_writeq
);
1077 spin_lock_init(&log
->l_grant_reserve_lock
);
1078 spin_lock_init(&log
->l_grant_write_lock
);
1080 error
= EFSCORRUPTED
;
1081 if (xfs_sb_version_hassector(&mp
->m_sb
)) {
1082 log2_size
= mp
->m_sb
.sb_logsectlog
;
1083 if (log2_size
< BBSHIFT
) {
1084 xfs_warn(mp
, "Log sector size too small (0x%x < 0x%x)",
1085 log2_size
, BBSHIFT
);
1089 log2_size
-= BBSHIFT
;
1090 if (log2_size
> mp
->m_sectbb_log
) {
1091 xfs_warn(mp
, "Log sector size too large (0x%x > 0x%x)",
1092 log2_size
, mp
->m_sectbb_log
);
1096 /* for larger sector sizes, must have v2 or external log */
1097 if (log2_size
&& log
->l_logBBstart
> 0 &&
1098 !xfs_sb_version_haslogv2(&mp
->m_sb
)) {
1100 "log sector size (0x%x) invalid for configuration.",
1105 log
->l_sectBBsize
= 1 << log2_size
;
1107 xlog_get_iclog_buffer_size(mp
, log
);
1110 bp
= xfs_buf_alloc(mp
->m_logdev_targp
, 0, log
->l_iclog_size
, 0);
1113 bp
->b_iodone
= xlog_iodone
;
1114 ASSERT(xfs_buf_islocked(bp
));
1117 spin_lock_init(&log
->l_icloglock
);
1118 init_waitqueue_head(&log
->l_flush_wait
);
1120 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1121 ASSERT((XFS_BUF_SIZE(bp
) & BBMASK
) == 0);
1123 iclogp
= &log
->l_iclog
;
1125 * The amount of memory to allocate for the iclog structure is
1126 * rather funky due to the way the structure is defined. It is
1127 * done this way so that we can use different sizes for machines
1128 * with different amounts of memory. See the definition of
1129 * xlog_in_core_t in xfs_log_priv.h for details.
1131 ASSERT(log
->l_iclog_size
>= 4096);
1132 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
1133 *iclogp
= kmem_zalloc(sizeof(xlog_in_core_t
), KM_MAYFAIL
);
1135 goto out_free_iclog
;
1138 iclog
->ic_prev
= prev_iclog
;
1141 bp
= xfs_buf_get_uncached(mp
->m_logdev_targp
,
1142 log
->l_iclog_size
, 0);
1144 goto out_free_iclog
;
1146 bp
->b_iodone
= xlog_iodone
;
1148 iclog
->ic_data
= bp
->b_addr
;
1150 log
->l_iclog_bak
[i
] = (xfs_caddr_t
)&(iclog
->ic_header
);
1152 head
= &iclog
->ic_header
;
1153 memset(head
, 0, sizeof(xlog_rec_header_t
));
1154 head
->h_magicno
= cpu_to_be32(XLOG_HEADER_MAGIC_NUM
);
1155 head
->h_version
= cpu_to_be32(
1156 xfs_sb_version_haslogv2(&log
->l_mp
->m_sb
) ? 2 : 1);
1157 head
->h_size
= cpu_to_be32(log
->l_iclog_size
);
1159 head
->h_fmt
= cpu_to_be32(XLOG_FMT
);
1160 memcpy(&head
->h_fs_uuid
, &mp
->m_sb
.sb_uuid
, sizeof(uuid_t
));
1162 iclog
->ic_size
= XFS_BUF_SIZE(bp
) - log
->l_iclog_hsize
;
1163 iclog
->ic_state
= XLOG_STATE_ACTIVE
;
1164 iclog
->ic_log
= log
;
1165 atomic_set(&iclog
->ic_refcnt
, 0);
1166 spin_lock_init(&iclog
->ic_callback_lock
);
1167 iclog
->ic_callback_tail
= &(iclog
->ic_callback
);
1168 iclog
->ic_datap
= (char *)iclog
->ic_data
+ log
->l_iclog_hsize
;
1170 ASSERT(xfs_buf_islocked(iclog
->ic_bp
));
1171 init_waitqueue_head(&iclog
->ic_force_wait
);
1172 init_waitqueue_head(&iclog
->ic_write_wait
);
1174 iclogp
= &iclog
->ic_next
;
1176 *iclogp
= log
->l_iclog
; /* complete ring */
1177 log
->l_iclog
->ic_prev
= prev_iclog
; /* re-write 1st prev ptr */
1179 error
= xlog_cil_init(log
);
1181 goto out_free_iclog
;
1185 for (iclog
= log
->l_iclog
; iclog
; iclog
= prev_iclog
) {
1186 prev_iclog
= iclog
->ic_next
;
1188 xfs_buf_free(iclog
->ic_bp
);
1191 spinlock_destroy(&log
->l_icloglock
);
1192 xfs_buf_free(log
->l_xbuf
);
1196 return ERR_PTR(-error
);
1197 } /* xlog_alloc_log */
1201 * Write out the commit record of a transaction associated with the given
1202 * ticket. Return the lsn of the commit record.
1207 struct xlog_ticket
*ticket
,
1208 struct xlog_in_core
**iclog
,
1209 xfs_lsn_t
*commitlsnp
)
1211 struct xfs_mount
*mp
= log
->l_mp
;
1213 struct xfs_log_iovec reg
= {
1216 .i_type
= XLOG_REG_TYPE_COMMIT
,
1218 struct xfs_log_vec vec
= {
1223 ASSERT_ALWAYS(iclog
);
1224 error
= xlog_write(log
, &vec
, ticket
, commitlsnp
, iclog
,
1227 xfs_force_shutdown(mp
, SHUTDOWN_LOG_IO_ERROR
);
1232 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1233 * log space. This code pushes on the lsn which would supposedly free up
1234 * the 25% which we want to leave free. We may need to adopt a policy which
1235 * pushes on an lsn which is further along in the log once we reach the high
1236 * water mark. In this manner, we would be creating a low water mark.
1239 xlog_grant_push_ail(
1243 xfs_lsn_t threshold_lsn
= 0;
1244 xfs_lsn_t last_sync_lsn
;
1247 int threshold_block
;
1248 int threshold_cycle
;
1251 ASSERT(BTOBB(need_bytes
) < log
->l_logBBsize
);
1253 free_bytes
= xlog_space_left(log
, &log
->l_grant_reserve_head
);
1254 free_blocks
= BTOBBT(free_bytes
);
1257 * Set the threshold for the minimum number of free blocks in the
1258 * log to the maximum of what the caller needs, one quarter of the
1259 * log, and 256 blocks.
1261 free_threshold
= BTOBB(need_bytes
);
1262 free_threshold
= MAX(free_threshold
, (log
->l_logBBsize
>> 2));
1263 free_threshold
= MAX(free_threshold
, 256);
1264 if (free_blocks
>= free_threshold
)
1267 xlog_crack_atomic_lsn(&log
->l_tail_lsn
, &threshold_cycle
,
1269 threshold_block
+= free_threshold
;
1270 if (threshold_block
>= log
->l_logBBsize
) {
1271 threshold_block
-= log
->l_logBBsize
;
1272 threshold_cycle
+= 1;
1274 threshold_lsn
= xlog_assign_lsn(threshold_cycle
,
1277 * Don't pass in an lsn greater than the lsn of the last
1278 * log record known to be on disk. Use a snapshot of the last sync lsn
1279 * so that it doesn't change between the compare and the set.
1281 last_sync_lsn
= atomic64_read(&log
->l_last_sync_lsn
);
1282 if (XFS_LSN_CMP(threshold_lsn
, last_sync_lsn
) > 0)
1283 threshold_lsn
= last_sync_lsn
;
1286 * Get the transaction layer to kick the dirty buffers out to
1287 * disk asynchronously. No point in trying to do this if
1288 * the filesystem is shutting down.
1290 if (!XLOG_FORCED_SHUTDOWN(log
))
1291 xfs_ail_push(log
->l_ailp
, threshold_lsn
);
1295 * The bdstrat callback function for log bufs. This gives us a central
1296 * place to trap bufs in case we get hit by a log I/O error and need to
1297 * shutdown. Actually, in practice, even when we didn't get a log error,
1298 * we transition the iclogs to IOERROR state *after* flushing all existing
1299 * iclogs to disk. This is because we don't want anymore new transactions to be
1300 * started or completed afterwards.
1306 struct xlog_in_core
*iclog
= bp
->b_fspriv
;
1308 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
1309 xfs_buf_ioerror(bp
, EIO
);
1311 xfs_buf_ioend(bp
, 0);
1313 * It would seem logical to return EIO here, but we rely on
1314 * the log state machine to propagate I/O errors instead of
1320 xfs_buf_iorequest(bp
);
1325 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1326 * fashion. Previously, we should have moved the current iclog
1327 * ptr in the log to point to the next available iclog. This allows further
1328 * write to continue while this code syncs out an iclog ready to go.
1329 * Before an in-core log can be written out, the data section must be scanned
1330 * to save away the 1st word of each BBSIZE block into the header. We replace
1331 * it with the current cycle count. Each BBSIZE block is tagged with the
1332 * cycle count because there in an implicit assumption that drives will
1333 * guarantee that entire 512 byte blocks get written at once. In other words,
1334 * we can't have part of a 512 byte block written and part not written. By
1335 * tagging each block, we will know which blocks are valid when recovering
1336 * after an unclean shutdown.
1338 * This routine is single threaded on the iclog. No other thread can be in
1339 * this routine with the same iclog. Changing contents of iclog can there-
1340 * fore be done without grabbing the state machine lock. Updating the global
1341 * log will require grabbing the lock though.
1343 * The entire log manager uses a logical block numbering scheme. Only
1344 * log_sync (and then only bwrite()) know about the fact that the log may
1345 * not start with block zero on a given device. The log block start offset
1346 * is added immediately before calling bwrite().
1350 xlog_sync(xlog_t
*log
,
1351 xlog_in_core_t
*iclog
)
1353 xfs_caddr_t dptr
; /* pointer to byte sized element */
1356 uint count
; /* byte count of bwrite */
1357 uint count_init
; /* initial count before roundup */
1358 int roundoff
; /* roundoff to BB or stripe */
1359 int split
= 0; /* split write into two regions */
1361 int v2
= xfs_sb_version_haslogv2(&log
->l_mp
->m_sb
);
1363 XFS_STATS_INC(xs_log_writes
);
1364 ASSERT(atomic_read(&iclog
->ic_refcnt
) == 0);
1366 /* Add for LR header */
1367 count_init
= log
->l_iclog_hsize
+ iclog
->ic_offset
;
1369 /* Round out the log write size */
1370 if (v2
&& log
->l_mp
->m_sb
.sb_logsunit
> 1) {
1371 /* we have a v2 stripe unit to use */
1372 count
= XLOG_LSUNITTOB(log
, XLOG_BTOLSUNIT(log
, count_init
));
1374 count
= BBTOB(BTOBB(count_init
));
1376 roundoff
= count
- count_init
;
1377 ASSERT(roundoff
>= 0);
1378 ASSERT((v2
&& log
->l_mp
->m_sb
.sb_logsunit
> 1 &&
1379 roundoff
< log
->l_mp
->m_sb
.sb_logsunit
)
1381 (log
->l_mp
->m_sb
.sb_logsunit
<= 1 &&
1382 roundoff
< BBTOB(1)));
1384 /* move grant heads by roundoff in sync */
1385 xlog_grant_add_space(log
, &log
->l_grant_reserve_head
, roundoff
);
1386 xlog_grant_add_space(log
, &log
->l_grant_write_head
, roundoff
);
1388 /* put cycle number in every block */
1389 xlog_pack_data(log
, iclog
, roundoff
);
1391 /* real byte length */
1393 iclog
->ic_header
.h_len
=
1394 cpu_to_be32(iclog
->ic_offset
+ roundoff
);
1396 iclog
->ic_header
.h_len
=
1397 cpu_to_be32(iclog
->ic_offset
);
1401 XFS_BUF_SET_ADDR(bp
, BLOCK_LSN(be64_to_cpu(iclog
->ic_header
.h_lsn
)));
1403 XFS_STATS_ADD(xs_log_blocks
, BTOBB(count
));
1405 /* Do we need to split this write into 2 parts? */
1406 if (XFS_BUF_ADDR(bp
) + BTOBB(count
) > log
->l_logBBsize
) {
1407 split
= count
- (BBTOB(log
->l_logBBsize
- XFS_BUF_ADDR(bp
)));
1408 count
= BBTOB(log
->l_logBBsize
- XFS_BUF_ADDR(bp
));
1409 iclog
->ic_bwritecnt
= 2; /* split into 2 writes */
1411 iclog
->ic_bwritecnt
= 1;
1413 XFS_BUF_SET_COUNT(bp
, count
);
1414 bp
->b_fspriv
= iclog
;
1415 XFS_BUF_ZEROFLAGS(bp
);
1417 bp
->b_flags
|= XBF_SYNCIO
;
1419 if (log
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
) {
1420 bp
->b_flags
|= XBF_FUA
;
1423 * Flush the data device before flushing the log to make
1424 * sure all meta data written back from the AIL actually made
1425 * it to disk before stamping the new log tail LSN into the
1426 * log buffer. For an external log we need to issue the
1427 * flush explicitly, and unfortunately synchronously here;
1428 * for an internal log we can simply use the block layer
1429 * state machine for preflushes.
1431 if (log
->l_mp
->m_logdev_targp
!= log
->l_mp
->m_ddev_targp
)
1432 xfs_blkdev_issue_flush(log
->l_mp
->m_ddev_targp
);
1434 bp
->b_flags
|= XBF_FLUSH
;
1437 ASSERT(XFS_BUF_ADDR(bp
) <= log
->l_logBBsize
-1);
1438 ASSERT(XFS_BUF_ADDR(bp
) + BTOBB(count
) <= log
->l_logBBsize
);
1440 xlog_verify_iclog(log
, iclog
, count
, B_TRUE
);
1442 /* account for log which doesn't start at block #0 */
1443 XFS_BUF_SET_ADDR(bp
, XFS_BUF_ADDR(bp
) + log
->l_logBBstart
);
1445 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1450 error
= xlog_bdstrat(bp
);
1452 xfs_buf_ioerror_alert(bp
, "xlog_sync");
1456 bp
= iclog
->ic_log
->l_xbuf
;
1457 XFS_BUF_SET_ADDR(bp
, 0); /* logical 0 */
1458 xfs_buf_associate_memory(bp
,
1459 (char *)&iclog
->ic_header
+ count
, split
);
1460 bp
->b_fspriv
= iclog
;
1461 XFS_BUF_ZEROFLAGS(bp
);
1463 bp
->b_flags
|= XBF_SYNCIO
;
1464 if (log
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
)
1465 bp
->b_flags
|= XBF_FUA
;
1468 * Bump the cycle numbers at the start of each block
1469 * since this part of the buffer is at the start of
1470 * a new cycle. Watch out for the header magic number
1473 for (i
= 0; i
< split
; i
+= BBSIZE
) {
1474 be32_add_cpu((__be32
*)dptr
, 1);
1475 if (be32_to_cpu(*(__be32
*)dptr
) == XLOG_HEADER_MAGIC_NUM
)
1476 be32_add_cpu((__be32
*)dptr
, 1);
1480 ASSERT(XFS_BUF_ADDR(bp
) <= log
->l_logBBsize
-1);
1481 ASSERT(XFS_BUF_ADDR(bp
) + BTOBB(count
) <= log
->l_logBBsize
);
1483 /* account for internal log which doesn't start at block #0 */
1484 XFS_BUF_SET_ADDR(bp
, XFS_BUF_ADDR(bp
) + log
->l_logBBstart
);
1486 error
= xlog_bdstrat(bp
);
1488 xfs_buf_ioerror_alert(bp
, "xlog_sync (split)");
1497 * Deallocate a log structure
1500 xlog_dealloc_log(xlog_t
*log
)
1502 xlog_in_core_t
*iclog
, *next_iclog
;
1505 xlog_cil_destroy(log
);
1508 * always need to ensure that the extra buffer does not point to memory
1509 * owned by another log buffer before we free it.
1511 xfs_buf_set_empty(log
->l_xbuf
, log
->l_iclog_size
);
1512 xfs_buf_free(log
->l_xbuf
);
1514 iclog
= log
->l_iclog
;
1515 for (i
=0; i
<log
->l_iclog_bufs
; i
++) {
1516 xfs_buf_free(iclog
->ic_bp
);
1517 next_iclog
= iclog
->ic_next
;
1521 spinlock_destroy(&log
->l_icloglock
);
1523 log
->l_mp
->m_log
= NULL
;
1525 } /* xlog_dealloc_log */
1528 * Update counters atomically now that memcpy is done.
1532 xlog_state_finish_copy(xlog_t
*log
,
1533 xlog_in_core_t
*iclog
,
1537 spin_lock(&log
->l_icloglock
);
1539 be32_add_cpu(&iclog
->ic_header
.h_num_logops
, record_cnt
);
1540 iclog
->ic_offset
+= copy_bytes
;
1542 spin_unlock(&log
->l_icloglock
);
1543 } /* xlog_state_finish_copy */
1549 * print out info relating to regions written which consume
1554 struct xfs_mount
*mp
,
1555 struct xlog_ticket
*ticket
)
1558 uint ophdr_spc
= ticket
->t_res_num_ophdrs
* (uint
)sizeof(xlog_op_header_t
);
1560 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1561 static char *res_type_str
[XLOG_REG_TYPE_MAX
] = {
1582 static char *trans_type_str
[XFS_TRANS_TYPE_MAX
] = {
1626 "xlog_write: reservation summary:\n"
1627 " trans type = %s (%u)\n"
1628 " unit res = %d bytes\n"
1629 " current res = %d bytes\n"
1630 " total reg = %u bytes (o/flow = %u bytes)\n"
1631 " ophdrs = %u (ophdr space = %u bytes)\n"
1632 " ophdr + reg = %u bytes\n"
1633 " num regions = %u\n",
1634 ((ticket
->t_trans_type
<= 0 ||
1635 ticket
->t_trans_type
> XFS_TRANS_TYPE_MAX
) ?
1636 "bad-trans-type" : trans_type_str
[ticket
->t_trans_type
-1]),
1637 ticket
->t_trans_type
,
1640 ticket
->t_res_arr_sum
, ticket
->t_res_o_flow
,
1641 ticket
->t_res_num_ophdrs
, ophdr_spc
,
1642 ticket
->t_res_arr_sum
+
1643 ticket
->t_res_o_flow
+ ophdr_spc
,
1646 for (i
= 0; i
< ticket
->t_res_num
; i
++) {
1647 uint r_type
= ticket
->t_res_arr
[i
].r_type
;
1648 xfs_warn(mp
, "region[%u]: %s - %u bytes\n", i
,
1649 ((r_type
<= 0 || r_type
> XLOG_REG_TYPE_MAX
) ?
1650 "bad-rtype" : res_type_str
[r_type
-1]),
1651 ticket
->t_res_arr
[i
].r_len
);
1654 xfs_alert_tag(mp
, XFS_PTAG_LOGRES
,
1655 "xlog_write: reservation ran out. Need to up reservation");
1656 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
1660 * Calculate the potential space needed by the log vector. Each region gets
1661 * its own xlog_op_header_t and may need to be double word aligned.
1664 xlog_write_calc_vec_length(
1665 struct xlog_ticket
*ticket
,
1666 struct xfs_log_vec
*log_vector
)
1668 struct xfs_log_vec
*lv
;
1673 /* acct for start rec of xact */
1674 if (ticket
->t_flags
& XLOG_TIC_INITED
)
1677 for (lv
= log_vector
; lv
; lv
= lv
->lv_next
) {
1678 headers
+= lv
->lv_niovecs
;
1680 for (i
= 0; i
< lv
->lv_niovecs
; i
++) {
1681 struct xfs_log_iovec
*vecp
= &lv
->lv_iovecp
[i
];
1684 xlog_tic_add_region(ticket
, vecp
->i_len
, vecp
->i_type
);
1688 ticket
->t_res_num_ophdrs
+= headers
;
1689 len
+= headers
* sizeof(struct xlog_op_header
);
1695 * If first write for transaction, insert start record We can't be trying to
1696 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1699 xlog_write_start_rec(
1700 struct xlog_op_header
*ophdr
,
1701 struct xlog_ticket
*ticket
)
1703 if (!(ticket
->t_flags
& XLOG_TIC_INITED
))
1706 ophdr
->oh_tid
= cpu_to_be32(ticket
->t_tid
);
1707 ophdr
->oh_clientid
= ticket
->t_clientid
;
1709 ophdr
->oh_flags
= XLOG_START_TRANS
;
1712 ticket
->t_flags
&= ~XLOG_TIC_INITED
;
1714 return sizeof(struct xlog_op_header
);
1717 static xlog_op_header_t
*
1718 xlog_write_setup_ophdr(
1720 struct xlog_op_header
*ophdr
,
1721 struct xlog_ticket
*ticket
,
1724 ophdr
->oh_tid
= cpu_to_be32(ticket
->t_tid
);
1725 ophdr
->oh_clientid
= ticket
->t_clientid
;
1728 /* are we copying a commit or unmount record? */
1729 ophdr
->oh_flags
= flags
;
1732 * We've seen logs corrupted with bad transaction client ids. This
1733 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1734 * and shut down the filesystem.
1736 switch (ophdr
->oh_clientid
) {
1737 case XFS_TRANSACTION
:
1743 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1744 ophdr
->oh_clientid
, ticket
);
1752 * Set up the parameters of the region copy into the log. This has
1753 * to handle region write split across multiple log buffers - this
1754 * state is kept external to this function so that this code can
1755 * can be written in an obvious, self documenting manner.
1758 xlog_write_setup_copy(
1759 struct xlog_ticket
*ticket
,
1760 struct xlog_op_header
*ophdr
,
1761 int space_available
,
1765 int *last_was_partial_copy
,
1766 int *bytes_consumed
)
1770 still_to_copy
= space_required
- *bytes_consumed
;
1771 *copy_off
= *bytes_consumed
;
1773 if (still_to_copy
<= space_available
) {
1774 /* write of region completes here */
1775 *copy_len
= still_to_copy
;
1776 ophdr
->oh_len
= cpu_to_be32(*copy_len
);
1777 if (*last_was_partial_copy
)
1778 ophdr
->oh_flags
|= (XLOG_END_TRANS
|XLOG_WAS_CONT_TRANS
);
1779 *last_was_partial_copy
= 0;
1780 *bytes_consumed
= 0;
1784 /* partial write of region, needs extra log op header reservation */
1785 *copy_len
= space_available
;
1786 ophdr
->oh_len
= cpu_to_be32(*copy_len
);
1787 ophdr
->oh_flags
|= XLOG_CONTINUE_TRANS
;
1788 if (*last_was_partial_copy
)
1789 ophdr
->oh_flags
|= XLOG_WAS_CONT_TRANS
;
1790 *bytes_consumed
+= *copy_len
;
1791 (*last_was_partial_copy
)++;
1793 /* account for new log op header */
1794 ticket
->t_curr_res
-= sizeof(struct xlog_op_header
);
1795 ticket
->t_res_num_ophdrs
++;
1797 return sizeof(struct xlog_op_header
);
1801 xlog_write_copy_finish(
1803 struct xlog_in_core
*iclog
,
1808 int *partial_copy_len
,
1810 struct xlog_in_core
**commit_iclog
)
1812 if (*partial_copy
) {
1814 * This iclog has already been marked WANT_SYNC by
1815 * xlog_state_get_iclog_space.
1817 xlog_state_finish_copy(log
, iclog
, *record_cnt
, *data_cnt
);
1820 return xlog_state_release_iclog(log
, iclog
);
1824 *partial_copy_len
= 0;
1826 if (iclog
->ic_size
- log_offset
<= sizeof(xlog_op_header_t
)) {
1827 /* no more space in this iclog - push it. */
1828 xlog_state_finish_copy(log
, iclog
, *record_cnt
, *data_cnt
);
1832 spin_lock(&log
->l_icloglock
);
1833 xlog_state_want_sync(log
, iclog
);
1834 spin_unlock(&log
->l_icloglock
);
1837 return xlog_state_release_iclog(log
, iclog
);
1838 ASSERT(flags
& XLOG_COMMIT_TRANS
);
1839 *commit_iclog
= iclog
;
1846 * Write some region out to in-core log
1848 * This will be called when writing externally provided regions or when
1849 * writing out a commit record for a given transaction.
1851 * General algorithm:
1852 * 1. Find total length of this write. This may include adding to the
1853 * lengths passed in.
1854 * 2. Check whether we violate the tickets reservation.
1855 * 3. While writing to this iclog
1856 * A. Reserve as much space in this iclog as can get
1857 * B. If this is first write, save away start lsn
1858 * C. While writing this region:
1859 * 1. If first write of transaction, write start record
1860 * 2. Write log operation header (header per region)
1861 * 3. Find out if we can fit entire region into this iclog
1862 * 4. Potentially, verify destination memcpy ptr
1863 * 5. Memcpy (partial) region
1864 * 6. If partial copy, release iclog; otherwise, continue
1865 * copying more regions into current iclog
1866 * 4. Mark want sync bit (in simulation mode)
1867 * 5. Release iclog for potential flush to on-disk log.
1870 * 1. Panic if reservation is overrun. This should never happen since
1871 * reservation amounts are generated internal to the filesystem.
1873 * 1. Tickets are single threaded data structures.
1874 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1875 * syncing routine. When a single log_write region needs to span
1876 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1877 * on all log operation writes which don't contain the end of the
1878 * region. The XLOG_END_TRANS bit is used for the in-core log
1879 * operation which contains the end of the continued log_write region.
1880 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1881 * we don't really know exactly how much space will be used. As a result,
1882 * we don't update ic_offset until the end when we know exactly how many
1883 * bytes have been written out.
1888 struct xfs_log_vec
*log_vector
,
1889 struct xlog_ticket
*ticket
,
1890 xfs_lsn_t
*start_lsn
,
1891 struct xlog_in_core
**commit_iclog
,
1894 struct xlog_in_core
*iclog
= NULL
;
1895 struct xfs_log_iovec
*vecp
;
1896 struct xfs_log_vec
*lv
;
1899 int partial_copy
= 0;
1900 int partial_copy_len
= 0;
1908 len
= xlog_write_calc_vec_length(ticket
, log_vector
);
1911 * Region headers and bytes are already accounted for.
1912 * We only need to take into account start records and
1913 * split regions in this function.
1915 if (ticket
->t_flags
& XLOG_TIC_INITED
)
1916 ticket
->t_curr_res
-= sizeof(xlog_op_header_t
);
1919 * Commit record headers need to be accounted for. These
1920 * come in as separate writes so are easy to detect.
1922 if (flags
& (XLOG_COMMIT_TRANS
| XLOG_UNMOUNT_TRANS
))
1923 ticket
->t_curr_res
-= sizeof(xlog_op_header_t
);
1925 if (ticket
->t_curr_res
< 0)
1926 xlog_print_tic_res(log
->l_mp
, ticket
);
1930 vecp
= lv
->lv_iovecp
;
1931 while (lv
&& index
< lv
->lv_niovecs
) {
1935 error
= xlog_state_get_iclog_space(log
, len
, &iclog
, ticket
,
1936 &contwr
, &log_offset
);
1940 ASSERT(log_offset
<= iclog
->ic_size
- 1);
1941 ptr
= iclog
->ic_datap
+ log_offset
;
1943 /* start_lsn is the first lsn written to. That's all we need. */
1945 *start_lsn
= be64_to_cpu(iclog
->ic_header
.h_lsn
);
1948 * This loop writes out as many regions as can fit in the amount
1949 * of space which was allocated by xlog_state_get_iclog_space().
1951 while (lv
&& index
< lv
->lv_niovecs
) {
1952 struct xfs_log_iovec
*reg
= &vecp
[index
];
1953 struct xlog_op_header
*ophdr
;
1958 ASSERT(reg
->i_len
% sizeof(__int32_t
) == 0);
1959 ASSERT((unsigned long)ptr
% sizeof(__int32_t
) == 0);
1961 start_rec_copy
= xlog_write_start_rec(ptr
, ticket
);
1962 if (start_rec_copy
) {
1964 xlog_write_adv_cnt(&ptr
, &len
, &log_offset
,
1968 ophdr
= xlog_write_setup_ophdr(log
, ptr
, ticket
, flags
);
1970 return XFS_ERROR(EIO
);
1972 xlog_write_adv_cnt(&ptr
, &len
, &log_offset
,
1973 sizeof(struct xlog_op_header
));
1975 len
+= xlog_write_setup_copy(ticket
, ophdr
,
1976 iclog
->ic_size
-log_offset
,
1978 ©_off
, ©_len
,
1981 xlog_verify_dest_ptr(log
, ptr
);
1984 ASSERT(copy_len
>= 0);
1985 memcpy(ptr
, reg
->i_addr
+ copy_off
, copy_len
);
1986 xlog_write_adv_cnt(&ptr
, &len
, &log_offset
, copy_len
);
1988 copy_len
+= start_rec_copy
+ sizeof(xlog_op_header_t
);
1990 data_cnt
+= contwr
? copy_len
: 0;
1992 error
= xlog_write_copy_finish(log
, iclog
, flags
,
1993 &record_cnt
, &data_cnt
,
2002 * if we had a partial copy, we need to get more iclog
2003 * space but we don't want to increment the region
2004 * index because there is still more is this region to
2007 * If we completed writing this region, and we flushed
2008 * the iclog (indicated by resetting of the record
2009 * count), then we also need to get more log space. If
2010 * this was the last record, though, we are done and
2016 if (++index
== lv
->lv_niovecs
) {
2020 vecp
= lv
->lv_iovecp
;
2022 if (record_cnt
== 0) {
2032 xlog_state_finish_copy(log
, iclog
, record_cnt
, data_cnt
);
2034 return xlog_state_release_iclog(log
, iclog
);
2036 ASSERT(flags
& XLOG_COMMIT_TRANS
);
2037 *commit_iclog
= iclog
;
2042 /*****************************************************************************
2044 * State Machine functions
2046 *****************************************************************************
2049 /* Clean iclogs starting from the head. This ordering must be
2050 * maintained, so an iclog doesn't become ACTIVE beyond one that
2051 * is SYNCING. This is also required to maintain the notion that we use
2052 * a ordered wait queue to hold off would be writers to the log when every
2053 * iclog is trying to sync to disk.
2055 * State Change: DIRTY -> ACTIVE
2058 xlog_state_clean_log(xlog_t
*log
)
2060 xlog_in_core_t
*iclog
;
2063 iclog
= log
->l_iclog
;
2065 if (iclog
->ic_state
== XLOG_STATE_DIRTY
) {
2066 iclog
->ic_state
= XLOG_STATE_ACTIVE
;
2067 iclog
->ic_offset
= 0;
2068 ASSERT(iclog
->ic_callback
== NULL
);
2070 * If the number of ops in this iclog indicate it just
2071 * contains the dummy transaction, we can
2072 * change state into IDLE (the second time around).
2073 * Otherwise we should change the state into
2075 * We don't need to cover the dummy.
2078 (be32_to_cpu(iclog
->ic_header
.h_num_logops
) ==
2083 * We have two dirty iclogs so start over
2084 * This could also be num of ops indicates
2085 * this is not the dummy going out.
2089 iclog
->ic_header
.h_num_logops
= 0;
2090 memset(iclog
->ic_header
.h_cycle_data
, 0,
2091 sizeof(iclog
->ic_header
.h_cycle_data
));
2092 iclog
->ic_header
.h_lsn
= 0;
2093 } else if (iclog
->ic_state
== XLOG_STATE_ACTIVE
)
2096 break; /* stop cleaning */
2097 iclog
= iclog
->ic_next
;
2098 } while (iclog
!= log
->l_iclog
);
2100 /* log is locked when we are called */
2102 * Change state for the dummy log recording.
2103 * We usually go to NEED. But we go to NEED2 if the changed indicates
2104 * we are done writing the dummy record.
2105 * If we are done with the second dummy recored (DONE2), then
2109 switch (log
->l_covered_state
) {
2110 case XLOG_STATE_COVER_IDLE
:
2111 case XLOG_STATE_COVER_NEED
:
2112 case XLOG_STATE_COVER_NEED2
:
2113 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2116 case XLOG_STATE_COVER_DONE
:
2118 log
->l_covered_state
= XLOG_STATE_COVER_NEED2
;
2120 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2123 case XLOG_STATE_COVER_DONE2
:
2125 log
->l_covered_state
= XLOG_STATE_COVER_IDLE
;
2127 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2134 } /* xlog_state_clean_log */
2137 xlog_get_lowest_lsn(
2140 xlog_in_core_t
*lsn_log
;
2141 xfs_lsn_t lowest_lsn
, lsn
;
2143 lsn_log
= log
->l_iclog
;
2146 if (!(lsn_log
->ic_state
& (XLOG_STATE_ACTIVE
|XLOG_STATE_DIRTY
))) {
2147 lsn
= be64_to_cpu(lsn_log
->ic_header
.h_lsn
);
2148 if ((lsn
&& !lowest_lsn
) ||
2149 (XFS_LSN_CMP(lsn
, lowest_lsn
) < 0)) {
2153 lsn_log
= lsn_log
->ic_next
;
2154 } while (lsn_log
!= log
->l_iclog
);
2160 xlog_state_do_callback(
2163 xlog_in_core_t
*ciclog
)
2165 xlog_in_core_t
*iclog
;
2166 xlog_in_core_t
*first_iclog
; /* used to know when we've
2167 * processed all iclogs once */
2168 xfs_log_callback_t
*cb
, *cb_next
;
2170 xfs_lsn_t lowest_lsn
;
2171 int ioerrors
; /* counter: iclogs with errors */
2172 int loopdidcallbacks
; /* flag: inner loop did callbacks*/
2173 int funcdidcallbacks
; /* flag: function did callbacks */
2174 int repeats
; /* for issuing console warnings if
2175 * looping too many times */
2178 spin_lock(&log
->l_icloglock
);
2179 first_iclog
= iclog
= log
->l_iclog
;
2181 funcdidcallbacks
= 0;
2186 * Scan all iclogs starting with the one pointed to by the
2187 * log. Reset this starting point each time the log is
2188 * unlocked (during callbacks).
2190 * Keep looping through iclogs until one full pass is made
2191 * without running any callbacks.
2193 first_iclog
= log
->l_iclog
;
2194 iclog
= log
->l_iclog
;
2195 loopdidcallbacks
= 0;
2200 /* skip all iclogs in the ACTIVE & DIRTY states */
2201 if (iclog
->ic_state
&
2202 (XLOG_STATE_ACTIVE
|XLOG_STATE_DIRTY
)) {
2203 iclog
= iclog
->ic_next
;
2208 * Between marking a filesystem SHUTDOWN and stopping
2209 * the log, we do flush all iclogs to disk (if there
2210 * wasn't a log I/O error). So, we do want things to
2211 * go smoothly in case of just a SHUTDOWN w/o a
2214 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
2216 * Can only perform callbacks in order. Since
2217 * this iclog is not in the DONE_SYNC/
2218 * DO_CALLBACK state, we skip the rest and
2219 * just try to clean up. If we set our iclog
2220 * to DO_CALLBACK, we will not process it when
2221 * we retry since a previous iclog is in the
2222 * CALLBACK and the state cannot change since
2223 * we are holding the l_icloglock.
2225 if (!(iclog
->ic_state
&
2226 (XLOG_STATE_DONE_SYNC
|
2227 XLOG_STATE_DO_CALLBACK
))) {
2228 if (ciclog
&& (ciclog
->ic_state
==
2229 XLOG_STATE_DONE_SYNC
)) {
2230 ciclog
->ic_state
= XLOG_STATE_DO_CALLBACK
;
2235 * We now have an iclog that is in either the
2236 * DO_CALLBACK or DONE_SYNC states. The other
2237 * states (WANT_SYNC, SYNCING, or CALLBACK were
2238 * caught by the above if and are going to
2239 * clean (i.e. we aren't doing their callbacks)
2244 * We will do one more check here to see if we
2245 * have chased our tail around.
2248 lowest_lsn
= xlog_get_lowest_lsn(log
);
2250 XFS_LSN_CMP(lowest_lsn
,
2251 be64_to_cpu(iclog
->ic_header
.h_lsn
)) < 0) {
2252 iclog
= iclog
->ic_next
;
2253 continue; /* Leave this iclog for
2257 iclog
->ic_state
= XLOG_STATE_CALLBACK
;
2261 * update the last_sync_lsn before we drop the
2262 * icloglock to ensure we are the only one that
2265 ASSERT(XFS_LSN_CMP(atomic64_read(&log
->l_last_sync_lsn
),
2266 be64_to_cpu(iclog
->ic_header
.h_lsn
)) <= 0);
2267 atomic64_set(&log
->l_last_sync_lsn
,
2268 be64_to_cpu(iclog
->ic_header
.h_lsn
));
2273 spin_unlock(&log
->l_icloglock
);
2276 * Keep processing entries in the callback list until
2277 * we come around and it is empty. We need to
2278 * atomically see that the list is empty and change the
2279 * state to DIRTY so that we don't miss any more
2280 * callbacks being added.
2282 spin_lock(&iclog
->ic_callback_lock
);
2283 cb
= iclog
->ic_callback
;
2285 iclog
->ic_callback_tail
= &(iclog
->ic_callback
);
2286 iclog
->ic_callback
= NULL
;
2287 spin_unlock(&iclog
->ic_callback_lock
);
2289 /* perform callbacks in the order given */
2290 for (; cb
; cb
= cb_next
) {
2291 cb_next
= cb
->cb_next
;
2292 cb
->cb_func(cb
->cb_arg
, aborted
);
2294 spin_lock(&iclog
->ic_callback_lock
);
2295 cb
= iclog
->ic_callback
;
2301 spin_lock(&log
->l_icloglock
);
2302 ASSERT(iclog
->ic_callback
== NULL
);
2303 spin_unlock(&iclog
->ic_callback_lock
);
2304 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
))
2305 iclog
->ic_state
= XLOG_STATE_DIRTY
;
2308 * Transition from DIRTY to ACTIVE if applicable.
2309 * NOP if STATE_IOERROR.
2311 xlog_state_clean_log(log
);
2313 /* wake up threads waiting in xfs_log_force() */
2314 wake_up_all(&iclog
->ic_force_wait
);
2316 iclog
= iclog
->ic_next
;
2317 } while (first_iclog
!= iclog
);
2319 if (repeats
> 5000) {
2320 flushcnt
+= repeats
;
2323 "%s: possible infinite loop (%d iterations)",
2324 __func__
, flushcnt
);
2326 } while (!ioerrors
&& loopdidcallbacks
);
2329 * make one last gasp attempt to see if iclogs are being left in
2333 if (funcdidcallbacks
) {
2334 first_iclog
= iclog
= log
->l_iclog
;
2336 ASSERT(iclog
->ic_state
!= XLOG_STATE_DO_CALLBACK
);
2338 * Terminate the loop if iclogs are found in states
2339 * which will cause other threads to clean up iclogs.
2341 * SYNCING - i/o completion will go through logs
2342 * DONE_SYNC - interrupt thread should be waiting for
2344 * IOERROR - give up hope all ye who enter here
2346 if (iclog
->ic_state
== XLOG_STATE_WANT_SYNC
||
2347 iclog
->ic_state
== XLOG_STATE_SYNCING
||
2348 iclog
->ic_state
== XLOG_STATE_DONE_SYNC
||
2349 iclog
->ic_state
== XLOG_STATE_IOERROR
)
2351 iclog
= iclog
->ic_next
;
2352 } while (first_iclog
!= iclog
);
2356 if (log
->l_iclog
->ic_state
& (XLOG_STATE_ACTIVE
|XLOG_STATE_IOERROR
))
2358 spin_unlock(&log
->l_icloglock
);
2361 wake_up_all(&log
->l_flush_wait
);
2366 * Finish transitioning this iclog to the dirty state.
2368 * Make sure that we completely execute this routine only when this is
2369 * the last call to the iclog. There is a good chance that iclog flushes,
2370 * when we reach the end of the physical log, get turned into 2 separate
2371 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2372 * routine. By using the reference count bwritecnt, we guarantee that only
2373 * the second completion goes through.
2375 * Callbacks could take time, so they are done outside the scope of the
2376 * global state machine log lock.
2379 xlog_state_done_syncing(
2380 xlog_in_core_t
*iclog
,
2383 xlog_t
*log
= iclog
->ic_log
;
2385 spin_lock(&log
->l_icloglock
);
2387 ASSERT(iclog
->ic_state
== XLOG_STATE_SYNCING
||
2388 iclog
->ic_state
== XLOG_STATE_IOERROR
);
2389 ASSERT(atomic_read(&iclog
->ic_refcnt
) == 0);
2390 ASSERT(iclog
->ic_bwritecnt
== 1 || iclog
->ic_bwritecnt
== 2);
2394 * If we got an error, either on the first buffer, or in the case of
2395 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2396 * and none should ever be attempted to be written to disk
2399 if (iclog
->ic_state
!= XLOG_STATE_IOERROR
) {
2400 if (--iclog
->ic_bwritecnt
== 1) {
2401 spin_unlock(&log
->l_icloglock
);
2404 iclog
->ic_state
= XLOG_STATE_DONE_SYNC
;
2408 * Someone could be sleeping prior to writing out the next
2409 * iclog buffer, we wake them all, one will get to do the
2410 * I/O, the others get to wait for the result.
2412 wake_up_all(&iclog
->ic_write_wait
);
2413 spin_unlock(&log
->l_icloglock
);
2414 xlog_state_do_callback(log
, aborted
, iclog
); /* also cleans log */
2415 } /* xlog_state_done_syncing */
2419 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2420 * sleep. We wait on the flush queue on the head iclog as that should be
2421 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2422 * we will wait here and all new writes will sleep until a sync completes.
2424 * The in-core logs are used in a circular fashion. They are not used
2425 * out-of-order even when an iclog past the head is free.
2428 * * log_offset where xlog_write() can start writing into the in-core
2430 * * in-core log pointer to which xlog_write() should write.
2431 * * boolean indicating this is a continued write to an in-core log.
2432 * If this is the last write, then the in-core log's offset field
2433 * needs to be incremented, depending on the amount of data which
2437 xlog_state_get_iclog_space(xlog_t
*log
,
2439 xlog_in_core_t
**iclogp
,
2440 xlog_ticket_t
*ticket
,
2441 int *continued_write
,
2445 xlog_rec_header_t
*head
;
2446 xlog_in_core_t
*iclog
;
2450 spin_lock(&log
->l_icloglock
);
2451 if (XLOG_FORCED_SHUTDOWN(log
)) {
2452 spin_unlock(&log
->l_icloglock
);
2453 return XFS_ERROR(EIO
);
2456 iclog
= log
->l_iclog
;
2457 if (iclog
->ic_state
!= XLOG_STATE_ACTIVE
) {
2458 XFS_STATS_INC(xs_log_noiclogs
);
2460 /* Wait for log writes to have flushed */
2461 xlog_wait(&log
->l_flush_wait
, &log
->l_icloglock
);
2465 head
= &iclog
->ic_header
;
2467 atomic_inc(&iclog
->ic_refcnt
); /* prevents sync */
2468 log_offset
= iclog
->ic_offset
;
2470 /* On the 1st write to an iclog, figure out lsn. This works
2471 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2472 * committing to. If the offset is set, that's how many blocks
2475 if (log_offset
== 0) {
2476 ticket
->t_curr_res
-= log
->l_iclog_hsize
;
2477 xlog_tic_add_region(ticket
,
2479 XLOG_REG_TYPE_LRHEADER
);
2480 head
->h_cycle
= cpu_to_be32(log
->l_curr_cycle
);
2481 head
->h_lsn
= cpu_to_be64(
2482 xlog_assign_lsn(log
->l_curr_cycle
, log
->l_curr_block
));
2483 ASSERT(log
->l_curr_block
>= 0);
2486 /* If there is enough room to write everything, then do it. Otherwise,
2487 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2488 * bit is on, so this will get flushed out. Don't update ic_offset
2489 * until you know exactly how many bytes get copied. Therefore, wait
2490 * until later to update ic_offset.
2492 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2493 * can fit into remaining data section.
2495 if (iclog
->ic_size
- iclog
->ic_offset
< 2*sizeof(xlog_op_header_t
)) {
2496 xlog_state_switch_iclogs(log
, iclog
, iclog
->ic_size
);
2499 * If I'm the only one writing to this iclog, sync it to disk.
2500 * We need to do an atomic compare and decrement here to avoid
2501 * racing with concurrent atomic_dec_and_lock() calls in
2502 * xlog_state_release_iclog() when there is more than one
2503 * reference to the iclog.
2505 if (!atomic_add_unless(&iclog
->ic_refcnt
, -1, 1)) {
2506 /* we are the only one */
2507 spin_unlock(&log
->l_icloglock
);
2508 error
= xlog_state_release_iclog(log
, iclog
);
2512 spin_unlock(&log
->l_icloglock
);
2517 /* Do we have enough room to write the full amount in the remainder
2518 * of this iclog? Or must we continue a write on the next iclog and
2519 * mark this iclog as completely taken? In the case where we switch
2520 * iclogs (to mark it taken), this particular iclog will release/sync
2521 * to disk in xlog_write().
2523 if (len
<= iclog
->ic_size
- iclog
->ic_offset
) {
2524 *continued_write
= 0;
2525 iclog
->ic_offset
+= len
;
2527 *continued_write
= 1;
2528 xlog_state_switch_iclogs(log
, iclog
, iclog
->ic_size
);
2532 ASSERT(iclog
->ic_offset
<= iclog
->ic_size
);
2533 spin_unlock(&log
->l_icloglock
);
2535 *logoffsetp
= log_offset
;
2537 } /* xlog_state_get_iclog_space */
2540 * Atomically get the log space required for a log ticket.
2542 * Once a ticket gets put onto the reserveq, it will only return after the
2543 * needed reservation is satisfied.
2545 * This function is structured so that it has a lock free fast path. This is
2546 * necessary because every new transaction reservation will come through this
2547 * path. Hence any lock will be globally hot if we take it unconditionally on
2550 * As tickets are only ever moved on and off the reserveq under the
2551 * l_grant_reserve_lock, we only need to take that lock if we are going to add
2552 * the ticket to the queue and sleep. We can avoid taking the lock if the ticket
2553 * was never added to the reserveq because the t_queue list head will be empty
2554 * and we hold the only reference to it so it can safely be checked unlocked.
2557 xlog_grant_log_space(
2559 struct xlog_ticket
*tic
)
2561 int free_bytes
, need_bytes
;
2564 ASSERT(!(log
->l_flags
& XLOG_ACTIVE_RECOVERY
));
2566 trace_xfs_log_grant_enter(log
, tic
);
2569 * If there are other waiters on the queue then give them a chance at
2570 * logspace before us. Wake up the first waiters, if we do not wake
2571 * up all the waiters then go to sleep waiting for more free space,
2572 * otherwise try to get some space for this transaction.
2574 need_bytes
= tic
->t_unit_res
;
2575 if (tic
->t_flags
& XFS_LOG_PERM_RESERV
)
2576 need_bytes
*= tic
->t_ocnt
;
2577 free_bytes
= xlog_space_left(log
, &log
->l_grant_reserve_head
);
2578 if (!list_empty_careful(&log
->l_reserveq
)) {
2579 spin_lock(&log
->l_grant_reserve_lock
);
2580 if (!xlog_reserveq_wake(log
, &free_bytes
) ||
2581 free_bytes
< need_bytes
)
2582 error
= xlog_reserveq_wait(log
, tic
, need_bytes
);
2583 spin_unlock(&log
->l_grant_reserve_lock
);
2584 } else if (free_bytes
< need_bytes
) {
2585 spin_lock(&log
->l_grant_reserve_lock
);
2586 error
= xlog_reserveq_wait(log
, tic
, need_bytes
);
2587 spin_unlock(&log
->l_grant_reserve_lock
);
2592 xlog_grant_add_space(log
, &log
->l_grant_reserve_head
, need_bytes
);
2593 xlog_grant_add_space(log
, &log
->l_grant_write_head
, need_bytes
);
2594 trace_xfs_log_grant_exit(log
, tic
);
2595 xlog_verify_grant_tail(log
);
2600 * Replenish the byte reservation required by moving the grant write head.
2602 * Similar to xlog_grant_log_space, the function is structured to have a lock
2606 xlog_regrant_write_log_space(
2608 struct xlog_ticket
*tic
)
2610 int free_bytes
, need_bytes
;
2613 tic
->t_curr_res
= tic
->t_unit_res
;
2614 xlog_tic_reset_res(tic
);
2619 ASSERT(!(log
->l_flags
& XLOG_ACTIVE_RECOVERY
));
2621 trace_xfs_log_regrant_write_enter(log
, tic
);
2624 * If there are other waiters on the queue then give them a chance at
2625 * logspace before us. Wake up the first waiters, if we do not wake
2626 * up all the waiters then go to sleep waiting for more free space,
2627 * otherwise try to get some space for this transaction.
2629 need_bytes
= tic
->t_unit_res
;
2630 free_bytes
= xlog_space_left(log
, &log
->l_grant_write_head
);
2631 if (!list_empty_careful(&log
->l_writeq
)) {
2632 spin_lock(&log
->l_grant_write_lock
);
2633 if (!xlog_writeq_wake(log
, &free_bytes
) ||
2634 free_bytes
< need_bytes
)
2635 error
= xlog_writeq_wait(log
, tic
, need_bytes
);
2636 spin_unlock(&log
->l_grant_write_lock
);
2637 } else if (free_bytes
< need_bytes
) {
2638 spin_lock(&log
->l_grant_write_lock
);
2639 error
= xlog_writeq_wait(log
, tic
, need_bytes
);
2640 spin_unlock(&log
->l_grant_write_lock
);
2646 xlog_grant_add_space(log
, &log
->l_grant_write_head
, need_bytes
);
2647 trace_xfs_log_regrant_write_exit(log
, tic
);
2648 xlog_verify_grant_tail(log
);
2652 /* The first cnt-1 times through here we don't need to
2653 * move the grant write head because the permanent
2654 * reservation has reserved cnt times the unit amount.
2655 * Release part of current permanent unit reservation and
2656 * reset current reservation to be one units worth. Also
2657 * move grant reservation head forward.
2660 xlog_regrant_reserve_log_space(xlog_t
*log
,
2661 xlog_ticket_t
*ticket
)
2663 trace_xfs_log_regrant_reserve_enter(log
, ticket
);
2665 if (ticket
->t_cnt
> 0)
2668 xlog_grant_sub_space(log
, &log
->l_grant_reserve_head
,
2669 ticket
->t_curr_res
);
2670 xlog_grant_sub_space(log
, &log
->l_grant_write_head
,
2671 ticket
->t_curr_res
);
2672 ticket
->t_curr_res
= ticket
->t_unit_res
;
2673 xlog_tic_reset_res(ticket
);
2675 trace_xfs_log_regrant_reserve_sub(log
, ticket
);
2677 /* just return if we still have some of the pre-reserved space */
2678 if (ticket
->t_cnt
> 0)
2681 xlog_grant_add_space(log
, &log
->l_grant_reserve_head
,
2682 ticket
->t_unit_res
);
2684 trace_xfs_log_regrant_reserve_exit(log
, ticket
);
2686 ticket
->t_curr_res
= ticket
->t_unit_res
;
2687 xlog_tic_reset_res(ticket
);
2688 } /* xlog_regrant_reserve_log_space */
2692 * Give back the space left from a reservation.
2694 * All the information we need to make a correct determination of space left
2695 * is present. For non-permanent reservations, things are quite easy. The
2696 * count should have been decremented to zero. We only need to deal with the
2697 * space remaining in the current reservation part of the ticket. If the
2698 * ticket contains a permanent reservation, there may be left over space which
2699 * needs to be released. A count of N means that N-1 refills of the current
2700 * reservation can be done before we need to ask for more space. The first
2701 * one goes to fill up the first current reservation. Once we run out of
2702 * space, the count will stay at zero and the only space remaining will be
2703 * in the current reservation field.
2706 xlog_ungrant_log_space(xlog_t
*log
,
2707 xlog_ticket_t
*ticket
)
2711 if (ticket
->t_cnt
> 0)
2714 trace_xfs_log_ungrant_enter(log
, ticket
);
2715 trace_xfs_log_ungrant_sub(log
, ticket
);
2718 * If this is a permanent reservation ticket, we may be able to free
2719 * up more space based on the remaining count.
2721 bytes
= ticket
->t_curr_res
;
2722 if (ticket
->t_cnt
> 0) {
2723 ASSERT(ticket
->t_flags
& XLOG_TIC_PERM_RESERV
);
2724 bytes
+= ticket
->t_unit_res
*ticket
->t_cnt
;
2727 xlog_grant_sub_space(log
, &log
->l_grant_reserve_head
, bytes
);
2728 xlog_grant_sub_space(log
, &log
->l_grant_write_head
, bytes
);
2730 trace_xfs_log_ungrant_exit(log
, ticket
);
2732 xfs_log_space_wake(log
->l_mp
);
2736 * Flush iclog to disk if this is the last reference to the given iclog and
2737 * the WANT_SYNC bit is set.
2739 * When this function is entered, the iclog is not necessarily in the
2740 * WANT_SYNC state. It may be sitting around waiting to get filled.
2745 xlog_state_release_iclog(
2747 xlog_in_core_t
*iclog
)
2749 int sync
= 0; /* do we sync? */
2751 if (iclog
->ic_state
& XLOG_STATE_IOERROR
)
2752 return XFS_ERROR(EIO
);
2754 ASSERT(atomic_read(&iclog
->ic_refcnt
) > 0);
2755 if (!atomic_dec_and_lock(&iclog
->ic_refcnt
, &log
->l_icloglock
))
2758 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2759 spin_unlock(&log
->l_icloglock
);
2760 return XFS_ERROR(EIO
);
2762 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2763 iclog
->ic_state
== XLOG_STATE_WANT_SYNC
);
2765 if (iclog
->ic_state
== XLOG_STATE_WANT_SYNC
) {
2766 /* update tail before writing to iclog */
2767 xfs_lsn_t tail_lsn
= xlog_assign_tail_lsn(log
->l_mp
);
2769 iclog
->ic_state
= XLOG_STATE_SYNCING
;
2770 iclog
->ic_header
.h_tail_lsn
= cpu_to_be64(tail_lsn
);
2771 xlog_verify_tail_lsn(log
, iclog
, tail_lsn
);
2772 /* cycle incremented when incrementing curr_block */
2774 spin_unlock(&log
->l_icloglock
);
2777 * We let the log lock go, so it's possible that we hit a log I/O
2778 * error or some other SHUTDOWN condition that marks the iclog
2779 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2780 * this iclog has consistent data, so we ignore IOERROR
2781 * flags after this point.
2784 return xlog_sync(log
, iclog
);
2786 } /* xlog_state_release_iclog */
2790 * This routine will mark the current iclog in the ring as WANT_SYNC
2791 * and move the current iclog pointer to the next iclog in the ring.
2792 * When this routine is called from xlog_state_get_iclog_space(), the
2793 * exact size of the iclog has not yet been determined. All we know is
2794 * that every data block. We have run out of space in this log record.
2797 xlog_state_switch_iclogs(xlog_t
*log
,
2798 xlog_in_core_t
*iclog
,
2801 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
);
2803 eventual_size
= iclog
->ic_offset
;
2804 iclog
->ic_state
= XLOG_STATE_WANT_SYNC
;
2805 iclog
->ic_header
.h_prev_block
= cpu_to_be32(log
->l_prev_block
);
2806 log
->l_prev_block
= log
->l_curr_block
;
2807 log
->l_prev_cycle
= log
->l_curr_cycle
;
2809 /* roll log?: ic_offset changed later */
2810 log
->l_curr_block
+= BTOBB(eventual_size
)+BTOBB(log
->l_iclog_hsize
);
2812 /* Round up to next log-sunit */
2813 if (xfs_sb_version_haslogv2(&log
->l_mp
->m_sb
) &&
2814 log
->l_mp
->m_sb
.sb_logsunit
> 1) {
2815 __uint32_t sunit_bb
= BTOBB(log
->l_mp
->m_sb
.sb_logsunit
);
2816 log
->l_curr_block
= roundup(log
->l_curr_block
, sunit_bb
);
2819 if (log
->l_curr_block
>= log
->l_logBBsize
) {
2820 log
->l_curr_cycle
++;
2821 if (log
->l_curr_cycle
== XLOG_HEADER_MAGIC_NUM
)
2822 log
->l_curr_cycle
++;
2823 log
->l_curr_block
-= log
->l_logBBsize
;
2824 ASSERT(log
->l_curr_block
>= 0);
2826 ASSERT(iclog
== log
->l_iclog
);
2827 log
->l_iclog
= iclog
->ic_next
;
2828 } /* xlog_state_switch_iclogs */
2831 * Write out all data in the in-core log as of this exact moment in time.
2833 * Data may be written to the in-core log during this call. However,
2834 * we don't guarantee this data will be written out. A change from past
2835 * implementation means this routine will *not* write out zero length LRs.
2837 * Basically, we try and perform an intelligent scan of the in-core logs.
2838 * If we determine there is no flushable data, we just return. There is no
2839 * flushable data if:
2841 * 1. the current iclog is active and has no data; the previous iclog
2842 * is in the active or dirty state.
2843 * 2. the current iclog is drity, and the previous iclog is in the
2844 * active or dirty state.
2848 * 1. the current iclog is not in the active nor dirty state.
2849 * 2. the current iclog dirty, and the previous iclog is not in the
2850 * active nor dirty state.
2851 * 3. the current iclog is active, and there is another thread writing
2852 * to this particular iclog.
2853 * 4. a) the current iclog is active and has no other writers
2854 * b) when we return from flushing out this iclog, it is still
2855 * not in the active nor dirty state.
2859 struct xfs_mount
*mp
,
2863 struct log
*log
= mp
->m_log
;
2864 struct xlog_in_core
*iclog
;
2867 XFS_STATS_INC(xs_log_force
);
2869 xlog_cil_force(log
);
2871 spin_lock(&log
->l_icloglock
);
2873 iclog
= log
->l_iclog
;
2874 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2875 spin_unlock(&log
->l_icloglock
);
2876 return XFS_ERROR(EIO
);
2879 /* If the head iclog is not active nor dirty, we just attach
2880 * ourselves to the head and go to sleep.
2882 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2883 iclog
->ic_state
== XLOG_STATE_DIRTY
) {
2885 * If the head is dirty or (active and empty), then
2886 * we need to look at the previous iclog. If the previous
2887 * iclog is active or dirty we are done. There is nothing
2888 * to sync out. Otherwise, we attach ourselves to the
2889 * previous iclog and go to sleep.
2891 if (iclog
->ic_state
== XLOG_STATE_DIRTY
||
2892 (atomic_read(&iclog
->ic_refcnt
) == 0
2893 && iclog
->ic_offset
== 0)) {
2894 iclog
= iclog
->ic_prev
;
2895 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2896 iclog
->ic_state
== XLOG_STATE_DIRTY
)
2901 if (atomic_read(&iclog
->ic_refcnt
) == 0) {
2902 /* We are the only one with access to this
2903 * iclog. Flush it out now. There should
2904 * be a roundoff of zero to show that someone
2905 * has already taken care of the roundoff from
2906 * the previous sync.
2908 atomic_inc(&iclog
->ic_refcnt
);
2909 lsn
= be64_to_cpu(iclog
->ic_header
.h_lsn
);
2910 xlog_state_switch_iclogs(log
, iclog
, 0);
2911 spin_unlock(&log
->l_icloglock
);
2913 if (xlog_state_release_iclog(log
, iclog
))
2914 return XFS_ERROR(EIO
);
2918 spin_lock(&log
->l_icloglock
);
2919 if (be64_to_cpu(iclog
->ic_header
.h_lsn
) == lsn
&&
2920 iclog
->ic_state
!= XLOG_STATE_DIRTY
)
2925 /* Someone else is writing to this iclog.
2926 * Use its call to flush out the data. However,
2927 * the other thread may not force out this LR,
2928 * so we mark it WANT_SYNC.
2930 xlog_state_switch_iclogs(log
, iclog
, 0);
2936 /* By the time we come around again, the iclog could've been filled
2937 * which would give it another lsn. If we have a new lsn, just
2938 * return because the relevant data has been flushed.
2941 if (flags
& XFS_LOG_SYNC
) {
2943 * We must check if we're shutting down here, before
2944 * we wait, while we're holding the l_icloglock.
2945 * Then we check again after waking up, in case our
2946 * sleep was disturbed by a bad news.
2948 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2949 spin_unlock(&log
->l_icloglock
);
2950 return XFS_ERROR(EIO
);
2952 XFS_STATS_INC(xs_log_force_sleep
);
2953 xlog_wait(&iclog
->ic_force_wait
, &log
->l_icloglock
);
2955 * No need to grab the log lock here since we're
2956 * only deciding whether or not to return EIO
2957 * and the memory read should be atomic.
2959 if (iclog
->ic_state
& XLOG_STATE_IOERROR
)
2960 return XFS_ERROR(EIO
);
2966 spin_unlock(&log
->l_icloglock
);
2972 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2973 * about errors or whether the log was flushed or not. This is the normal
2974 * interface to use when trying to unpin items or move the log forward.
2983 error
= _xfs_log_force(mp
, flags
, NULL
);
2985 xfs_warn(mp
, "%s: error %d returned.", __func__
, error
);
2989 * Force the in-core log to disk for a specific LSN.
2991 * Find in-core log with lsn.
2992 * If it is in the DIRTY state, just return.
2993 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2994 * state and go to sleep or return.
2995 * If it is in any other state, go to sleep or return.
2997 * Synchronous forces are implemented with a signal variable. All callers
2998 * to force a given lsn to disk will wait on a the sv attached to the
2999 * specific in-core log. When given in-core log finally completes its
3000 * write to disk, that thread will wake up all threads waiting on the
3005 struct xfs_mount
*mp
,
3010 struct log
*log
= mp
->m_log
;
3011 struct xlog_in_core
*iclog
;
3012 int already_slept
= 0;
3016 XFS_STATS_INC(xs_log_force
);
3018 lsn
= xlog_cil_force_lsn(log
, lsn
);
3019 if (lsn
== NULLCOMMITLSN
)
3023 spin_lock(&log
->l_icloglock
);
3024 iclog
= log
->l_iclog
;
3025 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3026 spin_unlock(&log
->l_icloglock
);
3027 return XFS_ERROR(EIO
);
3031 if (be64_to_cpu(iclog
->ic_header
.h_lsn
) != lsn
) {
3032 iclog
= iclog
->ic_next
;
3036 if (iclog
->ic_state
== XLOG_STATE_DIRTY
) {
3037 spin_unlock(&log
->l_icloglock
);
3041 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
) {
3043 * We sleep here if we haven't already slept (e.g.
3044 * this is the first time we've looked at the correct
3045 * iclog buf) and the buffer before us is going to
3046 * be sync'ed. The reason for this is that if we
3047 * are doing sync transactions here, by waiting for
3048 * the previous I/O to complete, we can allow a few
3049 * more transactions into this iclog before we close
3052 * Otherwise, we mark the buffer WANT_SYNC, and bump
3053 * up the refcnt so we can release the log (which
3054 * drops the ref count). The state switch keeps new
3055 * transaction commits from using this buffer. When
3056 * the current commits finish writing into the buffer,
3057 * the refcount will drop to zero and the buffer will
3060 if (!already_slept
&&
3061 (iclog
->ic_prev
->ic_state
&
3062 (XLOG_STATE_WANT_SYNC
| XLOG_STATE_SYNCING
))) {
3063 ASSERT(!(iclog
->ic_state
& XLOG_STATE_IOERROR
));
3065 XFS_STATS_INC(xs_log_force_sleep
);
3067 xlog_wait(&iclog
->ic_prev
->ic_write_wait
,
3074 atomic_inc(&iclog
->ic_refcnt
);
3075 xlog_state_switch_iclogs(log
, iclog
, 0);
3076 spin_unlock(&log
->l_icloglock
);
3077 if (xlog_state_release_iclog(log
, iclog
))
3078 return XFS_ERROR(EIO
);
3081 spin_lock(&log
->l_icloglock
);
3084 if ((flags
& XFS_LOG_SYNC
) && /* sleep */
3086 (XLOG_STATE_ACTIVE
| XLOG_STATE_DIRTY
))) {
3088 * Don't wait on completion if we know that we've
3089 * gotten a log write error.
3091 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3092 spin_unlock(&log
->l_icloglock
);
3093 return XFS_ERROR(EIO
);
3095 XFS_STATS_INC(xs_log_force_sleep
);
3096 xlog_wait(&iclog
->ic_force_wait
, &log
->l_icloglock
);
3098 * No need to grab the log lock here since we're
3099 * only deciding whether or not to return EIO
3100 * and the memory read should be atomic.
3102 if (iclog
->ic_state
& XLOG_STATE_IOERROR
)
3103 return XFS_ERROR(EIO
);
3107 } else { /* just return */
3108 spin_unlock(&log
->l_icloglock
);
3112 } while (iclog
!= log
->l_iclog
);
3114 spin_unlock(&log
->l_icloglock
);
3119 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3120 * about errors or whether the log was flushed or not. This is the normal
3121 * interface to use when trying to unpin items or move the log forward.
3131 error
= _xfs_log_force_lsn(mp
, lsn
, flags
, NULL
);
3133 xfs_warn(mp
, "%s: error %d returned.", __func__
, error
);
3137 * Called when we want to mark the current iclog as being ready to sync to
3141 xlog_state_want_sync(xlog_t
*log
, xlog_in_core_t
*iclog
)
3143 assert_spin_locked(&log
->l_icloglock
);
3145 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
) {
3146 xlog_state_switch_iclogs(log
, iclog
, 0);
3148 ASSERT(iclog
->ic_state
&
3149 (XLOG_STATE_WANT_SYNC
|XLOG_STATE_IOERROR
));
3154 /*****************************************************************************
3158 *****************************************************************************
3162 * Free a used ticket when its refcount falls to zero.
3166 xlog_ticket_t
*ticket
)
3168 ASSERT(atomic_read(&ticket
->t_ref
) > 0);
3169 if (atomic_dec_and_test(&ticket
->t_ref
))
3170 kmem_zone_free(xfs_log_ticket_zone
, ticket
);
3175 xlog_ticket_t
*ticket
)
3177 ASSERT(atomic_read(&ticket
->t_ref
) > 0);
3178 atomic_inc(&ticket
->t_ref
);
3183 * Allocate and initialise a new log ticket.
3194 struct xlog_ticket
*tic
;
3198 tic
= kmem_zone_zalloc(xfs_log_ticket_zone
, alloc_flags
);
3203 * Permanent reservations have up to 'cnt'-1 active log operations
3204 * in the log. A unit in this case is the amount of space for one
3205 * of these log operations. Normal reservations have a cnt of 1
3206 * and their unit amount is the total amount of space required.
3208 * The following lines of code account for non-transaction data
3209 * which occupy space in the on-disk log.
3211 * Normal form of a transaction is:
3212 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3213 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3215 * We need to account for all the leadup data and trailer data
3216 * around the transaction data.
3217 * And then we need to account for the worst case in terms of using
3219 * The worst case will happen if:
3220 * - the placement of the transaction happens to be such that the
3221 * roundoff is at its maximum
3222 * - the transaction data is synced before the commit record is synced
3223 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3224 * Therefore the commit record is in its own Log Record.
3225 * This can happen as the commit record is called with its
3226 * own region to xlog_write().
3227 * This then means that in the worst case, roundoff can happen for
3228 * the commit-rec as well.
3229 * The commit-rec is smaller than padding in this scenario and so it is
3230 * not added separately.
3233 /* for trans header */
3234 unit_bytes
+= sizeof(xlog_op_header_t
);
3235 unit_bytes
+= sizeof(xfs_trans_header_t
);
3238 unit_bytes
+= sizeof(xlog_op_header_t
);
3241 * for LR headers - the space for data in an iclog is the size minus
3242 * the space used for the headers. If we use the iclog size, then we
3243 * undercalculate the number of headers required.
3245 * Furthermore - the addition of op headers for split-recs might
3246 * increase the space required enough to require more log and op
3247 * headers, so take that into account too.
3249 * IMPORTANT: This reservation makes the assumption that if this
3250 * transaction is the first in an iclog and hence has the LR headers
3251 * accounted to it, then the remaining space in the iclog is
3252 * exclusively for this transaction. i.e. if the transaction is larger
3253 * than the iclog, it will be the only thing in that iclog.
3254 * Fundamentally, this means we must pass the entire log vector to
3255 * xlog_write to guarantee this.
3257 iclog_space
= log
->l_iclog_size
- log
->l_iclog_hsize
;
3258 num_headers
= howmany(unit_bytes
, iclog_space
);
3260 /* for split-recs - ophdrs added when data split over LRs */
3261 unit_bytes
+= sizeof(xlog_op_header_t
) * num_headers
;
3263 /* add extra header reservations if we overrun */
3264 while (!num_headers
||
3265 howmany(unit_bytes
, iclog_space
) > num_headers
) {
3266 unit_bytes
+= sizeof(xlog_op_header_t
);
3269 unit_bytes
+= log
->l_iclog_hsize
* num_headers
;
3271 /* for commit-rec LR header - note: padding will subsume the ophdr */
3272 unit_bytes
+= log
->l_iclog_hsize
;
3274 /* for roundoff padding for transaction data and one for commit record */
3275 if (xfs_sb_version_haslogv2(&log
->l_mp
->m_sb
) &&
3276 log
->l_mp
->m_sb
.sb_logsunit
> 1) {
3277 /* log su roundoff */
3278 unit_bytes
+= 2*log
->l_mp
->m_sb
.sb_logsunit
;
3281 unit_bytes
+= 2*BBSIZE
;
3284 atomic_set(&tic
->t_ref
, 1);
3285 tic
->t_task
= current
;
3286 INIT_LIST_HEAD(&tic
->t_queue
);
3287 tic
->t_unit_res
= unit_bytes
;
3288 tic
->t_curr_res
= unit_bytes
;
3291 tic
->t_tid
= random32();
3292 tic
->t_clientid
= client
;
3293 tic
->t_flags
= XLOG_TIC_INITED
;
3294 tic
->t_trans_type
= 0;
3295 if (xflags
& XFS_LOG_PERM_RESERV
)
3296 tic
->t_flags
|= XLOG_TIC_PERM_RESERV
;
3298 xlog_tic_reset_res(tic
);
3304 /******************************************************************************
3306 * Log debug routines
3308 ******************************************************************************
3312 * Make sure that the destination ptr is within the valid data region of
3313 * one of the iclogs. This uses backup pointers stored in a different
3314 * part of the log in case we trash the log structure.
3317 xlog_verify_dest_ptr(
3324 for (i
= 0; i
< log
->l_iclog_bufs
; i
++) {
3325 if (ptr
>= log
->l_iclog_bak
[i
] &&
3326 ptr
<= log
->l_iclog_bak
[i
] + log
->l_iclog_size
)
3331 xfs_emerg(log
->l_mp
, "%s: invalid ptr", __func__
);
3335 * Check to make sure the grant write head didn't just over lap the tail. If
3336 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3337 * the cycles differ by exactly one and check the byte count.
3339 * This check is run unlocked, so can give false positives. Rather than assert
3340 * on failures, use a warn-once flag and a panic tag to allow the admin to
3341 * determine if they want to panic the machine when such an error occurs. For
3342 * debug kernels this will have the same effect as using an assert but, unlinke
3343 * an assert, it can be turned off at runtime.
3346 xlog_verify_grant_tail(
3349 int tail_cycle
, tail_blocks
;
3352 xlog_crack_grant_head(&log
->l_grant_write_head
, &cycle
, &space
);
3353 xlog_crack_atomic_lsn(&log
->l_tail_lsn
, &tail_cycle
, &tail_blocks
);
3354 if (tail_cycle
!= cycle
) {
3355 if (cycle
- 1 != tail_cycle
&&
3356 !(log
->l_flags
& XLOG_TAIL_WARN
)) {
3357 xfs_alert_tag(log
->l_mp
, XFS_PTAG_LOGRES
,
3358 "%s: cycle - 1 != tail_cycle", __func__
);
3359 log
->l_flags
|= XLOG_TAIL_WARN
;
3362 if (space
> BBTOB(tail_blocks
) &&
3363 !(log
->l_flags
& XLOG_TAIL_WARN
)) {
3364 xfs_alert_tag(log
->l_mp
, XFS_PTAG_LOGRES
,
3365 "%s: space > BBTOB(tail_blocks)", __func__
);
3366 log
->l_flags
|= XLOG_TAIL_WARN
;
3371 /* check if it will fit */
3373 xlog_verify_tail_lsn(xlog_t
*log
,
3374 xlog_in_core_t
*iclog
,
3379 if (CYCLE_LSN(tail_lsn
) == log
->l_prev_cycle
) {
3381 log
->l_logBBsize
- (log
->l_prev_block
- BLOCK_LSN(tail_lsn
));
3382 if (blocks
< BTOBB(iclog
->ic_offset
)+BTOBB(log
->l_iclog_hsize
))
3383 xfs_emerg(log
->l_mp
, "%s: ran out of log space", __func__
);
3385 ASSERT(CYCLE_LSN(tail_lsn
)+1 == log
->l_prev_cycle
);
3387 if (BLOCK_LSN(tail_lsn
) == log
->l_prev_block
)
3388 xfs_emerg(log
->l_mp
, "%s: tail wrapped", __func__
);
3390 blocks
= BLOCK_LSN(tail_lsn
) - log
->l_prev_block
;
3391 if (blocks
< BTOBB(iclog
->ic_offset
) + 1)
3392 xfs_emerg(log
->l_mp
, "%s: ran out of log space", __func__
);
3394 } /* xlog_verify_tail_lsn */
3397 * Perform a number of checks on the iclog before writing to disk.
3399 * 1. Make sure the iclogs are still circular
3400 * 2. Make sure we have a good magic number
3401 * 3. Make sure we don't have magic numbers in the data
3402 * 4. Check fields of each log operation header for:
3403 * A. Valid client identifier
3404 * B. tid ptr value falls in valid ptr space (user space code)
3405 * C. Length in log record header is correct according to the
3406 * individual operation headers within record.
3407 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3408 * log, check the preceding blocks of the physical log to make sure all
3409 * the cycle numbers agree with the current cycle number.
3412 xlog_verify_iclog(xlog_t
*log
,
3413 xlog_in_core_t
*iclog
,
3417 xlog_op_header_t
*ophead
;
3418 xlog_in_core_t
*icptr
;
3419 xlog_in_core_2_t
*xhdr
;
3421 xfs_caddr_t base_ptr
;
3422 __psint_t field_offset
;
3424 int len
, i
, j
, k
, op_len
;
3427 /* check validity of iclog pointers */
3428 spin_lock(&log
->l_icloglock
);
3429 icptr
= log
->l_iclog
;
3430 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
3432 xfs_emerg(log
->l_mp
, "%s: invalid ptr", __func__
);
3433 icptr
= icptr
->ic_next
;
3435 if (icptr
!= log
->l_iclog
)
3436 xfs_emerg(log
->l_mp
, "%s: corrupt iclog ring", __func__
);
3437 spin_unlock(&log
->l_icloglock
);
3439 /* check log magic numbers */
3440 if (iclog
->ic_header
.h_magicno
!= cpu_to_be32(XLOG_HEADER_MAGIC_NUM
))
3441 xfs_emerg(log
->l_mp
, "%s: invalid magic num", __func__
);
3443 ptr
= (xfs_caddr_t
) &iclog
->ic_header
;
3444 for (ptr
+= BBSIZE
; ptr
< ((xfs_caddr_t
)&iclog
->ic_header
) + count
;
3446 if (*(__be32
*)ptr
== cpu_to_be32(XLOG_HEADER_MAGIC_NUM
))
3447 xfs_emerg(log
->l_mp
, "%s: unexpected magic num",
3452 len
= be32_to_cpu(iclog
->ic_header
.h_num_logops
);
3453 ptr
= iclog
->ic_datap
;
3455 ophead
= (xlog_op_header_t
*)ptr
;
3456 xhdr
= iclog
->ic_data
;
3457 for (i
= 0; i
< len
; i
++) {
3458 ophead
= (xlog_op_header_t
*)ptr
;
3460 /* clientid is only 1 byte */
3461 field_offset
= (__psint_t
)
3462 ((xfs_caddr_t
)&(ophead
->oh_clientid
) - base_ptr
);
3463 if (syncing
== B_FALSE
|| (field_offset
& 0x1ff)) {
3464 clientid
= ophead
->oh_clientid
;
3466 idx
= BTOBBT((xfs_caddr_t
)&(ophead
->oh_clientid
) - iclog
->ic_datap
);
3467 if (idx
>= (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
)) {
3468 j
= idx
/ (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3469 k
= idx
% (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3470 clientid
= xlog_get_client_id(
3471 xhdr
[j
].hic_xheader
.xh_cycle_data
[k
]);
3473 clientid
= xlog_get_client_id(
3474 iclog
->ic_header
.h_cycle_data
[idx
]);
3477 if (clientid
!= XFS_TRANSACTION
&& clientid
!= XFS_LOG
)
3479 "%s: invalid clientid %d op 0x%p offset 0x%lx",
3480 __func__
, clientid
, ophead
,
3481 (unsigned long)field_offset
);
3484 field_offset
= (__psint_t
)
3485 ((xfs_caddr_t
)&(ophead
->oh_len
) - base_ptr
);
3486 if (syncing
== B_FALSE
|| (field_offset
& 0x1ff)) {
3487 op_len
= be32_to_cpu(ophead
->oh_len
);
3489 idx
= BTOBBT((__psint_t
)&ophead
->oh_len
-
3490 (__psint_t
)iclog
->ic_datap
);
3491 if (idx
>= (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
)) {
3492 j
= idx
/ (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3493 k
= idx
% (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3494 op_len
= be32_to_cpu(xhdr
[j
].hic_xheader
.xh_cycle_data
[k
]);
3496 op_len
= be32_to_cpu(iclog
->ic_header
.h_cycle_data
[idx
]);
3499 ptr
+= sizeof(xlog_op_header_t
) + op_len
;
3501 } /* xlog_verify_iclog */
3505 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3511 xlog_in_core_t
*iclog
, *ic
;
3513 iclog
= log
->l_iclog
;
3514 if (! (iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
3516 * Mark all the incore logs IOERROR.
3517 * From now on, no log flushes will result.
3521 ic
->ic_state
= XLOG_STATE_IOERROR
;
3523 } while (ic
!= iclog
);
3527 * Return non-zero, if state transition has already happened.
3533 * This is called from xfs_force_shutdown, when we're forcibly
3534 * shutting down the filesystem, typically because of an IO error.
3535 * Our main objectives here are to make sure that:
3536 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3537 * parties to find out, 'atomically'.
3538 * b. those who're sleeping on log reservations, pinned objects and
3539 * other resources get woken up, and be told the bad news.
3540 * c. nothing new gets queued up after (a) and (b) are done.
3541 * d. if !logerror, flush the iclogs to disk, then seal them off
3544 * Note: for delayed logging the !logerror case needs to flush the regions
3545 * held in memory out to the iclogs before flushing them to disk. This needs
3546 * to be done before the log is marked as shutdown, otherwise the flush to the
3550 xfs_log_force_umount(
3551 struct xfs_mount
*mp
,
3561 * If this happens during log recovery, don't worry about
3562 * locking; the log isn't open for business yet.
3565 log
->l_flags
& XLOG_ACTIVE_RECOVERY
) {
3566 mp
->m_flags
|= XFS_MOUNT_FS_SHUTDOWN
;
3568 XFS_BUF_DONE(mp
->m_sb_bp
);
3573 * Somebody could've already done the hard work for us.
3574 * No need to get locks for this.
3576 if (logerror
&& log
->l_iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3577 ASSERT(XLOG_FORCED_SHUTDOWN(log
));
3583 * Flush the in memory commit item list before marking the log as
3584 * being shut down. We need to do it in this order to ensure all the
3585 * completed transactions are flushed to disk with the xfs_log_force()
3589 xlog_cil_force(log
);
3592 * mark the filesystem and the as in a shutdown state and wake
3593 * everybody up to tell them the bad news.
3595 spin_lock(&log
->l_icloglock
);
3596 mp
->m_flags
|= XFS_MOUNT_FS_SHUTDOWN
;
3598 XFS_BUF_DONE(mp
->m_sb_bp
);
3601 * This flag is sort of redundant because of the mount flag, but
3602 * it's good to maintain the separation between the log and the rest
3605 log
->l_flags
|= XLOG_IO_ERROR
;
3608 * If we hit a log error, we want to mark all the iclogs IOERROR
3609 * while we're still holding the loglock.
3612 retval
= xlog_state_ioerror(log
);
3613 spin_unlock(&log
->l_icloglock
);
3616 * We don't want anybody waiting for log reservations after this. That
3617 * means we have to wake up everybody queued up on reserveq as well as
3618 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3619 * we don't enqueue anything once the SHUTDOWN flag is set, and this
3620 * action is protected by the grant locks.
3622 spin_lock(&log
->l_grant_reserve_lock
);
3623 list_for_each_entry(tic
, &log
->l_reserveq
, t_queue
)
3624 wake_up_process(tic
->t_task
);
3625 spin_unlock(&log
->l_grant_reserve_lock
);
3627 spin_lock(&log
->l_grant_write_lock
);
3628 list_for_each_entry(tic
, &log
->l_writeq
, t_queue
)
3629 wake_up_process(tic
->t_task
);
3630 spin_unlock(&log
->l_grant_write_lock
);
3632 if (!(log
->l_iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
3635 * Force the incore logs to disk before shutting the
3636 * log down completely.
3638 _xfs_log_force(mp
, XFS_LOG_SYNC
, NULL
);
3640 spin_lock(&log
->l_icloglock
);
3641 retval
= xlog_state_ioerror(log
);
3642 spin_unlock(&log
->l_icloglock
);
3645 * Wake up everybody waiting on xfs_log_force.
3646 * Callback all log item committed functions as if the
3647 * log writes were completed.
3649 xlog_state_do_callback(log
, XFS_LI_ABORTED
, NULL
);
3651 #ifdef XFSERRORDEBUG
3653 xlog_in_core_t
*iclog
;
3655 spin_lock(&log
->l_icloglock
);
3656 iclog
= log
->l_iclog
;
3658 ASSERT(iclog
->ic_callback
== 0);
3659 iclog
= iclog
->ic_next
;
3660 } while (iclog
!= log
->l_iclog
);
3661 spin_unlock(&log
->l_icloglock
);
3664 /* return non-zero if log IOERROR transition had already happened */
3669 xlog_iclogs_empty(xlog_t
*log
)
3671 xlog_in_core_t
*iclog
;
3673 iclog
= log
->l_iclog
;
3675 /* endianness does not matter here, zero is zero in
3678 if (iclog
->ic_header
.h_num_logops
)
3680 iclog
= iclog
->ic_next
;
3681 } while (iclog
!= log
->l_iclog
);