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