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