xfs: don't try to write a start record into every iclog
[linux-block.git] / fs / xfs / xfs_log.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4 6#include "xfs.h"
a844f451 7#include "xfs_fs.h"
70a9883c 8#include "xfs_shared.h"
a4fbe6ab 9#include "xfs_format.h"
239880ef
DC
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
1da177e4 12#include "xfs_mount.h"
e9e899a2 13#include "xfs_errortag.h"
1da177e4 14#include "xfs_error.h"
239880ef
DC
15#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
17#include "xfs_log.h"
1da177e4 18#include "xfs_log_priv.h"
0b1b213f 19#include "xfs_trace.h"
baff4e44 20#include "xfs_sysfs.h"
61e63ecb 21#include "xfs_sb.h"
39353ff6 22#include "xfs_health.h"
1da177e4 23
eb01c9cd 24kmem_zone_t *xfs_log_ticket_zone;
1da177e4 25
1da177e4 26/* Local miscellaneous function prototypes */
ad223e60
MT
27STATIC int
28xlog_commit_record(
29 struct xlog *log,
30 struct xlog_ticket *ticket,
31 struct xlog_in_core **iclog,
32 xfs_lsn_t *commitlsnp);
33
9a8d2fdb
MT
34STATIC struct xlog *
35xlog_alloc_log(
36 struct xfs_mount *mp,
37 struct xfs_buftarg *log_target,
38 xfs_daddr_t blk_offset,
39 int num_bblks);
ad223e60
MT
40STATIC int
41xlog_space_left(
42 struct xlog *log,
43 atomic64_t *head);
9a8d2fdb
MT
44STATIC void
45xlog_dealloc_log(
46 struct xlog *log);
1da177e4
LT
47
48/* local state machine functions */
d15cbf2f 49STATIC void xlog_state_done_syncing(
12e6a0f4 50 struct xlog_in_core *iclog);
9a8d2fdb
MT
51STATIC int
52xlog_state_get_iclog_space(
53 struct xlog *log,
54 int len,
55 struct xlog_in_core **iclog,
56 struct xlog_ticket *ticket,
57 int *continued_write,
58 int *logoffsetp);
9a8d2fdb
MT
59STATIC void
60xlog_state_switch_iclogs(
61 struct xlog *log,
62 struct xlog_in_core *iclog,
63 int eventual_size);
64STATIC void
ad223e60 65xlog_grant_push_ail(
9a8d2fdb
MT
66 struct xlog *log,
67 int need_bytes);
68STATIC void
69xlog_regrant_reserve_log_space(
70 struct xlog *log,
71 struct xlog_ticket *ticket);
72STATIC void
73xlog_ungrant_log_space(
74 struct xlog *log,
75 struct xlog_ticket *ticket);
df732b29
CH
76STATIC void
77xlog_sync(
78 struct xlog *log,
79 struct xlog_in_core *iclog);
cfcbbbd0 80#if defined(DEBUG)
9a8d2fdb
MT
81STATIC void
82xlog_verify_dest_ptr(
83 struct xlog *log,
5809d5e0 84 void *ptr);
ad223e60
MT
85STATIC void
86xlog_verify_grant_tail(
9a8d2fdb
MT
87 struct xlog *log);
88STATIC void
89xlog_verify_iclog(
90 struct xlog *log,
91 struct xlog_in_core *iclog,
abca1f33 92 int count);
9a8d2fdb
MT
93STATIC void
94xlog_verify_tail_lsn(
95 struct xlog *log,
96 struct xlog_in_core *iclog,
97 xfs_lsn_t tail_lsn);
1da177e4
LT
98#else
99#define xlog_verify_dest_ptr(a,b)
3f336c6f 100#define xlog_verify_grant_tail(a)
abca1f33 101#define xlog_verify_iclog(a,b,c)
1da177e4
LT
102#define xlog_verify_tail_lsn(a,b,c)
103#endif
104
9a8d2fdb
MT
105STATIC int
106xlog_iclogs_empty(
107 struct xlog *log);
1da177e4 108
dd954c69 109static void
663e496a 110xlog_grant_sub_space(
ad223e60
MT
111 struct xlog *log,
112 atomic64_t *head,
113 int bytes)
dd954c69 114{
d0eb2f38
DC
115 int64_t head_val = atomic64_read(head);
116 int64_t new, old;
a69ed03c 117
d0eb2f38
DC
118 do {
119 int cycle, space;
a69ed03c 120
d0eb2f38 121 xlog_crack_grant_head_val(head_val, &cycle, &space);
a69ed03c 122
d0eb2f38
DC
123 space -= bytes;
124 if (space < 0) {
125 space += log->l_logsize;
126 cycle--;
127 }
128
129 old = head_val;
130 new = xlog_assign_grant_head_val(cycle, space);
131 head_val = atomic64_cmpxchg(head, old, new);
132 } while (head_val != old);
dd954c69
CH
133}
134
135static void
663e496a 136xlog_grant_add_space(
ad223e60
MT
137 struct xlog *log,
138 atomic64_t *head,
139 int bytes)
dd954c69 140{
d0eb2f38
DC
141 int64_t head_val = atomic64_read(head);
142 int64_t new, old;
a69ed03c 143
d0eb2f38
DC
144 do {
145 int tmp;
146 int cycle, space;
a69ed03c 147
d0eb2f38 148 xlog_crack_grant_head_val(head_val, &cycle, &space);
a69ed03c 149
d0eb2f38
DC
150 tmp = log->l_logsize - space;
151 if (tmp > bytes)
152 space += bytes;
153 else {
154 space = bytes - tmp;
155 cycle++;
156 }
157
158 old = head_val;
159 new = xlog_assign_grant_head_val(cycle, space);
160 head_val = atomic64_cmpxchg(head, old, new);
161 } while (head_val != old);
dd954c69 162}
a69ed03c 163
c303c5b8
CH
164STATIC void
165xlog_grant_head_init(
166 struct xlog_grant_head *head)
167{
168 xlog_assign_grant_head(&head->grant, 1, 0);
169 INIT_LIST_HEAD(&head->waiters);
170 spin_lock_init(&head->lock);
171}
172
a79bf2d7
CH
173STATIC void
174xlog_grant_head_wake_all(
175 struct xlog_grant_head *head)
176{
177 struct xlog_ticket *tic;
178
179 spin_lock(&head->lock);
180 list_for_each_entry(tic, &head->waiters, t_queue)
181 wake_up_process(tic->t_task);
182 spin_unlock(&head->lock);
183}
184
e179840d
CH
185static inline int
186xlog_ticket_reservation(
ad223e60 187 struct xlog *log,
e179840d
CH
188 struct xlog_grant_head *head,
189 struct xlog_ticket *tic)
9f9c19ec 190{
e179840d
CH
191 if (head == &log->l_write_head) {
192 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
193 return tic->t_unit_res;
194 } else {
9f9c19ec 195 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
e179840d 196 return tic->t_unit_res * tic->t_cnt;
9f9c19ec 197 else
e179840d 198 return tic->t_unit_res;
9f9c19ec 199 }
9f9c19ec
CH
200}
201
202STATIC bool
e179840d 203xlog_grant_head_wake(
ad223e60 204 struct xlog *log,
e179840d 205 struct xlog_grant_head *head,
9f9c19ec
CH
206 int *free_bytes)
207{
208 struct xlog_ticket *tic;
209 int need_bytes;
7c107afb 210 bool woken_task = false;
9f9c19ec 211
e179840d 212 list_for_each_entry(tic, &head->waiters, t_queue) {
7c107afb
DC
213
214 /*
215 * There is a chance that the size of the CIL checkpoints in
216 * progress at the last AIL push target calculation resulted in
217 * limiting the target to the log head (l_last_sync_lsn) at the
218 * time. This may not reflect where the log head is now as the
219 * CIL checkpoints may have completed.
220 *
221 * Hence when we are woken here, it may be that the head of the
222 * log that has moved rather than the tail. As the tail didn't
223 * move, there still won't be space available for the
224 * reservation we require. However, if the AIL has already
225 * pushed to the target defined by the old log head location, we
226 * will hang here waiting for something else to update the AIL
227 * push target.
228 *
229 * Therefore, if there isn't space to wake the first waiter on
230 * the grant head, we need to push the AIL again to ensure the
231 * target reflects both the current log tail and log head
232 * position before we wait for the tail to move again.
233 */
234
e179840d 235 need_bytes = xlog_ticket_reservation(log, head, tic);
7c107afb
DC
236 if (*free_bytes < need_bytes) {
237 if (!woken_task)
238 xlog_grant_push_ail(log, need_bytes);
9f9c19ec 239 return false;
7c107afb 240 }
9f9c19ec 241
e179840d
CH
242 *free_bytes -= need_bytes;
243 trace_xfs_log_grant_wake_up(log, tic);
14a7235f 244 wake_up_process(tic->t_task);
7c107afb 245 woken_task = true;
9f9c19ec
CH
246 }
247
248 return true;
249}
250
251STATIC int
23ee3df3 252xlog_grant_head_wait(
ad223e60 253 struct xlog *log,
23ee3df3 254 struct xlog_grant_head *head,
9f9c19ec 255 struct xlog_ticket *tic,
a30b0367
DC
256 int need_bytes) __releases(&head->lock)
257 __acquires(&head->lock)
9f9c19ec 258{
23ee3df3 259 list_add_tail(&tic->t_queue, &head->waiters);
9f9c19ec
CH
260
261 do {
262 if (XLOG_FORCED_SHUTDOWN(log))
263 goto shutdown;
264 xlog_grant_push_ail(log, need_bytes);
265
14a7235f 266 __set_current_state(TASK_UNINTERRUPTIBLE);
23ee3df3 267 spin_unlock(&head->lock);
14a7235f 268
ff6d6af2 269 XFS_STATS_INC(log->l_mp, xs_sleep_logspace);
9f9c19ec 270
14a7235f
CH
271 trace_xfs_log_grant_sleep(log, tic);
272 schedule();
9f9c19ec
CH
273 trace_xfs_log_grant_wake(log, tic);
274
23ee3df3 275 spin_lock(&head->lock);
9f9c19ec
CH
276 if (XLOG_FORCED_SHUTDOWN(log))
277 goto shutdown;
23ee3df3 278 } while (xlog_space_left(log, &head->grant) < need_bytes);
9f9c19ec
CH
279
280 list_del_init(&tic->t_queue);
281 return 0;
282shutdown:
283 list_del_init(&tic->t_queue);
2451337d 284 return -EIO;
9f9c19ec
CH
285}
286
42ceedb3
CH
287/*
288 * Atomically get the log space required for a log ticket.
289 *
290 * Once a ticket gets put onto head->waiters, it will only return after the
291 * needed reservation is satisfied.
292 *
293 * This function is structured so that it has a lock free fast path. This is
294 * necessary because every new transaction reservation will come through this
295 * path. Hence any lock will be globally hot if we take it unconditionally on
296 * every pass.
297 *
298 * As tickets are only ever moved on and off head->waiters under head->lock, we
299 * only need to take that lock if we are going to add the ticket to the queue
300 * and sleep. We can avoid taking the lock if the ticket was never added to
301 * head->waiters because the t_queue list head will be empty and we hold the
302 * only reference to it so it can safely be checked unlocked.
303 */
304STATIC int
305xlog_grant_head_check(
ad223e60 306 struct xlog *log,
42ceedb3
CH
307 struct xlog_grant_head *head,
308 struct xlog_ticket *tic,
309 int *need_bytes)
310{
311 int free_bytes;
312 int error = 0;
313
314 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
315
316 /*
317 * If there are other waiters on the queue then give them a chance at
318 * logspace before us. Wake up the first waiters, if we do not wake
319 * up all the waiters then go to sleep waiting for more free space,
320 * otherwise try to get some space for this transaction.
321 */
322 *need_bytes = xlog_ticket_reservation(log, head, tic);
323 free_bytes = xlog_space_left(log, &head->grant);
324 if (!list_empty_careful(&head->waiters)) {
325 spin_lock(&head->lock);
326 if (!xlog_grant_head_wake(log, head, &free_bytes) ||
327 free_bytes < *need_bytes) {
328 error = xlog_grant_head_wait(log, head, tic,
329 *need_bytes);
330 }
331 spin_unlock(&head->lock);
332 } else if (free_bytes < *need_bytes) {
333 spin_lock(&head->lock);
334 error = xlog_grant_head_wait(log, head, tic, *need_bytes);
335 spin_unlock(&head->lock);
336 }
337
338 return error;
339}
340
0adba536
CH
341static void
342xlog_tic_reset_res(xlog_ticket_t *tic)
343{
344 tic->t_res_num = 0;
345 tic->t_res_arr_sum = 0;
346 tic->t_res_num_ophdrs = 0;
347}
348
349static void
350xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
351{
352 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
353 /* add to overflow and start again */
354 tic->t_res_o_flow += tic->t_res_arr_sum;
355 tic->t_res_num = 0;
356 tic->t_res_arr_sum = 0;
357 }
358
359 tic->t_res_arr[tic->t_res_num].r_len = len;
360 tic->t_res_arr[tic->t_res_num].r_type = type;
361 tic->t_res_arr_sum += len;
362 tic->t_res_num++;
363}
dd954c69 364
9006fb91
CH
365/*
366 * Replenish the byte reservation required by moving the grant write head.
367 */
368int
369xfs_log_regrant(
370 struct xfs_mount *mp,
371 struct xlog_ticket *tic)
372{
ad223e60 373 struct xlog *log = mp->m_log;
9006fb91
CH
374 int need_bytes;
375 int error = 0;
376
377 if (XLOG_FORCED_SHUTDOWN(log))
2451337d 378 return -EIO;
9006fb91 379
ff6d6af2 380 XFS_STATS_INC(mp, xs_try_logspace);
9006fb91
CH
381
382 /*
383 * This is a new transaction on the ticket, so we need to change the
384 * transaction ID so that the next transaction has a different TID in
385 * the log. Just add one to the existing tid so that we can see chains
386 * of rolling transactions in the log easily.
387 */
388 tic->t_tid++;
389
390 xlog_grant_push_ail(log, tic->t_unit_res);
391
392 tic->t_curr_res = tic->t_unit_res;
393 xlog_tic_reset_res(tic);
394
395 if (tic->t_cnt > 0)
396 return 0;
397
398 trace_xfs_log_regrant(log, tic);
399
400 error = xlog_grant_head_check(log, &log->l_write_head, tic,
401 &need_bytes);
402 if (error)
403 goto out_error;
404
405 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
406 trace_xfs_log_regrant_exit(log, tic);
407 xlog_verify_grant_tail(log);
408 return 0;
409
410out_error:
411 /*
412 * If we are failing, make sure the ticket doesn't have any current
413 * reservations. We don't want to add this back when the ticket/
414 * transaction gets cancelled.
415 */
416 tic->t_curr_res = 0;
417 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
418 return error;
419}
420
421/*
a0e336ba 422 * Reserve log space and return a ticket corresponding to the reservation.
9006fb91
CH
423 *
424 * Each reservation is going to reserve extra space for a log record header.
425 * When writes happen to the on-disk log, we don't subtract the length of the
426 * log record header from any reservation. By wasting space in each
427 * reservation, we prevent over allocation problems.
428 */
429int
430xfs_log_reserve(
431 struct xfs_mount *mp,
432 int unit_bytes,
433 int cnt,
434 struct xlog_ticket **ticp,
c8ce540d 435 uint8_t client,
710b1e2c 436 bool permanent)
9006fb91 437{
ad223e60 438 struct xlog *log = mp->m_log;
9006fb91
CH
439 struct xlog_ticket *tic;
440 int need_bytes;
441 int error = 0;
442
443 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
444
445 if (XLOG_FORCED_SHUTDOWN(log))
2451337d 446 return -EIO;
9006fb91 447
ff6d6af2 448 XFS_STATS_INC(mp, xs_try_logspace);
9006fb91
CH
449
450 ASSERT(*ticp == NULL);
707e0dda 451 tic = xlog_ticket_alloc(log, unit_bytes, cnt, client, permanent, 0);
9006fb91
CH
452 *ticp = tic;
453
437a255a
DC
454 xlog_grant_push_ail(log, tic->t_cnt ? tic->t_unit_res * tic->t_cnt
455 : tic->t_unit_res);
9006fb91
CH
456
457 trace_xfs_log_reserve(log, tic);
458
459 error = xlog_grant_head_check(log, &log->l_reserve_head, tic,
460 &need_bytes);
461 if (error)
462 goto out_error;
463
464 xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
465 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
466 trace_xfs_log_reserve_exit(log, tic);
467 xlog_verify_grant_tail(log);
468 return 0;
469
470out_error:
471 /*
472 * If we are failing, make sure the ticket doesn't have any current
473 * reservations. We don't want to add this back when the ticket/
474 * transaction gets cancelled.
475 */
476 tic->t_curr_res = 0;
477 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
478 return error;
479}
480
481
1da177e4
LT
482/*
483 * NOTES:
484 *
485 * 1. currblock field gets updated at startup and after in-core logs
486 * marked as with WANT_SYNC.
487 */
488
489/*
490 * This routine is called when a user of a log manager ticket is done with
491 * the reservation. If the ticket was ever used, then a commit record for
492 * the associated transaction is written out as a log operation header with
493 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
494 * a given ticket. If the ticket was one with a permanent reservation, then
495 * a few operations are done differently. Permanent reservation tickets by
496 * default don't release the reservation. They just commit the current
497 * transaction with the belief that the reservation is still needed. A flag
498 * must be passed in before permanent reservations are actually released.
499 * When these type of tickets are not released, they need to be set into
500 * the inited state again. By doing this, a start record will be written
501 * out when the next write occurs.
502 */
503xfs_lsn_t
35a8a72f
CH
504xfs_log_done(
505 struct xfs_mount *mp,
506 struct xlog_ticket *ticket,
507 struct xlog_in_core **iclog,
f78c3901 508 bool regrant)
1da177e4 509{
ad223e60 510 struct xlog *log = mp->m_log;
35a8a72f 511 xfs_lsn_t lsn = 0;
1da177e4 512
1da177e4
LT
513 if (XLOG_FORCED_SHUTDOWN(log) ||
514 /*
515 * If nothing was ever written, don't write out commit record.
516 * If we get an error, just continue and give back the log ticket.
517 */
518 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
55b66332 519 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
1da177e4 520 lsn = (xfs_lsn_t) -1;
f78c3901 521 regrant = false;
1da177e4
LT
522 }
523
524
f78c3901 525 if (!regrant) {
0b1b213f
CH
526 trace_xfs_log_done_nonperm(log, ticket);
527
1da177e4 528 /*
c41564b5 529 * Release ticket if not permanent reservation or a specific
1da177e4
LT
530 * request has been made to release a permanent reservation.
531 */
532 xlog_ungrant_log_space(log, ticket);
1da177e4 533 } else {
0b1b213f
CH
534 trace_xfs_log_done_perm(log, ticket);
535
1da177e4 536 xlog_regrant_reserve_log_space(log, ticket);
c6a7b0f8
LM
537 /* If this ticket was a permanent reservation and we aren't
538 * trying to release it, reset the inited flags; so next time
539 * we write, a start record will be written out.
540 */
1da177e4 541 ticket->t_flags |= XLOG_TIC_INITED;
c6a7b0f8 542 }
1da177e4 543
f78c3901 544 xfs_log_ticket_put(ticket);
1da177e4 545 return lsn;
35a8a72f 546}
1da177e4 547
df732b29
CH
548static bool
549__xlog_state_release_iclog(
550 struct xlog *log,
551 struct xlog_in_core *iclog)
552{
553 lockdep_assert_held(&log->l_icloglock);
554
555 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
556 /* update tail before writing to iclog */
557 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
558
559 iclog->ic_state = XLOG_STATE_SYNCING;
560 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
561 xlog_verify_tail_lsn(log, iclog, tail_lsn);
562 /* cycle incremented when incrementing curr_block */
563 return true;
564 }
565
566 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
567 return false;
568}
569
570/*
571 * Flush iclog to disk if this is the last reference to the given iclog and the
572 * it is in the WANT_SYNC state.
573 */
574static int
575xlog_state_release_iclog(
576 struct xlog *log,
577 struct xlog_in_core *iclog)
578{
579 lockdep_assert_held(&log->l_icloglock);
580
1858bb0b 581 if (iclog->ic_state == XLOG_STATE_IOERROR)
df732b29
CH
582 return -EIO;
583
584 if (atomic_dec_and_test(&iclog->ic_refcnt) &&
585 __xlog_state_release_iclog(log, iclog)) {
586 spin_unlock(&log->l_icloglock);
587 xlog_sync(log, iclog);
588 spin_lock(&log->l_icloglock);
589 }
590
591 return 0;
592}
593
f97a43e4 594void
35a8a72f 595xfs_log_release_iclog(
35a8a72f 596 struct xlog_in_core *iclog)
1da177e4 597{
f97a43e4 598 struct xlog *log = iclog->ic_log;
a582f32f 599 bool sync = false;
1da177e4 600
df732b29 601 if (atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock)) {
a582f32f
CH
602 if (iclog->ic_state != XLOG_STATE_IOERROR)
603 sync = __xlog_state_release_iclog(log, iclog);
df732b29 604 spin_unlock(&log->l_icloglock);
df732b29 605 }
a582f32f
CH
606
607 if (sync)
608 xlog_sync(log, iclog);
1da177e4
LT
609}
610
1da177e4
LT
611/*
612 * Mount a log filesystem
613 *
614 * mp - ubiquitous xfs mount point structure
615 * log_target - buftarg of on-disk log device
616 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
617 * num_bblocks - Number of BBSIZE blocks in on-disk log
618 *
619 * Return error or zero.
620 */
621int
249a8c11
DC
622xfs_log_mount(
623 xfs_mount_t *mp,
624 xfs_buftarg_t *log_target,
625 xfs_daddr_t blk_offset,
626 int num_bblks)
1da177e4 627{
9c92ee20 628 bool fatal = xfs_sb_version_hascrc(&mp->m_sb);
3e7b91cf
JL
629 int error = 0;
630 int min_logfsbs;
249a8c11 631
c99d609a
DC
632 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
633 xfs_notice(mp, "Mounting V%d Filesystem",
634 XFS_SB_VERSION_NUM(&mp->m_sb));
635 } else {
a0fa2b67 636 xfs_notice(mp,
c99d609a
DC
637"Mounting V%d filesystem in no-recovery mode. Filesystem will be inconsistent.",
638 XFS_SB_VERSION_NUM(&mp->m_sb));
bd186aa9 639 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
1da177e4
LT
640 }
641
642 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
a6cb767e 643 if (IS_ERR(mp->m_log)) {
2451337d 644 error = PTR_ERR(mp->m_log);
644c3567
DC
645 goto out;
646 }
1da177e4 647
3e7b91cf
JL
648 /*
649 * Validate the given log space and drop a critical message via syslog
650 * if the log size is too small that would lead to some unexpected
651 * situations in transaction log space reservation stage.
652 *
653 * Note: we can't just reject the mount if the validation fails. This
654 * would mean that people would have to downgrade their kernel just to
655 * remedy the situation as there is no way to grow the log (short of
656 * black magic surgery with xfs_db).
657 *
658 * We can, however, reject mounts for CRC format filesystems, as the
659 * mkfs binary being used to make the filesystem should never create a
660 * filesystem with a log that is too small.
661 */
662 min_logfsbs = xfs_log_calc_minimum_size(mp);
663
664 if (mp->m_sb.sb_logblocks < min_logfsbs) {
665 xfs_warn(mp,
666 "Log size %d blocks too small, minimum size is %d blocks",
667 mp->m_sb.sb_logblocks, min_logfsbs);
2451337d 668 error = -EINVAL;
3e7b91cf
JL
669 } else if (mp->m_sb.sb_logblocks > XFS_MAX_LOG_BLOCKS) {
670 xfs_warn(mp,
671 "Log size %d blocks too large, maximum size is %lld blocks",
672 mp->m_sb.sb_logblocks, XFS_MAX_LOG_BLOCKS);
2451337d 673 error = -EINVAL;
3e7b91cf
JL
674 } else if (XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks) > XFS_MAX_LOG_BYTES) {
675 xfs_warn(mp,
676 "log size %lld bytes too large, maximum size is %lld bytes",
677 XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks),
678 XFS_MAX_LOG_BYTES);
2451337d 679 error = -EINVAL;
9c92ee20
DW
680 } else if (mp->m_sb.sb_logsunit > 1 &&
681 mp->m_sb.sb_logsunit % mp->m_sb.sb_blocksize) {
682 xfs_warn(mp,
683 "log stripe unit %u bytes must be a multiple of block size",
684 mp->m_sb.sb_logsunit);
685 error = -EINVAL;
686 fatal = true;
3e7b91cf
JL
687 }
688 if (error) {
9c92ee20
DW
689 /*
690 * Log check errors are always fatal on v5; or whenever bad
691 * metadata leads to a crash.
692 */
693 if (fatal) {
3e7b91cf
JL
694 xfs_crit(mp, "AAIEEE! Log failed size checks. Abort!");
695 ASSERT(0);
696 goto out_free_log;
697 }
f41febd2 698 xfs_crit(mp, "Log size out of supported range.");
3e7b91cf 699 xfs_crit(mp,
f41febd2 700"Continuing onwards, but if log hangs are experienced then please report this message in the bug report.");
3e7b91cf
JL
701 }
702
249a8c11
DC
703 /*
704 * Initialize the AIL now we have a log.
705 */
249a8c11
DC
706 error = xfs_trans_ail_init(mp);
707 if (error) {
a0fa2b67 708 xfs_warn(mp, "AIL initialisation failed: error %d", error);
26430752 709 goto out_free_log;
249a8c11 710 }
a9c21c1b 711 mp->m_log->l_ailp = mp->m_ail;
249a8c11 712
1da177e4
LT
713 /*
714 * skip log recovery on a norecovery mount. pretend it all
715 * just worked.
716 */
717 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
249a8c11 718 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
1da177e4
LT
719
720 if (readonly)
bd186aa9 721 mp->m_flags &= ~XFS_MOUNT_RDONLY;
1da177e4 722
65be6054 723 error = xlog_recover(mp->m_log);
1da177e4
LT
724
725 if (readonly)
bd186aa9 726 mp->m_flags |= XFS_MOUNT_RDONLY;
1da177e4 727 if (error) {
a0fa2b67
DC
728 xfs_warn(mp, "log mount/recovery failed: error %d",
729 error);
f0b2efad 730 xlog_recover_cancel(mp->m_log);
26430752 731 goto out_destroy_ail;
1da177e4
LT
732 }
733 }
734
baff4e44
BF
735 error = xfs_sysfs_init(&mp->m_log->l_kobj, &xfs_log_ktype, &mp->m_kobj,
736 "log");
737 if (error)
738 goto out_destroy_ail;
739
1da177e4
LT
740 /* Normal transactions can now occur */
741 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
742
71e330b5
DC
743 /*
744 * Now the log has been fully initialised and we know were our
745 * space grant counters are, we can initialise the permanent ticket
746 * needed for delayed logging to work.
747 */
748 xlog_cil_init_post_recovery(mp->m_log);
749
1da177e4 750 return 0;
26430752
CH
751
752out_destroy_ail:
753 xfs_trans_ail_destroy(mp);
754out_free_log:
755 xlog_dealloc_log(mp->m_log);
644c3567 756out:
249a8c11 757 return error;
26430752 758}
1da177e4
LT
759
760/*
f661f1e0
DC
761 * Finish the recovery of the file system. This is separate from the
762 * xfs_log_mount() call, because it depends on the code in xfs_mountfs() to read
763 * in the root and real-time bitmap inodes between calling xfs_log_mount() and
764 * here.
1da177e4 765 *
f661f1e0
DC
766 * If we finish recovery successfully, start the background log work. If we are
767 * not doing recovery, then we have a RO filesystem and we don't need to start
768 * it.
1da177e4
LT
769 */
770int
f0b2efad
BF
771xfs_log_mount_finish(
772 struct xfs_mount *mp)
1da177e4 773{
f661f1e0 774 int error = 0;
6f4a1eef 775 bool readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
f1b92bbc 776 bool recovered = mp->m_log->l_flags & XLOG_RECOVERY_NEEDED;
1da177e4 777
f0b2efad 778 if (mp->m_flags & XFS_MOUNT_NORECOVERY) {
bd186aa9 779 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
f0b2efad 780 return 0;
6f4a1eef
ES
781 } else if (readonly) {
782 /* Allow unlinked processing to proceed */
783 mp->m_flags &= ~XFS_MOUNT_RDONLY;
1da177e4
LT
784 }
785
8204f8dd
DW
786 /*
787 * During the second phase of log recovery, we need iget and
788 * iput to behave like they do for an active filesystem.
789 * xfs_fs_drop_inode needs to be able to prevent the deletion
790 * of inodes before we're done replaying log items on those
791 * inodes. Turn it off immediately after recovery finishes
792 * so that we don't leak the quota inodes if subsequent mount
793 * activities fail.
799ea9e9
DW
794 *
795 * We let all inodes involved in redo item processing end up on
796 * the LRU instead of being evicted immediately so that if we do
797 * something to an unlinked inode, the irele won't cause
798 * premature truncation and freeing of the inode, which results
799 * in log recovery failure. We have to evict the unreferenced
1751e8a6 800 * lru inodes after clearing SB_ACTIVE because we don't
799ea9e9
DW
801 * otherwise clean up the lru if there's a subsequent failure in
802 * xfs_mountfs, which leads to us leaking the inodes if nothing
803 * else (e.g. quotacheck) references the inodes before the
804 * mount failure occurs.
8204f8dd 805 */
1751e8a6 806 mp->m_super->s_flags |= SB_ACTIVE;
f0b2efad
BF
807 error = xlog_recover_finish(mp->m_log);
808 if (!error)
809 xfs_log_work_queue(mp);
1751e8a6 810 mp->m_super->s_flags &= ~SB_ACTIVE;
799ea9e9 811 evict_inodes(mp->m_super);
f0b2efad 812
f1b92bbc
BF
813 /*
814 * Drain the buffer LRU after log recovery. This is required for v4
815 * filesystems to avoid leaving around buffers with NULL verifier ops,
816 * but we do it unconditionally to make sure we're always in a clean
817 * cache state after mount.
818 *
819 * Don't push in the error case because the AIL may have pending intents
820 * that aren't removed until recovery is cancelled.
821 */
822 if (!error && recovered) {
823 xfs_log_force(mp, XFS_LOG_SYNC);
824 xfs_ail_push_all_sync(mp->m_ail);
825 }
826 xfs_wait_buftarg(mp->m_ddev_targp);
827
6f4a1eef
ES
828 if (readonly)
829 mp->m_flags |= XFS_MOUNT_RDONLY;
830
f0b2efad
BF
831 return error;
832}
833
834/*
835 * The mount has failed. Cancel the recovery if it hasn't completed and destroy
836 * the log.
837 */
a7a9250e 838void
f0b2efad
BF
839xfs_log_mount_cancel(
840 struct xfs_mount *mp)
841{
a7a9250e 842 xlog_recover_cancel(mp->m_log);
f0b2efad 843 xfs_log_unmount(mp);
1da177e4
LT
844}
845
81e5b50a
CH
846/*
847 * Wait for the iclog to be written disk, or return an error if the log has been
848 * shut down.
849 */
850static int
851xlog_wait_on_iclog(
852 struct xlog_in_core *iclog)
853 __releases(iclog->ic_log->l_icloglock)
854{
855 struct xlog *log = iclog->ic_log;
856
857 if (!XLOG_FORCED_SHUTDOWN(log) &&
858 iclog->ic_state != XLOG_STATE_ACTIVE &&
859 iclog->ic_state != XLOG_STATE_DIRTY) {
860 XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
861 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
862 } else {
863 spin_unlock(&log->l_icloglock);
864 }
865
866 if (XLOG_FORCED_SHUTDOWN(log))
867 return -EIO;
868 return 0;
869}
870
1da177e4
LT
871/*
872 * Final log writes as part of unmount.
873 *
874 * Mark the filesystem clean as unmount happens. Note that during relocation
875 * this routine needs to be executed as part of source-bag while the
876 * deallocation must not be done until source-end.
877 */
878
53235f22
DW
879/* Actually write the unmount record to disk. */
880static void
881xfs_log_write_unmount_record(
882 struct xfs_mount *mp)
883{
884 /* the data section must be 32 bit size aligned */
885 struct xfs_unmount_log_format magic = {
886 .magic = XLOG_UNMOUNT_TYPE,
887 };
888 struct xfs_log_iovec reg = {
889 .i_addr = &magic,
890 .i_len = sizeof(magic),
891 .i_type = XLOG_REG_TYPE_UNMOUNT,
892 };
893 struct xfs_log_vec vec = {
894 .lv_niovecs = 1,
895 .lv_iovecp = &reg,
896 };
897 struct xlog *log = mp->m_log;
898 struct xlog_in_core *iclog;
899 struct xlog_ticket *tic = NULL;
900 xfs_lsn_t lsn;
f467cad9 901 uint flags = XLOG_UNMOUNT_TRANS;
53235f22
DW
902 int error;
903
904 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0);
905 if (error)
906 goto out_err;
907
f467cad9
DW
908 /*
909 * If we think the summary counters are bad, clear the unmount header
910 * flag in the unmount record so that the summary counters will be
911 * recalculated during log recovery at next mount. Refer to
912 * xlog_check_unmount_rec for more details.
913 */
39353ff6 914 if (XFS_TEST_ERROR(xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS), mp,
f467cad9
DW
915 XFS_ERRTAG_FORCE_SUMMARY_RECALC)) {
916 xfs_alert(mp, "%s: will fix summary counters at next mount",
917 __func__);
918 flags &= ~XLOG_UNMOUNT_TRANS;
919 }
920
53235f22
DW
921 /* remove inited flag, and account for space used */
922 tic->t_flags = 0;
923 tic->t_curr_res -= sizeof(magic);
7ec94921 924 error = xlog_write(log, &vec, tic, &lsn, NULL, flags, false);
53235f22
DW
925 /*
926 * At this point, we're umounting anyway, so there's no point in
927 * transitioning log state to IOERROR. Just continue...
928 */
929out_err:
930 if (error)
931 xfs_alert(mp, "%s: unmount record failed", __func__);
932
933 spin_lock(&log->l_icloglock);
934 iclog = log->l_iclog;
935 atomic_inc(&iclog->ic_refcnt);
69363999
CH
936 if (iclog->ic_state == XLOG_STATE_ACTIVE)
937 xlog_state_switch_iclogs(log, iclog, 0);
938 else
939 ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC ||
940 iclog->ic_state == XLOG_STATE_IOERROR);
53235f22 941 error = xlog_state_release_iclog(log, iclog);
81e5b50a 942 xlog_wait_on_iclog(iclog);
53235f22
DW
943
944 if (tic) {
945 trace_xfs_log_umount_write(log, tic);
946 xlog_ungrant_log_space(log, tic);
947 xfs_log_ticket_put(tic);
948 }
949}
950
13859c98
CH
951static void
952xfs_log_unmount_verify_iclog(
953 struct xlog *log)
954{
955 struct xlog_in_core *iclog = log->l_iclog;
956
957 do {
958 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
959 ASSERT(iclog->ic_offset == 0);
960 } while ((iclog = iclog->ic_next) != log->l_iclog);
961}
962
1da177e4
LT
963/*
964 * Unmount record used to have a string "Unmount filesystem--" in the
965 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
966 * We just write the magic number now since that particular field isn't
8e159e72 967 * currently architecture converted and "Unmount" is a bit foo.
1da177e4
LT
968 * As far as I know, there weren't any dependencies on the old behaviour.
969 */
550319e9 970static void
13859c98
CH
971xfs_log_unmount_write(
972 struct xfs_mount *mp)
1da177e4 973{
13859c98 974 struct xlog *log = mp->m_log;
1da177e4 975
1da177e4 976 /*
757a69ef 977 * Don't write out unmount record on norecovery mounts or ro devices.
1da177e4
LT
978 * Or, if we are doing a forced umount (typically because of IO errors).
979 */
757a69ef 980 if (mp->m_flags & XFS_MOUNT_NORECOVERY ||
2d15d2c0 981 xfs_readonly_buftarg(log->l_targ)) {
757a69ef 982 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
550319e9 983 return;
757a69ef 984 }
1da177e4 985
550319e9 986 xfs_log_force(mp, XFS_LOG_SYNC);
1da177e4 987
6178d104
CH
988 if (XLOG_FORCED_SHUTDOWN(log))
989 return;
13859c98 990 xfs_log_unmount_verify_iclog(log);
6178d104 991 xfs_log_write_unmount_record(mp);
550319e9 992}
1da177e4
LT
993
994/*
c75921a7 995 * Empty the log for unmount/freeze.
cf2931db
DC
996 *
997 * To do this, we first need to shut down the background log work so it is not
998 * trying to cover the log as we clean up. We then need to unpin all objects in
999 * the log so we can then flush them out. Once they have completed their IO and
1000 * run the callbacks removing themselves from the AIL, we can write the unmount
c75921a7 1001 * record.
1da177e4
LT
1002 */
1003void
c75921a7
DC
1004xfs_log_quiesce(
1005 struct xfs_mount *mp)
1da177e4 1006{
f661f1e0 1007 cancel_delayed_work_sync(&mp->m_log->l_work);
cf2931db
DC
1008 xfs_log_force(mp, XFS_LOG_SYNC);
1009
1010 /*
1011 * The superblock buffer is uncached and while xfs_ail_push_all_sync()
1012 * will push it, xfs_wait_buftarg() will not wait for it. Further,
1013 * xfs_buf_iowait() cannot be used because it was pushed with the
1014 * XBF_ASYNC flag set, so we need to use a lock/unlock pair to wait for
1015 * the IO to complete.
1016 */
1017 xfs_ail_push_all_sync(mp->m_ail);
1018 xfs_wait_buftarg(mp->m_ddev_targp);
1019 xfs_buf_lock(mp->m_sb_bp);
1020 xfs_buf_unlock(mp->m_sb_bp);
1021
1022 xfs_log_unmount_write(mp);
c75921a7
DC
1023}
1024
1025/*
1026 * Shut down and release the AIL and Log.
1027 *
1028 * During unmount, we need to ensure we flush all the dirty metadata objects
1029 * from the AIL so that the log is empty before we write the unmount record to
1030 * the log. Once this is done, we can tear down the AIL and the log.
1031 */
1032void
1033xfs_log_unmount(
1034 struct xfs_mount *mp)
1035{
1036 xfs_log_quiesce(mp);
cf2931db 1037
249a8c11 1038 xfs_trans_ail_destroy(mp);
baff4e44
BF
1039
1040 xfs_sysfs_del(&mp->m_log->l_kobj);
1041
c41564b5 1042 xlog_dealloc_log(mp->m_log);
1da177e4
LT
1043}
1044
43f5efc5
DC
1045void
1046xfs_log_item_init(
1047 struct xfs_mount *mp,
1048 struct xfs_log_item *item,
1049 int type,
272e42b2 1050 const struct xfs_item_ops *ops)
43f5efc5
DC
1051{
1052 item->li_mountp = mp;
1053 item->li_ailp = mp->m_ail;
1054 item->li_type = type;
1055 item->li_ops = ops;
71e330b5
DC
1056 item->li_lv = NULL;
1057
1058 INIT_LIST_HEAD(&item->li_ail);
1059 INIT_LIST_HEAD(&item->li_cil);
643c8c05 1060 INIT_LIST_HEAD(&item->li_bio_list);
e6631f85 1061 INIT_LIST_HEAD(&item->li_trans);
43f5efc5
DC
1062}
1063
09a423a3
CH
1064/*
1065 * Wake up processes waiting for log space after we have moved the log tail.
09a423a3 1066 */
1da177e4 1067void
09a423a3 1068xfs_log_space_wake(
cfb7cdca 1069 struct xfs_mount *mp)
1da177e4 1070{
ad223e60 1071 struct xlog *log = mp->m_log;
cfb7cdca 1072 int free_bytes;
1da177e4 1073
1da177e4
LT
1074 if (XLOG_FORCED_SHUTDOWN(log))
1075 return;
1da177e4 1076
28496968 1077 if (!list_empty_careful(&log->l_write_head.waiters)) {
09a423a3
CH
1078 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
1079
28496968
CH
1080 spin_lock(&log->l_write_head.lock);
1081 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
e179840d 1082 xlog_grant_head_wake(log, &log->l_write_head, &free_bytes);
28496968 1083 spin_unlock(&log->l_write_head.lock);
1da177e4 1084 }
10547941 1085
28496968 1086 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
09a423a3
CH
1087 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
1088
28496968
CH
1089 spin_lock(&log->l_reserve_head.lock);
1090 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
e179840d 1091 xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes);
28496968 1092 spin_unlock(&log->l_reserve_head.lock);
1da177e4 1093 }
3f16b985 1094}
1da177e4
LT
1095
1096/*
2c6e24ce
DC
1097 * Determine if we have a transaction that has gone to disk that needs to be
1098 * covered. To begin the transition to the idle state firstly the log needs to
1099 * be idle. That means the CIL, the AIL and the iclogs needs to be empty before
1100 * we start attempting to cover the log.
b6f8dd49 1101 *
2c6e24ce
DC
1102 * Only if we are then in a state where covering is needed, the caller is
1103 * informed that dummy transactions are required to move the log into the idle
1104 * state.
1105 *
1106 * If there are any items in the AIl or CIL, then we do not want to attempt to
1107 * cover the log as we may be in a situation where there isn't log space
1108 * available to run a dummy transaction and this can lead to deadlocks when the
1109 * tail of the log is pinned by an item that is modified in the CIL. Hence
1110 * there's no point in running a dummy transaction at this point because we
1111 * can't start trying to idle the log until both the CIL and AIL are empty.
1da177e4 1112 */
0d5a75e9 1113static int
1da177e4
LT
1114xfs_log_need_covered(xfs_mount_t *mp)
1115{
9a8d2fdb 1116 struct xlog *log = mp->m_log;
2c6e24ce 1117 int needed = 0;
1da177e4 1118
91ee575f 1119 if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
1da177e4
LT
1120 return 0;
1121
2c6e24ce
DC
1122 if (!xlog_cil_empty(log))
1123 return 0;
1124
b22cd72c 1125 spin_lock(&log->l_icloglock);
b6f8dd49
DC
1126 switch (log->l_covered_state) {
1127 case XLOG_STATE_COVER_DONE:
1128 case XLOG_STATE_COVER_DONE2:
1129 case XLOG_STATE_COVER_IDLE:
1130 break;
1131 case XLOG_STATE_COVER_NEED:
1132 case XLOG_STATE_COVER_NEED2:
2c6e24ce
DC
1133 if (xfs_ail_min_lsn(log->l_ailp))
1134 break;
1135 if (!xlog_iclogs_empty(log))
1136 break;
1137
1138 needed = 1;
1139 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
1140 log->l_covered_state = XLOG_STATE_COVER_DONE;
1141 else
1142 log->l_covered_state = XLOG_STATE_COVER_DONE2;
1143 break;
b6f8dd49 1144 default:
1da177e4 1145 needed = 1;
b6f8dd49 1146 break;
1da177e4 1147 }
b22cd72c 1148 spin_unlock(&log->l_icloglock);
014c2544 1149 return needed;
1da177e4
LT
1150}
1151
09a423a3 1152/*
1da177e4
LT
1153 * We may be holding the log iclog lock upon entering this routine.
1154 */
1155xfs_lsn_t
1c304625 1156xlog_assign_tail_lsn_locked(
1c3cb9ec 1157 struct xfs_mount *mp)
1da177e4 1158{
ad223e60 1159 struct xlog *log = mp->m_log;
1c304625
CH
1160 struct xfs_log_item *lip;
1161 xfs_lsn_t tail_lsn;
1162
57e80956 1163 assert_spin_locked(&mp->m_ail->ail_lock);
1da177e4 1164
09a423a3
CH
1165 /*
1166 * To make sure we always have a valid LSN for the log tail we keep
1167 * track of the last LSN which was committed in log->l_last_sync_lsn,
1c304625 1168 * and use that when the AIL was empty.
09a423a3 1169 */
1c304625
CH
1170 lip = xfs_ail_min(mp->m_ail);
1171 if (lip)
1172 tail_lsn = lip->li_lsn;
1173 else
84f3c683 1174 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
750b9c90 1175 trace_xfs_log_assign_tail_lsn(log, tail_lsn);
1c3cb9ec 1176 atomic64_set(&log->l_tail_lsn, tail_lsn);
1da177e4 1177 return tail_lsn;
1c3cb9ec 1178}
1da177e4 1179
1c304625
CH
1180xfs_lsn_t
1181xlog_assign_tail_lsn(
1182 struct xfs_mount *mp)
1183{
1184 xfs_lsn_t tail_lsn;
1185
57e80956 1186 spin_lock(&mp->m_ail->ail_lock);
1c304625 1187 tail_lsn = xlog_assign_tail_lsn_locked(mp);
57e80956 1188 spin_unlock(&mp->m_ail->ail_lock);
1c304625
CH
1189
1190 return tail_lsn;
1191}
1192
1da177e4
LT
1193/*
1194 * Return the space in the log between the tail and the head. The head
1195 * is passed in the cycle/bytes formal parms. In the special case where
1196 * the reserve head has wrapped passed the tail, this calculation is no
1197 * longer valid. In this case, just return 0 which means there is no space
1198 * in the log. This works for all places where this function is called
1199 * with the reserve head. Of course, if the write head were to ever
1200 * wrap the tail, we should blow up. Rather than catch this case here,
1201 * we depend on other ASSERTions in other parts of the code. XXXmiken
1202 *
1203 * This code also handles the case where the reservation head is behind
1204 * the tail. The details of this case are described below, but the end
1205 * result is that we return the size of the log as the amount of space left.
1206 */
a8272ce0 1207STATIC int
a69ed03c 1208xlog_space_left(
ad223e60 1209 struct xlog *log,
c8a09ff8 1210 atomic64_t *head)
1da177e4 1211{
a69ed03c
DC
1212 int free_bytes;
1213 int tail_bytes;
1214 int tail_cycle;
1215 int head_cycle;
1216 int head_bytes;
1da177e4 1217
a69ed03c 1218 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
1c3cb9ec
DC
1219 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
1220 tail_bytes = BBTOB(tail_bytes);
a69ed03c
DC
1221 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
1222 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
1223 else if (tail_cycle + 1 < head_cycle)
1da177e4 1224 return 0;
a69ed03c
DC
1225 else if (tail_cycle < head_cycle) {
1226 ASSERT(tail_cycle == (head_cycle - 1));
1227 free_bytes = tail_bytes - head_bytes;
1da177e4
LT
1228 } else {
1229 /*
1230 * The reservation head is behind the tail.
1231 * In this case we just want to return the size of the
1232 * log as the amount of space left.
1233 */
f41febd2 1234 xfs_alert(log->l_mp, "xlog_space_left: head behind tail");
a0fa2b67 1235 xfs_alert(log->l_mp,
f41febd2
JP
1236 " tail_cycle = %d, tail_bytes = %d",
1237 tail_cycle, tail_bytes);
1238 xfs_alert(log->l_mp,
1239 " GH cycle = %d, GH bytes = %d",
1240 head_cycle, head_bytes);
1da177e4
LT
1241 ASSERT(0);
1242 free_bytes = log->l_logsize;
1243 }
1244 return free_bytes;
a69ed03c 1245}
1da177e4
LT
1246
1247
0d5a75e9 1248static void
79b54d9b
CH
1249xlog_ioend_work(
1250 struct work_struct *work)
1da177e4 1251{
79b54d9b
CH
1252 struct xlog_in_core *iclog =
1253 container_of(work, struct xlog_in_core, ic_end_io_work);
1254 struct xlog *log = iclog->ic_log;
79b54d9b 1255 int error;
1da177e4 1256
79b54d9b 1257 error = blk_status_to_errno(iclog->ic_bio.bi_status);
366fc4b8
CH
1258#ifdef DEBUG
1259 /* treat writes with injected CRC errors as failed */
1260 if (iclog->ic_fail_crc)
79b54d9b 1261 error = -EIO;
366fc4b8
CH
1262#endif
1263
1da177e4 1264 /*
366fc4b8 1265 * Race to shutdown the filesystem if we see an error.
1da177e4 1266 */
79b54d9b
CH
1267 if (XFS_TEST_ERROR(error, log->l_mp, XFS_ERRTAG_IODONE_IOERR)) {
1268 xfs_alert(log->l_mp, "log I/O error %d", error);
1269 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
1da177e4 1270 }
3db296f3 1271
12e6a0f4 1272 xlog_state_done_syncing(iclog);
79b54d9b 1273 bio_uninit(&iclog->ic_bio);
9c23eccc 1274
3db296f3 1275 /*
79b54d9b
CH
1276 * Drop the lock to signal that we are done. Nothing references the
1277 * iclog after this, so an unmount waiting on this lock can now tear it
1278 * down safely. As such, it is unsafe to reference the iclog after the
1279 * unlock as we could race with it being freed.
3db296f3 1280 */
79b54d9b 1281 up(&iclog->ic_sema);
c3f8fc73 1282}
1da177e4 1283
1da177e4
LT
1284/*
1285 * Return size of each in-core log record buffer.
1286 *
9da096fd 1287 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
1da177e4
LT
1288 *
1289 * If the filesystem blocksize is too large, we may need to choose a
1290 * larger size since the directory code currently logs entire blocks.
1291 */
1da177e4 1292STATIC void
9a8d2fdb
MT
1293xlog_get_iclog_buffer_size(
1294 struct xfs_mount *mp,
1295 struct xlog *log)
1da177e4 1296{
1cb51258 1297 if (mp->m_logbufs <= 0)
4f62282a
CH
1298 mp->m_logbufs = XLOG_MAX_ICLOGS;
1299 if (mp->m_logbsize <= 0)
1300 mp->m_logbsize = XLOG_BIG_RECORD_BSIZE;
1301
1302 log->l_iclog_bufs = mp->m_logbufs;
1303 log->l_iclog_size = mp->m_logbsize;
1da177e4
LT
1304
1305 /*
4f62282a 1306 * # headers = size / 32k - one header holds cycles from 32k of data.
1da177e4 1307 */
4f62282a
CH
1308 log->l_iclog_heads =
1309 DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE);
1310 log->l_iclog_hsize = log->l_iclog_heads << BBSHIFT;
1311}
1da177e4 1312
f661f1e0
DC
1313void
1314xfs_log_work_queue(
1315 struct xfs_mount *mp)
1316{
696a5620 1317 queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work,
f661f1e0
DC
1318 msecs_to_jiffies(xfs_syncd_centisecs * 10));
1319}
1320
1321/*
1322 * Every sync period we need to unpin all items in the AIL and push them to
1323 * disk. If there is nothing dirty, then we might need to cover the log to
1324 * indicate that the filesystem is idle.
1325 */
0d5a75e9 1326static void
f661f1e0
DC
1327xfs_log_worker(
1328 struct work_struct *work)
1329{
1330 struct xlog *log = container_of(to_delayed_work(work),
1331 struct xlog, l_work);
1332 struct xfs_mount *mp = log->l_mp;
1333
1334 /* dgc: errors ignored - not fatal and nowhere to report them */
61e63ecb
DC
1335 if (xfs_log_need_covered(mp)) {
1336 /*
1337 * Dump a transaction into the log that contains no real change.
1338 * This is needed to stamp the current tail LSN into the log
1339 * during the covering operation.
1340 *
1341 * We cannot use an inode here for this - that will push dirty
1342 * state back up into the VFS and then periodic inode flushing
1343 * will prevent log covering from making progress. Hence we
1344 * synchronously log the superblock instead to ensure the
1345 * superblock is immediately unpinned and can be written back.
1346 */
1347 xfs_sync_sb(mp, true);
1348 } else
f661f1e0
DC
1349 xfs_log_force(mp, 0);
1350
1351 /* start pushing all the metadata that is currently dirty */
1352 xfs_ail_push_all(mp->m_ail);
1353
1354 /* queue us up again */
1355 xfs_log_work_queue(mp);
1356}
1357
1da177e4
LT
1358/*
1359 * This routine initializes some of the log structure for a given mount point.
1360 * Its primary purpose is to fill in enough, so recovery can occur. However,
1361 * some other stuff may be filled in too.
1362 */
9a8d2fdb
MT
1363STATIC struct xlog *
1364xlog_alloc_log(
1365 struct xfs_mount *mp,
1366 struct xfs_buftarg *log_target,
1367 xfs_daddr_t blk_offset,
1368 int num_bblks)
1da177e4 1369{
9a8d2fdb 1370 struct xlog *log;
1da177e4
LT
1371 xlog_rec_header_t *head;
1372 xlog_in_core_t **iclogp;
1373 xlog_in_core_t *iclog, *prev_iclog=NULL;
1da177e4 1374 int i;
2451337d 1375 int error = -ENOMEM;
69ce58f0 1376 uint log2_size = 0;
1da177e4 1377
9a8d2fdb 1378 log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL);
a6cb767e 1379 if (!log) {
a0fa2b67 1380 xfs_warn(mp, "Log allocation failed: No memory!");
a6cb767e
DC
1381 goto out;
1382 }
1da177e4
LT
1383
1384 log->l_mp = mp;
1385 log->l_targ = log_target;
1386 log->l_logsize = BBTOB(num_bblks);
1387 log->l_logBBstart = blk_offset;
1388 log->l_logBBsize = num_bblks;
1389 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1390 log->l_flags |= XLOG_ACTIVE_RECOVERY;
f661f1e0 1391 INIT_DELAYED_WORK(&log->l_work, xfs_log_worker);
1da177e4
LT
1392
1393 log->l_prev_block = -1;
1da177e4 1394 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1c3cb9ec
DC
1395 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1396 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
1da177e4 1397 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
c303c5b8
CH
1398
1399 xlog_grant_head_init(&log->l_reserve_head);
1400 xlog_grant_head_init(&log->l_write_head);
1da177e4 1401
2451337d 1402 error = -EFSCORRUPTED;
62118709 1403 if (xfs_sb_version_hassector(&mp->m_sb)) {
69ce58f0
AE
1404 log2_size = mp->m_sb.sb_logsectlog;
1405 if (log2_size < BBSHIFT) {
a0fa2b67
DC
1406 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1407 log2_size, BBSHIFT);
a6cb767e
DC
1408 goto out_free_log;
1409 }
1410
69ce58f0
AE
1411 log2_size -= BBSHIFT;
1412 if (log2_size > mp->m_sectbb_log) {
a0fa2b67
DC
1413 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1414 log2_size, mp->m_sectbb_log);
a6cb767e
DC
1415 goto out_free_log;
1416 }
69ce58f0
AE
1417
1418 /* for larger sector sizes, must have v2 or external log */
1419 if (log2_size && log->l_logBBstart > 0 &&
1420 !xfs_sb_version_haslogv2(&mp->m_sb)) {
a0fa2b67
DC
1421 xfs_warn(mp,
1422 "log sector size (0x%x) invalid for configuration.",
1423 log2_size);
a6cb767e
DC
1424 goto out_free_log;
1425 }
1da177e4 1426 }
69ce58f0 1427 log->l_sectBBsize = 1 << log2_size;
1da177e4
LT
1428
1429 xlog_get_iclog_buffer_size(mp, log);
1430
007c61c6 1431 spin_lock_init(&log->l_icloglock);
eb40a875 1432 init_waitqueue_head(&log->l_flush_wait);
1da177e4 1433
1da177e4
LT
1434 iclogp = &log->l_iclog;
1435 /*
1436 * The amount of memory to allocate for the iclog structure is
1437 * rather funky due to the way the structure is defined. It is
1438 * done this way so that we can use different sizes for machines
1439 * with different amounts of memory. See the definition of
1440 * xlog_in_core_t in xfs_log_priv.h for details.
1441 */
1da177e4 1442 ASSERT(log->l_iclog_size >= 4096);
79b54d9b 1443 for (i = 0; i < log->l_iclog_bufs; i++) {
f8f9ee47 1444 int align_mask = xfs_buftarg_dma_alignment(mp->m_logdev_targp);
89b171ac
CH
1445 size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) *
1446 sizeof(struct bio_vec);
79b54d9b
CH
1447
1448 iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL);
1449 if (!iclog)
644c3567
DC
1450 goto out_free_iclog;
1451
79b54d9b 1452 *iclogp = iclog;
1da177e4
LT
1453 iclog->ic_prev = prev_iclog;
1454 prev_iclog = iclog;
1fa40b01 1455
f8f9ee47 1456 iclog->ic_data = kmem_alloc_io(log->l_iclog_size, align_mask,
3219e8cf 1457 KM_MAYFAIL | KM_ZERO);
79b54d9b 1458 if (!iclog->ic_data)
644c3567 1459 goto out_free_iclog;
4679b2d3 1460#ifdef DEBUG
5809d5e0 1461 log->l_iclog_bak[i] = &iclog->ic_header;
4679b2d3 1462#endif
1da177e4
LT
1463 head = &iclog->ic_header;
1464 memset(head, 0, sizeof(xlog_rec_header_t));
b53e675d
CH
1465 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1466 head->h_version = cpu_to_be32(
62118709 1467 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
b53e675d 1468 head->h_size = cpu_to_be32(log->l_iclog_size);
1da177e4 1469 /* new fields */
b53e675d 1470 head->h_fmt = cpu_to_be32(XLOG_FMT);
1da177e4
LT
1471 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1472
79b54d9b 1473 iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize;
1da177e4
LT
1474 iclog->ic_state = XLOG_STATE_ACTIVE;
1475 iclog->ic_log = log;
114d23aa
DC
1476 atomic_set(&iclog->ic_refcnt, 0);
1477 spin_lock_init(&iclog->ic_callback_lock);
89ae379d 1478 INIT_LIST_HEAD(&iclog->ic_callbacks);
b28708d6 1479 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
1da177e4 1480
eb40a875
DC
1481 init_waitqueue_head(&iclog->ic_force_wait);
1482 init_waitqueue_head(&iclog->ic_write_wait);
79b54d9b
CH
1483 INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work);
1484 sema_init(&iclog->ic_sema, 1);
1da177e4
LT
1485
1486 iclogp = &iclog->ic_next;
1487 }
1488 *iclogp = log->l_iclog; /* complete ring */
1489 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1490
1058d0f5
CH
1491 log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s",
1492 WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_HIGHPRI, 0,
e1d3d218 1493 mp->m_super->s_id);
1058d0f5
CH
1494 if (!log->l_ioend_workqueue)
1495 goto out_free_iclog;
1496
71e330b5
DC
1497 error = xlog_cil_init(log);
1498 if (error)
1058d0f5 1499 goto out_destroy_workqueue;
1da177e4 1500 return log;
644c3567 1501
1058d0f5
CH
1502out_destroy_workqueue:
1503 destroy_workqueue(log->l_ioend_workqueue);
644c3567
DC
1504out_free_iclog:
1505 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1506 prev_iclog = iclog->ic_next;
79b54d9b 1507 kmem_free(iclog->ic_data);
644c3567 1508 kmem_free(iclog);
798a9cad
BF
1509 if (prev_iclog == log->l_iclog)
1510 break;
644c3567 1511 }
644c3567
DC
1512out_free_log:
1513 kmem_free(log);
a6cb767e 1514out:
2451337d 1515 return ERR_PTR(error);
1da177e4
LT
1516} /* xlog_alloc_log */
1517
1518
1519/*
1520 * Write out the commit record of a transaction associated with the given
1521 * ticket. Return the lsn of the commit record.
1522 */
1523STATIC int
55b66332 1524xlog_commit_record(
ad223e60 1525 struct xlog *log,
55b66332
DC
1526 struct xlog_ticket *ticket,
1527 struct xlog_in_core **iclog,
1528 xfs_lsn_t *commitlsnp)
1da177e4 1529{
55b66332
DC
1530 struct xfs_mount *mp = log->l_mp;
1531 int error;
1532 struct xfs_log_iovec reg = {
1533 .i_addr = NULL,
1534 .i_len = 0,
1535 .i_type = XLOG_REG_TYPE_COMMIT,
1536 };
1537 struct xfs_log_vec vec = {
1538 .lv_niovecs = 1,
1539 .lv_iovecp = &reg,
1540 };
1da177e4
LT
1541
1542 ASSERT_ALWAYS(iclog);
55b66332 1543 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
7ec94921 1544 XLOG_COMMIT_TRANS, false);
55b66332 1545 if (error)
7d04a335 1546 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
014c2544 1547 return error;
55b66332 1548}
1da177e4
LT
1549
1550/*
1551 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1552 * log space. This code pushes on the lsn which would supposedly free up
1553 * the 25% which we want to leave free. We may need to adopt a policy which
1554 * pushes on an lsn which is further along in the log once we reach the high
1555 * water mark. In this manner, we would be creating a low water mark.
1556 */
a8272ce0 1557STATIC void
2ced19cb 1558xlog_grant_push_ail(
ad223e60 1559 struct xlog *log,
2ced19cb 1560 int need_bytes)
1da177e4 1561{
2ced19cb 1562 xfs_lsn_t threshold_lsn = 0;
84f3c683 1563 xfs_lsn_t last_sync_lsn;
2ced19cb
DC
1564 int free_blocks;
1565 int free_bytes;
1566 int threshold_block;
1567 int threshold_cycle;
1568 int free_threshold;
1569
1570 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1571
28496968 1572 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
2ced19cb
DC
1573 free_blocks = BTOBBT(free_bytes);
1574
1575 /*
1576 * Set the threshold for the minimum number of free blocks in the
1577 * log to the maximum of what the caller needs, one quarter of the
1578 * log, and 256 blocks.
1579 */
1580 free_threshold = BTOBB(need_bytes);
9bb54cb5
DC
1581 free_threshold = max(free_threshold, (log->l_logBBsize >> 2));
1582 free_threshold = max(free_threshold, 256);
2ced19cb
DC
1583 if (free_blocks >= free_threshold)
1584 return;
1585
1c3cb9ec
DC
1586 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1587 &threshold_block);
1588 threshold_block += free_threshold;
1da177e4 1589 if (threshold_block >= log->l_logBBsize) {
2ced19cb
DC
1590 threshold_block -= log->l_logBBsize;
1591 threshold_cycle += 1;
1da177e4 1592 }
2ced19cb
DC
1593 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1594 threshold_block);
1595 /*
1596 * Don't pass in an lsn greater than the lsn of the last
84f3c683
DC
1597 * log record known to be on disk. Use a snapshot of the last sync lsn
1598 * so that it doesn't change between the compare and the set.
1da177e4 1599 */
84f3c683
DC
1600 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1601 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1602 threshold_lsn = last_sync_lsn;
2ced19cb
DC
1603
1604 /*
1605 * Get the transaction layer to kick the dirty buffers out to
1606 * disk asynchronously. No point in trying to do this if
1607 * the filesystem is shutting down.
1608 */
1609 if (!XLOG_FORCED_SHUTDOWN(log))
fd074841 1610 xfs_ail_push(log->l_ailp, threshold_lsn);
2ced19cb 1611}
1da177e4 1612
0e446be4
CH
1613/*
1614 * Stamp cycle number in every block
1615 */
1616STATIC void
1617xlog_pack_data(
1618 struct xlog *log,
1619 struct xlog_in_core *iclog,
1620 int roundoff)
1621{
1622 int i, j, k;
1623 int size = iclog->ic_offset + roundoff;
1624 __be32 cycle_lsn;
b2a922cd 1625 char *dp;
0e446be4
CH
1626
1627 cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
1628
1629 dp = iclog->ic_datap;
1630 for (i = 0; i < BTOBB(size); i++) {
1631 if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE))
1632 break;
1633 iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
1634 *(__be32 *)dp = cycle_lsn;
1635 dp += BBSIZE;
1636 }
1637
1638 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1639 xlog_in_core_2_t *xhdr = iclog->ic_data;
1640
1641 for ( ; i < BTOBB(size); i++) {
1642 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1643 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1644 xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
1645 *(__be32 *)dp = cycle_lsn;
1646 dp += BBSIZE;
1647 }
1648
1649 for (i = 1; i < log->l_iclog_heads; i++)
1650 xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
1651 }
1652}
1653
1654/*
1655 * Calculate the checksum for a log buffer.
1656 *
1657 * This is a little more complicated than it should be because the various
1658 * headers and the actual data are non-contiguous.
1659 */
f9668a09 1660__le32
0e446be4
CH
1661xlog_cksum(
1662 struct xlog *log,
1663 struct xlog_rec_header *rhead,
1664 char *dp,
1665 int size)
1666{
c8ce540d 1667 uint32_t crc;
0e446be4
CH
1668
1669 /* first generate the crc for the record header ... */
cae028df 1670 crc = xfs_start_cksum_update((char *)rhead,
0e446be4
CH
1671 sizeof(struct xlog_rec_header),
1672 offsetof(struct xlog_rec_header, h_crc));
1673
1674 /* ... then for additional cycle data for v2 logs ... */
1675 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1676 union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead;
1677 int i;
a3f20014 1678 int xheads;
0e446be4 1679
a3f20014
BF
1680 xheads = size / XLOG_HEADER_CYCLE_SIZE;
1681 if (size % XLOG_HEADER_CYCLE_SIZE)
1682 xheads++;
0e446be4 1683
a3f20014 1684 for (i = 1; i < xheads; i++) {
0e446be4
CH
1685 crc = crc32c(crc, &xhdr[i].hic_xheader,
1686 sizeof(struct xlog_rec_ext_header));
1687 }
1688 }
1689
1690 /* ... and finally for the payload */
1691 crc = crc32c(crc, dp, size);
1692
1693 return xfs_end_cksum(crc);
1694}
1695
79b54d9b
CH
1696static void
1697xlog_bio_end_io(
1698 struct bio *bio)
1699{
1700 struct xlog_in_core *iclog = bio->bi_private;
1701
1058d0f5 1702 queue_work(iclog->ic_log->l_ioend_workqueue,
79b54d9b
CH
1703 &iclog->ic_end_io_work);
1704}
1705
842a42d1 1706static int
79b54d9b
CH
1707xlog_map_iclog_data(
1708 struct bio *bio,
1709 void *data,
1710 size_t count)
1711{
1712 do {
1713 struct page *page = kmem_to_page(data);
1714 unsigned int off = offset_in_page(data);
1715 size_t len = min_t(size_t, count, PAGE_SIZE - off);
1716
842a42d1
BF
1717 if (bio_add_page(bio, page, len, off) != len)
1718 return -EIO;
79b54d9b
CH
1719
1720 data += len;
1721 count -= len;
1722 } while (count);
842a42d1
BF
1723
1724 return 0;
79b54d9b
CH
1725}
1726
94860a30
CH
1727STATIC void
1728xlog_write_iclog(
1729 struct xlog *log,
1730 struct xlog_in_core *iclog,
94860a30 1731 uint64_t bno,
79b54d9b 1732 unsigned int count,
94860a30 1733 bool need_flush)
873ff550 1734{
94860a30 1735 ASSERT(bno < log->l_logBBsize);
94860a30
CH
1736
1737 /*
1738 * We lock the iclogbufs here so that we can serialise against I/O
1739 * completion during unmount. We might be processing a shutdown
1740 * triggered during unmount, and that can occur asynchronously to the
1741 * unmount thread, and hence we need to ensure that completes before
1742 * tearing down the iclogbufs. Hence we need to hold the buffer lock
1743 * across the log IO to archieve that.
1744 */
79b54d9b 1745 down(&iclog->ic_sema);
1858bb0b 1746 if (unlikely(iclog->ic_state == XLOG_STATE_IOERROR)) {
873ff550
CH
1747 /*
1748 * It would seem logical to return EIO here, but we rely on
1749 * the log state machine to propagate I/O errors instead of
79b54d9b
CH
1750 * doing it here. We kick of the state machine and unlock
1751 * the buffer manually, the code needs to be kept in sync
1752 * with the I/O completion path.
873ff550 1753 */
12e6a0f4 1754 xlog_state_done_syncing(iclog);
79b54d9b 1755 up(&iclog->ic_sema);
94860a30 1756 return;
873ff550
CH
1757 }
1758
79b54d9b
CH
1759 bio_init(&iclog->ic_bio, iclog->ic_bvec, howmany(count, PAGE_SIZE));
1760 bio_set_dev(&iclog->ic_bio, log->l_targ->bt_bdev);
1761 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno;
1762 iclog->ic_bio.bi_end_io = xlog_bio_end_io;
1763 iclog->ic_bio.bi_private = iclog;
1764 iclog->ic_bio.bi_opf = REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_FUA;
1765 if (need_flush)
1766 iclog->ic_bio.bi_opf |= REQ_PREFLUSH;
1767
842a42d1
BF
1768 if (xlog_map_iclog_data(&iclog->ic_bio, iclog->ic_data, count)) {
1769 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
1770 return;
1771 }
79b54d9b 1772 if (is_vmalloc_addr(iclog->ic_data))
2c68a1df 1773 flush_kernel_vmap_range(iclog->ic_data, count);
79b54d9b
CH
1774
1775 /*
1776 * If this log buffer would straddle the end of the log we will have
1777 * to split it up into two bios, so that we can continue at the start.
1778 */
1779 if (bno + BTOBB(count) > log->l_logBBsize) {
1780 struct bio *split;
1781
1782 split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno,
1783 GFP_NOIO, &fs_bio_set);
1784 bio_chain(split, &iclog->ic_bio);
1785 submit_bio(split);
1786
1787 /* restart at logical offset zero for the remainder */
1788 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart;
1789 }
1790
1791 submit_bio(&iclog->ic_bio);
873ff550 1792}
1da177e4 1793
56933848
CH
1794/*
1795 * We need to bump cycle number for the part of the iclog that is
1796 * written to the start of the log. Watch out for the header magic
1797 * number case, though.
1798 */
79b54d9b 1799static void
56933848
CH
1800xlog_split_iclog(
1801 struct xlog *log,
1802 void *data,
1803 uint64_t bno,
1804 unsigned int count)
1805{
1806 unsigned int split_offset = BBTOB(log->l_logBBsize - bno);
1807 unsigned int i;
1808
1809 for (i = split_offset; i < count; i += BBSIZE) {
1810 uint32_t cycle = get_unaligned_be32(data + i);
1811
1812 if (++cycle == XLOG_HEADER_MAGIC_NUM)
1813 cycle++;
1814 put_unaligned_be32(cycle, data + i);
1815 }
56933848
CH
1816}
1817
db0a6faf
CH
1818static int
1819xlog_calc_iclog_size(
1820 struct xlog *log,
1821 struct xlog_in_core *iclog,
1822 uint32_t *roundoff)
1823{
1824 uint32_t count_init, count;
1825 bool use_lsunit;
1826
1827 use_lsunit = xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
1828 log->l_mp->m_sb.sb_logsunit > 1;
1829
1830 /* Add for LR header */
1831 count_init = log->l_iclog_hsize + iclog->ic_offset;
1832
1833 /* Round out the log write size */
1834 if (use_lsunit) {
1835 /* we have a v2 stripe unit to use */
1836 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1837 } else {
1838 count = BBTOB(BTOBB(count_init));
1839 }
1840
1841 ASSERT(count >= count_init);
1842 *roundoff = count - count_init;
1843
1844 if (use_lsunit)
1845 ASSERT(*roundoff < log->l_mp->m_sb.sb_logsunit);
1846 else
1847 ASSERT(*roundoff < BBTOB(1));
1848 return count;
1849}
1850
1da177e4
LT
1851/*
1852 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1853 * fashion. Previously, we should have moved the current iclog
1854 * ptr in the log to point to the next available iclog. This allows further
1855 * write to continue while this code syncs out an iclog ready to go.
1856 * Before an in-core log can be written out, the data section must be scanned
1857 * to save away the 1st word of each BBSIZE block into the header. We replace
1858 * it with the current cycle count. Each BBSIZE block is tagged with the
1859 * cycle count because there in an implicit assumption that drives will
1860 * guarantee that entire 512 byte blocks get written at once. In other words,
1861 * we can't have part of a 512 byte block written and part not written. By
1862 * tagging each block, we will know which blocks are valid when recovering
1863 * after an unclean shutdown.
1864 *
1865 * This routine is single threaded on the iclog. No other thread can be in
1866 * this routine with the same iclog. Changing contents of iclog can there-
1867 * fore be done without grabbing the state machine lock. Updating the global
1868 * log will require grabbing the lock though.
1869 *
1870 * The entire log manager uses a logical block numbering scheme. Only
94860a30
CH
1871 * xlog_write_iclog knows about the fact that the log may not start with
1872 * block zero on a given device.
1da177e4 1873 */
94860a30 1874STATIC void
9a8d2fdb
MT
1875xlog_sync(
1876 struct xlog *log,
1877 struct xlog_in_core *iclog)
1da177e4 1878{
db0a6faf
CH
1879 unsigned int count; /* byte count of bwrite */
1880 unsigned int roundoff; /* roundoff to BB or stripe */
1881 uint64_t bno;
db0a6faf 1882 unsigned int size;
79b54d9b 1883 bool need_flush = true, split = false;
1da177e4 1884
155cc6b7 1885 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1da177e4 1886
db0a6faf 1887 count = xlog_calc_iclog_size(log, iclog, &roundoff);
1da177e4
LT
1888
1889 /* move grant heads by roundoff in sync */
28496968
CH
1890 xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
1891 xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
1da177e4
LT
1892
1893 /* put cycle number in every block */
1894 xlog_pack_data(log, iclog, roundoff);
1895
1896 /* real byte length */
0e446be4 1897 size = iclog->ic_offset;
db0a6faf 1898 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb))
0e446be4
CH
1899 size += roundoff;
1900 iclog->ic_header.h_len = cpu_to_be32(size);
1da177e4 1901
9b0489c1 1902 XFS_STATS_INC(log->l_mp, xs_log_writes);
ff6d6af2 1903 XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count));
1da177e4 1904
94860a30
CH
1905 bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn));
1906
1da177e4 1907 /* Do we need to split this write into 2 parts? */
79b54d9b
CH
1908 if (bno + BTOBB(count) > log->l_logBBsize) {
1909 xlog_split_iclog(log, &iclog->ic_header, bno, count);
1910 split = true;
1911 }
0e446be4
CH
1912
1913 /* calculcate the checksum */
1914 iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header,
1915 iclog->ic_datap, size);
609adfc2
BF
1916 /*
1917 * Intentionally corrupt the log record CRC based on the error injection
1918 * frequency, if defined. This facilitates testing log recovery in the
1919 * event of torn writes. Hence, set the IOABORT state to abort the log
1920 * write on I/O completion and shutdown the fs. The subsequent mount
1921 * detects the bad CRC and attempts to recover.
1922 */
366fc4b8 1923#ifdef DEBUG
3e88a007 1924 if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) {
e2a64192 1925 iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA);
366fc4b8 1926 iclog->ic_fail_crc = true;
609adfc2
BF
1927 xfs_warn(log->l_mp,
1928 "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.",
1929 be64_to_cpu(iclog->ic_header.h_lsn));
1930 }
366fc4b8 1931#endif
0e446be4 1932
2291dab2
DC
1933 /*
1934 * Flush the data device before flushing the log to make sure all meta
1935 * data written back from the AIL actually made it to disk before
1936 * stamping the new log tail LSN into the log buffer. For an external
1937 * log we need to issue the flush explicitly, and unfortunately
1938 * synchronously here; for an internal log we can simply use the block
1939 * layer state machine for preflushes.
1940 */
2d15d2c0 1941 if (log->l_targ != log->l_mp->m_ddev_targp || split) {
2291dab2 1942 xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
94860a30
CH
1943 need_flush = false;
1944 }
1da177e4 1945
abca1f33 1946 xlog_verify_iclog(log, iclog, count);
79b54d9b 1947 xlog_write_iclog(log, iclog, bno, count, need_flush);
94860a30 1948}
1da177e4 1949
1da177e4 1950/*
c41564b5 1951 * Deallocate a log structure
1da177e4 1952 */
a8272ce0 1953STATIC void
9a8d2fdb
MT
1954xlog_dealloc_log(
1955 struct xlog *log)
1da177e4
LT
1956{
1957 xlog_in_core_t *iclog, *next_iclog;
1da177e4
LT
1958 int i;
1959
71e330b5
DC
1960 xlog_cil_destroy(log);
1961
44396476 1962 /*
9c23eccc
DC
1963 * Cycle all the iclogbuf locks to make sure all log IO completion
1964 * is done before we tear down these buffers.
1965 */
1966 iclog = log->l_iclog;
1967 for (i = 0; i < log->l_iclog_bufs; i++) {
79b54d9b
CH
1968 down(&iclog->ic_sema);
1969 up(&iclog->ic_sema);
9c23eccc
DC
1970 iclog = iclog->ic_next;
1971 }
1972
1da177e4 1973 iclog = log->l_iclog;
9c23eccc 1974 for (i = 0; i < log->l_iclog_bufs; i++) {
1da177e4 1975 next_iclog = iclog->ic_next;
79b54d9b 1976 kmem_free(iclog->ic_data);
f0e2d93c 1977 kmem_free(iclog);
1da177e4
LT
1978 iclog = next_iclog;
1979 }
1da177e4 1980
1da177e4 1981 log->l_mp->m_log = NULL;
1058d0f5 1982 destroy_workqueue(log->l_ioend_workqueue);
f0e2d93c 1983 kmem_free(log);
c41564b5 1984} /* xlog_dealloc_log */
1da177e4
LT
1985
1986/*
1987 * Update counters atomically now that memcpy is done.
1988 */
1da177e4 1989static inline void
9a8d2fdb
MT
1990xlog_state_finish_copy(
1991 struct xlog *log,
1992 struct xlog_in_core *iclog,
1993 int record_cnt,
1994 int copy_bytes)
1da177e4 1995{
390aab0a 1996 lockdep_assert_held(&log->l_icloglock);
1da177e4 1997
413d57c9 1998 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1da177e4 1999 iclog->ic_offset += copy_bytes;
390aab0a 2000}
1da177e4 2001
7e9c6396
TS
2002/*
2003 * print out info relating to regions written which consume
2004 * the reservation
2005 */
71e330b5
DC
2006void
2007xlog_print_tic_res(
2008 struct xfs_mount *mp,
2009 struct xlog_ticket *ticket)
7e9c6396
TS
2010{
2011 uint i;
2012 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
2013
2014 /* match with XLOG_REG_TYPE_* in xfs_log.h */
5110cd82 2015#define REG_TYPE_STR(type, str) [XLOG_REG_TYPE_##type] = str
d31d7185 2016 static char *res_type_str[] = {
5110cd82
DW
2017 REG_TYPE_STR(BFORMAT, "bformat"),
2018 REG_TYPE_STR(BCHUNK, "bchunk"),
2019 REG_TYPE_STR(EFI_FORMAT, "efi_format"),
2020 REG_TYPE_STR(EFD_FORMAT, "efd_format"),
2021 REG_TYPE_STR(IFORMAT, "iformat"),
2022 REG_TYPE_STR(ICORE, "icore"),
2023 REG_TYPE_STR(IEXT, "iext"),
2024 REG_TYPE_STR(IBROOT, "ibroot"),
2025 REG_TYPE_STR(ILOCAL, "ilocal"),
2026 REG_TYPE_STR(IATTR_EXT, "iattr_ext"),
2027 REG_TYPE_STR(IATTR_BROOT, "iattr_broot"),
2028 REG_TYPE_STR(IATTR_LOCAL, "iattr_local"),
2029 REG_TYPE_STR(QFORMAT, "qformat"),
2030 REG_TYPE_STR(DQUOT, "dquot"),
2031 REG_TYPE_STR(QUOTAOFF, "quotaoff"),
2032 REG_TYPE_STR(LRHEADER, "LR header"),
2033 REG_TYPE_STR(UNMOUNT, "unmount"),
2034 REG_TYPE_STR(COMMIT, "commit"),
2035 REG_TYPE_STR(TRANSHDR, "trans header"),
d31d7185
DW
2036 REG_TYPE_STR(ICREATE, "inode create"),
2037 REG_TYPE_STR(RUI_FORMAT, "rui_format"),
2038 REG_TYPE_STR(RUD_FORMAT, "rud_format"),
2039 REG_TYPE_STR(CUI_FORMAT, "cui_format"),
2040 REG_TYPE_STR(CUD_FORMAT, "cud_format"),
2041 REG_TYPE_STR(BUI_FORMAT, "bui_format"),
2042 REG_TYPE_STR(BUD_FORMAT, "bud_format"),
7e9c6396 2043 };
d31d7185 2044 BUILD_BUG_ON(ARRAY_SIZE(res_type_str) != XLOG_REG_TYPE_MAX + 1);
5110cd82 2045#undef REG_TYPE_STR
7e9c6396 2046
7d2d5653 2047 xfs_warn(mp, "ticket reservation summary:");
f41febd2
JP
2048 xfs_warn(mp, " unit res = %d bytes",
2049 ticket->t_unit_res);
2050 xfs_warn(mp, " current res = %d bytes",
2051 ticket->t_curr_res);
2052 xfs_warn(mp, " total reg = %u bytes (o/flow = %u bytes)",
2053 ticket->t_res_arr_sum, ticket->t_res_o_flow);
2054 xfs_warn(mp, " ophdrs = %u (ophdr space = %u bytes)",
2055 ticket->t_res_num_ophdrs, ophdr_spc);
2056 xfs_warn(mp, " ophdr + reg = %u bytes",
2057 ticket->t_res_arr_sum + ticket->t_res_o_flow + ophdr_spc);
2058 xfs_warn(mp, " num regions = %u",
2059 ticket->t_res_num);
7e9c6396
TS
2060
2061 for (i = 0; i < ticket->t_res_num; i++) {
a0fa2b67 2062 uint r_type = ticket->t_res_arr[i].r_type;
08e96e1a 2063 xfs_warn(mp, "region[%u]: %s - %u bytes", i,
7e9c6396 2064 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
5110cd82 2065 "bad-rtype" : res_type_str[r_type]),
7e9c6396
TS
2066 ticket->t_res_arr[i].r_len);
2067 }
2068}
7e9c6396 2069
d4ca1d55
BF
2070/*
2071 * Print a summary of the transaction.
2072 */
2073void
2074xlog_print_trans(
e6631f85 2075 struct xfs_trans *tp)
d4ca1d55 2076{
e6631f85
DC
2077 struct xfs_mount *mp = tp->t_mountp;
2078 struct xfs_log_item *lip;
d4ca1d55
BF
2079
2080 /* dump core transaction and ticket info */
2081 xfs_warn(mp, "transaction summary:");
2c8f6265
BF
2082 xfs_warn(mp, " log res = %d", tp->t_log_res);
2083 xfs_warn(mp, " log count = %d", tp->t_log_count);
2084 xfs_warn(mp, " flags = 0x%x", tp->t_flags);
d4ca1d55
BF
2085
2086 xlog_print_tic_res(mp, tp->t_ticket);
2087
2088 /* dump each log item */
e6631f85 2089 list_for_each_entry(lip, &tp->t_items, li_trans) {
d4ca1d55
BF
2090 struct xfs_log_vec *lv = lip->li_lv;
2091 struct xfs_log_iovec *vec;
2092 int i;
2093
2094 xfs_warn(mp, "log item: ");
2095 xfs_warn(mp, " type = 0x%x", lip->li_type);
22525c17 2096 xfs_warn(mp, " flags = 0x%lx", lip->li_flags);
d4ca1d55
BF
2097 if (!lv)
2098 continue;
2099 xfs_warn(mp, " niovecs = %d", lv->lv_niovecs);
2100 xfs_warn(mp, " size = %d", lv->lv_size);
2101 xfs_warn(mp, " bytes = %d", lv->lv_bytes);
2102 xfs_warn(mp, " buf len = %d", lv->lv_buf_len);
2103
2104 /* dump each iovec for the log item */
2105 vec = lv->lv_iovecp;
2106 for (i = 0; i < lv->lv_niovecs; i++) {
2107 int dumplen = min(vec->i_len, 32);
2108
2109 xfs_warn(mp, " iovec[%d]", i);
2110 xfs_warn(mp, " type = 0x%x", vec->i_type);
2111 xfs_warn(mp, " len = %d", vec->i_len);
2112 xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i);
244e3dea 2113 xfs_hex_dump(vec->i_addr, dumplen);
d4ca1d55
BF
2114
2115 vec++;
2116 }
2117 }
2118}
2119
b5203cd0 2120/*
7ec94921
DC
2121 * Calculate the potential space needed by the log vector. We may need a start
2122 * record, and each region gets its own struct xlog_op_header and may need to be
2123 * double word aligned.
b5203cd0
DC
2124 */
2125static int
2126xlog_write_calc_vec_length(
2127 struct xlog_ticket *ticket,
7ec94921
DC
2128 struct xfs_log_vec *log_vector,
2129 bool need_start_rec)
b5203cd0 2130{
55b66332 2131 struct xfs_log_vec *lv;
7ec94921 2132 int headers = need_start_rec ? 1 : 0;
b5203cd0
DC
2133 int len = 0;
2134 int i;
2135
55b66332 2136 for (lv = log_vector; lv; lv = lv->lv_next) {
fd63875c
DC
2137 /* we don't write ordered log vectors */
2138 if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED)
2139 continue;
2140
55b66332
DC
2141 headers += lv->lv_niovecs;
2142
2143 for (i = 0; i < lv->lv_niovecs; i++) {
2144 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
b5203cd0 2145
55b66332
DC
2146 len += vecp->i_len;
2147 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
2148 }
b5203cd0
DC
2149 }
2150
2151 ticket->t_res_num_ophdrs += headers;
2152 len += headers * sizeof(struct xlog_op_header);
2153
2154 return len;
2155}
2156
7ec94921 2157static void
b5203cd0 2158xlog_write_start_rec(
e6b1f273 2159 struct xlog_op_header *ophdr,
b5203cd0
DC
2160 struct xlog_ticket *ticket)
2161{
b5203cd0
DC
2162 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2163 ophdr->oh_clientid = ticket->t_clientid;
2164 ophdr->oh_len = 0;
2165 ophdr->oh_flags = XLOG_START_TRANS;
2166 ophdr->oh_res2 = 0;
b5203cd0
DC
2167}
2168
2169static xlog_op_header_t *
2170xlog_write_setup_ophdr(
ad223e60 2171 struct xlog *log,
e6b1f273 2172 struct xlog_op_header *ophdr,
b5203cd0
DC
2173 struct xlog_ticket *ticket,
2174 uint flags)
2175{
b5203cd0
DC
2176 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2177 ophdr->oh_clientid = ticket->t_clientid;
2178 ophdr->oh_res2 = 0;
2179
2180 /* are we copying a commit or unmount record? */
2181 ophdr->oh_flags = flags;
2182
2183 /*
2184 * We've seen logs corrupted with bad transaction client ids. This
2185 * makes sure that XFS doesn't generate them on. Turn this into an EIO
2186 * and shut down the filesystem.
2187 */
2188 switch (ophdr->oh_clientid) {
2189 case XFS_TRANSACTION:
2190 case XFS_VOLUME:
2191 case XFS_LOG:
2192 break;
2193 default:
a0fa2b67 2194 xfs_warn(log->l_mp,
c9690043 2195 "Bad XFS transaction clientid 0x%x in ticket "PTR_FMT,
b5203cd0
DC
2196 ophdr->oh_clientid, ticket);
2197 return NULL;
2198 }
2199
2200 return ophdr;
2201}
2202
2203/*
2204 * Set up the parameters of the region copy into the log. This has
2205 * to handle region write split across multiple log buffers - this
2206 * state is kept external to this function so that this code can
ac0e300f 2207 * be written in an obvious, self documenting manner.
b5203cd0
DC
2208 */
2209static int
2210xlog_write_setup_copy(
2211 struct xlog_ticket *ticket,
2212 struct xlog_op_header *ophdr,
2213 int space_available,
2214 int space_required,
2215 int *copy_off,
2216 int *copy_len,
2217 int *last_was_partial_copy,
2218 int *bytes_consumed)
2219{
2220 int still_to_copy;
2221
2222 still_to_copy = space_required - *bytes_consumed;
2223 *copy_off = *bytes_consumed;
2224
2225 if (still_to_copy <= space_available) {
2226 /* write of region completes here */
2227 *copy_len = still_to_copy;
2228 ophdr->oh_len = cpu_to_be32(*copy_len);
2229 if (*last_was_partial_copy)
2230 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
2231 *last_was_partial_copy = 0;
2232 *bytes_consumed = 0;
2233 return 0;
2234 }
2235
2236 /* partial write of region, needs extra log op header reservation */
2237 *copy_len = space_available;
2238 ophdr->oh_len = cpu_to_be32(*copy_len);
2239 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
2240 if (*last_was_partial_copy)
2241 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
2242 *bytes_consumed += *copy_len;
2243 (*last_was_partial_copy)++;
2244
2245 /* account for new log op header */
2246 ticket->t_curr_res -= sizeof(struct xlog_op_header);
2247 ticket->t_res_num_ophdrs++;
2248
2249 return sizeof(struct xlog_op_header);
2250}
2251
2252static int
2253xlog_write_copy_finish(
ad223e60 2254 struct xlog *log,
b5203cd0
DC
2255 struct xlog_in_core *iclog,
2256 uint flags,
2257 int *record_cnt,
2258 int *data_cnt,
2259 int *partial_copy,
2260 int *partial_copy_len,
2261 int log_offset,
2262 struct xlog_in_core **commit_iclog)
2263{
df732b29
CH
2264 int error;
2265
b5203cd0
DC
2266 if (*partial_copy) {
2267 /*
2268 * This iclog has already been marked WANT_SYNC by
2269 * xlog_state_get_iclog_space.
2270 */
390aab0a 2271 spin_lock(&log->l_icloglock);
b5203cd0
DC
2272 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
2273 *record_cnt = 0;
2274 *data_cnt = 0;
df732b29 2275 goto release_iclog;
b5203cd0
DC
2276 }
2277
2278 *partial_copy = 0;
2279 *partial_copy_len = 0;
2280
2281 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
2282 /* no more space in this iclog - push it. */
390aab0a 2283 spin_lock(&log->l_icloglock);
b5203cd0
DC
2284 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
2285 *record_cnt = 0;
2286 *data_cnt = 0;
2287
69363999
CH
2288 if (iclog->ic_state == XLOG_STATE_ACTIVE)
2289 xlog_state_switch_iclogs(log, iclog, 0);
2290 else
2291 ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2292 iclog->ic_state == XLOG_STATE_IOERROR);
b5203cd0 2293 if (!commit_iclog)
df732b29
CH
2294 goto release_iclog;
2295 spin_unlock(&log->l_icloglock);
b5203cd0
DC
2296 ASSERT(flags & XLOG_COMMIT_TRANS);
2297 *commit_iclog = iclog;
2298 }
2299
2300 return 0;
df732b29
CH
2301
2302release_iclog:
2303 error = xlog_state_release_iclog(log, iclog);
2304 spin_unlock(&log->l_icloglock);
2305 return error;
b5203cd0
DC
2306}
2307
1da177e4
LT
2308/*
2309 * Write some region out to in-core log
2310 *
2311 * This will be called when writing externally provided regions or when
2312 * writing out a commit record for a given transaction.
2313 *
2314 * General algorithm:
2315 * 1. Find total length of this write. This may include adding to the
2316 * lengths passed in.
2317 * 2. Check whether we violate the tickets reservation.
2318 * 3. While writing to this iclog
2319 * A. Reserve as much space in this iclog as can get
2320 * B. If this is first write, save away start lsn
2321 * C. While writing this region:
2322 * 1. If first write of transaction, write start record
2323 * 2. Write log operation header (header per region)
2324 * 3. Find out if we can fit entire region into this iclog
2325 * 4. Potentially, verify destination memcpy ptr
2326 * 5. Memcpy (partial) region
2327 * 6. If partial copy, release iclog; otherwise, continue
2328 * copying more regions into current iclog
2329 * 4. Mark want sync bit (in simulation mode)
2330 * 5. Release iclog for potential flush to on-disk log.
2331 *
2332 * ERRORS:
2333 * 1. Panic if reservation is overrun. This should never happen since
2334 * reservation amounts are generated internal to the filesystem.
2335 * NOTES:
2336 * 1. Tickets are single threaded data structures.
2337 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
2338 * syncing routine. When a single log_write region needs to span
2339 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
2340 * on all log operation writes which don't contain the end of the
2341 * region. The XLOG_END_TRANS bit is used for the in-core log
2342 * operation which contains the end of the continued log_write region.
2343 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
2344 * we don't really know exactly how much space will be used. As a result,
2345 * we don't update ic_offset until the end when we know exactly how many
2346 * bytes have been written out.
2347 */
71e330b5 2348int
35a8a72f 2349xlog_write(
ad223e60 2350 struct xlog *log,
55b66332 2351 struct xfs_log_vec *log_vector,
35a8a72f
CH
2352 struct xlog_ticket *ticket,
2353 xfs_lsn_t *start_lsn,
2354 struct xlog_in_core **commit_iclog,
7ec94921
DC
2355 uint flags,
2356 bool need_start_rec)
1da177e4 2357{
99428ad0 2358 struct xlog_in_core *iclog = NULL;
55b66332
DC
2359 struct xfs_log_iovec *vecp;
2360 struct xfs_log_vec *lv;
99428ad0
CH
2361 int len;
2362 int index;
2363 int partial_copy = 0;
2364 int partial_copy_len = 0;
2365 int contwr = 0;
2366 int record_cnt = 0;
2367 int data_cnt = 0;
df732b29 2368 int error = 0;
99428ad0
CH
2369
2370 *start_lsn = 0;
2371
71e330b5 2372
93b8a585 2373 /*
7ec94921
DC
2374 * Region headers and bytes are already accounted for. We only need to
2375 * take into account start records and split regions in this function.
93b8a585 2376 */
7ec94921
DC
2377 if (ticket->t_flags & XLOG_TIC_INITED) {
2378 ticket->t_curr_res -= sizeof(struct xlog_op_header);
2379 ticket->t_flags &= ~XLOG_TIC_INITED;
2380 }
93b8a585
CH
2381
2382 /*
7ec94921
DC
2383 * Commit record headers and unmount records need to be accounted for.
2384 * These come in as separate writes so are easy to detect.
93b8a585 2385 */
7ec94921
DC
2386 if (!need_start_rec)
2387 ticket->t_curr_res -= sizeof(struct xlog_op_header);
7d2d5653
BF
2388 if (ticket->t_curr_res < 0) {
2389 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
2390 "ctx ticket reservation ran out. Need to up reservation");
55b66332 2391 xlog_print_tic_res(log->l_mp, ticket);
7d2d5653
BF
2392 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
2393 }
1da177e4 2394
7ec94921
DC
2395 len = xlog_write_calc_vec_length(ticket, log_vector, need_start_rec);
2396
55b66332
DC
2397 index = 0;
2398 lv = log_vector;
2399 vecp = lv->lv_iovecp;
fd63875c 2400 while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
e6b1f273 2401 void *ptr;
99428ad0 2402 int log_offset;
1da177e4 2403
99428ad0
CH
2404 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
2405 &contwr, &log_offset);
2406 if (error)
2407 return error;
1da177e4 2408
99428ad0 2409 ASSERT(log_offset <= iclog->ic_size - 1);
e6b1f273 2410 ptr = iclog->ic_datap + log_offset;
1da177e4 2411
99428ad0
CH
2412 /* start_lsn is the first lsn written to. That's all we need. */
2413 if (!*start_lsn)
2414 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
b5203cd0 2415
99428ad0
CH
2416 /*
2417 * This loop writes out as many regions as can fit in the amount
2418 * of space which was allocated by xlog_state_get_iclog_space().
2419 */
fd63875c
DC
2420 while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
2421 struct xfs_log_iovec *reg;
99428ad0 2422 struct xlog_op_header *ophdr;
99428ad0
CH
2423 int copy_len;
2424 int copy_off;
fd63875c
DC
2425 bool ordered = false;
2426
2427 /* ordered log vectors have no regions to write */
2428 if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED) {
2429 ASSERT(lv->lv_niovecs == 0);
2430 ordered = true;
2431 goto next_lv;
2432 }
99428ad0 2433
fd63875c 2434 reg = &vecp[index];
c8ce540d
DW
2435 ASSERT(reg->i_len % sizeof(int32_t) == 0);
2436 ASSERT((unsigned long)ptr % sizeof(int32_t) == 0);
99428ad0 2437
7ec94921
DC
2438 /*
2439 * Before we start formatting log vectors, we need to
2440 * write a start record. Only do this for the first
2441 * iclog we write to.
2442 */
2443 if (need_start_rec) {
2444 xlog_write_start_rec(ptr, ticket);
e6b1f273 2445 xlog_write_adv_cnt(&ptr, &len, &log_offset,
7ec94921 2446 sizeof(struct xlog_op_header));
99428ad0 2447 }
b5203cd0 2448
99428ad0
CH
2449 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
2450 if (!ophdr)
2451337d 2451 return -EIO;
99428ad0 2452
e6b1f273 2453 xlog_write_adv_cnt(&ptr, &len, &log_offset,
99428ad0
CH
2454 sizeof(struct xlog_op_header));
2455
2456 len += xlog_write_setup_copy(ticket, ophdr,
2457 iclog->ic_size-log_offset,
55b66332 2458 reg->i_len,
99428ad0
CH
2459 &copy_off, &copy_len,
2460 &partial_copy,
2461 &partial_copy_len);
2462 xlog_verify_dest_ptr(log, ptr);
2463
91f9f5fe
ES
2464 /*
2465 * Copy region.
2466 *
2467 * Unmount records just log an opheader, so can have
2468 * empty payloads with no data region to copy. Hence we
2469 * only copy the payload if the vector says it has data
2470 * to copy.
2471 */
99428ad0 2472 ASSERT(copy_len >= 0);
91f9f5fe
ES
2473 if (copy_len > 0) {
2474 memcpy(ptr, reg->i_addr + copy_off, copy_len);
2475 xlog_write_adv_cnt(&ptr, &len, &log_offset,
2476 copy_len);
2477 }
7ec94921 2478 copy_len += sizeof(struct xlog_op_header);
99428ad0 2479 record_cnt++;
7ec94921
DC
2480 if (need_start_rec) {
2481 copy_len += sizeof(struct xlog_op_header);
2482 record_cnt++;
2483 need_start_rec = false;
2484 }
99428ad0
CH
2485 data_cnt += contwr ? copy_len : 0;
2486
2487 error = xlog_write_copy_finish(log, iclog, flags,
2488 &record_cnt, &data_cnt,
2489 &partial_copy,
2490 &partial_copy_len,
2491 log_offset,
2492 commit_iclog);
2493 if (error)
2494 return error;
2495
2496 /*
2497 * if we had a partial copy, we need to get more iclog
2498 * space but we don't want to increment the region
2499 * index because there is still more is this region to
2500 * write.
2501 *
2502 * If we completed writing this region, and we flushed
2503 * the iclog (indicated by resetting of the record
2504 * count), then we also need to get more log space. If
2505 * this was the last record, though, we are done and
2506 * can just return.
2507 */
2508 if (partial_copy)
2509 break;
2510
55b66332 2511 if (++index == lv->lv_niovecs) {
fd63875c 2512next_lv:
55b66332
DC
2513 lv = lv->lv_next;
2514 index = 0;
2515 if (lv)
2516 vecp = lv->lv_iovecp;
2517 }
749f24f3 2518 if (record_cnt == 0 && !ordered) {
55b66332 2519 if (!lv)
99428ad0
CH
2520 return 0;
2521 break;
2522 }
2523 }
2524 }
2525
2526 ASSERT(len == 0);
2527
390aab0a 2528 spin_lock(&log->l_icloglock);
99428ad0 2529 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
df732b29
CH
2530 if (commit_iclog) {
2531 ASSERT(flags & XLOG_COMMIT_TRANS);
2532 *commit_iclog = iclog;
2533 } else {
2534 error = xlog_state_release_iclog(log, iclog);
2535 }
390aab0a 2536 spin_unlock(&log->l_icloglock);
1da177e4 2537
df732b29 2538 return error;
99428ad0 2539}
1da177e4
LT
2540
2541
2542/*****************************************************************************
2543 *
2544 * State Machine functions
2545 *
2546 *****************************************************************************
2547 */
2548
c814b4f2
CH
2549static void
2550xlog_state_activate_iclog(
2551 struct xlog_in_core *iclog,
2552 int *iclogs_changed)
2553{
2554 ASSERT(list_empty_careful(&iclog->ic_callbacks));
2555
2556 /*
2557 * If the number of ops in this iclog indicate it just contains the
2558 * dummy transaction, we can change state into IDLE (the second time
2559 * around). Otherwise we should change the state into NEED a dummy.
2560 * We don't need to cover the dummy.
2561 */
2562 if (*iclogs_changed == 0 &&
2563 iclog->ic_header.h_num_logops == cpu_to_be32(XLOG_COVER_OPS)) {
2564 *iclogs_changed = 1;
2565 } else {
2566 /*
2567 * We have two dirty iclogs so start over. This could also be
2568 * num of ops indicating this is not the dummy going out.
2569 */
2570 *iclogs_changed = 2;
2571 }
2572
2573 iclog->ic_state = XLOG_STATE_ACTIVE;
2574 iclog->ic_offset = 0;
2575 iclog->ic_header.h_num_logops = 0;
2576 memset(iclog->ic_header.h_cycle_data, 0,
2577 sizeof(iclog->ic_header.h_cycle_data));
2578 iclog->ic_header.h_lsn = 0;
2579}
2580
0383f543 2581/*
c814b4f2
CH
2582 * Loop through all iclogs and mark all iclogs currently marked DIRTY as
2583 * ACTIVE after iclog I/O has completed.
1da177e4 2584 */
c814b4f2
CH
2585static void
2586xlog_state_activate_iclogs(
0383f543 2587 struct xlog *log,
c814b4f2 2588 int *iclogs_changed)
1da177e4 2589{
c814b4f2 2590 struct xlog_in_core *iclog = log->l_iclog;
1da177e4 2591
1da177e4 2592 do {
c814b4f2
CH
2593 if (iclog->ic_state == XLOG_STATE_DIRTY)
2594 xlog_state_activate_iclog(iclog, iclogs_changed);
2595 /*
2596 * The ordering of marking iclogs ACTIVE must be maintained, so
2597 * an iclog doesn't become ACTIVE beyond one that is SYNCING.
2598 */
2599 else if (iclog->ic_state != XLOG_STATE_ACTIVE)
2600 break;
2601 } while ((iclog = iclog->ic_next) != log->l_iclog);
2602}
0383f543 2603
c814b4f2
CH
2604static int
2605xlog_covered_state(
2606 int prev_state,
2607 int iclogs_changed)
2608{
0383f543 2609 /*
c814b4f2
CH
2610 * We usually go to NEED. But we go to NEED2 if the changed indicates we
2611 * are done writing the dummy record. If we are done with the second
2612 * dummy recored (DONE2), then we go to IDLE.
0383f543 2613 */
c814b4f2
CH
2614 switch (prev_state) {
2615 case XLOG_STATE_COVER_IDLE:
2616 case XLOG_STATE_COVER_NEED:
2617 case XLOG_STATE_COVER_NEED2:
2618 break;
2619 case XLOG_STATE_COVER_DONE:
2620 if (iclogs_changed == 1)
2621 return XLOG_STATE_COVER_NEED2;
2622 break;
2623 case XLOG_STATE_COVER_DONE2:
2624 if (iclogs_changed == 1)
2625 return XLOG_STATE_COVER_IDLE;
2626 break;
2627 default:
2628 ASSERT(0);
2629 }
0383f543 2630
c814b4f2
CH
2631 return XLOG_STATE_COVER_NEED;
2632}
1da177e4 2633
c814b4f2
CH
2634STATIC void
2635xlog_state_clean_iclog(
2636 struct xlog *log,
2637 struct xlog_in_core *dirty_iclog)
2638{
2639 int iclogs_changed = 0;
1da177e4 2640
5781464b 2641 dirty_iclog->ic_state = XLOG_STATE_DIRTY;
1da177e4 2642
c814b4f2
CH
2643 xlog_state_activate_iclogs(log, &iclogs_changed);
2644 wake_up_all(&dirty_iclog->ic_force_wait);
2645
2646 if (iclogs_changed) {
2647 log->l_covered_state = xlog_covered_state(log->l_covered_state,
2648 iclogs_changed);
1da177e4 2649 }
0383f543 2650}
1da177e4
LT
2651
2652STATIC xfs_lsn_t
2653xlog_get_lowest_lsn(
9bff3132 2654 struct xlog *log)
1da177e4 2655{
9bff3132
CH
2656 struct xlog_in_core *iclog = log->l_iclog;
2657 xfs_lsn_t lowest_lsn = 0, lsn;
1da177e4 2658
1da177e4 2659 do {
1858bb0b
CH
2660 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2661 iclog->ic_state == XLOG_STATE_DIRTY)
9bff3132
CH
2662 continue;
2663
2664 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2665 if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0)
1da177e4 2666 lowest_lsn = lsn;
9bff3132
CH
2667 } while ((iclog = iclog->ic_next) != log->l_iclog);
2668
014c2544 2669 return lowest_lsn;
1da177e4
LT
2670}
2671
14e15f1b
DC
2672/*
2673 * Completion of a iclog IO does not imply that a transaction has completed, as
2674 * transactions can be large enough to span many iclogs. We cannot change the
2675 * tail of the log half way through a transaction as this may be the only
2676 * transaction in the log and moving the tail to point to the middle of it
2677 * will prevent recovery from finding the start of the transaction. Hence we
2678 * should only update the last_sync_lsn if this iclog contains transaction
2679 * completion callbacks on it.
2680 *
2681 * We have to do this before we drop the icloglock to ensure we are the only one
2682 * that can update it.
2683 *
2684 * If we are moving the last_sync_lsn forwards, we also need to ensure we kick
2685 * the reservation grant head pushing. This is due to the fact that the push
2686 * target is bound by the current last_sync_lsn value. Hence if we have a large
2687 * amount of log space bound up in this committing transaction then the
2688 * last_sync_lsn value may be the limiting factor preventing tail pushing from
2689 * freeing space in the log. Hence once we've updated the last_sync_lsn we
2690 * should push the AIL to ensure the push target (and hence the grant head) is
2691 * no longer bound by the old log head location and can move forwards and make
2692 * progress again.
2693 */
2694static void
2695xlog_state_set_callback(
2696 struct xlog *log,
2697 struct xlog_in_core *iclog,
2698 xfs_lsn_t header_lsn)
2699{
2700 iclog->ic_state = XLOG_STATE_CALLBACK;
2701
2702 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2703 header_lsn) <= 0);
2704
2705 if (list_empty_careful(&iclog->ic_callbacks))
2706 return;
2707
2708 atomic64_set(&log->l_last_sync_lsn, header_lsn);
2709 xlog_grant_push_ail(log, 0);
2710}
2711
5e96fa8d
DC
2712/*
2713 * Return true if we need to stop processing, false to continue to the next
2714 * iclog. The caller will need to run callbacks if the iclog is returned in the
2715 * XLOG_STATE_CALLBACK state.
2716 */
2717static bool
2718xlog_state_iodone_process_iclog(
2719 struct xlog *log,
2720 struct xlog_in_core *iclog,
5e96fa8d
DC
2721 bool *ioerror)
2722{
2723 xfs_lsn_t lowest_lsn;
14e15f1b 2724 xfs_lsn_t header_lsn;
5e96fa8d 2725
1858bb0b
CH
2726 switch (iclog->ic_state) {
2727 case XLOG_STATE_ACTIVE:
2728 case XLOG_STATE_DIRTY:
2729 /*
2730 * Skip all iclogs in the ACTIVE & DIRTY states:
2731 */
5e96fa8d 2732 return false;
1858bb0b
CH
2733 case XLOG_STATE_IOERROR:
2734 /*
2735 * Between marking a filesystem SHUTDOWN and stopping the log,
2736 * we do flush all iclogs to disk (if there wasn't a log I/O
2737 * error). So, we do want things to go smoothly in case of just
4b29ab04 2738 * a SHUTDOWN w/o a LOG_IO_ERROR.
1858bb0b 2739 */
5e96fa8d
DC
2740 *ioerror = true;
2741 return false;
1858bb0b 2742 case XLOG_STATE_DONE_SYNC:
1858bb0b 2743 /*
4b29ab04
CH
2744 * Now that we have an iclog that is in the DONE_SYNC state, do
2745 * one more check here to see if we have chased our tail around.
2746 * If this is not the lowest lsn iclog, then we will leave it
2747 * for another completion to process.
1858bb0b
CH
2748 */
2749 header_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2750 lowest_lsn = xlog_get_lowest_lsn(log);
2751 if (lowest_lsn && XFS_LSN_CMP(lowest_lsn, header_lsn) < 0)
2752 return false;
2753 xlog_state_set_callback(log, iclog, header_lsn);
2754 return false;
2755 default:
2756 /*
2757 * Can only perform callbacks in order. Since this iclog is not
4b29ab04
CH
2758 * in the DONE_SYNC state, we skip the rest and just try to
2759 * clean up.
1858bb0b 2760 */
5e96fa8d
DC
2761 return true;
2762 }
5e96fa8d
DC
2763}
2764
6546818c
DC
2765/*
2766 * Keep processing entries in the iclog callback list until we come around and
2767 * it is empty. We need to atomically see that the list is empty and change the
2768 * state to DIRTY so that we don't miss any more callbacks being added.
2769 *
2770 * This function is called with the icloglock held and returns with it held. We
2771 * drop it while running callbacks, however, as holding it over thousands of
2772 * callbacks is unnecessary and causes excessive contention if we do.
2773 */
2774static void
2775xlog_state_do_iclog_callbacks(
2776 struct xlog *log,
12e6a0f4 2777 struct xlog_in_core *iclog)
f7559793
DW
2778 __releases(&log->l_icloglock)
2779 __acquires(&log->l_icloglock)
6546818c
DC
2780{
2781 spin_unlock(&log->l_icloglock);
2782 spin_lock(&iclog->ic_callback_lock);
2783 while (!list_empty(&iclog->ic_callbacks)) {
2784 LIST_HEAD(tmp);
2785
2786 list_splice_init(&iclog->ic_callbacks, &tmp);
2787
2788 spin_unlock(&iclog->ic_callback_lock);
12e6a0f4 2789 xlog_cil_process_committed(&tmp);
6546818c
DC
2790 spin_lock(&iclog->ic_callback_lock);
2791 }
2792
2793 /*
2794 * Pick up the icloglock while still holding the callback lock so we
2795 * serialise against anyone trying to add more callbacks to this iclog
2796 * now we've finished processing.
2797 */
2798 spin_lock(&log->l_icloglock);
2799 spin_unlock(&iclog->ic_callback_lock);
2800}
2801
1da177e4
LT
2802STATIC void
2803xlog_state_do_callback(
12e6a0f4 2804 struct xlog *log)
1da177e4 2805{
5e96fa8d
DC
2806 struct xlog_in_core *iclog;
2807 struct xlog_in_core *first_iclog;
5e96fa8d
DC
2808 bool cycled_icloglock;
2809 bool ioerror;
2810 int flushcnt = 0;
2811 int repeats = 0;
1da177e4 2812
b22cd72c 2813 spin_lock(&log->l_icloglock);
1da177e4
LT
2814 do {
2815 /*
2816 * Scan all iclogs starting with the one pointed to by the
2817 * log. Reset this starting point each time the log is
2818 * unlocked (during callbacks).
2819 *
2820 * Keep looping through iclogs until one full pass is made
2821 * without running any callbacks.
2822 */
2823 first_iclog = log->l_iclog;
2824 iclog = log->l_iclog;
6546818c 2825 cycled_icloglock = false;
5e96fa8d 2826 ioerror = false;
1da177e4
LT
2827 repeats++;
2828
2829 do {
5e96fa8d 2830 if (xlog_state_iodone_process_iclog(log, iclog,
4b29ab04 2831 &ioerror))
5e96fa8d 2832 break;
1da177e4 2833
1858bb0b
CH
2834 if (iclog->ic_state != XLOG_STATE_CALLBACK &&
2835 iclog->ic_state != XLOG_STATE_IOERROR) {
1da177e4
LT
2836 iclog = iclog->ic_next;
2837 continue;
2838 }
2839
114d23aa 2840 /*
6546818c
DC
2841 * Running callbacks will drop the icloglock which means
2842 * we'll have to run at least one more complete loop.
114d23aa 2843 */
6546818c 2844 cycled_icloglock = true;
12e6a0f4 2845 xlog_state_do_iclog_callbacks(log, iclog);
5781464b
CH
2846 if (XLOG_FORCED_SHUTDOWN(log))
2847 wake_up_all(&iclog->ic_force_wait);
2848 else
2849 xlog_state_clean_iclog(log, iclog);
1da177e4
LT
2850 iclog = iclog->ic_next;
2851 } while (first_iclog != iclog);
a3c6685e
NS
2852
2853 if (repeats > 5000) {
2854 flushcnt += repeats;
2855 repeats = 0;
a0fa2b67 2856 xfs_warn(log->l_mp,
a3c6685e 2857 "%s: possible infinite loop (%d iterations)",
34a622b2 2858 __func__, flushcnt);
1da177e4 2859 }
5e96fa8d 2860 } while (!ioerror && cycled_icloglock);
1da177e4 2861
1858bb0b
CH
2862 if (log->l_iclog->ic_state == XLOG_STATE_ACTIVE ||
2863 log->l_iclog->ic_state == XLOG_STATE_IOERROR)
eb40a875 2864 wake_up_all(&log->l_flush_wait);
cdea5459
RR
2865
2866 spin_unlock(&log->l_icloglock);
d748c623 2867}
1da177e4
LT
2868
2869
2870/*
2871 * Finish transitioning this iclog to the dirty state.
2872 *
2873 * Make sure that we completely execute this routine only when this is
2874 * the last call to the iclog. There is a good chance that iclog flushes,
2875 * when we reach the end of the physical log, get turned into 2 separate
2876 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2877 * routine. By using the reference count bwritecnt, we guarantee that only
2878 * the second completion goes through.
2879 *
2880 * Callbacks could take time, so they are done outside the scope of the
12017faf 2881 * global state machine log lock.
1da177e4 2882 */
a8272ce0 2883STATIC void
1da177e4 2884xlog_state_done_syncing(
12e6a0f4 2885 struct xlog_in_core *iclog)
1da177e4 2886{
d15cbf2f 2887 struct xlog *log = iclog->ic_log;
1da177e4 2888
b22cd72c 2889 spin_lock(&log->l_icloglock);
155cc6b7 2890 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1da177e4
LT
2891
2892 /*
2893 * If we got an error, either on the first buffer, or in the case of
12e6a0f4
CH
2894 * split log writes, on the second, we shut down the file system and
2895 * no iclogs should ever be attempted to be written to disk again.
1da177e4 2896 */
12e6a0f4
CH
2897 if (!XLOG_FORCED_SHUTDOWN(log)) {
2898 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING);
1da177e4 2899 iclog->ic_state = XLOG_STATE_DONE_SYNC;
12e6a0f4 2900 }
1da177e4
LT
2901
2902 /*
2903 * Someone could be sleeping prior to writing out the next
2904 * iclog buffer, we wake them all, one will get to do the
2905 * I/O, the others get to wait for the result.
2906 */
eb40a875 2907 wake_up_all(&iclog->ic_write_wait);
b22cd72c 2908 spin_unlock(&log->l_icloglock);
12e6a0f4
CH
2909 xlog_state_do_callback(log); /* also cleans log */
2910}
1da177e4
LT
2911
2912/*
2913 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
12017faf
DC
2914 * sleep. We wait on the flush queue on the head iclog as that should be
2915 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2916 * we will wait here and all new writes will sleep until a sync completes.
1da177e4
LT
2917 *
2918 * The in-core logs are used in a circular fashion. They are not used
2919 * out-of-order even when an iclog past the head is free.
2920 *
2921 * return:
2922 * * log_offset where xlog_write() can start writing into the in-core
2923 * log's data space.
2924 * * in-core log pointer to which xlog_write() should write.
2925 * * boolean indicating this is a continued write to an in-core log.
2926 * If this is the last write, then the in-core log's offset field
2927 * needs to be incremented, depending on the amount of data which
2928 * is copied.
2929 */
a8272ce0 2930STATIC int
9a8d2fdb
MT
2931xlog_state_get_iclog_space(
2932 struct xlog *log,
2933 int len,
2934 struct xlog_in_core **iclogp,
2935 struct xlog_ticket *ticket,
2936 int *continued_write,
2937 int *logoffsetp)
1da177e4 2938{
1da177e4
LT
2939 int log_offset;
2940 xlog_rec_header_t *head;
2941 xlog_in_core_t *iclog;
1da177e4
LT
2942
2943restart:
b22cd72c 2944 spin_lock(&log->l_icloglock);
1da177e4 2945 if (XLOG_FORCED_SHUTDOWN(log)) {
b22cd72c 2946 spin_unlock(&log->l_icloglock);
2451337d 2947 return -EIO;
1da177e4
LT
2948 }
2949
2950 iclog = log->l_iclog;
d748c623 2951 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
ff6d6af2 2952 XFS_STATS_INC(log->l_mp, xs_log_noiclogs);
d748c623
MW
2953
2954 /* Wait for log writes to have flushed */
eb40a875 2955 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
1da177e4
LT
2956 goto restart;
2957 }
d748c623 2958
1da177e4
LT
2959 head = &iclog->ic_header;
2960
155cc6b7 2961 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
1da177e4
LT
2962 log_offset = iclog->ic_offset;
2963
2964 /* On the 1st write to an iclog, figure out lsn. This works
2965 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2966 * committing to. If the offset is set, that's how many blocks
2967 * must be written.
2968 */
2969 if (log_offset == 0) {
2970 ticket->t_curr_res -= log->l_iclog_hsize;
0adba536 2971 xlog_tic_add_region(ticket,
7e9c6396
TS
2972 log->l_iclog_hsize,
2973 XLOG_REG_TYPE_LRHEADER);
b53e675d
CH
2974 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2975 head->h_lsn = cpu_to_be64(
03bea6fe 2976 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
1da177e4
LT
2977 ASSERT(log->l_curr_block >= 0);
2978 }
2979
2980 /* If there is enough room to write everything, then do it. Otherwise,
2981 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2982 * bit is on, so this will get flushed out. Don't update ic_offset
2983 * until you know exactly how many bytes get copied. Therefore, wait
2984 * until later to update ic_offset.
2985 *
2986 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2987 * can fit into remaining data section.
2988 */
2989 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
df732b29
CH
2990 int error = 0;
2991
1da177e4
LT
2992 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2993
49641f1a 2994 /*
df732b29
CH
2995 * If we are the only one writing to this iclog, sync it to
2996 * disk. We need to do an atomic compare and decrement here to
2997 * avoid racing with concurrent atomic_dec_and_lock() calls in
49641f1a
DC
2998 * xlog_state_release_iclog() when there is more than one
2999 * reference to the iclog.
3000 */
df732b29 3001 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1))
49641f1a 3002 error = xlog_state_release_iclog(log, iclog);
df732b29
CH
3003 spin_unlock(&log->l_icloglock);
3004 if (error)
3005 return error;
1da177e4
LT
3006 goto restart;
3007 }
3008
3009 /* Do we have enough room to write the full amount in the remainder
3010 * of this iclog? Or must we continue a write on the next iclog and
3011 * mark this iclog as completely taken? In the case where we switch
3012 * iclogs (to mark it taken), this particular iclog will release/sync
3013 * to disk in xlog_write().
3014 */
3015 if (len <= iclog->ic_size - iclog->ic_offset) {
3016 *continued_write = 0;
3017 iclog->ic_offset += len;
3018 } else {
3019 *continued_write = 1;
3020 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
3021 }
3022 *iclogp = iclog;
3023
3024 ASSERT(iclog->ic_offset <= iclog->ic_size);
b22cd72c 3025 spin_unlock(&log->l_icloglock);
1da177e4
LT
3026
3027 *logoffsetp = log_offset;
3028 return 0;
3029} /* xlog_state_get_iclog_space */
3030
1da177e4
LT
3031/* The first cnt-1 times through here we don't need to
3032 * move the grant write head because the permanent
3033 * reservation has reserved cnt times the unit amount.
3034 * Release part of current permanent unit reservation and
3035 * reset current reservation to be one units worth. Also
3036 * move grant reservation head forward.
3037 */
3038STATIC void
9a8d2fdb
MT
3039xlog_regrant_reserve_log_space(
3040 struct xlog *log,
3041 struct xlog_ticket *ticket)
1da177e4 3042{
0b1b213f
CH
3043 trace_xfs_log_regrant_reserve_enter(log, ticket);
3044
1da177e4
LT
3045 if (ticket->t_cnt > 0)
3046 ticket->t_cnt--;
3047
28496968 3048 xlog_grant_sub_space(log, &log->l_reserve_head.grant,
a69ed03c 3049 ticket->t_curr_res);
28496968 3050 xlog_grant_sub_space(log, &log->l_write_head.grant,
a69ed03c 3051 ticket->t_curr_res);
1da177e4 3052 ticket->t_curr_res = ticket->t_unit_res;
0adba536 3053 xlog_tic_reset_res(ticket);
0b1b213f
CH
3054
3055 trace_xfs_log_regrant_reserve_sub(log, ticket);
3056
1da177e4 3057 /* just return if we still have some of the pre-reserved space */
d0eb2f38 3058 if (ticket->t_cnt > 0)
1da177e4 3059 return;
1da177e4 3060
28496968 3061 xlog_grant_add_space(log, &log->l_reserve_head.grant,
a69ed03c 3062 ticket->t_unit_res);
0b1b213f
CH
3063
3064 trace_xfs_log_regrant_reserve_exit(log, ticket);
3065
1da177e4 3066 ticket->t_curr_res = ticket->t_unit_res;
0adba536 3067 xlog_tic_reset_res(ticket);
1da177e4
LT
3068} /* xlog_regrant_reserve_log_space */
3069
3070
3071/*
3072 * Give back the space left from a reservation.
3073 *
3074 * All the information we need to make a correct determination of space left
3075 * is present. For non-permanent reservations, things are quite easy. The
3076 * count should have been decremented to zero. We only need to deal with the
3077 * space remaining in the current reservation part of the ticket. If the
3078 * ticket contains a permanent reservation, there may be left over space which
3079 * needs to be released. A count of N means that N-1 refills of the current
3080 * reservation can be done before we need to ask for more space. The first
3081 * one goes to fill up the first current reservation. Once we run out of
3082 * space, the count will stay at zero and the only space remaining will be
3083 * in the current reservation field.
3084 */
3085STATIC void
9a8d2fdb
MT
3086xlog_ungrant_log_space(
3087 struct xlog *log,
3088 struct xlog_ticket *ticket)
1da177e4 3089{
663e496a
DC
3090 int bytes;
3091
1da177e4
LT
3092 if (ticket->t_cnt > 0)
3093 ticket->t_cnt--;
3094
0b1b213f 3095 trace_xfs_log_ungrant_enter(log, ticket);
0b1b213f 3096 trace_xfs_log_ungrant_sub(log, ticket);
1da177e4 3097
663e496a
DC
3098 /*
3099 * If this is a permanent reservation ticket, we may be able to free
1da177e4
LT
3100 * up more space based on the remaining count.
3101 */
663e496a 3102 bytes = ticket->t_curr_res;
1da177e4
LT
3103 if (ticket->t_cnt > 0) {
3104 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
663e496a 3105 bytes += ticket->t_unit_res*ticket->t_cnt;
1da177e4
LT
3106 }
3107
28496968
CH
3108 xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
3109 xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
663e496a 3110
0b1b213f
CH
3111 trace_xfs_log_ungrant_exit(log, ticket);
3112
cfb7cdca 3113 xfs_log_space_wake(log->l_mp);
09a423a3 3114}
1da177e4 3115
1da177e4 3116/*
69363999
CH
3117 * Mark the current iclog in the ring as WANT_SYNC and move the current iclog
3118 * pointer to the next iclog in the ring.
3119 *
3120 * When called from xlog_state_get_iclog_space(), the exact size of the iclog
3121 * has not yet been determined, all we know is that we have run out of space in
3122 * the current iclog.
1da177e4
LT
3123 */
3124STATIC void
9a8d2fdb
MT
3125xlog_state_switch_iclogs(
3126 struct xlog *log,
3127 struct xlog_in_core *iclog,
3128 int eventual_size)
1da177e4
LT
3129{
3130 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
69363999
CH
3131 assert_spin_locked(&log->l_icloglock);
3132
1da177e4
LT
3133 if (!eventual_size)
3134 eventual_size = iclog->ic_offset;
3135 iclog->ic_state = XLOG_STATE_WANT_SYNC;
b53e675d 3136 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
1da177e4
LT
3137 log->l_prev_block = log->l_curr_block;
3138 log->l_prev_cycle = log->l_curr_cycle;
3139
3140 /* roll log?: ic_offset changed later */
3141 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
3142
3143 /* Round up to next log-sunit */
62118709 3144 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
1da177e4 3145 log->l_mp->m_sb.sb_logsunit > 1) {
c8ce540d 3146 uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
1da177e4
LT
3147 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
3148 }
3149
3150 if (log->l_curr_block >= log->l_logBBsize) {
a45086e2
BF
3151 /*
3152 * Rewind the current block before the cycle is bumped to make
3153 * sure that the combined LSN never transiently moves forward
3154 * when the log wraps to the next cycle. This is to support the
3155 * unlocked sample of these fields from xlog_valid_lsn(). Most
3156 * other cases should acquire l_icloglock.
3157 */
3158 log->l_curr_block -= log->l_logBBsize;
3159 ASSERT(log->l_curr_block >= 0);
3160 smp_wmb();
1da177e4
LT
3161 log->l_curr_cycle++;
3162 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
3163 log->l_curr_cycle++;
1da177e4
LT
3164 }
3165 ASSERT(iclog == log->l_iclog);
3166 log->l_iclog = iclog->ic_next;
3167} /* xlog_state_switch_iclogs */
3168
1da177e4
LT
3169/*
3170 * Write out all data in the in-core log as of this exact moment in time.
3171 *
3172 * Data may be written to the in-core log during this call. However,
3173 * we don't guarantee this data will be written out. A change from past
3174 * implementation means this routine will *not* write out zero length LRs.
3175 *
3176 * Basically, we try and perform an intelligent scan of the in-core logs.
3177 * If we determine there is no flushable data, we just return. There is no
3178 * flushable data if:
3179 *
3180 * 1. the current iclog is active and has no data; the previous iclog
3181 * is in the active or dirty state.
3182 * 2. the current iclog is drity, and the previous iclog is in the
3183 * active or dirty state.
3184 *
12017faf 3185 * We may sleep if:
1da177e4
LT
3186 *
3187 * 1. the current iclog is not in the active nor dirty state.
3188 * 2. the current iclog dirty, and the previous iclog is not in the
3189 * active nor dirty state.
3190 * 3. the current iclog is active, and there is another thread writing
3191 * to this particular iclog.
3192 * 4. a) the current iclog is active and has no other writers
3193 * b) when we return from flushing out this iclog, it is still
3194 * not in the active nor dirty state.
3195 */
a14a348b 3196int
60e5bb78 3197xfs_log_force(
a14a348b 3198 struct xfs_mount *mp,
60e5bb78 3199 uint flags)
1da177e4 3200{
ad223e60 3201 struct xlog *log = mp->m_log;
a14a348b
CH
3202 struct xlog_in_core *iclog;
3203 xfs_lsn_t lsn;
3204
ff6d6af2 3205 XFS_STATS_INC(mp, xs_log_force);
60e5bb78 3206 trace_xfs_log_force(mp, 0, _RET_IP_);
1da177e4 3207
93b8a585 3208 xlog_cil_force(log);
71e330b5 3209
b22cd72c 3210 spin_lock(&log->l_icloglock);
1da177e4 3211 iclog = log->l_iclog;
1858bb0b 3212 if (iclog->ic_state == XLOG_STATE_IOERROR)
e6b96570 3213 goto out_error;
1da177e4 3214
e6b96570
CH
3215 if (iclog->ic_state == XLOG_STATE_DIRTY ||
3216 (iclog->ic_state == XLOG_STATE_ACTIVE &&
3217 atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) {
1da177e4 3218 /*
e6b96570
CH
3219 * If the head is dirty or (active and empty), then we need to
3220 * look at the previous iclog.
3221 *
3222 * If the previous iclog is active or dirty we are done. There
3223 * is nothing to sync out. Otherwise, we attach ourselves to the
1da177e4
LT
3224 * previous iclog and go to sleep.
3225 */
e6b96570 3226 iclog = iclog->ic_prev;
e6b96570
CH
3227 } else if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3228 if (atomic_read(&iclog->ic_refcnt) == 0) {
3229 /*
3230 * We are the only one with access to this iclog.
3231 *
3232 * Flush it out now. There should be a roundoff of zero
3233 * to show that someone has already taken care of the
3234 * roundoff from the previous sync.
3235 */
3236 atomic_inc(&iclog->ic_refcnt);
3237 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
3238 xlog_state_switch_iclogs(log, iclog, 0);
e6b96570 3239 if (xlog_state_release_iclog(log, iclog))
df732b29 3240 goto out_error;
1da177e4 3241
81e5b50a 3242 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn)
e6b96570
CH
3243 goto out_unlock;
3244 } else {
3245 /*
3246 * Someone else is writing to this iclog.
3247 *
3248 * Use its call to flush out the data. However, the
3249 * other thread may not force out this LR, so we mark
3250 * it WANT_SYNC.
3251 */
3252 xlog_state_switch_iclogs(log, iclog, 0);
1da177e4 3253 }
e6b96570 3254 } else {
1da177e4 3255 /*
e6b96570
CH
3256 * If the head iclog is not active nor dirty, we just attach
3257 * ourselves to the head and go to sleep if necessary.
1da177e4 3258 */
e6b96570 3259 ;
1da177e4 3260 }
e6b96570 3261
81e5b50a
CH
3262 if (flags & XFS_LOG_SYNC)
3263 return xlog_wait_on_iclog(iclog);
e6b96570
CH
3264out_unlock:
3265 spin_unlock(&log->l_icloglock);
3266 return 0;
3267out_error:
3268 spin_unlock(&log->l_icloglock);
3269 return -EIO;
a14a348b 3270}
1da177e4 3271
3e4da466
CH
3272static int
3273__xfs_log_force_lsn(
a14a348b
CH
3274 struct xfs_mount *mp,
3275 xfs_lsn_t lsn,
3276 uint flags,
3e4da466
CH
3277 int *log_flushed,
3278 bool already_slept)
1da177e4 3279{
ad223e60 3280 struct xlog *log = mp->m_log;
a14a348b 3281 struct xlog_in_core *iclog;
71e330b5 3282
a14a348b
CH
3283 spin_lock(&log->l_icloglock);
3284 iclog = log->l_iclog;
1858bb0b 3285 if (iclog->ic_state == XLOG_STATE_IOERROR)
93806299 3286 goto out_error;
1da177e4 3287
93806299
CH
3288 while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3289 iclog = iclog->ic_next;
3290 if (iclog == log->l_iclog)
3291 goto out_unlock;
3292 }
a14a348b 3293
93806299
CH
3294 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3295 /*
3296 * We sleep here if we haven't already slept (e.g. this is the
3297 * first time we've looked at the correct iclog buf) and the
3298 * buffer before us is going to be sync'ed. The reason for this
3299 * is that if we are doing sync transactions here, by waiting
3300 * for the previous I/O to complete, we can allow a few more
3301 * transactions into this iclog before we close it down.
3302 *
3303 * Otherwise, we mark the buffer WANT_SYNC, and bump up the
3304 * refcnt so we can release the log (which drops the ref count).
3305 * The state switch keeps new transaction commits from using
3306 * this buffer. When the current commits finish writing into
3307 * the buffer, the refcount will drop to zero and the buffer
3308 * will go out then.
3309 */
3310 if (!already_slept &&
1858bb0b
CH
3311 (iclog->ic_prev->ic_state == XLOG_STATE_WANT_SYNC ||
3312 iclog->ic_prev->ic_state == XLOG_STATE_SYNCING)) {
93806299 3313 XFS_STATS_INC(mp, xs_log_force_sleep);
a14a348b 3314
93806299
CH
3315 xlog_wait(&iclog->ic_prev->ic_write_wait,
3316 &log->l_icloglock);
3e4da466 3317 return -EAGAIN;
1da177e4 3318 }
93806299
CH
3319 atomic_inc(&iclog->ic_refcnt);
3320 xlog_state_switch_iclogs(log, iclog, 0);
93806299 3321 if (xlog_state_release_iclog(log, iclog))
df732b29 3322 goto out_error;
93806299
CH
3323 if (log_flushed)
3324 *log_flushed = 1;
93806299 3325 }
1da177e4 3326
81e5b50a
CH
3327 if (flags & XFS_LOG_SYNC)
3328 return xlog_wait_on_iclog(iclog);
93806299 3329out_unlock:
a14a348b
CH
3330 spin_unlock(&log->l_icloglock);
3331 return 0;
93806299
CH
3332out_error:
3333 spin_unlock(&log->l_icloglock);
3334 return -EIO;
a14a348b
CH
3335}
3336
3e4da466
CH
3337/*
3338 * Force the in-core log to disk for a specific LSN.
3339 *
3340 * Find in-core log with lsn.
3341 * If it is in the DIRTY state, just return.
3342 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3343 * state and go to sleep or return.
3344 * If it is in any other state, go to sleep or return.
3345 *
3346 * Synchronous forces are implemented with a wait queue. All callers trying
3347 * to force a given lsn to disk must wait on the queue attached to the
3348 * specific in-core log. When given in-core log finally completes its write
3349 * to disk, that thread will wake up all threads waiting on the queue.
3350 */
3351int
3352xfs_log_force_lsn(
3353 struct xfs_mount *mp,
3354 xfs_lsn_t lsn,
3355 uint flags,
3356 int *log_flushed)
3357{
3358 int ret;
3359 ASSERT(lsn != 0);
3360
3361 XFS_STATS_INC(mp, xs_log_force);
3362 trace_xfs_log_force(mp, lsn, _RET_IP_);
3363
3364 lsn = xlog_cil_force_lsn(mp->m_log, lsn);
3365 if (lsn == NULLCOMMITLSN)
3366 return 0;
3367
3368 ret = __xfs_log_force_lsn(mp, lsn, flags, log_flushed, false);
3369 if (ret == -EAGAIN)
3370 ret = __xfs_log_force_lsn(mp, lsn, flags, log_flushed, true);
3371 return ret;
3372}
3373
1da177e4
LT
3374/*****************************************************************************
3375 *
3376 * TICKET functions
3377 *
3378 *****************************************************************************
3379 */
3380
3381/*
9da096fd 3382 * Free a used ticket when its refcount falls to zero.
1da177e4 3383 */
cc09c0dc
DC
3384void
3385xfs_log_ticket_put(
3386 xlog_ticket_t *ticket)
1da177e4 3387{
cc09c0dc 3388 ASSERT(atomic_read(&ticket->t_ref) > 0);
eb40a875 3389 if (atomic_dec_and_test(&ticket->t_ref))
377bcd5f 3390 kmem_cache_free(xfs_log_ticket_zone, ticket);
cc09c0dc 3391}
1da177e4 3392
cc09c0dc
DC
3393xlog_ticket_t *
3394xfs_log_ticket_get(
3395 xlog_ticket_t *ticket)
3396{
3397 ASSERT(atomic_read(&ticket->t_ref) > 0);
3398 atomic_inc(&ticket->t_ref);
3399 return ticket;
3400}
1da177e4
LT
3401
3402/*
e773fc93
JL
3403 * Figure out the total log space unit (in bytes) that would be
3404 * required for a log ticket.
1da177e4 3405 */
e773fc93
JL
3406int
3407xfs_log_calc_unit_res(
3408 struct xfs_mount *mp,
3409 int unit_bytes)
1da177e4 3410{
e773fc93
JL
3411 struct xlog *log = mp->m_log;
3412 int iclog_space;
3413 uint num_headers;
1da177e4
LT
3414
3415 /*
3416 * Permanent reservations have up to 'cnt'-1 active log operations
3417 * in the log. A unit in this case is the amount of space for one
3418 * of these log operations. Normal reservations have a cnt of 1
3419 * and their unit amount is the total amount of space required.
3420 *
3421 * The following lines of code account for non-transaction data
32fb9b57
TS
3422 * which occupy space in the on-disk log.
3423 *
3424 * Normal form of a transaction is:
3425 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3426 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3427 *
3428 * We need to account for all the leadup data and trailer data
3429 * around the transaction data.
3430 * And then we need to account for the worst case in terms of using
3431 * more space.
3432 * The worst case will happen if:
3433 * - the placement of the transaction happens to be such that the
3434 * roundoff is at its maximum
3435 * - the transaction data is synced before the commit record is synced
3436 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3437 * Therefore the commit record is in its own Log Record.
3438 * This can happen as the commit record is called with its
3439 * own region to xlog_write().
3440 * This then means that in the worst case, roundoff can happen for
3441 * the commit-rec as well.
3442 * The commit-rec is smaller than padding in this scenario and so it is
3443 * not added separately.
1da177e4
LT
3444 */
3445
32fb9b57
TS
3446 /* for trans header */
3447 unit_bytes += sizeof(xlog_op_header_t);
3448 unit_bytes += sizeof(xfs_trans_header_t);
3449
1da177e4 3450 /* for start-rec */
32fb9b57
TS
3451 unit_bytes += sizeof(xlog_op_header_t);
3452
9b9fc2b7
DC
3453 /*
3454 * for LR headers - the space for data in an iclog is the size minus
3455 * the space used for the headers. If we use the iclog size, then we
3456 * undercalculate the number of headers required.
3457 *
3458 * Furthermore - the addition of op headers for split-recs might
3459 * increase the space required enough to require more log and op
3460 * headers, so take that into account too.
3461 *
3462 * IMPORTANT: This reservation makes the assumption that if this
3463 * transaction is the first in an iclog and hence has the LR headers
3464 * accounted to it, then the remaining space in the iclog is
3465 * exclusively for this transaction. i.e. if the transaction is larger
3466 * than the iclog, it will be the only thing in that iclog.
3467 * Fundamentally, this means we must pass the entire log vector to
3468 * xlog_write to guarantee this.
3469 */
3470 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3471 num_headers = howmany(unit_bytes, iclog_space);
3472
3473 /* for split-recs - ophdrs added when data split over LRs */
3474 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3475
3476 /* add extra header reservations if we overrun */
3477 while (!num_headers ||
3478 howmany(unit_bytes, iclog_space) > num_headers) {
3479 unit_bytes += sizeof(xlog_op_header_t);
3480 num_headers++;
3481 }
32fb9b57 3482 unit_bytes += log->l_iclog_hsize * num_headers;
1da177e4 3483
32fb9b57
TS
3484 /* for commit-rec LR header - note: padding will subsume the ophdr */
3485 unit_bytes += log->l_iclog_hsize;
3486
32fb9b57 3487 /* for roundoff padding for transaction data and one for commit record */
e773fc93 3488 if (xfs_sb_version_haslogv2(&mp->m_sb) && mp->m_sb.sb_logsunit > 1) {
1da177e4 3489 /* log su roundoff */
e773fc93 3490 unit_bytes += 2 * mp->m_sb.sb_logsunit;
1da177e4
LT
3491 } else {
3492 /* BB roundoff */
e773fc93 3493 unit_bytes += 2 * BBSIZE;
1da177e4
LT
3494 }
3495
e773fc93
JL
3496 return unit_bytes;
3497}
3498
3499/*
3500 * Allocate and initialise a new log ticket.
3501 */
3502struct xlog_ticket *
3503xlog_ticket_alloc(
3504 struct xlog *log,
3505 int unit_bytes,
3506 int cnt,
3507 char client,
3508 bool permanent,
3509 xfs_km_flags_t alloc_flags)
3510{
3511 struct xlog_ticket *tic;
3512 int unit_res;
3513
3514 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
3515 if (!tic)
3516 return NULL;
3517
3518 unit_res = xfs_log_calc_unit_res(log->l_mp, unit_bytes);
3519
cc09c0dc 3520 atomic_set(&tic->t_ref, 1);
14a7235f 3521 tic->t_task = current;
10547941 3522 INIT_LIST_HEAD(&tic->t_queue);
e773fc93
JL
3523 tic->t_unit_res = unit_res;
3524 tic->t_curr_res = unit_res;
1da177e4
LT
3525 tic->t_cnt = cnt;
3526 tic->t_ocnt = cnt;
ecb3403d 3527 tic->t_tid = prandom_u32();
1da177e4
LT
3528 tic->t_clientid = client;
3529 tic->t_flags = XLOG_TIC_INITED;
9006fb91 3530 if (permanent)
1da177e4 3531 tic->t_flags |= XLOG_TIC_PERM_RESERV;
1da177e4 3532
0adba536 3533 xlog_tic_reset_res(tic);
7e9c6396 3534
1da177e4 3535 return tic;
cc09c0dc 3536}
1da177e4
LT
3537
3538
3539/******************************************************************************
3540 *
3541 * Log debug routines
3542 *
3543 ******************************************************************************
3544 */
cfcbbbd0 3545#if defined(DEBUG)
1da177e4
LT
3546/*
3547 * Make sure that the destination ptr is within the valid data region of
3548 * one of the iclogs. This uses backup pointers stored in a different
3549 * part of the log in case we trash the log structure.
3550 */
181fdfe6 3551STATIC void
e6b1f273 3552xlog_verify_dest_ptr(
ad223e60 3553 struct xlog *log,
5809d5e0 3554 void *ptr)
1da177e4
LT
3555{
3556 int i;
3557 int good_ptr = 0;
3558
e6b1f273
CH
3559 for (i = 0; i < log->l_iclog_bufs; i++) {
3560 if (ptr >= log->l_iclog_bak[i] &&
3561 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
1da177e4
LT
3562 good_ptr++;
3563 }
e6b1f273
CH
3564
3565 if (!good_ptr)
a0fa2b67 3566 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
e6b1f273 3567}
1da177e4 3568
da8a1a4a
DC
3569/*
3570 * Check to make sure the grant write head didn't just over lap the tail. If
3571 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3572 * the cycles differ by exactly one and check the byte count.
3573 *
3574 * This check is run unlocked, so can give false positives. Rather than assert
3575 * on failures, use a warn-once flag and a panic tag to allow the admin to
3576 * determine if they want to panic the machine when such an error occurs. For
3577 * debug kernels this will have the same effect as using an assert but, unlinke
3578 * an assert, it can be turned off at runtime.
3579 */
3f336c6f
DC
3580STATIC void
3581xlog_verify_grant_tail(
ad223e60 3582 struct xlog *log)
3f336c6f 3583{
1c3cb9ec 3584 int tail_cycle, tail_blocks;
a69ed03c 3585 int cycle, space;
3f336c6f 3586
28496968 3587 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
1c3cb9ec
DC
3588 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3589 if (tail_cycle != cycle) {
da8a1a4a
DC
3590 if (cycle - 1 != tail_cycle &&
3591 !(log->l_flags & XLOG_TAIL_WARN)) {
3592 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3593 "%s: cycle - 1 != tail_cycle", __func__);
3594 log->l_flags |= XLOG_TAIL_WARN;
3595 }
3596
3597 if (space > BBTOB(tail_blocks) &&
3598 !(log->l_flags & XLOG_TAIL_WARN)) {
3599 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3600 "%s: space > BBTOB(tail_blocks)", __func__);
3601 log->l_flags |= XLOG_TAIL_WARN;
3602 }
3f336c6f
DC
3603 }
3604}
3605
1da177e4
LT
3606/* check if it will fit */
3607STATIC void
9a8d2fdb
MT
3608xlog_verify_tail_lsn(
3609 struct xlog *log,
3610 struct xlog_in_core *iclog,
3611 xfs_lsn_t tail_lsn)
1da177e4
LT
3612{
3613 int blocks;
3614
3615 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3616 blocks =
3617 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3618 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
a0fa2b67 3619 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
1da177e4
LT
3620 } else {
3621 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3622
3623 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
a0fa2b67 3624 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
1da177e4
LT
3625
3626 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3627 if (blocks < BTOBB(iclog->ic_offset) + 1)
a0fa2b67 3628 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
1da177e4
LT
3629 }
3630} /* xlog_verify_tail_lsn */
3631
3632/*
3633 * Perform a number of checks on the iclog before writing to disk.
3634 *
3635 * 1. Make sure the iclogs are still circular
3636 * 2. Make sure we have a good magic number
3637 * 3. Make sure we don't have magic numbers in the data
3638 * 4. Check fields of each log operation header for:
3639 * A. Valid client identifier
3640 * B. tid ptr value falls in valid ptr space (user space code)
3641 * C. Length in log record header is correct according to the
3642 * individual operation headers within record.
3643 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3644 * log, check the preceding blocks of the physical log to make sure all
3645 * the cycle numbers agree with the current cycle number.
3646 */
3647STATIC void
9a8d2fdb
MT
3648xlog_verify_iclog(
3649 struct xlog *log,
3650 struct xlog_in_core *iclog,
abca1f33 3651 int count)
1da177e4
LT
3652{
3653 xlog_op_header_t *ophead;
3654 xlog_in_core_t *icptr;
3655 xlog_in_core_2_t *xhdr;
5809d5e0 3656 void *base_ptr, *ptr, *p;
db9d67d6 3657 ptrdiff_t field_offset;
c8ce540d 3658 uint8_t clientid;
1da177e4
LT
3659 int len, i, j, k, op_len;
3660 int idx;
1da177e4
LT
3661
3662 /* check validity of iclog pointers */
b22cd72c 3663 spin_lock(&log->l_icloglock);
1da177e4 3664 icptr = log->l_iclog;
643f7c4e
GB
3665 for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next)
3666 ASSERT(icptr);
3667
1da177e4 3668 if (icptr != log->l_iclog)
a0fa2b67 3669 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
b22cd72c 3670 spin_unlock(&log->l_icloglock);
1da177e4
LT
3671
3672 /* check log magic numbers */
69ef921b 3673 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
a0fa2b67 3674 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
1da177e4 3675
5809d5e0
CH
3676 base_ptr = ptr = &iclog->ic_header;
3677 p = &iclog->ic_header;
3678 for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) {
69ef921b 3679 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
a0fa2b67
DC
3680 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3681 __func__);
1da177e4
LT
3682 }
3683
3684 /* check fields */
b53e675d 3685 len = be32_to_cpu(iclog->ic_header.h_num_logops);
5809d5e0
CH
3686 base_ptr = ptr = iclog->ic_datap;
3687 ophead = ptr;
b28708d6 3688 xhdr = iclog->ic_data;
1da177e4 3689 for (i = 0; i < len; i++) {
5809d5e0 3690 ophead = ptr;
1da177e4
LT
3691
3692 /* clientid is only 1 byte */
5809d5e0
CH
3693 p = &ophead->oh_clientid;
3694 field_offset = p - base_ptr;
abca1f33 3695 if (field_offset & 0x1ff) {
1da177e4
LT
3696 clientid = ophead->oh_clientid;
3697 } else {
b2a922cd 3698 idx = BTOBBT((char *)&ophead->oh_clientid - iclog->ic_datap);
1da177e4
LT
3699 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3700 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3701 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
03bea6fe
CH
3702 clientid = xlog_get_client_id(
3703 xhdr[j].hic_xheader.xh_cycle_data[k]);
1da177e4 3704 } else {
03bea6fe
CH
3705 clientid = xlog_get_client_id(
3706 iclog->ic_header.h_cycle_data[idx]);
1da177e4
LT
3707 }
3708 }
3709 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
a0fa2b67 3710 xfs_warn(log->l_mp,
c9690043 3711 "%s: invalid clientid %d op "PTR_FMT" offset 0x%lx",
a0fa2b67
DC
3712 __func__, clientid, ophead,
3713 (unsigned long)field_offset);
1da177e4
LT
3714
3715 /* check length */
5809d5e0
CH
3716 p = &ophead->oh_len;
3717 field_offset = p - base_ptr;
abca1f33 3718 if (field_offset & 0x1ff) {
67fcb7bf 3719 op_len = be32_to_cpu(ophead->oh_len);
1da177e4 3720 } else {
db9d67d6
CH
3721 idx = BTOBBT((uintptr_t)&ophead->oh_len -
3722 (uintptr_t)iclog->ic_datap);
1da177e4
LT
3723 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3724 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3725 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
b53e675d 3726 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
1da177e4 3727 } else {
b53e675d 3728 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
1da177e4
LT
3729 }
3730 }
3731 ptr += sizeof(xlog_op_header_t) + op_len;
3732 }
3733} /* xlog_verify_iclog */
cfcbbbd0 3734#endif
1da177e4
LT
3735
3736/*
b22cd72c 3737 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
1da177e4
LT
3738 */
3739STATIC int
3740xlog_state_ioerror(
9a8d2fdb 3741 struct xlog *log)
1da177e4
LT
3742{
3743 xlog_in_core_t *iclog, *ic;
3744
3745 iclog = log->l_iclog;
1858bb0b 3746 if (iclog->ic_state != XLOG_STATE_IOERROR) {
1da177e4
LT
3747 /*
3748 * Mark all the incore logs IOERROR.
3749 * From now on, no log flushes will result.
3750 */
3751 ic = iclog;
3752 do {
3753 ic->ic_state = XLOG_STATE_IOERROR;
3754 ic = ic->ic_next;
3755 } while (ic != iclog);
014c2544 3756 return 0;
1da177e4
LT
3757 }
3758 /*
3759 * Return non-zero, if state transition has already happened.
3760 */
014c2544 3761 return 1;
1da177e4
LT
3762}
3763
3764/*
3765 * This is called from xfs_force_shutdown, when we're forcibly
3766 * shutting down the filesystem, typically because of an IO error.
3767 * Our main objectives here are to make sure that:
a870fe6d
DC
3768 * a. if !logerror, flush the logs to disk. Anything modified
3769 * after this is ignored.
3770 * b. the filesystem gets marked 'SHUTDOWN' for all interested
1da177e4 3771 * parties to find out, 'atomically'.
a870fe6d 3772 * c. those who're sleeping on log reservations, pinned objects and
1da177e4 3773 * other resources get woken up, and be told the bad news.
a870fe6d 3774 * d. nothing new gets queued up after (b) and (c) are done.
9da1ab18 3775 *
a870fe6d
DC
3776 * Note: for the !logerror case we need to flush the regions held in memory out
3777 * to disk first. This needs to be done before the log is marked as shutdown,
3778 * otherwise the iclog writes will fail.
1da177e4
LT
3779 */
3780int
3781xfs_log_force_umount(
3782 struct xfs_mount *mp,
3783 int logerror)
3784{
9a8d2fdb 3785 struct xlog *log;
1da177e4 3786 int retval;
1da177e4
LT
3787
3788 log = mp->m_log;
3789
3790 /*
3791 * If this happens during log recovery, don't worry about
3792 * locking; the log isn't open for business yet.
3793 */
3794 if (!log ||
3795 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3796 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
bac8dca9 3797 if (mp->m_sb_bp)
b0388bf1 3798 mp->m_sb_bp->b_flags |= XBF_DONE;
014c2544 3799 return 0;
1da177e4
LT
3800 }
3801
3802 /*
3803 * Somebody could've already done the hard work for us.
3804 * No need to get locks for this.
3805 */
1858bb0b 3806 if (logerror && log->l_iclog->ic_state == XLOG_STATE_IOERROR) {
1da177e4 3807 ASSERT(XLOG_FORCED_SHUTDOWN(log));
014c2544 3808 return 1;
1da177e4 3809 }
9da1ab18
DC
3810
3811 /*
a870fe6d
DC
3812 * Flush all the completed transactions to disk before marking the log
3813 * being shut down. We need to do it in this order to ensure that
3814 * completed operations are safely on disk before we shut down, and that
3815 * we don't have to issue any buffer IO after the shutdown flags are set
3816 * to guarantee this.
9da1ab18 3817 */
93b8a585 3818 if (!logerror)
60e5bb78 3819 xfs_log_force(mp, XFS_LOG_SYNC);
9da1ab18 3820
1da177e4 3821 /*
3f16b985
DC
3822 * mark the filesystem and the as in a shutdown state and wake
3823 * everybody up to tell them the bad news.
1da177e4 3824 */
b22cd72c 3825 spin_lock(&log->l_icloglock);
1da177e4 3826 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
bac8dca9 3827 if (mp->m_sb_bp)
b0388bf1 3828 mp->m_sb_bp->b_flags |= XBF_DONE;
bac8dca9 3829
1da177e4 3830 /*
a870fe6d
DC
3831 * Mark the log and the iclogs with IO error flags to prevent any
3832 * further log IO from being issued or completed.
1da177e4
LT
3833 */
3834 log->l_flags |= XLOG_IO_ERROR;
a870fe6d 3835 retval = xlog_state_ioerror(log);
b22cd72c 3836 spin_unlock(&log->l_icloglock);
1da177e4
LT
3837
3838 /*
10547941
DC
3839 * We don't want anybody waiting for log reservations after this. That
3840 * means we have to wake up everybody queued up on reserveq as well as
3841 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3842 * we don't enqueue anything once the SHUTDOWN flag is set, and this
3f16b985 3843 * action is protected by the grant locks.
1da177e4 3844 */
a79bf2d7
CH
3845 xlog_grant_head_wake_all(&log->l_reserve_head);
3846 xlog_grant_head_wake_all(&log->l_write_head);
1da177e4 3847
1da177e4 3848 /*
ac983517
DC
3849 * Wake up everybody waiting on xfs_log_force. Wake the CIL push first
3850 * as if the log writes were completed. The abort handling in the log
3851 * item committed callback functions will do this again under lock to
3852 * avoid races.
1da177e4 3853 */
cdea5459 3854 spin_lock(&log->l_cilp->xc_push_lock);
ac983517 3855 wake_up_all(&log->l_cilp->xc_commit_wait);
cdea5459 3856 spin_unlock(&log->l_cilp->xc_push_lock);
12e6a0f4 3857 xlog_state_do_callback(log);
1da177e4 3858
1da177e4 3859 /* return non-zero if log IOERROR transition had already happened */
014c2544 3860 return retval;
1da177e4
LT
3861}
3862
ba0f32d4 3863STATIC int
9a8d2fdb
MT
3864xlog_iclogs_empty(
3865 struct xlog *log)
1da177e4
LT
3866{
3867 xlog_in_core_t *iclog;
3868
3869 iclog = log->l_iclog;
3870 do {
3871 /* endianness does not matter here, zero is zero in
3872 * any language.
3873 */
3874 if (iclog->ic_header.h_num_logops)
014c2544 3875 return 0;
1da177e4
LT
3876 iclog = iclog->ic_next;
3877 } while (iclog != log->l_iclog);
014c2544 3878 return 1;
1da177e4 3879}
f661f1e0 3880
a45086e2
BF
3881/*
3882 * Verify that an LSN stamped into a piece of metadata is valid. This is
3883 * intended for use in read verifiers on v5 superblocks.
3884 */
3885bool
3886xfs_log_check_lsn(
3887 struct xfs_mount *mp,
3888 xfs_lsn_t lsn)
3889{
3890 struct xlog *log = mp->m_log;
3891 bool valid;
3892
3893 /*
3894 * norecovery mode skips mount-time log processing and unconditionally
3895 * resets the in-core LSN. We can't validate in this mode, but
3896 * modifications are not allowed anyways so just return true.
3897 */
3898 if (mp->m_flags & XFS_MOUNT_NORECOVERY)
3899 return true;
3900
3901 /*
3902 * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is
3903 * handled by recovery and thus safe to ignore here.
3904 */
3905 if (lsn == NULLCOMMITLSN)
3906 return true;
3907
3908 valid = xlog_valid_lsn(mp->m_log, lsn);
3909
3910 /* warn the user about what's gone wrong before verifier failure */
3911 if (!valid) {
3912 spin_lock(&log->l_icloglock);
3913 xfs_warn(mp,
3914"Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). "
3915"Please unmount and run xfs_repair (>= v4.3) to resolve.",
3916 CYCLE_LSN(lsn), BLOCK_LSN(lsn),
3917 log->l_curr_cycle, log->l_curr_block);
3918 spin_unlock(&log->l_icloglock);
3919 }
3920
3921 return valid;
3922}
0c60d3aa
DW
3923
3924bool
3925xfs_log_in_recovery(
3926 struct xfs_mount *mp)
3927{
3928 struct xlog *log = mp->m_log;
3929
3930 return log->l_flags & XLOG_ACTIVE_RECOVERY;
3931}