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