fs: allow short direct-io reads to be completed via buffered IO
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_log.c
blob2be019136287666ae84e565093924fe0ba8e76a8
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_error.h"
31 #include "xfs_log_priv.h"
32 #include "xfs_buf_item.h"
33 #include "xfs_bmap_btree.h"
34 #include "xfs_alloc_btree.h"
35 #include "xfs_ialloc_btree.h"
36 #include "xfs_log_recover.h"
37 #include "xfs_trans_priv.h"
38 #include "xfs_dir2_sf.h"
39 #include "xfs_attr_sf.h"
40 #include "xfs_dinode.h"
41 #include "xfs_inode.h"
42 #include "xfs_rw.h"
43 #include "xfs_trace.h"
45 kmem_zone_t *xfs_log_ticket_zone;
47 #define xlog_write_adv_cnt(ptr, len, off, bytes) \
48 { (ptr) += (bytes); \
49 (len) -= (bytes); \
50 (off) += (bytes);}
52 /* Local miscellaneous function prototypes */
53 STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54 xlog_in_core_t **, xfs_lsn_t *);
55 STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
56 xfs_buftarg_t *log_target,
57 xfs_daddr_t blk_offset,
58 int num_bblks);
59 STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
60 STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
61 STATIC void xlog_dealloc_log(xlog_t *log);
62 STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
63 int nentries, struct xlog_ticket *tic,
64 xfs_lsn_t *start_lsn,
65 xlog_in_core_t **commit_iclog,
66 uint flags);
68 /* local state machine functions */
69 STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70 STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71 STATIC int xlog_state_get_iclog_space(xlog_t *log,
72 int len,
73 xlog_in_core_t **iclog,
74 xlog_ticket_t *ticket,
75 int *continued_write,
76 int *logoffsetp);
77 STATIC int xlog_state_release_iclog(xlog_t *log,
78 xlog_in_core_t *iclog);
79 STATIC void xlog_state_switch_iclogs(xlog_t *log,
80 xlog_in_core_t *iclog,
81 int eventual_size);
82 STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
84 /* local functions to manipulate grant head */
85 STATIC int xlog_grant_log_space(xlog_t *log,
86 xlog_ticket_t *xtic);
87 STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
88 int need_bytes);
89 STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
90 xlog_ticket_t *ticket);
91 STATIC int xlog_regrant_write_log_space(xlog_t *log,
92 xlog_ticket_t *ticket);
93 STATIC void xlog_ungrant_log_space(xlog_t *log,
94 xlog_ticket_t *ticket);
97 /* local ticket functions */
98 STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log,
99 int unit_bytes,
100 int count,
101 char clientid,
102 uint flags);
104 #if defined(DEBUG)
105 STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
106 STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
107 STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
108 int count, boolean_t syncing);
109 STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
110 xfs_lsn_t tail_lsn);
111 #else
112 #define xlog_verify_dest_ptr(a,b)
113 #define xlog_verify_grant_head(a,b)
114 #define xlog_verify_iclog(a,b,c,d)
115 #define xlog_verify_tail_lsn(a,b,c)
116 #endif
118 STATIC int xlog_iclogs_empty(xlog_t *log);
121 static void
122 xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
124 if (*qp) {
125 tic->t_next = (*qp);
126 tic->t_prev = (*qp)->t_prev;
127 (*qp)->t_prev->t_next = tic;
128 (*qp)->t_prev = tic;
129 } else {
130 tic->t_prev = tic->t_next = tic;
131 *qp = tic;
134 tic->t_flags |= XLOG_TIC_IN_Q;
137 static void
138 xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
140 if (tic == tic->t_next) {
141 *qp = NULL;
142 } else {
143 *qp = tic->t_next;
144 tic->t_next->t_prev = tic->t_prev;
145 tic->t_prev->t_next = tic->t_next;
148 tic->t_next = tic->t_prev = NULL;
149 tic->t_flags &= ~XLOG_TIC_IN_Q;
152 static void
153 xlog_grant_sub_space(struct log *log, int bytes)
155 log->l_grant_write_bytes -= bytes;
156 if (log->l_grant_write_bytes < 0) {
157 log->l_grant_write_bytes += log->l_logsize;
158 log->l_grant_write_cycle--;
161 log->l_grant_reserve_bytes -= bytes;
162 if ((log)->l_grant_reserve_bytes < 0) {
163 log->l_grant_reserve_bytes += log->l_logsize;
164 log->l_grant_reserve_cycle--;
169 static void
170 xlog_grant_add_space_write(struct log *log, int bytes)
172 int tmp = log->l_logsize - log->l_grant_write_bytes;
173 if (tmp > bytes)
174 log->l_grant_write_bytes += bytes;
175 else {
176 log->l_grant_write_cycle++;
177 log->l_grant_write_bytes = bytes - tmp;
181 static void
182 xlog_grant_add_space_reserve(struct log *log, int bytes)
184 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
185 if (tmp > bytes)
186 log->l_grant_reserve_bytes += bytes;
187 else {
188 log->l_grant_reserve_cycle++;
189 log->l_grant_reserve_bytes = bytes - tmp;
193 static inline void
194 xlog_grant_add_space(struct log *log, int bytes)
196 xlog_grant_add_space_write(log, bytes);
197 xlog_grant_add_space_reserve(log, bytes);
200 static void
201 xlog_tic_reset_res(xlog_ticket_t *tic)
203 tic->t_res_num = 0;
204 tic->t_res_arr_sum = 0;
205 tic->t_res_num_ophdrs = 0;
208 static void
209 xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
211 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
212 /* add to overflow and start again */
213 tic->t_res_o_flow += tic->t_res_arr_sum;
214 tic->t_res_num = 0;
215 tic->t_res_arr_sum = 0;
218 tic->t_res_arr[tic->t_res_num].r_len = len;
219 tic->t_res_arr[tic->t_res_num].r_type = type;
220 tic->t_res_arr_sum += len;
221 tic->t_res_num++;
225 * NOTES:
227 * 1. currblock field gets updated at startup and after in-core logs
228 * marked as with WANT_SYNC.
232 * This routine is called when a user of a log manager ticket is done with
233 * the reservation. If the ticket was ever used, then a commit record for
234 * the associated transaction is written out as a log operation header with
235 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
236 * a given ticket. If the ticket was one with a permanent reservation, then
237 * a few operations are done differently. Permanent reservation tickets by
238 * default don't release the reservation. They just commit the current
239 * transaction with the belief that the reservation is still needed. A flag
240 * must be passed in before permanent reservations are actually released.
241 * When these type of tickets are not released, they need to be set into
242 * the inited state again. By doing this, a start record will be written
243 * out when the next write occurs.
245 xfs_lsn_t
246 xfs_log_done(
247 struct xfs_mount *mp,
248 struct xlog_ticket *ticket,
249 struct xlog_in_core **iclog,
250 uint flags)
252 struct log *log = mp->m_log;
253 xfs_lsn_t lsn = 0;
255 if (XLOG_FORCED_SHUTDOWN(log) ||
257 * If nothing was ever written, don't write out commit record.
258 * If we get an error, just continue and give back the log ticket.
260 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
261 (xlog_commit_record(mp, ticket, iclog, &lsn)))) {
262 lsn = (xfs_lsn_t) -1;
263 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
264 flags |= XFS_LOG_REL_PERM_RESERV;
269 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
270 (flags & XFS_LOG_REL_PERM_RESERV)) {
271 trace_xfs_log_done_nonperm(log, ticket);
274 * Release ticket if not permanent reservation or a specific
275 * request has been made to release a permanent reservation.
277 xlog_ungrant_log_space(log, ticket);
278 xfs_log_ticket_put(ticket);
279 } else {
280 trace_xfs_log_done_perm(log, ticket);
282 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 ticket->t_flags |= XLOG_TIC_INITED;
290 return lsn;
294 * Attaches a new iclog I/O completion callback routine during
295 * transaction commit. If the log is in error state, a non-zero
296 * return code is handed back and the caller is responsible for
297 * executing the callback at an appropriate time.
300 xfs_log_notify(
301 struct xfs_mount *mp,
302 struct xlog_in_core *iclog,
303 xfs_log_callback_t *cb)
305 int abortflg;
307 spin_lock(&iclog->ic_callback_lock);
308 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
309 if (!abortflg) {
310 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
311 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
312 cb->cb_next = NULL;
313 *(iclog->ic_callback_tail) = cb;
314 iclog->ic_callback_tail = &(cb->cb_next);
316 spin_unlock(&iclog->ic_callback_lock);
317 return abortflg;
321 xfs_log_release_iclog(
322 struct xfs_mount *mp,
323 struct xlog_in_core *iclog)
325 if (xlog_state_release_iclog(mp->m_log, iclog)) {
326 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
327 return EIO;
330 return 0;
334 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
335 * to the reservation.
336 * 2. Potentially, push buffers at tail of log to disk.
338 * Each reservation is going to reserve extra space for a log record header.
339 * When writes happen to the on-disk log, we don't subtract the length of the
340 * log record header from any reservation. By wasting space in each
341 * reservation, we prevent over allocation problems.
344 xfs_log_reserve(
345 struct xfs_mount *mp,
346 int unit_bytes,
347 int cnt,
348 struct xlog_ticket **ticket,
349 __uint8_t client,
350 uint flags,
351 uint t_type)
353 struct log *log = mp->m_log;
354 struct xlog_ticket *internal_ticket;
355 int retval = 0;
357 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
358 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
360 if (XLOG_FORCED_SHUTDOWN(log))
361 return XFS_ERROR(EIO);
363 XFS_STATS_INC(xs_try_logspace);
366 if (*ticket != NULL) {
367 ASSERT(flags & XFS_LOG_PERM_RESERV);
368 internal_ticket = *ticket;
370 trace_xfs_log_reserve(log, internal_ticket);
372 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
373 retval = xlog_regrant_write_log_space(log, internal_ticket);
374 } else {
375 /* may sleep if need to allocate more tickets */
376 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
377 client, flags);
378 if (!internal_ticket)
379 return XFS_ERROR(ENOMEM);
380 internal_ticket->t_trans_type = t_type;
381 *ticket = internal_ticket;
383 trace_xfs_log_reserve(log, internal_ticket);
385 xlog_grant_push_ail(mp,
386 (internal_ticket->t_unit_res *
387 internal_ticket->t_cnt));
388 retval = xlog_grant_log_space(log, internal_ticket);
391 return retval;
392 } /* xfs_log_reserve */
396 * Mount a log filesystem
398 * mp - ubiquitous xfs mount point structure
399 * log_target - buftarg of on-disk log device
400 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
401 * num_bblocks - Number of BBSIZE blocks in on-disk log
403 * Return error or zero.
406 xfs_log_mount(
407 xfs_mount_t *mp,
408 xfs_buftarg_t *log_target,
409 xfs_daddr_t blk_offset,
410 int num_bblks)
412 int error;
414 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
415 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
416 else {
417 cmn_err(CE_NOTE,
418 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
419 mp->m_fsname);
420 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
423 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
424 if (IS_ERR(mp->m_log)) {
425 error = -PTR_ERR(mp->m_log);
426 goto out;
430 * Initialize the AIL now we have a log.
432 error = xfs_trans_ail_init(mp);
433 if (error) {
434 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
435 goto out_free_log;
437 mp->m_log->l_ailp = mp->m_ail;
440 * skip log recovery on a norecovery mount. pretend it all
441 * just worked.
443 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
444 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
446 if (readonly)
447 mp->m_flags &= ~XFS_MOUNT_RDONLY;
449 error = xlog_recover(mp->m_log);
451 if (readonly)
452 mp->m_flags |= XFS_MOUNT_RDONLY;
453 if (error) {
454 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
455 goto out_destroy_ail;
459 /* Normal transactions can now occur */
460 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
462 return 0;
464 out_destroy_ail:
465 xfs_trans_ail_destroy(mp);
466 out_free_log:
467 xlog_dealloc_log(mp->m_log);
468 out:
469 return error;
473 * Finish the recovery of the file system. This is separate from
474 * the xfs_log_mount() call, because it depends on the code in
475 * xfs_mountfs() to read in the root and real-time bitmap inodes
476 * between calling xfs_log_mount() and here.
478 * mp - ubiquitous xfs mount point structure
481 xfs_log_mount_finish(xfs_mount_t *mp)
483 int error;
485 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
486 error = xlog_recover_finish(mp->m_log);
487 else {
488 error = 0;
489 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
492 return error;
496 * Final log writes as part of unmount.
498 * Mark the filesystem clean as unmount happens. Note that during relocation
499 * this routine needs to be executed as part of source-bag while the
500 * deallocation must not be done until source-end.
504 * Unmount record used to have a string "Unmount filesystem--" in the
505 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
506 * We just write the magic number now since that particular field isn't
507 * currently architecture converted and "nUmount" is a bit foo.
508 * As far as I know, there weren't any dependencies on the old behaviour.
512 xfs_log_unmount_write(xfs_mount_t *mp)
514 xlog_t *log = mp->m_log;
515 xlog_in_core_t *iclog;
516 #ifdef DEBUG
517 xlog_in_core_t *first_iclog;
518 #endif
519 xfs_log_iovec_t reg[1];
520 xlog_ticket_t *tic = NULL;
521 xfs_lsn_t lsn;
522 int error;
524 /* the data section must be 32 bit size aligned */
525 struct {
526 __uint16_t magic;
527 __uint16_t pad1;
528 __uint32_t pad2; /* may as well make it 64 bits */
529 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
532 * Don't write out unmount record on read-only mounts.
533 * Or, if we are doing a forced umount (typically because of IO errors).
535 if (mp->m_flags & XFS_MOUNT_RDONLY)
536 return 0;
538 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
539 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
541 #ifdef DEBUG
542 first_iclog = iclog = log->l_iclog;
543 do {
544 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
545 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
546 ASSERT(iclog->ic_offset == 0);
548 iclog = iclog->ic_next;
549 } while (iclog != first_iclog);
550 #endif
551 if (! (XLOG_FORCED_SHUTDOWN(log))) {
552 reg[0].i_addr = (void*)&magic;
553 reg[0].i_len = sizeof(magic);
554 reg[0].i_type = XLOG_REG_TYPE_UNMOUNT;
556 error = xfs_log_reserve(mp, 600, 1, &tic,
557 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
558 if (!error) {
559 /* remove inited flag */
560 ((xlog_ticket_t *)tic)->t_flags = 0;
561 error = xlog_write(mp, reg, 1, tic, &lsn,
562 NULL, XLOG_UNMOUNT_TRANS);
564 * At this point, we're umounting anyway,
565 * so there's no point in transitioning log state
566 * to IOERROR. Just continue...
570 if (error) {
571 xfs_fs_cmn_err(CE_ALERT, mp,
572 "xfs_log_unmount: unmount record failed");
576 spin_lock(&log->l_icloglock);
577 iclog = log->l_iclog;
578 atomic_inc(&iclog->ic_refcnt);
579 xlog_state_want_sync(log, iclog);
580 spin_unlock(&log->l_icloglock);
581 error = xlog_state_release_iclog(log, iclog);
583 spin_lock(&log->l_icloglock);
584 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
585 iclog->ic_state == XLOG_STATE_DIRTY)) {
586 if (!XLOG_FORCED_SHUTDOWN(log)) {
587 sv_wait(&iclog->ic_force_wait, PMEM,
588 &log->l_icloglock, s);
589 } else {
590 spin_unlock(&log->l_icloglock);
592 } else {
593 spin_unlock(&log->l_icloglock);
595 if (tic) {
596 trace_xfs_log_umount_write(log, tic);
597 xlog_ungrant_log_space(log, tic);
598 xfs_log_ticket_put(tic);
600 } else {
602 * We're already in forced_shutdown mode, couldn't
603 * even attempt to write out the unmount transaction.
605 * Go through the motions of sync'ing and releasing
606 * the iclog, even though no I/O will actually happen,
607 * we need to wait for other log I/Os that may already
608 * be in progress. Do this as a separate section of
609 * code so we'll know if we ever get stuck here that
610 * we're in this odd situation of trying to unmount
611 * a file system that went into forced_shutdown as
612 * the result of an unmount..
614 spin_lock(&log->l_icloglock);
615 iclog = log->l_iclog;
616 atomic_inc(&iclog->ic_refcnt);
618 xlog_state_want_sync(log, iclog);
619 spin_unlock(&log->l_icloglock);
620 error = xlog_state_release_iclog(log, iclog);
622 spin_lock(&log->l_icloglock);
624 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
625 || iclog->ic_state == XLOG_STATE_DIRTY
626 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
628 sv_wait(&iclog->ic_force_wait, PMEM,
629 &log->l_icloglock, s);
630 } else {
631 spin_unlock(&log->l_icloglock);
635 return error;
636 } /* xfs_log_unmount_write */
639 * Deallocate log structures for unmount/relocation.
641 * We need to stop the aild from running before we destroy
642 * and deallocate the log as the aild references the log.
644 void
645 xfs_log_unmount(xfs_mount_t *mp)
647 xfs_trans_ail_destroy(mp);
648 xlog_dealloc_log(mp->m_log);
652 * Write region vectors to log. The write happens using the space reservation
653 * of the ticket (tic). It is not a requirement that all writes for a given
654 * transaction occur with one call to xfs_log_write().
657 xfs_log_write(
658 struct xfs_mount *mp,
659 struct xfs_log_iovec reg[],
660 int nentries,
661 struct xlog_ticket *tic,
662 xfs_lsn_t *start_lsn)
664 struct log *log = mp->m_log;
665 int error;
667 if (XLOG_FORCED_SHUTDOWN(log))
668 return XFS_ERROR(EIO);
670 error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0);
671 if (error)
672 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
673 return error;
676 void
677 xfs_log_move_tail(xfs_mount_t *mp,
678 xfs_lsn_t tail_lsn)
680 xlog_ticket_t *tic;
681 xlog_t *log = mp->m_log;
682 int need_bytes, free_bytes, cycle, bytes;
684 if (XLOG_FORCED_SHUTDOWN(log))
685 return;
687 if (tail_lsn == 0) {
688 /* needed since sync_lsn is 64 bits */
689 spin_lock(&log->l_icloglock);
690 tail_lsn = log->l_last_sync_lsn;
691 spin_unlock(&log->l_icloglock);
694 spin_lock(&log->l_grant_lock);
696 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
697 * tail_lsn.
699 if (tail_lsn != 1) {
700 log->l_tail_lsn = tail_lsn;
703 if ((tic = log->l_write_headq)) {
704 #ifdef DEBUG
705 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
706 panic("Recovery problem");
707 #endif
708 cycle = log->l_grant_write_cycle;
709 bytes = log->l_grant_write_bytes;
710 free_bytes = xlog_space_left(log, cycle, bytes);
711 do {
712 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
714 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
715 break;
716 tail_lsn = 0;
717 free_bytes -= tic->t_unit_res;
718 sv_signal(&tic->t_wait);
719 tic = tic->t_next;
720 } while (tic != log->l_write_headq);
722 if ((tic = log->l_reserve_headq)) {
723 #ifdef DEBUG
724 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
725 panic("Recovery problem");
726 #endif
727 cycle = log->l_grant_reserve_cycle;
728 bytes = log->l_grant_reserve_bytes;
729 free_bytes = xlog_space_left(log, cycle, bytes);
730 do {
731 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
732 need_bytes = tic->t_unit_res*tic->t_cnt;
733 else
734 need_bytes = tic->t_unit_res;
735 if (free_bytes < need_bytes && tail_lsn != 1)
736 break;
737 tail_lsn = 0;
738 free_bytes -= need_bytes;
739 sv_signal(&tic->t_wait);
740 tic = tic->t_next;
741 } while (tic != log->l_reserve_headq);
743 spin_unlock(&log->l_grant_lock);
744 } /* xfs_log_move_tail */
747 * Determine if we have a transaction that has gone to disk
748 * that needs to be covered. To begin the transition to the idle state
749 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
750 * If we are then in a state where covering is needed, the caller is informed
751 * that dummy transactions are required to move the log into the idle state.
753 * Because this is called as part of the sync process, we should also indicate
754 * that dummy transactions should be issued in anything but the covered or
755 * idle states. This ensures that the log tail is accurately reflected in
756 * the log at the end of the sync, hence if a crash occurrs avoids replay
757 * of transactions where the metadata is already on disk.
760 xfs_log_need_covered(xfs_mount_t *mp)
762 int needed = 0;
763 xlog_t *log = mp->m_log;
765 if (!xfs_fs_writable(mp))
766 return 0;
768 spin_lock(&log->l_icloglock);
769 switch (log->l_covered_state) {
770 case XLOG_STATE_COVER_DONE:
771 case XLOG_STATE_COVER_DONE2:
772 case XLOG_STATE_COVER_IDLE:
773 break;
774 case XLOG_STATE_COVER_NEED:
775 case XLOG_STATE_COVER_NEED2:
776 if (!xfs_trans_ail_tail(log->l_ailp) &&
777 xlog_iclogs_empty(log)) {
778 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
779 log->l_covered_state = XLOG_STATE_COVER_DONE;
780 else
781 log->l_covered_state = XLOG_STATE_COVER_DONE2;
783 /* FALLTHRU */
784 default:
785 needed = 1;
786 break;
788 spin_unlock(&log->l_icloglock);
789 return needed;
792 /******************************************************************************
794 * local routines
796 ******************************************************************************
799 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
800 * The log manager must keep track of the last LR which was committed
801 * to disk. The lsn of this LR will become the new tail_lsn whenever
802 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
803 * the situation where stuff could be written into the log but nothing
804 * was ever in the AIL when asked. Eventually, we panic since the
805 * tail hits the head.
807 * We may be holding the log iclog lock upon entering this routine.
809 xfs_lsn_t
810 xlog_assign_tail_lsn(xfs_mount_t *mp)
812 xfs_lsn_t tail_lsn;
813 xlog_t *log = mp->m_log;
815 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
816 spin_lock(&log->l_grant_lock);
817 if (tail_lsn != 0) {
818 log->l_tail_lsn = tail_lsn;
819 } else {
820 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
822 spin_unlock(&log->l_grant_lock);
824 return tail_lsn;
825 } /* xlog_assign_tail_lsn */
829 * Return the space in the log between the tail and the head. The head
830 * is passed in the cycle/bytes formal parms. In the special case where
831 * the reserve head has wrapped passed the tail, this calculation is no
832 * longer valid. In this case, just return 0 which means there is no space
833 * in the log. This works for all places where this function is called
834 * with the reserve head. Of course, if the write head were to ever
835 * wrap the tail, we should blow up. Rather than catch this case here,
836 * we depend on other ASSERTions in other parts of the code. XXXmiken
838 * This code also handles the case where the reservation head is behind
839 * the tail. The details of this case are described below, but the end
840 * result is that we return the size of the log as the amount of space left.
842 STATIC int
843 xlog_space_left(xlog_t *log, int cycle, int bytes)
845 int free_bytes;
846 int tail_bytes;
847 int tail_cycle;
849 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
850 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
851 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
852 free_bytes = log->l_logsize - (bytes - tail_bytes);
853 } else if ((tail_cycle + 1) < cycle) {
854 return 0;
855 } else if (tail_cycle < cycle) {
856 ASSERT(tail_cycle == (cycle - 1));
857 free_bytes = tail_bytes - bytes;
858 } else {
860 * The reservation head is behind the tail.
861 * In this case we just want to return the size of the
862 * log as the amount of space left.
864 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
865 "xlog_space_left: head behind tail\n"
866 " tail_cycle = %d, tail_bytes = %d\n"
867 " GH cycle = %d, GH bytes = %d",
868 tail_cycle, tail_bytes, cycle, bytes);
869 ASSERT(0);
870 free_bytes = log->l_logsize;
872 return free_bytes;
873 } /* xlog_space_left */
877 * Log function which is called when an io completes.
879 * The log manager needs its own routine, in order to control what
880 * happens with the buffer after the write completes.
882 void
883 xlog_iodone(xfs_buf_t *bp)
885 xlog_in_core_t *iclog;
886 xlog_t *l;
887 int aborted;
889 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
890 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
891 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
892 aborted = 0;
893 l = iclog->ic_log;
896 * If the _XFS_BARRIER_FAILED flag was set by a lower
897 * layer, it means the underlying device no longer supports
898 * barrier I/O. Warn loudly and turn off barriers.
900 if (bp->b_flags & _XFS_BARRIER_FAILED) {
901 bp->b_flags &= ~_XFS_BARRIER_FAILED;
902 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
903 xfs_fs_cmn_err(CE_WARN, l->l_mp,
904 "xlog_iodone: Barriers are no longer supported"
905 " by device. Disabling barriers\n");
909 * Race to shutdown the filesystem if we see an error.
911 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
912 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
913 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
914 XFS_BUF_STALE(bp);
915 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
917 * This flag will be propagated to the trans-committed
918 * callback routines to let them know that the log-commit
919 * didn't succeed.
921 aborted = XFS_LI_ABORTED;
922 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
923 aborted = XFS_LI_ABORTED;
926 /* log I/O is always issued ASYNC */
927 ASSERT(XFS_BUF_ISASYNC(bp));
928 xlog_state_done_syncing(iclog, aborted);
930 * do not reference the buffer (bp) here as we could race
931 * with it being freed after writing the unmount record to the
932 * log.
935 } /* xlog_iodone */
938 * Return size of each in-core log record buffer.
940 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
942 * If the filesystem blocksize is too large, we may need to choose a
943 * larger size since the directory code currently logs entire blocks.
946 STATIC void
947 xlog_get_iclog_buffer_size(xfs_mount_t *mp,
948 xlog_t *log)
950 int size;
951 int xhdrs;
953 if (mp->m_logbufs <= 0)
954 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
955 else
956 log->l_iclog_bufs = mp->m_logbufs;
959 * Buffer size passed in from mount system call.
961 if (mp->m_logbsize > 0) {
962 size = log->l_iclog_size = mp->m_logbsize;
963 log->l_iclog_size_log = 0;
964 while (size != 1) {
965 log->l_iclog_size_log++;
966 size >>= 1;
969 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
970 /* # headers = size / 32k
971 * one header holds cycles from 32k of data
974 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
975 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
976 xhdrs++;
977 log->l_iclog_hsize = xhdrs << BBSHIFT;
978 log->l_iclog_heads = xhdrs;
979 } else {
980 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
981 log->l_iclog_hsize = BBSIZE;
982 log->l_iclog_heads = 1;
984 goto done;
987 /* All machines use 32kB buffers by default. */
988 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
989 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
991 /* the default log size is 16k or 32k which is one header sector */
992 log->l_iclog_hsize = BBSIZE;
993 log->l_iclog_heads = 1;
995 done:
996 /* are we being asked to make the sizes selected above visible? */
997 if (mp->m_logbufs == 0)
998 mp->m_logbufs = log->l_iclog_bufs;
999 if (mp->m_logbsize == 0)
1000 mp->m_logbsize = log->l_iclog_size;
1001 } /* xlog_get_iclog_buffer_size */
1005 * This routine initializes some of the log structure for a given mount point.
1006 * Its primary purpose is to fill in enough, so recovery can occur. However,
1007 * some other stuff may be filled in too.
1009 STATIC xlog_t *
1010 xlog_alloc_log(xfs_mount_t *mp,
1011 xfs_buftarg_t *log_target,
1012 xfs_daddr_t blk_offset,
1013 int num_bblks)
1015 xlog_t *log;
1016 xlog_rec_header_t *head;
1017 xlog_in_core_t **iclogp;
1018 xlog_in_core_t *iclog, *prev_iclog=NULL;
1019 xfs_buf_t *bp;
1020 int i;
1021 int iclogsize;
1022 int error = ENOMEM;
1024 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1025 if (!log) {
1026 xlog_warn("XFS: Log allocation failed: No memory!");
1027 goto out;
1030 log->l_mp = mp;
1031 log->l_targ = log_target;
1032 log->l_logsize = BBTOB(num_bblks);
1033 log->l_logBBstart = blk_offset;
1034 log->l_logBBsize = num_bblks;
1035 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1036 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1038 log->l_prev_block = -1;
1039 log->l_tail_lsn = xlog_assign_lsn(1, 0);
1040 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1041 log->l_last_sync_lsn = log->l_tail_lsn;
1042 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1043 log->l_grant_reserve_cycle = 1;
1044 log->l_grant_write_cycle = 1;
1046 error = EFSCORRUPTED;
1047 if (xfs_sb_version_hassector(&mp->m_sb)) {
1048 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1049 if (log->l_sectbb_log < 0 ||
1050 log->l_sectbb_log > mp->m_sectbb_log) {
1051 xlog_warn("XFS: Log sector size (0x%x) out of range.",
1052 log->l_sectbb_log);
1053 goto out_free_log;
1056 /* for larger sector sizes, must have v2 or external log */
1057 if (log->l_sectbb_log != 0 &&
1058 (log->l_logBBstart != 0 &&
1059 !xfs_sb_version_haslogv2(&mp->m_sb))) {
1060 xlog_warn("XFS: log sector size (0x%x) invalid "
1061 "for configuration.", log->l_sectbb_log);
1062 goto out_free_log;
1064 if (mp->m_sb.sb_logsectlog < BBSHIFT) {
1065 xlog_warn("XFS: Log sector log (0x%x) too small.",
1066 mp->m_sb.sb_logsectlog);
1067 goto out_free_log;
1070 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1072 xlog_get_iclog_buffer_size(mp, log);
1074 error = ENOMEM;
1075 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1076 if (!bp)
1077 goto out_free_log;
1078 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1079 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1080 ASSERT(XFS_BUF_ISBUSY(bp));
1081 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1082 log->l_xbuf = bp;
1084 spin_lock_init(&log->l_icloglock);
1085 spin_lock_init(&log->l_grant_lock);
1086 sv_init(&log->l_flush_wait, 0, "flush_wait");
1088 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1089 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1091 iclogp = &log->l_iclog;
1093 * The amount of memory to allocate for the iclog structure is
1094 * rather funky due to the way the structure is defined. It is
1095 * done this way so that we can use different sizes for machines
1096 * with different amounts of memory. See the definition of
1097 * xlog_in_core_t in xfs_log_priv.h for details.
1099 iclogsize = log->l_iclog_size;
1100 ASSERT(log->l_iclog_size >= 4096);
1101 for (i=0; i < log->l_iclog_bufs; i++) {
1102 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1103 if (!*iclogp)
1104 goto out_free_iclog;
1106 iclog = *iclogp;
1107 iclog->ic_prev = prev_iclog;
1108 prev_iclog = iclog;
1110 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1111 if (!bp)
1112 goto out_free_iclog;
1113 if (!XFS_BUF_CPSEMA(bp))
1114 ASSERT(0);
1115 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1116 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1117 iclog->ic_bp = bp;
1118 iclog->ic_data = bp->b_addr;
1119 #ifdef DEBUG
1120 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1121 #endif
1122 head = &iclog->ic_header;
1123 memset(head, 0, sizeof(xlog_rec_header_t));
1124 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1125 head->h_version = cpu_to_be32(
1126 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1127 head->h_size = cpu_to_be32(log->l_iclog_size);
1128 /* new fields */
1129 head->h_fmt = cpu_to_be32(XLOG_FMT);
1130 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1132 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1133 iclog->ic_state = XLOG_STATE_ACTIVE;
1134 iclog->ic_log = log;
1135 atomic_set(&iclog->ic_refcnt, 0);
1136 spin_lock_init(&iclog->ic_callback_lock);
1137 iclog->ic_callback_tail = &(iclog->ic_callback);
1138 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
1140 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1141 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1142 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1143 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
1145 iclogp = &iclog->ic_next;
1147 *iclogp = log->l_iclog; /* complete ring */
1148 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1150 return log;
1152 out_free_iclog:
1153 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1154 prev_iclog = iclog->ic_next;
1155 if (iclog->ic_bp) {
1156 sv_destroy(&iclog->ic_force_wait);
1157 sv_destroy(&iclog->ic_write_wait);
1158 xfs_buf_free(iclog->ic_bp);
1160 kmem_free(iclog);
1162 spinlock_destroy(&log->l_icloglock);
1163 spinlock_destroy(&log->l_grant_lock);
1164 xfs_buf_free(log->l_xbuf);
1165 out_free_log:
1166 kmem_free(log);
1167 out:
1168 return ERR_PTR(-error);
1169 } /* xlog_alloc_log */
1173 * Write out the commit record of a transaction associated with the given
1174 * ticket. Return the lsn of the commit record.
1176 STATIC int
1177 xlog_commit_record(xfs_mount_t *mp,
1178 xlog_ticket_t *ticket,
1179 xlog_in_core_t **iclog,
1180 xfs_lsn_t *commitlsnp)
1182 int error;
1183 xfs_log_iovec_t reg[1];
1185 reg[0].i_addr = NULL;
1186 reg[0].i_len = 0;
1187 reg[0].i_type = XLOG_REG_TYPE_COMMIT;
1189 ASSERT_ALWAYS(iclog);
1190 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1191 iclog, XLOG_COMMIT_TRANS))) {
1192 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1194 return error;
1195 } /* xlog_commit_record */
1199 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1200 * log space. This code pushes on the lsn which would supposedly free up
1201 * the 25% which we want to leave free. We may need to adopt a policy which
1202 * pushes on an lsn which is further along in the log once we reach the high
1203 * water mark. In this manner, we would be creating a low water mark.
1205 STATIC void
1206 xlog_grant_push_ail(xfs_mount_t *mp,
1207 int need_bytes)
1209 xlog_t *log = mp->m_log; /* pointer to the log */
1210 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1211 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1212 int free_blocks; /* free blocks left to write to */
1213 int free_bytes; /* free bytes left to write to */
1214 int threshold_block; /* block in lsn we'd like to be at */
1215 int threshold_cycle; /* lsn cycle we'd like to be at */
1216 int free_threshold;
1218 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1220 spin_lock(&log->l_grant_lock);
1221 free_bytes = xlog_space_left(log,
1222 log->l_grant_reserve_cycle,
1223 log->l_grant_reserve_bytes);
1224 tail_lsn = log->l_tail_lsn;
1225 free_blocks = BTOBBT(free_bytes);
1228 * Set the threshold for the minimum number of free blocks in the
1229 * log to the maximum of what the caller needs, one quarter of the
1230 * log, and 256 blocks.
1232 free_threshold = BTOBB(need_bytes);
1233 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1234 free_threshold = MAX(free_threshold, 256);
1235 if (free_blocks < free_threshold) {
1236 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1237 threshold_cycle = CYCLE_LSN(tail_lsn);
1238 if (threshold_block >= log->l_logBBsize) {
1239 threshold_block -= log->l_logBBsize;
1240 threshold_cycle += 1;
1242 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
1244 /* Don't pass in an lsn greater than the lsn of the last
1245 * log record known to be on disk.
1247 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1248 threshold_lsn = log->l_last_sync_lsn;
1250 spin_unlock(&log->l_grant_lock);
1253 * Get the transaction layer to kick the dirty buffers out to
1254 * disk asynchronously. No point in trying to do this if
1255 * the filesystem is shutting down.
1257 if (threshold_lsn &&
1258 !XLOG_FORCED_SHUTDOWN(log))
1259 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
1260 } /* xlog_grant_push_ail */
1263 * The bdstrat callback function for log bufs. This gives us a central
1264 * place to trap bufs in case we get hit by a log I/O error and need to
1265 * shutdown. Actually, in practice, even when we didn't get a log error,
1266 * we transition the iclogs to IOERROR state *after* flushing all existing
1267 * iclogs to disk. This is because we don't want anymore new transactions to be
1268 * started or completed afterwards.
1270 STATIC int
1271 xlog_bdstrat(
1272 struct xfs_buf *bp)
1274 struct xlog_in_core *iclog;
1276 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1277 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1278 XFS_BUF_ERROR(bp, EIO);
1279 XFS_BUF_STALE(bp);
1280 xfs_biodone(bp);
1282 * It would seem logical to return EIO here, but we rely on
1283 * the log state machine to propagate I/O errors instead of
1284 * doing it here.
1286 return 0;
1289 bp->b_flags |= _XBF_RUN_QUEUES;
1290 xfs_buf_iorequest(bp);
1291 return 0;
1295 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1296 * fashion. Previously, we should have moved the current iclog
1297 * ptr in the log to point to the next available iclog. This allows further
1298 * write to continue while this code syncs out an iclog ready to go.
1299 * Before an in-core log can be written out, the data section must be scanned
1300 * to save away the 1st word of each BBSIZE block into the header. We replace
1301 * it with the current cycle count. Each BBSIZE block is tagged with the
1302 * cycle count because there in an implicit assumption that drives will
1303 * guarantee that entire 512 byte blocks get written at once. In other words,
1304 * we can't have part of a 512 byte block written and part not written. By
1305 * tagging each block, we will know which blocks are valid when recovering
1306 * after an unclean shutdown.
1308 * This routine is single threaded on the iclog. No other thread can be in
1309 * this routine with the same iclog. Changing contents of iclog can there-
1310 * fore be done without grabbing the state machine lock. Updating the global
1311 * log will require grabbing the lock though.
1313 * The entire log manager uses a logical block numbering scheme. Only
1314 * log_sync (and then only bwrite()) know about the fact that the log may
1315 * not start with block zero on a given device. The log block start offset
1316 * is added immediately before calling bwrite().
1319 STATIC int
1320 xlog_sync(xlog_t *log,
1321 xlog_in_core_t *iclog)
1323 xfs_caddr_t dptr; /* pointer to byte sized element */
1324 xfs_buf_t *bp;
1325 int i;
1326 uint count; /* byte count of bwrite */
1327 uint count_init; /* initial count before roundup */
1328 int roundoff; /* roundoff to BB or stripe */
1329 int split = 0; /* split write into two regions */
1330 int error;
1331 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
1333 XFS_STATS_INC(xs_log_writes);
1334 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1336 /* Add for LR header */
1337 count_init = log->l_iclog_hsize + iclog->ic_offset;
1339 /* Round out the log write size */
1340 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1341 /* we have a v2 stripe unit to use */
1342 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1343 } else {
1344 count = BBTOB(BTOBB(count_init));
1346 roundoff = count - count_init;
1347 ASSERT(roundoff >= 0);
1348 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1349 roundoff < log->l_mp->m_sb.sb_logsunit)
1351 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1352 roundoff < BBTOB(1)));
1354 /* move grant heads by roundoff in sync */
1355 spin_lock(&log->l_grant_lock);
1356 xlog_grant_add_space(log, roundoff);
1357 spin_unlock(&log->l_grant_lock);
1359 /* put cycle number in every block */
1360 xlog_pack_data(log, iclog, roundoff);
1362 /* real byte length */
1363 if (v2) {
1364 iclog->ic_header.h_len =
1365 cpu_to_be32(iclog->ic_offset + roundoff);
1366 } else {
1367 iclog->ic_header.h_len =
1368 cpu_to_be32(iclog->ic_offset);
1371 bp = iclog->ic_bp;
1372 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1373 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1374 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
1376 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1378 /* Do we need to split this write into 2 parts? */
1379 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1380 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1381 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1382 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1383 } else {
1384 iclog->ic_bwritecnt = 1;
1386 XFS_BUF_SET_COUNT(bp, count);
1387 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1388 XFS_BUF_ZEROFLAGS(bp);
1389 XFS_BUF_BUSY(bp);
1390 XFS_BUF_ASYNC(bp);
1391 bp->b_flags |= XBF_LOG_BUFFER;
1393 * Do an ordered write for the log block.
1394 * Its unnecessary to flush the first split block in the log wrap case.
1396 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
1397 XFS_BUF_ORDERED(bp);
1399 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1400 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1402 xlog_verify_iclog(log, iclog, count, B_TRUE);
1404 /* account for log which doesn't start at block #0 */
1405 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1407 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1408 * is shutting down.
1410 XFS_BUF_WRITE(bp);
1412 if ((error = xlog_bdstrat(bp))) {
1413 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1414 XFS_BUF_ADDR(bp));
1415 return error;
1417 if (split) {
1418 bp = iclog->ic_log->l_xbuf;
1419 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1420 (unsigned long)1);
1421 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1422 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1423 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1424 (__psint_t)count), split);
1425 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1426 XFS_BUF_ZEROFLAGS(bp);
1427 XFS_BUF_BUSY(bp);
1428 XFS_BUF_ASYNC(bp);
1429 bp->b_flags |= XBF_LOG_BUFFER;
1430 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1431 XFS_BUF_ORDERED(bp);
1432 dptr = XFS_BUF_PTR(bp);
1434 * Bump the cycle numbers at the start of each block
1435 * since this part of the buffer is at the start of
1436 * a new cycle. Watch out for the header magic number
1437 * case, though.
1439 for (i = 0; i < split; i += BBSIZE) {
1440 be32_add_cpu((__be32 *)dptr, 1);
1441 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
1442 be32_add_cpu((__be32 *)dptr, 1);
1443 dptr += BBSIZE;
1446 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1447 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1449 /* account for internal log which doesn't start at block #0 */
1450 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1451 XFS_BUF_WRITE(bp);
1452 if ((error = xlog_bdstrat(bp))) {
1453 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1454 bp, XFS_BUF_ADDR(bp));
1455 return error;
1458 return 0;
1459 } /* xlog_sync */
1463 * Deallocate a log structure
1465 STATIC void
1466 xlog_dealloc_log(xlog_t *log)
1468 xlog_in_core_t *iclog, *next_iclog;
1469 int i;
1471 iclog = log->l_iclog;
1472 for (i=0; i<log->l_iclog_bufs; i++) {
1473 sv_destroy(&iclog->ic_force_wait);
1474 sv_destroy(&iclog->ic_write_wait);
1475 xfs_buf_free(iclog->ic_bp);
1476 next_iclog = iclog->ic_next;
1477 kmem_free(iclog);
1478 iclog = next_iclog;
1480 spinlock_destroy(&log->l_icloglock);
1481 spinlock_destroy(&log->l_grant_lock);
1483 xfs_buf_free(log->l_xbuf);
1484 log->l_mp->m_log = NULL;
1485 kmem_free(log);
1486 } /* xlog_dealloc_log */
1489 * Update counters atomically now that memcpy is done.
1491 /* ARGSUSED */
1492 static inline void
1493 xlog_state_finish_copy(xlog_t *log,
1494 xlog_in_core_t *iclog,
1495 int record_cnt,
1496 int copy_bytes)
1498 spin_lock(&log->l_icloglock);
1500 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1501 iclog->ic_offset += copy_bytes;
1503 spin_unlock(&log->l_icloglock);
1504 } /* xlog_state_finish_copy */
1510 * print out info relating to regions written which consume
1511 * the reservation
1513 STATIC void
1514 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1516 uint i;
1517 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1519 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1520 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1521 "bformat",
1522 "bchunk",
1523 "efi_format",
1524 "efd_format",
1525 "iformat",
1526 "icore",
1527 "iext",
1528 "ibroot",
1529 "ilocal",
1530 "iattr_ext",
1531 "iattr_broot",
1532 "iattr_local",
1533 "qformat",
1534 "dquot",
1535 "quotaoff",
1536 "LR header",
1537 "unmount",
1538 "commit",
1539 "trans header"
1541 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1542 "SETATTR_NOT_SIZE",
1543 "SETATTR_SIZE",
1544 "INACTIVE",
1545 "CREATE",
1546 "CREATE_TRUNC",
1547 "TRUNCATE_FILE",
1548 "REMOVE",
1549 "LINK",
1550 "RENAME",
1551 "MKDIR",
1552 "RMDIR",
1553 "SYMLINK",
1554 "SET_DMATTRS",
1555 "GROWFS",
1556 "STRAT_WRITE",
1557 "DIOSTRAT",
1558 "WRITE_SYNC",
1559 "WRITEID",
1560 "ADDAFORK",
1561 "ATTRINVAL",
1562 "ATRUNCATE",
1563 "ATTR_SET",
1564 "ATTR_RM",
1565 "ATTR_FLAG",
1566 "CLEAR_AGI_BUCKET",
1567 "QM_SBCHANGE",
1568 "DUMMY1",
1569 "DUMMY2",
1570 "QM_QUOTAOFF",
1571 "QM_DQALLOC",
1572 "QM_SETQLIM",
1573 "QM_DQCLUSTER",
1574 "QM_QINOCREATE",
1575 "QM_QUOTAOFF_END",
1576 "SB_UNIT",
1577 "FSYNC_TS",
1578 "GROWFSRT_ALLOC",
1579 "GROWFSRT_ZERO",
1580 "GROWFSRT_FREE",
1581 "SWAPEXT"
1584 xfs_fs_cmn_err(CE_WARN, mp,
1585 "xfs_log_write: reservation summary:\n"
1586 " trans type = %s (%u)\n"
1587 " unit res = %d bytes\n"
1588 " current res = %d bytes\n"
1589 " total reg = %u bytes (o/flow = %u bytes)\n"
1590 " ophdrs = %u (ophdr space = %u bytes)\n"
1591 " ophdr + reg = %u bytes\n"
1592 " num regions = %u\n",
1593 ((ticket->t_trans_type <= 0 ||
1594 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1595 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1596 ticket->t_trans_type,
1597 ticket->t_unit_res,
1598 ticket->t_curr_res,
1599 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1600 ticket->t_res_num_ophdrs, ophdr_spc,
1601 ticket->t_res_arr_sum +
1602 ticket->t_res_o_flow + ophdr_spc,
1603 ticket->t_res_num);
1605 for (i = 0; i < ticket->t_res_num; i++) {
1606 uint r_type = ticket->t_res_arr[i].r_type;
1607 cmn_err(CE_WARN,
1608 "region[%u]: %s - %u bytes\n",
1610 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1611 "bad-rtype" : res_type_str[r_type-1]),
1612 ticket->t_res_arr[i].r_len);
1617 * Write some region out to in-core log
1619 * This will be called when writing externally provided regions or when
1620 * writing out a commit record for a given transaction.
1622 * General algorithm:
1623 * 1. Find total length of this write. This may include adding to the
1624 * lengths passed in.
1625 * 2. Check whether we violate the tickets reservation.
1626 * 3. While writing to this iclog
1627 * A. Reserve as much space in this iclog as can get
1628 * B. If this is first write, save away start lsn
1629 * C. While writing this region:
1630 * 1. If first write of transaction, write start record
1631 * 2. Write log operation header (header per region)
1632 * 3. Find out if we can fit entire region into this iclog
1633 * 4. Potentially, verify destination memcpy ptr
1634 * 5. Memcpy (partial) region
1635 * 6. If partial copy, release iclog; otherwise, continue
1636 * copying more regions into current iclog
1637 * 4. Mark want sync bit (in simulation mode)
1638 * 5. Release iclog for potential flush to on-disk log.
1640 * ERRORS:
1641 * 1. Panic if reservation is overrun. This should never happen since
1642 * reservation amounts are generated internal to the filesystem.
1643 * NOTES:
1644 * 1. Tickets are single threaded data structures.
1645 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1646 * syncing routine. When a single log_write region needs to span
1647 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1648 * on all log operation writes which don't contain the end of the
1649 * region. The XLOG_END_TRANS bit is used for the in-core log
1650 * operation which contains the end of the continued log_write region.
1651 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1652 * we don't really know exactly how much space will be used. As a result,
1653 * we don't update ic_offset until the end when we know exactly how many
1654 * bytes have been written out.
1656 STATIC int
1657 xlog_write(
1658 struct xfs_mount *mp,
1659 struct xfs_log_iovec reg[],
1660 int nentries,
1661 struct xlog_ticket *ticket,
1662 xfs_lsn_t *start_lsn,
1663 struct xlog_in_core **commit_iclog,
1664 uint flags)
1666 xlog_t *log = mp->m_log;
1667 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
1668 xlog_op_header_t *logop_head; /* ptr to log operation header */
1669 __psint_t ptr; /* copy address into data region */
1670 int len; /* # xlog_write() bytes 2 still copy */
1671 int index; /* region index currently copying */
1672 int log_offset; /* offset (from 0) into data region */
1673 int start_rec_copy; /* # bytes to copy for start record */
1674 int partial_copy; /* did we split a region? */
1675 int partial_copy_len;/* # bytes copied if split region */
1676 int need_copy; /* # bytes need to memcpy this region */
1677 int copy_len; /* # bytes actually memcpy'ing */
1678 int copy_off; /* # bytes from entry start */
1679 int contwr; /* continued write of in-core log? */
1680 int error;
1681 int record_cnt = 0, data_cnt = 0;
1683 partial_copy_len = partial_copy = 0;
1685 /* Calculate potential maximum space. Each region gets its own
1686 * xlog_op_header_t and may need to be double word aligned.
1688 len = 0;
1689 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
1690 len += sizeof(xlog_op_header_t);
1691 ticket->t_res_num_ophdrs++;
1694 for (index = 0; index < nentries; index++) {
1695 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
1696 ticket->t_res_num_ophdrs++;
1697 len += reg[index].i_len;
1698 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
1700 contwr = *start_lsn = 0;
1702 if (ticket->t_curr_res < len) {
1703 xlog_print_tic_res(mp, ticket);
1704 #ifdef DEBUG
1705 xlog_panic(
1706 "xfs_log_write: reservation ran out. Need to up reservation");
1707 #else
1708 /* Customer configurable panic */
1709 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1710 "xfs_log_write: reservation ran out. Need to up reservation");
1711 /* If we did not panic, shutdown the filesystem */
1712 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1713 #endif
1714 } else
1715 ticket->t_curr_res -= len;
1717 for (index = 0; index < nentries; ) {
1718 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1719 &contwr, &log_offset)))
1720 return error;
1722 ASSERT(log_offset <= iclog->ic_size - 1);
1723 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1725 /* start_lsn is the first lsn written to. That's all we need. */
1726 if (! *start_lsn)
1727 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
1729 /* This loop writes out as many regions as can fit in the amount
1730 * of space which was allocated by xlog_state_get_iclog_space().
1732 while (index < nentries) {
1733 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1734 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1735 start_rec_copy = 0;
1737 /* If first write for transaction, insert start record.
1738 * We can't be trying to commit if we are inited. We can't
1739 * have any "partial_copy" if we are inited.
1741 if (ticket->t_flags & XLOG_TIC_INITED) {
1742 logop_head = (xlog_op_header_t *)ptr;
1743 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1744 logop_head->oh_clientid = ticket->t_clientid;
1745 logop_head->oh_len = 0;
1746 logop_head->oh_flags = XLOG_START_TRANS;
1747 logop_head->oh_res2 = 0;
1748 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1749 record_cnt++;
1751 start_rec_copy = sizeof(xlog_op_header_t);
1752 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1755 /* Copy log operation header directly into data section */
1756 logop_head = (xlog_op_header_t *)ptr;
1757 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
1758 logop_head->oh_clientid = ticket->t_clientid;
1759 logop_head->oh_res2 = 0;
1761 /* header copied directly */
1762 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1764 /* are we copying a commit or unmount record? */
1765 logop_head->oh_flags = flags;
1768 * We've seen logs corrupted with bad transaction client
1769 * ids. This makes sure that XFS doesn't generate them on.
1770 * Turn this into an EIO and shut down the filesystem.
1772 switch (logop_head->oh_clientid) {
1773 case XFS_TRANSACTION:
1774 case XFS_VOLUME:
1775 case XFS_LOG:
1776 break;
1777 default:
1778 xfs_fs_cmn_err(CE_WARN, mp,
1779 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1780 logop_head->oh_clientid, ticket);
1781 return XFS_ERROR(EIO);
1784 /* Partial write last time? => (partial_copy != 0)
1785 * need_copy is the amount we'd like to copy if everything could
1786 * fit in the current memcpy.
1788 need_copy = reg[index].i_len - partial_copy_len;
1790 copy_off = partial_copy_len;
1791 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1792 copy_len = need_copy;
1793 logop_head->oh_len = cpu_to_be32(copy_len);
1794 if (partial_copy)
1795 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1796 partial_copy_len = partial_copy = 0;
1797 } else { /* partial write */
1798 copy_len = iclog->ic_size - log_offset;
1799 logop_head->oh_len = cpu_to_be32(copy_len);
1800 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1801 if (partial_copy)
1802 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1803 partial_copy_len += copy_len;
1804 partial_copy++;
1805 len += sizeof(xlog_op_header_t); /* from splitting of region */
1806 /* account for new log op header */
1807 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1808 ticket->t_res_num_ophdrs++;
1810 xlog_verify_dest_ptr(log, ptr);
1812 /* copy region */
1813 ASSERT(copy_len >= 0);
1814 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1815 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1817 /* make copy_len total bytes copied, including headers */
1818 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1819 record_cnt++;
1820 data_cnt += contwr ? copy_len : 0;
1821 if (partial_copy) { /* copied partial region */
1822 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1823 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1824 record_cnt = data_cnt = 0;
1825 if ((error = xlog_state_release_iclog(log, iclog)))
1826 return error;
1827 break; /* don't increment index */
1828 } else { /* copied entire region */
1829 index++;
1830 partial_copy_len = partial_copy = 0;
1832 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1833 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1834 record_cnt = data_cnt = 0;
1835 spin_lock(&log->l_icloglock);
1836 xlog_state_want_sync(log, iclog);
1837 spin_unlock(&log->l_icloglock);
1838 if (commit_iclog) {
1839 ASSERT(flags & XLOG_COMMIT_TRANS);
1840 *commit_iclog = iclog;
1841 } else if ((error = xlog_state_release_iclog(log, iclog)))
1842 return error;
1843 if (index == nentries)
1844 return 0; /* we are done */
1845 else
1846 break;
1848 } /* if (partial_copy) */
1849 } /* while (index < nentries) */
1850 } /* for (index = 0; index < nentries; ) */
1851 ASSERT(len == 0);
1853 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1854 if (commit_iclog) {
1855 ASSERT(flags & XLOG_COMMIT_TRANS);
1856 *commit_iclog = iclog;
1857 return 0;
1859 return xlog_state_release_iclog(log, iclog);
1860 } /* xlog_write */
1863 /*****************************************************************************
1865 * State Machine functions
1867 *****************************************************************************
1870 /* Clean iclogs starting from the head. This ordering must be
1871 * maintained, so an iclog doesn't become ACTIVE beyond one that
1872 * is SYNCING. This is also required to maintain the notion that we use
1873 * a ordered wait queue to hold off would be writers to the log when every
1874 * iclog is trying to sync to disk.
1876 * State Change: DIRTY -> ACTIVE
1878 STATIC void
1879 xlog_state_clean_log(xlog_t *log)
1881 xlog_in_core_t *iclog;
1882 int changed = 0;
1884 iclog = log->l_iclog;
1885 do {
1886 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1887 iclog->ic_state = XLOG_STATE_ACTIVE;
1888 iclog->ic_offset = 0;
1889 ASSERT(iclog->ic_callback == NULL);
1891 * If the number of ops in this iclog indicate it just
1892 * contains the dummy transaction, we can
1893 * change state into IDLE (the second time around).
1894 * Otherwise we should change the state into
1895 * NEED a dummy.
1896 * We don't need to cover the dummy.
1898 if (!changed &&
1899 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1900 XLOG_COVER_OPS)) {
1901 changed = 1;
1902 } else {
1904 * We have two dirty iclogs so start over
1905 * This could also be num of ops indicates
1906 * this is not the dummy going out.
1908 changed = 2;
1910 iclog->ic_header.h_num_logops = 0;
1911 memset(iclog->ic_header.h_cycle_data, 0,
1912 sizeof(iclog->ic_header.h_cycle_data));
1913 iclog->ic_header.h_lsn = 0;
1914 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1915 /* do nothing */;
1916 else
1917 break; /* stop cleaning */
1918 iclog = iclog->ic_next;
1919 } while (iclog != log->l_iclog);
1921 /* log is locked when we are called */
1923 * Change state for the dummy log recording.
1924 * We usually go to NEED. But we go to NEED2 if the changed indicates
1925 * we are done writing the dummy record.
1926 * If we are done with the second dummy recored (DONE2), then
1927 * we go to IDLE.
1929 if (changed) {
1930 switch (log->l_covered_state) {
1931 case XLOG_STATE_COVER_IDLE:
1932 case XLOG_STATE_COVER_NEED:
1933 case XLOG_STATE_COVER_NEED2:
1934 log->l_covered_state = XLOG_STATE_COVER_NEED;
1935 break;
1937 case XLOG_STATE_COVER_DONE:
1938 if (changed == 1)
1939 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1940 else
1941 log->l_covered_state = XLOG_STATE_COVER_NEED;
1942 break;
1944 case XLOG_STATE_COVER_DONE2:
1945 if (changed == 1)
1946 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1947 else
1948 log->l_covered_state = XLOG_STATE_COVER_NEED;
1949 break;
1951 default:
1952 ASSERT(0);
1955 } /* xlog_state_clean_log */
1957 STATIC xfs_lsn_t
1958 xlog_get_lowest_lsn(
1959 xlog_t *log)
1961 xlog_in_core_t *lsn_log;
1962 xfs_lsn_t lowest_lsn, lsn;
1964 lsn_log = log->l_iclog;
1965 lowest_lsn = 0;
1966 do {
1967 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
1968 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
1969 if ((lsn && !lowest_lsn) ||
1970 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
1971 lowest_lsn = lsn;
1974 lsn_log = lsn_log->ic_next;
1975 } while (lsn_log != log->l_iclog);
1976 return lowest_lsn;
1980 STATIC void
1981 xlog_state_do_callback(
1982 xlog_t *log,
1983 int aborted,
1984 xlog_in_core_t *ciclog)
1986 xlog_in_core_t *iclog;
1987 xlog_in_core_t *first_iclog; /* used to know when we've
1988 * processed all iclogs once */
1989 xfs_log_callback_t *cb, *cb_next;
1990 int flushcnt = 0;
1991 xfs_lsn_t lowest_lsn;
1992 int ioerrors; /* counter: iclogs with errors */
1993 int loopdidcallbacks; /* flag: inner loop did callbacks*/
1994 int funcdidcallbacks; /* flag: function did callbacks */
1995 int repeats; /* for issuing console warnings if
1996 * looping too many times */
1997 int wake = 0;
1999 spin_lock(&log->l_icloglock);
2000 first_iclog = iclog = log->l_iclog;
2001 ioerrors = 0;
2002 funcdidcallbacks = 0;
2003 repeats = 0;
2005 do {
2007 * Scan all iclogs starting with the one pointed to by the
2008 * log. Reset this starting point each time the log is
2009 * unlocked (during callbacks).
2011 * Keep looping through iclogs until one full pass is made
2012 * without running any callbacks.
2014 first_iclog = log->l_iclog;
2015 iclog = log->l_iclog;
2016 loopdidcallbacks = 0;
2017 repeats++;
2019 do {
2021 /* skip all iclogs in the ACTIVE & DIRTY states */
2022 if (iclog->ic_state &
2023 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2024 iclog = iclog->ic_next;
2025 continue;
2029 * Between marking a filesystem SHUTDOWN and stopping
2030 * the log, we do flush all iclogs to disk (if there
2031 * wasn't a log I/O error). So, we do want things to
2032 * go smoothly in case of just a SHUTDOWN w/o a
2033 * LOG_IO_ERROR.
2035 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2037 * Can only perform callbacks in order. Since
2038 * this iclog is not in the DONE_SYNC/
2039 * DO_CALLBACK state, we skip the rest and
2040 * just try to clean up. If we set our iclog
2041 * to DO_CALLBACK, we will not process it when
2042 * we retry since a previous iclog is in the
2043 * CALLBACK and the state cannot change since
2044 * we are holding the l_icloglock.
2046 if (!(iclog->ic_state &
2047 (XLOG_STATE_DONE_SYNC |
2048 XLOG_STATE_DO_CALLBACK))) {
2049 if (ciclog && (ciclog->ic_state ==
2050 XLOG_STATE_DONE_SYNC)) {
2051 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2053 break;
2056 * We now have an iclog that is in either the
2057 * DO_CALLBACK or DONE_SYNC states. The other
2058 * states (WANT_SYNC, SYNCING, or CALLBACK were
2059 * caught by the above if and are going to
2060 * clean (i.e. we aren't doing their callbacks)
2061 * see the above if.
2065 * We will do one more check here to see if we
2066 * have chased our tail around.
2069 lowest_lsn = xlog_get_lowest_lsn(log);
2070 if (lowest_lsn &&
2071 XFS_LSN_CMP(lowest_lsn,
2072 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
2073 iclog = iclog->ic_next;
2074 continue; /* Leave this iclog for
2075 * another thread */
2078 iclog->ic_state = XLOG_STATE_CALLBACK;
2080 spin_unlock(&log->l_icloglock);
2082 /* l_last_sync_lsn field protected by
2083 * l_grant_lock. Don't worry about iclog's lsn.
2084 * No one else can be here except us.
2086 spin_lock(&log->l_grant_lock);
2087 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2088 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2089 log->l_last_sync_lsn =
2090 be64_to_cpu(iclog->ic_header.h_lsn);
2091 spin_unlock(&log->l_grant_lock);
2093 } else {
2094 spin_unlock(&log->l_icloglock);
2095 ioerrors++;
2099 * Keep processing entries in the callback list until
2100 * we come around and it is empty. We need to
2101 * atomically see that the list is empty and change the
2102 * state to DIRTY so that we don't miss any more
2103 * callbacks being added.
2105 spin_lock(&iclog->ic_callback_lock);
2106 cb = iclog->ic_callback;
2107 while (cb) {
2108 iclog->ic_callback_tail = &(iclog->ic_callback);
2109 iclog->ic_callback = NULL;
2110 spin_unlock(&iclog->ic_callback_lock);
2112 /* perform callbacks in the order given */
2113 for (; cb; cb = cb_next) {
2114 cb_next = cb->cb_next;
2115 cb->cb_func(cb->cb_arg, aborted);
2117 spin_lock(&iclog->ic_callback_lock);
2118 cb = iclog->ic_callback;
2121 loopdidcallbacks++;
2122 funcdidcallbacks++;
2124 spin_lock(&log->l_icloglock);
2125 ASSERT(iclog->ic_callback == NULL);
2126 spin_unlock(&iclog->ic_callback_lock);
2127 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2128 iclog->ic_state = XLOG_STATE_DIRTY;
2131 * Transition from DIRTY to ACTIVE if applicable.
2132 * NOP if STATE_IOERROR.
2134 xlog_state_clean_log(log);
2136 /* wake up threads waiting in xfs_log_force() */
2137 sv_broadcast(&iclog->ic_force_wait);
2139 iclog = iclog->ic_next;
2140 } while (first_iclog != iclog);
2142 if (repeats > 5000) {
2143 flushcnt += repeats;
2144 repeats = 0;
2145 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2146 "%s: possible infinite loop (%d iterations)",
2147 __func__, flushcnt);
2149 } while (!ioerrors && loopdidcallbacks);
2152 * make one last gasp attempt to see if iclogs are being left in
2153 * limbo..
2155 #ifdef DEBUG
2156 if (funcdidcallbacks) {
2157 first_iclog = iclog = log->l_iclog;
2158 do {
2159 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2161 * Terminate the loop if iclogs are found in states
2162 * which will cause other threads to clean up iclogs.
2164 * SYNCING - i/o completion will go through logs
2165 * DONE_SYNC - interrupt thread should be waiting for
2166 * l_icloglock
2167 * IOERROR - give up hope all ye who enter here
2169 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2170 iclog->ic_state == XLOG_STATE_SYNCING ||
2171 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2172 iclog->ic_state == XLOG_STATE_IOERROR )
2173 break;
2174 iclog = iclog->ic_next;
2175 } while (first_iclog != iclog);
2177 #endif
2179 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2180 wake = 1;
2181 spin_unlock(&log->l_icloglock);
2183 if (wake)
2184 sv_broadcast(&log->l_flush_wait);
2189 * Finish transitioning this iclog to the dirty state.
2191 * Make sure that we completely execute this routine only when this is
2192 * the last call to the iclog. There is a good chance that iclog flushes,
2193 * when we reach the end of the physical log, get turned into 2 separate
2194 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2195 * routine. By using the reference count bwritecnt, we guarantee that only
2196 * the second completion goes through.
2198 * Callbacks could take time, so they are done outside the scope of the
2199 * global state machine log lock.
2201 STATIC void
2202 xlog_state_done_syncing(
2203 xlog_in_core_t *iclog,
2204 int aborted)
2206 xlog_t *log = iclog->ic_log;
2208 spin_lock(&log->l_icloglock);
2210 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2211 iclog->ic_state == XLOG_STATE_IOERROR);
2212 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2213 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2217 * If we got an error, either on the first buffer, or in the case of
2218 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2219 * and none should ever be attempted to be written to disk
2220 * again.
2222 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2223 if (--iclog->ic_bwritecnt == 1) {
2224 spin_unlock(&log->l_icloglock);
2225 return;
2227 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2231 * Someone could be sleeping prior to writing out the next
2232 * iclog buffer, we wake them all, one will get to do the
2233 * I/O, the others get to wait for the result.
2235 sv_broadcast(&iclog->ic_write_wait);
2236 spin_unlock(&log->l_icloglock);
2237 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2238 } /* xlog_state_done_syncing */
2242 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2243 * sleep. We wait on the flush queue on the head iclog as that should be
2244 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2245 * we will wait here and all new writes will sleep until a sync completes.
2247 * The in-core logs are used in a circular fashion. They are not used
2248 * out-of-order even when an iclog past the head is free.
2250 * return:
2251 * * log_offset where xlog_write() can start writing into the in-core
2252 * log's data space.
2253 * * in-core log pointer to which xlog_write() should write.
2254 * * boolean indicating this is a continued write to an in-core log.
2255 * If this is the last write, then the in-core log's offset field
2256 * needs to be incremented, depending on the amount of data which
2257 * is copied.
2259 STATIC int
2260 xlog_state_get_iclog_space(xlog_t *log,
2261 int len,
2262 xlog_in_core_t **iclogp,
2263 xlog_ticket_t *ticket,
2264 int *continued_write,
2265 int *logoffsetp)
2267 int log_offset;
2268 xlog_rec_header_t *head;
2269 xlog_in_core_t *iclog;
2270 int error;
2272 restart:
2273 spin_lock(&log->l_icloglock);
2274 if (XLOG_FORCED_SHUTDOWN(log)) {
2275 spin_unlock(&log->l_icloglock);
2276 return XFS_ERROR(EIO);
2279 iclog = log->l_iclog;
2280 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2281 XFS_STATS_INC(xs_log_noiclogs);
2283 /* Wait for log writes to have flushed */
2284 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
2285 goto restart;
2288 head = &iclog->ic_header;
2290 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
2291 log_offset = iclog->ic_offset;
2293 /* On the 1st write to an iclog, figure out lsn. This works
2294 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2295 * committing to. If the offset is set, that's how many blocks
2296 * must be written.
2298 if (log_offset == 0) {
2299 ticket->t_curr_res -= log->l_iclog_hsize;
2300 xlog_tic_add_region(ticket,
2301 log->l_iclog_hsize,
2302 XLOG_REG_TYPE_LRHEADER);
2303 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2304 head->h_lsn = cpu_to_be64(
2305 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
2306 ASSERT(log->l_curr_block >= 0);
2309 /* If there is enough room to write everything, then do it. Otherwise,
2310 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2311 * bit is on, so this will get flushed out. Don't update ic_offset
2312 * until you know exactly how many bytes get copied. Therefore, wait
2313 * until later to update ic_offset.
2315 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2316 * can fit into remaining data section.
2318 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2319 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2322 * If I'm the only one writing to this iclog, sync it to disk.
2323 * We need to do an atomic compare and decrement here to avoid
2324 * racing with concurrent atomic_dec_and_lock() calls in
2325 * xlog_state_release_iclog() when there is more than one
2326 * reference to the iclog.
2328 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2329 /* we are the only one */
2330 spin_unlock(&log->l_icloglock);
2331 error = xlog_state_release_iclog(log, iclog);
2332 if (error)
2333 return error;
2334 } else {
2335 spin_unlock(&log->l_icloglock);
2337 goto restart;
2340 /* Do we have enough room to write the full amount in the remainder
2341 * of this iclog? Or must we continue a write on the next iclog and
2342 * mark this iclog as completely taken? In the case where we switch
2343 * iclogs (to mark it taken), this particular iclog will release/sync
2344 * to disk in xlog_write().
2346 if (len <= iclog->ic_size - iclog->ic_offset) {
2347 *continued_write = 0;
2348 iclog->ic_offset += len;
2349 } else {
2350 *continued_write = 1;
2351 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2353 *iclogp = iclog;
2355 ASSERT(iclog->ic_offset <= iclog->ic_size);
2356 spin_unlock(&log->l_icloglock);
2358 *logoffsetp = log_offset;
2359 return 0;
2360 } /* xlog_state_get_iclog_space */
2363 * Atomically get the log space required for a log ticket.
2365 * Once a ticket gets put onto the reserveq, it will only return after
2366 * the needed reservation is satisfied.
2368 STATIC int
2369 xlog_grant_log_space(xlog_t *log,
2370 xlog_ticket_t *tic)
2372 int free_bytes;
2373 int need_bytes;
2374 #ifdef DEBUG
2375 xfs_lsn_t tail_lsn;
2376 #endif
2379 #ifdef DEBUG
2380 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2381 panic("grant Recovery problem");
2382 #endif
2384 /* Is there space or do we need to sleep? */
2385 spin_lock(&log->l_grant_lock);
2387 trace_xfs_log_grant_enter(log, tic);
2389 /* something is already sleeping; insert new transaction at end */
2390 if (log->l_reserve_headq) {
2391 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2393 trace_xfs_log_grant_sleep1(log, tic);
2396 * Gotta check this before going to sleep, while we're
2397 * holding the grant lock.
2399 if (XLOG_FORCED_SHUTDOWN(log))
2400 goto error_return;
2402 XFS_STATS_INC(xs_sleep_logspace);
2403 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2405 * If we got an error, and the filesystem is shutting down,
2406 * we'll catch it down below. So just continue...
2408 trace_xfs_log_grant_wake1(log, tic);
2409 spin_lock(&log->l_grant_lock);
2411 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2412 need_bytes = tic->t_unit_res*tic->t_ocnt;
2413 else
2414 need_bytes = tic->t_unit_res;
2416 redo:
2417 if (XLOG_FORCED_SHUTDOWN(log))
2418 goto error_return;
2420 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2421 log->l_grant_reserve_bytes);
2422 if (free_bytes < need_bytes) {
2423 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2424 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2426 trace_xfs_log_grant_sleep2(log, tic);
2428 spin_unlock(&log->l_grant_lock);
2429 xlog_grant_push_ail(log->l_mp, need_bytes);
2430 spin_lock(&log->l_grant_lock);
2432 XFS_STATS_INC(xs_sleep_logspace);
2433 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2435 spin_lock(&log->l_grant_lock);
2436 if (XLOG_FORCED_SHUTDOWN(log))
2437 goto error_return;
2439 trace_xfs_log_grant_wake2(log, tic);
2441 goto redo;
2442 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2443 xlog_del_ticketq(&log->l_reserve_headq, tic);
2445 /* we've got enough space */
2446 xlog_grant_add_space(log, need_bytes);
2447 #ifdef DEBUG
2448 tail_lsn = log->l_tail_lsn;
2450 * Check to make sure the grant write head didn't just over lap the
2451 * tail. If the cycles are the same, we can't be overlapping.
2452 * Otherwise, make sure that the cycles differ by exactly one and
2453 * check the byte count.
2455 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2456 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2457 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2459 #endif
2460 trace_xfs_log_grant_exit(log, tic);
2461 xlog_verify_grant_head(log, 1);
2462 spin_unlock(&log->l_grant_lock);
2463 return 0;
2465 error_return:
2466 if (tic->t_flags & XLOG_TIC_IN_Q)
2467 xlog_del_ticketq(&log->l_reserve_headq, tic);
2469 trace_xfs_log_grant_error(log, tic);
2472 * If we are failing, make sure the ticket doesn't have any
2473 * current reservations. We don't want to add this back when
2474 * the ticket/transaction gets cancelled.
2476 tic->t_curr_res = 0;
2477 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2478 spin_unlock(&log->l_grant_lock);
2479 return XFS_ERROR(EIO);
2480 } /* xlog_grant_log_space */
2484 * Replenish the byte reservation required by moving the grant write head.
2488 STATIC int
2489 xlog_regrant_write_log_space(xlog_t *log,
2490 xlog_ticket_t *tic)
2492 int free_bytes, need_bytes;
2493 xlog_ticket_t *ntic;
2494 #ifdef DEBUG
2495 xfs_lsn_t tail_lsn;
2496 #endif
2498 tic->t_curr_res = tic->t_unit_res;
2499 xlog_tic_reset_res(tic);
2501 if (tic->t_cnt > 0)
2502 return 0;
2504 #ifdef DEBUG
2505 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2506 panic("regrant Recovery problem");
2507 #endif
2509 spin_lock(&log->l_grant_lock);
2511 trace_xfs_log_regrant_write_enter(log, tic);
2513 if (XLOG_FORCED_SHUTDOWN(log))
2514 goto error_return;
2516 /* If there are other waiters on the queue then give them a
2517 * chance at logspace before us. Wake up the first waiters,
2518 * if we do not wake up all the waiters then go to sleep waiting
2519 * for more free space, otherwise try to get some space for
2520 * this transaction.
2522 need_bytes = tic->t_unit_res;
2523 if ((ntic = log->l_write_headq)) {
2524 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2525 log->l_grant_write_bytes);
2526 do {
2527 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2529 if (free_bytes < ntic->t_unit_res)
2530 break;
2531 free_bytes -= ntic->t_unit_res;
2532 sv_signal(&ntic->t_wait);
2533 ntic = ntic->t_next;
2534 } while (ntic != log->l_write_headq);
2536 if (ntic != log->l_write_headq) {
2537 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2538 xlog_ins_ticketq(&log->l_write_headq, tic);
2540 trace_xfs_log_regrant_write_sleep1(log, tic);
2542 spin_unlock(&log->l_grant_lock);
2543 xlog_grant_push_ail(log->l_mp, need_bytes);
2544 spin_lock(&log->l_grant_lock);
2546 XFS_STATS_INC(xs_sleep_logspace);
2547 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
2548 &log->l_grant_lock, s);
2550 /* If we're shutting down, this tic is already
2551 * off the queue */
2552 spin_lock(&log->l_grant_lock);
2553 if (XLOG_FORCED_SHUTDOWN(log))
2554 goto error_return;
2556 trace_xfs_log_regrant_write_wake1(log, tic);
2560 redo:
2561 if (XLOG_FORCED_SHUTDOWN(log))
2562 goto error_return;
2564 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2565 log->l_grant_write_bytes);
2566 if (free_bytes < need_bytes) {
2567 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2568 xlog_ins_ticketq(&log->l_write_headq, tic);
2569 spin_unlock(&log->l_grant_lock);
2570 xlog_grant_push_ail(log->l_mp, need_bytes);
2571 spin_lock(&log->l_grant_lock);
2573 XFS_STATS_INC(xs_sleep_logspace);
2574 trace_xfs_log_regrant_write_sleep2(log, tic);
2576 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2578 /* If we're shutting down, this tic is already off the queue */
2579 spin_lock(&log->l_grant_lock);
2580 if (XLOG_FORCED_SHUTDOWN(log))
2581 goto error_return;
2583 trace_xfs_log_regrant_write_wake2(log, tic);
2584 goto redo;
2585 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2586 xlog_del_ticketq(&log->l_write_headq, tic);
2588 /* we've got enough space */
2589 xlog_grant_add_space_write(log, need_bytes);
2590 #ifdef DEBUG
2591 tail_lsn = log->l_tail_lsn;
2592 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2593 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2594 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2596 #endif
2598 trace_xfs_log_regrant_write_exit(log, tic);
2600 xlog_verify_grant_head(log, 1);
2601 spin_unlock(&log->l_grant_lock);
2602 return 0;
2605 error_return:
2606 if (tic->t_flags & XLOG_TIC_IN_Q)
2607 xlog_del_ticketq(&log->l_reserve_headq, tic);
2609 trace_xfs_log_regrant_write_error(log, tic);
2612 * If we are failing, make sure the ticket doesn't have any
2613 * current reservations. We don't want to add this back when
2614 * the ticket/transaction gets cancelled.
2616 tic->t_curr_res = 0;
2617 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2618 spin_unlock(&log->l_grant_lock);
2619 return XFS_ERROR(EIO);
2620 } /* xlog_regrant_write_log_space */
2623 /* The first cnt-1 times through here we don't need to
2624 * move the grant write head because the permanent
2625 * reservation has reserved cnt times the unit amount.
2626 * Release part of current permanent unit reservation and
2627 * reset current reservation to be one units worth. Also
2628 * move grant reservation head forward.
2630 STATIC void
2631 xlog_regrant_reserve_log_space(xlog_t *log,
2632 xlog_ticket_t *ticket)
2634 trace_xfs_log_regrant_reserve_enter(log, ticket);
2636 if (ticket->t_cnt > 0)
2637 ticket->t_cnt--;
2639 spin_lock(&log->l_grant_lock);
2640 xlog_grant_sub_space(log, ticket->t_curr_res);
2641 ticket->t_curr_res = ticket->t_unit_res;
2642 xlog_tic_reset_res(ticket);
2644 trace_xfs_log_regrant_reserve_sub(log, ticket);
2646 xlog_verify_grant_head(log, 1);
2648 /* just return if we still have some of the pre-reserved space */
2649 if (ticket->t_cnt > 0) {
2650 spin_unlock(&log->l_grant_lock);
2651 return;
2654 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
2656 trace_xfs_log_regrant_reserve_exit(log, ticket);
2658 xlog_verify_grant_head(log, 0);
2659 spin_unlock(&log->l_grant_lock);
2660 ticket->t_curr_res = ticket->t_unit_res;
2661 xlog_tic_reset_res(ticket);
2662 } /* xlog_regrant_reserve_log_space */
2666 * Give back the space left from a reservation.
2668 * All the information we need to make a correct determination of space left
2669 * is present. For non-permanent reservations, things are quite easy. The
2670 * count should have been decremented to zero. We only need to deal with the
2671 * space remaining in the current reservation part of the ticket. If the
2672 * ticket contains a permanent reservation, there may be left over space which
2673 * needs to be released. A count of N means that N-1 refills of the current
2674 * reservation can be done before we need to ask for more space. The first
2675 * one goes to fill up the first current reservation. Once we run out of
2676 * space, the count will stay at zero and the only space remaining will be
2677 * in the current reservation field.
2679 STATIC void
2680 xlog_ungrant_log_space(xlog_t *log,
2681 xlog_ticket_t *ticket)
2683 if (ticket->t_cnt > 0)
2684 ticket->t_cnt--;
2686 spin_lock(&log->l_grant_lock);
2687 trace_xfs_log_ungrant_enter(log, ticket);
2689 xlog_grant_sub_space(log, ticket->t_curr_res);
2691 trace_xfs_log_ungrant_sub(log, ticket);
2693 /* If this is a permanent reservation ticket, we may be able to free
2694 * up more space based on the remaining count.
2696 if (ticket->t_cnt > 0) {
2697 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2698 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
2701 trace_xfs_log_ungrant_exit(log, ticket);
2703 xlog_verify_grant_head(log, 1);
2704 spin_unlock(&log->l_grant_lock);
2705 xfs_log_move_tail(log->l_mp, 1);
2706 } /* xlog_ungrant_log_space */
2710 * Flush iclog to disk if this is the last reference to the given iclog and
2711 * the WANT_SYNC bit is set.
2713 * When this function is entered, the iclog is not necessarily in the
2714 * WANT_SYNC state. It may be sitting around waiting to get filled.
2718 STATIC int
2719 xlog_state_release_iclog(
2720 xlog_t *log,
2721 xlog_in_core_t *iclog)
2723 int sync = 0; /* do we sync? */
2725 if (iclog->ic_state & XLOG_STATE_IOERROR)
2726 return XFS_ERROR(EIO);
2728 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2729 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2730 return 0;
2732 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2733 spin_unlock(&log->l_icloglock);
2734 return XFS_ERROR(EIO);
2736 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2737 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2739 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2740 /* update tail before writing to iclog */
2741 xlog_assign_tail_lsn(log->l_mp);
2742 sync++;
2743 iclog->ic_state = XLOG_STATE_SYNCING;
2744 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
2745 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2746 /* cycle incremented when incrementing curr_block */
2748 spin_unlock(&log->l_icloglock);
2751 * We let the log lock go, so it's possible that we hit a log I/O
2752 * error or some other SHUTDOWN condition that marks the iclog
2753 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2754 * this iclog has consistent data, so we ignore IOERROR
2755 * flags after this point.
2757 if (sync)
2758 return xlog_sync(log, iclog);
2759 return 0;
2760 } /* xlog_state_release_iclog */
2764 * This routine will mark the current iclog in the ring as WANT_SYNC
2765 * and move the current iclog pointer to the next iclog in the ring.
2766 * When this routine is called from xlog_state_get_iclog_space(), the
2767 * exact size of the iclog has not yet been determined. All we know is
2768 * that every data block. We have run out of space in this log record.
2770 STATIC void
2771 xlog_state_switch_iclogs(xlog_t *log,
2772 xlog_in_core_t *iclog,
2773 int eventual_size)
2775 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2776 if (!eventual_size)
2777 eventual_size = iclog->ic_offset;
2778 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2779 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
2780 log->l_prev_block = log->l_curr_block;
2781 log->l_prev_cycle = log->l_curr_cycle;
2783 /* roll log?: ic_offset changed later */
2784 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2786 /* Round up to next log-sunit */
2787 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
2788 log->l_mp->m_sb.sb_logsunit > 1) {
2789 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2790 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2793 if (log->l_curr_block >= log->l_logBBsize) {
2794 log->l_curr_cycle++;
2795 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2796 log->l_curr_cycle++;
2797 log->l_curr_block -= log->l_logBBsize;
2798 ASSERT(log->l_curr_block >= 0);
2800 ASSERT(iclog == log->l_iclog);
2801 log->l_iclog = iclog->ic_next;
2802 } /* xlog_state_switch_iclogs */
2805 * Write out all data in the in-core log as of this exact moment in time.
2807 * Data may be written to the in-core log during this call. However,
2808 * we don't guarantee this data will be written out. A change from past
2809 * implementation means this routine will *not* write out zero length LRs.
2811 * Basically, we try and perform an intelligent scan of the in-core logs.
2812 * If we determine there is no flushable data, we just return. There is no
2813 * flushable data if:
2815 * 1. the current iclog is active and has no data; the previous iclog
2816 * is in the active or dirty state.
2817 * 2. the current iclog is drity, and the previous iclog is in the
2818 * active or dirty state.
2820 * We may sleep if:
2822 * 1. the current iclog is not in the active nor dirty state.
2823 * 2. the current iclog dirty, and the previous iclog is not in the
2824 * active nor dirty state.
2825 * 3. the current iclog is active, and there is another thread writing
2826 * to this particular iclog.
2827 * 4. a) the current iclog is active and has no other writers
2828 * b) when we return from flushing out this iclog, it is still
2829 * not in the active nor dirty state.
2832 _xfs_log_force(
2833 struct xfs_mount *mp,
2834 uint flags,
2835 int *log_flushed)
2837 struct log *log = mp->m_log;
2838 struct xlog_in_core *iclog;
2839 xfs_lsn_t lsn;
2841 XFS_STATS_INC(xs_log_force);
2843 spin_lock(&log->l_icloglock);
2845 iclog = log->l_iclog;
2846 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2847 spin_unlock(&log->l_icloglock);
2848 return XFS_ERROR(EIO);
2851 /* If the head iclog is not active nor dirty, we just attach
2852 * ourselves to the head and go to sleep.
2854 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2855 iclog->ic_state == XLOG_STATE_DIRTY) {
2857 * If the head is dirty or (active and empty), then
2858 * we need to look at the previous iclog. If the previous
2859 * iclog is active or dirty we are done. There is nothing
2860 * to sync out. Otherwise, we attach ourselves to the
2861 * previous iclog and go to sleep.
2863 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2864 (atomic_read(&iclog->ic_refcnt) == 0
2865 && iclog->ic_offset == 0)) {
2866 iclog = iclog->ic_prev;
2867 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2868 iclog->ic_state == XLOG_STATE_DIRTY)
2869 goto no_sleep;
2870 else
2871 goto maybe_sleep;
2872 } else {
2873 if (atomic_read(&iclog->ic_refcnt) == 0) {
2874 /* We are the only one with access to this
2875 * iclog. Flush it out now. There should
2876 * be a roundoff of zero to show that someone
2877 * has already taken care of the roundoff from
2878 * the previous sync.
2880 atomic_inc(&iclog->ic_refcnt);
2881 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2882 xlog_state_switch_iclogs(log, iclog, 0);
2883 spin_unlock(&log->l_icloglock);
2885 if (xlog_state_release_iclog(log, iclog))
2886 return XFS_ERROR(EIO);
2888 if (log_flushed)
2889 *log_flushed = 1;
2890 spin_lock(&log->l_icloglock);
2891 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
2892 iclog->ic_state != XLOG_STATE_DIRTY)
2893 goto maybe_sleep;
2894 else
2895 goto no_sleep;
2896 } else {
2897 /* Someone else is writing to this iclog.
2898 * Use its call to flush out the data. However,
2899 * the other thread may not force out this LR,
2900 * so we mark it WANT_SYNC.
2902 xlog_state_switch_iclogs(log, iclog, 0);
2903 goto maybe_sleep;
2908 /* By the time we come around again, the iclog could've been filled
2909 * which would give it another lsn. If we have a new lsn, just
2910 * return because the relevant data has been flushed.
2912 maybe_sleep:
2913 if (flags & XFS_LOG_SYNC) {
2915 * We must check if we're shutting down here, before
2916 * we wait, while we're holding the l_icloglock.
2917 * Then we check again after waking up, in case our
2918 * sleep was disturbed by a bad news.
2920 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2921 spin_unlock(&log->l_icloglock);
2922 return XFS_ERROR(EIO);
2924 XFS_STATS_INC(xs_log_force_sleep);
2925 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
2927 * No need to grab the log lock here since we're
2928 * only deciding whether or not to return EIO
2929 * and the memory read should be atomic.
2931 if (iclog->ic_state & XLOG_STATE_IOERROR)
2932 return XFS_ERROR(EIO);
2933 if (log_flushed)
2934 *log_flushed = 1;
2935 } else {
2937 no_sleep:
2938 spin_unlock(&log->l_icloglock);
2940 return 0;
2944 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2945 * about errors or whether the log was flushed or not. This is the normal
2946 * interface to use when trying to unpin items or move the log forward.
2948 void
2949 xfs_log_force(
2950 xfs_mount_t *mp,
2951 uint flags)
2953 int error;
2955 error = _xfs_log_force(mp, flags, NULL);
2956 if (error) {
2957 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
2958 "error %d returned.", error);
2963 * Force the in-core log to disk for a specific LSN.
2965 * Find in-core log with lsn.
2966 * If it is in the DIRTY state, just return.
2967 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2968 * state and go to sleep or return.
2969 * If it is in any other state, go to sleep or return.
2971 * Synchronous forces are implemented with a signal variable. All callers
2972 * to force a given lsn to disk will wait on a the sv attached to the
2973 * specific in-core log. When given in-core log finally completes its
2974 * write to disk, that thread will wake up all threads waiting on the
2975 * sv.
2978 _xfs_log_force_lsn(
2979 struct xfs_mount *mp,
2980 xfs_lsn_t lsn,
2981 uint flags,
2982 int *log_flushed)
2984 struct log *log = mp->m_log;
2985 struct xlog_in_core *iclog;
2986 int already_slept = 0;
2988 ASSERT(lsn != 0);
2990 XFS_STATS_INC(xs_log_force);
2992 try_again:
2993 spin_lock(&log->l_icloglock);
2994 iclog = log->l_iclog;
2995 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2996 spin_unlock(&log->l_icloglock);
2997 return XFS_ERROR(EIO);
3000 do {
3001 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3002 iclog = iclog->ic_next;
3003 continue;
3006 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3007 spin_unlock(&log->l_icloglock);
3008 return 0;
3011 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3013 * We sleep here if we haven't already slept (e.g.
3014 * this is the first time we've looked at the correct
3015 * iclog buf) and the buffer before us is going to
3016 * be sync'ed. The reason for this is that if we
3017 * are doing sync transactions here, by waiting for
3018 * the previous I/O to complete, we can allow a few
3019 * more transactions into this iclog before we close
3020 * it down.
3022 * Otherwise, we mark the buffer WANT_SYNC, and bump
3023 * up the refcnt so we can release the log (which
3024 * drops the ref count). The state switch keeps new
3025 * transaction commits from using this buffer. When
3026 * the current commits finish writing into the buffer,
3027 * the refcount will drop to zero and the buffer will
3028 * go out then.
3030 if (!already_slept &&
3031 (iclog->ic_prev->ic_state &
3032 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3033 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3035 XFS_STATS_INC(xs_log_force_sleep);
3037 sv_wait(&iclog->ic_prev->ic_write_wait,
3038 PSWP, &log->l_icloglock, s);
3039 if (log_flushed)
3040 *log_flushed = 1;
3041 already_slept = 1;
3042 goto try_again;
3044 atomic_inc(&iclog->ic_refcnt);
3045 xlog_state_switch_iclogs(log, iclog, 0);
3046 spin_unlock(&log->l_icloglock);
3047 if (xlog_state_release_iclog(log, iclog))
3048 return XFS_ERROR(EIO);
3049 if (log_flushed)
3050 *log_flushed = 1;
3051 spin_lock(&log->l_icloglock);
3054 if ((flags & XFS_LOG_SYNC) && /* sleep */
3055 !(iclog->ic_state &
3056 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3058 * Don't wait on completion if we know that we've
3059 * gotten a log write error.
3061 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3062 spin_unlock(&log->l_icloglock);
3063 return XFS_ERROR(EIO);
3065 XFS_STATS_INC(xs_log_force_sleep);
3066 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3068 * No need to grab the log lock here since we're
3069 * only deciding whether or not to return EIO
3070 * and the memory read should be atomic.
3072 if (iclog->ic_state & XLOG_STATE_IOERROR)
3073 return XFS_ERROR(EIO);
3075 if (log_flushed)
3076 *log_flushed = 1;
3077 } else { /* just return */
3078 spin_unlock(&log->l_icloglock);
3081 return 0;
3082 } while (iclog != log->l_iclog);
3084 spin_unlock(&log->l_icloglock);
3085 return 0;
3089 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3090 * about errors or whether the log was flushed or not. This is the normal
3091 * interface to use when trying to unpin items or move the log forward.
3093 void
3094 xfs_log_force_lsn(
3095 xfs_mount_t *mp,
3096 xfs_lsn_t lsn,
3097 uint flags)
3099 int error;
3101 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3102 if (error) {
3103 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3104 "error %d returned.", error);
3109 * Called when we want to mark the current iclog as being ready to sync to
3110 * disk.
3112 STATIC void
3113 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3115 assert_spin_locked(&log->l_icloglock);
3117 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3118 xlog_state_switch_iclogs(log, iclog, 0);
3119 } else {
3120 ASSERT(iclog->ic_state &
3121 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3126 /*****************************************************************************
3128 * TICKET functions
3130 *****************************************************************************
3134 * Free a used ticket when its refcount falls to zero.
3136 void
3137 xfs_log_ticket_put(
3138 xlog_ticket_t *ticket)
3140 ASSERT(atomic_read(&ticket->t_ref) > 0);
3141 if (atomic_dec_and_test(&ticket->t_ref)) {
3142 sv_destroy(&ticket->t_wait);
3143 kmem_zone_free(xfs_log_ticket_zone, ticket);
3147 xlog_ticket_t *
3148 xfs_log_ticket_get(
3149 xlog_ticket_t *ticket)
3151 ASSERT(atomic_read(&ticket->t_ref) > 0);
3152 atomic_inc(&ticket->t_ref);
3153 return ticket;
3157 * Allocate and initialise a new log ticket.
3159 STATIC xlog_ticket_t *
3160 xlog_ticket_alloc(xlog_t *log,
3161 int unit_bytes,
3162 int cnt,
3163 char client,
3164 uint xflags)
3166 xlog_ticket_t *tic;
3167 uint num_headers;
3169 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3170 if (!tic)
3171 return NULL;
3174 * Permanent reservations have up to 'cnt'-1 active log operations
3175 * in the log. A unit in this case is the amount of space for one
3176 * of these log operations. Normal reservations have a cnt of 1
3177 * and their unit amount is the total amount of space required.
3179 * The following lines of code account for non-transaction data
3180 * which occupy space in the on-disk log.
3182 * Normal form of a transaction is:
3183 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3184 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3186 * We need to account for all the leadup data and trailer data
3187 * around the transaction data.
3188 * And then we need to account for the worst case in terms of using
3189 * more space.
3190 * The worst case will happen if:
3191 * - the placement of the transaction happens to be such that the
3192 * roundoff is at its maximum
3193 * - the transaction data is synced before the commit record is synced
3194 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3195 * Therefore the commit record is in its own Log Record.
3196 * This can happen as the commit record is called with its
3197 * own region to xlog_write().
3198 * This then means that in the worst case, roundoff can happen for
3199 * the commit-rec as well.
3200 * The commit-rec is smaller than padding in this scenario and so it is
3201 * not added separately.
3204 /* for trans header */
3205 unit_bytes += sizeof(xlog_op_header_t);
3206 unit_bytes += sizeof(xfs_trans_header_t);
3208 /* for start-rec */
3209 unit_bytes += sizeof(xlog_op_header_t);
3211 /* for LR headers */
3212 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3213 unit_bytes += log->l_iclog_hsize * num_headers;
3215 /* for commit-rec LR header - note: padding will subsume the ophdr */
3216 unit_bytes += log->l_iclog_hsize;
3218 /* for split-recs - ophdrs added when data split over LRs */
3219 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3221 /* for roundoff padding for transaction data and one for commit record */
3222 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
3223 log->l_mp->m_sb.sb_logsunit > 1) {
3224 /* log su roundoff */
3225 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3226 } else {
3227 /* BB roundoff */
3228 unit_bytes += 2*BBSIZE;
3231 atomic_set(&tic->t_ref, 1);
3232 tic->t_unit_res = unit_bytes;
3233 tic->t_curr_res = unit_bytes;
3234 tic->t_cnt = cnt;
3235 tic->t_ocnt = cnt;
3236 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3237 tic->t_clientid = client;
3238 tic->t_flags = XLOG_TIC_INITED;
3239 tic->t_trans_type = 0;
3240 if (xflags & XFS_LOG_PERM_RESERV)
3241 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3242 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
3244 xlog_tic_reset_res(tic);
3246 return tic;
3250 /******************************************************************************
3252 * Log debug routines
3254 ******************************************************************************
3256 #if defined(DEBUG)
3258 * Make sure that the destination ptr is within the valid data region of
3259 * one of the iclogs. This uses backup pointers stored in a different
3260 * part of the log in case we trash the log structure.
3262 void
3263 xlog_verify_dest_ptr(xlog_t *log,
3264 __psint_t ptr)
3266 int i;
3267 int good_ptr = 0;
3269 for (i=0; i < log->l_iclog_bufs; i++) {
3270 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3271 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3272 good_ptr++;
3274 if (! good_ptr)
3275 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3276 } /* xlog_verify_dest_ptr */
3278 STATIC void
3279 xlog_verify_grant_head(xlog_t *log, int equals)
3281 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3282 if (equals)
3283 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3284 else
3285 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3286 } else {
3287 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3288 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3290 } /* xlog_verify_grant_head */
3292 /* check if it will fit */
3293 STATIC void
3294 xlog_verify_tail_lsn(xlog_t *log,
3295 xlog_in_core_t *iclog,
3296 xfs_lsn_t tail_lsn)
3298 int blocks;
3300 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3301 blocks =
3302 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3303 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3304 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3305 } else {
3306 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3308 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3309 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3311 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3312 if (blocks < BTOBB(iclog->ic_offset) + 1)
3313 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3315 } /* xlog_verify_tail_lsn */
3318 * Perform a number of checks on the iclog before writing to disk.
3320 * 1. Make sure the iclogs are still circular
3321 * 2. Make sure we have a good magic number
3322 * 3. Make sure we don't have magic numbers in the data
3323 * 4. Check fields of each log operation header for:
3324 * A. Valid client identifier
3325 * B. tid ptr value falls in valid ptr space (user space code)
3326 * C. Length in log record header is correct according to the
3327 * individual operation headers within record.
3328 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3329 * log, check the preceding blocks of the physical log to make sure all
3330 * the cycle numbers agree with the current cycle number.
3332 STATIC void
3333 xlog_verify_iclog(xlog_t *log,
3334 xlog_in_core_t *iclog,
3335 int count,
3336 boolean_t syncing)
3338 xlog_op_header_t *ophead;
3339 xlog_in_core_t *icptr;
3340 xlog_in_core_2_t *xhdr;
3341 xfs_caddr_t ptr;
3342 xfs_caddr_t base_ptr;
3343 __psint_t field_offset;
3344 __uint8_t clientid;
3345 int len, i, j, k, op_len;
3346 int idx;
3348 /* check validity of iclog pointers */
3349 spin_lock(&log->l_icloglock);
3350 icptr = log->l_iclog;
3351 for (i=0; i < log->l_iclog_bufs; i++) {
3352 if (icptr == NULL)
3353 xlog_panic("xlog_verify_iclog: invalid ptr");
3354 icptr = icptr->ic_next;
3356 if (icptr != log->l_iclog)
3357 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3358 spin_unlock(&log->l_icloglock);
3360 /* check log magic numbers */
3361 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
3362 xlog_panic("xlog_verify_iclog: invalid magic num");
3364 ptr = (xfs_caddr_t) &iclog->ic_header;
3365 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
3366 ptr += BBSIZE) {
3367 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
3368 xlog_panic("xlog_verify_iclog: unexpected magic num");
3371 /* check fields */
3372 len = be32_to_cpu(iclog->ic_header.h_num_logops);
3373 ptr = iclog->ic_datap;
3374 base_ptr = ptr;
3375 ophead = (xlog_op_header_t *)ptr;
3376 xhdr = iclog->ic_data;
3377 for (i = 0; i < len; i++) {
3378 ophead = (xlog_op_header_t *)ptr;
3380 /* clientid is only 1 byte */
3381 field_offset = (__psint_t)
3382 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3383 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3384 clientid = ophead->oh_clientid;
3385 } else {
3386 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3387 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3388 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3389 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3390 clientid = xlog_get_client_id(
3391 xhdr[j].hic_xheader.xh_cycle_data[k]);
3392 } else {
3393 clientid = xlog_get_client_id(
3394 iclog->ic_header.h_cycle_data[idx]);
3397 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3398 cmn_err(CE_WARN, "xlog_verify_iclog: "
3399 "invalid clientid %d op 0x%p offset 0x%lx",
3400 clientid, ophead, (unsigned long)field_offset);
3402 /* check length */
3403 field_offset = (__psint_t)
3404 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3405 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3406 op_len = be32_to_cpu(ophead->oh_len);
3407 } else {
3408 idx = BTOBBT((__psint_t)&ophead->oh_len -
3409 (__psint_t)iclog->ic_datap);
3410 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3411 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3412 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3413 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
3414 } else {
3415 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
3418 ptr += sizeof(xlog_op_header_t) + op_len;
3420 } /* xlog_verify_iclog */
3421 #endif
3424 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3426 STATIC int
3427 xlog_state_ioerror(
3428 xlog_t *log)
3430 xlog_in_core_t *iclog, *ic;
3432 iclog = log->l_iclog;
3433 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3435 * Mark all the incore logs IOERROR.
3436 * From now on, no log flushes will result.
3438 ic = iclog;
3439 do {
3440 ic->ic_state = XLOG_STATE_IOERROR;
3441 ic = ic->ic_next;
3442 } while (ic != iclog);
3443 return 0;
3446 * Return non-zero, if state transition has already happened.
3448 return 1;
3452 * This is called from xfs_force_shutdown, when we're forcibly
3453 * shutting down the filesystem, typically because of an IO error.
3454 * Our main objectives here are to make sure that:
3455 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3456 * parties to find out, 'atomically'.
3457 * b. those who're sleeping on log reservations, pinned objects and
3458 * other resources get woken up, and be told the bad news.
3459 * c. nothing new gets queued up after (a) and (b) are done.
3460 * d. if !logerror, flush the iclogs to disk, then seal them off
3461 * for business.
3464 xfs_log_force_umount(
3465 struct xfs_mount *mp,
3466 int logerror)
3468 xlog_ticket_t *tic;
3469 xlog_t *log;
3470 int retval;
3472 log = mp->m_log;
3475 * If this happens during log recovery, don't worry about
3476 * locking; the log isn't open for business yet.
3478 if (!log ||
3479 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3480 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3481 if (mp->m_sb_bp)
3482 XFS_BUF_DONE(mp->m_sb_bp);
3483 return 0;
3487 * Somebody could've already done the hard work for us.
3488 * No need to get locks for this.
3490 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3491 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3492 return 1;
3494 retval = 0;
3496 * We must hold both the GRANT lock and the LOG lock,
3497 * before we mark the filesystem SHUTDOWN and wake
3498 * everybody up to tell the bad news.
3500 spin_lock(&log->l_icloglock);
3501 spin_lock(&log->l_grant_lock);
3502 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3503 if (mp->m_sb_bp)
3504 XFS_BUF_DONE(mp->m_sb_bp);
3507 * This flag is sort of redundant because of the mount flag, but
3508 * it's good to maintain the separation between the log and the rest
3509 * of XFS.
3511 log->l_flags |= XLOG_IO_ERROR;
3514 * If we hit a log error, we want to mark all the iclogs IOERROR
3515 * while we're still holding the loglock.
3517 if (logerror)
3518 retval = xlog_state_ioerror(log);
3519 spin_unlock(&log->l_icloglock);
3522 * We don't want anybody waiting for log reservations
3523 * after this. That means we have to wake up everybody
3524 * queued up on reserve_headq as well as write_headq.
3525 * In addition, we make sure in xlog_{re}grant_log_space
3526 * that we don't enqueue anything once the SHUTDOWN flag
3527 * is set, and this action is protected by the GRANTLOCK.
3529 if ((tic = log->l_reserve_headq)) {
3530 do {
3531 sv_signal(&tic->t_wait);
3532 tic = tic->t_next;
3533 } while (tic != log->l_reserve_headq);
3536 if ((tic = log->l_write_headq)) {
3537 do {
3538 sv_signal(&tic->t_wait);
3539 tic = tic->t_next;
3540 } while (tic != log->l_write_headq);
3542 spin_unlock(&log->l_grant_lock);
3544 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3545 ASSERT(!logerror);
3547 * Force the incore logs to disk before shutting the
3548 * log down completely.
3550 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3552 spin_lock(&log->l_icloglock);
3553 retval = xlog_state_ioerror(log);
3554 spin_unlock(&log->l_icloglock);
3557 * Wake up everybody waiting on xfs_log_force.
3558 * Callback all log item committed functions as if the
3559 * log writes were completed.
3561 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3563 #ifdef XFSERRORDEBUG
3565 xlog_in_core_t *iclog;
3567 spin_lock(&log->l_icloglock);
3568 iclog = log->l_iclog;
3569 do {
3570 ASSERT(iclog->ic_callback == 0);
3571 iclog = iclog->ic_next;
3572 } while (iclog != log->l_iclog);
3573 spin_unlock(&log->l_icloglock);
3575 #endif
3576 /* return non-zero if log IOERROR transition had already happened */
3577 return retval;
3580 STATIC int
3581 xlog_iclogs_empty(xlog_t *log)
3583 xlog_in_core_t *iclog;
3585 iclog = log->l_iclog;
3586 do {
3587 /* endianness does not matter here, zero is zero in
3588 * any language.
3590 if (iclog->ic_header.h_num_logops)
3591 return 0;
3592 iclog = iclog->ic_next;
3593 } while (iclog != log->l_iclog);
3594 return 1;