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