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