MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / xfs / xfs_log.c
blob7db33af12520b4b1903607c0e0a86bd3c8ce9163
1 /*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * High level interface routines for log manager
37 #include "xfs.h"
38 #include "xfs_macros.h"
39 #include "xfs_types.h"
40 #include "xfs_inum.h"
41 #include "xfs_ag.h"
42 #include "xfs_sb.h"
43 #include "xfs_log.h"
44 #include "xfs_trans.h"
45 #include "xfs_dir.h"
46 #include "xfs_dmapi.h"
47 #include "xfs_mount.h"
48 #include "xfs_error.h"
49 #include "xfs_log_priv.h"
50 #include "xfs_buf_item.h"
51 #include "xfs_alloc_btree.h"
52 #include "xfs_log_recover.h"
53 #include "xfs_bit.h"
54 #include "xfs_rw.h"
55 #include "xfs_trans_priv.h"
58 #define xlog_write_adv_cnt(ptr, len, off, bytes) \
59 { (ptr) += (bytes); \
60 (len) -= (bytes); \
61 (off) += (bytes);}
63 /* Local miscellaneous function prototypes */
64 STATIC int xlog_bdstrat_cb(struct xfs_buf *);
65 STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
66 xlog_in_core_t **, xfs_lsn_t *);
67 STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
68 xfs_buftarg_t *log_target,
69 xfs_daddr_t blk_offset,
70 int num_bblks);
71 STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
72 STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
73 STATIC void xlog_unalloc_log(xlog_t *log);
74 STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
75 int nentries, xfs_log_ticket_t tic,
76 xfs_lsn_t *start_lsn,
77 xlog_in_core_t **commit_iclog,
78 uint flags);
80 /* local state machine functions */
81 STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
82 STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
83 STATIC int xlog_state_get_iclog_space(xlog_t *log,
84 int len,
85 xlog_in_core_t **iclog,
86 xlog_ticket_t *ticket,
87 int *continued_write,
88 int *logoffsetp);
89 STATIC void xlog_state_put_ticket(xlog_t *log,
90 xlog_ticket_t *tic);
91 STATIC int xlog_state_release_iclog(xlog_t *log,
92 xlog_in_core_t *iclog);
93 STATIC void xlog_state_switch_iclogs(xlog_t *log,
94 xlog_in_core_t *iclog,
95 int eventual_size);
96 STATIC int xlog_state_sync(xlog_t *log, xfs_lsn_t lsn, uint flags);
97 STATIC int xlog_state_sync_all(xlog_t *log, uint flags);
98 STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
100 /* local functions to manipulate grant head */
101 STATIC int xlog_grant_log_space(xlog_t *log,
102 xlog_ticket_t *xtic);
103 STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
104 int need_bytes);
105 STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
106 xlog_ticket_t *ticket);
107 STATIC int xlog_regrant_write_log_space(xlog_t *log,
108 xlog_ticket_t *ticket);
109 STATIC void xlog_ungrant_log_space(xlog_t *log,
110 xlog_ticket_t *ticket);
113 /* local ticket functions */
114 STATIC void xlog_state_ticket_alloc(xlog_t *log);
115 STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
116 int unit_bytes,
117 int count,
118 char clientid,
119 uint flags);
120 STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
122 /* local debug functions */
123 #if defined(DEBUG) && !defined(XLOG_NOLOG)
124 STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
125 STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
126 STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
127 int count, boolean_t syncing);
128 STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
129 xfs_lsn_t tail_lsn);
130 #else
131 #define xlog_verify_dest_ptr(a,b)
132 #define xlog_verify_grant_head(a,b)
133 #define xlog_verify_iclog(a,b,c,d)
134 #define xlog_verify_tail_lsn(a,b,c)
135 #endif
137 int xlog_iclogs_empty(xlog_t *log);
139 #ifdef DEBUG
140 int xlog_do_error = 0;
141 int xlog_req_num = 0;
142 int xlog_error_mod = 33;
143 #endif
145 #define XLOG_FORCED_SHUTDOWN(log) (log->l_flags & XLOG_IO_ERROR)
148 * 0 => disable log manager
149 * 1 => enable log manager
150 * 2 => enable log manager and log debugging
152 #if defined(XLOG_NOLOG) || defined(DEBUG)
153 int xlog_debug = 1;
154 xfs_buftarg_t *xlog_target;
155 #endif
157 #if defined(XFS_LOG_TRACE)
159 void
160 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
162 if (! log->l_grant_trace) {
163 log->l_grant_trace = ktrace_alloc(1024, KM_NOSLEEP);
164 if (! log->l_grant_trace)
165 return;
168 ktrace_enter(log->l_grant_trace,
169 (void *)tic,
170 (void *)log->l_reserve_headq,
171 (void *)log->l_write_headq,
172 (void *)((unsigned long)log->l_grant_reserve_cycle),
173 (void *)((unsigned long)log->l_grant_reserve_bytes),
174 (void *)((unsigned long)log->l_grant_write_cycle),
175 (void *)((unsigned long)log->l_grant_write_bytes),
176 (void *)((unsigned long)log->l_curr_cycle),
177 (void *)((unsigned long)log->l_curr_block),
178 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn, ARCH_NOCONVERT)),
179 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn, ARCH_NOCONVERT)),
180 (void *)string,
181 (void *)((unsigned long)13),
182 (void *)((unsigned long)14),
183 (void *)((unsigned long)15),
184 (void *)((unsigned long)16));
187 void
188 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
190 pid_t pid;
192 pid = current_pid();
194 if (!iclog->ic_trace)
195 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
196 ktrace_enter(iclog->ic_trace,
197 (void *)((unsigned long)state),
198 (void *)((unsigned long)pid),
199 (void *)0,
200 (void *)0,
201 (void *)0,
202 (void *)0,
203 (void *)0,
204 (void *)0,
205 (void *)0,
206 (void *)0,
207 (void *)0,
208 (void *)0,
209 (void *)0,
210 (void *)0,
211 (void *)0,
212 (void *)0);
215 #else
216 #define xlog_trace_loggrant(log,tic,string)
217 #define xlog_trace_iclog(iclog,state)
218 #endif /* XFS_LOG_TRACE */
221 * NOTES:
223 * 1. currblock field gets updated at startup and after in-core logs
224 * marked as with WANT_SYNC.
228 * This routine is called when a user of a log manager ticket is done with
229 * the reservation. If the ticket was ever used, then a commit record for
230 * the associated transaction is written out as a log operation header with
231 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
232 * a given ticket. If the ticket was one with a permanent reservation, then
233 * a few operations are done differently. Permanent reservation tickets by
234 * default don't release the reservation. They just commit the current
235 * transaction with the belief that the reservation is still needed. A flag
236 * must be passed in before permanent reservations are actually released.
237 * When these type of tickets are not released, they need to be set into
238 * the inited state again. By doing this, a start record will be written
239 * out when the next write occurs.
241 xfs_lsn_t
242 xfs_log_done(xfs_mount_t *mp,
243 xfs_log_ticket_t xtic,
244 void **iclog,
245 uint flags)
247 xlog_t *log = mp->m_log;
248 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
249 xfs_lsn_t lsn = 0;
251 #if defined(DEBUG) || defined(XLOG_NOLOG)
252 if (!xlog_debug && xlog_target == log->l_targ)
253 return 0;
254 #endif
256 if (XLOG_FORCED_SHUTDOWN(log) ||
258 * If nothing was ever written, don't write out commit record.
259 * If we get an error, just continue and give back the log ticket.
261 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
262 (xlog_commit_record(mp, ticket,
263 (xlog_in_core_t **)iclog, &lsn)))) {
264 lsn = (xfs_lsn_t) -1;
265 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
266 flags |= XFS_LOG_REL_PERM_RESERV;
271 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
272 (flags & XFS_LOG_REL_PERM_RESERV)) {
274 * Release ticket if not permanent reservation or a specifc
275 * request has been made to release a permanent reservation.
277 xlog_ungrant_log_space(log, ticket);
278 xlog_state_put_ticket(log, ticket);
279 } else {
280 xlog_regrant_reserve_log_space(log, ticket);
283 /* If this ticket was a permanent reservation and we aren't
284 * trying to release it, reset the inited flags; so next time
285 * we write, a start record will be written out.
287 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
288 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
289 ticket->t_flags |= XLOG_TIC_INITED;
291 return lsn;
292 } /* xfs_log_done */
296 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
297 * the force is done synchronously.
299 * Asynchronous forces are implemented by setting the WANT_SYNC
300 * bit in the appropriate in-core log and then returning.
302 * Synchronous forces are implemented with a semaphore. All callers
303 * to force a given lsn to disk will wait on a semaphore attached to the
304 * specific in-core log. When given in-core log finally completes its
305 * write to disk, that thread will wake up all threads waiting on the
306 * semaphore.
309 xfs_log_force(xfs_mount_t *mp,
310 xfs_lsn_t lsn,
311 uint flags)
313 int rval;
314 xlog_t *log = mp->m_log;
316 #if defined(DEBUG) || defined(XLOG_NOLOG)
317 if (!xlog_debug && xlog_target == log->l_targ)
318 return 0;
319 #endif
321 ASSERT(flags & XFS_LOG_FORCE);
323 XFS_STATS_INC(xs_log_force);
325 if ((log->l_flags & XLOG_IO_ERROR) == 0) {
326 if (lsn == 0)
327 rval = xlog_state_sync_all(log, flags);
328 else
329 rval = xlog_state_sync(log, lsn, flags);
330 } else {
331 rval = XFS_ERROR(EIO);
334 return rval;
336 } /* xfs_log_force */
339 * Attaches a new iclog I/O completion callback routine during
340 * transaction commit. If the log is in error state, a non-zero
341 * return code is handed back and the caller is responsible for
342 * executing the callback at an appropriate time.
345 xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
346 void *iclog_hndl, /* iclog to hang callback off */
347 xfs_log_callback_t *cb)
349 xlog_t *log = mp->m_log;
350 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
351 int abortflg, spl;
353 #if defined(DEBUG) || defined(XLOG_NOLOG)
354 if (!xlog_debug && xlog_target == log->l_targ)
355 return 0;
356 #endif
357 cb->cb_next = NULL;
358 spl = LOG_LOCK(log);
359 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
360 if (!abortflg) {
361 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
362 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
363 cb->cb_next = NULL;
364 *(iclog->ic_callback_tail) = cb;
365 iclog->ic_callback_tail = &(cb->cb_next);
367 LOG_UNLOCK(log, spl);
368 return abortflg;
369 } /* xfs_log_notify */
372 xfs_log_release_iclog(xfs_mount_t *mp,
373 void *iclog_hndl)
375 xlog_t *log = mp->m_log;
376 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
378 if (xlog_state_release_iclog(log, iclog)) {
379 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
380 return(EIO);
383 return 0;
387 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
388 * to the reservation.
389 * 2. Potentially, push buffers at tail of log to disk.
391 * Each reservation is going to reserve extra space for a log record header.
392 * When writes happen to the on-disk log, we don't subtract the length of the
393 * log record header from any reservation. By wasting space in each
394 * reservation, we prevent over allocation problems.
397 xfs_log_reserve(xfs_mount_t *mp,
398 int unit_bytes,
399 int cnt,
400 xfs_log_ticket_t *ticket,
401 __uint8_t client,
402 uint flags)
404 xlog_t *log = mp->m_log;
405 xlog_ticket_t *internal_ticket;
406 int retval;
408 #if defined(DEBUG) || defined(XLOG_NOLOG)
409 if (!xlog_debug && xlog_target == log->l_targ)
410 return 0;
411 #endif
412 retval = 0;
413 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
414 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
416 if (XLOG_FORCED_SHUTDOWN(log))
417 return XFS_ERROR(EIO);
419 XFS_STATS_INC(xs_try_logspace);
421 if (*ticket != NULL) {
422 ASSERT(flags & XFS_LOG_PERM_RESERV);
423 internal_ticket = (xlog_ticket_t *)*ticket;
424 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
425 retval = xlog_regrant_write_log_space(log, internal_ticket);
426 } else {
427 /* may sleep if need to allocate more tickets */
428 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
429 client, flags);
430 *ticket = internal_ticket;
431 xlog_grant_push_ail(mp,
432 (internal_ticket->t_unit_res *
433 internal_ticket->t_cnt));
434 retval = xlog_grant_log_space(log, internal_ticket);
437 return retval;
438 } /* xfs_log_reserve */
442 * Mount a log filesystem
444 * mp - ubiquitous xfs mount point structure
445 * log_target - buftarg of on-disk log device
446 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
447 * num_bblocks - Number of BBSIZE blocks in on-disk log
449 * Return error or zero.
452 xfs_log_mount(xfs_mount_t *mp,
453 xfs_buftarg_t *log_target,
454 xfs_daddr_t blk_offset,
455 int num_bblks)
457 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
458 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
459 else {
460 cmn_err(CE_NOTE,
461 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
462 mp->m_fsname);
463 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
466 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
468 #if defined(DEBUG) || defined(XLOG_NOLOG)
469 if (!xlog_debug) {
470 cmn_err(CE_NOTE, "log dev: %s", XFS_BUFTARG_NAME(log_target));
471 return 0;
473 #endif
475 * skip log recovery on a norecovery mount. pretend it all
476 * just worked.
478 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
479 int error;
480 vfs_t *vfsp = XFS_MTOVFS(mp);
481 int readonly = (vfsp->vfs_flag & VFS_RDONLY);
483 if (readonly)
484 vfsp->vfs_flag &= ~VFS_RDONLY;
486 error = xlog_recover(mp->m_log, readonly);
488 if (readonly)
489 vfsp->vfs_flag |= VFS_RDONLY;
490 if (error) {
491 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
492 xlog_unalloc_log(mp->m_log);
493 return error;
497 /* Normal transactions can now occur */
498 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
500 /* End mounting message in xfs_log_mount_finish */
501 return 0;
502 } /* xfs_log_mount */
505 * Finish the recovery of the file system. This is separate from
506 * the xfs_log_mount() call, because it depends on the code in
507 * xfs_mountfs() to read in the root and real-time bitmap inodes
508 * between calling xfs_log_mount() and here.
510 * mp - ubiquitous xfs mount point structure
513 xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
515 int error;
517 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
518 error = xlog_recover_finish(mp->m_log, mfsi_flags);
519 else {
520 error = 0;
521 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
524 return error;
528 * Unmount processing for the log.
531 xfs_log_unmount(xfs_mount_t *mp)
533 int error;
535 error = xfs_log_unmount_write(mp);
536 xfs_log_unmount_dealloc(mp);
537 return (error);
541 * Final log writes as part of unmount.
543 * Mark the filesystem clean as unmount happens. Note that during relocation
544 * this routine needs to be executed as part of source-bag while the
545 * deallocation must not be done until source-end.
549 * Unmount record used to have a string "Unmount filesystem--" in the
550 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
551 * We just write the magic number now since that particular field isn't
552 * currently architecture converted and "nUmount" is a bit foo.
553 * As far as I know, there weren't any dependencies on the old behaviour.
557 xfs_log_unmount_write(xfs_mount_t *mp)
559 xlog_t *log = mp->m_log;
560 xlog_in_core_t *iclog;
561 #ifdef DEBUG
562 xlog_in_core_t *first_iclog;
563 #endif
564 xfs_log_iovec_t reg[1];
565 xfs_log_ticket_t tic = NULL;
566 xfs_lsn_t lsn;
567 int error;
568 SPLDECL(s);
570 /* the data section must be 32 bit size aligned */
571 struct {
572 __uint16_t magic;
573 __uint16_t pad1;
574 __uint32_t pad2; /* may as well make it 64 bits */
575 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
577 #if defined(DEBUG) || defined(XLOG_NOLOG)
578 if (!xlog_debug && xlog_target == log->l_targ)
579 return 0;
580 #endif
583 * Don't write out unmount record on read-only mounts.
584 * Or, if we are doing a forced umount (typically because of IO errors).
586 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
587 return 0;
589 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
591 #ifdef DEBUG
592 first_iclog = iclog = log->l_iclog;
593 do {
594 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
595 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
596 ASSERT(iclog->ic_offset == 0);
598 iclog = iclog->ic_next;
599 } while (iclog != first_iclog);
600 #endif
601 if (! (XLOG_FORCED_SHUTDOWN(log))) {
602 reg[0].i_addr = (void*)&magic;
603 reg[0].i_len = sizeof(magic);
605 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0);
606 if (!error) {
607 /* remove inited flag */
608 ((xlog_ticket_t *)tic)->t_flags = 0;
609 error = xlog_write(mp, reg, 1, tic, &lsn,
610 NULL, XLOG_UNMOUNT_TRANS);
612 * At this point, we're umounting anyway,
613 * so there's no point in transitioning log state
614 * to IOERROR. Just continue...
618 if (error) {
619 xfs_fs_cmn_err(CE_ALERT, mp,
620 "xfs_log_unmount: unmount record failed");
624 s = LOG_LOCK(log);
625 iclog = log->l_iclog;
626 iclog->ic_refcnt++;
627 LOG_UNLOCK(log, s);
628 xlog_state_want_sync(log, iclog);
629 (void) xlog_state_release_iclog(log, iclog);
631 s = LOG_LOCK(log);
632 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
633 iclog->ic_state == XLOG_STATE_DIRTY)) {
634 if (!XLOG_FORCED_SHUTDOWN(log)) {
635 sv_wait(&iclog->ic_forcesema, PMEM,
636 &log->l_icloglock, s);
637 } else {
638 LOG_UNLOCK(log, s);
640 } else {
641 LOG_UNLOCK(log, s);
643 if (tic)
644 xlog_state_put_ticket(log, tic);
645 } else {
647 * We're already in forced_shutdown mode, couldn't
648 * even attempt to write out the unmount transaction.
650 * Go through the motions of sync'ing and releasing
651 * the iclog, even though no I/O will actually happen,
652 * we need to wait for other log I/O's that may already
653 * be in progress. Do this as a separate section of
654 * code so we'll know if we ever get stuck here that
655 * we're in this odd situation of trying to unmount
656 * a file system that went into forced_shutdown as
657 * the result of an unmount..
659 s = LOG_LOCK(log);
660 iclog = log->l_iclog;
661 iclog->ic_refcnt++;
662 LOG_UNLOCK(log, s);
664 xlog_state_want_sync(log, iclog);
665 (void) xlog_state_release_iclog(log, iclog);
667 s = LOG_LOCK(log);
669 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
670 || iclog->ic_state == XLOG_STATE_DIRTY
671 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
673 sv_wait(&iclog->ic_forcesema, PMEM,
674 &log->l_icloglock, s);
675 } else {
676 LOG_UNLOCK(log, s);
680 return 0;
681 } /* xfs_log_unmount_write */
684 * Deallocate log structures for unmount/relocation.
686 void
687 xfs_log_unmount_dealloc(xfs_mount_t *mp)
689 xlog_unalloc_log(mp->m_log);
693 * Write region vectors to log. The write happens using the space reservation
694 * of the ticket (tic). It is not a requirement that all writes for a given
695 * transaction occur with one call to xfs_log_write().
698 xfs_log_write(xfs_mount_t * mp,
699 xfs_log_iovec_t reg[],
700 int nentries,
701 xfs_log_ticket_t tic,
702 xfs_lsn_t *start_lsn)
704 int error;
705 xlog_t *log = mp->m_log;
707 #if defined(DEBUG) || defined(XLOG_NOLOG)
708 if (!xlog_debug && xlog_target == log->l_targ) {
709 *start_lsn = 0;
710 return 0;
712 #endif
713 if (XLOG_FORCED_SHUTDOWN(log))
714 return XFS_ERROR(EIO);
716 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
717 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
719 return (error);
720 } /* xfs_log_write */
723 void
724 xfs_log_move_tail(xfs_mount_t *mp,
725 xfs_lsn_t tail_lsn)
727 xlog_ticket_t *tic;
728 xlog_t *log = mp->m_log;
729 int need_bytes, free_bytes, cycle, bytes;
730 SPLDECL(s);
732 #if defined(DEBUG) || defined(XLOG_NOLOG)
733 if (!xlog_debug && xlog_target == log->l_targ)
734 return;
735 #endif
736 /* XXXsup tmp */
737 if (XLOG_FORCED_SHUTDOWN(log))
738 return;
739 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
741 if (tail_lsn == 0) {
742 /* needed since sync_lsn is 64 bits */
743 s = LOG_LOCK(log);
744 tail_lsn = log->l_last_sync_lsn;
745 LOG_UNLOCK(log, s);
748 s = GRANT_LOCK(log);
750 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
751 * tail_lsn.
753 if (tail_lsn != 1) {
754 log->l_tail_lsn = tail_lsn;
757 if ((tic = log->l_write_headq)) {
758 #ifdef DEBUG
759 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
760 panic("Recovery problem");
761 #endif
762 cycle = log->l_grant_write_cycle;
763 bytes = log->l_grant_write_bytes;
764 free_bytes = xlog_space_left(log, cycle, bytes);
765 do {
766 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
768 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
769 break;
770 tail_lsn = 0;
771 free_bytes -= tic->t_unit_res;
772 sv_signal(&tic->t_sema);
773 tic = tic->t_next;
774 } while (tic != log->l_write_headq);
776 if ((tic = log->l_reserve_headq)) {
777 #ifdef DEBUG
778 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
779 panic("Recovery problem");
780 #endif
781 cycle = log->l_grant_reserve_cycle;
782 bytes = log->l_grant_reserve_bytes;
783 free_bytes = xlog_space_left(log, cycle, bytes);
784 do {
785 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
786 need_bytes = tic->t_unit_res*tic->t_cnt;
787 else
788 need_bytes = tic->t_unit_res;
789 if (free_bytes < need_bytes && tail_lsn != 1)
790 break;
791 tail_lsn = 0;
792 free_bytes -= need_bytes;
793 sv_signal(&tic->t_sema);
794 tic = tic->t_next;
795 } while (tic != log->l_reserve_headq);
797 GRANT_UNLOCK(log, s);
798 } /* xfs_log_move_tail */
801 * Determine if we have a transaction that has gone to disk
802 * that needs to be covered. Log activity needs to be idle (no AIL and
803 * nothing in the iclogs). And, we need to be in the right state indicating
804 * something has gone out.
807 xfs_log_need_covered(xfs_mount_t *mp)
809 SPLDECL(s);
810 int needed = 0, gen;
811 xlog_t *log = mp->m_log;
812 vfs_t *vfsp = XFS_MTOVFS(mp);
814 if (fs_frozen(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
815 (vfsp->vfs_flag & VFS_RDONLY))
816 return 0;
818 s = LOG_LOCK(log);
819 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
820 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
821 && !xfs_trans_first_ail(mp, &gen)
822 && xlog_iclogs_empty(log)) {
823 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
824 log->l_covered_state = XLOG_STATE_COVER_DONE;
825 else {
826 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
827 log->l_covered_state = XLOG_STATE_COVER_DONE2;
829 needed = 1;
831 LOG_UNLOCK(log, s);
832 return(needed);
835 /******************************************************************************
837 * local routines
839 ******************************************************************************
842 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
843 * The log manager must keep track of the last LR which was committed
844 * to disk. The lsn of this LR will become the new tail_lsn whenever
845 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
846 * the situation where stuff could be written into the log but nothing
847 * was ever in the AIL when asked. Eventually, we panic since the
848 * tail hits the head.
850 * We may be holding the log iclog lock upon entering this routine.
852 xfs_lsn_t
853 xlog_assign_tail_lsn(xfs_mount_t *mp)
855 xfs_lsn_t tail_lsn;
856 SPLDECL(s);
857 xlog_t *log = mp->m_log;
859 tail_lsn = xfs_trans_tail_ail(mp);
860 s = GRANT_LOCK(log);
861 if (tail_lsn != 0) {
862 log->l_tail_lsn = tail_lsn;
863 } else {
864 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
866 GRANT_UNLOCK(log, s);
868 return tail_lsn;
869 } /* xlog_assign_tail_lsn */
873 * Return the space in the log between the tail and the head. The head
874 * is passed in the cycle/bytes formal parms. In the special case where
875 * the reserve head has wrapped passed the tail, this calculation is no
876 * longer valid. In this case, just return 0 which means there is no space
877 * in the log. This works for all places where this function is called
878 * with the reserve head. Of course, if the write head were to ever
879 * wrap the tail, we should blow up. Rather than catch this case here,
880 * we depend on other ASSERTions in other parts of the code. XXXmiken
882 * This code also handles the case where the reservation head is behind
883 * the tail. The details of this case are described below, but the end
884 * result is that we return the size of the log as the amount of space left.
887 xlog_space_left(xlog_t *log, int cycle, int bytes)
889 int free_bytes;
890 int tail_bytes;
891 int tail_cycle;
893 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn, ARCH_NOCONVERT));
894 tail_cycle = CYCLE_LSN(log->l_tail_lsn, ARCH_NOCONVERT);
895 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
896 free_bytes = log->l_logsize - (bytes - tail_bytes);
897 } else if ((tail_cycle + 1) < cycle) {
898 return 0;
899 } else if (tail_cycle < cycle) {
900 ASSERT(tail_cycle == (cycle - 1));
901 free_bytes = tail_bytes - bytes;
902 } else {
904 * The reservation head is behind the tail.
905 * This can only happen when the AIL is empty so the tail
906 * is equal to the head and the l_roundoff value in the
907 * log structure is taking up the difference between the
908 * reservation head and the tail. The bytes accounted for
909 * by the l_roundoff field are temporarily 'lost' to the
910 * reservation mechanism, but they are cleaned up when the
911 * log buffers that created them are reused. These lost
912 * bytes are what allow the reservation head to fall behind
913 * the tail in the case that the log is 'empty'.
914 * In this case we just want to return the size of the
915 * log as the amount of space left.
917 ASSERT((tail_cycle == (cycle + 1)) ||
918 ((bytes + log->l_roundoff) >= tail_bytes));
919 free_bytes = log->l_logsize;
921 return free_bytes;
922 } /* xlog_space_left */
926 * Log function which is called when an io completes.
928 * The log manager needs its own routine, in order to control what
929 * happens with the buffer after the write completes.
931 void
932 xlog_iodone(xfs_buf_t *bp)
934 xlog_in_core_t *iclog;
935 xlog_t *l;
936 int aborted;
938 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
939 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
940 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
941 aborted = 0;
944 * Some versions of cpp barf on the recursive definition of
945 * ic_log -> hic_fields.ic_log and expand ic_log twice when
946 * it is passed through two macros. Workaround broken cpp.
948 l = iclog->ic_log;
951 * Race to shutdown the filesystem if we see an error.
953 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
954 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
955 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
956 XFS_BUF_STALE(bp);
957 xfs_force_shutdown(l->l_mp, XFS_LOG_IO_ERROR);
959 * This flag will be propagated to the trans-committed
960 * callback routines to let them know that the log-commit
961 * didn't succeed.
963 aborted = XFS_LI_ABORTED;
964 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
965 aborted = XFS_LI_ABORTED;
967 xlog_state_done_syncing(iclog, aborted);
968 if (!(XFS_BUF_ISASYNC(bp))) {
970 * Corresponding psema() will be done in bwrite(). If we don't
971 * vsema() here, panic.
973 XFS_BUF_V_IODONESEMA(bp);
975 } /* xlog_iodone */
978 * The bdstrat callback function for log bufs. This gives us a central
979 * place to trap bufs in case we get hit by a log I/O error and need to
980 * shutdown. Actually, in practice, even when we didn't get a log error,
981 * we transition the iclogs to IOERROR state *after* flushing all existing
982 * iclogs to disk. This is because we don't want anymore new transactions to be
983 * started or completed afterwards.
985 STATIC int
986 xlog_bdstrat_cb(struct xfs_buf *bp)
988 xlog_in_core_t *iclog;
990 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
992 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
993 /* note for irix bstrat will need struct bdevsw passed
994 * Fix the following macro if the code ever is merged
996 XFS_bdstrat(bp);
997 return 0;
1000 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1001 XFS_BUF_ERROR(bp, EIO);
1002 XFS_BUF_STALE(bp);
1003 xfs_biodone(bp);
1004 return (XFS_ERROR(EIO));
1010 * Return size of each in-core log record buffer.
1012 * Low memory machines only get 2 16KB buffers. We don't want to waste
1013 * memory here. However, all other machines get at least 2 32KB buffers.
1014 * The number is hard coded because we don't care about the minimum
1015 * memory size, just 32MB systems.
1017 * If the filesystem blocksize is too large, we may need to choose a
1018 * larger size since the directory code currently logs entire blocks.
1021 STATIC void
1022 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1023 xlog_t *log)
1025 int size;
1026 int xhdrs;
1028 #if defined(DEBUG) || defined(XLOG_NOLOG)
1030 * When logbufs == 0, someone has disabled the log from the FSTAB
1031 * file. This is not a documented feature. We need to set xlog_debug
1032 * to zero (this deactivates the log) and set xlog_target to the
1033 * appropriate device. Only one filesystem may be affected as such
1034 * since this is just a performance hack to test what we might be able
1035 * to get if the log were not present.
1037 if (mp->m_logbufs == 0) {
1038 xlog_debug = 0;
1039 xlog_target = log->l_targ;
1040 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1041 } else
1042 #endif
1045 * This is the normal path. If m_logbufs == -1, then the
1046 * admin has chosen to use the system defaults for logbuffers.
1048 if (mp->m_logbufs == -1) {
1049 if (xfs_physmem <= btoc(128*1024*1024)) {
1050 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1051 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1052 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1053 } else {
1054 /* 256K with 32K bufs */
1055 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1057 } else
1058 log->l_iclog_bufs = mp->m_logbufs;
1060 #if defined(DEBUG) || defined(XLOG_NOLOG)
1061 /* We are reactivating a filesystem after it was inactive */
1062 if (log->l_targ == xlog_target) {
1063 xlog_target = NULL;
1064 xlog_debug = 1;
1066 #endif
1070 * Buffer size passed in from mount system call.
1072 if (mp->m_logbsize != -1) {
1073 size = log->l_iclog_size = mp->m_logbsize;
1074 log->l_iclog_size_log = 0;
1075 while (size != 1) {
1076 log->l_iclog_size_log++;
1077 size >>= 1;
1080 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1081 /* # headers = size / 32K
1082 * one header holds cycles from 32K of data
1085 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1086 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1087 xhdrs++;
1088 log->l_iclog_hsize = xhdrs << BBSHIFT;
1089 log->l_iclog_heads = xhdrs;
1090 } else {
1091 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1092 log->l_iclog_hsize = BBSIZE;
1093 log->l_iclog_heads = 1;
1095 return;
1099 * Special case machines that have less than 32MB of memory.
1100 * All machines with more memory use 32KB buffers.
1102 if (xfs_physmem <= btoc(32*1024*1024)) {
1103 /* Don't change; min configuration */
1104 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1105 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1106 } else {
1107 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1108 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1111 /* the default log size is 16k or 32k which is one header sector */
1112 log->l_iclog_hsize = BBSIZE;
1113 log->l_iclog_heads = 1;
1116 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1117 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1119 if (mp->m_sb.sb_blocksize >= 16*1024) {
1120 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1121 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1122 if (mp->m_logbufs == -1) {
1123 switch (mp->m_sb.sb_blocksize) {
1124 case 16*1024: /* 16 KB */
1125 log->l_iclog_bufs = 3;
1126 break;
1127 case 32*1024: /* 32 KB */
1128 log->l_iclog_bufs = 4;
1129 break;
1130 case 64*1024: /* 64 KB */
1131 log->l_iclog_bufs = 8;
1132 break;
1133 default:
1134 xlog_panic("XFS: Invalid blocksize");
1135 break;
1139 } /* xlog_get_iclog_buffer_size */
1143 * This routine initializes some of the log structure for a given mount point.
1144 * Its primary purpose is to fill in enough, so recovery can occur. However,
1145 * some other stuff may be filled in too.
1147 STATIC xlog_t *
1148 xlog_alloc_log(xfs_mount_t *mp,
1149 xfs_buftarg_t *log_target,
1150 xfs_daddr_t blk_offset,
1151 int num_bblks)
1153 xlog_t *log;
1154 xlog_rec_header_t *head;
1155 xlog_in_core_t **iclogp;
1156 xlog_in_core_t *iclog, *prev_iclog=NULL;
1157 xfs_buf_t *bp;
1158 int i;
1159 int iclogsize;
1161 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1163 log->l_mp = mp;
1164 log->l_targ = log_target;
1165 log->l_logsize = BBTOB(num_bblks);
1166 log->l_logBBstart = blk_offset;
1167 log->l_logBBsize = num_bblks;
1168 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1169 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1171 log->l_prev_block = -1;
1172 ASSIGN_ANY_LSN(log->l_tail_lsn, 1, 0, ARCH_NOCONVERT);
1173 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1174 log->l_last_sync_lsn = log->l_tail_lsn;
1175 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1176 log->l_grant_reserve_cycle = 1;
1177 log->l_grant_write_cycle = 1;
1179 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1180 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1181 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1182 /* for larger sector sizes, must have v2 or external log */
1183 ASSERT(log->l_sectbb_log == 0 ||
1184 log->l_logBBstart == 0 ||
1185 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1186 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1188 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1190 xlog_get_iclog_buffer_size(mp, log);
1192 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1193 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1194 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1195 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1196 ASSERT(XFS_BUF_ISBUSY(bp));
1197 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1198 log->l_xbuf = bp;
1200 spinlock_init(&log->l_icloglock, "iclog");
1201 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1202 initnsema(&log->l_flushsema, 0, "ic-flush");
1203 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1205 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1206 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1208 iclogp = &log->l_iclog;
1210 * The amount of memory to allocate for the iclog structure is
1211 * rather funky due to the way the structure is defined. It is
1212 * done this way so that we can use different sizes for machines
1213 * with different amounts of memory. See the definition of
1214 * xlog_in_core_t in xfs_log_priv.h for details.
1216 iclogsize = log->l_iclog_size;
1217 ASSERT(log->l_iclog_size >= 4096);
1218 for (i=0; i < log->l_iclog_bufs; i++) {
1219 *iclogp = (xlog_in_core_t *)
1220 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1221 iclog = *iclogp;
1222 iclog->hic_data = (xlog_in_core_2_t *)
1223 kmem_zalloc(iclogsize, KM_SLEEP);
1225 iclog->ic_prev = prev_iclog;
1226 prev_iclog = iclog;
1227 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1229 head = &iclog->ic_header;
1230 memset(head, 0, sizeof(xlog_rec_header_t));
1231 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1232 INT_SET(head->h_version, ARCH_CONVERT,
1233 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1234 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1235 /* new fields */
1236 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1237 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1239 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1240 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1241 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1242 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1243 iclog->ic_bp = bp;
1245 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1246 iclog->ic_state = XLOG_STATE_ACTIVE;
1247 iclog->ic_log = log;
1248 iclog->ic_callback_tail = &(iclog->ic_callback);
1249 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1251 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1252 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1253 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1254 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1256 iclogp = &iclog->ic_next;
1258 *iclogp = log->l_iclog; /* complete ring */
1259 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1261 return log;
1262 } /* xlog_alloc_log */
1266 * Write out the commit record of a transaction associated with the given
1267 * ticket. Return the lsn of the commit record.
1269 STATIC int
1270 xlog_commit_record(xfs_mount_t *mp,
1271 xlog_ticket_t *ticket,
1272 xlog_in_core_t **iclog,
1273 xfs_lsn_t *commitlsnp)
1275 int error;
1276 xfs_log_iovec_t reg[1];
1278 reg[0].i_addr = NULL;
1279 reg[0].i_len = 0;
1281 ASSERT_ALWAYS(iclog);
1282 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1283 iclog, XLOG_COMMIT_TRANS))) {
1284 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
1286 return (error);
1287 } /* xlog_commit_record */
1291 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1292 * log space. This code pushes on the lsn which would supposedly free up
1293 * the 25% which we want to leave free. We may need to adopt a policy which
1294 * pushes on an lsn which is further along in the log once we reach the high
1295 * water mark. In this manner, we would be creating a low water mark.
1297 void
1298 xlog_grant_push_ail(xfs_mount_t *mp,
1299 int need_bytes)
1301 xlog_t *log = mp->m_log; /* pointer to the log */
1302 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1303 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1304 int free_blocks; /* free blocks left to write to */
1305 int free_bytes; /* free bytes left to write to */
1306 int threshold_block; /* block in lsn we'd like to be at */
1307 int threshold_cycle; /* lsn cycle we'd like to be at */
1308 int free_threshold;
1309 SPLDECL(s);
1311 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1313 s = GRANT_LOCK(log);
1314 free_bytes = xlog_space_left(log,
1315 log->l_grant_reserve_cycle,
1316 log->l_grant_reserve_bytes);
1317 tail_lsn = log->l_tail_lsn;
1318 free_blocks = BTOBBT(free_bytes);
1321 * Set the threshold for the minimum number of free blocks in the
1322 * log to the maximum of what the caller needs, one quarter of the
1323 * log, and 256 blocks.
1325 free_threshold = BTOBB(need_bytes);
1326 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1327 free_threshold = MAX(free_threshold, 256);
1328 if (free_blocks < free_threshold) {
1329 threshold_block = BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) + free_threshold;
1330 threshold_cycle = CYCLE_LSN(tail_lsn, ARCH_NOCONVERT);
1331 if (threshold_block >= log->l_logBBsize) {
1332 threshold_block -= log->l_logBBsize;
1333 threshold_cycle += 1;
1335 ASSIGN_ANY_LSN(threshold_lsn, threshold_cycle,
1336 threshold_block, ARCH_NOCONVERT);
1338 /* Don't pass in an lsn greater than the lsn of the last
1339 * log record known to be on disk.
1341 if (XFS_LSN_CMP_ARCH(threshold_lsn, log->l_last_sync_lsn, ARCH_NOCONVERT) > 0)
1342 threshold_lsn = log->l_last_sync_lsn;
1344 GRANT_UNLOCK(log, s);
1347 * Get the transaction layer to kick the dirty buffers out to
1348 * disk asynchronously. No point in trying to do this if
1349 * the filesystem is shutting down.
1351 if (threshold_lsn &&
1352 !XLOG_FORCED_SHUTDOWN(log))
1353 xfs_trans_push_ail(mp, threshold_lsn);
1354 } /* xlog_grant_push_ail */
1358 * Flush out the in-core log (iclog) to the on-disk log in a synchronous or
1359 * asynchronous fashion. Previously, we should have moved the current iclog
1360 * ptr in the log to point to the next available iclog. This allows further
1361 * write to continue while this code syncs out an iclog ready to go.
1362 * Before an in-core log can be written out, the data section must be scanned
1363 * to save away the 1st word of each BBSIZE block into the header. We replace
1364 * it with the current cycle count. Each BBSIZE block is tagged with the
1365 * cycle count because there in an implicit assumption that drives will
1366 * guarantee that entire 512 byte blocks get written at once. In other words,
1367 * we can't have part of a 512 byte block written and part not written. By
1368 * tagging each block, we will know which blocks are valid when recovering
1369 * after an unclean shutdown.
1371 * This routine is single threaded on the iclog. No other thread can be in
1372 * this routine with the same iclog. Changing contents of iclog can there-
1373 * fore be done without grabbing the state machine lock. Updating the global
1374 * log will require grabbing the lock though.
1376 * The entire log manager uses a logical block numbering scheme. Only
1377 * log_sync (and then only bwrite()) know about the fact that the log may
1378 * not start with block zero on a given device. The log block start offset
1379 * is added immediately before calling bwrite().
1383 xlog_sync(xlog_t *log,
1384 xlog_in_core_t *iclog)
1386 xfs_caddr_t dptr; /* pointer to byte sized element */
1387 xfs_buf_t *bp;
1388 int i, ops;
1389 uint count; /* byte count of bwrite */
1390 uint count_init; /* initial count before roundup */
1391 int split = 0; /* split write into two regions */
1392 int error;
1394 XFS_STATS_INC(xs_log_writes);
1395 ASSERT(iclog->ic_refcnt == 0);
1397 /* Add for LR header */
1398 count_init = log->l_iclog_hsize + iclog->ic_offset;
1400 /* Round out the log write size */
1401 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
1402 log->l_mp->m_sb.sb_logsunit > 1) {
1403 /* we have a v2 stripe unit to use */
1404 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1405 } else {
1406 count = BBTOB(BTOBB(count_init));
1408 iclog->ic_roundoff = count - count_init;
1409 log->l_roundoff += iclog->ic_roundoff;
1411 xlog_pack_data(log, iclog); /* put cycle number in every block */
1413 /* real byte length */
1414 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb)) {
1415 INT_SET(iclog->ic_header.h_len,
1416 ARCH_CONVERT,
1417 iclog->ic_offset + iclog->ic_roundoff);
1418 } else {
1419 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1422 /* put ops count in correct order */
1423 ops = iclog->ic_header.h_num_logops;
1424 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1426 bp = iclog->ic_bp;
1427 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1428 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1429 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(iclog->ic_header.h_lsn, ARCH_CONVERT));
1431 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1433 /* Do we need to split this write into 2 parts? */
1434 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1435 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1436 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1437 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1438 } else {
1439 iclog->ic_bwritecnt = 1;
1441 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1442 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1443 XFS_BUF_BUSY(bp);
1444 XFS_BUF_ASYNC(bp);
1446 * Do a disk write cache flush for the log block.
1447 * This is a bit of a sledgehammer, it would be better
1448 * to use a tag barrier here that just prevents reordering.
1449 * It may not be needed to flush the first split block in the log wrap
1450 * case, but do it anyways to be safe -AK
1452 if (!(log->l_mp->m_flags & XFS_MOUNT_NOLOGFLUSH))
1453 XFS_BUF_FLUSH(bp);
1455 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1456 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1458 xlog_verify_iclog(log, iclog, count, B_TRUE);
1460 /* account for log which doesn't start at block #0 */
1461 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1463 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1464 * is shutting down.
1466 XFS_BUF_WRITE(bp);
1468 if ((error = XFS_bwrite(bp))) {
1469 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1470 XFS_BUF_ADDR(bp));
1471 return (error);
1473 if (split) {
1474 bp = iclog->ic_log->l_xbuf;
1475 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1476 (unsigned long)1);
1477 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1478 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1479 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1480 (__psint_t)count), split);
1481 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1482 XFS_BUF_BUSY(bp);
1483 XFS_BUF_ASYNC(bp);
1484 if (!(log->l_mp->m_flags & XFS_MOUNT_NOLOGFLUSH))
1485 XFS_BUF_FLUSH(bp);
1486 dptr = XFS_BUF_PTR(bp);
1488 * Bump the cycle numbers at the start of each block
1489 * since this part of the buffer is at the start of
1490 * a new cycle. Watch out for the header magic number
1491 * case, though.
1493 for (i=0; i<split; i += BBSIZE) {
1494 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1495 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1496 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1497 dptr += BBSIZE;
1500 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1501 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1503 /* account for internal log which does't start at block #0 */
1504 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1505 XFS_BUF_WRITE(bp);
1506 if ((error = XFS_bwrite(bp))) {
1507 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1508 bp, XFS_BUF_ADDR(bp));
1509 return (error);
1512 return (0);
1513 } /* xlog_sync */
1517 * Unallocate a log structure
1519 void
1520 xlog_unalloc_log(xlog_t *log)
1522 xlog_in_core_t *iclog, *next_iclog;
1523 xlog_ticket_t *tic, *next_tic;
1524 int i;
1527 iclog = log->l_iclog;
1528 for (i=0; i<log->l_iclog_bufs; i++) {
1529 sv_destroy(&iclog->ic_forcesema);
1530 sv_destroy(&iclog->ic_writesema);
1531 xfs_buf_free(iclog->ic_bp);
1532 #ifdef XFS_LOG_TRACE
1533 if (iclog->ic_trace != NULL) {
1534 ktrace_free(iclog->ic_trace);
1536 #endif
1537 next_iclog = iclog->ic_next;
1538 kmem_free(iclog->hic_data, log->l_iclog_size);
1539 kmem_free(iclog, sizeof(xlog_in_core_t));
1540 iclog = next_iclog;
1542 freesema(&log->l_flushsema);
1543 spinlock_destroy(&log->l_icloglock);
1544 spinlock_destroy(&log->l_grant_lock);
1546 /* XXXsup take a look at this again. */
1547 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1548 !XLOG_FORCED_SHUTDOWN(log)) {
1549 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1550 "xlog_unalloc_log: (cnt: %d, total: %d)",
1551 log->l_ticket_cnt, log->l_ticket_tcnt);
1552 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1554 } else {
1555 tic = log->l_unmount_free;
1556 while (tic) {
1557 next_tic = tic->t_next;
1558 kmem_free(tic, NBPP);
1559 tic = next_tic;
1562 xfs_buf_free(log->l_xbuf);
1563 #ifdef XFS_LOG_TRACE
1564 if (log->l_trace != NULL) {
1565 ktrace_free(log->l_trace);
1567 if (log->l_grant_trace != NULL) {
1568 ktrace_free(log->l_grant_trace);
1570 #endif
1571 log->l_mp->m_log = NULL;
1572 kmem_free(log, sizeof(xlog_t));
1573 } /* xlog_unalloc_log */
1576 * Update counters atomically now that memcpy is done.
1578 /* ARGSUSED */
1579 static inline void
1580 xlog_state_finish_copy(xlog_t *log,
1581 xlog_in_core_t *iclog,
1582 int record_cnt,
1583 int copy_bytes)
1585 SPLDECL(s);
1587 s = LOG_LOCK(log);
1589 iclog->ic_header.h_num_logops += record_cnt;
1590 iclog->ic_offset += copy_bytes;
1592 LOG_UNLOCK(log, s);
1593 } /* xlog_state_finish_copy */
1599 * Write some region out to in-core log
1601 * This will be called when writing externally provided regions or when
1602 * writing out a commit record for a given transaction.
1604 * General algorithm:
1605 * 1. Find total length of this write. This may include adding to the
1606 * lengths passed in.
1607 * 2. Check whether we violate the tickets reservation.
1608 * 3. While writing to this iclog
1609 * A. Reserve as much space in this iclog as can get
1610 * B. If this is first write, save away start lsn
1611 * C. While writing this region:
1612 * 1. If first write of transaction, write start record
1613 * 2. Write log operation header (header per region)
1614 * 3. Find out if we can fit entire region into this iclog
1615 * 4. Potentially, verify destination memcpy ptr
1616 * 5. Memcpy (partial) region
1617 * 6. If partial copy, release iclog; otherwise, continue
1618 * copying more regions into current iclog
1619 * 4. Mark want sync bit (in simulation mode)
1620 * 5. Release iclog for potential flush to on-disk log.
1622 * ERRORS:
1623 * 1. Panic if reservation is overrun. This should never happen since
1624 * reservation amounts are generated internal to the filesystem.
1625 * NOTES:
1626 * 1. Tickets are single threaded data structures.
1627 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1628 * syncing routine. When a single log_write region needs to span
1629 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1630 * on all log operation writes which don't contain the end of the
1631 * region. The XLOG_END_TRANS bit is used for the in-core log
1632 * operation which contains the end of the continued log_write region.
1633 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1634 * we don't really know exactly how much space will be used. As a result,
1635 * we don't update ic_offset until the end when we know exactly how many
1636 * bytes have been written out.
1639 xlog_write(xfs_mount_t * mp,
1640 xfs_log_iovec_t reg[],
1641 int nentries,
1642 xfs_log_ticket_t tic,
1643 xfs_lsn_t *start_lsn,
1644 xlog_in_core_t **commit_iclog,
1645 uint flags)
1647 xlog_t *log = mp->m_log;
1648 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1649 xlog_op_header_t *logop_head; /* ptr to log operation header */
1650 xlog_in_core_t *iclog; /* ptr to current in-core log */
1651 __psint_t ptr; /* copy address into data region */
1652 int len; /* # xlog_write() bytes 2 still copy */
1653 int index; /* region index currently copying */
1654 int log_offset; /* offset (from 0) into data region */
1655 int start_rec_copy; /* # bytes to copy for start record */
1656 int partial_copy; /* did we split a region? */
1657 int partial_copy_len;/* # bytes copied if split region */
1658 int need_copy; /* # bytes need to memcpy this region */
1659 int copy_len; /* # bytes actually memcpy'ing */
1660 int copy_off; /* # bytes from entry start */
1661 int contwr; /* continued write of in-core log? */
1662 int error;
1663 int record_cnt = 0, data_cnt = 0;
1665 partial_copy_len = partial_copy = 0;
1667 /* Calculate potential maximum space. Each region gets its own
1668 * xlog_op_header_t and may need to be double word aligned.
1670 len = 0;
1671 if (ticket->t_flags & XLOG_TIC_INITED) /* acct for start rec of xact */
1672 len += sizeof(xlog_op_header_t);
1674 for (index = 0; index < nentries; index++) {
1675 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1676 len += reg[index].i_len;
1678 contwr = *start_lsn = 0;
1680 if (ticket->t_curr_res < len) {
1681 #ifdef DEBUG
1682 xlog_panic(
1683 "xfs_log_write: reservation ran out. Need to up reservation");
1684 #else
1685 /* Customer configurable panic */
1686 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1687 "xfs_log_write: reservation ran out. Need to up reservation");
1688 /* If we did not panic, shutdown the filesystem */
1689 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1690 #endif
1691 } else
1692 ticket->t_curr_res -= len;
1694 for (index = 0; index < nentries; ) {
1695 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1696 &contwr, &log_offset)))
1697 return (error);
1699 ASSERT(log_offset <= iclog->ic_size - 1);
1700 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1702 /* start_lsn is the first lsn written to. That's all we need. */
1703 if (! *start_lsn)
1704 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1706 /* This loop writes out as many regions as can fit in the amount
1707 * of space which was allocated by xlog_state_get_iclog_space().
1709 while (index < nentries) {
1710 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1711 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1712 start_rec_copy = 0;
1714 /* If first write for transaction, insert start record.
1715 * We can't be trying to commit if we are inited. We can't
1716 * have any "partial_copy" if we are inited.
1718 if (ticket->t_flags & XLOG_TIC_INITED) {
1719 logop_head = (xlog_op_header_t *)ptr;
1720 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1721 logop_head->oh_clientid = ticket->t_clientid;
1722 INT_ZERO(logop_head->oh_len, ARCH_CONVERT);
1723 logop_head->oh_flags = XLOG_START_TRANS;
1724 INT_ZERO(logop_head->oh_res2, ARCH_CONVERT);
1725 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1726 record_cnt++;
1728 start_rec_copy = sizeof(xlog_op_header_t);
1729 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1732 /* Copy log operation header directly into data section */
1733 logop_head = (xlog_op_header_t *)ptr;
1734 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1735 logop_head->oh_clientid = ticket->t_clientid;
1736 INT_ZERO(logop_head->oh_res2, ARCH_CONVERT);
1738 /* header copied directly */
1739 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1741 /* are we copying a commit or unmount record? */
1742 logop_head->oh_flags = flags;
1745 * We've seen logs corrupted with bad transaction client
1746 * ids. This makes sure that XFS doesn't generate them on.
1747 * Turn this into an EIO and shut down the filesystem.
1749 switch (logop_head->oh_clientid) {
1750 case XFS_TRANSACTION:
1751 case XFS_VOLUME:
1752 case XFS_LOG:
1753 break;
1754 default:
1755 xfs_fs_cmn_err(CE_WARN, mp,
1756 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1757 logop_head->oh_clientid, tic);
1758 return XFS_ERROR(EIO);
1761 /* Partial write last time? => (partial_copy != 0)
1762 * need_copy is the amount we'd like to copy if everything could
1763 * fit in the current memcpy.
1765 need_copy = reg[index].i_len - partial_copy_len;
1767 copy_off = partial_copy_len;
1768 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1769 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1770 if (partial_copy)
1771 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1772 partial_copy_len = partial_copy = 0;
1773 } else { /* partial write */
1774 copy_len = iclog->ic_size - log_offset;
1775 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1776 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1777 if (partial_copy)
1778 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1779 partial_copy_len += copy_len;
1780 partial_copy++;
1781 len += sizeof(xlog_op_header_t); /* from splitting of region */
1782 /* account for new log op header */
1783 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1785 xlog_verify_dest_ptr(log, ptr);
1787 /* copy region */
1788 ASSERT(copy_len >= 0);
1789 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1790 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1792 /* make copy_len total bytes copied, including headers */
1793 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1794 record_cnt++;
1795 data_cnt += contwr ? copy_len : 0;
1796 if (partial_copy) { /* copied partial region */
1797 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1798 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1799 record_cnt = data_cnt = 0;
1800 if ((error = xlog_state_release_iclog(log, iclog)))
1801 return (error);
1802 break; /* don't increment index */
1803 } else { /* copied entire region */
1804 index++;
1805 partial_copy_len = partial_copy = 0;
1807 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1808 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1809 record_cnt = data_cnt = 0;
1810 xlog_state_want_sync(log, iclog);
1811 if (commit_iclog) {
1812 ASSERT(flags & XLOG_COMMIT_TRANS);
1813 *commit_iclog = iclog;
1814 } else if ((error = xlog_state_release_iclog(log, iclog)))
1815 return (error);
1816 if (index == nentries)
1817 return 0; /* we are done */
1818 else
1819 break;
1821 } /* if (partial_copy) */
1822 } /* while (index < nentries) */
1823 } /* for (index = 0; index < nentries; ) */
1824 ASSERT(len == 0);
1826 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1827 if (commit_iclog) {
1828 ASSERT(flags & XLOG_COMMIT_TRANS);
1829 *commit_iclog = iclog;
1830 return 0;
1832 return (xlog_state_release_iclog(log, iclog));
1833 } /* xlog_write */
1836 /*****************************************************************************
1838 * State Machine functions
1840 *****************************************************************************
1843 /* Clean iclogs starting from the head. This ordering must be
1844 * maintained, so an iclog doesn't become ACTIVE beyond one that
1845 * is SYNCING. This is also required to maintain the notion that we use
1846 * a counting semaphore to hold off would be writers to the log when every
1847 * iclog is trying to sync to disk.
1849 * State Change: DIRTY -> ACTIVE
1851 void
1852 xlog_state_clean_log(xlog_t *log)
1854 xlog_in_core_t *iclog;
1855 int changed = 0;
1857 iclog = log->l_iclog;
1858 do {
1859 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1860 iclog->ic_state = XLOG_STATE_ACTIVE;
1861 iclog->ic_offset = 0;
1862 iclog->ic_callback = NULL; /* don't need to free */
1864 * If the number of ops in this iclog indicate it just
1865 * contains the dummy transaction, we can
1866 * change state into IDLE (the second time around).
1867 * Otherwise we should change the state into
1868 * NEED a dummy.
1869 * We don't need to cover the dummy.
1871 if (!changed &&
1872 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1873 changed = 1;
1874 } else {
1876 * We have two dirty iclogs so start over
1877 * This could also be num of ops indicates
1878 * this is not the dummy going out.
1880 changed = 2;
1882 INT_ZERO(iclog->ic_header.h_num_logops, ARCH_CONVERT);
1883 memset(iclog->ic_header.h_cycle_data, 0,
1884 sizeof(iclog->ic_header.h_cycle_data));
1885 INT_ZERO(iclog->ic_header.h_lsn, ARCH_CONVERT);
1886 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1887 /* do nothing */;
1888 else
1889 break; /* stop cleaning */
1890 iclog = iclog->ic_next;
1891 } while (iclog != log->l_iclog);
1893 /* log is locked when we are called */
1895 * Change state for the dummy log recording.
1896 * We usually go to NEED. But we go to NEED2 if the changed indicates
1897 * we are done writing the dummy record.
1898 * If we are done with the second dummy recored (DONE2), then
1899 * we go to IDLE.
1901 if (changed) {
1902 switch (log->l_covered_state) {
1903 case XLOG_STATE_COVER_IDLE:
1904 case XLOG_STATE_COVER_NEED:
1905 case XLOG_STATE_COVER_NEED2:
1906 log->l_covered_state = XLOG_STATE_COVER_NEED;
1907 break;
1909 case XLOG_STATE_COVER_DONE:
1910 if (changed == 1)
1911 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1912 else
1913 log->l_covered_state = XLOG_STATE_COVER_NEED;
1914 break;
1916 case XLOG_STATE_COVER_DONE2:
1917 if (changed == 1)
1918 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1919 else
1920 log->l_covered_state = XLOG_STATE_COVER_NEED;
1921 break;
1923 default:
1924 ASSERT(0);
1927 } /* xlog_state_clean_log */
1929 STATIC xfs_lsn_t
1930 xlog_get_lowest_lsn(
1931 xlog_t *log)
1933 xlog_in_core_t *lsn_log;
1934 xfs_lsn_t lowest_lsn, lsn;
1936 lsn_log = log->l_iclog;
1937 lowest_lsn = 0;
1938 do {
1939 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
1940 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
1941 if ((lsn && !lowest_lsn) ||
1942 (XFS_LSN_CMP_ARCH(lsn, lowest_lsn, ARCH_NOCONVERT) < 0)) {
1943 lowest_lsn = lsn;
1946 lsn_log = lsn_log->ic_next;
1947 } while (lsn_log != log->l_iclog);
1948 return(lowest_lsn);
1952 STATIC void
1953 xlog_state_do_callback(
1954 xlog_t *log,
1955 int aborted,
1956 xlog_in_core_t *ciclog)
1958 xlog_in_core_t *iclog;
1959 xlog_in_core_t *first_iclog; /* used to know when we've
1960 * processed all iclogs once */
1961 xfs_log_callback_t *cb, *cb_next;
1962 int flushcnt = 0;
1963 xfs_lsn_t lowest_lsn;
1964 int ioerrors; /* counter: iclogs with errors */
1965 int loopdidcallbacks; /* flag: inner loop did callbacks*/
1966 int funcdidcallbacks; /* flag: function did callbacks */
1967 int repeats; /* for issuing console warnings if
1968 * looping too many times */
1969 SPLDECL(s);
1971 s = LOG_LOCK(log);
1972 first_iclog = iclog = log->l_iclog;
1973 ioerrors = 0;
1974 funcdidcallbacks = 0;
1975 repeats = 0;
1977 do {
1979 * Scan all iclogs starting with the one pointed to by the
1980 * log. Reset this starting point each time the log is
1981 * unlocked (during callbacks).
1983 * Keep looping through iclogs until one full pass is made
1984 * without running any callbacks.
1986 first_iclog = log->l_iclog;
1987 iclog = log->l_iclog;
1988 loopdidcallbacks = 0;
1989 repeats++;
1991 do {
1993 /* skip all iclogs in the ACTIVE & DIRTY states */
1994 if (iclog->ic_state &
1995 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
1996 iclog = iclog->ic_next;
1997 continue;
2001 * Between marking a filesystem SHUTDOWN and stopping
2002 * the log, we do flush all iclogs to disk (if there
2003 * wasn't a log I/O error). So, we do want things to
2004 * go smoothly in case of just a SHUTDOWN w/o a
2005 * LOG_IO_ERROR.
2007 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2009 * Can only perform callbacks in order. Since
2010 * this iclog is not in the DONE_SYNC/
2011 * DO_CALLBACK state, we skip the rest and
2012 * just try to clean up. If we set our iclog
2013 * to DO_CALLBACK, we will not process it when
2014 * we retry since a previous iclog is in the
2015 * CALLBACK and the state cannot change since
2016 * we are holding the LOG_LOCK.
2018 if (!(iclog->ic_state &
2019 (XLOG_STATE_DONE_SYNC |
2020 XLOG_STATE_DO_CALLBACK))) {
2021 if (ciclog && (ciclog->ic_state ==
2022 XLOG_STATE_DONE_SYNC)) {
2023 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2025 break;
2028 * We now have an iclog that is in either the
2029 * DO_CALLBACK or DONE_SYNC states. The other
2030 * states (WANT_SYNC, SYNCING, or CALLBACK were
2031 * caught by the above if and are going to
2032 * clean (i.e. we aren't doing their callbacks)
2033 * see the above if.
2037 * We will do one more check here to see if we
2038 * have chased our tail around.
2041 lowest_lsn = xlog_get_lowest_lsn(log);
2042 if (lowest_lsn && (
2043 XFS_LSN_CMP_ARCH(
2044 lowest_lsn,
2045 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT),
2046 ARCH_NOCONVERT
2047 )<0)) {
2048 iclog = iclog->ic_next;
2049 continue; /* Leave this iclog for
2050 * another thread */
2053 iclog->ic_state = XLOG_STATE_CALLBACK;
2055 LOG_UNLOCK(log, s);
2057 /* l_last_sync_lsn field protected by
2058 * GRANT_LOCK. Don't worry about iclog's lsn.
2059 * No one else can be here except us.
2061 s = GRANT_LOCK(log);
2062 ASSERT(XFS_LSN_CMP_ARCH(
2063 log->l_last_sync_lsn,
2064 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT),
2065 ARCH_NOCONVERT
2066 )<=0);
2067 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2068 GRANT_UNLOCK(log, s);
2071 * Keep processing entries in the callback list
2072 * until we come around and it is empty. We
2073 * need to atomically see that the list is
2074 * empty and change the state to DIRTY so that
2075 * we don't miss any more callbacks being added.
2077 s = LOG_LOCK(log);
2078 } else {
2079 ioerrors++;
2081 cb = iclog->ic_callback;
2083 while (cb != 0) {
2084 iclog->ic_callback_tail = &(iclog->ic_callback);
2085 iclog->ic_callback = NULL;
2086 LOG_UNLOCK(log, s);
2088 /* perform callbacks in the order given */
2089 for (; cb != 0; cb = cb_next) {
2090 cb_next = cb->cb_next;
2091 cb->cb_func(cb->cb_arg, aborted);
2093 s = LOG_LOCK(log);
2094 cb = iclog->ic_callback;
2097 loopdidcallbacks++;
2098 funcdidcallbacks++;
2100 ASSERT(iclog->ic_callback == 0);
2101 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2102 iclog->ic_state = XLOG_STATE_DIRTY;
2105 * Transition from DIRTY to ACTIVE if applicable.
2106 * NOP if STATE_IOERROR.
2108 xlog_state_clean_log(log);
2110 /* wake up threads waiting in xfs_log_force() */
2111 sv_broadcast(&iclog->ic_forcesema);
2113 iclog = iclog->ic_next;
2114 } while (first_iclog != iclog);
2115 if (repeats && (repeats % 10) == 0) {
2116 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2117 "xlog_state_do_callback: looping %d", repeats);
2119 } while (!ioerrors && loopdidcallbacks);
2122 * make one last gasp attempt to see if iclogs are being left in
2123 * limbo..
2125 #ifdef DEBUG
2126 if (funcdidcallbacks) {
2127 first_iclog = iclog = log->l_iclog;
2128 do {
2129 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2131 * Terminate the loop if iclogs are found in states
2132 * which will cause other threads to clean up iclogs.
2134 * SYNCING - i/o completion will go through logs
2135 * DONE_SYNC - interrupt thread should be waiting for
2136 * LOG_LOCK
2137 * IOERROR - give up hope all ye who enter here
2139 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2140 iclog->ic_state == XLOG_STATE_SYNCING ||
2141 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2142 iclog->ic_state == XLOG_STATE_IOERROR )
2143 break;
2144 iclog = iclog->ic_next;
2145 } while (first_iclog != iclog);
2147 #endif
2149 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2150 flushcnt = log->l_flushcnt;
2151 log->l_flushcnt = 0;
2153 LOG_UNLOCK(log, s);
2154 while (flushcnt--)
2155 vsema(&log->l_flushsema);
2156 } /* xlog_state_do_callback */
2160 * Finish transitioning this iclog to the dirty state.
2162 * Make sure that we completely execute this routine only when this is
2163 * the last call to the iclog. There is a good chance that iclog flushes,
2164 * when we reach the end of the physical log, get turned into 2 separate
2165 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2166 * routine. By using the reference count bwritecnt, we guarantee that only
2167 * the second completion goes through.
2169 * Callbacks could take time, so they are done outside the scope of the
2170 * global state machine log lock. Assume that the calls to cvsema won't
2171 * take a long time. At least we know it won't sleep.
2173 void
2174 xlog_state_done_syncing(
2175 xlog_in_core_t *iclog,
2176 int aborted)
2178 xlog_t *log = iclog->ic_log;
2179 SPLDECL(s);
2181 s = LOG_LOCK(log);
2183 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2184 iclog->ic_state == XLOG_STATE_IOERROR);
2185 ASSERT(iclog->ic_refcnt == 0);
2186 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2190 * If we got an error, either on the first buffer, or in the case of
2191 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2192 * and none should ever be attempted to be written to disk
2193 * again.
2195 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2196 if (--iclog->ic_bwritecnt == 1) {
2197 LOG_UNLOCK(log, s);
2198 return;
2200 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2204 * Someone could be sleeping prior to writing out the next
2205 * iclog buffer, we wake them all, one will get to do the
2206 * I/O, the others get to wait for the result.
2208 sv_broadcast(&iclog->ic_writesema);
2209 LOG_UNLOCK(log, s);
2210 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2211 } /* xlog_state_done_syncing */
2215 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2216 * sleep. The flush semaphore is set to the number of in-core buffers and
2217 * decremented around disk syncing. Therefore, if all buffers are syncing,
2218 * this semaphore will cause new writes to sleep until a sync completes.
2219 * Otherwise, this code just does p() followed by v(). This approximates
2220 * a sleep/wakeup except we can't race.
2222 * The in-core logs are used in a circular fashion. They are not used
2223 * out-of-order even when an iclog past the head is free.
2225 * return:
2226 * * log_offset where xlog_write() can start writing into the in-core
2227 * log's data space.
2228 * * in-core log pointer to which xlog_write() should write.
2229 * * boolean indicating this is a continued write to an in-core log.
2230 * If this is the last write, then the in-core log's offset field
2231 * needs to be incremented, depending on the amount of data which
2232 * is copied.
2235 xlog_state_get_iclog_space(xlog_t *log,
2236 int len,
2237 xlog_in_core_t **iclogp,
2238 xlog_ticket_t *ticket,
2239 int *continued_write,
2240 int *logoffsetp)
2242 SPLDECL(s);
2243 int log_offset;
2244 xlog_rec_header_t *head;
2245 xlog_in_core_t *iclog;
2246 int error;
2248 restart:
2249 s = LOG_LOCK(log);
2250 if (XLOG_FORCED_SHUTDOWN(log)) {
2251 LOG_UNLOCK(log, s);
2252 return XFS_ERROR(EIO);
2255 iclog = log->l_iclog;
2256 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2257 log->l_flushcnt++;
2258 LOG_UNLOCK(log, s);
2259 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2260 XFS_STATS_INC(xs_log_noiclogs);
2261 /* Ensure that log writes happen */
2262 psema(&log->l_flushsema, PINOD);
2263 goto restart;
2265 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2266 head = &iclog->ic_header;
2268 iclog->ic_refcnt++; /* prevents sync */
2269 log_offset = iclog->ic_offset;
2271 /* On the 1st write to an iclog, figure out lsn. This works
2272 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2273 * committing to. If the offset is set, that's how many blocks
2274 * must be written.
2276 if (log_offset == 0) {
2277 ticket->t_curr_res -= log->l_iclog_hsize;
2278 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2279 ASSIGN_LSN(head->h_lsn, log, ARCH_CONVERT);
2280 ASSERT(log->l_curr_block >= 0);
2282 /* round off error from last write with this iclog */
2283 ticket->t_curr_res -= iclog->ic_roundoff;
2284 log->l_roundoff -= iclog->ic_roundoff;
2285 iclog->ic_roundoff = 0;
2288 /* If there is enough room to write everything, then do it. Otherwise,
2289 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2290 * bit is on, so this will get flushed out. Don't update ic_offset
2291 * until you know exactly how many bytes get copied. Therefore, wait
2292 * until later to update ic_offset.
2294 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2295 * can fit into remaining data section.
2297 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2298 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2300 /* If I'm the only one writing to this iclog, sync it to disk */
2301 if (iclog->ic_refcnt == 1) {
2302 LOG_UNLOCK(log, s);
2303 if ((error = xlog_state_release_iclog(log, iclog)))
2304 return (error);
2305 } else {
2306 iclog->ic_refcnt--;
2307 LOG_UNLOCK(log, s);
2309 goto restart;
2312 /* Do we have enough room to write the full amount in the remainder
2313 * of this iclog? Or must we continue a write on the next iclog and
2314 * mark this iclog as completely taken? In the case where we switch
2315 * iclogs (to mark it taken), this particular iclog will release/sync
2316 * to disk in xlog_write().
2318 if (len <= iclog->ic_size - iclog->ic_offset) {
2319 *continued_write = 0;
2320 iclog->ic_offset += len;
2321 } else {
2322 *continued_write = 1;
2323 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2325 *iclogp = iclog;
2327 ASSERT(iclog->ic_offset <= iclog->ic_size);
2328 LOG_UNLOCK(log, s);
2330 *logoffsetp = log_offset;
2331 return 0;
2332 } /* xlog_state_get_iclog_space */
2335 * Atomically get the log space required for a log ticket.
2337 * Once a ticket gets put onto the reserveq, it will only return after
2338 * the needed reservation is satisfied.
2340 STATIC int
2341 xlog_grant_log_space(xlog_t *log,
2342 xlog_ticket_t *tic)
2344 int free_bytes;
2345 int need_bytes;
2346 SPLDECL(s);
2347 #ifdef DEBUG
2348 xfs_lsn_t tail_lsn;
2349 #endif
2352 #ifdef DEBUG
2353 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2354 panic("grant Recovery problem");
2355 #endif
2357 /* Is there space or do we need to sleep? */
2358 s = GRANT_LOCK(log);
2359 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2361 /* something is already sleeping; insert new transaction at end */
2362 if (log->l_reserve_headq) {
2363 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2364 xlog_trace_loggrant(log, tic,
2365 "xlog_grant_log_space: sleep 1");
2367 * Gotta check this before going to sleep, while we're
2368 * holding the grant lock.
2370 if (XLOG_FORCED_SHUTDOWN(log))
2371 goto error_return;
2373 XFS_STATS_INC(xs_sleep_logspace);
2374 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2376 * If we got an error, and the filesystem is shutting down,
2377 * we'll catch it down below. So just continue...
2379 xlog_trace_loggrant(log, tic,
2380 "xlog_grant_log_space: wake 1");
2381 s = GRANT_LOCK(log);
2383 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2384 need_bytes = tic->t_unit_res*tic->t_ocnt;
2385 else
2386 need_bytes = tic->t_unit_res;
2388 redo:
2389 if (XLOG_FORCED_SHUTDOWN(log))
2390 goto error_return;
2392 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2393 log->l_grant_reserve_bytes);
2394 if (free_bytes < need_bytes) {
2395 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2396 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2397 xlog_trace_loggrant(log, tic,
2398 "xlog_grant_log_space: sleep 2");
2399 XFS_STATS_INC(xs_sleep_logspace);
2400 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2402 if (XLOG_FORCED_SHUTDOWN(log)) {
2403 s = GRANT_LOCK(log);
2404 goto error_return;
2407 xlog_trace_loggrant(log, tic,
2408 "xlog_grant_log_space: wake 2");
2409 xlog_grant_push_ail(log->l_mp, need_bytes);
2410 s = GRANT_LOCK(log);
2411 goto redo;
2412 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2413 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2415 /* we've got enough space */
2416 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w');
2417 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'r');
2418 #ifdef DEBUG
2419 tail_lsn = log->l_tail_lsn;
2421 * Check to make sure the grant write head didn't just over lap the
2422 * tail. If the cycles are the same, we can't be overlapping.
2423 * Otherwise, make sure that the cycles differ by exactly one and
2424 * check the byte count.
2426 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) != log->l_grant_write_cycle) {
2427 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn, ARCH_NOCONVERT));
2428 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn, ARCH_NOCONVERT)));
2430 #endif
2431 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2432 xlog_verify_grant_head(log, 1);
2433 GRANT_UNLOCK(log, s);
2434 return 0;
2436 error_return:
2437 if (tic->t_flags & XLOG_TIC_IN_Q)
2438 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2439 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2441 * If we are failing, make sure the ticket doesn't have any
2442 * current reservations. We don't want to add this back when
2443 * the ticket/transaction gets cancelled.
2445 tic->t_curr_res = 0;
2446 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2447 GRANT_UNLOCK(log, s);
2448 return XFS_ERROR(EIO);
2449 } /* xlog_grant_log_space */
2453 * Replenish the byte reservation required by moving the grant write head.
2457 STATIC int
2458 xlog_regrant_write_log_space(xlog_t *log,
2459 xlog_ticket_t *tic)
2461 SPLDECL(s);
2462 int free_bytes, need_bytes;
2463 xlog_ticket_t *ntic;
2464 #ifdef DEBUG
2465 xfs_lsn_t tail_lsn;
2466 #endif
2468 tic->t_curr_res = tic->t_unit_res;
2470 if (tic->t_cnt > 0)
2471 return (0);
2473 #ifdef DEBUG
2474 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2475 panic("regrant Recovery problem");
2476 #endif
2478 s = GRANT_LOCK(log);
2479 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2481 if (XLOG_FORCED_SHUTDOWN(log))
2482 goto error_return;
2484 /* If there are other waiters on the queue then give them a
2485 * chance at logspace before us. Wake up the first waiters,
2486 * if we do not wake up all the waiters then go to sleep waiting
2487 * for more free space, otherwise try to get some space for
2488 * this transaction.
2491 if ((ntic = log->l_write_headq)) {
2492 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2493 log->l_grant_write_bytes);
2494 do {
2495 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2497 if (free_bytes < ntic->t_unit_res)
2498 break;
2499 free_bytes -= ntic->t_unit_res;
2500 sv_signal(&ntic->t_sema);
2501 ntic = ntic->t_next;
2502 } while (ntic != log->l_write_headq);
2504 if (ntic != log->l_write_headq) {
2505 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2506 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2508 xlog_trace_loggrant(log, tic,
2509 "xlog_regrant_write_log_space: sleep 1");
2510 XFS_STATS_INC(xs_sleep_logspace);
2511 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2512 &log->l_grant_lock, s);
2514 /* If we're shutting down, this tic is already
2515 * off the queue */
2516 if (XLOG_FORCED_SHUTDOWN(log)) {
2517 s = GRANT_LOCK(log);
2518 goto error_return;
2521 xlog_trace_loggrant(log, tic,
2522 "xlog_regrant_write_log_space: wake 1");
2523 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2524 s = GRANT_LOCK(log);
2528 need_bytes = tic->t_unit_res;
2530 redo:
2531 if (XLOG_FORCED_SHUTDOWN(log))
2532 goto error_return;
2534 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2535 log->l_grant_write_bytes);
2536 if (free_bytes < need_bytes) {
2537 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2538 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2539 XFS_STATS_INC(xs_sleep_logspace);
2540 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2542 /* If we're shutting down, this tic is already off the queue */
2543 if (XLOG_FORCED_SHUTDOWN(log)) {
2544 s = GRANT_LOCK(log);
2545 goto error_return;
2548 xlog_trace_loggrant(log, tic,
2549 "xlog_regrant_write_log_space: wake 2");
2550 xlog_grant_push_ail(log->l_mp, need_bytes);
2551 s = GRANT_LOCK(log);
2552 goto redo;
2553 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2554 XLOG_DEL_TICKETQ(log->l_write_headq, tic);
2556 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w'); /* we've got enough space */
2557 #ifdef DEBUG
2558 tail_lsn = log->l_tail_lsn;
2559 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) != log->l_grant_write_cycle) {
2560 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn, ARCH_NOCONVERT));
2561 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn, ARCH_NOCONVERT)));
2563 #endif
2565 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2566 xlog_verify_grant_head(log, 1);
2567 GRANT_UNLOCK(log, s);
2568 return (0);
2571 error_return:
2572 if (tic->t_flags & XLOG_TIC_IN_Q)
2573 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2574 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2576 * If we are failing, make sure the ticket doesn't have any
2577 * current reservations. We don't want to add this back when
2578 * the ticket/transaction gets cancelled.
2580 tic->t_curr_res = 0;
2581 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2582 GRANT_UNLOCK(log, s);
2583 return XFS_ERROR(EIO);
2584 } /* xlog_regrant_write_log_space */
2587 /* The first cnt-1 times through here we don't need to
2588 * move the grant write head because the permanent
2589 * reservation has reserved cnt times the unit amount.
2590 * Release part of current permanent unit reservation and
2591 * reset current reservation to be one units worth. Also
2592 * move grant reservation head forward.
2594 STATIC void
2595 xlog_regrant_reserve_log_space(xlog_t *log,
2596 xlog_ticket_t *ticket)
2598 SPLDECL(s);
2600 xlog_trace_loggrant(log, ticket,
2601 "xlog_regrant_reserve_log_space: enter");
2602 if (ticket->t_cnt > 0)
2603 ticket->t_cnt--;
2605 s = GRANT_LOCK(log);
2606 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2607 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2608 ticket->t_curr_res = ticket->t_unit_res;
2609 xlog_trace_loggrant(log, ticket,
2610 "xlog_regrant_reserve_log_space: sub current res");
2611 xlog_verify_grant_head(log, 1);
2613 /* just return if we still have some of the pre-reserved space */
2614 if (ticket->t_cnt > 0) {
2615 GRANT_UNLOCK(log, s);
2616 return;
2619 XLOG_GRANT_ADD_SPACE(log, ticket->t_unit_res, 'r');
2620 xlog_trace_loggrant(log, ticket,
2621 "xlog_regrant_reserve_log_space: exit");
2622 xlog_verify_grant_head(log, 0);
2623 GRANT_UNLOCK(log, s);
2624 ticket->t_curr_res = ticket->t_unit_res;
2625 } /* xlog_regrant_reserve_log_space */
2629 * Give back the space left from a reservation.
2631 * All the information we need to make a correct determination of space left
2632 * is present. For non-permanent reservations, things are quite easy. The
2633 * count should have been decremented to zero. We only need to deal with the
2634 * space remaining in the current reservation part of the ticket. If the
2635 * ticket contains a permanent reservation, there may be left over space which
2636 * needs to be released. A count of N means that N-1 refills of the current
2637 * reservation can be done before we need to ask for more space. The first
2638 * one goes to fill up the first current reservation. Once we run out of
2639 * space, the count will stay at zero and the only space remaining will be
2640 * in the current reservation field.
2642 STATIC void
2643 xlog_ungrant_log_space(xlog_t *log,
2644 xlog_ticket_t *ticket)
2646 SPLDECL(s);
2648 if (ticket->t_cnt > 0)
2649 ticket->t_cnt--;
2651 s = GRANT_LOCK(log);
2652 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2654 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2655 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2657 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2659 /* If this is a permanent reservation ticket, we may be able to free
2660 * up more space based on the remaining count.
2662 if (ticket->t_cnt > 0) {
2663 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2664 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'w');
2665 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'r');
2668 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2669 xlog_verify_grant_head(log, 1);
2670 GRANT_UNLOCK(log, s);
2671 xfs_log_move_tail(log->l_mp, 1);
2672 } /* xlog_ungrant_log_space */
2676 * Atomically put back used ticket.
2678 void
2679 xlog_state_put_ticket(xlog_t *log,
2680 xlog_ticket_t *tic)
2682 unsigned long s;
2684 s = LOG_LOCK(log);
2685 xlog_ticket_put(log, tic);
2686 LOG_UNLOCK(log, s);
2687 } /* xlog_state_put_ticket */
2690 * Flush iclog to disk if this is the last reference to the given iclog and
2691 * the WANT_SYNC bit is set.
2693 * When this function is entered, the iclog is not necessarily in the
2694 * WANT_SYNC state. It may be sitting around waiting to get filled.
2699 xlog_state_release_iclog(xlog_t *log,
2700 xlog_in_core_t *iclog)
2702 SPLDECL(s);
2703 int sync = 0; /* do we sync? */
2705 xlog_assign_tail_lsn(log->l_mp);
2707 s = LOG_LOCK(log);
2709 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2710 LOG_UNLOCK(log, s);
2711 return XFS_ERROR(EIO);
2714 ASSERT(iclog->ic_refcnt > 0);
2715 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2716 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2718 if (--iclog->ic_refcnt == 0 &&
2719 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2720 sync++;
2721 iclog->ic_state = XLOG_STATE_SYNCING;
2722 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2723 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2724 /* cycle incremented when incrementing curr_block */
2727 LOG_UNLOCK(log, s);
2730 * We let the log lock go, so it's possible that we hit a log I/O
2731 * error or someother SHUTDOWN condition that marks the iclog
2732 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2733 * this iclog has consistent data, so we ignore IOERROR
2734 * flags after this point.
2736 if (sync) {
2737 return xlog_sync(log, iclog);
2739 return (0);
2741 } /* xlog_state_release_iclog */
2745 * This routine will mark the current iclog in the ring as WANT_SYNC
2746 * and move the current iclog pointer to the next iclog in the ring.
2747 * When this routine is called from xlog_state_get_iclog_space(), the
2748 * exact size of the iclog has not yet been determined. All we know is
2749 * that every data block. We have run out of space in this log record.
2751 STATIC void
2752 xlog_state_switch_iclogs(xlog_t *log,
2753 xlog_in_core_t *iclog,
2754 int eventual_size)
2756 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2757 if (!eventual_size)
2758 eventual_size = iclog->ic_offset;
2759 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2760 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2761 log->l_prev_block = log->l_curr_block;
2762 log->l_prev_cycle = log->l_curr_cycle;
2764 /* roll log?: ic_offset changed later */
2765 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2767 /* Round up to next log-sunit */
2768 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2769 log->l_mp->m_sb.sb_logsunit > 1) {
2770 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2771 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2774 if (log->l_curr_block >= log->l_logBBsize) {
2775 log->l_curr_cycle++;
2776 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2777 log->l_curr_cycle++;
2778 log->l_curr_block -= log->l_logBBsize;
2779 ASSERT(log->l_curr_block >= 0);
2781 ASSERT(iclog == log->l_iclog);
2782 log->l_iclog = iclog->ic_next;
2783 } /* xlog_state_switch_iclogs */
2787 * Write out all data in the in-core log as of this exact moment in time.
2789 * Data may be written to the in-core log during this call. However,
2790 * we don't guarantee this data will be written out. A change from past
2791 * implementation means this routine will *not* write out zero length LRs.
2793 * Basically, we try and perform an intelligent scan of the in-core logs.
2794 * If we determine there is no flushable data, we just return. There is no
2795 * flushable data if:
2797 * 1. the current iclog is active and has no data; the previous iclog
2798 * is in the active or dirty state.
2799 * 2. the current iclog is drity, and the previous iclog is in the
2800 * active or dirty state.
2802 * We may sleep (call psema) if:
2804 * 1. the current iclog is not in the active nor dirty state.
2805 * 2. the current iclog dirty, and the previous iclog is not in the
2806 * active nor dirty state.
2807 * 3. the current iclog is active, and there is another thread writing
2808 * to this particular iclog.
2809 * 4. a) the current iclog is active and has no other writers
2810 * b) when we return from flushing out this iclog, it is still
2811 * not in the active nor dirty state.
2813 STATIC int
2814 xlog_state_sync_all(xlog_t *log, uint flags)
2816 xlog_in_core_t *iclog;
2817 xfs_lsn_t lsn;
2818 SPLDECL(s);
2820 s = LOG_LOCK(log);
2822 iclog = log->l_iclog;
2823 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2824 LOG_UNLOCK(log, s);
2825 return XFS_ERROR(EIO);
2828 /* If the head iclog is not active nor dirty, we just attach
2829 * ourselves to the head and go to sleep.
2831 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2832 iclog->ic_state == XLOG_STATE_DIRTY) {
2834 * If the head is dirty or (active and empty), then
2835 * we need to look at the previous iclog. If the previous
2836 * iclog is active or dirty we are done. There is nothing
2837 * to sync out. Otherwise, we attach ourselves to the
2838 * previous iclog and go to sleep.
2840 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2841 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2842 iclog = iclog->ic_prev;
2843 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2844 iclog->ic_state == XLOG_STATE_DIRTY)
2845 goto no_sleep;
2846 else
2847 goto maybe_sleep;
2848 } else {
2849 if (iclog->ic_refcnt == 0) {
2850 /* We are the only one with access to this
2851 * iclog. Flush it out now. There should
2852 * be a roundoff of zero to show that someone
2853 * has already taken care of the roundoff from
2854 * the previous sync.
2856 ASSERT(iclog->ic_roundoff == 0);
2857 iclog->ic_refcnt++;
2858 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2859 xlog_state_switch_iclogs(log, iclog, 0);
2860 LOG_UNLOCK(log, s);
2862 if (xlog_state_release_iclog(log, iclog))
2863 return XFS_ERROR(EIO);
2864 s = LOG_LOCK(log);
2865 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2866 iclog->ic_state != XLOG_STATE_DIRTY)
2867 goto maybe_sleep;
2868 else
2869 goto no_sleep;
2870 } else {
2871 /* Someone else is writing to this iclog.
2872 * Use its call to flush out the data. However,
2873 * the other thread may not force out this LR,
2874 * so we mark it WANT_SYNC.
2876 xlog_state_switch_iclogs(log, iclog, 0);
2877 goto maybe_sleep;
2882 /* By the time we come around again, the iclog could've been filled
2883 * which would give it another lsn. If we have a new lsn, just
2884 * return because the relevant data has been flushed.
2886 maybe_sleep:
2887 if (flags & XFS_LOG_SYNC) {
2889 * We must check if we're shutting down here, before
2890 * we wait, while we're holding the LOG_LOCK.
2891 * Then we check again after waking up, in case our
2892 * sleep was disturbed by a bad news.
2894 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2895 LOG_UNLOCK(log, s);
2896 return XFS_ERROR(EIO);
2898 XFS_STATS_INC(xs_log_force_sleep);
2899 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
2901 * No need to grab the log lock here since we're
2902 * only deciding whether or not to return EIO
2903 * and the memory read should be atomic.
2905 if (iclog->ic_state & XLOG_STATE_IOERROR)
2906 return XFS_ERROR(EIO);
2908 } else {
2910 no_sleep:
2911 LOG_UNLOCK(log, s);
2913 return 0;
2914 } /* xlog_state_sync_all */
2918 * Used by code which implements synchronous log forces.
2920 * Find in-core log with lsn.
2921 * If it is in the DIRTY state, just return.
2922 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2923 * state and go to sleep or return.
2924 * If it is in any other state, go to sleep or return.
2926 * If filesystem activity goes to zero, the iclog will get flushed only by
2927 * bdflush().
2930 xlog_state_sync(xlog_t *log,
2931 xfs_lsn_t lsn,
2932 uint flags)
2934 xlog_in_core_t *iclog;
2935 int already_slept = 0;
2936 SPLDECL(s);
2939 try_again:
2940 s = LOG_LOCK(log);
2941 iclog = log->l_iclog;
2943 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2944 LOG_UNLOCK(log, s);
2945 return XFS_ERROR(EIO);
2948 do {
2949 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
2950 iclog = iclog->ic_next;
2951 continue;
2954 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2955 LOG_UNLOCK(log, s);
2956 return 0;
2959 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
2961 * We sleep here if we haven't already slept (e.g.
2962 * this is the first time we've looked at the correct
2963 * iclog buf) and the buffer before us is going to
2964 * be sync'ed. The reason for this is that if we
2965 * are doing sync transactions here, by waiting for
2966 * the previous I/O to complete, we can allow a few
2967 * more transactions into this iclog before we close
2968 * it down.
2970 * Otherwise, we mark the buffer WANT_SYNC, and bump
2971 * up the refcnt so we can release the log (which drops
2972 * the ref count). The state switch keeps new transaction
2973 * commits from using this buffer. When the current commits
2974 * finish writing into the buffer, the refcount will drop to
2975 * zero and the buffer will go out then.
2977 if (!already_slept &&
2978 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
2979 XLOG_STATE_SYNCING))) {
2980 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
2981 XFS_STATS_INC(xs_log_force_sleep);
2982 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
2983 &log->l_icloglock, s);
2984 already_slept = 1;
2985 goto try_again;
2986 } else {
2987 iclog->ic_refcnt++;
2988 xlog_state_switch_iclogs(log, iclog, 0);
2989 LOG_UNLOCK(log, s);
2990 if (xlog_state_release_iclog(log, iclog))
2991 return XFS_ERROR(EIO);
2992 s = LOG_LOCK(log);
2996 if ((flags & XFS_LOG_SYNC) && /* sleep */
2997 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3000 * Don't wait on the forcesema if we know that we've
3001 * gotten a log write error.
3003 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3004 LOG_UNLOCK(log, s);
3005 return XFS_ERROR(EIO);
3007 XFS_STATS_INC(xs_log_force_sleep);
3008 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3010 * No need to grab the log lock here since we're
3011 * only deciding whether or not to return EIO
3012 * and the memory read should be atomic.
3014 if (iclog->ic_state & XLOG_STATE_IOERROR)
3015 return XFS_ERROR(EIO);
3016 } else { /* just return */
3017 LOG_UNLOCK(log, s);
3019 return 0;
3021 } while (iclog != log->l_iclog);
3023 LOG_UNLOCK(log, s);
3024 return (0);
3025 } /* xlog_state_sync */
3029 * Called when we want to mark the current iclog as being ready to sync to
3030 * disk.
3032 void
3033 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3035 SPLDECL(s);
3037 s = LOG_LOCK(log);
3039 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3040 xlog_state_switch_iclogs(log, iclog, 0);
3041 } else {
3042 ASSERT(iclog->ic_state &
3043 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3046 LOG_UNLOCK(log, s);
3047 } /* xlog_state_want_sync */
3051 /*****************************************************************************
3053 * TICKET functions
3055 *****************************************************************************
3059 * Algorithm doesn't take into account page size. ;-(
3061 STATIC void
3062 xlog_state_ticket_alloc(xlog_t *log)
3064 xlog_ticket_t *t_list;
3065 xlog_ticket_t *next;
3066 xfs_caddr_t buf;
3067 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3068 SPLDECL(s);
3071 * The kmem_zalloc may sleep, so we shouldn't be holding the
3072 * global lock. XXXmiken: may want to use zone allocator.
3074 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3076 s = LOG_LOCK(log);
3078 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3079 t_list = (xlog_ticket_t *)buf;
3080 t_list->t_next = log->l_unmount_free;
3081 log->l_unmount_free = t_list++;
3082 log->l_ticket_cnt++;
3083 log->l_ticket_tcnt++;
3085 /* Next ticket becomes first ticket attached to ticket free list */
3086 if (log->l_freelist != NULL) {
3087 ASSERT(log->l_tail != NULL);
3088 log->l_tail->t_next = t_list;
3089 } else {
3090 log->l_freelist = t_list;
3092 log->l_ticket_cnt++;
3093 log->l_ticket_tcnt++;
3095 /* Cycle through rest of alloc'ed memory, building up free Q */
3096 for ( ; i > 0; i--) {
3097 next = t_list + 1;
3098 t_list->t_next = next;
3099 t_list = next;
3100 log->l_ticket_cnt++;
3101 log->l_ticket_tcnt++;
3103 t_list->t_next = NULL;
3104 log->l_tail = t_list;
3105 LOG_UNLOCK(log, s);
3106 } /* xlog_state_ticket_alloc */
3110 * Put ticket into free list
3112 * Assumption: log lock is held around this call.
3114 STATIC void
3115 xlog_ticket_put(xlog_t *log,
3116 xlog_ticket_t *ticket)
3118 sv_destroy(&ticket->t_sema);
3121 * Don't think caching will make that much difference. It's
3122 * more important to make debug easier.
3124 #if 0
3125 /* real code will want to use LIFO for caching */
3126 ticket->t_next = log->l_freelist;
3127 log->l_freelist = ticket;
3128 /* no need to clear fields */
3129 #else
3130 /* When we debug, it is easier if tickets are cycled */
3131 ticket->t_next = NULL;
3132 if (log->l_tail != 0) {
3133 log->l_tail->t_next = ticket;
3134 } else {
3135 ASSERT(log->l_freelist == 0);
3136 log->l_freelist = ticket;
3138 log->l_tail = ticket;
3139 #endif /* DEBUG */
3140 log->l_ticket_cnt++;
3141 } /* xlog_ticket_put */
3145 * Grab ticket off freelist or allocation some more
3147 xlog_ticket_t *
3148 xlog_ticket_get(xlog_t *log,
3149 int unit_bytes,
3150 int cnt,
3151 char client,
3152 uint xflags)
3154 xlog_ticket_t *tic;
3155 uint num_headers;
3156 SPLDECL(s);
3158 alloc:
3159 if (log->l_freelist == NULL)
3160 xlog_state_ticket_alloc(log); /* potentially sleep */
3162 s = LOG_LOCK(log);
3163 if (log->l_freelist == NULL) {
3164 LOG_UNLOCK(log, s);
3165 goto alloc;
3167 tic = log->l_freelist;
3168 log->l_freelist = tic->t_next;
3169 if (log->l_freelist == NULL)
3170 log->l_tail = NULL;
3171 log->l_ticket_cnt--;
3172 LOG_UNLOCK(log, s);
3175 * Permanent reservations have up to 'cnt'-1 active log operations
3176 * in the log. A unit in this case is the amount of space for one
3177 * of these log operations. Normal reservations have a cnt of 1
3178 * and their unit amount is the total amount of space required.
3180 * The following lines of code account for non-transaction data
3181 * which occupy space in the on-disk log.
3184 /* for start-rec */
3185 unit_bytes += sizeof(xlog_op_header_t);
3187 /* for padding */
3188 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3189 log->l_mp->m_sb.sb_logsunit > 1) {
3190 /* log su roundoff */
3191 unit_bytes += log->l_mp->m_sb.sb_logsunit;
3192 } else {
3193 /* BB roundoff */
3194 unit_bytes += BBSIZE;
3197 /* for commit-rec */
3198 unit_bytes += sizeof(xlog_op_header_t);
3200 /* for LR headers */
3201 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3202 unit_bytes += log->l_iclog_hsize * num_headers;
3204 tic->t_unit_res = unit_bytes;
3205 tic->t_curr_res = unit_bytes;
3206 tic->t_cnt = cnt;
3207 tic->t_ocnt = cnt;
3208 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3209 tic->t_clientid = client;
3210 tic->t_flags = XLOG_TIC_INITED;
3211 if (xflags & XFS_LOG_PERM_RESERV)
3212 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3213 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3215 return tic;
3216 } /* xlog_ticket_get */
3219 /******************************************************************************
3221 * Log debug routines
3223 ******************************************************************************
3225 #if defined(DEBUG) && !defined(XLOG_NOLOG)
3227 * Make sure that the destination ptr is within the valid data region of
3228 * one of the iclogs. This uses backup pointers stored in a different
3229 * part of the log in case we trash the log structure.
3231 void
3232 xlog_verify_dest_ptr(xlog_t *log,
3233 __psint_t ptr)
3235 int i;
3236 int good_ptr = 0;
3238 for (i=0; i < log->l_iclog_bufs; i++) {
3239 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3240 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3241 good_ptr++;
3243 if (! good_ptr)
3244 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3245 } /* xlog_verify_dest_ptr */
3247 STATIC void
3248 xlog_verify_grant_head(xlog_t *log, int equals)
3250 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3251 if (equals)
3252 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3253 else
3254 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3255 } else {
3256 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3257 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3259 } /* xlog_verify_grant_head */
3261 /* check if it will fit */
3262 STATIC void
3263 xlog_verify_tail_lsn(xlog_t *log,
3264 xlog_in_core_t *iclog,
3265 xfs_lsn_t tail_lsn)
3267 int blocks;
3269 if (CYCLE_LSN(tail_lsn, ARCH_NOCONVERT) == log->l_prev_cycle) {
3270 blocks =
3271 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn, ARCH_NOCONVERT));
3272 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3273 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3274 } else {
3275 ASSERT(CYCLE_LSN(tail_lsn, ARCH_NOCONVERT)+1 == log->l_prev_cycle);
3277 if (BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) == log->l_prev_block)
3278 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3280 blocks = BLOCK_LSN(tail_lsn, ARCH_NOCONVERT) - log->l_prev_block;
3281 if (blocks < BTOBB(iclog->ic_offset) + 1)
3282 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3284 } /* xlog_verify_tail_lsn */
3287 * Perform a number of checks on the iclog before writing to disk.
3289 * 1. Make sure the iclogs are still circular
3290 * 2. Make sure we have a good magic number
3291 * 3. Make sure we don't have magic numbers in the data
3292 * 4. Check fields of each log operation header for:
3293 * A. Valid client identifier
3294 * B. tid ptr value falls in valid ptr space (user space code)
3295 * C. Length in log record header is correct according to the
3296 * individual operation headers within record.
3297 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3298 * log, check the preceding blocks of the physical log to make sure all
3299 * the cycle numbers agree with the current cycle number.
3301 STATIC void
3302 xlog_verify_iclog(xlog_t *log,
3303 xlog_in_core_t *iclog,
3304 int count,
3305 boolean_t syncing)
3307 xlog_op_header_t *ophead;
3308 xlog_in_core_t *icptr;
3309 xlog_in_core_2_t *xhdr;
3310 xfs_caddr_t ptr;
3311 xfs_caddr_t base_ptr;
3312 __psint_t field_offset;
3313 __uint8_t clientid;
3314 int len, i, j, k, op_len;
3315 int idx;
3316 SPLDECL(s);
3318 /* check validity of iclog pointers */
3319 s = LOG_LOCK(log);
3320 icptr = log->l_iclog;
3321 for (i=0; i < log->l_iclog_bufs; i++) {
3322 if (icptr == 0)
3323 xlog_panic("xlog_verify_iclog: invalid ptr");
3324 icptr = icptr->ic_next;
3326 if (icptr != log->l_iclog)
3327 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3328 LOG_UNLOCK(log, s);
3330 /* check log magic numbers */
3331 ptr = (xfs_caddr_t) &(iclog->ic_header);
3332 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3333 xlog_panic("xlog_verify_iclog: invalid magic num");
3335 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3336 ptr += BBSIZE) {
3337 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3338 xlog_panic("xlog_verify_iclog: unexpected magic num");
3341 /* check fields */
3342 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3343 ptr = iclog->ic_datap;
3344 base_ptr = ptr;
3345 ophead = (xlog_op_header_t *)ptr;
3346 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3347 for (i = 0; i < len; i++) {
3348 ophead = (xlog_op_header_t *)ptr;
3350 /* clientid is only 1 byte */
3351 field_offset = (__psint_t)
3352 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3353 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3354 clientid = ophead->oh_clientid;
3355 } else {
3356 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3357 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3358 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3359 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3360 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3361 } else {
3362 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3365 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3366 cmn_err(CE_WARN, "xlog_verify_iclog: invalid clientid %d op 0x%p offset 0x%x", clientid, ophead, field_offset);
3368 /* check length */
3369 field_offset = (__psint_t)
3370 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3371 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3372 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3373 } else {
3374 idx = BTOBBT((__psint_t)&ophead->oh_len -
3375 (__psint_t)iclog->ic_datap);
3376 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3377 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3378 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3379 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3380 } else {
3381 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3384 ptr += sizeof(xlog_op_header_t) + op_len;
3386 } /* xlog_verify_iclog */
3387 #endif /* DEBUG && !XLOG_NOLOG */
3390 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3392 STATIC int
3393 xlog_state_ioerror(
3394 xlog_t *log)
3396 xlog_in_core_t *iclog, *ic;
3398 iclog = log->l_iclog;
3399 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3401 * Mark all the incore logs IOERROR.
3402 * From now on, no log flushes will result.
3404 ic = iclog;
3405 do {
3406 ic->ic_state = XLOG_STATE_IOERROR;
3407 ic = ic->ic_next;
3408 } while (ic != iclog);
3409 return (0);
3412 * Return non-zero, if state transition has already happened.
3414 return (1);
3418 * This is called from xfs_force_shutdown, when we're forcibly
3419 * shutting down the filesystem, typically because of an IO error.
3420 * Our main objectives here are to make sure that:
3421 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3422 * parties to find out, 'atomically'.
3423 * b. those who're sleeping on log reservations, pinned objects and
3424 * other resources get woken up, and be told the bad news.
3425 * c. nothing new gets queued up after (a) and (b) are done.
3426 * d. if !logerror, flush the iclogs to disk, then seal them off
3427 * for business.
3430 xfs_log_force_umount(
3431 struct xfs_mount *mp,
3432 int logerror)
3434 xlog_ticket_t *tic;
3435 xlog_t *log;
3436 int retval;
3437 SPLDECL(s);
3438 SPLDECL(s2);
3440 log = mp->m_log;
3443 * If this happens during log recovery, don't worry about
3444 * locking; the log isn't open for business yet.
3446 if (!log ||
3447 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3448 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3449 XFS_BUF_DONE(mp->m_sb_bp);
3450 return (0);
3454 * Somebody could've already done the hard work for us.
3455 * No need to get locks for this.
3457 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3458 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3459 return (1);
3461 retval = 0;
3463 * We must hold both the GRANT lock and the LOG lock,
3464 * before we mark the filesystem SHUTDOWN and wake
3465 * everybody up to tell the bad news.
3467 s = GRANT_LOCK(log);
3468 s2 = LOG_LOCK(log);
3469 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3470 XFS_BUF_DONE(mp->m_sb_bp);
3472 * This flag is sort of redundant because of the mount flag, but
3473 * it's good to maintain the separation between the log and the rest
3474 * of XFS.
3476 log->l_flags |= XLOG_IO_ERROR;
3479 * If we hit a log error, we want to mark all the iclogs IOERROR
3480 * while we're still holding the loglock.
3482 if (logerror)
3483 retval = xlog_state_ioerror(log);
3484 LOG_UNLOCK(log, s2);
3487 * We don't want anybody waiting for log reservations
3488 * after this. That means we have to wake up everybody
3489 * queued up on reserve_headq as well as write_headq.
3490 * In addition, we make sure in xlog_{re}grant_log_space
3491 * that we don't enqueue anything once the SHUTDOWN flag
3492 * is set, and this action is protected by the GRANTLOCK.
3494 if ((tic = log->l_reserve_headq)) {
3495 do {
3496 sv_signal(&tic->t_sema);
3497 tic = tic->t_next;
3498 } while (tic != log->l_reserve_headq);
3501 if ((tic = log->l_write_headq)) {
3502 do {
3503 sv_signal(&tic->t_sema);
3504 tic = tic->t_next;
3505 } while (tic != log->l_write_headq);
3507 GRANT_UNLOCK(log, s);
3509 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3510 ASSERT(!logerror);
3512 * Force the incore logs to disk before shutting the
3513 * log down completely.
3515 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC);
3516 s2 = LOG_LOCK(log);
3517 retval = xlog_state_ioerror(log);
3518 LOG_UNLOCK(log, s2);
3521 * Wake up everybody waiting on xfs_log_force.
3522 * Callback all log item committed functions as if the
3523 * log writes were completed.
3525 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3527 #ifdef XFSERRORDEBUG
3529 xlog_in_core_t *iclog;
3531 s = LOG_LOCK(log);
3532 iclog = log->l_iclog;
3533 do {
3534 ASSERT(iclog->ic_callback == 0);
3535 iclog = iclog->ic_next;
3536 } while (iclog != log->l_iclog);
3537 LOG_UNLOCK(log, s);
3539 #endif
3540 /* return non-zero if log IOERROR transition had already happened */
3541 return (retval);
3545 xlog_iclogs_empty(xlog_t *log)
3547 xlog_in_core_t *iclog;
3549 iclog = log->l_iclog;
3550 do {
3551 /* endianness does not matter here, zero is zero in
3552 * any language.
3554 if (iclog->ic_header.h_num_logops)
3555 return(0);
3556 iclog = iclog->ic_next;
3557 } while (iclog != log->l_iclog);
3558 return(1);