blob: 8edfd35317d173a8e24a2b2a8d43ad1af620e7c4 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070013#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
17#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_log_priv.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000019#include "xfs_trace.h"
Brian Fosterbaff4e42014-07-15 08:07:29 +100020#include "xfs_sysfs.h"
Dave Chinner61e63ec2015-01-22 09:10:31 +110021#include "xfs_sb.h"
Darrick J. Wong39353ff2019-04-12 07:41:15 -070022#include "xfs_health.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
David Chinnereb01c9c2008-04-10 12:18:46 +100024kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Local miscellaneous function prototypes */
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050027STATIC struct xlog *
28xlog_alloc_log(
29 struct xfs_mount *mp,
30 struct xfs_buftarg *log_target,
31 xfs_daddr_t blk_offset,
32 int num_bblks);
Mark Tinguelyad223e62012-06-14 09:22:15 -050033STATIC int
34xlog_space_left(
35 struct xlog *log,
36 atomic64_t *head);
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050037STATIC void
38xlog_dealloc_log(
39 struct xlog *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* local state machine functions */
Christoph Hellwigd15cbf22019-06-28 19:27:30 -070042STATIC void xlog_state_done_syncing(
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -070043 struct xlog_in_core *iclog);
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050044STATIC int
45xlog_state_get_iclog_space(
46 struct xlog *log,
47 int len,
48 struct xlog_in_core **iclog,
49 struct xlog_ticket *ticket,
50 int *continued_write,
51 int *logoffsetp);
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050052STATIC void
53xlog_state_switch_iclogs(
54 struct xlog *log,
55 struct xlog_in_core *iclog,
56 int eventual_size);
57STATIC void
Mark Tinguelyad223e62012-06-14 09:22:15 -050058xlog_grant_push_ail(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050059 struct xlog *log,
60 int need_bytes);
61STATIC void
Christoph Hellwigdf732b22019-10-14 10:36:41 -070062xlog_sync(
63 struct xlog *log,
64 struct xlog_in_core *iclog);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +110065#if defined(DEBUG)
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050066STATIC void
67xlog_verify_dest_ptr(
68 struct xlog *log,
Christoph Hellwig5809d5e2015-06-22 09:44:47 +100069 void *ptr);
Mark Tinguelyad223e62012-06-14 09:22:15 -050070STATIC void
71xlog_verify_grant_tail(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050072 struct xlog *log);
73STATIC void
74xlog_verify_iclog(
75 struct xlog *log,
76 struct xlog_in_core *iclog,
Christoph Hellwigabca1f32019-06-28 19:27:24 -070077 int count);
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050078STATIC void
79xlog_verify_tail_lsn(
80 struct xlog *log,
Dave Chinner9d110012021-07-28 17:14:11 -070081 struct xlog_in_core *iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#else
83#define xlog_verify_dest_ptr(a,b)
Dave Chinner3f336c62010-12-21 12:02:52 +110084#define xlog_verify_grant_tail(a)
Christoph Hellwigabca1f32019-06-28 19:27:24 -070085#define xlog_verify_iclog(a,b,c)
Dave Chinner9d110012021-07-28 17:14:11 -070086#define xlog_verify_tail_lsn(a,b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#endif
88
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050089STATIC int
90xlog_iclogs_empty(
91 struct xlog *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Brian Foster303591a2021-01-22 16:48:22 -080093static int
94xfs_log_cover(struct xfs_mount *);
95
Christoph Hellwigdd954c62006-01-11 15:34:50 +110096static void
Dave Chinner663e4962010-12-21 12:06:05 +110097xlog_grant_sub_space(
Mark Tinguelyad223e62012-06-14 09:22:15 -050098 struct xlog *log,
99 atomic64_t *head,
100 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100101{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100102 int64_t head_val = atomic64_read(head);
103 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100104
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100105 do {
106 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100107
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100108 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100109
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100110 space -= bytes;
111 if (space < 0) {
112 space += log->l_logsize;
113 cycle--;
114 }
115
116 old = head_val;
117 new = xlog_assign_grant_head_val(cycle, space);
118 head_val = atomic64_cmpxchg(head, old, new);
119 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100120}
121
122static void
Dave Chinner663e4962010-12-21 12:06:05 +1100123xlog_grant_add_space(
Mark Tinguelyad223e62012-06-14 09:22:15 -0500124 struct xlog *log,
125 atomic64_t *head,
126 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100127{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100128 int64_t head_val = atomic64_read(head);
129 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100130
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100131 do {
132 int tmp;
133 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100134
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100135 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100136
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100137 tmp = log->l_logsize - space;
138 if (tmp > bytes)
139 space += bytes;
140 else {
141 space = bytes - tmp;
142 cycle++;
143 }
144
145 old = head_val;
146 new = xlog_assign_grant_head_val(cycle, space);
147 head_val = atomic64_cmpxchg(head, old, new);
148 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100149}
Dave Chinnera69ed032010-12-21 12:08:20 +1100150
Christoph Hellwigc303c5b2012-02-20 02:31:26 +0000151STATIC void
152xlog_grant_head_init(
153 struct xlog_grant_head *head)
154{
155 xlog_assign_grant_head(&head->grant, 1, 0);
156 INIT_LIST_HEAD(&head->waiters);
157 spin_lock_init(&head->lock);
158}
159
Christoph Hellwiga79bf2d2012-02-20 02:31:27 +0000160STATIC void
161xlog_grant_head_wake_all(
162 struct xlog_grant_head *head)
163{
164 struct xlog_ticket *tic;
165
166 spin_lock(&head->lock);
167 list_for_each_entry(tic, &head->waiters, t_queue)
168 wake_up_process(tic->t_task);
169 spin_unlock(&head->lock);
170}
171
Christoph Hellwige179840d2012-02-20 02:31:29 +0000172static inline int
173xlog_ticket_reservation(
Mark Tinguelyad223e62012-06-14 09:22:15 -0500174 struct xlog *log,
Christoph Hellwige179840d2012-02-20 02:31:29 +0000175 struct xlog_grant_head *head,
176 struct xlog_ticket *tic)
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000177{
Christoph Hellwige179840d2012-02-20 02:31:29 +0000178 if (head == &log->l_write_head) {
179 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
180 return tic->t_unit_res;
181 } else {
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000182 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
Christoph Hellwige179840d2012-02-20 02:31:29 +0000183 return tic->t_unit_res * tic->t_cnt;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000184 else
Christoph Hellwige179840d2012-02-20 02:31:29 +0000185 return tic->t_unit_res;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000186 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000187}
188
189STATIC bool
Christoph Hellwige179840d2012-02-20 02:31:29 +0000190xlog_grant_head_wake(
Mark Tinguelyad223e62012-06-14 09:22:15 -0500191 struct xlog *log,
Christoph Hellwige179840d2012-02-20 02:31:29 +0000192 struct xlog_grant_head *head,
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000193 int *free_bytes)
194{
195 struct xlog_ticket *tic;
196 int need_bytes;
Dave Chinner7c107af2019-09-05 17:32:48 -0700197 bool woken_task = false;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000198
Christoph Hellwige179840d2012-02-20 02:31:29 +0000199 list_for_each_entry(tic, &head->waiters, t_queue) {
Dave Chinner7c107af2019-09-05 17:32:48 -0700200
201 /*
202 * There is a chance that the size of the CIL checkpoints in
203 * progress at the last AIL push target calculation resulted in
204 * limiting the target to the log head (l_last_sync_lsn) at the
205 * time. This may not reflect where the log head is now as the
206 * CIL checkpoints may have completed.
207 *
208 * Hence when we are woken here, it may be that the head of the
209 * log that has moved rather than the tail. As the tail didn't
210 * move, there still won't be space available for the
211 * reservation we require. However, if the AIL has already
212 * pushed to the target defined by the old log head location, we
213 * will hang here waiting for something else to update the AIL
214 * push target.
215 *
216 * Therefore, if there isn't space to wake the first waiter on
217 * the grant head, we need to push the AIL again to ensure the
218 * target reflects both the current log tail and log head
219 * position before we wait for the tail to move again.
220 */
221
Christoph Hellwige179840d2012-02-20 02:31:29 +0000222 need_bytes = xlog_ticket_reservation(log, head, tic);
Dave Chinner7c107af2019-09-05 17:32:48 -0700223 if (*free_bytes < need_bytes) {
224 if (!woken_task)
225 xlog_grant_push_ail(log, need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000226 return false;
Dave Chinner7c107af2019-09-05 17:32:48 -0700227 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000228
Christoph Hellwige179840d2012-02-20 02:31:29 +0000229 *free_bytes -= need_bytes;
230 trace_xfs_log_grant_wake_up(log, tic);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000231 wake_up_process(tic->t_task);
Dave Chinner7c107af2019-09-05 17:32:48 -0700232 woken_task = true;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000233 }
234
235 return true;
236}
237
238STATIC int
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000239xlog_grant_head_wait(
Mark Tinguelyad223e62012-06-14 09:22:15 -0500240 struct xlog *log,
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000241 struct xlog_grant_head *head,
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000242 struct xlog_ticket *tic,
Dave Chinnera30b0362013-09-02 20:49:36 +1000243 int need_bytes) __releases(&head->lock)
244 __acquires(&head->lock)
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000245{
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000246 list_add_tail(&tic->t_queue, &head->waiters);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000247
248 do {
Dave Chinner2039a272021-08-10 17:59:01 -0700249 if (xlog_is_shutdown(log))
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000250 goto shutdown;
251 xlog_grant_push_ail(log, need_bytes);
252
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000253 __set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000254 spin_unlock(&head->lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000255
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100256 XFS_STATS_INC(log->l_mp, xs_sleep_logspace);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000257
258 trace_xfs_log_grant_sleep(log, tic);
259 schedule();
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000260 trace_xfs_log_grant_wake(log, tic);
261
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000262 spin_lock(&head->lock);
Dave Chinner2039a272021-08-10 17:59:01 -0700263 if (xlog_is_shutdown(log))
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000264 goto shutdown;
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000265 } while (xlog_space_left(log, &head->grant) < need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000266
267 list_del_init(&tic->t_queue);
268 return 0;
269shutdown:
270 list_del_init(&tic->t_queue);
Dave Chinner24513372014-06-25 14:58:08 +1000271 return -EIO;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000272}
273
Christoph Hellwig42ceedb2012-02-20 02:31:30 +0000274/*
275 * Atomically get the log space required for a log ticket.
276 *
277 * Once a ticket gets put onto head->waiters, it will only return after the
278 * needed reservation is satisfied.
279 *
280 * This function is structured so that it has a lock free fast path. This is
281 * necessary because every new transaction reservation will come through this
282 * path. Hence any lock will be globally hot if we take it unconditionally on
283 * every pass.
284 *
285 * As tickets are only ever moved on and off head->waiters under head->lock, we
286 * only need to take that lock if we are going to add the ticket to the queue
287 * and sleep. We can avoid taking the lock if the ticket was never added to
288 * head->waiters because the t_queue list head will be empty and we hold the
289 * only reference to it so it can safely be checked unlocked.
290 */
291STATIC int
292xlog_grant_head_check(
Mark Tinguelyad223e62012-06-14 09:22:15 -0500293 struct xlog *log,
Christoph Hellwig42ceedb2012-02-20 02:31:30 +0000294 struct xlog_grant_head *head,
295 struct xlog_ticket *tic,
296 int *need_bytes)
297{
298 int free_bytes;
299 int error = 0;
300
301 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
302
303 /*
304 * If there are other waiters on the queue then give them a chance at
305 * logspace before us. Wake up the first waiters, if we do not wake
306 * up all the waiters then go to sleep waiting for more free space,
307 * otherwise try to get some space for this transaction.
308 */
309 *need_bytes = xlog_ticket_reservation(log, head, tic);
310 free_bytes = xlog_space_left(log, &head->grant);
311 if (!list_empty_careful(&head->waiters)) {
312 spin_lock(&head->lock);
313 if (!xlog_grant_head_wake(log, head, &free_bytes) ||
314 free_bytes < *need_bytes) {
315 error = xlog_grant_head_wait(log, head, tic,
316 *need_bytes);
317 }
318 spin_unlock(&head->lock);
319 } else if (free_bytes < *need_bytes) {
320 spin_lock(&head->lock);
321 error = xlog_grant_head_wait(log, head, tic, *need_bytes);
322 spin_unlock(&head->lock);
323 }
324
325 return error;
326}
327
Christoph Hellwig0adba532007-08-30 17:21:46 +1000328static void
329xlog_tic_reset_res(xlog_ticket_t *tic)
330{
331 tic->t_res_num = 0;
332 tic->t_res_arr_sum = 0;
333 tic->t_res_num_ophdrs = 0;
334}
335
336static void
337xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
338{
339 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
340 /* add to overflow and start again */
341 tic->t_res_o_flow += tic->t_res_arr_sum;
342 tic->t_res_num = 0;
343 tic->t_res_arr_sum = 0;
344 }
345
346 tic->t_res_arr[tic->t_res_num].r_len = len;
347 tic->t_res_arr[tic->t_res_num].r_type = type;
348 tic->t_res_arr_sum += len;
349 tic->t_res_num++;
350}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100351
Brian Foster50d25482021-01-22 16:48:20 -0800352bool
353xfs_log_writable(
354 struct xfs_mount *mp)
355{
356 /*
Darrick J. Wong8e9800f2021-04-29 14:39:33 -0700357 * Do not write to the log on norecovery mounts, if the data or log
358 * devices are read-only, or if the filesystem is shutdown. Read-only
359 * mounts allow internal writes for log recovery and unmount purposes,
360 * so don't restrict that case.
Brian Foster50d25482021-01-22 16:48:20 -0800361 */
362 if (mp->m_flags & XFS_MOUNT_NORECOVERY)
363 return false;
Darrick J. Wong8e9800f2021-04-29 14:39:33 -0700364 if (xfs_readonly_buftarg(mp->m_ddev_targp))
365 return false;
Brian Foster50d25482021-01-22 16:48:20 -0800366 if (xfs_readonly_buftarg(mp->m_log->l_targ))
367 return false;
Dave Chinner2039a272021-08-10 17:59:01 -0700368 if (xlog_is_shutdown(mp->m_log))
Brian Foster50d25482021-01-22 16:48:20 -0800369 return false;
370 return true;
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000374 * Replenish the byte reservation required by moving the grant write head.
375 */
376int
377xfs_log_regrant(
378 struct xfs_mount *mp,
379 struct xlog_ticket *tic)
380{
Mark Tinguelyad223e62012-06-14 09:22:15 -0500381 struct xlog *log = mp->m_log;
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000382 int need_bytes;
383 int error = 0;
384
Dave Chinner2039a272021-08-10 17:59:01 -0700385 if (xlog_is_shutdown(log))
Dave Chinner24513372014-06-25 14:58:08 +1000386 return -EIO;
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000387
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100388 XFS_STATS_INC(mp, xs_try_logspace);
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000389
390 /*
391 * This is a new transaction on the ticket, so we need to change the
392 * transaction ID so that the next transaction has a different TID in
393 * the log. Just add one to the existing tid so that we can see chains
394 * of rolling transactions in the log easily.
395 */
396 tic->t_tid++;
397
398 xlog_grant_push_ail(log, tic->t_unit_res);
399
400 tic->t_curr_res = tic->t_unit_res;
401 xlog_tic_reset_res(tic);
402
403 if (tic->t_cnt > 0)
404 return 0;
405
406 trace_xfs_log_regrant(log, tic);
407
408 error = xlog_grant_head_check(log, &log->l_write_head, tic,
409 &need_bytes);
410 if (error)
411 goto out_error;
412
413 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
414 trace_xfs_log_regrant_exit(log, tic);
415 xlog_verify_grant_tail(log);
416 return 0;
417
418out_error:
419 /*
420 * If we are failing, make sure the ticket doesn't have any current
421 * reservations. We don't want to add this back when the ticket/
422 * transaction gets cancelled.
423 */
424 tic->t_curr_res = 0;
425 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
426 return error;
427}
428
429/*
Huang Chonga0e336ba2018-08-03 08:17:54 -0700430 * Reserve log space and return a ticket corresponding to the reservation.
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000431 *
432 * Each reservation is going to reserve extra space for a log record header.
433 * When writes happen to the on-disk log, we don't subtract the length of the
434 * log record header from any reservation. By wasting space in each
435 * reservation, we prevent over allocation problems.
436 */
437int
438xfs_log_reserve(
439 struct xfs_mount *mp,
440 int unit_bytes,
441 int cnt,
442 struct xlog_ticket **ticp,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700443 uint8_t client,
Christoph Hellwig710b1e22016-04-06 09:20:36 +1000444 bool permanent)
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000445{
Mark Tinguelyad223e62012-06-14 09:22:15 -0500446 struct xlog *log = mp->m_log;
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000447 struct xlog_ticket *tic;
448 int need_bytes;
449 int error = 0;
450
451 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
452
Dave Chinner2039a272021-08-10 17:59:01 -0700453 if (xlog_is_shutdown(log))
Dave Chinner24513372014-06-25 14:58:08 +1000454 return -EIO;
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000455
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100456 XFS_STATS_INC(mp, xs_try_logspace);
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000457
458 ASSERT(*ticp == NULL);
Carlos Maiolinoca4f2582020-07-22 09:23:17 -0700459 tic = xlog_ticket_alloc(log, unit_bytes, cnt, client, permanent);
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000460 *ticp = tic;
461
Dave Chinner437a2552012-11-28 13:01:00 +1100462 xlog_grant_push_ail(log, tic->t_cnt ? tic->t_unit_res * tic->t_cnt
463 : tic->t_unit_res);
Christoph Hellwig9006fb92012-02-20 02:31:31 +0000464
465 trace_xfs_log_reserve(log, tic);
466
467 error = xlog_grant_head_check(log, &log->l_reserve_head, tic,
468 &need_bytes);
469 if (error)
470 goto out_error;
471
472 xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
473 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
474 trace_xfs_log_reserve_exit(log, tic);
475 xlog_verify_grant_tail(log);
476 return 0;
477
478out_error:
479 /*
480 * If we are failing, make sure the ticket doesn't have any current
481 * reservations. We don't want to add this back when the ticket/
482 * transaction gets cancelled.
483 */
484 tic->t_curr_res = 0;
485 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
486 return error;
487}
488
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700489/*
490 * Flush iclog to disk if this is the last reference to the given iclog and the
Dave Chinner9d110012021-07-28 17:14:11 -0700491 * it is in the WANT_SYNC state.
492 *
493 * If the caller passes in a non-zero @old_tail_lsn and the current log tail
494 * does not match, there may be metadata on disk that must be persisted before
495 * this iclog is written. To satisfy that requirement, set the
496 * XLOG_ICL_NEED_FLUSH flag as a condition for writing this iclog with the new
497 * log tail value.
498 *
499 * If XLOG_ICL_NEED_FUA is already set on the iclog, we need to ensure that the
500 * log tail is updated correctly. NEED_FUA indicates that the iclog will be
501 * written to stable storage, and implies that a commit record is contained
502 * within the iclog. We need to ensure that the log tail does not move beyond
503 * the tail that the first commit record in the iclog ordered against, otherwise
504 * correct recovery of that checkpoint becomes dependent on future operations
505 * performed on this iclog.
506 *
507 * Hence if NEED_FUA is set and the current iclog tail lsn is empty, write the
508 * current tail into iclog. Once the iclog tail is set, future operations must
509 * not modify it, otherwise they potentially violate ordering constraints for
510 * the checkpoint commit that wrote the initial tail lsn value. The tail lsn in
511 * the iclog will get zeroed on activation of the iclog after sync, so we
512 * always capture the tail lsn on the iclog on the first NEED_FUA release
513 * regardless of the number of active reference counts on this iclog.
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700514 */
Dave Chinner9d110012021-07-28 17:14:11 -0700515
Dave Chinnereef983f2021-06-18 08:21:51 -0700516int
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700517xlog_state_release_iclog(
518 struct xlog *log,
Dave Chinner0dc8f7f2021-07-27 16:23:48 -0700519 struct xlog_in_core *iclog,
520 xfs_lsn_t old_tail_lsn)
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700521{
Dave Chinner9d392062021-07-27 16:23:47 -0700522 xfs_lsn_t tail_lsn;
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700523 lockdep_assert_held(&log->l_icloglock);
524
Dave Chinner956f6da2021-06-18 11:57:05 -0700525 trace_xlog_iclog_release(iclog, _RET_IP_);
Dave Chinner5112e2062021-08-10 17:59:01 -0700526 if (xlog_is_shutdown(log))
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700527 return -EIO;
528
Dave Chinner0dc8f7f2021-07-27 16:23:48 -0700529 /*
530 * Grabbing the current log tail needs to be atomic w.r.t. the writing
531 * of the tail LSN into the iclog so we guarantee that the log tail does
532 * not move between deciding if a cache flush is required and writing
533 * the LSN into the iclog below.
534 */
535 if (old_tail_lsn || iclog->ic_state == XLOG_STATE_WANT_SYNC) {
536 tail_lsn = xlog_assign_tail_lsn(log->l_mp);
537
538 if (old_tail_lsn && tail_lsn != old_tail_lsn)
539 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
Dave Chinner9d110012021-07-28 17:14:11 -0700540
541 if ((iclog->ic_flags & XLOG_ICL_NEED_FUA) &&
542 !iclog->ic_header.h_tail_lsn)
543 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
Dave Chinner0dc8f7f2021-07-27 16:23:48 -0700544 }
545
Dave Chinner9d392062021-07-27 16:23:47 -0700546 if (!atomic_dec_and_test(&iclog->ic_refcnt))
547 return 0;
548
549 if (iclog->ic_state != XLOG_STATE_WANT_SYNC) {
550 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
551 return 0;
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700552 }
553
Dave Chinner9d392062021-07-27 16:23:47 -0700554 iclog->ic_state = XLOG_STATE_SYNCING;
Dave Chinner9d110012021-07-28 17:14:11 -0700555 if (!iclog->ic_header.h_tail_lsn)
556 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
557 xlog_verify_tail_lsn(log, iclog);
Dave Chinner9d392062021-07-27 16:23:47 -0700558 trace_xlog_iclog_syncing(iclog, _RET_IP_);
559
560 spin_unlock(&log->l_icloglock);
561 xlog_sync(log, iclog);
562 spin_lock(&log->l_icloglock);
Christoph Hellwigdf732b22019-10-14 10:36:41 -0700563 return 0;
564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * Mount a log filesystem
568 *
569 * mp - ubiquitous xfs mount point structure
570 * log_target - buftarg of on-disk log device
571 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
572 * num_bblocks - Number of BBSIZE blocks in on-disk log
573 *
574 * Return error or zero.
575 */
576int
David Chinner249a8c12008-02-05 12:13:32 +1100577xfs_log_mount(
578 xfs_mount_t *mp,
579 xfs_buftarg_t *log_target,
580 xfs_daddr_t blk_offset,
581 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Darrick J. Wong9c92ee22017-10-25 16:59:43 -0700583 bool fatal = xfs_sb_version_hascrc(&mp->m_sb);
Jie Liu3e7b91c2013-08-12 20:50:03 +1000584 int error = 0;
585 int min_logfsbs;
David Chinner249a8c12008-02-05 12:13:32 +1100586
Dave Chinnerc99d6092014-05-05 16:18:37 +1000587 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
588 xfs_notice(mp, "Mounting V%d Filesystem",
589 XFS_SB_VERSION_NUM(&mp->m_sb));
590 } else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100591 xfs_notice(mp,
Dave Chinnerc99d6092014-05-05 16:18:37 +1000592"Mounting V%d filesystem in no-recovery mode. Filesystem will be inconsistent.",
593 XFS_SB_VERSION_NUM(&mp->m_sb));
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000594 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200598 if (IS_ERR(mp->m_log)) {
Dave Chinner24513372014-06-25 14:58:08 +1000599 error = PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100600 goto out;
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
Jie Liu3e7b91c2013-08-12 20:50:03 +1000604 * Validate the given log space and drop a critical message via syslog
605 * if the log size is too small that would lead to some unexpected
606 * situations in transaction log space reservation stage.
607 *
608 * Note: we can't just reject the mount if the validation fails. This
609 * would mean that people would have to downgrade their kernel just to
610 * remedy the situation as there is no way to grow the log (short of
611 * black magic surgery with xfs_db).
612 *
613 * We can, however, reject mounts for CRC format filesystems, as the
614 * mkfs binary being used to make the filesystem should never create a
615 * filesystem with a log that is too small.
616 */
617 min_logfsbs = xfs_log_calc_minimum_size(mp);
618
619 if (mp->m_sb.sb_logblocks < min_logfsbs) {
620 xfs_warn(mp,
621 "Log size %d blocks too small, minimum size is %d blocks",
622 mp->m_sb.sb_logblocks, min_logfsbs);
Dave Chinner24513372014-06-25 14:58:08 +1000623 error = -EINVAL;
Jie Liu3e7b91c2013-08-12 20:50:03 +1000624 } else if (mp->m_sb.sb_logblocks > XFS_MAX_LOG_BLOCKS) {
625 xfs_warn(mp,
626 "Log size %d blocks too large, maximum size is %lld blocks",
627 mp->m_sb.sb_logblocks, XFS_MAX_LOG_BLOCKS);
Dave Chinner24513372014-06-25 14:58:08 +1000628 error = -EINVAL;
Jie Liu3e7b91c2013-08-12 20:50:03 +1000629 } else if (XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks) > XFS_MAX_LOG_BYTES) {
630 xfs_warn(mp,
631 "log size %lld bytes too large, maximum size is %lld bytes",
632 XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks),
633 XFS_MAX_LOG_BYTES);
Dave Chinner24513372014-06-25 14:58:08 +1000634 error = -EINVAL;
Darrick J. Wong9c92ee22017-10-25 16:59:43 -0700635 } else if (mp->m_sb.sb_logsunit > 1 &&
636 mp->m_sb.sb_logsunit % mp->m_sb.sb_blocksize) {
637 xfs_warn(mp,
638 "log stripe unit %u bytes must be a multiple of block size",
639 mp->m_sb.sb_logsunit);
640 error = -EINVAL;
641 fatal = true;
Jie Liu3e7b91c2013-08-12 20:50:03 +1000642 }
643 if (error) {
Darrick J. Wong9c92ee22017-10-25 16:59:43 -0700644 /*
645 * Log check errors are always fatal on v5; or whenever bad
646 * metadata leads to a crash.
647 */
648 if (fatal) {
Jie Liu3e7b91c2013-08-12 20:50:03 +1000649 xfs_crit(mp, "AAIEEE! Log failed size checks. Abort!");
650 ASSERT(0);
651 goto out_free_log;
652 }
Joe Perchesf41febd2015-07-29 11:52:04 +1000653 xfs_crit(mp, "Log size out of supported range.");
Jie Liu3e7b91c2013-08-12 20:50:03 +1000654 xfs_crit(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +1000655"Continuing onwards, but if log hangs are experienced then please report this message in the bug report.");
Jie Liu3e7b91c2013-08-12 20:50:03 +1000656 }
657
658 /*
David Chinner249a8c12008-02-05 12:13:32 +1100659 * Initialize the AIL now we have a log.
660 */
David Chinner249a8c12008-02-05 12:13:32 +1100661 error = xfs_trans_ail_init(mp);
662 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100663 xfs_warn(mp, "AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100664 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100665 }
David Chinnera9c21c12008-10-30 17:39:35 +1100666 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100667
668 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * skip log recovery on a norecovery mount. pretend it all
670 * just worked.
671 */
672 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100673 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000676 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Eric Sandeen65be6052006-01-11 15:34:19 +1100678 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000681 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100683 xfs_warn(mp, "log mount/recovery failed: error %d",
684 error);
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000685 xlog_recover_cancel(mp->m_log);
Christoph Hellwig26430752009-02-12 19:55:48 +0100686 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 }
689
Brian Fosterbaff4e42014-07-15 08:07:29 +1000690 error = xfs_sysfs_init(&mp->m_log->l_kobj, &xfs_log_ktype, &mp->m_kobj,
691 "log");
692 if (error)
693 goto out_destroy_ail;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Normal transactions can now occur */
696 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
697
Dave Chinner71e330b2010-05-21 14:37:18 +1000698 /*
699 * Now the log has been fully initialised and we know were our
700 * space grant counters are, we can initialise the permanent ticket
701 * needed for delayed logging to work.
702 */
703 xlog_cil_init_post_recovery(mp->m_log);
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100706
707out_destroy_ail:
708 xfs_trans_ail_destroy(mp);
709out_free_log:
710 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100711out:
David Chinner249a8c12008-02-05 12:13:32 +1100712 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715/*
Dave Chinnerf661f1e2012-10-08 21:56:02 +1100716 * Finish the recovery of the file system. This is separate from the
717 * xfs_log_mount() call, because it depends on the code in xfs_mountfs() to read
718 * in the root and real-time bitmap inodes between calling xfs_log_mount() and
719 * here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 *
Dave Chinnerf661f1e2012-10-08 21:56:02 +1100721 * If we finish recovery successfully, start the background log work. If we are
722 * not doing recovery, then we have a RO filesystem and we don't need to start
723 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
725int
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000726xfs_log_mount_finish(
727 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Dave Chinnerfd67d8a2021-08-10 17:59:02 -0700729 struct xlog *log = mp->m_log;
730 bool readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
731 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000733 if (mp->m_flags & XFS_MOUNT_NORECOVERY) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000734 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000735 return 0;
Eric Sandeen6f4a1ee2017-08-08 18:21:49 -0700736 } else if (readonly) {
737 /* Allow unlinked processing to proceed */
738 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Darrick J. Wong8204f8d2017-08-10 14:20:28 -0700741 /*
742 * During the second phase of log recovery, we need iget and
743 * iput to behave like they do for an active filesystem.
744 * xfs_fs_drop_inode needs to be able to prevent the deletion
745 * of inodes before we're done replaying log items on those
746 * inodes. Turn it off immediately after recovery finishes
747 * so that we don't leak the quota inodes if subsequent mount
748 * activities fail.
Darrick J. Wong799ea9e2017-08-18 18:08:25 -0700749 *
750 * We let all inodes involved in redo item processing end up on
751 * the LRU instead of being evicted immediately so that if we do
752 * something to an unlinked inode, the irele won't cause
753 * premature truncation and freeing of the inode, which results
754 * in log recovery failure. We have to evict the unreferenced
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800755 * lru inodes after clearing SB_ACTIVE because we don't
Darrick J. Wong799ea9e2017-08-18 18:08:25 -0700756 * otherwise clean up the lru if there's a subsequent failure in
757 * xfs_mountfs, which leads to us leaking the inodes if nothing
758 * else (e.g. quotacheck) references the inodes before the
759 * mount failure occurs.
Darrick J. Wong8204f8d2017-08-10 14:20:28 -0700760 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800761 mp->m_super->s_flags |= SB_ACTIVE;
Dave Chinnerfd67d8a2021-08-10 17:59:02 -0700762 if (log->l_flags & XLOG_RECOVERY_NEEDED)
763 error = xlog_recover_finish(log);
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000764 if (!error)
765 xfs_log_work_queue(mp);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800766 mp->m_super->s_flags &= ~SB_ACTIVE;
Darrick J. Wong799ea9e2017-08-18 18:08:25 -0700767 evict_inodes(mp->m_super);
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000768
Brian Fosterf1b92bbc2017-10-26 09:31:16 -0700769 /*
770 * Drain the buffer LRU after log recovery. This is required for v4
771 * filesystems to avoid leaving around buffers with NULL verifier ops,
772 * but we do it unconditionally to make sure we're always in a clean
773 * cache state after mount.
774 *
775 * Don't push in the error case because the AIL may have pending intents
776 * that aren't removed until recovery is cancelled.
777 */
Dave Chinnerfd67d8a2021-08-10 17:59:02 -0700778 if (log->l_flags & XLOG_RECOVERY_NEEDED) {
779 if (!error) {
780 xfs_log_force(mp, XFS_LOG_SYNC);
781 xfs_ail_push_all_sync(mp->m_ail);
782 }
783 xfs_notice(mp, "Ending recovery (logdev: %s)",
784 mp->m_logname ? mp->m_logname : "internal");
785 } else {
786 xfs_info(mp, "Ending clean mount");
Brian Fosterf1b92bbc2017-10-26 09:31:16 -0700787 }
Brian Foster10fb9ac2021-01-22 16:48:19 -0800788 xfs_buftarg_drain(mp->m_ddev_targp);
Brian Fosterf1b92bbc2017-10-26 09:31:16 -0700789
Dave Chinnerfd67d8a2021-08-10 17:59:02 -0700790 log->l_flags &= ~XLOG_RECOVERY_NEEDED;
Eric Sandeen6f4a1ee2017-08-08 18:21:49 -0700791 if (readonly)
792 mp->m_flags |= XFS_MOUNT_RDONLY;
793
Darrick J. Wong4e6b8272021-06-18 11:57:07 -0700794 /* Make sure the log is dead if we're returning failure. */
Dave Chinnerfd67d8a2021-08-10 17:59:02 -0700795 ASSERT(!error || xlog_is_shutdown(log));
Darrick J. Wong4e6b8272021-06-18 11:57:07 -0700796
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000797 return error;
798}
799
800/*
801 * The mount has failed. Cancel the recovery if it hasn't completed and destroy
802 * the log.
803 */
Hariprasad Kelama7a92502019-07-03 07:34:18 -0700804void
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000805xfs_log_mount_cancel(
806 struct xfs_mount *mp)
807{
Hariprasad Kelama7a92502019-07-03 07:34:18 -0700808 xlog_recover_cancel(mp->m_log);
Brian Fosterf0b2efa2015-08-19 09:58:36 +1000809 xfs_log_unmount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/*
Dave Chinner45eddb42021-07-27 16:23:48 -0700813 * Flush out the iclog to disk ensuring that device caches are flushed and
814 * the iclog hits stable storage before any completion waiters are woken.
815 */
816static inline int
817xlog_force_iclog(
818 struct xlog_in_core *iclog)
819{
820 atomic_inc(&iclog->ic_refcnt);
Dave Chinner2bf1ec02021-07-27 16:23:49 -0700821 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
Dave Chinner45eddb42021-07-27 16:23:48 -0700822 if (iclog->ic_state == XLOG_STATE_ACTIVE)
823 xlog_state_switch_iclogs(iclog->ic_log, iclog, 0);
824 return xlog_state_release_iclog(iclog->ic_log, iclog, 0);
825}
826
827/*
Dave Chinnera79b28c2021-06-18 08:21:48 -0700828 * Wait for the iclog and all prior iclogs to be written disk as required by the
829 * log force state machine. Waiting on ic_force_wait ensures iclog completions
830 * have been ordered and callbacks run before we are woken here, hence
831 * guaranteeing that all the iclogs up to this one are on stable storage.
Christoph Hellwig81e5b502020-03-20 08:49:18 -0700832 */
Dave Chinnera79b28c2021-06-18 08:21:48 -0700833int
Christoph Hellwig81e5b502020-03-20 08:49:18 -0700834xlog_wait_on_iclog(
835 struct xlog_in_core *iclog)
836 __releases(iclog->ic_log->l_icloglock)
837{
838 struct xlog *log = iclog->ic_log;
839
Dave Chinner956f6da2021-06-18 11:57:05 -0700840 trace_xlog_iclog_wait_on(iclog, _RET_IP_);
Dave Chinner2039a272021-08-10 17:59:01 -0700841 if (!xlog_is_shutdown(log) &&
Christoph Hellwig81e5b502020-03-20 08:49:18 -0700842 iclog->ic_state != XLOG_STATE_ACTIVE &&
843 iclog->ic_state != XLOG_STATE_DIRTY) {
844 XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
845 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
846 } else {
847 spin_unlock(&log->l_icloglock);
848 }
849
Dave Chinner2039a272021-08-10 17:59:01 -0700850 if (xlog_is_shutdown(log))
Christoph Hellwig81e5b502020-03-20 08:49:18 -0700851 return -EIO;
852 return 0;
853}
854
855/*
Dave Chinner3c702f92020-03-25 18:18:24 -0700856 * Write out an unmount record using the ticket provided. We have to account for
857 * the data space used in the unmount ticket as this write is not done from a
858 * transaction context that has already done the accounting for us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Dave Chinner3c702f92020-03-25 18:18:24 -0700860static int
861xlog_write_unmount_record(
862 struct xlog *log,
Dave Chinner3468bb12021-06-18 08:21:50 -0700863 struct xlog_ticket *ticket)
Darrick J. Wong53235f22018-07-20 09:28:39 -0700864{
Dave Chinner3c702f92020-03-25 18:18:24 -0700865 struct xfs_unmount_log_format ulf = {
Darrick J. Wong53235f22018-07-20 09:28:39 -0700866 .magic = XLOG_UNMOUNT_TYPE,
867 };
868 struct xfs_log_iovec reg = {
Dave Chinner3c702f92020-03-25 18:18:24 -0700869 .i_addr = &ulf,
870 .i_len = sizeof(ulf),
Darrick J. Wong53235f22018-07-20 09:28:39 -0700871 .i_type = XLOG_REG_TYPE_UNMOUNT,
872 };
873 struct xfs_log_vec vec = {
874 .lv_niovecs = 1,
875 .lv_iovecp = &reg,
876 };
Dave Chinner3c702f92020-03-25 18:18:24 -0700877
878 /* account for space used by record data */
879 ticket->t_curr_res -= sizeof(ulf);
Dave Chinnereef983f2021-06-18 08:21:51 -0700880
Dave Chinner3468bb12021-06-18 08:21:50 -0700881 return xlog_write(log, &vec, ticket, NULL, NULL, XLOG_UNMOUNT_TRANS);
Dave Chinner3c702f92020-03-25 18:18:24 -0700882}
883
884/*
885 * Mark the filesystem clean by writing an unmount record to the head of the
886 * log.
887 */
888static void
889xlog_unmount_write(
890 struct xlog *log)
891{
892 struct xfs_mount *mp = log->l_mp;
Darrick J. Wong53235f22018-07-20 09:28:39 -0700893 struct xlog_in_core *iclog;
894 struct xlog_ticket *tic = NULL;
Darrick J. Wong53235f22018-07-20 09:28:39 -0700895 int error;
896
897 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0);
898 if (error)
899 goto out_err;
900
Dave Chinner3468bb12021-06-18 08:21:50 -0700901 error = xlog_write_unmount_record(log, tic);
Darrick J. Wong53235f22018-07-20 09:28:39 -0700902 /*
903 * At this point, we're umounting anyway, so there's no point in
Dave Chinner5112e2062021-08-10 17:59:01 -0700904 * transitioning log state to shutdown. Just continue...
Darrick J. Wong53235f22018-07-20 09:28:39 -0700905 */
906out_err:
907 if (error)
908 xfs_alert(mp, "%s: unmount record failed", __func__);
909
910 spin_lock(&log->l_icloglock);
911 iclog = log->l_iclog;
Dave Chinner45eddb42021-07-27 16:23:48 -0700912 error = xlog_force_iclog(iclog);
Christoph Hellwig81e5b502020-03-20 08:49:18 -0700913 xlog_wait_on_iclog(iclog);
Darrick J. Wong53235f22018-07-20 09:28:39 -0700914
915 if (tic) {
916 trace_xfs_log_umount_write(log, tic);
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -0700917 xfs_log_ticket_ungrant(log, tic);
Darrick J. Wong53235f22018-07-20 09:28:39 -0700918 }
919}
920
Christoph Hellwig13859c92020-03-12 16:52:51 -0700921static void
922xfs_log_unmount_verify_iclog(
923 struct xlog *log)
924{
925 struct xlog_in_core *iclog = log->l_iclog;
926
927 do {
928 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
929 ASSERT(iclog->ic_offset == 0);
930 } while ((iclog = iclog->ic_next) != log->l_iclog);
931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*
934 * Unmount record used to have a string "Unmount filesystem--" in the
935 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
936 * We just write the magic number now since that particular field isn't
Zhi Yong Wu8e159e72013-08-12 03:15:00 +0000937 * currently architecture converted and "Unmount" is a bit foo.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * As far as I know, there weren't any dependencies on the old behaviour.
939 */
Christoph Hellwig550319e2020-03-12 16:52:50 -0700940static void
Christoph Hellwig13859c92020-03-12 16:52:51 -0700941xfs_log_unmount_write(
942 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Christoph Hellwig13859c92020-03-12 16:52:51 -0700944 struct xlog *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Brian Foster50d25482021-01-22 16:48:20 -0800946 if (!xfs_log_writable(mp))
Christoph Hellwig550319e2020-03-12 16:52:50 -0700947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Christoph Hellwig550319e2020-03-12 16:52:50 -0700949 xfs_log_force(mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dave Chinner2039a272021-08-10 17:59:01 -0700951 if (xlog_is_shutdown(log))
Christoph Hellwig6178d102020-03-12 16:52:51 -0700952 return;
Darrick J. Wong5cc3c002020-03-26 10:26:44 -0700953
954 /*
955 * If we think the summary counters are bad, avoid writing the unmount
956 * record to force log recovery at next mount, after which the summary
957 * counters will be recalculated. Refer to xlog_check_unmount_rec for
958 * more details.
959 */
960 if (XFS_TEST_ERROR(xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS), mp,
961 XFS_ERRTAG_FORCE_SUMMARY_RECALC)) {
962 xfs_alert(mp, "%s: will fix summary counters at next mount",
963 __func__);
964 return;
965 }
966
Christoph Hellwig13859c92020-03-12 16:52:51 -0700967 xfs_log_unmount_verify_iclog(log);
Dave Chinner3c702f92020-03-25 18:18:24 -0700968 xlog_unmount_write(log);
Christoph Hellwig550319e2020-03-12 16:52:50 -0700969}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971/*
Dave Chinnerc75921a2012-10-08 21:56:08 +1100972 * Empty the log for unmount/freeze.
Dave Chinnercf2931d2012-10-08 21:56:03 +1100973 *
974 * To do this, we first need to shut down the background log work so it is not
975 * trying to cover the log as we clean up. We then need to unpin all objects in
976 * the log so we can then flush them out. Once they have completed their IO and
Brian Foster303591a2021-01-22 16:48:22 -0800977 * run the callbacks removing themselves from the AIL, we can cover the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 */
Brian Foster303591a2021-01-22 16:48:22 -0800979int
Dave Chinnerc75921a2012-10-08 21:56:08 +1100980xfs_log_quiesce(
981 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Darrick J. Wong908ce712021-08-08 08:27:12 -0700983 /*
984 * Clear log incompat features since we're quiescing the log. Report
985 * failures, though it's not fatal to have a higher log feature
986 * protection level than the log contents actually require.
987 */
988 if (xfs_clear_incompat_log_features(mp)) {
989 int error;
990
991 error = xfs_sync_sb(mp, false);
992 if (error)
993 xfs_warn(mp,
994 "Failed to clear log incompat features on quiesce");
995 }
996
Dave Chinnerf661f1e2012-10-08 21:56:02 +1100997 cancel_delayed_work_sync(&mp->m_log->l_work);
Dave Chinnercf2931d2012-10-08 21:56:03 +1100998 xfs_log_force(mp, XFS_LOG_SYNC);
999
1000 /*
1001 * The superblock buffer is uncached and while xfs_ail_push_all_sync()
Brian Foster8321ddb2021-01-22 16:48:20 -08001002 * will push it, xfs_buftarg_wait() will not wait for it. Further,
Dave Chinnercf2931d2012-10-08 21:56:03 +11001003 * xfs_buf_iowait() cannot be used because it was pushed with the
1004 * XBF_ASYNC flag set, so we need to use a lock/unlock pair to wait for
1005 * the IO to complete.
1006 */
1007 xfs_ail_push_all_sync(mp->m_ail);
Brian Foster8321ddb2021-01-22 16:48:20 -08001008 xfs_buftarg_wait(mp->m_ddev_targp);
Dave Chinnercf2931d2012-10-08 21:56:03 +11001009 xfs_buf_lock(mp->m_sb_bp);
1010 xfs_buf_unlock(mp->m_sb_bp);
Brian Foster303591a2021-01-22 16:48:22 -08001011
1012 return xfs_log_cover(mp);
Brian Foster9e54ee02021-01-22 16:48:21 -08001013}
Dave Chinnercf2931d2012-10-08 21:56:03 +11001014
Brian Foster9e54ee02021-01-22 16:48:21 -08001015void
1016xfs_log_clean(
1017 struct xfs_mount *mp)
1018{
1019 xfs_log_quiesce(mp);
Dave Chinnercf2931d2012-10-08 21:56:03 +11001020 xfs_log_unmount_write(mp);
Dave Chinnerc75921a2012-10-08 21:56:08 +11001021}
1022
1023/*
1024 * Shut down and release the AIL and Log.
1025 *
1026 * During unmount, we need to ensure we flush all the dirty metadata objects
1027 * from the AIL so that the log is empty before we write the unmount record to
1028 * the log. Once this is done, we can tear down the AIL and the log.
1029 */
1030void
1031xfs_log_unmount(
1032 struct xfs_mount *mp)
1033{
Brian Foster9e54ee02021-01-22 16:48:21 -08001034 xfs_log_clean(mp);
Dave Chinnercf2931d2012-10-08 21:56:03 +11001035
Brian Foster8321ddb2021-01-22 16:48:20 -08001036 xfs_buftarg_drain(mp->m_ddev_targp);
1037
David Chinner249a8c12008-02-05 12:13:32 +11001038 xfs_trans_ail_destroy(mp);
Brian Fosterbaff4e42014-07-15 08:07:29 +10001039
1040 xfs_sysfs_del(&mp->m_log->l_kobj);
1041
Nathan Scottc41564b2006-03-29 08:55:14 +10001042 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Dave Chinner43f5efc2010-03-23 10:10:00 +11001045void
1046xfs_log_item_init(
1047 struct xfs_mount *mp,
1048 struct xfs_log_item *item,
1049 int type,
Christoph Hellwig272e42b2011-10-28 09:54:24 +00001050 const struct xfs_item_ops *ops)
Dave Chinner43f5efc2010-03-23 10:10:00 +11001051{
1052 item->li_mountp = mp;
1053 item->li_ailp = mp->m_ail;
1054 item->li_type = type;
1055 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +10001056 item->li_lv = NULL;
1057
1058 INIT_LIST_HEAD(&item->li_ail);
1059 INIT_LIST_HEAD(&item->li_cil);
Carlos Maiolino643c8c02018-01-24 13:38:49 -08001060 INIT_LIST_HEAD(&item->li_bio_list);
Dave Chinnere6631f82018-05-09 07:49:37 -07001061 INIT_LIST_HEAD(&item->li_trans);
Dave Chinner43f5efc2010-03-23 10:10:00 +11001062}
1063
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001064/*
1065 * Wake up processes waiting for log space after we have moved the log tail.
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067void
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001068xfs_log_space_wake(
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +00001069 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Mark Tinguelyad223e62012-06-14 09:22:15 -05001071 struct xlog *log = mp->m_log;
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +00001072 int free_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dave Chinner2039a272021-08-10 17:59:01 -07001074 if (xlog_is_shutdown(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Christoph Hellwig28496962012-02-20 02:31:25 +00001077 if (!list_empty_careful(&log->l_write_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001078 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
1079
Christoph Hellwig28496962012-02-20 02:31:25 +00001080 spin_lock(&log->l_write_head.lock);
1081 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
Christoph Hellwige179840d2012-02-20 02:31:29 +00001082 xlog_grant_head_wake(log, &log->l_write_head, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00001083 spin_unlock(&log->l_write_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Dave Chinner10547942010-12-21 12:02:25 +11001085
Christoph Hellwig28496962012-02-20 02:31:25 +00001086 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001087 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
1088
Christoph Hellwig28496962012-02-20 02:31:25 +00001089 spin_lock(&log->l_reserve_head.lock);
1090 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Christoph Hellwige179840d2012-02-20 02:31:29 +00001091 xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00001092 spin_unlock(&log->l_reserve_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Dave Chinner3f16b982010-12-21 12:29:01 +11001094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096/*
Dave Chinner2c6e24c2013-10-15 09:17:49 +11001097 * Determine if we have a transaction that has gone to disk that needs to be
1098 * covered. To begin the transition to the idle state firstly the log needs to
1099 * be idle. That means the CIL, the AIL and the iclogs needs to be empty before
1100 * we start attempting to cover the log.
Dave Chinnerb6f8dd42010-04-13 15:06:44 +10001101 *
Dave Chinner2c6e24c2013-10-15 09:17:49 +11001102 * Only if we are then in a state where covering is needed, the caller is
1103 * informed that dummy transactions are required to move the log into the idle
1104 * state.
1105 *
1106 * If there are any items in the AIl or CIL, then we do not want to attempt to
1107 * cover the log as we may be in a situation where there isn't log space
1108 * available to run a dummy transaction and this can lead to deadlocks when the
1109 * tail of the log is pinned by an item that is modified in the CIL. Hence
1110 * there's no point in running a dummy transaction at this point because we
1111 * can't start trying to idle the log until both the CIL and AIL are empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 */
Brian Foster37444fc2021-01-22 16:48:21 -08001113static bool
1114xfs_log_need_covered(
1115 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Brian Foster37444fc2021-01-22 16:48:21 -08001117 struct xlog *log = mp->m_log;
1118 bool needed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Dave Chinner2c6e24c2013-10-15 09:17:49 +11001120 if (!xlog_cil_empty(log))
kernel test robot8646b982021-02-10 17:27:31 -08001121 return false;
Dave Chinner2c6e24c2013-10-15 09:17:49 +11001122
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001123 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +10001124 switch (log->l_covered_state) {
1125 case XLOG_STATE_COVER_DONE:
1126 case XLOG_STATE_COVER_DONE2:
1127 case XLOG_STATE_COVER_IDLE:
1128 break;
1129 case XLOG_STATE_COVER_NEED:
1130 case XLOG_STATE_COVER_NEED2:
Dave Chinner2c6e24c2013-10-15 09:17:49 +11001131 if (xfs_ail_min_lsn(log->l_ailp))
1132 break;
1133 if (!xlog_iclogs_empty(log))
1134 break;
1135
Brian Foster37444fc2021-01-22 16:48:21 -08001136 needed = true;
Dave Chinner2c6e24c2013-10-15 09:17:49 +11001137 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
1138 log->l_covered_state = XLOG_STATE_COVER_DONE;
1139 else
1140 log->l_covered_state = XLOG_STATE_COVER_DONE2;
1141 break;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +10001142 default:
Brian Foster37444fc2021-01-22 16:48:21 -08001143 needed = true;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +10001144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001146 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01001147 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001150/*
Brian Foster303591a2021-01-22 16:48:22 -08001151 * Explicitly cover the log. This is similar to background log covering but
1152 * intended for usage in quiesce codepaths. The caller is responsible to ensure
1153 * the log is idle and suitable for covering. The CIL, iclog buffers and AIL
1154 * must all be empty.
1155 */
1156static int
1157xfs_log_cover(
1158 struct xfs_mount *mp)
1159{
Brian Foster303591a2021-01-22 16:48:22 -08001160 int error = 0;
Brian Fosterf46e5a12021-01-22 16:48:23 -08001161 bool need_covered;
Brian Foster303591a2021-01-22 16:48:22 -08001162
Brian Foster4533fc62021-01-26 19:14:55 -08001163 ASSERT((xlog_cil_empty(mp->m_log) && xlog_iclogs_empty(mp->m_log) &&
1164 !xfs_ail_min_lsn(mp->m_log->l_ailp)) ||
Dave Chinner2039a272021-08-10 17:59:01 -07001165 xlog_is_shutdown(mp->m_log));
Brian Foster303591a2021-01-22 16:48:22 -08001166
1167 if (!xfs_log_writable(mp))
1168 return 0;
1169
1170 /*
Brian Fosterf46e5a12021-01-22 16:48:23 -08001171 * xfs_log_need_covered() is not idempotent because it progresses the
1172 * state machine if the log requires covering. Therefore, we must call
1173 * this function once and use the result until we've issued an sb sync.
1174 * Do so first to make that abundantly clear.
1175 *
1176 * Fall into the covering sequence if the log needs covering or the
1177 * mount has lazy superblock accounting to sync to disk. The sb sync
1178 * used for covering accumulates the in-core counters, so covering
1179 * handles this for us.
1180 */
1181 need_covered = xfs_log_need_covered(mp);
1182 if (!need_covered && !xfs_sb_version_haslazysbcount(&mp->m_sb))
1183 return 0;
1184
1185 /*
Brian Foster303591a2021-01-22 16:48:22 -08001186 * To cover the log, commit the superblock twice (at most) in
1187 * independent checkpoints. The first serves as a reference for the
1188 * tail pointer. The sync transaction and AIL push empties the AIL and
1189 * updates the in-core tail to the LSN of the first checkpoint. The
1190 * second commit updates the on-disk tail with the in-core LSN,
1191 * covering the log. Push the AIL one more time to leave it empty, as
1192 * we found it.
1193 */
Brian Fosterf46e5a12021-01-22 16:48:23 -08001194 do {
Brian Foster303591a2021-01-22 16:48:22 -08001195 error = xfs_sync_sb(mp, true);
1196 if (error)
1197 break;
1198 xfs_ail_push_all_sync(mp->m_ail);
Brian Fosterf46e5a12021-01-22 16:48:23 -08001199 } while (xfs_log_need_covered(mp));
Brian Foster303591a2021-01-22 16:48:22 -08001200
1201 return error;
1202}
1203
1204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * We may be holding the log iclog lock upon entering this routine.
1206 */
1207xfs_lsn_t
Christoph Hellwig1c304622012-04-23 15:58:33 +10001208xlog_assign_tail_lsn_locked(
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001209 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Mark Tinguelyad223e62012-06-14 09:22:15 -05001211 struct xlog *log = mp->m_log;
Christoph Hellwig1c304622012-04-23 15:58:33 +10001212 struct xfs_log_item *lip;
1213 xfs_lsn_t tail_lsn;
1214
Matthew Wilcox57e80952018-03-07 14:59:39 -08001215 assert_spin_locked(&mp->m_ail->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001217 /*
1218 * To make sure we always have a valid LSN for the log tail we keep
1219 * track of the last LSN which was committed in log->l_last_sync_lsn,
Christoph Hellwig1c304622012-04-23 15:58:33 +10001220 * and use that when the AIL was empty.
Christoph Hellwig09a423a2012-02-20 02:31:20 +00001221 */
Christoph Hellwig1c304622012-04-23 15:58:33 +10001222 lip = xfs_ail_min(mp->m_ail);
1223 if (lip)
1224 tail_lsn = lip->li_lsn;
1225 else
Dave Chinner84f3c682010-12-03 22:11:29 +11001226 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Dave Chinner750b9c92013-11-01 15:27:18 +11001227 trace_xfs_log_assign_tail_lsn(log, tail_lsn);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001228 atomic64_set(&log->l_tail_lsn, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return tail_lsn;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Christoph Hellwig1c304622012-04-23 15:58:33 +10001232xfs_lsn_t
1233xlog_assign_tail_lsn(
1234 struct xfs_mount *mp)
1235{
1236 xfs_lsn_t tail_lsn;
1237
Matthew Wilcox57e80952018-03-07 14:59:39 -08001238 spin_lock(&mp->m_ail->ail_lock);
Christoph Hellwig1c304622012-04-23 15:58:33 +10001239 tail_lsn = xlog_assign_tail_lsn_locked(mp);
Matthew Wilcox57e80952018-03-07 14:59:39 -08001240 spin_unlock(&mp->m_ail->ail_lock);
Christoph Hellwig1c304622012-04-23 15:58:33 +10001241
1242 return tail_lsn;
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245/*
1246 * Return the space in the log between the tail and the head. The head
1247 * is passed in the cycle/bytes formal parms. In the special case where
1248 * the reserve head has wrapped passed the tail, this calculation is no
1249 * longer valid. In this case, just return 0 which means there is no space
1250 * in the log. This works for all places where this function is called
1251 * with the reserve head. Of course, if the write head were to ever
1252 * wrap the tail, we should blow up. Rather than catch this case here,
1253 * we depend on other ASSERTions in other parts of the code. XXXmiken
1254 *
1255 * This code also handles the case where the reservation head is behind
1256 * the tail. The details of this case are described below, but the end
1257 * result is that we return the size of the log as the amount of space left.
1258 */
David Chinnera8272ce2007-11-23 16:28:09 +11001259STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +11001260xlog_space_left(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001261 struct xlog *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +11001262 atomic64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Dave Chinnera69ed032010-12-21 12:08:20 +11001264 int free_bytes;
1265 int tail_bytes;
1266 int tail_cycle;
1267 int head_cycle;
1268 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Dave Chinnera69ed032010-12-21 12:08:20 +11001270 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001271 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
1272 tail_bytes = BBTOB(tail_bytes);
Dave Chinnera69ed032010-12-21 12:08:20 +11001273 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
1274 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
1275 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +11001277 else if (tail_cycle < head_cycle) {
1278 ASSERT(tail_cycle == (head_cycle - 1));
1279 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else {
1281 /*
1282 * The reservation head is behind the tail.
1283 * In this case we just want to return the size of the
1284 * log as the amount of space left.
1285 */
Joe Perchesf41febd2015-07-29 11:52:04 +10001286 xfs_alert(log->l_mp, "xlog_space_left: head behind tail");
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001287 xfs_alert(log->l_mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001288 " tail_cycle = %d, tail_bytes = %d",
1289 tail_cycle, tail_bytes);
1290 xfs_alert(log->l_mp,
1291 " GH cycle = %d, GH bytes = %d",
1292 head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ASSERT(0);
1294 free_bytes = log->l_logsize;
1295 }
1296 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +11001297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299
Eric Sandeen0d5a75e2016-06-01 17:38:15 +10001300static void
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001301xlog_ioend_work(
1302 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001304 struct xlog_in_core *iclog =
1305 container_of(work, struct xlog_in_core, ic_end_io_work);
1306 struct xlog *log = iclog->ic_log;
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001307 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001309 error = blk_status_to_errno(iclog->ic_bio.bi_status);
Christoph Hellwig366fc4b2019-06-28 19:27:21 -07001310#ifdef DEBUG
1311 /* treat writes with injected CRC errors as failed */
1312 if (iclog->ic_fail_crc)
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001313 error = -EIO;
Christoph Hellwig366fc4b2019-06-28 19:27:21 -07001314#endif
Brian Foster609adfc2016-01-05 07:41:16 +11001315
Christoph Hellwig366fc4b2019-06-28 19:27:21 -07001316 /*
1317 * Race to shutdown the filesystem if we see an error.
1318 */
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001319 if (XFS_TEST_ERROR(error, log->l_mp, XFS_ERRTAG_IODONE_IOERR)) {
1320 xfs_alert(log->l_mp, "log I/O error %d", error);
1321 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
David Chinner3db296f2007-05-14 18:24:16 +10001323
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07001324 xlog_state_done_syncing(iclog);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001325 bio_uninit(&iclog->ic_bio);
Dave Chinner9c23ecc2014-04-17 08:15:26 +10001326
David Chinner3db296f2007-05-14 18:24:16 +10001327 /*
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001328 * Drop the lock to signal that we are done. Nothing references the
1329 * iclog after this, so an unmount waiting on this lock can now tear it
1330 * down safely. As such, it is unsafe to reference the iclog after the
1331 * unlock as we could race with it being freed.
David Chinner3db296f2007-05-14 18:24:16 +10001332 */
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001333 up(&iclog->ic_sema);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11001334}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * Return size of each in-core log record buffer.
1338 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001339 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
1341 * If the filesystem blocksize is too large, we may need to choose a
1342 * larger size since the directory code currently logs entire blocks.
1343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344STATIC void
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001345xlog_get_iclog_buffer_size(
1346 struct xfs_mount *mp,
1347 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Eric Sandeen1cb51252007-08-16 16:24:43 +10001349 if (mp->m_logbufs <= 0)
Christoph Hellwig4f622822019-06-28 19:27:20 -07001350 mp->m_logbufs = XLOG_MAX_ICLOGS;
1351 if (mp->m_logbsize <= 0)
1352 mp->m_logbsize = XLOG_BIG_RECORD_BSIZE;
1353
1354 log->l_iclog_bufs = mp->m_logbufs;
1355 log->l_iclog_size = mp->m_logbsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
Christoph Hellwig4f622822019-06-28 19:27:20 -07001358 * # headers = size / 32k - one header holds cycles from 32k of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
Christoph Hellwig4f622822019-06-28 19:27:20 -07001360 log->l_iclog_heads =
1361 DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE);
1362 log->l_iclog_hsize = log->l_iclog_heads << BBSHIFT;
1363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dave Chinnerf661f1e2012-10-08 21:56:02 +11001365void
1366xfs_log_work_queue(
1367 struct xfs_mount *mp)
1368{
Brian Foster696a5622017-03-28 14:51:44 -07001369 queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work,
Dave Chinnerf661f1e2012-10-08 21:56:02 +11001370 msecs_to_jiffies(xfs_syncd_centisecs * 10));
1371}
1372
1373/*
Darrick J. Wong2b73a2c2021-08-08 08:27:12 -07001374 * Clear the log incompat flags if we have the opportunity.
1375 *
1376 * This only happens if we're about to log the second dummy transaction as part
1377 * of covering the log and we can get the log incompat feature usage lock.
1378 */
1379static inline void
1380xlog_clear_incompat(
1381 struct xlog *log)
1382{
1383 struct xfs_mount *mp = log->l_mp;
1384
1385 if (!xfs_sb_has_incompat_log_feature(&mp->m_sb,
1386 XFS_SB_FEAT_INCOMPAT_LOG_ALL))
1387 return;
1388
1389 if (log->l_covered_state != XLOG_STATE_COVER_DONE2)
1390 return;
1391
1392 if (!down_write_trylock(&log->l_incompat_users))
1393 return;
1394
1395 xfs_clear_incompat_log_features(mp);
1396 up_write(&log->l_incompat_users);
1397}
1398
1399/*
Dave Chinnerf661f1e2012-10-08 21:56:02 +11001400 * Every sync period we need to unpin all items in the AIL and push them to
1401 * disk. If there is nothing dirty, then we might need to cover the log to
1402 * indicate that the filesystem is idle.
1403 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +10001404static void
Dave Chinnerf661f1e2012-10-08 21:56:02 +11001405xfs_log_worker(
1406 struct work_struct *work)
1407{
1408 struct xlog *log = container_of(to_delayed_work(work),
1409 struct xlog, l_work);
1410 struct xfs_mount *mp = log->l_mp;
1411
1412 /* dgc: errors ignored - not fatal and nowhere to report them */
Brian Foster37444fc2021-01-22 16:48:21 -08001413 if (xfs_fs_writable(mp, SB_FREEZE_WRITE) && xfs_log_need_covered(mp)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001414 /*
1415 * Dump a transaction into the log that contains no real change.
1416 * This is needed to stamp the current tail LSN into the log
1417 * during the covering operation.
1418 *
1419 * We cannot use an inode here for this - that will push dirty
1420 * state back up into the VFS and then periodic inode flushing
1421 * will prevent log covering from making progress. Hence we
1422 * synchronously log the superblock instead to ensure the
1423 * superblock is immediately unpinned and can be written back.
1424 */
Darrick J. Wong2b73a2c2021-08-08 08:27:12 -07001425 xlog_clear_incompat(log);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001426 xfs_sync_sb(mp, true);
1427 } else
Dave Chinnerf661f1e2012-10-08 21:56:02 +11001428 xfs_log_force(mp, 0);
1429
1430 /* start pushing all the metadata that is currently dirty */
1431 xfs_ail_push_all(mp->m_ail);
1432
1433 /* queue us up again */
1434 xfs_log_work_queue(mp);
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
1438 * This routine initializes some of the log structure for a given mount point.
1439 * Its primary purpose is to fill in enough, so recovery can occur. However,
1440 * some other stuff may be filled in too.
1441 */
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001442STATIC struct xlog *
1443xlog_alloc_log(
1444 struct xfs_mount *mp,
1445 struct xfs_buftarg *log_target,
1446 xfs_daddr_t blk_offset,
1447 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001449 struct xlog *log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 xlog_rec_header_t *head;
1451 xlog_in_core_t **iclogp;
1452 xlog_in_core_t *iclog, *prev_iclog=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int i;
Dave Chinner24513372014-06-25 14:58:08 +10001454 int error = -ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001455 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001457 log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001458 if (!log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001459 xfs_warn(mp, "Log allocation failed: No memory!");
Dave Chinnera6cb7672009-04-06 18:39:27 +02001460 goto out;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 log->l_mp = mp;
1464 log->l_targ = log_target;
1465 log->l_logsize = BBTOB(num_bblks);
1466 log->l_logBBstart = blk_offset;
1467 log->l_logBBsize = num_bblks;
1468 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1469 log->l_flags |= XLOG_ACTIVE_RECOVERY;
Dave Chinnerf661f1e2012-10-08 21:56:02 +11001470 INIT_DELAYED_WORK(&log->l_work, xfs_log_worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 log->l_prev_block = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001474 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1475 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Christoph Hellwigc303c5b2012-02-20 02:31:26 +00001477
Dave Chinnera6a65fe2021-06-18 08:21:48 -07001478 if (xfs_sb_version_haslogv2(&mp->m_sb) && mp->m_sb.sb_logsunit > 1)
1479 log->l_iclog_roundoff = mp->m_sb.sb_logsunit;
1480 else
1481 log->l_iclog_roundoff = BBSIZE;
1482
Christoph Hellwigc303c5b2012-02-20 02:31:26 +00001483 xlog_grant_head_init(&log->l_reserve_head);
1484 xlog_grant_head_init(&log->l_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Dave Chinner24513372014-06-25 14:58:08 +10001486 error = -EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001487 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001488 log2_size = mp->m_sb.sb_logsectlog;
1489 if (log2_size < BBSHIFT) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001490 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1491 log2_size, BBSHIFT);
Alex Elder69ce58f2010-04-20 17:09:59 +10001492 goto out_free_log;
1493 }
1494
1495 log2_size -= BBSHIFT;
1496 if (log2_size > mp->m_sectbb_log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001497 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1498 log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001499 goto out_free_log;
1500 }
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001503 if (log2_size && log->l_logBBstart > 0 &&
1504 !xfs_sb_version_haslogv2(&mp->m_sb)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001505 xfs_warn(mp,
1506 "log sector size (0x%x) invalid for configuration.",
1507 log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001508 goto out_free_log;
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001511 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Darrick J. Wong2b73a2c2021-08-08 08:27:12 -07001513 init_rwsem(&log->l_incompat_users);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 xlog_get_iclog_buffer_size(mp, log);
1516
Eric Sandeen007c61c2007-10-11 17:43:56 +10001517 spin_lock_init(&log->l_icloglock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001518 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 iclogp = &log->l_iclog;
1521 /*
1522 * The amount of memory to allocate for the iclog structure is
1523 * rather funky due to the way the structure is defined. It is
1524 * done this way so that we can use different sizes for machines
1525 * with different amounts of memory. See the definition of
1526 * xlog_in_core_t in xfs_log_priv.h for details.
1527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ASSERT(log->l_iclog_size >= 4096);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001529 for (i = 0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig89b171a2019-06-28 19:31:36 -07001530 size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) *
1531 sizeof(struct bio_vec);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001532
1533 iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL);
1534 if (!iclog)
Dave Chinner644c3562008-11-10 16:50:24 +11001535 goto out_free_iclog;
1536
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001537 *iclogp = iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 iclog->ic_prev = prev_iclog;
1539 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001540
Dave Chinnerd6345252021-08-09 10:10:01 -07001541 iclog->ic_data = kvzalloc(log->l_iclog_size,
1542 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001543 if (!iclog->ic_data)
Dave Chinner644c3562008-11-10 16:50:24 +11001544 goto out_free_iclog;
David Chinner4679b2d2008-04-10 12:18:54 +10001545#ifdef DEBUG
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10001546 log->l_iclog_bak[i] = &iclog->ic_header;
David Chinner4679b2d2008-04-10 12:18:54 +10001547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 head = &iclog->ic_header;
1549 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001550 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1551 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001552 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001553 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001555 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1557
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001558 iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 iclog->ic_state = XLOG_STATE_ACTIVE;
1560 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001561 atomic_set(&iclog->ic_refcnt, 0);
Christoph Hellwig89ae3792019-06-28 19:27:34 -07001562 INIT_LIST_HEAD(&iclog->ic_callbacks);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001563 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Dave Chinnereb40a872010-12-21 12:09:01 +11001565 init_waitqueue_head(&iclog->ic_force_wait);
1566 init_waitqueue_head(&iclog->ic_write_wait);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001567 INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work);
1568 sema_init(&iclog->ic_sema, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 iclogp = &iclog->ic_next;
1571 }
1572 *iclogp = log->l_iclog; /* complete ring */
1573 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1574
Christoph Hellwig1058d0f2019-06-28 19:27:25 -07001575 log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s",
Darrick J. Wong05a302a2021-01-22 16:48:42 -08001576 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM |
1577 WQ_HIGHPRI),
1578 0, mp->m_super->s_id);
Christoph Hellwig1058d0f2019-06-28 19:27:25 -07001579 if (!log->l_ioend_workqueue)
1580 goto out_free_iclog;
1581
Dave Chinner71e330b2010-05-21 14:37:18 +10001582 error = xlog_cil_init(log);
1583 if (error)
Christoph Hellwig1058d0f2019-06-28 19:27:25 -07001584 goto out_destroy_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001586
Christoph Hellwig1058d0f2019-06-28 19:27:25 -07001587out_destroy_workqueue:
1588 destroy_workqueue(log->l_ioend_workqueue);
Dave Chinner644c3562008-11-10 16:50:24 +11001589out_free_iclog:
1590 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1591 prev_iclog = iclog->ic_next;
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001592 kmem_free(iclog->ic_data);
Dave Chinner644c3562008-11-10 16:50:24 +11001593 kmem_free(iclog);
Brian Foster798a9ca2019-12-03 07:53:15 -08001594 if (prev_iclog == log->l_iclog)
1595 break;
Dave Chinner644c3562008-11-10 16:50:24 +11001596 }
Dave Chinner644c3562008-11-10 16:50:24 +11001597out_free_log:
1598 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001599out:
Dave Chinner24513372014-06-25 14:58:08 +10001600 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601} /* xlog_alloc_log */
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603/*
1604 * Write out the commit record of a transaction associated with the given
Dave Chinnerf10e925d2020-03-25 18:18:23 -07001605 * ticket to close off a running log write. Return the lsn of the commit record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
Dave Chinnerf10e925d2020-03-25 18:18:23 -07001607int
Dave Chinner55b66332010-03-23 11:43:17 +11001608xlog_commit_record(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001609 struct xlog *log,
Dave Chinner55b66332010-03-23 11:43:17 +11001610 struct xlog_ticket *ticket,
1611 struct xlog_in_core **iclog,
Dave Chinnerf10e925d2020-03-25 18:18:23 -07001612 xfs_lsn_t *lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Dave Chinner55b66332010-03-23 11:43:17 +11001614 struct xfs_log_iovec reg = {
1615 .i_addr = NULL,
1616 .i_len = 0,
1617 .i_type = XLOG_REG_TYPE_COMMIT,
1618 };
1619 struct xfs_log_vec vec = {
1620 .lv_niovecs = 1,
1621 .lv_iovecp = &reg,
1622 };
Dave Chinnerf10e925d2020-03-25 18:18:23 -07001623 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Dave Chinner2039a272021-08-10 17:59:01 -07001625 if (xlog_is_shutdown(log))
Dave Chinnerf10e925d2020-03-25 18:18:23 -07001626 return -EIO;
1627
Dave Chinner3468bb12021-06-18 08:21:50 -07001628 error = xlog_write(log, &vec, ticket, lsn, iclog, XLOG_COMMIT_TRANS);
Dave Chinner55b66332010-03-23 11:43:17 +11001629 if (error)
Dave Chinnerf10e925d2020-03-25 18:18:23 -07001630 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001631 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634/*
Darrick J. Wonged1575d2020-09-25 17:39:51 -07001635 * Compute the LSN that we'd need to push the log tail towards in order to have
1636 * (a) enough on-disk log space to log the number of bytes specified, (b) at
1637 * least 25% of the log space free, and (c) at least 256 blocks free. If the
1638 * log free space already meets all three thresholds, this function returns
1639 * NULLCOMMITLSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 */
Darrick J. Wonged1575d2020-09-25 17:39:51 -07001641xfs_lsn_t
1642xlog_grant_push_threshold(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001643 struct xlog *log,
Dave Chinner2ced19c2010-12-21 12:09:20 +11001644 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001646 xfs_lsn_t threshold_lsn = 0;
Dave Chinner84f3c682010-12-03 22:11:29 +11001647 xfs_lsn_t last_sync_lsn;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001648 int free_blocks;
1649 int free_bytes;
1650 int threshold_block;
1651 int threshold_cycle;
1652 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Dave Chinner2ced19c2010-12-21 12:09:20 +11001654 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Christoph Hellwig28496962012-02-20 02:31:25 +00001656 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001657 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Dave Chinner2ced19c2010-12-21 12:09:20 +11001659 /*
1660 * Set the threshold for the minimum number of free blocks in the
1661 * log to the maximum of what the caller needs, one quarter of the
1662 * log, and 256 blocks.
1663 */
1664 free_threshold = BTOBB(need_bytes);
Dave Chinner9bb54cb2018-06-07 07:54:02 -07001665 free_threshold = max(free_threshold, (log->l_logBBsize >> 2));
1666 free_threshold = max(free_threshold, 256);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001667 if (free_blocks >= free_threshold)
Darrick J. Wonged1575d2020-09-25 17:39:51 -07001668 return NULLCOMMITLSN;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001669
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001670 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1671 &threshold_block);
1672 threshold_block += free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001674 threshold_block -= log->l_logBBsize;
1675 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001677 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1678 threshold_block);
1679 /*
1680 * Don't pass in an lsn greater than the lsn of the last
Dave Chinner84f3c682010-12-03 22:11:29 +11001681 * log record known to be on disk. Use a snapshot of the last sync lsn
1682 * so that it doesn't change between the compare and the set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 */
Dave Chinner84f3c682010-12-03 22:11:29 +11001684 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1685 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1686 threshold_lsn = last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Darrick J. Wonged1575d2020-09-25 17:39:51 -07001688 return threshold_lsn;
1689}
1690
1691/*
1692 * Push the tail of the log if we need to do so to maintain the free log space
1693 * thresholds set out by xlog_grant_push_threshold. We may need to adopt a
1694 * policy which pushes on an lsn which is further along in the log once we
1695 * reach the high water mark. In this manner, we would be creating a low water
1696 * mark.
1697 */
1698STATIC void
1699xlog_grant_push_ail(
1700 struct xlog *log,
1701 int need_bytes)
1702{
1703 xfs_lsn_t threshold_lsn;
1704
1705 threshold_lsn = xlog_grant_push_threshold(log, need_bytes);
Dave Chinner2039a272021-08-10 17:59:01 -07001706 if (threshold_lsn == NULLCOMMITLSN || xlog_is_shutdown(log))
Darrick J. Wonged1575d2020-09-25 17:39:51 -07001707 return;
1708
Dave Chinner2ced19c2010-12-21 12:09:20 +11001709 /*
1710 * Get the transaction layer to kick the dirty buffers out to
1711 * disk asynchronously. No point in trying to do this if
1712 * the filesystem is shutting down.
1713 */
Darrick J. Wonged1575d2020-09-25 17:39:51 -07001714 xfs_ail_push(log->l_ailp, threshold_lsn);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001715}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001717/*
Christoph Hellwig0e446be2012-11-12 22:54:24 +11001718 * Stamp cycle number in every block
1719 */
1720STATIC void
1721xlog_pack_data(
1722 struct xlog *log,
1723 struct xlog_in_core *iclog,
1724 int roundoff)
1725{
1726 int i, j, k;
1727 int size = iclog->ic_offset + roundoff;
1728 __be32 cycle_lsn;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10001729 char *dp;
Christoph Hellwig0e446be2012-11-12 22:54:24 +11001730
1731 cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
1732
1733 dp = iclog->ic_datap;
1734 for (i = 0; i < BTOBB(size); i++) {
1735 if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE))
1736 break;
1737 iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
1738 *(__be32 *)dp = cycle_lsn;
1739 dp += BBSIZE;
1740 }
1741
1742 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1743 xlog_in_core_2_t *xhdr = iclog->ic_data;
1744
1745 for ( ; i < BTOBB(size); i++) {
1746 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1747 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1748 xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
1749 *(__be32 *)dp = cycle_lsn;
1750 dp += BBSIZE;
1751 }
1752
1753 for (i = 1; i < log->l_iclog_heads; i++)
1754 xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
1755 }
1756}
1757
1758/*
1759 * Calculate the checksum for a log buffer.
1760 *
1761 * This is a little more complicated than it should be because the various
1762 * headers and the actual data are non-contiguous.
1763 */
Dave Chinnerf9668a02012-11-28 13:01:03 +11001764__le32
Christoph Hellwig0e446be2012-11-12 22:54:24 +11001765xlog_cksum(
1766 struct xlog *log,
1767 struct xlog_rec_header *rhead,
1768 char *dp,
1769 int size)
1770{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001771 uint32_t crc;
Christoph Hellwig0e446be2012-11-12 22:54:24 +11001772
1773 /* first generate the crc for the record header ... */
Dave Chinnercae028d2016-12-05 14:40:32 +11001774 crc = xfs_start_cksum_update((char *)rhead,
Christoph Hellwig0e446be2012-11-12 22:54:24 +11001775 sizeof(struct xlog_rec_header),
1776 offsetof(struct xlog_rec_header, h_crc));
1777
1778 /* ... then for additional cycle data for v2 logs ... */
1779 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1780 union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead;
1781 int i;
Brian Fostera3f20012015-08-19 09:59:50 +10001782 int xheads;
Christoph Hellwig0e446be2012-11-12 22:54:24 +11001783
Gao Xiang0c771b92020-09-22 09:41:06 -07001784 xheads = DIV_ROUND_UP(size, XLOG_HEADER_CYCLE_SIZE);
Brian Fostera3f20012015-08-19 09:59:50 +10001785
1786 for (i = 1; i < xheads; i++) {
Christoph Hellwig0e446be2012-11-12 22:54:24 +11001787 crc = crc32c(crc, &xhdr[i].hic_xheader,
1788 sizeof(struct xlog_rec_ext_header));
1789 }
1790 }
1791
1792 /* ... and finally for the payload */
1793 crc = crc32c(crc, dp, size);
1794
1795 return xfs_end_cksum(crc);
1796}
1797
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001798static void
1799xlog_bio_end_io(
1800 struct bio *bio)
1801{
1802 struct xlog_in_core *iclog = bio->bi_private;
1803
Christoph Hellwig1058d0f2019-06-28 19:27:25 -07001804 queue_work(iclog->ic_log->l_ioend_workqueue,
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001805 &iclog->ic_end_io_work);
1806}
1807
Brian Foster842a42d2020-03-25 09:17:13 -07001808static int
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001809xlog_map_iclog_data(
1810 struct bio *bio,
1811 void *data,
1812 size_t count)
1813{
1814 do {
1815 struct page *page = kmem_to_page(data);
1816 unsigned int off = offset_in_page(data);
1817 size_t len = min_t(size_t, count, PAGE_SIZE - off);
1818
Brian Foster842a42d2020-03-25 09:17:13 -07001819 if (bio_add_page(bio, page, len, off) != len)
1820 return -EIO;
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001821
1822 data += len;
1823 count -= len;
1824 } while (count);
Brian Foster842a42d2020-03-25 09:17:13 -07001825
1826 return 0;
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001827}
1828
Christoph Hellwig94860a32019-06-28 19:27:22 -07001829STATIC void
1830xlog_write_iclog(
1831 struct xlog *log,
1832 struct xlog_in_core *iclog,
Christoph Hellwig94860a32019-06-28 19:27:22 -07001833 uint64_t bno,
Dave Chinnereef983f2021-06-18 08:21:51 -07001834 unsigned int count)
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001835{
Christoph Hellwig94860a32019-06-28 19:27:22 -07001836 ASSERT(bno < log->l_logBBsize);
Dave Chinner956f6da2021-06-18 11:57:05 -07001837 trace_xlog_iclog_write(iclog, _RET_IP_);
Christoph Hellwig94860a32019-06-28 19:27:22 -07001838
1839 /*
1840 * We lock the iclogbufs here so that we can serialise against I/O
1841 * completion during unmount. We might be processing a shutdown
1842 * triggered during unmount, and that can occur asynchronously to the
1843 * unmount thread, and hence we need to ensure that completes before
1844 * tearing down the iclogbufs. Hence we need to hold the buffer lock
1845 * across the log IO to archieve that.
1846 */
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001847 down(&iclog->ic_sema);
Dave Chinner5112e2062021-08-10 17:59:01 -07001848 if (xlog_is_shutdown(log)) {
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001849 /*
1850 * It would seem logical to return EIO here, but we rely on
1851 * the log state machine to propagate I/O errors instead of
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001852 * doing it here. We kick of the state machine and unlock
1853 * the buffer manually, the code needs to be kept in sync
1854 * with the I/O completion path.
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001855 */
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07001856 xlog_state_done_syncing(iclog);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001857 up(&iclog->ic_sema);
Christoph Hellwig94860a32019-06-28 19:27:22 -07001858 return;
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001859 }
1860
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001861 bio_init(&iclog->ic_bio, iclog->ic_bvec, howmany(count, PAGE_SIZE));
1862 bio_set_dev(&iclog->ic_bio, log->l_targ->bt_bdev);
1863 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno;
1864 iclog->ic_bio.bi_end_io = xlog_bio_end_io;
1865 iclog->ic_bio.bi_private = iclog;
Dave Chinner2def2842020-03-24 20:10:27 -07001866
1867 /*
1868 * We use REQ_SYNC | REQ_IDLE here to tell the block layer the are more
1869 * IOs coming immediately after this one. This prevents the block layer
1870 * writeback throttle from throttling log writes behind background
1871 * metadata writeback and causing priority inversions.
1872 */
Dave Chinnereef983f2021-06-18 08:21:51 -07001873 iclog->ic_bio.bi_opf = REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_IDLE;
Dave Chinnerb5d721e2021-07-27 16:23:47 -07001874 if (iclog->ic_flags & XLOG_ICL_NEED_FLUSH) {
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001875 iclog->ic_bio.bi_opf |= REQ_PREFLUSH;
Dave Chinnerb5d721e2021-07-27 16:23:47 -07001876 /*
1877 * For external log devices, we also need to flush the data
1878 * device cache first to ensure all metadata writeback covered
1879 * by the LSN in this iclog is on stable storage. This is slow,
1880 * but it *must* complete before we issue the external log IO.
1881 */
1882 if (log->l_targ != log->l_mp->m_ddev_targp)
1883 blkdev_issue_flush(log->l_mp->m_ddev_targp->bt_bdev);
1884 }
Dave Chinnereef983f2021-06-18 08:21:51 -07001885 if (iclog->ic_flags & XLOG_ICL_NEED_FUA)
1886 iclog->ic_bio.bi_opf |= REQ_FUA;
Dave Chinnerb5d721e2021-07-27 16:23:47 -07001887
Dave Chinnereef983f2021-06-18 08:21:51 -07001888 iclog->ic_flags &= ~(XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001889
Brian Foster842a42d2020-03-25 09:17:13 -07001890 if (xlog_map_iclog_data(&iclog->ic_bio, iclog->ic_data, count)) {
1891 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
1892 return;
1893 }
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001894 if (is_vmalloc_addr(iclog->ic_data))
Christoph Hellwig2c68a1d2019-10-14 10:36:40 -07001895 flush_kernel_vmap_range(iclog->ic_data, count);
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001896
1897 /*
1898 * If this log buffer would straddle the end of the log we will have
1899 * to split it up into two bios, so that we can continue at the start.
1900 */
1901 if (bno + BTOBB(count) > log->l_logBBsize) {
1902 struct bio *split;
1903
1904 split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno,
1905 GFP_NOIO, &fs_bio_set);
1906 bio_chain(split, &iclog->ic_bio);
1907 submit_bio(split);
1908
1909 /* restart at logical offset zero for the remainder */
1910 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart;
1911 }
1912
1913 submit_bio(&iclog->ic_bio);
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001914}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916/*
Christoph Hellwig56933842019-06-28 19:27:22 -07001917 * We need to bump cycle number for the part of the iclog that is
1918 * written to the start of the log. Watch out for the header magic
1919 * number case, though.
1920 */
Christoph Hellwig79b54d92019-06-28 19:27:25 -07001921static void
Christoph Hellwig56933842019-06-28 19:27:22 -07001922xlog_split_iclog(
1923 struct xlog *log,
1924 void *data,
1925 uint64_t bno,
1926 unsigned int count)
1927{
1928 unsigned int split_offset = BBTOB(log->l_logBBsize - bno);
1929 unsigned int i;
1930
1931 for (i = split_offset; i < count; i += BBSIZE) {
1932 uint32_t cycle = get_unaligned_be32(data + i);
1933
1934 if (++cycle == XLOG_HEADER_MAGIC_NUM)
1935 cycle++;
1936 put_unaligned_be32(cycle, data + i);
1937 }
Christoph Hellwig56933842019-06-28 19:27:22 -07001938}
1939
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001940static int
1941xlog_calc_iclog_size(
1942 struct xlog *log,
1943 struct xlog_in_core *iclog,
1944 uint32_t *roundoff)
1945{
1946 uint32_t count_init, count;
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001947
1948 /* Add for LR header */
1949 count_init = log->l_iclog_hsize + iclog->ic_offset;
Dave Chinnera6a65fe2021-06-18 08:21:48 -07001950 count = roundup(count_init, log->l_iclog_roundoff);
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001951
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001952 *roundoff = count - count_init;
1953
Dave Chinnera6a65fe2021-06-18 08:21:48 -07001954 ASSERT(count >= count_init);
1955 ASSERT(*roundoff < log->l_iclog_roundoff);
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001956 return count;
1957}
1958
Christoph Hellwig56933842019-06-28 19:27:22 -07001959/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1961 * fashion. Previously, we should have moved the current iclog
1962 * ptr in the log to point to the next available iclog. This allows further
1963 * write to continue while this code syncs out an iclog ready to go.
1964 * Before an in-core log can be written out, the data section must be scanned
1965 * to save away the 1st word of each BBSIZE block into the header. We replace
1966 * it with the current cycle count. Each BBSIZE block is tagged with the
1967 * cycle count because there in an implicit assumption that drives will
1968 * guarantee that entire 512 byte blocks get written at once. In other words,
1969 * we can't have part of a 512 byte block written and part not written. By
1970 * tagging each block, we will know which blocks are valid when recovering
1971 * after an unclean shutdown.
1972 *
1973 * This routine is single threaded on the iclog. No other thread can be in
1974 * this routine with the same iclog. Changing contents of iclog can there-
1975 * fore be done without grabbing the state machine lock. Updating the global
1976 * log will require grabbing the lock though.
1977 *
1978 * The entire log manager uses a logical block numbering scheme. Only
Christoph Hellwig94860a32019-06-28 19:27:22 -07001979 * xlog_write_iclog knows about the fact that the log may not start with
1980 * block zero on a given device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 */
Christoph Hellwig94860a32019-06-28 19:27:22 -07001982STATIC void
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001983xlog_sync(
1984 struct xlog *log,
1985 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001987 unsigned int count; /* byte count of bwrite */
1988 unsigned int roundoff; /* roundoff to BB or stripe */
1989 uint64_t bno;
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001990 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
David Chinner155cc6b2008-03-06 13:44:14 +11001992 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Dave Chinner956f6da2021-06-18 11:57:05 -07001993 trace_xlog_iclog_sync(iclog, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07001995 count = xlog_calc_iclog_size(log, iclog, &roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* move grant heads by roundoff in sync */
Christoph Hellwig28496962012-02-20 02:31:25 +00001998 xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
1999 xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* put cycle number in every block */
2002 xlog_pack_data(log, iclog, roundoff);
2003
2004 /* real byte length */
Christoph Hellwig0e446be2012-11-12 22:54:24 +11002005 size = iclog->ic_offset;
Christoph Hellwigdb0a6fa2019-06-28 19:27:23 -07002006 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb))
Christoph Hellwig0e446be2012-11-12 22:54:24 +11002007 size += roundoff;
2008 iclog->ic_header.h_len = cpu_to_be32(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Christoph Hellwig9b0489c2019-06-28 19:27:23 -07002010 XFS_STATS_INC(log->l_mp, xs_log_writes);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002011 XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Christoph Hellwig94860a32019-06-28 19:27:22 -07002013 bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn));
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* Do we need to split this write into 2 parts? */
Dave Chinnereef983f2021-06-18 08:21:51 -07002016 if (bno + BTOBB(count) > log->l_logBBsize)
Christoph Hellwig79b54d92019-06-28 19:27:25 -07002017 xlog_split_iclog(log, &iclog->ic_header, bno, count);
Christoph Hellwig0e446be2012-11-12 22:54:24 +11002018
2019 /* calculcate the checksum */
2020 iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header,
2021 iclog->ic_datap, size);
Brian Foster609adfc2016-01-05 07:41:16 +11002022 /*
2023 * Intentionally corrupt the log record CRC based on the error injection
2024 * frequency, if defined. This facilitates testing log recovery in the
2025 * event of torn writes. Hence, set the IOABORT state to abort the log
2026 * write on I/O completion and shutdown the fs. The subsequent mount
2027 * detects the bad CRC and attempts to recover.
2028 */
Christoph Hellwig366fc4b2019-06-28 19:27:21 -07002029#ifdef DEBUG
Brian Foster3e88a002017-06-27 09:52:32 -07002030 if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) {
Christoph Hellwige2a64192017-04-21 11:24:40 -07002031 iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA);
Christoph Hellwig366fc4b2019-06-28 19:27:21 -07002032 iclog->ic_fail_crc = true;
Brian Foster609adfc2016-01-05 07:41:16 +11002033 xfs_warn(log->l_mp,
2034 "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.",
2035 be64_to_cpu(iclog->ic_header.h_lsn));
2036 }
Christoph Hellwig366fc4b2019-06-28 19:27:21 -07002037#endif
Christoph Hellwigabca1f32019-06-28 19:27:24 -07002038 xlog_verify_iclog(log, iclog, count);
Dave Chinnereef983f2021-06-18 08:21:51 -07002039 xlog_write_iclog(log, iclog, bno, count);
Christoph Hellwig94860a32019-06-28 19:27:22 -07002040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042/*
Nathan Scottc41564b2006-03-29 08:55:14 +10002043 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 */
David Chinnera8272ce2007-11-23 16:28:09 +11002045STATIC void
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002046xlog_dealloc_log(
2047 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 int i;
2051
Dave Chinner71e330b2010-05-21 14:37:18 +10002052 xlog_cil_destroy(log);
2053
Dave Chinner44396472011-04-21 09:34:27 +00002054 /*
Dave Chinner9c23ecc2014-04-17 08:15:26 +10002055 * Cycle all the iclogbuf locks to make sure all log IO completion
2056 * is done before we tear down these buffers.
Dave Chinner44396472011-04-21 09:34:27 +00002057 */
Dave Chinner9c23ecc2014-04-17 08:15:26 +10002058 iclog = log->l_iclog;
2059 for (i = 0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig79b54d92019-06-28 19:27:25 -07002060 down(&iclog->ic_sema);
2061 up(&iclog->ic_sema);
Dave Chinner9c23ecc2014-04-17 08:15:26 +10002062 iclog = iclog->ic_next;
2063 }
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 iclog = log->l_iclog;
Dave Chinner9c23ecc2014-04-17 08:15:26 +10002066 for (i = 0; i < log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 next_iclog = iclog->ic_next;
Christoph Hellwig79b54d92019-06-28 19:27:25 -07002068 kmem_free(iclog->ic_data);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002069 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 iclog = next_iclog;
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 log->l_mp->m_log = NULL;
Christoph Hellwig1058d0f2019-06-28 19:27:25 -07002074 destroy_workqueue(log->l_ioend_workqueue);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002075 kmem_free(log);
Dave Chinnerb8432992020-03-25 18:18:24 -07002076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078/*
2079 * Update counters atomically now that memcpy is done.
2080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081static inline void
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002082xlog_state_finish_copy(
2083 struct xlog *log,
2084 struct xlog_in_core *iclog,
2085 int record_cnt,
2086 int copy_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Christoph Hellwig390aab02019-10-14 10:36:41 -07002088 lockdep_assert_held(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002090 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 iclog->ic_offset += copy_bytes;
Christoph Hellwig390aab02019-10-14 10:36:41 -07002092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002095 * print out info relating to regions written which consume
2096 * the reservation
2097 */
Dave Chinner71e330b2010-05-21 14:37:18 +10002098void
2099xlog_print_tic_res(
2100 struct xfs_mount *mp,
2101 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002102{
2103 uint i;
2104 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
2105
2106 /* match with XLOG_REG_TYPE_* in xfs_log.h */
Darrick J. Wong5110cd82016-03-07 08:40:03 +11002107#define REG_TYPE_STR(type, str) [XLOG_REG_TYPE_##type] = str
Darrick J. Wongd31d7182019-05-23 08:45:21 -07002108 static char *res_type_str[] = {
Darrick J. Wong5110cd82016-03-07 08:40:03 +11002109 REG_TYPE_STR(BFORMAT, "bformat"),
2110 REG_TYPE_STR(BCHUNK, "bchunk"),
2111 REG_TYPE_STR(EFI_FORMAT, "efi_format"),
2112 REG_TYPE_STR(EFD_FORMAT, "efd_format"),
2113 REG_TYPE_STR(IFORMAT, "iformat"),
2114 REG_TYPE_STR(ICORE, "icore"),
2115 REG_TYPE_STR(IEXT, "iext"),
2116 REG_TYPE_STR(IBROOT, "ibroot"),
2117 REG_TYPE_STR(ILOCAL, "ilocal"),
2118 REG_TYPE_STR(IATTR_EXT, "iattr_ext"),
2119 REG_TYPE_STR(IATTR_BROOT, "iattr_broot"),
2120 REG_TYPE_STR(IATTR_LOCAL, "iattr_local"),
2121 REG_TYPE_STR(QFORMAT, "qformat"),
2122 REG_TYPE_STR(DQUOT, "dquot"),
2123 REG_TYPE_STR(QUOTAOFF, "quotaoff"),
2124 REG_TYPE_STR(LRHEADER, "LR header"),
2125 REG_TYPE_STR(UNMOUNT, "unmount"),
2126 REG_TYPE_STR(COMMIT, "commit"),
2127 REG_TYPE_STR(TRANSHDR, "trans header"),
Darrick J. Wongd31d7182019-05-23 08:45:21 -07002128 REG_TYPE_STR(ICREATE, "inode create"),
2129 REG_TYPE_STR(RUI_FORMAT, "rui_format"),
2130 REG_TYPE_STR(RUD_FORMAT, "rud_format"),
2131 REG_TYPE_STR(CUI_FORMAT, "cui_format"),
2132 REG_TYPE_STR(CUD_FORMAT, "cud_format"),
2133 REG_TYPE_STR(BUI_FORMAT, "bui_format"),
2134 REG_TYPE_STR(BUD_FORMAT, "bud_format"),
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002135 };
Darrick J. Wongd31d7182019-05-23 08:45:21 -07002136 BUILD_BUG_ON(ARRAY_SIZE(res_type_str) != XLOG_REG_TYPE_MAX + 1);
Darrick J. Wong5110cd82016-03-07 08:40:03 +11002137#undef REG_TYPE_STR
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002138
Brian Foster7d2d56532017-06-14 21:29:48 -07002139 xfs_warn(mp, "ticket reservation summary:");
Joe Perchesf41febd2015-07-29 11:52:04 +10002140 xfs_warn(mp, " unit res = %d bytes",
2141 ticket->t_unit_res);
2142 xfs_warn(mp, " current res = %d bytes",
2143 ticket->t_curr_res);
2144 xfs_warn(mp, " total reg = %u bytes (o/flow = %u bytes)",
2145 ticket->t_res_arr_sum, ticket->t_res_o_flow);
2146 xfs_warn(mp, " ophdrs = %u (ophdr space = %u bytes)",
2147 ticket->t_res_num_ophdrs, ophdr_spc);
2148 xfs_warn(mp, " ophdr + reg = %u bytes",
2149 ticket->t_res_arr_sum + ticket->t_res_o_flow + ophdr_spc);
2150 xfs_warn(mp, " num regions = %u",
2151 ticket->t_res_num);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002152
2153 for (i = 0; i < ticket->t_res_num; i++) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002154 uint r_type = ticket->t_res_arr[i].r_type;
Eric Sandeen08e96e12013-10-11 20:59:05 -05002155 xfs_warn(mp, "region[%u]: %s - %u bytes", i,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002156 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
Darrick J. Wong5110cd82016-03-07 08:40:03 +11002157 "bad-rtype" : res_type_str[r_type]),
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002158 ticket->t_res_arr[i].r_len);
2159 }
2160}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002161
2162/*
Brian Fosterd4ca1d52017-06-14 21:29:50 -07002163 * Print a summary of the transaction.
2164 */
2165void
2166xlog_print_trans(
Dave Chinnere6631f82018-05-09 07:49:37 -07002167 struct xfs_trans *tp)
Brian Fosterd4ca1d52017-06-14 21:29:50 -07002168{
Dave Chinnere6631f82018-05-09 07:49:37 -07002169 struct xfs_mount *mp = tp->t_mountp;
2170 struct xfs_log_item *lip;
Brian Fosterd4ca1d52017-06-14 21:29:50 -07002171
2172 /* dump core transaction and ticket info */
2173 xfs_warn(mp, "transaction summary:");
Brian Foster2c8f6262018-01-08 10:41:35 -08002174 xfs_warn(mp, " log res = %d", tp->t_log_res);
2175 xfs_warn(mp, " log count = %d", tp->t_log_count);
2176 xfs_warn(mp, " flags = 0x%x", tp->t_flags);
Brian Fosterd4ca1d52017-06-14 21:29:50 -07002177
2178 xlog_print_tic_res(mp, tp->t_ticket);
2179
2180 /* dump each log item */
Dave Chinnere6631f82018-05-09 07:49:37 -07002181 list_for_each_entry(lip, &tp->t_items, li_trans) {
Brian Fosterd4ca1d52017-06-14 21:29:50 -07002182 struct xfs_log_vec *lv = lip->li_lv;
2183 struct xfs_log_iovec *vec;
2184 int i;
2185
2186 xfs_warn(mp, "log item: ");
2187 xfs_warn(mp, " type = 0x%x", lip->li_type);
Dave Chinner22525c12018-05-09 07:47:34 -07002188 xfs_warn(mp, " flags = 0x%lx", lip->li_flags);
Brian Fosterd4ca1d52017-06-14 21:29:50 -07002189 if (!lv)
2190 continue;
2191 xfs_warn(mp, " niovecs = %d", lv->lv_niovecs);
2192 xfs_warn(mp, " size = %d", lv->lv_size);
2193 xfs_warn(mp, " bytes = %d", lv->lv_bytes);
2194 xfs_warn(mp, " buf len = %d", lv->lv_buf_len);
2195
2196 /* dump each iovec for the log item */
2197 vec = lv->lv_iovecp;
2198 for (i = 0; i < lv->lv_niovecs; i++) {
2199 int dumplen = min(vec->i_len, 32);
2200
2201 xfs_warn(mp, " iovec[%d]", i);
2202 xfs_warn(mp, " type = 0x%x", vec->i_type);
2203 xfs_warn(mp, " len = %d", vec->i_len);
2204 xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i);
kbuild test robot244e3de2017-06-26 08:54:16 -07002205 xfs_hex_dump(vec->i_addr, dumplen);
Brian Fosterd4ca1d52017-06-14 21:29:50 -07002206
2207 vec++;
2208 }
2209 }
2210}
2211
2212/*
Dave Chinner7ec94922020-03-25 18:18:20 -07002213 * Calculate the potential space needed by the log vector. We may need a start
2214 * record, and each region gets its own struct xlog_op_header and may need to be
2215 * double word aligned.
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002216 */
2217static int
2218xlog_write_calc_vec_length(
2219 struct xlog_ticket *ticket,
Dave Chinner7ec94922020-03-25 18:18:20 -07002220 struct xfs_log_vec *log_vector,
Dave Chinner3468bb12021-06-18 08:21:50 -07002221 uint optype)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002222{
Dave Chinner55b66332010-03-23 11:43:17 +11002223 struct xfs_log_vec *lv;
Dave Chinner3468bb12021-06-18 08:21:50 -07002224 int headers = 0;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002225 int len = 0;
2226 int i;
2227
Dave Chinner3468bb12021-06-18 08:21:50 -07002228 if (optype & XLOG_START_TRANS)
2229 headers++;
2230
Dave Chinner55b66332010-03-23 11:43:17 +11002231 for (lv = log_vector; lv; lv = lv->lv_next) {
Dave Chinnerfd638752013-06-27 16:04:51 +10002232 /* we don't write ordered log vectors */
2233 if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED)
2234 continue;
2235
Dave Chinner55b66332010-03-23 11:43:17 +11002236 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002237
Dave Chinner55b66332010-03-23 11:43:17 +11002238 for (i = 0; i < lv->lv_niovecs; i++) {
2239 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
2240
2241 len += vecp->i_len;
2242 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
2243 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002244 }
2245
2246 ticket->t_res_num_ophdrs += headers;
2247 len += headers * sizeof(struct xlog_op_header);
2248
2249 return len;
2250}
2251
Dave Chinner7ec94922020-03-25 18:18:20 -07002252static void
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002253xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002254 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002255 struct xlog_ticket *ticket)
2256{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002257 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2258 ophdr->oh_clientid = ticket->t_clientid;
2259 ophdr->oh_len = 0;
2260 ophdr->oh_flags = XLOG_START_TRANS;
2261 ophdr->oh_res2 = 0;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002262}
2263
2264static xlog_op_header_t *
2265xlog_write_setup_ophdr(
Mark Tinguelyad223e62012-06-14 09:22:15 -05002266 struct xlog *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002267 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002268 struct xlog_ticket *ticket,
2269 uint flags)
2270{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002271 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2272 ophdr->oh_clientid = ticket->t_clientid;
2273 ophdr->oh_res2 = 0;
2274
2275 /* are we copying a commit or unmount record? */
2276 ophdr->oh_flags = flags;
2277
2278 /*
2279 * We've seen logs corrupted with bad transaction client ids. This
2280 * makes sure that XFS doesn't generate them on. Turn this into an EIO
2281 * and shut down the filesystem.
2282 */
2283 switch (ophdr->oh_clientid) {
2284 case XFS_TRANSACTION:
2285 case XFS_VOLUME:
2286 case XFS_LOG:
2287 break;
2288 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002289 xfs_warn(log->l_mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08002290 "Bad XFS transaction clientid 0x%x in ticket "PTR_FMT,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002291 ophdr->oh_clientid, ticket);
2292 return NULL;
2293 }
2294
2295 return ophdr;
2296}
2297
2298/*
2299 * Set up the parameters of the region copy into the log. This has
2300 * to handle region write split across multiple log buffers - this
2301 * state is kept external to this function so that this code can
Zhi Yong Wuac0e3002013-08-07 10:11:02 +00002302 * be written in an obvious, self documenting manner.
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002303 */
2304static int
2305xlog_write_setup_copy(
2306 struct xlog_ticket *ticket,
2307 struct xlog_op_header *ophdr,
2308 int space_available,
2309 int space_required,
2310 int *copy_off,
2311 int *copy_len,
2312 int *last_was_partial_copy,
2313 int *bytes_consumed)
2314{
2315 int still_to_copy;
2316
2317 still_to_copy = space_required - *bytes_consumed;
2318 *copy_off = *bytes_consumed;
2319
2320 if (still_to_copy <= space_available) {
2321 /* write of region completes here */
2322 *copy_len = still_to_copy;
2323 ophdr->oh_len = cpu_to_be32(*copy_len);
2324 if (*last_was_partial_copy)
2325 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
2326 *last_was_partial_copy = 0;
2327 *bytes_consumed = 0;
2328 return 0;
2329 }
2330
2331 /* partial write of region, needs extra log op header reservation */
2332 *copy_len = space_available;
2333 ophdr->oh_len = cpu_to_be32(*copy_len);
2334 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
2335 if (*last_was_partial_copy)
2336 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
2337 *bytes_consumed += *copy_len;
2338 (*last_was_partial_copy)++;
2339
2340 /* account for new log op header */
2341 ticket->t_curr_res -= sizeof(struct xlog_op_header);
2342 ticket->t_res_num_ophdrs++;
2343
2344 return sizeof(struct xlog_op_header);
2345}
2346
2347static int
2348xlog_write_copy_finish(
Mark Tinguelyad223e62012-06-14 09:22:15 -05002349 struct xlog *log,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002350 struct xlog_in_core *iclog,
2351 uint flags,
2352 int *record_cnt,
2353 int *data_cnt,
2354 int *partial_copy,
2355 int *partial_copy_len,
2356 int log_offset,
2357 struct xlog_in_core **commit_iclog)
2358{
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002359 int error;
2360
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002361 if (*partial_copy) {
2362 /*
2363 * This iclog has already been marked WANT_SYNC by
2364 * xlog_state_get_iclog_space.
2365 */
Christoph Hellwig390aab02019-10-14 10:36:41 -07002366 spin_lock(&log->l_icloglock);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002367 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
2368 *record_cnt = 0;
2369 *data_cnt = 0;
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002370 goto release_iclog;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002371 }
2372
2373 *partial_copy = 0;
2374 *partial_copy_len = 0;
2375
2376 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
2377 /* no more space in this iclog - push it. */
Christoph Hellwig390aab02019-10-14 10:36:41 -07002378 spin_lock(&log->l_icloglock);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002379 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
2380 *record_cnt = 0;
2381 *data_cnt = 0;
2382
Christoph Hellwig69363992020-03-20 08:49:21 -07002383 if (iclog->ic_state == XLOG_STATE_ACTIVE)
2384 xlog_state_switch_iclogs(log, iclog, 0);
2385 else
2386 ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC ||
Dave Chinner5112e2062021-08-10 17:59:01 -07002387 xlog_is_shutdown(log));
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002388 if (!commit_iclog)
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002389 goto release_iclog;
2390 spin_unlock(&log->l_icloglock);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002391 ASSERT(flags & XLOG_COMMIT_TRANS);
2392 *commit_iclog = iclog;
2393 }
2394
2395 return 0;
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002396
2397release_iclog:
Dave Chinner0dc8f7f2021-07-27 16:23:48 -07002398 error = xlog_state_release_iclog(log, iclog, 0);
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002399 spin_unlock(&log->l_icloglock);
2400 return error;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002401}
2402
2403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * Write some region out to in-core log
2405 *
2406 * This will be called when writing externally provided regions or when
2407 * writing out a commit record for a given transaction.
2408 *
2409 * General algorithm:
2410 * 1. Find total length of this write. This may include adding to the
2411 * lengths passed in.
2412 * 2. Check whether we violate the tickets reservation.
2413 * 3. While writing to this iclog
2414 * A. Reserve as much space in this iclog as can get
2415 * B. If this is first write, save away start lsn
2416 * C. While writing this region:
2417 * 1. If first write of transaction, write start record
2418 * 2. Write log operation header (header per region)
2419 * 3. Find out if we can fit entire region into this iclog
2420 * 4. Potentially, verify destination memcpy ptr
2421 * 5. Memcpy (partial) region
2422 * 6. If partial copy, release iclog; otherwise, continue
2423 * copying more regions into current iclog
2424 * 4. Mark want sync bit (in simulation mode)
2425 * 5. Release iclog for potential flush to on-disk log.
2426 *
2427 * ERRORS:
2428 * 1. Panic if reservation is overrun. This should never happen since
2429 * reservation amounts are generated internal to the filesystem.
2430 * NOTES:
2431 * 1. Tickets are single threaded data structures.
2432 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
2433 * syncing routine. When a single log_write region needs to span
2434 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
2435 * on all log operation writes which don't contain the end of the
2436 * region. The XLOG_END_TRANS bit is used for the in-core log
2437 * operation which contains the end of the continued log_write region.
2438 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
2439 * we don't really know exactly how much space will be used. As a result,
2440 * we don't update ic_offset until the end when we know exactly how many
2441 * bytes have been written out.
2442 */
Dave Chinner71e330b2010-05-21 14:37:18 +10002443int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00002444xlog_write(
Mark Tinguelyad223e62012-06-14 09:22:15 -05002445 struct xlog *log,
Dave Chinner55b66332010-03-23 11:43:17 +11002446 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00002447 struct xlog_ticket *ticket,
2448 xfs_lsn_t *start_lsn,
2449 struct xlog_in_core **commit_iclog,
Dave Chinner3468bb12021-06-18 08:21:50 -07002450 uint optype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002452 struct xlog_in_core *iclog = NULL;
Dave Chinner9590e9c2020-03-25 18:18:21 -07002453 struct xfs_log_vec *lv = log_vector;
2454 struct xfs_log_iovec *vecp = lv->lv_iovecp;
2455 int index = 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002456 int len;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002457 int partial_copy = 0;
2458 int partial_copy_len = 0;
2459 int contwr = 0;
2460 int record_cnt = 0;
2461 int data_cnt = 0;
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002462 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002464 /*
Dave Chinner9590e9c2020-03-25 18:18:21 -07002465 * If this is a commit or unmount transaction, we don't need a start
2466 * record to be written. We do, however, have to account for the
2467 * commit or unmount header that gets written. Hence we always have
2468 * to account for an extra xlog_op_header here.
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002469 */
Dave Chinner9590e9c2020-03-25 18:18:21 -07002470 ticket->t_curr_res -= sizeof(struct xlog_op_header);
Brian Foster7d2d56532017-06-14 21:29:48 -07002471 if (ticket->t_curr_res < 0) {
2472 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
2473 "ctx ticket reservation ran out. Need to up reservation");
Dave Chinner55b66332010-03-23 11:43:17 +11002474 xlog_print_tic_res(log->l_mp, ticket);
Brian Foster7d2d56532017-06-14 21:29:48 -07002475 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
2476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Dave Chinner3468bb12021-06-18 08:21:50 -07002478 len = xlog_write_calc_vec_length(ticket, log_vector, optype);
2479 if (start_lsn)
2480 *start_lsn = 0;
Dave Chinnerfd638752013-06-27 16:04:51 +10002481 while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002482 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002483 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002485 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
2486 &contwr, &log_offset);
2487 if (error)
2488 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002490 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002491 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Dave Chinnereef983f2021-06-18 08:21:51 -07002493 /* Start_lsn is the first lsn written to. */
Dave Chinner3468bb12021-06-18 08:21:50 -07002494 if (start_lsn && !*start_lsn)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002495 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002497 /*
2498 * This loop writes out as many regions as can fit in the amount
2499 * of space which was allocated by xlog_state_get_iclog_space().
2500 */
Dave Chinnerfd638752013-06-27 16:04:51 +10002501 while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
2502 struct xfs_log_iovec *reg;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002503 struct xlog_op_header *ophdr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002504 int copy_len;
2505 int copy_off;
Dave Chinnerfd638752013-06-27 16:04:51 +10002506 bool ordered = false;
Dave Chinner3468bb12021-06-18 08:21:50 -07002507 bool wrote_start_rec = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Dave Chinnerfd638752013-06-27 16:04:51 +10002509 /* ordered log vectors have no regions to write */
2510 if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED) {
2511 ASSERT(lv->lv_niovecs == 0);
2512 ordered = true;
2513 goto next_lv;
2514 }
2515
2516 reg = &vecp[index];
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002517 ASSERT(reg->i_len % sizeof(int32_t) == 0);
2518 ASSERT((unsigned long)ptr % sizeof(int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Dave Chinner7ec94922020-03-25 18:18:20 -07002520 /*
2521 * Before we start formatting log vectors, we need to
2522 * write a start record. Only do this for the first
2523 * iclog we write to.
2524 */
Dave Chinner3468bb12021-06-18 08:21:50 -07002525 if (optype & XLOG_START_TRANS) {
Dave Chinner7ec94922020-03-25 18:18:20 -07002526 xlog_write_start_rec(ptr, ticket);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002527 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Dave Chinner7ec94922020-03-25 18:18:20 -07002528 sizeof(struct xlog_op_header));
Dave Chinner3468bb12021-06-18 08:21:50 -07002529 optype &= ~XLOG_START_TRANS;
2530 wrote_start_rec = true;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Dave Chinner3468bb12021-06-18 08:21:50 -07002533 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, optype);
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002534 if (!ophdr)
Dave Chinner24513372014-06-25 14:58:08 +10002535 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002537 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002538 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002540 len += xlog_write_setup_copy(ticket, ophdr,
2541 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11002542 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002543 &copy_off, &copy_len,
2544 &partial_copy,
2545 &partial_copy_len);
2546 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002547
Eric Sandeen91f9f5f2015-10-12 16:04:15 +11002548 /*
2549 * Copy region.
2550 *
2551 * Unmount records just log an opheader, so can have
2552 * empty payloads with no data region to copy. Hence we
2553 * only copy the payload if the vector says it has data
2554 * to copy.
2555 */
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002556 ASSERT(copy_len >= 0);
Eric Sandeen91f9f5f2015-10-12 16:04:15 +11002557 if (copy_len > 0) {
2558 memcpy(ptr, reg->i_addr + copy_off, copy_len);
2559 xlog_write_adv_cnt(&ptr, &len, &log_offset,
2560 copy_len);
2561 }
Dave Chinner7ec94922020-03-25 18:18:20 -07002562 copy_len += sizeof(struct xlog_op_header);
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002563 record_cnt++;
Dave Chinner3468bb12021-06-18 08:21:50 -07002564 if (wrote_start_rec) {
Dave Chinner7ec94922020-03-25 18:18:20 -07002565 copy_len += sizeof(struct xlog_op_header);
2566 record_cnt++;
Dave Chinner7ec94922020-03-25 18:18:20 -07002567 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002568 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Dave Chinner3468bb12021-06-18 08:21:50 -07002570 error = xlog_write_copy_finish(log, iclog, optype,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002571 &record_cnt, &data_cnt,
2572 &partial_copy,
2573 &partial_copy_len,
2574 log_offset,
2575 commit_iclog);
2576 if (error)
2577 return error;
2578
2579 /*
2580 * if we had a partial copy, we need to get more iclog
2581 * space but we don't want to increment the region
2582 * index because there is still more is this region to
2583 * write.
2584 *
2585 * If we completed writing this region, and we flushed
2586 * the iclog (indicated by resetting of the record
2587 * count), then we also need to get more log space. If
2588 * this was the last record, though, we are done and
2589 * can just return.
2590 */
2591 if (partial_copy)
2592 break;
2593
Dave Chinner55b66332010-03-23 11:43:17 +11002594 if (++index == lv->lv_niovecs) {
Dave Chinnerfd638752013-06-27 16:04:51 +10002595next_lv:
Dave Chinner55b66332010-03-23 11:43:17 +11002596 lv = lv->lv_next;
2597 index = 0;
2598 if (lv)
2599 vecp = lv->lv_iovecp;
2600 }
Thomas Meyer749f24f2017-10-09 11:38:54 -07002601 if (record_cnt == 0 && !ordered) {
Dave Chinner55b66332010-03-23 11:43:17 +11002602 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002603 return 0;
2604 break;
2605 }
2606 }
2607 }
2608
2609 ASSERT(len == 0);
2610
Christoph Hellwig390aab02019-10-14 10:36:41 -07002611 spin_lock(&log->l_icloglock);
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002612 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002613 if (commit_iclog) {
Dave Chinner3468bb12021-06-18 08:21:50 -07002614 ASSERT(optype & XLOG_COMMIT_TRANS);
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002615 *commit_iclog = iclog;
2616 } else {
Dave Chinner0dc8f7f2021-07-27 16:23:48 -07002617 error = xlog_state_release_iclog(log, iclog, 0);
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002618 }
Christoph Hellwig390aab02019-10-14 10:36:41 -07002619 spin_unlock(&log->l_icloglock);
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002620
Christoph Hellwigdf732b22019-10-14 10:36:41 -07002621 return error;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002624static void
2625xlog_state_activate_iclog(
2626 struct xlog_in_core *iclog,
2627 int *iclogs_changed)
2628{
2629 ASSERT(list_empty_careful(&iclog->ic_callbacks));
Dave Chinner956f6da2021-06-18 11:57:05 -07002630 trace_xlog_iclog_activate(iclog, _RET_IP_);
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002631
2632 /*
2633 * If the number of ops in this iclog indicate it just contains the
2634 * dummy transaction, we can change state into IDLE (the second time
2635 * around). Otherwise we should change the state into NEED a dummy.
2636 * We don't need to cover the dummy.
2637 */
2638 if (*iclogs_changed == 0 &&
2639 iclog->ic_header.h_num_logops == cpu_to_be32(XLOG_COVER_OPS)) {
2640 *iclogs_changed = 1;
2641 } else {
2642 /*
2643 * We have two dirty iclogs so start over. This could also be
2644 * num of ops indicating this is not the dummy going out.
2645 */
2646 *iclogs_changed = 2;
2647 }
2648
2649 iclog->ic_state = XLOG_STATE_ACTIVE;
2650 iclog->ic_offset = 0;
2651 iclog->ic_header.h_num_logops = 0;
2652 memset(iclog->ic_header.h_cycle_data, 0,
2653 sizeof(iclog->ic_header.h_cycle_data));
2654 iclog->ic_header.h_lsn = 0;
Dave Chinner9d110012021-07-28 17:14:11 -07002655 iclog->ic_header.h_tail_lsn = 0;
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002656}
2657
Dave Chinner0383f542019-09-05 17:32:52 -07002658/*
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002659 * Loop through all iclogs and mark all iclogs currently marked DIRTY as
2660 * ACTIVE after iclog I/O has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 */
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002662static void
2663xlog_state_activate_iclogs(
2664 struct xlog *log,
2665 int *iclogs_changed)
2666{
2667 struct xlog_in_core *iclog = log->l_iclog;
2668
2669 do {
2670 if (iclog->ic_state == XLOG_STATE_DIRTY)
2671 xlog_state_activate_iclog(iclog, iclogs_changed);
2672 /*
2673 * The ordering of marking iclogs ACTIVE must be maintained, so
2674 * an iclog doesn't become ACTIVE beyond one that is SYNCING.
2675 */
2676 else if (iclog->ic_state != XLOG_STATE_ACTIVE)
2677 break;
2678 } while ((iclog = iclog->ic_next) != log->l_iclog);
2679}
2680
2681static int
2682xlog_covered_state(
2683 int prev_state,
2684 int iclogs_changed)
2685{
2686 /*
Brian Fosterb0eb9e12021-01-22 16:48:22 -08002687 * We go to NEED for any non-covering writes. We go to NEED2 if we just
2688 * wrote the first covering record (DONE). We go to IDLE if we just
2689 * wrote the second covering record (DONE2) and remain in IDLE until a
2690 * non-covering write occurs.
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002691 */
2692 switch (prev_state) {
2693 case XLOG_STATE_COVER_IDLE:
Brian Fosterb0eb9e12021-01-22 16:48:22 -08002694 if (iclogs_changed == 1)
2695 return XLOG_STATE_COVER_IDLE;
Gustavo A. R. Silva53004ee2021-04-20 17:54:36 -05002696 fallthrough;
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002697 case XLOG_STATE_COVER_NEED:
2698 case XLOG_STATE_COVER_NEED2:
2699 break;
2700 case XLOG_STATE_COVER_DONE:
2701 if (iclogs_changed == 1)
2702 return XLOG_STATE_COVER_NEED2;
2703 break;
2704 case XLOG_STATE_COVER_DONE2:
2705 if (iclogs_changed == 1)
2706 return XLOG_STATE_COVER_IDLE;
2707 break;
2708 default:
2709 ASSERT(0);
2710 }
2711
2712 return XLOG_STATE_COVER_NEED;
2713}
2714
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002715STATIC void
Dave Chinner0383f542019-09-05 17:32:52 -07002716xlog_state_clean_iclog(
2717 struct xlog *log,
2718 struct xlog_in_core *dirty_iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002720 int iclogs_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Dave Chinner956f6da2021-06-18 11:57:05 -07002722 trace_xlog_iclog_clean(dirty_iclog, _RET_IP_);
2723
Christoph Hellwig57814642020-03-20 08:49:21 -07002724 dirty_iclog->ic_state = XLOG_STATE_DIRTY;
Dave Chinner0383f542019-09-05 17:32:52 -07002725
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002726 xlog_state_activate_iclogs(log, &iclogs_changed);
Dave Chinner0383f542019-09-05 17:32:52 -07002727 wake_up_all(&dirty_iclog->ic_force_wait);
2728
Christoph Hellwigc814b4f2020-03-20 08:49:20 -07002729 if (iclogs_changed) {
2730 log->l_covered_state = xlog_covered_state(log->l_covered_state,
2731 iclogs_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
Dave Chinner0383f542019-09-05 17:32:52 -07002733}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735STATIC xfs_lsn_t
2736xlog_get_lowest_lsn(
Christoph Hellwig9bff31322019-06-28 19:27:20 -07002737 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Christoph Hellwig9bff31322019-06-28 19:27:20 -07002739 struct xlog_in_core *iclog = log->l_iclog;
2740 xfs_lsn_t lowest_lsn = 0, lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 do {
Christoph Hellwig1858bb02019-10-14 10:36:43 -07002743 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2744 iclog->ic_state == XLOG_STATE_DIRTY)
Christoph Hellwig9bff31322019-06-28 19:27:20 -07002745 continue;
2746
2747 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2748 if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 lowest_lsn = lsn;
Christoph Hellwig9bff31322019-06-28 19:27:20 -07002750 } while ((iclog = iclog->ic_next) != log->l_iclog);
2751
Jesper Juhl014c2542006-01-15 02:37:08 +01002752 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
Dave Chinner65468182019-09-05 17:32:50 -07002755/*
Dave Chinner14e15f12019-09-05 17:32:52 -07002756 * Completion of a iclog IO does not imply that a transaction has completed, as
2757 * transactions can be large enough to span many iclogs. We cannot change the
2758 * tail of the log half way through a transaction as this may be the only
2759 * transaction in the log and moving the tail to point to the middle of it
2760 * will prevent recovery from finding the start of the transaction. Hence we
2761 * should only update the last_sync_lsn if this iclog contains transaction
2762 * completion callbacks on it.
2763 *
2764 * We have to do this before we drop the icloglock to ensure we are the only one
2765 * that can update it.
2766 *
2767 * If we are moving the last_sync_lsn forwards, we also need to ensure we kick
2768 * the reservation grant head pushing. This is due to the fact that the push
2769 * target is bound by the current last_sync_lsn value. Hence if we have a large
2770 * amount of log space bound up in this committing transaction then the
2771 * last_sync_lsn value may be the limiting factor preventing tail pushing from
2772 * freeing space in the log. Hence once we've updated the last_sync_lsn we
2773 * should push the AIL to ensure the push target (and hence the grant head) is
2774 * no longer bound by the old log head location and can move forwards and make
2775 * progress again.
2776 */
2777static void
2778xlog_state_set_callback(
2779 struct xlog *log,
2780 struct xlog_in_core *iclog,
2781 xfs_lsn_t header_lsn)
2782{
Dave Chinner956f6da2021-06-18 11:57:05 -07002783 trace_xlog_iclog_callback(iclog, _RET_IP_);
Dave Chinner14e15f12019-09-05 17:32:52 -07002784 iclog->ic_state = XLOG_STATE_CALLBACK;
2785
2786 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2787 header_lsn) <= 0);
2788
2789 if (list_empty_careful(&iclog->ic_callbacks))
2790 return;
2791
2792 atomic64_set(&log->l_last_sync_lsn, header_lsn);
2793 xlog_grant_push_ail(log, 0);
2794}
2795
2796/*
Dave Chinner5e96fa82019-09-05 17:32:51 -07002797 * Return true if we need to stop processing, false to continue to the next
2798 * iclog. The caller will need to run callbacks if the iclog is returned in the
2799 * XLOG_STATE_CALLBACK state.
2800 */
2801static bool
2802xlog_state_iodone_process_iclog(
2803 struct xlog *log,
Dave Chinner5112e2062021-08-10 17:59:01 -07002804 struct xlog_in_core *iclog)
Dave Chinner5e96fa82019-09-05 17:32:51 -07002805{
2806 xfs_lsn_t lowest_lsn;
Dave Chinner14e15f12019-09-05 17:32:52 -07002807 xfs_lsn_t header_lsn;
Dave Chinner5e96fa82019-09-05 17:32:51 -07002808
Christoph Hellwig1858bb02019-10-14 10:36:43 -07002809 switch (iclog->ic_state) {
2810 case XLOG_STATE_ACTIVE:
2811 case XLOG_STATE_DIRTY:
2812 /*
2813 * Skip all iclogs in the ACTIVE & DIRTY states:
2814 */
Dave Chinner5e96fa82019-09-05 17:32:51 -07002815 return false;
Christoph Hellwig1858bb02019-10-14 10:36:43 -07002816 case XLOG_STATE_DONE_SYNC:
Christoph Hellwig1858bb02019-10-14 10:36:43 -07002817 /*
Christoph Hellwig4b29ab02019-10-14 10:36:43 -07002818 * Now that we have an iclog that is in the DONE_SYNC state, do
2819 * one more check here to see if we have chased our tail around.
2820 * If this is not the lowest lsn iclog, then we will leave it
2821 * for another completion to process.
Christoph Hellwig1858bb02019-10-14 10:36:43 -07002822 */
2823 header_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2824 lowest_lsn = xlog_get_lowest_lsn(log);
2825 if (lowest_lsn && XFS_LSN_CMP(lowest_lsn, header_lsn) < 0)
2826 return false;
2827 xlog_state_set_callback(log, iclog, header_lsn);
2828 return false;
2829 default:
2830 /*
2831 * Can only perform callbacks in order. Since this iclog is not
Christoph Hellwig4b29ab02019-10-14 10:36:43 -07002832 * in the DONE_SYNC state, we skip the rest and just try to
2833 * clean up.
Christoph Hellwig1858bb02019-10-14 10:36:43 -07002834 */
Dave Chinner5e96fa82019-09-05 17:32:51 -07002835 return true;
2836 }
Dave Chinner5e96fa82019-09-05 17:32:51 -07002837}
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839STATIC void
2840xlog_state_do_callback(
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07002841 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Dave Chinner5e96fa82019-09-05 17:32:51 -07002843 struct xlog_in_core *iclog;
2844 struct xlog_in_core *first_iclog;
Dave Chinner5e96fa82019-09-05 17:32:51 -07002845 bool cycled_icloglock;
Dave Chinner5e96fa82019-09-05 17:32:51 -07002846 int flushcnt = 0;
2847 int repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002849 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 do {
2851 /*
2852 * Scan all iclogs starting with the one pointed to by the
2853 * log. Reset this starting point each time the log is
2854 * unlocked (during callbacks).
2855 *
2856 * Keep looping through iclogs until one full pass is made
2857 * without running any callbacks.
2858 */
Dave Chinner65468182019-09-05 17:32:50 -07002859 cycled_icloglock = false;
Dave Chinner5112e2062021-08-10 17:59:01 -07002860 first_iclog = log->l_iclog;
2861 iclog = first_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 do {
Dave Chinnera1bb8502021-06-25 11:21:01 -07002864 LIST_HEAD(cb_list);
2865
Dave Chinner5112e2062021-08-10 17:59:01 -07002866 if (!xlog_is_shutdown(log)) {
2867 if (xlog_state_iodone_process_iclog(log, iclog))
2868 break;
2869 if (iclog->ic_state != XLOG_STATE_CALLBACK) {
2870 iclog = iclog->ic_next;
2871 continue;
2872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Dave Chinnera1bb8502021-06-25 11:21:01 -07002874 list_splice_init(&iclog->ic_callbacks, &cb_list);
Dave Chinner6be00102021-06-25 11:21:00 -07002875 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Dave Chinnera1bb8502021-06-25 11:21:01 -07002877 trace_xlog_iclog_callbacks_start(iclog, _RET_IP_);
2878 xlog_cil_process_committed(&cb_list);
2879 trace_xlog_iclog_callbacks_done(iclog, _RET_IP_);
Dave Chinner65468182019-09-05 17:32:50 -07002880 cycled_icloglock = true;
Dave Chinner6be00102021-06-25 11:21:00 -07002881
2882 spin_lock(&log->l_icloglock);
Dave Chinner2039a272021-08-10 17:59:01 -07002883 if (xlog_is_shutdown(log))
Christoph Hellwig57814642020-03-20 08:49:21 -07002884 wake_up_all(&iclog->ic_force_wait);
2885 else
2886 xlog_state_clean_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 iclog = iclog->ic_next;
Dave Chinner5112e2062021-08-10 17:59:01 -07002888 } while (iclog != first_iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002889
Dave Chinner5112e2062021-08-10 17:59:01 -07002890 if (++repeats > 5000) {
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002891 flushcnt += repeats;
2892 repeats = 0;
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002893 xfs_warn(log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002894 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002895 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
Dave Chinner5112e2062021-08-10 17:59:01 -07002897 } while (!xlog_is_shutdown(log) && cycled_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Christoph Hellwig1858bb02019-10-14 10:36:43 -07002899 if (log->l_iclog->ic_state == XLOG_STATE_ACTIVE ||
Dave Chinner5112e2062021-08-10 17:59:01 -07002900 xlog_is_shutdown(log))
Dave Chinnereb40a872010-12-21 12:09:01 +11002901 wake_up_all(&log->l_flush_wait);
Rik van Rielcdea5452019-09-05 17:32:48 -07002902
2903 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906
2907/*
2908 * Finish transitioning this iclog to the dirty state.
2909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002911 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 */
David Chinnera8272ce2007-11-23 16:28:09 +11002913STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914xlog_state_done_syncing(
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07002915 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Christoph Hellwigd15cbf22019-06-28 19:27:30 -07002917 struct xlog *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002919 spin_lock(&log->l_icloglock);
David Chinner155cc6b2008-03-06 13:44:14 +11002920 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Dave Chinner956f6da2021-06-18 11:57:05 -07002921 trace_xlog_iclog_sync_done(iclog, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 /*
2924 * If we got an error, either on the first buffer, or in the case of
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07002925 * split log writes, on the second, we shut down the file system and
2926 * no iclogs should ever be attempted to be written to disk again.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
Dave Chinner2039a272021-08-10 17:59:01 -07002928 if (!xlog_is_shutdown(log)) {
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07002929 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 iclog->ic_state = XLOG_STATE_DONE_SYNC;
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07002931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 /*
2934 * Someone could be sleeping prior to writing out the next
2935 * iclog buffer, we wake them all, one will get to do the
2936 * I/O, the others get to wait for the result.
2937 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002938 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002939 spin_unlock(&log->l_icloglock);
Dave Chinnerb8432992020-03-25 18:18:24 -07002940 xlog_state_do_callback(log);
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07002941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943/*
2944 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002945 * sleep. We wait on the flush queue on the head iclog as that should be
2946 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2947 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 *
2949 * The in-core logs are used in a circular fashion. They are not used
2950 * out-of-order even when an iclog past the head is free.
2951 *
2952 * return:
2953 * * log_offset where xlog_write() can start writing into the in-core
2954 * log's data space.
2955 * * in-core log pointer to which xlog_write() should write.
2956 * * boolean indicating this is a continued write to an in-core log.
2957 * If this is the last write, then the in-core log's offset field
2958 * needs to be incremented, depending on the amount of data which
2959 * is copied.
2960 */
David Chinnera8272ce2007-11-23 16:28:09 +11002961STATIC int
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002962xlog_state_get_iclog_space(
2963 struct xlog *log,
2964 int len,
2965 struct xlog_in_core **iclogp,
2966 struct xlog_ticket *ticket,
2967 int *continued_write,
2968 int *logoffsetp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 int log_offset;
2971 xlog_rec_header_t *head;
2972 xlog_in_core_t *iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002975 spin_lock(&log->l_icloglock);
Dave Chinner2039a272021-08-10 17:59:01 -07002976 if (xlog_is_shutdown(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002977 spin_unlock(&log->l_icloglock);
Dave Chinner24513372014-06-25 14:58:08 +10002978 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980
2981 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002982 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002983 XFS_STATS_INC(log->l_mp, xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002984
2985 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002986 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 goto restart;
2988 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 head = &iclog->ic_header;
2991
David Chinner155cc6b2008-03-06 13:44:14 +11002992 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 log_offset = iclog->ic_offset;
2994
Dave Chinner956f6da2021-06-18 11:57:05 -07002995 trace_xlog_iclog_get_space(iclog, _RET_IP_);
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 /* On the 1st write to an iclog, figure out lsn. This works
2998 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2999 * committing to. If the offset is set, that's how many blocks
3000 * must be written.
3001 */
3002 if (log_offset == 0) {
3003 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10003004 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003005 log->l_iclog_hsize,
3006 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003007 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
3008 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003009 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 ASSERT(log->l_curr_block >= 0);
3011 }
3012
3013 /* If there is enough room to write everything, then do it. Otherwise,
3014 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
3015 * bit is on, so this will get flushed out. Don't update ic_offset
3016 * until you know exactly how many bytes get copied. Therefore, wait
3017 * until later to update ic_offset.
3018 *
3019 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
3020 * can fit into remaining data section.
3021 */
3022 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
Christoph Hellwigdf732b22019-10-14 10:36:41 -07003023 int error = 0;
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
3026
Dave Chinner49641f12008-07-11 17:43:55 +10003027 /*
Christoph Hellwigdf732b22019-10-14 10:36:41 -07003028 * If we are the only one writing to this iclog, sync it to
3029 * disk. We need to do an atomic compare and decrement here to
3030 * avoid racing with concurrent atomic_dec_and_lock() calls in
Dave Chinner49641f12008-07-11 17:43:55 +10003031 * xlog_state_release_iclog() when there is more than one
3032 * reference to the iclog.
3033 */
Christoph Hellwigdf732b22019-10-14 10:36:41 -07003034 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1))
Dave Chinner0dc8f7f2021-07-27 16:23:48 -07003035 error = xlog_state_release_iclog(log, iclog, 0);
Christoph Hellwigdf732b22019-10-14 10:36:41 -07003036 spin_unlock(&log->l_icloglock);
3037 if (error)
3038 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 goto restart;
3040 }
3041
3042 /* Do we have enough room to write the full amount in the remainder
3043 * of this iclog? Or must we continue a write on the next iclog and
3044 * mark this iclog as completely taken? In the case where we switch
3045 * iclogs (to mark it taken), this particular iclog will release/sync
3046 * to disk in xlog_write().
3047 */
3048 if (len <= iclog->ic_size - iclog->ic_offset) {
3049 *continued_write = 0;
3050 iclog->ic_offset += len;
3051 } else {
3052 *continued_write = 1;
3053 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
3054 }
3055 *iclogp = iclog;
3056
3057 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003058 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060 *logoffsetp = log_offset;
3061 return 0;
Dave Chinnerb8432992020-03-25 18:18:24 -07003062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003064/*
Dave Chinnerb8432992020-03-25 18:18:24 -07003065 * The first cnt-1 times a ticket goes through here we don't need to move the
3066 * grant write head because the permanent reservation has reserved cnt times the
3067 * unit amount. Release part of current permanent unit reservation and reset
3068 * current reservation to be one units worth. Also move grant reservation head
3069 * forward.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 */
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003071void
3072xfs_log_ticket_regrant(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003073 struct xlog *log,
3074 struct xlog_ticket *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003076 trace_xfs_log_ticket_regrant(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (ticket->t_cnt > 0)
3079 ticket->t_cnt--;
3080
Christoph Hellwig28496962012-02-20 02:31:25 +00003081 xlog_grant_sub_space(log, &log->l_reserve_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11003082 ticket->t_curr_res);
Christoph Hellwig28496962012-02-20 02:31:25 +00003083 xlog_grant_sub_space(log, &log->l_write_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11003084 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10003086 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003087
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003088 trace_xfs_log_ticket_regrant_sub(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 /* just return if we still have some of the pre-reserved space */
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003091 if (!ticket->t_cnt) {
3092 xlog_grant_add_space(log, &log->l_reserve_head.grant,
3093 ticket->t_unit_res);
3094 trace_xfs_log_ticket_regrant_exit(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003096 ticket->t_curr_res = ticket->t_unit_res;
3097 xlog_tic_reset_res(ticket);
3098 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003099
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003100 xfs_log_ticket_put(ticket);
3101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103/*
3104 * Give back the space left from a reservation.
3105 *
3106 * All the information we need to make a correct determination of space left
3107 * is present. For non-permanent reservations, things are quite easy. The
3108 * count should have been decremented to zero. We only need to deal with the
3109 * space remaining in the current reservation part of the ticket. If the
3110 * ticket contains a permanent reservation, there may be left over space which
3111 * needs to be released. A count of N means that N-1 refills of the current
3112 * reservation can be done before we need to ask for more space. The first
3113 * one goes to fill up the first current reservation. Once we run out of
3114 * space, the count will stay at zero and the only space remaining will be
3115 * in the current reservation field.
3116 */
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003117void
3118xfs_log_ticket_ungrant(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003119 struct xlog *log,
3120 struct xlog_ticket *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003122 int bytes;
3123
3124 trace_xfs_log_ticket_ungrant(log, ticket);
Dave Chinner663e4962010-12-21 12:06:05 +11003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (ticket->t_cnt > 0)
3127 ticket->t_cnt--;
3128
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003129 trace_xfs_log_ticket_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Dave Chinner663e4962010-12-21 12:06:05 +11003131 /*
3132 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 * up more space based on the remaining count.
3134 */
Dave Chinner663e4962010-12-21 12:06:05 +11003135 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (ticket->t_cnt > 0) {
3137 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11003138 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
3140
Christoph Hellwig28496962012-02-20 02:31:25 +00003141 xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
3142 xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11003143
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003144 trace_xfs_log_ticket_ungrant_exit(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003145
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +00003146 xfs_log_space_wake(log->l_mp);
Christoph Hellwig8b41e3f2020-03-25 18:18:23 -07003147 xfs_log_ticket_put(ticket);
Christoph Hellwig09a423a2012-02-20 02:31:20 +00003148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150/*
Dave Chinnerb8432992020-03-25 18:18:24 -07003151 * This routine will mark the current iclog in the ring as WANT_SYNC and move
3152 * the current iclog pointer to the next iclog in the ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 */
3154STATIC void
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003155xlog_state_switch_iclogs(
3156 struct xlog *log,
3157 struct xlog_in_core *iclog,
3158 int eventual_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
3160 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
Christoph Hellwig69363992020-03-20 08:49:21 -07003161 assert_spin_locked(&log->l_icloglock);
Dave Chinner956f6da2021-06-18 11:57:05 -07003162 trace_xlog_iclog_switch(iclog, _RET_IP_);
Christoph Hellwig69363992020-03-20 08:49:21 -07003163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (!eventual_size)
3165 eventual_size = iclog->ic_offset;
3166 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003167 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 log->l_prev_block = log->l_curr_block;
3169 log->l_prev_cycle = log->l_curr_cycle;
3170
3171 /* roll log?: ic_offset changed later */
3172 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
3173
3174 /* Round up to next log-sunit */
Dave Chinnera6a65fe2021-06-18 08:21:48 -07003175 if (log->l_iclog_roundoff > BBSIZE) {
Geert Uytterhoeven18842e02021-06-18 08:24:04 -07003176 uint32_t sunit_bb = BTOBB(log->l_iclog_roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
3178 }
3179
3180 if (log->l_curr_block >= log->l_logBBsize) {
Brian Fostera45086e2015-10-12 15:59:25 +11003181 /*
3182 * Rewind the current block before the cycle is bumped to make
3183 * sure that the combined LSN never transiently moves forward
3184 * when the log wraps to the next cycle. This is to support the
3185 * unlocked sample of these fields from xlog_valid_lsn(). Most
3186 * other cases should acquire l_icloglock.
3187 */
3188 log->l_curr_block -= log->l_logBBsize;
3189 ASSERT(log->l_curr_block >= 0);
3190 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 log->l_curr_cycle++;
3192 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
3193 log->l_curr_cycle++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
3195 ASSERT(iclog == log->l_iclog);
3196 log->l_iclog = iclog->ic_next;
Dave Chinnerb8432992020-03-25 18:18:24 -07003197}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199/*
Dave Chinner8191d822021-07-27 16:23:49 -07003200 * Force the iclog to disk and check if the iclog has been completed before
3201 * xlog_force_iclog() returns. This can happen on synchronous (e.g.
3202 * pmem) or fast async storage because we drop the icloglock to issue the IO.
3203 * If completion has already occurred, tell the caller so that it can avoid an
3204 * unnecessary wait on the iclog.
3205 */
3206static int
3207xlog_force_and_check_iclog(
3208 struct xlog_in_core *iclog,
3209 bool *completed)
3210{
3211 xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header.h_lsn);
3212 int error;
3213
3214 *completed = false;
3215 error = xlog_force_iclog(iclog);
3216 if (error)
3217 return error;
3218
3219 /*
3220 * If the iclog has already been completed and reused the header LSN
3221 * will have been rewritten by completion
3222 */
3223 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn)
3224 *completed = true;
3225 return 0;
3226}
3227
3228/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 * Write out all data in the in-core log as of this exact moment in time.
3230 *
3231 * Data may be written to the in-core log during this call. However,
3232 * we don't guarantee this data will be written out. A change from past
3233 * implementation means this routine will *not* write out zero length LRs.
3234 *
3235 * Basically, we try and perform an intelligent scan of the in-core logs.
3236 * If we determine there is no flushable data, we just return. There is no
3237 * flushable data if:
3238 *
3239 * 1. the current iclog is active and has no data; the previous iclog
3240 * is in the active or dirty state.
3241 * 2. the current iclog is drity, and the previous iclog is in the
3242 * active or dirty state.
3243 *
David Chinner12017fa2008-08-13 16:34:31 +10003244 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 *
3246 * 1. the current iclog is not in the active nor dirty state.
3247 * 2. the current iclog dirty, and the previous iclog is not in the
3248 * active nor dirty state.
3249 * 3. the current iclog is active, and there is another thread writing
3250 * to this particular iclog.
3251 * 4. a) the current iclog is active and has no other writers
3252 * b) when we return from flushing out this iclog, it is still
3253 * not in the active nor dirty state.
3254 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003255int
Christoph Hellwig60e5bb72018-03-13 23:15:28 -07003256xfs_log_force(
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003257 struct xfs_mount *mp,
Christoph Hellwig60e5bb72018-03-13 23:15:28 -07003258 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
Mark Tinguelyad223e62012-06-14 09:22:15 -05003260 struct xlog *log = mp->m_log;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003261 struct xlog_in_core *iclog;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003262
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003263 XFS_STATS_INC(mp, xs_log_force);
Christoph Hellwig60e5bb72018-03-13 23:15:28 -07003264 trace_xfs_log_force(mp, 0, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003266 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10003267
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003268 spin_lock(&log->l_icloglock);
Dave Chinner5112e2062021-08-10 17:59:01 -07003269 if (xlog_is_shutdown(log))
Christoph Hellwige6b965702018-03-13 23:15:29 -07003270 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Dave Chinner5112e2062021-08-10 17:59:01 -07003272 iclog = log->l_iclog;
Dave Chinner956f6da2021-06-18 11:57:05 -07003273 trace_xlog_iclog_force(iclog, _RET_IP_);
3274
Christoph Hellwige6b965702018-03-13 23:15:29 -07003275 if (iclog->ic_state == XLOG_STATE_DIRTY ||
3276 (iclog->ic_state == XLOG_STATE_ACTIVE &&
3277 atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 /*
Christoph Hellwige6b965702018-03-13 23:15:29 -07003279 * If the head is dirty or (active and empty), then we need to
3280 * look at the previous iclog.
3281 *
3282 * If the previous iclog is active or dirty we are done. There
3283 * is nothing to sync out. Otherwise, we attach ourselves to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 * previous iclog and go to sleep.
3285 */
Christoph Hellwige6b965702018-03-13 23:15:29 -07003286 iclog = iclog->ic_prev;
Christoph Hellwige6b965702018-03-13 23:15:29 -07003287 } else if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3288 if (atomic_read(&iclog->ic_refcnt) == 0) {
Dave Chinner45eddb42021-07-27 16:23:48 -07003289 /* We have exclusive access to this iclog. */
Dave Chinner8191d822021-07-27 16:23:49 -07003290 bool completed;
3291
3292 if (xlog_force_and_check_iclog(iclog, &completed))
Christoph Hellwigdf732b22019-10-14 10:36:41 -07003293 goto out_error;
Christoph Hellwige6b965702018-03-13 23:15:29 -07003294
Dave Chinner8191d822021-07-27 16:23:49 -07003295 if (completed)
Christoph Hellwige6b965702018-03-13 23:15:29 -07003296 goto out_unlock;
3297 } else {
3298 /*
Dave Chinner2bf1ec02021-07-27 16:23:49 -07003299 * Someone else is still writing to this iclog, so we
3300 * need to ensure that when they release the iclog it
3301 * gets synced immediately as we may be waiting on it.
Christoph Hellwige6b965702018-03-13 23:15:29 -07003302 */
3303 xlog_state_switch_iclogs(log, iclog, 0);
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
Christoph Hellwige6b965702018-03-13 23:15:29 -07003306
Dave Chinner2bf1ec02021-07-27 16:23:49 -07003307 /*
3308 * The iclog we are about to wait on may contain the checkpoint pushed
3309 * by the above xlog_cil_force() call, but it may not have been pushed
3310 * to disk yet. Like the ACTIVE case above, we need to make sure caches
3311 * are flushed when this iclog is written.
3312 */
3313 if (iclog->ic_state == XLOG_STATE_WANT_SYNC)
3314 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
3315
Christoph Hellwig81e5b502020-03-20 08:49:18 -07003316 if (flags & XFS_LOG_SYNC)
3317 return xlog_wait_on_iclog(iclog);
Christoph Hellwige6b965702018-03-13 23:15:29 -07003318out_unlock:
3319 spin_unlock(&log->l_icloglock);
3320 return 0;
3321out_error:
3322 spin_unlock(&log->l_icloglock);
3323 return -EIO;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003326static int
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003327xlog_force_lsn(
3328 struct xlog *log,
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003329 xfs_lsn_t lsn,
3330 uint flags,
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003331 int *log_flushed,
3332 bool already_slept)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003334 struct xlog_in_core *iclog;
Dave Chinner8191d822021-07-27 16:23:49 -07003335 bool completed;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003336
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003337 spin_lock(&log->l_icloglock);
Dave Chinner5112e2062021-08-10 17:59:01 -07003338 if (xlog_is_shutdown(log))
Christoph Hellwig93806292018-03-13 23:15:29 -07003339 goto out_error;
3340
Dave Chinner5112e2062021-08-10 17:59:01 -07003341 iclog = log->l_iclog;
Christoph Hellwig93806292018-03-13 23:15:29 -07003342 while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
Dave Chinner956f6da2021-06-18 11:57:05 -07003343 trace_xlog_iclog_force_lsn(iclog, _RET_IP_);
Christoph Hellwig93806292018-03-13 23:15:29 -07003344 iclog = iclog->ic_next;
3345 if (iclog == log->l_iclog)
3346 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
3348
Dave Chinner2bf1ec02021-07-27 16:23:49 -07003349 switch (iclog->ic_state) {
3350 case XLOG_STATE_ACTIVE:
Christoph Hellwig93806292018-03-13 23:15:29 -07003351 /*
3352 * We sleep here if we haven't already slept (e.g. this is the
3353 * first time we've looked at the correct iclog buf) and the
3354 * buffer before us is going to be sync'ed. The reason for this
3355 * is that if we are doing sync transactions here, by waiting
3356 * for the previous I/O to complete, we can allow a few more
3357 * transactions into this iclog before we close it down.
3358 *
3359 * Otherwise, we mark the buffer WANT_SYNC, and bump up the
3360 * refcnt so we can release the log (which drops the ref count).
3361 * The state switch keeps new transaction commits from using
3362 * this buffer. When the current commits finish writing into
3363 * the buffer, the refcount will drop to zero and the buffer
3364 * will go out then.
3365 */
3366 if (!already_slept &&
Christoph Hellwig1858bb02019-10-14 10:36:43 -07003367 (iclog->ic_prev->ic_state == XLOG_STATE_WANT_SYNC ||
3368 iclog->ic_prev->ic_state == XLOG_STATE_SYNCING)) {
Christoph Hellwig93806292018-03-13 23:15:29 -07003369 xlog_wait(&iclog->ic_prev->ic_write_wait,
3370 &log->l_icloglock);
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003371 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
Dave Chinner8191d822021-07-27 16:23:49 -07003373 if (xlog_force_and_check_iclog(iclog, &completed))
Christoph Hellwigdf732b22019-10-14 10:36:41 -07003374 goto out_error;
Christoph Hellwig93806292018-03-13 23:15:29 -07003375 if (log_flushed)
3376 *log_flushed = 1;
Dave Chinner8191d822021-07-27 16:23:49 -07003377 if (completed)
3378 goto out_unlock;
Dave Chinner2bf1ec02021-07-27 16:23:49 -07003379 break;
3380 case XLOG_STATE_WANT_SYNC:
3381 /*
3382 * This iclog may contain the checkpoint pushed by the
3383 * xlog_cil_force_seq() call, but there are other writers still
3384 * accessing it so it hasn't been pushed to disk yet. Like the
3385 * ACTIVE case above, we need to make sure caches are flushed
3386 * when this iclog is written.
3387 */
3388 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
3389 break;
3390 default:
3391 /*
3392 * The entire checkpoint was written by the CIL force and is on
3393 * its way to disk already. It will be stable when it
3394 * completes, so we don't need to manipulate caches here at all.
3395 * We just need to wait for completion if necessary.
3396 */
3397 break;
Christoph Hellwig93806292018-03-13 23:15:29 -07003398 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003399
Christoph Hellwig81e5b502020-03-20 08:49:18 -07003400 if (flags & XFS_LOG_SYNC)
3401 return xlog_wait_on_iclog(iclog);
Christoph Hellwig93806292018-03-13 23:15:29 -07003402out_unlock:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003403 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return 0;
Christoph Hellwig93806292018-03-13 23:15:29 -07003405out_error:
3406 spin_unlock(&log->l_icloglock);
3407 return -EIO;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003410/*
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003411 * Force the in-core log to disk for a specific LSN.
3412 *
3413 * Find in-core log with lsn.
3414 * If it is in the DIRTY state, just return.
3415 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3416 * state and go to sleep or return.
3417 * If it is in any other state, go to sleep or return.
3418 *
3419 * Synchronous forces are implemented with a wait queue. All callers trying
3420 * to force a given lsn to disk must wait on the queue attached to the
3421 * specific in-core log. When given in-core log finally completes its write
3422 * to disk, that thread will wake up all threads waiting on the queue.
3423 */
3424int
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003425xfs_log_force_seq(
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003426 struct xfs_mount *mp,
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003427 xfs_csn_t seq,
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003428 uint flags,
3429 int *log_flushed)
3430{
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003431 struct xlog *log = mp->m_log;
3432 xfs_lsn_t lsn;
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003433 int ret;
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003434 ASSERT(seq != 0);
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003435
3436 XFS_STATS_INC(mp, xs_log_force);
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003437 trace_xfs_log_force(mp, seq, _RET_IP_);
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003438
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003439 lsn = xlog_cil_force_seq(log, seq);
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003440 if (lsn == NULLCOMMITLSN)
3441 return 0;
3442
Dave Chinner5f9b4b02021-06-18 08:21:52 -07003443 ret = xlog_force_lsn(log, lsn, flags, log_flushed, false);
3444 if (ret == -EAGAIN) {
3445 XFS_STATS_INC(mp, xs_log_force_sleep);
3446 ret = xlog_force_lsn(log, lsn, flags, log_flushed, true);
3447 }
Christoph Hellwig3e4da462018-03-13 23:15:30 -07003448 return ret;
3449}
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003452 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003454void
3455xfs_log_ticket_put(
3456 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003458 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003459 if (atomic_dec_and_test(&ticket->t_ref))
Carlos Maiolino377bcd52019-11-14 12:43:04 -08003460 kmem_cache_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003463xlog_ticket_t *
3464xfs_log_ticket_get(
3465 xlog_ticket_t *ticket)
3466{
3467 ASSERT(atomic_read(&ticket->t_ref) > 0);
3468 atomic_inc(&ticket->t_ref);
3469 return ticket;
3470}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472/*
Jie Liue773fc92013-08-12 20:50:01 +10003473 * Figure out the total log space unit (in bytes) that would be
3474 * required for a log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 */
Dave Chinnera6a65fe2021-06-18 08:21:48 -07003476static int
3477xlog_calc_unit_res(
3478 struct xlog *log,
Jie Liue773fc92013-08-12 20:50:01 +10003479 int unit_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Jie Liue773fc92013-08-12 20:50:01 +10003481 int iclog_space;
3482 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484 /*
3485 * Permanent reservations have up to 'cnt'-1 active log operations
3486 * in the log. A unit in this case is the amount of space for one
3487 * of these log operations. Normal reservations have a cnt of 1
3488 * and their unit amount is the total amount of space required.
3489 *
3490 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003491 * which occupy space in the on-disk log.
3492 *
3493 * Normal form of a transaction is:
3494 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3495 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3496 *
3497 * We need to account for all the leadup data and trailer data
3498 * around the transaction data.
3499 * And then we need to account for the worst case in terms of using
3500 * more space.
3501 * The worst case will happen if:
3502 * - the placement of the transaction happens to be such that the
3503 * roundoff is at its maximum
3504 * - the transaction data is synced before the commit record is synced
3505 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3506 * Therefore the commit record is in its own Log Record.
3507 * This can happen as the commit record is called with its
3508 * own region to xlog_write().
3509 * This then means that in the worst case, roundoff can happen for
3510 * the commit-rec as well.
3511 * The commit-rec is smaller than padding in this scenario and so it is
3512 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 */
3514
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003515 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003517 unit_bytes += sizeof(xfs_trans_header_t);
3518
3519 /* for start-rec */
3520 unit_bytes += sizeof(xlog_op_header_t);
3521
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003522 /*
3523 * for LR headers - the space for data in an iclog is the size minus
3524 * the space used for the headers. If we use the iclog size, then we
3525 * undercalculate the number of headers required.
3526 *
3527 * Furthermore - the addition of op headers for split-recs might
3528 * increase the space required enough to require more log and op
3529 * headers, so take that into account too.
3530 *
3531 * IMPORTANT: This reservation makes the assumption that if this
3532 * transaction is the first in an iclog and hence has the LR headers
3533 * accounted to it, then the remaining space in the iclog is
3534 * exclusively for this transaction. i.e. if the transaction is larger
3535 * than the iclog, it will be the only thing in that iclog.
3536 * Fundamentally, this means we must pass the entire log vector to
3537 * xlog_write to guarantee this.
3538 */
3539 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3540 num_headers = howmany(unit_bytes, iclog_space);
3541
3542 /* for split-recs - ophdrs added when data split over LRs */
3543 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3544
3545 /* add extra header reservations if we overrun */
3546 while (!num_headers ||
3547 howmany(unit_bytes, iclog_space) > num_headers) {
3548 unit_bytes += sizeof(xlog_op_header_t);
3549 num_headers++;
3550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 unit_bytes += log->l_iclog_hsize * num_headers;
3552
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003553 /* for commit-rec LR header - note: padding will subsume the ophdr */
3554 unit_bytes += log->l_iclog_hsize;
3555
Dave Chinnera6a65fe2021-06-18 08:21:48 -07003556 /* roundoff padding for transaction data and one for commit record */
3557 unit_bytes += 2 * log->l_iclog_roundoff;
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003558
Jie Liue773fc92013-08-12 20:50:01 +10003559 return unit_bytes;
3560}
3561
Dave Chinnera6a65fe2021-06-18 08:21:48 -07003562int
3563xfs_log_calc_unit_res(
3564 struct xfs_mount *mp,
3565 int unit_bytes)
3566{
3567 return xlog_calc_unit_res(mp->m_log, unit_bytes);
3568}
3569
Jie Liue773fc92013-08-12 20:50:01 +10003570/*
3571 * Allocate and initialise a new log ticket.
3572 */
3573struct xlog_ticket *
3574xlog_ticket_alloc(
3575 struct xlog *log,
3576 int unit_bytes,
3577 int cnt,
3578 char client,
Carlos Maiolinoca4f2582020-07-22 09:23:17 -07003579 bool permanent)
Jie Liue773fc92013-08-12 20:50:01 +10003580{
3581 struct xlog_ticket *tic;
3582 int unit_res;
3583
Carlos Maiolinoca4f2582020-07-22 09:23:17 -07003584 tic = kmem_cache_zalloc(xfs_log_ticket_zone, GFP_NOFS | __GFP_NOFAIL);
Jie Liue773fc92013-08-12 20:50:01 +10003585
Dave Chinnera6a65fe2021-06-18 08:21:48 -07003586 unit_res = xlog_calc_unit_res(log, unit_bytes);
Jie Liue773fc92013-08-12 20:50:01 +10003587
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003588 atomic_set(&tic->t_ref, 1);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003589 tic->t_task = current;
Dave Chinner10547942010-12-21 12:02:25 +11003590 INIT_LIST_HEAD(&tic->t_queue);
Jie Liue773fc92013-08-12 20:50:01 +10003591 tic->t_unit_res = unit_res;
3592 tic->t_curr_res = unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 tic->t_cnt = cnt;
3594 tic->t_ocnt = cnt;
Akinobu Mitaecb34032013-03-04 21:58:20 +09003595 tic->t_tid = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 tic->t_clientid = client;
Christoph Hellwig9006fb92012-02-20 02:31:31 +00003597 if (permanent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Christoph Hellwig0adba532007-08-30 17:21:46 +10003600 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003605#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606/*
3607 * Make sure that the destination ptr is within the valid data region of
3608 * one of the iclogs. This uses backup pointers stored in a different
3609 * part of the log in case we trash the log structure.
3610 */
Christoph Hellwig181fdfe2017-11-06 11:54:02 -08003611STATIC void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003612xlog_verify_dest_ptr(
Mark Tinguelyad223e62012-06-14 09:22:15 -05003613 struct xlog *log,
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10003614 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 int i;
3617 int good_ptr = 0;
3618
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003619 for (i = 0; i < log->l_iclog_bufs; i++) {
3620 if (ptr >= log->l_iclog_bak[i] &&
3621 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 good_ptr++;
3623 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003624
3625 if (!good_ptr)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003626 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003629/*
3630 * Check to make sure the grant write head didn't just over lap the tail. If
3631 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3632 * the cycles differ by exactly one and check the byte count.
3633 *
3634 * This check is run unlocked, so can give false positives. Rather than assert
3635 * on failures, use a warn-once flag and a panic tag to allow the admin to
3636 * determine if they want to panic the machine when such an error occurs. For
3637 * debug kernels this will have the same effect as using an assert but, unlinke
3638 * an assert, it can be turned off at runtime.
3639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640STATIC void
Dave Chinner3f336c62010-12-21 12:02:52 +11003641xlog_verify_grant_tail(
Mark Tinguelyad223e62012-06-14 09:22:15 -05003642 struct xlog *log)
Dave Chinner3f336c62010-12-21 12:02:52 +11003643{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003644 int tail_cycle, tail_blocks;
Dave Chinnera69ed032010-12-21 12:08:20 +11003645 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003646
Christoph Hellwig28496962012-02-20 02:31:25 +00003647 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003648 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3649 if (tail_cycle != cycle) {
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003650 if (cycle - 1 != tail_cycle &&
3651 !(log->l_flags & XLOG_TAIL_WARN)) {
3652 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3653 "%s: cycle - 1 != tail_cycle", __func__);
3654 log->l_flags |= XLOG_TAIL_WARN;
3655 }
3656
3657 if (space > BBTOB(tail_blocks) &&
3658 !(log->l_flags & XLOG_TAIL_WARN)) {
3659 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3660 "%s: space > BBTOB(tail_blocks)", __func__);
3661 log->l_flags |= XLOG_TAIL_WARN;
3662 }
Dave Chinner3f336c62010-12-21 12:02:52 +11003663 }
3664}
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666/* check if it will fit */
3667STATIC void
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003668xlog_verify_tail_lsn(
3669 struct xlog *log,
Dave Chinner9d110012021-07-28 17:14:11 -07003670 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Dave Chinner9d110012021-07-28 17:14:11 -07003672 xfs_lsn_t tail_lsn = be64_to_cpu(iclog->ic_header.h_tail_lsn);
3673 int blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
3675 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3676 blocks =
3677 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3678 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003679 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 } else {
3681 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3682
3683 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003684 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3687 if (blocks < BTOBB(iclog->ic_offset) + 1)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003688 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 }
Dave Chinnerb8432992020-03-25 18:18:24 -07003690}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692/*
3693 * Perform a number of checks on the iclog before writing to disk.
3694 *
3695 * 1. Make sure the iclogs are still circular
3696 * 2. Make sure we have a good magic number
3697 * 3. Make sure we don't have magic numbers in the data
3698 * 4. Check fields of each log operation header for:
3699 * A. Valid client identifier
3700 * B. tid ptr value falls in valid ptr space (user space code)
3701 * C. Length in log record header is correct according to the
3702 * individual operation headers within record.
3703 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3704 * log, check the preceding blocks of the physical log to make sure all
3705 * the cycle numbers agree with the current cycle number.
3706 */
3707STATIC void
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003708xlog_verify_iclog(
3709 struct xlog *log,
3710 struct xlog_in_core *iclog,
Christoph Hellwigabca1f32019-06-28 19:27:24 -07003711 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
3713 xlog_op_header_t *ophead;
3714 xlog_in_core_t *icptr;
3715 xlog_in_core_2_t *xhdr;
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10003716 void *base_ptr, *ptr, *p;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +10003717 ptrdiff_t field_offset;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07003718 uint8_t clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 int len, i, j, k, op_len;
3720 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003723 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 icptr = log->l_iclog;
Geyslan G. Bem643f7c42013-10-30 16:01:00 -05003725 for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next)
3726 ASSERT(icptr);
3727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 if (icptr != log->l_iclog)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003729 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003730 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
3732 /* check log magic numbers */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003733 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003734 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10003736 base_ptr = ptr = &iclog->ic_header;
3737 p = &iclog->ic_header;
3738 for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003739 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003740 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3741 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
3744 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003745 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10003746 base_ptr = ptr = iclog->ic_datap;
3747 ophead = ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003748 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 for (i = 0; i < len; i++) {
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10003750 ophead = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 /* clientid is only 1 byte */
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10003753 p = &ophead->oh_clientid;
3754 field_offset = p - base_ptr;
Christoph Hellwigabca1f32019-06-28 19:27:24 -07003755 if (field_offset & 0x1ff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 clientid = ophead->oh_clientid;
3757 } else {
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10003758 idx = BTOBBT((char *)&ophead->oh_clientid - iclog->ic_datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3760 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3761 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003762 clientid = xlog_get_client_id(
3763 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003765 clientid = xlog_get_client_id(
3766 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768 }
3769 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003770 xfs_warn(log->l_mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003771 "%s: invalid clientid %d op "PTR_FMT" offset 0x%lx",
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003772 __func__, clientid, ophead,
3773 (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 /* check length */
Christoph Hellwig5809d5e2015-06-22 09:44:47 +10003776 p = &ophead->oh_len;
3777 field_offset = p - base_ptr;
Christoph Hellwigabca1f32019-06-28 19:27:24 -07003778 if (field_offset & 0x1ff) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003779 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 } else {
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +10003781 idx = BTOBBT((uintptr_t)&ophead->oh_len -
3782 (uintptr_t)iclog->ic_datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3784 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3785 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003786 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003788 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790 }
3791 ptr += sizeof(xlog_op_header_t) + op_len;
3792 }
Dave Chinnerb8432992020-03-25 18:18:24 -07003793}
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003794#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 * This is called from xfs_force_shutdown, when we're forcibly
3798 * shutting down the filesystem, typically because of an IO error.
3799 * Our main objectives here are to make sure that:
Dave Chinnera870fe6d2014-10-02 09:02:28 +10003800 * a. if !logerror, flush the logs to disk. Anything modified
3801 * after this is ignored.
3802 * b. the filesystem gets marked 'SHUTDOWN' for all interested
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 * parties to find out, 'atomically'.
Dave Chinnera870fe6d2014-10-02 09:02:28 +10003804 * c. those who're sleeping on log reservations, pinned objects and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 * other resources get woken up, and be told the bad news.
Dave Chinnera870fe6d2014-10-02 09:02:28 +10003806 * d. nothing new gets queued up after (b) and (c) are done.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003807 *
Dave Chinnera870fe6d2014-10-02 09:02:28 +10003808 * Note: for the !logerror case we need to flush the regions held in memory out
3809 * to disk first. This needs to be done before the log is marked as shutdown,
3810 * otherwise the iclog writes will fail.
Dave Chinner5112e2062021-08-10 17:59:01 -07003811 *
3812 * Return non-zero if log shutdown transition had already happened.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 */
3814int
3815xfs_log_force_umount(
3816 struct xfs_mount *mp,
3817 int logerror)
3818{
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003819 struct xlog *log;
Dave Chinner5112e2062021-08-10 17:59:01 -07003820 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 log = mp->m_log;
3823
3824 /*
3825 * If this happens during log recovery, don't worry about
3826 * locking; the log isn't open for business yet.
3827 */
3828 if (!log ||
3829 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3830 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003831 if (mp->m_sb_bp)
Dave Chinnerb0388bf2016-02-10 15:01:11 +11003832 mp->m_sb_bp->b_flags |= XBF_DONE;
Jesper Juhl014c2542006-01-15 02:37:08 +01003833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 }
3835
3836 /*
3837 * Somebody could've already done the hard work for us.
3838 * No need to get locks for this.
3839 */
Dave Chinner5112e2062021-08-10 17:59:01 -07003840 if (logerror && xlog_is_shutdown(log))
Jesper Juhl014c2542006-01-15 02:37:08 +01003841 return 1;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003842
3843 /*
Dave Chinnera870fe6d2014-10-02 09:02:28 +10003844 * Flush all the completed transactions to disk before marking the log
3845 * being shut down. We need to do it in this order to ensure that
3846 * completed operations are safely on disk before we shut down, and that
3847 * we don't have to issue any buffer IO after the shutdown flags are set
3848 * to guarantee this.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003849 */
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003850 if (!logerror)
Christoph Hellwig60e5bb72018-03-13 23:15:28 -07003851 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 /*
Dave Chinner3f16b982010-12-21 12:29:01 +11003854 * mark the filesystem and the as in a shutdown state and wake
3855 * everybody up to tell them the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003857 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003859 if (mp->m_sb_bp)
Dave Chinnerb0388bf2016-02-10 15:01:11 +11003860 mp->m_sb_bp->b_flags |= XBF_DONE;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 /*
Dave Chinnera870fe6d2014-10-02 09:02:28 +10003863 * Mark the log and the iclogs with IO error flags to prevent any
3864 * further log IO from being issued or completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 */
Dave Chinner5112e2062021-08-10 17:59:01 -07003866 if (!(log->l_flags & XLOG_IO_ERROR)) {
3867 log->l_flags |= XLOG_IO_ERROR;
3868 retval = 1;
3869 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003870 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 /*
Dave Chinner10547942010-12-21 12:02:25 +11003873 * We don't want anybody waiting for log reservations after this. That
3874 * means we have to wake up everybody queued up on reserveq as well as
3875 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3876 * we don't enqueue anything once the SHUTDOWN flag is set, and this
Dave Chinner3f16b982010-12-21 12:29:01 +11003877 * action is protected by the grant locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 */
Christoph Hellwiga79bf2d2012-02-20 02:31:27 +00003879 xlog_grant_head_wake_all(&log->l_reserve_head);
3880 xlog_grant_head_wake_all(&log->l_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 /*
Dave Chinnerac983512014-05-07 08:05:50 +10003883 * Wake up everybody waiting on xfs_log_force. Wake the CIL push first
3884 * as if the log writes were completed. The abort handling in the log
3885 * item committed callback functions will do this again under lock to
3886 * avoid races.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 */
Rik van Rielcdea5452019-09-05 17:32:48 -07003888 spin_lock(&log->l_cilp->xc_push_lock);
Dave Chinnerac983512014-05-07 08:05:50 +10003889 wake_up_all(&log->l_cilp->xc_commit_wait);
Rik van Rielcdea5452019-09-05 17:32:48 -07003890 spin_unlock(&log->l_cilp->xc_push_lock);
Christoph Hellwig12e6a0f2020-03-20 08:49:20 -07003891 xlog_state_do_callback(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Jesper Juhl014c2542006-01-15 02:37:08 +01003893 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003896STATIC int
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003897xlog_iclogs_empty(
3898 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899{
3900 xlog_in_core_t *iclog;
3901
3902 iclog = log->l_iclog;
3903 do {
3904 /* endianness does not matter here, zero is zero in
3905 * any language.
3906 */
3907 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 iclog = iclog->ic_next;
3910 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003911 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
Dave Chinnerf661f1e2012-10-08 21:56:02 +11003913
Brian Fostera45086e2015-10-12 15:59:25 +11003914/*
3915 * Verify that an LSN stamped into a piece of metadata is valid. This is
3916 * intended for use in read verifiers on v5 superblocks.
3917 */
3918bool
3919xfs_log_check_lsn(
3920 struct xfs_mount *mp,
3921 xfs_lsn_t lsn)
3922{
3923 struct xlog *log = mp->m_log;
3924 bool valid;
3925
3926 /*
3927 * norecovery mode skips mount-time log processing and unconditionally
3928 * resets the in-core LSN. We can't validate in this mode, but
3929 * modifications are not allowed anyways so just return true.
3930 */
3931 if (mp->m_flags & XFS_MOUNT_NORECOVERY)
3932 return true;
3933
3934 /*
3935 * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is
3936 * handled by recovery and thus safe to ignore here.
3937 */
3938 if (lsn == NULLCOMMITLSN)
3939 return true;
3940
3941 valid = xlog_valid_lsn(mp->m_log, lsn);
3942
3943 /* warn the user about what's gone wrong before verifier failure */
3944 if (!valid) {
3945 spin_lock(&log->l_icloglock);
3946 xfs_warn(mp,
3947"Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). "
3948"Please unmount and run xfs_repair (>= v4.3) to resolve.",
3949 CYCLE_LSN(lsn), BLOCK_LSN(lsn),
3950 log->l_curr_cycle, log->l_curr_block);
3951 spin_unlock(&log->l_icloglock);
3952 }
3953
3954 return valid;
3955}
Darrick J. Wong0c60d3a2018-08-01 07:40:48 -07003956
3957bool
3958xfs_log_in_recovery(
3959 struct xfs_mount *mp)
3960{
3961 struct xlog *log = mp->m_log;
3962
3963 return log->l_flags & XLOG_ACTIVE_RECOVERY;
3964}
Darrick J. Wong2b73a2c2021-08-08 08:27:12 -07003965
3966/*
3967 * Notify the log that we're about to start using a feature that is protected
3968 * by a log incompat feature flag. This will prevent log covering from
3969 * clearing those flags.
3970 */
3971void
3972xlog_use_incompat_feat(
3973 struct xlog *log)
3974{
3975 down_read(&log->l_incompat_users);
3976}
3977
3978/* Notify the log that we've finished using log incompat features. */
3979void
3980xlog_drop_incompat_feat(
3981 struct xlog *log)
3982{
3983 up_read(&log->l_incompat_users);
3984}