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