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.
501 xfs_log_mount(xfs_mount_t
*mp
,
502 xfs_buftarg_t
*log_target
,
503 xfs_daddr_t blk_offset
,
506 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
))
507 cmn_err(CE_NOTE
, "XFS mounting filesystem %s", mp
->m_fsname
);
510 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
512 ASSERT(mp
->m_flags
& XFS_MOUNT_RDONLY
);
515 mp
->m_log
= xlog_alloc_log(mp
, log_target
, blk_offset
, num_bblks
);
518 * skip log recovery on a norecovery mount. pretend it all
521 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
)) {
522 int error
, readonly
= (mp
->m_flags
& XFS_MOUNT_RDONLY
);
525 mp
->m_flags
&= ~XFS_MOUNT_RDONLY
;
527 error
= xlog_recover(mp
->m_log
);
530 mp
->m_flags
|= XFS_MOUNT_RDONLY
;
532 cmn_err(CE_WARN
, "XFS: log mount/recovery failed: error %d", error
);
533 xlog_dealloc_log(mp
->m_log
);
538 /* Normal transactions can now occur */
539 mp
->m_log
->l_flags
&= ~XLOG_ACTIVE_RECOVERY
;
541 /* End mounting message in xfs_log_mount_finish */
543 } /* xfs_log_mount */
546 * Finish the recovery of the file system. This is separate from
547 * the xfs_log_mount() call, because it depends on the code in
548 * xfs_mountfs() to read in the root and real-time bitmap inodes
549 * between calling xfs_log_mount() and here.
551 * mp - ubiquitous xfs mount point structure
554 xfs_log_mount_finish(xfs_mount_t
*mp
, int mfsi_flags
)
558 if (!(mp
->m_flags
& XFS_MOUNT_NORECOVERY
))
559 error
= xlog_recover_finish(mp
->m_log
, mfsi_flags
);
562 ASSERT(mp
->m_flags
& XFS_MOUNT_RDONLY
);
569 * Unmount processing for the log.
572 xfs_log_unmount(xfs_mount_t
*mp
)
576 error
= xfs_log_unmount_write(mp
);
577 xfs_log_unmount_dealloc(mp
);
582 * Final log writes as part of unmount.
584 * Mark the filesystem clean as unmount happens. Note that during relocation
585 * this routine needs to be executed as part of source-bag while the
586 * deallocation must not be done until source-end.
590 * Unmount record used to have a string "Unmount filesystem--" in the
591 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
592 * We just write the magic number now since that particular field isn't
593 * currently architecture converted and "nUmount" is a bit foo.
594 * As far as I know, there weren't any dependencies on the old behaviour.
598 xfs_log_unmount_write(xfs_mount_t
*mp
)
600 xlog_t
*log
= mp
->m_log
;
601 xlog_in_core_t
*iclog
;
603 xlog_in_core_t
*first_iclog
;
605 xfs_log_iovec_t reg
[1];
606 xfs_log_ticket_t tic
= NULL
;
610 /* the data section must be 32 bit size aligned */
614 __uint32_t pad2
; /* may as well make it 64 bits */
615 } magic
= { XLOG_UNMOUNT_TYPE
, 0, 0 };
618 * Don't write out unmount record on read-only mounts.
619 * Or, if we are doing a forced umount (typically because of IO errors).
621 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
624 xfs_log_force(mp
, 0, XFS_LOG_FORCE
|XFS_LOG_SYNC
);
627 first_iclog
= iclog
= log
->l_iclog
;
629 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
630 ASSERT(iclog
->ic_state
& XLOG_STATE_ACTIVE
);
631 ASSERT(iclog
->ic_offset
== 0);
633 iclog
= iclog
->ic_next
;
634 } while (iclog
!= first_iclog
);
636 if (! (XLOG_FORCED_SHUTDOWN(log
))) {
637 reg
[0].i_addr
= (void*)&magic
;
638 reg
[0].i_len
= sizeof(magic
);
639 XLOG_VEC_SET_TYPE(®
[0], XLOG_REG_TYPE_UNMOUNT
);
641 error
= xfs_log_reserve(mp
, 600, 1, &tic
,
642 XFS_LOG
, 0, XLOG_UNMOUNT_REC_TYPE
);
644 /* remove inited flag */
645 ((xlog_ticket_t
*)tic
)->t_flags
= 0;
646 error
= xlog_write(mp
, reg
, 1, tic
, &lsn
,
647 NULL
, XLOG_UNMOUNT_TRANS
);
649 * At this point, we're umounting anyway,
650 * so there's no point in transitioning log state
651 * to IOERROR. Just continue...
656 xfs_fs_cmn_err(CE_ALERT
, mp
,
657 "xfs_log_unmount: unmount record failed");
661 spin_lock(&log
->l_icloglock
);
662 iclog
= log
->l_iclog
;
664 spin_unlock(&log
->l_icloglock
);
665 xlog_state_want_sync(log
, iclog
);
666 (void) xlog_state_release_iclog(log
, iclog
);
668 spin_lock(&log
->l_icloglock
);
669 if (!(iclog
->ic_state
== XLOG_STATE_ACTIVE
||
670 iclog
->ic_state
== XLOG_STATE_DIRTY
)) {
671 if (!XLOG_FORCED_SHUTDOWN(log
)) {
672 sv_wait(&iclog
->ic_forcesema
, PMEM
,
673 &log
->l_icloglock
, s
);
675 spin_unlock(&log
->l_icloglock
);
678 spin_unlock(&log
->l_icloglock
);
681 xlog_trace_loggrant(log
, tic
, "unmount rec");
682 xlog_ungrant_log_space(log
, tic
);
683 xlog_state_put_ticket(log
, tic
);
687 * We're already in forced_shutdown mode, couldn't
688 * even attempt to write out the unmount transaction.
690 * Go through the motions of sync'ing and releasing
691 * the iclog, even though no I/O will actually happen,
692 * we need to wait for other log I/Os that may already
693 * be in progress. Do this as a separate section of
694 * code so we'll know if we ever get stuck here that
695 * we're in this odd situation of trying to unmount
696 * a file system that went into forced_shutdown as
697 * the result of an unmount..
699 spin_lock(&log
->l_icloglock
);
700 iclog
= log
->l_iclog
;
702 spin_unlock(&log
->l_icloglock
);
704 xlog_state_want_sync(log
, iclog
);
705 (void) xlog_state_release_iclog(log
, iclog
);
707 spin_lock(&log
->l_icloglock
);
709 if ( ! ( iclog
->ic_state
== XLOG_STATE_ACTIVE
710 || iclog
->ic_state
== XLOG_STATE_DIRTY
711 || iclog
->ic_state
== XLOG_STATE_IOERROR
) ) {
713 sv_wait(&iclog
->ic_forcesema
, PMEM
,
714 &log
->l_icloglock
, s
);
716 spin_unlock(&log
->l_icloglock
);
721 } /* xfs_log_unmount_write */
724 * Deallocate log structures for unmount/relocation.
727 xfs_log_unmount_dealloc(xfs_mount_t
*mp
)
729 xlog_dealloc_log(mp
->m_log
);
733 * Write region vectors to log. The write happens using the space reservation
734 * of the ticket (tic). It is not a requirement that all writes for a given
735 * transaction occur with one call to xfs_log_write().
738 xfs_log_write(xfs_mount_t
* mp
,
739 xfs_log_iovec_t reg
[],
741 xfs_log_ticket_t tic
,
742 xfs_lsn_t
*start_lsn
)
745 xlog_t
*log
= mp
->m_log
;
747 if (XLOG_FORCED_SHUTDOWN(log
))
748 return XFS_ERROR(EIO
);
750 if ((error
= xlog_write(mp
, reg
, nentries
, tic
, start_lsn
, NULL
, 0))) {
751 xfs_force_shutdown(mp
, SHUTDOWN_LOG_IO_ERROR
);
754 } /* xfs_log_write */
758 xfs_log_move_tail(xfs_mount_t
*mp
,
762 xlog_t
*log
= mp
->m_log
;
763 int need_bytes
, free_bytes
, cycle
, bytes
;
765 if (XLOG_FORCED_SHUTDOWN(log
))
767 ASSERT(!XFS_FORCED_SHUTDOWN(mp
));
770 /* needed since sync_lsn is 64 bits */
771 spin_lock(&log
->l_icloglock
);
772 tail_lsn
= log
->l_last_sync_lsn
;
773 spin_unlock(&log
->l_icloglock
);
776 spin_lock(&log
->l_grant_lock
);
778 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
782 log
->l_tail_lsn
= tail_lsn
;
785 if ((tic
= log
->l_write_headq
)) {
787 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
788 panic("Recovery problem");
790 cycle
= log
->l_grant_write_cycle
;
791 bytes
= log
->l_grant_write_bytes
;
792 free_bytes
= xlog_space_left(log
, cycle
, bytes
);
794 ASSERT(tic
->t_flags
& XLOG_TIC_PERM_RESERV
);
796 if (free_bytes
< tic
->t_unit_res
&& tail_lsn
!= 1)
799 free_bytes
-= tic
->t_unit_res
;
800 sv_signal(&tic
->t_sema
);
802 } while (tic
!= log
->l_write_headq
);
804 if ((tic
= log
->l_reserve_headq
)) {
806 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
807 panic("Recovery problem");
809 cycle
= log
->l_grant_reserve_cycle
;
810 bytes
= log
->l_grant_reserve_bytes
;
811 free_bytes
= xlog_space_left(log
, cycle
, bytes
);
813 if (tic
->t_flags
& XLOG_TIC_PERM_RESERV
)
814 need_bytes
= tic
->t_unit_res
*tic
->t_cnt
;
816 need_bytes
= tic
->t_unit_res
;
817 if (free_bytes
< need_bytes
&& tail_lsn
!= 1)
820 free_bytes
-= need_bytes
;
821 sv_signal(&tic
->t_sema
);
823 } while (tic
!= log
->l_reserve_headq
);
825 spin_unlock(&log
->l_grant_lock
);
826 } /* xfs_log_move_tail */
829 * Determine if we have a transaction that has gone to disk
830 * that needs to be covered. Log activity needs to be idle (no AIL and
831 * nothing in the iclogs). And, we need to be in the right state indicating
832 * something has gone out.
835 xfs_log_need_covered(xfs_mount_t
*mp
)
838 xlog_t
*log
= mp
->m_log
;
840 if (!xfs_fs_writable(mp
))
843 spin_lock(&log
->l_icloglock
);
844 if (((log
->l_covered_state
== XLOG_STATE_COVER_NEED
) ||
845 (log
->l_covered_state
== XLOG_STATE_COVER_NEED2
))
846 && !xfs_trans_first_ail(mp
, &gen
)
847 && xlog_iclogs_empty(log
)) {
848 if (log
->l_covered_state
== XLOG_STATE_COVER_NEED
)
849 log
->l_covered_state
= XLOG_STATE_COVER_DONE
;
851 ASSERT(log
->l_covered_state
== XLOG_STATE_COVER_NEED2
);
852 log
->l_covered_state
= XLOG_STATE_COVER_DONE2
;
856 spin_unlock(&log
->l_icloglock
);
860 /******************************************************************************
864 ******************************************************************************
867 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
868 * The log manager must keep track of the last LR which was committed
869 * to disk. The lsn of this LR will become the new tail_lsn whenever
870 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
871 * the situation where stuff could be written into the log but nothing
872 * was ever in the AIL when asked. Eventually, we panic since the
873 * tail hits the head.
875 * We may be holding the log iclog lock upon entering this routine.
878 xlog_assign_tail_lsn(xfs_mount_t
*mp
)
881 xlog_t
*log
= mp
->m_log
;
883 tail_lsn
= xfs_trans_tail_ail(mp
);
884 spin_lock(&log
->l_grant_lock
);
886 log
->l_tail_lsn
= tail_lsn
;
888 tail_lsn
= log
->l_tail_lsn
= log
->l_last_sync_lsn
;
890 spin_unlock(&log
->l_grant_lock
);
893 } /* xlog_assign_tail_lsn */
897 * Return the space in the log between the tail and the head. The head
898 * is passed in the cycle/bytes formal parms. In the special case where
899 * the reserve head has wrapped passed the tail, this calculation is no
900 * longer valid. In this case, just return 0 which means there is no space
901 * in the log. This works for all places where this function is called
902 * with the reserve head. Of course, if the write head were to ever
903 * wrap the tail, we should blow up. Rather than catch this case here,
904 * we depend on other ASSERTions in other parts of the code. XXXmiken
906 * This code also handles the case where the reservation head is behind
907 * the tail. The details of this case are described below, but the end
908 * result is that we return the size of the log as the amount of space left.
911 xlog_space_left(xlog_t
*log
, int cycle
, int bytes
)
917 tail_bytes
= BBTOB(BLOCK_LSN(log
->l_tail_lsn
));
918 tail_cycle
= CYCLE_LSN(log
->l_tail_lsn
);
919 if ((tail_cycle
== cycle
) && (bytes
>= tail_bytes
)) {
920 free_bytes
= log
->l_logsize
- (bytes
- tail_bytes
);
921 } else if ((tail_cycle
+ 1) < cycle
) {
923 } else if (tail_cycle
< cycle
) {
924 ASSERT(tail_cycle
== (cycle
- 1));
925 free_bytes
= tail_bytes
- bytes
;
928 * The reservation head is behind the tail.
929 * In this case we just want to return the size of the
930 * log as the amount of space left.
932 xfs_fs_cmn_err(CE_ALERT
, log
->l_mp
,
933 "xlog_space_left: head behind tail\n"
934 " tail_cycle = %d, tail_bytes = %d\n"
935 " GH cycle = %d, GH bytes = %d",
936 tail_cycle
, tail_bytes
, cycle
, bytes
);
938 free_bytes
= log
->l_logsize
;
941 } /* xlog_space_left */
945 * Log function which is called when an io completes.
947 * The log manager needs its own routine, in order to control what
948 * happens with the buffer after the write completes.
951 xlog_iodone(xfs_buf_t
*bp
)
953 xlog_in_core_t
*iclog
;
957 iclog
= XFS_BUF_FSPRIVATE(bp
, xlog_in_core_t
*);
958 ASSERT(XFS_BUF_FSPRIVATE2(bp
, unsigned long) == (unsigned long) 2);
959 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)1);
963 * Some versions of cpp barf on the recursive definition of
964 * ic_log -> hic_fields.ic_log and expand ic_log twice when
965 * it is passed through two macros. Workaround broken cpp.
970 * If the ordered flag has been removed by a lower
971 * layer, it means the underlyin device no longer supports
972 * barrier I/O. Warn loudly and turn off barriers.
974 if ((l
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
) && !XFS_BUF_ORDERED(bp
)) {
975 l
->l_mp
->m_flags
&= ~XFS_MOUNT_BARRIER
;
976 xfs_fs_cmn_err(CE_WARN
, l
->l_mp
,
977 "xlog_iodone: Barriers are no longer supported"
978 " by device. Disabling barriers\n");
979 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp
);
983 * Race to shutdown the filesystem if we see an error.
985 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp
)), l
->l_mp
,
986 XFS_ERRTAG_IODONE_IOERR
, XFS_RANDOM_IODONE_IOERR
)) {
987 xfs_ioerror_alert("xlog_iodone", l
->l_mp
, bp
, XFS_BUF_ADDR(bp
));
989 xfs_force_shutdown(l
->l_mp
, SHUTDOWN_LOG_IO_ERROR
);
991 * This flag will be propagated to the trans-committed
992 * callback routines to let them know that the log-commit
995 aborted
= XFS_LI_ABORTED
;
996 } else if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
997 aborted
= XFS_LI_ABORTED
;
1000 /* log I/O is always issued ASYNC */
1001 ASSERT(XFS_BUF_ISASYNC(bp
));
1002 xlog_state_done_syncing(iclog
, aborted
);
1004 * do not reference the buffer (bp) here as we could race
1005 * with it being freed after writing the unmount record to the
1012 * The bdstrat callback function for log bufs. This gives us a central
1013 * place to trap bufs in case we get hit by a log I/O error and need to
1014 * shutdown. Actually, in practice, even when we didn't get a log error,
1015 * we transition the iclogs to IOERROR state *after* flushing all existing
1016 * iclogs to disk. This is because we don't want anymore new transactions to be
1017 * started or completed afterwards.
1020 xlog_bdstrat_cb(struct xfs_buf
*bp
)
1022 xlog_in_core_t
*iclog
;
1024 iclog
= XFS_BUF_FSPRIVATE(bp
, xlog_in_core_t
*);
1026 if ((iclog
->ic_state
& XLOG_STATE_IOERROR
) == 0) {
1027 /* note for irix bstrat will need struct bdevsw passed
1028 * Fix the following macro if the code ever is merged
1034 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp
);
1035 XFS_BUF_ERROR(bp
, EIO
);
1038 return XFS_ERROR(EIO
);
1044 * Return size of each in-core log record buffer.
1046 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
1048 * If the filesystem blocksize is too large, we may need to choose a
1049 * larger size since the directory code currently logs entire blocks.
1053 xlog_get_iclog_buffer_size(xfs_mount_t
*mp
,
1059 if (mp
->m_logbufs
<= 0)
1060 log
->l_iclog_bufs
= XLOG_MAX_ICLOGS
;
1062 log
->l_iclog_bufs
= mp
->m_logbufs
;
1065 * Buffer size passed in from mount system call.
1067 if (mp
->m_logbsize
> 0) {
1068 size
= log
->l_iclog_size
= mp
->m_logbsize
;
1069 log
->l_iclog_size_log
= 0;
1071 log
->l_iclog_size_log
++;
1075 if (XFS_SB_VERSION_HASLOGV2(&mp
->m_sb
)) {
1076 /* # headers = size / 32K
1077 * one header holds cycles from 32K of data
1080 xhdrs
= mp
->m_logbsize
/ XLOG_HEADER_CYCLE_SIZE
;
1081 if (mp
->m_logbsize
% XLOG_HEADER_CYCLE_SIZE
)
1083 log
->l_iclog_hsize
= xhdrs
<< BBSHIFT
;
1084 log
->l_iclog_heads
= xhdrs
;
1086 ASSERT(mp
->m_logbsize
<= XLOG_BIG_RECORD_BSIZE
);
1087 log
->l_iclog_hsize
= BBSIZE
;
1088 log
->l_iclog_heads
= 1;
1093 /* All machines use 32KB buffers by default. */
1094 log
->l_iclog_size
= XLOG_BIG_RECORD_BSIZE
;
1095 log
->l_iclog_size_log
= XLOG_BIG_RECORD_BSHIFT
;
1097 /* the default log size is 16k or 32k which is one header sector */
1098 log
->l_iclog_hsize
= BBSIZE
;
1099 log
->l_iclog_heads
= 1;
1102 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1103 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1105 if (mp
->m_sb
.sb_blocksize
>= 16*1024) {
1106 log
->l_iclog_size
= XLOG_BIG_RECORD_BSIZE
;
1107 log
->l_iclog_size_log
= XLOG_BIG_RECORD_BSHIFT
;
1108 if (mp
->m_logbufs
<= 0) {
1109 switch (mp
->m_sb
.sb_blocksize
) {
1110 case 16*1024: /* 16 KB */
1111 log
->l_iclog_bufs
= 3;
1113 case 32*1024: /* 32 KB */
1114 log
->l_iclog_bufs
= 4;
1116 case 64*1024: /* 64 KB */
1117 log
->l_iclog_bufs
= 8;
1120 xlog_panic("XFS: Invalid blocksize");
1126 done
: /* are we being asked to make the sizes selected above visible? */
1127 if (mp
->m_logbufs
== 0)
1128 mp
->m_logbufs
= log
->l_iclog_bufs
;
1129 if (mp
->m_logbsize
== 0)
1130 mp
->m_logbsize
= log
->l_iclog_size
;
1131 } /* xlog_get_iclog_buffer_size */
1135 * This routine initializes some of the log structure for a given mount point.
1136 * Its primary purpose is to fill in enough, so recovery can occur. However,
1137 * some other stuff may be filled in too.
1140 xlog_alloc_log(xfs_mount_t
*mp
,
1141 xfs_buftarg_t
*log_target
,
1142 xfs_daddr_t blk_offset
,
1146 xlog_rec_header_t
*head
;
1147 xlog_in_core_t
**iclogp
;
1148 xlog_in_core_t
*iclog
, *prev_iclog
=NULL
;
1153 log
= (xlog_t
*)kmem_zalloc(sizeof(xlog_t
), KM_SLEEP
);
1156 log
->l_targ
= log_target
;
1157 log
->l_logsize
= BBTOB(num_bblks
);
1158 log
->l_logBBstart
= blk_offset
;
1159 log
->l_logBBsize
= num_bblks
;
1160 log
->l_covered_state
= XLOG_STATE_COVER_IDLE
;
1161 log
->l_flags
|= XLOG_ACTIVE_RECOVERY
;
1163 log
->l_prev_block
= -1;
1164 log
->l_tail_lsn
= xlog_assign_lsn(1, 0);
1165 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1166 log
->l_last_sync_lsn
= log
->l_tail_lsn
;
1167 log
->l_curr_cycle
= 1; /* 0 is bad since this is initial value */
1168 log
->l_grant_reserve_cycle
= 1;
1169 log
->l_grant_write_cycle
= 1;
1171 if (XFS_SB_VERSION_HASSECTOR(&mp
->m_sb
)) {
1172 log
->l_sectbb_log
= mp
->m_sb
.sb_logsectlog
- BBSHIFT
;
1173 ASSERT(log
->l_sectbb_log
<= mp
->m_sectbb_log
);
1174 /* for larger sector sizes, must have v2 or external log */
1175 ASSERT(log
->l_sectbb_log
== 0 ||
1176 log
->l_logBBstart
== 0 ||
1177 XFS_SB_VERSION_HASLOGV2(&mp
->m_sb
));
1178 ASSERT(mp
->m_sb
.sb_logsectlog
>= BBSHIFT
);
1180 log
->l_sectbb_mask
= (1 << log
->l_sectbb_log
) - 1;
1182 xlog_get_iclog_buffer_size(mp
, log
);
1184 bp
= xfs_buf_get_empty(log
->l_iclog_size
, mp
->m_logdev_targp
);
1185 XFS_BUF_SET_IODONE_FUNC(bp
, xlog_iodone
);
1186 XFS_BUF_SET_BDSTRAT_FUNC(bp
, xlog_bdstrat_cb
);
1187 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)1);
1188 ASSERT(XFS_BUF_ISBUSY(bp
));
1189 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
1192 spin_lock_init(&log
->l_icloglock
);
1193 spin_lock_init(&log
->l_grant_lock
);
1194 initnsema(&log
->l_flushsema
, 0, "ic-flush");
1195 xlog_state_ticket_alloc(log
); /* wait until after icloglock inited */
1197 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1198 ASSERT((XFS_BUF_SIZE(bp
) & BBMASK
) == 0);
1200 iclogp
= &log
->l_iclog
;
1202 * The amount of memory to allocate for the iclog structure is
1203 * rather funky due to the way the structure is defined. It is
1204 * done this way so that we can use different sizes for machines
1205 * with different amounts of memory. See the definition of
1206 * xlog_in_core_t in xfs_log_priv.h for details.
1208 iclogsize
= log
->l_iclog_size
;
1209 ASSERT(log
->l_iclog_size
>= 4096);
1210 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
1211 *iclogp
= (xlog_in_core_t
*)
1212 kmem_zalloc(sizeof(xlog_in_core_t
), KM_SLEEP
);
1214 iclog
->ic_prev
= prev_iclog
;
1217 bp
= xfs_buf_get_noaddr(log
->l_iclog_size
, mp
->m_logdev_targp
);
1218 if (!XFS_BUF_CPSEMA(bp
))
1220 XFS_BUF_SET_IODONE_FUNC(bp
, xlog_iodone
);
1221 XFS_BUF_SET_BDSTRAT_FUNC(bp
, xlog_bdstrat_cb
);
1222 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)1);
1224 iclog
->hic_data
= bp
->b_addr
;
1226 log
->l_iclog_bak
[i
] = (xfs_caddr_t
)&(iclog
->ic_header
);
1228 head
= &iclog
->ic_header
;
1229 memset(head
, 0, sizeof(xlog_rec_header_t
));
1230 INT_SET(head
->h_magicno
, ARCH_CONVERT
, XLOG_HEADER_MAGIC_NUM
);
1231 INT_SET(head
->h_version
, ARCH_CONVERT
,
1232 XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
) ? 2 : 1);
1233 INT_SET(head
->h_size
, ARCH_CONVERT
, log
->l_iclog_size
);
1235 INT_SET(head
->h_fmt
, ARCH_CONVERT
, XLOG_FMT
);
1236 memcpy(&head
->h_fs_uuid
, &mp
->m_sb
.sb_uuid
, sizeof(uuid_t
));
1239 iclog
->ic_size
= XFS_BUF_SIZE(bp
) - log
->l_iclog_hsize
;
1240 iclog
->ic_state
= XLOG_STATE_ACTIVE
;
1241 iclog
->ic_log
= log
;
1242 iclog
->ic_callback_tail
= &(iclog
->ic_callback
);
1243 iclog
->ic_datap
= (char *)iclog
->hic_data
+ log
->l_iclog_hsize
;
1245 ASSERT(XFS_BUF_ISBUSY(iclog
->ic_bp
));
1246 ASSERT(XFS_BUF_VALUSEMA(iclog
->ic_bp
) <= 0);
1247 sv_init(&iclog
->ic_forcesema
, SV_DEFAULT
, "iclog-force");
1248 sv_init(&iclog
->ic_writesema
, SV_DEFAULT
, "iclog-write");
1250 iclogp
= &iclog
->ic_next
;
1252 *iclogp
= log
->l_iclog
; /* complete ring */
1253 log
->l_iclog
->ic_prev
= prev_iclog
; /* re-write 1st prev ptr */
1256 } /* xlog_alloc_log */
1260 * Write out the commit record of a transaction associated with the given
1261 * ticket. Return the lsn of the commit record.
1264 xlog_commit_record(xfs_mount_t
*mp
,
1265 xlog_ticket_t
*ticket
,
1266 xlog_in_core_t
**iclog
,
1267 xfs_lsn_t
*commitlsnp
)
1270 xfs_log_iovec_t reg
[1];
1272 reg
[0].i_addr
= NULL
;
1274 XLOG_VEC_SET_TYPE(®
[0], XLOG_REG_TYPE_COMMIT
);
1276 ASSERT_ALWAYS(iclog
);
1277 if ((error
= xlog_write(mp
, reg
, 1, ticket
, commitlsnp
,
1278 iclog
, XLOG_COMMIT_TRANS
))) {
1279 xfs_force_shutdown(mp
, SHUTDOWN_LOG_IO_ERROR
);
1282 } /* xlog_commit_record */
1286 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1287 * log space. This code pushes on the lsn which would supposedly free up
1288 * the 25% which we want to leave free. We may need to adopt a policy which
1289 * pushes on an lsn which is further along in the log once we reach the high
1290 * water mark. In this manner, we would be creating a low water mark.
1293 xlog_grant_push_ail(xfs_mount_t
*mp
,
1296 xlog_t
*log
= mp
->m_log
; /* pointer to the log */
1297 xfs_lsn_t tail_lsn
; /* lsn of the log tail */
1298 xfs_lsn_t threshold_lsn
= 0; /* lsn we'd like to be at */
1299 int free_blocks
; /* free blocks left to write to */
1300 int free_bytes
; /* free bytes left to write to */
1301 int threshold_block
; /* block in lsn we'd like to be at */
1302 int threshold_cycle
; /* lsn cycle we'd like to be at */
1305 ASSERT(BTOBB(need_bytes
) < log
->l_logBBsize
);
1307 spin_lock(&log
->l_grant_lock
);
1308 free_bytes
= xlog_space_left(log
,
1309 log
->l_grant_reserve_cycle
,
1310 log
->l_grant_reserve_bytes
);
1311 tail_lsn
= log
->l_tail_lsn
;
1312 free_blocks
= BTOBBT(free_bytes
);
1315 * Set the threshold for the minimum number of free blocks in the
1316 * log to the maximum of what the caller needs, one quarter of the
1317 * log, and 256 blocks.
1319 free_threshold
= BTOBB(need_bytes
);
1320 free_threshold
= MAX(free_threshold
, (log
->l_logBBsize
>> 2));
1321 free_threshold
= MAX(free_threshold
, 256);
1322 if (free_blocks
< free_threshold
) {
1323 threshold_block
= BLOCK_LSN(tail_lsn
) + free_threshold
;
1324 threshold_cycle
= CYCLE_LSN(tail_lsn
);
1325 if (threshold_block
>= log
->l_logBBsize
) {
1326 threshold_block
-= log
->l_logBBsize
;
1327 threshold_cycle
+= 1;
1329 threshold_lsn
= xlog_assign_lsn(threshold_cycle
, threshold_block
);
1331 /* Don't pass in an lsn greater than the lsn of the last
1332 * log record known to be on disk.
1334 if (XFS_LSN_CMP(threshold_lsn
, log
->l_last_sync_lsn
) > 0)
1335 threshold_lsn
= log
->l_last_sync_lsn
;
1337 spin_unlock(&log
->l_grant_lock
);
1340 * Get the transaction layer to kick the dirty buffers out to
1341 * disk asynchronously. No point in trying to do this if
1342 * the filesystem is shutting down.
1344 if (threshold_lsn
&&
1345 !XLOG_FORCED_SHUTDOWN(log
))
1346 xfs_trans_push_ail(mp
, threshold_lsn
);
1347 } /* xlog_grant_push_ail */
1351 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1352 * fashion. Previously, we should have moved the current iclog
1353 * ptr in the log to point to the next available iclog. This allows further
1354 * write to continue while this code syncs out an iclog ready to go.
1355 * Before an in-core log can be written out, the data section must be scanned
1356 * to save away the 1st word of each BBSIZE block into the header. We replace
1357 * it with the current cycle count. Each BBSIZE block is tagged with the
1358 * cycle count because there in an implicit assumption that drives will
1359 * guarantee that entire 512 byte blocks get written at once. In other words,
1360 * we can't have part of a 512 byte block written and part not written. By
1361 * tagging each block, we will know which blocks are valid when recovering
1362 * after an unclean shutdown.
1364 * This routine is single threaded on the iclog. No other thread can be in
1365 * this routine with the same iclog. Changing contents of iclog can there-
1366 * fore be done without grabbing the state machine lock. Updating the global
1367 * log will require grabbing the lock though.
1369 * The entire log manager uses a logical block numbering scheme. Only
1370 * log_sync (and then only bwrite()) know about the fact that the log may
1371 * not start with block zero on a given device. The log block start offset
1372 * is added immediately before calling bwrite().
1376 xlog_sync(xlog_t
*log
,
1377 xlog_in_core_t
*iclog
)
1379 xfs_caddr_t dptr
; /* pointer to byte sized element */
1382 uint count
; /* byte count of bwrite */
1383 uint count_init
; /* initial count before roundup */
1384 int roundoff
; /* roundoff to BB or stripe */
1385 int split
= 0; /* split write into two regions */
1387 int v2
= XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
);
1389 XFS_STATS_INC(xs_log_writes
);
1390 ASSERT(iclog
->ic_refcnt
== 0);
1392 /* Add for LR header */
1393 count_init
= log
->l_iclog_hsize
+ iclog
->ic_offset
;
1395 /* Round out the log write size */
1396 if (v2
&& log
->l_mp
->m_sb
.sb_logsunit
> 1) {
1397 /* we have a v2 stripe unit to use */
1398 count
= XLOG_LSUNITTOB(log
, XLOG_BTOLSUNIT(log
, count_init
));
1400 count
= BBTOB(BTOBB(count_init
));
1402 roundoff
= count
- count_init
;
1403 ASSERT(roundoff
>= 0);
1404 ASSERT((v2
&& log
->l_mp
->m_sb
.sb_logsunit
> 1 &&
1405 roundoff
< log
->l_mp
->m_sb
.sb_logsunit
)
1407 (log
->l_mp
->m_sb
.sb_logsunit
<= 1 &&
1408 roundoff
< BBTOB(1)));
1410 /* move grant heads by roundoff in sync */
1411 spin_lock(&log
->l_grant_lock
);
1412 xlog_grant_add_space(log
, roundoff
);
1413 spin_unlock(&log
->l_grant_lock
);
1415 /* put cycle number in every block */
1416 xlog_pack_data(log
, iclog
, roundoff
);
1418 /* real byte length */
1420 INT_SET(iclog
->ic_header
.h_len
,
1422 iclog
->ic_offset
+ roundoff
);
1424 INT_SET(iclog
->ic_header
.h_len
, ARCH_CONVERT
, iclog
->ic_offset
);
1427 /* put ops count in correct order */
1428 ops
= iclog
->ic_header
.h_num_logops
;
1429 INT_SET(iclog
->ic_header
.h_num_logops
, ARCH_CONVERT
, ops
);
1432 ASSERT(XFS_BUF_FSPRIVATE2(bp
, unsigned long) == (unsigned long)1);
1433 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)2);
1434 XFS_BUF_SET_ADDR(bp
, BLOCK_LSN(INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
)));
1436 XFS_STATS_ADD(xs_log_blocks
, BTOBB(count
));
1438 /* Do we need to split this write into 2 parts? */
1439 if (XFS_BUF_ADDR(bp
) + BTOBB(count
) > log
->l_logBBsize
) {
1440 split
= count
- (BBTOB(log
->l_logBBsize
- XFS_BUF_ADDR(bp
)));
1441 count
= BBTOB(log
->l_logBBsize
- XFS_BUF_ADDR(bp
));
1442 iclog
->ic_bwritecnt
= 2; /* split into 2 writes */
1444 iclog
->ic_bwritecnt
= 1;
1446 XFS_BUF_SET_COUNT(bp
, count
);
1447 XFS_BUF_SET_FSPRIVATE(bp
, iclog
); /* save for later */
1448 XFS_BUF_ZEROFLAGS(bp
);
1452 * Do an ordered write for the log block.
1453 * Its unnecessary to flush the first split block in the log wrap case.
1455 if (!split
&& (log
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
))
1456 XFS_BUF_ORDERED(bp
);
1458 ASSERT(XFS_BUF_ADDR(bp
) <= log
->l_logBBsize
-1);
1459 ASSERT(XFS_BUF_ADDR(bp
) + BTOBB(count
) <= log
->l_logBBsize
);
1461 xlog_verify_iclog(log
, iclog
, count
, B_TRUE
);
1463 /* account for log which doesn't start at block #0 */
1464 XFS_BUF_SET_ADDR(bp
, XFS_BUF_ADDR(bp
) + log
->l_logBBstart
);
1466 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1471 if ((error
= XFS_bwrite(bp
))) {
1472 xfs_ioerror_alert("xlog_sync", log
->l_mp
, bp
,
1477 bp
= iclog
->ic_log
->l_xbuf
;
1478 ASSERT(XFS_BUF_FSPRIVATE2(bp
, unsigned long) ==
1480 XFS_BUF_SET_FSPRIVATE2(bp
, (unsigned long)2);
1481 XFS_BUF_SET_ADDR(bp
, 0); /* logical 0 */
1482 XFS_BUF_SET_PTR(bp
, (xfs_caddr_t
)((__psint_t
)&(iclog
->ic_header
)+
1483 (__psint_t
)count
), split
);
1484 XFS_BUF_SET_FSPRIVATE(bp
, iclog
);
1485 XFS_BUF_ZEROFLAGS(bp
);
1488 if (log
->l_mp
->m_flags
& XFS_MOUNT_BARRIER
)
1489 XFS_BUF_ORDERED(bp
);
1490 dptr
= XFS_BUF_PTR(bp
);
1492 * Bump the cycle numbers at the start of each block
1493 * since this part of the buffer is at the start of
1494 * a new cycle. Watch out for the header magic number
1497 for (i
=0; i
<split
; i
+= BBSIZE
) {
1498 INT_MOD(*(uint
*)dptr
, ARCH_CONVERT
, +1);
1499 if (INT_GET(*(uint
*)dptr
, ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
)
1500 INT_MOD(*(uint
*)dptr
, ARCH_CONVERT
, +1);
1504 ASSERT(XFS_BUF_ADDR(bp
) <= log
->l_logBBsize
-1);
1505 ASSERT(XFS_BUF_ADDR(bp
) + BTOBB(count
) <= log
->l_logBBsize
);
1507 /* account for internal log which doesn't start at block #0 */
1508 XFS_BUF_SET_ADDR(bp
, XFS_BUF_ADDR(bp
) + log
->l_logBBstart
);
1510 if ((error
= XFS_bwrite(bp
))) {
1511 xfs_ioerror_alert("xlog_sync (split)", log
->l_mp
,
1512 bp
, XFS_BUF_ADDR(bp
));
1521 * Deallocate a log structure
1524 xlog_dealloc_log(xlog_t
*log
)
1526 xlog_in_core_t
*iclog
, *next_iclog
;
1527 xlog_ticket_t
*tic
, *next_tic
;
1530 iclog
= log
->l_iclog
;
1531 for (i
=0; i
<log
->l_iclog_bufs
; i
++) {
1532 sv_destroy(&iclog
->ic_forcesema
);
1533 sv_destroy(&iclog
->ic_writesema
);
1534 xfs_buf_free(iclog
->ic_bp
);
1535 #ifdef XFS_LOG_TRACE
1536 if (iclog
->ic_trace
!= NULL
) {
1537 ktrace_free(iclog
->ic_trace
);
1540 next_iclog
= iclog
->ic_next
;
1541 kmem_free(iclog
, sizeof(xlog_in_core_t
));
1544 freesema(&log
->l_flushsema
);
1545 spinlock_destroy(&log
->l_icloglock
);
1546 spinlock_destroy(&log
->l_grant_lock
);
1548 /* XXXsup take a look at this again. */
1549 if ((log
->l_ticket_cnt
!= log
->l_ticket_tcnt
) &&
1550 !XLOG_FORCED_SHUTDOWN(log
)) {
1551 xfs_fs_cmn_err(CE_WARN
, log
->l_mp
,
1552 "xlog_dealloc_log: (cnt: %d, total: %d)",
1553 log
->l_ticket_cnt
, log
->l_ticket_tcnt
);
1554 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1557 tic
= log
->l_unmount_free
;
1559 next_tic
= tic
->t_next
;
1560 kmem_free(tic
, NBPP
);
1564 xfs_buf_free(log
->l_xbuf
);
1565 #ifdef XFS_LOG_TRACE
1566 if (log
->l_trace
!= NULL
) {
1567 ktrace_free(log
->l_trace
);
1569 if (log
->l_grant_trace
!= NULL
) {
1570 ktrace_free(log
->l_grant_trace
);
1573 log
->l_mp
->m_log
= NULL
;
1574 kmem_free(log
, sizeof(xlog_t
));
1575 } /* xlog_dealloc_log */
1578 * Update counters atomically now that memcpy is done.
1582 xlog_state_finish_copy(xlog_t
*log
,
1583 xlog_in_core_t
*iclog
,
1587 spin_lock(&log
->l_icloglock
);
1589 iclog
->ic_header
.h_num_logops
+= record_cnt
;
1590 iclog
->ic_offset
+= copy_bytes
;
1592 spin_unlock(&log
->l_icloglock
);
1593 } /* xlog_state_finish_copy */
1599 * print out info relating to regions written which consume
1603 xlog_print_tic_res(xfs_mount_t
*mp
, xlog_ticket_t
*ticket
)
1606 uint ophdr_spc
= ticket
->t_res_num_ophdrs
* (uint
)sizeof(xlog_op_header_t
);
1608 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1609 static char *res_type_str
[XLOG_REG_TYPE_MAX
] = {
1630 static char *trans_type_str
[XFS_TRANS_TYPE_MAX
] = {
1673 xfs_fs_cmn_err(CE_WARN
, mp
,
1674 "xfs_log_write: reservation summary:\n"
1675 " trans type = %s (%u)\n"
1676 " unit res = %d bytes\n"
1677 " current res = %d bytes\n"
1678 " total reg = %u bytes (o/flow = %u bytes)\n"
1679 " ophdrs = %u (ophdr space = %u bytes)\n"
1680 " ophdr + reg = %u bytes\n"
1681 " num regions = %u\n",
1682 ((ticket
->t_trans_type
<= 0 ||
1683 ticket
->t_trans_type
> XFS_TRANS_TYPE_MAX
) ?
1684 "bad-trans-type" : trans_type_str
[ticket
->t_trans_type
-1]),
1685 ticket
->t_trans_type
,
1688 ticket
->t_res_arr_sum
, ticket
->t_res_o_flow
,
1689 ticket
->t_res_num_ophdrs
, ophdr_spc
,
1690 ticket
->t_res_arr_sum
+
1691 ticket
->t_res_o_flow
+ ophdr_spc
,
1694 for (i
= 0; i
< ticket
->t_res_num
; i
++) {
1695 uint r_type
= ticket
->t_res_arr
[i
].r_type
;
1697 "region[%u]: %s - %u bytes\n",
1699 ((r_type
<= 0 || r_type
> XLOG_REG_TYPE_MAX
) ?
1700 "bad-rtype" : res_type_str
[r_type
-1]),
1701 ticket
->t_res_arr
[i
].r_len
);
1706 * Write some region out to in-core log
1708 * This will be called when writing externally provided regions or when
1709 * writing out a commit record for a given transaction.
1711 * General algorithm:
1712 * 1. Find total length of this write. This may include adding to the
1713 * lengths passed in.
1714 * 2. Check whether we violate the tickets reservation.
1715 * 3. While writing to this iclog
1716 * A. Reserve as much space in this iclog as can get
1717 * B. If this is first write, save away start lsn
1718 * C. While writing this region:
1719 * 1. If first write of transaction, write start record
1720 * 2. Write log operation header (header per region)
1721 * 3. Find out if we can fit entire region into this iclog
1722 * 4. Potentially, verify destination memcpy ptr
1723 * 5. Memcpy (partial) region
1724 * 6. If partial copy, release iclog; otherwise, continue
1725 * copying more regions into current iclog
1726 * 4. Mark want sync bit (in simulation mode)
1727 * 5. Release iclog for potential flush to on-disk log.
1730 * 1. Panic if reservation is overrun. This should never happen since
1731 * reservation amounts are generated internal to the filesystem.
1733 * 1. Tickets are single threaded data structures.
1734 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1735 * syncing routine. When a single log_write region needs to span
1736 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1737 * on all log operation writes which don't contain the end of the
1738 * region. The XLOG_END_TRANS bit is used for the in-core log
1739 * operation which contains the end of the continued log_write region.
1740 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1741 * we don't really know exactly how much space will be used. As a result,
1742 * we don't update ic_offset until the end when we know exactly how many
1743 * bytes have been written out.
1746 xlog_write(xfs_mount_t
* mp
,
1747 xfs_log_iovec_t reg
[],
1749 xfs_log_ticket_t tic
,
1750 xfs_lsn_t
*start_lsn
,
1751 xlog_in_core_t
**commit_iclog
,
1754 xlog_t
*log
= mp
->m_log
;
1755 xlog_ticket_t
*ticket
= (xlog_ticket_t
*)tic
;
1756 xlog_in_core_t
*iclog
= NULL
; /* ptr to current in-core log */
1757 xlog_op_header_t
*logop_head
; /* ptr to log operation header */
1758 __psint_t ptr
; /* copy address into data region */
1759 int len
; /* # xlog_write() bytes 2 still copy */
1760 int index
; /* region index currently copying */
1761 int log_offset
; /* offset (from 0) into data region */
1762 int start_rec_copy
; /* # bytes to copy for start record */
1763 int partial_copy
; /* did we split a region? */
1764 int partial_copy_len
;/* # bytes copied if split region */
1765 int need_copy
; /* # bytes need to memcpy this region */
1766 int copy_len
; /* # bytes actually memcpy'ing */
1767 int copy_off
; /* # bytes from entry start */
1768 int contwr
; /* continued write of in-core log? */
1770 int record_cnt
= 0, data_cnt
= 0;
1772 partial_copy_len
= partial_copy
= 0;
1774 /* Calculate potential maximum space. Each region gets its own
1775 * xlog_op_header_t and may need to be double word aligned.
1778 if (ticket
->t_flags
& XLOG_TIC_INITED
) { /* acct for start rec of xact */
1779 len
+= sizeof(xlog_op_header_t
);
1780 ticket
->t_res_num_ophdrs
++;
1783 for (index
= 0; index
< nentries
; index
++) {
1784 len
+= sizeof(xlog_op_header_t
); /* each region gets >= 1 */
1785 ticket
->t_res_num_ophdrs
++;
1786 len
+= reg
[index
].i_len
;
1787 xlog_tic_add_region(ticket
, reg
[index
].i_len
, reg
[index
].i_type
);
1789 contwr
= *start_lsn
= 0;
1791 if (ticket
->t_curr_res
< len
) {
1792 xlog_print_tic_res(mp
, ticket
);
1795 "xfs_log_write: reservation ran out. Need to up reservation");
1797 /* Customer configurable panic */
1798 xfs_cmn_err(XFS_PTAG_LOGRES
, CE_ALERT
, mp
,
1799 "xfs_log_write: reservation ran out. Need to up reservation");
1800 /* If we did not panic, shutdown the filesystem */
1801 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
1804 ticket
->t_curr_res
-= len
;
1806 for (index
= 0; index
< nentries
; ) {
1807 if ((error
= xlog_state_get_iclog_space(log
, len
, &iclog
, ticket
,
1808 &contwr
, &log_offset
)))
1811 ASSERT(log_offset
<= iclog
->ic_size
- 1);
1812 ptr
= (__psint_t
) ((char *)iclog
->ic_datap
+log_offset
);
1814 /* start_lsn is the first lsn written to. That's all we need. */
1816 *start_lsn
= INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
);
1818 /* This loop writes out as many regions as can fit in the amount
1819 * of space which was allocated by xlog_state_get_iclog_space().
1821 while (index
< nentries
) {
1822 ASSERT(reg
[index
].i_len
% sizeof(__int32_t
) == 0);
1823 ASSERT((__psint_t
)ptr
% sizeof(__int32_t
) == 0);
1826 /* If first write for transaction, insert start record.
1827 * We can't be trying to commit if we are inited. We can't
1828 * have any "partial_copy" if we are inited.
1830 if (ticket
->t_flags
& XLOG_TIC_INITED
) {
1831 logop_head
= (xlog_op_header_t
*)ptr
;
1832 logop_head
->oh_tid
= cpu_to_be32(ticket
->t_tid
);
1833 logop_head
->oh_clientid
= ticket
->t_clientid
;
1834 logop_head
->oh_len
= 0;
1835 logop_head
->oh_flags
= XLOG_START_TRANS
;
1836 logop_head
->oh_res2
= 0;
1837 ticket
->t_flags
&= ~XLOG_TIC_INITED
; /* clear bit */
1840 start_rec_copy
= sizeof(xlog_op_header_t
);
1841 xlog_write_adv_cnt(ptr
, len
, log_offset
, start_rec_copy
);
1844 /* Copy log operation header directly into data section */
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_res2
= 0;
1850 /* header copied directly */
1851 xlog_write_adv_cnt(ptr
, len
, log_offset
, sizeof(xlog_op_header_t
));
1853 /* are we copying a commit or unmount record? */
1854 logop_head
->oh_flags
= flags
;
1857 * We've seen logs corrupted with bad transaction client
1858 * ids. This makes sure that XFS doesn't generate them on.
1859 * Turn this into an EIO and shut down the filesystem.
1861 switch (logop_head
->oh_clientid
) {
1862 case XFS_TRANSACTION
:
1867 xfs_fs_cmn_err(CE_WARN
, mp
,
1868 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1869 logop_head
->oh_clientid
, tic
);
1870 return XFS_ERROR(EIO
);
1873 /* Partial write last time? => (partial_copy != 0)
1874 * need_copy is the amount we'd like to copy if everything could
1875 * fit in the current memcpy.
1877 need_copy
= reg
[index
].i_len
- partial_copy_len
;
1879 copy_off
= partial_copy_len
;
1880 if (need_copy
<= iclog
->ic_size
- log_offset
) { /*complete write */
1881 copy_len
= need_copy
;
1882 logop_head
->oh_len
= cpu_to_be32(copy_len
);
1884 logop_head
->oh_flags
|= (XLOG_END_TRANS
|XLOG_WAS_CONT_TRANS
);
1885 partial_copy_len
= partial_copy
= 0;
1886 } else { /* partial write */
1887 copy_len
= iclog
->ic_size
- log_offset
;
1888 logop_head
->oh_len
= cpu_to_be32(copy_len
);
1889 logop_head
->oh_flags
|= XLOG_CONTINUE_TRANS
;
1891 logop_head
->oh_flags
|= XLOG_WAS_CONT_TRANS
;
1892 partial_copy_len
+= copy_len
;
1894 len
+= sizeof(xlog_op_header_t
); /* from splitting of region */
1895 /* account for new log op header */
1896 ticket
->t_curr_res
-= sizeof(xlog_op_header_t
);
1897 ticket
->t_res_num_ophdrs
++;
1899 xlog_verify_dest_ptr(log
, ptr
);
1902 ASSERT(copy_len
>= 0);
1903 memcpy((xfs_caddr_t
)ptr
, reg
[index
].i_addr
+ copy_off
, copy_len
);
1904 xlog_write_adv_cnt(ptr
, len
, log_offset
, copy_len
);
1906 /* make copy_len total bytes copied, including headers */
1907 copy_len
+= start_rec_copy
+ sizeof(xlog_op_header_t
);
1909 data_cnt
+= contwr
? copy_len
: 0;
1910 if (partial_copy
) { /* copied partial region */
1911 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1912 xlog_state_finish_copy(log
, iclog
, record_cnt
, data_cnt
);
1913 record_cnt
= data_cnt
= 0;
1914 if ((error
= xlog_state_release_iclog(log
, iclog
)))
1916 break; /* don't increment index */
1917 } else { /* copied entire region */
1919 partial_copy_len
= partial_copy
= 0;
1921 if (iclog
->ic_size
- log_offset
<= sizeof(xlog_op_header_t
)) {
1922 xlog_state_finish_copy(log
, iclog
, record_cnt
, data_cnt
);
1923 record_cnt
= data_cnt
= 0;
1924 xlog_state_want_sync(log
, iclog
);
1926 ASSERT(flags
& XLOG_COMMIT_TRANS
);
1927 *commit_iclog
= iclog
;
1928 } else if ((error
= xlog_state_release_iclog(log
, iclog
)))
1930 if (index
== nentries
)
1931 return 0; /* we are done */
1935 } /* if (partial_copy) */
1936 } /* while (index < nentries) */
1937 } /* for (index = 0; index < nentries; ) */
1940 xlog_state_finish_copy(log
, iclog
, record_cnt
, data_cnt
);
1942 ASSERT(flags
& XLOG_COMMIT_TRANS
);
1943 *commit_iclog
= iclog
;
1946 return xlog_state_release_iclog(log
, iclog
);
1950 /*****************************************************************************
1952 * State Machine functions
1954 *****************************************************************************
1957 /* Clean iclogs starting from the head. This ordering must be
1958 * maintained, so an iclog doesn't become ACTIVE beyond one that
1959 * is SYNCING. This is also required to maintain the notion that we use
1960 * a counting semaphore to hold off would be writers to the log when every
1961 * iclog is trying to sync to disk.
1963 * State Change: DIRTY -> ACTIVE
1966 xlog_state_clean_log(xlog_t
*log
)
1968 xlog_in_core_t
*iclog
;
1971 iclog
= log
->l_iclog
;
1973 if (iclog
->ic_state
== XLOG_STATE_DIRTY
) {
1974 iclog
->ic_state
= XLOG_STATE_ACTIVE
;
1975 iclog
->ic_offset
= 0;
1976 iclog
->ic_callback
= NULL
; /* don't need to free */
1978 * If the number of ops in this iclog indicate it just
1979 * contains the dummy transaction, we can
1980 * change state into IDLE (the second time around).
1981 * Otherwise we should change the state into
1983 * We don't need to cover the dummy.
1986 (INT_GET(iclog
->ic_header
.h_num_logops
, ARCH_CONVERT
) == XLOG_COVER_OPS
)) {
1990 * We have two dirty iclogs so start over
1991 * This could also be num of ops indicates
1992 * this is not the dummy going out.
1996 iclog
->ic_header
.h_num_logops
= 0;
1997 memset(iclog
->ic_header
.h_cycle_data
, 0,
1998 sizeof(iclog
->ic_header
.h_cycle_data
));
1999 iclog
->ic_header
.h_lsn
= 0;
2000 } else if (iclog
->ic_state
== XLOG_STATE_ACTIVE
)
2003 break; /* stop cleaning */
2004 iclog
= iclog
->ic_next
;
2005 } while (iclog
!= log
->l_iclog
);
2007 /* log is locked when we are called */
2009 * Change state for the dummy log recording.
2010 * We usually go to NEED. But we go to NEED2 if the changed indicates
2011 * we are done writing the dummy record.
2012 * If we are done with the second dummy recored (DONE2), then
2016 switch (log
->l_covered_state
) {
2017 case XLOG_STATE_COVER_IDLE
:
2018 case XLOG_STATE_COVER_NEED
:
2019 case XLOG_STATE_COVER_NEED2
:
2020 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2023 case XLOG_STATE_COVER_DONE
:
2025 log
->l_covered_state
= XLOG_STATE_COVER_NEED2
;
2027 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2030 case XLOG_STATE_COVER_DONE2
:
2032 log
->l_covered_state
= XLOG_STATE_COVER_IDLE
;
2034 log
->l_covered_state
= XLOG_STATE_COVER_NEED
;
2041 } /* xlog_state_clean_log */
2044 xlog_get_lowest_lsn(
2047 xlog_in_core_t
*lsn_log
;
2048 xfs_lsn_t lowest_lsn
, lsn
;
2050 lsn_log
= log
->l_iclog
;
2053 if (!(lsn_log
->ic_state
& (XLOG_STATE_ACTIVE
|XLOG_STATE_DIRTY
))) {
2054 lsn
= INT_GET(lsn_log
->ic_header
.h_lsn
, ARCH_CONVERT
);
2055 if ((lsn
&& !lowest_lsn
) ||
2056 (XFS_LSN_CMP(lsn
, lowest_lsn
) < 0)) {
2060 lsn_log
= lsn_log
->ic_next
;
2061 } while (lsn_log
!= log
->l_iclog
);
2067 xlog_state_do_callback(
2070 xlog_in_core_t
*ciclog
)
2072 xlog_in_core_t
*iclog
;
2073 xlog_in_core_t
*first_iclog
; /* used to know when we've
2074 * processed all iclogs once */
2075 xfs_log_callback_t
*cb
, *cb_next
;
2077 xfs_lsn_t lowest_lsn
;
2078 int ioerrors
; /* counter: iclogs with errors */
2079 int loopdidcallbacks
; /* flag: inner loop did callbacks*/
2080 int funcdidcallbacks
; /* flag: function did callbacks */
2081 int repeats
; /* for issuing console warnings if
2082 * looping too many times */
2084 spin_lock(&log
->l_icloglock
);
2085 first_iclog
= iclog
= log
->l_iclog
;
2087 funcdidcallbacks
= 0;
2092 * Scan all iclogs starting with the one pointed to by the
2093 * log. Reset this starting point each time the log is
2094 * unlocked (during callbacks).
2096 * Keep looping through iclogs until one full pass is made
2097 * without running any callbacks.
2099 first_iclog
= log
->l_iclog
;
2100 iclog
= log
->l_iclog
;
2101 loopdidcallbacks
= 0;
2106 /* skip all iclogs in the ACTIVE & DIRTY states */
2107 if (iclog
->ic_state
&
2108 (XLOG_STATE_ACTIVE
|XLOG_STATE_DIRTY
)) {
2109 iclog
= iclog
->ic_next
;
2114 * Between marking a filesystem SHUTDOWN and stopping
2115 * the log, we do flush all iclogs to disk (if there
2116 * wasn't a log I/O error). So, we do want things to
2117 * go smoothly in case of just a SHUTDOWN w/o a
2120 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
2122 * Can only perform callbacks in order. Since
2123 * this iclog is not in the DONE_SYNC/
2124 * DO_CALLBACK state, we skip the rest and
2125 * just try to clean up. If we set our iclog
2126 * to DO_CALLBACK, we will not process it when
2127 * we retry since a previous iclog is in the
2128 * CALLBACK and the state cannot change since
2129 * we are holding the l_icloglock.
2131 if (!(iclog
->ic_state
&
2132 (XLOG_STATE_DONE_SYNC
|
2133 XLOG_STATE_DO_CALLBACK
))) {
2134 if (ciclog
&& (ciclog
->ic_state
==
2135 XLOG_STATE_DONE_SYNC
)) {
2136 ciclog
->ic_state
= XLOG_STATE_DO_CALLBACK
;
2141 * We now have an iclog that is in either the
2142 * DO_CALLBACK or DONE_SYNC states. The other
2143 * states (WANT_SYNC, SYNCING, or CALLBACK were
2144 * caught by the above if and are going to
2145 * clean (i.e. we aren't doing their callbacks)
2150 * We will do one more check here to see if we
2151 * have chased our tail around.
2154 lowest_lsn
= xlog_get_lowest_lsn(log
);
2158 INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
)
2160 iclog
= iclog
->ic_next
;
2161 continue; /* Leave this iclog for
2165 iclog
->ic_state
= XLOG_STATE_CALLBACK
;
2167 spin_unlock(&log
->l_icloglock
);
2169 /* l_last_sync_lsn field protected by
2170 * l_grant_lock. Don't worry about iclog's lsn.
2171 * No one else can be here except us.
2173 spin_lock(&log
->l_grant_lock
);
2175 log
->l_last_sync_lsn
,
2176 INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
)
2178 log
->l_last_sync_lsn
= INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
);
2179 spin_unlock(&log
->l_grant_lock
);
2182 * Keep processing entries in the callback list
2183 * until we come around and it is empty. We
2184 * need to atomically see that the list is
2185 * empty and change the state to DIRTY so that
2186 * we don't miss any more callbacks being added.
2188 spin_lock(&log
->l_icloglock
);
2192 cb
= iclog
->ic_callback
;
2195 iclog
->ic_callback_tail
= &(iclog
->ic_callback
);
2196 iclog
->ic_callback
= NULL
;
2197 spin_unlock(&log
->l_icloglock
);
2199 /* perform callbacks in the order given */
2200 for (; cb
; cb
= cb_next
) {
2201 cb_next
= cb
->cb_next
;
2202 cb
->cb_func(cb
->cb_arg
, aborted
);
2204 spin_lock(&log
->l_icloglock
);
2205 cb
= iclog
->ic_callback
;
2211 ASSERT(iclog
->ic_callback
== NULL
);
2212 if (!(iclog
->ic_state
& XLOG_STATE_IOERROR
))
2213 iclog
->ic_state
= XLOG_STATE_DIRTY
;
2216 * Transition from DIRTY to ACTIVE if applicable.
2217 * NOP if STATE_IOERROR.
2219 xlog_state_clean_log(log
);
2221 /* wake up threads waiting in xfs_log_force() */
2222 sv_broadcast(&iclog
->ic_forcesema
);
2224 iclog
= iclog
->ic_next
;
2225 } while (first_iclog
!= iclog
);
2227 if (repeats
> 5000) {
2228 flushcnt
+= repeats
;
2230 xfs_fs_cmn_err(CE_WARN
, log
->l_mp
,
2231 "%s: possible infinite loop (%d iterations)",
2232 __FUNCTION__
, flushcnt
);
2234 } while (!ioerrors
&& loopdidcallbacks
);
2237 * make one last gasp attempt to see if iclogs are being left in
2241 if (funcdidcallbacks
) {
2242 first_iclog
= iclog
= log
->l_iclog
;
2244 ASSERT(iclog
->ic_state
!= XLOG_STATE_DO_CALLBACK
);
2246 * Terminate the loop if iclogs are found in states
2247 * which will cause other threads to clean up iclogs.
2249 * SYNCING - i/o completion will go through logs
2250 * DONE_SYNC - interrupt thread should be waiting for
2252 * IOERROR - give up hope all ye who enter here
2254 if (iclog
->ic_state
== XLOG_STATE_WANT_SYNC
||
2255 iclog
->ic_state
== XLOG_STATE_SYNCING
||
2256 iclog
->ic_state
== XLOG_STATE_DONE_SYNC
||
2257 iclog
->ic_state
== XLOG_STATE_IOERROR
)
2259 iclog
= iclog
->ic_next
;
2260 } while (first_iclog
!= iclog
);
2265 if (log
->l_iclog
->ic_state
& (XLOG_STATE_ACTIVE
|XLOG_STATE_IOERROR
)) {
2266 flushcnt
= log
->l_flushcnt
;
2267 log
->l_flushcnt
= 0;
2269 spin_unlock(&log
->l_icloglock
);
2271 vsema(&log
->l_flushsema
);
2272 } /* xlog_state_do_callback */
2276 * Finish transitioning this iclog to the dirty state.
2278 * Make sure that we completely execute this routine only when this is
2279 * the last call to the iclog. There is a good chance that iclog flushes,
2280 * when we reach the end of the physical log, get turned into 2 separate
2281 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2282 * routine. By using the reference count bwritecnt, we guarantee that only
2283 * the second completion goes through.
2285 * Callbacks could take time, so they are done outside the scope of the
2286 * global state machine log lock. Assume that the calls to cvsema won't
2287 * take a long time. At least we know it won't sleep.
2290 xlog_state_done_syncing(
2291 xlog_in_core_t
*iclog
,
2294 xlog_t
*log
= iclog
->ic_log
;
2296 spin_lock(&log
->l_icloglock
);
2298 ASSERT(iclog
->ic_state
== XLOG_STATE_SYNCING
||
2299 iclog
->ic_state
== XLOG_STATE_IOERROR
);
2300 ASSERT(iclog
->ic_refcnt
== 0);
2301 ASSERT(iclog
->ic_bwritecnt
== 1 || iclog
->ic_bwritecnt
== 2);
2305 * If we got an error, either on the first buffer, or in the case of
2306 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2307 * and none should ever be attempted to be written to disk
2310 if (iclog
->ic_state
!= XLOG_STATE_IOERROR
) {
2311 if (--iclog
->ic_bwritecnt
== 1) {
2312 spin_unlock(&log
->l_icloglock
);
2315 iclog
->ic_state
= XLOG_STATE_DONE_SYNC
;
2319 * Someone could be sleeping prior to writing out the next
2320 * iclog buffer, we wake them all, one will get to do the
2321 * I/O, the others get to wait for the result.
2323 sv_broadcast(&iclog
->ic_writesema
);
2324 spin_unlock(&log
->l_icloglock
);
2325 xlog_state_do_callback(log
, aborted
, iclog
); /* also cleans log */
2326 } /* xlog_state_done_syncing */
2330 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2331 * sleep. The flush semaphore is set to the number of in-core buffers and
2332 * decremented around disk syncing. Therefore, if all buffers are syncing,
2333 * this semaphore will cause new writes to sleep until a sync completes.
2334 * Otherwise, this code just does p() followed by v(). This approximates
2335 * a sleep/wakeup except we can't race.
2337 * The in-core logs are used in a circular fashion. They are not used
2338 * out-of-order even when an iclog past the head is free.
2341 * * log_offset where xlog_write() can start writing into the in-core
2343 * * in-core log pointer to which xlog_write() should write.
2344 * * boolean indicating this is a continued write to an in-core log.
2345 * If this is the last write, then the in-core log's offset field
2346 * needs to be incremented, depending on the amount of data which
2350 xlog_state_get_iclog_space(xlog_t
*log
,
2352 xlog_in_core_t
**iclogp
,
2353 xlog_ticket_t
*ticket
,
2354 int *continued_write
,
2358 xlog_rec_header_t
*head
;
2359 xlog_in_core_t
*iclog
;
2363 spin_lock(&log
->l_icloglock
);
2364 if (XLOG_FORCED_SHUTDOWN(log
)) {
2365 spin_unlock(&log
->l_icloglock
);
2366 return XFS_ERROR(EIO
);
2369 iclog
= log
->l_iclog
;
2370 if (! (iclog
->ic_state
== XLOG_STATE_ACTIVE
)) {
2372 spin_unlock(&log
->l_icloglock
);
2373 xlog_trace_iclog(iclog
, XLOG_TRACE_SLEEP_FLUSH
);
2374 XFS_STATS_INC(xs_log_noiclogs
);
2375 /* Ensure that log writes happen */
2376 psema(&log
->l_flushsema
, PINOD
);
2379 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
);
2380 head
= &iclog
->ic_header
;
2382 iclog
->ic_refcnt
++; /* prevents sync */
2383 log_offset
= iclog
->ic_offset
;
2385 /* On the 1st write to an iclog, figure out lsn. This works
2386 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2387 * committing to. If the offset is set, that's how many blocks
2390 if (log_offset
== 0) {
2391 ticket
->t_curr_res
-= log
->l_iclog_hsize
;
2392 xlog_tic_add_region(ticket
,
2394 XLOG_REG_TYPE_LRHEADER
);
2395 INT_SET(head
->h_cycle
, ARCH_CONVERT
, log
->l_curr_cycle
);
2396 INT_SET(head
->h_lsn
, ARCH_CONVERT
,
2397 xlog_assign_lsn(log
->l_curr_cycle
, log
->l_curr_block
));
2398 ASSERT(log
->l_curr_block
>= 0);
2401 /* If there is enough room to write everything, then do it. Otherwise,
2402 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2403 * bit is on, so this will get flushed out. Don't update ic_offset
2404 * until you know exactly how many bytes get copied. Therefore, wait
2405 * until later to update ic_offset.
2407 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2408 * can fit into remaining data section.
2410 if (iclog
->ic_size
- iclog
->ic_offset
< 2*sizeof(xlog_op_header_t
)) {
2411 xlog_state_switch_iclogs(log
, iclog
, iclog
->ic_size
);
2413 /* If I'm the only one writing to this iclog, sync it to disk */
2414 if (iclog
->ic_refcnt
== 1) {
2415 spin_unlock(&log
->l_icloglock
);
2416 if ((error
= xlog_state_release_iclog(log
, iclog
)))
2420 spin_unlock(&log
->l_icloglock
);
2425 /* Do we have enough room to write the full amount in the remainder
2426 * of this iclog? Or must we continue a write on the next iclog and
2427 * mark this iclog as completely taken? In the case where we switch
2428 * iclogs (to mark it taken), this particular iclog will release/sync
2429 * to disk in xlog_write().
2431 if (len
<= iclog
->ic_size
- iclog
->ic_offset
) {
2432 *continued_write
= 0;
2433 iclog
->ic_offset
+= len
;
2435 *continued_write
= 1;
2436 xlog_state_switch_iclogs(log
, iclog
, iclog
->ic_size
);
2440 ASSERT(iclog
->ic_offset
<= iclog
->ic_size
);
2441 spin_unlock(&log
->l_icloglock
);
2443 *logoffsetp
= log_offset
;
2445 } /* xlog_state_get_iclog_space */
2448 * Atomically get the log space required for a log ticket.
2450 * Once a ticket gets put onto the reserveq, it will only return after
2451 * the needed reservation is satisfied.
2454 xlog_grant_log_space(xlog_t
*log
,
2465 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
2466 panic("grant Recovery problem");
2469 /* Is there space or do we need to sleep? */
2470 spin_lock(&log
->l_grant_lock
);
2471 xlog_trace_loggrant(log
, tic
, "xlog_grant_log_space: enter");
2473 /* something is already sleeping; insert new transaction at end */
2474 if (log
->l_reserve_headq
) {
2475 xlog_ins_ticketq(&log
->l_reserve_headq
, tic
);
2476 xlog_trace_loggrant(log
, tic
,
2477 "xlog_grant_log_space: sleep 1");
2479 * Gotta check this before going to sleep, while we're
2480 * holding the grant lock.
2482 if (XLOG_FORCED_SHUTDOWN(log
))
2485 XFS_STATS_INC(xs_sleep_logspace
);
2486 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
, &log
->l_grant_lock
, s
);
2488 * If we got an error, and the filesystem is shutting down,
2489 * we'll catch it down below. So just continue...
2491 xlog_trace_loggrant(log
, tic
,
2492 "xlog_grant_log_space: wake 1");
2493 spin_lock(&log
->l_grant_lock
);
2495 if (tic
->t_flags
& XFS_LOG_PERM_RESERV
)
2496 need_bytes
= tic
->t_unit_res
*tic
->t_ocnt
;
2498 need_bytes
= tic
->t_unit_res
;
2501 if (XLOG_FORCED_SHUTDOWN(log
))
2504 free_bytes
= xlog_space_left(log
, log
->l_grant_reserve_cycle
,
2505 log
->l_grant_reserve_bytes
);
2506 if (free_bytes
< need_bytes
) {
2507 if ((tic
->t_flags
& XLOG_TIC_IN_Q
) == 0)
2508 xlog_ins_ticketq(&log
->l_reserve_headq
, tic
);
2509 xlog_trace_loggrant(log
, tic
,
2510 "xlog_grant_log_space: sleep 2");
2511 XFS_STATS_INC(xs_sleep_logspace
);
2512 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
, &log
->l_grant_lock
, s
);
2514 if (XLOG_FORCED_SHUTDOWN(log
)) {
2515 spin_lock(&log
->l_grant_lock
);
2519 xlog_trace_loggrant(log
, tic
,
2520 "xlog_grant_log_space: wake 2");
2521 xlog_grant_push_ail(log
->l_mp
, need_bytes
);
2522 spin_lock(&log
->l_grant_lock
);
2524 } else if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2525 xlog_del_ticketq(&log
->l_reserve_headq
, tic
);
2527 /* we've got enough space */
2528 xlog_grant_add_space(log
, need_bytes
);
2530 tail_lsn
= log
->l_tail_lsn
;
2532 * Check to make sure the grant write head didn't just over lap the
2533 * tail. If the cycles are the same, we can't be overlapping.
2534 * Otherwise, make sure that the cycles differ by exactly one and
2535 * check the byte count.
2537 if (CYCLE_LSN(tail_lsn
) != log
->l_grant_write_cycle
) {
2538 ASSERT(log
->l_grant_write_cycle
-1 == CYCLE_LSN(tail_lsn
));
2539 ASSERT(log
->l_grant_write_bytes
<= BBTOB(BLOCK_LSN(tail_lsn
)));
2542 xlog_trace_loggrant(log
, tic
, "xlog_grant_log_space: exit");
2543 xlog_verify_grant_head(log
, 1);
2544 spin_unlock(&log
->l_grant_lock
);
2548 if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2549 xlog_del_ticketq(&log
->l_reserve_headq
, tic
);
2550 xlog_trace_loggrant(log
, tic
, "xlog_grant_log_space: err_ret");
2552 * If we are failing, make sure the ticket doesn't have any
2553 * current reservations. We don't want to add this back when
2554 * the ticket/transaction gets cancelled.
2556 tic
->t_curr_res
= 0;
2557 tic
->t_cnt
= 0; /* ungrant will give back unit_res * t_cnt. */
2558 spin_unlock(&log
->l_grant_lock
);
2559 return XFS_ERROR(EIO
);
2560 } /* xlog_grant_log_space */
2564 * Replenish the byte reservation required by moving the grant write head.
2569 xlog_regrant_write_log_space(xlog_t
*log
,
2572 int free_bytes
, need_bytes
;
2573 xlog_ticket_t
*ntic
;
2578 tic
->t_curr_res
= tic
->t_unit_res
;
2579 xlog_tic_reset_res(tic
);
2585 if (log
->l_flags
& XLOG_ACTIVE_RECOVERY
)
2586 panic("regrant Recovery problem");
2589 spin_lock(&log
->l_grant_lock
);
2590 xlog_trace_loggrant(log
, tic
, "xlog_regrant_write_log_space: enter");
2592 if (XLOG_FORCED_SHUTDOWN(log
))
2595 /* If there are other waiters on the queue then give them a
2596 * chance at logspace before us. Wake up the first waiters,
2597 * if we do not wake up all the waiters then go to sleep waiting
2598 * for more free space, otherwise try to get some space for
2602 if ((ntic
= log
->l_write_headq
)) {
2603 free_bytes
= xlog_space_left(log
, log
->l_grant_write_cycle
,
2604 log
->l_grant_write_bytes
);
2606 ASSERT(ntic
->t_flags
& XLOG_TIC_PERM_RESERV
);
2608 if (free_bytes
< ntic
->t_unit_res
)
2610 free_bytes
-= ntic
->t_unit_res
;
2611 sv_signal(&ntic
->t_sema
);
2612 ntic
= ntic
->t_next
;
2613 } while (ntic
!= log
->l_write_headq
);
2615 if (ntic
!= log
->l_write_headq
) {
2616 if ((tic
->t_flags
& XLOG_TIC_IN_Q
) == 0)
2617 xlog_ins_ticketq(&log
->l_write_headq
, tic
);
2619 xlog_trace_loggrant(log
, tic
,
2620 "xlog_regrant_write_log_space: sleep 1");
2621 XFS_STATS_INC(xs_sleep_logspace
);
2622 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
,
2623 &log
->l_grant_lock
, s
);
2625 /* If we're shutting down, this tic is already
2627 if (XLOG_FORCED_SHUTDOWN(log
)) {
2628 spin_lock(&log
->l_grant_lock
);
2632 xlog_trace_loggrant(log
, tic
,
2633 "xlog_regrant_write_log_space: wake 1");
2634 xlog_grant_push_ail(log
->l_mp
, tic
->t_unit_res
);
2635 spin_lock(&log
->l_grant_lock
);
2639 need_bytes
= tic
->t_unit_res
;
2642 if (XLOG_FORCED_SHUTDOWN(log
))
2645 free_bytes
= xlog_space_left(log
, log
->l_grant_write_cycle
,
2646 log
->l_grant_write_bytes
);
2647 if (free_bytes
< need_bytes
) {
2648 if ((tic
->t_flags
& XLOG_TIC_IN_Q
) == 0)
2649 xlog_ins_ticketq(&log
->l_write_headq
, tic
);
2650 XFS_STATS_INC(xs_sleep_logspace
);
2651 sv_wait(&tic
->t_sema
, PINOD
|PLTWAIT
, &log
->l_grant_lock
, s
);
2653 /* If we're shutting down, this tic is already off the queue */
2654 if (XLOG_FORCED_SHUTDOWN(log
)) {
2655 spin_lock(&log
->l_grant_lock
);
2659 xlog_trace_loggrant(log
, tic
,
2660 "xlog_regrant_write_log_space: wake 2");
2661 xlog_grant_push_ail(log
->l_mp
, need_bytes
);
2662 spin_lock(&log
->l_grant_lock
);
2664 } else if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2665 xlog_del_ticketq(&log
->l_write_headq
, tic
);
2667 /* we've got enough space */
2668 xlog_grant_add_space_write(log
, need_bytes
);
2670 tail_lsn
= log
->l_tail_lsn
;
2671 if (CYCLE_LSN(tail_lsn
) != log
->l_grant_write_cycle
) {
2672 ASSERT(log
->l_grant_write_cycle
-1 == CYCLE_LSN(tail_lsn
));
2673 ASSERT(log
->l_grant_write_bytes
<= BBTOB(BLOCK_LSN(tail_lsn
)));
2677 xlog_trace_loggrant(log
, tic
, "xlog_regrant_write_log_space: exit");
2678 xlog_verify_grant_head(log
, 1);
2679 spin_unlock(&log
->l_grant_lock
);
2684 if (tic
->t_flags
& XLOG_TIC_IN_Q
)
2685 xlog_del_ticketq(&log
->l_reserve_headq
, tic
);
2686 xlog_trace_loggrant(log
, tic
, "xlog_regrant_write_log_space: err_ret");
2688 * If we are failing, make sure the ticket doesn't have any
2689 * current reservations. We don't want to add this back when
2690 * the ticket/transaction gets cancelled.
2692 tic
->t_curr_res
= 0;
2693 tic
->t_cnt
= 0; /* ungrant will give back unit_res * t_cnt. */
2694 spin_unlock(&log
->l_grant_lock
);
2695 return XFS_ERROR(EIO
);
2696 } /* xlog_regrant_write_log_space */
2699 /* The first cnt-1 times through here we don't need to
2700 * move the grant write head because the permanent
2701 * reservation has reserved cnt times the unit amount.
2702 * Release part of current permanent unit reservation and
2703 * reset current reservation to be one units worth. Also
2704 * move grant reservation head forward.
2707 xlog_regrant_reserve_log_space(xlog_t
*log
,
2708 xlog_ticket_t
*ticket
)
2710 xlog_trace_loggrant(log
, ticket
,
2711 "xlog_regrant_reserve_log_space: enter");
2712 if (ticket
->t_cnt
> 0)
2715 spin_lock(&log
->l_grant_lock
);
2716 xlog_grant_sub_space(log
, ticket
->t_curr_res
);
2717 ticket
->t_curr_res
= ticket
->t_unit_res
;
2718 xlog_tic_reset_res(ticket
);
2719 xlog_trace_loggrant(log
, ticket
,
2720 "xlog_regrant_reserve_log_space: sub current res");
2721 xlog_verify_grant_head(log
, 1);
2723 /* just return if we still have some of the pre-reserved space */
2724 if (ticket
->t_cnt
> 0) {
2725 spin_unlock(&log
->l_grant_lock
);
2729 xlog_grant_add_space_reserve(log
, ticket
->t_unit_res
);
2730 xlog_trace_loggrant(log
, ticket
,
2731 "xlog_regrant_reserve_log_space: exit");
2732 xlog_verify_grant_head(log
, 0);
2733 spin_unlock(&log
->l_grant_lock
);
2734 ticket
->t_curr_res
= ticket
->t_unit_res
;
2735 xlog_tic_reset_res(ticket
);
2736 } /* xlog_regrant_reserve_log_space */
2740 * Give back the space left from a reservation.
2742 * All the information we need to make a correct determination of space left
2743 * is present. For non-permanent reservations, things are quite easy. The
2744 * count should have been decremented to zero. We only need to deal with the
2745 * space remaining in the current reservation part of the ticket. If the
2746 * ticket contains a permanent reservation, there may be left over space which
2747 * needs to be released. A count of N means that N-1 refills of the current
2748 * reservation can be done before we need to ask for more space. The first
2749 * one goes to fill up the first current reservation. Once we run out of
2750 * space, the count will stay at zero and the only space remaining will be
2751 * in the current reservation field.
2754 xlog_ungrant_log_space(xlog_t
*log
,
2755 xlog_ticket_t
*ticket
)
2757 if (ticket
->t_cnt
> 0)
2760 spin_lock(&log
->l_grant_lock
);
2761 xlog_trace_loggrant(log
, ticket
, "xlog_ungrant_log_space: enter");
2763 xlog_grant_sub_space(log
, ticket
->t_curr_res
);
2765 xlog_trace_loggrant(log
, ticket
, "xlog_ungrant_log_space: sub current");
2767 /* If this is a permanent reservation ticket, we may be able to free
2768 * up more space based on the remaining count.
2770 if (ticket
->t_cnt
> 0) {
2771 ASSERT(ticket
->t_flags
& XLOG_TIC_PERM_RESERV
);
2772 xlog_grant_sub_space(log
, ticket
->t_unit_res
*ticket
->t_cnt
);
2775 xlog_trace_loggrant(log
, ticket
, "xlog_ungrant_log_space: exit");
2776 xlog_verify_grant_head(log
, 1);
2777 spin_unlock(&log
->l_grant_lock
);
2778 xfs_log_move_tail(log
->l_mp
, 1);
2779 } /* xlog_ungrant_log_space */
2783 * Atomically put back used ticket.
2786 xlog_state_put_ticket(xlog_t
*log
,
2789 spin_lock(&log
->l_icloglock
);
2790 xlog_ticket_put(log
, tic
);
2791 spin_unlock(&log
->l_icloglock
);
2792 } /* xlog_state_put_ticket */
2795 * Flush iclog to disk if this is the last reference to the given iclog and
2796 * the WANT_SYNC bit is set.
2798 * When this function is entered, the iclog is not necessarily in the
2799 * WANT_SYNC state. It may be sitting around waiting to get filled.
2804 xlog_state_release_iclog(xlog_t
*log
,
2805 xlog_in_core_t
*iclog
)
2807 int sync
= 0; /* do we sync? */
2809 xlog_assign_tail_lsn(log
->l_mp
);
2811 spin_lock(&log
->l_icloglock
);
2813 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2814 spin_unlock(&log
->l_icloglock
);
2815 return XFS_ERROR(EIO
);
2818 ASSERT(iclog
->ic_refcnt
> 0);
2819 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2820 iclog
->ic_state
== XLOG_STATE_WANT_SYNC
);
2822 if (--iclog
->ic_refcnt
== 0 &&
2823 iclog
->ic_state
== XLOG_STATE_WANT_SYNC
) {
2825 iclog
->ic_state
= XLOG_STATE_SYNCING
;
2826 INT_SET(iclog
->ic_header
.h_tail_lsn
, ARCH_CONVERT
, log
->l_tail_lsn
);
2827 xlog_verify_tail_lsn(log
, iclog
, log
->l_tail_lsn
);
2828 /* cycle incremented when incrementing curr_block */
2831 spin_unlock(&log
->l_icloglock
);
2834 * We let the log lock go, so it's possible that we hit a log I/O
2835 * error or some other SHUTDOWN condition that marks the iclog
2836 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2837 * this iclog has consistent data, so we ignore IOERROR
2838 * flags after this point.
2841 return xlog_sync(log
, iclog
);
2845 } /* xlog_state_release_iclog */
2849 * This routine will mark the current iclog in the ring as WANT_SYNC
2850 * and move the current iclog pointer to the next iclog in the ring.
2851 * When this routine is called from xlog_state_get_iclog_space(), the
2852 * exact size of the iclog has not yet been determined. All we know is
2853 * that every data block. We have run out of space in this log record.
2856 xlog_state_switch_iclogs(xlog_t
*log
,
2857 xlog_in_core_t
*iclog
,
2860 ASSERT(iclog
->ic_state
== XLOG_STATE_ACTIVE
);
2862 eventual_size
= iclog
->ic_offset
;
2863 iclog
->ic_state
= XLOG_STATE_WANT_SYNC
;
2864 INT_SET(iclog
->ic_header
.h_prev_block
, ARCH_CONVERT
, log
->l_prev_block
);
2865 log
->l_prev_block
= log
->l_curr_block
;
2866 log
->l_prev_cycle
= log
->l_curr_cycle
;
2868 /* roll log?: ic_offset changed later */
2869 log
->l_curr_block
+= BTOBB(eventual_size
)+BTOBB(log
->l_iclog_hsize
);
2871 /* Round up to next log-sunit */
2872 if (XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
) &&
2873 log
->l_mp
->m_sb
.sb_logsunit
> 1) {
2874 __uint32_t sunit_bb
= BTOBB(log
->l_mp
->m_sb
.sb_logsunit
);
2875 log
->l_curr_block
= roundup(log
->l_curr_block
, sunit_bb
);
2878 if (log
->l_curr_block
>= log
->l_logBBsize
) {
2879 log
->l_curr_cycle
++;
2880 if (log
->l_curr_cycle
== XLOG_HEADER_MAGIC_NUM
)
2881 log
->l_curr_cycle
++;
2882 log
->l_curr_block
-= log
->l_logBBsize
;
2883 ASSERT(log
->l_curr_block
>= 0);
2885 ASSERT(iclog
== log
->l_iclog
);
2886 log
->l_iclog
= iclog
->ic_next
;
2887 } /* xlog_state_switch_iclogs */
2891 * Write out all data in the in-core log as of this exact moment in time.
2893 * Data may be written to the in-core log during this call. However,
2894 * we don't guarantee this data will be written out. A change from past
2895 * implementation means this routine will *not* write out zero length LRs.
2897 * Basically, we try and perform an intelligent scan of the in-core logs.
2898 * If we determine there is no flushable data, we just return. There is no
2899 * flushable data if:
2901 * 1. the current iclog is active and has no data; the previous iclog
2902 * is in the active or dirty state.
2903 * 2. the current iclog is drity, and the previous iclog is in the
2904 * active or dirty state.
2906 * We may sleep (call psema) if:
2908 * 1. the current iclog is not in the active nor dirty state.
2909 * 2. the current iclog dirty, and the previous iclog is not in the
2910 * active nor dirty state.
2911 * 3. the current iclog is active, and there is another thread writing
2912 * to this particular iclog.
2913 * 4. a) the current iclog is active and has no other writers
2914 * b) when we return from flushing out this iclog, it is still
2915 * not in the active nor dirty state.
2918 xlog_state_sync_all(xlog_t
*log
, uint flags
, int *log_flushed
)
2920 xlog_in_core_t
*iclog
;
2923 spin_lock(&log
->l_icloglock
);
2925 iclog
= log
->l_iclog
;
2926 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2927 spin_unlock(&log
->l_icloglock
);
2928 return XFS_ERROR(EIO
);
2931 /* If the head iclog is not active nor dirty, we just attach
2932 * ourselves to the head and go to sleep.
2934 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2935 iclog
->ic_state
== XLOG_STATE_DIRTY
) {
2937 * If the head is dirty or (active and empty), then
2938 * we need to look at the previous iclog. If the previous
2939 * iclog is active or dirty we are done. There is nothing
2940 * to sync out. Otherwise, we attach ourselves to the
2941 * previous iclog and go to sleep.
2943 if (iclog
->ic_state
== XLOG_STATE_DIRTY
||
2944 (iclog
->ic_refcnt
== 0 && iclog
->ic_offset
== 0)) {
2945 iclog
= iclog
->ic_prev
;
2946 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
||
2947 iclog
->ic_state
== XLOG_STATE_DIRTY
)
2952 if (iclog
->ic_refcnt
== 0) {
2953 /* We are the only one with access to this
2954 * iclog. Flush it out now. There should
2955 * be a roundoff of zero to show that someone
2956 * has already taken care of the roundoff from
2957 * the previous sync.
2960 lsn
= INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
);
2961 xlog_state_switch_iclogs(log
, iclog
, 0);
2962 spin_unlock(&log
->l_icloglock
);
2964 if (xlog_state_release_iclog(log
, iclog
))
2965 return XFS_ERROR(EIO
);
2967 spin_lock(&log
->l_icloglock
);
2968 if (INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
) == lsn
&&
2969 iclog
->ic_state
!= XLOG_STATE_DIRTY
)
2974 /* Someone else is writing to this iclog.
2975 * Use its call to flush out the data. However,
2976 * the other thread may not force out this LR,
2977 * so we mark it WANT_SYNC.
2979 xlog_state_switch_iclogs(log
, iclog
, 0);
2985 /* By the time we come around again, the iclog could've been filled
2986 * which would give it another lsn. If we have a new lsn, just
2987 * return because the relevant data has been flushed.
2990 if (flags
& XFS_LOG_SYNC
) {
2992 * We must check if we're shutting down here, before
2993 * we wait, while we're holding the l_icloglock.
2994 * Then we check again after waking up, in case our
2995 * sleep was disturbed by a bad news.
2997 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
2998 spin_unlock(&log
->l_icloglock
);
2999 return XFS_ERROR(EIO
);
3001 XFS_STATS_INC(xs_log_force_sleep
);
3002 sv_wait(&iclog
->ic_forcesema
, PINOD
, &log
->l_icloglock
, s
);
3004 * No need to grab the log lock here since we're
3005 * only deciding whether or not to return EIO
3006 * and the memory read should be atomic.
3008 if (iclog
->ic_state
& XLOG_STATE_IOERROR
)
3009 return XFS_ERROR(EIO
);
3015 spin_unlock(&log
->l_icloglock
);
3018 } /* xlog_state_sync_all */
3022 * Used by code which implements synchronous log forces.
3024 * Find in-core log with lsn.
3025 * If it is in the DIRTY state, just return.
3026 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3027 * state and go to sleep or return.
3028 * If it is in any other state, go to sleep or return.
3030 * If filesystem activity goes to zero, the iclog will get flushed only by
3034 xlog_state_sync(xlog_t
*log
,
3039 xlog_in_core_t
*iclog
;
3040 int already_slept
= 0;
3043 spin_lock(&log
->l_icloglock
);
3044 iclog
= log
->l_iclog
;
3046 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3047 spin_unlock(&log
->l_icloglock
);
3048 return XFS_ERROR(EIO
);
3052 if (INT_GET(iclog
->ic_header
.h_lsn
, ARCH_CONVERT
) != lsn
) {
3053 iclog
= iclog
->ic_next
;
3057 if (iclog
->ic_state
== XLOG_STATE_DIRTY
) {
3058 spin_unlock(&log
->l_icloglock
);
3062 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
) {
3064 * We sleep here if we haven't already slept (e.g.
3065 * this is the first time we've looked at the correct
3066 * iclog buf) and the buffer before us is going to
3067 * be sync'ed. The reason for this is that if we
3068 * are doing sync transactions here, by waiting for
3069 * the previous I/O to complete, we can allow a few
3070 * more transactions into this iclog before we close
3073 * Otherwise, we mark the buffer WANT_SYNC, and bump
3074 * up the refcnt so we can release the log (which drops
3075 * the ref count). The state switch keeps new transaction
3076 * commits from using this buffer. When the current commits
3077 * finish writing into the buffer, the refcount will drop to
3078 * zero and the buffer will go out then.
3080 if (!already_slept
&&
3081 (iclog
->ic_prev
->ic_state
& (XLOG_STATE_WANT_SYNC
|
3082 XLOG_STATE_SYNCING
))) {
3083 ASSERT(!(iclog
->ic_state
& XLOG_STATE_IOERROR
));
3084 XFS_STATS_INC(xs_log_force_sleep
);
3085 sv_wait(&iclog
->ic_prev
->ic_writesema
, PSWP
,
3086 &log
->l_icloglock
, s
);
3092 xlog_state_switch_iclogs(log
, iclog
, 0);
3093 spin_unlock(&log
->l_icloglock
);
3094 if (xlog_state_release_iclog(log
, iclog
))
3095 return XFS_ERROR(EIO
);
3097 spin_lock(&log
->l_icloglock
);
3101 if ((flags
& XFS_LOG_SYNC
) && /* sleep */
3102 !(iclog
->ic_state
& (XLOG_STATE_ACTIVE
| XLOG_STATE_DIRTY
))) {
3105 * Don't wait on the forcesema if we know that we've
3106 * gotten a log write error.
3108 if (iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3109 spin_unlock(&log
->l_icloglock
);
3110 return XFS_ERROR(EIO
);
3112 XFS_STATS_INC(xs_log_force_sleep
);
3113 sv_wait(&iclog
->ic_forcesema
, PSWP
, &log
->l_icloglock
, s
);
3115 * No need to grab the log lock here since we're
3116 * only deciding whether or not to return EIO
3117 * and the memory read should be atomic.
3119 if (iclog
->ic_state
& XLOG_STATE_IOERROR
)
3120 return XFS_ERROR(EIO
);
3122 } else { /* just return */
3123 spin_unlock(&log
->l_icloglock
);
3127 } while (iclog
!= log
->l_iclog
);
3129 spin_unlock(&log
->l_icloglock
);
3131 } /* xlog_state_sync */
3135 * Called when we want to mark the current iclog as being ready to sync to
3139 xlog_state_want_sync(xlog_t
*log
, xlog_in_core_t
*iclog
)
3141 spin_lock(&log
->l_icloglock
);
3143 if (iclog
->ic_state
== XLOG_STATE_ACTIVE
) {
3144 xlog_state_switch_iclogs(log
, iclog
, 0);
3146 ASSERT(iclog
->ic_state
&
3147 (XLOG_STATE_WANT_SYNC
|XLOG_STATE_IOERROR
));
3150 spin_unlock(&log
->l_icloglock
);
3151 } /* xlog_state_want_sync */
3155 /*****************************************************************************
3159 *****************************************************************************
3163 * Algorithm doesn't take into account page size. ;-(
3166 xlog_state_ticket_alloc(xlog_t
*log
)
3168 xlog_ticket_t
*t_list
;
3169 xlog_ticket_t
*next
;
3171 uint i
= (NBPP
/ sizeof(xlog_ticket_t
)) - 2;
3174 * The kmem_zalloc may sleep, so we shouldn't be holding the
3175 * global lock. XXXmiken: may want to use zone allocator.
3177 buf
= (xfs_caddr_t
) kmem_zalloc(NBPP
, KM_SLEEP
);
3179 spin_lock(&log
->l_icloglock
);
3181 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3182 t_list
= (xlog_ticket_t
*)buf
;
3183 t_list
->t_next
= log
->l_unmount_free
;
3184 log
->l_unmount_free
= t_list
++;
3185 log
->l_ticket_cnt
++;
3186 log
->l_ticket_tcnt
++;
3188 /* Next ticket becomes first ticket attached to ticket free list */
3189 if (log
->l_freelist
!= NULL
) {
3190 ASSERT(log
->l_tail
!= NULL
);
3191 log
->l_tail
->t_next
= t_list
;
3193 log
->l_freelist
= t_list
;
3195 log
->l_ticket_cnt
++;
3196 log
->l_ticket_tcnt
++;
3198 /* Cycle through rest of alloc'ed memory, building up free Q */
3199 for ( ; i
> 0; i
--) {
3201 t_list
->t_next
= next
;
3203 log
->l_ticket_cnt
++;
3204 log
->l_ticket_tcnt
++;
3206 t_list
->t_next
= NULL
;
3207 log
->l_tail
= t_list
;
3208 spin_unlock(&log
->l_icloglock
);
3209 } /* xlog_state_ticket_alloc */
3213 * Put ticket into free list
3215 * Assumption: log lock is held around this call.
3218 xlog_ticket_put(xlog_t
*log
,
3219 xlog_ticket_t
*ticket
)
3221 sv_destroy(&ticket
->t_sema
);
3224 * Don't think caching will make that much difference. It's
3225 * more important to make debug easier.
3228 /* real code will want to use LIFO for caching */
3229 ticket
->t_next
= log
->l_freelist
;
3230 log
->l_freelist
= ticket
;
3231 /* no need to clear fields */
3233 /* When we debug, it is easier if tickets are cycled */
3234 ticket
->t_next
= NULL
;
3236 log
->l_tail
->t_next
= ticket
;
3238 ASSERT(log
->l_freelist
== NULL
);
3239 log
->l_freelist
= ticket
;
3241 log
->l_tail
= ticket
;
3243 log
->l_ticket_cnt
++;
3244 } /* xlog_ticket_put */
3248 * Grab ticket off freelist or allocation some more
3251 xlog_ticket_get(xlog_t
*log
,
3261 if (log
->l_freelist
== NULL
)
3262 xlog_state_ticket_alloc(log
); /* potentially sleep */
3264 spin_lock(&log
->l_icloglock
);
3265 if (log
->l_freelist
== NULL
) {
3266 spin_unlock(&log
->l_icloglock
);
3269 tic
= log
->l_freelist
;
3270 log
->l_freelist
= tic
->t_next
;
3271 if (log
->l_freelist
== NULL
)
3273 log
->l_ticket_cnt
--;
3274 spin_unlock(&log
->l_icloglock
);
3277 * Permanent reservations have up to 'cnt'-1 active log operations
3278 * in the log. A unit in this case is the amount of space for one
3279 * of these log operations. Normal reservations have a cnt of 1
3280 * and their unit amount is the total amount of space required.
3282 * The following lines of code account for non-transaction data
3283 * which occupy space in the on-disk log.
3285 * Normal form of a transaction is:
3286 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3287 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3289 * We need to account for all the leadup data and trailer data
3290 * around the transaction data.
3291 * And then we need to account for the worst case in terms of using
3293 * The worst case will happen if:
3294 * - the placement of the transaction happens to be such that the
3295 * roundoff is at its maximum
3296 * - the transaction data is synced before the commit record is synced
3297 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3298 * Therefore the commit record is in its own Log Record.
3299 * This can happen as the commit record is called with its
3300 * own region to xlog_write().
3301 * This then means that in the worst case, roundoff can happen for
3302 * the commit-rec as well.
3303 * The commit-rec is smaller than padding in this scenario and so it is
3304 * not added separately.
3307 /* for trans header */
3308 unit_bytes
+= sizeof(xlog_op_header_t
);
3309 unit_bytes
+= sizeof(xfs_trans_header_t
);
3312 unit_bytes
+= sizeof(xlog_op_header_t
);
3314 /* for LR headers */
3315 num_headers
= ((unit_bytes
+ log
->l_iclog_size
-1) >> log
->l_iclog_size_log
);
3316 unit_bytes
+= log
->l_iclog_hsize
* num_headers
;
3318 /* for commit-rec LR header - note: padding will subsume the ophdr */
3319 unit_bytes
+= log
->l_iclog_hsize
;
3321 /* for split-recs - ophdrs added when data split over LRs */
3322 unit_bytes
+= sizeof(xlog_op_header_t
) * num_headers
;
3324 /* for roundoff padding for transaction data and one for commit record */
3325 if (XFS_SB_VERSION_HASLOGV2(&log
->l_mp
->m_sb
) &&
3326 log
->l_mp
->m_sb
.sb_logsunit
> 1) {
3327 /* log su roundoff */
3328 unit_bytes
+= 2*log
->l_mp
->m_sb
.sb_logsunit
;
3331 unit_bytes
+= 2*BBSIZE
;
3334 tic
->t_unit_res
= unit_bytes
;
3335 tic
->t_curr_res
= unit_bytes
;
3338 tic
->t_tid
= (xlog_tid_t
)((__psint_t
)tic
& 0xffffffff);
3339 tic
->t_clientid
= client
;
3340 tic
->t_flags
= XLOG_TIC_INITED
;
3341 tic
->t_trans_type
= 0;
3342 if (xflags
& XFS_LOG_PERM_RESERV
)
3343 tic
->t_flags
|= XLOG_TIC_PERM_RESERV
;
3344 sv_init(&(tic
->t_sema
), SV_DEFAULT
, "logtick");
3346 xlog_tic_reset_res(tic
);
3349 } /* xlog_ticket_get */
3352 /******************************************************************************
3354 * Log debug routines
3356 ******************************************************************************
3360 * Make sure that the destination ptr is within the valid data region of
3361 * one of the iclogs. This uses backup pointers stored in a different
3362 * part of the log in case we trash the log structure.
3365 xlog_verify_dest_ptr(xlog_t
*log
,
3371 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
3372 if (ptr
>= (__psint_t
)log
->l_iclog_bak
[i
] &&
3373 ptr
<= (__psint_t
)log
->l_iclog_bak
[i
]+log
->l_iclog_size
)
3377 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3378 } /* xlog_verify_dest_ptr */
3381 xlog_verify_grant_head(xlog_t
*log
, int equals
)
3383 if (log
->l_grant_reserve_cycle
== log
->l_grant_write_cycle
) {
3385 ASSERT(log
->l_grant_reserve_bytes
>= log
->l_grant_write_bytes
);
3387 ASSERT(log
->l_grant_reserve_bytes
> log
->l_grant_write_bytes
);
3389 ASSERT(log
->l_grant_reserve_cycle
-1 == log
->l_grant_write_cycle
);
3390 ASSERT(log
->l_grant_write_bytes
>= log
->l_grant_reserve_bytes
);
3392 } /* xlog_verify_grant_head */
3394 /* check if it will fit */
3396 xlog_verify_tail_lsn(xlog_t
*log
,
3397 xlog_in_core_t
*iclog
,
3402 if (CYCLE_LSN(tail_lsn
) == log
->l_prev_cycle
) {
3404 log
->l_logBBsize
- (log
->l_prev_block
- BLOCK_LSN(tail_lsn
));
3405 if (blocks
< BTOBB(iclog
->ic_offset
)+BTOBB(log
->l_iclog_hsize
))
3406 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3408 ASSERT(CYCLE_LSN(tail_lsn
)+1 == log
->l_prev_cycle
);
3410 if (BLOCK_LSN(tail_lsn
) == log
->l_prev_block
)
3411 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3413 blocks
= BLOCK_LSN(tail_lsn
) - log
->l_prev_block
;
3414 if (blocks
< BTOBB(iclog
->ic_offset
) + 1)
3415 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3417 } /* xlog_verify_tail_lsn */
3420 * Perform a number of checks on the iclog before writing to disk.
3422 * 1. Make sure the iclogs are still circular
3423 * 2. Make sure we have a good magic number
3424 * 3. Make sure we don't have magic numbers in the data
3425 * 4. Check fields of each log operation header for:
3426 * A. Valid client identifier
3427 * B. tid ptr value falls in valid ptr space (user space code)
3428 * C. Length in log record header is correct according to the
3429 * individual operation headers within record.
3430 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3431 * log, check the preceding blocks of the physical log to make sure all
3432 * the cycle numbers agree with the current cycle number.
3435 xlog_verify_iclog(xlog_t
*log
,
3436 xlog_in_core_t
*iclog
,
3440 xlog_op_header_t
*ophead
;
3441 xlog_in_core_t
*icptr
;
3442 xlog_in_core_2_t
*xhdr
;
3444 xfs_caddr_t base_ptr
;
3445 __psint_t field_offset
;
3447 int len
, i
, j
, k
, op_len
;
3450 /* check validity of iclog pointers */
3451 spin_lock(&log
->l_icloglock
);
3452 icptr
= log
->l_iclog
;
3453 for (i
=0; i
< log
->l_iclog_bufs
; i
++) {
3455 xlog_panic("xlog_verify_iclog: invalid ptr");
3456 icptr
= icptr
->ic_next
;
3458 if (icptr
!= log
->l_iclog
)
3459 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3460 spin_unlock(&log
->l_icloglock
);
3462 /* check log magic numbers */
3463 ptr
= (xfs_caddr_t
) &(iclog
->ic_header
);
3464 if (INT_GET(*(uint
*)ptr
, ARCH_CONVERT
) != XLOG_HEADER_MAGIC_NUM
)
3465 xlog_panic("xlog_verify_iclog: invalid magic num");
3467 for (ptr
+= BBSIZE
; ptr
< ((xfs_caddr_t
)&(iclog
->ic_header
))+count
;
3469 if (INT_GET(*(uint
*)ptr
, ARCH_CONVERT
) == XLOG_HEADER_MAGIC_NUM
)
3470 xlog_panic("xlog_verify_iclog: unexpected magic num");
3474 len
= INT_GET(iclog
->ic_header
.h_num_logops
, ARCH_CONVERT
);
3475 ptr
= iclog
->ic_datap
;
3477 ophead
= (xlog_op_header_t
*)ptr
;
3478 xhdr
= (xlog_in_core_2_t
*)&iclog
->ic_header
;
3479 for (i
= 0; i
< len
; i
++) {
3480 ophead
= (xlog_op_header_t
*)ptr
;
3482 /* clientid is only 1 byte */
3483 field_offset
= (__psint_t
)
3484 ((xfs_caddr_t
)&(ophead
->oh_clientid
) - base_ptr
);
3485 if (syncing
== B_FALSE
|| (field_offset
& 0x1ff)) {
3486 clientid
= ophead
->oh_clientid
;
3488 idx
= BTOBBT((xfs_caddr_t
)&(ophead
->oh_clientid
) - iclog
->ic_datap
);
3489 if (idx
>= (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
)) {
3490 j
= idx
/ (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3491 k
= idx
% (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3492 clientid
= xlog_get_client_id(
3493 xhdr
[j
].hic_xheader
.xh_cycle_data
[k
]);
3495 clientid
= xlog_get_client_id(
3496 iclog
->ic_header
.h_cycle_data
[idx
]);
3499 if (clientid
!= XFS_TRANSACTION
&& clientid
!= XFS_LOG
)
3500 cmn_err(CE_WARN
, "xlog_verify_iclog: "
3501 "invalid clientid %d op 0x%p offset 0x%lx",
3502 clientid
, ophead
, (unsigned long)field_offset
);
3505 field_offset
= (__psint_t
)
3506 ((xfs_caddr_t
)&(ophead
->oh_len
) - base_ptr
);
3507 if (syncing
== B_FALSE
|| (field_offset
& 0x1ff)) {
3508 op_len
= be32_to_cpu(ophead
->oh_len
);
3510 idx
= BTOBBT((__psint_t
)&ophead
->oh_len
-
3511 (__psint_t
)iclog
->ic_datap
);
3512 if (idx
>= (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
)) {
3513 j
= idx
/ (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3514 k
= idx
% (XLOG_HEADER_CYCLE_SIZE
/ BBSIZE
);
3515 op_len
= INT_GET(xhdr
[j
].hic_xheader
.xh_cycle_data
[k
], ARCH_CONVERT
);
3517 op_len
= INT_GET(iclog
->ic_header
.h_cycle_data
[idx
], ARCH_CONVERT
);
3520 ptr
+= sizeof(xlog_op_header_t
) + op_len
;
3522 } /* xlog_verify_iclog */
3526 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3532 xlog_in_core_t
*iclog
, *ic
;
3534 iclog
= log
->l_iclog
;
3535 if (! (iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
3537 * Mark all the incore logs IOERROR.
3538 * From now on, no log flushes will result.
3542 ic
->ic_state
= XLOG_STATE_IOERROR
;
3544 } while (ic
!= iclog
);
3548 * Return non-zero, if state transition has already happened.
3554 * This is called from xfs_force_shutdown, when we're forcibly
3555 * shutting down the filesystem, typically because of an IO error.
3556 * Our main objectives here are to make sure that:
3557 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3558 * parties to find out, 'atomically'.
3559 * b. those who're sleeping on log reservations, pinned objects and
3560 * other resources get woken up, and be told the bad news.
3561 * c. nothing new gets queued up after (a) and (b) are done.
3562 * d. if !logerror, flush the iclogs to disk, then seal them off
3566 xfs_log_force_umount(
3567 struct xfs_mount
*mp
,
3578 * If this happens during log recovery, don't worry about
3579 * locking; the log isn't open for business yet.
3582 log
->l_flags
& XLOG_ACTIVE_RECOVERY
) {
3583 mp
->m_flags
|= XFS_MOUNT_FS_SHUTDOWN
;
3584 XFS_BUF_DONE(mp
->m_sb_bp
);
3589 * Somebody could've already done the hard work for us.
3590 * No need to get locks for this.
3592 if (logerror
&& log
->l_iclog
->ic_state
& XLOG_STATE_IOERROR
) {
3593 ASSERT(XLOG_FORCED_SHUTDOWN(log
));
3598 * We must hold both the GRANT lock and the LOG lock,
3599 * before we mark the filesystem SHUTDOWN and wake
3600 * everybody up to tell the bad news.
3602 spin_lock(&log
->l_grant_lock
);
3603 spin_lock(&log
->l_icloglock
);
3604 mp
->m_flags
|= XFS_MOUNT_FS_SHUTDOWN
;
3605 XFS_BUF_DONE(mp
->m_sb_bp
);
3607 * This flag is sort of redundant because of the mount flag, but
3608 * it's good to maintain the separation between the log and the rest
3611 log
->l_flags
|= XLOG_IO_ERROR
;
3614 * If we hit a log error, we want to mark all the iclogs IOERROR
3615 * while we're still holding the loglock.
3618 retval
= xlog_state_ioerror(log
);
3619 spin_unlock(&log
->l_icloglock
);
3622 * We don't want anybody waiting for log reservations
3623 * after this. That means we have to wake up everybody
3624 * queued up on reserve_headq as well as write_headq.
3625 * In addition, we make sure in xlog_{re}grant_log_space
3626 * that we don't enqueue anything once the SHUTDOWN flag
3627 * is set, and this action is protected by the GRANTLOCK.
3629 if ((tic
= log
->l_reserve_headq
)) {
3631 sv_signal(&tic
->t_sema
);
3633 } while (tic
!= log
->l_reserve_headq
);
3636 if ((tic
= log
->l_write_headq
)) {
3638 sv_signal(&tic
->t_sema
);
3640 } while (tic
!= log
->l_write_headq
);
3642 spin_unlock(&log
->l_grant_lock
);
3644 if (! (log
->l_iclog
->ic_state
& XLOG_STATE_IOERROR
)) {
3647 * Force the incore logs to disk before shutting the
3648 * log down completely.
3650 xlog_state_sync_all(log
, XFS_LOG_FORCE
|XFS_LOG_SYNC
, &dummy
);
3651 spin_lock(&log
->l_icloglock
);
3652 retval
= xlog_state_ioerror(log
);
3653 spin_unlock(&log
->l_icloglock
);
3656 * Wake up everybody waiting on xfs_log_force.
3657 * Callback all log item committed functions as if the
3658 * log writes were completed.
3660 xlog_state_do_callback(log
, XFS_LI_ABORTED
, NULL
);
3662 #ifdef XFSERRORDEBUG
3664 xlog_in_core_t
*iclog
;
3666 spin_lock(&log
->l_icloglock
);
3667 iclog
= log
->l_iclog
;
3669 ASSERT(iclog
->ic_callback
== 0);
3670 iclog
= iclog
->ic_next
;
3671 } while (iclog
!= log
->l_iclog
);
3672 spin_unlock(&log
->l_icloglock
);
3675 /* return non-zero if log IOERROR transition had already happened */
3680 xlog_iclogs_empty(xlog_t
*log
)
3682 xlog_in_core_t
*iclog
;
3684 iclog
= log
->l_iclog
;
3686 /* endianness does not matter here, zero is zero in
3689 if (iclog
->ic_header
.h_num_logops
)
3691 iclog
= iclog
->ic_next
;
3692 } while (iclog
!= log
->l_iclog
);