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