2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_error.h"
31 #include "xfs_log_priv.h"
32 #include "xfs_buf_item.h"
33 #include "xfs_bmap_btree.h"
34 #include "xfs_alloc_btree.h"
35 #include "xfs_ialloc_btree.h"
36 #include "xfs_log_recover.h"
37 #include "xfs_trans_priv.h"
38 #include "xfs_dir2_sf.h"
39 #include "xfs_attr_sf.h"
40 #include "xfs_dinode.h"
41 #include "xfs_inode.h"
45 #define xlog_write_adv_cnt(ptr, len, off, bytes) \
50 /* Local miscellaneous function prototypes */
51 STATIC
int xlog_bdstrat_cb(struct xfs_buf
*);
52 STATIC
int xlog_commit_record(xfs_mount_t
*mp
, xlog_ticket_t
*ticket
,
53 xlog_in_core_t
**, xfs_lsn_t
*);
54 STATIC xlog_t
* xlog_alloc_log(xfs_mount_t
*mp
,
55 xfs_buftarg_t
*log_target
,
56 xfs_daddr_t blk_offset
,
58 STATIC
int xlog_space_left(xlog_t
*log
, int cycle
, int bytes
);
59 STATIC
int xlog_sync(xlog_t
*log
, xlog_in_core_t
*iclog
);
60 STATIC
void xlog_dealloc_log(xlog_t
*log
);
61 STATIC
int xlog_write(xfs_mount_t
*mp
, xfs_log_iovec_t region
[],
62 int nentries
, xfs_log_ticket_t tic
,
64 xlog_in_core_t
**commit_iclog
,
67 /* local state machine functions */
68 STATIC
void xlog_state_done_syncing(xlog_in_core_t
*iclog
, int);
69 STATIC
void xlog_state_do_callback(xlog_t
*log
,int aborted
, xlog_in_core_t
*iclog
);
70 STATIC
int xlog_state_get_iclog_space(xlog_t
*log
,
72 xlog_in_core_t
**iclog
,
73 xlog_ticket_t
*ticket
,
76 STATIC
void xlog_state_put_ticket(xlog_t
*log
,
78 STATIC
int xlog_state_release_iclog(xlog_t
*log
,
79 xlog_in_core_t
*iclog
);
80 STATIC
void xlog_state_switch_iclogs(xlog_t
*log
,
81 xlog_in_core_t
*iclog
,
83 STATIC
int xlog_state_sync(xlog_t
*log
,
87 STATIC
int xlog_state_sync_all(xlog_t
*log
, uint flags
, int *log_flushed
);
88 STATIC
void xlog_state_want_sync(xlog_t
*log
, xlog_in_core_t
*iclog
);
90 /* local functions to manipulate grant head */
91 STATIC
int xlog_grant_log_space(xlog_t
*log
,
93 STATIC
void xlog_grant_push_ail(xfs_mount_t
*mp
,
95 STATIC
void xlog_regrant_reserve_log_space(xlog_t
*log
,
96 xlog_ticket_t
*ticket
);
97 STATIC
int xlog_regrant_write_log_space(xlog_t
*log
,
98 xlog_ticket_t
*ticket
);
99 STATIC
void xlog_ungrant_log_space(xlog_t
*log
,
100 xlog_ticket_t
*ticket
);
103 /* local ticket functions */
104 STATIC
void xlog_state_ticket_alloc(xlog_t
*log
);
105 STATIC xlog_ticket_t
*xlog_ticket_get(xlog_t
*log
,
110 STATIC
void xlog_ticket_put(xlog_t
*log
, xlog_ticket_t
*ticket
);
113 STATIC
void xlog_verify_dest_ptr(xlog_t
*log
, __psint_t ptr
);
114 STATIC
void xlog_verify_grant_head(xlog_t
*log
, int equals
);
115 STATIC
void xlog_verify_iclog(xlog_t
*log
, xlog_in_core_t
*iclog
,
116 int count
, boolean_t syncing
);
117 STATIC
void xlog_verify_tail_lsn(xlog_t
*log
, xlog_in_core_t
*iclog
,
120 #define xlog_verify_dest_ptr(a,b)
121 #define xlog_verify_grant_head(a,b)
122 #define xlog_verify_iclog(a,b,c,d)
123 #define xlog_verify_tail_lsn(a,b,c)
126 STATIC
int xlog_iclogs_empty(xlog_t
*log
);
128 #if defined(XFS_LOG_TRACE)
130 xlog_trace_loggrant(xlog_t
*log
, xlog_ticket_t
*tic
, xfs_caddr_t string
)
134 if (!log
->l_grant_trace
) {
135 log
->l_grant_trace
= ktrace_alloc(2048, KM_NOSLEEP
);
136 if (!log
->l_grant_trace
)
139 /* ticket counts are 1 byte each */
140 cnts
= ((unsigned long)tic
->t_ocnt
) | ((unsigned long)tic
->t_cnt
) << 8;
142 ktrace_enter(log
->l_grant_trace
,
144 (void *)log
->l_reserve_headq
,
145 (void *)log
->l_write_headq
,
146 (void *)((unsigned long)log
->l_grant_reserve_cycle
),
147 (void *)((unsigned long)log
->l_grant_reserve_bytes
),
148 (void *)((unsigned long)log
->l_grant_write_cycle
),
149 (void *)((unsigned long)log
->l_grant_write_bytes
),
150 (void *)((unsigned long)log
->l_curr_cycle
),
151 (void *)((unsigned long)log
->l_curr_block
),
152 (void *)((unsigned long)CYCLE_LSN(log
->l_tail_lsn
)),
153 (void *)((unsigned long)BLOCK_LSN(log
->l_tail_lsn
)),
155 (void *)((unsigned long)tic
->t_trans_type
),
157 (void *)((unsigned long)tic
->t_curr_res
),
158 (void *)((unsigned long)tic
->t_unit_res
));
162 xlog_trace_iclog(xlog_in_core_t
*iclog
, uint state
)
164 if (!iclog
->ic_trace
)
165 iclog
->ic_trace
= ktrace_alloc(256, KM_SLEEP
);
166 ktrace_enter(iclog
->ic_trace
,
167 (void *)((unsigned long)state
),
168 (void *)((unsigned long)current_pid()),
169 (void *)NULL
, (void *)NULL
, (void *)NULL
, (void *)NULL
,
170 (void *)NULL
, (void *)NULL
, (void *)NULL
, (void *)NULL
,
171 (void *)NULL
, (void *)NULL
, (void *)NULL
, (void *)NULL
,
172 (void *)NULL
, (void *)NULL
);
175 #define xlog_trace_loggrant(log,tic,string)
176 #define xlog_trace_iclog(iclog,state)
177 #endif /* XFS_LOG_TRACE */
181 xlog_ins_ticketq(struct xlog_ticket
**qp
, struct xlog_ticket
*tic
)
185 tic
->t_prev
= (*qp
)->t_prev
;
186 (*qp
)->t_prev
->t_next
= tic
;
189 tic
->t_prev
= tic
->t_next
= tic
;
193 tic
->t_flags
|= XLOG_TIC_IN_Q
;
197 xlog_del_ticketq(struct xlog_ticket
**qp
, struct xlog_ticket
*tic
)
199 if (tic
== tic
->t_next
) {
203 tic
->t_next
->t_prev
= tic
->t_prev
;
204 tic
->t_prev
->t_next
= tic
->t_next
;
207 tic
->t_next
= tic
->t_prev
= NULL
;
208 tic
->t_flags
&= ~XLOG_TIC_IN_Q
;
212 xlog_grant_sub_space(struct log
*log
, int bytes
)
214 log
->l_grant_write_bytes
-= bytes
;
215 if (log
->l_grant_write_bytes
< 0) {
216 log
->l_grant_write_bytes
+= log
->l_logsize
;
217 log
->l_grant_write_cycle
--;
220 log
->l_grant_reserve_bytes
-= bytes
;
221 if ((log
)->l_grant_reserve_bytes
< 0) {
222 log
->l_grant_reserve_bytes
+= log
->l_logsize
;
223 log
->l_grant_reserve_cycle
--;
229 xlog_grant_add_space_write(struct log
*log
, int bytes
)
231 log
->l_grant_write_bytes
+= bytes
;
232 if (log
->l_grant_write_bytes
> log
->l_logsize
) {
233 log
->l_grant_write_bytes
-= log
->l_logsize
;
234 log
->l_grant_write_cycle
++;
239 xlog_grant_add_space_reserve(struct log
*log
, int bytes
)
241 log
->l_grant_reserve_bytes
+= bytes
;
242 if (log
->l_grant_reserve_bytes
> log
->l_logsize
) {
243 log
->l_grant_reserve_bytes
-= log
->l_logsize
;
244 log
->l_grant_reserve_cycle
++;
249 xlog_grant_add_space(struct log
*log
, int bytes
)
251 xlog_grant_add_space_write(log
, bytes
);
252 xlog_grant_add_space_reserve(log
, bytes
);
256 xlog_tic_reset_res(xlog_ticket_t
*tic
)
259 tic
->t_res_arr_sum
= 0;
260 tic
->t_res_num_ophdrs
= 0;
264 xlog_tic_add_region(xlog_ticket_t
*tic
, uint len
, uint type
)
266 if (tic
->t_res_num
== XLOG_TIC_LEN_MAX
) {
267 /* add to overflow and start again */
268 tic
->t_res_o_flow
+= tic
->t_res_arr_sum
;
270 tic
->t_res_arr_sum
= 0;
273 tic
->t_res_arr
[tic
->t_res_num
].r_len
= len
;
274 tic
->t_res_arr
[tic
->t_res_num
].r_type
= type
;
275 tic
->t_res_arr_sum
+= len
;
282 * 1. currblock field gets updated at startup and after in-core logs
283 * marked as with WANT_SYNC.
287 * This routine is called when a user of a log manager ticket is done with
288 * the reservation. If the ticket was ever used, then a commit record for
289 * the associated transaction is written out as a log operation header with
290 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
291 * a given ticket. If the ticket was one with a permanent reservation, then
292 * a few operations are done differently. Permanent reservation tickets by
293 * default don't release the reservation. They just commit the current
294 * transaction with the belief that the reservation is still needed. A flag
295 * must be passed in before permanent reservations are actually released.
296 * When these type of tickets are not released, they need to be set into
297 * the inited state again. By doing this, a start record will be written
298 * out when the next write occurs.
301 xfs_log_done(xfs_mount_t
*mp
,
302 xfs_log_ticket_t xtic
,
306 xlog_t
*log
= mp
->m_log
;
307 xlog_ticket_t
*ticket
= (xfs_log_ticket_t
) xtic
;
310 if (XLOG_FORCED_SHUTDOWN(log
) ||
312 * If nothing was ever written, don't write out commit record.
313 * If we get an error, just continue and give back the log ticket.
315 (((ticket
->t_flags
& XLOG_TIC_INITED
) == 0) &&
316 (xlog_commit_record(mp
, ticket
,
317 (xlog_in_core_t
**)iclog
, &lsn
)))) {
318 lsn
= (xfs_lsn_t
) -1;
319 if (ticket
->t_flags
& XLOG_TIC_PERM_RESERV
) {
320 flags
|= XFS_LOG_REL_PERM_RESERV
;
325 if ((ticket
->t_flags
& XLOG_TIC_PERM_RESERV
) == 0 ||
326 (flags
& XFS_LOG_REL_PERM_RESERV
)) {
328 * Release ticket if not permanent reservation or a specific
329 * request has been made to release a permanent reservation.
331 xlog_trace_loggrant(log
, ticket
, "xfs_log_done: (non-permanent)");
332 xlog_ungrant_log_space(log
, ticket
);
333 xlog_state_put_ticket(log
, ticket
);
335 xlog_trace_loggrant(log
, ticket
, "xfs_log_done: (permanent)");
336 xlog_regrant_reserve_log_space(log
, ticket
);
339 /* If this ticket was a permanent reservation and we aren't
340 * trying to release it, reset the inited flags; so next time
341 * we write, a start record will be written out.
343 if ((ticket
->t_flags
& XLOG_TIC_PERM_RESERV
) &&
344 (flags
& XFS_LOG_REL_PERM_RESERV
) == 0)
345 ticket
->t_flags
|= XLOG_TIC_INITED
;
352 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
353 * the force is done synchronously.
355 * Asynchronous forces are implemented by setting the WANT_SYNC
356 * bit in the appropriate in-core log and then returning.
358 * Synchronous forces are implemented with a semaphore. All callers
359 * to force a given lsn to disk will wait on a semaphore attached to the
360 * specific in-core log. When given in-core log finally completes its
361 * write to disk, that thread will wake up all threads waiting on the
371 xlog_t
*log
= mp
->m_log
;
375 log_flushed
= &dummy
;
377 ASSERT(flags
& XFS_LOG_FORCE
);
379 XFS_STATS_INC(xs_log_force
);
381 if (log
->l_flags
& XLOG_IO_ERROR
)
382 return XFS_ERROR(EIO
);
384 return xlog_state_sync_all(log
, flags
, log_flushed
);
386 return xlog_state_sync(log
, lsn
, flags
, log_flushed
);
387 } /* xfs_log_force */
390 * Attaches a new iclog I/O completion callback routine during
391 * transaction commit. If the log is in error state, a non-zero
392 * return code is handed back and the caller is responsible for
393 * executing the callback at an appropriate time.
396 xfs_log_notify(xfs_mount_t
*mp
, /* mount of partition */
397 void *iclog_hndl
, /* iclog to hang callback off */
398 xfs_log_callback_t
*cb
)
400 xlog_t
*log
= mp
->m_log
;
401 xlog_in_core_t
*iclog
= (xlog_in_core_t
*)iclog_hndl
;
405 spin_lock(&log
->l_icloglock
);
406 abortflg
= (iclog
->ic_state
& XLOG_STATE_IOERROR
);
408 ASSERT_ALWAYS((iclog
->ic_state
== XLOG_STATE_ACTIVE
) ||
409 (iclog
->ic_state
== XLOG_STATE_WANT_SYNC
));
411 *(iclog
->ic_callback_tail
) = cb
;
412 iclog
->ic_callback_tail
= &(cb
->cb_next
);
414 spin_unlock(&log
->l_icloglock
);
416 } /* xfs_log_notify */
419 xfs_log_release_iclog(xfs_mount_t
*mp
,
422 xlog_t
*log
= mp
->m_log
;
423 xlog_in_core_t
*iclog
= (xlog_in_core_t
*)iclog_hndl
;
425 if (xlog_state_release_iclog(log
, iclog
)) {
426 xfs_force_shutdown(mp
, SHUTDOWN_LOG_IO_ERROR
);
434 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
435 * to the reservation.
436 * 2. Potentially, push buffers at tail of log to disk.
438 * Each reservation is going to reserve extra space for a log record header.
439 * When writes happen to the on-disk log, we don't subtract the length of the
440 * log record header from any reservation. By wasting space in each
441 * reservation, we prevent over allocation problems.
444 xfs_log_reserve(xfs_mount_t
*mp
,
447 xfs_log_ticket_t
*ticket
,
452 xlog_t
*log
= mp
->m_log
;
453 xlog_ticket_t
*internal_ticket
;
456 ASSERT(client
== XFS_TRANSACTION
|| client
== XFS_LOG
);
457 ASSERT((flags
& XFS_LOG_NOSLEEP
) == 0);
459 if (XLOG_FORCED_SHUTDOWN(log
))
460 return XFS_ERROR(EIO
);
462 XFS_STATS_INC(xs_try_logspace
);
464 if (*ticket
!= NULL
) {
465 ASSERT(flags
& XFS_LOG_PERM_RESERV
);
466 internal_ticket
= (xlog_ticket_t
*)*ticket
;
467 xlog_trace_loggrant(log
, internal_ticket
, "xfs_log_reserve: existing ticket (permanent trans)");
468 xlog_grant_push_ail(mp
, internal_ticket
->t_unit_res
);
469 retval
= xlog_regrant_write_log_space(log
, internal_ticket
);
471 /* may sleep if need to allocate more tickets */
472 internal_ticket
= xlog_ticket_get(log
, unit_bytes
, cnt
,
474 internal_ticket
->t_trans_type
= t_type
;
475 *ticket
= internal_ticket
;
476 xlog_trace_loggrant(log
, internal_ticket
,
477 (internal_ticket
->t_flags
& XLOG_TIC_PERM_RESERV
) ?
478 "xfs_log_reserve: create new ticket (permanent trans)" :
479 "xfs_log_reserve: create new ticket");
480 xlog_grant_push_ail(mp
,
481 (internal_ticket
->t_unit_res
*
482 internal_ticket
->t_cnt
));
483 retval
= xlog_grant_log_space(log
, internal_ticket
);
487 } /* xfs_log_reserve */
491 * Mount a log filesystem
493 * mp - ubiquitous xfs mount point structure
494 * log_target - buftarg of on-disk log device
495 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
496 * num_bblocks - Number of BBSIZE blocks in on-disk log
498 * Return error or zero.
503 xfs_buftarg_t
*log_target
,
504 xfs_daddr_t blk_offset
,
509 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
))
510 cmn_err(CE_NOTE
, "XFS mounting filesystem %s", mp
->m_fsname
);
513 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
515 ASSERT(mp
->m_flags
& XFS_MOUNT_RDONLY
);
518 mp
->m_log
= xlog_alloc_log(mp
, log_target
, blk_offset
, num_bblks
);
521 * Initialize the AIL now we have a log.
523 spin_lock_init(&mp
->m_ail_lock
);
524 error
= xfs_trans_ail_init(mp
);
526 cmn_err(CE_WARN
, "XFS: AIL initialisation failed: error %d", error
);
531 * skip log recovery on a norecovery mount. pretend it all
534 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
)) {
535 int readonly
= (mp
->m_flags
& XFS_MOUNT_RDONLY
);
538 mp
->m_flags
&= ~XFS_MOUNT_RDONLY
;
540 error
= xlog_recover(mp
->m_log
);
543 mp
->m_flags
|= XFS_MOUNT_RDONLY
;
545 cmn_err(CE_WARN
, "XFS: log mount/recovery failed: error %d", error
);
550 /* Normal transactions can now occur */
551 mp
->m_log
->l_flags
&= ~XLOG_ACTIVE_RECOVERY
;
553 /* End mounting message in xfs_log_mount_finish */
556 xfs_log_unmount_dealloc(mp
);
558 } /* xfs_log_mount */
561 * Finish the recovery of the file system. This is separate from
562 * the xfs_log_mount() call, because it depends on the code in
563 * xfs_mountfs() to read in the root and real-time bitmap inodes
564 * between calling xfs_log_mount() and here.
566 * mp - ubiquitous xfs mount point structure
569 xfs_log_mount_finish(xfs_mount_t
*mp
, int mfsi_flags
)
573 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
))
574 error
= xlog_recover_finish(mp
->m_log
, mfsi_flags
);
577 ASSERT(mp
->m_flags
& XFS_MOUNT_RDONLY
);
584 * Unmount processing for the log.
587 xfs_log_unmount(xfs_mount_t
*mp
)
591 error
= xfs_log_unmount_write(mp
);
592 xfs_log_unmount_dealloc(mp
);
597 * Final log writes as part of unmount.
599 * Mark the filesystem clean as unmount happens. Note that during relocation
600 * this routine needs to be executed as part of source-bag while the
601 * deallocation must not be done until source-end.
605 * Unmount record used to have a string "Unmount filesystem--" in the
606 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
607 * We just write the magic number now since that particular field isn't
608 * currently architecture converted and "nUmount" is a bit foo.
609 * As far as I know, there weren't any dependencies on the old behaviour.
613 xfs_log_unmount_write(xfs_mount_t
*mp
)
615 xlog_t
*log
= mp
->m_log
;
616 xlog_in_core_t
*iclog
;
618 xlog_in_core_t
*first_iclog
;
620 xfs_log_iovec_t reg
[1];
621 xfs_log_ticket_t tic
= NULL
;
625 /* the data section must be 32 bit size aligned */
629 __uint32_t pad2
; /* may as well make it 64 bits */
630 } magic
= { XLOG_UNMOUNT_TYPE
, 0, 0 };
633 * Don't write out unmount record on read-only mounts.
634 * Or, if we are doing a forced umount (typically because of IO errors).
636 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
639 xfs_log_force(mp
, 0, XFS_LOG_FORCE
|XFS_LOG_SYNC
);
642 first_iclog
= iclog
= log
->l_iclog
;
644 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
645 ASSERT(iclog
->ic_state
& XLOG_STATE_ACTIVE
);
646 ASSERT(iclog
->ic_offset
== 0);
648 iclog
= iclog
->ic_next
;
649 } while (iclog
!= first_iclog
);
651 if (! (XLOG_FORCED_SHUTDOWN(log
))) {
652 reg
[0].i_addr
= (void*)&magic
;
653 reg
[0].i_len
= sizeof(magic
);
654 XLOG_VEC_SET_TYPE(®
[0], XLOG_REG_TYPE_UNMOUNT
);
656 error
= xfs_log_reserve(mp
, 600, 1, &tic
,
657 XFS_LOG
, 0, XLOG_UNMOUNT_REC_TYPE
);
659 /* remove inited flag */
660 ((xlog_ticket_t
*)tic
)->t_flags
= 0;
661 error
= xlog_write(mp
, reg
, 1, tic
, &lsn
,
662 NULL
, XLOG_UNMOUNT_TRANS
);
664 * At this point, we're umounting anyway,
665 * so there's no point in transitioning log state
666 * to IOERROR. Just continue...
671 xfs_fs_cmn_err(CE_ALERT
, mp
,
672 "xfs_log_unmount: unmount record failed");
676 spin_lock(&log
->l_icloglock
);
677 iclog
= log
->l_iclog
;
679 spin_unlock(&log
->l_icloglock
);
680 xlog_state_want_sync(log
, iclog
);
681 (void) xlog_state_release_iclog(log
, iclog
);
683 spin_lock(&log
->l_icloglock
);
684 if (!(iclog
->ic_state
== XLOG_STATE_ACTIVE
||
685 iclog
->ic_state
== XLOG_STATE_DIRTY
)) {
686 if (!XLOG_FORCED_SHUTDOWN(log
)) {
687 sv_wait(&iclog
->ic_forcesema
, PMEM
,
688 &log
->l_icloglock
, s
);
690 spin_unlock(&log
->l_icloglock
);
693 spin_unlock(&log
->l_icloglock
);
696 xlog_trace_loggrant(log
, tic
, "unmount rec");
697 xlog_ungrant_log_space(log
, tic
);
698 xlog_state_put_ticket(log
, tic
);
702 * We're already in forced_shutdown mode, couldn't
703 * even attempt to write out the unmount transaction.
705 * Go through the motions of sync'ing and releasing
706 * the iclog, even though no I/O will actually happen,
707 * we need to wait for other log I/Os that may already
708 * be in progress. Do this as a separate section of
709 * code so we'll know if we ever get stuck here that
710 * we're in this odd situation of trying to unmount
711 * a file system that went into forced_shutdown as
712 * the result of an unmount..
714 spin_lock(&log
->l_icloglock
);
715 iclog
= log
->l_iclog
;
717 spin_unlock(&log
->l_icloglock
);
719 xlog_state_want_sync(log
, iclog
);
720 (void) xlog_state_release_iclog(log
, iclog
);
722 spin_lock(&log
->l_icloglock
);
724 if ( ! ( iclog
->ic_state
== XLOG_STATE_ACTIVE
725 || iclog
->ic_state
== XLOG_STATE_DIRTY
726 || iclog
->ic_state
== XLOG_STATE_IOERROR
) ) {
728 sv_wait(&iclog
->ic_forcesema
, PMEM
,
729 &log
->l_icloglock
, s
);
731 spin_unlock(&log
->l_icloglock
);
736 } /* xfs_log_unmount_write */
739 * Deallocate log structures for unmount/relocation.
741 * We need to stop the aild from running before we destroy
742 * and deallocate the log as the aild references the log.
745 xfs_log_unmount_dealloc(xfs_mount_t
*mp
)
747 xfs_trans_ail_destroy(mp
);
748 xlog_dealloc_log(mp
->m_log
);
752 * Write region vectors to log. The write happens using the space reservation
753 * of the ticket (tic). It is not a requirement that all writes for a given
754 * transaction occur with one call to xfs_log_write().
757 xfs_log_write(xfs_mount_t
* mp
,
758 xfs_log_iovec_t reg
[],
760 xfs_log_ticket_t tic
,
761 xfs_lsn_t
*start_lsn
)
764 xlog_t
*log
= mp
->m_log
;
766 if (XLOG_FORCED_SHUTDOWN(log
))
767 return XFS_ERROR(EIO
);
769 if ((error
= xlog_write(mp
, reg
, nentries
, tic
, start_lsn
, NULL
, 0))) {
770 xfs_force_shutdown(mp
, SHUTDOWN_LOG_IO_ERROR
);
773 } /* xfs_log_write */
777 xfs_log_move_tail(xfs_mount_t
*mp
,
781 xlog_t
*log
= mp
->m_log
;
782 int need_bytes
, free_bytes
, cycle
, bytes
;
784 if (XLOG_FORCED_SHUTDOWN(log
))
788 /* needed since sync_lsn is 64 bits */
789 spin_lock(&log
->l_icloglock
);
790 tail_lsn
= log
->l_last_sync_lsn
;
791 spin_unlock(&log
->l_icloglock
);
794 spin_lock(&log
->l_grant_lock
);
796 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
800 log
->l_tail_lsn
= tail_lsn
;
803 if ((tic
= log
->l_write_headq
)) {
805 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
806 panic("Recovery problem");
808 cycle
= log
->l_grant_write_cycle
;
809 bytes
= log
->l_grant_write_bytes
;
810 free_bytes
= xlog_space_left(log
, cycle
, bytes
);
812 ASSERT(tic
->t_flags
& XLOG_TIC_PERM_RESERV
);
814 if (free_bytes
< tic
->t_unit_res
&& tail_lsn
!= 1)
817 free_bytes
-= tic
->t_unit_res
;
818 sv_signal(&tic
->t_sema
);
820 } while (tic
!= log
->l_write_headq
);
822 if ((tic
= log
->l_reserve_headq
)) {
824 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
825 panic("Recovery problem");
827 cycle
= log
->l_grant_reserve_cycle
;
828 bytes
= log
->l_grant_reserve_bytes
;
829 free_bytes
= xlog_space_left(log
, cycle
, bytes
);
831 if (tic
->t_flags
& XLOG_TIC_PERM_RESERV
)
832 need_bytes
= tic
->t_unit_res
*tic
->t_cnt
;
834 need_bytes
= tic
->t_unit_res
;
835 if (free_bytes
< need_bytes
&& tail_lsn
!= 1)
838 free_bytes
-= need_bytes
;
839 sv_signal(&tic
->t_sema
);
841 } while (tic
!= log
->l_reserve_headq
);
843 spin_unlock(&log
->l_grant_lock
);
844 } /* xfs_log_move_tail */
847 * Determine if we have a transaction that has gone to disk
848 * that needs to be covered. Log activity needs to be idle (no AIL and
849 * nothing in the iclogs). And, we need to be in the right state indicating
850 * something has gone out.
853 xfs_log_need_covered(xfs_mount_t
*mp
)
856 xlog_t
*log
= mp
->m_log
;
858 if (!xfs_fs_writable(mp
))
861 spin_lock(&log
->l_icloglock
);
862 if (((log
->l_covered_state
== XLOG_STATE_COVER_NEED
) ||
863 (log
->l_covered_state
== XLOG_STATE_COVER_NEED2
))
864 && !xfs_trans_first_ail(mp
, &gen
)
865 && xlog_iclogs_empty(log
)) {
866 if (log
->l_covered_state
== XLOG_STATE_COVER_NEED
)
867 log
->l_covered_state
= XLOG_STATE_COVER_DONE
;
869 ASSERT(log
->l_covered_state
== XLOG_STATE_COVER_NEED2
);
870 log
->l_covered_state
= XLOG_STATE_COVER_DONE2
;
874 spin_unlock(&log
->l_icloglock
);
878 /******************************************************************************
882 ******************************************************************************
885 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
886 * The log manager must keep track of the last LR which was committed
887 * to disk. The lsn of this LR will become the new tail_lsn whenever
888 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
889 * the situation where stuff could be written into the log but nothing
890 * was ever in the AIL when asked. Eventually, we panic since the
891 * tail hits the head.
893 * We may be holding the log iclog lock upon entering this routine.
896 xlog_assign_tail_lsn(xfs_mount_t
*mp
)
899 xlog_t
*log
= mp
->m_log
;
901 tail_lsn
= xfs_trans_tail_ail(mp
);
902 spin_lock(&log
->l_grant_lock
);
904 log
->l_tail_lsn
= tail_lsn
;
906 tail_lsn
= log
->l_tail_lsn
= log
->l_last_sync_lsn
;
908 spin_unlock(&log
->l_grant_lock
);
911 } /* xlog_assign_tail_lsn */
915 * Return the space in the log between the tail and the head. The head
916 * is passed in the cycle/bytes formal parms. In the special case where
917 * the reserve head has wrapped passed the tail, this calculation is no
918 * longer valid. In this case, just return 0 which means there is no space
919 * in the log. This works for all places where this function is called
920 * with the reserve head. Of course, if the write head were to ever
921 * wrap the tail, we should blow up. Rather than catch this case here,
922 * we depend on other ASSERTions in other parts of the code. XXXmiken
924 * This code also handles the case where the reservation head is behind
925 * the tail. The details of this case are described below, but the end
926 * result is that we return the size of the log as the amount of space left.
929 xlog_space_left(xlog_t
*log
, int cycle
, int bytes
)
935 tail_bytes
= BBTOB(BLOCK_LSN(log
->l_tail_lsn
));
936 tail_cycle
= CYCLE_LSN(log
->l_tail_lsn
);
937 if ((tail_cycle
== cycle
) && (bytes
>= tail_bytes
)) {
938 free_bytes
= log
->l_logsize
- (bytes
- tail_bytes
);
939 } else if ((tail_cycle
+ 1) < cycle
) {
941 } else if (tail_cycle
< cycle
) {
942 ASSERT(tail_cycle
== (cycle
- 1));
943 free_bytes
= tail_bytes
- bytes
;
946 * The reservation head is behind the tail.
947 * In this case we just want to return the size of the
948 * log as the amount of space left.
950 xfs_fs_cmn_err(CE_ALERT
, log
->l_mp
,
951 "xlog_space_left: head behind tail\n"
952 " tail_cycle = %d, tail_bytes = %d\n"
953 " GH cycle = %d, GH bytes = %d",
954 tail_cycle
, tail_bytes
, cycle
, bytes
);
956 free_bytes
= log
->l_logsize
;
959 } /* xlog_space_left */
963 * Log function which is called when an io completes.
965 * The log manager needs its own routine, in order to control what
966 * happens with the buffer after the write completes.
969 xlog_iodone(xfs_buf_t
*bp
)
971 xlog_in_core_t
*iclog
;
975 iclog
= XFS_BUF_FSPRIVATE(bp
, xlog_in_core_t
*);
976 ASSERT(XFS_BUF_FSPRIVATE2(bp
, unsigned long) == (unsigned long) 2);
977 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)1);
981 * Some versions of cpp barf on the recursive definition of
982 * ic_log -> hic_fields.ic_log and expand ic_log twice when
983 * it is passed through two macros. Workaround broken cpp.
988 * If the ordered flag has been removed by a lower
989 * layer, it means the underlyin device no longer supports
990 * barrier I/O. Warn loudly and turn off barriers.
992 if ((l
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
) && !XFS_BUF_ORDERED(bp
)) {
993 l
->l_mp
->m_flags
&= ~XFS_MOUNT_BARRIER
;
994 xfs_fs_cmn_err(CE_WARN
, l
->l_mp
,
995 "xlog_iodone: Barriers are no longer supported"
996 " by device. Disabling barriers\n");
997 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp
);
1001 * Race to shutdown the filesystem if we see an error.
1003 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp
)), l
->l_mp
,
1004 XFS_ERRTAG_IODONE_IOERR
, XFS_RANDOM_IODONE_IOERR
)) {
1005 xfs_ioerror_alert("xlog_iodone", l
->l_mp
, bp
, XFS_BUF_ADDR(bp
));
1007 xfs_force_shutdown(l
->l_mp
, SHUTDOWN_LOG_IO_ERROR
);
1009 * This flag will be propagated to the trans-committed
1010 * callback routines to let them know that the log-commit
1013 aborted
= XFS_LI_ABORTED
;
1014 } else if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
1015 aborted
= XFS_LI_ABORTED
;
1018 /* log I/O is always issued ASYNC */
1019 ASSERT(XFS_BUF_ISASYNC(bp
));
1020 xlog_state_done_syncing(iclog
, aborted
);
1022 * do not reference the buffer (bp) here as we could race
1023 * with it being freed after writing the unmount record to the
1030 * The bdstrat callback function for log bufs. This gives us a central
1031 * place to trap bufs in case we get hit by a log I/O error and need to
1032 * shutdown. Actually, in practice, even when we didn't get a log error,
1033 * we transition the iclogs to IOERROR state *after* flushing all existing
1034 * iclogs to disk. This is because we don't want anymore new transactions to be
1035 * started or completed afterwards.
1038 xlog_bdstrat_cb(struct xfs_buf
*bp
)
1040 xlog_in_core_t
*iclog
;
1042 iclog
= XFS_BUF_FSPRIVATE(bp
, xlog_in_core_t
*);
1044 if ((iclog
->ic_state
& XLOG_STATE_IOERROR
) == 0) {
1045 /* note for irix bstrat will need struct bdevsw passed
1046 * Fix the following macro if the code ever is merged
1052 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp
);
1053 XFS_BUF_ERROR(bp
, EIO
);
1056 return XFS_ERROR(EIO
);
1062 * Return size of each in-core log record buffer.
1064 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
1066 * If the filesystem blocksize is too large, we may need to choose a
1067 * larger size since the directory code currently logs entire blocks.
1071 xlog_get_iclog_buffer_size(xfs_mount_t
*mp
,
1077 if (mp
->m_logbufs
<= 0)
1078 log
->l_iclog_bufs
= XLOG_MAX_ICLOGS
;
1080 log
->l_iclog_bufs
= mp
->m_logbufs
;
1083 * Buffer size passed in from mount system call.
1085 if (mp
->m_logbsize
> 0) {
1086 size
= log
->l_iclog_size
= mp
->m_logbsize
;
1087 log
->l_iclog_size_log
= 0;
1089 log
->l_iclog_size_log
++;
1093 if (XFS_SB_VERSION_HASLOGV2(&mp
->m_sb
)) {
1094 /* # headers = size / 32K
1095 * one header holds cycles from 32K of data
1098 xhdrs
= mp
->m_logbsize
/ XLOG_HEADER_CYCLE_SIZE
;
1099 if (mp
->m_logbsize
% XLOG_HEADER_CYCLE_SIZE
)
1101 log
->l_iclog_hsize
= xhdrs
<< BBSHIFT
;
1102 log
->l_iclog_heads
= xhdrs
;
1104 ASSERT(mp
->m_logbsize
<= XLOG_BIG_RECORD_BSIZE
);
1105 log
->l_iclog_hsize
= BBSIZE
;
1106 log
->l_iclog_heads
= 1;
1111 /* All machines use 32KB buffers by default. */
1112 log
->l_iclog_size
= XLOG_BIG_RECORD_BSIZE
;
1113 log
->l_iclog_size_log
= XLOG_BIG_RECORD_BSHIFT
;
1115 /* the default log size is 16k or 32k which is one header sector */
1116 log
->l_iclog_hsize
= BBSIZE
;
1117 log
->l_iclog_heads
= 1;
1120 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1121 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1123 if (mp
->m_sb
.sb_blocksize
>= 16*1024) {
1124 log
->l_iclog_size
= XLOG_BIG_RECORD_BSIZE
;
1125 log
->l_iclog_size_log
= XLOG_BIG_RECORD_BSHIFT
;
1126 if (mp
->m_logbufs
<= 0) {
1127 switch (mp
->m_sb
.sb_blocksize
) {
1128 case 16*1024: /* 16 KB */
1129 log
->l_iclog_bufs
= 3;
1131 case 32*1024: /* 32 KB */
1132 log
->l_iclog_bufs
= 4;
1134 case 64*1024: /* 64 KB */
1135 log
->l_iclog_bufs
= 8;
1138 xlog_panic("XFS: Invalid blocksize");
1144 done
: /* are we being asked to make the sizes selected above visible? */
1145 if (mp
->m_logbufs
== 0)
1146 mp
->m_logbufs
= log
->l_iclog_bufs
;
1147 if (mp
->m_logbsize
== 0)
1148 mp
->m_logbsize
= log
->l_iclog_size
;
1149 } /* xlog_get_iclog_buffer_size */
1153 * This routine initializes some of the log structure for a given mount point.
1154 * Its primary purpose is to fill in enough, so recovery can occur. However,
1155 * some other stuff may be filled in too.
1158 xlog_alloc_log(xfs_mount_t
*mp
,
1159 xfs_buftarg_t
*log_target
,
1160 xfs_daddr_t blk_offset
,
1164 xlog_rec_header_t
*head
;
1165 xlog_in_core_t
**iclogp
;
1166 xlog_in_core_t
*iclog
, *prev_iclog
=NULL
;
1171 log
= (xlog_t
*)kmem_zalloc(sizeof(xlog_t
), KM_SLEEP
);
1174 log
->l_targ
= log_target
;
1175 log
->l_logsize
= BBTOB(num_bblks
);
1176 log
->l_logBBstart
= blk_offset
;
1177 log
->l_logBBsize
= num_bblks
;
1178 log
->l_covered_state
= XLOG_STATE_COVER_IDLE
;
1179 log
->l_flags
|= XLOG_ACTIVE_RECOVERY
;
1181 log
->l_prev_block
= -1;
1182 log
->l_tail_lsn
= xlog_assign_lsn(1, 0);
1183 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1184 log
->l_last_sync_lsn
= log
->l_tail_lsn
;
1185 log
->l_curr_cycle
= 1; /* 0 is bad since this is initial value */
1186 log
->l_grant_reserve_cycle
= 1;
1187 log
->l_grant_write_cycle
= 1;
1189 if (XFS_SB_VERSION_HASSECTOR(&mp
->m_sb
)) {
1190 log
->l_sectbb_log
= mp
->m_sb
.sb_logsectlog
- BBSHIFT
;
1191 ASSERT(log
->l_sectbb_log
<= mp
->m_sectbb_log
);
1192 /* for larger sector sizes, must have v2 or external log */
1193 ASSERT(log
->l_sectbb_log
== 0 ||
1194 log
->l_logBBstart
== 0 ||
1195 XFS_SB_VERSION_HASLOGV2(&mp
->m_sb
));
1196 ASSERT(mp
->m_sb
.sb_logsectlog
>= BBSHIFT
);
1198 log
->l_sectbb_mask
= (1 << log
->l_sectbb_log
) - 1;
1200 xlog_get_iclog_buffer_size(mp
, log
);
1202 bp
= xfs_buf_get_empty(log
->l_iclog_size
, mp
->m_logdev_targp
);
1203 XFS_BUF_SET_IODONE_FUNC(bp
, xlog_iodone
);
1204 XFS_BUF_SET_BDSTRAT_FUNC(bp
, xlog_bdstrat_cb
);
1205 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)1);
1206 ASSERT(XFS_BUF_ISBUSY(bp
));
1207 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
1210 spin_lock_init(&log
->l_icloglock
);
1211 spin_lock_init(&log
->l_grant_lock
);
1212 initnsema(&log
->l_flushsema
, 0, "ic-flush");
1213 xlog_state_ticket_alloc(log
); /* wait until after icloglock inited */
1215 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1216 ASSERT((XFS_BUF_SIZE(bp
) & BBMASK
) == 0);
1218 iclogp
= &log
->l_iclog
;
1220 * The amount of memory to allocate for the iclog structure is
1221 * rather funky due to the way the structure is defined. It is
1222 * done this way so that we can use different sizes for machines
1223 * with different amounts of memory. See the definition of
1224 * xlog_in_core_t in xfs_log_priv.h for details.
1226 iclogsize
= log
->l_iclog_size
;
1227 ASSERT(log
->l_iclog_size
>= 4096);
1228 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
1229 *iclogp
= (xlog_in_core_t
*)
1230 kmem_zalloc(sizeof(xlog_in_core_t
), KM_SLEEP
);
1232 iclog
->ic_prev
= prev_iclog
;
1235 bp
= xfs_buf_get_noaddr(log
->l_iclog_size
, mp
->m_logdev_targp
);
1236 if (!XFS_BUF_CPSEMA(bp
))
1238 XFS_BUF_SET_IODONE_FUNC(bp
, xlog_iodone
);
1239 XFS_BUF_SET_BDSTRAT_FUNC(bp
, xlog_bdstrat_cb
);
1240 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)1);
1242 iclog
->hic_data
= bp
->b_addr
;
1244 log
->l_iclog_bak
[i
] = (xfs_caddr_t
)&(iclog
->ic_header
);
1246 head
= &iclog
->ic_header
;
1247 memset(head
, 0, sizeof(xlog_rec_header_t
));
1248 head
->h_magicno
= cpu_to_be32(XLOG_HEADER_MAGIC_NUM
);
1249 head
->h_version
= cpu_to_be32(
1250 XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
) ? 2 : 1);
1251 head
->h_size
= cpu_to_be32(log
->l_iclog_size
);
1253 head
->h_fmt
= cpu_to_be32(XLOG_FMT
);
1254 memcpy(&head
->h_fs_uuid
, &mp
->m_sb
.sb_uuid
, sizeof(uuid_t
));
1257 iclog
->ic_size
= XFS_BUF_SIZE(bp
) - log
->l_iclog_hsize
;
1258 iclog
->ic_state
= XLOG_STATE_ACTIVE
;
1259 iclog
->ic_log
= log
;
1260 iclog
->ic_callback_tail
= &(iclog
->ic_callback
);
1261 iclog
->ic_datap
= (char *)iclog
->hic_data
+ log
->l_iclog_hsize
;
1263 ASSERT(XFS_BUF_ISBUSY(iclog
->ic_bp
));
1264 ASSERT(XFS_BUF_VALUSEMA(iclog
->ic_bp
) <= 0);
1265 sv_init(&iclog
->ic_forcesema
, SV_DEFAULT
, "iclog-force");
1266 sv_init(&iclog
->ic_writesema
, SV_DEFAULT
, "iclog-write");
1268 iclogp
= &iclog
->ic_next
;
1270 *iclogp
= log
->l_iclog
; /* complete ring */
1271 log
->l_iclog
->ic_prev
= prev_iclog
; /* re-write 1st prev ptr */
1274 } /* xlog_alloc_log */
1278 * Write out the commit record of a transaction associated with the given
1279 * ticket. Return the lsn of the commit record.
1282 xlog_commit_record(xfs_mount_t
*mp
,
1283 xlog_ticket_t
*ticket
,
1284 xlog_in_core_t
**iclog
,
1285 xfs_lsn_t
*commitlsnp
)
1288 xfs_log_iovec_t reg
[1];
1290 reg
[0].i_addr
= NULL
;
1292 XLOG_VEC_SET_TYPE(®
[0], XLOG_REG_TYPE_COMMIT
);
1294 ASSERT_ALWAYS(iclog
);
1295 if ((error
= xlog_write(mp
, reg
, 1, ticket
, commitlsnp
,
1296 iclog
, XLOG_COMMIT_TRANS
))) {
1297 xfs_force_shutdown(mp
, SHUTDOWN_LOG_IO_ERROR
);
1300 } /* xlog_commit_record */
1304 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1305 * log space. This code pushes on the lsn which would supposedly free up
1306 * the 25% which we want to leave free. We may need to adopt a policy which
1307 * pushes on an lsn which is further along in the log once we reach the high
1308 * water mark. In this manner, we would be creating a low water mark.
1311 xlog_grant_push_ail(xfs_mount_t
*mp
,
1314 xlog_t
*log
= mp
->m_log
; /* pointer to the log */
1315 xfs_lsn_t tail_lsn
; /* lsn of the log tail */
1316 xfs_lsn_t threshold_lsn
= 0; /* lsn we'd like to be at */
1317 int free_blocks
; /* free blocks left to write to */
1318 int free_bytes
; /* free bytes left to write to */
1319 int threshold_block
; /* block in lsn we'd like to be at */
1320 int threshold_cycle
; /* lsn cycle we'd like to be at */
1323 ASSERT(BTOBB(need_bytes
) < log
->l_logBBsize
);
1325 spin_lock(&log
->l_grant_lock
);
1326 free_bytes
= xlog_space_left(log
,
1327 log
->l_grant_reserve_cycle
,
1328 log
->l_grant_reserve_bytes
);
1329 tail_lsn
= log
->l_tail_lsn
;
1330 free_blocks
= BTOBBT(free_bytes
);
1333 * Set the threshold for the minimum number of free blocks in the
1334 * log to the maximum of what the caller needs, one quarter of the
1335 * log, and 256 blocks.
1337 free_threshold
= BTOBB(need_bytes
);
1338 free_threshold
= MAX(free_threshold
, (log
->l_logBBsize
>> 2));
1339 free_threshold
= MAX(free_threshold
, 256);
1340 if (free_blocks
< free_threshold
) {
1341 threshold_block
= BLOCK_LSN(tail_lsn
) + free_threshold
;
1342 threshold_cycle
= CYCLE_LSN(tail_lsn
);
1343 if (threshold_block
>= log
->l_logBBsize
) {
1344 threshold_block
-= log
->l_logBBsize
;
1345 threshold_cycle
+= 1;
1347 threshold_lsn
= xlog_assign_lsn(threshold_cycle
, threshold_block
);
1349 /* Don't pass in an lsn greater than the lsn of the last
1350 * log record known to be on disk.
1352 if (XFS_LSN_CMP(threshold_lsn
, log
->l_last_sync_lsn
) > 0)
1353 threshold_lsn
= log
->l_last_sync_lsn
;
1355 spin_unlock(&log
->l_grant_lock
);
1358 * Get the transaction layer to kick the dirty buffers out to
1359 * disk asynchronously. No point in trying to do this if
1360 * the filesystem is shutting down.
1362 if (threshold_lsn
&&
1363 !XLOG_FORCED_SHUTDOWN(log
))
1364 xfs_trans_push_ail(mp
, threshold_lsn
);
1365 } /* xlog_grant_push_ail */
1369 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1370 * fashion. Previously, we should have moved the current iclog
1371 * ptr in the log to point to the next available iclog. This allows further
1372 * write to continue while this code syncs out an iclog ready to go.
1373 * Before an in-core log can be written out, the data section must be scanned
1374 * to save away the 1st word of each BBSIZE block into the header. We replace
1375 * it with the current cycle count. Each BBSIZE block is tagged with the
1376 * cycle count because there in an implicit assumption that drives will
1377 * guarantee that entire 512 byte blocks get written at once. In other words,
1378 * we can't have part of a 512 byte block written and part not written. By
1379 * tagging each block, we will know which blocks are valid when recovering
1380 * after an unclean shutdown.
1382 * This routine is single threaded on the iclog. No other thread can be in
1383 * this routine with the same iclog. Changing contents of iclog can there-
1384 * fore be done without grabbing the state machine lock. Updating the global
1385 * log will require grabbing the lock though.
1387 * The entire log manager uses a logical block numbering scheme. Only
1388 * log_sync (and then only bwrite()) know about the fact that the log may
1389 * not start with block zero on a given device. The log block start offset
1390 * is added immediately before calling bwrite().
1394 xlog_sync(xlog_t
*log
,
1395 xlog_in_core_t
*iclog
)
1397 xfs_caddr_t dptr
; /* pointer to byte sized element */
1400 uint count
; /* byte count of bwrite */
1401 uint count_init
; /* initial count before roundup */
1402 int roundoff
; /* roundoff to BB or stripe */
1403 int split
= 0; /* split write into two regions */
1405 int v2
= XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
);
1407 XFS_STATS_INC(xs_log_writes
);
1408 ASSERT(iclog
->ic_refcnt
== 0);
1410 /* Add for LR header */
1411 count_init
= log
->l_iclog_hsize
+ iclog
->ic_offset
;
1413 /* Round out the log write size */
1414 if (v2
&& log
->l_mp
->m_sb
.sb_logsunit
> 1) {
1415 /* we have a v2 stripe unit to use */
1416 count
= XLOG_LSUNITTOB(log
, XLOG_BTOLSUNIT(log
, count_init
));
1418 count
= BBTOB(BTOBB(count_init
));
1420 roundoff
= count
- count_init
;
1421 ASSERT(roundoff
>= 0);
1422 ASSERT((v2
&& log
->l_mp
->m_sb
.sb_logsunit
> 1 &&
1423 roundoff
< log
->l_mp
->m_sb
.sb_logsunit
)
1425 (log
->l_mp
->m_sb
.sb_logsunit
<= 1 &&
1426 roundoff
< BBTOB(1)));
1428 /* move grant heads by roundoff in sync */
1429 spin_lock(&log
->l_grant_lock
);
1430 xlog_grant_add_space(log
, roundoff
);
1431 spin_unlock(&log
->l_grant_lock
);
1433 /* put cycle number in every block */
1434 xlog_pack_data(log
, iclog
, roundoff
);
1436 /* real byte length */
1438 iclog
->ic_header
.h_len
=
1439 cpu_to_be32(iclog
->ic_offset
+ roundoff
);
1441 iclog
->ic_header
.h_len
=
1442 cpu_to_be32(iclog
->ic_offset
);
1446 ASSERT(XFS_BUF_FSPRIVATE2(bp
, unsigned long) == (unsigned long)1);
1447 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)2);
1448 XFS_BUF_SET_ADDR(bp
, BLOCK_LSN(be64_to_cpu(iclog
->ic_header
.h_lsn
)));
1450 XFS_STATS_ADD(xs_log_blocks
, BTOBB(count
));
1452 /* Do we need to split this write into 2 parts? */
1453 if (XFS_BUF_ADDR(bp
) + BTOBB(count
) > log
->l_logBBsize
) {
1454 split
= count
- (BBTOB(log
->l_logBBsize
- XFS_BUF_ADDR(bp
)));
1455 count
= BBTOB(log
->l_logBBsize
- XFS_BUF_ADDR(bp
));
1456 iclog
->ic_bwritecnt
= 2; /* split into 2 writes */
1458 iclog
->ic_bwritecnt
= 1;
1460 XFS_BUF_SET_COUNT(bp
, count
);
1461 XFS_BUF_SET_FSPRIVATE(bp
, iclog
); /* save for later */
1462 XFS_BUF_ZEROFLAGS(bp
);
1466 * Do an ordered write for the log block.
1467 * Its unnecessary to flush the first split block in the log wrap case.
1469 if (!split
&& (log
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
))
1470 XFS_BUF_ORDERED(bp
);
1472 ASSERT(XFS_BUF_ADDR(bp
) <= log
->l_logBBsize
-1);
1473 ASSERT(XFS_BUF_ADDR(bp
) + BTOBB(count
) <= log
->l_logBBsize
);
1475 xlog_verify_iclog(log
, iclog
, count
, B_TRUE
);
1477 /* account for log which doesn't start at block #0 */
1478 XFS_BUF_SET_ADDR(bp
, XFS_BUF_ADDR(bp
) + log
->l_logBBstart
);
1480 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1485 if ((error
= XFS_bwrite(bp
))) {
1486 xfs_ioerror_alert("xlog_sync", log
->l_mp
, bp
,
1491 bp
= iclog
->ic_log
->l_xbuf
;
1492 ASSERT(XFS_BUF_FSPRIVATE2(bp
, unsigned long) ==
1494 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)2);
1495 XFS_BUF_SET_ADDR(bp
, 0); /* logical 0 */
1496 XFS_BUF_SET_PTR(bp
, (xfs_caddr_t
)((__psint_t
)&(iclog
->ic_header
)+
1497 (__psint_t
)count
), split
);
1498 XFS_BUF_SET_FSPRIVATE(bp
, iclog
);
1499 XFS_BUF_ZEROFLAGS(bp
);
1502 if (log
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
)
1503 XFS_BUF_ORDERED(bp
);
1504 dptr
= XFS_BUF_PTR(bp
);
1506 * Bump the cycle numbers at the start of each block
1507 * since this part of the buffer is at the start of
1508 * a new cycle. Watch out for the header magic number
1511 for (i
= 0; i
< split
; i
+= BBSIZE
) {
1512 be32_add_cpu((__be32
*)dptr
, 1);
1513 if (be32_to_cpu(*(__be32
*)dptr
) == XLOG_HEADER_MAGIC_NUM
)
1514 be32_add_cpu((__be32
*)dptr
, 1);
1518 ASSERT(XFS_BUF_ADDR(bp
) <= log
->l_logBBsize
-1);
1519 ASSERT(XFS_BUF_ADDR(bp
) + BTOBB(count
) <= log
->l_logBBsize
);
1521 /* account for internal log which doesn't start at block #0 */
1522 XFS_BUF_SET_ADDR(bp
, XFS_BUF_ADDR(bp
) + log
->l_logBBstart
);
1524 if ((error
= XFS_bwrite(bp
))) {
1525 xfs_ioerror_alert("xlog_sync (split)", log
->l_mp
,
1526 bp
, XFS_BUF_ADDR(bp
));
1535 * Deallocate a log structure
1538 xlog_dealloc_log(xlog_t
*log
)
1540 xlog_in_core_t
*iclog
, *next_iclog
;
1541 xlog_ticket_t
*tic
, *next_tic
;
1544 iclog
= log
->l_iclog
;
1545 for (i
=0; i
<log
->l_iclog_bufs
; i
++) {
1546 sv_destroy(&iclog
->ic_forcesema
);
1547 sv_destroy(&iclog
->ic_writesema
);
1548 xfs_buf_free(iclog
->ic_bp
);
1549 #ifdef XFS_LOG_TRACE
1550 if (iclog
->ic_trace
!= NULL
) {
1551 ktrace_free(iclog
->ic_trace
);
1554 next_iclog
= iclog
->ic_next
;
1555 kmem_free(iclog
, sizeof(xlog_in_core_t
));
1558 freesema(&log
->l_flushsema
);
1559 spinlock_destroy(&log
->l_icloglock
);
1560 spinlock_destroy(&log
->l_grant_lock
);
1562 /* XXXsup take a look at this again. */
1563 if ((log
->l_ticket_cnt
!= log
->l_ticket_tcnt
) &&
1564 !XLOG_FORCED_SHUTDOWN(log
)) {
1565 xfs_fs_cmn_err(CE_WARN
, log
->l_mp
,
1566 "xlog_dealloc_log: (cnt: %d, total: %d)",
1567 log
->l_ticket_cnt
, log
->l_ticket_tcnt
);
1568 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1571 tic
= log
->l_unmount_free
;
1573 next_tic
= tic
->t_next
;
1574 kmem_free(tic
, PAGE_SIZE
);
1578 xfs_buf_free(log
->l_xbuf
);
1579 #ifdef XFS_LOG_TRACE
1580 if (log
->l_trace
!= NULL
) {
1581 ktrace_free(log
->l_trace
);
1583 if (log
->l_grant_trace
!= NULL
) {
1584 ktrace_free(log
->l_grant_trace
);
1587 log
->l_mp
->m_log
= NULL
;
1588 kmem_free(log
, sizeof(xlog_t
));
1589 } /* xlog_dealloc_log */
1592 * Update counters atomically now that memcpy is done.
1596 xlog_state_finish_copy(xlog_t
*log
,
1597 xlog_in_core_t
*iclog
,
1601 spin_lock(&log
->l_icloglock
);
1603 be32_add_cpu(&iclog
->ic_header
.h_num_logops
, record_cnt
);
1604 iclog
->ic_offset
+= copy_bytes
;
1606 spin_unlock(&log
->l_icloglock
);
1607 } /* xlog_state_finish_copy */
1613 * print out info relating to regions written which consume
1617 xlog_print_tic_res(xfs_mount_t
*mp
, xlog_ticket_t
*ticket
)
1620 uint ophdr_spc
= ticket
->t_res_num_ophdrs
* (uint
)sizeof(xlog_op_header_t
);
1622 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1623 static char *res_type_str
[XLOG_REG_TYPE_MAX
] = {
1644 static char *trans_type_str
[XFS_TRANS_TYPE_MAX
] = {
1687 xfs_fs_cmn_err(CE_WARN
, mp
,
1688 "xfs_log_write: reservation summary:\n"
1689 " trans type = %s (%u)\n"
1690 " unit res = %d bytes\n"
1691 " current res = %d bytes\n"
1692 " total reg = %u bytes (o/flow = %u bytes)\n"
1693 " ophdrs = %u (ophdr space = %u bytes)\n"
1694 " ophdr + reg = %u bytes\n"
1695 " num regions = %u\n",
1696 ((ticket
->t_trans_type
<= 0 ||
1697 ticket
->t_trans_type
> XFS_TRANS_TYPE_MAX
) ?
1698 "bad-trans-type" : trans_type_str
[ticket
->t_trans_type
-1]),
1699 ticket
->t_trans_type
,
1702 ticket
->t_res_arr_sum
, ticket
->t_res_o_flow
,
1703 ticket
->t_res_num_ophdrs
, ophdr_spc
,
1704 ticket
->t_res_arr_sum
+
1705 ticket
->t_res_o_flow
+ ophdr_spc
,
1708 for (i
= 0; i
< ticket
->t_res_num
; i
++) {
1709 uint r_type
= ticket
->t_res_arr
[i
].r_type
;
1711 "region[%u]: %s - %u bytes\n",
1713 ((r_type
<= 0 || r_type
> XLOG_REG_TYPE_MAX
) ?
1714 "bad-rtype" : res_type_str
[r_type
-1]),
1715 ticket
->t_res_arr
[i
].r_len
);
1720 * Write some region out to in-core log
1722 * This will be called when writing externally provided regions or when
1723 * writing out a commit record for a given transaction.
1725 * General algorithm:
1726 * 1. Find total length of this write. This may include adding to the
1727 * lengths passed in.
1728 * 2. Check whether we violate the tickets reservation.
1729 * 3. While writing to this iclog
1730 * A. Reserve as much space in this iclog as can get
1731 * B. If this is first write, save away start lsn
1732 * C. While writing this region:
1733 * 1. If first write of transaction, write start record
1734 * 2. Write log operation header (header per region)
1735 * 3. Find out if we can fit entire region into this iclog
1736 * 4. Potentially, verify destination memcpy ptr
1737 * 5. Memcpy (partial) region
1738 * 6. If partial copy, release iclog; otherwise, continue
1739 * copying more regions into current iclog
1740 * 4. Mark want sync bit (in simulation mode)
1741 * 5. Release iclog for potential flush to on-disk log.
1744 * 1. Panic if reservation is overrun. This should never happen since
1745 * reservation amounts are generated internal to the filesystem.
1747 * 1. Tickets are single threaded data structures.
1748 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1749 * syncing routine. When a single log_write region needs to span
1750 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1751 * on all log operation writes which don't contain the end of the
1752 * region. The XLOG_END_TRANS bit is used for the in-core log
1753 * operation which contains the end of the continued log_write region.
1754 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1755 * we don't really know exactly how much space will be used. As a result,
1756 * we don't update ic_offset until the end when we know exactly how many
1757 * bytes have been written out.
1760 xlog_write(xfs_mount_t
* mp
,
1761 xfs_log_iovec_t reg
[],
1763 xfs_log_ticket_t tic
,
1764 xfs_lsn_t
*start_lsn
,
1765 xlog_in_core_t
**commit_iclog
,
1768 xlog_t
*log
= mp
->m_log
;
1769 xlog_ticket_t
*ticket
= (xlog_ticket_t
*)tic
;
1770 xlog_in_core_t
*iclog
= NULL
; /* ptr to current in-core log */
1771 xlog_op_header_t
*logop_head
; /* ptr to log operation header */
1772 __psint_t ptr
; /* copy address into data region */
1773 int len
; /* # xlog_write() bytes 2 still copy */
1774 int index
; /* region index currently copying */
1775 int log_offset
; /* offset (from 0) into data region */
1776 int start_rec_copy
; /* # bytes to copy for start record */
1777 int partial_copy
; /* did we split a region? */
1778 int partial_copy_len
;/* # bytes copied if split region */
1779 int need_copy
; /* # bytes need to memcpy this region */
1780 int copy_len
; /* # bytes actually memcpy'ing */
1781 int copy_off
; /* # bytes from entry start */
1782 int contwr
; /* continued write of in-core log? */
1784 int record_cnt
= 0, data_cnt
= 0;
1786 partial_copy_len
= partial_copy
= 0;
1788 /* Calculate potential maximum space. Each region gets its own
1789 * xlog_op_header_t and may need to be double word aligned.
1792 if (ticket
->t_flags
& XLOG_TIC_INITED
) { /* acct for start rec of xact */
1793 len
+= sizeof(xlog_op_header_t
);
1794 ticket
->t_res_num_ophdrs
++;
1797 for (index
= 0; index
< nentries
; index
++) {
1798 len
+= sizeof(xlog_op_header_t
); /* each region gets >= 1 */
1799 ticket
->t_res_num_ophdrs
++;
1800 len
+= reg
[index
].i_len
;
1801 xlog_tic_add_region(ticket
, reg
[index
].i_len
, reg
[index
].i_type
);
1803 contwr
= *start_lsn
= 0;
1805 if (ticket
->t_curr_res
< len
) {
1806 xlog_print_tic_res(mp
, ticket
);
1809 "xfs_log_write: reservation ran out. Need to up reservation");
1811 /* Customer configurable panic */
1812 xfs_cmn_err(XFS_PTAG_LOGRES
, CE_ALERT
, mp
,
1813 "xfs_log_write: reservation ran out. Need to up reservation");
1814 /* If we did not panic, shutdown the filesystem */
1815 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
1818 ticket
->t_curr_res
-= len
;
1820 for (index
= 0; index
< nentries
; ) {
1821 if ((error
= xlog_state_get_iclog_space(log
, len
, &iclog
, ticket
,
1822 &contwr
, &log_offset
)))
1825 ASSERT(log_offset
<= iclog
->ic_size
- 1);
1826 ptr
= (__psint_t
) ((char *)iclog
->ic_datap
+log_offset
);
1828 /* start_lsn is the first lsn written to. That's all we need. */
1830 *start_lsn
= be64_to_cpu(iclog
->ic_header
.h_lsn
);
1832 /* This loop writes out as many regions as can fit in the amount
1833 * of space which was allocated by xlog_state_get_iclog_space().
1835 while (index
< nentries
) {
1836 ASSERT(reg
[index
].i_len
% sizeof(__int32_t
) == 0);
1837 ASSERT((__psint_t
)ptr
% sizeof(__int32_t
) == 0);
1840 /* If first write for transaction, insert start record.
1841 * We can't be trying to commit if we are inited. We can't
1842 * have any "partial_copy" if we are inited.
1844 if (ticket
->t_flags
& XLOG_TIC_INITED
) {
1845 logop_head
= (xlog_op_header_t
*)ptr
;
1846 logop_head
->oh_tid
= cpu_to_be32(ticket
->t_tid
);
1847 logop_head
->oh_clientid
= ticket
->t_clientid
;
1848 logop_head
->oh_len
= 0;
1849 logop_head
->oh_flags
= XLOG_START_TRANS
;
1850 logop_head
->oh_res2
= 0;
1851 ticket
->t_flags
&= ~XLOG_TIC_INITED
; /* clear bit */
1854 start_rec_copy
= sizeof(xlog_op_header_t
);
1855 xlog_write_adv_cnt(ptr
, len
, log_offset
, start_rec_copy
);
1858 /* Copy log operation header directly into data section */
1859 logop_head
= (xlog_op_header_t
*)ptr
;
1860 logop_head
->oh_tid
= cpu_to_be32(ticket
->t_tid
);
1861 logop_head
->oh_clientid
= ticket
->t_clientid
;
1862 logop_head
->oh_res2
= 0;
1864 /* header copied directly */
1865 xlog_write_adv_cnt(ptr
, len
, log_offset
, sizeof(xlog_op_header_t
));
1867 /* are we copying a commit or unmount record? */
1868 logop_head
->oh_flags
= flags
;
1871 * We've seen logs corrupted with bad transaction client
1872 * ids. This makes sure that XFS doesn't generate them on.
1873 * Turn this into an EIO and shut down the filesystem.
1875 switch (logop_head
->oh_clientid
) {
1876 case XFS_TRANSACTION
:
1881 xfs_fs_cmn_err(CE_WARN
, mp
,
1882 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1883 logop_head
->oh_clientid
, tic
);
1884 return XFS_ERROR(EIO
);
1887 /* Partial write last time? => (partial_copy != 0)
1888 * need_copy is the amount we'd like to copy if everything could
1889 * fit in the current memcpy.
1891 need_copy
= reg
[index
].i_len
- partial_copy_len
;
1893 copy_off
= partial_copy_len
;
1894 if (need_copy
<= iclog
->ic_size
- log_offset
) { /*complete write */
1895 copy_len
= need_copy
;
1896 logop_head
->oh_len
= cpu_to_be32(copy_len
);
1898 logop_head
->oh_flags
|= (XLOG_END_TRANS
|XLOG_WAS_CONT_TRANS
);
1899 partial_copy_len
= partial_copy
= 0;
1900 } else { /* partial write */
1901 copy_len
= iclog
->ic_size
- log_offset
;
1902 logop_head
->oh_len
= cpu_to_be32(copy_len
);
1903 logop_head
->oh_flags
|= XLOG_CONTINUE_TRANS
;
1905 logop_head
->oh_flags
|= XLOG_WAS_CONT_TRANS
;
1906 partial_copy_len
+= copy_len
;
1908 len
+= sizeof(xlog_op_header_t
); /* from splitting of region */
1909 /* account for new log op header */
1910 ticket
->t_curr_res
-= sizeof(xlog_op_header_t
);
1911 ticket
->t_res_num_ophdrs
++;
1913 xlog_verify_dest_ptr(log
, ptr
);
1916 ASSERT(copy_len
>= 0);
1917 memcpy((xfs_caddr_t
)ptr
, reg
[index
].i_addr
+ copy_off
, copy_len
);
1918 xlog_write_adv_cnt(ptr
, len
, log_offset
, copy_len
);
1920 /* make copy_len total bytes copied, including headers */
1921 copy_len
+= start_rec_copy
+ sizeof(xlog_op_header_t
);
1923 data_cnt
+= contwr
? copy_len
: 0;
1924 if (partial_copy
) { /* copied partial region */
1925 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1926 xlog_state_finish_copy(log
, iclog
, record_cnt
, data_cnt
);
1927 record_cnt
= data_cnt
= 0;
1928 if ((error
= xlog_state_release_iclog(log
, iclog
)))
1930 break; /* don't increment index */
1931 } else { /* copied entire region */
1933 partial_copy_len
= partial_copy
= 0;
1935 if (iclog
->ic_size
- log_offset
<= sizeof(xlog_op_header_t
)) {
1936 xlog_state_finish_copy(log
, iclog
, record_cnt
, data_cnt
);
1937 record_cnt
= data_cnt
= 0;
1938 xlog_state_want_sync(log
, iclog
);
1940 ASSERT(flags
& XLOG_COMMIT_TRANS
);
1941 *commit_iclog
= iclog
;
1942 } else if ((error
= xlog_state_release_iclog(log
, iclog
)))
1944 if (index
== nentries
)
1945 return 0; /* we are done */
1949 } /* if (partial_copy) */
1950 } /* while (index < nentries) */
1951 } /* for (index = 0; index < nentries; ) */
1954 xlog_state_finish_copy(log
, iclog
, record_cnt
, data_cnt
);
1956 ASSERT(flags
& XLOG_COMMIT_TRANS
);
1957 *commit_iclog
= iclog
;
1960 return xlog_state_release_iclog(log
, iclog
);
1964 /*****************************************************************************
1966 * State Machine functions
1968 *****************************************************************************
1971 /* Clean iclogs starting from the head. This ordering must be
1972 * maintained, so an iclog doesn't become ACTIVE beyond one that
1973 * is SYNCING. This is also required to maintain the notion that we use
1974 * a counting semaphore to hold off would be writers to the log when every
1975 * iclog is trying to sync to disk.
1977 * State Change: DIRTY -> ACTIVE
1980 xlog_state_clean_log(xlog_t
*log
)
1982 xlog_in_core_t
*iclog
;
1985 iclog
= log
->l_iclog
;
1987 if (iclog
->ic_state
== XLOG_STATE_DIRTY
) {
1988 iclog
->ic_state
= XLOG_STATE_ACTIVE
;
1989 iclog
->ic_offset
= 0;
1990 iclog
->ic_callback
= NULL
; /* don't need to free */
1992 * If the number of ops in this iclog indicate it just
1993 * contains the dummy transaction, we can
1994 * change state into IDLE (the second time around).
1995 * Otherwise we should change the state into
1997 * We don't need to cover the dummy.
2000 (be32_to_cpu(iclog
->ic_header
.h_num_logops
) ==
2005 * We have two dirty iclogs so start over
2006 * This could also be num of ops indicates
2007 * this is not the dummy going out.
2011 iclog
->ic_header
.h_num_logops
= 0;
2012 memset(iclog
->ic_header
.h_cycle_data
, 0,
2013 sizeof(iclog
->ic_header
.h_cycle_data
));
2014 iclog
->ic_header
.h_lsn
= 0;
2015 } else if (iclog
->ic_state
== XLOG_STATE_ACTIVE
)
2018 break; /* stop cleaning */
2019 iclog
= iclog
->ic_next
;
2020 } while (iclog
!= log
->l_iclog
);
2022 /* log is locked when we are called */
2024 * Change state for the dummy log recording.
2025 * We usually go to NEED. But we go to NEED2 if the changed indicates
2026 * we are done writing the dummy record.
2027 * If we are done with the second dummy recored (DONE2), then
2031 switch (log
->l_covered_state
) {
2032 case XLOG_STATE_COVER_IDLE
:
2033 case XLOG_STATE_COVER_NEED
:
2034 case XLOG_STATE_COVER_NEED2
:
2035 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2038 case XLOG_STATE_COVER_DONE
:
2040 log
->l_covered_state
= XLOG_STATE_COVER_NEED2
;
2042 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2045 case XLOG_STATE_COVER_DONE2
:
2047 log
->l_covered_state
= XLOG_STATE_COVER_IDLE
;
2049 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2056 } /* xlog_state_clean_log */
2059 xlog_get_lowest_lsn(
2062 xlog_in_core_t
*lsn_log
;
2063 xfs_lsn_t lowest_lsn
, lsn
;
2065 lsn_log
= log
->l_iclog
;
2068 if (!(lsn_log
->ic_state
& (XLOG_STATE_ACTIVE
|XLOG_STATE_DIRTY
))) {
2069 lsn
= be64_to_cpu(lsn_log
->ic_header
.h_lsn
);
2070 if ((lsn
&& !lowest_lsn
) ||
2071 (XFS_LSN_CMP(lsn
, lowest_lsn
) < 0)) {
2075 lsn_log
= lsn_log
->ic_next
;
2076 } while (lsn_log
!= log
->l_iclog
);
2082 xlog_state_do_callback(
2085 xlog_in_core_t
*ciclog
)
2087 xlog_in_core_t
*iclog
;
2088 xlog_in_core_t
*first_iclog
; /* used to know when we've
2089 * processed all iclogs once */
2090 xfs_log_callback_t
*cb
, *cb_next
;
2092 xfs_lsn_t lowest_lsn
;
2093 int ioerrors
; /* counter: iclogs with errors */
2094 int loopdidcallbacks
; /* flag: inner loop did callbacks*/
2095 int funcdidcallbacks
; /* flag: function did callbacks */
2096 int repeats
; /* for issuing console warnings if
2097 * looping too many times */
2099 spin_lock(&log
->l_icloglock
);
2100 first_iclog
= iclog
= log
->l_iclog
;
2102 funcdidcallbacks
= 0;
2107 * Scan all iclogs starting with the one pointed to by the
2108 * log. Reset this starting point each time the log is
2109 * unlocked (during callbacks).
2111 * Keep looping through iclogs until one full pass is made
2112 * without running any callbacks.
2114 first_iclog
= log
->l_iclog
;
2115 iclog
= log
->l_iclog
;
2116 loopdidcallbacks
= 0;
2121 /* skip all iclogs in the ACTIVE & DIRTY states */
2122 if (iclog
->ic_state
&
2123 (XLOG_STATE_ACTIVE
|XLOG_STATE_DIRTY
)) {
2124 iclog
= iclog
->ic_next
;
2129 * Between marking a filesystem SHUTDOWN and stopping
2130 * the log, we do flush all iclogs to disk (if there
2131 * wasn't a log I/O error). So, we do want things to
2132 * go smoothly in case of just a SHUTDOWN w/o a
2135 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
2137 * Can only perform callbacks in order. Since
2138 * this iclog is not in the DONE_SYNC/
2139 * DO_CALLBACK state, we skip the rest and
2140 * just try to clean up. If we set our iclog
2141 * to DO_CALLBACK, we will not process it when
2142 * we retry since a previous iclog is in the
2143 * CALLBACK and the state cannot change since
2144 * we are holding the l_icloglock.
2146 if (!(iclog
->ic_state
&
2147 (XLOG_STATE_DONE_SYNC
|
2148 XLOG_STATE_DO_CALLBACK
))) {
2149 if (ciclog
&& (ciclog
->ic_state
==
2150 XLOG_STATE_DONE_SYNC
)) {
2151 ciclog
->ic_state
= XLOG_STATE_DO_CALLBACK
;
2156 * We now have an iclog that is in either the
2157 * DO_CALLBACK or DONE_SYNC states. The other
2158 * states (WANT_SYNC, SYNCING, or CALLBACK were
2159 * caught by the above if and are going to
2160 * clean (i.e. we aren't doing their callbacks)
2165 * We will do one more check here to see if we
2166 * have chased our tail around.
2169 lowest_lsn
= xlog_get_lowest_lsn(log
);
2171 XFS_LSN_CMP(lowest_lsn
,
2172 be64_to_cpu(iclog
->ic_header
.h_lsn
)) < 0) {
2173 iclog
= iclog
->ic_next
;
2174 continue; /* Leave this iclog for
2178 iclog
->ic_state
= XLOG_STATE_CALLBACK
;
2180 spin_unlock(&log
->l_icloglock
);
2182 /* l_last_sync_lsn field protected by
2183 * l_grant_lock. Don't worry about iclog's lsn.
2184 * No one else can be here except us.
2186 spin_lock(&log
->l_grant_lock
);
2187 ASSERT(XFS_LSN_CMP(log
->l_last_sync_lsn
,
2188 be64_to_cpu(iclog
->ic_header
.h_lsn
)) <= 0);
2189 log
->l_last_sync_lsn
=
2190 be64_to_cpu(iclog
->ic_header
.h_lsn
);
2191 spin_unlock(&log
->l_grant_lock
);
2194 * Keep processing entries in the callback list
2195 * until we come around and it is empty. We
2196 * need to atomically see that the list is
2197 * empty and change the state to DIRTY so that
2198 * we don't miss any more callbacks being added.
2200 spin_lock(&log
->l_icloglock
);
2204 cb
= iclog
->ic_callback
;
2207 iclog
->ic_callback_tail
= &(iclog
->ic_callback
);
2208 iclog
->ic_callback
= NULL
;
2209 spin_unlock(&log
->l_icloglock
);
2211 /* perform callbacks in the order given */
2212 for (; cb
; cb
= cb_next
) {
2213 cb_next
= cb
->cb_next
;
2214 cb
->cb_func(cb
->cb_arg
, aborted
);
2216 spin_lock(&log
->l_icloglock
);
2217 cb
= iclog
->ic_callback
;
2223 ASSERT(iclog
->ic_callback
== NULL
);
2224 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
))
2225 iclog
->ic_state
= XLOG_STATE_DIRTY
;
2228 * Transition from DIRTY to ACTIVE if applicable.
2229 * NOP if STATE_IOERROR.
2231 xlog_state_clean_log(log
);
2233 /* wake up threads waiting in xfs_log_force() */
2234 sv_broadcast(&iclog
->ic_forcesema
);
2236 iclog
= iclog
->ic_next
;
2237 } while (first_iclog
!= iclog
);
2239 if (repeats
> 5000) {
2240 flushcnt
+= repeats
;
2242 xfs_fs_cmn_err(CE_WARN
, log
->l_mp
,
2243 "%s: possible infinite loop (%d iterations)",
2244 __FUNCTION__
, flushcnt
);
2246 } while (!ioerrors
&& loopdidcallbacks
);
2249 * make one last gasp attempt to see if iclogs are being left in
2253 if (funcdidcallbacks
) {
2254 first_iclog
= iclog
= log
->l_iclog
;
2256 ASSERT(iclog
->ic_state
!= XLOG_STATE_DO_CALLBACK
);
2258 * Terminate the loop if iclogs are found in states
2259 * which will cause other threads to clean up iclogs.
2261 * SYNCING - i/o completion will go through logs
2262 * DONE_SYNC - interrupt thread should be waiting for
2264 * IOERROR - give up hope all ye who enter here
2266 if (iclog
->ic_state
== XLOG_STATE_WANT_SYNC
||
2267 iclog
->ic_state
== XLOG_STATE_SYNCING
||
2268 iclog
->ic_state
== XLOG_STATE_DONE_SYNC
||
2269 iclog
->ic_state
== XLOG_STATE_IOERROR
)
2271 iclog
= iclog
->ic_next
;
2272 } while (first_iclog
!= iclog
);
2277 if (log
->l_iclog
->ic_state
& (XLOG_STATE_ACTIVE
|XLOG_STATE_IOERROR
)) {
2278 flushcnt
= log
->l_flushcnt
;
2279 log
->l_flushcnt
= 0;
2281 spin_unlock(&log
->l_icloglock
);
2283 vsema(&log
->l_flushsema
);
2284 } /* xlog_state_do_callback */
2288 * Finish transitioning this iclog to the dirty state.
2290 * Make sure that we completely execute this routine only when this is
2291 * the last call to the iclog. There is a good chance that iclog flushes,
2292 * when we reach the end of the physical log, get turned into 2 separate
2293 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2294 * routine. By using the reference count bwritecnt, we guarantee that only
2295 * the second completion goes through.
2297 * Callbacks could take time, so they are done outside the scope of the
2298 * global state machine log lock. Assume that the calls to cvsema won't
2299 * take a long time. At least we know it won't sleep.
2302 xlog_state_done_syncing(
2303 xlog_in_core_t
*iclog
,
2306 xlog_t
*log
= iclog
->ic_log
;
2308 spin_lock(&log
->l_icloglock
);
2310 ASSERT(iclog
->ic_state
== XLOG_STATE_SYNCING
||
2311 iclog
->ic_state
== XLOG_STATE_IOERROR
);
2312 ASSERT(iclog
->ic_refcnt
== 0);
2313 ASSERT(iclog
->ic_bwritecnt
== 1 || iclog
->ic_bwritecnt
== 2);
2317 * If we got an error, either on the first buffer, or in the case of
2318 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2319 * and none should ever be attempted to be written to disk
2322 if (iclog
->ic_state
!= XLOG_STATE_IOERROR
) {
2323 if (--iclog
->ic_bwritecnt
== 1) {
2324 spin_unlock(&log
->l_icloglock
);
2327 iclog
->ic_state
= XLOG_STATE_DONE_SYNC
;
2331 * Someone could be sleeping prior to writing out the next
2332 * iclog buffer, we wake them all, one will get to do the
2333 * I/O, the others get to wait for the result.
2335 sv_broadcast(&iclog
->ic_writesema
);
2336 spin_unlock(&log
->l_icloglock
);
2337 xlog_state_do_callback(log
, aborted
, iclog
); /* also cleans log */
2338 } /* xlog_state_done_syncing */
2342 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2343 * sleep. The flush semaphore is set to the number of in-core buffers and
2344 * decremented around disk syncing. Therefore, if all buffers are syncing,
2345 * this semaphore will cause new writes to sleep until a sync completes.
2346 * Otherwise, this code just does p() followed by v(). This approximates
2347 * a sleep/wakeup except we can't race.
2349 * The in-core logs are used in a circular fashion. They are not used
2350 * out-of-order even when an iclog past the head is free.
2353 * * log_offset where xlog_write() can start writing into the in-core
2355 * * in-core log pointer to which xlog_write() should write.
2356 * * boolean indicating this is a continued write to an in-core log.
2357 * If this is the last write, then the in-core log's offset field
2358 * needs to be incremented, depending on the amount of data which
2362 xlog_state_get_iclog_space(xlog_t
*log
,
2364 xlog_in_core_t
**iclogp
,
2365 xlog_ticket_t
*ticket
,
2366 int *continued_write
,
2370 xlog_rec_header_t
*head
;
2371 xlog_in_core_t
*iclog
;
2375 spin_lock(&log
->l_icloglock
);
2376 if (XLOG_FORCED_SHUTDOWN(log
)) {
2377 spin_unlock(&log
->l_icloglock
);
2378 return XFS_ERROR(EIO
);
2381 iclog
= log
->l_iclog
;
2382 if (! (iclog
->ic_state
== XLOG_STATE_ACTIVE
)) {
2384 spin_unlock(&log
->l_icloglock
);
2385 xlog_trace_iclog(iclog
, XLOG_TRACE_SLEEP_FLUSH
);
2386 XFS_STATS_INC(xs_log_noiclogs
);
2387 /* Ensure that log writes happen */
2388 psema(&log
->l_flushsema
, PINOD
);
2391 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
);
2392 head
= &iclog
->ic_header
;
2394 iclog
->ic_refcnt
++; /* prevents sync */
2395 log_offset
= iclog
->ic_offset
;
2397 /* On the 1st write to an iclog, figure out lsn. This works
2398 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2399 * committing to. If the offset is set, that's how many blocks
2402 if (log_offset
== 0) {
2403 ticket
->t_curr_res
-= log
->l_iclog_hsize
;
2404 xlog_tic_add_region(ticket
,
2406 XLOG_REG_TYPE_LRHEADER
);
2407 head
->h_cycle
= cpu_to_be32(log
->l_curr_cycle
);
2408 head
->h_lsn
= cpu_to_be64(
2409 xlog_assign_lsn(log
->l_curr_cycle
, log
->l_curr_block
));
2410 ASSERT(log
->l_curr_block
>= 0);
2413 /* If there is enough room to write everything, then do it. Otherwise,
2414 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2415 * bit is on, so this will get flushed out. Don't update ic_offset
2416 * until you know exactly how many bytes get copied. Therefore, wait
2417 * until later to update ic_offset.
2419 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2420 * can fit into remaining data section.
2422 if (iclog
->ic_size
- iclog
->ic_offset
< 2*sizeof(xlog_op_header_t
)) {
2423 xlog_state_switch_iclogs(log
, iclog
, iclog
->ic_size
);
2425 /* If I'm the only one writing to this iclog, sync it to disk */
2426 if (iclog
->ic_refcnt
== 1) {
2427 spin_unlock(&log
->l_icloglock
);
2428 if ((error
= xlog_state_release_iclog(log
, iclog
)))
2432 spin_unlock(&log
->l_icloglock
);
2437 /* Do we have enough room to write the full amount in the remainder
2438 * of this iclog? Or must we continue a write on the next iclog and
2439 * mark this iclog as completely taken? In the case where we switch
2440 * iclogs (to mark it taken), this particular iclog will release/sync
2441 * to disk in xlog_write().
2443 if (len
<= iclog
->ic_size
- iclog
->ic_offset
) {
2444 *continued_write
= 0;
2445 iclog
->ic_offset
+= len
;
2447 *continued_write
= 1;
2448 xlog_state_switch_iclogs(log
, iclog
, iclog
->ic_size
);
2452 ASSERT(iclog
->ic_offset
<= iclog
->ic_size
);
2453 spin_unlock(&log
->l_icloglock
);
2455 *logoffsetp
= log_offset
;
2457 } /* xlog_state_get_iclog_space */
2460 * Atomically get the log space required for a log ticket.
2462 * Once a ticket gets put onto the reserveq, it will only return after
2463 * the needed reservation is satisfied.
2466 xlog_grant_log_space(xlog_t
*log
,
2477 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
2478 panic("grant Recovery problem");
2481 /* Is there space or do we need to sleep? */
2482 spin_lock(&log
->l_grant_lock
);
2483 xlog_trace_loggrant(log
, tic
, "xlog_grant_log_space: enter");
2485 /* something is already sleeping; insert new transaction at end */
2486 if (log
->l_reserve_headq
) {
2487 xlog_ins_ticketq(&log
->l_reserve_headq
, tic
);
2488 xlog_trace_loggrant(log
, tic
,
2489 "xlog_grant_log_space: sleep 1");
2491 * Gotta check this before going to sleep, while we're
2492 * holding the grant lock.
2494 if (XLOG_FORCED_SHUTDOWN(log
))
2497 XFS_STATS_INC(xs_sleep_logspace
);
2498 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
, &log
->l_grant_lock
, s
);
2500 * If we got an error, and the filesystem is shutting down,
2501 * we'll catch it down below. So just continue...
2503 xlog_trace_loggrant(log
, tic
,
2504 "xlog_grant_log_space: wake 1");
2505 spin_lock(&log
->l_grant_lock
);
2507 if (tic
->t_flags
& XFS_LOG_PERM_RESERV
)
2508 need_bytes
= tic
->t_unit_res
*tic
->t_ocnt
;
2510 need_bytes
= tic
->t_unit_res
;
2513 if (XLOG_FORCED_SHUTDOWN(log
))
2516 free_bytes
= xlog_space_left(log
, log
->l_grant_reserve_cycle
,
2517 log
->l_grant_reserve_bytes
);
2518 if (free_bytes
< need_bytes
) {
2519 if ((tic
->t_flags
& XLOG_TIC_IN_Q
) == 0)
2520 xlog_ins_ticketq(&log
->l_reserve_headq
, tic
);
2521 xlog_trace_loggrant(log
, tic
,
2522 "xlog_grant_log_space: sleep 2");
2523 XFS_STATS_INC(xs_sleep_logspace
);
2524 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
, &log
->l_grant_lock
, s
);
2526 if (XLOG_FORCED_SHUTDOWN(log
)) {
2527 spin_lock(&log
->l_grant_lock
);
2531 xlog_trace_loggrant(log
, tic
,
2532 "xlog_grant_log_space: wake 2");
2533 xlog_grant_push_ail(log
->l_mp
, need_bytes
);
2534 spin_lock(&log
->l_grant_lock
);
2536 } else if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2537 xlog_del_ticketq(&log
->l_reserve_headq
, tic
);
2539 /* we've got enough space */
2540 xlog_grant_add_space(log
, need_bytes
);
2542 tail_lsn
= log
->l_tail_lsn
;
2544 * Check to make sure the grant write head didn't just over lap the
2545 * tail. If the cycles are the same, we can't be overlapping.
2546 * Otherwise, make sure that the cycles differ by exactly one and
2547 * check the byte count.
2549 if (CYCLE_LSN(tail_lsn
) != log
->l_grant_write_cycle
) {
2550 ASSERT(log
->l_grant_write_cycle
-1 == CYCLE_LSN(tail_lsn
));
2551 ASSERT(log
->l_grant_write_bytes
<= BBTOB(BLOCK_LSN(tail_lsn
)));
2554 xlog_trace_loggrant(log
, tic
, "xlog_grant_log_space: exit");
2555 xlog_verify_grant_head(log
, 1);
2556 spin_unlock(&log
->l_grant_lock
);
2560 if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2561 xlog_del_ticketq(&log
->l_reserve_headq
, tic
);
2562 xlog_trace_loggrant(log
, tic
, "xlog_grant_log_space: err_ret");
2564 * If we are failing, make sure the ticket doesn't have any
2565 * current reservations. We don't want to add this back when
2566 * the ticket/transaction gets cancelled.
2568 tic
->t_curr_res
= 0;
2569 tic
->t_cnt
= 0; /* ungrant will give back unit_res * t_cnt. */
2570 spin_unlock(&log
->l_grant_lock
);
2571 return XFS_ERROR(EIO
);
2572 } /* xlog_grant_log_space */
2576 * Replenish the byte reservation required by moving the grant write head.
2581 xlog_regrant_write_log_space(xlog_t
*log
,
2584 int free_bytes
, need_bytes
;
2585 xlog_ticket_t
*ntic
;
2590 tic
->t_curr_res
= tic
->t_unit_res
;
2591 xlog_tic_reset_res(tic
);
2597 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
2598 panic("regrant Recovery problem");
2601 spin_lock(&log
->l_grant_lock
);
2602 xlog_trace_loggrant(log
, tic
, "xlog_regrant_write_log_space: enter");
2604 if (XLOG_FORCED_SHUTDOWN(log
))
2607 /* If there are other waiters on the queue then give them a
2608 * chance at logspace before us. Wake up the first waiters,
2609 * if we do not wake up all the waiters then go to sleep waiting
2610 * for more free space, otherwise try to get some space for
2614 if ((ntic
= log
->l_write_headq
)) {
2615 free_bytes
= xlog_space_left(log
, log
->l_grant_write_cycle
,
2616 log
->l_grant_write_bytes
);
2618 ASSERT(ntic
->t_flags
& XLOG_TIC_PERM_RESERV
);
2620 if (free_bytes
< ntic
->t_unit_res
)
2622 free_bytes
-= ntic
->t_unit_res
;
2623 sv_signal(&ntic
->t_sema
);
2624 ntic
= ntic
->t_next
;
2625 } while (ntic
!= log
->l_write_headq
);
2627 if (ntic
!= log
->l_write_headq
) {
2628 if ((tic
->t_flags
& XLOG_TIC_IN_Q
) == 0)
2629 xlog_ins_ticketq(&log
->l_write_headq
, tic
);
2631 xlog_trace_loggrant(log
, tic
,
2632 "xlog_regrant_write_log_space: sleep 1");
2633 XFS_STATS_INC(xs_sleep_logspace
);
2634 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
,
2635 &log
->l_grant_lock
, s
);
2637 /* If we're shutting down, this tic is already
2639 if (XLOG_FORCED_SHUTDOWN(log
)) {
2640 spin_lock(&log
->l_grant_lock
);
2644 xlog_trace_loggrant(log
, tic
,
2645 "xlog_regrant_write_log_space: wake 1");
2646 xlog_grant_push_ail(log
->l_mp
, tic
->t_unit_res
);
2647 spin_lock(&log
->l_grant_lock
);
2651 need_bytes
= tic
->t_unit_res
;
2654 if (XLOG_FORCED_SHUTDOWN(log
))
2657 free_bytes
= xlog_space_left(log
, log
->l_grant_write_cycle
,
2658 log
->l_grant_write_bytes
);
2659 if (free_bytes
< need_bytes
) {
2660 if ((tic
->t_flags
& XLOG_TIC_IN_Q
) == 0)
2661 xlog_ins_ticketq(&log
->l_write_headq
, tic
);
2662 XFS_STATS_INC(xs_sleep_logspace
);
2663 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
, &log
->l_grant_lock
, s
);
2665 /* If we're shutting down, this tic is already off the queue */
2666 if (XLOG_FORCED_SHUTDOWN(log
)) {
2667 spin_lock(&log
->l_grant_lock
);
2671 xlog_trace_loggrant(log
, tic
,
2672 "xlog_regrant_write_log_space: wake 2");
2673 xlog_grant_push_ail(log
->l_mp
, need_bytes
);
2674 spin_lock(&log
->l_grant_lock
);
2676 } else if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2677 xlog_del_ticketq(&log
->l_write_headq
, tic
);
2679 /* we've got enough space */
2680 xlog_grant_add_space_write(log
, need_bytes
);
2682 tail_lsn
= log
->l_tail_lsn
;
2683 if (CYCLE_LSN(tail_lsn
) != log
->l_grant_write_cycle
) {
2684 ASSERT(log
->l_grant_write_cycle
-1 == CYCLE_LSN(tail_lsn
));
2685 ASSERT(log
->l_grant_write_bytes
<= BBTOB(BLOCK_LSN(tail_lsn
)));
2689 xlog_trace_loggrant(log
, tic
, "xlog_regrant_write_log_space: exit");
2690 xlog_verify_grant_head(log
, 1);
2691 spin_unlock(&log
->l_grant_lock
);
2696 if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2697 xlog_del_ticketq(&log
->l_reserve_headq
, tic
);
2698 xlog_trace_loggrant(log
, tic
, "xlog_regrant_write_log_space: err_ret");
2700 * If we are failing, make sure the ticket doesn't have any
2701 * current reservations. We don't want to add this back when
2702 * the ticket/transaction gets cancelled.
2704 tic
->t_curr_res
= 0;
2705 tic
->t_cnt
= 0; /* ungrant will give back unit_res * t_cnt. */
2706 spin_unlock(&log
->l_grant_lock
);
2707 return XFS_ERROR(EIO
);
2708 } /* xlog_regrant_write_log_space */
2711 /* The first cnt-1 times through here we don't need to
2712 * move the grant write head because the permanent
2713 * reservation has reserved cnt times the unit amount.
2714 * Release part of current permanent unit reservation and
2715 * reset current reservation to be one units worth. Also
2716 * move grant reservation head forward.
2719 xlog_regrant_reserve_log_space(xlog_t
*log
,
2720 xlog_ticket_t
*ticket
)
2722 xlog_trace_loggrant(log
, ticket
,
2723 "xlog_regrant_reserve_log_space: enter");
2724 if (ticket
->t_cnt
> 0)
2727 spin_lock(&log
->l_grant_lock
);
2728 xlog_grant_sub_space(log
, ticket
->t_curr_res
);
2729 ticket
->t_curr_res
= ticket
->t_unit_res
;
2730 xlog_tic_reset_res(ticket
);
2731 xlog_trace_loggrant(log
, ticket
,
2732 "xlog_regrant_reserve_log_space: sub current res");
2733 xlog_verify_grant_head(log
, 1);
2735 /* just return if we still have some of the pre-reserved space */
2736 if (ticket
->t_cnt
> 0) {
2737 spin_unlock(&log
->l_grant_lock
);
2741 xlog_grant_add_space_reserve(log
, ticket
->t_unit_res
);
2742 xlog_trace_loggrant(log
, ticket
,
2743 "xlog_regrant_reserve_log_space: exit");
2744 xlog_verify_grant_head(log
, 0);
2745 spin_unlock(&log
->l_grant_lock
);
2746 ticket
->t_curr_res
= ticket
->t_unit_res
;
2747 xlog_tic_reset_res(ticket
);
2748 } /* xlog_regrant_reserve_log_space */
2752 * Give back the space left from a reservation.
2754 * All the information we need to make a correct determination of space left
2755 * is present. For non-permanent reservations, things are quite easy. The
2756 * count should have been decremented to zero. We only need to deal with the
2757 * space remaining in the current reservation part of the ticket. If the
2758 * ticket contains a permanent reservation, there may be left over space which
2759 * needs to be released. A count of N means that N-1 refills of the current
2760 * reservation can be done before we need to ask for more space. The first
2761 * one goes to fill up the first current reservation. Once we run out of
2762 * space, the count will stay at zero and the only space remaining will be
2763 * in the current reservation field.
2766 xlog_ungrant_log_space(xlog_t
*log
,
2767 xlog_ticket_t
*ticket
)
2769 if (ticket
->t_cnt
> 0)
2772 spin_lock(&log
->l_grant_lock
);
2773 xlog_trace_loggrant(log
, ticket
, "xlog_ungrant_log_space: enter");
2775 xlog_grant_sub_space(log
, ticket
->t_curr_res
);
2777 xlog_trace_loggrant(log
, ticket
, "xlog_ungrant_log_space: sub current");
2779 /* If this is a permanent reservation ticket, we may be able to free
2780 * up more space based on the remaining count.
2782 if (ticket
->t_cnt
> 0) {
2783 ASSERT(ticket
->t_flags
& XLOG_TIC_PERM_RESERV
);
2784 xlog_grant_sub_space(log
, ticket
->t_unit_res
*ticket
->t_cnt
);
2787 xlog_trace_loggrant(log
, ticket
, "xlog_ungrant_log_space: exit");
2788 xlog_verify_grant_head(log
, 1);
2789 spin_unlock(&log
->l_grant_lock
);
2790 xfs_log_move_tail(log
->l_mp
, 1);
2791 } /* xlog_ungrant_log_space */
2795 * Atomically put back used ticket.
2798 xlog_state_put_ticket(xlog_t
*log
,
2801 spin_lock(&log
->l_icloglock
);
2802 xlog_ticket_put(log
, tic
);
2803 spin_unlock(&log
->l_icloglock
);
2804 } /* xlog_state_put_ticket */
2807 * Flush iclog to disk if this is the last reference to the given iclog and
2808 * the WANT_SYNC bit is set.
2810 * When this function is entered, the iclog is not necessarily in the
2811 * WANT_SYNC state. It may be sitting around waiting to get filled.
2816 xlog_state_release_iclog(xlog_t
*log
,
2817 xlog_in_core_t
*iclog
)
2819 int sync
= 0; /* do we sync? */
2821 xlog_assign_tail_lsn(log
->l_mp
);
2823 spin_lock(&log
->l_icloglock
);
2825 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2826 spin_unlock(&log
->l_icloglock
);
2827 return XFS_ERROR(EIO
);
2830 ASSERT(iclog
->ic_refcnt
> 0);
2831 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2832 iclog
->ic_state
== XLOG_STATE_WANT_SYNC
);
2834 if (--iclog
->ic_refcnt
== 0 &&
2835 iclog
->ic_state
== XLOG_STATE_WANT_SYNC
) {
2837 iclog
->ic_state
= XLOG_STATE_SYNCING
;
2838 iclog
->ic_header
.h_tail_lsn
= cpu_to_be64(log
->l_tail_lsn
);
2839 xlog_verify_tail_lsn(log
, iclog
, log
->l_tail_lsn
);
2840 /* cycle incremented when incrementing curr_block */
2843 spin_unlock(&log
->l_icloglock
);
2846 * We let the log lock go, so it's possible that we hit a log I/O
2847 * error or some other SHUTDOWN condition that marks the iclog
2848 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2849 * this iclog has consistent data, so we ignore IOERROR
2850 * flags after this point.
2853 return xlog_sync(log
, iclog
);
2857 } /* xlog_state_release_iclog */
2861 * This routine will mark the current iclog in the ring as WANT_SYNC
2862 * and move the current iclog pointer to the next iclog in the ring.
2863 * When this routine is called from xlog_state_get_iclog_space(), the
2864 * exact size of the iclog has not yet been determined. All we know is
2865 * that every data block. We have run out of space in this log record.
2868 xlog_state_switch_iclogs(xlog_t
*log
,
2869 xlog_in_core_t
*iclog
,
2872 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
);
2874 eventual_size
= iclog
->ic_offset
;
2875 iclog
->ic_state
= XLOG_STATE_WANT_SYNC
;
2876 iclog
->ic_header
.h_prev_block
= cpu_to_be32(log
->l_prev_block
);
2877 log
->l_prev_block
= log
->l_curr_block
;
2878 log
->l_prev_cycle
= log
->l_curr_cycle
;
2880 /* roll log?: ic_offset changed later */
2881 log
->l_curr_block
+= BTOBB(eventual_size
)+BTOBB(log
->l_iclog_hsize
);
2883 /* Round up to next log-sunit */
2884 if (XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
) &&
2885 log
->l_mp
->m_sb
.sb_logsunit
> 1) {
2886 __uint32_t sunit_bb
= BTOBB(log
->l_mp
->m_sb
.sb_logsunit
);
2887 log
->l_curr_block
= roundup(log
->l_curr_block
, sunit_bb
);
2890 if (log
->l_curr_block
>= log
->l_logBBsize
) {
2891 log
->l_curr_cycle
++;
2892 if (log
->l_curr_cycle
== XLOG_HEADER_MAGIC_NUM
)
2893 log
->l_curr_cycle
++;
2894 log
->l_curr_block
-= log
->l_logBBsize
;
2895 ASSERT(log
->l_curr_block
>= 0);
2897 ASSERT(iclog
== log
->l_iclog
);
2898 log
->l_iclog
= iclog
->ic_next
;
2899 } /* xlog_state_switch_iclogs */
2903 * Write out all data in the in-core log as of this exact moment in time.
2905 * Data may be written to the in-core log during this call. However,
2906 * we don't guarantee this data will be written out. A change from past
2907 * implementation means this routine will *not* write out zero length LRs.
2909 * Basically, we try and perform an intelligent scan of the in-core logs.
2910 * If we determine there is no flushable data, we just return. There is no
2911 * flushable data if:
2913 * 1. the current iclog is active and has no data; the previous iclog
2914 * is in the active or dirty state.
2915 * 2. the current iclog is drity, and the previous iclog is in the
2916 * active or dirty state.
2918 * We may sleep (call psema) if:
2920 * 1. the current iclog is not in the active nor dirty state.
2921 * 2. the current iclog dirty, and the previous iclog is not in the
2922 * active nor dirty state.
2923 * 3. the current iclog is active, and there is another thread writing
2924 * to this particular iclog.
2925 * 4. a) the current iclog is active and has no other writers
2926 * b) when we return from flushing out this iclog, it is still
2927 * not in the active nor dirty state.
2930 xlog_state_sync_all(xlog_t
*log
, uint flags
, int *log_flushed
)
2932 xlog_in_core_t
*iclog
;
2935 spin_lock(&log
->l_icloglock
);
2937 iclog
= log
->l_iclog
;
2938 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2939 spin_unlock(&log
->l_icloglock
);
2940 return XFS_ERROR(EIO
);
2943 /* If the head iclog is not active nor dirty, we just attach
2944 * ourselves to the head and go to sleep.
2946 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2947 iclog
->ic_state
== XLOG_STATE_DIRTY
) {
2949 * If the head is dirty or (active and empty), then
2950 * we need to look at the previous iclog. If the previous
2951 * iclog is active or dirty we are done. There is nothing
2952 * to sync out. Otherwise, we attach ourselves to the
2953 * previous iclog and go to sleep.
2955 if (iclog
->ic_state
== XLOG_STATE_DIRTY
||
2956 (iclog
->ic_refcnt
== 0 && iclog
->ic_offset
== 0)) {
2957 iclog
= iclog
->ic_prev
;
2958 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2959 iclog
->ic_state
== XLOG_STATE_DIRTY
)
2964 if (iclog
->ic_refcnt
== 0) {
2965 /* We are the only one with access to this
2966 * iclog. Flush it out now. There should
2967 * be a roundoff of zero to show that someone
2968 * has already taken care of the roundoff from
2969 * the previous sync.
2972 lsn
= be64_to_cpu(iclog
->ic_header
.h_lsn
);
2973 xlog_state_switch_iclogs(log
, iclog
, 0);
2974 spin_unlock(&log
->l_icloglock
);
2976 if (xlog_state_release_iclog(log
, iclog
))
2977 return XFS_ERROR(EIO
);
2979 spin_lock(&log
->l_icloglock
);
2980 if (be64_to_cpu(iclog
->ic_header
.h_lsn
) == lsn
&&
2981 iclog
->ic_state
!= XLOG_STATE_DIRTY
)
2986 /* Someone else is writing to this iclog.
2987 * Use its call to flush out the data. However,
2988 * the other thread may not force out this LR,
2989 * so we mark it WANT_SYNC.
2991 xlog_state_switch_iclogs(log
, iclog
, 0);
2997 /* By the time we come around again, the iclog could've been filled
2998 * which would give it another lsn. If we have a new lsn, just
2999 * return because the relevant data has been flushed.
3002 if (flags
& XFS_LOG_SYNC
) {
3004 * We must check if we're shutting down here, before
3005 * we wait, while we're holding the l_icloglock.
3006 * Then we check again after waking up, in case our
3007 * sleep was disturbed by a bad news.
3009 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3010 spin_unlock(&log
->l_icloglock
);
3011 return XFS_ERROR(EIO
);
3013 XFS_STATS_INC(xs_log_force_sleep
);
3014 sv_wait(&iclog
->ic_forcesema
, PINOD
, &log
->l_icloglock
, s
);
3016 * No need to grab the log lock here since we're
3017 * only deciding whether or not to return EIO
3018 * and the memory read should be atomic.
3020 if (iclog
->ic_state
& XLOG_STATE_IOERROR
)
3021 return XFS_ERROR(EIO
);
3027 spin_unlock(&log
->l_icloglock
);
3030 } /* xlog_state_sync_all */
3034 * Used by code which implements synchronous log forces.
3036 * Find in-core log with lsn.
3037 * If it is in the DIRTY state, just return.
3038 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3039 * state and go to sleep or return.
3040 * If it is in any other state, go to sleep or return.
3042 * If filesystem activity goes to zero, the iclog will get flushed only by
3046 xlog_state_sync(xlog_t
*log
,
3051 xlog_in_core_t
*iclog
;
3052 int already_slept
= 0;
3055 spin_lock(&log
->l_icloglock
);
3056 iclog
= log
->l_iclog
;
3058 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3059 spin_unlock(&log
->l_icloglock
);
3060 return XFS_ERROR(EIO
);
3064 if (be64_to_cpu(iclog
->ic_header
.h_lsn
) != lsn
) {
3065 iclog
= iclog
->ic_next
;
3069 if (iclog
->ic_state
== XLOG_STATE_DIRTY
) {
3070 spin_unlock(&log
->l_icloglock
);
3074 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
) {
3076 * We sleep here if we haven't already slept (e.g.
3077 * this is the first time we've looked at the correct
3078 * iclog buf) and the buffer before us is going to
3079 * be sync'ed. The reason for this is that if we
3080 * are doing sync transactions here, by waiting for
3081 * the previous I/O to complete, we can allow a few
3082 * more transactions into this iclog before we close
3085 * Otherwise, we mark the buffer WANT_SYNC, and bump
3086 * up the refcnt so we can release the log (which drops
3087 * the ref count). The state switch keeps new transaction
3088 * commits from using this buffer. When the current commits
3089 * finish writing into the buffer, the refcount will drop to
3090 * zero and the buffer will go out then.
3092 if (!already_slept
&&
3093 (iclog
->ic_prev
->ic_state
& (XLOG_STATE_WANT_SYNC
|
3094 XLOG_STATE_SYNCING
))) {
3095 ASSERT(!(iclog
->ic_state
& XLOG_STATE_IOERROR
));
3096 XFS_STATS_INC(xs_log_force_sleep
);
3097 sv_wait(&iclog
->ic_prev
->ic_writesema
, PSWP
,
3098 &log
->l_icloglock
, s
);
3104 xlog_state_switch_iclogs(log
, iclog
, 0);
3105 spin_unlock(&log
->l_icloglock
);
3106 if (xlog_state_release_iclog(log
, iclog
))
3107 return XFS_ERROR(EIO
);
3109 spin_lock(&log
->l_icloglock
);
3113 if ((flags
& XFS_LOG_SYNC
) && /* sleep */
3114 !(iclog
->ic_state
& (XLOG_STATE_ACTIVE
| XLOG_STATE_DIRTY
))) {
3117 * Don't wait on the forcesema if we know that we've
3118 * gotten a log write error.
3120 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3121 spin_unlock(&log
->l_icloglock
);
3122 return XFS_ERROR(EIO
);
3124 XFS_STATS_INC(xs_log_force_sleep
);
3125 sv_wait(&iclog
->ic_forcesema
, PSWP
, &log
->l_icloglock
, s
);
3127 * No need to grab the log lock here since we're
3128 * only deciding whether or not to return EIO
3129 * and the memory read should be atomic.
3131 if (iclog
->ic_state
& XLOG_STATE_IOERROR
)
3132 return XFS_ERROR(EIO
);
3134 } else { /* just return */
3135 spin_unlock(&log
->l_icloglock
);
3139 } while (iclog
!= log
->l_iclog
);
3141 spin_unlock(&log
->l_icloglock
);
3143 } /* xlog_state_sync */
3147 * Called when we want to mark the current iclog as being ready to sync to
3151 xlog_state_want_sync(xlog_t
*log
, xlog_in_core_t
*iclog
)
3153 spin_lock(&log
->l_icloglock
);
3155 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
) {
3156 xlog_state_switch_iclogs(log
, iclog
, 0);
3158 ASSERT(iclog
->ic_state
&
3159 (XLOG_STATE_WANT_SYNC
|XLOG_STATE_IOERROR
));
3162 spin_unlock(&log
->l_icloglock
);
3163 } /* xlog_state_want_sync */
3167 /*****************************************************************************
3171 *****************************************************************************
3175 * Algorithm doesn't take into account page size. ;-(
3178 xlog_state_ticket_alloc(xlog_t
*log
)
3180 xlog_ticket_t
*t_list
;
3181 xlog_ticket_t
*next
;
3183 uint i
= (PAGE_SIZE
/ sizeof(xlog_ticket_t
)) - 2;
3186 * The kmem_zalloc may sleep, so we shouldn't be holding the
3187 * global lock. XXXmiken: may want to use zone allocator.
3189 buf
= (xfs_caddr_t
) kmem_zalloc(PAGE_SIZE
, KM_SLEEP
);
3191 spin_lock(&log
->l_icloglock
);
3193 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3194 t_list
= (xlog_ticket_t
*)buf
;
3195 t_list
->t_next
= log
->l_unmount_free
;
3196 log
->l_unmount_free
= t_list
++;
3197 log
->l_ticket_cnt
++;
3198 log
->l_ticket_tcnt
++;
3200 /* Next ticket becomes first ticket attached to ticket free list */
3201 if (log
->l_freelist
!= NULL
) {
3202 ASSERT(log
->l_tail
!= NULL
);
3203 log
->l_tail
->t_next
= t_list
;
3205 log
->l_freelist
= t_list
;
3207 log
->l_ticket_cnt
++;
3208 log
->l_ticket_tcnt
++;
3210 /* Cycle through rest of alloc'ed memory, building up free Q */
3211 for ( ; i
> 0; i
--) {
3213 t_list
->t_next
= next
;
3215 log
->l_ticket_cnt
++;
3216 log
->l_ticket_tcnt
++;
3218 t_list
->t_next
= NULL
;
3219 log
->l_tail
= t_list
;
3220 spin_unlock(&log
->l_icloglock
);
3221 } /* xlog_state_ticket_alloc */
3225 * Put ticket into free list
3227 * Assumption: log lock is held around this call.
3230 xlog_ticket_put(xlog_t
*log
,
3231 xlog_ticket_t
*ticket
)
3233 sv_destroy(&ticket
->t_sema
);
3236 * Don't think caching will make that much difference. It's
3237 * more important to make debug easier.
3240 /* real code will want to use LIFO for caching */
3241 ticket
->t_next
= log
->l_freelist
;
3242 log
->l_freelist
= ticket
;
3243 /* no need to clear fields */
3245 /* When we debug, it is easier if tickets are cycled */
3246 ticket
->t_next
= NULL
;
3248 log
->l_tail
->t_next
= ticket
;
3250 ASSERT(log
->l_freelist
== NULL
);
3251 log
->l_freelist
= ticket
;
3253 log
->l_tail
= ticket
;
3255 log
->l_ticket_cnt
++;
3256 } /* xlog_ticket_put */
3260 * Grab ticket off freelist or allocation some more
3262 STATIC xlog_ticket_t
*
3263 xlog_ticket_get(xlog_t
*log
,
3273 if (log
->l_freelist
== NULL
)
3274 xlog_state_ticket_alloc(log
); /* potentially sleep */
3276 spin_lock(&log
->l_icloglock
);
3277 if (log
->l_freelist
== NULL
) {
3278 spin_unlock(&log
->l_icloglock
);
3281 tic
= log
->l_freelist
;
3282 log
->l_freelist
= tic
->t_next
;
3283 if (log
->l_freelist
== NULL
)
3285 log
->l_ticket_cnt
--;
3286 spin_unlock(&log
->l_icloglock
);
3289 * Permanent reservations have up to 'cnt'-1 active log operations
3290 * in the log. A unit in this case is the amount of space for one
3291 * of these log operations. Normal reservations have a cnt of 1
3292 * and their unit amount is the total amount of space required.
3294 * The following lines of code account for non-transaction data
3295 * which occupy space in the on-disk log.
3297 * Normal form of a transaction is:
3298 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3299 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3301 * We need to account for all the leadup data and trailer data
3302 * around the transaction data.
3303 * And then we need to account for the worst case in terms of using
3305 * The worst case will happen if:
3306 * - the placement of the transaction happens to be such that the
3307 * roundoff is at its maximum
3308 * - the transaction data is synced before the commit record is synced
3309 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3310 * Therefore the commit record is in its own Log Record.
3311 * This can happen as the commit record is called with its
3312 * own region to xlog_write().
3313 * This then means that in the worst case, roundoff can happen for
3314 * the commit-rec as well.
3315 * The commit-rec is smaller than padding in this scenario and so it is
3316 * not added separately.
3319 /* for trans header */
3320 unit_bytes
+= sizeof(xlog_op_header_t
);
3321 unit_bytes
+= sizeof(xfs_trans_header_t
);
3324 unit_bytes
+= sizeof(xlog_op_header_t
);
3326 /* for LR headers */
3327 num_headers
= ((unit_bytes
+ log
->l_iclog_size
-1) >> log
->l_iclog_size_log
);
3328 unit_bytes
+= log
->l_iclog_hsize
* num_headers
;
3330 /* for commit-rec LR header - note: padding will subsume the ophdr */
3331 unit_bytes
+= log
->l_iclog_hsize
;
3333 /* for split-recs - ophdrs added when data split over LRs */
3334 unit_bytes
+= sizeof(xlog_op_header_t
) * num_headers
;
3336 /* for roundoff padding for transaction data and one for commit record */
3337 if (XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
) &&
3338 log
->l_mp
->m_sb
.sb_logsunit
> 1) {
3339 /* log su roundoff */
3340 unit_bytes
+= 2*log
->l_mp
->m_sb
.sb_logsunit
;
3343 unit_bytes
+= 2*BBSIZE
;
3346 tic
->t_unit_res
= unit_bytes
;
3347 tic
->t_curr_res
= unit_bytes
;
3350 tic
->t_tid
= (xlog_tid_t
)((__psint_t
)tic
& 0xffffffff);
3351 tic
->t_clientid
= client
;
3352 tic
->t_flags
= XLOG_TIC_INITED
;
3353 tic
->t_trans_type
= 0;
3354 if (xflags
& XFS_LOG_PERM_RESERV
)
3355 tic
->t_flags
|= XLOG_TIC_PERM_RESERV
;
3356 sv_init(&(tic
->t_sema
), SV_DEFAULT
, "logtick");
3358 xlog_tic_reset_res(tic
);
3361 } /* xlog_ticket_get */
3364 /******************************************************************************
3366 * Log debug routines
3368 ******************************************************************************
3372 * Make sure that the destination ptr is within the valid data region of
3373 * one of the iclogs. This uses backup pointers stored in a different
3374 * part of the log in case we trash the log structure.
3377 xlog_verify_dest_ptr(xlog_t
*log
,
3383 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
3384 if (ptr
>= (__psint_t
)log
->l_iclog_bak
[i
] &&
3385 ptr
<= (__psint_t
)log
->l_iclog_bak
[i
]+log
->l_iclog_size
)
3389 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3390 } /* xlog_verify_dest_ptr */
3393 xlog_verify_grant_head(xlog_t
*log
, int equals
)
3395 if (log
->l_grant_reserve_cycle
== log
->l_grant_write_cycle
) {
3397 ASSERT(log
->l_grant_reserve_bytes
>= log
->l_grant_write_bytes
);
3399 ASSERT(log
->l_grant_reserve_bytes
> log
->l_grant_write_bytes
);
3401 ASSERT(log
->l_grant_reserve_cycle
-1 == log
->l_grant_write_cycle
);
3402 ASSERT(log
->l_grant_write_bytes
>= log
->l_grant_reserve_bytes
);
3404 } /* xlog_verify_grant_head */
3406 /* check if it will fit */
3408 xlog_verify_tail_lsn(xlog_t
*log
,
3409 xlog_in_core_t
*iclog
,
3414 if (CYCLE_LSN(tail_lsn
) == log
->l_prev_cycle
) {
3416 log
->l_logBBsize
- (log
->l_prev_block
- BLOCK_LSN(tail_lsn
));
3417 if (blocks
< BTOBB(iclog
->ic_offset
)+BTOBB(log
->l_iclog_hsize
))
3418 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3420 ASSERT(CYCLE_LSN(tail_lsn
)+1 == log
->l_prev_cycle
);
3422 if (BLOCK_LSN(tail_lsn
) == log
->l_prev_block
)
3423 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3425 blocks
= BLOCK_LSN(tail_lsn
) - log
->l_prev_block
;
3426 if (blocks
< BTOBB(iclog
->ic_offset
) + 1)
3427 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3429 } /* xlog_verify_tail_lsn */
3432 * Perform a number of checks on the iclog before writing to disk.
3434 * 1. Make sure the iclogs are still circular
3435 * 2. Make sure we have a good magic number
3436 * 3. Make sure we don't have magic numbers in the data
3437 * 4. Check fields of each log operation header for:
3438 * A. Valid client identifier
3439 * B. tid ptr value falls in valid ptr space (user space code)
3440 * C. Length in log record header is correct according to the
3441 * individual operation headers within record.
3442 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3443 * log, check the preceding blocks of the physical log to make sure all
3444 * the cycle numbers agree with the current cycle number.
3447 xlog_verify_iclog(xlog_t
*log
,
3448 xlog_in_core_t
*iclog
,
3452 xlog_op_header_t
*ophead
;
3453 xlog_in_core_t
*icptr
;
3454 xlog_in_core_2_t
*xhdr
;
3456 xfs_caddr_t base_ptr
;
3457 __psint_t field_offset
;
3459 int len
, i
, j
, k
, op_len
;
3462 /* check validity of iclog pointers */
3463 spin_lock(&log
->l_icloglock
);
3464 icptr
= log
->l_iclog
;
3465 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
3467 xlog_panic("xlog_verify_iclog: invalid ptr");
3468 icptr
= icptr
->ic_next
;
3470 if (icptr
!= log
->l_iclog
)
3471 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3472 spin_unlock(&log
->l_icloglock
);
3474 /* check log magic numbers */
3475 if (be32_to_cpu(iclog
->ic_header
.h_magicno
) != XLOG_HEADER_MAGIC_NUM
)
3476 xlog_panic("xlog_verify_iclog: invalid magic num");
3478 ptr
= (xfs_caddr_t
) &iclog
->ic_header
;
3479 for (ptr
+= BBSIZE
; ptr
< ((xfs_caddr_t
)&iclog
->ic_header
) + count
;
3481 if (be32_to_cpu(*(__be32
*)ptr
) == XLOG_HEADER_MAGIC_NUM
)
3482 xlog_panic("xlog_verify_iclog: unexpected magic num");
3486 len
= be32_to_cpu(iclog
->ic_header
.h_num_logops
);
3487 ptr
= iclog
->ic_datap
;
3489 ophead
= (xlog_op_header_t
*)ptr
;
3490 xhdr
= (xlog_in_core_2_t
*)&iclog
->ic_header
;
3491 for (i
= 0; i
< len
; i
++) {
3492 ophead
= (xlog_op_header_t
*)ptr
;
3494 /* clientid is only 1 byte */
3495 field_offset
= (__psint_t
)
3496 ((xfs_caddr_t
)&(ophead
->oh_clientid
) - base_ptr
);
3497 if (syncing
== B_FALSE
|| (field_offset
& 0x1ff)) {
3498 clientid
= ophead
->oh_clientid
;
3500 idx
= BTOBBT((xfs_caddr_t
)&(ophead
->oh_clientid
) - iclog
->ic_datap
);
3501 if (idx
>= (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
)) {
3502 j
= idx
/ (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3503 k
= idx
% (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3504 clientid
= xlog_get_client_id(
3505 xhdr
[j
].hic_xheader
.xh_cycle_data
[k
]);
3507 clientid
= xlog_get_client_id(
3508 iclog
->ic_header
.h_cycle_data
[idx
]);
3511 if (clientid
!= XFS_TRANSACTION
&& clientid
!= XFS_LOG
)
3512 cmn_err(CE_WARN
, "xlog_verify_iclog: "
3513 "invalid clientid %d op 0x%p offset 0x%lx",
3514 clientid
, ophead
, (unsigned long)field_offset
);
3517 field_offset
= (__psint_t
)
3518 ((xfs_caddr_t
)&(ophead
->oh_len
) - base_ptr
);
3519 if (syncing
== B_FALSE
|| (field_offset
& 0x1ff)) {
3520 op_len
= be32_to_cpu(ophead
->oh_len
);
3522 idx
= BTOBBT((__psint_t
)&ophead
->oh_len
-
3523 (__psint_t
)iclog
->ic_datap
);
3524 if (idx
>= (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
)) {
3525 j
= idx
/ (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3526 k
= idx
% (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3527 op_len
= be32_to_cpu(xhdr
[j
].hic_xheader
.xh_cycle_data
[k
]);
3529 op_len
= be32_to_cpu(iclog
->ic_header
.h_cycle_data
[idx
]);
3532 ptr
+= sizeof(xlog_op_header_t
) + op_len
;
3534 } /* xlog_verify_iclog */
3538 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3544 xlog_in_core_t
*iclog
, *ic
;
3546 iclog
= log
->l_iclog
;
3547 if (! (iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
3549 * Mark all the incore logs IOERROR.
3550 * From now on, no log flushes will result.
3554 ic
->ic_state
= XLOG_STATE_IOERROR
;
3556 } while (ic
!= iclog
);
3560 * Return non-zero, if state transition has already happened.
3566 * This is called from xfs_force_shutdown, when we're forcibly
3567 * shutting down the filesystem, typically because of an IO error.
3568 * Our main objectives here are to make sure that:
3569 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3570 * parties to find out, 'atomically'.
3571 * b. those who're sleeping on log reservations, pinned objects and
3572 * other resources get woken up, and be told the bad news.
3573 * c. nothing new gets queued up after (a) and (b) are done.
3574 * d. if !logerror, flush the iclogs to disk, then seal them off
3578 xfs_log_force_umount(
3579 struct xfs_mount
*mp
,
3590 * If this happens during log recovery, don't worry about
3591 * locking; the log isn't open for business yet.
3594 log
->l_flags
& XLOG_ACTIVE_RECOVERY
) {
3595 mp
->m_flags
|= XFS_MOUNT_FS_SHUTDOWN
;
3596 XFS_BUF_DONE(mp
->m_sb_bp
);
3601 * Somebody could've already done the hard work for us.
3602 * No need to get locks for this.
3604 if (logerror
&& log
->l_iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3605 ASSERT(XLOG_FORCED_SHUTDOWN(log
));
3610 * We must hold both the GRANT lock and the LOG lock,
3611 * before we mark the filesystem SHUTDOWN and wake
3612 * everybody up to tell the bad news.
3614 spin_lock(&log
->l_grant_lock
);
3615 spin_lock(&log
->l_icloglock
);
3616 mp
->m_flags
|= XFS_MOUNT_FS_SHUTDOWN
;
3617 XFS_BUF_DONE(mp
->m_sb_bp
);
3619 * This flag is sort of redundant because of the mount flag, but
3620 * it's good to maintain the separation between the log and the rest
3623 log
->l_flags
|= XLOG_IO_ERROR
;
3626 * If we hit a log error, we want to mark all the iclogs IOERROR
3627 * while we're still holding the loglock.
3630 retval
= xlog_state_ioerror(log
);
3631 spin_unlock(&log
->l_icloglock
);
3634 * We don't want anybody waiting for log reservations
3635 * after this. That means we have to wake up everybody
3636 * queued up on reserve_headq as well as write_headq.
3637 * In addition, we make sure in xlog_{re}grant_log_space
3638 * that we don't enqueue anything once the SHUTDOWN flag
3639 * is set, and this action is protected by the GRANTLOCK.
3641 if ((tic
= log
->l_reserve_headq
)) {
3643 sv_signal(&tic
->t_sema
);
3645 } while (tic
!= log
->l_reserve_headq
);
3648 if ((tic
= log
->l_write_headq
)) {
3650 sv_signal(&tic
->t_sema
);
3652 } while (tic
!= log
->l_write_headq
);
3654 spin_unlock(&log
->l_grant_lock
);
3656 if (! (log
->l_iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
3659 * Force the incore logs to disk before shutting the
3660 * log down completely.
3662 xlog_state_sync_all(log
, XFS_LOG_FORCE
|XFS_LOG_SYNC
, &dummy
);
3663 spin_lock(&log
->l_icloglock
);
3664 retval
= xlog_state_ioerror(log
);
3665 spin_unlock(&log
->l_icloglock
);
3668 * Wake up everybody waiting on xfs_log_force.
3669 * Callback all log item committed functions as if the
3670 * log writes were completed.
3672 xlog_state_do_callback(log
, XFS_LI_ABORTED
, NULL
);
3674 #ifdef XFSERRORDEBUG
3676 xlog_in_core_t
*iclog
;
3678 spin_lock(&log
->l_icloglock
);
3679 iclog
= log
->l_iclog
;
3681 ASSERT(iclog
->ic_callback
== 0);
3682 iclog
= iclog
->ic_next
;
3683 } while (iclog
!= log
->l_iclog
);
3684 spin_unlock(&log
->l_icloglock
);
3687 /* return non-zero if log IOERROR transition had already happened */
3692 xlog_iclogs_empty(xlog_t
*log
)
3694 xlog_in_core_t
*iclog
;
3696 iclog
= log
->l_iclog
;
3698 /* endianness does not matter here, zero is zero in
3701 if (iclog
->ic_header
.h_num_logops
)
3703 iclog
= iclog
->ic_next
;
3704 } while (iclog
!= log
->l_iclog
);