blob: 5eb82306ef76d36eeb4ee7c7a5882b5d5daa0d9c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44
45#define xlog_write_adv_cnt(ptr, len, off, bytes) \
46 { (ptr) += (bytes); \
47 (len) -= (bytes); \
48 (off) += (bytes);}
49
50/* Local miscellaneous function prototypes */
51STATIC int xlog_bdstrat_cb(struct xfs_buf *);
52STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
53 xlog_in_core_t **, xfs_lsn_t *);
54STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
55 xfs_buftarg_t *log_target,
56 xfs_daddr_t blk_offset,
57 int num_bblks);
58STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
59STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100060STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
62 int nentries, xfs_log_ticket_t tic,
63 xfs_lsn_t *start_lsn,
64 xlog_in_core_t **commit_iclog,
65 uint flags);
66
67/* local state machine functions */
68STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
69STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
70STATIC int xlog_state_get_iclog_space(xlog_t *log,
71 int len,
72 xlog_in_core_t **iclog,
73 xlog_ticket_t *ticket,
74 int *continued_write,
75 int *logoffsetp);
76STATIC void xlog_state_put_ticket(xlog_t *log,
77 xlog_ticket_t *tic);
78STATIC int xlog_state_release_iclog(xlog_t *log,
79 xlog_in_core_t *iclog);
80STATIC void xlog_state_switch_iclogs(xlog_t *log,
81 xlog_in_core_t *iclog,
82 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110083STATIC int xlog_state_sync(xlog_t *log,
84 xfs_lsn_t lsn,
85 uint flags,
86 int *log_flushed);
87STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
89
90/* local functions to manipulate grant head */
91STATIC int xlog_grant_log_space(xlog_t *log,
92 xlog_ticket_t *xtic);
93STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
94 int need_bytes);
95STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
96 xlog_ticket_t *ticket);
97STATIC int xlog_regrant_write_log_space(xlog_t *log,
98 xlog_ticket_t *ticket);
99STATIC void xlog_ungrant_log_space(xlog_t *log,
100 xlog_ticket_t *ticket);
101
102
103/* local ticket functions */
104STATIC void xlog_state_ticket_alloc(xlog_t *log);
105STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
106 int unit_bytes,
107 int count,
108 char clientid,
109 uint flags);
110STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
111
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100112#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
114STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
115STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
116 int count, boolean_t syncing);
117STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
118 xfs_lsn_t tail_lsn);
119#else
120#define xlog_verify_dest_ptr(a,b)
121#define xlog_verify_grant_head(a,b)
122#define xlog_verify_iclog(a,b,c,d)
123#define xlog_verify_tail_lsn(a,b,c)
124#endif
125
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000126STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#if defined(XFS_LOG_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void
130xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
131{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000132 unsigned long cnts;
133
134 if (!log->l_grant_trace) {
135 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
136 if (!log->l_grant_trace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
138 }
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000139 /* ticket counts are 1 byte each */
140 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 ktrace_enter(log->l_grant_trace,
143 (void *)tic,
144 (void *)log->l_reserve_headq,
145 (void *)log->l_write_headq,
146 (void *)((unsigned long)log->l_grant_reserve_cycle),
147 (void *)((unsigned long)log->l_grant_reserve_bytes),
148 (void *)((unsigned long)log->l_grant_write_cycle),
149 (void *)((unsigned long)log->l_grant_write_bytes),
150 (void *)((unsigned long)log->l_curr_cycle),
151 (void *)((unsigned long)log->l_curr_block),
152 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
153 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
154 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000155 (void *)((unsigned long)tic->t_trans_type),
156 (void *)cnts,
157 (void *)((unsigned long)tic->t_curr_res),
158 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void
162xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!iclog->ic_trace)
165 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
166 ktrace_enter(iclog->ic_trace,
167 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100168 (void *)((unsigned long)current_pid()),
169 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
170 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
171 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
172 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#else
175#define xlog_trace_loggrant(log,tic,string)
176#define xlog_trace_iclog(iclog,state)
177#endif /* XFS_LOG_TRACE */
178
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100179
180static void
181xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
182{
183 if (*qp) {
184 tic->t_next = (*qp);
185 tic->t_prev = (*qp)->t_prev;
186 (*qp)->t_prev->t_next = tic;
187 (*qp)->t_prev = tic;
188 } else {
189 tic->t_prev = tic->t_next = tic;
190 *qp = tic;
191 }
192
193 tic->t_flags |= XLOG_TIC_IN_Q;
194}
195
196static void
197xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
198{
199 if (tic == tic->t_next) {
200 *qp = NULL;
201 } else {
202 *qp = tic->t_next;
203 tic->t_next->t_prev = tic->t_prev;
204 tic->t_prev->t_next = tic->t_next;
205 }
206
207 tic->t_next = tic->t_prev = NULL;
208 tic->t_flags &= ~XLOG_TIC_IN_Q;
209}
210
211static void
212xlog_grant_sub_space(struct log *log, int bytes)
213{
214 log->l_grant_write_bytes -= bytes;
215 if (log->l_grant_write_bytes < 0) {
216 log->l_grant_write_bytes += log->l_logsize;
217 log->l_grant_write_cycle--;
218 }
219
220 log->l_grant_reserve_bytes -= bytes;
221 if ((log)->l_grant_reserve_bytes < 0) {
222 log->l_grant_reserve_bytes += log->l_logsize;
223 log->l_grant_reserve_cycle--;
224 }
225
226}
227
228static void
229xlog_grant_add_space_write(struct log *log, int bytes)
230{
231 log->l_grant_write_bytes += bytes;
232 if (log->l_grant_write_bytes > log->l_logsize) {
233 log->l_grant_write_bytes -= log->l_logsize;
234 log->l_grant_write_cycle++;
235 }
236}
237
238static void
239xlog_grant_add_space_reserve(struct log *log, int bytes)
240{
241 log->l_grant_reserve_bytes += bytes;
242 if (log->l_grant_reserve_bytes > log->l_logsize) {
243 log->l_grant_reserve_bytes -= log->l_logsize;
244 log->l_grant_reserve_cycle++;
245 }
246}
247
248static inline void
249xlog_grant_add_space(struct log *log, int bytes)
250{
251 xlog_grant_add_space_write(log, bytes);
252 xlog_grant_add_space_reserve(log, bytes);
253}
254
Christoph Hellwig0adba532007-08-30 17:21:46 +1000255static void
256xlog_tic_reset_res(xlog_ticket_t *tic)
257{
258 tic->t_res_num = 0;
259 tic->t_res_arr_sum = 0;
260 tic->t_res_num_ophdrs = 0;
261}
262
263static void
264xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
265{
266 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
267 /* add to overflow and start again */
268 tic->t_res_o_flow += tic->t_res_arr_sum;
269 tic->t_res_num = 0;
270 tic->t_res_arr_sum = 0;
271 }
272
273 tic->t_res_arr[tic->t_res_num].r_len = len;
274 tic->t_res_arr[tic->t_res_num].r_type = type;
275 tic->t_res_arr_sum += len;
276 tic->t_res_num++;
277}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * NOTES:
281 *
282 * 1. currblock field gets updated at startup and after in-core logs
283 * marked as with WANT_SYNC.
284 */
285
286/*
287 * This routine is called when a user of a log manager ticket is done with
288 * the reservation. If the ticket was ever used, then a commit record for
289 * the associated transaction is written out as a log operation header with
290 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
291 * a given ticket. If the ticket was one with a permanent reservation, then
292 * a few operations are done differently. Permanent reservation tickets by
293 * default don't release the reservation. They just commit the current
294 * transaction with the belief that the reservation is still needed. A flag
295 * must be passed in before permanent reservations are actually released.
296 * When these type of tickets are not released, they need to be set into
297 * the inited state again. By doing this, a start record will be written
298 * out when the next write occurs.
299 */
300xfs_lsn_t
301xfs_log_done(xfs_mount_t *mp,
302 xfs_log_ticket_t xtic,
303 void **iclog,
304 uint flags)
305{
306 xlog_t *log = mp->m_log;
307 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
308 xfs_lsn_t lsn = 0;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (XLOG_FORCED_SHUTDOWN(log) ||
311 /*
312 * If nothing was ever written, don't write out commit record.
313 * If we get an error, just continue and give back the log ticket.
314 */
315 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
316 (xlog_commit_record(mp, ticket,
317 (xlog_in_core_t **)iclog, &lsn)))) {
318 lsn = (xfs_lsn_t) -1;
319 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
320 flags |= XFS_LOG_REL_PERM_RESERV;
321 }
322 }
323
324
325 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
326 (flags & XFS_LOG_REL_PERM_RESERV)) {
327 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000328 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * request has been made to release a permanent reservation.
330 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000331 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xlog_ungrant_log_space(log, ticket);
333 xlog_state_put_ticket(log, ticket);
334 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000335 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 xlog_regrant_reserve_log_space(log, ticket);
337 }
338
339 /* If this ticket was a permanent reservation and we aren't
340 * trying to release it, reset the inited flags; so next time
341 * we write, a start record will be written out.
342 */
343 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
344 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
345 ticket->t_flags |= XLOG_TIC_INITED;
346
347 return lsn;
348} /* xfs_log_done */
349
350
351/*
352 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
353 * the force is done synchronously.
354 *
355 * Asynchronous forces are implemented by setting the WANT_SYNC
356 * bit in the appropriate in-core log and then returning.
357 *
358 * Synchronous forces are implemented with a semaphore. All callers
359 * to force a given lsn to disk will wait on a semaphore attached to the
360 * specific in-core log. When given in-core log finally completes its
361 * write to disk, that thread will wake up all threads waiting on the
362 * semaphore.
363 */
364int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100365_xfs_log_force(
366 xfs_mount_t *mp,
367 xfs_lsn_t lsn,
368 uint flags,
369 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100371 xlog_t *log = mp->m_log;
372 int dummy;
373
374 if (!log_flushed)
375 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ASSERT(flags & XFS_LOG_FORCE);
378
379 XFS_STATS_INC(xs_log_force);
380
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100381 if (log->l_flags & XLOG_IO_ERROR)
382 return XFS_ERROR(EIO);
383 if (lsn == 0)
384 return xlog_state_sync_all(log, flags, log_flushed);
385 else
386 return xlog_state_sync(log, lsn, flags, log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387} /* xfs_log_force */
388
389/*
390 * Attaches a new iclog I/O completion callback routine during
391 * transaction commit. If the log is in error state, a non-zero
392 * return code is handed back and the caller is responsible for
393 * executing the callback at an appropriate time.
394 */
395int
396xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
397 void *iclog_hndl, /* iclog to hang callback off */
398 xfs_log_callback_t *cb)
399{
400 xlog_t *log = mp->m_log;
401 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000402 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cb->cb_next = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000405 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
407 if (!abortflg) {
408 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
409 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
410 cb->cb_next = NULL;
411 *(iclog->ic_callback_tail) = cb;
412 iclog->ic_callback_tail = &(cb->cb_next);
413 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000414 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return abortflg;
416} /* xfs_log_notify */
417
418int
419xfs_log_release_iclog(xfs_mount_t *mp,
420 void *iclog_hndl)
421{
422 xlog_t *log = mp->m_log;
423 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
424
425 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000426 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100427 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 return 0;
431}
432
433/*
434 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
435 * to the reservation.
436 * 2. Potentially, push buffers at tail of log to disk.
437 *
438 * Each reservation is going to reserve extra space for a log record header.
439 * When writes happen to the on-disk log, we don't subtract the length of the
440 * log record header from any reservation. By wasting space in each
441 * reservation, we prevent over allocation problems.
442 */
443int
444xfs_log_reserve(xfs_mount_t *mp,
445 int unit_bytes,
446 int cnt,
447 xfs_log_ticket_t *ticket,
448 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000449 uint flags,
450 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 xlog_t *log = mp->m_log;
453 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100454 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
457 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
458
459 if (XLOG_FORCED_SHUTDOWN(log))
460 return XFS_ERROR(EIO);
461
462 XFS_STATS_INC(xs_try_logspace);
463
464 if (*ticket != NULL) {
465 ASSERT(flags & XFS_LOG_PERM_RESERV);
466 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000467 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
469 retval = xlog_regrant_write_log_space(log, internal_ticket);
470 } else {
471 /* may sleep if need to allocate more tickets */
472 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
473 client, flags);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000474 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000476 xlog_trace_loggrant(log, internal_ticket,
477 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
478 "xfs_log_reserve: create new ticket (permanent trans)" :
479 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 xlog_grant_push_ail(mp,
481 (internal_ticket->t_unit_res *
482 internal_ticket->t_cnt));
483 retval = xlog_grant_log_space(log, internal_ticket);
484 }
485
486 return retval;
487} /* xfs_log_reserve */
488
489
490/*
491 * Mount a log filesystem
492 *
493 * mp - ubiquitous xfs mount point structure
494 * log_target - buftarg of on-disk log device
495 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
496 * num_bblocks - Number of BBSIZE blocks in on-disk log
497 *
498 * Return error or zero.
499 */
500int
501xfs_log_mount(xfs_mount_t *mp,
502 xfs_buftarg_t *log_target,
503 xfs_daddr_t blk_offset,
504 int num_bblks)
505{
506 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
507 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
508 else {
509 cmn_err(CE_NOTE,
510 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
511 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000512 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * skip log recovery on a norecovery mount. pretend it all
519 * just worked.
520 */
521 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000522 int error, readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000525 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Eric Sandeen65be6052006-01-11 15:34:19 +1100527 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000530 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (error) {
532 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Nathan Scottc41564b2006-03-29 08:55:14 +1000533 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return error;
535 }
536 }
537
538 /* Normal transactions can now occur */
539 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
540
541 /* End mounting message in xfs_log_mount_finish */
542 return 0;
543} /* xfs_log_mount */
544
545/*
546 * Finish the recovery of the file system. This is separate from
547 * the xfs_log_mount() call, because it depends on the code in
548 * xfs_mountfs() to read in the root and real-time bitmap inodes
549 * between calling xfs_log_mount() and here.
550 *
551 * mp - ubiquitous xfs mount point structure
552 */
553int
554xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
555{
556 int error;
557
558 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
559 error = xlog_recover_finish(mp->m_log, mfsi_flags);
560 else {
561 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000562 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 return error;
566}
567
568/*
569 * Unmount processing for the log.
570 */
571int
572xfs_log_unmount(xfs_mount_t *mp)
573{
574 int error;
575
576 error = xfs_log_unmount_write(mp);
577 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
582 * Final log writes as part of unmount.
583 *
584 * Mark the filesystem clean as unmount happens. Note that during relocation
585 * this routine needs to be executed as part of source-bag while the
586 * deallocation must not be done until source-end.
587 */
588
589/*
590 * Unmount record used to have a string "Unmount filesystem--" in the
591 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
592 * We just write the magic number now since that particular field isn't
593 * currently architecture converted and "nUmount" is a bit foo.
594 * As far as I know, there weren't any dependencies on the old behaviour.
595 */
596
597int
598xfs_log_unmount_write(xfs_mount_t *mp)
599{
600 xlog_t *log = mp->m_log;
601 xlog_in_core_t *iclog;
602#ifdef DEBUG
603 xlog_in_core_t *first_iclog;
604#endif
605 xfs_log_iovec_t reg[1];
606 xfs_log_ticket_t tic = NULL;
607 xfs_lsn_t lsn;
608 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* the data section must be 32 bit size aligned */
611 struct {
612 __uint16_t magic;
613 __uint16_t pad1;
614 __uint32_t pad2; /* may as well make it 64 bits */
615 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /*
618 * Don't write out unmount record on read-only mounts.
619 * Or, if we are doing a forced umount (typically because of IO errors).
620 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000621 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623
624 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
625
626#ifdef DEBUG
627 first_iclog = iclog = log->l_iclog;
628 do {
629 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
630 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
631 ASSERT(iclog->ic_offset == 0);
632 }
633 iclog = iclog->ic_next;
634 } while (iclog != first_iclog);
635#endif
636 if (! (XLOG_FORCED_SHUTDOWN(log))) {
637 reg[0].i_addr = (void*)&magic;
638 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000639 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Tim Shimmin955e47a2006-09-28 11:04:16 +1000641 error = xfs_log_reserve(mp, 600, 1, &tic,
642 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!error) {
644 /* remove inited flag */
645 ((xlog_ticket_t *)tic)->t_flags = 0;
646 error = xlog_write(mp, reg, 1, tic, &lsn,
647 NULL, XLOG_UNMOUNT_TRANS);
648 /*
649 * At this point, we're umounting anyway,
650 * so there's no point in transitioning log state
651 * to IOERROR. Just continue...
652 */
653 }
654
655 if (error) {
656 xfs_fs_cmn_err(CE_ALERT, mp,
657 "xfs_log_unmount: unmount record failed");
658 }
659
660
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000661 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 iclog = log->l_iclog;
663 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000664 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xlog_state_want_sync(log, iclog);
666 (void) xlog_state_release_iclog(log, iclog);
667
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000668 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
670 iclog->ic_state == XLOG_STATE_DIRTY)) {
671 if (!XLOG_FORCED_SHUTDOWN(log)) {
672 sv_wait(&iclog->ic_forcesema, PMEM,
673 &log->l_icloglock, s);
674 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000675 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000678 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000680 if (tic) {
681 xlog_trace_loggrant(log, tic, "unmount rec");
682 xlog_ungrant_log_space(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xlog_state_put_ticket(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 } else {
686 /*
687 * We're already in forced_shutdown mode, couldn't
688 * even attempt to write out the unmount transaction.
689 *
690 * Go through the motions of sync'ing and releasing
691 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000692 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * be in progress. Do this as a separate section of
694 * code so we'll know if we ever get stuck here that
695 * we're in this odd situation of trying to unmount
696 * a file system that went into forced_shutdown as
697 * the result of an unmount..
698 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000699 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 iclog = log->l_iclog;
701 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000702 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 xlog_state_want_sync(log, iclog);
705 (void) xlog_state_release_iclog(log, iclog);
706
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000707 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
710 || iclog->ic_state == XLOG_STATE_DIRTY
711 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
712
713 sv_wait(&iclog->ic_forcesema, PMEM,
714 &log->l_icloglock, s);
715 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000716 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 }
719
720 return 0;
721} /* xfs_log_unmount_write */
722
723/*
724 * Deallocate log structures for unmount/relocation.
725 */
726void
727xfs_log_unmount_dealloc(xfs_mount_t *mp)
728{
Nathan Scottc41564b2006-03-29 08:55:14 +1000729 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/*
733 * Write region vectors to log. The write happens using the space reservation
734 * of the ticket (tic). It is not a requirement that all writes for a given
735 * transaction occur with one call to xfs_log_write().
736 */
737int
738xfs_log_write(xfs_mount_t * mp,
739 xfs_log_iovec_t reg[],
740 int nentries,
741 xfs_log_ticket_t tic,
742 xfs_lsn_t *start_lsn)
743{
744 int error;
745 xlog_t *log = mp->m_log;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (XLOG_FORCED_SHUTDOWN(log))
748 return XFS_ERROR(EIO);
749
750 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000751 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754} /* xfs_log_write */
755
756
757void
758xfs_log_move_tail(xfs_mount_t *mp,
759 xfs_lsn_t tail_lsn)
760{
761 xlog_ticket_t *tic;
762 xlog_t *log = mp->m_log;
763 int need_bytes, free_bytes, cycle, bytes;
764 SPLDECL(s);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (XLOG_FORCED_SHUTDOWN(log))
767 return;
768 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
769
770 if (tail_lsn == 0) {
771 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000772 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000774 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777 s = GRANT_LOCK(log);
778
779 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
780 * tail_lsn.
781 */
782 if (tail_lsn != 1) {
783 log->l_tail_lsn = tail_lsn;
784 }
785
786 if ((tic = log->l_write_headq)) {
787#ifdef DEBUG
788 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
789 panic("Recovery problem");
790#endif
791 cycle = log->l_grant_write_cycle;
792 bytes = log->l_grant_write_bytes;
793 free_bytes = xlog_space_left(log, cycle, bytes);
794 do {
795 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
796
797 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
798 break;
799 tail_lsn = 0;
800 free_bytes -= tic->t_unit_res;
801 sv_signal(&tic->t_sema);
802 tic = tic->t_next;
803 } while (tic != log->l_write_headq);
804 }
805 if ((tic = log->l_reserve_headq)) {
806#ifdef DEBUG
807 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
808 panic("Recovery problem");
809#endif
810 cycle = log->l_grant_reserve_cycle;
811 bytes = log->l_grant_reserve_bytes;
812 free_bytes = xlog_space_left(log, cycle, bytes);
813 do {
814 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
815 need_bytes = tic->t_unit_res*tic->t_cnt;
816 else
817 need_bytes = tic->t_unit_res;
818 if (free_bytes < need_bytes && tail_lsn != 1)
819 break;
820 tail_lsn = 0;
821 free_bytes -= need_bytes;
822 sv_signal(&tic->t_sema);
823 tic = tic->t_next;
824 } while (tic != log->l_reserve_headq);
825 }
826 GRANT_UNLOCK(log, s);
827} /* xfs_log_move_tail */
828
829/*
830 * Determine if we have a transaction that has gone to disk
831 * that needs to be covered. Log activity needs to be idle (no AIL and
832 * nothing in the iclogs). And, we need to be in the right state indicating
833 * something has gone out.
834 */
835int
836xfs_log_need_covered(xfs_mount_t *mp)
837{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int needed = 0, gen;
839 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David Chinner92821e22007-05-24 15:26:31 +1000841 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 0;
843
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000844 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
846 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
847 && !xfs_trans_first_ail(mp, &gen)
848 && xlog_iclogs_empty(log)) {
849 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
850 log->l_covered_state = XLOG_STATE_COVER_DONE;
851 else {
852 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
853 log->l_covered_state = XLOG_STATE_COVER_DONE2;
854 }
855 needed = 1;
856 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000857 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100858 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861/******************************************************************************
862 *
863 * local routines
864 *
865 ******************************************************************************
866 */
867
868/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
869 * The log manager must keep track of the last LR which was committed
870 * to disk. The lsn of this LR will become the new tail_lsn whenever
871 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
872 * the situation where stuff could be written into the log but nothing
873 * was ever in the AIL when asked. Eventually, we panic since the
874 * tail hits the head.
875 *
876 * We may be holding the log iclog lock upon entering this routine.
877 */
878xfs_lsn_t
879xlog_assign_tail_lsn(xfs_mount_t *mp)
880{
881 xfs_lsn_t tail_lsn;
882 SPLDECL(s);
883 xlog_t *log = mp->m_log;
884
885 tail_lsn = xfs_trans_tail_ail(mp);
886 s = GRANT_LOCK(log);
887 if (tail_lsn != 0) {
888 log->l_tail_lsn = tail_lsn;
889 } else {
890 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
891 }
892 GRANT_UNLOCK(log, s);
893
894 return tail_lsn;
895} /* xlog_assign_tail_lsn */
896
897
898/*
899 * Return the space in the log between the tail and the head. The head
900 * is passed in the cycle/bytes formal parms. In the special case where
901 * the reserve head has wrapped passed the tail, this calculation is no
902 * longer valid. In this case, just return 0 which means there is no space
903 * in the log. This works for all places where this function is called
904 * with the reserve head. Of course, if the write head were to ever
905 * wrap the tail, we should blow up. Rather than catch this case here,
906 * we depend on other ASSERTions in other parts of the code. XXXmiken
907 *
908 * This code also handles the case where the reservation head is behind
909 * the tail. The details of this case are described below, but the end
910 * result is that we return the size of the log as the amount of space left.
911 */
912int
913xlog_space_left(xlog_t *log, int cycle, int bytes)
914{
915 int free_bytes;
916 int tail_bytes;
917 int tail_cycle;
918
919 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
920 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
921 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
922 free_bytes = log->l_logsize - (bytes - tail_bytes);
923 } else if ((tail_cycle + 1) < cycle) {
924 return 0;
925 } else if (tail_cycle < cycle) {
926 ASSERT(tail_cycle == (cycle - 1));
927 free_bytes = tail_bytes - bytes;
928 } else {
929 /*
930 * The reservation head is behind the tail.
931 * In this case we just want to return the size of the
932 * log as the amount of space left.
933 */
934 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
935 "xlog_space_left: head behind tail\n"
936 " tail_cycle = %d, tail_bytes = %d\n"
937 " GH cycle = %d, GH bytes = %d",
938 tail_cycle, tail_bytes, cycle, bytes);
939 ASSERT(0);
940 free_bytes = log->l_logsize;
941 }
942 return free_bytes;
943} /* xlog_space_left */
944
945
946/*
947 * Log function which is called when an io completes.
948 *
949 * The log manager needs its own routine, in order to control what
950 * happens with the buffer after the write completes.
951 */
952void
953xlog_iodone(xfs_buf_t *bp)
954{
955 xlog_in_core_t *iclog;
956 xlog_t *l;
957 int aborted;
958
959 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
960 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
961 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
962 aborted = 0;
963
964 /*
965 * Some versions of cpp barf on the recursive definition of
966 * ic_log -> hic_fields.ic_log and expand ic_log twice when
967 * it is passed through two macros. Workaround broken cpp.
968 */
969 l = iclog->ic_log;
970
971 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000972 * If the ordered flag has been removed by a lower
973 * layer, it means the underlyin device no longer supports
974 * barrier I/O. Warn loudly and turn off barriers.
975 */
976 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
977 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
978 xfs_fs_cmn_err(CE_WARN, l->l_mp,
979 "xlog_iodone: Barriers are no longer supported"
980 " by device. Disabling barriers\n");
981 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
982 }
983
984 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * Race to shutdown the filesystem if we see an error.
986 */
987 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
988 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
989 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
990 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000991 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /*
993 * This flag will be propagated to the trans-committed
994 * callback routines to let them know that the log-commit
995 * didn't succeed.
996 */
997 aborted = XFS_LI_ABORTED;
998 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
999 aborted = XFS_LI_ABORTED;
1000 }
David Chinner3db296f2007-05-14 18:24:16 +10001001
1002 /* log I/O is always issued ASYNC */
1003 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001005 /*
1006 * do not reference the buffer (bp) here as we could race
1007 * with it being freed after writing the unmount record to the
1008 * log.
1009 */
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011} /* xlog_iodone */
1012
1013/*
1014 * The bdstrat callback function for log bufs. This gives us a central
1015 * place to trap bufs in case we get hit by a log I/O error and need to
1016 * shutdown. Actually, in practice, even when we didn't get a log error,
1017 * we transition the iclogs to IOERROR state *after* flushing all existing
1018 * iclogs to disk. This is because we don't want anymore new transactions to be
1019 * started or completed afterwards.
1020 */
1021STATIC int
1022xlog_bdstrat_cb(struct xfs_buf *bp)
1023{
1024 xlog_in_core_t *iclog;
1025
1026 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1027
1028 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1029 /* note for irix bstrat will need struct bdevsw passed
1030 * Fix the following macro if the code ever is merged
1031 */
1032 XFS_bdstrat(bp);
1033 return 0;
1034 }
1035
1036 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1037 XFS_BUF_ERROR(bp, EIO);
1038 XFS_BUF_STALE(bp);
1039 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001040 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042
1043}
1044
1045/*
1046 * Return size of each in-core log record buffer.
1047 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001048 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *
1050 * If the filesystem blocksize is too large, we may need to choose a
1051 * larger size since the directory code currently logs entire blocks.
1052 */
1053
1054STATIC void
1055xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1056 xlog_t *log)
1057{
1058 int size;
1059 int xhdrs;
1060
Eric Sandeen1cb51252007-08-16 16:24:43 +10001061 if (mp->m_logbufs <= 0)
1062 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1063 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001064 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /*
1067 * Buffer size passed in from mount system call.
1068 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001069 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 size = log->l_iclog_size = mp->m_logbsize;
1071 log->l_iclog_size_log = 0;
1072 while (size != 1) {
1073 log->l_iclog_size_log++;
1074 size >>= 1;
1075 }
1076
1077 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1078 /* # headers = size / 32K
1079 * one header holds cycles from 32K of data
1080 */
1081
1082 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1083 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1084 xhdrs++;
1085 log->l_iclog_hsize = xhdrs << BBSHIFT;
1086 log->l_iclog_heads = xhdrs;
1087 } else {
1088 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1089 log->l_iclog_hsize = BBSIZE;
1090 log->l_iclog_heads = 1;
1091 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001092 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Eric Sandeen1cb51252007-08-16 16:24:43 +10001095 /* All machines use 32KB buffers by default. */
1096 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1097 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /* the default log size is 16k or 32k which is one header sector */
1100 log->l_iclog_hsize = BBSIZE;
1101 log->l_iclog_heads = 1;
1102
1103 /*
1104 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1105 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1106 */
1107 if (mp->m_sb.sb_blocksize >= 16*1024) {
1108 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1109 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001110 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 switch (mp->m_sb.sb_blocksize) {
1112 case 16*1024: /* 16 KB */
1113 log->l_iclog_bufs = 3;
1114 break;
1115 case 32*1024: /* 32 KB */
1116 log->l_iclog_bufs = 4;
1117 break;
1118 case 64*1024: /* 64 KB */
1119 log->l_iclog_bufs = 8;
1120 break;
1121 default:
1122 xlog_panic("XFS: Invalid blocksize");
1123 break;
1124 }
1125 }
1126 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001127
1128done: /* are we being asked to make the sizes selected above visible? */
1129 if (mp->m_logbufs == 0)
1130 mp->m_logbufs = log->l_iclog_bufs;
1131 if (mp->m_logbsize == 0)
1132 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133} /* xlog_get_iclog_buffer_size */
1134
1135
1136/*
1137 * This routine initializes some of the log structure for a given mount point.
1138 * Its primary purpose is to fill in enough, so recovery can occur. However,
1139 * some other stuff may be filled in too.
1140 */
1141STATIC xlog_t *
1142xlog_alloc_log(xfs_mount_t *mp,
1143 xfs_buftarg_t *log_target,
1144 xfs_daddr_t blk_offset,
1145 int num_bblks)
1146{
1147 xlog_t *log;
1148 xlog_rec_header_t *head;
1149 xlog_in_core_t **iclogp;
1150 xlog_in_core_t *iclog, *prev_iclog=NULL;
1151 xfs_buf_t *bp;
1152 int i;
1153 int iclogsize;
1154
1155 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1156
1157 log->l_mp = mp;
1158 log->l_targ = log_target;
1159 log->l_logsize = BBTOB(num_bblks);
1160 log->l_logBBstart = blk_offset;
1161 log->l_logBBsize = num_bblks;
1162 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1163 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1164
1165 log->l_prev_block = -1;
1166 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1167 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1168 log->l_last_sync_lsn = log->l_tail_lsn;
1169 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1170 log->l_grant_reserve_cycle = 1;
1171 log->l_grant_write_cycle = 1;
1172
1173 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1174 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1175 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1176 /* for larger sector sizes, must have v2 or external log */
1177 ASSERT(log->l_sectbb_log == 0 ||
1178 log->l_logBBstart == 0 ||
1179 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1180 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1181 }
1182 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1183
1184 xlog_get_iclog_buffer_size(mp, log);
1185
1186 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1187 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1188 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1189 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1190 ASSERT(XFS_BUF_ISBUSY(bp));
1191 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1192 log->l_xbuf = bp;
1193
1194 spinlock_init(&log->l_icloglock, "iclog");
1195 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1196 initnsema(&log->l_flushsema, 0, "ic-flush");
1197 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1198
1199 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1200 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1201
1202 iclogp = &log->l_iclog;
1203 /*
1204 * The amount of memory to allocate for the iclog structure is
1205 * rather funky due to the way the structure is defined. It is
1206 * done this way so that we can use different sizes for machines
1207 * with different amounts of memory. See the definition of
1208 * xlog_in_core_t in xfs_log_priv.h for details.
1209 */
1210 iclogsize = log->l_iclog_size;
1211 ASSERT(log->l_iclog_size >= 4096);
1212 for (i=0; i < log->l_iclog_bufs; i++) {
1213 *iclogp = (xlog_in_core_t *)
1214 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1215 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 iclog->ic_prev = prev_iclog;
1217 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001218
1219 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1220 if (!XFS_BUF_CPSEMA(bp))
1221 ASSERT(0);
1222 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1223 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1224 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1225 iclog->ic_bp = bp;
1226 iclog->hic_data = bp->b_addr;
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1229
1230 head = &iclog->ic_header;
1231 memset(head, 0, sizeof(xlog_rec_header_t));
1232 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1233 INT_SET(head->h_version, ARCH_CONVERT,
1234 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1235 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1236 /* new fields */
1237 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1238 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1242 iclog->ic_state = XLOG_STATE_ACTIVE;
1243 iclog->ic_log = log;
1244 iclog->ic_callback_tail = &(iclog->ic_callback);
1245 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1246
1247 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1248 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1249 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1250 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1251
1252 iclogp = &iclog->ic_next;
1253 }
1254 *iclogp = log->l_iclog; /* complete ring */
1255 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1256
1257 return log;
1258} /* xlog_alloc_log */
1259
1260
1261/*
1262 * Write out the commit record of a transaction associated with the given
1263 * ticket. Return the lsn of the commit record.
1264 */
1265STATIC int
1266xlog_commit_record(xfs_mount_t *mp,
1267 xlog_ticket_t *ticket,
1268 xlog_in_core_t **iclog,
1269 xfs_lsn_t *commitlsnp)
1270{
1271 int error;
1272 xfs_log_iovec_t reg[1];
1273
1274 reg[0].i_addr = NULL;
1275 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001276 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 ASSERT_ALWAYS(iclog);
1279 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1280 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001281 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001283 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284} /* xlog_commit_record */
1285
1286
1287/*
1288 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1289 * log space. This code pushes on the lsn which would supposedly free up
1290 * the 25% which we want to leave free. We may need to adopt a policy which
1291 * pushes on an lsn which is further along in the log once we reach the high
1292 * water mark. In this manner, we would be creating a low water mark.
1293 */
1294void
1295xlog_grant_push_ail(xfs_mount_t *mp,
1296 int need_bytes)
1297{
1298 xlog_t *log = mp->m_log; /* pointer to the log */
1299 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1300 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1301 int free_blocks; /* free blocks left to write to */
1302 int free_bytes; /* free bytes left to write to */
1303 int threshold_block; /* block in lsn we'd like to be at */
1304 int threshold_cycle; /* lsn cycle we'd like to be at */
1305 int free_threshold;
1306 SPLDECL(s);
1307
1308 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1309
1310 s = GRANT_LOCK(log);
1311 free_bytes = xlog_space_left(log,
1312 log->l_grant_reserve_cycle,
1313 log->l_grant_reserve_bytes);
1314 tail_lsn = log->l_tail_lsn;
1315 free_blocks = BTOBBT(free_bytes);
1316
1317 /*
1318 * Set the threshold for the minimum number of free blocks in the
1319 * log to the maximum of what the caller needs, one quarter of the
1320 * log, and 256 blocks.
1321 */
1322 free_threshold = BTOBB(need_bytes);
1323 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1324 free_threshold = MAX(free_threshold, 256);
1325 if (free_blocks < free_threshold) {
1326 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1327 threshold_cycle = CYCLE_LSN(tail_lsn);
1328 if (threshold_block >= log->l_logBBsize) {
1329 threshold_block -= log->l_logBBsize;
1330 threshold_cycle += 1;
1331 }
1332 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1333 threshold_block);
1334
1335 /* Don't pass in an lsn greater than the lsn of the last
1336 * log record known to be on disk.
1337 */
1338 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1339 threshold_lsn = log->l_last_sync_lsn;
1340 }
1341 GRANT_UNLOCK(log, s);
1342
1343 /*
1344 * Get the transaction layer to kick the dirty buffers out to
1345 * disk asynchronously. No point in trying to do this if
1346 * the filesystem is shutting down.
1347 */
1348 if (threshold_lsn &&
1349 !XLOG_FORCED_SHUTDOWN(log))
1350 xfs_trans_push_ail(mp, threshold_lsn);
1351} /* xlog_grant_push_ail */
1352
1353
1354/*
1355 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1356 * fashion. Previously, we should have moved the current iclog
1357 * ptr in the log to point to the next available iclog. This allows further
1358 * write to continue while this code syncs out an iclog ready to go.
1359 * Before an in-core log can be written out, the data section must be scanned
1360 * to save away the 1st word of each BBSIZE block into the header. We replace
1361 * it with the current cycle count. Each BBSIZE block is tagged with the
1362 * cycle count because there in an implicit assumption that drives will
1363 * guarantee that entire 512 byte blocks get written at once. In other words,
1364 * we can't have part of a 512 byte block written and part not written. By
1365 * tagging each block, we will know which blocks are valid when recovering
1366 * after an unclean shutdown.
1367 *
1368 * This routine is single threaded on the iclog. No other thread can be in
1369 * this routine with the same iclog. Changing contents of iclog can there-
1370 * fore be done without grabbing the state machine lock. Updating the global
1371 * log will require grabbing the lock though.
1372 *
1373 * The entire log manager uses a logical block numbering scheme. Only
1374 * log_sync (and then only bwrite()) know about the fact that the log may
1375 * not start with block zero on a given device. The log block start offset
1376 * is added immediately before calling bwrite().
1377 */
1378
1379int
1380xlog_sync(xlog_t *log,
1381 xlog_in_core_t *iclog)
1382{
1383 xfs_caddr_t dptr; /* pointer to byte sized element */
1384 xfs_buf_t *bp;
1385 int i, ops;
1386 uint count; /* byte count of bwrite */
1387 uint count_init; /* initial count before roundup */
1388 int roundoff; /* roundoff to BB or stripe */
1389 int split = 0; /* split write into two regions */
1390 int error;
1391 SPLDECL(s);
1392 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1393
1394 XFS_STATS_INC(xs_log_writes);
1395 ASSERT(iclog->ic_refcnt == 0);
1396
1397 /* Add for LR header */
1398 count_init = log->l_iclog_hsize + iclog->ic_offset;
1399
1400 /* Round out the log write size */
1401 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1402 /* we have a v2 stripe unit to use */
1403 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1404 } else {
1405 count = BBTOB(BTOBB(count_init));
1406 }
1407 roundoff = count - count_init;
1408 ASSERT(roundoff >= 0);
1409 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1410 roundoff < log->l_mp->m_sb.sb_logsunit)
1411 ||
1412 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1413 roundoff < BBTOB(1)));
1414
1415 /* move grant heads by roundoff in sync */
1416 s = GRANT_LOCK(log);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001417 xlog_grant_add_space(log, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 GRANT_UNLOCK(log, s);
1419
1420 /* put cycle number in every block */
1421 xlog_pack_data(log, iclog, roundoff);
1422
1423 /* real byte length */
1424 if (v2) {
1425 INT_SET(iclog->ic_header.h_len,
1426 ARCH_CONVERT,
1427 iclog->ic_offset + roundoff);
1428 } else {
1429 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1430 }
1431
1432 /* put ops count in correct order */
1433 ops = iclog->ic_header.h_num_logops;
1434 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1435
Nathan Scottf5faad72006-07-28 17:04:44 +10001436 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1438 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1439 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1440
1441 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1442
1443 /* Do we need to split this write into 2 parts? */
1444 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1445 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1446 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1447 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1448 } else {
1449 iclog->ic_bwritecnt = 1;
1450 }
David Chinner511105b2007-05-24 15:21:57 +10001451 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001453 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 XFS_BUF_BUSY(bp);
1455 XFS_BUF_ASYNC(bp);
1456 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001457 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001458 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001460 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001461 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1464 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1465
1466 xlog_verify_iclog(log, iclog, count, B_TRUE);
1467
1468 /* account for log which doesn't start at block #0 */
1469 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1470 /*
1471 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1472 * is shutting down.
1473 */
1474 XFS_BUF_WRITE(bp);
1475
1476 if ((error = XFS_bwrite(bp))) {
1477 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1478 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001479 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001482 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1484 (unsigned long)1);
1485 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1486 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1487 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1488 (__psint_t)count), split);
1489 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001490 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 XFS_BUF_BUSY(bp);
1492 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001493 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1494 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 dptr = XFS_BUF_PTR(bp);
1496 /*
1497 * Bump the cycle numbers at the start of each block
1498 * since this part of the buffer is at the start of
1499 * a new cycle. Watch out for the header magic number
1500 * case, though.
1501 */
1502 for (i=0; i<split; i += BBSIZE) {
1503 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1504 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1505 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1506 dptr += BBSIZE;
1507 }
1508
1509 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1510 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1511
Nathan Scottc41564b2006-03-29 08:55:14 +10001512 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1514 XFS_BUF_WRITE(bp);
1515 if ((error = XFS_bwrite(bp))) {
1516 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1517 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001518 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522} /* xlog_sync */
1523
1524
1525/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001526 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
1528void
Nathan Scottc41564b2006-03-29 08:55:14 +10001529xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 xlog_in_core_t *iclog, *next_iclog;
1532 xlog_ticket_t *tic, *next_tic;
1533 int i;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 iclog = log->l_iclog;
1536 for (i=0; i<log->l_iclog_bufs; i++) {
1537 sv_destroy(&iclog->ic_forcesema);
1538 sv_destroy(&iclog->ic_writesema);
1539 xfs_buf_free(iclog->ic_bp);
1540#ifdef XFS_LOG_TRACE
1541 if (iclog->ic_trace != NULL) {
1542 ktrace_free(iclog->ic_trace);
1543 }
1544#endif
1545 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 kmem_free(iclog, sizeof(xlog_in_core_t));
1547 iclog = next_iclog;
1548 }
1549 freesema(&log->l_flushsema);
1550 spinlock_destroy(&log->l_icloglock);
1551 spinlock_destroy(&log->l_grant_lock);
1552
1553 /* XXXsup take a look at this again. */
1554 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1555 !XLOG_FORCED_SHUTDOWN(log)) {
1556 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001557 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 log->l_ticket_cnt, log->l_ticket_tcnt);
1559 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1560
1561 } else {
1562 tic = log->l_unmount_free;
1563 while (tic) {
1564 next_tic = tic->t_next;
1565 kmem_free(tic, NBPP);
1566 tic = next_tic;
1567 }
1568 }
1569 xfs_buf_free(log->l_xbuf);
1570#ifdef XFS_LOG_TRACE
1571 if (log->l_trace != NULL) {
1572 ktrace_free(log->l_trace);
1573 }
1574 if (log->l_grant_trace != NULL) {
1575 ktrace_free(log->l_grant_trace);
1576 }
1577#endif
1578 log->l_mp->m_log = NULL;
1579 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001580} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582/*
1583 * Update counters atomically now that memcpy is done.
1584 */
1585/* ARGSUSED */
1586static inline void
1587xlog_state_finish_copy(xlog_t *log,
1588 xlog_in_core_t *iclog,
1589 int record_cnt,
1590 int copy_bytes)
1591{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001592 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 iclog->ic_header.h_num_logops += record_cnt;
1595 iclog->ic_offset += copy_bytes;
1596
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001597 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598} /* xlog_state_finish_copy */
1599
1600
1601
1602
1603/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001604 * print out info relating to regions written which consume
1605 * the reservation
1606 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001607STATIC void
1608xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1609{
1610 uint i;
1611 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1612
1613 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1614 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1615 "bformat",
1616 "bchunk",
1617 "efi_format",
1618 "efd_format",
1619 "iformat",
1620 "icore",
1621 "iext",
1622 "ibroot",
1623 "ilocal",
1624 "iattr_ext",
1625 "iattr_broot",
1626 "iattr_local",
1627 "qformat",
1628 "dquot",
1629 "quotaoff",
1630 "LR header",
1631 "unmount",
1632 "commit",
1633 "trans header"
1634 };
1635 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1636 "SETATTR_NOT_SIZE",
1637 "SETATTR_SIZE",
1638 "INACTIVE",
1639 "CREATE",
1640 "CREATE_TRUNC",
1641 "TRUNCATE_FILE",
1642 "REMOVE",
1643 "LINK",
1644 "RENAME",
1645 "MKDIR",
1646 "RMDIR",
1647 "SYMLINK",
1648 "SET_DMATTRS",
1649 "GROWFS",
1650 "STRAT_WRITE",
1651 "DIOSTRAT",
1652 "WRITE_SYNC",
1653 "WRITEID",
1654 "ADDAFORK",
1655 "ATTRINVAL",
1656 "ATRUNCATE",
1657 "ATTR_SET",
1658 "ATTR_RM",
1659 "ATTR_FLAG",
1660 "CLEAR_AGI_BUCKET",
1661 "QM_SBCHANGE",
1662 "DUMMY1",
1663 "DUMMY2",
1664 "QM_QUOTAOFF",
1665 "QM_DQALLOC",
1666 "QM_SETQLIM",
1667 "QM_DQCLUSTER",
1668 "QM_QINOCREATE",
1669 "QM_QUOTAOFF_END",
1670 "SB_UNIT",
1671 "FSYNC_TS",
1672 "GROWFSRT_ALLOC",
1673 "GROWFSRT_ZERO",
1674 "GROWFSRT_FREE",
1675 "SWAPEXT"
1676 };
1677
1678 xfs_fs_cmn_err(CE_WARN, mp,
1679 "xfs_log_write: reservation summary:\n"
1680 " trans type = %s (%u)\n"
1681 " unit res = %d bytes\n"
1682 " current res = %d bytes\n"
1683 " total reg = %u bytes (o/flow = %u bytes)\n"
1684 " ophdrs = %u (ophdr space = %u bytes)\n"
1685 " ophdr + reg = %u bytes\n"
1686 " num regions = %u\n",
1687 ((ticket->t_trans_type <= 0 ||
1688 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1689 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1690 ticket->t_trans_type,
1691 ticket->t_unit_res,
1692 ticket->t_curr_res,
1693 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1694 ticket->t_res_num_ophdrs, ophdr_spc,
1695 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001696 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001697 ticket->t_res_num);
1698
1699 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001700 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001701 cmn_err(CE_WARN,
1702 "region[%u]: %s - %u bytes\n",
1703 i,
1704 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1705 "bad-rtype" : res_type_str[r_type-1]),
1706 ticket->t_res_arr[i].r_len);
1707 }
1708}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001709
1710/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 * Write some region out to in-core log
1712 *
1713 * This will be called when writing externally provided regions or when
1714 * writing out a commit record for a given transaction.
1715 *
1716 * General algorithm:
1717 * 1. Find total length of this write. This may include adding to the
1718 * lengths passed in.
1719 * 2. Check whether we violate the tickets reservation.
1720 * 3. While writing to this iclog
1721 * A. Reserve as much space in this iclog as can get
1722 * B. If this is first write, save away start lsn
1723 * C. While writing this region:
1724 * 1. If first write of transaction, write start record
1725 * 2. Write log operation header (header per region)
1726 * 3. Find out if we can fit entire region into this iclog
1727 * 4. Potentially, verify destination memcpy ptr
1728 * 5. Memcpy (partial) region
1729 * 6. If partial copy, release iclog; otherwise, continue
1730 * copying more regions into current iclog
1731 * 4. Mark want sync bit (in simulation mode)
1732 * 5. Release iclog for potential flush to on-disk log.
1733 *
1734 * ERRORS:
1735 * 1. Panic if reservation is overrun. This should never happen since
1736 * reservation amounts are generated internal to the filesystem.
1737 * NOTES:
1738 * 1. Tickets are single threaded data structures.
1739 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1740 * syncing routine. When a single log_write region needs to span
1741 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1742 * on all log operation writes which don't contain the end of the
1743 * region. The XLOG_END_TRANS bit is used for the in-core log
1744 * operation which contains the end of the continued log_write region.
1745 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1746 * we don't really know exactly how much space will be used. As a result,
1747 * we don't update ic_offset until the end when we know exactly how many
1748 * bytes have been written out.
1749 */
1750int
1751xlog_write(xfs_mount_t * mp,
1752 xfs_log_iovec_t reg[],
1753 int nentries,
1754 xfs_log_ticket_t tic,
1755 xfs_lsn_t *start_lsn,
1756 xlog_in_core_t **commit_iclog,
1757 uint flags)
1758{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001759 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001761 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 __psint_t ptr; /* copy address into data region */
1764 int len; /* # xlog_write() bytes 2 still copy */
1765 int index; /* region index currently copying */
1766 int log_offset; /* offset (from 0) into data region */
1767 int start_rec_copy; /* # bytes to copy for start record */
1768 int partial_copy; /* did we split a region? */
1769 int partial_copy_len;/* # bytes copied if split region */
1770 int need_copy; /* # bytes need to memcpy this region */
1771 int copy_len; /* # bytes actually memcpy'ing */
1772 int copy_off; /* # bytes from entry start */
1773 int contwr; /* continued write of in-core log? */
1774 int error;
1775 int record_cnt = 0, data_cnt = 0;
1776
1777 partial_copy_len = partial_copy = 0;
1778
1779 /* Calculate potential maximum space. Each region gets its own
1780 * xlog_op_header_t and may need to be double word aligned.
1781 */
1782 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001783 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001785 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 for (index = 0; index < nentries; index++) {
1789 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001790 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001792 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 contwr = *start_lsn = 0;
1795
1796 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001797 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798#ifdef DEBUG
1799 xlog_panic(
1800 "xfs_log_write: reservation ran out. Need to up reservation");
1801#else
1802 /* Customer configurable panic */
1803 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1804 "xfs_log_write: reservation ran out. Need to up reservation");
1805 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001806 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#endif
1808 } else
1809 ticket->t_curr_res -= len;
1810
1811 for (index = 0; index < nentries; ) {
1812 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1813 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001814 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 ASSERT(log_offset <= iclog->ic_size - 1);
1817 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1818
1819 /* start_lsn is the first lsn written to. That's all we need. */
1820 if (! *start_lsn)
1821 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1822
1823 /* This loop writes out as many regions as can fit in the amount
1824 * of space which was allocated by xlog_state_get_iclog_space().
1825 */
1826 while (index < nentries) {
1827 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1828 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1829 start_rec_copy = 0;
1830
1831 /* If first write for transaction, insert start record.
1832 * We can't be trying to commit if we are inited. We can't
1833 * have any "partial_copy" if we are inited.
1834 */
1835 if (ticket->t_flags & XLOG_TIC_INITED) {
1836 logop_head = (xlog_op_header_t *)ptr;
1837 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1838 logop_head->oh_clientid = ticket->t_clientid;
1839 logop_head->oh_len = 0;
1840 logop_head->oh_flags = XLOG_START_TRANS;
1841 logop_head->oh_res2 = 0;
1842 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1843 record_cnt++;
1844
1845 start_rec_copy = sizeof(xlog_op_header_t);
1846 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1847 }
1848
1849 /* Copy log operation header directly into data section */
1850 logop_head = (xlog_op_header_t *)ptr;
1851 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1852 logop_head->oh_clientid = ticket->t_clientid;
1853 logop_head->oh_res2 = 0;
1854
1855 /* header copied directly */
1856 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1857
1858 /* are we copying a commit or unmount record? */
1859 logop_head->oh_flags = flags;
1860
1861 /*
1862 * We've seen logs corrupted with bad transaction client
1863 * ids. This makes sure that XFS doesn't generate them on.
1864 * Turn this into an EIO and shut down the filesystem.
1865 */
1866 switch (logop_head->oh_clientid) {
1867 case XFS_TRANSACTION:
1868 case XFS_VOLUME:
1869 case XFS_LOG:
1870 break;
1871 default:
1872 xfs_fs_cmn_err(CE_WARN, mp,
1873 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1874 logop_head->oh_clientid, tic);
1875 return XFS_ERROR(EIO);
1876 }
1877
1878 /* Partial write last time? => (partial_copy != 0)
1879 * need_copy is the amount we'd like to copy if everything could
1880 * fit in the current memcpy.
1881 */
1882 need_copy = reg[index].i_len - partial_copy_len;
1883
1884 copy_off = partial_copy_len;
1885 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1886 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1887 if (partial_copy)
1888 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1889 partial_copy_len = partial_copy = 0;
1890 } else { /* partial write */
1891 copy_len = iclog->ic_size - log_offset;
1892 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1893 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1894 if (partial_copy)
1895 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1896 partial_copy_len += copy_len;
1897 partial_copy++;
1898 len += sizeof(xlog_op_header_t); /* from splitting of region */
1899 /* account for new log op header */
1900 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001901 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 xlog_verify_dest_ptr(log, ptr);
1904
1905 /* copy region */
1906 ASSERT(copy_len >= 0);
1907 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1908 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1909
1910 /* make copy_len total bytes copied, including headers */
1911 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1912 record_cnt++;
1913 data_cnt += contwr ? copy_len : 0;
1914 if (partial_copy) { /* copied partial region */
1915 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1916 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1917 record_cnt = data_cnt = 0;
1918 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001919 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 break; /* don't increment index */
1921 } else { /* copied entire region */
1922 index++;
1923 partial_copy_len = partial_copy = 0;
1924
1925 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1926 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1927 record_cnt = data_cnt = 0;
1928 xlog_state_want_sync(log, iclog);
1929 if (commit_iclog) {
1930 ASSERT(flags & XLOG_COMMIT_TRANS);
1931 *commit_iclog = iclog;
1932 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001933 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (index == nentries)
1935 return 0; /* we are done */
1936 else
1937 break;
1938 }
1939 } /* if (partial_copy) */
1940 } /* while (index < nentries) */
1941 } /* for (index = 0; index < nentries; ) */
1942 ASSERT(len == 0);
1943
1944 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1945 if (commit_iclog) {
1946 ASSERT(flags & XLOG_COMMIT_TRANS);
1947 *commit_iclog = iclog;
1948 return 0;
1949 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001950 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951} /* xlog_write */
1952
1953
1954/*****************************************************************************
1955 *
1956 * State Machine functions
1957 *
1958 *****************************************************************************
1959 */
1960
1961/* Clean iclogs starting from the head. This ordering must be
1962 * maintained, so an iclog doesn't become ACTIVE beyond one that
1963 * is SYNCING. This is also required to maintain the notion that we use
1964 * a counting semaphore to hold off would be writers to the log when every
1965 * iclog is trying to sync to disk.
1966 *
1967 * State Change: DIRTY -> ACTIVE
1968 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001969STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970xlog_state_clean_log(xlog_t *log)
1971{
1972 xlog_in_core_t *iclog;
1973 int changed = 0;
1974
1975 iclog = log->l_iclog;
1976 do {
1977 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1978 iclog->ic_state = XLOG_STATE_ACTIVE;
1979 iclog->ic_offset = 0;
1980 iclog->ic_callback = NULL; /* don't need to free */
1981 /*
1982 * If the number of ops in this iclog indicate it just
1983 * contains the dummy transaction, we can
1984 * change state into IDLE (the second time around).
1985 * Otherwise we should change the state into
1986 * NEED a dummy.
1987 * We don't need to cover the dummy.
1988 */
1989 if (!changed &&
1990 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1991 changed = 1;
1992 } else {
1993 /*
1994 * We have two dirty iclogs so start over
1995 * This could also be num of ops indicates
1996 * this is not the dummy going out.
1997 */
1998 changed = 2;
1999 }
2000 iclog->ic_header.h_num_logops = 0;
2001 memset(iclog->ic_header.h_cycle_data, 0,
2002 sizeof(iclog->ic_header.h_cycle_data));
2003 iclog->ic_header.h_lsn = 0;
2004 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2005 /* do nothing */;
2006 else
2007 break; /* stop cleaning */
2008 iclog = iclog->ic_next;
2009 } while (iclog != log->l_iclog);
2010
2011 /* log is locked when we are called */
2012 /*
2013 * Change state for the dummy log recording.
2014 * We usually go to NEED. But we go to NEED2 if the changed indicates
2015 * we are done writing the dummy record.
2016 * If we are done with the second dummy recored (DONE2), then
2017 * we go to IDLE.
2018 */
2019 if (changed) {
2020 switch (log->l_covered_state) {
2021 case XLOG_STATE_COVER_IDLE:
2022 case XLOG_STATE_COVER_NEED:
2023 case XLOG_STATE_COVER_NEED2:
2024 log->l_covered_state = XLOG_STATE_COVER_NEED;
2025 break;
2026
2027 case XLOG_STATE_COVER_DONE:
2028 if (changed == 1)
2029 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2030 else
2031 log->l_covered_state = XLOG_STATE_COVER_NEED;
2032 break;
2033
2034 case XLOG_STATE_COVER_DONE2:
2035 if (changed == 1)
2036 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2037 else
2038 log->l_covered_state = XLOG_STATE_COVER_NEED;
2039 break;
2040
2041 default:
2042 ASSERT(0);
2043 }
2044 }
2045} /* xlog_state_clean_log */
2046
2047STATIC xfs_lsn_t
2048xlog_get_lowest_lsn(
2049 xlog_t *log)
2050{
2051 xlog_in_core_t *lsn_log;
2052 xfs_lsn_t lowest_lsn, lsn;
2053
2054 lsn_log = log->l_iclog;
2055 lowest_lsn = 0;
2056 do {
2057 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2058 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
2059 if ((lsn && !lowest_lsn) ||
2060 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2061 lowest_lsn = lsn;
2062 }
2063 }
2064 lsn_log = lsn_log->ic_next;
2065 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002066 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069
2070STATIC void
2071xlog_state_do_callback(
2072 xlog_t *log,
2073 int aborted,
2074 xlog_in_core_t *ciclog)
2075{
2076 xlog_in_core_t *iclog;
2077 xlog_in_core_t *first_iclog; /* used to know when we've
2078 * processed all iclogs once */
2079 xfs_log_callback_t *cb, *cb_next;
2080 int flushcnt = 0;
2081 xfs_lsn_t lowest_lsn;
2082 int ioerrors; /* counter: iclogs with errors */
2083 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2084 int funcdidcallbacks; /* flag: function did callbacks */
2085 int repeats; /* for issuing console warnings if
2086 * looping too many times */
2087 SPLDECL(s);
2088
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002089 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 first_iclog = iclog = log->l_iclog;
2091 ioerrors = 0;
2092 funcdidcallbacks = 0;
2093 repeats = 0;
2094
2095 do {
2096 /*
2097 * Scan all iclogs starting with the one pointed to by the
2098 * log. Reset this starting point each time the log is
2099 * unlocked (during callbacks).
2100 *
2101 * Keep looping through iclogs until one full pass is made
2102 * without running any callbacks.
2103 */
2104 first_iclog = log->l_iclog;
2105 iclog = log->l_iclog;
2106 loopdidcallbacks = 0;
2107 repeats++;
2108
2109 do {
2110
2111 /* skip all iclogs in the ACTIVE & DIRTY states */
2112 if (iclog->ic_state &
2113 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2114 iclog = iclog->ic_next;
2115 continue;
2116 }
2117
2118 /*
2119 * Between marking a filesystem SHUTDOWN and stopping
2120 * the log, we do flush all iclogs to disk (if there
2121 * wasn't a log I/O error). So, we do want things to
2122 * go smoothly in case of just a SHUTDOWN w/o a
2123 * LOG_IO_ERROR.
2124 */
2125 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2126 /*
2127 * Can only perform callbacks in order. Since
2128 * this iclog is not in the DONE_SYNC/
2129 * DO_CALLBACK state, we skip the rest and
2130 * just try to clean up. If we set our iclog
2131 * to DO_CALLBACK, we will not process it when
2132 * we retry since a previous iclog is in the
2133 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002134 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 */
2136 if (!(iclog->ic_state &
2137 (XLOG_STATE_DONE_SYNC |
2138 XLOG_STATE_DO_CALLBACK))) {
2139 if (ciclog && (ciclog->ic_state ==
2140 XLOG_STATE_DONE_SYNC)) {
2141 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2142 }
2143 break;
2144 }
2145 /*
2146 * We now have an iclog that is in either the
2147 * DO_CALLBACK or DONE_SYNC states. The other
2148 * states (WANT_SYNC, SYNCING, or CALLBACK were
2149 * caught by the above if and are going to
2150 * clean (i.e. we aren't doing their callbacks)
2151 * see the above if.
2152 */
2153
2154 /*
2155 * We will do one more check here to see if we
2156 * have chased our tail around.
2157 */
2158
2159 lowest_lsn = xlog_get_lowest_lsn(log);
2160 if (lowest_lsn && (
2161 XFS_LSN_CMP(
2162 lowest_lsn,
2163 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2164 )<0)) {
2165 iclog = iclog->ic_next;
2166 continue; /* Leave this iclog for
2167 * another thread */
2168 }
2169
2170 iclog->ic_state = XLOG_STATE_CALLBACK;
2171
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002172 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 /* l_last_sync_lsn field protected by
2175 * GRANT_LOCK. Don't worry about iclog's lsn.
2176 * No one else can be here except us.
2177 */
2178 s = GRANT_LOCK(log);
2179 ASSERT(XFS_LSN_CMP(
2180 log->l_last_sync_lsn,
2181 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2182 )<=0);
2183 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2184 GRANT_UNLOCK(log, s);
2185
2186 /*
2187 * Keep processing entries in the callback list
2188 * until we come around and it is empty. We
2189 * need to atomically see that the list is
2190 * empty and change the state to DIRTY so that
2191 * we don't miss any more callbacks being added.
2192 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002193 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 } else {
2195 ioerrors++;
2196 }
2197 cb = iclog->ic_callback;
2198
Christoph Hellwig4b809162007-08-16 15:37:36 +10002199 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 iclog->ic_callback_tail = &(iclog->ic_callback);
2201 iclog->ic_callback = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002202 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002205 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 cb_next = cb->cb_next;
2207 cb->cb_func(cb->cb_arg, aborted);
2208 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002209 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 cb = iclog->ic_callback;
2211 }
2212
2213 loopdidcallbacks++;
2214 funcdidcallbacks++;
2215
Christoph Hellwig4b809162007-08-16 15:37:36 +10002216 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2218 iclog->ic_state = XLOG_STATE_DIRTY;
2219
2220 /*
2221 * Transition from DIRTY to ACTIVE if applicable.
2222 * NOP if STATE_IOERROR.
2223 */
2224 xlog_state_clean_log(log);
2225
2226 /* wake up threads waiting in xfs_log_force() */
2227 sv_broadcast(&iclog->ic_forcesema);
2228
2229 iclog = iclog->ic_next;
2230 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002231
2232 if (repeats > 5000) {
2233 flushcnt += repeats;
2234 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002236 "%s: possible infinite loop (%d iterations)",
2237 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
2239 } while (!ioerrors && loopdidcallbacks);
2240
2241 /*
2242 * make one last gasp attempt to see if iclogs are being left in
2243 * limbo..
2244 */
2245#ifdef DEBUG
2246 if (funcdidcallbacks) {
2247 first_iclog = iclog = log->l_iclog;
2248 do {
2249 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2250 /*
2251 * Terminate the loop if iclogs are found in states
2252 * which will cause other threads to clean up iclogs.
2253 *
2254 * SYNCING - i/o completion will go through logs
2255 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002256 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 * IOERROR - give up hope all ye who enter here
2258 */
2259 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2260 iclog->ic_state == XLOG_STATE_SYNCING ||
2261 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2262 iclog->ic_state == XLOG_STATE_IOERROR )
2263 break;
2264 iclog = iclog->ic_next;
2265 } while (first_iclog != iclog);
2266 }
2267#endif
2268
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002269 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2271 flushcnt = log->l_flushcnt;
2272 log->l_flushcnt = 0;
2273 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002274 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 while (flushcnt--)
2276 vsema(&log->l_flushsema);
2277} /* xlog_state_do_callback */
2278
2279
2280/*
2281 * Finish transitioning this iclog to the dirty state.
2282 *
2283 * Make sure that we completely execute this routine only when this is
2284 * the last call to the iclog. There is a good chance that iclog flushes,
2285 * when we reach the end of the physical log, get turned into 2 separate
2286 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2287 * routine. By using the reference count bwritecnt, we guarantee that only
2288 * the second completion goes through.
2289 *
2290 * Callbacks could take time, so they are done outside the scope of the
2291 * global state machine log lock. Assume that the calls to cvsema won't
2292 * take a long time. At least we know it won't sleep.
2293 */
2294void
2295xlog_state_done_syncing(
2296 xlog_in_core_t *iclog,
2297 int aborted)
2298{
2299 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002301 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2304 iclog->ic_state == XLOG_STATE_IOERROR);
2305 ASSERT(iclog->ic_refcnt == 0);
2306 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2307
2308
2309 /*
2310 * If we got an error, either on the first buffer, or in the case of
2311 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2312 * and none should ever be attempted to be written to disk
2313 * again.
2314 */
2315 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2316 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002317 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return;
2319 }
2320 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2321 }
2322
2323 /*
2324 * Someone could be sleeping prior to writing out the next
2325 * iclog buffer, we wake them all, one will get to do the
2326 * I/O, the others get to wait for the result.
2327 */
2328 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002329 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2331} /* xlog_state_done_syncing */
2332
2333
2334/*
2335 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2336 * sleep. The flush semaphore is set to the number of in-core buffers and
2337 * decremented around disk syncing. Therefore, if all buffers are syncing,
2338 * this semaphore will cause new writes to sleep until a sync completes.
2339 * Otherwise, this code just does p() followed by v(). This approximates
2340 * a sleep/wakeup except we can't race.
2341 *
2342 * The in-core logs are used in a circular fashion. They are not used
2343 * out-of-order even when an iclog past the head is free.
2344 *
2345 * return:
2346 * * log_offset where xlog_write() can start writing into the in-core
2347 * log's data space.
2348 * * in-core log pointer to which xlog_write() should write.
2349 * * boolean indicating this is a continued write to an in-core log.
2350 * If this is the last write, then the in-core log's offset field
2351 * needs to be incremented, depending on the amount of data which
2352 * is copied.
2353 */
2354int
2355xlog_state_get_iclog_space(xlog_t *log,
2356 int len,
2357 xlog_in_core_t **iclogp,
2358 xlog_ticket_t *ticket,
2359 int *continued_write,
2360 int *logoffsetp)
2361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int log_offset;
2363 xlog_rec_header_t *head;
2364 xlog_in_core_t *iclog;
2365 int error;
2366
2367restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002368 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002370 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return XFS_ERROR(EIO);
2372 }
2373
2374 iclog = log->l_iclog;
2375 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2376 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002377 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2379 XFS_STATS_INC(xs_log_noiclogs);
2380 /* Ensure that log writes happen */
2381 psema(&log->l_flushsema, PINOD);
2382 goto restart;
2383 }
2384 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2385 head = &iclog->ic_header;
2386
2387 iclog->ic_refcnt++; /* prevents sync */
2388 log_offset = iclog->ic_offset;
2389
2390 /* On the 1st write to an iclog, figure out lsn. This works
2391 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2392 * committing to. If the offset is set, that's how many blocks
2393 * must be written.
2394 */
2395 if (log_offset == 0) {
2396 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002397 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002398 log->l_iclog_hsize,
2399 XLOG_REG_TYPE_LRHEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2401 ASSIGN_LSN(head->h_lsn, log);
2402 ASSERT(log->l_curr_block >= 0);
2403 }
2404
2405 /* If there is enough room to write everything, then do it. Otherwise,
2406 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2407 * bit is on, so this will get flushed out. Don't update ic_offset
2408 * until you know exactly how many bytes get copied. Therefore, wait
2409 * until later to update ic_offset.
2410 *
2411 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2412 * can fit into remaining data section.
2413 */
2414 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2415 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2416
2417 /* If I'm the only one writing to this iclog, sync it to disk */
2418 if (iclog->ic_refcnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002419 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002421 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 } else {
2423 iclog->ic_refcnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002424 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426 goto restart;
2427 }
2428
2429 /* Do we have enough room to write the full amount in the remainder
2430 * of this iclog? Or must we continue a write on the next iclog and
2431 * mark this iclog as completely taken? In the case where we switch
2432 * iclogs (to mark it taken), this particular iclog will release/sync
2433 * to disk in xlog_write().
2434 */
2435 if (len <= iclog->ic_size - iclog->ic_offset) {
2436 *continued_write = 0;
2437 iclog->ic_offset += len;
2438 } else {
2439 *continued_write = 1;
2440 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2441 }
2442 *iclogp = iclog;
2443
2444 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002445 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 *logoffsetp = log_offset;
2448 return 0;
2449} /* xlog_state_get_iclog_space */
2450
2451/*
2452 * Atomically get the log space required for a log ticket.
2453 *
2454 * Once a ticket gets put onto the reserveq, it will only return after
2455 * the needed reservation is satisfied.
2456 */
2457STATIC int
2458xlog_grant_log_space(xlog_t *log,
2459 xlog_ticket_t *tic)
2460{
2461 int free_bytes;
2462 int need_bytes;
2463 SPLDECL(s);
2464#ifdef DEBUG
2465 xfs_lsn_t tail_lsn;
2466#endif
2467
2468
2469#ifdef DEBUG
2470 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2471 panic("grant Recovery problem");
2472#endif
2473
2474 /* Is there space or do we need to sleep? */
2475 s = GRANT_LOCK(log);
2476 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2477
2478 /* something is already sleeping; insert new transaction at end */
2479 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002480 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 xlog_trace_loggrant(log, tic,
2482 "xlog_grant_log_space: sleep 1");
2483 /*
2484 * Gotta check this before going to sleep, while we're
2485 * holding the grant lock.
2486 */
2487 if (XLOG_FORCED_SHUTDOWN(log))
2488 goto error_return;
2489
2490 XFS_STATS_INC(xs_sleep_logspace);
2491 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2492 /*
2493 * If we got an error, and the filesystem is shutting down,
2494 * we'll catch it down below. So just continue...
2495 */
2496 xlog_trace_loggrant(log, tic,
2497 "xlog_grant_log_space: wake 1");
2498 s = GRANT_LOCK(log);
2499 }
2500 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2501 need_bytes = tic->t_unit_res*tic->t_ocnt;
2502 else
2503 need_bytes = tic->t_unit_res;
2504
2505redo:
2506 if (XLOG_FORCED_SHUTDOWN(log))
2507 goto error_return;
2508
2509 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2510 log->l_grant_reserve_bytes);
2511 if (free_bytes < need_bytes) {
2512 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002513 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 xlog_trace_loggrant(log, tic,
2515 "xlog_grant_log_space: sleep 2");
2516 XFS_STATS_INC(xs_sleep_logspace);
2517 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2518
2519 if (XLOG_FORCED_SHUTDOWN(log)) {
2520 s = GRANT_LOCK(log);
2521 goto error_return;
2522 }
2523
2524 xlog_trace_loggrant(log, tic,
2525 "xlog_grant_log_space: wake 2");
2526 xlog_grant_push_ail(log->l_mp, need_bytes);
2527 s = GRANT_LOCK(log);
2528 goto redo;
2529 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002530 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002533 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534#ifdef DEBUG
2535 tail_lsn = log->l_tail_lsn;
2536 /*
2537 * Check to make sure the grant write head didn't just over lap the
2538 * tail. If the cycles are the same, we can't be overlapping.
2539 * Otherwise, make sure that the cycles differ by exactly one and
2540 * check the byte count.
2541 */
2542 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2543 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2544 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2545 }
2546#endif
2547 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2548 xlog_verify_grant_head(log, 1);
2549 GRANT_UNLOCK(log, s);
2550 return 0;
2551
2552 error_return:
2553 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002554 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2556 /*
2557 * If we are failing, make sure the ticket doesn't have any
2558 * current reservations. We don't want to add this back when
2559 * the ticket/transaction gets cancelled.
2560 */
2561 tic->t_curr_res = 0;
2562 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2563 GRANT_UNLOCK(log, s);
2564 return XFS_ERROR(EIO);
2565} /* xlog_grant_log_space */
2566
2567
2568/*
2569 * Replenish the byte reservation required by moving the grant write head.
2570 *
2571 *
2572 */
2573STATIC int
2574xlog_regrant_write_log_space(xlog_t *log,
2575 xlog_ticket_t *tic)
2576{
2577 SPLDECL(s);
2578 int free_bytes, need_bytes;
2579 xlog_ticket_t *ntic;
2580#ifdef DEBUG
2581 xfs_lsn_t tail_lsn;
2582#endif
2583
2584 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002585 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590#ifdef DEBUG
2591 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2592 panic("regrant Recovery problem");
2593#endif
2594
2595 s = GRANT_LOCK(log);
2596 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2597
2598 if (XLOG_FORCED_SHUTDOWN(log))
2599 goto error_return;
2600
2601 /* If there are other waiters on the queue then give them a
2602 * chance at logspace before us. Wake up the first waiters,
2603 * if we do not wake up all the waiters then go to sleep waiting
2604 * for more free space, otherwise try to get some space for
2605 * this transaction.
2606 */
2607
2608 if ((ntic = log->l_write_headq)) {
2609 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2610 log->l_grant_write_bytes);
2611 do {
2612 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2613
2614 if (free_bytes < ntic->t_unit_res)
2615 break;
2616 free_bytes -= ntic->t_unit_res;
2617 sv_signal(&ntic->t_sema);
2618 ntic = ntic->t_next;
2619 } while (ntic != log->l_write_headq);
2620
2621 if (ntic != log->l_write_headq) {
2622 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002623 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 xlog_trace_loggrant(log, tic,
2626 "xlog_regrant_write_log_space: sleep 1");
2627 XFS_STATS_INC(xs_sleep_logspace);
2628 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2629 &log->l_grant_lock, s);
2630
2631 /* If we're shutting down, this tic is already
2632 * off the queue */
2633 if (XLOG_FORCED_SHUTDOWN(log)) {
2634 s = GRANT_LOCK(log);
2635 goto error_return;
2636 }
2637
2638 xlog_trace_loggrant(log, tic,
2639 "xlog_regrant_write_log_space: wake 1");
2640 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2641 s = GRANT_LOCK(log);
2642 }
2643 }
2644
2645 need_bytes = tic->t_unit_res;
2646
2647redo:
2648 if (XLOG_FORCED_SHUTDOWN(log))
2649 goto error_return;
2650
2651 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2652 log->l_grant_write_bytes);
2653 if (free_bytes < need_bytes) {
2654 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002655 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 XFS_STATS_INC(xs_sleep_logspace);
2657 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2658
2659 /* If we're shutting down, this tic is already off the queue */
2660 if (XLOG_FORCED_SHUTDOWN(log)) {
2661 s = GRANT_LOCK(log);
2662 goto error_return;
2663 }
2664
2665 xlog_trace_loggrant(log, tic,
2666 "xlog_regrant_write_log_space: wake 2");
2667 xlog_grant_push_ail(log->l_mp, need_bytes);
2668 s = GRANT_LOCK(log);
2669 goto redo;
2670 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002671 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002673 /* we've got enough space */
2674 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675#ifdef DEBUG
2676 tail_lsn = log->l_tail_lsn;
2677 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2678 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2679 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2680 }
2681#endif
2682
2683 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2684 xlog_verify_grant_head(log, 1);
2685 GRANT_UNLOCK(log, s);
Jesper Juhl014c2542006-01-15 02:37:08 +01002686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688
2689 error_return:
2690 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002691 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2693 /*
2694 * If we are failing, make sure the ticket doesn't have any
2695 * current reservations. We don't want to add this back when
2696 * the ticket/transaction gets cancelled.
2697 */
2698 tic->t_curr_res = 0;
2699 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2700 GRANT_UNLOCK(log, s);
2701 return XFS_ERROR(EIO);
2702} /* xlog_regrant_write_log_space */
2703
2704
2705/* The first cnt-1 times through here we don't need to
2706 * move the grant write head because the permanent
2707 * reservation has reserved cnt times the unit amount.
2708 * Release part of current permanent unit reservation and
2709 * reset current reservation to be one units worth. Also
2710 * move grant reservation head forward.
2711 */
2712STATIC void
2713xlog_regrant_reserve_log_space(xlog_t *log,
2714 xlog_ticket_t *ticket)
2715{
2716 SPLDECL(s);
2717
2718 xlog_trace_loggrant(log, ticket,
2719 "xlog_regrant_reserve_log_space: enter");
2720 if (ticket->t_cnt > 0)
2721 ticket->t_cnt--;
2722
2723 s = GRANT_LOCK(log);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002724 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002726 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 xlog_trace_loggrant(log, ticket,
2728 "xlog_regrant_reserve_log_space: sub current res");
2729 xlog_verify_grant_head(log, 1);
2730
2731 /* just return if we still have some of the pre-reserved space */
2732 if (ticket->t_cnt > 0) {
2733 GRANT_UNLOCK(log, s);
2734 return;
2735 }
2736
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002737 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 xlog_trace_loggrant(log, ticket,
2739 "xlog_regrant_reserve_log_space: exit");
2740 xlog_verify_grant_head(log, 0);
2741 GRANT_UNLOCK(log, s);
2742 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002743 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744} /* xlog_regrant_reserve_log_space */
2745
2746
2747/*
2748 * Give back the space left from a reservation.
2749 *
2750 * All the information we need to make a correct determination of space left
2751 * is present. For non-permanent reservations, things are quite easy. The
2752 * count should have been decremented to zero. We only need to deal with the
2753 * space remaining in the current reservation part of the ticket. If the
2754 * ticket contains a permanent reservation, there may be left over space which
2755 * needs to be released. A count of N means that N-1 refills of the current
2756 * reservation can be done before we need to ask for more space. The first
2757 * one goes to fill up the first current reservation. Once we run out of
2758 * space, the count will stay at zero and the only space remaining will be
2759 * in the current reservation field.
2760 */
2761STATIC void
2762xlog_ungrant_log_space(xlog_t *log,
2763 xlog_ticket_t *ticket)
2764{
2765 SPLDECL(s);
2766
2767 if (ticket->t_cnt > 0)
2768 ticket->t_cnt--;
2769
2770 s = GRANT_LOCK(log);
2771 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2772
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002773 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2776
2777 /* If this is a permanent reservation ticket, we may be able to free
2778 * up more space based on the remaining count.
2779 */
2780 if (ticket->t_cnt > 0) {
2781 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002782 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
2784
2785 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2786 xlog_verify_grant_head(log, 1);
2787 GRANT_UNLOCK(log, s);
2788 xfs_log_move_tail(log->l_mp, 1);
2789} /* xlog_ungrant_log_space */
2790
2791
2792/*
2793 * Atomically put back used ticket.
2794 */
2795void
2796xlog_state_put_ticket(xlog_t *log,
2797 xlog_ticket_t *tic)
2798{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002799 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 xlog_ticket_put(log, tic);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002801 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802} /* xlog_state_put_ticket */
2803
2804/*
2805 * Flush iclog to disk if this is the last reference to the given iclog and
2806 * the WANT_SYNC bit is set.
2807 *
2808 * When this function is entered, the iclog is not necessarily in the
2809 * WANT_SYNC state. It may be sitting around waiting to get filled.
2810 *
2811 *
2812 */
2813int
2814xlog_state_release_iclog(xlog_t *log,
2815 xlog_in_core_t *iclog)
2816{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 int sync = 0; /* do we sync? */
2818
2819 xlog_assign_tail_lsn(log->l_mp);
2820
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002821 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002824 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return XFS_ERROR(EIO);
2826 }
2827
2828 ASSERT(iclog->ic_refcnt > 0);
2829 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2830 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2831
2832 if (--iclog->ic_refcnt == 0 &&
2833 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2834 sync++;
2835 iclog->ic_state = XLOG_STATE_SYNCING;
2836 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2837 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2838 /* cycle incremented when incrementing curr_block */
2839 }
2840
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002841 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 /*
2844 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002845 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2847 * this iclog has consistent data, so we ignore IOERROR
2848 * flags after this point.
2849 */
2850 if (sync) {
2851 return xlog_sync(log, iclog);
2852 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855} /* xlog_state_release_iclog */
2856
2857
2858/*
2859 * This routine will mark the current iclog in the ring as WANT_SYNC
2860 * and move the current iclog pointer to the next iclog in the ring.
2861 * When this routine is called from xlog_state_get_iclog_space(), the
2862 * exact size of the iclog has not yet been determined. All we know is
2863 * that every data block. We have run out of space in this log record.
2864 */
2865STATIC void
2866xlog_state_switch_iclogs(xlog_t *log,
2867 xlog_in_core_t *iclog,
2868 int eventual_size)
2869{
2870 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2871 if (!eventual_size)
2872 eventual_size = iclog->ic_offset;
2873 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2874 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2875 log->l_prev_block = log->l_curr_block;
2876 log->l_prev_cycle = log->l_curr_cycle;
2877
2878 /* roll log?: ic_offset changed later */
2879 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2880
2881 /* Round up to next log-sunit */
2882 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2883 log->l_mp->m_sb.sb_logsunit > 1) {
2884 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2885 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2886 }
2887
2888 if (log->l_curr_block >= log->l_logBBsize) {
2889 log->l_curr_cycle++;
2890 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2891 log->l_curr_cycle++;
2892 log->l_curr_block -= log->l_logBBsize;
2893 ASSERT(log->l_curr_block >= 0);
2894 }
2895 ASSERT(iclog == log->l_iclog);
2896 log->l_iclog = iclog->ic_next;
2897} /* xlog_state_switch_iclogs */
2898
2899
2900/*
2901 * Write out all data in the in-core log as of this exact moment in time.
2902 *
2903 * Data may be written to the in-core log during this call. However,
2904 * we don't guarantee this data will be written out. A change from past
2905 * implementation means this routine will *not* write out zero length LRs.
2906 *
2907 * Basically, we try and perform an intelligent scan of the in-core logs.
2908 * If we determine there is no flushable data, we just return. There is no
2909 * flushable data if:
2910 *
2911 * 1. the current iclog is active and has no data; the previous iclog
2912 * is in the active or dirty state.
2913 * 2. the current iclog is drity, and the previous iclog is in the
2914 * active or dirty state.
2915 *
2916 * We may sleep (call psema) if:
2917 *
2918 * 1. the current iclog is not in the active nor dirty state.
2919 * 2. the current iclog dirty, and the previous iclog is not in the
2920 * active nor dirty state.
2921 * 3. the current iclog is active, and there is another thread writing
2922 * to this particular iclog.
2923 * 4. a) the current iclog is active and has no other writers
2924 * b) when we return from flushing out this iclog, it is still
2925 * not in the active nor dirty state.
2926 */
2927STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002928xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 xlog_in_core_t *iclog;
2931 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002933 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 iclog = log->l_iclog;
2936 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002937 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return XFS_ERROR(EIO);
2939 }
2940
2941 /* If the head iclog is not active nor dirty, we just attach
2942 * ourselves to the head and go to sleep.
2943 */
2944 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2945 iclog->ic_state == XLOG_STATE_DIRTY) {
2946 /*
2947 * If the head is dirty or (active and empty), then
2948 * we need to look at the previous iclog. If the previous
2949 * iclog is active or dirty we are done. There is nothing
2950 * to sync out. Otherwise, we attach ourselves to the
2951 * previous iclog and go to sleep.
2952 */
2953 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2954 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2955 iclog = iclog->ic_prev;
2956 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2957 iclog->ic_state == XLOG_STATE_DIRTY)
2958 goto no_sleep;
2959 else
2960 goto maybe_sleep;
2961 } else {
2962 if (iclog->ic_refcnt == 0) {
2963 /* We are the only one with access to this
2964 * iclog. Flush it out now. There should
2965 * be a roundoff of zero to show that someone
2966 * has already taken care of the roundoff from
2967 * the previous sync.
2968 */
2969 iclog->ic_refcnt++;
2970 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2971 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002972 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 if (xlog_state_release_iclog(log, iclog))
2975 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002976 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002977 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2979 iclog->ic_state != XLOG_STATE_DIRTY)
2980 goto maybe_sleep;
2981 else
2982 goto no_sleep;
2983 } else {
2984 /* Someone else is writing to this iclog.
2985 * Use its call to flush out the data. However,
2986 * the other thread may not force out this LR,
2987 * so we mark it WANT_SYNC.
2988 */
2989 xlog_state_switch_iclogs(log, iclog, 0);
2990 goto maybe_sleep;
2991 }
2992 }
2993 }
2994
2995 /* By the time we come around again, the iclog could've been filled
2996 * which would give it another lsn. If we have a new lsn, just
2997 * return because the relevant data has been flushed.
2998 */
2999maybe_sleep:
3000 if (flags & XFS_LOG_SYNC) {
3001 /*
3002 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003003 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 * Then we check again after waking up, in case our
3005 * sleep was disturbed by a bad news.
3006 */
3007 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003008 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return XFS_ERROR(EIO);
3010 }
3011 XFS_STATS_INC(xs_log_force_sleep);
3012 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3013 /*
3014 * No need to grab the log lock here since we're
3015 * only deciding whether or not to return EIO
3016 * and the memory read should be atomic.
3017 */
3018 if (iclog->ic_state & XLOG_STATE_IOERROR)
3019 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003020 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 } else {
3023
3024no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003025 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
3027 return 0;
3028} /* xlog_state_sync_all */
3029
3030
3031/*
3032 * Used by code which implements synchronous log forces.
3033 *
3034 * Find in-core log with lsn.
3035 * If it is in the DIRTY state, just return.
3036 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3037 * state and go to sleep or return.
3038 * If it is in any other state, go to sleep or return.
3039 *
3040 * If filesystem activity goes to zero, the iclog will get flushed only by
3041 * bdflush().
3042 */
3043int
3044xlog_state_sync(xlog_t *log,
3045 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003046 uint flags,
3047 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 xlog_in_core_t *iclog;
3050 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003053 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 iclog = log->l_iclog;
3055
3056 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003057 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return XFS_ERROR(EIO);
3059 }
3060
3061 do {
3062 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
3063 iclog = iclog->ic_next;
3064 continue;
3065 }
3066
3067 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003068 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return 0;
3070 }
3071
3072 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3073 /*
3074 * We sleep here if we haven't already slept (e.g.
3075 * this is the first time we've looked at the correct
3076 * iclog buf) and the buffer before us is going to
3077 * be sync'ed. The reason for this is that if we
3078 * are doing sync transactions here, by waiting for
3079 * the previous I/O to complete, we can allow a few
3080 * more transactions into this iclog before we close
3081 * it down.
3082 *
3083 * Otherwise, we mark the buffer WANT_SYNC, and bump
3084 * up the refcnt so we can release the log (which drops
3085 * the ref count). The state switch keeps new transaction
3086 * commits from using this buffer. When the current commits
3087 * finish writing into the buffer, the refcount will drop to
3088 * zero and the buffer will go out then.
3089 */
3090 if (!already_slept &&
3091 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3092 XLOG_STATE_SYNCING))) {
3093 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3094 XFS_STATS_INC(xs_log_force_sleep);
3095 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3096 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003097 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 already_slept = 1;
3099 goto try_again;
3100 } else {
3101 iclog->ic_refcnt++;
3102 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003103 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (xlog_state_release_iclog(log, iclog))
3105 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003106 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003107 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109 }
3110
3111 if ((flags & XFS_LOG_SYNC) && /* sleep */
3112 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3113
3114 /*
3115 * Don't wait on the forcesema if we know that we've
3116 * gotten a log write error.
3117 */
3118 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003119 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return XFS_ERROR(EIO);
3121 }
3122 XFS_STATS_INC(xs_log_force_sleep);
3123 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3124 /*
3125 * No need to grab the log lock here since we're
3126 * only deciding whether or not to return EIO
3127 * and the memory read should be atomic.
3128 */
3129 if (iclog->ic_state & XLOG_STATE_IOERROR)
3130 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003131 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003133 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
3135 return 0;
3136
3137 } while (iclog != log->l_iclog);
3138
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003139 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141} /* xlog_state_sync */
3142
3143
3144/*
3145 * Called when we want to mark the current iclog as being ready to sync to
3146 * disk.
3147 */
3148void
3149xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3150{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003151 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3154 xlog_state_switch_iclogs(log, iclog, 0);
3155 } else {
3156 ASSERT(iclog->ic_state &
3157 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3158 }
3159
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003160 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161} /* xlog_state_want_sync */
3162
3163
3164
3165/*****************************************************************************
3166 *
3167 * TICKET functions
3168 *
3169 *****************************************************************************
3170 */
3171
3172/*
3173 * Algorithm doesn't take into account page size. ;-(
3174 */
3175STATIC void
3176xlog_state_ticket_alloc(xlog_t *log)
3177{
3178 xlog_ticket_t *t_list;
3179 xlog_ticket_t *next;
3180 xfs_caddr_t buf;
3181 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 /*
3184 * The kmem_zalloc may sleep, so we shouldn't be holding the
3185 * global lock. XXXmiken: may want to use zone allocator.
3186 */
3187 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3188
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003189 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3192 t_list = (xlog_ticket_t *)buf;
3193 t_list->t_next = log->l_unmount_free;
3194 log->l_unmount_free = t_list++;
3195 log->l_ticket_cnt++;
3196 log->l_ticket_tcnt++;
3197
3198 /* Next ticket becomes first ticket attached to ticket free list */
3199 if (log->l_freelist != NULL) {
3200 ASSERT(log->l_tail != NULL);
3201 log->l_tail->t_next = t_list;
3202 } else {
3203 log->l_freelist = t_list;
3204 }
3205 log->l_ticket_cnt++;
3206 log->l_ticket_tcnt++;
3207
3208 /* Cycle through rest of alloc'ed memory, building up free Q */
3209 for ( ; i > 0; i--) {
3210 next = t_list + 1;
3211 t_list->t_next = next;
3212 t_list = next;
3213 log->l_ticket_cnt++;
3214 log->l_ticket_tcnt++;
3215 }
3216 t_list->t_next = NULL;
3217 log->l_tail = t_list;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003218 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219} /* xlog_state_ticket_alloc */
3220
3221
3222/*
3223 * Put ticket into free list
3224 *
3225 * Assumption: log lock is held around this call.
3226 */
3227STATIC void
3228xlog_ticket_put(xlog_t *log,
3229 xlog_ticket_t *ticket)
3230{
3231 sv_destroy(&ticket->t_sema);
3232
3233 /*
3234 * Don't think caching will make that much difference. It's
3235 * more important to make debug easier.
3236 */
3237#if 0
3238 /* real code will want to use LIFO for caching */
3239 ticket->t_next = log->l_freelist;
3240 log->l_freelist = ticket;
3241 /* no need to clear fields */
3242#else
3243 /* When we debug, it is easier if tickets are cycled */
3244 ticket->t_next = NULL;
Christoph Hellwig4b809162007-08-16 15:37:36 +10003245 if (log->l_tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 log->l_tail->t_next = ticket;
3247 } else {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003248 ASSERT(log->l_freelist == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 log->l_freelist = ticket;
3250 }
3251 log->l_tail = ticket;
3252#endif /* DEBUG */
3253 log->l_ticket_cnt++;
3254} /* xlog_ticket_put */
3255
3256
3257/*
3258 * Grab ticket off freelist or allocation some more
3259 */
3260xlog_ticket_t *
3261xlog_ticket_get(xlog_t *log,
3262 int unit_bytes,
3263 int cnt,
3264 char client,
3265 uint xflags)
3266{
3267 xlog_ticket_t *tic;
3268 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 alloc:
3271 if (log->l_freelist == NULL)
3272 xlog_state_ticket_alloc(log); /* potentially sleep */
3273
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003274 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (log->l_freelist == NULL) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003276 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 goto alloc;
3278 }
3279 tic = log->l_freelist;
3280 log->l_freelist = tic->t_next;
3281 if (log->l_freelist == NULL)
3282 log->l_tail = NULL;
3283 log->l_ticket_cnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003284 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 /*
3287 * Permanent reservations have up to 'cnt'-1 active log operations
3288 * in the log. A unit in this case is the amount of space for one
3289 * of these log operations. Normal reservations have a cnt of 1
3290 * and their unit amount is the total amount of space required.
3291 *
3292 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003293 * which occupy space in the on-disk log.
3294 *
3295 * Normal form of a transaction is:
3296 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3297 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3298 *
3299 * We need to account for all the leadup data and trailer data
3300 * around the transaction data.
3301 * And then we need to account for the worst case in terms of using
3302 * more space.
3303 * The worst case will happen if:
3304 * - the placement of the transaction happens to be such that the
3305 * roundoff is at its maximum
3306 * - the transaction data is synced before the commit record is synced
3307 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3308 * Therefore the commit record is in its own Log Record.
3309 * This can happen as the commit record is called with its
3310 * own region to xlog_write().
3311 * This then means that in the worst case, roundoff can happen for
3312 * the commit-rec as well.
3313 * The commit-rec is smaller than padding in this scenario and so it is
3314 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 */
3316
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003317 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003319 unit_bytes += sizeof(xfs_trans_header_t);
3320
3321 /* for start-rec */
3322 unit_bytes += sizeof(xlog_op_header_t);
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 /* for LR headers */
3325 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3326 unit_bytes += log->l_iclog_hsize * num_headers;
3327
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003328 /* for commit-rec LR header - note: padding will subsume the ophdr */
3329 unit_bytes += log->l_iclog_hsize;
3330
3331 /* for split-recs - ophdrs added when data split over LRs */
3332 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3333
3334 /* for roundoff padding for transaction data and one for commit record */
3335 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3336 log->l_mp->m_sb.sb_logsunit > 1) {
3337 /* log su roundoff */
3338 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3339 } else {
3340 /* BB roundoff */
3341 unit_bytes += 2*BBSIZE;
3342 }
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 tic->t_unit_res = unit_bytes;
3345 tic->t_curr_res = unit_bytes;
3346 tic->t_cnt = cnt;
3347 tic->t_ocnt = cnt;
3348 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3349 tic->t_clientid = client;
3350 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003351 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (xflags & XFS_LOG_PERM_RESERV)
3353 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3354 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3355
Christoph Hellwig0adba532007-08-30 17:21:46 +10003356 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return tic;
3359} /* xlog_ticket_get */
3360
3361
3362/******************************************************************************
3363 *
3364 * Log debug routines
3365 *
3366 ******************************************************************************
3367 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003368#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369/*
3370 * Make sure that the destination ptr is within the valid data region of
3371 * one of the iclogs. This uses backup pointers stored in a different
3372 * part of the log in case we trash the log structure.
3373 */
3374void
3375xlog_verify_dest_ptr(xlog_t *log,
3376 __psint_t ptr)
3377{
3378 int i;
3379 int good_ptr = 0;
3380
3381 for (i=0; i < log->l_iclog_bufs; i++) {
3382 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3383 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3384 good_ptr++;
3385 }
3386 if (! good_ptr)
3387 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3388} /* xlog_verify_dest_ptr */
3389
3390STATIC void
3391xlog_verify_grant_head(xlog_t *log, int equals)
3392{
3393 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3394 if (equals)
3395 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3396 else
3397 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3398 } else {
3399 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3400 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3401 }
3402} /* xlog_verify_grant_head */
3403
3404/* check if it will fit */
3405STATIC void
3406xlog_verify_tail_lsn(xlog_t *log,
3407 xlog_in_core_t *iclog,
3408 xfs_lsn_t tail_lsn)
3409{
3410 int blocks;
3411
3412 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3413 blocks =
3414 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3415 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3416 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3417 } else {
3418 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3419
3420 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3421 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3422
3423 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3424 if (blocks < BTOBB(iclog->ic_offset) + 1)
3425 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3426 }
3427} /* xlog_verify_tail_lsn */
3428
3429/*
3430 * Perform a number of checks on the iclog before writing to disk.
3431 *
3432 * 1. Make sure the iclogs are still circular
3433 * 2. Make sure we have a good magic number
3434 * 3. Make sure we don't have magic numbers in the data
3435 * 4. Check fields of each log operation header for:
3436 * A. Valid client identifier
3437 * B. tid ptr value falls in valid ptr space (user space code)
3438 * C. Length in log record header is correct according to the
3439 * individual operation headers within record.
3440 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3441 * log, check the preceding blocks of the physical log to make sure all
3442 * the cycle numbers agree with the current cycle number.
3443 */
3444STATIC void
3445xlog_verify_iclog(xlog_t *log,
3446 xlog_in_core_t *iclog,
3447 int count,
3448 boolean_t syncing)
3449{
3450 xlog_op_header_t *ophead;
3451 xlog_in_core_t *icptr;
3452 xlog_in_core_2_t *xhdr;
3453 xfs_caddr_t ptr;
3454 xfs_caddr_t base_ptr;
3455 __psint_t field_offset;
3456 __uint8_t clientid;
3457 int len, i, j, k, op_len;
3458 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003461 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 icptr = log->l_iclog;
3463 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003464 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 xlog_panic("xlog_verify_iclog: invalid ptr");
3466 icptr = icptr->ic_next;
3467 }
3468 if (icptr != log->l_iclog)
3469 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003470 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 /* check log magic numbers */
3473 ptr = (xfs_caddr_t) &(iclog->ic_header);
3474 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3475 xlog_panic("xlog_verify_iclog: invalid magic num");
3476
3477 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3478 ptr += BBSIZE) {
3479 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3480 xlog_panic("xlog_verify_iclog: unexpected magic num");
3481 }
3482
3483 /* check fields */
3484 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3485 ptr = iclog->ic_datap;
3486 base_ptr = ptr;
3487 ophead = (xlog_op_header_t *)ptr;
3488 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3489 for (i = 0; i < len; i++) {
3490 ophead = (xlog_op_header_t *)ptr;
3491
3492 /* clientid is only 1 byte */
3493 field_offset = (__psint_t)
3494 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3495 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3496 clientid = ophead->oh_clientid;
3497 } else {
3498 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3499 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3500 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3501 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3502 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3503 } else {
3504 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3505 }
3506 }
3507 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003508 cmn_err(CE_WARN, "xlog_verify_iclog: "
3509 "invalid clientid %d op 0x%p offset 0x%lx",
3510 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512 /* check length */
3513 field_offset = (__psint_t)
3514 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3515 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3516 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3517 } else {
3518 idx = BTOBBT((__psint_t)&ophead->oh_len -
3519 (__psint_t)iclog->ic_datap);
3520 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3521 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3522 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3523 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3524 } else {
3525 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3526 }
3527 }
3528 ptr += sizeof(xlog_op_header_t) + op_len;
3529 }
3530} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003531#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003534 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 */
3536STATIC int
3537xlog_state_ioerror(
3538 xlog_t *log)
3539{
3540 xlog_in_core_t *iclog, *ic;
3541
3542 iclog = log->l_iclog;
3543 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3544 /*
3545 * Mark all the incore logs IOERROR.
3546 * From now on, no log flushes will result.
3547 */
3548 ic = iclog;
3549 do {
3550 ic->ic_state = XLOG_STATE_IOERROR;
3551 ic = ic->ic_next;
3552 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
3555 /*
3556 * Return non-zero, if state transition has already happened.
3557 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003558 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
3561/*
3562 * This is called from xfs_force_shutdown, when we're forcibly
3563 * shutting down the filesystem, typically because of an IO error.
3564 * Our main objectives here are to make sure that:
3565 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3566 * parties to find out, 'atomically'.
3567 * b. those who're sleeping on log reservations, pinned objects and
3568 * other resources get woken up, and be told the bad news.
3569 * c. nothing new gets queued up after (a) and (b) are done.
3570 * d. if !logerror, flush the iclogs to disk, then seal them off
3571 * for business.
3572 */
3573int
3574xfs_log_force_umount(
3575 struct xfs_mount *mp,
3576 int logerror)
3577{
3578 xlog_ticket_t *tic;
3579 xlog_t *log;
3580 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003581 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 SPLDECL(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584 log = mp->m_log;
3585
3586 /*
3587 * If this happens during log recovery, don't worry about
3588 * locking; the log isn't open for business yet.
3589 */
3590 if (!log ||
3591 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3592 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3593 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
3596
3597 /*
3598 * Somebody could've already done the hard work for us.
3599 * No need to get locks for this.
3600 */
3601 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3602 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003603 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605 retval = 0;
3606 /*
3607 * We must hold both the GRANT lock and the LOG lock,
3608 * before we mark the filesystem SHUTDOWN and wake
3609 * everybody up to tell the bad news.
3610 */
3611 s = GRANT_LOCK(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003612 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3614 XFS_BUF_DONE(mp->m_sb_bp);
3615 /*
3616 * This flag is sort of redundant because of the mount flag, but
3617 * it's good to maintain the separation between the log and the rest
3618 * of XFS.
3619 */
3620 log->l_flags |= XLOG_IO_ERROR;
3621
3622 /*
3623 * If we hit a log error, we want to mark all the iclogs IOERROR
3624 * while we're still holding the loglock.
3625 */
3626 if (logerror)
3627 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003628 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 /*
3631 * We don't want anybody waiting for log reservations
3632 * after this. That means we have to wake up everybody
3633 * queued up on reserve_headq as well as write_headq.
3634 * In addition, we make sure in xlog_{re}grant_log_space
3635 * that we don't enqueue anything once the SHUTDOWN flag
3636 * is set, and this action is protected by the GRANTLOCK.
3637 */
3638 if ((tic = log->l_reserve_headq)) {
3639 do {
3640 sv_signal(&tic->t_sema);
3641 tic = tic->t_next;
3642 } while (tic != log->l_reserve_headq);
3643 }
3644
3645 if ((tic = log->l_write_headq)) {
3646 do {
3647 sv_signal(&tic->t_sema);
3648 tic = tic->t_next;
3649 } while (tic != log->l_write_headq);
3650 }
3651 GRANT_UNLOCK(log, s);
3652
3653 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3654 ASSERT(!logerror);
3655 /*
3656 * Force the incore logs to disk before shutting the
3657 * log down completely.
3658 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003659 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003660 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003662 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 }
3664 /*
3665 * Wake up everybody waiting on xfs_log_force.
3666 * Callback all log item committed functions as if the
3667 * log writes were completed.
3668 */
3669 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3670
3671#ifdef XFSERRORDEBUG
3672 {
3673 xlog_in_core_t *iclog;
3674
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003675 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 iclog = log->l_iclog;
3677 do {
3678 ASSERT(iclog->ic_callback == 0);
3679 iclog = iclog->ic_next;
3680 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003681 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
3683#endif
3684 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003685 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686}
3687
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003688STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689xlog_iclogs_empty(xlog_t *log)
3690{
3691 xlog_in_core_t *iclog;
3692
3693 iclog = log->l_iclog;
3694 do {
3695 /* endianness does not matter here, zero is zero in
3696 * any language.
3697 */
3698 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 iclog = iclog->ic_next;
3701 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003702 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}