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