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