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