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