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