]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/xfs_log.c
xfs: reindent xlog_write
[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
b5203cd0
DC
1632/*
1633 * Calculate the potential space needed by the log vector. Each region gets
1634 * its own xlog_op_header_t and may need to be double word aligned.
1635 */
1636static int
1637xlog_write_calc_vec_length(
1638 struct xlog_ticket *ticket,
1639 struct xfs_log_iovec reg[],
1640 int nentries)
1641{
1642 int headers = 0;
1643 int len = 0;
1644 int i;
1645
1646 /* acct for start rec of xact */
1647 if (ticket->t_flags & XLOG_TIC_INITED)
1648 headers++;
1649
1650 for (i = 0; i < nentries; i++) {
1651 /* each region gets >= 1 */
1652 headers++;
1653
1654 len += reg[i].i_len;
1655 xlog_tic_add_region(ticket, reg[i].i_len, reg[i].i_type);
1656 }
1657
1658 ticket->t_res_num_ophdrs += headers;
1659 len += headers * sizeof(struct xlog_op_header);
1660
1661 return len;
1662}
1663
1664/*
1665 * If first write for transaction, insert start record We can't be trying to
1666 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1667 */
1668static int
1669xlog_write_start_rec(
1670 __psint_t ptr,
1671 struct xlog_ticket *ticket)
1672{
1673 struct xlog_op_header *ophdr = (struct xlog_op_header *)ptr;
1674
1675 if (!(ticket->t_flags & XLOG_TIC_INITED))
1676 return 0;
1677
1678 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1679 ophdr->oh_clientid = ticket->t_clientid;
1680 ophdr->oh_len = 0;
1681 ophdr->oh_flags = XLOG_START_TRANS;
1682 ophdr->oh_res2 = 0;
1683
1684 ticket->t_flags &= ~XLOG_TIC_INITED;
1685
1686 return sizeof(struct xlog_op_header);
1687}
1688
1689static xlog_op_header_t *
1690xlog_write_setup_ophdr(
1691 struct log *log,
1692 __psint_t ptr,
1693 struct xlog_ticket *ticket,
1694 uint flags)
1695{
1696 struct xlog_op_header *ophdr = (struct xlog_op_header *)ptr;
1697
1698 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1699 ophdr->oh_clientid = ticket->t_clientid;
1700 ophdr->oh_res2 = 0;
1701
1702 /* are we copying a commit or unmount record? */
1703 ophdr->oh_flags = flags;
1704
1705 /*
1706 * We've seen logs corrupted with bad transaction client ids. This
1707 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1708 * and shut down the filesystem.
1709 */
1710 switch (ophdr->oh_clientid) {
1711 case XFS_TRANSACTION:
1712 case XFS_VOLUME:
1713 case XFS_LOG:
1714 break;
1715 default:
1716 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1717 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1718 ophdr->oh_clientid, ticket);
1719 return NULL;
1720 }
1721
1722 return ophdr;
1723}
1724
1725/*
1726 * Set up the parameters of the region copy into the log. This has
1727 * to handle region write split across multiple log buffers - this
1728 * state is kept external to this function so that this code can
1729 * can be written in an obvious, self documenting manner.
1730 */
1731static int
1732xlog_write_setup_copy(
1733 struct xlog_ticket *ticket,
1734 struct xlog_op_header *ophdr,
1735 int space_available,
1736 int space_required,
1737 int *copy_off,
1738 int *copy_len,
1739 int *last_was_partial_copy,
1740 int *bytes_consumed)
1741{
1742 int still_to_copy;
1743
1744 still_to_copy = space_required - *bytes_consumed;
1745 *copy_off = *bytes_consumed;
1746
1747 if (still_to_copy <= space_available) {
1748 /* write of region completes here */
1749 *copy_len = still_to_copy;
1750 ophdr->oh_len = cpu_to_be32(*copy_len);
1751 if (*last_was_partial_copy)
1752 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1753 *last_was_partial_copy = 0;
1754 *bytes_consumed = 0;
1755 return 0;
1756 }
1757
1758 /* partial write of region, needs extra log op header reservation */
1759 *copy_len = space_available;
1760 ophdr->oh_len = cpu_to_be32(*copy_len);
1761 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1762 if (*last_was_partial_copy)
1763 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1764 *bytes_consumed += *copy_len;
1765 (*last_was_partial_copy)++;
1766
1767 /* account for new log op header */
1768 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1769 ticket->t_res_num_ophdrs++;
1770
1771 return sizeof(struct xlog_op_header);
1772}
1773
1774static int
1775xlog_write_copy_finish(
1776 struct log *log,
1777 struct xlog_in_core *iclog,
1778 uint flags,
1779 int *record_cnt,
1780 int *data_cnt,
1781 int *partial_copy,
1782 int *partial_copy_len,
1783 int log_offset,
1784 struct xlog_in_core **commit_iclog)
1785{
1786 if (*partial_copy) {
1787 /*
1788 * This iclog has already been marked WANT_SYNC by
1789 * xlog_state_get_iclog_space.
1790 */
1791 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1792 *record_cnt = 0;
1793 *data_cnt = 0;
1794 return xlog_state_release_iclog(log, iclog);
1795 }
1796
1797 *partial_copy = 0;
1798 *partial_copy_len = 0;
1799
1800 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1801 /* no more space in this iclog - push it. */
1802 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1803 *record_cnt = 0;
1804 *data_cnt = 0;
1805
1806 spin_lock(&log->l_icloglock);
1807 xlog_state_want_sync(log, iclog);
1808 spin_unlock(&log->l_icloglock);
1809
1810 if (!commit_iclog)
1811 return xlog_state_release_iclog(log, iclog);
1812 ASSERT(flags & XLOG_COMMIT_TRANS);
1813 *commit_iclog = iclog;
1814 }
1815
1816 return 0;
1817}
1818
1da177e4
LT
1819/*
1820 * Write some region out to in-core log
1821 *
1822 * This will be called when writing externally provided regions or when
1823 * writing out a commit record for a given transaction.
1824 *
1825 * General algorithm:
1826 * 1. Find total length of this write. This may include adding to the
1827 * lengths passed in.
1828 * 2. Check whether we violate the tickets reservation.
1829 * 3. While writing to this iclog
1830 * A. Reserve as much space in this iclog as can get
1831 * B. If this is first write, save away start lsn
1832 * C. While writing this region:
1833 * 1. If first write of transaction, write start record
1834 * 2. Write log operation header (header per region)
1835 * 3. Find out if we can fit entire region into this iclog
1836 * 4. Potentially, verify destination memcpy ptr
1837 * 5. Memcpy (partial) region
1838 * 6. If partial copy, release iclog; otherwise, continue
1839 * copying more regions into current iclog
1840 * 4. Mark want sync bit (in simulation mode)
1841 * 5. Release iclog for potential flush to on-disk log.
1842 *
1843 * ERRORS:
1844 * 1. Panic if reservation is overrun. This should never happen since
1845 * reservation amounts are generated internal to the filesystem.
1846 * NOTES:
1847 * 1. Tickets are single threaded data structures.
1848 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1849 * syncing routine. When a single log_write region needs to span
1850 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1851 * on all log operation writes which don't contain the end of the
1852 * region. The XLOG_END_TRANS bit is used for the in-core log
1853 * operation which contains the end of the continued log_write region.
1854 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1855 * we don't really know exactly how much space will be used. As a result,
1856 * we don't update ic_offset until the end when we know exactly how many
1857 * bytes have been written out.
1858 */
a8272ce0 1859STATIC int
35a8a72f
CH
1860xlog_write(
1861 struct xfs_mount *mp,
1862 struct xfs_log_iovec reg[],
1863 int nentries,
1864 struct xlog_ticket *ticket,
1865 xfs_lsn_t *start_lsn,
1866 struct xlog_in_core **commit_iclog,
1867 uint flags)
1da177e4 1868{
99428ad0
CH
1869 struct log *log = mp->m_log;
1870 struct xlog_in_core *iclog = NULL;
1871 int len;
1872 int index;
1873 int partial_copy = 0;
1874 int partial_copy_len = 0;
1875 int contwr = 0;
1876 int record_cnt = 0;
1877 int data_cnt = 0;
1878 int error;
1879
1880 *start_lsn = 0;
1881
1882 len = xlog_write_calc_vec_length(ticket, reg, nentries);
1883 if (ticket->t_curr_res < len) {
1884 xlog_print_tic_res(mp, ticket);
1da177e4 1885#ifdef DEBUG
99428ad0
CH
1886 xlog_panic(
1887 "xfs_log_write: reservation ran out. Need to up reservation");
1da177e4 1888#else
99428ad0
CH
1889 /* Customer configurable panic */
1890 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1891 "xfs_log_write: reservation ran out. Need to up reservation");
1892
1893 /* If we did not panic, shutdown the filesystem */
1894 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1da177e4 1895#endif
99428ad0
CH
1896 }
1897
1da177e4
LT
1898 ticket->t_curr_res -= len;
1899
99428ad0
CH
1900 for (index = 0; index < nentries; ) {
1901 __psint_t ptr;
1902 int log_offset;
1da177e4 1903
99428ad0
CH
1904 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1905 &contwr, &log_offset);
1906 if (error)
1907 return error;
1da177e4 1908
99428ad0
CH
1909 ASSERT(log_offset <= iclog->ic_size - 1);
1910 ptr = (__psint_t)((char *)iclog->ic_datap + log_offset);
1da177e4 1911
99428ad0
CH
1912 /* start_lsn is the first lsn written to. That's all we need. */
1913 if (!*start_lsn)
1914 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
b5203cd0 1915
99428ad0
CH
1916 /*
1917 * This loop writes out as many regions as can fit in the amount
1918 * of space which was allocated by xlog_state_get_iclog_space().
1919 */
1920 while (index < nentries) {
1921 struct xlog_op_header *ophdr;
1922 int start_rec_copy;
1923 int copy_len;
1924 int copy_off;
1925
1926 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1927 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1928
1929 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1930 if (start_rec_copy) {
1931 record_cnt++;
1932 xlog_write_adv_cnt(ptr, len, log_offset,
1933 start_rec_copy);
1934 }
b5203cd0 1935
99428ad0
CH
1936 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1937 if (!ophdr)
1938 return XFS_ERROR(EIO);
1939
1940 xlog_write_adv_cnt(ptr, len, log_offset,
1941 sizeof(struct xlog_op_header));
1942
1943 len += xlog_write_setup_copy(ticket, ophdr,
1944 iclog->ic_size-log_offset,
1945 reg[index].i_len,
1946 &copy_off, &copy_len,
1947 &partial_copy,
1948 &partial_copy_len);
1949 xlog_verify_dest_ptr(log, ptr);
1950
1951 /* copy region */
1952 ASSERT(copy_len >= 0);
1953 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off,
1954 copy_len);
1955 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1956
1957 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1958 record_cnt++;
1959 data_cnt += contwr ? copy_len : 0;
1960
1961 error = xlog_write_copy_finish(log, iclog, flags,
1962 &record_cnt, &data_cnt,
1963 &partial_copy,
1964 &partial_copy_len,
1965 log_offset,
1966 commit_iclog);
1967 if (error)
1968 return error;
1969
1970 /*
1971 * if we had a partial copy, we need to get more iclog
1972 * space but we don't want to increment the region
1973 * index because there is still more is this region to
1974 * write.
1975 *
1976 * If we completed writing this region, and we flushed
1977 * the iclog (indicated by resetting of the record
1978 * count), then we also need to get more log space. If
1979 * this was the last record, though, we are done and
1980 * can just return.
1981 */
1982 if (partial_copy)
1983 break;
1984
1985 index++;
1986 if (record_cnt == 0) {
1987 if (index == nentries)
1988 return 0;
1989 break;
1990 }
1991 }
1992 }
1993
1994 ASSERT(len == 0);
1995
1996 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1997 if (!commit_iclog)
1998 return xlog_state_release_iclog(log, iclog);
1da177e4 1999
1da177e4
LT
2000 ASSERT(flags & XLOG_COMMIT_TRANS);
2001 *commit_iclog = iclog;
2002 return 0;
99428ad0 2003}
1da177e4
LT
2004
2005
2006/*****************************************************************************
2007 *
2008 * State Machine functions
2009 *
2010 *****************************************************************************
2011 */
2012
2013/* Clean iclogs starting from the head. This ordering must be
2014 * maintained, so an iclog doesn't become ACTIVE beyond one that
2015 * is SYNCING. This is also required to maintain the notion that we use
12017faf 2016 * a ordered wait queue to hold off would be writers to the log when every
1da177e4
LT
2017 * iclog is trying to sync to disk.
2018 *
2019 * State Change: DIRTY -> ACTIVE
2020 */
ba0f32d4 2021STATIC void
1da177e4
LT
2022xlog_state_clean_log(xlog_t *log)
2023{
2024 xlog_in_core_t *iclog;
2025 int changed = 0;
2026
2027 iclog = log->l_iclog;
2028 do {
2029 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2030 iclog->ic_state = XLOG_STATE_ACTIVE;
2031 iclog->ic_offset = 0;
114d23aa 2032 ASSERT(iclog->ic_callback == NULL);
1da177e4
LT
2033 /*
2034 * If the number of ops in this iclog indicate it just
2035 * contains the dummy transaction, we can
2036 * change state into IDLE (the second time around).
2037 * Otherwise we should change the state into
2038 * NEED a dummy.
2039 * We don't need to cover the dummy.
2040 */
2041 if (!changed &&
b53e675d
CH
2042 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2043 XLOG_COVER_OPS)) {
1da177e4
LT
2044 changed = 1;
2045 } else {
2046 /*
2047 * We have two dirty iclogs so start over
2048 * This could also be num of ops indicates
2049 * this is not the dummy going out.
2050 */
2051 changed = 2;
2052 }
2053 iclog->ic_header.h_num_logops = 0;
2054 memset(iclog->ic_header.h_cycle_data, 0,
2055 sizeof(iclog->ic_header.h_cycle_data));
2056 iclog->ic_header.h_lsn = 0;
2057 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2058 /* do nothing */;
2059 else
2060 break; /* stop cleaning */
2061 iclog = iclog->ic_next;
2062 } while (iclog != log->l_iclog);
2063
2064 /* log is locked when we are called */
2065 /*
2066 * Change state for the dummy log recording.
2067 * We usually go to NEED. But we go to NEED2 if the changed indicates
2068 * we are done writing the dummy record.
2069 * If we are done with the second dummy recored (DONE2), then
2070 * we go to IDLE.
2071 */
2072 if (changed) {
2073 switch (log->l_covered_state) {
2074 case XLOG_STATE_COVER_IDLE:
2075 case XLOG_STATE_COVER_NEED:
2076 case XLOG_STATE_COVER_NEED2:
2077 log->l_covered_state = XLOG_STATE_COVER_NEED;
2078 break;
2079
2080 case XLOG_STATE_COVER_DONE:
2081 if (changed == 1)
2082 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2083 else
2084 log->l_covered_state = XLOG_STATE_COVER_NEED;
2085 break;
2086
2087 case XLOG_STATE_COVER_DONE2:
2088 if (changed == 1)
2089 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2090 else
2091 log->l_covered_state = XLOG_STATE_COVER_NEED;
2092 break;
2093
2094 default:
2095 ASSERT(0);
2096 }
2097 }
2098} /* xlog_state_clean_log */
2099
2100STATIC xfs_lsn_t
2101xlog_get_lowest_lsn(
2102 xlog_t *log)
2103{
2104 xlog_in_core_t *lsn_log;
2105 xfs_lsn_t lowest_lsn, lsn;
2106
2107 lsn_log = log->l_iclog;
2108 lowest_lsn = 0;
2109 do {
2110 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
b53e675d 2111 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
1da177e4
LT
2112 if ((lsn && !lowest_lsn) ||
2113 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2114 lowest_lsn = lsn;
2115 }
2116 }
2117 lsn_log = lsn_log->ic_next;
2118 } while (lsn_log != log->l_iclog);
014c2544 2119 return lowest_lsn;
1da177e4
LT
2120}
2121
2122
2123STATIC void
2124xlog_state_do_callback(
2125 xlog_t *log,
2126 int aborted,
2127 xlog_in_core_t *ciclog)
2128{
2129 xlog_in_core_t *iclog;
2130 xlog_in_core_t *first_iclog; /* used to know when we've
2131 * processed all iclogs once */
2132 xfs_log_callback_t *cb, *cb_next;
2133 int flushcnt = 0;
2134 xfs_lsn_t lowest_lsn;
2135 int ioerrors; /* counter: iclogs with errors */
2136 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2137 int funcdidcallbacks; /* flag: function did callbacks */
2138 int repeats; /* for issuing console warnings if
2139 * looping too many times */
d748c623 2140 int wake = 0;
1da177e4 2141
b22cd72c 2142 spin_lock(&log->l_icloglock);
1da177e4
LT
2143 first_iclog = iclog = log->l_iclog;
2144 ioerrors = 0;
2145 funcdidcallbacks = 0;
2146 repeats = 0;
2147
2148 do {
2149 /*
2150 * Scan all iclogs starting with the one pointed to by the
2151 * log. Reset this starting point each time the log is
2152 * unlocked (during callbacks).
2153 *
2154 * Keep looping through iclogs until one full pass is made
2155 * without running any callbacks.
2156 */
2157 first_iclog = log->l_iclog;
2158 iclog = log->l_iclog;
2159 loopdidcallbacks = 0;
2160 repeats++;
2161
2162 do {
2163
2164 /* skip all iclogs in the ACTIVE & DIRTY states */
2165 if (iclog->ic_state &
2166 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2167 iclog = iclog->ic_next;
2168 continue;
2169 }
2170
2171 /*
2172 * Between marking a filesystem SHUTDOWN and stopping
2173 * the log, we do flush all iclogs to disk (if there
2174 * wasn't a log I/O error). So, we do want things to
2175 * go smoothly in case of just a SHUTDOWN w/o a
2176 * LOG_IO_ERROR.
2177 */
2178 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2179 /*
2180 * Can only perform callbacks in order. Since
2181 * this iclog is not in the DONE_SYNC/
2182 * DO_CALLBACK state, we skip the rest and
2183 * just try to clean up. If we set our iclog
2184 * to DO_CALLBACK, we will not process it when
2185 * we retry since a previous iclog is in the
2186 * CALLBACK and the state cannot change since
b22cd72c 2187 * we are holding the l_icloglock.
1da177e4
LT
2188 */
2189 if (!(iclog->ic_state &
2190 (XLOG_STATE_DONE_SYNC |
2191 XLOG_STATE_DO_CALLBACK))) {
2192 if (ciclog && (ciclog->ic_state ==
2193 XLOG_STATE_DONE_SYNC)) {
2194 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2195 }
2196 break;
2197 }
2198 /*
2199 * We now have an iclog that is in either the
2200 * DO_CALLBACK or DONE_SYNC states. The other
2201 * states (WANT_SYNC, SYNCING, or CALLBACK were
2202 * caught by the above if and are going to
2203 * clean (i.e. we aren't doing their callbacks)
2204 * see the above if.
2205 */
2206
2207 /*
2208 * We will do one more check here to see if we
2209 * have chased our tail around.
2210 */
2211
2212 lowest_lsn = xlog_get_lowest_lsn(log);
b53e675d
CH
2213 if (lowest_lsn &&
2214 XFS_LSN_CMP(lowest_lsn,
2215 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
1da177e4
LT
2216 iclog = iclog->ic_next;
2217 continue; /* Leave this iclog for
2218 * another thread */
2219 }
2220
2221 iclog->ic_state = XLOG_STATE_CALLBACK;
2222
b22cd72c 2223 spin_unlock(&log->l_icloglock);
1da177e4
LT
2224
2225 /* l_last_sync_lsn field protected by
c8b5ea28 2226 * l_grant_lock. Don't worry about iclog's lsn.
1da177e4
LT
2227 * No one else can be here except us.
2228 */
c8b5ea28 2229 spin_lock(&log->l_grant_lock);
b53e675d
CH
2230 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2231 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2232 log->l_last_sync_lsn =
2233 be64_to_cpu(iclog->ic_header.h_lsn);
c8b5ea28 2234 spin_unlock(&log->l_grant_lock);
1da177e4 2235
1da177e4 2236 } else {
114d23aa 2237 spin_unlock(&log->l_icloglock);
1da177e4
LT
2238 ioerrors++;
2239 }
1da177e4 2240
114d23aa
DC
2241 /*
2242 * Keep processing entries in the callback list until
2243 * we come around and it is empty. We need to
2244 * atomically see that the list is empty and change the
2245 * state to DIRTY so that we don't miss any more
2246 * callbacks being added.
2247 */
2248 spin_lock(&iclog->ic_callback_lock);
2249 cb = iclog->ic_callback;
4b80916b 2250 while (cb) {
1da177e4
LT
2251 iclog->ic_callback_tail = &(iclog->ic_callback);
2252 iclog->ic_callback = NULL;
114d23aa 2253 spin_unlock(&iclog->ic_callback_lock);
1da177e4
LT
2254
2255 /* perform callbacks in the order given */
4b80916b 2256 for (; cb; cb = cb_next) {
1da177e4
LT
2257 cb_next = cb->cb_next;
2258 cb->cb_func(cb->cb_arg, aborted);
2259 }
114d23aa 2260 spin_lock(&iclog->ic_callback_lock);
1da177e4
LT
2261 cb = iclog->ic_callback;
2262 }
2263
2264 loopdidcallbacks++;
2265 funcdidcallbacks++;
2266
114d23aa 2267 spin_lock(&log->l_icloglock);
4b80916b 2268 ASSERT(iclog->ic_callback == NULL);
114d23aa 2269 spin_unlock(&iclog->ic_callback_lock);
1da177e4
LT
2270 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2271 iclog->ic_state = XLOG_STATE_DIRTY;
2272
2273 /*
2274 * Transition from DIRTY to ACTIVE if applicable.
2275 * NOP if STATE_IOERROR.
2276 */
2277 xlog_state_clean_log(log);
2278
2279 /* wake up threads waiting in xfs_log_force() */
12017faf 2280 sv_broadcast(&iclog->ic_force_wait);
1da177e4
LT
2281
2282 iclog = iclog->ic_next;
2283 } while (first_iclog != iclog);
a3c6685e
NS
2284
2285 if (repeats > 5000) {
2286 flushcnt += repeats;
2287 repeats = 0;
1da177e4 2288 xfs_fs_cmn_err(CE_WARN, log->l_mp,
a3c6685e 2289 "%s: possible infinite loop (%d iterations)",
34a622b2 2290 __func__, flushcnt);
1da177e4
LT
2291 }
2292 } while (!ioerrors && loopdidcallbacks);
2293
2294 /*
2295 * make one last gasp attempt to see if iclogs are being left in
2296 * limbo..
2297 */
2298#ifdef DEBUG
2299 if (funcdidcallbacks) {
2300 first_iclog = iclog = log->l_iclog;
2301 do {
2302 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2303 /*
2304 * Terminate the loop if iclogs are found in states
2305 * which will cause other threads to clean up iclogs.
2306 *
2307 * SYNCING - i/o completion will go through logs
2308 * DONE_SYNC - interrupt thread should be waiting for
b22cd72c 2309 * l_icloglock
1da177e4
LT
2310 * IOERROR - give up hope all ye who enter here
2311 */
2312 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2313 iclog->ic_state == XLOG_STATE_SYNCING ||
2314 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2315 iclog->ic_state == XLOG_STATE_IOERROR )
2316 break;
2317 iclog = iclog->ic_next;
2318 } while (first_iclog != iclog);
2319 }
2320#endif
2321
d748c623
MW
2322 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2323 wake = 1;
b22cd72c 2324 spin_unlock(&log->l_icloglock);
d748c623
MW
2325
2326 if (wake)
2327 sv_broadcast(&log->l_flush_wait);
2328}
1da177e4
LT
2329
2330
2331/*
2332 * Finish transitioning this iclog to the dirty state.
2333 *
2334 * Make sure that we completely execute this routine only when this is
2335 * the last call to the iclog. There is a good chance that iclog flushes,
2336 * when we reach the end of the physical log, get turned into 2 separate
2337 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2338 * routine. By using the reference count bwritecnt, we guarantee that only
2339 * the second completion goes through.
2340 *
2341 * Callbacks could take time, so they are done outside the scope of the
12017faf 2342 * global state machine log lock.
1da177e4 2343 */
a8272ce0 2344STATIC void
1da177e4
LT
2345xlog_state_done_syncing(
2346 xlog_in_core_t *iclog,
2347 int aborted)
2348{
2349 xlog_t *log = iclog->ic_log;
1da177e4 2350
b22cd72c 2351 spin_lock(&log->l_icloglock);
1da177e4
LT
2352
2353 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2354 iclog->ic_state == XLOG_STATE_IOERROR);
155cc6b7 2355 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1da177e4
LT
2356 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2357
2358
2359 /*
2360 * If we got an error, either on the first buffer, or in the case of
2361 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2362 * and none should ever be attempted to be written to disk
2363 * again.
2364 */
2365 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2366 if (--iclog->ic_bwritecnt == 1) {
b22cd72c 2367 spin_unlock(&log->l_icloglock);
1da177e4
LT
2368 return;
2369 }
2370 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2371 }
2372
2373 /*
2374 * Someone could be sleeping prior to writing out the next
2375 * iclog buffer, we wake them all, one will get to do the
2376 * I/O, the others get to wait for the result.
2377 */
12017faf 2378 sv_broadcast(&iclog->ic_write_wait);
b22cd72c 2379 spin_unlock(&log->l_icloglock);
1da177e4
LT
2380 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2381} /* xlog_state_done_syncing */
2382
2383
2384/*
2385 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
12017faf
DC
2386 * sleep. We wait on the flush queue on the head iclog as that should be
2387 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2388 * we will wait here and all new writes will sleep until a sync completes.
1da177e4
LT
2389 *
2390 * The in-core logs are used in a circular fashion. They are not used
2391 * out-of-order even when an iclog past the head is free.
2392 *
2393 * return:
2394 * * log_offset where xlog_write() can start writing into the in-core
2395 * log's data space.
2396 * * in-core log pointer to which xlog_write() should write.
2397 * * boolean indicating this is a continued write to an in-core log.
2398 * If this is the last write, then the in-core log's offset field
2399 * needs to be incremented, depending on the amount of data which
2400 * is copied.
2401 */
a8272ce0 2402STATIC int
1da177e4
LT
2403xlog_state_get_iclog_space(xlog_t *log,
2404 int len,
2405 xlog_in_core_t **iclogp,
2406 xlog_ticket_t *ticket,
2407 int *continued_write,
2408 int *logoffsetp)
2409{
1da177e4
LT
2410 int log_offset;
2411 xlog_rec_header_t *head;
2412 xlog_in_core_t *iclog;
2413 int error;
2414
2415restart:
b22cd72c 2416 spin_lock(&log->l_icloglock);
1da177e4 2417 if (XLOG_FORCED_SHUTDOWN(log)) {
b22cd72c 2418 spin_unlock(&log->l_icloglock);
1da177e4
LT
2419 return XFS_ERROR(EIO);
2420 }
2421
2422 iclog = log->l_iclog;
d748c623 2423 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
1da177e4 2424 XFS_STATS_INC(xs_log_noiclogs);
d748c623
MW
2425
2426 /* Wait for log writes to have flushed */
2427 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
1da177e4
LT
2428 goto restart;
2429 }
d748c623 2430
1da177e4
LT
2431 head = &iclog->ic_header;
2432
155cc6b7 2433 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
1da177e4
LT
2434 log_offset = iclog->ic_offset;
2435
2436 /* On the 1st write to an iclog, figure out lsn. This works
2437 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2438 * committing to. If the offset is set, that's how many blocks
2439 * must be written.
2440 */
2441 if (log_offset == 0) {
2442 ticket->t_curr_res -= log->l_iclog_hsize;
0adba536 2443 xlog_tic_add_region(ticket,
7e9c6396
TS
2444 log->l_iclog_hsize,
2445 XLOG_REG_TYPE_LRHEADER);
b53e675d
CH
2446 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2447 head->h_lsn = cpu_to_be64(
03bea6fe 2448 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
1da177e4
LT
2449 ASSERT(log->l_curr_block >= 0);
2450 }
2451
2452 /* If there is enough room to write everything, then do it. Otherwise,
2453 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2454 * bit is on, so this will get flushed out. Don't update ic_offset
2455 * until you know exactly how many bytes get copied. Therefore, wait
2456 * until later to update ic_offset.
2457 *
2458 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2459 * can fit into remaining data section.
2460 */
2461 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2462 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2463
49641f1a
DC
2464 /*
2465 * If I'm the only one writing to this iclog, sync it to disk.
2466 * We need to do an atomic compare and decrement here to avoid
2467 * racing with concurrent atomic_dec_and_lock() calls in
2468 * xlog_state_release_iclog() when there is more than one
2469 * reference to the iclog.
2470 */
2471 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2472 /* we are the only one */
b22cd72c 2473 spin_unlock(&log->l_icloglock);
49641f1a
DC
2474 error = xlog_state_release_iclog(log, iclog);
2475 if (error)
014c2544 2476 return error;
1da177e4 2477 } else {
b22cd72c 2478 spin_unlock(&log->l_icloglock);
1da177e4
LT
2479 }
2480 goto restart;
2481 }
2482
2483 /* Do we have enough room to write the full amount in the remainder
2484 * of this iclog? Or must we continue a write on the next iclog and
2485 * mark this iclog as completely taken? In the case where we switch
2486 * iclogs (to mark it taken), this particular iclog will release/sync
2487 * to disk in xlog_write().
2488 */
2489 if (len <= iclog->ic_size - iclog->ic_offset) {
2490 *continued_write = 0;
2491 iclog->ic_offset += len;
2492 } else {
2493 *continued_write = 1;
2494 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2495 }
2496 *iclogp = iclog;
2497
2498 ASSERT(iclog->ic_offset <= iclog->ic_size);
b22cd72c 2499 spin_unlock(&log->l_icloglock);
1da177e4
LT
2500
2501 *logoffsetp = log_offset;
2502 return 0;
2503} /* xlog_state_get_iclog_space */
2504
2505/*
2506 * Atomically get the log space required for a log ticket.
2507 *
2508 * Once a ticket gets put onto the reserveq, it will only return after
2509 * the needed reservation is satisfied.
2510 */
2511STATIC int
2512xlog_grant_log_space(xlog_t *log,
2513 xlog_ticket_t *tic)
2514{
2515 int free_bytes;
2516 int need_bytes;
1da177e4
LT
2517#ifdef DEBUG
2518 xfs_lsn_t tail_lsn;
2519#endif
2520
2521
2522#ifdef DEBUG
2523 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2524 panic("grant Recovery problem");
2525#endif
2526
2527 /* Is there space or do we need to sleep? */
c8b5ea28 2528 spin_lock(&log->l_grant_lock);
0b1b213f
CH
2529
2530 trace_xfs_log_grant_enter(log, tic);
1da177e4
LT
2531
2532 /* something is already sleeping; insert new transaction at end */
2533 if (log->l_reserve_headq) {
dd954c69 2534 xlog_ins_ticketq(&log->l_reserve_headq, tic);
0b1b213f
CH
2535
2536 trace_xfs_log_grant_sleep1(log, tic);
2537
1da177e4
LT
2538 /*
2539 * Gotta check this before going to sleep, while we're
2540 * holding the grant lock.
2541 */
2542 if (XLOG_FORCED_SHUTDOWN(log))
2543 goto error_return;
2544
2545 XFS_STATS_INC(xs_sleep_logspace);
12017faf 2546 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
1da177e4
LT
2547 /*
2548 * If we got an error, and the filesystem is shutting down,
2549 * we'll catch it down below. So just continue...
2550 */
0b1b213f 2551 trace_xfs_log_grant_wake1(log, tic);
c8b5ea28 2552 spin_lock(&log->l_grant_lock);
1da177e4
LT
2553 }
2554 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2555 need_bytes = tic->t_unit_res*tic->t_ocnt;
2556 else
2557 need_bytes = tic->t_unit_res;
2558
2559redo:
2560 if (XLOG_FORCED_SHUTDOWN(log))
2561 goto error_return;
2562
2563 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2564 log->l_grant_reserve_bytes);
2565 if (free_bytes < need_bytes) {
2566 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
dd954c69 2567 xlog_ins_ticketq(&log->l_reserve_headq, tic);
0b1b213f
CH
2568
2569 trace_xfs_log_grant_sleep2(log, tic);
2570
9d7fef74
DC
2571 spin_unlock(&log->l_grant_lock);
2572 xlog_grant_push_ail(log->l_mp, need_bytes);
2573 spin_lock(&log->l_grant_lock);
2574
1da177e4 2575 XFS_STATS_INC(xs_sleep_logspace);
12017faf 2576 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
1da177e4 2577
9d7fef74
DC
2578 spin_lock(&log->l_grant_lock);
2579 if (XLOG_FORCED_SHUTDOWN(log))
1da177e4 2580 goto error_return;
1da177e4 2581
0b1b213f
CH
2582 trace_xfs_log_grant_wake2(log, tic);
2583
1da177e4
LT
2584 goto redo;
2585 } else if (tic->t_flags & XLOG_TIC_IN_Q)
dd954c69 2586 xlog_del_ticketq(&log->l_reserve_headq, tic);
1da177e4
LT
2587
2588 /* we've got enough space */
dd954c69 2589 xlog_grant_add_space(log, need_bytes);
1da177e4
LT
2590#ifdef DEBUG
2591 tail_lsn = log->l_tail_lsn;
2592 /*
2593 * Check to make sure the grant write head didn't just over lap the
2594 * tail. If the cycles are the same, we can't be overlapping.
2595 * Otherwise, make sure that the cycles differ by exactly one and
2596 * check the byte count.
2597 */
2598 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2599 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2600 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2601 }
2602#endif
0b1b213f 2603 trace_xfs_log_grant_exit(log, tic);
1da177e4 2604 xlog_verify_grant_head(log, 1);
c8b5ea28 2605 spin_unlock(&log->l_grant_lock);
1da177e4
LT
2606 return 0;
2607
2608 error_return:
2609 if (tic->t_flags & XLOG_TIC_IN_Q)
dd954c69 2610 xlog_del_ticketq(&log->l_reserve_headq, tic);
0b1b213f
CH
2611
2612 trace_xfs_log_grant_error(log, tic);
2613
1da177e4
LT
2614 /*
2615 * If we are failing, make sure the ticket doesn't have any
2616 * current reservations. We don't want to add this back when
2617 * the ticket/transaction gets cancelled.
2618 */
2619 tic->t_curr_res = 0;
2620 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
c8b5ea28 2621 spin_unlock(&log->l_grant_lock);
1da177e4
LT
2622 return XFS_ERROR(EIO);
2623} /* xlog_grant_log_space */
2624
2625
2626/*
2627 * Replenish the byte reservation required by moving the grant write head.
2628 *
2629 *
2630 */
2631STATIC int
2632xlog_regrant_write_log_space(xlog_t *log,
2633 xlog_ticket_t *tic)
2634{
1da177e4
LT
2635 int free_bytes, need_bytes;
2636 xlog_ticket_t *ntic;
2637#ifdef DEBUG
2638 xfs_lsn_t tail_lsn;
2639#endif
2640
2641 tic->t_curr_res = tic->t_unit_res;
0adba536 2642 xlog_tic_reset_res(tic);
1da177e4
LT
2643
2644 if (tic->t_cnt > 0)
014c2544 2645 return 0;
1da177e4
LT
2646
2647#ifdef DEBUG
2648 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2649 panic("regrant Recovery problem");
2650#endif
2651
c8b5ea28 2652 spin_lock(&log->l_grant_lock);
0b1b213f
CH
2653
2654 trace_xfs_log_regrant_write_enter(log, tic);
1da177e4
LT
2655
2656 if (XLOG_FORCED_SHUTDOWN(log))
2657 goto error_return;
2658
2659 /* If there are other waiters on the queue then give them a
2660 * chance at logspace before us. Wake up the first waiters,
2661 * if we do not wake up all the waiters then go to sleep waiting
2662 * for more free space, otherwise try to get some space for
2663 * this transaction.
2664 */
9d7fef74 2665 need_bytes = tic->t_unit_res;
1da177e4
LT
2666 if ((ntic = log->l_write_headq)) {
2667 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2668 log->l_grant_write_bytes);
2669 do {
2670 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2671
2672 if (free_bytes < ntic->t_unit_res)
2673 break;
2674 free_bytes -= ntic->t_unit_res;
12017faf 2675 sv_signal(&ntic->t_wait);
1da177e4
LT
2676 ntic = ntic->t_next;
2677 } while (ntic != log->l_write_headq);
2678
2679 if (ntic != log->l_write_headq) {
2680 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
dd954c69 2681 xlog_ins_ticketq(&log->l_write_headq, tic);
1da177e4 2682
0b1b213f
CH
2683 trace_xfs_log_regrant_write_sleep1(log, tic);
2684
9d7fef74
DC
2685 spin_unlock(&log->l_grant_lock);
2686 xlog_grant_push_ail(log->l_mp, need_bytes);
2687 spin_lock(&log->l_grant_lock);
2688
1da177e4 2689 XFS_STATS_INC(xs_sleep_logspace);
12017faf 2690 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
1da177e4
LT
2691 &log->l_grant_lock, s);
2692
2693 /* If we're shutting down, this tic is already
2694 * off the queue */
9d7fef74
DC
2695 spin_lock(&log->l_grant_lock);
2696 if (XLOG_FORCED_SHUTDOWN(log))
1da177e4 2697 goto error_return;
1da177e4 2698
0b1b213f 2699 trace_xfs_log_regrant_write_wake1(log, tic);
1da177e4
LT
2700 }
2701 }
2702
1da177e4
LT
2703redo:
2704 if (XLOG_FORCED_SHUTDOWN(log))
2705 goto error_return;
2706
2707 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2708 log->l_grant_write_bytes);
2709 if (free_bytes < need_bytes) {
2710 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
dd954c69 2711 xlog_ins_ticketq(&log->l_write_headq, tic);
9d7fef74
DC
2712 spin_unlock(&log->l_grant_lock);
2713 xlog_grant_push_ail(log->l_mp, need_bytes);
2714 spin_lock(&log->l_grant_lock);
2715
1da177e4 2716 XFS_STATS_INC(xs_sleep_logspace);
0b1b213f
CH
2717 trace_xfs_log_regrant_write_sleep2(log, tic);
2718
12017faf 2719 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
1da177e4
LT
2720
2721 /* If we're shutting down, this tic is already off the queue */
9d7fef74
DC
2722 spin_lock(&log->l_grant_lock);
2723 if (XLOG_FORCED_SHUTDOWN(log))
1da177e4 2724 goto error_return;
1da177e4 2725
0b1b213f 2726 trace_xfs_log_regrant_write_wake2(log, tic);
1da177e4
LT
2727 goto redo;
2728 } else if (tic->t_flags & XLOG_TIC_IN_Q)
dd954c69 2729 xlog_del_ticketq(&log->l_write_headq, tic);
1da177e4 2730
dd954c69
CH
2731 /* we've got enough space */
2732 xlog_grant_add_space_write(log, need_bytes);
1da177e4
LT
2733#ifdef DEBUG
2734 tail_lsn = log->l_tail_lsn;
2735 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2736 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2737 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2738 }
2739#endif
2740
0b1b213f
CH
2741 trace_xfs_log_regrant_write_exit(log, tic);
2742
1da177e4 2743 xlog_verify_grant_head(log, 1);
c8b5ea28 2744 spin_unlock(&log->l_grant_lock);
014c2544 2745 return 0;
1da177e4
LT
2746
2747
2748 error_return:
2749 if (tic->t_flags & XLOG_TIC_IN_Q)
dd954c69 2750 xlog_del_ticketq(&log->l_reserve_headq, tic);
0b1b213f
CH
2751
2752 trace_xfs_log_regrant_write_error(log, tic);
2753
1da177e4
LT
2754 /*
2755 * If we are failing, make sure the ticket doesn't have any
2756 * current reservations. We don't want to add this back when
2757 * the ticket/transaction gets cancelled.
2758 */
2759 tic->t_curr_res = 0;
2760 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
c8b5ea28 2761 spin_unlock(&log->l_grant_lock);
1da177e4
LT
2762 return XFS_ERROR(EIO);
2763} /* xlog_regrant_write_log_space */
2764
2765
2766/* The first cnt-1 times through here we don't need to
2767 * move the grant write head because the permanent
2768 * reservation has reserved cnt times the unit amount.
2769 * Release part of current permanent unit reservation and
2770 * reset current reservation to be one units worth. Also
2771 * move grant reservation head forward.
2772 */
2773STATIC void
2774xlog_regrant_reserve_log_space(xlog_t *log,
2775 xlog_ticket_t *ticket)
2776{
0b1b213f
CH
2777 trace_xfs_log_regrant_reserve_enter(log, ticket);
2778
1da177e4
LT
2779 if (ticket->t_cnt > 0)
2780 ticket->t_cnt--;
2781
c8b5ea28 2782 spin_lock(&log->l_grant_lock);
dd954c69 2783 xlog_grant_sub_space(log, ticket->t_curr_res);
1da177e4 2784 ticket->t_curr_res = ticket->t_unit_res;
0adba536 2785 xlog_tic_reset_res(ticket);
0b1b213f
CH
2786
2787 trace_xfs_log_regrant_reserve_sub(log, ticket);
2788
1da177e4
LT
2789 xlog_verify_grant_head(log, 1);
2790
2791 /* just return if we still have some of the pre-reserved space */
2792 if (ticket->t_cnt > 0) {
c8b5ea28 2793 spin_unlock(&log->l_grant_lock);
1da177e4
LT
2794 return;
2795 }
2796
dd954c69 2797 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
0b1b213f
CH
2798
2799 trace_xfs_log_regrant_reserve_exit(log, ticket);
2800
1da177e4 2801 xlog_verify_grant_head(log, 0);
c8b5ea28 2802 spin_unlock(&log->l_grant_lock);
1da177e4 2803 ticket->t_curr_res = ticket->t_unit_res;
0adba536 2804 xlog_tic_reset_res(ticket);
1da177e4
LT
2805} /* xlog_regrant_reserve_log_space */
2806
2807
2808/*
2809 * Give back the space left from a reservation.
2810 *
2811 * All the information we need to make a correct determination of space left
2812 * is present. For non-permanent reservations, things are quite easy. The
2813 * count should have been decremented to zero. We only need to deal with the
2814 * space remaining in the current reservation part of the ticket. If the
2815 * ticket contains a permanent reservation, there may be left over space which
2816 * needs to be released. A count of N means that N-1 refills of the current
2817 * reservation can be done before we need to ask for more space. The first
2818 * one goes to fill up the first current reservation. Once we run out of
2819 * space, the count will stay at zero and the only space remaining will be
2820 * in the current reservation field.
2821 */
2822STATIC void
2823xlog_ungrant_log_space(xlog_t *log,
2824 xlog_ticket_t *ticket)
2825{
1da177e4
LT
2826 if (ticket->t_cnt > 0)
2827 ticket->t_cnt--;
2828
c8b5ea28 2829 spin_lock(&log->l_grant_lock);
0b1b213f 2830 trace_xfs_log_ungrant_enter(log, ticket);
1da177e4 2831
dd954c69 2832 xlog_grant_sub_space(log, ticket->t_curr_res);
1da177e4 2833
0b1b213f 2834 trace_xfs_log_ungrant_sub(log, ticket);
1da177e4
LT
2835
2836 /* If this is a permanent reservation ticket, we may be able to free
2837 * up more space based on the remaining count.
2838 */
2839 if (ticket->t_cnt > 0) {
2840 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
dd954c69 2841 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
1da177e4
LT
2842 }
2843
0b1b213f
CH
2844 trace_xfs_log_ungrant_exit(log, ticket);
2845
1da177e4 2846 xlog_verify_grant_head(log, 1);
c8b5ea28 2847 spin_unlock(&log->l_grant_lock);
1da177e4
LT
2848 xfs_log_move_tail(log->l_mp, 1);
2849} /* xlog_ungrant_log_space */
2850
2851
1da177e4
LT
2852/*
2853 * Flush iclog to disk if this is the last reference to the given iclog and
2854 * the WANT_SYNC bit is set.
2855 *
2856 * When this function is entered, the iclog is not necessarily in the
2857 * WANT_SYNC state. It may be sitting around waiting to get filled.
2858 *
2859 *
2860 */
a8272ce0 2861STATIC int
b589334c
DC
2862xlog_state_release_iclog(
2863 xlog_t *log,
2864 xlog_in_core_t *iclog)
1da177e4 2865{
1da177e4
LT
2866 int sync = 0; /* do we sync? */
2867
155cc6b7
DC
2868 if (iclog->ic_state & XLOG_STATE_IOERROR)
2869 return XFS_ERROR(EIO);
2870
2871 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2872 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2873 return 0;
2874
1da177e4 2875 if (iclog->ic_state & XLOG_STATE_IOERROR) {
b22cd72c 2876 spin_unlock(&log->l_icloglock);
1da177e4
LT
2877 return XFS_ERROR(EIO);
2878 }
1da177e4
LT
2879 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2880 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2881
155cc6b7 2882 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
b589334c
DC
2883 /* update tail before writing to iclog */
2884 xlog_assign_tail_lsn(log->l_mp);
1da177e4
LT
2885 sync++;
2886 iclog->ic_state = XLOG_STATE_SYNCING;
b53e675d 2887 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
1da177e4
LT
2888 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2889 /* cycle incremented when incrementing curr_block */
2890 }
b22cd72c 2891 spin_unlock(&log->l_icloglock);
1da177e4
LT
2892
2893 /*
2894 * We let the log lock go, so it's possible that we hit a log I/O
c41564b5 2895 * error or some other SHUTDOWN condition that marks the iclog
1da177e4
LT
2896 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2897 * this iclog has consistent data, so we ignore IOERROR
2898 * flags after this point.
2899 */
b589334c 2900 if (sync)
1da177e4 2901 return xlog_sync(log, iclog);
014c2544 2902 return 0;
1da177e4
LT
2903} /* xlog_state_release_iclog */
2904
2905
2906/*
2907 * This routine will mark the current iclog in the ring as WANT_SYNC
2908 * and move the current iclog pointer to the next iclog in the ring.
2909 * When this routine is called from xlog_state_get_iclog_space(), the
2910 * exact size of the iclog has not yet been determined. All we know is
2911 * that every data block. We have run out of space in this log record.
2912 */
2913STATIC void
2914xlog_state_switch_iclogs(xlog_t *log,
2915 xlog_in_core_t *iclog,
2916 int eventual_size)
2917{
2918 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2919 if (!eventual_size)
2920 eventual_size = iclog->ic_offset;
2921 iclog->ic_state = XLOG_STATE_WANT_SYNC;
b53e675d 2922 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
1da177e4
LT
2923 log->l_prev_block = log->l_curr_block;
2924 log->l_prev_cycle = log->l_curr_cycle;
2925
2926 /* roll log?: ic_offset changed later */
2927 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2928
2929 /* Round up to next log-sunit */
62118709 2930 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
1da177e4
LT
2931 log->l_mp->m_sb.sb_logsunit > 1) {
2932 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2933 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2934 }
2935
2936 if (log->l_curr_block >= log->l_logBBsize) {
2937 log->l_curr_cycle++;
2938 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2939 log->l_curr_cycle++;
2940 log->l_curr_block -= log->l_logBBsize;
2941 ASSERT(log->l_curr_block >= 0);
2942 }
2943 ASSERT(iclog == log->l_iclog);
2944 log->l_iclog = iclog->ic_next;
2945} /* xlog_state_switch_iclogs */
2946
1da177e4
LT
2947/*
2948 * Write out all data in the in-core log as of this exact moment in time.
2949 *
2950 * Data may be written to the in-core log during this call. However,
2951 * we don't guarantee this data will be written out. A change from past
2952 * implementation means this routine will *not* write out zero length LRs.
2953 *
2954 * Basically, we try and perform an intelligent scan of the in-core logs.
2955 * If we determine there is no flushable data, we just return. There is no
2956 * flushable data if:
2957 *
2958 * 1. the current iclog is active and has no data; the previous iclog
2959 * is in the active or dirty state.
2960 * 2. the current iclog is drity, and the previous iclog is in the
2961 * active or dirty state.
2962 *
12017faf 2963 * We may sleep if:
1da177e4
LT
2964 *
2965 * 1. the current iclog is not in the active nor dirty state.
2966 * 2. the current iclog dirty, and the previous iclog is not in the
2967 * active nor dirty state.
2968 * 3. the current iclog is active, and there is another thread writing
2969 * to this particular iclog.
2970 * 4. a) the current iclog is active and has no other writers
2971 * b) when we return from flushing out this iclog, it is still
2972 * not in the active nor dirty state.
2973 */
a14a348b
CH
2974int
2975_xfs_log_force(
2976 struct xfs_mount *mp,
2977 uint flags,
2978 int *log_flushed)
1da177e4 2979{
a14a348b
CH
2980 struct log *log = mp->m_log;
2981 struct xlog_in_core *iclog;
2982 xfs_lsn_t lsn;
2983
2984 XFS_STATS_INC(xs_log_force);
1da177e4 2985
b22cd72c 2986 spin_lock(&log->l_icloglock);
1da177e4
LT
2987
2988 iclog = log->l_iclog;
2989 if (iclog->ic_state & XLOG_STATE_IOERROR) {
b22cd72c 2990 spin_unlock(&log->l_icloglock);
1da177e4
LT
2991 return XFS_ERROR(EIO);
2992 }
2993
2994 /* If the head iclog is not active nor dirty, we just attach
2995 * ourselves to the head and go to sleep.
2996 */
2997 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2998 iclog->ic_state == XLOG_STATE_DIRTY) {
2999 /*
3000 * If the head is dirty or (active and empty), then
3001 * we need to look at the previous iclog. If the previous
3002 * iclog is active or dirty we are done. There is nothing
3003 * to sync out. Otherwise, we attach ourselves to the
3004 * previous iclog and go to sleep.
3005 */
3006 if (iclog->ic_state == XLOG_STATE_DIRTY ||
155cc6b7
DC
3007 (atomic_read(&iclog->ic_refcnt) == 0
3008 && iclog->ic_offset == 0)) {
1da177e4
LT
3009 iclog = iclog->ic_prev;
3010 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
3011 iclog->ic_state == XLOG_STATE_DIRTY)
3012 goto no_sleep;
3013 else
3014 goto maybe_sleep;
3015 } else {
155cc6b7 3016 if (atomic_read(&iclog->ic_refcnt) == 0) {
1da177e4
LT
3017 /* We are the only one with access to this
3018 * iclog. Flush it out now. There should
3019 * be a roundoff of zero to show that someone
3020 * has already taken care of the roundoff from
3021 * the previous sync.
3022 */
155cc6b7 3023 atomic_inc(&iclog->ic_refcnt);
b53e675d 3024 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
1da177e4 3025 xlog_state_switch_iclogs(log, iclog, 0);
b22cd72c 3026 spin_unlock(&log->l_icloglock);
1da177e4
LT
3027
3028 if (xlog_state_release_iclog(log, iclog))
3029 return XFS_ERROR(EIO);
a14a348b
CH
3030
3031 if (log_flushed)
3032 *log_flushed = 1;
b22cd72c 3033 spin_lock(&log->l_icloglock);
b53e675d 3034 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
1da177e4
LT
3035 iclog->ic_state != XLOG_STATE_DIRTY)
3036 goto maybe_sleep;
3037 else
3038 goto no_sleep;
3039 } else {
3040 /* Someone else is writing to this iclog.
3041 * Use its call to flush out the data. However,
3042 * the other thread may not force out this LR,
3043 * so we mark it WANT_SYNC.
3044 */
3045 xlog_state_switch_iclogs(log, iclog, 0);
3046 goto maybe_sleep;
3047 }
3048 }
3049 }
3050
3051 /* By the time we come around again, the iclog could've been filled
3052 * which would give it another lsn. If we have a new lsn, just
3053 * return because the relevant data has been flushed.
3054 */
3055maybe_sleep:
3056 if (flags & XFS_LOG_SYNC) {
3057 /*
3058 * We must check if we're shutting down here, before
b22cd72c 3059 * we wait, while we're holding the l_icloglock.
1da177e4
LT
3060 * Then we check again after waking up, in case our
3061 * sleep was disturbed by a bad news.
3062 */
3063 if (iclog->ic_state & XLOG_STATE_IOERROR) {
b22cd72c 3064 spin_unlock(&log->l_icloglock);
1da177e4
LT
3065 return XFS_ERROR(EIO);
3066 }
3067 XFS_STATS_INC(xs_log_force_sleep);
12017faf 3068 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
1da177e4
LT
3069 /*
3070 * No need to grab the log lock here since we're
3071 * only deciding whether or not to return EIO
3072 * and the memory read should be atomic.
3073 */
3074 if (iclog->ic_state & XLOG_STATE_IOERROR)
3075 return XFS_ERROR(EIO);
a14a348b
CH
3076 if (log_flushed)
3077 *log_flushed = 1;
1da177e4
LT
3078 } else {
3079
3080no_sleep:
b22cd72c 3081 spin_unlock(&log->l_icloglock);
1da177e4
LT
3082 }
3083 return 0;
a14a348b 3084}
1da177e4 3085
a14a348b
CH
3086/*
3087 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
3088 * about errors or whether the log was flushed or not. This is the normal
3089 * interface to use when trying to unpin items or move the log forward.
3090 */
3091void
3092xfs_log_force(
3093 xfs_mount_t *mp,
3094 uint flags)
3095{
3096 int error;
3097
3098 error = _xfs_log_force(mp, flags, NULL);
3099 if (error) {
3100 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3101 "error %d returned.", error);
3102 }
3103}
1da177e4
LT
3104
3105/*
a14a348b 3106 * Force the in-core log to disk for a specific LSN.
1da177e4
LT
3107 *
3108 * Find in-core log with lsn.
3109 * If it is in the DIRTY state, just return.
3110 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3111 * state and go to sleep or return.
3112 * If it is in any other state, go to sleep or return.
3113 *
a14a348b
CH
3114 * Synchronous forces are implemented with a signal variable. All callers
3115 * to force a given lsn to disk will wait on a the sv attached to the
3116 * specific in-core log. When given in-core log finally completes its
3117 * write to disk, that thread will wake up all threads waiting on the
3118 * sv.
1da177e4 3119 */
a14a348b
CH
3120int
3121_xfs_log_force_lsn(
3122 struct xfs_mount *mp,
3123 xfs_lsn_t lsn,
3124 uint flags,
3125 int *log_flushed)
1da177e4 3126{
a14a348b
CH
3127 struct log *log = mp->m_log;
3128 struct xlog_in_core *iclog;
3129 int already_slept = 0;
1da177e4 3130
a14a348b 3131 ASSERT(lsn != 0);
1da177e4 3132
a14a348b 3133 XFS_STATS_INC(xs_log_force);
1da177e4 3134
a14a348b
CH
3135try_again:
3136 spin_lock(&log->l_icloglock);
3137 iclog = log->l_iclog;
3138 if (iclog->ic_state & XLOG_STATE_IOERROR) {
b22cd72c 3139 spin_unlock(&log->l_icloglock);
a14a348b 3140 return XFS_ERROR(EIO);
1da177e4
LT
3141 }
3142
a14a348b
CH
3143 do {
3144 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3145 iclog = iclog->ic_next;
3146 continue;
3147 }
3148
3149 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3150 spin_unlock(&log->l_icloglock);
3151 return 0;
3152 }
3153
3154 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3155 /*
3156 * We sleep here if we haven't already slept (e.g.
3157 * this is the first time we've looked at the correct
3158 * iclog buf) and the buffer before us is going to
3159 * be sync'ed. The reason for this is that if we
3160 * are doing sync transactions here, by waiting for
3161 * the previous I/O to complete, we can allow a few
3162 * more transactions into this iclog before we close
3163 * it down.
3164 *
3165 * Otherwise, we mark the buffer WANT_SYNC, and bump
3166 * up the refcnt so we can release the log (which
3167 * drops the ref count). The state switch keeps new
3168 * transaction commits from using this buffer. When
3169 * the current commits finish writing into the buffer,
3170 * the refcount will drop to zero and the buffer will
3171 * go out then.
3172 */
3173 if (!already_slept &&
3174 (iclog->ic_prev->ic_state &
3175 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3176 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3177
3178 XFS_STATS_INC(xs_log_force_sleep);
3179
3180 sv_wait(&iclog->ic_prev->ic_write_wait,
3181 PSWP, &log->l_icloglock, s);
3182 if (log_flushed)
3183 *log_flushed = 1;
3184 already_slept = 1;
3185 goto try_again;
3186 }
155cc6b7 3187 atomic_inc(&iclog->ic_refcnt);
1da177e4 3188 xlog_state_switch_iclogs(log, iclog, 0);
b22cd72c 3189 spin_unlock(&log->l_icloglock);
1da177e4
LT
3190 if (xlog_state_release_iclog(log, iclog))
3191 return XFS_ERROR(EIO);
a14a348b
CH
3192 if (log_flushed)
3193 *log_flushed = 1;
b22cd72c 3194 spin_lock(&log->l_icloglock);
1da177e4 3195 }
1da177e4 3196
a14a348b
CH
3197 if ((flags & XFS_LOG_SYNC) && /* sleep */
3198 !(iclog->ic_state &
3199 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3200 /*
3201 * Don't wait on completion if we know that we've
3202 * gotten a log write error.
3203 */
3204 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3205 spin_unlock(&log->l_icloglock);
3206 return XFS_ERROR(EIO);
3207 }
3208 XFS_STATS_INC(xs_log_force_sleep);
3209 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3210 /*
3211 * No need to grab the log lock here since we're
3212 * only deciding whether or not to return EIO
3213 * and the memory read should be atomic.
3214 */
3215 if (iclog->ic_state & XLOG_STATE_IOERROR)
3216 return XFS_ERROR(EIO);
1da177e4 3217
a14a348b
CH
3218 if (log_flushed)
3219 *log_flushed = 1;
3220 } else { /* just return */
b22cd72c 3221 spin_unlock(&log->l_icloglock);
1da177e4 3222 }
1da177e4 3223
a14a348b
CH
3224 return 0;
3225 } while (iclog != log->l_iclog);
1da177e4 3226
a14a348b
CH
3227 spin_unlock(&log->l_icloglock);
3228 return 0;
3229}
3230
3231/*
3232 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3233 * about errors or whether the log was flushed or not. This is the normal
3234 * interface to use when trying to unpin items or move the log forward.
3235 */
3236void
3237xfs_log_force_lsn(
3238 xfs_mount_t *mp,
3239 xfs_lsn_t lsn,
3240 uint flags)
3241{
3242 int error;
1da177e4 3243
a14a348b
CH
3244 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3245 if (error) {
3246 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3247 "error %d returned.", error);
3248 }
3249}
1da177e4
LT
3250
3251/*
3252 * Called when we want to mark the current iclog as being ready to sync to
3253 * disk.
3254 */
a8272ce0 3255STATIC void
1da177e4
LT
3256xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3257{
a8914f3a 3258 assert_spin_locked(&log->l_icloglock);
1da177e4
LT
3259
3260 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3261 xlog_state_switch_iclogs(log, iclog, 0);
3262 } else {
3263 ASSERT(iclog->ic_state &
3264 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3265 }
39e2defe 3266}
1da177e4
LT
3267
3268
3269/*****************************************************************************
3270 *
3271 * TICKET functions
3272 *
3273 *****************************************************************************
3274 */
3275
3276/*
9da096fd 3277 * Free a used ticket when its refcount falls to zero.
1da177e4 3278 */
cc09c0dc
DC
3279void
3280xfs_log_ticket_put(
3281 xlog_ticket_t *ticket)
1da177e4 3282{
cc09c0dc
DC
3283 ASSERT(atomic_read(&ticket->t_ref) > 0);
3284 if (atomic_dec_and_test(&ticket->t_ref)) {
3285 sv_destroy(&ticket->t_wait);
3286 kmem_zone_free(xfs_log_ticket_zone, ticket);
3287 }
3288}
1da177e4 3289
cc09c0dc
DC
3290xlog_ticket_t *
3291xfs_log_ticket_get(
3292 xlog_ticket_t *ticket)
3293{
3294 ASSERT(atomic_read(&ticket->t_ref) > 0);
3295 atomic_inc(&ticket->t_ref);
3296 return ticket;
3297}
1da177e4
LT
3298
3299/*
eb01c9cd 3300 * Allocate and initialise a new log ticket.
1da177e4 3301 */
a8272ce0 3302STATIC xlog_ticket_t *
9b9fc2b7
DC
3303xlog_ticket_alloc(
3304 struct log *log,
3305 int unit_bytes,
3306 int cnt,
3307 char client,
3308 uint xflags)
1da177e4 3309{
9b9fc2b7 3310 struct xlog_ticket *tic;
1da177e4 3311 uint num_headers;
9b9fc2b7 3312 int iclog_space;
1da177e4 3313
eb01c9cd
DC
3314 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3315 if (!tic)
3316 return NULL;
1da177e4
LT
3317
3318 /*
3319 * Permanent reservations have up to 'cnt'-1 active log operations
3320 * in the log. A unit in this case is the amount of space for one
3321 * of these log operations. Normal reservations have a cnt of 1
3322 * and their unit amount is the total amount of space required.
3323 *
3324 * The following lines of code account for non-transaction data
32fb9b57
TS
3325 * which occupy space in the on-disk log.
3326 *
3327 * Normal form of a transaction is:
3328 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3329 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3330 *
3331 * We need to account for all the leadup data and trailer data
3332 * around the transaction data.
3333 * And then we need to account for the worst case in terms of using
3334 * more space.
3335 * The worst case will happen if:
3336 * - the placement of the transaction happens to be such that the
3337 * roundoff is at its maximum
3338 * - the transaction data is synced before the commit record is synced
3339 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3340 * Therefore the commit record is in its own Log Record.
3341 * This can happen as the commit record is called with its
3342 * own region to xlog_write().
3343 * This then means that in the worst case, roundoff can happen for
3344 * the commit-rec as well.
3345 * The commit-rec is smaller than padding in this scenario and so it is
3346 * not added separately.
1da177e4
LT
3347 */
3348
32fb9b57
TS
3349 /* for trans header */
3350 unit_bytes += sizeof(xlog_op_header_t);
3351 unit_bytes += sizeof(xfs_trans_header_t);
3352
1da177e4 3353 /* for start-rec */
32fb9b57
TS
3354 unit_bytes += sizeof(xlog_op_header_t);
3355
9b9fc2b7
DC
3356 /*
3357 * for LR headers - the space for data in an iclog is the size minus
3358 * the space used for the headers. If we use the iclog size, then we
3359 * undercalculate the number of headers required.
3360 *
3361 * Furthermore - the addition of op headers for split-recs might
3362 * increase the space required enough to require more log and op
3363 * headers, so take that into account too.
3364 *
3365 * IMPORTANT: This reservation makes the assumption that if this
3366 * transaction is the first in an iclog and hence has the LR headers
3367 * accounted to it, then the remaining space in the iclog is
3368 * exclusively for this transaction. i.e. if the transaction is larger
3369 * than the iclog, it will be the only thing in that iclog.
3370 * Fundamentally, this means we must pass the entire log vector to
3371 * xlog_write to guarantee this.
3372 */
3373 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3374 num_headers = howmany(unit_bytes, iclog_space);
3375
3376 /* for split-recs - ophdrs added when data split over LRs */
3377 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3378
3379 /* add extra header reservations if we overrun */
3380 while (!num_headers ||
3381 howmany(unit_bytes, iclog_space) > num_headers) {
3382 unit_bytes += sizeof(xlog_op_header_t);
3383 num_headers++;
3384 }
32fb9b57 3385 unit_bytes += log->l_iclog_hsize * num_headers;
1da177e4 3386
32fb9b57
TS
3387 /* for commit-rec LR header - note: padding will subsume the ophdr */
3388 unit_bytes += log->l_iclog_hsize;
3389
32fb9b57 3390 /* for roundoff padding for transaction data and one for commit record */
62118709 3391 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
32fb9b57 3392 log->l_mp->m_sb.sb_logsunit > 1) {
1da177e4 3393 /* log su roundoff */
32fb9b57 3394 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
1da177e4
LT
3395 } else {
3396 /* BB roundoff */
32fb9b57 3397 unit_bytes += 2*BBSIZE;
1da177e4
LT
3398 }
3399
cc09c0dc 3400 atomic_set(&tic->t_ref, 1);
1da177e4
LT
3401 tic->t_unit_res = unit_bytes;
3402 tic->t_curr_res = unit_bytes;
3403 tic->t_cnt = cnt;
3404 tic->t_ocnt = cnt;
3405 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3406 tic->t_clientid = client;
3407 tic->t_flags = XLOG_TIC_INITED;
7e9c6396 3408 tic->t_trans_type = 0;
1da177e4
LT
3409 if (xflags & XFS_LOG_PERM_RESERV)
3410 tic->t_flags |= XLOG_TIC_PERM_RESERV;
9b9fc2b7 3411 sv_init(&tic->t_wait, SV_DEFAULT, "logtick");
1da177e4 3412
0adba536 3413 xlog_tic_reset_res(tic);
7e9c6396 3414
1da177e4 3415 return tic;
cc09c0dc 3416}
1da177e4
LT
3417
3418
3419/******************************************************************************
3420 *
3421 * Log debug routines
3422 *
3423 ******************************************************************************
3424 */
cfcbbbd0 3425#if defined(DEBUG)
1da177e4
LT
3426/*
3427 * Make sure that the destination ptr is within the valid data region of
3428 * one of the iclogs. This uses backup pointers stored in a different
3429 * part of the log in case we trash the log structure.
3430 */
3431void
3432xlog_verify_dest_ptr(xlog_t *log,
3433 __psint_t ptr)
3434{
3435 int i;
3436 int good_ptr = 0;
3437
3438 for (i=0; i < log->l_iclog_bufs; i++) {
3439 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3440 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3441 good_ptr++;
3442 }
3443 if (! good_ptr)
3444 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3445} /* xlog_verify_dest_ptr */
3446
3447STATIC void
3448xlog_verify_grant_head(xlog_t *log, int equals)
3449{
3450 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3451 if (equals)
3452 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3453 else
3454 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3455 } else {
3456 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3457 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3458 }
3459} /* xlog_verify_grant_head */
3460
3461/* check if it will fit */
3462STATIC void
3463xlog_verify_tail_lsn(xlog_t *log,
3464 xlog_in_core_t *iclog,
3465 xfs_lsn_t tail_lsn)
3466{
3467 int blocks;
3468
3469 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3470 blocks =
3471 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3472 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3473 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3474 } else {
3475 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3476
3477 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3478 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3479
3480 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3481 if (blocks < BTOBB(iclog->ic_offset) + 1)
3482 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3483 }
3484} /* xlog_verify_tail_lsn */
3485
3486/*
3487 * Perform a number of checks on the iclog before writing to disk.
3488 *
3489 * 1. Make sure the iclogs are still circular
3490 * 2. Make sure we have a good magic number
3491 * 3. Make sure we don't have magic numbers in the data
3492 * 4. Check fields of each log operation header for:
3493 * A. Valid client identifier
3494 * B. tid ptr value falls in valid ptr space (user space code)
3495 * C. Length in log record header is correct according to the
3496 * individual operation headers within record.
3497 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3498 * log, check the preceding blocks of the physical log to make sure all
3499 * the cycle numbers agree with the current cycle number.
3500 */
3501STATIC void
3502xlog_verify_iclog(xlog_t *log,
3503 xlog_in_core_t *iclog,
3504 int count,
3505 boolean_t syncing)
3506{
3507 xlog_op_header_t *ophead;
3508 xlog_in_core_t *icptr;
3509 xlog_in_core_2_t *xhdr;
3510 xfs_caddr_t ptr;
3511 xfs_caddr_t base_ptr;
3512 __psint_t field_offset;
3513 __uint8_t clientid;
3514 int len, i, j, k, op_len;
3515 int idx;
1da177e4
LT
3516
3517 /* check validity of iclog pointers */
b22cd72c 3518 spin_lock(&log->l_icloglock);
1da177e4
LT
3519 icptr = log->l_iclog;
3520 for (i=0; i < log->l_iclog_bufs; i++) {
4b80916b 3521 if (icptr == NULL)
1da177e4
LT
3522 xlog_panic("xlog_verify_iclog: invalid ptr");
3523 icptr = icptr->ic_next;
3524 }
3525 if (icptr != log->l_iclog)
3526 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
b22cd72c 3527 spin_unlock(&log->l_icloglock);
1da177e4
LT
3528
3529 /* check log magic numbers */
b53e675d 3530 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
1da177e4
LT
3531 xlog_panic("xlog_verify_iclog: invalid magic num");
3532
b53e675d
CH
3533 ptr = (xfs_caddr_t) &iclog->ic_header;
3534 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
1da177e4 3535 ptr += BBSIZE) {
b53e675d 3536 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
1da177e4
LT
3537 xlog_panic("xlog_verify_iclog: unexpected magic num");
3538 }
3539
3540 /* check fields */
b53e675d 3541 len = be32_to_cpu(iclog->ic_header.h_num_logops);
1da177e4
LT
3542 ptr = iclog->ic_datap;
3543 base_ptr = ptr;
3544 ophead = (xlog_op_header_t *)ptr;
b28708d6 3545 xhdr = iclog->ic_data;
1da177e4
LT
3546 for (i = 0; i < len; i++) {
3547 ophead = (xlog_op_header_t *)ptr;
3548
3549 /* clientid is only 1 byte */
3550 field_offset = (__psint_t)
3551 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3552 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3553 clientid = ophead->oh_clientid;
3554 } else {
3555 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3556 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3557 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3558 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
03bea6fe
CH
3559 clientid = xlog_get_client_id(
3560 xhdr[j].hic_xheader.xh_cycle_data[k]);
1da177e4 3561 } else {
03bea6fe
CH
3562 clientid = xlog_get_client_id(
3563 iclog->ic_header.h_cycle_data[idx]);
1da177e4
LT
3564 }
3565 }
3566 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
da1650a5
CH
3567 cmn_err(CE_WARN, "xlog_verify_iclog: "
3568 "invalid clientid %d op 0x%p offset 0x%lx",
3569 clientid, ophead, (unsigned long)field_offset);
1da177e4
LT
3570
3571 /* check length */
3572 field_offset = (__psint_t)
3573 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3574 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
67fcb7bf 3575 op_len = be32_to_cpu(ophead->oh_len);
1da177e4
LT
3576 } else {
3577 idx = BTOBBT((__psint_t)&ophead->oh_len -
3578 (__psint_t)iclog->ic_datap);
3579 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3580 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3581 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
b53e675d 3582 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
1da177e4 3583 } else {
b53e675d 3584 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
1da177e4
LT
3585 }
3586 }
3587 ptr += sizeof(xlog_op_header_t) + op_len;
3588 }
3589} /* xlog_verify_iclog */
cfcbbbd0 3590#endif
1da177e4
LT
3591
3592/*
b22cd72c 3593 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
1da177e4
LT
3594 */
3595STATIC int
3596xlog_state_ioerror(
3597 xlog_t *log)
3598{
3599 xlog_in_core_t *iclog, *ic;
3600
3601 iclog = log->l_iclog;
3602 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3603 /*
3604 * Mark all the incore logs IOERROR.
3605 * From now on, no log flushes will result.
3606 */
3607 ic = iclog;
3608 do {
3609 ic->ic_state = XLOG_STATE_IOERROR;
3610 ic = ic->ic_next;
3611 } while (ic != iclog);
014c2544 3612 return 0;
1da177e4
LT
3613 }
3614 /*
3615 * Return non-zero, if state transition has already happened.
3616 */
014c2544 3617 return 1;
1da177e4
LT
3618}
3619
3620/*
3621 * This is called from xfs_force_shutdown, when we're forcibly
3622 * shutting down the filesystem, typically because of an IO error.
3623 * Our main objectives here are to make sure that:
3624 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3625 * parties to find out, 'atomically'.
3626 * b. those who're sleeping on log reservations, pinned objects and
3627 * other resources get woken up, and be told the bad news.
3628 * c. nothing new gets queued up after (a) and (b) are done.
3629 * d. if !logerror, flush the iclogs to disk, then seal them off
3630 * for business.
3631 */
3632int
3633xfs_log_force_umount(
3634 struct xfs_mount *mp,
3635 int logerror)
3636{
3637 xlog_ticket_t *tic;
3638 xlog_t *log;
3639 int retval;
1da177e4
LT
3640
3641 log = mp->m_log;
3642
3643 /*
3644 * If this happens during log recovery, don't worry about
3645 * locking; the log isn't open for business yet.
3646 */
3647 if (!log ||
3648 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3649 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
bac8dca9
CH
3650 if (mp->m_sb_bp)
3651 XFS_BUF_DONE(mp->m_sb_bp);
014c2544 3652 return 0;
1da177e4
LT
3653 }
3654
3655 /*
3656 * Somebody could've already done the hard work for us.
3657 * No need to get locks for this.
3658 */
3659 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3660 ASSERT(XLOG_FORCED_SHUTDOWN(log));
014c2544 3661 return 1;
1da177e4
LT
3662 }
3663 retval = 0;
3664 /*
3665 * We must hold both the GRANT lock and the LOG lock,
3666 * before we mark the filesystem SHUTDOWN and wake
3667 * everybody up to tell the bad news.
3668 */
b22cd72c 3669 spin_lock(&log->l_icloglock);
6b1d1a73 3670 spin_lock(&log->l_grant_lock);
1da177e4 3671 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
bac8dca9
CH
3672 if (mp->m_sb_bp)
3673 XFS_BUF_DONE(mp->m_sb_bp);
3674
1da177e4
LT
3675 /*
3676 * This flag is sort of redundant because of the mount flag, but
3677 * it's good to maintain the separation between the log and the rest
3678 * of XFS.
3679 */
3680 log->l_flags |= XLOG_IO_ERROR;
3681
3682 /*
3683 * If we hit a log error, we want to mark all the iclogs IOERROR
3684 * while we're still holding the loglock.
3685 */
3686 if (logerror)
3687 retval = xlog_state_ioerror(log);
b22cd72c 3688 spin_unlock(&log->l_icloglock);
1da177e4
LT
3689
3690 /*
3691 * We don't want anybody waiting for log reservations
3692 * after this. That means we have to wake up everybody
3693 * queued up on reserve_headq as well as write_headq.
3694 * In addition, we make sure in xlog_{re}grant_log_space
3695 * that we don't enqueue anything once the SHUTDOWN flag
3696 * is set, and this action is protected by the GRANTLOCK.
3697 */
3698 if ((tic = log->l_reserve_headq)) {
3699 do {
12017faf 3700 sv_signal(&tic->t_wait);
1da177e4
LT
3701 tic = tic->t_next;
3702 } while (tic != log->l_reserve_headq);
3703 }
3704
3705 if ((tic = log->l_write_headq)) {
3706 do {
12017faf 3707 sv_signal(&tic->t_wait);
1da177e4
LT
3708 tic = tic->t_next;
3709 } while (tic != log->l_write_headq);
3710 }
c8b5ea28 3711 spin_unlock(&log->l_grant_lock);
1da177e4 3712
a14a348b 3713 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
1da177e4
LT
3714 ASSERT(!logerror);
3715 /*
3716 * Force the incore logs to disk before shutting the
3717 * log down completely.
3718 */
a14a348b
CH
3719 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3720
b22cd72c 3721 spin_lock(&log->l_icloglock);
1da177e4 3722 retval = xlog_state_ioerror(log);
b22cd72c 3723 spin_unlock(&log->l_icloglock);
1da177e4
LT
3724 }
3725 /*
3726 * Wake up everybody waiting on xfs_log_force.
3727 * Callback all log item committed functions as if the
3728 * log writes were completed.
3729 */
3730 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3731
3732#ifdef XFSERRORDEBUG
3733 {
3734 xlog_in_core_t *iclog;
3735
b22cd72c 3736 spin_lock(&log->l_icloglock);
1da177e4
LT
3737 iclog = log->l_iclog;
3738 do {
3739 ASSERT(iclog->ic_callback == 0);
3740 iclog = iclog->ic_next;
3741 } while (iclog != log->l_iclog);
b22cd72c 3742 spin_unlock(&log->l_icloglock);
1da177e4
LT
3743 }
3744#endif
3745 /* return non-zero if log IOERROR transition had already happened */
014c2544 3746 return retval;
1da177e4
LT
3747}
3748
ba0f32d4 3749STATIC int
1da177e4
LT
3750xlog_iclogs_empty(xlog_t *log)
3751{
3752 xlog_in_core_t *iclog;
3753
3754 iclog = log->l_iclog;
3755 do {
3756 /* endianness does not matter here, zero is zero in
3757 * any language.
3758 */
3759 if (iclog->ic_header.h_num_logops)
014c2544 3760 return 0;
1da177e4
LT
3761 iclog = iclog->ic_next;
3762 } while (iclog != log->l_iclog);
014c2544 3763 return 1;
1da177e4 3764}