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