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