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