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