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