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