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