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