10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott * Copyright (c) 2000-2005 Silicon Graphics, Inc.
47b718769SNathan Scott * All Rights Reserved.
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds #include "xfs.h"
7a844f451SNathan Scott #include "xfs_fs.h"
870a9883cSDave Chinner #include "xfs_shared.h"
9a4fbe6abSDave Chinner #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
121da177e4SLinus Torvalds #include "xfs_mount.h"
13e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
141da177e4SLinus Torvalds #include "xfs_error.h"
15239880efSDave Chinner #include "xfs_trans.h"
16239880efSDave Chinner #include "xfs_trans_priv.h"
17239880efSDave Chinner #include "xfs_log.h"
181da177e4SLinus Torvalds #include "xfs_log_priv.h"
190b1b213fSChristoph Hellwig #include "xfs_trace.h"
20baff4e44SBrian Foster #include "xfs_sysfs.h"
2161e63ecbSDave Chinner #include "xfs_sb.h"
2239353ff6SDarrick J. Wong #include "xfs_health.h"
231da177e4SLinus Torvalds
24182696fbSDarrick J. Wong struct kmem_cache *xfs_log_ticket_cache;
251da177e4SLinus Torvalds
261da177e4SLinus Torvalds /* Local miscellaneous function prototypes */
279a8d2fdbSMark Tinguely STATIC struct xlog *
289a8d2fdbSMark Tinguely xlog_alloc_log(
299a8d2fdbSMark Tinguely struct xfs_mount *mp,
309a8d2fdbSMark Tinguely struct xfs_buftarg *log_target,
311da177e4SLinus Torvalds xfs_daddr_t blk_offset,
321da177e4SLinus Torvalds int num_bblks);
33ad223e60SMark Tinguely STATIC int
34ad223e60SMark Tinguely xlog_space_left(
35ad223e60SMark Tinguely struct xlog *log,
36ad223e60SMark Tinguely atomic64_t *head);
379a8d2fdbSMark Tinguely STATIC void
389a8d2fdbSMark Tinguely xlog_dealloc_log(
399a8d2fdbSMark Tinguely struct xlog *log);
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds /* local state machine functions */
42d15cbf2fSChristoph Hellwig STATIC void xlog_state_done_syncing(
4312e6a0f4SChristoph Hellwig struct xlog_in_core *iclog);
44502a01faSDave Chinner STATIC void xlog_state_do_callback(
45502a01faSDave Chinner struct xlog *log);
469a8d2fdbSMark Tinguely STATIC int
479a8d2fdbSMark Tinguely xlog_state_get_iclog_space(
489a8d2fdbSMark Tinguely struct xlog *log,
491da177e4SLinus Torvalds int len,
509a8d2fdbSMark Tinguely struct xlog_in_core **iclog,
519a8d2fdbSMark Tinguely struct xlog_ticket *ticket,
521da177e4SLinus Torvalds int *logoffsetp);
539a8d2fdbSMark Tinguely STATIC void
54ad223e60SMark Tinguely xlog_grant_push_ail(
55ad223e60SMark Tinguely struct xlog *log,
561da177e4SLinus Torvalds int need_bytes);
579a8d2fdbSMark Tinguely STATIC void
58df732b29SChristoph Hellwig xlog_sync(
59df732b29SChristoph Hellwig struct xlog *log,
60d9f68777SDave Chinner struct xlog_in_core *iclog,
61d9f68777SDave Chinner struct xlog_ticket *ticket);
62cfcbbbd0SNathan Scott #if defined(DEBUG)
639a8d2fdbSMark Tinguely STATIC void
64ad223e60SMark Tinguely xlog_verify_grant_tail(
65ad223e60SMark Tinguely struct xlog *log);
669a8d2fdbSMark Tinguely STATIC void
679a8d2fdbSMark Tinguely xlog_verify_iclog(
689a8d2fdbSMark Tinguely struct xlog *log,
699a8d2fdbSMark Tinguely struct xlog_in_core *iclog,
70abca1f33SChristoph Hellwig int count);
719a8d2fdbSMark Tinguely STATIC void
729a8d2fdbSMark Tinguely xlog_verify_tail_lsn(
739a8d2fdbSMark Tinguely struct xlog *log,
749d110014SDave Chinner struct xlog_in_core *iclog);
751da177e4SLinus Torvalds #else
763f336c6fSDave Chinner #define xlog_verify_grant_tail(a)
77abca1f33SChristoph Hellwig #define xlog_verify_iclog(a,b,c)
789d110014SDave Chinner #define xlog_verify_tail_lsn(a,b)
791da177e4SLinus Torvalds #endif
801da177e4SLinus Torvalds
819a8d2fdbSMark Tinguely STATIC int
829a8d2fdbSMark Tinguely xlog_iclogs_empty(
839a8d2fdbSMark Tinguely struct xlog *log);
841da177e4SLinus Torvalds
85303591a0SBrian Foster static int
86303591a0SBrian Foster xfs_log_cover(struct xfs_mount *);
87303591a0SBrian Foster
888d547cf9SDave Chinner /*
898d547cf9SDave Chinner * We need to make sure the buffer pointer returned is naturally aligned for the
908d547cf9SDave Chinner * biggest basic data type we put into it. We have already accounted for this
918d547cf9SDave Chinner * padding when sizing the buffer.
928d547cf9SDave Chinner *
938d547cf9SDave Chinner * However, this padding does not get written into the log, and hence we have to
948d547cf9SDave Chinner * track the space used by the log vectors separately to prevent log space hangs
958d547cf9SDave Chinner * due to inaccurate accounting (i.e. a leak) of the used log space through the
968d547cf9SDave Chinner * CIL context ticket.
978d547cf9SDave Chinner *
988d547cf9SDave Chinner * We also add space for the xlog_op_header that describes this region in the
998d547cf9SDave Chinner * log. This prepends the data region we return to the caller to copy their data
1008d547cf9SDave Chinner * into, so do all the static initialisation of the ophdr now. Because the ophdr
1018d547cf9SDave Chinner * is not 8 byte aligned, we have to be careful to ensure that we align the
1028d547cf9SDave Chinner * start of the buffer such that the region we return to the call is 8 byte
1038d547cf9SDave Chinner * aligned and packed against the tail of the ophdr.
1048d547cf9SDave Chinner */
1058d547cf9SDave Chinner void *
xlog_prepare_iovec(struct xfs_log_vec * lv,struct xfs_log_iovec ** vecp,uint type)1068d547cf9SDave Chinner xlog_prepare_iovec(
1078d547cf9SDave Chinner struct xfs_log_vec *lv,
1088d547cf9SDave Chinner struct xfs_log_iovec **vecp,
1098d547cf9SDave Chinner uint type)
1108d547cf9SDave Chinner {
1118d547cf9SDave Chinner struct xfs_log_iovec *vec = *vecp;
1128d547cf9SDave Chinner struct xlog_op_header *oph;
1138d547cf9SDave Chinner uint32_t len;
1148d547cf9SDave Chinner void *buf;
1158d547cf9SDave Chinner
1168d547cf9SDave Chinner if (vec) {
1178d547cf9SDave Chinner ASSERT(vec - lv->lv_iovecp < lv->lv_niovecs);
1188d547cf9SDave Chinner vec++;
1198d547cf9SDave Chinner } else {
1208d547cf9SDave Chinner vec = &lv->lv_iovecp[0];
1218d547cf9SDave Chinner }
1228d547cf9SDave Chinner
1238d547cf9SDave Chinner len = lv->lv_buf_len + sizeof(struct xlog_op_header);
1248d547cf9SDave Chinner if (!IS_ALIGNED(len, sizeof(uint64_t))) {
1258d547cf9SDave Chinner lv->lv_buf_len = round_up(len, sizeof(uint64_t)) -
1268d547cf9SDave Chinner sizeof(struct xlog_op_header);
1278d547cf9SDave Chinner }
1288d547cf9SDave Chinner
1298d547cf9SDave Chinner vec->i_type = type;
1308d547cf9SDave Chinner vec->i_addr = lv->lv_buf + lv->lv_buf_len;
1318d547cf9SDave Chinner
1328d547cf9SDave Chinner oph = vec->i_addr;
1338d547cf9SDave Chinner oph->oh_clientid = XFS_TRANSACTION;
1348d547cf9SDave Chinner oph->oh_res2 = 0;
1358d547cf9SDave Chinner oph->oh_flags = 0;
1368d547cf9SDave Chinner
1378d547cf9SDave Chinner buf = vec->i_addr + sizeof(struct xlog_op_header);
1388d547cf9SDave Chinner ASSERT(IS_ALIGNED((unsigned long)buf, sizeof(uint64_t)));
1398d547cf9SDave Chinner
1408d547cf9SDave Chinner *vecp = vec;
1418d547cf9SDave Chinner return buf;
1428d547cf9SDave Chinner }
1438d547cf9SDave Chinner
144dd954c69SChristoph Hellwig static void
xlog_grant_sub_space(struct xlog * log,atomic64_t * head,int bytes)145663e496aSDave Chinner xlog_grant_sub_space(
146ad223e60SMark Tinguely struct xlog *log,
147c8a09ff8SDave Chinner atomic64_t *head,
148663e496aSDave Chinner int bytes)
149dd954c69SChristoph Hellwig {
150d0eb2f38SDave Chinner int64_t head_val = atomic64_read(head);
151d0eb2f38SDave Chinner int64_t new, old;
152d0eb2f38SDave Chinner
153d0eb2f38SDave Chinner do {
154a69ed03cSDave Chinner int cycle, space;
155a69ed03cSDave Chinner
156d0eb2f38SDave Chinner xlog_crack_grant_head_val(head_val, &cycle, &space);
157a69ed03cSDave Chinner
158a69ed03cSDave Chinner space -= bytes;
159a69ed03cSDave Chinner if (space < 0) {
160a69ed03cSDave Chinner space += log->l_logsize;
161a69ed03cSDave Chinner cycle--;
162dd954c69SChristoph Hellwig }
163a69ed03cSDave Chinner
164d0eb2f38SDave Chinner old = head_val;
165d0eb2f38SDave Chinner new = xlog_assign_grant_head_val(cycle, space);
166d0eb2f38SDave Chinner head_val = atomic64_cmpxchg(head, old, new);
167d0eb2f38SDave Chinner } while (head_val != old);
168dd954c69SChristoph Hellwig }
169dd954c69SChristoph Hellwig
170dd954c69SChristoph Hellwig static void
xlog_grant_add_space(struct xlog * log,atomic64_t * head,int bytes)171663e496aSDave Chinner xlog_grant_add_space(
172ad223e60SMark Tinguely struct xlog *log,
173c8a09ff8SDave Chinner atomic64_t *head,
174663e496aSDave Chinner int bytes)
175dd954c69SChristoph Hellwig {
176d0eb2f38SDave Chinner int64_t head_val = atomic64_read(head);
177d0eb2f38SDave Chinner int64_t new, old;
178d0eb2f38SDave Chinner
179d0eb2f38SDave Chinner do {
180a69ed03cSDave Chinner int tmp;
181a69ed03cSDave Chinner int cycle, space;
182a69ed03cSDave Chinner
183d0eb2f38SDave Chinner xlog_crack_grant_head_val(head_val, &cycle, &space);
184a69ed03cSDave Chinner
185a69ed03cSDave Chinner tmp = log->l_logsize - space;
186d729eae8SMichael Nishimoto if (tmp > bytes)
187a69ed03cSDave Chinner space += bytes;
188d729eae8SMichael Nishimoto else {
189a69ed03cSDave Chinner space = bytes - tmp;
190a69ed03cSDave Chinner cycle++;
191dd954c69SChristoph Hellwig }
192a69ed03cSDave Chinner
193d0eb2f38SDave Chinner old = head_val;
194d0eb2f38SDave Chinner new = xlog_assign_grant_head_val(cycle, space);
195d0eb2f38SDave Chinner head_val = atomic64_cmpxchg(head, old, new);
196d0eb2f38SDave Chinner } while (head_val != old);
197dd954c69SChristoph Hellwig }
198a69ed03cSDave Chinner
199c303c5b8SChristoph Hellwig STATIC void
xlog_grant_head_init(struct xlog_grant_head * head)200c303c5b8SChristoph Hellwig xlog_grant_head_init(
201c303c5b8SChristoph Hellwig struct xlog_grant_head *head)
202c303c5b8SChristoph Hellwig {
203c303c5b8SChristoph Hellwig xlog_assign_grant_head(&head->grant, 1, 0);
204c303c5b8SChristoph Hellwig INIT_LIST_HEAD(&head->waiters);
205c303c5b8SChristoph Hellwig spin_lock_init(&head->lock);
206c303c5b8SChristoph Hellwig }
207c303c5b8SChristoph Hellwig
208a79bf2d7SChristoph Hellwig STATIC void
xlog_grant_head_wake_all(struct xlog_grant_head * head)209a79bf2d7SChristoph Hellwig xlog_grant_head_wake_all(
210a79bf2d7SChristoph Hellwig struct xlog_grant_head *head)
211a79bf2d7SChristoph Hellwig {
212a79bf2d7SChristoph Hellwig struct xlog_ticket *tic;
213a79bf2d7SChristoph Hellwig
214a79bf2d7SChristoph Hellwig spin_lock(&head->lock);
215a79bf2d7SChristoph Hellwig list_for_each_entry(tic, &head->waiters, t_queue)
216a79bf2d7SChristoph Hellwig wake_up_process(tic->t_task);
217a79bf2d7SChristoph Hellwig spin_unlock(&head->lock);
218a79bf2d7SChristoph Hellwig }
219a79bf2d7SChristoph Hellwig
220e179840dSChristoph Hellwig static inline int
xlog_ticket_reservation(struct xlog * log,struct xlog_grant_head * head,struct xlog_ticket * tic)221e179840dSChristoph Hellwig xlog_ticket_reservation(
222ad223e60SMark Tinguely struct xlog *log,
223e179840dSChristoph Hellwig struct xlog_grant_head *head,
224e179840dSChristoph Hellwig struct xlog_ticket *tic)
2259f9c19ecSChristoph Hellwig {
226e179840dSChristoph Hellwig if (head == &log->l_write_head) {
2279f9c19ecSChristoph Hellwig ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
228e179840dSChristoph Hellwig return tic->t_unit_res;
22956171040SZeng Heng }
23056171040SZeng Heng
231e179840dSChristoph Hellwig if (tic->t_flags & XLOG_TIC_PERM_RESERV)
232e179840dSChristoph Hellwig return tic->t_unit_res * tic->t_cnt;
23356171040SZeng Heng
234e179840dSChristoph Hellwig return tic->t_unit_res;
235e179840dSChristoph Hellwig }
2369f9c19ecSChristoph Hellwig
237e179840dSChristoph Hellwig STATIC bool
xlog_grant_head_wake(struct xlog * log,struct xlog_grant_head * head,int * free_bytes)238e179840dSChristoph Hellwig xlog_grant_head_wake(
239ad223e60SMark Tinguely struct xlog *log,
240e179840dSChristoph Hellwig struct xlog_grant_head *head,
241e179840dSChristoph Hellwig int *free_bytes)
242e179840dSChristoph Hellwig {
243e179840dSChristoph Hellwig struct xlog_ticket *tic;
244e179840dSChristoph Hellwig int need_bytes;
2457c107afbSDave Chinner bool woken_task = false;
2469f9c19ecSChristoph Hellwig
247e179840dSChristoph Hellwig list_for_each_entry(tic, &head->waiters, t_queue) {
2487c107afbSDave Chinner
2497c107afbSDave Chinner /*
2507c107afbSDave Chinner * There is a chance that the size of the CIL checkpoints in
2517c107afbSDave Chinner * progress at the last AIL push target calculation resulted in
2527c107afbSDave Chinner * limiting the target to the log head (l_last_sync_lsn) at the
2537c107afbSDave Chinner * time. This may not reflect where the log head is now as the
2547c107afbSDave Chinner * CIL checkpoints may have completed.
2557c107afbSDave Chinner *
2567c107afbSDave Chinner * Hence when we are woken here, it may be that the head of the
2577c107afbSDave Chinner * log that has moved rather than the tail. As the tail didn't
2587c107afbSDave Chinner * move, there still won't be space available for the
2597c107afbSDave Chinner * reservation we require. However, if the AIL has already
2607c107afbSDave Chinner * pushed to the target defined by the old log head location, we
2617c107afbSDave Chinner * will hang here waiting for something else to update the AIL
2627c107afbSDave Chinner * push target.
2637c107afbSDave Chinner *
2647c107afbSDave Chinner * Therefore, if there isn't space to wake the first waiter on
2657c107afbSDave Chinner * the grant head, we need to push the AIL again to ensure the
2667c107afbSDave Chinner * target reflects both the current log tail and log head
2677c107afbSDave Chinner * position before we wait for the tail to move again.
2687c107afbSDave Chinner */
2697c107afbSDave Chinner
270e179840dSChristoph Hellwig need_bytes = xlog_ticket_reservation(log, head, tic);
2717c107afbSDave Chinner if (*free_bytes < need_bytes) {
2727c107afbSDave Chinner if (!woken_task)
2737c107afbSDave Chinner xlog_grant_push_ail(log, need_bytes);
2749f9c19ecSChristoph Hellwig return false;
2757c107afbSDave Chinner }
2769f9c19ecSChristoph Hellwig
277e179840dSChristoph Hellwig *free_bytes -= need_bytes;
278e179840dSChristoph Hellwig trace_xfs_log_grant_wake_up(log, tic);
27914a7235fSChristoph Hellwig wake_up_process(tic->t_task);
2807c107afbSDave Chinner woken_task = true;
2819f9c19ecSChristoph Hellwig }
2829f9c19ecSChristoph Hellwig
2839f9c19ecSChristoph Hellwig return true;
2849f9c19ecSChristoph Hellwig }
2859f9c19ecSChristoph Hellwig
2869f9c19ecSChristoph Hellwig STATIC int
xlog_grant_head_wait(struct xlog * log,struct xlog_grant_head * head,struct xlog_ticket * tic,int need_bytes)28723ee3df3SChristoph Hellwig xlog_grant_head_wait(
288ad223e60SMark Tinguely struct xlog *log,
28923ee3df3SChristoph Hellwig struct xlog_grant_head *head,
2909f9c19ecSChristoph Hellwig struct xlog_ticket *tic,
291a30b0367SDave Chinner int need_bytes) __releases(&head->lock)
292a30b0367SDave Chinner __acquires(&head->lock)
2939f9c19ecSChristoph Hellwig {
29423ee3df3SChristoph Hellwig list_add_tail(&tic->t_queue, &head->waiters);
2959f9c19ecSChristoph Hellwig
2969f9c19ecSChristoph Hellwig do {
2972039a272SDave Chinner if (xlog_is_shutdown(log))
2989f9c19ecSChristoph Hellwig goto shutdown;
2999f9c19ecSChristoph Hellwig xlog_grant_push_ail(log, need_bytes);
3009f9c19ecSChristoph Hellwig
30114a7235fSChristoph Hellwig __set_current_state(TASK_UNINTERRUPTIBLE);
30223ee3df3SChristoph Hellwig spin_unlock(&head->lock);
3039f9c19ecSChristoph Hellwig
304ff6d6af2SBill O'Donnell XFS_STATS_INC(log->l_mp, xs_sleep_logspace);
30514a7235fSChristoph Hellwig
30614a7235fSChristoph Hellwig trace_xfs_log_grant_sleep(log, tic);
30714a7235fSChristoph Hellwig schedule();
3089f9c19ecSChristoph Hellwig trace_xfs_log_grant_wake(log, tic);
3099f9c19ecSChristoph Hellwig
31023ee3df3SChristoph Hellwig spin_lock(&head->lock);
3112039a272SDave Chinner if (xlog_is_shutdown(log))
3129f9c19ecSChristoph Hellwig goto shutdown;
31323ee3df3SChristoph Hellwig } while (xlog_space_left(log, &head->grant) < need_bytes);
3149f9c19ecSChristoph Hellwig
3159f9c19ecSChristoph Hellwig list_del_init(&tic->t_queue);
3169f9c19ecSChristoph Hellwig return 0;
3179f9c19ecSChristoph Hellwig shutdown:
3189f9c19ecSChristoph Hellwig list_del_init(&tic->t_queue);
3192451337dSDave Chinner return -EIO;
3209f9c19ecSChristoph Hellwig }
3219f9c19ecSChristoph Hellwig
32242ceedb3SChristoph Hellwig /*
32342ceedb3SChristoph Hellwig * Atomically get the log space required for a log ticket.
32442ceedb3SChristoph Hellwig *
32542ceedb3SChristoph Hellwig * Once a ticket gets put onto head->waiters, it will only return after the
32642ceedb3SChristoph Hellwig * needed reservation is satisfied.
32742ceedb3SChristoph Hellwig *
32842ceedb3SChristoph Hellwig * This function is structured so that it has a lock free fast path. This is
32942ceedb3SChristoph Hellwig * necessary because every new transaction reservation will come through this
33042ceedb3SChristoph Hellwig * path. Hence any lock will be globally hot if we take it unconditionally on
33142ceedb3SChristoph Hellwig * every pass.
33242ceedb3SChristoph Hellwig *
33342ceedb3SChristoph Hellwig * As tickets are only ever moved on and off head->waiters under head->lock, we
33442ceedb3SChristoph Hellwig * only need to take that lock if we are going to add the ticket to the queue
33542ceedb3SChristoph Hellwig * and sleep. We can avoid taking the lock if the ticket was never added to
33642ceedb3SChristoph Hellwig * head->waiters because the t_queue list head will be empty and we hold the
33742ceedb3SChristoph Hellwig * only reference to it so it can safely be checked unlocked.
33842ceedb3SChristoph Hellwig */
33942ceedb3SChristoph Hellwig STATIC int
xlog_grant_head_check(struct xlog * log,struct xlog_grant_head * head,struct xlog_ticket * tic,int * need_bytes)34042ceedb3SChristoph Hellwig xlog_grant_head_check(
341ad223e60SMark Tinguely struct xlog *log,
34242ceedb3SChristoph Hellwig struct xlog_grant_head *head,
34342ceedb3SChristoph Hellwig struct xlog_ticket *tic,
34442ceedb3SChristoph Hellwig int *need_bytes)
34542ceedb3SChristoph Hellwig {
34642ceedb3SChristoph Hellwig int free_bytes;
34742ceedb3SChristoph Hellwig int error = 0;
34842ceedb3SChristoph Hellwig
349e1d06e5fSDave Chinner ASSERT(!xlog_in_recovery(log));
35042ceedb3SChristoph Hellwig
35142ceedb3SChristoph Hellwig /*
35242ceedb3SChristoph Hellwig * If there are other waiters on the queue then give them a chance at
35342ceedb3SChristoph Hellwig * logspace before us. Wake up the first waiters, if we do not wake
35442ceedb3SChristoph Hellwig * up all the waiters then go to sleep waiting for more free space,
35542ceedb3SChristoph Hellwig * otherwise try to get some space for this transaction.
35642ceedb3SChristoph Hellwig */
35742ceedb3SChristoph Hellwig *need_bytes = xlog_ticket_reservation(log, head, tic);
35842ceedb3SChristoph Hellwig free_bytes = xlog_space_left(log, &head->grant);
35942ceedb3SChristoph Hellwig if (!list_empty_careful(&head->waiters)) {
36042ceedb3SChristoph Hellwig spin_lock(&head->lock);
36142ceedb3SChristoph Hellwig if (!xlog_grant_head_wake(log, head, &free_bytes) ||
36242ceedb3SChristoph Hellwig free_bytes < *need_bytes) {
36342ceedb3SChristoph Hellwig error = xlog_grant_head_wait(log, head, tic,
36442ceedb3SChristoph Hellwig *need_bytes);
36542ceedb3SChristoph Hellwig }
36642ceedb3SChristoph Hellwig spin_unlock(&head->lock);
36742ceedb3SChristoph Hellwig } else if (free_bytes < *need_bytes) {
36842ceedb3SChristoph Hellwig spin_lock(&head->lock);
36942ceedb3SChristoph Hellwig error = xlog_grant_head_wait(log, head, tic, *need_bytes);
37042ceedb3SChristoph Hellwig spin_unlock(&head->lock);
37142ceedb3SChristoph Hellwig }
37242ceedb3SChristoph Hellwig
37342ceedb3SChristoph Hellwig return error;
37442ceedb3SChristoph Hellwig }
37542ceedb3SChristoph Hellwig
37650d25484SBrian Foster bool
xfs_log_writable(struct xfs_mount * mp)37750d25484SBrian Foster xfs_log_writable(
37850d25484SBrian Foster struct xfs_mount *mp)
37950d25484SBrian Foster {
38050d25484SBrian Foster /*
3818e9800f9SDarrick J. Wong * Do not write to the log on norecovery mounts, if the data or log
3828e9800f9SDarrick J. Wong * devices are read-only, or if the filesystem is shutdown. Read-only
3838e9800f9SDarrick J. Wong * mounts allow internal writes for log recovery and unmount purposes,
3848e9800f9SDarrick J. Wong * so don't restrict that case.
38550d25484SBrian Foster */
3860560f31aSDave Chinner if (xfs_has_norecovery(mp))
38750d25484SBrian Foster return false;
3888e9800f9SDarrick J. Wong if (xfs_readonly_buftarg(mp->m_ddev_targp))
3898e9800f9SDarrick J. Wong return false;
39050d25484SBrian Foster if (xfs_readonly_buftarg(mp->m_log->l_targ))
39150d25484SBrian Foster return false;
3922039a272SDave Chinner if (xlog_is_shutdown(mp->m_log))
39350d25484SBrian Foster return false;
39450d25484SBrian Foster return true;
39550d25484SBrian Foster }
39650d25484SBrian Foster
3971da177e4SLinus Torvalds /*
3989006fb91SChristoph Hellwig * Replenish the byte reservation required by moving the grant write head.
3999006fb91SChristoph Hellwig */
4009006fb91SChristoph Hellwig int
xfs_log_regrant(struct xfs_mount * mp,struct xlog_ticket * tic)4019006fb91SChristoph Hellwig xfs_log_regrant(
4029006fb91SChristoph Hellwig struct xfs_mount *mp,
4039006fb91SChristoph Hellwig struct xlog_ticket *tic)
4049006fb91SChristoph Hellwig {
405ad223e60SMark Tinguely struct xlog *log = mp->m_log;
4069006fb91SChristoph Hellwig int need_bytes;
4079006fb91SChristoph Hellwig int error = 0;
4089006fb91SChristoph Hellwig
4092039a272SDave Chinner if (xlog_is_shutdown(log))
4102451337dSDave Chinner return -EIO;
4119006fb91SChristoph Hellwig
412ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_try_logspace);
4139006fb91SChristoph Hellwig
4149006fb91SChristoph Hellwig /*
4159006fb91SChristoph Hellwig * This is a new transaction on the ticket, so we need to change the
4169006fb91SChristoph Hellwig * transaction ID so that the next transaction has a different TID in
4179006fb91SChristoph Hellwig * the log. Just add one to the existing tid so that we can see chains
4189006fb91SChristoph Hellwig * of rolling transactions in the log easily.
4199006fb91SChristoph Hellwig */
4209006fb91SChristoph Hellwig tic->t_tid++;
4219006fb91SChristoph Hellwig
4229006fb91SChristoph Hellwig xlog_grant_push_ail(log, tic->t_unit_res);
4239006fb91SChristoph Hellwig
4249006fb91SChristoph Hellwig tic->t_curr_res = tic->t_unit_res;
4259006fb91SChristoph Hellwig if (tic->t_cnt > 0)
4269006fb91SChristoph Hellwig return 0;
4279006fb91SChristoph Hellwig
4289006fb91SChristoph Hellwig trace_xfs_log_regrant(log, tic);
4299006fb91SChristoph Hellwig
4309006fb91SChristoph Hellwig error = xlog_grant_head_check(log, &log->l_write_head, tic,
4319006fb91SChristoph Hellwig &need_bytes);
4329006fb91SChristoph Hellwig if (error)
4339006fb91SChristoph Hellwig goto out_error;
4349006fb91SChristoph Hellwig
4359006fb91SChristoph Hellwig xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
4369006fb91SChristoph Hellwig trace_xfs_log_regrant_exit(log, tic);
4379006fb91SChristoph Hellwig xlog_verify_grant_tail(log);
4389006fb91SChristoph Hellwig return 0;
4399006fb91SChristoph Hellwig
4409006fb91SChristoph Hellwig out_error:
4419006fb91SChristoph Hellwig /*
4429006fb91SChristoph Hellwig * If we are failing, make sure the ticket doesn't have any current
4439006fb91SChristoph Hellwig * reservations. We don't want to add this back when the ticket/
4449006fb91SChristoph Hellwig * transaction gets cancelled.
4459006fb91SChristoph Hellwig */
4469006fb91SChristoph Hellwig tic->t_curr_res = 0;
4479006fb91SChristoph Hellwig tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
4489006fb91SChristoph Hellwig return error;
4499006fb91SChristoph Hellwig }
4509006fb91SChristoph Hellwig
4519006fb91SChristoph Hellwig /*
452a0e336baSHuang Chong * Reserve log space and return a ticket corresponding to the reservation.
4539006fb91SChristoph Hellwig *
4549006fb91SChristoph Hellwig * Each reservation is going to reserve extra space for a log record header.
4559006fb91SChristoph Hellwig * When writes happen to the on-disk log, we don't subtract the length of the
4569006fb91SChristoph Hellwig * log record header from any reservation. By wasting space in each
4579006fb91SChristoph Hellwig * reservation, we prevent over allocation problems.
4589006fb91SChristoph Hellwig */
4599006fb91SChristoph Hellwig int
xfs_log_reserve(struct xfs_mount * mp,int unit_bytes,int cnt,struct xlog_ticket ** ticp,bool permanent)4609006fb91SChristoph Hellwig xfs_log_reserve(
4619006fb91SChristoph Hellwig struct xfs_mount *mp,
4629006fb91SChristoph Hellwig int unit_bytes,
4639006fb91SChristoph Hellwig int cnt,
4649006fb91SChristoph Hellwig struct xlog_ticket **ticp,
465710b1e2cSChristoph Hellwig bool permanent)
4669006fb91SChristoph Hellwig {
467ad223e60SMark Tinguely struct xlog *log = mp->m_log;
4689006fb91SChristoph Hellwig struct xlog_ticket *tic;
4699006fb91SChristoph Hellwig int need_bytes;
4709006fb91SChristoph Hellwig int error = 0;
4719006fb91SChristoph Hellwig
4722039a272SDave Chinner if (xlog_is_shutdown(log))
4732451337dSDave Chinner return -EIO;
4749006fb91SChristoph Hellwig
475ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_try_logspace);
4769006fb91SChristoph Hellwig
4779006fb91SChristoph Hellwig ASSERT(*ticp == NULL);
478c7610dceSDave Chinner tic = xlog_ticket_alloc(log, unit_bytes, cnt, permanent);
4799006fb91SChristoph Hellwig *ticp = tic;
4809006fb91SChristoph Hellwig
481437a255aSDave Chinner xlog_grant_push_ail(log, tic->t_cnt ? tic->t_unit_res * tic->t_cnt
482437a255aSDave Chinner : tic->t_unit_res);
4839006fb91SChristoph Hellwig
4849006fb91SChristoph Hellwig trace_xfs_log_reserve(log, tic);
4859006fb91SChristoph Hellwig
4869006fb91SChristoph Hellwig error = xlog_grant_head_check(log, &log->l_reserve_head, tic,
4879006fb91SChristoph Hellwig &need_bytes);
4889006fb91SChristoph Hellwig if (error)
4899006fb91SChristoph Hellwig goto out_error;
4909006fb91SChristoph Hellwig
4919006fb91SChristoph Hellwig xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
4929006fb91SChristoph Hellwig xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
4939006fb91SChristoph Hellwig trace_xfs_log_reserve_exit(log, tic);
4949006fb91SChristoph Hellwig xlog_verify_grant_tail(log);
4959006fb91SChristoph Hellwig return 0;
4969006fb91SChristoph Hellwig
4979006fb91SChristoph Hellwig out_error:
4989006fb91SChristoph Hellwig /*
4999006fb91SChristoph Hellwig * If we are failing, make sure the ticket doesn't have any current
5009006fb91SChristoph Hellwig * reservations. We don't want to add this back when the ticket/
5019006fb91SChristoph Hellwig * transaction gets cancelled.
5029006fb91SChristoph Hellwig */
5039006fb91SChristoph Hellwig tic->t_curr_res = 0;
5049006fb91SChristoph Hellwig tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
5059006fb91SChristoph Hellwig return error;
5069006fb91SChristoph Hellwig }
5079006fb91SChristoph Hellwig
508df732b29SChristoph Hellwig /*
509aad7272aSDave Chinner * Run all the pending iclog callbacks and wake log force waiters and iclog
510aad7272aSDave Chinner * space waiters so they can process the newly set shutdown state. We really
511aad7272aSDave Chinner * don't care what order we process callbacks here because the log is shut down
512cd6f79d1SDave Chinner * and so state cannot change on disk anymore. However, we cannot wake waiters
513cd6f79d1SDave Chinner * until the callbacks have been processed because we may be in unmount and
514cd6f79d1SDave Chinner * we must ensure that all AIL operations the callbacks perform have completed
515cd6f79d1SDave Chinner * before we tear down the AIL.
516502a01faSDave Chinner *
517502a01faSDave Chinner * We avoid processing actively referenced iclogs so that we don't run callbacks
518502a01faSDave Chinner * while the iclog owner might still be preparing the iclog for IO submssion.
519502a01faSDave Chinner * These will be caught by xlog_state_iclog_release() and call this function
520502a01faSDave Chinner * again to process any callbacks that may have been added to that iclog.
521aad7272aSDave Chinner */
522aad7272aSDave Chinner static void
xlog_state_shutdown_callbacks(struct xlog * log)523aad7272aSDave Chinner xlog_state_shutdown_callbacks(
524aad7272aSDave Chinner struct xlog *log)
525aad7272aSDave Chinner {
526aad7272aSDave Chinner struct xlog_in_core *iclog;
527aad7272aSDave Chinner LIST_HEAD(cb_list);
528aad7272aSDave Chinner
529aad7272aSDave Chinner iclog = log->l_iclog;
530aad7272aSDave Chinner do {
531502a01faSDave Chinner if (atomic_read(&iclog->ic_refcnt)) {
532502a01faSDave Chinner /* Reference holder will re-run iclog callbacks. */
533502a01faSDave Chinner continue;
534502a01faSDave Chinner }
535aad7272aSDave Chinner list_splice_init(&iclog->ic_callbacks, &cb_list);
536cd6f79d1SDave Chinner spin_unlock(&log->l_icloglock);
537cd6f79d1SDave Chinner
538cd6f79d1SDave Chinner xlog_cil_process_committed(&cb_list);
539cd6f79d1SDave Chinner
540cd6f79d1SDave Chinner spin_lock(&log->l_icloglock);
541502a01faSDave Chinner wake_up_all(&iclog->ic_write_wait);
542aad7272aSDave Chinner wake_up_all(&iclog->ic_force_wait);
543aad7272aSDave Chinner } while ((iclog = iclog->ic_next) != log->l_iclog);
544aad7272aSDave Chinner
545aad7272aSDave Chinner wake_up_all(&log->l_flush_wait);
546aad7272aSDave Chinner }
547aad7272aSDave Chinner
548aad7272aSDave Chinner /*
549df732b29SChristoph Hellwig * Flush iclog to disk if this is the last reference to the given iclog and the
5509d110014SDave Chinner * it is in the WANT_SYNC state.
5519d110014SDave Chinner *
5529d110014SDave Chinner * If XLOG_ICL_NEED_FUA is already set on the iclog, we need to ensure that the
5539d110014SDave Chinner * log tail is updated correctly. NEED_FUA indicates that the iclog will be
5549d110014SDave Chinner * written to stable storage, and implies that a commit record is contained
5559d110014SDave Chinner * within the iclog. We need to ensure that the log tail does not move beyond
5569d110014SDave Chinner * the tail that the first commit record in the iclog ordered against, otherwise
5579d110014SDave Chinner * correct recovery of that checkpoint becomes dependent on future operations
5589d110014SDave Chinner * performed on this iclog.
5599d110014SDave Chinner *
5609d110014SDave Chinner * Hence if NEED_FUA is set and the current iclog tail lsn is empty, write the
5619d110014SDave Chinner * current tail into iclog. Once the iclog tail is set, future operations must
5629d110014SDave Chinner * not modify it, otherwise they potentially violate ordering constraints for
5639d110014SDave Chinner * the checkpoint commit that wrote the initial tail lsn value. The tail lsn in
5649d110014SDave Chinner * the iclog will get zeroed on activation of the iclog after sync, so we
5659d110014SDave Chinner * always capture the tail lsn on the iclog on the first NEED_FUA release
5669d110014SDave Chinner * regardless of the number of active reference counts on this iclog.
567df732b29SChristoph Hellwig */
568eef983ffSDave Chinner int
xlog_state_release_iclog(struct xlog * log,struct xlog_in_core * iclog,struct xlog_ticket * ticket)569df732b29SChristoph Hellwig xlog_state_release_iclog(
570df732b29SChristoph Hellwig struct xlog *log,
571d9f68777SDave Chinner struct xlog_in_core *iclog,
572d9f68777SDave Chinner struct xlog_ticket *ticket)
573df732b29SChristoph Hellwig {
5749d392064SDave Chinner xfs_lsn_t tail_lsn;
575502a01faSDave Chinner bool last_ref;
576502a01faSDave Chinner
577df732b29SChristoph Hellwig lockdep_assert_held(&log->l_icloglock);
578df732b29SChristoph Hellwig
579956f6daaSDave Chinner trace_xlog_iclog_release(iclog, _RET_IP_);
5800dc8f7f1SDave Chinner /*
5810dc8f7f1SDave Chinner * Grabbing the current log tail needs to be atomic w.r.t. the writing
5820dc8f7f1SDave Chinner * of the tail LSN into the iclog so we guarantee that the log tail does
583919edbadSDave Chinner * not move between the first time we know that the iclog needs to be
584919edbadSDave Chinner * made stable and when we eventually submit it.
5850dc8f7f1SDave Chinner */
586919edbadSDave Chinner if ((iclog->ic_state == XLOG_STATE_WANT_SYNC ||
587919edbadSDave Chinner (iclog->ic_flags & XLOG_ICL_NEED_FUA)) &&
588919edbadSDave Chinner !iclog->ic_header.h_tail_lsn) {
5890dc8f7f1SDave Chinner tail_lsn = xlog_assign_tail_lsn(log->l_mp);
5909d110014SDave Chinner iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
5910dc8f7f1SDave Chinner }
5920dc8f7f1SDave Chinner
593502a01faSDave Chinner last_ref = atomic_dec_and_test(&iclog->ic_refcnt);
594502a01faSDave Chinner
595502a01faSDave Chinner if (xlog_is_shutdown(log)) {
596502a01faSDave Chinner /*
597502a01faSDave Chinner * If there are no more references to this iclog, process the
598502a01faSDave Chinner * pending iclog callbacks that were waiting on the release of
599502a01faSDave Chinner * this iclog.
600502a01faSDave Chinner */
601cd6f79d1SDave Chinner if (last_ref)
602502a01faSDave Chinner xlog_state_shutdown_callbacks(log);
603502a01faSDave Chinner return -EIO;
604502a01faSDave Chinner }
605502a01faSDave Chinner
606502a01faSDave Chinner if (!last_ref)
6079d392064SDave Chinner return 0;
6089d392064SDave Chinner
6099d392064SDave Chinner if (iclog->ic_state != XLOG_STATE_WANT_SYNC) {
6109d392064SDave Chinner ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
6119d392064SDave Chinner return 0;
6129d392064SDave Chinner }
6139d392064SDave Chinner
6149d392064SDave Chinner iclog->ic_state = XLOG_STATE_SYNCING;
6159d110014SDave Chinner xlog_verify_tail_lsn(log, iclog);
6169d392064SDave Chinner trace_xlog_iclog_syncing(iclog, _RET_IP_);
6179d392064SDave Chinner
618df732b29SChristoph Hellwig spin_unlock(&log->l_icloglock);
619d9f68777SDave Chinner xlog_sync(log, iclog, ticket);
620df732b29SChristoph Hellwig spin_lock(&log->l_icloglock);
621df732b29SChristoph Hellwig return 0;
622df732b29SChristoph Hellwig }
623df732b29SChristoph Hellwig
6241da177e4SLinus Torvalds /*
6251da177e4SLinus Torvalds * Mount a log filesystem
6261da177e4SLinus Torvalds *
6271da177e4SLinus Torvalds * mp - ubiquitous xfs mount point structure
6281da177e4SLinus Torvalds * log_target - buftarg of on-disk log device
6291da177e4SLinus Torvalds * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
6301da177e4SLinus Torvalds * num_bblocks - Number of BBSIZE blocks in on-disk log
6311da177e4SLinus Torvalds *
6321da177e4SLinus Torvalds * Return error or zero.
6331da177e4SLinus Torvalds */
6341da177e4SLinus Torvalds int
xfs_log_mount(xfs_mount_t * mp,xfs_buftarg_t * log_target,xfs_daddr_t blk_offset,int num_bblks)635249a8c11SDavid Chinner xfs_log_mount(
636249a8c11SDavid Chinner xfs_mount_t *mp,
6371da177e4SLinus Torvalds xfs_buftarg_t *log_target,
6381da177e4SLinus Torvalds xfs_daddr_t blk_offset,
6391da177e4SLinus Torvalds int num_bblks)
6401da177e4SLinus Torvalds {
641e1d06e5fSDave Chinner struct xlog *log;
6423e7b91cfSJie Liu int error = 0;
6433e7b91cfSJie Liu int min_logfsbs;
644249a8c11SDavid Chinner
6450560f31aSDave Chinner if (!xfs_has_norecovery(mp)) {
64664c80dfdSLukas Herbolt xfs_notice(mp, "Mounting V%d Filesystem %pU",
64764c80dfdSLukas Herbolt XFS_SB_VERSION_NUM(&mp->m_sb),
64864c80dfdSLukas Herbolt &mp->m_sb.sb_uuid);
649c99d609aSDave Chinner } else {
650a0fa2b67SDave Chinner xfs_notice(mp,
65164c80dfdSLukas Herbolt "Mounting V%d filesystem %pU in no-recovery mode. Filesystem will be inconsistent.",
65264c80dfdSLukas Herbolt XFS_SB_VERSION_NUM(&mp->m_sb),
65364c80dfdSLukas Herbolt &mp->m_sb.sb_uuid);
6542e973b2cSDave Chinner ASSERT(xfs_is_readonly(mp));
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds
657e1d06e5fSDave Chinner log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
658e1d06e5fSDave Chinner if (IS_ERR(log)) {
659e1d06e5fSDave Chinner error = PTR_ERR(log);
660644c3567SDave Chinner goto out;
661644c3567SDave Chinner }
662e1d06e5fSDave Chinner mp->m_log = log;
6631da177e4SLinus Torvalds
6641da177e4SLinus Torvalds /*
665f1e1765aSDave Chinner * Now that we have set up the log and it's internal geometry
666f1e1765aSDave Chinner * parameters, we can validate the given log space and drop a critical
667f1e1765aSDave Chinner * message via syslog if the log size is too small. A log that is too
668f1e1765aSDave Chinner * small can lead to unexpected situations in transaction log space
669f1e1765aSDave Chinner * reservation stage. The superblock verifier has already validated all
670f1e1765aSDave Chinner * the other log geometry constraints, so we don't have to check those
671f1e1765aSDave Chinner * here.
6723e7b91cfSJie Liu *
673f1e1765aSDave Chinner * Note: For v4 filesystems, we can't just reject the mount if the
674f1e1765aSDave Chinner * validation fails. This would mean that people would have to
675f1e1765aSDave Chinner * downgrade their kernel just to remedy the situation as there is no
676f1e1765aSDave Chinner * way to grow the log (short of black magic surgery with xfs_db).
6773e7b91cfSJie Liu *
678f1e1765aSDave Chinner * We can, however, reject mounts for V5 format filesystems, as the
6793e7b91cfSJie Liu * mkfs binary being used to make the filesystem should never create a
6803e7b91cfSJie Liu * filesystem with a log that is too small.
6813e7b91cfSJie Liu */
6823e7b91cfSJie Liu min_logfsbs = xfs_log_calc_minimum_size(mp);
6833e7b91cfSJie Liu if (mp->m_sb.sb_logblocks < min_logfsbs) {
6843e7b91cfSJie Liu xfs_warn(mp,
6853e7b91cfSJie Liu "Log size %d blocks too small, minimum size is %d blocks",
6863e7b91cfSJie Liu mp->m_sb.sb_logblocks, min_logfsbs);
687f1e1765aSDave Chinner
6889c92ee20SDarrick J. Wong /*
6899c92ee20SDarrick J. Wong * Log check errors are always fatal on v5; or whenever bad
6909c92ee20SDarrick J. Wong * metadata leads to a crash.
6919c92ee20SDarrick J. Wong */
692f1e1765aSDave Chinner if (xfs_has_crc(mp)) {
6933e7b91cfSJie Liu xfs_crit(mp, "AAIEEE! Log failed size checks. Abort!");
6943e7b91cfSJie Liu ASSERT(0);
695f1e1765aSDave Chinner error = -EINVAL;
6963e7b91cfSJie Liu goto out_free_log;
6973e7b91cfSJie Liu }
698f41febd2SJoe Perches xfs_crit(mp, "Log size out of supported range.");
6993e7b91cfSJie Liu xfs_crit(mp,
700f41febd2SJoe Perches "Continuing onwards, but if log hangs are experienced then please report this message in the bug report.");
7013e7b91cfSJie Liu }
7023e7b91cfSJie Liu
7033e7b91cfSJie Liu /*
704249a8c11SDavid Chinner * Initialize the AIL now we have a log.
705249a8c11SDavid Chinner */
706249a8c11SDavid Chinner error = xfs_trans_ail_init(mp);
707249a8c11SDavid Chinner if (error) {
708a0fa2b67SDave Chinner xfs_warn(mp, "AIL initialisation failed: error %d", error);
70926430752SChristoph Hellwig goto out_free_log;
710249a8c11SDavid Chinner }
711e1d06e5fSDave Chinner log->l_ailp = mp->m_ail;
712249a8c11SDavid Chinner
713249a8c11SDavid Chinner /*
7141da177e4SLinus Torvalds * skip log recovery on a norecovery mount. pretend it all
7151da177e4SLinus Torvalds * just worked.
7161da177e4SLinus Torvalds */
7170560f31aSDave Chinner if (!xfs_has_norecovery(mp)) {
718e1d06e5fSDave Chinner error = xlog_recover(log);
7191da177e4SLinus Torvalds if (error) {
720a0fa2b67SDave Chinner xfs_warn(mp, "log mount/recovery failed: error %d",
721a0fa2b67SDave Chinner error);
722e1d06e5fSDave Chinner xlog_recover_cancel(log);
72326430752SChristoph Hellwig goto out_destroy_ail;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds
727e1d06e5fSDave Chinner error = xfs_sysfs_init(&log->l_kobj, &xfs_log_ktype, &mp->m_kobj,
728baff4e44SBrian Foster "log");
729baff4e44SBrian Foster if (error)
730baff4e44SBrian Foster goto out_destroy_ail;
731baff4e44SBrian Foster
7321da177e4SLinus Torvalds /* Normal transactions can now occur */
733e1d06e5fSDave Chinner clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
7341da177e4SLinus Torvalds
73571e330b5SDave Chinner /*
73671e330b5SDave Chinner * Now the log has been fully initialised and we know were our
73771e330b5SDave Chinner * space grant counters are, we can initialise the permanent ticket
73871e330b5SDave Chinner * needed for delayed logging to work.
73971e330b5SDave Chinner */
740e1d06e5fSDave Chinner xlog_cil_init_post_recovery(log);
74171e330b5SDave Chinner
7421da177e4SLinus Torvalds return 0;
74326430752SChristoph Hellwig
74426430752SChristoph Hellwig out_destroy_ail:
74526430752SChristoph Hellwig xfs_trans_ail_destroy(mp);
74626430752SChristoph Hellwig out_free_log:
747e1d06e5fSDave Chinner xlog_dealloc_log(log);
748644c3567SDave Chinner out:
749249a8c11SDavid Chinner return error;
75026430752SChristoph Hellwig }
7511da177e4SLinus Torvalds
7521da177e4SLinus Torvalds /*
753f661f1e0SDave Chinner * Finish the recovery of the file system. This is separate from the
754f661f1e0SDave Chinner * xfs_log_mount() call, because it depends on the code in xfs_mountfs() to read
755f661f1e0SDave Chinner * in the root and real-time bitmap inodes between calling xfs_log_mount() and
756f661f1e0SDave Chinner * here.
7571da177e4SLinus Torvalds *
758f661f1e0SDave Chinner * If we finish recovery successfully, start the background log work. If we are
759f661f1e0SDave Chinner * not doing recovery, then we have a RO filesystem and we don't need to start
760f661f1e0SDave Chinner * it.
7611da177e4SLinus Torvalds */
7621da177e4SLinus Torvalds int
xfs_log_mount_finish(struct xfs_mount * mp)763f0b2efadSBrian Foster xfs_log_mount_finish(
764f0b2efadSBrian Foster struct xfs_mount *mp)
7651da177e4SLinus Torvalds {
766fd67d8a0SDave Chinner struct xlog *log = mp->m_log;
767fd67d8a0SDave Chinner int error = 0;
7681da177e4SLinus Torvalds
7690560f31aSDave Chinner if (xfs_has_norecovery(mp)) {
7702e973b2cSDave Chinner ASSERT(xfs_is_readonly(mp));
771f0b2efadSBrian Foster return 0;
772f0b2efadSBrian Foster }
773f0b2efadSBrian Foster
7748204f8ddSDarrick J. Wong /*
7758204f8ddSDarrick J. Wong * During the second phase of log recovery, we need iget and
7768204f8ddSDarrick J. Wong * iput to behave like they do for an active filesystem.
7778204f8ddSDarrick J. Wong * xfs_fs_drop_inode needs to be able to prevent the deletion
7788204f8ddSDarrick J. Wong * of inodes before we're done replaying log items on those
7798204f8ddSDarrick J. Wong * inodes. Turn it off immediately after recovery finishes
7808204f8ddSDarrick J. Wong * so that we don't leak the quota inodes if subsequent mount
7818204f8ddSDarrick J. Wong * activities fail.
782799ea9e9SDarrick J. Wong *
783799ea9e9SDarrick J. Wong * We let all inodes involved in redo item processing end up on
784799ea9e9SDarrick J. Wong * the LRU instead of being evicted immediately so that if we do
785799ea9e9SDarrick J. Wong * something to an unlinked inode, the irele won't cause
786799ea9e9SDarrick J. Wong * premature truncation and freeing of the inode, which results
787799ea9e9SDarrick J. Wong * in log recovery failure. We have to evict the unreferenced
7881751e8a6SLinus Torvalds * lru inodes after clearing SB_ACTIVE because we don't
789799ea9e9SDarrick J. Wong * otherwise clean up the lru if there's a subsequent failure in
790799ea9e9SDarrick J. Wong * xfs_mountfs, which leads to us leaking the inodes if nothing
791799ea9e9SDarrick J. Wong * else (e.g. quotacheck) references the inodes before the
792799ea9e9SDarrick J. Wong * mount failure occurs.
7938204f8ddSDarrick J. Wong */
7941751e8a6SLinus Torvalds mp->m_super->s_flags |= SB_ACTIVE;
795a9a4bc8cSDave Chinner xfs_log_work_queue(mp);
796e1d06e5fSDave Chinner if (xlog_recovery_needed(log))
797fd67d8a0SDave Chinner error = xlog_recover_finish(log);
7981751e8a6SLinus Torvalds mp->m_super->s_flags &= ~SB_ACTIVE;
799799ea9e9SDarrick J. Wong evict_inodes(mp->m_super);
800f0b2efadSBrian Foster
801f1b92bbcSBrian Foster /*
802f1b92bbcSBrian Foster * Drain the buffer LRU after log recovery. This is required for v4
803f1b92bbcSBrian Foster * filesystems to avoid leaving around buffers with NULL verifier ops,
804f1b92bbcSBrian Foster * but we do it unconditionally to make sure we're always in a clean
805f1b92bbcSBrian Foster * cache state after mount.
806f1b92bbcSBrian Foster *
807f1b92bbcSBrian Foster * Don't push in the error case because the AIL may have pending intents
808f1b92bbcSBrian Foster * that aren't removed until recovery is cancelled.
809f1b92bbcSBrian Foster */
810e1d06e5fSDave Chinner if (xlog_recovery_needed(log)) {
811fd67d8a0SDave Chinner if (!error) {
812f1b92bbcSBrian Foster xfs_log_force(mp, XFS_LOG_SYNC);
813f1b92bbcSBrian Foster xfs_ail_push_all_sync(mp->m_ail);
814f1b92bbcSBrian Foster }
815fd67d8a0SDave Chinner xfs_notice(mp, "Ending recovery (logdev: %s)",
816fd67d8a0SDave Chinner mp->m_logname ? mp->m_logname : "internal");
817fd67d8a0SDave Chinner } else {
818fd67d8a0SDave Chinner xfs_info(mp, "Ending clean mount");
819fd67d8a0SDave Chinner }
82010fb9ac1SBrian Foster xfs_buftarg_drain(mp->m_ddev_targp);
821f1b92bbcSBrian Foster
822e1d06e5fSDave Chinner clear_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate);
8236f4a1eefSEric Sandeen
8244e6b8270SDarrick J. Wong /* Make sure the log is dead if we're returning failure. */
825fd67d8a0SDave Chinner ASSERT(!error || xlog_is_shutdown(log));
8264e6b8270SDarrick J. Wong
827f0b2efadSBrian Foster return error;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds
830f0b2efadSBrian Foster /*
831f0b2efadSBrian Foster * The mount has failed. Cancel the recovery if it hasn't completed and destroy
832f0b2efadSBrian Foster * the log.
833f0b2efadSBrian Foster */
834a7a9250eSHariprasad Kelam void
xfs_log_mount_cancel(struct xfs_mount * mp)835f0b2efadSBrian Foster xfs_log_mount_cancel(
836f0b2efadSBrian Foster struct xfs_mount *mp)
837f0b2efadSBrian Foster {
838a7a9250eSHariprasad Kelam xlog_recover_cancel(mp->m_log);
839f0b2efadSBrian Foster xfs_log_unmount(mp);
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds
8421da177e4SLinus Torvalds /*
84345eddb41SDave Chinner * Flush out the iclog to disk ensuring that device caches are flushed and
84445eddb41SDave Chinner * the iclog hits stable storage before any completion waiters are woken.
84545eddb41SDave Chinner */
84645eddb41SDave Chinner static inline int
xlog_force_iclog(struct xlog_in_core * iclog)84745eddb41SDave Chinner xlog_force_iclog(
84845eddb41SDave Chinner struct xlog_in_core *iclog)
84945eddb41SDave Chinner {
85045eddb41SDave Chinner atomic_inc(&iclog->ic_refcnt);
8512bf1ec0fSDave Chinner iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
85245eddb41SDave Chinner if (iclog->ic_state == XLOG_STATE_ACTIVE)
85345eddb41SDave Chinner xlog_state_switch_iclogs(iclog->ic_log, iclog, 0);
854d9f68777SDave Chinner return xlog_state_release_iclog(iclog->ic_log, iclog, NULL);
85545eddb41SDave Chinner }
85645eddb41SDave Chinner
85745eddb41SDave Chinner /*
8581eb52a6aSGuo Xuenan * Cycle all the iclogbuf locks to make sure all log IO completion
8591eb52a6aSGuo Xuenan * is done before we tear down these buffers.
8601eb52a6aSGuo Xuenan */
8611eb52a6aSGuo Xuenan static void
xlog_wait_iclog_completion(struct xlog * log)8621eb52a6aSGuo Xuenan xlog_wait_iclog_completion(struct xlog *log)
8631eb52a6aSGuo Xuenan {
8641eb52a6aSGuo Xuenan int i;
8651eb52a6aSGuo Xuenan struct xlog_in_core *iclog = log->l_iclog;
8661eb52a6aSGuo Xuenan
8671eb52a6aSGuo Xuenan for (i = 0; i < log->l_iclog_bufs; i++) {
8681eb52a6aSGuo Xuenan down(&iclog->ic_sema);
8691eb52a6aSGuo Xuenan up(&iclog->ic_sema);
8701eb52a6aSGuo Xuenan iclog = iclog->ic_next;
8711eb52a6aSGuo Xuenan }
8721eb52a6aSGuo Xuenan }
8731eb52a6aSGuo Xuenan
8741eb52a6aSGuo Xuenan /*
875a79b28c2SDave Chinner * Wait for the iclog and all prior iclogs to be written disk as required by the
876a79b28c2SDave Chinner * log force state machine. Waiting on ic_force_wait ensures iclog completions
877a79b28c2SDave Chinner * have been ordered and callbacks run before we are woken here, hence
878a79b28c2SDave Chinner * guaranteeing that all the iclogs up to this one are on stable storage.
87981e5b50aSChristoph Hellwig */
880a79b28c2SDave Chinner int
xlog_wait_on_iclog(struct xlog_in_core * iclog)88181e5b50aSChristoph Hellwig xlog_wait_on_iclog(
88281e5b50aSChristoph Hellwig struct xlog_in_core *iclog)
88381e5b50aSChristoph Hellwig __releases(iclog->ic_log->l_icloglock)
88481e5b50aSChristoph Hellwig {
88581e5b50aSChristoph Hellwig struct xlog *log = iclog->ic_log;
88681e5b50aSChristoph Hellwig
887956f6daaSDave Chinner trace_xlog_iclog_wait_on(iclog, _RET_IP_);
8882039a272SDave Chinner if (!xlog_is_shutdown(log) &&
88981e5b50aSChristoph Hellwig iclog->ic_state != XLOG_STATE_ACTIVE &&
89081e5b50aSChristoph Hellwig iclog->ic_state != XLOG_STATE_DIRTY) {
89181e5b50aSChristoph Hellwig XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
89281e5b50aSChristoph Hellwig xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
89381e5b50aSChristoph Hellwig } else {
89481e5b50aSChristoph Hellwig spin_unlock(&log->l_icloglock);
89581e5b50aSChristoph Hellwig }
89681e5b50aSChristoph Hellwig
8972039a272SDave Chinner if (xlog_is_shutdown(log))
89881e5b50aSChristoph Hellwig return -EIO;
89981e5b50aSChristoph Hellwig return 0;
90081e5b50aSChristoph Hellwig }
90181e5b50aSChristoph Hellwig
90281e5b50aSChristoph Hellwig /*
9033c702f95SDave Chinner * Write out an unmount record using the ticket provided. We have to account for
9043c702f95SDave Chinner * the data space used in the unmount ticket as this write is not done from a
9053c702f95SDave Chinner * transaction context that has already done the accounting for us.
9061da177e4SLinus Torvalds */
9073c702f95SDave Chinner static int
xlog_write_unmount_record(struct xlog * log,struct xlog_ticket * ticket)9083c702f95SDave Chinner xlog_write_unmount_record(
9093c702f95SDave Chinner struct xlog *log,
9103468bb1cSDave Chinner struct xlog_ticket *ticket)
91153235f22SDarrick J. Wong {
912ffa04c1fSDave Chinner struct {
913ffa04c1fSDave Chinner struct xlog_op_header ophdr;
914ffa04c1fSDave Chinner struct xfs_unmount_log_format ulf;
915ffa04c1fSDave Chinner } unmount_rec = {
916ffa04c1fSDave Chinner .ophdr = {
917ffa04c1fSDave Chinner .oh_clientid = XFS_LOG,
918ffa04c1fSDave Chinner .oh_tid = cpu_to_be32(ticket->t_tid),
919ffa04c1fSDave Chinner .oh_flags = XLOG_UNMOUNT_TRANS,
920ffa04c1fSDave Chinner },
921ffa04c1fSDave Chinner .ulf = {
92253235f22SDarrick J. Wong .magic = XLOG_UNMOUNT_TYPE,
923ffa04c1fSDave Chinner },
92453235f22SDarrick J. Wong };
92553235f22SDarrick J. Wong struct xfs_log_iovec reg = {
926ffa04c1fSDave Chinner .i_addr = &unmount_rec,
927ffa04c1fSDave Chinner .i_len = sizeof(unmount_rec),
92853235f22SDarrick J. Wong .i_type = XLOG_REG_TYPE_UNMOUNT,
92953235f22SDarrick J. Wong };
93053235f22SDarrick J. Wong struct xfs_log_vec vec = {
93153235f22SDarrick J. Wong .lv_niovecs = 1,
93253235f22SDarrick J. Wong .lv_iovecp = ®,
93353235f22SDarrick J. Wong };
93416924853SDave Chinner LIST_HEAD(lv_chain);
93516924853SDave Chinner list_add(&vec.lv_list, &lv_chain);
9363c702f95SDave Chinner
937ffa04c1fSDave Chinner BUILD_BUG_ON((sizeof(struct xlog_op_header) +
938ffa04c1fSDave Chinner sizeof(struct xfs_unmount_log_format)) !=
939ffa04c1fSDave Chinner sizeof(unmount_rec));
940eef983ffSDave Chinner
9413c702f95SDave Chinner /* account for space used by record data */
942ffa04c1fSDave Chinner ticket->t_curr_res -= sizeof(unmount_rec);
943eef983ffSDave Chinner
94416924853SDave Chinner return xlog_write(log, NULL, &lv_chain, ticket, reg.i_len);
9453c702f95SDave Chinner }
9463c702f95SDave Chinner
9473c702f95SDave Chinner /*
9483c702f95SDave Chinner * Mark the filesystem clean by writing an unmount record to the head of the
9493c702f95SDave Chinner * log.
9503c702f95SDave Chinner */
9513c702f95SDave Chinner static void
xlog_unmount_write(struct xlog * log)9523c702f95SDave Chinner xlog_unmount_write(
9533c702f95SDave Chinner struct xlog *log)
9543c702f95SDave Chinner {
9553c702f95SDave Chinner struct xfs_mount *mp = log->l_mp;
95653235f22SDarrick J. Wong struct xlog_in_core *iclog;
95753235f22SDarrick J. Wong struct xlog_ticket *tic = NULL;
95853235f22SDarrick J. Wong int error;
95953235f22SDarrick J. Wong
960c7610dceSDave Chinner error = xfs_log_reserve(mp, 600, 1, &tic, 0);
96153235f22SDarrick J. Wong if (error)
96253235f22SDarrick J. Wong goto out_err;
96353235f22SDarrick J. Wong
9643468bb1cSDave Chinner error = xlog_write_unmount_record(log, tic);
96553235f22SDarrick J. Wong /*
96653235f22SDarrick J. Wong * At this point, we're umounting anyway, so there's no point in
9675112e206SDave Chinner * transitioning log state to shutdown. Just continue...
96853235f22SDarrick J. Wong */
96953235f22SDarrick J. Wong out_err:
97053235f22SDarrick J. Wong if (error)
97153235f22SDarrick J. Wong xfs_alert(mp, "%s: unmount record failed", __func__);
97253235f22SDarrick J. Wong
97353235f22SDarrick J. Wong spin_lock(&log->l_icloglock);
97453235f22SDarrick J. Wong iclog = log->l_iclog;
97545eddb41SDave Chinner error = xlog_force_iclog(iclog);
97681e5b50aSChristoph Hellwig xlog_wait_on_iclog(iclog);
97753235f22SDarrick J. Wong
97853235f22SDarrick J. Wong if (tic) {
97953235f22SDarrick J. Wong trace_xfs_log_umount_write(log, tic);
9808b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(log, tic);
98153235f22SDarrick J. Wong }
98253235f22SDarrick J. Wong }
98353235f22SDarrick J. Wong
98413859c98SChristoph Hellwig static void
xfs_log_unmount_verify_iclog(struct xlog * log)98513859c98SChristoph Hellwig xfs_log_unmount_verify_iclog(
98613859c98SChristoph Hellwig struct xlog *log)
98713859c98SChristoph Hellwig {
98813859c98SChristoph Hellwig struct xlog_in_core *iclog = log->l_iclog;
98913859c98SChristoph Hellwig
99013859c98SChristoph Hellwig do {
99113859c98SChristoph Hellwig ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
99213859c98SChristoph Hellwig ASSERT(iclog->ic_offset == 0);
99313859c98SChristoph Hellwig } while ((iclog = iclog->ic_next) != log->l_iclog);
99413859c98SChristoph Hellwig }
99513859c98SChristoph Hellwig
9961da177e4SLinus Torvalds /*
9971da177e4SLinus Torvalds * Unmount record used to have a string "Unmount filesystem--" in the
9981da177e4SLinus Torvalds * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
9991da177e4SLinus Torvalds * We just write the magic number now since that particular field isn't
10008e159e72SZhi Yong Wu * currently architecture converted and "Unmount" is a bit foo.
10011da177e4SLinus Torvalds * As far as I know, there weren't any dependencies on the old behaviour.
10021da177e4SLinus Torvalds */
1003550319e9SChristoph Hellwig static void
xfs_log_unmount_write(struct xfs_mount * mp)100413859c98SChristoph Hellwig xfs_log_unmount_write(
100513859c98SChristoph Hellwig struct xfs_mount *mp)
10061da177e4SLinus Torvalds {
10079a8d2fdbSMark Tinguely struct xlog *log = mp->m_log;
10081da177e4SLinus Torvalds
100950d25484SBrian Foster if (!xfs_log_writable(mp))
1010550319e9SChristoph Hellwig return;
10111da177e4SLinus Torvalds
1012550319e9SChristoph Hellwig xfs_log_force(mp, XFS_LOG_SYNC);
10131da177e4SLinus Torvalds
10142039a272SDave Chinner if (xlog_is_shutdown(log))
10156178d104SChristoph Hellwig return;
10165cc3c006SDarrick J. Wong
10175cc3c006SDarrick J. Wong /*
10185cc3c006SDarrick J. Wong * If we think the summary counters are bad, avoid writing the unmount
10195cc3c006SDarrick J. Wong * record to force log recovery at next mount, after which the summary
10205cc3c006SDarrick J. Wong * counters will be recalculated. Refer to xlog_check_unmount_rec for
10215cc3c006SDarrick J. Wong * more details.
10225cc3c006SDarrick J. Wong */
10235cc3c006SDarrick J. Wong if (XFS_TEST_ERROR(xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS), mp,
10245cc3c006SDarrick J. Wong XFS_ERRTAG_FORCE_SUMMARY_RECALC)) {
10255cc3c006SDarrick J. Wong xfs_alert(mp, "%s: will fix summary counters at next mount",
10265cc3c006SDarrick J. Wong __func__);
10275cc3c006SDarrick J. Wong return;
10285cc3c006SDarrick J. Wong }
10295cc3c006SDarrick J. Wong
103013859c98SChristoph Hellwig xfs_log_unmount_verify_iclog(log);
10313c702f95SDave Chinner xlog_unmount_write(log);
1032550319e9SChristoph Hellwig }
10331da177e4SLinus Torvalds
10341da177e4SLinus Torvalds /*
1035c75921a7SDave Chinner * Empty the log for unmount/freeze.
1036cf2931dbSDave Chinner *
1037cf2931dbSDave Chinner * To do this, we first need to shut down the background log work so it is not
1038cf2931dbSDave Chinner * trying to cover the log as we clean up. We then need to unpin all objects in
1039cf2931dbSDave Chinner * the log so we can then flush them out. Once they have completed their IO and
1040303591a0SBrian Foster * run the callbacks removing themselves from the AIL, we can cover the log.
10411da177e4SLinus Torvalds */
1042303591a0SBrian Foster int
xfs_log_quiesce(struct xfs_mount * mp)1043c75921a7SDave Chinner xfs_log_quiesce(
1044c75921a7SDave Chinner struct xfs_mount *mp)
10451da177e4SLinus Torvalds {
1046908ce71eSDarrick J. Wong /*
1047908ce71eSDarrick J. Wong * Clear log incompat features since we're quiescing the log. Report
1048908ce71eSDarrick J. Wong * failures, though it's not fatal to have a higher log feature
1049908ce71eSDarrick J. Wong * protection level than the log contents actually require.
1050908ce71eSDarrick J. Wong */
1051908ce71eSDarrick J. Wong if (xfs_clear_incompat_log_features(mp)) {
1052908ce71eSDarrick J. Wong int error;
1053908ce71eSDarrick J. Wong
1054908ce71eSDarrick J. Wong error = xfs_sync_sb(mp, false);
1055908ce71eSDarrick J. Wong if (error)
1056908ce71eSDarrick J. Wong xfs_warn(mp,
1057908ce71eSDarrick J. Wong "Failed to clear log incompat features on quiesce");
1058908ce71eSDarrick J. Wong }
1059908ce71eSDarrick J. Wong
1060f661f1e0SDave Chinner cancel_delayed_work_sync(&mp->m_log->l_work);
1061cf2931dbSDave Chinner xfs_log_force(mp, XFS_LOG_SYNC);
1062cf2931dbSDave Chinner
1063cf2931dbSDave Chinner /*
1064cf2931dbSDave Chinner * The superblock buffer is uncached and while xfs_ail_push_all_sync()
10658321ddb2SBrian Foster * will push it, xfs_buftarg_wait() will not wait for it. Further,
1066cf2931dbSDave Chinner * xfs_buf_iowait() cannot be used because it was pushed with the
1067cf2931dbSDave Chinner * XBF_ASYNC flag set, so we need to use a lock/unlock pair to wait for
1068cf2931dbSDave Chinner * the IO to complete.
1069cf2931dbSDave Chinner */
1070cf2931dbSDave Chinner xfs_ail_push_all_sync(mp->m_ail);
10718321ddb2SBrian Foster xfs_buftarg_wait(mp->m_ddev_targp);
1072cf2931dbSDave Chinner xfs_buf_lock(mp->m_sb_bp);
1073cf2931dbSDave Chinner xfs_buf_unlock(mp->m_sb_bp);
1074303591a0SBrian Foster
1075303591a0SBrian Foster return xfs_log_cover(mp);
10769e54ee0fSBrian Foster }
1077cf2931dbSDave Chinner
10789e54ee0fSBrian Foster void
xfs_log_clean(struct xfs_mount * mp)10799e54ee0fSBrian Foster xfs_log_clean(
10809e54ee0fSBrian Foster struct xfs_mount *mp)
10819e54ee0fSBrian Foster {
10829e54ee0fSBrian Foster xfs_log_quiesce(mp);
1083cf2931dbSDave Chinner xfs_log_unmount_write(mp);
1084c75921a7SDave Chinner }
1085c75921a7SDave Chinner
1086c75921a7SDave Chinner /*
1087c75921a7SDave Chinner * Shut down and release the AIL and Log.
1088c75921a7SDave Chinner *
1089c75921a7SDave Chinner * During unmount, we need to ensure we flush all the dirty metadata objects
1090c75921a7SDave Chinner * from the AIL so that the log is empty before we write the unmount record to
1091c75921a7SDave Chinner * the log. Once this is done, we can tear down the AIL and the log.
1092c75921a7SDave Chinner */
1093c75921a7SDave Chinner void
xfs_log_unmount(struct xfs_mount * mp)1094c75921a7SDave Chinner xfs_log_unmount(
1095c75921a7SDave Chinner struct xfs_mount *mp)
1096c75921a7SDave Chinner {
10979e54ee0fSBrian Foster xfs_log_clean(mp);
1098cf2931dbSDave Chinner
10991eb52a6aSGuo Xuenan /*
11001eb52a6aSGuo Xuenan * If shutdown has come from iclog IO context, the log
11011eb52a6aSGuo Xuenan * cleaning will have been skipped and so we need to wait
11021eb52a6aSGuo Xuenan * for the iclog to complete shutdown processing before we
11031eb52a6aSGuo Xuenan * tear anything down.
11041eb52a6aSGuo Xuenan */
11051eb52a6aSGuo Xuenan xlog_wait_iclog_completion(mp->m_log);
11061eb52a6aSGuo Xuenan
11078321ddb2SBrian Foster xfs_buftarg_drain(mp->m_ddev_targp);
11088321ddb2SBrian Foster
1109249a8c11SDavid Chinner xfs_trans_ail_destroy(mp);
1110baff4e44SBrian Foster
1111baff4e44SBrian Foster xfs_sysfs_del(&mp->m_log->l_kobj);
1112baff4e44SBrian Foster
1113c41564b5SNathan Scott xlog_dealloc_log(mp->m_log);
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds
111643f5efc5SDave Chinner void
xfs_log_item_init(struct xfs_mount * mp,struct xfs_log_item * item,int type,const struct xfs_item_ops * ops)111743f5efc5SDave Chinner xfs_log_item_init(
111843f5efc5SDave Chinner struct xfs_mount *mp,
111943f5efc5SDave Chinner struct xfs_log_item *item,
112043f5efc5SDave Chinner int type,
1121272e42b2SChristoph Hellwig const struct xfs_item_ops *ops)
112243f5efc5SDave Chinner {
1123d86142ddSDave Chinner item->li_log = mp->m_log;
112443f5efc5SDave Chinner item->li_ailp = mp->m_ail;
112543f5efc5SDave Chinner item->li_type = type;
112643f5efc5SDave Chinner item->li_ops = ops;
112771e330b5SDave Chinner item->li_lv = NULL;
112871e330b5SDave Chinner
112971e330b5SDave Chinner INIT_LIST_HEAD(&item->li_ail);
113071e330b5SDave Chinner INIT_LIST_HEAD(&item->li_cil);
1131643c8c05SCarlos Maiolino INIT_LIST_HEAD(&item->li_bio_list);
1132e6631f85SDave Chinner INIT_LIST_HEAD(&item->li_trans);
113343f5efc5SDave Chinner }
113443f5efc5SDave Chinner
113509a423a3SChristoph Hellwig /*
113609a423a3SChristoph Hellwig * Wake up processes waiting for log space after we have moved the log tail.
113709a423a3SChristoph Hellwig */
11381da177e4SLinus Torvalds void
xfs_log_space_wake(struct xfs_mount * mp)113909a423a3SChristoph Hellwig xfs_log_space_wake(
1140cfb7cdcaSChristoph Hellwig struct xfs_mount *mp)
11411da177e4SLinus Torvalds {
1142ad223e60SMark Tinguely struct xlog *log = mp->m_log;
1143cfb7cdcaSChristoph Hellwig int free_bytes;
11441da177e4SLinus Torvalds
11452039a272SDave Chinner if (xlog_is_shutdown(log))
11461da177e4SLinus Torvalds return;
11471da177e4SLinus Torvalds
114828496968SChristoph Hellwig if (!list_empty_careful(&log->l_write_head.waiters)) {
1149e1d06e5fSDave Chinner ASSERT(!xlog_in_recovery(log));
115009a423a3SChristoph Hellwig
115128496968SChristoph Hellwig spin_lock(&log->l_write_head.lock);
115228496968SChristoph Hellwig free_bytes = xlog_space_left(log, &log->l_write_head.grant);
1153e179840dSChristoph Hellwig xlog_grant_head_wake(log, &log->l_write_head, &free_bytes);
115428496968SChristoph Hellwig spin_unlock(&log->l_write_head.lock);
115510547941SDave Chinner }
115610547941SDave Chinner
115728496968SChristoph Hellwig if (!list_empty_careful(&log->l_reserve_head.waiters)) {
1158e1d06e5fSDave Chinner ASSERT(!xlog_in_recovery(log));
115909a423a3SChristoph Hellwig
116028496968SChristoph Hellwig spin_lock(&log->l_reserve_head.lock);
116128496968SChristoph Hellwig free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
1162e179840dSChristoph Hellwig xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes);
116328496968SChristoph Hellwig spin_unlock(&log->l_reserve_head.lock);
11641da177e4SLinus Torvalds }
11653f16b985SDave Chinner }
11661da177e4SLinus Torvalds
11671da177e4SLinus Torvalds /*
11682c6e24ceSDave Chinner * Determine if we have a transaction that has gone to disk that needs to be
11692c6e24ceSDave Chinner * covered. To begin the transition to the idle state firstly the log needs to
11702c6e24ceSDave Chinner * be idle. That means the CIL, the AIL and the iclogs needs to be empty before
11712c6e24ceSDave Chinner * we start attempting to cover the log.
1172b6f8dd49SDave Chinner *
11732c6e24ceSDave Chinner * Only if we are then in a state where covering is needed, the caller is
11742c6e24ceSDave Chinner * informed that dummy transactions are required to move the log into the idle
11752c6e24ceSDave Chinner * state.
11762c6e24ceSDave Chinner *
11772c6e24ceSDave Chinner * If there are any items in the AIl or CIL, then we do not want to attempt to
11782c6e24ceSDave Chinner * cover the log as we may be in a situation where there isn't log space
11792c6e24ceSDave Chinner * available to run a dummy transaction and this can lead to deadlocks when the
11802c6e24ceSDave Chinner * tail of the log is pinned by an item that is modified in the CIL. Hence
11812c6e24ceSDave Chinner * there's no point in running a dummy transaction at this point because we
11822c6e24ceSDave Chinner * can't start trying to idle the log until both the CIL and AIL are empty.
11831da177e4SLinus Torvalds */
118437444fc4SBrian Foster static bool
xfs_log_need_covered(struct xfs_mount * mp)118537444fc4SBrian Foster xfs_log_need_covered(
118637444fc4SBrian Foster struct xfs_mount *mp)
11871da177e4SLinus Torvalds {
11889a8d2fdbSMark Tinguely struct xlog *log = mp->m_log;
118937444fc4SBrian Foster bool needed = false;
11901da177e4SLinus Torvalds
11912c6e24ceSDave Chinner if (!xlog_cil_empty(log))
11928646b982Skernel test robot return false;
11932c6e24ceSDave Chinner
1194b22cd72cSEric Sandeen spin_lock(&log->l_icloglock);
1195b6f8dd49SDave Chinner switch (log->l_covered_state) {
1196b6f8dd49SDave Chinner case XLOG_STATE_COVER_DONE:
1197b6f8dd49SDave Chinner case XLOG_STATE_COVER_DONE2:
1198b6f8dd49SDave Chinner case XLOG_STATE_COVER_IDLE:
1199b6f8dd49SDave Chinner break;
1200b6f8dd49SDave Chinner case XLOG_STATE_COVER_NEED:
1201b6f8dd49SDave Chinner case XLOG_STATE_COVER_NEED2:
12022c6e24ceSDave Chinner if (xfs_ail_min_lsn(log->l_ailp))
12032c6e24ceSDave Chinner break;
12042c6e24ceSDave Chinner if (!xlog_iclogs_empty(log))
12052c6e24ceSDave Chinner break;
12062c6e24ceSDave Chinner
120737444fc4SBrian Foster needed = true;
12081da177e4SLinus Torvalds if (log->l_covered_state == XLOG_STATE_COVER_NEED)
12091da177e4SLinus Torvalds log->l_covered_state = XLOG_STATE_COVER_DONE;
1210b6f8dd49SDave Chinner else
12111da177e4SLinus Torvalds log->l_covered_state = XLOG_STATE_COVER_DONE2;
12122c6e24ceSDave Chinner break;
1213b6f8dd49SDave Chinner default:
121437444fc4SBrian Foster needed = true;
1215b6f8dd49SDave Chinner break;
12161da177e4SLinus Torvalds }
1217b22cd72cSEric Sandeen spin_unlock(&log->l_icloglock);
1218014c2544SJesper Juhl return needed;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds
122109a423a3SChristoph Hellwig /*
1222303591a0SBrian Foster * Explicitly cover the log. This is similar to background log covering but
1223303591a0SBrian Foster * intended for usage in quiesce codepaths. The caller is responsible to ensure
1224303591a0SBrian Foster * the log is idle and suitable for covering. The CIL, iclog buffers and AIL
1225303591a0SBrian Foster * must all be empty.
1226303591a0SBrian Foster */
1227303591a0SBrian Foster static int
xfs_log_cover(struct xfs_mount * mp)1228303591a0SBrian Foster xfs_log_cover(
1229303591a0SBrian Foster struct xfs_mount *mp)
1230303591a0SBrian Foster {
1231303591a0SBrian Foster int error = 0;
1232f46e5a17SBrian Foster bool need_covered;
1233303591a0SBrian Foster
12344533fc63SBrian Foster ASSERT((xlog_cil_empty(mp->m_log) && xlog_iclogs_empty(mp->m_log) &&
12354533fc63SBrian Foster !xfs_ail_min_lsn(mp->m_log->l_ailp)) ||
12362039a272SDave Chinner xlog_is_shutdown(mp->m_log));
1237303591a0SBrian Foster
1238303591a0SBrian Foster if (!xfs_log_writable(mp))
1239303591a0SBrian Foster return 0;
1240303591a0SBrian Foster
1241303591a0SBrian Foster /*
1242f46e5a17SBrian Foster * xfs_log_need_covered() is not idempotent because it progresses the
1243f46e5a17SBrian Foster * state machine if the log requires covering. Therefore, we must call
1244f46e5a17SBrian Foster * this function once and use the result until we've issued an sb sync.
1245f46e5a17SBrian Foster * Do so first to make that abundantly clear.
1246f46e5a17SBrian Foster *
1247f46e5a17SBrian Foster * Fall into the covering sequence if the log needs covering or the
1248f46e5a17SBrian Foster * mount has lazy superblock accounting to sync to disk. The sb sync
1249f46e5a17SBrian Foster * used for covering accumulates the in-core counters, so covering
1250f46e5a17SBrian Foster * handles this for us.
1251f46e5a17SBrian Foster */
1252f46e5a17SBrian Foster need_covered = xfs_log_need_covered(mp);
125338c26bfdSDave Chinner if (!need_covered && !xfs_has_lazysbcount(mp))
1254f46e5a17SBrian Foster return 0;
1255f46e5a17SBrian Foster
1256f46e5a17SBrian Foster /*
1257303591a0SBrian Foster * To cover the log, commit the superblock twice (at most) in
1258303591a0SBrian Foster * independent checkpoints. The first serves as a reference for the
1259303591a0SBrian Foster * tail pointer. The sync transaction and AIL push empties the AIL and
1260303591a0SBrian Foster * updates the in-core tail to the LSN of the first checkpoint. The
1261303591a0SBrian Foster * second commit updates the on-disk tail with the in-core LSN,
1262303591a0SBrian Foster * covering the log. Push the AIL one more time to leave it empty, as
1263303591a0SBrian Foster * we found it.
1264303591a0SBrian Foster */
1265f46e5a17SBrian Foster do {
1266303591a0SBrian Foster error = xfs_sync_sb(mp, true);
1267303591a0SBrian Foster if (error)
1268303591a0SBrian Foster break;
1269303591a0SBrian Foster xfs_ail_push_all_sync(mp->m_ail);
1270f46e5a17SBrian Foster } while (xfs_log_need_covered(mp));
1271303591a0SBrian Foster
1272303591a0SBrian Foster return error;
1273303591a0SBrian Foster }
1274303591a0SBrian Foster
1275303591a0SBrian Foster /*
12761da177e4SLinus Torvalds * We may be holding the log iclog lock upon entering this routine.
12771da177e4SLinus Torvalds */
12781da177e4SLinus Torvalds xfs_lsn_t
xlog_assign_tail_lsn_locked(struct xfs_mount * mp)12791c304625SChristoph Hellwig xlog_assign_tail_lsn_locked(
12801c3cb9ecSDave Chinner struct xfs_mount *mp)
12811da177e4SLinus Torvalds {
1282ad223e60SMark Tinguely struct xlog *log = mp->m_log;
12831c304625SChristoph Hellwig struct xfs_log_item *lip;
12841c304625SChristoph Hellwig xfs_lsn_t tail_lsn;
12851c304625SChristoph Hellwig
128657e80956SMatthew Wilcox assert_spin_locked(&mp->m_ail->ail_lock);
12871da177e4SLinus Torvalds
128809a423a3SChristoph Hellwig /*
128909a423a3SChristoph Hellwig * To make sure we always have a valid LSN for the log tail we keep
129009a423a3SChristoph Hellwig * track of the last LSN which was committed in log->l_last_sync_lsn,
12911c304625SChristoph Hellwig * and use that when the AIL was empty.
129209a423a3SChristoph Hellwig */
12931c304625SChristoph Hellwig lip = xfs_ail_min(mp->m_ail);
12941c304625SChristoph Hellwig if (lip)
12951c304625SChristoph Hellwig tail_lsn = lip->li_lsn;
12961c304625SChristoph Hellwig else
129784f3c683SDave Chinner tail_lsn = atomic64_read(&log->l_last_sync_lsn);
1298750b9c90SDave Chinner trace_xfs_log_assign_tail_lsn(log, tail_lsn);
12991c3cb9ecSDave Chinner atomic64_set(&log->l_tail_lsn, tail_lsn);
13001da177e4SLinus Torvalds return tail_lsn;
13011c3cb9ecSDave Chinner }
13021da177e4SLinus Torvalds
13031c304625SChristoph Hellwig xfs_lsn_t
xlog_assign_tail_lsn(struct xfs_mount * mp)13041c304625SChristoph Hellwig xlog_assign_tail_lsn(
13051c304625SChristoph Hellwig struct xfs_mount *mp)
13061c304625SChristoph Hellwig {
13071c304625SChristoph Hellwig xfs_lsn_t tail_lsn;
13081c304625SChristoph Hellwig
130957e80956SMatthew Wilcox spin_lock(&mp->m_ail->ail_lock);
13101c304625SChristoph Hellwig tail_lsn = xlog_assign_tail_lsn_locked(mp);
131157e80956SMatthew Wilcox spin_unlock(&mp->m_ail->ail_lock);
13121c304625SChristoph Hellwig
13131c304625SChristoph Hellwig return tail_lsn;
13141c304625SChristoph Hellwig }
13151c304625SChristoph Hellwig
13161da177e4SLinus Torvalds /*
13171da177e4SLinus Torvalds * Return the space in the log between the tail and the head. The head
13181da177e4SLinus Torvalds * is passed in the cycle/bytes formal parms. In the special case where
13191da177e4SLinus Torvalds * the reserve head has wrapped passed the tail, this calculation is no
13201da177e4SLinus Torvalds * longer valid. In this case, just return 0 which means there is no space
13211da177e4SLinus Torvalds * in the log. This works for all places where this function is called
13221da177e4SLinus Torvalds * with the reserve head. Of course, if the write head were to ever
13231da177e4SLinus Torvalds * wrap the tail, we should blow up. Rather than catch this case here,
13241da177e4SLinus Torvalds * we depend on other ASSERTions in other parts of the code. XXXmiken
13251da177e4SLinus Torvalds *
13262562c322SDave Chinner * If reservation head is behind the tail, we have a problem. Warn about it,
13272562c322SDave Chinner * but then treat it as if the log is empty.
13282562c322SDave Chinner *
13292562c322SDave Chinner * If the log is shut down, the head and tail may be invalid or out of whack, so
13302562c322SDave Chinner * shortcut invalidity asserts in this case so that we don't trigger them
13312562c322SDave Chinner * falsely.
13321da177e4SLinus Torvalds */
1333a8272ce0SDavid Chinner STATIC int
xlog_space_left(struct xlog * log,atomic64_t * head)1334a69ed03cSDave Chinner xlog_space_left(
1335ad223e60SMark Tinguely struct xlog *log,
1336c8a09ff8SDave Chinner atomic64_t *head)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds int tail_bytes;
13391da177e4SLinus Torvalds int tail_cycle;
1340a69ed03cSDave Chinner int head_cycle;
1341a69ed03cSDave Chinner int head_bytes;
13421da177e4SLinus Torvalds
1343a69ed03cSDave Chinner xlog_crack_grant_head(head, &head_cycle, &head_bytes);
13441c3cb9ecSDave Chinner xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
13451c3cb9ecSDave Chinner tail_bytes = BBTOB(tail_bytes);
1346a69ed03cSDave Chinner if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
13472562c322SDave Chinner return log->l_logsize - (head_bytes - tail_bytes);
13482562c322SDave Chinner if (tail_cycle + 1 < head_cycle)
13491da177e4SLinus Torvalds return 0;
13502562c322SDave Chinner
13512562c322SDave Chinner /* Ignore potential inconsistency when shutdown. */
13522562c322SDave Chinner if (xlog_is_shutdown(log))
13532562c322SDave Chinner return log->l_logsize;
13542562c322SDave Chinner
13552562c322SDave Chinner if (tail_cycle < head_cycle) {
1356a69ed03cSDave Chinner ASSERT(tail_cycle == (head_cycle - 1));
13572562c322SDave Chinner return tail_bytes - head_bytes;
13582562c322SDave Chinner }
13592562c322SDave Chinner
13601da177e4SLinus Torvalds /*
13612562c322SDave Chinner * The reservation head is behind the tail. In this case we just want to
13622562c322SDave Chinner * return the size of the log as the amount of space left.
13631da177e4SLinus Torvalds */
1364f41febd2SJoe Perches xfs_alert(log->l_mp, "xlog_space_left: head behind tail");
13652562c322SDave Chinner xfs_alert(log->l_mp, " tail_cycle = %d, tail_bytes = %d",
1366f41febd2SJoe Perches tail_cycle, tail_bytes);
13672562c322SDave Chinner xfs_alert(log->l_mp, " GH cycle = %d, GH bytes = %d",
1368f41febd2SJoe Perches head_cycle, head_bytes);
13691da177e4SLinus Torvalds ASSERT(0);
13702562c322SDave Chinner return log->l_logsize;
1371a69ed03cSDave Chinner }
13721da177e4SLinus Torvalds
13731da177e4SLinus Torvalds
13740d5a75e9SEric Sandeen static void
xlog_ioend_work(struct work_struct * work)137579b54d9bSChristoph Hellwig xlog_ioend_work(
137679b54d9bSChristoph Hellwig struct work_struct *work)
13771da177e4SLinus Torvalds {
137879b54d9bSChristoph Hellwig struct xlog_in_core *iclog =
137979b54d9bSChristoph Hellwig container_of(work, struct xlog_in_core, ic_end_io_work);
138079b54d9bSChristoph Hellwig struct xlog *log = iclog->ic_log;
138179b54d9bSChristoph Hellwig int error;
13821da177e4SLinus Torvalds
138379b54d9bSChristoph Hellwig error = blk_status_to_errno(iclog->ic_bio.bi_status);
1384366fc4b8SChristoph Hellwig #ifdef DEBUG
1385366fc4b8SChristoph Hellwig /* treat writes with injected CRC errors as failed */
1386366fc4b8SChristoph Hellwig if (iclog->ic_fail_crc)
138779b54d9bSChristoph Hellwig error = -EIO;
1388366fc4b8SChristoph Hellwig #endif
1389609adfc2SBrian Foster
1390366fc4b8SChristoph Hellwig /*
1391366fc4b8SChristoph Hellwig * Race to shutdown the filesystem if we see an error.
1392366fc4b8SChristoph Hellwig */
139379b54d9bSChristoph Hellwig if (XFS_TEST_ERROR(error, log->l_mp, XFS_ERRTAG_IODONE_IOERR)) {
139479b54d9bSChristoph Hellwig xfs_alert(log->l_mp, "log I/O error %d", error);
1395b5f17becSDave Chinner xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
13961da177e4SLinus Torvalds }
13973db296f3SDavid Chinner
139812e6a0f4SChristoph Hellwig xlog_state_done_syncing(iclog);
139979b54d9bSChristoph Hellwig bio_uninit(&iclog->ic_bio);
14009c23ecccSDave Chinner
14011da177e4SLinus Torvalds /*
140279b54d9bSChristoph Hellwig * Drop the lock to signal that we are done. Nothing references the
140379b54d9bSChristoph Hellwig * iclog after this, so an unmount waiting on this lock can now tear it
140479b54d9bSChristoph Hellwig * down safely. As such, it is unsafe to reference the iclog after the
140579b54d9bSChristoph Hellwig * unlock as we could race with it being freed.
14061da177e4SLinus Torvalds */
140779b54d9bSChristoph Hellwig up(&iclog->ic_sema);
1408c3f8fc73SDave Chinner }
14091da177e4SLinus Torvalds
14101da177e4SLinus Torvalds /*
14111da177e4SLinus Torvalds * Return size of each in-core log record buffer.
14121da177e4SLinus Torvalds *
14139da096fdSMalcolm Parsons * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
14141da177e4SLinus Torvalds *
14151da177e4SLinus Torvalds * If the filesystem blocksize is too large, we may need to choose a
14161da177e4SLinus Torvalds * larger size since the directory code currently logs entire blocks.
14171da177e4SLinus Torvalds */
14181da177e4SLinus Torvalds STATIC void
xlog_get_iclog_buffer_size(struct xfs_mount * mp,struct xlog * log)14199a8d2fdbSMark Tinguely xlog_get_iclog_buffer_size(
14209a8d2fdbSMark Tinguely struct xfs_mount *mp,
14219a8d2fdbSMark Tinguely struct xlog *log)
14221da177e4SLinus Torvalds {
14231cb51258SEric Sandeen if (mp->m_logbufs <= 0)
14244f62282aSChristoph Hellwig mp->m_logbufs = XLOG_MAX_ICLOGS;
14254f62282aSChristoph Hellwig if (mp->m_logbsize <= 0)
14264f62282aSChristoph Hellwig mp->m_logbsize = XLOG_BIG_RECORD_BSIZE;
14274f62282aSChristoph Hellwig
14281da177e4SLinus Torvalds log->l_iclog_bufs = mp->m_logbufs;
14294f62282aSChristoph Hellwig log->l_iclog_size = mp->m_logbsize;
14301da177e4SLinus Torvalds
14311da177e4SLinus Torvalds /*
14324f62282aSChristoph Hellwig * # headers = size / 32k - one header holds cycles from 32k of data.
14331da177e4SLinus Torvalds */
14344f62282aSChristoph Hellwig log->l_iclog_heads =
14354f62282aSChristoph Hellwig DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE);
14364f62282aSChristoph Hellwig log->l_iclog_hsize = log->l_iclog_heads << BBSHIFT;
14371da177e4SLinus Torvalds }
14381da177e4SLinus Torvalds
1439f661f1e0SDave Chinner void
xfs_log_work_queue(struct xfs_mount * mp)1440f661f1e0SDave Chinner xfs_log_work_queue(
1441f661f1e0SDave Chinner struct xfs_mount *mp)
1442f661f1e0SDave Chinner {
1443696a5620SBrian Foster queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work,
1444f661f1e0SDave Chinner msecs_to_jiffies(xfs_syncd_centisecs * 10));
1445f661f1e0SDave Chinner }
1446f661f1e0SDave Chinner
1447f661f1e0SDave Chinner /*
14482b73a2c8SDarrick J. Wong * Clear the log incompat flags if we have the opportunity.
14492b73a2c8SDarrick J. Wong *
14502b73a2c8SDarrick J. Wong * This only happens if we're about to log the second dummy transaction as part
14512b73a2c8SDarrick J. Wong * of covering the log and we can get the log incompat feature usage lock.
14522b73a2c8SDarrick J. Wong */
14532b73a2c8SDarrick J. Wong static inline void
xlog_clear_incompat(struct xlog * log)14542b73a2c8SDarrick J. Wong xlog_clear_incompat(
14552b73a2c8SDarrick J. Wong struct xlog *log)
14562b73a2c8SDarrick J. Wong {
14572b73a2c8SDarrick J. Wong struct xfs_mount *mp = log->l_mp;
14582b73a2c8SDarrick J. Wong
14592b73a2c8SDarrick J. Wong if (!xfs_sb_has_incompat_log_feature(&mp->m_sb,
14602b73a2c8SDarrick J. Wong XFS_SB_FEAT_INCOMPAT_LOG_ALL))
14612b73a2c8SDarrick J. Wong return;
14622b73a2c8SDarrick J. Wong
14632b73a2c8SDarrick J. Wong if (log->l_covered_state != XLOG_STATE_COVER_DONE2)
14642b73a2c8SDarrick J. Wong return;
14652b73a2c8SDarrick J. Wong
14662b73a2c8SDarrick J. Wong if (!down_write_trylock(&log->l_incompat_users))
14672b73a2c8SDarrick J. Wong return;
14682b73a2c8SDarrick J. Wong
14692b73a2c8SDarrick J. Wong xfs_clear_incompat_log_features(mp);
14702b73a2c8SDarrick J. Wong up_write(&log->l_incompat_users);
14712b73a2c8SDarrick J. Wong }
14722b73a2c8SDarrick J. Wong
14732b73a2c8SDarrick J. Wong /*
1474f661f1e0SDave Chinner * Every sync period we need to unpin all items in the AIL and push them to
1475f661f1e0SDave Chinner * disk. If there is nothing dirty, then we might need to cover the log to
1476f661f1e0SDave Chinner * indicate that the filesystem is idle.
1477f661f1e0SDave Chinner */
14780d5a75e9SEric Sandeen static void
xfs_log_worker(struct work_struct * work)1479f661f1e0SDave Chinner xfs_log_worker(
1480f661f1e0SDave Chinner struct work_struct *work)
1481f661f1e0SDave Chinner {
1482f661f1e0SDave Chinner struct xlog *log = container_of(to_delayed_work(work),
1483f661f1e0SDave Chinner struct xlog, l_work);
1484f661f1e0SDave Chinner struct xfs_mount *mp = log->l_mp;
1485f661f1e0SDave Chinner
1486f661f1e0SDave Chinner /* dgc: errors ignored - not fatal and nowhere to report them */
148737444fc4SBrian Foster if (xfs_fs_writable(mp, SB_FREEZE_WRITE) && xfs_log_need_covered(mp)) {
148861e63ecbSDave Chinner /*
148961e63ecbSDave Chinner * Dump a transaction into the log that contains no real change.
149061e63ecbSDave Chinner * This is needed to stamp the current tail LSN into the log
149161e63ecbSDave Chinner * during the covering operation.
149261e63ecbSDave Chinner *
149361e63ecbSDave Chinner * We cannot use an inode here for this - that will push dirty
149461e63ecbSDave Chinner * state back up into the VFS and then periodic inode flushing
149561e63ecbSDave Chinner * will prevent log covering from making progress. Hence we
149661e63ecbSDave Chinner * synchronously log the superblock instead to ensure the
149761e63ecbSDave Chinner * superblock is immediately unpinned and can be written back.
149861e63ecbSDave Chinner */
14992b73a2c8SDarrick J. Wong xlog_clear_incompat(log);
150061e63ecbSDave Chinner xfs_sync_sb(mp, true);
150161e63ecbSDave Chinner } else
1502f661f1e0SDave Chinner xfs_log_force(mp, 0);
1503f661f1e0SDave Chinner
1504f661f1e0SDave Chinner /* start pushing all the metadata that is currently dirty */
1505f661f1e0SDave Chinner xfs_ail_push_all(mp->m_ail);
1506f661f1e0SDave Chinner
1507f661f1e0SDave Chinner /* queue us up again */
1508f661f1e0SDave Chinner xfs_log_work_queue(mp);
1509f661f1e0SDave Chinner }
1510f661f1e0SDave Chinner
15111da177e4SLinus Torvalds /*
15121da177e4SLinus Torvalds * This routine initializes some of the log structure for a given mount point.
15131da177e4SLinus Torvalds * Its primary purpose is to fill in enough, so recovery can occur. However,
15141da177e4SLinus Torvalds * some other stuff may be filled in too.
15151da177e4SLinus Torvalds */
15169a8d2fdbSMark Tinguely STATIC struct xlog *
xlog_alloc_log(struct xfs_mount * mp,struct xfs_buftarg * log_target,xfs_daddr_t blk_offset,int num_bblks)15179a8d2fdbSMark Tinguely xlog_alloc_log(
15189a8d2fdbSMark Tinguely struct xfs_mount *mp,
15199a8d2fdbSMark Tinguely struct xfs_buftarg *log_target,
15201da177e4SLinus Torvalds xfs_daddr_t blk_offset,
15211da177e4SLinus Torvalds int num_bblks)
15221da177e4SLinus Torvalds {
15239a8d2fdbSMark Tinguely struct xlog *log;
15241da177e4SLinus Torvalds xlog_rec_header_t *head;
15251da177e4SLinus Torvalds xlog_in_core_t **iclogp;
15261da177e4SLinus Torvalds xlog_in_core_t *iclog, *prev_iclog=NULL;
15271da177e4SLinus Torvalds int i;
15282451337dSDave Chinner int error = -ENOMEM;
152969ce58f0SAlex Elder uint log2_size = 0;
15301da177e4SLinus Torvalds
15319a8d2fdbSMark Tinguely log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL);
1532a6cb767eSDave Chinner if (!log) {
1533a0fa2b67SDave Chinner xfs_warn(mp, "Log allocation failed: No memory!");
1534a6cb767eSDave Chinner goto out;
1535a6cb767eSDave Chinner }
15361da177e4SLinus Torvalds
15371da177e4SLinus Torvalds log->l_mp = mp;
15381da177e4SLinus Torvalds log->l_targ = log_target;
15391da177e4SLinus Torvalds log->l_logsize = BBTOB(num_bblks);
15401da177e4SLinus Torvalds log->l_logBBstart = blk_offset;
15411da177e4SLinus Torvalds log->l_logBBsize = num_bblks;
15421da177e4SLinus Torvalds log->l_covered_state = XLOG_STATE_COVER_IDLE;
1543e1d06e5fSDave Chinner set_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
1544f661f1e0SDave Chinner INIT_DELAYED_WORK(&log->l_work, xfs_log_worker);
1545*cd3c2cf3SDarrick J. Wong INIT_LIST_HEAD(&log->r_dfops);
15461da177e4SLinus Torvalds
15471da177e4SLinus Torvalds log->l_prev_block = -1;
15481da177e4SLinus Torvalds /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
15491c3cb9ecSDave Chinner xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
15501c3cb9ecSDave Chinner xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
15511da177e4SLinus Torvalds log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1552c303c5b8SChristoph Hellwig
155338c26bfdSDave Chinner if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1)
1554a6a65fefSDave Chinner log->l_iclog_roundoff = mp->m_sb.sb_logsunit;
1555a6a65fefSDave Chinner else
1556a6a65fefSDave Chinner log->l_iclog_roundoff = BBSIZE;
1557a6a65fefSDave Chinner
1558c303c5b8SChristoph Hellwig xlog_grant_head_init(&log->l_reserve_head);
1559c303c5b8SChristoph Hellwig xlog_grant_head_init(&log->l_write_head);
15601da177e4SLinus Torvalds
15612451337dSDave Chinner error = -EFSCORRUPTED;
156238c26bfdSDave Chinner if (xfs_has_sector(mp)) {
156369ce58f0SAlex Elder log2_size = mp->m_sb.sb_logsectlog;
156469ce58f0SAlex Elder if (log2_size < BBSHIFT) {
1565a0fa2b67SDave Chinner xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1566a0fa2b67SDave Chinner log2_size, BBSHIFT);
156769ce58f0SAlex Elder goto out_free_log;
156869ce58f0SAlex Elder }
156969ce58f0SAlex Elder
157069ce58f0SAlex Elder log2_size -= BBSHIFT;
157169ce58f0SAlex Elder if (log2_size > mp->m_sectbb_log) {
1572a0fa2b67SDave Chinner xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1573a0fa2b67SDave Chinner log2_size, mp->m_sectbb_log);
1574a6cb767eSDave Chinner goto out_free_log;
1575a6cb767eSDave Chinner }
1576a6cb767eSDave Chinner
15771da177e4SLinus Torvalds /* for larger sector sizes, must have v2 or external log */
157869ce58f0SAlex Elder if (log2_size && log->l_logBBstart > 0 &&
157938c26bfdSDave Chinner !xfs_has_logv2(mp)) {
1580a0fa2b67SDave Chinner xfs_warn(mp,
1581a0fa2b67SDave Chinner "log sector size (0x%x) invalid for configuration.",
1582a0fa2b67SDave Chinner log2_size);
1583a6cb767eSDave Chinner goto out_free_log;
1584a6cb767eSDave Chinner }
15851da177e4SLinus Torvalds }
158669ce58f0SAlex Elder log->l_sectBBsize = 1 << log2_size;
15871da177e4SLinus Torvalds
15882b73a2c8SDarrick J. Wong init_rwsem(&log->l_incompat_users);
15892b73a2c8SDarrick J. Wong
15901da177e4SLinus Torvalds xlog_get_iclog_buffer_size(mp, log);
15911da177e4SLinus Torvalds
1592007c61c6SEric Sandeen spin_lock_init(&log->l_icloglock);
1593eb40a875SDave Chinner init_waitqueue_head(&log->l_flush_wait);
15941da177e4SLinus Torvalds
15951da177e4SLinus Torvalds iclogp = &log->l_iclog;
15961da177e4SLinus Torvalds /*
15971da177e4SLinus Torvalds * The amount of memory to allocate for the iclog structure is
15981da177e4SLinus Torvalds * rather funky due to the way the structure is defined. It is
15991da177e4SLinus Torvalds * done this way so that we can use different sizes for machines
16001da177e4SLinus Torvalds * with different amounts of memory. See the definition of
16011da177e4SLinus Torvalds * xlog_in_core_t in xfs_log_priv.h for details.
16021da177e4SLinus Torvalds */
16031da177e4SLinus Torvalds ASSERT(log->l_iclog_size >= 4096);
16041da177e4SLinus Torvalds for (i = 0; i < log->l_iclog_bufs; i++) {
160589b171acSChristoph Hellwig size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) *
160689b171acSChristoph Hellwig sizeof(struct bio_vec);
160779b54d9bSChristoph Hellwig
160879b54d9bSChristoph Hellwig iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL);
160979b54d9bSChristoph Hellwig if (!iclog)
1610644c3567SDave Chinner goto out_free_iclog;
1611644c3567SDave Chinner
161279b54d9bSChristoph Hellwig *iclogp = iclog;
16131da177e4SLinus Torvalds iclog->ic_prev = prev_iclog;
16141da177e4SLinus Torvalds prev_iclog = iclog;
16151fa40b01SChristoph Hellwig
1616d634525dSDave Chinner iclog->ic_data = kvzalloc(log->l_iclog_size,
1617d634525dSDave Chinner GFP_KERNEL | __GFP_RETRY_MAYFAIL);
161879b54d9bSChristoph Hellwig if (!iclog->ic_data)
1619644c3567SDave Chinner goto out_free_iclog;
16201da177e4SLinus Torvalds head = &iclog->ic_header;
16211da177e4SLinus Torvalds memset(head, 0, sizeof(xlog_rec_header_t));
1622b53e675dSChristoph Hellwig head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1623b53e675dSChristoph Hellwig head->h_version = cpu_to_be32(
162438c26bfdSDave Chinner xfs_has_logv2(log->l_mp) ? 2 : 1);
1625b53e675dSChristoph Hellwig head->h_size = cpu_to_be32(log->l_iclog_size);
16261da177e4SLinus Torvalds /* new fields */
1627b53e675dSChristoph Hellwig head->h_fmt = cpu_to_be32(XLOG_FMT);
16281da177e4SLinus Torvalds memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
16291da177e4SLinus Torvalds
163079b54d9bSChristoph Hellwig iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize;
16311da177e4SLinus Torvalds iclog->ic_state = XLOG_STATE_ACTIVE;
16321da177e4SLinus Torvalds iclog->ic_log = log;
1633114d23aaSDavid Chinner atomic_set(&iclog->ic_refcnt, 0);
163489ae379dSChristoph Hellwig INIT_LIST_HEAD(&iclog->ic_callbacks);
1635decb545fSChristoph Hellwig iclog->ic_datap = (void *)iclog->ic_data + log->l_iclog_hsize;
16361da177e4SLinus Torvalds
1637eb40a875SDave Chinner init_waitqueue_head(&iclog->ic_force_wait);
1638eb40a875SDave Chinner init_waitqueue_head(&iclog->ic_write_wait);
163979b54d9bSChristoph Hellwig INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work);
164079b54d9bSChristoph Hellwig sema_init(&iclog->ic_sema, 1);
16411da177e4SLinus Torvalds
16421da177e4SLinus Torvalds iclogp = &iclog->ic_next;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds *iclogp = log->l_iclog; /* complete ring */
16451da177e4SLinus Torvalds log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
16461da177e4SLinus Torvalds
16471058d0f5SChristoph Hellwig log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s",
164805a302a1SDarrick J. Wong XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM |
164905a302a1SDarrick J. Wong WQ_HIGHPRI),
165005a302a1SDarrick J. Wong 0, mp->m_super->s_id);
16511058d0f5SChristoph Hellwig if (!log->l_ioend_workqueue)
16521058d0f5SChristoph Hellwig goto out_free_iclog;
16531058d0f5SChristoph Hellwig
165471e330b5SDave Chinner error = xlog_cil_init(log);
165571e330b5SDave Chinner if (error)
16561058d0f5SChristoph Hellwig goto out_destroy_workqueue;
16571da177e4SLinus Torvalds return log;
1658644c3567SDave Chinner
16591058d0f5SChristoph Hellwig out_destroy_workqueue:
16601058d0f5SChristoph Hellwig destroy_workqueue(log->l_ioend_workqueue);
1661644c3567SDave Chinner out_free_iclog:
1662644c3567SDave Chinner for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1663644c3567SDave Chinner prev_iclog = iclog->ic_next;
166479b54d9bSChristoph Hellwig kmem_free(iclog->ic_data);
1665644c3567SDave Chinner kmem_free(iclog);
1666798a9cadSBrian Foster if (prev_iclog == log->l_iclog)
1667798a9cadSBrian Foster break;
1668644c3567SDave Chinner }
1669644c3567SDave Chinner out_free_log:
1670644c3567SDave Chinner kmem_free(log);
1671a6cb767eSDave Chinner out:
16722451337dSDave Chinner return ERR_PTR(error);
16731da177e4SLinus Torvalds } /* xlog_alloc_log */
16741da177e4SLinus Torvalds
16751da177e4SLinus Torvalds /*
1676ed1575daSDarrick J. Wong * Compute the LSN that we'd need to push the log tail towards in order to have
1677ed1575daSDarrick J. Wong * (a) enough on-disk log space to log the number of bytes specified, (b) at
1678ed1575daSDarrick J. Wong * least 25% of the log space free, and (c) at least 256 blocks free. If the
1679ed1575daSDarrick J. Wong * log free space already meets all three thresholds, this function returns
1680ed1575daSDarrick J. Wong * NULLCOMMITLSN.
16811da177e4SLinus Torvalds */
1682ed1575daSDarrick J. Wong xfs_lsn_t
xlog_grant_push_threshold(struct xlog * log,int need_bytes)1683ed1575daSDarrick J. Wong xlog_grant_push_threshold(
1684ad223e60SMark Tinguely struct xlog *log,
16851da177e4SLinus Torvalds int need_bytes)
16861da177e4SLinus Torvalds {
16872ced19cbSDave Chinner xfs_lsn_t threshold_lsn = 0;
168884f3c683SDave Chinner xfs_lsn_t last_sync_lsn;
16892ced19cbSDave Chinner int free_blocks;
16902ced19cbSDave Chinner int free_bytes;
16912ced19cbSDave Chinner int threshold_block;
16922ced19cbSDave Chinner int threshold_cycle;
16931da177e4SLinus Torvalds int free_threshold;
16941da177e4SLinus Torvalds
16951da177e4SLinus Torvalds ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
16961da177e4SLinus Torvalds
169728496968SChristoph Hellwig free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
16981da177e4SLinus Torvalds free_blocks = BTOBBT(free_bytes);
16991da177e4SLinus Torvalds
17001da177e4SLinus Torvalds /*
17011da177e4SLinus Torvalds * Set the threshold for the minimum number of free blocks in the
17021da177e4SLinus Torvalds * log to the maximum of what the caller needs, one quarter of the
17031da177e4SLinus Torvalds * log, and 256 blocks.
17041da177e4SLinus Torvalds */
17051da177e4SLinus Torvalds free_threshold = BTOBB(need_bytes);
17069bb54cb5SDave Chinner free_threshold = max(free_threshold, (log->l_logBBsize >> 2));
17079bb54cb5SDave Chinner free_threshold = max(free_threshold, 256);
17082ced19cbSDave Chinner if (free_blocks >= free_threshold)
1709ed1575daSDarrick J. Wong return NULLCOMMITLSN;
17102ced19cbSDave Chinner
17111c3cb9ecSDave Chinner xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
17121c3cb9ecSDave Chinner &threshold_block);
17131c3cb9ecSDave Chinner threshold_block += free_threshold;
17141da177e4SLinus Torvalds if (threshold_block >= log->l_logBBsize) {
17151da177e4SLinus Torvalds threshold_block -= log->l_logBBsize;
17161da177e4SLinus Torvalds threshold_cycle += 1;
17171da177e4SLinus Torvalds }
17182ced19cbSDave Chinner threshold_lsn = xlog_assign_lsn(threshold_cycle,
17192ced19cbSDave Chinner threshold_block);
17202ced19cbSDave Chinner /*
17212ced19cbSDave Chinner * Don't pass in an lsn greater than the lsn of the last
172284f3c683SDave Chinner * log record known to be on disk. Use a snapshot of the last sync lsn
172384f3c683SDave Chinner * so that it doesn't change between the compare and the set.
17241da177e4SLinus Torvalds */
172584f3c683SDave Chinner last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
172684f3c683SDave Chinner if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
172784f3c683SDave Chinner threshold_lsn = last_sync_lsn;
17281da177e4SLinus Torvalds
1729ed1575daSDarrick J. Wong return threshold_lsn;
1730ed1575daSDarrick J. Wong }
1731ed1575daSDarrick J. Wong
1732ed1575daSDarrick J. Wong /*
1733ed1575daSDarrick J. Wong * Push the tail of the log if we need to do so to maintain the free log space
1734ed1575daSDarrick J. Wong * thresholds set out by xlog_grant_push_threshold. We may need to adopt a
1735ed1575daSDarrick J. Wong * policy which pushes on an lsn which is further along in the log once we
1736ed1575daSDarrick J. Wong * reach the high water mark. In this manner, we would be creating a low water
1737ed1575daSDarrick J. Wong * mark.
1738ed1575daSDarrick J. Wong */
1739ed1575daSDarrick J. Wong STATIC void
xlog_grant_push_ail(struct xlog * log,int need_bytes)1740ed1575daSDarrick J. Wong xlog_grant_push_ail(
1741ed1575daSDarrick J. Wong struct xlog *log,
1742ed1575daSDarrick J. Wong int need_bytes)
1743ed1575daSDarrick J. Wong {
1744ed1575daSDarrick J. Wong xfs_lsn_t threshold_lsn;
1745ed1575daSDarrick J. Wong
1746ed1575daSDarrick J. Wong threshold_lsn = xlog_grant_push_threshold(log, need_bytes);
17472039a272SDave Chinner if (threshold_lsn == NULLCOMMITLSN || xlog_is_shutdown(log))
1748ed1575daSDarrick J. Wong return;
1749ed1575daSDarrick J. Wong
17501da177e4SLinus Torvalds /*
17511da177e4SLinus Torvalds * Get the transaction layer to kick the dirty buffers out to
17521da177e4SLinus Torvalds * disk asynchronously. No point in trying to do this if
17531da177e4SLinus Torvalds * the filesystem is shutting down.
17541da177e4SLinus Torvalds */
1755fd074841SDave Chinner xfs_ail_push(log->l_ailp, threshold_lsn);
17562ced19cbSDave Chinner }
17571da177e4SLinus Torvalds
1758873ff550SChristoph Hellwig /*
17590e446be4SChristoph Hellwig * Stamp cycle number in every block
17600e446be4SChristoph Hellwig */
17610e446be4SChristoph Hellwig STATIC void
xlog_pack_data(struct xlog * log,struct xlog_in_core * iclog,int roundoff)17620e446be4SChristoph Hellwig xlog_pack_data(
17630e446be4SChristoph Hellwig struct xlog *log,
17640e446be4SChristoph Hellwig struct xlog_in_core *iclog,
17650e446be4SChristoph Hellwig int roundoff)
17660e446be4SChristoph Hellwig {
17670e446be4SChristoph Hellwig int i, j, k;
17680e446be4SChristoph Hellwig int size = iclog->ic_offset + roundoff;
17690e446be4SChristoph Hellwig __be32 cycle_lsn;
1770b2a922cdSChristoph Hellwig char *dp;
17710e446be4SChristoph Hellwig
17720e446be4SChristoph Hellwig cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
17730e446be4SChristoph Hellwig
17740e446be4SChristoph Hellwig dp = iclog->ic_datap;
17750e446be4SChristoph Hellwig for (i = 0; i < BTOBB(size); i++) {
17760e446be4SChristoph Hellwig if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE))
17770e446be4SChristoph Hellwig break;
17780e446be4SChristoph Hellwig iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
17790e446be4SChristoph Hellwig *(__be32 *)dp = cycle_lsn;
17800e446be4SChristoph Hellwig dp += BBSIZE;
17810e446be4SChristoph Hellwig }
17820e446be4SChristoph Hellwig
178338c26bfdSDave Chinner if (xfs_has_logv2(log->l_mp)) {
17840e446be4SChristoph Hellwig xlog_in_core_2_t *xhdr = iclog->ic_data;
17850e446be4SChristoph Hellwig
17860e446be4SChristoph Hellwig for ( ; i < BTOBB(size); i++) {
17870e446be4SChristoph Hellwig j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
17880e446be4SChristoph Hellwig k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
17890e446be4SChristoph Hellwig xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
17900e446be4SChristoph Hellwig *(__be32 *)dp = cycle_lsn;
17910e446be4SChristoph Hellwig dp += BBSIZE;
17920e446be4SChristoph Hellwig }
17930e446be4SChristoph Hellwig
17940e446be4SChristoph Hellwig for (i = 1; i < log->l_iclog_heads; i++)
17950e446be4SChristoph Hellwig xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
17960e446be4SChristoph Hellwig }
17970e446be4SChristoph Hellwig }
17980e446be4SChristoph Hellwig
17990e446be4SChristoph Hellwig /*
18000e446be4SChristoph Hellwig * Calculate the checksum for a log buffer.
18010e446be4SChristoph Hellwig *
18020e446be4SChristoph Hellwig * This is a little more complicated than it should be because the various
18030e446be4SChristoph Hellwig * headers and the actual data are non-contiguous.
18040e446be4SChristoph Hellwig */
1805f9668a09SDave Chinner __le32
xlog_cksum(struct xlog * log,struct xlog_rec_header * rhead,char * dp,int size)18060e446be4SChristoph Hellwig xlog_cksum(
18070e446be4SChristoph Hellwig struct xlog *log,
18080e446be4SChristoph Hellwig struct xlog_rec_header *rhead,
18090e446be4SChristoph Hellwig char *dp,
18100e446be4SChristoph Hellwig int size)
18110e446be4SChristoph Hellwig {
1812c8ce540dSDarrick J. Wong uint32_t crc;
18130e446be4SChristoph Hellwig
18140e446be4SChristoph Hellwig /* first generate the crc for the record header ... */
1815cae028dfSDave Chinner crc = xfs_start_cksum_update((char *)rhead,
18160e446be4SChristoph Hellwig sizeof(struct xlog_rec_header),
18170e446be4SChristoph Hellwig offsetof(struct xlog_rec_header, h_crc));
18180e446be4SChristoph Hellwig
18190e446be4SChristoph Hellwig /* ... then for additional cycle data for v2 logs ... */
182038c26bfdSDave Chinner if (xfs_has_logv2(log->l_mp)) {
18210e446be4SChristoph Hellwig union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead;
18220e446be4SChristoph Hellwig int i;
1823a3f20014SBrian Foster int xheads;
18240e446be4SChristoph Hellwig
18250c771b99SGao Xiang xheads = DIV_ROUND_UP(size, XLOG_HEADER_CYCLE_SIZE);
1826a3f20014SBrian Foster
1827a3f20014SBrian Foster for (i = 1; i < xheads; i++) {
18280e446be4SChristoph Hellwig crc = crc32c(crc, &xhdr[i].hic_xheader,
18290e446be4SChristoph Hellwig sizeof(struct xlog_rec_ext_header));
18300e446be4SChristoph Hellwig }
18310e446be4SChristoph Hellwig }
18320e446be4SChristoph Hellwig
18330e446be4SChristoph Hellwig /* ... and finally for the payload */
18340e446be4SChristoph Hellwig crc = crc32c(crc, dp, size);
18350e446be4SChristoph Hellwig
18360e446be4SChristoph Hellwig return xfs_end_cksum(crc);
18370e446be4SChristoph Hellwig }
18380e446be4SChristoph Hellwig
183979b54d9bSChristoph Hellwig static void
xlog_bio_end_io(struct bio * bio)184079b54d9bSChristoph Hellwig xlog_bio_end_io(
184179b54d9bSChristoph Hellwig struct bio *bio)
184279b54d9bSChristoph Hellwig {
184379b54d9bSChristoph Hellwig struct xlog_in_core *iclog = bio->bi_private;
184479b54d9bSChristoph Hellwig
18451058d0f5SChristoph Hellwig queue_work(iclog->ic_log->l_ioend_workqueue,
184679b54d9bSChristoph Hellwig &iclog->ic_end_io_work);
184779b54d9bSChristoph Hellwig }
184879b54d9bSChristoph Hellwig
1849842a42d1SBrian Foster static int
xlog_map_iclog_data(struct bio * bio,void * data,size_t count)185079b54d9bSChristoph Hellwig xlog_map_iclog_data(
185179b54d9bSChristoph Hellwig struct bio *bio,
185279b54d9bSChristoph Hellwig void *data,
185379b54d9bSChristoph Hellwig size_t count)
185479b54d9bSChristoph Hellwig {
185579b54d9bSChristoph Hellwig do {
185679b54d9bSChristoph Hellwig struct page *page = kmem_to_page(data);
185779b54d9bSChristoph Hellwig unsigned int off = offset_in_page(data);
185879b54d9bSChristoph Hellwig size_t len = min_t(size_t, count, PAGE_SIZE - off);
185979b54d9bSChristoph Hellwig
1860842a42d1SBrian Foster if (bio_add_page(bio, page, len, off) != len)
1861842a42d1SBrian Foster return -EIO;
186279b54d9bSChristoph Hellwig
186379b54d9bSChristoph Hellwig data += len;
186479b54d9bSChristoph Hellwig count -= len;
186579b54d9bSChristoph Hellwig } while (count);
1866842a42d1SBrian Foster
1867842a42d1SBrian Foster return 0;
186879b54d9bSChristoph Hellwig }
186979b54d9bSChristoph Hellwig
187094860a30SChristoph Hellwig STATIC void
xlog_write_iclog(struct xlog * log,struct xlog_in_core * iclog,uint64_t bno,unsigned int count)187194860a30SChristoph Hellwig xlog_write_iclog(
187294860a30SChristoph Hellwig struct xlog *log,
187394860a30SChristoph Hellwig struct xlog_in_core *iclog,
187494860a30SChristoph Hellwig uint64_t bno,
1875eef983ffSDave Chinner unsigned int count)
1876873ff550SChristoph Hellwig {
187794860a30SChristoph Hellwig ASSERT(bno < log->l_logBBsize);
1878956f6daaSDave Chinner trace_xlog_iclog_write(iclog, _RET_IP_);
187994860a30SChristoph Hellwig
188094860a30SChristoph Hellwig /*
188194860a30SChristoph Hellwig * We lock the iclogbufs here so that we can serialise against I/O
188294860a30SChristoph Hellwig * completion during unmount. We might be processing a shutdown
188394860a30SChristoph Hellwig * triggered during unmount, and that can occur asynchronously to the
188494860a30SChristoph Hellwig * unmount thread, and hence we need to ensure that completes before
188594860a30SChristoph Hellwig * tearing down the iclogbufs. Hence we need to hold the buffer lock
188694860a30SChristoph Hellwig * across the log IO to archieve that.
188794860a30SChristoph Hellwig */
188879b54d9bSChristoph Hellwig down(&iclog->ic_sema);
18895112e206SDave Chinner if (xlog_is_shutdown(log)) {
1890873ff550SChristoph Hellwig /*
1891873ff550SChristoph Hellwig * It would seem logical to return EIO here, but we rely on
1892873ff550SChristoph Hellwig * the log state machine to propagate I/O errors instead of
189379b54d9bSChristoph Hellwig * doing it here. We kick of the state machine and unlock
189479b54d9bSChristoph Hellwig * the buffer manually, the code needs to be kept in sync
189579b54d9bSChristoph Hellwig * with the I/O completion path.
1896873ff550SChristoph Hellwig */
1897c86562e6SLeah Rumancik goto sync;
1898873ff550SChristoph Hellwig }
1899873ff550SChristoph Hellwig
19002def2845SDave Chinner /*
19012def2845SDave Chinner * We use REQ_SYNC | REQ_IDLE here to tell the block layer the are more
19022def2845SDave Chinner * IOs coming immediately after this one. This prevents the block layer
19032def2845SDave Chinner * writeback throttle from throttling log writes behind background
19042def2845SDave Chinner * metadata writeback and causing priority inversions.
19052def2845SDave Chinner */
190649add496SChristoph Hellwig bio_init(&iclog->ic_bio, log->l_targ->bt_bdev, iclog->ic_bvec,
190749add496SChristoph Hellwig howmany(count, PAGE_SIZE),
190849add496SChristoph Hellwig REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_IDLE);
190949add496SChristoph Hellwig iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno;
191049add496SChristoph Hellwig iclog->ic_bio.bi_end_io = xlog_bio_end_io;
191149add496SChristoph Hellwig iclog->ic_bio.bi_private = iclog;
191249add496SChristoph Hellwig
1913b5d721eaSDave Chinner if (iclog->ic_flags & XLOG_ICL_NEED_FLUSH) {
191479b54d9bSChristoph Hellwig iclog->ic_bio.bi_opf |= REQ_PREFLUSH;
1915b5d721eaSDave Chinner /*
1916b5d721eaSDave Chinner * For external log devices, we also need to flush the data
1917b5d721eaSDave Chinner * device cache first to ensure all metadata writeback covered
1918b5d721eaSDave Chinner * by the LSN in this iclog is on stable storage. This is slow,
1919b5d721eaSDave Chinner * but it *must* complete before we issue the external log IO.
19207d839e32SDarrick J. Wong *
19217d839e32SDarrick J. Wong * If the flush fails, we cannot conclude that past metadata
19227d839e32SDarrick J. Wong * writeback from the log succeeded. Repeating the flush is
19237d839e32SDarrick J. Wong * not possible, hence we must shut down with log IO error to
19247d839e32SDarrick J. Wong * avoid shutdown re-entering this path and erroring out again.
1925b5d721eaSDave Chinner */
19267d839e32SDarrick J. Wong if (log->l_targ != log->l_mp->m_ddev_targp &&
1927c86562e6SLeah Rumancik blkdev_issue_flush(log->l_mp->m_ddev_targp->bt_bdev))
1928c86562e6SLeah Rumancik goto shutdown;
1929b5d721eaSDave Chinner }
1930eef983ffSDave Chinner if (iclog->ic_flags & XLOG_ICL_NEED_FUA)
1931eef983ffSDave Chinner iclog->ic_bio.bi_opf |= REQ_FUA;
1932b5d721eaSDave Chinner
1933eef983ffSDave Chinner iclog->ic_flags &= ~(XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA);
193479b54d9bSChristoph Hellwig
1935c86562e6SLeah Rumancik if (xlog_map_iclog_data(&iclog->ic_bio, iclog->ic_data, count))
1936c86562e6SLeah Rumancik goto shutdown;
1937c86562e6SLeah Rumancik
193879b54d9bSChristoph Hellwig if (is_vmalloc_addr(iclog->ic_data))
19392c68a1dfSChristoph Hellwig flush_kernel_vmap_range(iclog->ic_data, count);
194079b54d9bSChristoph Hellwig
194179b54d9bSChristoph Hellwig /*
194279b54d9bSChristoph Hellwig * If this log buffer would straddle the end of the log we will have
194379b54d9bSChristoph Hellwig * to split it up into two bios, so that we can continue at the start.
194479b54d9bSChristoph Hellwig */
194579b54d9bSChristoph Hellwig if (bno + BTOBB(count) > log->l_logBBsize) {
194679b54d9bSChristoph Hellwig struct bio *split;
194779b54d9bSChristoph Hellwig
194879b54d9bSChristoph Hellwig split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno,
194979b54d9bSChristoph Hellwig GFP_NOIO, &fs_bio_set);
195079b54d9bSChristoph Hellwig bio_chain(split, &iclog->ic_bio);
195179b54d9bSChristoph Hellwig submit_bio(split);
195279b54d9bSChristoph Hellwig
195379b54d9bSChristoph Hellwig /* restart at logical offset zero for the remainder */
195479b54d9bSChristoph Hellwig iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart;
195579b54d9bSChristoph Hellwig }
195679b54d9bSChristoph Hellwig
195779b54d9bSChristoph Hellwig submit_bio(&iclog->ic_bio);
1958c86562e6SLeah Rumancik return;
1959c86562e6SLeah Rumancik shutdown:
1960c86562e6SLeah Rumancik xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
1961c86562e6SLeah Rumancik sync:
1962c86562e6SLeah Rumancik xlog_state_done_syncing(iclog);
1963c86562e6SLeah Rumancik up(&iclog->ic_sema);
1964873ff550SChristoph Hellwig }
19651da177e4SLinus Torvalds
19661da177e4SLinus Torvalds /*
196756933848SChristoph Hellwig * We need to bump cycle number for the part of the iclog that is
196856933848SChristoph Hellwig * written to the start of the log. Watch out for the header magic
196956933848SChristoph Hellwig * number case, though.
197056933848SChristoph Hellwig */
197179b54d9bSChristoph Hellwig static void
xlog_split_iclog(struct xlog * log,void * data,uint64_t bno,unsigned int count)197256933848SChristoph Hellwig xlog_split_iclog(
197356933848SChristoph Hellwig struct xlog *log,
197456933848SChristoph Hellwig void *data,
197556933848SChristoph Hellwig uint64_t bno,
197656933848SChristoph Hellwig unsigned int count)
197756933848SChristoph Hellwig {
197856933848SChristoph Hellwig unsigned int split_offset = BBTOB(log->l_logBBsize - bno);
197956933848SChristoph Hellwig unsigned int i;
198056933848SChristoph Hellwig
198156933848SChristoph Hellwig for (i = split_offset; i < count; i += BBSIZE) {
198256933848SChristoph Hellwig uint32_t cycle = get_unaligned_be32(data + i);
198356933848SChristoph Hellwig
198456933848SChristoph Hellwig if (++cycle == XLOG_HEADER_MAGIC_NUM)
198556933848SChristoph Hellwig cycle++;
198656933848SChristoph Hellwig put_unaligned_be32(cycle, data + i);
198756933848SChristoph Hellwig }
198856933848SChristoph Hellwig }
198956933848SChristoph Hellwig
1990db0a6fafSChristoph Hellwig static int
xlog_calc_iclog_size(struct xlog * log,struct xlog_in_core * iclog,uint32_t * roundoff)1991db0a6fafSChristoph Hellwig xlog_calc_iclog_size(
1992db0a6fafSChristoph Hellwig struct xlog *log,
1993db0a6fafSChristoph Hellwig struct xlog_in_core *iclog,
1994db0a6fafSChristoph Hellwig uint32_t *roundoff)
1995db0a6fafSChristoph Hellwig {
1996db0a6fafSChristoph Hellwig uint32_t count_init, count;
1997db0a6fafSChristoph Hellwig
1998db0a6fafSChristoph Hellwig /* Add for LR header */
1999db0a6fafSChristoph Hellwig count_init = log->l_iclog_hsize + iclog->ic_offset;
2000a6a65fefSDave Chinner count = roundup(count_init, log->l_iclog_roundoff);
2001db0a6fafSChristoph Hellwig
2002db0a6fafSChristoph Hellwig *roundoff = count - count_init;
2003db0a6fafSChristoph Hellwig
2004a6a65fefSDave Chinner ASSERT(count >= count_init);
2005a6a65fefSDave Chinner ASSERT(*roundoff < log->l_iclog_roundoff);
2006db0a6fafSChristoph Hellwig return count;
2007db0a6fafSChristoph Hellwig }
2008db0a6fafSChristoph Hellwig
200956933848SChristoph Hellwig /*
20101da177e4SLinus Torvalds * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
20111da177e4SLinus Torvalds * fashion. Previously, we should have moved the current iclog
20121da177e4SLinus Torvalds * ptr in the log to point to the next available iclog. This allows further
20131da177e4SLinus Torvalds * write to continue while this code syncs out an iclog ready to go.
20141da177e4SLinus Torvalds * Before an in-core log can be written out, the data section must be scanned
20151da177e4SLinus Torvalds * to save away the 1st word of each BBSIZE block into the header. We replace
20161da177e4SLinus Torvalds * it with the current cycle count. Each BBSIZE block is tagged with the
20171da177e4SLinus Torvalds * cycle count because there in an implicit assumption that drives will
20181da177e4SLinus Torvalds * guarantee that entire 512 byte blocks get written at once. In other words,
20191da177e4SLinus Torvalds * we can't have part of a 512 byte block written and part not written. By
20201da177e4SLinus Torvalds * tagging each block, we will know which blocks are valid when recovering
20211da177e4SLinus Torvalds * after an unclean shutdown.
20221da177e4SLinus Torvalds *
20231da177e4SLinus Torvalds * This routine is single threaded on the iclog. No other thread can be in
20241da177e4SLinus Torvalds * this routine with the same iclog. Changing contents of iclog can there-
20251da177e4SLinus Torvalds * fore be done without grabbing the state machine lock. Updating the global
20261da177e4SLinus Torvalds * log will require grabbing the lock though.
20271da177e4SLinus Torvalds *
20281da177e4SLinus Torvalds * The entire log manager uses a logical block numbering scheme. Only
202994860a30SChristoph Hellwig * xlog_write_iclog knows about the fact that the log may not start with
203094860a30SChristoph Hellwig * block zero on a given device.
20311da177e4SLinus Torvalds */
203294860a30SChristoph Hellwig STATIC void
xlog_sync(struct xlog * log,struct xlog_in_core * iclog,struct xlog_ticket * ticket)20339a8d2fdbSMark Tinguely xlog_sync(
20349a8d2fdbSMark Tinguely struct xlog *log,
2035d9f68777SDave Chinner struct xlog_in_core *iclog,
2036d9f68777SDave Chinner struct xlog_ticket *ticket)
20371da177e4SLinus Torvalds {
2038db0a6fafSChristoph Hellwig unsigned int count; /* byte count of bwrite */
2039db0a6fafSChristoph Hellwig unsigned int roundoff; /* roundoff to BB or stripe */
204094860a30SChristoph Hellwig uint64_t bno;
2041db0a6fafSChristoph Hellwig unsigned int size;
20421da177e4SLinus Torvalds
2043155cc6b7SDavid Chinner ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2044956f6daaSDave Chinner trace_xlog_iclog_sync(iclog, _RET_IP_);
20451da177e4SLinus Torvalds
2046db0a6fafSChristoph Hellwig count = xlog_calc_iclog_size(log, iclog, &roundoff);
20471da177e4SLinus Torvalds
2048d9f68777SDave Chinner /*
2049d9f68777SDave Chinner * If we have a ticket, account for the roundoff via the ticket
2050d9f68777SDave Chinner * reservation to avoid touching the hot grant heads needlessly.
2051d9f68777SDave Chinner * Otherwise, we have to move grant heads directly.
2052d9f68777SDave Chinner */
2053d9f68777SDave Chinner if (ticket) {
2054d9f68777SDave Chinner ticket->t_curr_res -= roundoff;
2055d9f68777SDave Chinner } else {
205628496968SChristoph Hellwig xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
205728496968SChristoph Hellwig xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
2058d9f68777SDave Chinner }
20591da177e4SLinus Torvalds
20601da177e4SLinus Torvalds /* put cycle number in every block */
20611da177e4SLinus Torvalds xlog_pack_data(log, iclog, roundoff);
20621da177e4SLinus Torvalds
20631da177e4SLinus Torvalds /* real byte length */
20640e446be4SChristoph Hellwig size = iclog->ic_offset;
206538c26bfdSDave Chinner if (xfs_has_logv2(log->l_mp))
20660e446be4SChristoph Hellwig size += roundoff;
20670e446be4SChristoph Hellwig iclog->ic_header.h_len = cpu_to_be32(size);
20681da177e4SLinus Torvalds
20699b0489c1SChristoph Hellwig XFS_STATS_INC(log->l_mp, xs_log_writes);
2070ff6d6af2SBill O'Donnell XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count));
20711da177e4SLinus Torvalds
207294860a30SChristoph Hellwig bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn));
207394860a30SChristoph Hellwig
20741da177e4SLinus Torvalds /* Do we need to split this write into 2 parts? */
2075eef983ffSDave Chinner if (bno + BTOBB(count) > log->l_logBBsize)
207679b54d9bSChristoph Hellwig xlog_split_iclog(log, &iclog->ic_header, bno, count);
20770e446be4SChristoph Hellwig
20780e446be4SChristoph Hellwig /* calculcate the checksum */
20790e446be4SChristoph Hellwig iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header,
20800e446be4SChristoph Hellwig iclog->ic_datap, size);
2081609adfc2SBrian Foster /*
2082609adfc2SBrian Foster * Intentionally corrupt the log record CRC based on the error injection
2083609adfc2SBrian Foster * frequency, if defined. This facilitates testing log recovery in the
2084609adfc2SBrian Foster * event of torn writes. Hence, set the IOABORT state to abort the log
2085609adfc2SBrian Foster * write on I/O completion and shutdown the fs. The subsequent mount
2086609adfc2SBrian Foster * detects the bad CRC and attempts to recover.
2087609adfc2SBrian Foster */
2088366fc4b8SChristoph Hellwig #ifdef DEBUG
20893e88a007SBrian Foster if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) {
2090e2a64192SChristoph Hellwig iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA);
2091366fc4b8SChristoph Hellwig iclog->ic_fail_crc = true;
2092609adfc2SBrian Foster xfs_warn(log->l_mp,
2093609adfc2SBrian Foster "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.",
2094609adfc2SBrian Foster be64_to_cpu(iclog->ic_header.h_lsn));
2095609adfc2SBrian Foster }
2096366fc4b8SChristoph Hellwig #endif
2097abca1f33SChristoph Hellwig xlog_verify_iclog(log, iclog, count);
2098eef983ffSDave Chinner xlog_write_iclog(log, iclog, bno, count);
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds
21011da177e4SLinus Torvalds /*
2102c41564b5SNathan Scott * Deallocate a log structure
21031da177e4SLinus Torvalds */
2104a8272ce0SDavid Chinner STATIC void
xlog_dealloc_log(struct xlog * log)21059a8d2fdbSMark Tinguely xlog_dealloc_log(
21069a8d2fdbSMark Tinguely struct xlog *log)
21071da177e4SLinus Torvalds {
21081da177e4SLinus Torvalds xlog_in_core_t *iclog, *next_iclog;
21091da177e4SLinus Torvalds int i;
21101da177e4SLinus Torvalds
211144396476SDave Chinner /*
21127561cea5SDarrick J. Wong * Destroy the CIL after waiting for iclog IO completion because an
21137561cea5SDarrick J. Wong * iclog EIO error will try to shut down the log, which accesses the
21147561cea5SDarrick J. Wong * CIL to wake up the waiters.
21157561cea5SDarrick J. Wong */
21167561cea5SDarrick J. Wong xlog_cil_destroy(log);
21177561cea5SDarrick J. Wong
21181da177e4SLinus Torvalds iclog = log->l_iclog;
21191da177e4SLinus Torvalds for (i = 0; i < log->l_iclog_bufs; i++) {
21201da177e4SLinus Torvalds next_iclog = iclog->ic_next;
212179b54d9bSChristoph Hellwig kmem_free(iclog->ic_data);
2122f0e2d93cSDenys Vlasenko kmem_free(iclog);
21231da177e4SLinus Torvalds iclog = next_iclog;
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds
21261da177e4SLinus Torvalds log->l_mp->m_log = NULL;
21271058d0f5SChristoph Hellwig destroy_workqueue(log->l_ioend_workqueue);
2128f0e2d93cSDenys Vlasenko kmem_free(log);
2129b843299bSDave Chinner }
21301da177e4SLinus Torvalds
21311da177e4SLinus Torvalds /*
21321da177e4SLinus Torvalds * Update counters atomically now that memcpy is done.
21331da177e4SLinus Torvalds */
21341da177e4SLinus Torvalds static inline void
xlog_state_finish_copy(struct xlog * log,struct xlog_in_core * iclog,int record_cnt,int copy_bytes)21359a8d2fdbSMark Tinguely xlog_state_finish_copy(
21369a8d2fdbSMark Tinguely struct xlog *log,
21379a8d2fdbSMark Tinguely struct xlog_in_core *iclog,
21381da177e4SLinus Torvalds int record_cnt,
21391da177e4SLinus Torvalds int copy_bytes)
21401da177e4SLinus Torvalds {
2141390aab0aSChristoph Hellwig lockdep_assert_held(&log->l_icloglock);
21421da177e4SLinus Torvalds
2143413d57c9SMarcin Slusarz be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
21441da177e4SLinus Torvalds iclog->ic_offset += copy_bytes;
2145390aab0aSChristoph Hellwig }
21461da177e4SLinus Torvalds
21471da177e4SLinus Torvalds /*
21487e9c6396STim Shimmin * print out info relating to regions written which consume
21497e9c6396STim Shimmin * the reservation
21507e9c6396STim Shimmin */
215171e330b5SDave Chinner void
xlog_print_tic_res(struct xfs_mount * mp,struct xlog_ticket * ticket)215271e330b5SDave Chinner xlog_print_tic_res(
215371e330b5SDave Chinner struct xfs_mount *mp,
215471e330b5SDave Chinner struct xlog_ticket *ticket)
21557e9c6396STim Shimmin {
21567d2d5653SBrian Foster xfs_warn(mp, "ticket reservation summary:");
2157c5141320SDave Chinner xfs_warn(mp, " unit res = %d bytes", ticket->t_unit_res);
2158c5141320SDave Chinner xfs_warn(mp, " current res = %d bytes", ticket->t_curr_res);
2159c5141320SDave Chinner xfs_warn(mp, " original count = %d", ticket->t_ocnt);
2160c5141320SDave Chinner xfs_warn(mp, " remaining count = %d", ticket->t_cnt);
21617e9c6396STim Shimmin }
21627e9c6396STim Shimmin
21637e9c6396STim Shimmin /*
2164d4ca1d55SBrian Foster * Print a summary of the transaction.
2165d4ca1d55SBrian Foster */
2166d4ca1d55SBrian Foster void
xlog_print_trans(struct xfs_trans * tp)2167d4ca1d55SBrian Foster xlog_print_trans(
2168d4ca1d55SBrian Foster struct xfs_trans *tp)
2169d4ca1d55SBrian Foster {
2170d4ca1d55SBrian Foster struct xfs_mount *mp = tp->t_mountp;
2171e6631f85SDave Chinner struct xfs_log_item *lip;
2172d4ca1d55SBrian Foster
2173d4ca1d55SBrian Foster /* dump core transaction and ticket info */
2174d4ca1d55SBrian Foster xfs_warn(mp, "transaction summary:");
21752c8f6265SBrian Foster xfs_warn(mp, " log res = %d", tp->t_log_res);
21762c8f6265SBrian Foster xfs_warn(mp, " log count = %d", tp->t_log_count);
2177d4ca1d55SBrian Foster xfs_warn(mp, " flags = 0x%x", tp->t_flags);
2178d4ca1d55SBrian Foster
2179d4ca1d55SBrian Foster xlog_print_tic_res(mp, tp->t_ticket);
2180d4ca1d55SBrian Foster
2181d4ca1d55SBrian Foster /* dump each log item */
2182e6631f85SDave Chinner list_for_each_entry(lip, &tp->t_items, li_trans) {
2183d4ca1d55SBrian Foster struct xfs_log_vec *lv = lip->li_lv;
2184d4ca1d55SBrian Foster struct xfs_log_iovec *vec;
2185d4ca1d55SBrian Foster int i;
2186d4ca1d55SBrian Foster
2187d4ca1d55SBrian Foster xfs_warn(mp, "log item: ");
2188d4ca1d55SBrian Foster xfs_warn(mp, " type = 0x%x", lip->li_type);
218922525c17SDave Chinner xfs_warn(mp, " flags = 0x%lx", lip->li_flags);
2190d4ca1d55SBrian Foster if (!lv)
2191d4ca1d55SBrian Foster continue;
2192d4ca1d55SBrian Foster xfs_warn(mp, " niovecs = %d", lv->lv_niovecs);
2193d4ca1d55SBrian Foster xfs_warn(mp, " size = %d", lv->lv_size);
2194d4ca1d55SBrian Foster xfs_warn(mp, " bytes = %d", lv->lv_bytes);
2195d4ca1d55SBrian Foster xfs_warn(mp, " buf len = %d", lv->lv_buf_len);
2196d4ca1d55SBrian Foster
2197d4ca1d55SBrian Foster /* dump each iovec for the log item */
2198d4ca1d55SBrian Foster vec = lv->lv_iovecp;
2199d4ca1d55SBrian Foster for (i = 0; i < lv->lv_niovecs; i++) {
2200d4ca1d55SBrian Foster int dumplen = min(vec->i_len, 32);
2201d4ca1d55SBrian Foster
2202d4ca1d55SBrian Foster xfs_warn(mp, " iovec[%d]", i);
2203d4ca1d55SBrian Foster xfs_warn(mp, " type = 0x%x", vec->i_type);
2204d4ca1d55SBrian Foster xfs_warn(mp, " len = %d", vec->i_len);
2205d4ca1d55SBrian Foster xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i);
2206244e3deaSkbuild test robot xfs_hex_dump(vec->i_addr, dumplen);
2207d4ca1d55SBrian Foster
2208d4ca1d55SBrian Foster vec++;
2209d4ca1d55SBrian Foster }
2210d4ca1d55SBrian Foster }
2211d4ca1d55SBrian Foster }
2212d4ca1d55SBrian Foster
2213db357078SDave Chinner static inline void
xlog_write_iovec(struct xlog_in_core * iclog,uint32_t * log_offset,void * data,uint32_t write_len,int * bytes_left,uint32_t * record_cnt,uint32_t * data_cnt)2214db357078SDave Chinner xlog_write_iovec(
2215b5203cd0SDave Chinner struct xlog_in_core *iclog,
2216db357078SDave Chinner uint32_t *log_offset,
2217db357078SDave Chinner void *data,
2218db357078SDave Chinner uint32_t write_len,
2219db357078SDave Chinner int *bytes_left,
2220db357078SDave Chinner uint32_t *record_cnt,
2221db357078SDave Chinner uint32_t *data_cnt)
2222b5203cd0SDave Chinner {
22231236bbe8SChristoph Hellwig ASSERT(*log_offset < iclog->ic_log->l_iclog_size);
2224db357078SDave Chinner ASSERT(*log_offset % sizeof(int32_t) == 0);
2225db357078SDave Chinner ASSERT(write_len % sizeof(int32_t) == 0);
2226db357078SDave Chinner
2227db357078SDave Chinner memcpy(iclog->ic_datap + *log_offset, data, write_len);
2228db357078SDave Chinner *log_offset += write_len;
2229db357078SDave Chinner *bytes_left -= write_len;
2230db357078SDave Chinner (*record_cnt)++;
2231db357078SDave Chinner *data_cnt += write_len;
2232db357078SDave Chinner }
2233db357078SDave Chinner
2234db357078SDave Chinner /*
2235db357078SDave Chinner * Write log vectors into a single iclog which is guaranteed by the caller
2236db357078SDave Chinner * to have enough space to write the entire log vector into.
2237db357078SDave Chinner */
2238db357078SDave Chinner static void
xlog_write_full(struct xfs_log_vec * lv,struct xlog_ticket * ticket,struct xlog_in_core * iclog,uint32_t * log_offset,uint32_t * len,uint32_t * record_cnt,uint32_t * data_cnt)2239db357078SDave Chinner xlog_write_full(
2240db357078SDave Chinner struct xfs_log_vec *lv,
2241db357078SDave Chinner struct xlog_ticket *ticket,
2242db357078SDave Chinner struct xlog_in_core *iclog,
2243db357078SDave Chinner uint32_t *log_offset,
2244db357078SDave Chinner uint32_t *len,
2245db357078SDave Chinner uint32_t *record_cnt,
2246db357078SDave Chinner uint32_t *data_cnt)
2247db357078SDave Chinner {
2248db357078SDave Chinner int index;
2249db357078SDave Chinner
2250ad3e3693SDave Chinner ASSERT(*log_offset + *len <= iclog->ic_size ||
2251ad3e3693SDave Chinner iclog->ic_state == XLOG_STATE_WANT_SYNC);
2252db357078SDave Chinner
2253db357078SDave Chinner /*
2254db357078SDave Chinner * Ordered log vectors have no regions to write so this
2255db357078SDave Chinner * loop will naturally skip them.
2256db357078SDave Chinner */
2257db357078SDave Chinner for (index = 0; index < lv->lv_niovecs; index++) {
2258db357078SDave Chinner struct xfs_log_iovec *reg = &lv->lv_iovecp[index];
2259db357078SDave Chinner struct xlog_op_header *ophdr = reg->i_addr;
2260db357078SDave Chinner
2261db357078SDave Chinner ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2262db357078SDave Chinner xlog_write_iovec(iclog, log_offset, reg->i_addr,
2263db357078SDave Chinner reg->i_len, len, record_cnt, data_cnt);
2264db357078SDave Chinner }
2265db357078SDave Chinner }
2266db357078SDave Chinner
2267b5203cd0SDave Chinner static int
xlog_write_get_more_iclog_space(struct xlog_ticket * ticket,struct xlog_in_core ** iclogp,uint32_t * log_offset,uint32_t len,uint32_t * record_cnt,uint32_t * data_cnt)2268ad3e3693SDave Chinner xlog_write_get_more_iclog_space(
2269b5203cd0SDave Chinner struct xlog_ticket *ticket,
2270ad3e3693SDave Chinner struct xlog_in_core **iclogp,
2271ad3e3693SDave Chinner uint32_t *log_offset,
2272ad3e3693SDave Chinner uint32_t len,
2273ad3e3693SDave Chinner uint32_t *record_cnt,
2274be8ddda5SDave Chinner uint32_t *data_cnt)
2275b5203cd0SDave Chinner {
2276ad3e3693SDave Chinner struct xlog_in_core *iclog = *iclogp;
2277ad3e3693SDave Chinner struct xlog *log = iclog->ic_log;
2278df732b29SChristoph Hellwig int error;
2279df732b29SChristoph Hellwig
2280390aab0aSChristoph Hellwig spin_lock(&log->l_icloglock);
2281ad3e3693SDave Chinner ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC);
2282b5203cd0SDave Chinner xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
2283d9f68777SDave Chinner error = xlog_state_release_iclog(log, iclog, ticket);
2284df732b29SChristoph Hellwig spin_unlock(&log->l_icloglock);
2285ad3e3693SDave Chinner if (error)
2286df732b29SChristoph Hellwig return error;
2287ad3e3693SDave Chinner
2288be8ddda5SDave Chinner error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
2289be8ddda5SDave Chinner log_offset);
2290ad3e3693SDave Chinner if (error)
2291ad3e3693SDave Chinner return error;
2292ad3e3693SDave Chinner *record_cnt = 0;
2293ad3e3693SDave Chinner *data_cnt = 0;
2294ad3e3693SDave Chinner *iclogp = iclog;
2295ad3e3693SDave Chinner return 0;
2296ad3e3693SDave Chinner }
2297ad3e3693SDave Chinner
2298ad3e3693SDave Chinner /*
2299ad3e3693SDave Chinner * Write log vectors into a single iclog which is smaller than the current chain
2300ad3e3693SDave Chinner * length. We write until we cannot fit a full record into the remaining space
2301ad3e3693SDave Chinner * and then stop. We return the log vector that is to be written that cannot
2302ad3e3693SDave Chinner * wholly fit in the iclog.
2303ad3e3693SDave Chinner */
2304ad3e3693SDave Chinner static int
xlog_write_partial(struct xfs_log_vec * lv,struct xlog_ticket * ticket,struct xlog_in_core ** iclogp,uint32_t * log_offset,uint32_t * len,uint32_t * record_cnt,uint32_t * data_cnt)2305ad3e3693SDave Chinner xlog_write_partial(
2306ad3e3693SDave Chinner struct xfs_log_vec *lv,
2307ad3e3693SDave Chinner struct xlog_ticket *ticket,
2308ad3e3693SDave Chinner struct xlog_in_core **iclogp,
2309ad3e3693SDave Chinner uint32_t *log_offset,
2310ad3e3693SDave Chinner uint32_t *len,
2311ad3e3693SDave Chinner uint32_t *record_cnt,
2312be8ddda5SDave Chinner uint32_t *data_cnt)
2313ad3e3693SDave Chinner {
2314ad3e3693SDave Chinner struct xlog_in_core *iclog = *iclogp;
2315ad3e3693SDave Chinner struct xlog_op_header *ophdr;
2316ad3e3693SDave Chinner int index = 0;
2317ad3e3693SDave Chinner uint32_t rlen;
2318ad3e3693SDave Chinner int error;
2319ad3e3693SDave Chinner
2320ad3e3693SDave Chinner /* walk the logvec, copying until we run out of space in the iclog */
2321ad3e3693SDave Chinner for (index = 0; index < lv->lv_niovecs; index++) {
2322ad3e3693SDave Chinner struct xfs_log_iovec *reg = &lv->lv_iovecp[index];
2323ad3e3693SDave Chinner uint32_t reg_offset = 0;
2324ad3e3693SDave Chinner
2325ad3e3693SDave Chinner /*
2326ad3e3693SDave Chinner * The first region of a continuation must have a non-zero
2327ad3e3693SDave Chinner * length otherwise log recovery will just skip over it and
2328ad3e3693SDave Chinner * start recovering from the next opheader it finds. Because we
2329ad3e3693SDave Chinner * mark the next opheader as a continuation, recovery will then
2330ad3e3693SDave Chinner * incorrectly add the continuation to the previous region and
2331ad3e3693SDave Chinner * that breaks stuff.
2332ad3e3693SDave Chinner *
2333ad3e3693SDave Chinner * Hence if there isn't space for region data after the
2334ad3e3693SDave Chinner * opheader, then we need to start afresh with a new iclog.
2335ad3e3693SDave Chinner */
2336ad3e3693SDave Chinner if (iclog->ic_size - *log_offset <=
2337ad3e3693SDave Chinner sizeof(struct xlog_op_header)) {
2338ad3e3693SDave Chinner error = xlog_write_get_more_iclog_space(ticket,
2339ad3e3693SDave Chinner &iclog, log_offset, *len, record_cnt,
2340be8ddda5SDave Chinner data_cnt);
2341ad3e3693SDave Chinner if (error)
2342ad3e3693SDave Chinner return error;
2343ad3e3693SDave Chinner }
2344ad3e3693SDave Chinner
2345ad3e3693SDave Chinner ophdr = reg->i_addr;
2346ad3e3693SDave Chinner rlen = min_t(uint32_t, reg->i_len, iclog->ic_size - *log_offset);
2347ad3e3693SDave Chinner
2348ad3e3693SDave Chinner ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2349ad3e3693SDave Chinner ophdr->oh_len = cpu_to_be32(rlen - sizeof(struct xlog_op_header));
2350ad3e3693SDave Chinner if (rlen != reg->i_len)
2351ad3e3693SDave Chinner ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
2352ad3e3693SDave Chinner
2353ad3e3693SDave Chinner xlog_write_iovec(iclog, log_offset, reg->i_addr,
2354ad3e3693SDave Chinner rlen, len, record_cnt, data_cnt);
2355ad3e3693SDave Chinner
2356ad3e3693SDave Chinner /* If we wrote the whole region, move to the next. */
2357ad3e3693SDave Chinner if (rlen == reg->i_len)
2358ad3e3693SDave Chinner continue;
2359ad3e3693SDave Chinner
2360ad3e3693SDave Chinner /*
2361ad3e3693SDave Chinner * We now have a partially written iovec, but it can span
2362ad3e3693SDave Chinner * multiple iclogs so we loop here. First we release the iclog
2363ad3e3693SDave Chinner * we currently have, then we get a new iclog and add a new
2364ad3e3693SDave Chinner * opheader. Then we continue copying from where we were until
2365ad3e3693SDave Chinner * we either complete the iovec or fill the iclog. If we
2366ad3e3693SDave Chinner * complete the iovec, then we increment the index and go right
2367ad3e3693SDave Chinner * back to the top of the outer loop. if we fill the iclog, we
2368ad3e3693SDave Chinner * run the inner loop again.
2369ad3e3693SDave Chinner *
2370ad3e3693SDave Chinner * This is complicated by the tail of a region using all the
2371ad3e3693SDave Chinner * space in an iclog and hence requiring us to release the iclog
2372ad3e3693SDave Chinner * and get a new one before returning to the outer loop. We must
2373ad3e3693SDave Chinner * always guarantee that we exit this inner loop with at least
2374ad3e3693SDave Chinner * space for log transaction opheaders left in the current
2375ad3e3693SDave Chinner * iclog, hence we cannot just terminate the loop at the end
2376ad3e3693SDave Chinner * of the of the continuation. So we loop while there is no
2377ad3e3693SDave Chinner * space left in the current iclog, and check for the end of the
2378ad3e3693SDave Chinner * continuation after getting a new iclog.
2379ad3e3693SDave Chinner */
2380ad3e3693SDave Chinner do {
2381ad3e3693SDave Chinner /*
2382ad3e3693SDave Chinner * Ensure we include the continuation opheader in the
2383ad3e3693SDave Chinner * space we need in the new iclog by adding that size
2384ad3e3693SDave Chinner * to the length we require. This continuation opheader
2385ad3e3693SDave Chinner * needs to be accounted to the ticket as the space it
2386ad3e3693SDave Chinner * consumes hasn't been accounted to the lv we are
2387ad3e3693SDave Chinner * writing.
2388ad3e3693SDave Chinner */
2389ad3e3693SDave Chinner error = xlog_write_get_more_iclog_space(ticket,
2390ad3e3693SDave Chinner &iclog, log_offset,
2391ad3e3693SDave Chinner *len + sizeof(struct xlog_op_header),
2392be8ddda5SDave Chinner record_cnt, data_cnt);
2393ad3e3693SDave Chinner if (error)
2394ad3e3693SDave Chinner return error;
2395ad3e3693SDave Chinner
2396ad3e3693SDave Chinner ophdr = iclog->ic_datap + *log_offset;
2397ad3e3693SDave Chinner ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2398ad3e3693SDave Chinner ophdr->oh_clientid = XFS_TRANSACTION;
2399ad3e3693SDave Chinner ophdr->oh_res2 = 0;
2400ad3e3693SDave Chinner ophdr->oh_flags = XLOG_WAS_CONT_TRANS;
2401ad3e3693SDave Chinner
2402ad3e3693SDave Chinner ticket->t_curr_res -= sizeof(struct xlog_op_header);
2403ad3e3693SDave Chinner *log_offset += sizeof(struct xlog_op_header);
2404ad3e3693SDave Chinner *data_cnt += sizeof(struct xlog_op_header);
2405ad3e3693SDave Chinner
2406ad3e3693SDave Chinner /*
2407ad3e3693SDave Chinner * If rlen fits in the iclog, then end the region
2408ad3e3693SDave Chinner * continuation. Otherwise we're going around again.
2409ad3e3693SDave Chinner */
2410ad3e3693SDave Chinner reg_offset += rlen;
2411ad3e3693SDave Chinner rlen = reg->i_len - reg_offset;
2412ad3e3693SDave Chinner if (rlen <= iclog->ic_size - *log_offset)
2413ad3e3693SDave Chinner ophdr->oh_flags |= XLOG_END_TRANS;
2414ad3e3693SDave Chinner else
2415ad3e3693SDave Chinner ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
2416ad3e3693SDave Chinner
2417ad3e3693SDave Chinner rlen = min_t(uint32_t, rlen, iclog->ic_size - *log_offset);
2418ad3e3693SDave Chinner ophdr->oh_len = cpu_to_be32(rlen);
2419ad3e3693SDave Chinner
2420ad3e3693SDave Chinner xlog_write_iovec(iclog, log_offset,
2421ad3e3693SDave Chinner reg->i_addr + reg_offset,
2422ad3e3693SDave Chinner rlen, len, record_cnt, data_cnt);
2423ad3e3693SDave Chinner
2424ad3e3693SDave Chinner } while (ophdr->oh_flags & XLOG_CONTINUE_TRANS);
2425ad3e3693SDave Chinner }
2426ad3e3693SDave Chinner
2427ad3e3693SDave Chinner /*
2428ad3e3693SDave Chinner * No more iovecs remain in this logvec so return the next log vec to
2429ad3e3693SDave Chinner * the caller so it can go back to fast path copying.
2430ad3e3693SDave Chinner */
2431ad3e3693SDave Chinner *iclogp = iclog;
2432ad3e3693SDave Chinner return 0;
2433b5203cd0SDave Chinner }
2434b5203cd0SDave Chinner
2435b5203cd0SDave Chinner /*
24361da177e4SLinus Torvalds * Write some region out to in-core log
24371da177e4SLinus Torvalds *
24381da177e4SLinus Torvalds * This will be called when writing externally provided regions or when
24391da177e4SLinus Torvalds * writing out a commit record for a given transaction.
24401da177e4SLinus Torvalds *
24411da177e4SLinus Torvalds * General algorithm:
24421da177e4SLinus Torvalds * 1. Find total length of this write. This may include adding to the
24431da177e4SLinus Torvalds * lengths passed in.
24441da177e4SLinus Torvalds * 2. Check whether we violate the tickets reservation.
24451da177e4SLinus Torvalds * 3. While writing to this iclog
24461da177e4SLinus Torvalds * A. Reserve as much space in this iclog as can get
24471da177e4SLinus Torvalds * B. If this is first write, save away start lsn
24481da177e4SLinus Torvalds * C. While writing this region:
24491da177e4SLinus Torvalds * 1. If first write of transaction, write start record
24501da177e4SLinus Torvalds * 2. Write log operation header (header per region)
24511da177e4SLinus Torvalds * 3. Find out if we can fit entire region into this iclog
24521da177e4SLinus Torvalds * 4. Potentially, verify destination memcpy ptr
24531da177e4SLinus Torvalds * 5. Memcpy (partial) region
24541da177e4SLinus Torvalds * 6. If partial copy, release iclog; otherwise, continue
24551da177e4SLinus Torvalds * copying more regions into current iclog
24561da177e4SLinus Torvalds * 4. Mark want sync bit (in simulation mode)
24571da177e4SLinus Torvalds * 5. Release iclog for potential flush to on-disk log.
24581da177e4SLinus Torvalds *
24591da177e4SLinus Torvalds * ERRORS:
24601da177e4SLinus Torvalds * 1. Panic if reservation is overrun. This should never happen since
24611da177e4SLinus Torvalds * reservation amounts are generated internal to the filesystem.
24621da177e4SLinus Torvalds * NOTES:
24631da177e4SLinus Torvalds * 1. Tickets are single threaded data structures.
24641da177e4SLinus Torvalds * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
24651da177e4SLinus Torvalds * syncing routine. When a single log_write region needs to span
24661da177e4SLinus Torvalds * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
24671da177e4SLinus Torvalds * on all log operation writes which don't contain the end of the
24681da177e4SLinus Torvalds * region. The XLOG_END_TRANS bit is used for the in-core log
24691da177e4SLinus Torvalds * operation which contains the end of the continued log_write region.
24701da177e4SLinus Torvalds * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
24711da177e4SLinus Torvalds * we don't really know exactly how much space will be used. As a result,
24721da177e4SLinus Torvalds * we don't update ic_offset until the end when we know exactly how many
24731da177e4SLinus Torvalds * bytes have been written out.
24741da177e4SLinus Torvalds */
247571e330b5SDave Chinner int
xlog_write(struct xlog * log,struct xfs_cil_ctx * ctx,struct list_head * lv_chain,struct xlog_ticket * ticket,uint32_t len)247635a8a72fSChristoph Hellwig xlog_write(
2477ad223e60SMark Tinguely struct xlog *log,
2478c45aba40SDave Chinner struct xfs_cil_ctx *ctx,
247916924853SDave Chinner struct list_head *lv_chain,
248035a8a72fSChristoph Hellwig struct xlog_ticket *ticket,
2481d80fc291SDave Chinner uint32_t len)
2482d80fc291SDave Chinner
24831da177e4SLinus Torvalds {
248499428ad0SChristoph Hellwig struct xlog_in_core *iclog = NULL;
248516924853SDave Chinner struct xfs_log_vec *lv;
2486db357078SDave Chinner uint32_t record_cnt = 0;
2487db357078SDave Chinner uint32_t data_cnt = 0;
2488df732b29SChristoph Hellwig int error = 0;
2489ad3e3693SDave Chinner int log_offset;
24901da177e4SLinus Torvalds
24917d2d5653SBrian Foster if (ticket->t_curr_res < 0) {
24927d2d5653SBrian Foster xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
24937d2d5653SBrian Foster "ctx ticket reservation ran out. Need to up reservation");
249471e330b5SDave Chinner xlog_print_tic_res(log->l_mp, ticket);
2495b5f17becSDave Chinner xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
24967d2d5653SBrian Foster }
249771e330b5SDave Chinner
249899428ad0SChristoph Hellwig error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
2499be8ddda5SDave Chinner &log_offset);
250099428ad0SChristoph Hellwig if (error)
2501014c2544SJesper Juhl return error;
25021da177e4SLinus Torvalds
25031da177e4SLinus Torvalds ASSERT(log_offset <= iclog->ic_size - 1);
25041da177e4SLinus Torvalds
2505c45aba40SDave Chinner /*
2506c45aba40SDave Chinner * If we have a context pointer, pass it the first iclog we are
2507c45aba40SDave Chinner * writing to so it can record state needed for iclog write
2508c45aba40SDave Chinner * ordering.
2509c45aba40SDave Chinner */
2510ad3e3693SDave Chinner if (ctx)
2511c45aba40SDave Chinner xlog_cil_set_ctx_write_state(ctx, iclog);
25121da177e4SLinus Torvalds
251316924853SDave Chinner list_for_each_entry(lv, lv_chain, lv_list) {
251499428ad0SChristoph Hellwig /*
2515ad3e3693SDave Chinner * If the entire log vec does not fit in the iclog, punt it to
2516ad3e3693SDave Chinner * the partial copy loop which can handle this case.
25171da177e4SLinus Torvalds */
2518ad3e3693SDave Chinner if (lv->lv_niovecs &&
2519ad3e3693SDave Chinner lv->lv_bytes > iclog->ic_size - log_offset) {
2520ad3e3693SDave Chinner error = xlog_write_partial(lv, ticket, &iclog,
2521ad3e3693SDave Chinner &log_offset, &len, &record_cnt,
2522be8ddda5SDave Chinner &data_cnt);
2523ad3e3693SDave Chinner if (error) {
25247ec94921SDave Chinner /*
2525ad3e3693SDave Chinner * We have no iclog to release, so just return
2526ad3e3693SDave Chinner * the error immediately.
25277ec94921SDave Chinner */
2528014c2544SJesper Juhl return error;
2529ad3e3693SDave Chinner }
2530ad3e3693SDave Chinner } else {
2531db357078SDave Chinner xlog_write_full(lv, ticket, iclog, &log_offset,
2532db357078SDave Chinner &len, &record_cnt, &data_cnt);
2533ad3e3693SDave Chinner }
253455b66332SDave Chinner }
25351da177e4SLinus Torvalds ASSERT(len == 0);
25361da177e4SLinus Torvalds
2537ad3e3693SDave Chinner /*
2538ad3e3693SDave Chinner * We've already been guaranteed that the last writes will fit inside
2539ad3e3693SDave Chinner * the current iclog, and hence it will already have the space used by
2540ad3e3693SDave Chinner * those writes accounted to it. Hence we do not need to update the
2541ad3e3693SDave Chinner * iclog with the number of bytes written here.
2542ad3e3693SDave Chinner */
2543390aab0aSChristoph Hellwig spin_lock(&log->l_icloglock);
2544ad3e3693SDave Chinner xlog_state_finish_copy(log, iclog, record_cnt, 0);
2545d9f68777SDave Chinner error = xlog_state_release_iclog(log, iclog, ticket);
2546df732b29SChristoph Hellwig spin_unlock(&log->l_icloglock);
2547df732b29SChristoph Hellwig
2548df732b29SChristoph Hellwig return error;
25491da177e4SLinus Torvalds }
25501da177e4SLinus Torvalds
2551c814b4f2SChristoph Hellwig static void
xlog_state_activate_iclog(struct xlog_in_core * iclog,int * iclogs_changed)2552c814b4f2SChristoph Hellwig xlog_state_activate_iclog(
2553c814b4f2SChristoph Hellwig struct xlog_in_core *iclog,
2554c814b4f2SChristoph Hellwig int *iclogs_changed)
2555c814b4f2SChristoph Hellwig {
2556c814b4f2SChristoph Hellwig ASSERT(list_empty_careful(&iclog->ic_callbacks));
2557956f6daaSDave Chinner trace_xlog_iclog_activate(iclog, _RET_IP_);
2558c814b4f2SChristoph Hellwig
25590383f543SDave Chinner /*
2560c814b4f2SChristoph Hellwig * If the number of ops in this iclog indicate it just contains the
2561c814b4f2SChristoph Hellwig * dummy transaction, we can change state into IDLE (the second time
2562c814b4f2SChristoph Hellwig * around). Otherwise we should change the state into NEED a dummy.
2563c814b4f2SChristoph Hellwig * We don't need to cover the dummy.
25641da177e4SLinus Torvalds */
2565c814b4f2SChristoph Hellwig if (*iclogs_changed == 0 &&
2566c814b4f2SChristoph Hellwig iclog->ic_header.h_num_logops == cpu_to_be32(XLOG_COVER_OPS)) {
2567c814b4f2SChristoph Hellwig *iclogs_changed = 1;
2568c814b4f2SChristoph Hellwig } else {
2569c814b4f2SChristoph Hellwig /*
2570c814b4f2SChristoph Hellwig * We have two dirty iclogs so start over. This could also be
2571c814b4f2SChristoph Hellwig * num of ops indicating this is not the dummy going out.
2572c814b4f2SChristoph Hellwig */
2573c814b4f2SChristoph Hellwig *iclogs_changed = 2;
2574c814b4f2SChristoph Hellwig }
2575c814b4f2SChristoph Hellwig
2576c814b4f2SChristoph Hellwig iclog->ic_state = XLOG_STATE_ACTIVE;
2577c814b4f2SChristoph Hellwig iclog->ic_offset = 0;
2578c814b4f2SChristoph Hellwig iclog->ic_header.h_num_logops = 0;
2579c814b4f2SChristoph Hellwig memset(iclog->ic_header.h_cycle_data, 0,
2580c814b4f2SChristoph Hellwig sizeof(iclog->ic_header.h_cycle_data));
2581c814b4f2SChristoph Hellwig iclog->ic_header.h_lsn = 0;
25829d110014SDave Chinner iclog->ic_header.h_tail_lsn = 0;
2583c814b4f2SChristoph Hellwig }
2584c814b4f2SChristoph Hellwig
2585c814b4f2SChristoph Hellwig /*
2586c814b4f2SChristoph Hellwig * Loop through all iclogs and mark all iclogs currently marked DIRTY as
2587c814b4f2SChristoph Hellwig * ACTIVE after iclog I/O has completed.
2588c814b4f2SChristoph Hellwig */
2589c814b4f2SChristoph Hellwig static void
xlog_state_activate_iclogs(struct xlog * log,int * iclogs_changed)2590c814b4f2SChristoph Hellwig xlog_state_activate_iclogs(
2591c814b4f2SChristoph Hellwig struct xlog *log,
2592c814b4f2SChristoph Hellwig int *iclogs_changed)
2593c814b4f2SChristoph Hellwig {
2594c814b4f2SChristoph Hellwig struct xlog_in_core *iclog = log->l_iclog;
2595c814b4f2SChristoph Hellwig
2596c814b4f2SChristoph Hellwig do {
2597c814b4f2SChristoph Hellwig if (iclog->ic_state == XLOG_STATE_DIRTY)
2598c814b4f2SChristoph Hellwig xlog_state_activate_iclog(iclog, iclogs_changed);
2599c814b4f2SChristoph Hellwig /*
2600c814b4f2SChristoph Hellwig * The ordering of marking iclogs ACTIVE must be maintained, so
2601c814b4f2SChristoph Hellwig * an iclog doesn't become ACTIVE beyond one that is SYNCING.
2602c814b4f2SChristoph Hellwig */
2603c814b4f2SChristoph Hellwig else if (iclog->ic_state != XLOG_STATE_ACTIVE)
2604c814b4f2SChristoph Hellwig break;
2605c814b4f2SChristoph Hellwig } while ((iclog = iclog->ic_next) != log->l_iclog);
2606c814b4f2SChristoph Hellwig }
2607c814b4f2SChristoph Hellwig
2608c814b4f2SChristoph Hellwig static int
xlog_covered_state(int prev_state,int iclogs_changed)2609c814b4f2SChristoph Hellwig xlog_covered_state(
2610c814b4f2SChristoph Hellwig int prev_state,
2611c814b4f2SChristoph Hellwig int iclogs_changed)
2612c814b4f2SChristoph Hellwig {
2613c814b4f2SChristoph Hellwig /*
2614b0eb9e11SBrian Foster * We go to NEED for any non-covering writes. We go to NEED2 if we just
2615b0eb9e11SBrian Foster * wrote the first covering record (DONE). We go to IDLE if we just
2616b0eb9e11SBrian Foster * wrote the second covering record (DONE2) and remain in IDLE until a
2617b0eb9e11SBrian Foster * non-covering write occurs.
2618c814b4f2SChristoph Hellwig */
2619c814b4f2SChristoph Hellwig switch (prev_state) {
2620c814b4f2SChristoph Hellwig case XLOG_STATE_COVER_IDLE:
2621b0eb9e11SBrian Foster if (iclogs_changed == 1)
2622b0eb9e11SBrian Foster return XLOG_STATE_COVER_IDLE;
262353004ee7SGustavo A. R. Silva fallthrough;
2624c814b4f2SChristoph Hellwig case XLOG_STATE_COVER_NEED:
2625c814b4f2SChristoph Hellwig case XLOG_STATE_COVER_NEED2:
2626c814b4f2SChristoph Hellwig break;
2627c814b4f2SChristoph Hellwig case XLOG_STATE_COVER_DONE:
2628c814b4f2SChristoph Hellwig if (iclogs_changed == 1)
2629c814b4f2SChristoph Hellwig return XLOG_STATE_COVER_NEED2;
2630c814b4f2SChristoph Hellwig break;
2631c814b4f2SChristoph Hellwig case XLOG_STATE_COVER_DONE2:
2632c814b4f2SChristoph Hellwig if (iclogs_changed == 1)
2633c814b4f2SChristoph Hellwig return XLOG_STATE_COVER_IDLE;
2634c814b4f2SChristoph Hellwig break;
2635c814b4f2SChristoph Hellwig default:
2636c814b4f2SChristoph Hellwig ASSERT(0);
2637c814b4f2SChristoph Hellwig }
2638c814b4f2SChristoph Hellwig
2639c814b4f2SChristoph Hellwig return XLOG_STATE_COVER_NEED;
2640c814b4f2SChristoph Hellwig }
2641c814b4f2SChristoph Hellwig
2642ba0f32d4SChristoph Hellwig STATIC void
xlog_state_clean_iclog(struct xlog * log,struct xlog_in_core * dirty_iclog)26430383f543SDave Chinner xlog_state_clean_iclog(
26440383f543SDave Chinner struct xlog *log,
26450383f543SDave Chinner struct xlog_in_core *dirty_iclog)
26461da177e4SLinus Torvalds {
2647c814b4f2SChristoph Hellwig int iclogs_changed = 0;
26481da177e4SLinus Torvalds
2649956f6daaSDave Chinner trace_xlog_iclog_clean(dirty_iclog, _RET_IP_);
2650956f6daaSDave Chinner
26510383f543SDave Chinner dirty_iclog->ic_state = XLOG_STATE_DIRTY;
26520383f543SDave Chinner
2653c814b4f2SChristoph Hellwig xlog_state_activate_iclogs(log, &iclogs_changed);
26540383f543SDave Chinner wake_up_all(&dirty_iclog->ic_force_wait);
26550383f543SDave Chinner
2656c814b4f2SChristoph Hellwig if (iclogs_changed) {
2657c814b4f2SChristoph Hellwig log->l_covered_state = xlog_covered_state(log->l_covered_state,
2658c814b4f2SChristoph Hellwig iclogs_changed);
26591da177e4SLinus Torvalds }
26600383f543SDave Chinner }
26611da177e4SLinus Torvalds
26621da177e4SLinus Torvalds STATIC xfs_lsn_t
xlog_get_lowest_lsn(struct xlog * log)26631da177e4SLinus Torvalds xlog_get_lowest_lsn(
26649a8d2fdbSMark Tinguely struct xlog *log)
26651da177e4SLinus Torvalds {
26669bff3132SChristoph Hellwig struct xlog_in_core *iclog = log->l_iclog;
26679bff3132SChristoph Hellwig xfs_lsn_t lowest_lsn = 0, lsn;
26681da177e4SLinus Torvalds
26691da177e4SLinus Torvalds do {
26701858bb0bSChristoph Hellwig if (iclog->ic_state == XLOG_STATE_ACTIVE ||
26711858bb0bSChristoph Hellwig iclog->ic_state == XLOG_STATE_DIRTY)
26729bff3132SChristoph Hellwig continue;
26739bff3132SChristoph Hellwig
26749bff3132SChristoph Hellwig lsn = be64_to_cpu(iclog->ic_header.h_lsn);
26759bff3132SChristoph Hellwig if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0)
26761da177e4SLinus Torvalds lowest_lsn = lsn;
26779bff3132SChristoph Hellwig } while ((iclog = iclog->ic_next) != log->l_iclog);
26789bff3132SChristoph Hellwig
2679014c2544SJesper Juhl return lowest_lsn;
26801da177e4SLinus Torvalds }
26811da177e4SLinus Torvalds
26826546818cSDave Chinner /*
268314e15f1bSDave Chinner * Completion of a iclog IO does not imply that a transaction has completed, as
268414e15f1bSDave Chinner * transactions can be large enough to span many iclogs. We cannot change the
268514e15f1bSDave Chinner * tail of the log half way through a transaction as this may be the only
268614e15f1bSDave Chinner * transaction in the log and moving the tail to point to the middle of it
268714e15f1bSDave Chinner * will prevent recovery from finding the start of the transaction. Hence we
268814e15f1bSDave Chinner * should only update the last_sync_lsn if this iclog contains transaction
268914e15f1bSDave Chinner * completion callbacks on it.
269014e15f1bSDave Chinner *
269114e15f1bSDave Chinner * We have to do this before we drop the icloglock to ensure we are the only one
269214e15f1bSDave Chinner * that can update it.
269314e15f1bSDave Chinner *
269414e15f1bSDave Chinner * If we are moving the last_sync_lsn forwards, we also need to ensure we kick
269514e15f1bSDave Chinner * the reservation grant head pushing. This is due to the fact that the push
269614e15f1bSDave Chinner * target is bound by the current last_sync_lsn value. Hence if we have a large
269714e15f1bSDave Chinner * amount of log space bound up in this committing transaction then the
269814e15f1bSDave Chinner * last_sync_lsn value may be the limiting factor preventing tail pushing from
269914e15f1bSDave Chinner * freeing space in the log. Hence once we've updated the last_sync_lsn we
270014e15f1bSDave Chinner * should push the AIL to ensure the push target (and hence the grant head) is
270114e15f1bSDave Chinner * no longer bound by the old log head location and can move forwards and make
270214e15f1bSDave Chinner * progress again.
270314e15f1bSDave Chinner */
270414e15f1bSDave Chinner static void
xlog_state_set_callback(struct xlog * log,struct xlog_in_core * iclog,xfs_lsn_t header_lsn)270514e15f1bSDave Chinner xlog_state_set_callback(
270614e15f1bSDave Chinner struct xlog *log,
270714e15f1bSDave Chinner struct xlog_in_core *iclog,
270814e15f1bSDave Chinner xfs_lsn_t header_lsn)
270914e15f1bSDave Chinner {
2710956f6daaSDave Chinner trace_xlog_iclog_callback(iclog, _RET_IP_);
271114e15f1bSDave Chinner iclog->ic_state = XLOG_STATE_CALLBACK;
271214e15f1bSDave Chinner
271314e15f1bSDave Chinner ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
271414e15f1bSDave Chinner header_lsn) <= 0);
271514e15f1bSDave Chinner
271614e15f1bSDave Chinner if (list_empty_careful(&iclog->ic_callbacks))
271714e15f1bSDave Chinner return;
271814e15f1bSDave Chinner
271914e15f1bSDave Chinner atomic64_set(&log->l_last_sync_lsn, header_lsn);
272014e15f1bSDave Chinner xlog_grant_push_ail(log, 0);
272114e15f1bSDave Chinner }
272214e15f1bSDave Chinner
272314e15f1bSDave Chinner /*
27245e96fa8dSDave Chinner * Return true if we need to stop processing, false to continue to the next
27255e96fa8dSDave Chinner * iclog. The caller will need to run callbacks if the iclog is returned in the
27265e96fa8dSDave Chinner * XLOG_STATE_CALLBACK state.
27275e96fa8dSDave Chinner */
27285e96fa8dSDave Chinner static bool
xlog_state_iodone_process_iclog(struct xlog * log,struct xlog_in_core * iclog)27295e96fa8dSDave Chinner xlog_state_iodone_process_iclog(
27305e96fa8dSDave Chinner struct xlog *log,
27315112e206SDave Chinner struct xlog_in_core *iclog)
27325e96fa8dSDave Chinner {
27335e96fa8dSDave Chinner xfs_lsn_t lowest_lsn;
273414e15f1bSDave Chinner xfs_lsn_t header_lsn;
27355e96fa8dSDave Chinner
27361858bb0bSChristoph Hellwig switch (iclog->ic_state) {
27371858bb0bSChristoph Hellwig case XLOG_STATE_ACTIVE:
27381858bb0bSChristoph Hellwig case XLOG_STATE_DIRTY:
27395e96fa8dSDave Chinner /*
27401858bb0bSChristoph Hellwig * Skip all iclogs in the ACTIVE & DIRTY states:
27415e96fa8dSDave Chinner */
27421858bb0bSChristoph Hellwig return false;
27431858bb0bSChristoph Hellwig case XLOG_STATE_DONE_SYNC:
27445e96fa8dSDave Chinner /*
27454b29ab04SChristoph Hellwig * Now that we have an iclog that is in the DONE_SYNC state, do
27464b29ab04SChristoph Hellwig * one more check here to see if we have chased our tail around.
27474b29ab04SChristoph Hellwig * If this is not the lowest lsn iclog, then we will leave it
27484b29ab04SChristoph Hellwig * for another completion to process.
27495e96fa8dSDave Chinner */
275014e15f1bSDave Chinner header_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
27515e96fa8dSDave Chinner lowest_lsn = xlog_get_lowest_lsn(log);
275214e15f1bSDave Chinner if (lowest_lsn && XFS_LSN_CMP(lowest_lsn, header_lsn) < 0)
275314e15f1bSDave Chinner return false;
275414e15f1bSDave Chinner xlog_state_set_callback(log, iclog, header_lsn);
27555e96fa8dSDave Chinner return false;
27561858bb0bSChristoph Hellwig default:
27571858bb0bSChristoph Hellwig /*
27581858bb0bSChristoph Hellwig * Can only perform callbacks in order. Since this iclog is not
27594b29ab04SChristoph Hellwig * in the DONE_SYNC state, we skip the rest and just try to
27604b29ab04SChristoph Hellwig * clean up.
27611858bb0bSChristoph Hellwig */
27621858bb0bSChristoph Hellwig return true;
27631858bb0bSChristoph Hellwig }
27645e96fa8dSDave Chinner }
27655e96fa8dSDave Chinner
27661da177e4SLinus Torvalds /*
27678bb92005SDave Chinner * Loop over all the iclogs, running attached callbacks on them. Return true if
2768aad7272aSDave Chinner * we ran any callbacks, indicating that we dropped the icloglock. We don't need
2769aad7272aSDave Chinner * to handle transient shutdown state here at all because
2770aad7272aSDave Chinner * xlog_state_shutdown_callbacks() will be run to do the necessary shutdown
2771aad7272aSDave Chinner * cleanup of the callbacks.
27721da177e4SLinus Torvalds */
27738bb92005SDave Chinner static bool
xlog_state_do_iclog_callbacks(struct xlog * log)27748bb92005SDave Chinner xlog_state_do_iclog_callbacks(
27758bb92005SDave Chinner struct xlog *log)
27768bb92005SDave Chinner __releases(&log->l_icloglock)
27778bb92005SDave Chinner __acquires(&log->l_icloglock)
27788bb92005SDave Chinner {
27798bb92005SDave Chinner struct xlog_in_core *first_iclog = log->l_iclog;
27808bb92005SDave Chinner struct xlog_in_core *iclog = first_iclog;
27818bb92005SDave Chinner bool ran_callback = false;
27821da177e4SLinus Torvalds
27831da177e4SLinus Torvalds do {
2784a1bb8505SDave Chinner LIST_HEAD(cb_list);
2785a1bb8505SDave Chinner
27865112e206SDave Chinner if (xlog_state_iodone_process_iclog(log, iclog))
27875e96fa8dSDave Chinner break;
27885112e206SDave Chinner if (iclog->ic_state != XLOG_STATE_CALLBACK) {
27891da177e4SLinus Torvalds iclog = iclog->ic_next;
27901da177e4SLinus Torvalds continue;
27911da177e4SLinus Torvalds }
2792a1bb8505SDave Chinner list_splice_init(&iclog->ic_callbacks, &cb_list);
27936be00102SDave Chinner spin_unlock(&log->l_icloglock);
27941da177e4SLinus Torvalds
2795a1bb8505SDave Chinner trace_xlog_iclog_callbacks_start(iclog, _RET_IP_);
2796a1bb8505SDave Chinner xlog_cil_process_committed(&cb_list);
2797a1bb8505SDave Chinner trace_xlog_iclog_callbacks_done(iclog, _RET_IP_);
27988bb92005SDave Chinner ran_callback = true;
27996be00102SDave Chinner
28006be00102SDave Chinner spin_lock(&log->l_icloglock);
28010383f543SDave Chinner xlog_state_clean_iclog(log, iclog);
28021da177e4SLinus Torvalds iclog = iclog->ic_next;
28035112e206SDave Chinner } while (iclog != first_iclog);
2804a3c6685eSNathan Scott
28058bb92005SDave Chinner return ran_callback;
28068bb92005SDave Chinner }
28078bb92005SDave Chinner
28088bb92005SDave Chinner
28098bb92005SDave Chinner /*
28108bb92005SDave Chinner * Loop running iclog completion callbacks until there are no more iclogs in a
28118bb92005SDave Chinner * state that can run callbacks.
28128bb92005SDave Chinner */
28138bb92005SDave Chinner STATIC void
xlog_state_do_callback(struct xlog * log)28148bb92005SDave Chinner xlog_state_do_callback(
28158bb92005SDave Chinner struct xlog *log)
28168bb92005SDave Chinner {
28178bb92005SDave Chinner int flushcnt = 0;
28188bb92005SDave Chinner int repeats = 0;
28198bb92005SDave Chinner
28208bb92005SDave Chinner spin_lock(&log->l_icloglock);
28218bb92005SDave Chinner while (xlog_state_do_iclog_callbacks(log)) {
28228bb92005SDave Chinner if (xlog_is_shutdown(log))
28238bb92005SDave Chinner break;
28248bb92005SDave Chinner
28255112e206SDave Chinner if (++repeats > 5000) {
2826a3c6685eSNathan Scott flushcnt += repeats;
2827a3c6685eSNathan Scott repeats = 0;
2828a0fa2b67SDave Chinner xfs_warn(log->l_mp,
2829a3c6685eSNathan Scott "%s: possible infinite loop (%d iterations)",
283034a622b2SHarvey Harrison __func__, flushcnt);
28311da177e4SLinus Torvalds }
28328bb92005SDave Chinner }
28331da177e4SLinus Torvalds
2834aad7272aSDave Chinner if (log->l_iclog->ic_state == XLOG_STATE_ACTIVE)
2835eb40a875SDave Chinner wake_up_all(&log->l_flush_wait);
2836cdea5459SRik van Riel
2837cdea5459SRik van Riel spin_unlock(&log->l_icloglock);
2838d748c623SMatthew Wilcox }
28391da177e4SLinus Torvalds
28401da177e4SLinus Torvalds
28411da177e4SLinus Torvalds /*
28421da177e4SLinus Torvalds * Finish transitioning this iclog to the dirty state.
28431da177e4SLinus Torvalds *
28441da177e4SLinus Torvalds * Callbacks could take time, so they are done outside the scope of the
284512017fafSDavid Chinner * global state machine log lock.
28461da177e4SLinus Torvalds */
2847a8272ce0SDavid Chinner STATIC void
xlog_state_done_syncing(struct xlog_in_core * iclog)28481da177e4SLinus Torvalds xlog_state_done_syncing(
284912e6a0f4SChristoph Hellwig struct xlog_in_core *iclog)
28501da177e4SLinus Torvalds {
28519a8d2fdbSMark Tinguely struct xlog *log = iclog->ic_log;
28521da177e4SLinus Torvalds
2853b22cd72cSEric Sandeen spin_lock(&log->l_icloglock);
2854155cc6b7SDavid Chinner ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2855956f6daaSDave Chinner trace_xlog_iclog_sync_done(iclog, _RET_IP_);
28561da177e4SLinus Torvalds
28571da177e4SLinus Torvalds /*
28581da177e4SLinus Torvalds * If we got an error, either on the first buffer, or in the case of
285912e6a0f4SChristoph Hellwig * split log writes, on the second, we shut down the file system and
286012e6a0f4SChristoph Hellwig * no iclogs should ever be attempted to be written to disk again.
28611da177e4SLinus Torvalds */
28622039a272SDave Chinner if (!xlog_is_shutdown(log)) {
286312e6a0f4SChristoph Hellwig ASSERT(iclog->ic_state == XLOG_STATE_SYNCING);
28641da177e4SLinus Torvalds iclog->ic_state = XLOG_STATE_DONE_SYNC;
286512e6a0f4SChristoph Hellwig }
28661da177e4SLinus Torvalds
28671da177e4SLinus Torvalds /*
28681da177e4SLinus Torvalds * Someone could be sleeping prior to writing out the next
28691da177e4SLinus Torvalds * iclog buffer, we wake them all, one will get to do the
28701da177e4SLinus Torvalds * I/O, the others get to wait for the result.
28711da177e4SLinus Torvalds */
2872eb40a875SDave Chinner wake_up_all(&iclog->ic_write_wait);
2873b22cd72cSEric Sandeen spin_unlock(&log->l_icloglock);
2874b843299bSDave Chinner xlog_state_do_callback(log);
287512e6a0f4SChristoph Hellwig }
28761da177e4SLinus Torvalds
28771da177e4SLinus Torvalds /*
28781da177e4SLinus Torvalds * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
287912017fafSDavid Chinner * sleep. We wait on the flush queue on the head iclog as that should be
288012017fafSDavid Chinner * the first iclog to complete flushing. Hence if all iclogs are syncing,
288112017fafSDavid Chinner * we will wait here and all new writes will sleep until a sync completes.
28821da177e4SLinus Torvalds *
28831da177e4SLinus Torvalds * The in-core logs are used in a circular fashion. They are not used
28841da177e4SLinus Torvalds * out-of-order even when an iclog past the head is free.
28851da177e4SLinus Torvalds *
28861da177e4SLinus Torvalds * return:
28871da177e4SLinus Torvalds * * log_offset where xlog_write() can start writing into the in-core
28881da177e4SLinus Torvalds * log's data space.
28891da177e4SLinus Torvalds * * in-core log pointer to which xlog_write() should write.
28901da177e4SLinus Torvalds * * boolean indicating this is a continued write to an in-core log.
28911da177e4SLinus Torvalds * If this is the last write, then the in-core log's offset field
28921da177e4SLinus Torvalds * needs to be incremented, depending on the amount of data which
28931da177e4SLinus Torvalds * is copied.
28941da177e4SLinus Torvalds */
2895a8272ce0SDavid Chinner STATIC int
xlog_state_get_iclog_space(struct xlog * log,int len,struct xlog_in_core ** iclogp,struct xlog_ticket * ticket,int * logoffsetp)28969a8d2fdbSMark Tinguely xlog_state_get_iclog_space(
28979a8d2fdbSMark Tinguely struct xlog *log,
28981da177e4SLinus Torvalds int len,
28999a8d2fdbSMark Tinguely struct xlog_in_core **iclogp,
29009a8d2fdbSMark Tinguely struct xlog_ticket *ticket,
29011da177e4SLinus Torvalds int *logoffsetp)
29021da177e4SLinus Torvalds {
29031da177e4SLinus Torvalds int log_offset;
29041da177e4SLinus Torvalds xlog_rec_header_t *head;
29051da177e4SLinus Torvalds xlog_in_core_t *iclog;
29061da177e4SLinus Torvalds
29071da177e4SLinus Torvalds restart:
2908b22cd72cSEric Sandeen spin_lock(&log->l_icloglock);
29092039a272SDave Chinner if (xlog_is_shutdown(log)) {
2910b22cd72cSEric Sandeen spin_unlock(&log->l_icloglock);
29112451337dSDave Chinner return -EIO;
29121da177e4SLinus Torvalds }
29131da177e4SLinus Torvalds
29141da177e4SLinus Torvalds iclog = log->l_iclog;
2915d748c623SMatthew Wilcox if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2916ff6d6af2SBill O'Donnell XFS_STATS_INC(log->l_mp, xs_log_noiclogs);
2917d748c623SMatthew Wilcox
2918d748c623SMatthew Wilcox /* Wait for log writes to have flushed */
2919eb40a875SDave Chinner xlog_wait(&log->l_flush_wait, &log->l_icloglock);
29201da177e4SLinus Torvalds goto restart;
29211da177e4SLinus Torvalds }
2922d748c623SMatthew Wilcox
29231da177e4SLinus Torvalds head = &iclog->ic_header;
29241da177e4SLinus Torvalds
2925155cc6b7SDavid Chinner atomic_inc(&iclog->ic_refcnt); /* prevents sync */
29261da177e4SLinus Torvalds log_offset = iclog->ic_offset;
29271da177e4SLinus Torvalds
2928956f6daaSDave Chinner trace_xlog_iclog_get_space(iclog, _RET_IP_);
2929956f6daaSDave Chinner
29301da177e4SLinus Torvalds /* On the 1st write to an iclog, figure out lsn. This works
29311da177e4SLinus Torvalds * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
29321da177e4SLinus Torvalds * committing to. If the offset is set, that's how many blocks
29331da177e4SLinus Torvalds * must be written.
29341da177e4SLinus Torvalds */
29351da177e4SLinus Torvalds if (log_offset == 0) {
29361da177e4SLinus Torvalds ticket->t_curr_res -= log->l_iclog_hsize;
2937b53e675dSChristoph Hellwig head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2938b53e675dSChristoph Hellwig head->h_lsn = cpu_to_be64(
293903bea6feSChristoph Hellwig xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
29401da177e4SLinus Torvalds ASSERT(log->l_curr_block >= 0);
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds
29431da177e4SLinus Torvalds /* If there is enough room to write everything, then do it. Otherwise,
29441da177e4SLinus Torvalds * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
29451da177e4SLinus Torvalds * bit is on, so this will get flushed out. Don't update ic_offset
29461da177e4SLinus Torvalds * until you know exactly how many bytes get copied. Therefore, wait
29471da177e4SLinus Torvalds * until later to update ic_offset.
29481da177e4SLinus Torvalds *
29491da177e4SLinus Torvalds * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
29501da177e4SLinus Torvalds * can fit into remaining data section.
29511da177e4SLinus Torvalds */
29521da177e4SLinus Torvalds if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2953df732b29SChristoph Hellwig int error = 0;
2954df732b29SChristoph Hellwig
29551da177e4SLinus Torvalds xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
29561da177e4SLinus Torvalds
295749641f1aSDave Chinner /*
2958df732b29SChristoph Hellwig * If we are the only one writing to this iclog, sync it to
2959df732b29SChristoph Hellwig * disk. We need to do an atomic compare and decrement here to
2960df732b29SChristoph Hellwig * avoid racing with concurrent atomic_dec_and_lock() calls in
296149641f1aSDave Chinner * xlog_state_release_iclog() when there is more than one
296249641f1aSDave Chinner * reference to the iclog.
296349641f1aSDave Chinner */
2964df732b29SChristoph Hellwig if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1))
2965d9f68777SDave Chinner error = xlog_state_release_iclog(log, iclog, ticket);
2966df732b29SChristoph Hellwig spin_unlock(&log->l_icloglock);
296749641f1aSDave Chinner if (error)
2968014c2544SJesper Juhl return error;
29691da177e4SLinus Torvalds goto restart;
29701da177e4SLinus Torvalds }
29711da177e4SLinus Torvalds
29721da177e4SLinus Torvalds /* Do we have enough room to write the full amount in the remainder
29731da177e4SLinus Torvalds * of this iclog? Or must we continue a write on the next iclog and
29741da177e4SLinus Torvalds * mark this iclog as completely taken? In the case where we switch
29751da177e4SLinus Torvalds * iclogs (to mark it taken), this particular iclog will release/sync
29761da177e4SLinus Torvalds * to disk in xlog_write().
29771da177e4SLinus Torvalds */
2978be8ddda5SDave Chinner if (len <= iclog->ic_size - iclog->ic_offset)
29791da177e4SLinus Torvalds iclog->ic_offset += len;
2980be8ddda5SDave Chinner else
29811da177e4SLinus Torvalds xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
29821da177e4SLinus Torvalds *iclogp = iclog;
29831da177e4SLinus Torvalds
29841da177e4SLinus Torvalds ASSERT(iclog->ic_offset <= iclog->ic_size);
2985b22cd72cSEric Sandeen spin_unlock(&log->l_icloglock);
29861da177e4SLinus Torvalds
29871da177e4SLinus Torvalds *logoffsetp = log_offset;
29881da177e4SLinus Torvalds return 0;
2989b843299bSDave Chinner }
29901da177e4SLinus Torvalds
29918b41e3f9SChristoph Hellwig /*
2992b843299bSDave Chinner * The first cnt-1 times a ticket goes through here we don't need to move the
2993b843299bSDave Chinner * grant write head because the permanent reservation has reserved cnt times the
2994b843299bSDave Chinner * unit amount. Release part of current permanent unit reservation and reset
2995b843299bSDave Chinner * current reservation to be one units worth. Also move grant reservation head
2996b843299bSDave Chinner * forward.
29971da177e4SLinus Torvalds */
29988b41e3f9SChristoph Hellwig void
xfs_log_ticket_regrant(struct xlog * log,struct xlog_ticket * ticket)29998b41e3f9SChristoph Hellwig xfs_log_ticket_regrant(
30009a8d2fdbSMark Tinguely struct xlog *log,
30019a8d2fdbSMark Tinguely struct xlog_ticket *ticket)
30021da177e4SLinus Torvalds {
30038b41e3f9SChristoph Hellwig trace_xfs_log_ticket_regrant(log, ticket);
30040b1b213fSChristoph Hellwig
30051da177e4SLinus Torvalds if (ticket->t_cnt > 0)
30061da177e4SLinus Torvalds ticket->t_cnt--;
30071da177e4SLinus Torvalds
300828496968SChristoph Hellwig xlog_grant_sub_space(log, &log->l_reserve_head.grant,
3009663e496aSDave Chinner ticket->t_curr_res);
301028496968SChristoph Hellwig xlog_grant_sub_space(log, &log->l_write_head.grant,
3011663e496aSDave Chinner ticket->t_curr_res);
30121da177e4SLinus Torvalds ticket->t_curr_res = ticket->t_unit_res;
30130b1b213fSChristoph Hellwig
30148b41e3f9SChristoph Hellwig trace_xfs_log_ticket_regrant_sub(log, ticket);
30150b1b213fSChristoph Hellwig
30161da177e4SLinus Torvalds /* just return if we still have some of the pre-reserved space */
30178b41e3f9SChristoph Hellwig if (!ticket->t_cnt) {
301828496968SChristoph Hellwig xlog_grant_add_space(log, &log->l_reserve_head.grant,
3019663e496aSDave Chinner ticket->t_unit_res);
30208b41e3f9SChristoph Hellwig trace_xfs_log_ticket_regrant_exit(log, ticket);
30210b1b213fSChristoph Hellwig
30221da177e4SLinus Torvalds ticket->t_curr_res = ticket->t_unit_res;
30238b41e3f9SChristoph Hellwig }
30241da177e4SLinus Torvalds
30258b41e3f9SChristoph Hellwig xfs_log_ticket_put(ticket);
30268b41e3f9SChristoph Hellwig }
30271da177e4SLinus Torvalds
30281da177e4SLinus Torvalds /*
30291da177e4SLinus Torvalds * Give back the space left from a reservation.
30301da177e4SLinus Torvalds *
30311da177e4SLinus Torvalds * All the information we need to make a correct determination of space left
30321da177e4SLinus Torvalds * is present. For non-permanent reservations, things are quite easy. The
30331da177e4SLinus Torvalds * count should have been decremented to zero. We only need to deal with the
30341da177e4SLinus Torvalds * space remaining in the current reservation part of the ticket. If the
30351da177e4SLinus Torvalds * ticket contains a permanent reservation, there may be left over space which
30361da177e4SLinus Torvalds * needs to be released. A count of N means that N-1 refills of the current
30371da177e4SLinus Torvalds * reservation can be done before we need to ask for more space. The first
30381da177e4SLinus Torvalds * one goes to fill up the first current reservation. Once we run out of
30391da177e4SLinus Torvalds * space, the count will stay at zero and the only space remaining will be
30401da177e4SLinus Torvalds * in the current reservation field.
30411da177e4SLinus Torvalds */
30428b41e3f9SChristoph Hellwig void
xfs_log_ticket_ungrant(struct xlog * log,struct xlog_ticket * ticket)30438b41e3f9SChristoph Hellwig xfs_log_ticket_ungrant(
30449a8d2fdbSMark Tinguely struct xlog *log,
30459a8d2fdbSMark Tinguely struct xlog_ticket *ticket)
30461da177e4SLinus Torvalds {
3047663e496aSDave Chinner int bytes;
3048663e496aSDave Chinner
30498b41e3f9SChristoph Hellwig trace_xfs_log_ticket_ungrant(log, ticket);
30508b41e3f9SChristoph Hellwig
30511da177e4SLinus Torvalds if (ticket->t_cnt > 0)
30521da177e4SLinus Torvalds ticket->t_cnt--;
30531da177e4SLinus Torvalds
30548b41e3f9SChristoph Hellwig trace_xfs_log_ticket_ungrant_sub(log, ticket);
30551da177e4SLinus Torvalds
3056663e496aSDave Chinner /*
3057663e496aSDave Chinner * If this is a permanent reservation ticket, we may be able to free
30581da177e4SLinus Torvalds * up more space based on the remaining count.
30591da177e4SLinus Torvalds */
3060663e496aSDave Chinner bytes = ticket->t_curr_res;
30611da177e4SLinus Torvalds if (ticket->t_cnt > 0) {
30621da177e4SLinus Torvalds ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
3063663e496aSDave Chinner bytes += ticket->t_unit_res*ticket->t_cnt;
30641da177e4SLinus Torvalds }
30651da177e4SLinus Torvalds
306628496968SChristoph Hellwig xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
306728496968SChristoph Hellwig xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
3068663e496aSDave Chinner
30698b41e3f9SChristoph Hellwig trace_xfs_log_ticket_ungrant_exit(log, ticket);
30700b1b213fSChristoph Hellwig
3071cfb7cdcaSChristoph Hellwig xfs_log_space_wake(log->l_mp);
30728b41e3f9SChristoph Hellwig xfs_log_ticket_put(ticket);
307309a423a3SChristoph Hellwig }
30741da177e4SLinus Torvalds
30751da177e4SLinus Torvalds /*
3076b843299bSDave Chinner * This routine will mark the current iclog in the ring as WANT_SYNC and move
3077b843299bSDave Chinner * the current iclog pointer to the next iclog in the ring.
30781da177e4SLinus Torvalds */
30790020a190SDave Chinner void
xlog_state_switch_iclogs(struct xlog * log,struct xlog_in_core * iclog,int eventual_size)30809a8d2fdbSMark Tinguely xlog_state_switch_iclogs(
30819a8d2fdbSMark Tinguely struct xlog *log,
30829a8d2fdbSMark Tinguely struct xlog_in_core *iclog,
30831da177e4SLinus Torvalds int eventual_size)
30841da177e4SLinus Torvalds {
30851da177e4SLinus Torvalds ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
308669363999SChristoph Hellwig assert_spin_locked(&log->l_icloglock);
3087956f6daaSDave Chinner trace_xlog_iclog_switch(iclog, _RET_IP_);
308869363999SChristoph Hellwig
30891da177e4SLinus Torvalds if (!eventual_size)
30901da177e4SLinus Torvalds eventual_size = iclog->ic_offset;
30911da177e4SLinus Torvalds iclog->ic_state = XLOG_STATE_WANT_SYNC;
3092b53e675dSChristoph Hellwig iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
30931da177e4SLinus Torvalds log->l_prev_block = log->l_curr_block;
30941da177e4SLinus Torvalds log->l_prev_cycle = log->l_curr_cycle;
30951da177e4SLinus Torvalds
30961da177e4SLinus Torvalds /* roll log?: ic_offset changed later */
30971da177e4SLinus Torvalds log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
30981da177e4SLinus Torvalds
30991da177e4SLinus Torvalds /* Round up to next log-sunit */
3100a6a65fefSDave Chinner if (log->l_iclog_roundoff > BBSIZE) {
310118842e0aSGeert Uytterhoeven uint32_t sunit_bb = BTOBB(log->l_iclog_roundoff);
31021da177e4SLinus Torvalds log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
31031da177e4SLinus Torvalds }
31041da177e4SLinus Torvalds
31051da177e4SLinus Torvalds if (log->l_curr_block >= log->l_logBBsize) {
3106a45086e2SBrian Foster /*
3107a45086e2SBrian Foster * Rewind the current block before the cycle is bumped to make
3108a45086e2SBrian Foster * sure that the combined LSN never transiently moves forward
3109a45086e2SBrian Foster * when the log wraps to the next cycle. This is to support the
3110a45086e2SBrian Foster * unlocked sample of these fields from xlog_valid_lsn(). Most
3111a45086e2SBrian Foster * other cases should acquire l_icloglock.
3112a45086e2SBrian Foster */
3113a45086e2SBrian Foster log->l_curr_block -= log->l_logBBsize;
3114a45086e2SBrian Foster ASSERT(log->l_curr_block >= 0);
3115a45086e2SBrian Foster smp_wmb();
31161da177e4SLinus Torvalds log->l_curr_cycle++;
31171da177e4SLinus Torvalds if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
31181da177e4SLinus Torvalds log->l_curr_cycle++;
31191da177e4SLinus Torvalds }
31201da177e4SLinus Torvalds ASSERT(iclog == log->l_iclog);
31211da177e4SLinus Torvalds log->l_iclog = iclog->ic_next;
3122b843299bSDave Chinner }
31231da177e4SLinus Torvalds
31241da177e4SLinus Torvalds /*
31258191d822SDave Chinner * Force the iclog to disk and check if the iclog has been completed before
31268191d822SDave Chinner * xlog_force_iclog() returns. This can happen on synchronous (e.g.
31278191d822SDave Chinner * pmem) or fast async storage because we drop the icloglock to issue the IO.
31288191d822SDave Chinner * If completion has already occurred, tell the caller so that it can avoid an
31298191d822SDave Chinner * unnecessary wait on the iclog.
31308191d822SDave Chinner */
31318191d822SDave Chinner static int
xlog_force_and_check_iclog(struct xlog_in_core * iclog,bool * completed)31328191d822SDave Chinner xlog_force_and_check_iclog(
31338191d822SDave Chinner struct xlog_in_core *iclog,
31348191d822SDave Chinner bool *completed)
31358191d822SDave Chinner {
31368191d822SDave Chinner xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header.h_lsn);
31378191d822SDave Chinner int error;
31388191d822SDave Chinner
31398191d822SDave Chinner *completed = false;
31408191d822SDave Chinner error = xlog_force_iclog(iclog);
31418191d822SDave Chinner if (error)
31428191d822SDave Chinner return error;
31438191d822SDave Chinner
31448191d822SDave Chinner /*
31458191d822SDave Chinner * If the iclog has already been completed and reused the header LSN
31468191d822SDave Chinner * will have been rewritten by completion
31478191d822SDave Chinner */
31488191d822SDave Chinner if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn)
31498191d822SDave Chinner *completed = true;
31508191d822SDave Chinner return 0;
31518191d822SDave Chinner }
31528191d822SDave Chinner
31538191d822SDave Chinner /*
31541da177e4SLinus Torvalds * Write out all data in the in-core log as of this exact moment in time.
31551da177e4SLinus Torvalds *
31561da177e4SLinus Torvalds * Data may be written to the in-core log during this call. However,
31571da177e4SLinus Torvalds * we don't guarantee this data will be written out. A change from past
31581da177e4SLinus Torvalds * implementation means this routine will *not* write out zero length LRs.
31591da177e4SLinus Torvalds *
31601da177e4SLinus Torvalds * Basically, we try and perform an intelligent scan of the in-core logs.
31611da177e4SLinus Torvalds * If we determine there is no flushable data, we just return. There is no
31621da177e4SLinus Torvalds * flushable data if:
31631da177e4SLinus Torvalds *
31641da177e4SLinus Torvalds * 1. the current iclog is active and has no data; the previous iclog
31651da177e4SLinus Torvalds * is in the active or dirty state.
31661da177e4SLinus Torvalds * 2. the current iclog is drity, and the previous iclog is in the
31671da177e4SLinus Torvalds * active or dirty state.
31681da177e4SLinus Torvalds *
316912017fafSDavid Chinner * We may sleep if:
31701da177e4SLinus Torvalds *
31711da177e4SLinus Torvalds * 1. the current iclog is not in the active nor dirty state.
31721da177e4SLinus Torvalds * 2. the current iclog dirty, and the previous iclog is not in the
31731da177e4SLinus Torvalds * active nor dirty state.
31741da177e4SLinus Torvalds * 3. the current iclog is active, and there is another thread writing
31751da177e4SLinus Torvalds * to this particular iclog.
31761da177e4SLinus Torvalds * 4. a) the current iclog is active and has no other writers
31771da177e4SLinus Torvalds * b) when we return from flushing out this iclog, it is still
31781da177e4SLinus Torvalds * not in the active nor dirty state.
31791da177e4SLinus Torvalds */
3180a14a348bSChristoph Hellwig int
xfs_log_force(struct xfs_mount * mp,uint flags)318160e5bb78SChristoph Hellwig xfs_log_force(
3182a14a348bSChristoph Hellwig struct xfs_mount *mp,
318360e5bb78SChristoph Hellwig uint flags)
31841da177e4SLinus Torvalds {
3185ad223e60SMark Tinguely struct xlog *log = mp->m_log;
3186a14a348bSChristoph Hellwig struct xlog_in_core *iclog;
31871da177e4SLinus Torvalds
3188ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_log_force);
318960e5bb78SChristoph Hellwig trace_xfs_log_force(mp, 0, _RET_IP_);
3190a14a348bSChristoph Hellwig
3191a44f13edSDave Chinner xlog_cil_force(log);
319271e330b5SDave Chinner
3193b22cd72cSEric Sandeen spin_lock(&log->l_icloglock);
31945112e206SDave Chinner if (xlog_is_shutdown(log))
3195e6b96570SChristoph Hellwig goto out_error;
31961da177e4SLinus Torvalds
31975112e206SDave Chinner iclog = log->l_iclog;
3198956f6daaSDave Chinner trace_xlog_iclog_force(iclog, _RET_IP_);
3199956f6daaSDave Chinner
3200e6b96570SChristoph Hellwig if (iclog->ic_state == XLOG_STATE_DIRTY ||
3201e6b96570SChristoph Hellwig (iclog->ic_state == XLOG_STATE_ACTIVE &&
3202e6b96570SChristoph Hellwig atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) {
32031da177e4SLinus Torvalds /*
3204e6b96570SChristoph Hellwig * If the head is dirty or (active and empty), then we need to
3205e6b96570SChristoph Hellwig * look at the previous iclog.
3206e6b96570SChristoph Hellwig *
3207e6b96570SChristoph Hellwig * If the previous iclog is active or dirty we are done. There
3208e6b96570SChristoph Hellwig * is nothing to sync out. Otherwise, we attach ourselves to the
32091da177e4SLinus Torvalds * previous iclog and go to sleep.
32101da177e4SLinus Torvalds */
32111da177e4SLinus Torvalds iclog = iclog->ic_prev;
3212e6b96570SChristoph Hellwig } else if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3213155cc6b7SDavid Chinner if (atomic_read(&iclog->ic_refcnt) == 0) {
321445eddb41SDave Chinner /* We have exclusive access to this iclog. */
32158191d822SDave Chinner bool completed;
32168191d822SDave Chinner
32178191d822SDave Chinner if (xlog_force_and_check_iclog(iclog, &completed))
3218df732b29SChristoph Hellwig goto out_error;
3219a14a348bSChristoph Hellwig
32208191d822SDave Chinner if (completed)
3221e6b96570SChristoph Hellwig goto out_unlock;
32221da177e4SLinus Torvalds } else {
3223e6b96570SChristoph Hellwig /*
32242bf1ec0fSDave Chinner * Someone else is still writing to this iclog, so we
32252bf1ec0fSDave Chinner * need to ensure that when they release the iclog it
32262bf1ec0fSDave Chinner * gets synced immediately as we may be waiting on it.
32271da177e4SLinus Torvalds */
32281da177e4SLinus Torvalds xlog_state_switch_iclogs(log, iclog, 0);
32291da177e4SLinus Torvalds }
32301da177e4SLinus Torvalds }
32311da177e4SLinus Torvalds
32322bf1ec0fSDave Chinner /*
32332bf1ec0fSDave Chinner * The iclog we are about to wait on may contain the checkpoint pushed
32342bf1ec0fSDave Chinner * by the above xlog_cil_force() call, but it may not have been pushed
32352bf1ec0fSDave Chinner * to disk yet. Like the ACTIVE case above, we need to make sure caches
32362bf1ec0fSDave Chinner * are flushed when this iclog is written.
32372bf1ec0fSDave Chinner */
32382bf1ec0fSDave Chinner if (iclog->ic_state == XLOG_STATE_WANT_SYNC)
32392bf1ec0fSDave Chinner iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
32402bf1ec0fSDave Chinner
324181e5b50aSChristoph Hellwig if (flags & XFS_LOG_SYNC)
324281e5b50aSChristoph Hellwig return xlog_wait_on_iclog(iclog);
3243e6b96570SChristoph Hellwig out_unlock:
3244e6b96570SChristoph Hellwig spin_unlock(&log->l_icloglock);
3245e6b96570SChristoph Hellwig return 0;
3246e6b96570SChristoph Hellwig out_error:
3247e6b96570SChristoph Hellwig spin_unlock(&log->l_icloglock);
3248e6b96570SChristoph Hellwig return -EIO;
3249a14a348bSChristoph Hellwig }
32501da177e4SLinus Torvalds
32510020a190SDave Chinner /*
32520020a190SDave Chinner * Force the log to a specific LSN.
32530020a190SDave Chinner *
32540020a190SDave Chinner * If an iclog with that lsn can be found:
32550020a190SDave Chinner * If it is in the DIRTY state, just return.
32560020a190SDave Chinner * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
32570020a190SDave Chinner * state and go to sleep or return.
32580020a190SDave Chinner * If it is in any other state, go to sleep or return.
32590020a190SDave Chinner *
32600020a190SDave Chinner * Synchronous forces are implemented with a wait queue. All callers trying
32610020a190SDave Chinner * to force a given lsn to disk must wait on the queue attached to the
32620020a190SDave Chinner * specific in-core log. When given in-core log finally completes its write
32630020a190SDave Chinner * to disk, that thread will wake up all threads waiting on the queue.
32640020a190SDave Chinner */
32653e4da466SChristoph Hellwig static int
xlog_force_lsn(struct xlog * log,xfs_lsn_t lsn,uint flags,int * log_flushed,bool already_slept)32665f9b4b0dSDave Chinner xlog_force_lsn(
32675f9b4b0dSDave Chinner struct xlog *log,
32681da177e4SLinus Torvalds xfs_lsn_t lsn,
3269f538d4daSChristoph Hellwig uint flags,
32703e4da466SChristoph Hellwig int *log_flushed,
32713e4da466SChristoph Hellwig bool already_slept)
32721da177e4SLinus Torvalds {
3273a14a348bSChristoph Hellwig struct xlog_in_core *iclog;
32748191d822SDave Chinner bool completed;
32751da177e4SLinus Torvalds
3276b22cd72cSEric Sandeen spin_lock(&log->l_icloglock);
32775112e206SDave Chinner if (xlog_is_shutdown(log))
327893806299SChristoph Hellwig goto out_error;
32791da177e4SLinus Torvalds
32805112e206SDave Chinner iclog = log->l_iclog;
328193806299SChristoph Hellwig while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3282956f6daaSDave Chinner trace_xlog_iclog_force_lsn(iclog, _RET_IP_);
32831da177e4SLinus Torvalds iclog = iclog->ic_next;
328493806299SChristoph Hellwig if (iclog == log->l_iclog)
328593806299SChristoph Hellwig goto out_unlock;
32861da177e4SLinus Torvalds }
32871da177e4SLinus Torvalds
32882bf1ec0fSDave Chinner switch (iclog->ic_state) {
32892bf1ec0fSDave Chinner case XLOG_STATE_ACTIVE:
32901da177e4SLinus Torvalds /*
329193806299SChristoph Hellwig * We sleep here if we haven't already slept (e.g. this is the
329293806299SChristoph Hellwig * first time we've looked at the correct iclog buf) and the
329393806299SChristoph Hellwig * buffer before us is going to be sync'ed. The reason for this
329493806299SChristoph Hellwig * is that if we are doing sync transactions here, by waiting
329593806299SChristoph Hellwig * for the previous I/O to complete, we can allow a few more
329693806299SChristoph Hellwig * transactions into this iclog before we close it down.
32971da177e4SLinus Torvalds *
329893806299SChristoph Hellwig * Otherwise, we mark the buffer WANT_SYNC, and bump up the
329993806299SChristoph Hellwig * refcnt so we can release the log (which drops the ref count).
330093806299SChristoph Hellwig * The state switch keeps new transaction commits from using
330193806299SChristoph Hellwig * this buffer. When the current commits finish writing into
330293806299SChristoph Hellwig * the buffer, the refcount will drop to zero and the buffer
330393806299SChristoph Hellwig * will go out then.
33041da177e4SLinus Torvalds */
33051da177e4SLinus Torvalds if (!already_slept &&
33061858bb0bSChristoph Hellwig (iclog->ic_prev->ic_state == XLOG_STATE_WANT_SYNC ||
33071858bb0bSChristoph Hellwig iclog->ic_prev->ic_state == XLOG_STATE_SYNCING)) {
3308eb40a875SDave Chinner xlog_wait(&iclog->ic_prev->ic_write_wait,
3309eb40a875SDave Chinner &log->l_icloglock);
33103e4da466SChristoph Hellwig return -EAGAIN;
3311a14a348bSChristoph Hellwig }
33128191d822SDave Chinner if (xlog_force_and_check_iclog(iclog, &completed))
3313df732b29SChristoph Hellwig goto out_error;
3314a14a348bSChristoph Hellwig if (log_flushed)
3315f538d4daSChristoph Hellwig *log_flushed = 1;
33168191d822SDave Chinner if (completed)
33178191d822SDave Chinner goto out_unlock;
33182bf1ec0fSDave Chinner break;
33192bf1ec0fSDave Chinner case XLOG_STATE_WANT_SYNC:
33202bf1ec0fSDave Chinner /*
33212bf1ec0fSDave Chinner * This iclog may contain the checkpoint pushed by the
33222bf1ec0fSDave Chinner * xlog_cil_force_seq() call, but there are other writers still
33232bf1ec0fSDave Chinner * accessing it so it hasn't been pushed to disk yet. Like the
33242bf1ec0fSDave Chinner * ACTIVE case above, we need to make sure caches are flushed
33252bf1ec0fSDave Chinner * when this iclog is written.
33262bf1ec0fSDave Chinner */
33272bf1ec0fSDave Chinner iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
33282bf1ec0fSDave Chinner break;
33292bf1ec0fSDave Chinner default:
33302bf1ec0fSDave Chinner /*
33312bf1ec0fSDave Chinner * The entire checkpoint was written by the CIL force and is on
33322bf1ec0fSDave Chinner * its way to disk already. It will be stable when it
33332bf1ec0fSDave Chinner * completes, so we don't need to manipulate caches here at all.
33342bf1ec0fSDave Chinner * We just need to wait for completion if necessary.
33352bf1ec0fSDave Chinner */
33362bf1ec0fSDave Chinner break;
33371da177e4SLinus Torvalds }
33381da177e4SLinus Torvalds
333981e5b50aSChristoph Hellwig if (flags & XFS_LOG_SYNC)
334081e5b50aSChristoph Hellwig return xlog_wait_on_iclog(iclog);
334193806299SChristoph Hellwig out_unlock:
3342b22cd72cSEric Sandeen spin_unlock(&log->l_icloglock);
3343014c2544SJesper Juhl return 0;
334493806299SChristoph Hellwig out_error:
334593806299SChristoph Hellwig spin_unlock(&log->l_icloglock);
334693806299SChristoph Hellwig return -EIO;
3347a14a348bSChristoph Hellwig }
33481da177e4SLinus Torvalds
3349a14a348bSChristoph Hellwig /*
33500020a190SDave Chinner * Force the log to a specific checkpoint sequence.
33513e4da466SChristoph Hellwig *
33520020a190SDave Chinner * First force the CIL so that all the required changes have been flushed to the
33530020a190SDave Chinner * iclogs. If the CIL force completed it will return a commit LSN that indicates
33540020a190SDave Chinner * the iclog that needs to be flushed to stable storage. If the caller needs
33550020a190SDave Chinner * a synchronous log force, we will wait on the iclog with the LSN returned by
33560020a190SDave Chinner * xlog_cil_force_seq() to be completed.
33573e4da466SChristoph Hellwig */
33583e4da466SChristoph Hellwig int
xfs_log_force_seq(struct xfs_mount * mp,xfs_csn_t seq,uint flags,int * log_flushed)33595f9b4b0dSDave Chinner xfs_log_force_seq(
33603e4da466SChristoph Hellwig struct xfs_mount *mp,
33615f9b4b0dSDave Chinner xfs_csn_t seq,
33623e4da466SChristoph Hellwig uint flags,
33633e4da466SChristoph Hellwig int *log_flushed)
33643e4da466SChristoph Hellwig {
33655f9b4b0dSDave Chinner struct xlog *log = mp->m_log;
33665f9b4b0dSDave Chinner xfs_lsn_t lsn;
33673e4da466SChristoph Hellwig int ret;
33685f9b4b0dSDave Chinner ASSERT(seq != 0);
33693e4da466SChristoph Hellwig
33703e4da466SChristoph Hellwig XFS_STATS_INC(mp, xs_log_force);
33715f9b4b0dSDave Chinner trace_xfs_log_force(mp, seq, _RET_IP_);
33723e4da466SChristoph Hellwig
33735f9b4b0dSDave Chinner lsn = xlog_cil_force_seq(log, seq);
33743e4da466SChristoph Hellwig if (lsn == NULLCOMMITLSN)
33753e4da466SChristoph Hellwig return 0;
33763e4da466SChristoph Hellwig
33775f9b4b0dSDave Chinner ret = xlog_force_lsn(log, lsn, flags, log_flushed, false);
33785f9b4b0dSDave Chinner if (ret == -EAGAIN) {
33795f9b4b0dSDave Chinner XFS_STATS_INC(mp, xs_log_force_sleep);
33805f9b4b0dSDave Chinner ret = xlog_force_lsn(log, lsn, flags, log_flushed, true);
33815f9b4b0dSDave Chinner }
33823e4da466SChristoph Hellwig return ret;
33833e4da466SChristoph Hellwig }
33843e4da466SChristoph Hellwig
33851da177e4SLinus Torvalds /*
33869da096fdSMalcolm Parsons * Free a used ticket when its refcount falls to zero.
33871da177e4SLinus Torvalds */
3388cc09c0dcSDave Chinner void
xfs_log_ticket_put(xlog_ticket_t * ticket)3389cc09c0dcSDave Chinner xfs_log_ticket_put(
33901da177e4SLinus Torvalds xlog_ticket_t *ticket)
33911da177e4SLinus Torvalds {
3392cc09c0dcSDave Chinner ASSERT(atomic_read(&ticket->t_ref) > 0);
3393eb40a875SDave Chinner if (atomic_dec_and_test(&ticket->t_ref))
3394182696fbSDarrick J. Wong kmem_cache_free(xfs_log_ticket_cache, ticket);
3395cc09c0dcSDave Chinner }
33961da177e4SLinus Torvalds
3397cc09c0dcSDave Chinner xlog_ticket_t *
xfs_log_ticket_get(xlog_ticket_t * ticket)3398cc09c0dcSDave Chinner xfs_log_ticket_get(
3399cc09c0dcSDave Chinner xlog_ticket_t *ticket)
3400cc09c0dcSDave Chinner {
3401cc09c0dcSDave Chinner ASSERT(atomic_read(&ticket->t_ref) > 0);
3402cc09c0dcSDave Chinner atomic_inc(&ticket->t_ref);
3403cc09c0dcSDave Chinner return ticket;
3404cc09c0dcSDave Chinner }
34051da177e4SLinus Torvalds
34061da177e4SLinus Torvalds /*
3407e773fc93SJie Liu * Figure out the total log space unit (in bytes) that would be
3408e773fc93SJie Liu * required for a log ticket.
34091da177e4SLinus Torvalds */
3410a6a65fefSDave Chinner static int
xlog_calc_unit_res(struct xlog * log,int unit_bytes,int * niclogs)3411a6a65fefSDave Chinner xlog_calc_unit_res(
3412a6a65fefSDave Chinner struct xlog *log,
341331151cc3SDave Chinner int unit_bytes,
341431151cc3SDave Chinner int *niclogs)
34151da177e4SLinus Torvalds {
34169b9fc2b7SDave Chinner int iclog_space;
3417e773fc93SJie Liu uint num_headers;
34181da177e4SLinus Torvalds
34191da177e4SLinus Torvalds /*
34201da177e4SLinus Torvalds * Permanent reservations have up to 'cnt'-1 active log operations
34211da177e4SLinus Torvalds * in the log. A unit in this case is the amount of space for one
34221da177e4SLinus Torvalds * of these log operations. Normal reservations have a cnt of 1
34231da177e4SLinus Torvalds * and their unit amount is the total amount of space required.
34241da177e4SLinus Torvalds *
34251da177e4SLinus Torvalds * The following lines of code account for non-transaction data
34261da177e4SLinus Torvalds * which occupy space in the on-disk log.
342732fb9b57STim Shimmin *
342832fb9b57STim Shimmin * Normal form of a transaction is:
342932fb9b57STim Shimmin * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
343032fb9b57STim Shimmin * and then there are LR hdrs, split-recs and roundoff at end of syncs.
343132fb9b57STim Shimmin *
343232fb9b57STim Shimmin * We need to account for all the leadup data and trailer data
343332fb9b57STim Shimmin * around the transaction data.
343432fb9b57STim Shimmin * And then we need to account for the worst case in terms of using
343532fb9b57STim Shimmin * more space.
343632fb9b57STim Shimmin * The worst case will happen if:
343732fb9b57STim Shimmin * - the placement of the transaction happens to be such that the
343832fb9b57STim Shimmin * roundoff is at its maximum
343932fb9b57STim Shimmin * - the transaction data is synced before the commit record is synced
344032fb9b57STim Shimmin * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
344132fb9b57STim Shimmin * Therefore the commit record is in its own Log Record.
344232fb9b57STim Shimmin * This can happen as the commit record is called with its
344332fb9b57STim Shimmin * own region to xlog_write().
344432fb9b57STim Shimmin * This then means that in the worst case, roundoff can happen for
344532fb9b57STim Shimmin * the commit-rec as well.
344632fb9b57STim Shimmin * The commit-rec is smaller than padding in this scenario and so it is
344732fb9b57STim Shimmin * not added separately.
34481da177e4SLinus Torvalds */
34491da177e4SLinus Torvalds
345032fb9b57STim Shimmin /* for trans header */
34511da177e4SLinus Torvalds unit_bytes += sizeof(xlog_op_header_t);
345232fb9b57STim Shimmin unit_bytes += sizeof(xfs_trans_header_t);
34531da177e4SLinus Torvalds
345432fb9b57STim Shimmin /* for start-rec */
34551da177e4SLinus Torvalds unit_bytes += sizeof(xlog_op_header_t);
34561da177e4SLinus Torvalds
34579b9fc2b7SDave Chinner /*
34589b9fc2b7SDave Chinner * for LR headers - the space for data in an iclog is the size minus
34599b9fc2b7SDave Chinner * the space used for the headers. If we use the iclog size, then we
34609b9fc2b7SDave Chinner * undercalculate the number of headers required.
34619b9fc2b7SDave Chinner *
34629b9fc2b7SDave Chinner * Furthermore - the addition of op headers for split-recs might
34639b9fc2b7SDave Chinner * increase the space required enough to require more log and op
34649b9fc2b7SDave Chinner * headers, so take that into account too.
34659b9fc2b7SDave Chinner *
34669b9fc2b7SDave Chinner * IMPORTANT: This reservation makes the assumption that if this
34679b9fc2b7SDave Chinner * transaction is the first in an iclog and hence has the LR headers
34689b9fc2b7SDave Chinner * accounted to it, then the remaining space in the iclog is
34699b9fc2b7SDave Chinner * exclusively for this transaction. i.e. if the transaction is larger
34709b9fc2b7SDave Chinner * than the iclog, it will be the only thing in that iclog.
34719b9fc2b7SDave Chinner * Fundamentally, this means we must pass the entire log vector to
34729b9fc2b7SDave Chinner * xlog_write to guarantee this.
34739b9fc2b7SDave Chinner */
34749b9fc2b7SDave Chinner iclog_space = log->l_iclog_size - log->l_iclog_hsize;
34759b9fc2b7SDave Chinner num_headers = howmany(unit_bytes, iclog_space);
34769b9fc2b7SDave Chinner
34779b9fc2b7SDave Chinner /* for split-recs - ophdrs added when data split over LRs */
34789b9fc2b7SDave Chinner unit_bytes += sizeof(xlog_op_header_t) * num_headers;
34799b9fc2b7SDave Chinner
34809b9fc2b7SDave Chinner /* add extra header reservations if we overrun */
34819b9fc2b7SDave Chinner while (!num_headers ||
34829b9fc2b7SDave Chinner howmany(unit_bytes, iclog_space) > num_headers) {
34839b9fc2b7SDave Chinner unit_bytes += sizeof(xlog_op_header_t);
34849b9fc2b7SDave Chinner num_headers++;
34859b9fc2b7SDave Chinner }
34861da177e4SLinus Torvalds unit_bytes += log->l_iclog_hsize * num_headers;
34871da177e4SLinus Torvalds
348832fb9b57STim Shimmin /* for commit-rec LR header - note: padding will subsume the ophdr */
348932fb9b57STim Shimmin unit_bytes += log->l_iclog_hsize;
349032fb9b57STim Shimmin
3491a6a65fefSDave Chinner /* roundoff padding for transaction data and one for commit record */
3492a6a65fefSDave Chinner unit_bytes += 2 * log->l_iclog_roundoff;
349332fb9b57STim Shimmin
349431151cc3SDave Chinner if (niclogs)
349531151cc3SDave Chinner *niclogs = num_headers;
3496e773fc93SJie Liu return unit_bytes;
3497e773fc93SJie Liu }
3498e773fc93SJie Liu
3499a6a65fefSDave Chinner int
xfs_log_calc_unit_res(struct xfs_mount * mp,int unit_bytes)3500a6a65fefSDave Chinner xfs_log_calc_unit_res(
3501a6a65fefSDave Chinner struct xfs_mount *mp,
3502a6a65fefSDave Chinner int unit_bytes)
3503a6a65fefSDave Chinner {
350431151cc3SDave Chinner return xlog_calc_unit_res(mp->m_log, unit_bytes, NULL);
3505a6a65fefSDave Chinner }
3506a6a65fefSDave Chinner
3507e773fc93SJie Liu /*
3508e773fc93SJie Liu * Allocate and initialise a new log ticket.
3509e773fc93SJie Liu */
3510e773fc93SJie Liu struct xlog_ticket *
xlog_ticket_alloc(struct xlog * log,int unit_bytes,int cnt,bool permanent)3511e773fc93SJie Liu xlog_ticket_alloc(
3512e773fc93SJie Liu struct xlog *log,
3513e773fc93SJie Liu int unit_bytes,
3514e773fc93SJie Liu int cnt,
3515ca4f2589SCarlos Maiolino bool permanent)
3516e773fc93SJie Liu {
3517e773fc93SJie Liu struct xlog_ticket *tic;
3518e773fc93SJie Liu int unit_res;
3519e773fc93SJie Liu
3520182696fbSDarrick J. Wong tic = kmem_cache_zalloc(xfs_log_ticket_cache, GFP_NOFS | __GFP_NOFAIL);
3521e773fc93SJie Liu
352231151cc3SDave Chinner unit_res = xlog_calc_unit_res(log, unit_bytes, &tic->t_iclog_hdrs);
3523e773fc93SJie Liu
3524cc09c0dcSDave Chinner atomic_set(&tic->t_ref, 1);
352514a7235fSChristoph Hellwig tic->t_task = current;
352610547941SDave Chinner INIT_LIST_HEAD(&tic->t_queue);
3527e773fc93SJie Liu tic->t_unit_res = unit_res;
3528e773fc93SJie Liu tic->t_curr_res = unit_res;
35291da177e4SLinus Torvalds tic->t_cnt = cnt;
35301da177e4SLinus Torvalds tic->t_ocnt = cnt;
3531a251c17aSJason A. Donenfeld tic->t_tid = get_random_u32();
35329006fb91SChristoph Hellwig if (permanent)
35331da177e4SLinus Torvalds tic->t_flags |= XLOG_TIC_PERM_RESERV;
35341da177e4SLinus Torvalds
35351da177e4SLinus Torvalds return tic;
3536cc09c0dcSDave Chinner }
35371da177e4SLinus Torvalds
3538cfcbbbd0SNathan Scott #if defined(DEBUG)
35391da177e4SLinus Torvalds /*
3540da8a1a4aSDave Chinner * Check to make sure the grant write head didn't just over lap the tail. If
3541da8a1a4aSDave Chinner * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3542da8a1a4aSDave Chinner * the cycles differ by exactly one and check the byte count.
3543da8a1a4aSDave Chinner *
3544da8a1a4aSDave Chinner * This check is run unlocked, so can give false positives. Rather than assert
3545da8a1a4aSDave Chinner * on failures, use a warn-once flag and a panic tag to allow the admin to
3546da8a1a4aSDave Chinner * determine if they want to panic the machine when such an error occurs. For
3547da8a1a4aSDave Chinner * debug kernels this will have the same effect as using an assert but, unlinke
3548da8a1a4aSDave Chinner * an assert, it can be turned off at runtime.
3549da8a1a4aSDave Chinner */
35501da177e4SLinus Torvalds STATIC void
xlog_verify_grant_tail(struct xlog * log)35513f336c6fSDave Chinner xlog_verify_grant_tail(
3552ad223e60SMark Tinguely struct xlog *log)
35533f336c6fSDave Chinner {
35541c3cb9ecSDave Chinner int tail_cycle, tail_blocks;
3555a69ed03cSDave Chinner int cycle, space;
35563f336c6fSDave Chinner
355728496968SChristoph Hellwig xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
35581c3cb9ecSDave Chinner xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
35591c3cb9ecSDave Chinner if (tail_cycle != cycle) {
3560da8a1a4aSDave Chinner if (cycle - 1 != tail_cycle &&
3561e1d06e5fSDave Chinner !test_and_set_bit(XLOG_TAIL_WARN, &log->l_opstate)) {
3562da8a1a4aSDave Chinner xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3563da8a1a4aSDave Chinner "%s: cycle - 1 != tail_cycle", __func__);
3564da8a1a4aSDave Chinner }
3565da8a1a4aSDave Chinner
3566da8a1a4aSDave Chinner if (space > BBTOB(tail_blocks) &&
3567e1d06e5fSDave Chinner !test_and_set_bit(XLOG_TAIL_WARN, &log->l_opstate)) {
3568da8a1a4aSDave Chinner xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3569da8a1a4aSDave Chinner "%s: space > BBTOB(tail_blocks)", __func__);
3570da8a1a4aSDave Chinner }
35713f336c6fSDave Chinner }
35723f336c6fSDave Chinner }
35733f336c6fSDave Chinner
35741da177e4SLinus Torvalds /* check if it will fit */
35751da177e4SLinus Torvalds STATIC void
xlog_verify_tail_lsn(struct xlog * log,struct xlog_in_core * iclog)35769a8d2fdbSMark Tinguely xlog_verify_tail_lsn(
35779a8d2fdbSMark Tinguely struct xlog *log,
35789d110014SDave Chinner struct xlog_in_core *iclog)
35791da177e4SLinus Torvalds {
35809d110014SDave Chinner xfs_lsn_t tail_lsn = be64_to_cpu(iclog->ic_header.h_tail_lsn);
35811da177e4SLinus Torvalds int blocks;
35821da177e4SLinus Torvalds
35831da177e4SLinus Torvalds if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
35841da177e4SLinus Torvalds blocks =
35851da177e4SLinus Torvalds log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
35861da177e4SLinus Torvalds if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3587a0fa2b67SDave Chinner xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
35881da177e4SLinus Torvalds } else {
35891da177e4SLinus Torvalds ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
35901da177e4SLinus Torvalds
35911da177e4SLinus Torvalds if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3592a0fa2b67SDave Chinner xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
35931da177e4SLinus Torvalds
35941da177e4SLinus Torvalds blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
35951da177e4SLinus Torvalds if (blocks < BTOBB(iclog->ic_offset) + 1)
3596a0fa2b67SDave Chinner xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
35971da177e4SLinus Torvalds }
3598b843299bSDave Chinner }
35991da177e4SLinus Torvalds
36001da177e4SLinus Torvalds /*
36011da177e4SLinus Torvalds * Perform a number of checks on the iclog before writing to disk.
36021da177e4SLinus Torvalds *
36031da177e4SLinus Torvalds * 1. Make sure the iclogs are still circular
36041da177e4SLinus Torvalds * 2. Make sure we have a good magic number
36051da177e4SLinus Torvalds * 3. Make sure we don't have magic numbers in the data
36061da177e4SLinus Torvalds * 4. Check fields of each log operation header for:
36071da177e4SLinus Torvalds * A. Valid client identifier
36081da177e4SLinus Torvalds * B. tid ptr value falls in valid ptr space (user space code)
36091da177e4SLinus Torvalds * C. Length in log record header is correct according to the
36101da177e4SLinus Torvalds * individual operation headers within record.
36111da177e4SLinus Torvalds * 5. When a bwrite will occur within 5 blocks of the front of the physical
36121da177e4SLinus Torvalds * log, check the preceding blocks of the physical log to make sure all
36131da177e4SLinus Torvalds * the cycle numbers agree with the current cycle number.
36141da177e4SLinus Torvalds */
36151da177e4SLinus Torvalds STATIC void
xlog_verify_iclog(struct xlog * log,struct xlog_in_core * iclog,int count)36169a8d2fdbSMark Tinguely xlog_verify_iclog(
36179a8d2fdbSMark Tinguely struct xlog *log,
36189a8d2fdbSMark Tinguely struct xlog_in_core *iclog,
3619abca1f33SChristoph Hellwig int count)
36201da177e4SLinus Torvalds {
36211da177e4SLinus Torvalds xlog_op_header_t *ophead;
36221da177e4SLinus Torvalds xlog_in_core_t *icptr;
36231da177e4SLinus Torvalds xlog_in_core_2_t *xhdr;
36245809d5e0SChristoph Hellwig void *base_ptr, *ptr, *p;
3625db9d67d6SChristoph Hellwig ptrdiff_t field_offset;
3626c8ce540dSDarrick J. Wong uint8_t clientid;
36271da177e4SLinus Torvalds int len, i, j, k, op_len;
36281da177e4SLinus Torvalds int idx;
36291da177e4SLinus Torvalds
36301da177e4SLinus Torvalds /* check validity of iclog pointers */
3631b22cd72cSEric Sandeen spin_lock(&log->l_icloglock);
36321da177e4SLinus Torvalds icptr = log->l_iclog;
3633643f7c4eSGeyslan G. Bem for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next)
3634643f7c4eSGeyslan G. Bem ASSERT(icptr);
3635643f7c4eSGeyslan G. Bem
36361da177e4SLinus Torvalds if (icptr != log->l_iclog)
3637a0fa2b67SDave Chinner xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
3638b22cd72cSEric Sandeen spin_unlock(&log->l_icloglock);
36391da177e4SLinus Torvalds
36401da177e4SLinus Torvalds /* check log magic numbers */
364169ef921bSChristoph Hellwig if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
3642a0fa2b67SDave Chinner xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
36431da177e4SLinus Torvalds
36445809d5e0SChristoph Hellwig base_ptr = ptr = &iclog->ic_header;
36455809d5e0SChristoph Hellwig p = &iclog->ic_header;
36465809d5e0SChristoph Hellwig for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) {
364769ef921bSChristoph Hellwig if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
3648a0fa2b67SDave Chinner xfs_emerg(log->l_mp, "%s: unexpected magic num",
3649a0fa2b67SDave Chinner __func__);
36501da177e4SLinus Torvalds }
36511da177e4SLinus Torvalds
36521da177e4SLinus Torvalds /* check fields */
3653b53e675dSChristoph Hellwig len = be32_to_cpu(iclog->ic_header.h_num_logops);
36545809d5e0SChristoph Hellwig base_ptr = ptr = iclog->ic_datap;
36555809d5e0SChristoph Hellwig ophead = ptr;
3656b28708d6SChristoph Hellwig xhdr = iclog->ic_data;
36571da177e4SLinus Torvalds for (i = 0; i < len; i++) {
36585809d5e0SChristoph Hellwig ophead = ptr;
36591da177e4SLinus Torvalds
36601da177e4SLinus Torvalds /* clientid is only 1 byte */
36615809d5e0SChristoph Hellwig p = &ophead->oh_clientid;
36625809d5e0SChristoph Hellwig field_offset = p - base_ptr;
3663abca1f33SChristoph Hellwig if (field_offset & 0x1ff) {
36641da177e4SLinus Torvalds clientid = ophead->oh_clientid;
36651da177e4SLinus Torvalds } else {
3666decb545fSChristoph Hellwig idx = BTOBBT((void *)&ophead->oh_clientid - iclog->ic_datap);
36671da177e4SLinus Torvalds if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
36681da177e4SLinus Torvalds j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
36691da177e4SLinus Torvalds k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
367003bea6feSChristoph Hellwig clientid = xlog_get_client_id(
367103bea6feSChristoph Hellwig xhdr[j].hic_xheader.xh_cycle_data[k]);
36721da177e4SLinus Torvalds } else {
367303bea6feSChristoph Hellwig clientid = xlog_get_client_id(
367403bea6feSChristoph Hellwig iclog->ic_header.h_cycle_data[idx]);
36751da177e4SLinus Torvalds }
36761da177e4SLinus Torvalds }
3677ad3e3693SDave Chinner if (clientid != XFS_TRANSACTION && clientid != XFS_LOG) {
3678a0fa2b67SDave Chinner xfs_warn(log->l_mp,
3679ad3e3693SDave Chinner "%s: op %d invalid clientid %d op "PTR_FMT" offset 0x%lx",
3680ad3e3693SDave Chinner __func__, i, clientid, ophead,
3681a0fa2b67SDave Chinner (unsigned long)field_offset);
3682ad3e3693SDave Chinner }
36831da177e4SLinus Torvalds
36841da177e4SLinus Torvalds /* check length */
36855809d5e0SChristoph Hellwig p = &ophead->oh_len;
36865809d5e0SChristoph Hellwig field_offset = p - base_ptr;
3687abca1f33SChristoph Hellwig if (field_offset & 0x1ff) {
368867fcb7bfSChristoph Hellwig op_len = be32_to_cpu(ophead->oh_len);
36891da177e4SLinus Torvalds } else {
3690decb545fSChristoph Hellwig idx = BTOBBT((void *)&ophead->oh_len - iclog->ic_datap);
36911da177e4SLinus Torvalds if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
36921da177e4SLinus Torvalds j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
36931da177e4SLinus Torvalds k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3694b53e675dSChristoph Hellwig op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
36951da177e4SLinus Torvalds } else {
3696b53e675dSChristoph Hellwig op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
36971da177e4SLinus Torvalds }
36981da177e4SLinus Torvalds }
36991da177e4SLinus Torvalds ptr += sizeof(xlog_op_header_t) + op_len;
37001da177e4SLinus Torvalds }
3701b843299bSDave Chinner }
3702cfcbbbd0SNathan Scott #endif
37031da177e4SLinus Torvalds
37041da177e4SLinus Torvalds /*
3705b5f17becSDave Chinner * Perform a forced shutdown on the log.
3706b5f17becSDave Chinner *
3707b5f17becSDave Chinner * This can be called from low level log code to trigger a shutdown, or from the
3708b5f17becSDave Chinner * high level mount shutdown code when the mount shuts down.
3709b36d4651SDave Chinner *
37101da177e4SLinus Torvalds * Our main objectives here are to make sure that:
3711b36d4651SDave Chinner * a. if the shutdown was not due to a log IO error, flush the logs to
3712b36d4651SDave Chinner * disk. Anything modified after this is ignored.
3713b36d4651SDave Chinner * b. the log gets atomically marked 'XLOG_IO_ERROR' for all interested
3714b36d4651SDave Chinner * parties to find out. Nothing new gets queued after this is done.
3715b36d4651SDave Chinner * c. Tasks sleeping on log reservations, pinned objects and
3716b36d4651SDave Chinner * other resources get woken up.
3717b5f17becSDave Chinner * d. The mount is also marked as shut down so that log triggered shutdowns
3718b5f17becSDave Chinner * still behave the same as if they called xfs_forced_shutdown().
37199da1ab18SDave Chinner *
3720b36d4651SDave Chinner * Return true if the shutdown cause was a log IO error and we actually shut the
3721b36d4651SDave Chinner * log down.
37221da177e4SLinus Torvalds */
3723b36d4651SDave Chinner bool
xlog_force_shutdown(struct xlog * log,uint32_t shutdown_flags)3724b36d4651SDave Chinner xlog_force_shutdown(
3725b36d4651SDave Chinner struct xlog *log,
37262eb7550dSDave Chinner uint32_t shutdown_flags)
37271da177e4SLinus Torvalds {
3728b36d4651SDave Chinner bool log_error = (shutdown_flags & SHUTDOWN_LOG_IO_ERROR);
37291da177e4SLinus Torvalds
37305652ef31SDave Chinner if (!log)
3731b36d4651SDave Chinner return false;
37321da177e4SLinus Torvalds
37339da1ab18SDave Chinner /*
3734a870fe6dSDave Chinner * Flush all the completed transactions to disk before marking the log
3735b36d4651SDave Chinner * being shut down. We need to do this first as shutting down the log
3736b36d4651SDave Chinner * before the force will prevent the log force from flushing the iclogs
3737b36d4651SDave Chinner * to disk.
3738b36d4651SDave Chinner *
37395652ef31SDave Chinner * When we are in recovery, there are no transactions to flush, and
37405652ef31SDave Chinner * we don't want to touch the log because we don't want to perturb the
37415652ef31SDave Chinner * current head/tail for future recovery attempts. Hence we need to
37425652ef31SDave Chinner * avoid a log force in this case.
37435652ef31SDave Chinner *
37445652ef31SDave Chinner * If we are shutting down due to a log IO error, then we must avoid
37455652ef31SDave Chinner * trying to write the log as that may just result in more IO errors and
37465652ef31SDave Chinner * an endless shutdown/force loop.
37479da1ab18SDave Chinner */
37485652ef31SDave Chinner if (!log_error && !xlog_in_recovery(log))
3749b36d4651SDave Chinner xfs_log_force(log->l_mp, XFS_LOG_SYNC);
37509da1ab18SDave Chinner
37511da177e4SLinus Torvalds /*
3752b36d4651SDave Chinner * Atomically set the shutdown state. If the shutdown state is already
3753b36d4651SDave Chinner * set, there someone else is performing the shutdown and so we are done
3754b36d4651SDave Chinner * here. This should never happen because we should only ever get called
3755b36d4651SDave Chinner * once by the first shutdown caller.
3756b36d4651SDave Chinner *
3757b36d4651SDave Chinner * Much of the log state machine transitions assume that shutdown state
3758b36d4651SDave Chinner * cannot change once they hold the log->l_icloglock. Hence we need to
3759b36d4651SDave Chinner * hold that lock here, even though we use the atomic test_and_set_bit()
3760b36d4651SDave Chinner * operation to set the shutdown state.
37611da177e4SLinus Torvalds */
3762b22cd72cSEric Sandeen spin_lock(&log->l_icloglock);
3763b36d4651SDave Chinner if (test_and_set_bit(XLOG_IO_ERROR, &log->l_opstate)) {
3764b36d4651SDave Chinner spin_unlock(&log->l_icloglock);
3765b36d4651SDave Chinner return false;
3766b36d4651SDave Chinner }
3767b22cd72cSEric Sandeen spin_unlock(&log->l_icloglock);
37681da177e4SLinus Torvalds
37691da177e4SLinus Torvalds /*
3770b5f17becSDave Chinner * If this log shutdown also sets the mount shutdown state, issue a
3771b5f17becSDave Chinner * shutdown warning message.
3772b5f17becSDave Chinner */
3773b5f17becSDave Chinner if (!test_and_set_bit(XFS_OPSTATE_SHUTDOWN, &log->l_mp->m_opstate)) {
3774b5f17becSDave Chinner xfs_alert_tag(log->l_mp, XFS_PTAG_SHUTDOWN_LOGERROR,
3775b5f17becSDave Chinner "Filesystem has been shut down due to log error (0x%x).",
3776b5f17becSDave Chinner shutdown_flags);
3777b5f17becSDave Chinner xfs_alert(log->l_mp,
3778b5f17becSDave Chinner "Please unmount the filesystem and rectify the problem(s).");
3779b5f17becSDave Chinner if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
3780b5f17becSDave Chinner xfs_stack_trace();
3781b5f17becSDave Chinner }
3782b5f17becSDave Chinner
3783b5f17becSDave Chinner /*
378410547941SDave Chinner * We don't want anybody waiting for log reservations after this. That
378510547941SDave Chinner * means we have to wake up everybody queued up on reserveq as well as
378610547941SDave Chinner * writeq. In addition, we make sure in xlog_{re}grant_log_space that
378710547941SDave Chinner * we don't enqueue anything once the SHUTDOWN flag is set, and this
37883f16b985SDave Chinner * action is protected by the grant locks.
37891da177e4SLinus Torvalds */
3790a79bf2d7SChristoph Hellwig xlog_grant_head_wake_all(&log->l_reserve_head);
3791a79bf2d7SChristoph Hellwig xlog_grant_head_wake_all(&log->l_write_head);
37921da177e4SLinus Torvalds
37931da177e4SLinus Torvalds /*
3794ac983517SDave Chinner * Wake up everybody waiting on xfs_log_force. Wake the CIL push first
3795ac983517SDave Chinner * as if the log writes were completed. The abort handling in the log
3796ac983517SDave Chinner * item committed callback functions will do this again under lock to
3797ac983517SDave Chinner * avoid races.
37981da177e4SLinus Torvalds */
3799cdea5459SRik van Riel spin_lock(&log->l_cilp->xc_push_lock);
380068a74dcaSDave Chinner wake_up_all(&log->l_cilp->xc_start_wait);
3801ac983517SDave Chinner wake_up_all(&log->l_cilp->xc_commit_wait);
3802cdea5459SRik van Riel spin_unlock(&log->l_cilp->xc_push_lock);
38031da177e4SLinus Torvalds
3804cd6f79d1SDave Chinner spin_lock(&log->l_icloglock);
3805a45086e2SBrian Foster xlog_state_shutdown_callbacks(log);
3806cd6f79d1SDave Chinner spin_unlock(&log->l_icloglock);
38071da177e4SLinus Torvalds
380841e63621SDave Chinner wake_up_var(&log->l_opstate);
38091da177e4SLinus Torvalds return log_error;
38101da177e4SLinus Torvalds }
38111da177e4SLinus Torvalds
38121da177e4SLinus Torvalds STATIC int
xlog_iclogs_empty(struct xlog * log)38131da177e4SLinus Torvalds xlog_iclogs_empty(
38141da177e4SLinus Torvalds struct xlog *log)
38151da177e4SLinus Torvalds {
38161da177e4SLinus Torvalds xlog_in_core_t *iclog;
38171da177e4SLinus Torvalds
38181da177e4SLinus Torvalds iclog = log->l_iclog;
38191da177e4SLinus Torvalds do {
38201da177e4SLinus Torvalds /* endianness does not matter here, zero is zero in
38211da177e4SLinus Torvalds * any language.
38221da177e4SLinus Torvalds */
38231da177e4SLinus Torvalds if (iclog->ic_header.h_num_logops)
38241da177e4SLinus Torvalds return 0;
38251da177e4SLinus Torvalds iclog = iclog->ic_next;
38261da177e4SLinus Torvalds } while (iclog != log->l_iclog);
38271da177e4SLinus Torvalds return 1;
38281da177e4SLinus Torvalds }
38291da177e4SLinus Torvalds
38301da177e4SLinus Torvalds /*
38311da177e4SLinus Torvalds * Verify that an LSN stamped into a piece of metadata is valid. This is
38321da177e4SLinus Torvalds * intended for use in read verifiers on v5 superblocks.
38331da177e4SLinus Torvalds */
38341da177e4SLinus Torvalds bool
xfs_log_check_lsn(struct xfs_mount * mp,xfs_lsn_t lsn)38351da177e4SLinus Torvalds xfs_log_check_lsn(
38361da177e4SLinus Torvalds struct xfs_mount *mp,
38371da177e4SLinus Torvalds xfs_lsn_t lsn)
38381da177e4SLinus Torvalds {
38391da177e4SLinus Torvalds struct xlog *log = mp->m_log;
38401da177e4SLinus Torvalds bool valid;
38411da177e4SLinus Torvalds
38421da177e4SLinus Torvalds /*
38431da177e4SLinus Torvalds * norecovery mode skips mount-time log processing and unconditionally
38441da177e4SLinus Torvalds * resets the in-core LSN. We can't validate in this mode, but
38451da177e4SLinus Torvalds * modifications are not allowed anyways so just return true.
38461da177e4SLinus Torvalds */
38470560f31aSDave Chinner if (xfs_has_norecovery(mp))
3848a45086e2SBrian Foster return true;
3849a45086e2SBrian Foster
3850a45086e2SBrian Foster /*
3851a45086e2SBrian Foster * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is
3852a45086e2SBrian Foster * handled by recovery and thus safe to ignore here.
3853a45086e2SBrian Foster */
3854a45086e2SBrian Foster if (lsn == NULLCOMMITLSN)
3855a45086e2SBrian Foster return true;
3856a45086e2SBrian Foster
3857a45086e2SBrian Foster valid = xlog_valid_lsn(mp->m_log, lsn);
3858a45086e2SBrian Foster
3859a45086e2SBrian Foster /* warn the user about what's gone wrong before verifier failure */
3860a45086e2SBrian Foster if (!valid) {
3861a45086e2SBrian Foster spin_lock(&log->l_icloglock);
3862a45086e2SBrian Foster xfs_warn(mp,
3863a45086e2SBrian Foster "Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). "
3864a45086e2SBrian Foster "Please unmount and run xfs_repair (>= v4.3) to resolve.",
3865a45086e2SBrian Foster CYCLE_LSN(lsn), BLOCK_LSN(lsn),
3866a45086e2SBrian Foster log->l_curr_cycle, log->l_curr_block);
3867a45086e2SBrian Foster spin_unlock(&log->l_icloglock);
3868a45086e2SBrian Foster }
3869a45086e2SBrian Foster
3870a45086e2SBrian Foster return valid;
3871a45086e2SBrian Foster }
38720c60d3aaSDarrick J. Wong
38732b73a2c8SDarrick J. Wong /*
38742b73a2c8SDarrick J. Wong * Notify the log that we're about to start using a feature that is protected
38752b73a2c8SDarrick J. Wong * by a log incompat feature flag. This will prevent log covering from
38762b73a2c8SDarrick J. Wong * clearing those flags.
38772b73a2c8SDarrick J. Wong */
38782b73a2c8SDarrick J. Wong void
xlog_use_incompat_feat(struct xlog * log)38792b73a2c8SDarrick J. Wong xlog_use_incompat_feat(
38802b73a2c8SDarrick J. Wong struct xlog *log)
38812b73a2c8SDarrick J. Wong {
38822b73a2c8SDarrick J. Wong down_read(&log->l_incompat_users);
38832b73a2c8SDarrick J. Wong }
38842b73a2c8SDarrick J. Wong
38852b73a2c8SDarrick J. Wong /* Notify the log that we've finished using log incompat features. */
38862b73a2c8SDarrick J. Wong void
xlog_drop_incompat_feat(struct xlog * log)38872b73a2c8SDarrick J. Wong xlog_drop_incompat_feat(
38882b73a2c8SDarrick J. Wong struct xlog *log)
38892b73a2c8SDarrick J. Wong {
38902b73a2c8SDarrick J. Wong up_read(&log->l_incompat_users);
38912b73a2c8SDarrick J. Wong }
3892