blob: b9d3ad35240ee9960d47a293c32c8e8e6af4a274 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33/*
34 * High level interface routines for log manager
35 */
36
37#include "xfs.h"
38#include "xfs_macros.h"
39#include "xfs_types.h"
40#include "xfs_inum.h"
41#include "xfs_ag.h"
42#include "xfs_sb.h"
43#include "xfs_log.h"
44#include "xfs_trans.h"
45#include "xfs_dir.h"
46#include "xfs_dmapi.h"
47#include "xfs_mount.h"
48#include "xfs_error.h"
49#include "xfs_log_priv.h"
50#include "xfs_buf_item.h"
51#include "xfs_alloc_btree.h"
52#include "xfs_log_recover.h"
53#include "xfs_bit.h"
54#include "xfs_rw.h"
55#include "xfs_trans_priv.h"
56
57
58#define xlog_write_adv_cnt(ptr, len, off, bytes) \
59 { (ptr) += (bytes); \
60 (len) -= (bytes); \
61 (off) += (bytes);}
62
63/* Local miscellaneous function prototypes */
64STATIC int xlog_bdstrat_cb(struct xfs_buf *);
65STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
66 xlog_in_core_t **, xfs_lsn_t *);
67STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
68 xfs_buftarg_t *log_target,
69 xfs_daddr_t blk_offset,
70 int num_bblks);
71STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
72STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
73STATIC void xlog_unalloc_log(xlog_t *log);
74STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
75 int nentries, xfs_log_ticket_t tic,
76 xfs_lsn_t *start_lsn,
77 xlog_in_core_t **commit_iclog,
78 uint flags);
79
80/* local state machine functions */
81STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
82STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
83STATIC int xlog_state_get_iclog_space(xlog_t *log,
84 int len,
85 xlog_in_core_t **iclog,
86 xlog_ticket_t *ticket,
87 int *continued_write,
88 int *logoffsetp);
89STATIC void xlog_state_put_ticket(xlog_t *log,
90 xlog_ticket_t *tic);
91STATIC int xlog_state_release_iclog(xlog_t *log,
92 xlog_in_core_t *iclog);
93STATIC void xlog_state_switch_iclogs(xlog_t *log,
94 xlog_in_core_t *iclog,
95 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110096STATIC int xlog_state_sync(xlog_t *log,
97 xfs_lsn_t lsn,
98 uint flags,
99 int *log_flushed);
100STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
102
103/* local functions to manipulate grant head */
104STATIC int xlog_grant_log_space(xlog_t *log,
105 xlog_ticket_t *xtic);
106STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
107 int need_bytes);
108STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
109 xlog_ticket_t *ticket);
110STATIC int xlog_regrant_write_log_space(xlog_t *log,
111 xlog_ticket_t *ticket);
112STATIC void xlog_ungrant_log_space(xlog_t *log,
113 xlog_ticket_t *ticket);
114
115
116/* local ticket functions */
117STATIC void xlog_state_ticket_alloc(xlog_t *log);
118STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
119 int unit_bytes,
120 int count,
121 char clientid,
122 uint flags);
123STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
124
125/* local debug functions */
126#if defined(DEBUG) && !defined(XLOG_NOLOG)
127STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
128STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
129STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
130 int count, boolean_t syncing);
131STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
132 xfs_lsn_t tail_lsn);
133#else
134#define xlog_verify_dest_ptr(a,b)
135#define xlog_verify_grant_head(a,b)
136#define xlog_verify_iclog(a,b,c,d)
137#define xlog_verify_tail_lsn(a,b,c)
138#endif
139
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000140STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142#ifdef DEBUG
143int xlog_do_error = 0;
144int xlog_req_num = 0;
145int xlog_error_mod = 33;
146#endif
147
148#define XLOG_FORCED_SHUTDOWN(log) (log->l_flags & XLOG_IO_ERROR)
149
150/*
151 * 0 => disable log manager
152 * 1 => enable log manager
153 * 2 => enable log manager and log debugging
154 */
155#if defined(XLOG_NOLOG) || defined(DEBUG)
156int xlog_debug = 1;
157xfs_buftarg_t *xlog_target;
158#endif
159
160#if defined(XFS_LOG_TRACE)
161
162void
163xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
164{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000165 unsigned long cnts;
166
167 if (!log->l_grant_trace) {
168 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
169 if (!log->l_grant_trace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return;
171 }
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000172 /* ticket counts are 1 byte each */
173 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 ktrace_enter(log->l_grant_trace,
176 (void *)tic,
177 (void *)log->l_reserve_headq,
178 (void *)log->l_write_headq,
179 (void *)((unsigned long)log->l_grant_reserve_cycle),
180 (void *)((unsigned long)log->l_grant_reserve_bytes),
181 (void *)((unsigned long)log->l_grant_write_cycle),
182 (void *)((unsigned long)log->l_grant_write_bytes),
183 (void *)((unsigned long)log->l_curr_cycle),
184 (void *)((unsigned long)log->l_curr_block),
185 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
186 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
187 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000188 (void *)((unsigned long)tic->t_trans_type),
189 (void *)cnts,
190 (void *)((unsigned long)tic->t_curr_res),
191 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194void
195xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
196{
197 pid_t pid;
198
199 pid = current_pid();
200
201 if (!iclog->ic_trace)
202 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
203 ktrace_enter(iclog->ic_trace,
204 (void *)((unsigned long)state),
205 (void *)((unsigned long)pid),
206 (void *)0,
207 (void *)0,
208 (void *)0,
209 (void *)0,
210 (void *)0,
211 (void *)0,
212 (void *)0,
213 (void *)0,
214 (void *)0,
215 (void *)0,
216 (void *)0,
217 (void *)0,
218 (void *)0,
219 (void *)0);
220}
221
222#else
223#define xlog_trace_loggrant(log,tic,string)
224#define xlog_trace_iclog(iclog,state)
225#endif /* XFS_LOG_TRACE */
226
227/*
228 * NOTES:
229 *
230 * 1. currblock field gets updated at startup and after in-core logs
231 * marked as with WANT_SYNC.
232 */
233
234/*
235 * This routine is called when a user of a log manager ticket is done with
236 * the reservation. If the ticket was ever used, then a commit record for
237 * the associated transaction is written out as a log operation header with
238 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
239 * a given ticket. If the ticket was one with a permanent reservation, then
240 * a few operations are done differently. Permanent reservation tickets by
241 * default don't release the reservation. They just commit the current
242 * transaction with the belief that the reservation is still needed. A flag
243 * must be passed in before permanent reservations are actually released.
244 * When these type of tickets are not released, they need to be set into
245 * the inited state again. By doing this, a start record will be written
246 * out when the next write occurs.
247 */
248xfs_lsn_t
249xfs_log_done(xfs_mount_t *mp,
250 xfs_log_ticket_t xtic,
251 void **iclog,
252 uint flags)
253{
254 xlog_t *log = mp->m_log;
255 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
256 xfs_lsn_t lsn = 0;
257
258#if defined(DEBUG) || defined(XLOG_NOLOG)
259 if (!xlog_debug && xlog_target == log->l_targ)
260 return 0;
261#endif
262
263 if (XLOG_FORCED_SHUTDOWN(log) ||
264 /*
265 * If nothing was ever written, don't write out commit record.
266 * If we get an error, just continue and give back the log ticket.
267 */
268 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
269 (xlog_commit_record(mp, ticket,
270 (xlog_in_core_t **)iclog, &lsn)))) {
271 lsn = (xfs_lsn_t) -1;
272 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
273 flags |= XFS_LOG_REL_PERM_RESERV;
274 }
275 }
276
277
278 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
279 (flags & XFS_LOG_REL_PERM_RESERV)) {
280 /*
281 * Release ticket if not permanent reservation or a specifc
282 * request has been made to release a permanent reservation.
283 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000284 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 xlog_ungrant_log_space(log, ticket);
286 xlog_state_put_ticket(log, ticket);
287 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000288 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xlog_regrant_reserve_log_space(log, ticket);
290 }
291
292 /* If this ticket was a permanent reservation and we aren't
293 * trying to release it, reset the inited flags; so next time
294 * we write, a start record will be written out.
295 */
296 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
297 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
298 ticket->t_flags |= XLOG_TIC_INITED;
299
300 return lsn;
301} /* xfs_log_done */
302
303
304/*
305 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
306 * the force is done synchronously.
307 *
308 * Asynchronous forces are implemented by setting the WANT_SYNC
309 * bit in the appropriate in-core log and then returning.
310 *
311 * Synchronous forces are implemented with a semaphore. All callers
312 * to force a given lsn to disk will wait on a semaphore attached to the
313 * specific in-core log. When given in-core log finally completes its
314 * write to disk, that thread will wake up all threads waiting on the
315 * semaphore.
316 */
317int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100318_xfs_log_force(
319 xfs_mount_t *mp,
320 xfs_lsn_t lsn,
321 uint flags,
322 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100324 xlog_t *log = mp->m_log;
325 int dummy;
326
327 if (!log_flushed)
328 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330#if defined(DEBUG) || defined(XLOG_NOLOG)
331 if (!xlog_debug && xlog_target == log->l_targ)
332 return 0;
333#endif
334
335 ASSERT(flags & XFS_LOG_FORCE);
336
337 XFS_STATS_INC(xs_log_force);
338
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100339 if (log->l_flags & XLOG_IO_ERROR)
340 return XFS_ERROR(EIO);
341 if (lsn == 0)
342 return xlog_state_sync_all(log, flags, log_flushed);
343 else
344 return xlog_state_sync(log, lsn, flags, log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345} /* xfs_log_force */
346
347/*
348 * Attaches a new iclog I/O completion callback routine during
349 * transaction commit. If the log is in error state, a non-zero
350 * return code is handed back and the caller is responsible for
351 * executing the callback at an appropriate time.
352 */
353int
354xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
355 void *iclog_hndl, /* iclog to hang callback off */
356 xfs_log_callback_t *cb)
357{
358 xlog_t *log = mp->m_log;
359 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
360 int abortflg, spl;
361
362#if defined(DEBUG) || defined(XLOG_NOLOG)
363 if (!xlog_debug && xlog_target == log->l_targ)
364 return 0;
365#endif
366 cb->cb_next = NULL;
367 spl = LOG_LOCK(log);
368 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
369 if (!abortflg) {
370 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
371 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
372 cb->cb_next = NULL;
373 *(iclog->ic_callback_tail) = cb;
374 iclog->ic_callback_tail = &(cb->cb_next);
375 }
376 LOG_UNLOCK(log, spl);
377 return abortflg;
378} /* xfs_log_notify */
379
380int
381xfs_log_release_iclog(xfs_mount_t *mp,
382 void *iclog_hndl)
383{
384 xlog_t *log = mp->m_log;
385 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
386
387 if (xlog_state_release_iclog(log, iclog)) {
388 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
389 return(EIO);
390 }
391
392 return 0;
393}
394
395/*
396 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
397 * to the reservation.
398 * 2. Potentially, push buffers at tail of log to disk.
399 *
400 * Each reservation is going to reserve extra space for a log record header.
401 * When writes happen to the on-disk log, we don't subtract the length of the
402 * log record header from any reservation. By wasting space in each
403 * reservation, we prevent over allocation problems.
404 */
405int
406xfs_log_reserve(xfs_mount_t *mp,
407 int unit_bytes,
408 int cnt,
409 xfs_log_ticket_t *ticket,
410 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000411 uint flags,
412 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 xlog_t *log = mp->m_log;
415 xlog_ticket_t *internal_ticket;
416 int retval;
417
418#if defined(DEBUG) || defined(XLOG_NOLOG)
419 if (!xlog_debug && xlog_target == log->l_targ)
420 return 0;
421#endif
422 retval = 0;
423 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
424 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
425
426 if (XLOG_FORCED_SHUTDOWN(log))
427 return XFS_ERROR(EIO);
428
429 XFS_STATS_INC(xs_try_logspace);
430
431 if (*ticket != NULL) {
432 ASSERT(flags & XFS_LOG_PERM_RESERV);
433 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000434 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
436 retval = xlog_regrant_write_log_space(log, internal_ticket);
437 } else {
438 /* may sleep if need to allocate more tickets */
439 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
440 client, flags);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000441 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000443 xlog_trace_loggrant(log, internal_ticket,
444 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
445 "xfs_log_reserve: create new ticket (permanent trans)" :
446 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xlog_grant_push_ail(mp,
448 (internal_ticket->t_unit_res *
449 internal_ticket->t_cnt));
450 retval = xlog_grant_log_space(log, internal_ticket);
451 }
452
453 return retval;
454} /* xfs_log_reserve */
455
456
457/*
458 * Mount a log filesystem
459 *
460 * mp - ubiquitous xfs mount point structure
461 * log_target - buftarg of on-disk log device
462 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
463 * num_bblocks - Number of BBSIZE blocks in on-disk log
464 *
465 * Return error or zero.
466 */
467int
468xfs_log_mount(xfs_mount_t *mp,
469 xfs_buftarg_t *log_target,
470 xfs_daddr_t blk_offset,
471 int num_bblks)
472{
473 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
474 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
475 else {
476 cmn_err(CE_NOTE,
477 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
478 mp->m_fsname);
479 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
480 }
481
482 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
483
484#if defined(DEBUG) || defined(XLOG_NOLOG)
485 if (!xlog_debug) {
486 cmn_err(CE_NOTE, "log dev: %s", XFS_BUFTARG_NAME(log_target));
487 return 0;
488 }
489#endif
490 /*
491 * skip log recovery on a norecovery mount. pretend it all
492 * just worked.
493 */
494 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
495 int error;
496 vfs_t *vfsp = XFS_MTOVFS(mp);
497 int readonly = (vfsp->vfs_flag & VFS_RDONLY);
498
499 if (readonly)
500 vfsp->vfs_flag &= ~VFS_RDONLY;
501
502 error = xlog_recover(mp->m_log, readonly);
503
504 if (readonly)
505 vfsp->vfs_flag |= VFS_RDONLY;
506 if (error) {
507 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
508 xlog_unalloc_log(mp->m_log);
509 return error;
510 }
511 }
512
513 /* Normal transactions can now occur */
514 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
515
516 /* End mounting message in xfs_log_mount_finish */
517 return 0;
518} /* xfs_log_mount */
519
520/*
521 * Finish the recovery of the file system. This is separate from
522 * the xfs_log_mount() call, because it depends on the code in
523 * xfs_mountfs() to read in the root and real-time bitmap inodes
524 * between calling xfs_log_mount() and here.
525 *
526 * mp - ubiquitous xfs mount point structure
527 */
528int
529xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
530{
531 int error;
532
533 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
534 error = xlog_recover_finish(mp->m_log, mfsi_flags);
535 else {
536 error = 0;
537 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
538 }
539
540 return error;
541}
542
543/*
544 * Unmount processing for the log.
545 */
546int
547xfs_log_unmount(xfs_mount_t *mp)
548{
549 int error;
550
551 error = xfs_log_unmount_write(mp);
552 xfs_log_unmount_dealloc(mp);
553 return (error);
554}
555
556/*
557 * Final log writes as part of unmount.
558 *
559 * Mark the filesystem clean as unmount happens. Note that during relocation
560 * this routine needs to be executed as part of source-bag while the
561 * deallocation must not be done until source-end.
562 */
563
564/*
565 * Unmount record used to have a string "Unmount filesystem--" in the
566 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
567 * We just write the magic number now since that particular field isn't
568 * currently architecture converted and "nUmount" is a bit foo.
569 * As far as I know, there weren't any dependencies on the old behaviour.
570 */
571
572int
573xfs_log_unmount_write(xfs_mount_t *mp)
574{
575 xlog_t *log = mp->m_log;
576 xlog_in_core_t *iclog;
577#ifdef DEBUG
578 xlog_in_core_t *first_iclog;
579#endif
580 xfs_log_iovec_t reg[1];
581 xfs_log_ticket_t tic = NULL;
582 xfs_lsn_t lsn;
583 int error;
584 SPLDECL(s);
585
586 /* the data section must be 32 bit size aligned */
587 struct {
588 __uint16_t magic;
589 __uint16_t pad1;
590 __uint32_t pad2; /* may as well make it 64 bits */
591 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
592
593#if defined(DEBUG) || defined(XLOG_NOLOG)
594 if (!xlog_debug && xlog_target == log->l_targ)
595 return 0;
596#endif
597
598 /*
599 * Don't write out unmount record on read-only mounts.
600 * Or, if we are doing a forced umount (typically because of IO errors).
601 */
602 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
603 return 0;
604
605 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
606
607#ifdef DEBUG
608 first_iclog = iclog = log->l_iclog;
609 do {
610 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
611 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
612 ASSERT(iclog->ic_offset == 0);
613 }
614 iclog = iclog->ic_next;
615 } while (iclog != first_iclog);
616#endif
617 if (! (XLOG_FORCED_SHUTDOWN(log))) {
618 reg[0].i_addr = (void*)&magic;
619 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000620 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000622 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!error) {
624 /* remove inited flag */
625 ((xlog_ticket_t *)tic)->t_flags = 0;
626 error = xlog_write(mp, reg, 1, tic, &lsn,
627 NULL, XLOG_UNMOUNT_TRANS);
628 /*
629 * At this point, we're umounting anyway,
630 * so there's no point in transitioning log state
631 * to IOERROR. Just continue...
632 */
633 }
634
635 if (error) {
636 xfs_fs_cmn_err(CE_ALERT, mp,
637 "xfs_log_unmount: unmount record failed");
638 }
639
640
641 s = LOG_LOCK(log);
642 iclog = log->l_iclog;
643 iclog->ic_refcnt++;
644 LOG_UNLOCK(log, s);
645 xlog_state_want_sync(log, iclog);
646 (void) xlog_state_release_iclog(log, iclog);
647
648 s = LOG_LOCK(log);
649 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
650 iclog->ic_state == XLOG_STATE_DIRTY)) {
651 if (!XLOG_FORCED_SHUTDOWN(log)) {
652 sv_wait(&iclog->ic_forcesema, PMEM,
653 &log->l_icloglock, s);
654 } else {
655 LOG_UNLOCK(log, s);
656 }
657 } else {
658 LOG_UNLOCK(log, s);
659 }
660 if (tic)
661 xlog_state_put_ticket(log, tic);
662 } else {
663 /*
664 * We're already in forced_shutdown mode, couldn't
665 * even attempt to write out the unmount transaction.
666 *
667 * Go through the motions of sync'ing and releasing
668 * the iclog, even though no I/O will actually happen,
669 * we need to wait for other log I/O's that may already
670 * be in progress. Do this as a separate section of
671 * code so we'll know if we ever get stuck here that
672 * we're in this odd situation of trying to unmount
673 * a file system that went into forced_shutdown as
674 * the result of an unmount..
675 */
676 s = LOG_LOCK(log);
677 iclog = log->l_iclog;
678 iclog->ic_refcnt++;
679 LOG_UNLOCK(log, s);
680
681 xlog_state_want_sync(log, iclog);
682 (void) xlog_state_release_iclog(log, iclog);
683
684 s = LOG_LOCK(log);
685
686 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
687 || iclog->ic_state == XLOG_STATE_DIRTY
688 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
689
690 sv_wait(&iclog->ic_forcesema, PMEM,
691 &log->l_icloglock, s);
692 } else {
693 LOG_UNLOCK(log, s);
694 }
695 }
696
697 return 0;
698} /* xfs_log_unmount_write */
699
700/*
701 * Deallocate log structures for unmount/relocation.
702 */
703void
704xfs_log_unmount_dealloc(xfs_mount_t *mp)
705{
706 xlog_unalloc_log(mp->m_log);
707}
708
709/*
710 * Write region vectors to log. The write happens using the space reservation
711 * of the ticket (tic). It is not a requirement that all writes for a given
712 * transaction occur with one call to xfs_log_write().
713 */
714int
715xfs_log_write(xfs_mount_t * mp,
716 xfs_log_iovec_t reg[],
717 int nentries,
718 xfs_log_ticket_t tic,
719 xfs_lsn_t *start_lsn)
720{
721 int error;
722 xlog_t *log = mp->m_log;
723
724#if defined(DEBUG) || defined(XLOG_NOLOG)
725 if (!xlog_debug && xlog_target == log->l_targ) {
726 *start_lsn = 0;
727 return 0;
728 }
729#endif
730 if (XLOG_FORCED_SHUTDOWN(log))
731 return XFS_ERROR(EIO);
732
733 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
734 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
735 }
736 return (error);
737} /* xfs_log_write */
738
739
740void
741xfs_log_move_tail(xfs_mount_t *mp,
742 xfs_lsn_t tail_lsn)
743{
744 xlog_ticket_t *tic;
745 xlog_t *log = mp->m_log;
746 int need_bytes, free_bytes, cycle, bytes;
747 SPLDECL(s);
748
749#if defined(DEBUG) || defined(XLOG_NOLOG)
750 if (!xlog_debug && xlog_target == log->l_targ)
751 return;
752#endif
753 /* XXXsup tmp */
754 if (XLOG_FORCED_SHUTDOWN(log))
755 return;
756 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
757
758 if (tail_lsn == 0) {
759 /* needed since sync_lsn is 64 bits */
760 s = LOG_LOCK(log);
761 tail_lsn = log->l_last_sync_lsn;
762 LOG_UNLOCK(log, s);
763 }
764
765 s = GRANT_LOCK(log);
766
767 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
768 * tail_lsn.
769 */
770 if (tail_lsn != 1) {
771 log->l_tail_lsn = tail_lsn;
772 }
773
774 if ((tic = log->l_write_headq)) {
775#ifdef DEBUG
776 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
777 panic("Recovery problem");
778#endif
779 cycle = log->l_grant_write_cycle;
780 bytes = log->l_grant_write_bytes;
781 free_bytes = xlog_space_left(log, cycle, bytes);
782 do {
783 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
784
785 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
786 break;
787 tail_lsn = 0;
788 free_bytes -= tic->t_unit_res;
789 sv_signal(&tic->t_sema);
790 tic = tic->t_next;
791 } while (tic != log->l_write_headq);
792 }
793 if ((tic = log->l_reserve_headq)) {
794#ifdef DEBUG
795 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
796 panic("Recovery problem");
797#endif
798 cycle = log->l_grant_reserve_cycle;
799 bytes = log->l_grant_reserve_bytes;
800 free_bytes = xlog_space_left(log, cycle, bytes);
801 do {
802 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
803 need_bytes = tic->t_unit_res*tic->t_cnt;
804 else
805 need_bytes = tic->t_unit_res;
806 if (free_bytes < need_bytes && tail_lsn != 1)
807 break;
808 tail_lsn = 0;
809 free_bytes -= need_bytes;
810 sv_signal(&tic->t_sema);
811 tic = tic->t_next;
812 } while (tic != log->l_reserve_headq);
813 }
814 GRANT_UNLOCK(log, s);
815} /* xfs_log_move_tail */
816
817/*
818 * Determine if we have a transaction that has gone to disk
819 * that needs to be covered. Log activity needs to be idle (no AIL and
820 * nothing in the iclogs). And, we need to be in the right state indicating
821 * something has gone out.
822 */
823int
824xfs_log_need_covered(xfs_mount_t *mp)
825{
826 SPLDECL(s);
827 int needed = 0, gen;
828 xlog_t *log = mp->m_log;
829 vfs_t *vfsp = XFS_MTOVFS(mp);
830
831 if (fs_frozen(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
832 (vfsp->vfs_flag & VFS_RDONLY))
833 return 0;
834
835 s = LOG_LOCK(log);
836 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
837 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
838 && !xfs_trans_first_ail(mp, &gen)
839 && xlog_iclogs_empty(log)) {
840 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
841 log->l_covered_state = XLOG_STATE_COVER_DONE;
842 else {
843 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
844 log->l_covered_state = XLOG_STATE_COVER_DONE2;
845 }
846 needed = 1;
847 }
848 LOG_UNLOCK(log, s);
849 return(needed);
850}
851
852/******************************************************************************
853 *
854 * local routines
855 *
856 ******************************************************************************
857 */
858
859/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
860 * The log manager must keep track of the last LR which was committed
861 * to disk. The lsn of this LR will become the new tail_lsn whenever
862 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
863 * the situation where stuff could be written into the log but nothing
864 * was ever in the AIL when asked. Eventually, we panic since the
865 * tail hits the head.
866 *
867 * We may be holding the log iclog lock upon entering this routine.
868 */
869xfs_lsn_t
870xlog_assign_tail_lsn(xfs_mount_t *mp)
871{
872 xfs_lsn_t tail_lsn;
873 SPLDECL(s);
874 xlog_t *log = mp->m_log;
875
876 tail_lsn = xfs_trans_tail_ail(mp);
877 s = GRANT_LOCK(log);
878 if (tail_lsn != 0) {
879 log->l_tail_lsn = tail_lsn;
880 } else {
881 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
882 }
883 GRANT_UNLOCK(log, s);
884
885 return tail_lsn;
886} /* xlog_assign_tail_lsn */
887
888
889/*
890 * Return the space in the log between the tail and the head. The head
891 * is passed in the cycle/bytes formal parms. In the special case where
892 * the reserve head has wrapped passed the tail, this calculation is no
893 * longer valid. In this case, just return 0 which means there is no space
894 * in the log. This works for all places where this function is called
895 * with the reserve head. Of course, if the write head were to ever
896 * wrap the tail, we should blow up. Rather than catch this case here,
897 * we depend on other ASSERTions in other parts of the code. XXXmiken
898 *
899 * This code also handles the case where the reservation head is behind
900 * the tail. The details of this case are described below, but the end
901 * result is that we return the size of the log as the amount of space left.
902 */
903int
904xlog_space_left(xlog_t *log, int cycle, int bytes)
905{
906 int free_bytes;
907 int tail_bytes;
908 int tail_cycle;
909
910 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
911 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
912 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
913 free_bytes = log->l_logsize - (bytes - tail_bytes);
914 } else if ((tail_cycle + 1) < cycle) {
915 return 0;
916 } else if (tail_cycle < cycle) {
917 ASSERT(tail_cycle == (cycle - 1));
918 free_bytes = tail_bytes - bytes;
919 } else {
920 /*
921 * The reservation head is behind the tail.
922 * In this case we just want to return the size of the
923 * log as the amount of space left.
924 */
925 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
926 "xlog_space_left: head behind tail\n"
927 " tail_cycle = %d, tail_bytes = %d\n"
928 " GH cycle = %d, GH bytes = %d",
929 tail_cycle, tail_bytes, cycle, bytes);
930 ASSERT(0);
931 free_bytes = log->l_logsize;
932 }
933 return free_bytes;
934} /* xlog_space_left */
935
936
937/*
938 * Log function which is called when an io completes.
939 *
940 * The log manager needs its own routine, in order to control what
941 * happens with the buffer after the write completes.
942 */
943void
944xlog_iodone(xfs_buf_t *bp)
945{
946 xlog_in_core_t *iclog;
947 xlog_t *l;
948 int aborted;
949
950 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
951 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
952 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
953 aborted = 0;
954
955 /*
956 * Some versions of cpp barf on the recursive definition of
957 * ic_log -> hic_fields.ic_log and expand ic_log twice when
958 * it is passed through two macros. Workaround broken cpp.
959 */
960 l = iclog->ic_log;
961
962 /*
963 * Race to shutdown the filesystem if we see an error.
964 */
965 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
966 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
967 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
968 XFS_BUF_STALE(bp);
969 xfs_force_shutdown(l->l_mp, XFS_LOG_IO_ERROR);
970 /*
971 * This flag will be propagated to the trans-committed
972 * callback routines to let them know that the log-commit
973 * didn't succeed.
974 */
975 aborted = XFS_LI_ABORTED;
976 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
977 aborted = XFS_LI_ABORTED;
978 }
979 xlog_state_done_syncing(iclog, aborted);
980 if (!(XFS_BUF_ISASYNC(bp))) {
981 /*
982 * Corresponding psema() will be done in bwrite(). If we don't
983 * vsema() here, panic.
984 */
985 XFS_BUF_V_IODONESEMA(bp);
986 }
987} /* xlog_iodone */
988
989/*
990 * The bdstrat callback function for log bufs. This gives us a central
991 * place to trap bufs in case we get hit by a log I/O error and need to
992 * shutdown. Actually, in practice, even when we didn't get a log error,
993 * we transition the iclogs to IOERROR state *after* flushing all existing
994 * iclogs to disk. This is because we don't want anymore new transactions to be
995 * started or completed afterwards.
996 */
997STATIC int
998xlog_bdstrat_cb(struct xfs_buf *bp)
999{
1000 xlog_in_core_t *iclog;
1001
1002 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1003
1004 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1005 /* note for irix bstrat will need struct bdevsw passed
1006 * Fix the following macro if the code ever is merged
1007 */
1008 XFS_bdstrat(bp);
1009 return 0;
1010 }
1011
1012 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1013 XFS_BUF_ERROR(bp, EIO);
1014 XFS_BUF_STALE(bp);
1015 xfs_biodone(bp);
1016 return (XFS_ERROR(EIO));
1017
1018
1019}
1020
1021/*
1022 * Return size of each in-core log record buffer.
1023 *
1024 * Low memory machines only get 2 16KB buffers. We don't want to waste
1025 * memory here. However, all other machines get at least 2 32KB buffers.
1026 * The number is hard coded because we don't care about the minimum
1027 * memory size, just 32MB systems.
1028 *
1029 * If the filesystem blocksize is too large, we may need to choose a
1030 * larger size since the directory code currently logs entire blocks.
1031 */
1032
1033STATIC void
1034xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1035 xlog_t *log)
1036{
1037 int size;
1038 int xhdrs;
1039
1040#if defined(DEBUG) || defined(XLOG_NOLOG)
1041 /*
1042 * When logbufs == 0, someone has disabled the log from the FSTAB
1043 * file. This is not a documented feature. We need to set xlog_debug
1044 * to zero (this deactivates the log) and set xlog_target to the
1045 * appropriate device. Only one filesystem may be affected as such
1046 * since this is just a performance hack to test what we might be able
1047 * to get if the log were not present.
1048 */
1049 if (mp->m_logbufs == 0) {
1050 xlog_debug = 0;
1051 xlog_target = log->l_targ;
1052 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1053 } else
1054#endif
1055 {
1056 /*
1057 * This is the normal path. If m_logbufs == -1, then the
1058 * admin has chosen to use the system defaults for logbuffers.
1059 */
1060 if (mp->m_logbufs == -1) {
1061 if (xfs_physmem <= btoc(128*1024*1024)) {
1062 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1063 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1064 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1065 } else {
1066 /* 256K with 32K bufs */
1067 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1068 }
1069 } else
1070 log->l_iclog_bufs = mp->m_logbufs;
1071
1072#if defined(DEBUG) || defined(XLOG_NOLOG)
1073 /* We are reactivating a filesystem after it was inactive */
1074 if (log->l_targ == xlog_target) {
1075 xlog_target = NULL;
1076 xlog_debug = 1;
1077 }
1078#endif
1079 }
1080
1081 /*
1082 * Buffer size passed in from mount system call.
1083 */
1084 if (mp->m_logbsize != -1) {
1085 size = log->l_iclog_size = mp->m_logbsize;
1086 log->l_iclog_size_log = 0;
1087 while (size != 1) {
1088 log->l_iclog_size_log++;
1089 size >>= 1;
1090 }
1091
1092 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1093 /* # headers = size / 32K
1094 * one header holds cycles from 32K of data
1095 */
1096
1097 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1098 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1099 xhdrs++;
1100 log->l_iclog_hsize = xhdrs << BBSHIFT;
1101 log->l_iclog_heads = xhdrs;
1102 } else {
1103 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1104 log->l_iclog_hsize = BBSIZE;
1105 log->l_iclog_heads = 1;
1106 }
1107 return;
1108 }
1109
1110 /*
1111 * Special case machines that have less than 32MB of memory.
1112 * All machines with more memory use 32KB buffers.
1113 */
1114 if (xfs_physmem <= btoc(32*1024*1024)) {
1115 /* Don't change; min configuration */
1116 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1117 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1118 } else {
1119 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1120 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1121 }
1122
1123 /* the default log size is 16k or 32k which is one header sector */
1124 log->l_iclog_hsize = BBSIZE;
1125 log->l_iclog_heads = 1;
1126
1127 /*
1128 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1129 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1130 */
1131 if (mp->m_sb.sb_blocksize >= 16*1024) {
1132 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1133 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1134 if (mp->m_logbufs == -1) {
1135 switch (mp->m_sb.sb_blocksize) {
1136 case 16*1024: /* 16 KB */
1137 log->l_iclog_bufs = 3;
1138 break;
1139 case 32*1024: /* 32 KB */
1140 log->l_iclog_bufs = 4;
1141 break;
1142 case 64*1024: /* 64 KB */
1143 log->l_iclog_bufs = 8;
1144 break;
1145 default:
1146 xlog_panic("XFS: Invalid blocksize");
1147 break;
1148 }
1149 }
1150 }
1151} /* xlog_get_iclog_buffer_size */
1152
1153
1154/*
1155 * This routine initializes some of the log structure for a given mount point.
1156 * Its primary purpose is to fill in enough, so recovery can occur. However,
1157 * some other stuff may be filled in too.
1158 */
1159STATIC xlog_t *
1160xlog_alloc_log(xfs_mount_t *mp,
1161 xfs_buftarg_t *log_target,
1162 xfs_daddr_t blk_offset,
1163 int num_bblks)
1164{
1165 xlog_t *log;
1166 xlog_rec_header_t *head;
1167 xlog_in_core_t **iclogp;
1168 xlog_in_core_t *iclog, *prev_iclog=NULL;
1169 xfs_buf_t *bp;
1170 int i;
1171 int iclogsize;
1172
1173 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1174
1175 log->l_mp = mp;
1176 log->l_targ = log_target;
1177 log->l_logsize = BBTOB(num_bblks);
1178 log->l_logBBstart = blk_offset;
1179 log->l_logBBsize = num_bblks;
1180 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1181 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1182
1183 log->l_prev_block = -1;
1184 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1185 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1186 log->l_last_sync_lsn = log->l_tail_lsn;
1187 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1188 log->l_grant_reserve_cycle = 1;
1189 log->l_grant_write_cycle = 1;
1190
1191 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1192 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1193 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1194 /* for larger sector sizes, must have v2 or external log */
1195 ASSERT(log->l_sectbb_log == 0 ||
1196 log->l_logBBstart == 0 ||
1197 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1198 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1199 }
1200 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1201
1202 xlog_get_iclog_buffer_size(mp, log);
1203
1204 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1205 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1206 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1207 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1208 ASSERT(XFS_BUF_ISBUSY(bp));
1209 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1210 log->l_xbuf = bp;
1211
1212 spinlock_init(&log->l_icloglock, "iclog");
1213 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1214 initnsema(&log->l_flushsema, 0, "ic-flush");
1215 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1216
1217 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1218 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1219
1220 iclogp = &log->l_iclog;
1221 /*
1222 * The amount of memory to allocate for the iclog structure is
1223 * rather funky due to the way the structure is defined. It is
1224 * done this way so that we can use different sizes for machines
1225 * with different amounts of memory. See the definition of
1226 * xlog_in_core_t in xfs_log_priv.h for details.
1227 */
1228 iclogsize = log->l_iclog_size;
1229 ASSERT(log->l_iclog_size >= 4096);
1230 for (i=0; i < log->l_iclog_bufs; i++) {
1231 *iclogp = (xlog_in_core_t *)
1232 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1233 iclog = *iclogp;
1234 iclog->hic_data = (xlog_in_core_2_t *)
1235 kmem_zalloc(iclogsize, KM_SLEEP);
1236
1237 iclog->ic_prev = prev_iclog;
1238 prev_iclog = iclog;
1239 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1240
1241 head = &iclog->ic_header;
1242 memset(head, 0, sizeof(xlog_rec_header_t));
1243 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1244 INT_SET(head->h_version, ARCH_CONVERT,
1245 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1246 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1247 /* new fields */
1248 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1249 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1250
1251 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1252 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1253 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1254 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1255 iclog->ic_bp = bp;
1256
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;
1262
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");
1267
1268 iclogp = &iclog->ic_next;
1269 }
1270 *iclogp = log->l_iclog; /* complete ring */
1271 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1272
1273 return log;
1274} /* xlog_alloc_log */
1275
1276
1277/*
1278 * Write out the commit record of a transaction associated with the given
1279 * ticket. Return the lsn of the commit record.
1280 */
1281STATIC int
1282xlog_commit_record(xfs_mount_t *mp,
1283 xlog_ticket_t *ticket,
1284 xlog_in_core_t **iclog,
1285 xfs_lsn_t *commitlsnp)
1286{
1287 int error;
1288 xfs_log_iovec_t reg[1];
1289
1290 reg[0].i_addr = NULL;
1291 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001292 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 ASSERT_ALWAYS(iclog);
1295 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1296 iclog, XLOG_COMMIT_TRANS))) {
1297 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
1298 }
1299 return (error);
1300} /* xlog_commit_record */
1301
1302
1303/*
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.
1309 */
1310void
1311xlog_grant_push_ail(xfs_mount_t *mp,
1312 int need_bytes)
1313{
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;
1322 SPLDECL(s);
1323
1324 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1325
1326 s = GRANT_LOCK(log);
1327 free_bytes = xlog_space_left(log,
1328 log->l_grant_reserve_cycle,
1329 log->l_grant_reserve_bytes);
1330 tail_lsn = log->l_tail_lsn;
1331 free_blocks = BTOBBT(free_bytes);
1332
1333 /*
1334 * Set the threshold for the minimum number of free blocks in the
1335 * log to the maximum of what the caller needs, one quarter of the
1336 * log, and 256 blocks.
1337 */
1338 free_threshold = BTOBB(need_bytes);
1339 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1340 free_threshold = MAX(free_threshold, 256);
1341 if (free_blocks < free_threshold) {
1342 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1343 threshold_cycle = CYCLE_LSN(tail_lsn);
1344 if (threshold_block >= log->l_logBBsize) {
1345 threshold_block -= log->l_logBBsize;
1346 threshold_cycle += 1;
1347 }
1348 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1349 threshold_block);
1350
1351 /* Don't pass in an lsn greater than the lsn of the last
1352 * log record known to be on disk.
1353 */
1354 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1355 threshold_lsn = log->l_last_sync_lsn;
1356 }
1357 GRANT_UNLOCK(log, s);
1358
1359 /*
1360 * Get the transaction layer to kick the dirty buffers out to
1361 * disk asynchronously. No point in trying to do this if
1362 * the filesystem is shutting down.
1363 */
1364 if (threshold_lsn &&
1365 !XLOG_FORCED_SHUTDOWN(log))
1366 xfs_trans_push_ail(mp, threshold_lsn);
1367} /* xlog_grant_push_ail */
1368
1369
1370/*
1371 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1372 * fashion. Previously, we should have moved the current iclog
1373 * ptr in the log to point to the next available iclog. This allows further
1374 * write to continue while this code syncs out an iclog ready to go.
1375 * Before an in-core log can be written out, the data section must be scanned
1376 * to save away the 1st word of each BBSIZE block into the header. We replace
1377 * it with the current cycle count. Each BBSIZE block is tagged with the
1378 * cycle count because there in an implicit assumption that drives will
1379 * guarantee that entire 512 byte blocks get written at once. In other words,
1380 * we can't have part of a 512 byte block written and part not written. By
1381 * tagging each block, we will know which blocks are valid when recovering
1382 * after an unclean shutdown.
1383 *
1384 * This routine is single threaded on the iclog. No other thread can be in
1385 * this routine with the same iclog. Changing contents of iclog can there-
1386 * fore be done without grabbing the state machine lock. Updating the global
1387 * log will require grabbing the lock though.
1388 *
1389 * The entire log manager uses a logical block numbering scheme. Only
1390 * log_sync (and then only bwrite()) know about the fact that the log may
1391 * not start with block zero on a given device. The log block start offset
1392 * is added immediately before calling bwrite().
1393 */
1394
1395int
1396xlog_sync(xlog_t *log,
1397 xlog_in_core_t *iclog)
1398{
1399 xfs_caddr_t dptr; /* pointer to byte sized element */
1400 xfs_buf_t *bp;
1401 int i, ops;
1402 uint count; /* byte count of bwrite */
1403 uint count_init; /* initial count before roundup */
1404 int roundoff; /* roundoff to BB or stripe */
1405 int split = 0; /* split write into two regions */
1406 int error;
1407 SPLDECL(s);
1408 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1409
1410 XFS_STATS_INC(xs_log_writes);
1411 ASSERT(iclog->ic_refcnt == 0);
1412
1413 /* Add for LR header */
1414 count_init = log->l_iclog_hsize + iclog->ic_offset;
1415
1416 /* Round out the log write size */
1417 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1418 /* we have a v2 stripe unit to use */
1419 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1420 } else {
1421 count = BBTOB(BTOBB(count_init));
1422 }
1423 roundoff = count - count_init;
1424 ASSERT(roundoff >= 0);
1425 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1426 roundoff < log->l_mp->m_sb.sb_logsunit)
1427 ||
1428 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1429 roundoff < BBTOB(1)));
1430
1431 /* move grant heads by roundoff in sync */
1432 s = GRANT_LOCK(log);
1433 XLOG_GRANT_ADD_SPACE(log, roundoff, 'w');
1434 XLOG_GRANT_ADD_SPACE(log, roundoff, 'r');
1435 GRANT_UNLOCK(log, s);
1436
1437 /* put cycle number in every block */
1438 xlog_pack_data(log, iclog, roundoff);
1439
1440 /* real byte length */
1441 if (v2) {
1442 INT_SET(iclog->ic_header.h_len,
1443 ARCH_CONVERT,
1444 iclog->ic_offset + roundoff);
1445 } else {
1446 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1447 }
1448
1449 /* put ops count in correct order */
1450 ops = iclog->ic_header.h_num_logops;
1451 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1452
1453 bp = iclog->ic_bp;
1454 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1455 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1456 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1457
1458 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1459
1460 /* Do we need to split this write into 2 parts? */
1461 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1462 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1463 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1464 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1465 } else {
1466 iclog->ic_bwritecnt = 1;
1467 }
1468 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1469 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
1470 XFS_BUF_BUSY(bp);
1471 XFS_BUF_ASYNC(bp);
1472 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001473 * Do an ordered write for the log block.
1474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * It may not be needed to flush the first split block in the log wrap
1476 * case, but do it anyways to be safe -AK
1477 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001478 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1479 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1482 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1483
1484 xlog_verify_iclog(log, iclog, count, B_TRUE);
1485
1486 /* account for log which doesn't start at block #0 */
1487 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1488 /*
1489 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1490 * is shutting down.
1491 */
1492 XFS_BUF_WRITE(bp);
1493
1494 if ((error = XFS_bwrite(bp))) {
1495 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1496 XFS_BUF_ADDR(bp));
1497 return (error);
1498 }
1499 if (split) {
1500 bp = iclog->ic_log->l_xbuf;
1501 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1502 (unsigned long)1);
1503 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1504 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1505 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1506 (__psint_t)count), split);
1507 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1508 XFS_BUF_BUSY(bp);
1509 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001510 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1511 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 dptr = XFS_BUF_PTR(bp);
1513 /*
1514 * Bump the cycle numbers at the start of each block
1515 * since this part of the buffer is at the start of
1516 * a new cycle. Watch out for the header magic number
1517 * case, though.
1518 */
1519 for (i=0; i<split; i += BBSIZE) {
1520 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1521 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1522 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1523 dptr += BBSIZE;
1524 }
1525
1526 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1527 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1528
1529 /* account for internal log which does't start at block #0 */
1530 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1531 XFS_BUF_WRITE(bp);
1532 if ((error = XFS_bwrite(bp))) {
1533 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1534 bp, XFS_BUF_ADDR(bp));
1535 return (error);
1536 }
1537 }
1538 return (0);
1539} /* xlog_sync */
1540
1541
1542/*
1543 * Unallocate a log structure
1544 */
1545void
1546xlog_unalloc_log(xlog_t *log)
1547{
1548 xlog_in_core_t *iclog, *next_iclog;
1549 xlog_ticket_t *tic, *next_tic;
1550 int i;
1551
1552
1553 iclog = log->l_iclog;
1554 for (i=0; i<log->l_iclog_bufs; i++) {
1555 sv_destroy(&iclog->ic_forcesema);
1556 sv_destroy(&iclog->ic_writesema);
1557 xfs_buf_free(iclog->ic_bp);
1558#ifdef XFS_LOG_TRACE
1559 if (iclog->ic_trace != NULL) {
1560 ktrace_free(iclog->ic_trace);
1561 }
1562#endif
1563 next_iclog = iclog->ic_next;
1564 kmem_free(iclog->hic_data, log->l_iclog_size);
1565 kmem_free(iclog, sizeof(xlog_in_core_t));
1566 iclog = next_iclog;
1567 }
1568 freesema(&log->l_flushsema);
1569 spinlock_destroy(&log->l_icloglock);
1570 spinlock_destroy(&log->l_grant_lock);
1571
1572 /* XXXsup take a look at this again. */
1573 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1574 !XLOG_FORCED_SHUTDOWN(log)) {
1575 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1576 "xlog_unalloc_log: (cnt: %d, total: %d)",
1577 log->l_ticket_cnt, log->l_ticket_tcnt);
1578 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1579
1580 } else {
1581 tic = log->l_unmount_free;
1582 while (tic) {
1583 next_tic = tic->t_next;
1584 kmem_free(tic, NBPP);
1585 tic = next_tic;
1586 }
1587 }
1588 xfs_buf_free(log->l_xbuf);
1589#ifdef XFS_LOG_TRACE
1590 if (log->l_trace != NULL) {
1591 ktrace_free(log->l_trace);
1592 }
1593 if (log->l_grant_trace != NULL) {
1594 ktrace_free(log->l_grant_trace);
1595 }
1596#endif
1597 log->l_mp->m_log = NULL;
1598 kmem_free(log, sizeof(xlog_t));
1599} /* xlog_unalloc_log */
1600
1601/*
1602 * Update counters atomically now that memcpy is done.
1603 */
1604/* ARGSUSED */
1605static inline void
1606xlog_state_finish_copy(xlog_t *log,
1607 xlog_in_core_t *iclog,
1608 int record_cnt,
1609 int copy_bytes)
1610{
1611 SPLDECL(s);
1612
1613 s = LOG_LOCK(log);
1614
1615 iclog->ic_header.h_num_logops += record_cnt;
1616 iclog->ic_offset += copy_bytes;
1617
1618 LOG_UNLOCK(log, s);
1619} /* xlog_state_finish_copy */
1620
1621
1622
1623
1624/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001625 * print out info relating to regions written which consume
1626 * the reservation
1627 */
1628#if defined(XFS_LOG_RES_DEBUG)
1629STATIC void
1630xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1631{
1632 uint i;
1633 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1634
1635 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1636 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1637 "bformat",
1638 "bchunk",
1639 "efi_format",
1640 "efd_format",
1641 "iformat",
1642 "icore",
1643 "iext",
1644 "ibroot",
1645 "ilocal",
1646 "iattr_ext",
1647 "iattr_broot",
1648 "iattr_local",
1649 "qformat",
1650 "dquot",
1651 "quotaoff",
1652 "LR header",
1653 "unmount",
1654 "commit",
1655 "trans header"
1656 };
1657 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1658 "SETATTR_NOT_SIZE",
1659 "SETATTR_SIZE",
1660 "INACTIVE",
1661 "CREATE",
1662 "CREATE_TRUNC",
1663 "TRUNCATE_FILE",
1664 "REMOVE",
1665 "LINK",
1666 "RENAME",
1667 "MKDIR",
1668 "RMDIR",
1669 "SYMLINK",
1670 "SET_DMATTRS",
1671 "GROWFS",
1672 "STRAT_WRITE",
1673 "DIOSTRAT",
1674 "WRITE_SYNC",
1675 "WRITEID",
1676 "ADDAFORK",
1677 "ATTRINVAL",
1678 "ATRUNCATE",
1679 "ATTR_SET",
1680 "ATTR_RM",
1681 "ATTR_FLAG",
1682 "CLEAR_AGI_BUCKET",
1683 "QM_SBCHANGE",
1684 "DUMMY1",
1685 "DUMMY2",
1686 "QM_QUOTAOFF",
1687 "QM_DQALLOC",
1688 "QM_SETQLIM",
1689 "QM_DQCLUSTER",
1690 "QM_QINOCREATE",
1691 "QM_QUOTAOFF_END",
1692 "SB_UNIT",
1693 "FSYNC_TS",
1694 "GROWFSRT_ALLOC",
1695 "GROWFSRT_ZERO",
1696 "GROWFSRT_FREE",
1697 "SWAPEXT"
1698 };
1699
1700 xfs_fs_cmn_err(CE_WARN, mp,
1701 "xfs_log_write: reservation summary:\n"
1702 " trans type = %s (%u)\n"
1703 " unit res = %d bytes\n"
1704 " current res = %d bytes\n"
1705 " total reg = %u bytes (o/flow = %u bytes)\n"
1706 " ophdrs = %u (ophdr space = %u bytes)\n"
1707 " ophdr + reg = %u bytes\n"
1708 " num regions = %u\n",
1709 ((ticket->t_trans_type <= 0 ||
1710 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1711 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1712 ticket->t_trans_type,
1713 ticket->t_unit_res,
1714 ticket->t_curr_res,
1715 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1716 ticket->t_res_num_ophdrs, ophdr_spc,
1717 ticket->t_res_arr_sum +
1718 ticket->t_res_o_flow + ophdr_spc,
1719 ticket->t_res_num);
1720
1721 for (i = 0; i < ticket->t_res_num; i++) {
1722 uint r_type = ticket->t_res_arr[i].r_type;
1723 cmn_err(CE_WARN,
1724 "region[%u]: %s - %u bytes\n",
1725 i,
1726 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1727 "bad-rtype" : res_type_str[r_type-1]),
1728 ticket->t_res_arr[i].r_len);
1729 }
1730}
1731#else
1732#define xlog_print_tic_res(mp, ticket)
1733#endif
1734
1735/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 * Write some region out to in-core log
1737 *
1738 * This will be called when writing externally provided regions or when
1739 * writing out a commit record for a given transaction.
1740 *
1741 * General algorithm:
1742 * 1. Find total length of this write. This may include adding to the
1743 * lengths passed in.
1744 * 2. Check whether we violate the tickets reservation.
1745 * 3. While writing to this iclog
1746 * A. Reserve as much space in this iclog as can get
1747 * B. If this is first write, save away start lsn
1748 * C. While writing this region:
1749 * 1. If first write of transaction, write start record
1750 * 2. Write log operation header (header per region)
1751 * 3. Find out if we can fit entire region into this iclog
1752 * 4. Potentially, verify destination memcpy ptr
1753 * 5. Memcpy (partial) region
1754 * 6. If partial copy, release iclog; otherwise, continue
1755 * copying more regions into current iclog
1756 * 4. Mark want sync bit (in simulation mode)
1757 * 5. Release iclog for potential flush to on-disk log.
1758 *
1759 * ERRORS:
1760 * 1. Panic if reservation is overrun. This should never happen since
1761 * reservation amounts are generated internal to the filesystem.
1762 * NOTES:
1763 * 1. Tickets are single threaded data structures.
1764 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1765 * syncing routine. When a single log_write region needs to span
1766 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1767 * on all log operation writes which don't contain the end of the
1768 * region. The XLOG_END_TRANS bit is used for the in-core log
1769 * operation which contains the end of the continued log_write region.
1770 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1771 * we don't really know exactly how much space will be used. As a result,
1772 * we don't update ic_offset until the end when we know exactly how many
1773 * bytes have been written out.
1774 */
1775int
1776xlog_write(xfs_mount_t * mp,
1777 xfs_log_iovec_t reg[],
1778 int nentries,
1779 xfs_log_ticket_t tic,
1780 xfs_lsn_t *start_lsn,
1781 xlog_in_core_t **commit_iclog,
1782 uint flags)
1783{
1784 xlog_t *log = mp->m_log;
1785 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
1786 xlog_op_header_t *logop_head; /* ptr to log operation header */
1787 xlog_in_core_t *iclog; /* ptr to current in-core log */
1788 __psint_t ptr; /* copy address into data region */
1789 int len; /* # xlog_write() bytes 2 still copy */
1790 int index; /* region index currently copying */
1791 int log_offset; /* offset (from 0) into data region */
1792 int start_rec_copy; /* # bytes to copy for start record */
1793 int partial_copy; /* did we split a region? */
1794 int partial_copy_len;/* # bytes copied if split region */
1795 int need_copy; /* # bytes need to memcpy this region */
1796 int copy_len; /* # bytes actually memcpy'ing */
1797 int copy_off; /* # bytes from entry start */
1798 int contwr; /* continued write of in-core log? */
1799 int error;
1800 int record_cnt = 0, data_cnt = 0;
1801
1802 partial_copy_len = partial_copy = 0;
1803
1804 /* Calculate potential maximum space. Each region gets its own
1805 * xlog_op_header_t and may need to be double word aligned.
1806 */
1807 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001808 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 len += sizeof(xlog_op_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001810 XLOG_TIC_ADD_OPHDR(ticket);
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 for (index = 0; index < nentries; index++) {
1814 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001815 XLOG_TIC_ADD_OPHDR(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 len += reg[index].i_len;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001817 XLOG_TIC_ADD_REGION(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 contwr = *start_lsn = 0;
1820
1821 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001822 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823#ifdef DEBUG
1824 xlog_panic(
1825 "xfs_log_write: reservation ran out. Need to up reservation");
1826#else
1827 /* Customer configurable panic */
1828 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1829 "xfs_log_write: reservation ran out. Need to up reservation");
1830 /* If we did not panic, shutdown the filesystem */
1831 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1832#endif
1833 } else
1834 ticket->t_curr_res -= len;
1835
1836 for (index = 0; index < nentries; ) {
1837 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1838 &contwr, &log_offset)))
1839 return (error);
1840
1841 ASSERT(log_offset <= iclog->ic_size - 1);
1842 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1843
1844 /* start_lsn is the first lsn written to. That's all we need. */
1845 if (! *start_lsn)
1846 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1847
1848 /* This loop writes out as many regions as can fit in the amount
1849 * of space which was allocated by xlog_state_get_iclog_space().
1850 */
1851 while (index < nentries) {
1852 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1853 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1854 start_rec_copy = 0;
1855
1856 /* If first write for transaction, insert start record.
1857 * We can't be trying to commit if we are inited. We can't
1858 * have any "partial_copy" if we are inited.
1859 */
1860 if (ticket->t_flags & XLOG_TIC_INITED) {
1861 logop_head = (xlog_op_header_t *)ptr;
1862 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1863 logop_head->oh_clientid = ticket->t_clientid;
1864 logop_head->oh_len = 0;
1865 logop_head->oh_flags = XLOG_START_TRANS;
1866 logop_head->oh_res2 = 0;
1867 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1868 record_cnt++;
1869
1870 start_rec_copy = sizeof(xlog_op_header_t);
1871 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1872 }
1873
1874 /* Copy log operation header directly into data section */
1875 logop_head = (xlog_op_header_t *)ptr;
1876 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1877 logop_head->oh_clientid = ticket->t_clientid;
1878 logop_head->oh_res2 = 0;
1879
1880 /* header copied directly */
1881 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1882
1883 /* are we copying a commit or unmount record? */
1884 logop_head->oh_flags = flags;
1885
1886 /*
1887 * We've seen logs corrupted with bad transaction client
1888 * ids. This makes sure that XFS doesn't generate them on.
1889 * Turn this into an EIO and shut down the filesystem.
1890 */
1891 switch (logop_head->oh_clientid) {
1892 case XFS_TRANSACTION:
1893 case XFS_VOLUME:
1894 case XFS_LOG:
1895 break;
1896 default:
1897 xfs_fs_cmn_err(CE_WARN, mp,
1898 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1899 logop_head->oh_clientid, tic);
1900 return XFS_ERROR(EIO);
1901 }
1902
1903 /* Partial write last time? => (partial_copy != 0)
1904 * need_copy is the amount we'd like to copy if everything could
1905 * fit in the current memcpy.
1906 */
1907 need_copy = reg[index].i_len - partial_copy_len;
1908
1909 copy_off = partial_copy_len;
1910 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1911 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1912 if (partial_copy)
1913 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1914 partial_copy_len = partial_copy = 0;
1915 } else { /* partial write */
1916 copy_len = iclog->ic_size - log_offset;
1917 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1918 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1919 if (partial_copy)
1920 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1921 partial_copy_len += copy_len;
1922 partial_copy++;
1923 len += sizeof(xlog_op_header_t); /* from splitting of region */
1924 /* account for new log op header */
1925 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001926 XLOG_TIC_ADD_OPHDR(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 xlog_verify_dest_ptr(log, ptr);
1929
1930 /* copy region */
1931 ASSERT(copy_len >= 0);
1932 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1933 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1934
1935 /* make copy_len total bytes copied, including headers */
1936 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1937 record_cnt++;
1938 data_cnt += contwr ? copy_len : 0;
1939 if (partial_copy) { /* copied partial region */
1940 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1941 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1942 record_cnt = data_cnt = 0;
1943 if ((error = xlog_state_release_iclog(log, iclog)))
1944 return (error);
1945 break; /* don't increment index */
1946 } else { /* copied entire region */
1947 index++;
1948 partial_copy_len = partial_copy = 0;
1949
1950 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1951 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1952 record_cnt = data_cnt = 0;
1953 xlog_state_want_sync(log, iclog);
1954 if (commit_iclog) {
1955 ASSERT(flags & XLOG_COMMIT_TRANS);
1956 *commit_iclog = iclog;
1957 } else if ((error = xlog_state_release_iclog(log, iclog)))
1958 return (error);
1959 if (index == nentries)
1960 return 0; /* we are done */
1961 else
1962 break;
1963 }
1964 } /* if (partial_copy) */
1965 } /* while (index < nentries) */
1966 } /* for (index = 0; index < nentries; ) */
1967 ASSERT(len == 0);
1968
1969 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1970 if (commit_iclog) {
1971 ASSERT(flags & XLOG_COMMIT_TRANS);
1972 *commit_iclog = iclog;
1973 return 0;
1974 }
1975 return (xlog_state_release_iclog(log, iclog));
1976} /* xlog_write */
1977
1978
1979/*****************************************************************************
1980 *
1981 * State Machine functions
1982 *
1983 *****************************************************************************
1984 */
1985
1986/* Clean iclogs starting from the head. This ordering must be
1987 * maintained, so an iclog doesn't become ACTIVE beyond one that
1988 * is SYNCING. This is also required to maintain the notion that we use
1989 * a counting semaphore to hold off would be writers to the log when every
1990 * iclog is trying to sync to disk.
1991 *
1992 * State Change: DIRTY -> ACTIVE
1993 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001994STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995xlog_state_clean_log(xlog_t *log)
1996{
1997 xlog_in_core_t *iclog;
1998 int changed = 0;
1999
2000 iclog = log->l_iclog;
2001 do {
2002 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2003 iclog->ic_state = XLOG_STATE_ACTIVE;
2004 iclog->ic_offset = 0;
2005 iclog->ic_callback = NULL; /* don't need to free */
2006 /*
2007 * If the number of ops in this iclog indicate it just
2008 * contains the dummy transaction, we can
2009 * change state into IDLE (the second time around).
2010 * Otherwise we should change the state into
2011 * NEED a dummy.
2012 * We don't need to cover the dummy.
2013 */
2014 if (!changed &&
2015 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
2016 changed = 1;
2017 } else {
2018 /*
2019 * We have two dirty iclogs so start over
2020 * This could also be num of ops indicates
2021 * this is not the dummy going out.
2022 */
2023 changed = 2;
2024 }
2025 iclog->ic_header.h_num_logops = 0;
2026 memset(iclog->ic_header.h_cycle_data, 0,
2027 sizeof(iclog->ic_header.h_cycle_data));
2028 iclog->ic_header.h_lsn = 0;
2029 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2030 /* do nothing */;
2031 else
2032 break; /* stop cleaning */
2033 iclog = iclog->ic_next;
2034 } while (iclog != log->l_iclog);
2035
2036 /* log is locked when we are called */
2037 /*
2038 * Change state for the dummy log recording.
2039 * We usually go to NEED. But we go to NEED2 if the changed indicates
2040 * we are done writing the dummy record.
2041 * If we are done with the second dummy recored (DONE2), then
2042 * we go to IDLE.
2043 */
2044 if (changed) {
2045 switch (log->l_covered_state) {
2046 case XLOG_STATE_COVER_IDLE:
2047 case XLOG_STATE_COVER_NEED:
2048 case XLOG_STATE_COVER_NEED2:
2049 log->l_covered_state = XLOG_STATE_COVER_NEED;
2050 break;
2051
2052 case XLOG_STATE_COVER_DONE:
2053 if (changed == 1)
2054 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2055 else
2056 log->l_covered_state = XLOG_STATE_COVER_NEED;
2057 break;
2058
2059 case XLOG_STATE_COVER_DONE2:
2060 if (changed == 1)
2061 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2062 else
2063 log->l_covered_state = XLOG_STATE_COVER_NEED;
2064 break;
2065
2066 default:
2067 ASSERT(0);
2068 }
2069 }
2070} /* xlog_state_clean_log */
2071
2072STATIC xfs_lsn_t
2073xlog_get_lowest_lsn(
2074 xlog_t *log)
2075{
2076 xlog_in_core_t *lsn_log;
2077 xfs_lsn_t lowest_lsn, lsn;
2078
2079 lsn_log = log->l_iclog;
2080 lowest_lsn = 0;
2081 do {
2082 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2083 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
2084 if ((lsn && !lowest_lsn) ||
2085 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2086 lowest_lsn = lsn;
2087 }
2088 }
2089 lsn_log = lsn_log->ic_next;
2090 } while (lsn_log != log->l_iclog);
2091 return(lowest_lsn);
2092}
2093
2094
2095STATIC void
2096xlog_state_do_callback(
2097 xlog_t *log,
2098 int aborted,
2099 xlog_in_core_t *ciclog)
2100{
2101 xlog_in_core_t *iclog;
2102 xlog_in_core_t *first_iclog; /* used to know when we've
2103 * processed all iclogs once */
2104 xfs_log_callback_t *cb, *cb_next;
2105 int flushcnt = 0;
2106 xfs_lsn_t lowest_lsn;
2107 int ioerrors; /* counter: iclogs with errors */
2108 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2109 int funcdidcallbacks; /* flag: function did callbacks */
2110 int repeats; /* for issuing console warnings if
2111 * looping too many times */
2112 SPLDECL(s);
2113
2114 s = LOG_LOCK(log);
2115 first_iclog = iclog = log->l_iclog;
2116 ioerrors = 0;
2117 funcdidcallbacks = 0;
2118 repeats = 0;
2119
2120 do {
2121 /*
2122 * Scan all iclogs starting with the one pointed to by the
2123 * log. Reset this starting point each time the log is
2124 * unlocked (during callbacks).
2125 *
2126 * Keep looping through iclogs until one full pass is made
2127 * without running any callbacks.
2128 */
2129 first_iclog = log->l_iclog;
2130 iclog = log->l_iclog;
2131 loopdidcallbacks = 0;
2132 repeats++;
2133
2134 do {
2135
2136 /* skip all iclogs in the ACTIVE & DIRTY states */
2137 if (iclog->ic_state &
2138 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2139 iclog = iclog->ic_next;
2140 continue;
2141 }
2142
2143 /*
2144 * Between marking a filesystem SHUTDOWN and stopping
2145 * the log, we do flush all iclogs to disk (if there
2146 * wasn't a log I/O error). So, we do want things to
2147 * go smoothly in case of just a SHUTDOWN w/o a
2148 * LOG_IO_ERROR.
2149 */
2150 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2151 /*
2152 * Can only perform callbacks in order. Since
2153 * this iclog is not in the DONE_SYNC/
2154 * DO_CALLBACK state, we skip the rest and
2155 * just try to clean up. If we set our iclog
2156 * to DO_CALLBACK, we will not process it when
2157 * we retry since a previous iclog is in the
2158 * CALLBACK and the state cannot change since
2159 * we are holding the LOG_LOCK.
2160 */
2161 if (!(iclog->ic_state &
2162 (XLOG_STATE_DONE_SYNC |
2163 XLOG_STATE_DO_CALLBACK))) {
2164 if (ciclog && (ciclog->ic_state ==
2165 XLOG_STATE_DONE_SYNC)) {
2166 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2167 }
2168 break;
2169 }
2170 /*
2171 * We now have an iclog that is in either the
2172 * DO_CALLBACK or DONE_SYNC states. The other
2173 * states (WANT_SYNC, SYNCING, or CALLBACK were
2174 * caught by the above if and are going to
2175 * clean (i.e. we aren't doing their callbacks)
2176 * see the above if.
2177 */
2178
2179 /*
2180 * We will do one more check here to see if we
2181 * have chased our tail around.
2182 */
2183
2184 lowest_lsn = xlog_get_lowest_lsn(log);
2185 if (lowest_lsn && (
2186 XFS_LSN_CMP(
2187 lowest_lsn,
2188 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2189 )<0)) {
2190 iclog = iclog->ic_next;
2191 continue; /* Leave this iclog for
2192 * another thread */
2193 }
2194
2195 iclog->ic_state = XLOG_STATE_CALLBACK;
2196
2197 LOG_UNLOCK(log, s);
2198
2199 /* l_last_sync_lsn field protected by
2200 * GRANT_LOCK. Don't worry about iclog's lsn.
2201 * No one else can be here except us.
2202 */
2203 s = GRANT_LOCK(log);
2204 ASSERT(XFS_LSN_CMP(
2205 log->l_last_sync_lsn,
2206 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2207 )<=0);
2208 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2209 GRANT_UNLOCK(log, s);
2210
2211 /*
2212 * Keep processing entries in the callback list
2213 * until we come around and it is empty. We
2214 * need to atomically see that the list is
2215 * empty and change the state to DIRTY so that
2216 * we don't miss any more callbacks being added.
2217 */
2218 s = LOG_LOCK(log);
2219 } else {
2220 ioerrors++;
2221 }
2222 cb = iclog->ic_callback;
2223
2224 while (cb != 0) {
2225 iclog->ic_callback_tail = &(iclog->ic_callback);
2226 iclog->ic_callback = NULL;
2227 LOG_UNLOCK(log, s);
2228
2229 /* perform callbacks in the order given */
2230 for (; cb != 0; cb = cb_next) {
2231 cb_next = cb->cb_next;
2232 cb->cb_func(cb->cb_arg, aborted);
2233 }
2234 s = LOG_LOCK(log);
2235 cb = iclog->ic_callback;
2236 }
2237
2238 loopdidcallbacks++;
2239 funcdidcallbacks++;
2240
2241 ASSERT(iclog->ic_callback == 0);
2242 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2243 iclog->ic_state = XLOG_STATE_DIRTY;
2244
2245 /*
2246 * Transition from DIRTY to ACTIVE if applicable.
2247 * NOP if STATE_IOERROR.
2248 */
2249 xlog_state_clean_log(log);
2250
2251 /* wake up threads waiting in xfs_log_force() */
2252 sv_broadcast(&iclog->ic_forcesema);
2253
2254 iclog = iclog->ic_next;
2255 } while (first_iclog != iclog);
2256 if (repeats && (repeats % 10) == 0) {
2257 xfs_fs_cmn_err(CE_WARN, log->l_mp,
2258 "xlog_state_do_callback: looping %d", repeats);
2259 }
2260 } while (!ioerrors && loopdidcallbacks);
2261
2262 /*
2263 * make one last gasp attempt to see if iclogs are being left in
2264 * limbo..
2265 */
2266#ifdef DEBUG
2267 if (funcdidcallbacks) {
2268 first_iclog = iclog = log->l_iclog;
2269 do {
2270 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2271 /*
2272 * Terminate the loop if iclogs are found in states
2273 * which will cause other threads to clean up iclogs.
2274 *
2275 * SYNCING - i/o completion will go through logs
2276 * DONE_SYNC - interrupt thread should be waiting for
2277 * LOG_LOCK
2278 * IOERROR - give up hope all ye who enter here
2279 */
2280 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2281 iclog->ic_state == XLOG_STATE_SYNCING ||
2282 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2283 iclog->ic_state == XLOG_STATE_IOERROR )
2284 break;
2285 iclog = iclog->ic_next;
2286 } while (first_iclog != iclog);
2287 }
2288#endif
2289
2290 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2291 flushcnt = log->l_flushcnt;
2292 log->l_flushcnt = 0;
2293 }
2294 LOG_UNLOCK(log, s);
2295 while (flushcnt--)
2296 vsema(&log->l_flushsema);
2297} /* xlog_state_do_callback */
2298
2299
2300/*
2301 * Finish transitioning this iclog to the dirty state.
2302 *
2303 * Make sure that we completely execute this routine only when this is
2304 * the last call to the iclog. There is a good chance that iclog flushes,
2305 * when we reach the end of the physical log, get turned into 2 separate
2306 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2307 * routine. By using the reference count bwritecnt, we guarantee that only
2308 * the second completion goes through.
2309 *
2310 * Callbacks could take time, so they are done outside the scope of the
2311 * global state machine log lock. Assume that the calls to cvsema won't
2312 * take a long time. At least we know it won't sleep.
2313 */
2314void
2315xlog_state_done_syncing(
2316 xlog_in_core_t *iclog,
2317 int aborted)
2318{
2319 xlog_t *log = iclog->ic_log;
2320 SPLDECL(s);
2321
2322 s = LOG_LOCK(log);
2323
2324 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2325 iclog->ic_state == XLOG_STATE_IOERROR);
2326 ASSERT(iclog->ic_refcnt == 0);
2327 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2328
2329
2330 /*
2331 * If we got an error, either on the first buffer, or in the case of
2332 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2333 * and none should ever be attempted to be written to disk
2334 * again.
2335 */
2336 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2337 if (--iclog->ic_bwritecnt == 1) {
2338 LOG_UNLOCK(log, s);
2339 return;
2340 }
2341 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2342 }
2343
2344 /*
2345 * Someone could be sleeping prior to writing out the next
2346 * iclog buffer, we wake them all, one will get to do the
2347 * I/O, the others get to wait for the result.
2348 */
2349 sv_broadcast(&iclog->ic_writesema);
2350 LOG_UNLOCK(log, s);
2351 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2352} /* xlog_state_done_syncing */
2353
2354
2355/*
2356 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2357 * sleep. The flush semaphore is set to the number of in-core buffers and
2358 * decremented around disk syncing. Therefore, if all buffers are syncing,
2359 * this semaphore will cause new writes to sleep until a sync completes.
2360 * Otherwise, this code just does p() followed by v(). This approximates
2361 * a sleep/wakeup except we can't race.
2362 *
2363 * The in-core logs are used in a circular fashion. They are not used
2364 * out-of-order even when an iclog past the head is free.
2365 *
2366 * return:
2367 * * log_offset where xlog_write() can start writing into the in-core
2368 * log's data space.
2369 * * in-core log pointer to which xlog_write() should write.
2370 * * boolean indicating this is a continued write to an in-core log.
2371 * If this is the last write, then the in-core log's offset field
2372 * needs to be incremented, depending on the amount of data which
2373 * is copied.
2374 */
2375int
2376xlog_state_get_iclog_space(xlog_t *log,
2377 int len,
2378 xlog_in_core_t **iclogp,
2379 xlog_ticket_t *ticket,
2380 int *continued_write,
2381 int *logoffsetp)
2382{
2383 SPLDECL(s);
2384 int log_offset;
2385 xlog_rec_header_t *head;
2386 xlog_in_core_t *iclog;
2387 int error;
2388
2389restart:
2390 s = LOG_LOCK(log);
2391 if (XLOG_FORCED_SHUTDOWN(log)) {
2392 LOG_UNLOCK(log, s);
2393 return XFS_ERROR(EIO);
2394 }
2395
2396 iclog = log->l_iclog;
2397 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2398 log->l_flushcnt++;
2399 LOG_UNLOCK(log, s);
2400 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2401 XFS_STATS_INC(xs_log_noiclogs);
2402 /* Ensure that log writes happen */
2403 psema(&log->l_flushsema, PINOD);
2404 goto restart;
2405 }
2406 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2407 head = &iclog->ic_header;
2408
2409 iclog->ic_refcnt++; /* prevents sync */
2410 log_offset = iclog->ic_offset;
2411
2412 /* On the 1st write to an iclog, figure out lsn. This works
2413 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2414 * committing to. If the offset is set, that's how many blocks
2415 * must be written.
2416 */
2417 if (log_offset == 0) {
2418 ticket->t_curr_res -= log->l_iclog_hsize;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002419 XLOG_TIC_ADD_REGION(ticket,
2420 log->l_iclog_hsize,
2421 XLOG_REG_TYPE_LRHEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2423 ASSIGN_LSN(head->h_lsn, log);
2424 ASSERT(log->l_curr_block >= 0);
2425 }
2426
2427 /* If there is enough room to write everything, then do it. Otherwise,
2428 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2429 * bit is on, so this will get flushed out. Don't update ic_offset
2430 * until you know exactly how many bytes get copied. Therefore, wait
2431 * until later to update ic_offset.
2432 *
2433 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2434 * can fit into remaining data section.
2435 */
2436 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2437 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2438
2439 /* If I'm the only one writing to this iclog, sync it to disk */
2440 if (iclog->ic_refcnt == 1) {
2441 LOG_UNLOCK(log, s);
2442 if ((error = xlog_state_release_iclog(log, iclog)))
2443 return (error);
2444 } else {
2445 iclog->ic_refcnt--;
2446 LOG_UNLOCK(log, s);
2447 }
2448 goto restart;
2449 }
2450
2451 /* Do we have enough room to write the full amount in the remainder
2452 * of this iclog? Or must we continue a write on the next iclog and
2453 * mark this iclog as completely taken? In the case where we switch
2454 * iclogs (to mark it taken), this particular iclog will release/sync
2455 * to disk in xlog_write().
2456 */
2457 if (len <= iclog->ic_size - iclog->ic_offset) {
2458 *continued_write = 0;
2459 iclog->ic_offset += len;
2460 } else {
2461 *continued_write = 1;
2462 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2463 }
2464 *iclogp = iclog;
2465
2466 ASSERT(iclog->ic_offset <= iclog->ic_size);
2467 LOG_UNLOCK(log, s);
2468
2469 *logoffsetp = log_offset;
2470 return 0;
2471} /* xlog_state_get_iclog_space */
2472
2473/*
2474 * Atomically get the log space required for a log ticket.
2475 *
2476 * Once a ticket gets put onto the reserveq, it will only return after
2477 * the needed reservation is satisfied.
2478 */
2479STATIC int
2480xlog_grant_log_space(xlog_t *log,
2481 xlog_ticket_t *tic)
2482{
2483 int free_bytes;
2484 int need_bytes;
2485 SPLDECL(s);
2486#ifdef DEBUG
2487 xfs_lsn_t tail_lsn;
2488#endif
2489
2490
2491#ifdef DEBUG
2492 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2493 panic("grant Recovery problem");
2494#endif
2495
2496 /* Is there space or do we need to sleep? */
2497 s = GRANT_LOCK(log);
2498 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2499
2500 /* something is already sleeping; insert new transaction at end */
2501 if (log->l_reserve_headq) {
2502 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2503 xlog_trace_loggrant(log, tic,
2504 "xlog_grant_log_space: sleep 1");
2505 /*
2506 * Gotta check this before going to sleep, while we're
2507 * holding the grant lock.
2508 */
2509 if (XLOG_FORCED_SHUTDOWN(log))
2510 goto error_return;
2511
2512 XFS_STATS_INC(xs_sleep_logspace);
2513 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2514 /*
2515 * If we got an error, and the filesystem is shutting down,
2516 * we'll catch it down below. So just continue...
2517 */
2518 xlog_trace_loggrant(log, tic,
2519 "xlog_grant_log_space: wake 1");
2520 s = GRANT_LOCK(log);
2521 }
2522 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2523 need_bytes = tic->t_unit_res*tic->t_ocnt;
2524 else
2525 need_bytes = tic->t_unit_res;
2526
2527redo:
2528 if (XLOG_FORCED_SHUTDOWN(log))
2529 goto error_return;
2530
2531 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2532 log->l_grant_reserve_bytes);
2533 if (free_bytes < need_bytes) {
2534 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2535 XLOG_INS_TICKETQ(log->l_reserve_headq, tic);
2536 xlog_trace_loggrant(log, tic,
2537 "xlog_grant_log_space: sleep 2");
2538 XFS_STATS_INC(xs_sleep_logspace);
2539 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2540
2541 if (XLOG_FORCED_SHUTDOWN(log)) {
2542 s = GRANT_LOCK(log);
2543 goto error_return;
2544 }
2545
2546 xlog_trace_loggrant(log, tic,
2547 "xlog_grant_log_space: wake 2");
2548 xlog_grant_push_ail(log->l_mp, need_bytes);
2549 s = GRANT_LOCK(log);
2550 goto redo;
2551 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2552 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2553
2554 /* we've got enough space */
2555 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w');
2556 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'r');
2557#ifdef DEBUG
2558 tail_lsn = log->l_tail_lsn;
2559 /*
2560 * Check to make sure the grant write head didn't just over lap the
2561 * tail. If the cycles are the same, we can't be overlapping.
2562 * Otherwise, make sure that the cycles differ by exactly one and
2563 * check the byte count.
2564 */
2565 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2566 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2567 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2568 }
2569#endif
2570 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2571 xlog_verify_grant_head(log, 1);
2572 GRANT_UNLOCK(log, s);
2573 return 0;
2574
2575 error_return:
2576 if (tic->t_flags & XLOG_TIC_IN_Q)
2577 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2578 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2579 /*
2580 * If we are failing, make sure the ticket doesn't have any
2581 * current reservations. We don't want to add this back when
2582 * the ticket/transaction gets cancelled.
2583 */
2584 tic->t_curr_res = 0;
2585 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2586 GRANT_UNLOCK(log, s);
2587 return XFS_ERROR(EIO);
2588} /* xlog_grant_log_space */
2589
2590
2591/*
2592 * Replenish the byte reservation required by moving the grant write head.
2593 *
2594 *
2595 */
2596STATIC int
2597xlog_regrant_write_log_space(xlog_t *log,
2598 xlog_ticket_t *tic)
2599{
2600 SPLDECL(s);
2601 int free_bytes, need_bytes;
2602 xlog_ticket_t *ntic;
2603#ifdef DEBUG
2604 xfs_lsn_t tail_lsn;
2605#endif
2606
2607 tic->t_curr_res = tic->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002608 XLOG_TIC_RESET_RES(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (tic->t_cnt > 0)
2611 return (0);
2612
2613#ifdef DEBUG
2614 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2615 panic("regrant Recovery problem");
2616#endif
2617
2618 s = GRANT_LOCK(log);
2619 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2620
2621 if (XLOG_FORCED_SHUTDOWN(log))
2622 goto error_return;
2623
2624 /* If there are other waiters on the queue then give them a
2625 * chance at logspace before us. Wake up the first waiters,
2626 * if we do not wake up all the waiters then go to sleep waiting
2627 * for more free space, otherwise try to get some space for
2628 * this transaction.
2629 */
2630
2631 if ((ntic = log->l_write_headq)) {
2632 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2633 log->l_grant_write_bytes);
2634 do {
2635 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2636
2637 if (free_bytes < ntic->t_unit_res)
2638 break;
2639 free_bytes -= ntic->t_unit_res;
2640 sv_signal(&ntic->t_sema);
2641 ntic = ntic->t_next;
2642 } while (ntic != log->l_write_headq);
2643
2644 if (ntic != log->l_write_headq) {
2645 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2646 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2647
2648 xlog_trace_loggrant(log, tic,
2649 "xlog_regrant_write_log_space: sleep 1");
2650 XFS_STATS_INC(xs_sleep_logspace);
2651 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2652 &log->l_grant_lock, s);
2653
2654 /* If we're shutting down, this tic is already
2655 * off the queue */
2656 if (XLOG_FORCED_SHUTDOWN(log)) {
2657 s = GRANT_LOCK(log);
2658 goto error_return;
2659 }
2660
2661 xlog_trace_loggrant(log, tic,
2662 "xlog_regrant_write_log_space: wake 1");
2663 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2664 s = GRANT_LOCK(log);
2665 }
2666 }
2667
2668 need_bytes = tic->t_unit_res;
2669
2670redo:
2671 if (XLOG_FORCED_SHUTDOWN(log))
2672 goto error_return;
2673
2674 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2675 log->l_grant_write_bytes);
2676 if (free_bytes < need_bytes) {
2677 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2678 XLOG_INS_TICKETQ(log->l_write_headq, tic);
2679 XFS_STATS_INC(xs_sleep_logspace);
2680 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2681
2682 /* If we're shutting down, this tic is already off the queue */
2683 if (XLOG_FORCED_SHUTDOWN(log)) {
2684 s = GRANT_LOCK(log);
2685 goto error_return;
2686 }
2687
2688 xlog_trace_loggrant(log, tic,
2689 "xlog_regrant_write_log_space: wake 2");
2690 xlog_grant_push_ail(log->l_mp, need_bytes);
2691 s = GRANT_LOCK(log);
2692 goto redo;
2693 } else if (tic->t_flags & XLOG_TIC_IN_Q)
2694 XLOG_DEL_TICKETQ(log->l_write_headq, tic);
2695
2696 XLOG_GRANT_ADD_SPACE(log, need_bytes, 'w'); /* we've got enough space */
2697#ifdef DEBUG
2698 tail_lsn = log->l_tail_lsn;
2699 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2700 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2701 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2702 }
2703#endif
2704
2705 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2706 xlog_verify_grant_head(log, 1);
2707 GRANT_UNLOCK(log, s);
2708 return (0);
2709
2710
2711 error_return:
2712 if (tic->t_flags & XLOG_TIC_IN_Q)
2713 XLOG_DEL_TICKETQ(log->l_reserve_headq, tic);
2714 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2715 /*
2716 * If we are failing, make sure the ticket doesn't have any
2717 * current reservations. We don't want to add this back when
2718 * the ticket/transaction gets cancelled.
2719 */
2720 tic->t_curr_res = 0;
2721 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2722 GRANT_UNLOCK(log, s);
2723 return XFS_ERROR(EIO);
2724} /* xlog_regrant_write_log_space */
2725
2726
2727/* The first cnt-1 times through here we don't need to
2728 * move the grant write head because the permanent
2729 * reservation has reserved cnt times the unit amount.
2730 * Release part of current permanent unit reservation and
2731 * reset current reservation to be one units worth. Also
2732 * move grant reservation head forward.
2733 */
2734STATIC void
2735xlog_regrant_reserve_log_space(xlog_t *log,
2736 xlog_ticket_t *ticket)
2737{
2738 SPLDECL(s);
2739
2740 xlog_trace_loggrant(log, ticket,
2741 "xlog_regrant_reserve_log_space: enter");
2742 if (ticket->t_cnt > 0)
2743 ticket->t_cnt--;
2744
2745 s = GRANT_LOCK(log);
2746 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2747 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2748 ticket->t_curr_res = ticket->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002749 XLOG_TIC_RESET_RES(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 xlog_trace_loggrant(log, ticket,
2751 "xlog_regrant_reserve_log_space: sub current res");
2752 xlog_verify_grant_head(log, 1);
2753
2754 /* just return if we still have some of the pre-reserved space */
2755 if (ticket->t_cnt > 0) {
2756 GRANT_UNLOCK(log, s);
2757 return;
2758 }
2759
2760 XLOG_GRANT_ADD_SPACE(log, ticket->t_unit_res, 'r');
2761 xlog_trace_loggrant(log, ticket,
2762 "xlog_regrant_reserve_log_space: exit");
2763 xlog_verify_grant_head(log, 0);
2764 GRANT_UNLOCK(log, s);
2765 ticket->t_curr_res = ticket->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002766 XLOG_TIC_RESET_RES(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767} /* xlog_regrant_reserve_log_space */
2768
2769
2770/*
2771 * Give back the space left from a reservation.
2772 *
2773 * All the information we need to make a correct determination of space left
2774 * is present. For non-permanent reservations, things are quite easy. The
2775 * count should have been decremented to zero. We only need to deal with the
2776 * space remaining in the current reservation part of the ticket. If the
2777 * ticket contains a permanent reservation, there may be left over space which
2778 * needs to be released. A count of N means that N-1 refills of the current
2779 * reservation can be done before we need to ask for more space. The first
2780 * one goes to fill up the first current reservation. Once we run out of
2781 * space, the count will stay at zero and the only space remaining will be
2782 * in the current reservation field.
2783 */
2784STATIC void
2785xlog_ungrant_log_space(xlog_t *log,
2786 xlog_ticket_t *ticket)
2787{
2788 SPLDECL(s);
2789
2790 if (ticket->t_cnt > 0)
2791 ticket->t_cnt--;
2792
2793 s = GRANT_LOCK(log);
2794 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2795
2796 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'w');
2797 XLOG_GRANT_SUB_SPACE(log, ticket->t_curr_res, 'r');
2798
2799 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2800
2801 /* If this is a permanent reservation ticket, we may be able to free
2802 * up more space based on the remaining count.
2803 */
2804 if (ticket->t_cnt > 0) {
2805 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2806 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'w');
2807 XLOG_GRANT_SUB_SPACE(log, ticket->t_unit_res*ticket->t_cnt,'r');
2808 }
2809
2810 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2811 xlog_verify_grant_head(log, 1);
2812 GRANT_UNLOCK(log, s);
2813 xfs_log_move_tail(log->l_mp, 1);
2814} /* xlog_ungrant_log_space */
2815
2816
2817/*
2818 * Atomically put back used ticket.
2819 */
2820void
2821xlog_state_put_ticket(xlog_t *log,
2822 xlog_ticket_t *tic)
2823{
2824 unsigned long s;
2825
2826 s = LOG_LOCK(log);
2827 xlog_ticket_put(log, tic);
2828 LOG_UNLOCK(log, s);
2829} /* xlog_state_put_ticket */
2830
2831/*
2832 * Flush iclog to disk if this is the last reference to the given iclog and
2833 * the WANT_SYNC bit is set.
2834 *
2835 * When this function is entered, the iclog is not necessarily in the
2836 * WANT_SYNC state. It may be sitting around waiting to get filled.
2837 *
2838 *
2839 */
2840int
2841xlog_state_release_iclog(xlog_t *log,
2842 xlog_in_core_t *iclog)
2843{
2844 SPLDECL(s);
2845 int sync = 0; /* do we sync? */
2846
2847 xlog_assign_tail_lsn(log->l_mp);
2848
2849 s = LOG_LOCK(log);
2850
2851 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2852 LOG_UNLOCK(log, s);
2853 return XFS_ERROR(EIO);
2854 }
2855
2856 ASSERT(iclog->ic_refcnt > 0);
2857 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2858 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2859
2860 if (--iclog->ic_refcnt == 0 &&
2861 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2862 sync++;
2863 iclog->ic_state = XLOG_STATE_SYNCING;
2864 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2865 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2866 /* cycle incremented when incrementing curr_block */
2867 }
2868
2869 LOG_UNLOCK(log, s);
2870
2871 /*
2872 * We let the log lock go, so it's possible that we hit a log I/O
2873 * error or someother SHUTDOWN condition that marks the iclog
2874 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2875 * this iclog has consistent data, so we ignore IOERROR
2876 * flags after this point.
2877 */
2878 if (sync) {
2879 return xlog_sync(log, iclog);
2880 }
2881 return (0);
2882
2883} /* xlog_state_release_iclog */
2884
2885
2886/*
2887 * This routine will mark the current iclog in the ring as WANT_SYNC
2888 * and move the current iclog pointer to the next iclog in the ring.
2889 * When this routine is called from xlog_state_get_iclog_space(), the
2890 * exact size of the iclog has not yet been determined. All we know is
2891 * that every data block. We have run out of space in this log record.
2892 */
2893STATIC void
2894xlog_state_switch_iclogs(xlog_t *log,
2895 xlog_in_core_t *iclog,
2896 int eventual_size)
2897{
2898 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2899 if (!eventual_size)
2900 eventual_size = iclog->ic_offset;
2901 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2902 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2903 log->l_prev_block = log->l_curr_block;
2904 log->l_prev_cycle = log->l_curr_cycle;
2905
2906 /* roll log?: ic_offset changed later */
2907 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2908
2909 /* Round up to next log-sunit */
2910 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2911 log->l_mp->m_sb.sb_logsunit > 1) {
2912 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2913 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2914 }
2915
2916 if (log->l_curr_block >= log->l_logBBsize) {
2917 log->l_curr_cycle++;
2918 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2919 log->l_curr_cycle++;
2920 log->l_curr_block -= log->l_logBBsize;
2921 ASSERT(log->l_curr_block >= 0);
2922 }
2923 ASSERT(iclog == log->l_iclog);
2924 log->l_iclog = iclog->ic_next;
2925} /* xlog_state_switch_iclogs */
2926
2927
2928/*
2929 * Write out all data in the in-core log as of this exact moment in time.
2930 *
2931 * Data may be written to the in-core log during this call. However,
2932 * we don't guarantee this data will be written out. A change from past
2933 * implementation means this routine will *not* write out zero length LRs.
2934 *
2935 * Basically, we try and perform an intelligent scan of the in-core logs.
2936 * If we determine there is no flushable data, we just return. There is no
2937 * flushable data if:
2938 *
2939 * 1. the current iclog is active and has no data; the previous iclog
2940 * is in the active or dirty state.
2941 * 2. the current iclog is drity, and the previous iclog is in the
2942 * active or dirty state.
2943 *
2944 * We may sleep (call psema) if:
2945 *
2946 * 1. the current iclog is not in the active nor dirty state.
2947 * 2. the current iclog dirty, and the previous iclog is not in the
2948 * active nor dirty state.
2949 * 3. the current iclog is active, and there is another thread writing
2950 * to this particular iclog.
2951 * 4. a) the current iclog is active and has no other writers
2952 * b) when we return from flushing out this iclog, it is still
2953 * not in the active nor dirty state.
2954 */
2955STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002956xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 xlog_in_core_t *iclog;
2959 xfs_lsn_t lsn;
2960 SPLDECL(s);
2961
2962 s = LOG_LOCK(log);
2963
2964 iclog = log->l_iclog;
2965 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2966 LOG_UNLOCK(log, s);
2967 return XFS_ERROR(EIO);
2968 }
2969
2970 /* If the head iclog is not active nor dirty, we just attach
2971 * ourselves to the head and go to sleep.
2972 */
2973 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2974 iclog->ic_state == XLOG_STATE_DIRTY) {
2975 /*
2976 * If the head is dirty or (active and empty), then
2977 * we need to look at the previous iclog. If the previous
2978 * iclog is active or dirty we are done. There is nothing
2979 * to sync out. Otherwise, we attach ourselves to the
2980 * previous iclog and go to sleep.
2981 */
2982 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2983 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2984 iclog = iclog->ic_prev;
2985 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2986 iclog->ic_state == XLOG_STATE_DIRTY)
2987 goto no_sleep;
2988 else
2989 goto maybe_sleep;
2990 } else {
2991 if (iclog->ic_refcnt == 0) {
2992 /* We are the only one with access to this
2993 * iclog. Flush it out now. There should
2994 * be a roundoff of zero to show that someone
2995 * has already taken care of the roundoff from
2996 * the previous sync.
2997 */
2998 iclog->ic_refcnt++;
2999 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
3000 xlog_state_switch_iclogs(log, iclog, 0);
3001 LOG_UNLOCK(log, s);
3002
3003 if (xlog_state_release_iclog(log, iclog))
3004 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003005 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 s = LOG_LOCK(log);
3007 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
3008 iclog->ic_state != XLOG_STATE_DIRTY)
3009 goto maybe_sleep;
3010 else
3011 goto no_sleep;
3012 } else {
3013 /* Someone else is writing to this iclog.
3014 * Use its call to flush out the data. However,
3015 * the other thread may not force out this LR,
3016 * so we mark it WANT_SYNC.
3017 */
3018 xlog_state_switch_iclogs(log, iclog, 0);
3019 goto maybe_sleep;
3020 }
3021 }
3022 }
3023
3024 /* By the time we come around again, the iclog could've been filled
3025 * which would give it another lsn. If we have a new lsn, just
3026 * return because the relevant data has been flushed.
3027 */
3028maybe_sleep:
3029 if (flags & XFS_LOG_SYNC) {
3030 /*
3031 * We must check if we're shutting down here, before
3032 * we wait, while we're holding the LOG_LOCK.
3033 * Then we check again after waking up, in case our
3034 * sleep was disturbed by a bad news.
3035 */
3036 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3037 LOG_UNLOCK(log, s);
3038 return XFS_ERROR(EIO);
3039 }
3040 XFS_STATS_INC(xs_log_force_sleep);
3041 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3042 /*
3043 * No need to grab the log lock here since we're
3044 * only deciding whether or not to return EIO
3045 * and the memory read should be atomic.
3046 */
3047 if (iclog->ic_state & XLOG_STATE_IOERROR)
3048 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003049 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 } else {
3052
3053no_sleep:
3054 LOG_UNLOCK(log, s);
3055 }
3056 return 0;
3057} /* xlog_state_sync_all */
3058
3059
3060/*
3061 * Used by code which implements synchronous log forces.
3062 *
3063 * Find in-core log with lsn.
3064 * If it is in the DIRTY state, just return.
3065 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3066 * state and go to sleep or return.
3067 * If it is in any other state, go to sleep or return.
3068 *
3069 * If filesystem activity goes to zero, the iclog will get flushed only by
3070 * bdflush().
3071 */
3072int
3073xlog_state_sync(xlog_t *log,
3074 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003075 uint flags,
3076 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
3078 xlog_in_core_t *iclog;
3079 int already_slept = 0;
3080 SPLDECL(s);
3081
3082
3083try_again:
3084 s = LOG_LOCK(log);
3085 iclog = log->l_iclog;
3086
3087 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3088 LOG_UNLOCK(log, s);
3089 return XFS_ERROR(EIO);
3090 }
3091
3092 do {
3093 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
3094 iclog = iclog->ic_next;
3095 continue;
3096 }
3097
3098 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3099 LOG_UNLOCK(log, s);
3100 return 0;
3101 }
3102
3103 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3104 /*
3105 * We sleep here if we haven't already slept (e.g.
3106 * this is the first time we've looked at the correct
3107 * iclog buf) and the buffer before us is going to
3108 * be sync'ed. The reason for this is that if we
3109 * are doing sync transactions here, by waiting for
3110 * the previous I/O to complete, we can allow a few
3111 * more transactions into this iclog before we close
3112 * it down.
3113 *
3114 * Otherwise, we mark the buffer WANT_SYNC, and bump
3115 * up the refcnt so we can release the log (which drops
3116 * the ref count). The state switch keeps new transaction
3117 * commits from using this buffer. When the current commits
3118 * finish writing into the buffer, the refcount will drop to
3119 * zero and the buffer will go out then.
3120 */
3121 if (!already_slept &&
3122 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3123 XLOG_STATE_SYNCING))) {
3124 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3125 XFS_STATS_INC(xs_log_force_sleep);
3126 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3127 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003128 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 already_slept = 1;
3130 goto try_again;
3131 } else {
3132 iclog->ic_refcnt++;
3133 xlog_state_switch_iclogs(log, iclog, 0);
3134 LOG_UNLOCK(log, s);
3135 if (xlog_state_release_iclog(log, iclog))
3136 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003137 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 s = LOG_LOCK(log);
3139 }
3140 }
3141
3142 if ((flags & XFS_LOG_SYNC) && /* sleep */
3143 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3144
3145 /*
3146 * Don't wait on the forcesema if we know that we've
3147 * gotten a log write error.
3148 */
3149 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3150 LOG_UNLOCK(log, s);
3151 return XFS_ERROR(EIO);
3152 }
3153 XFS_STATS_INC(xs_log_force_sleep);
3154 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3155 /*
3156 * No need to grab the log lock here since we're
3157 * only deciding whether or not to return EIO
3158 * and the memory read should be atomic.
3159 */
3160 if (iclog->ic_state & XLOG_STATE_IOERROR)
3161 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003162 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 } else { /* just return */
3164 LOG_UNLOCK(log, s);
3165 }
3166 return 0;
3167
3168 } while (iclog != log->l_iclog);
3169
3170 LOG_UNLOCK(log, s);
3171 return (0);
3172} /* xlog_state_sync */
3173
3174
3175/*
3176 * Called when we want to mark the current iclog as being ready to sync to
3177 * disk.
3178 */
3179void
3180xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3181{
3182 SPLDECL(s);
3183
3184 s = LOG_LOCK(log);
3185
3186 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3187 xlog_state_switch_iclogs(log, iclog, 0);
3188 } else {
3189 ASSERT(iclog->ic_state &
3190 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3191 }
3192
3193 LOG_UNLOCK(log, s);
3194} /* xlog_state_want_sync */
3195
3196
3197
3198/*****************************************************************************
3199 *
3200 * TICKET functions
3201 *
3202 *****************************************************************************
3203 */
3204
3205/*
3206 * Algorithm doesn't take into account page size. ;-(
3207 */
3208STATIC void
3209xlog_state_ticket_alloc(xlog_t *log)
3210{
3211 xlog_ticket_t *t_list;
3212 xlog_ticket_t *next;
3213 xfs_caddr_t buf;
3214 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3215 SPLDECL(s);
3216
3217 /*
3218 * The kmem_zalloc may sleep, so we shouldn't be holding the
3219 * global lock. XXXmiken: may want to use zone allocator.
3220 */
3221 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3222
3223 s = LOG_LOCK(log);
3224
3225 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3226 t_list = (xlog_ticket_t *)buf;
3227 t_list->t_next = log->l_unmount_free;
3228 log->l_unmount_free = t_list++;
3229 log->l_ticket_cnt++;
3230 log->l_ticket_tcnt++;
3231
3232 /* Next ticket becomes first ticket attached to ticket free list */
3233 if (log->l_freelist != NULL) {
3234 ASSERT(log->l_tail != NULL);
3235 log->l_tail->t_next = t_list;
3236 } else {
3237 log->l_freelist = t_list;
3238 }
3239 log->l_ticket_cnt++;
3240 log->l_ticket_tcnt++;
3241
3242 /* Cycle through rest of alloc'ed memory, building up free Q */
3243 for ( ; i > 0; i--) {
3244 next = t_list + 1;
3245 t_list->t_next = next;
3246 t_list = next;
3247 log->l_ticket_cnt++;
3248 log->l_ticket_tcnt++;
3249 }
3250 t_list->t_next = NULL;
3251 log->l_tail = t_list;
3252 LOG_UNLOCK(log, s);
3253} /* xlog_state_ticket_alloc */
3254
3255
3256/*
3257 * Put ticket into free list
3258 *
3259 * Assumption: log lock is held around this call.
3260 */
3261STATIC void
3262xlog_ticket_put(xlog_t *log,
3263 xlog_ticket_t *ticket)
3264{
3265 sv_destroy(&ticket->t_sema);
3266
3267 /*
3268 * Don't think caching will make that much difference. It's
3269 * more important to make debug easier.
3270 */
3271#if 0
3272 /* real code will want to use LIFO for caching */
3273 ticket->t_next = log->l_freelist;
3274 log->l_freelist = ticket;
3275 /* no need to clear fields */
3276#else
3277 /* When we debug, it is easier if tickets are cycled */
3278 ticket->t_next = NULL;
3279 if (log->l_tail != 0) {
3280 log->l_tail->t_next = ticket;
3281 } else {
3282 ASSERT(log->l_freelist == 0);
3283 log->l_freelist = ticket;
3284 }
3285 log->l_tail = ticket;
3286#endif /* DEBUG */
3287 log->l_ticket_cnt++;
3288} /* xlog_ticket_put */
3289
3290
3291/*
3292 * Grab ticket off freelist or allocation some more
3293 */
3294xlog_ticket_t *
3295xlog_ticket_get(xlog_t *log,
3296 int unit_bytes,
3297 int cnt,
3298 char client,
3299 uint xflags)
3300{
3301 xlog_ticket_t *tic;
3302 uint num_headers;
3303 SPLDECL(s);
3304
3305 alloc:
3306 if (log->l_freelist == NULL)
3307 xlog_state_ticket_alloc(log); /* potentially sleep */
3308
3309 s = LOG_LOCK(log);
3310 if (log->l_freelist == NULL) {
3311 LOG_UNLOCK(log, s);
3312 goto alloc;
3313 }
3314 tic = log->l_freelist;
3315 log->l_freelist = tic->t_next;
3316 if (log->l_freelist == NULL)
3317 log->l_tail = NULL;
3318 log->l_ticket_cnt--;
3319 LOG_UNLOCK(log, s);
3320
3321 /*
3322 * Permanent reservations have up to 'cnt'-1 active log operations
3323 * in the log. A unit in this case is the amount of space for one
3324 * of these log operations. Normal reservations have a cnt of 1
3325 * and their unit amount is the total amount of space required.
3326 *
3327 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003328 * which occupy space in the on-disk log.
3329 *
3330 * Normal form of a transaction is:
3331 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3332 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3333 *
3334 * We need to account for all the leadup data and trailer data
3335 * around the transaction data.
3336 * And then we need to account for the worst case in terms of using
3337 * more space.
3338 * The worst case will happen if:
3339 * - the placement of the transaction happens to be such that the
3340 * roundoff is at its maximum
3341 * - the transaction data is synced before the commit record is synced
3342 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3343 * Therefore the commit record is in its own Log Record.
3344 * This can happen as the commit record is called with its
3345 * own region to xlog_write().
3346 * This then means that in the worst case, roundoff can happen for
3347 * the commit-rec as well.
3348 * The commit-rec is smaller than padding in this scenario and so it is
3349 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 */
3351
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003352 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003354 unit_bytes += sizeof(xfs_trans_header_t);
3355
3356 /* for start-rec */
3357 unit_bytes += sizeof(xlog_op_header_t);
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 /* for LR headers */
3360 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3361 unit_bytes += log->l_iclog_hsize * num_headers;
3362
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003363 /* for commit-rec LR header - note: padding will subsume the ophdr */
3364 unit_bytes += log->l_iclog_hsize;
3365
3366 /* for split-recs - ophdrs added when data split over LRs */
3367 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3368
3369 /* for roundoff padding for transaction data and one for commit record */
3370 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3371 log->l_mp->m_sb.sb_logsunit > 1) {
3372 /* log su roundoff */
3373 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3374 } else {
3375 /* BB roundoff */
3376 unit_bytes += 2*BBSIZE;
3377 }
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 tic->t_unit_res = unit_bytes;
3380 tic->t_curr_res = unit_bytes;
3381 tic->t_cnt = cnt;
3382 tic->t_ocnt = cnt;
3383 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3384 tic->t_clientid = client;
3385 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003386 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (xflags & XFS_LOG_PERM_RESERV)
3388 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3389 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3390
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003391 XLOG_TIC_RESET_RES(tic);
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 return tic;
3394} /* xlog_ticket_get */
3395
3396
3397/******************************************************************************
3398 *
3399 * Log debug routines
3400 *
3401 ******************************************************************************
3402 */
3403#if defined(DEBUG) && !defined(XLOG_NOLOG)
3404/*
3405 * Make sure that the destination ptr is within the valid data region of
3406 * one of the iclogs. This uses backup pointers stored in a different
3407 * part of the log in case we trash the log structure.
3408 */
3409void
3410xlog_verify_dest_ptr(xlog_t *log,
3411 __psint_t ptr)
3412{
3413 int i;
3414 int good_ptr = 0;
3415
3416 for (i=0; i < log->l_iclog_bufs; i++) {
3417 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3418 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3419 good_ptr++;
3420 }
3421 if (! good_ptr)
3422 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3423} /* xlog_verify_dest_ptr */
3424
3425STATIC void
3426xlog_verify_grant_head(xlog_t *log, int equals)
3427{
3428 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3429 if (equals)
3430 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3431 else
3432 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3433 } else {
3434 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3435 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3436 }
3437} /* xlog_verify_grant_head */
3438
3439/* check if it will fit */
3440STATIC void
3441xlog_verify_tail_lsn(xlog_t *log,
3442 xlog_in_core_t *iclog,
3443 xfs_lsn_t tail_lsn)
3444{
3445 int blocks;
3446
3447 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3448 blocks =
3449 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3450 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3451 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3452 } else {
3453 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3454
3455 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3456 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3457
3458 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3459 if (blocks < BTOBB(iclog->ic_offset) + 1)
3460 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3461 }
3462} /* xlog_verify_tail_lsn */
3463
3464/*
3465 * Perform a number of checks on the iclog before writing to disk.
3466 *
3467 * 1. Make sure the iclogs are still circular
3468 * 2. Make sure we have a good magic number
3469 * 3. Make sure we don't have magic numbers in the data
3470 * 4. Check fields of each log operation header for:
3471 * A. Valid client identifier
3472 * B. tid ptr value falls in valid ptr space (user space code)
3473 * C. Length in log record header is correct according to the
3474 * individual operation headers within record.
3475 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3476 * log, check the preceding blocks of the physical log to make sure all
3477 * the cycle numbers agree with the current cycle number.
3478 */
3479STATIC void
3480xlog_verify_iclog(xlog_t *log,
3481 xlog_in_core_t *iclog,
3482 int count,
3483 boolean_t syncing)
3484{
3485 xlog_op_header_t *ophead;
3486 xlog_in_core_t *icptr;
3487 xlog_in_core_2_t *xhdr;
3488 xfs_caddr_t ptr;
3489 xfs_caddr_t base_ptr;
3490 __psint_t field_offset;
3491 __uint8_t clientid;
3492 int len, i, j, k, op_len;
3493 int idx;
3494 SPLDECL(s);
3495
3496 /* check validity of iclog pointers */
3497 s = LOG_LOCK(log);
3498 icptr = log->l_iclog;
3499 for (i=0; i < log->l_iclog_bufs; i++) {
3500 if (icptr == 0)
3501 xlog_panic("xlog_verify_iclog: invalid ptr");
3502 icptr = icptr->ic_next;
3503 }
3504 if (icptr != log->l_iclog)
3505 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3506 LOG_UNLOCK(log, s);
3507
3508 /* check log magic numbers */
3509 ptr = (xfs_caddr_t) &(iclog->ic_header);
3510 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3511 xlog_panic("xlog_verify_iclog: invalid magic num");
3512
3513 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3514 ptr += BBSIZE) {
3515 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3516 xlog_panic("xlog_verify_iclog: unexpected magic num");
3517 }
3518
3519 /* check fields */
3520 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3521 ptr = iclog->ic_datap;
3522 base_ptr = ptr;
3523 ophead = (xlog_op_header_t *)ptr;
3524 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3525 for (i = 0; i < len; i++) {
3526 ophead = (xlog_op_header_t *)ptr;
3527
3528 /* clientid is only 1 byte */
3529 field_offset = (__psint_t)
3530 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3531 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3532 clientid = ophead->oh_clientid;
3533 } else {
3534 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3535 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3536 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3537 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3538 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3539 } else {
3540 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3541 }
3542 }
3543 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003544 cmn_err(CE_WARN, "xlog_verify_iclog: "
3545 "invalid clientid %d op 0x%p offset 0x%lx",
3546 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 /* check length */
3549 field_offset = (__psint_t)
3550 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3551 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3552 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3553 } else {
3554 idx = BTOBBT((__psint_t)&ophead->oh_len -
3555 (__psint_t)iclog->ic_datap);
3556 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3557 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3558 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3559 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3560 } else {
3561 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3562 }
3563 }
3564 ptr += sizeof(xlog_op_header_t) + op_len;
3565 }
3566} /* xlog_verify_iclog */
3567#endif /* DEBUG && !XLOG_NOLOG */
3568
3569/*
3570 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3571 */
3572STATIC int
3573xlog_state_ioerror(
3574 xlog_t *log)
3575{
3576 xlog_in_core_t *iclog, *ic;
3577
3578 iclog = log->l_iclog;
3579 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3580 /*
3581 * Mark all the incore logs IOERROR.
3582 * From now on, no log flushes will result.
3583 */
3584 ic = iclog;
3585 do {
3586 ic->ic_state = XLOG_STATE_IOERROR;
3587 ic = ic->ic_next;
3588 } while (ic != iclog);
3589 return (0);
3590 }
3591 /*
3592 * Return non-zero, if state transition has already happened.
3593 */
3594 return (1);
3595}
3596
3597/*
3598 * This is called from xfs_force_shutdown, when we're forcibly
3599 * shutting down the filesystem, typically because of an IO error.
3600 * Our main objectives here are to make sure that:
3601 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3602 * parties to find out, 'atomically'.
3603 * b. those who're sleeping on log reservations, pinned objects and
3604 * other resources get woken up, and be told the bad news.
3605 * c. nothing new gets queued up after (a) and (b) are done.
3606 * d. if !logerror, flush the iclogs to disk, then seal them off
3607 * for business.
3608 */
3609int
3610xfs_log_force_umount(
3611 struct xfs_mount *mp,
3612 int logerror)
3613{
3614 xlog_ticket_t *tic;
3615 xlog_t *log;
3616 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003617 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 SPLDECL(s);
3619 SPLDECL(s2);
3620
3621 log = mp->m_log;
3622
3623 /*
3624 * If this happens during log recovery, don't worry about
3625 * locking; the log isn't open for business yet.
3626 */
3627 if (!log ||
3628 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3629 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3630 XFS_BUF_DONE(mp->m_sb_bp);
3631 return (0);
3632 }
3633
3634 /*
3635 * Somebody could've already done the hard work for us.
3636 * No need to get locks for this.
3637 */
3638 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3639 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3640 return (1);
3641 }
3642 retval = 0;
3643 /*
3644 * We must hold both the GRANT lock and the LOG lock,
3645 * before we mark the filesystem SHUTDOWN and wake
3646 * everybody up to tell the bad news.
3647 */
3648 s = GRANT_LOCK(log);
3649 s2 = LOG_LOCK(log);
3650 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3651 XFS_BUF_DONE(mp->m_sb_bp);
3652 /*
3653 * This flag is sort of redundant because of the mount flag, but
3654 * it's good to maintain the separation between the log and the rest
3655 * of XFS.
3656 */
3657 log->l_flags |= XLOG_IO_ERROR;
3658
3659 /*
3660 * If we hit a log error, we want to mark all the iclogs IOERROR
3661 * while we're still holding the loglock.
3662 */
3663 if (logerror)
3664 retval = xlog_state_ioerror(log);
3665 LOG_UNLOCK(log, s2);
3666
3667 /*
3668 * We don't want anybody waiting for log reservations
3669 * after this. That means we have to wake up everybody
3670 * queued up on reserve_headq as well as write_headq.
3671 * In addition, we make sure in xlog_{re}grant_log_space
3672 * that we don't enqueue anything once the SHUTDOWN flag
3673 * is set, and this action is protected by the GRANTLOCK.
3674 */
3675 if ((tic = log->l_reserve_headq)) {
3676 do {
3677 sv_signal(&tic->t_sema);
3678 tic = tic->t_next;
3679 } while (tic != log->l_reserve_headq);
3680 }
3681
3682 if ((tic = log->l_write_headq)) {
3683 do {
3684 sv_signal(&tic->t_sema);
3685 tic = tic->t_next;
3686 } while (tic != log->l_write_headq);
3687 }
3688 GRANT_UNLOCK(log, s);
3689
3690 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3691 ASSERT(!logerror);
3692 /*
3693 * Force the incore logs to disk before shutting the
3694 * log down completely.
3695 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003696 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 s2 = LOG_LOCK(log);
3698 retval = xlog_state_ioerror(log);
3699 LOG_UNLOCK(log, s2);
3700 }
3701 /*
3702 * Wake up everybody waiting on xfs_log_force.
3703 * Callback all log item committed functions as if the
3704 * log writes were completed.
3705 */
3706 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3707
3708#ifdef XFSERRORDEBUG
3709 {
3710 xlog_in_core_t *iclog;
3711
3712 s = LOG_LOCK(log);
3713 iclog = log->l_iclog;
3714 do {
3715 ASSERT(iclog->ic_callback == 0);
3716 iclog = iclog->ic_next;
3717 } while (iclog != log->l_iclog);
3718 LOG_UNLOCK(log, s);
3719 }
3720#endif
3721 /* return non-zero if log IOERROR transition had already happened */
3722 return (retval);
3723}
3724
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003725STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726xlog_iclogs_empty(xlog_t *log)
3727{
3728 xlog_in_core_t *iclog;
3729
3730 iclog = log->l_iclog;
3731 do {
3732 /* endianness does not matter here, zero is zero in
3733 * any language.
3734 */
3735 if (iclog->ic_header.h_num_logops)
3736 return(0);
3737 iclog = iclog->ic_next;
3738 } while (iclog != log->l_iclog);
3739 return(1);
3740}