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