blob: 7a5b12d93537615f20c15fe56c2af798bba9f959 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000401 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Chinner114d23a2008-04-10 12:18:39 +1000403 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
405 if (!abortflg) {
406 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
407 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
408 cb->cb_next = NULL;
409 *(iclog->ic_callback_tail) = cb;
410 iclog->ic_callback_tail = &(cb->cb_next);
411 }
David Chinner114d23a2008-04-10 12:18:39 +1000412 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return abortflg;
414} /* xfs_log_notify */
415
416int
417xfs_log_release_iclog(xfs_mount_t *mp,
418 void *iclog_hndl)
419{
420 xlog_t *log = mp->m_log;
421 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
422
423 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000424 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100425 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
428 return 0;
429}
430
431/*
432 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
433 * to the reservation.
434 * 2. Potentially, push buffers at tail of log to disk.
435 *
436 * Each reservation is going to reserve extra space for a log record header.
437 * When writes happen to the on-disk log, we don't subtract the length of the
438 * log record header from any reservation. By wasting space in each
439 * reservation, we prevent over allocation problems.
440 */
441int
442xfs_log_reserve(xfs_mount_t *mp,
443 int unit_bytes,
444 int cnt,
445 xfs_log_ticket_t *ticket,
446 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000447 uint flags,
448 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 xlog_t *log = mp->m_log;
451 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100452 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
455 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
456
457 if (XLOG_FORCED_SHUTDOWN(log))
458 return XFS_ERROR(EIO);
459
460 XFS_STATS_INC(xs_try_logspace);
461
462 if (*ticket != NULL) {
463 ASSERT(flags & XFS_LOG_PERM_RESERV);
464 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000465 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
467 retval = xlog_regrant_write_log_space(log, internal_ticket);
468 } else {
469 /* may sleep if need to allocate more tickets */
470 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
471 client, flags);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000472 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000474 xlog_trace_loggrant(log, internal_ticket,
475 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
476 "xfs_log_reserve: create new ticket (permanent trans)" :
477 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 xlog_grant_push_ail(mp,
479 (internal_ticket->t_unit_res *
480 internal_ticket->t_cnt));
481 retval = xlog_grant_log_space(log, internal_ticket);
482 }
483
484 return retval;
485} /* xfs_log_reserve */
486
487
488/*
489 * Mount a log filesystem
490 *
491 * mp - ubiquitous xfs mount point structure
492 * log_target - buftarg of on-disk log device
493 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
494 * num_bblocks - Number of BBSIZE blocks in on-disk log
495 *
496 * Return error or zero.
497 */
498int
David Chinner249a8c12008-02-05 12:13:32 +1100499xfs_log_mount(
500 xfs_mount_t *mp,
501 xfs_buftarg_t *log_target,
502 xfs_daddr_t blk_offset,
503 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
David Chinner249a8c12008-02-05 12:13:32 +1100505 int error;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
508 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
509 else {
510 cmn_err(CE_NOTE,
511 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
512 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000513 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
David Chinner249a8c12008-02-05 12:13:32 +1100519 * Initialize the AIL now we have a log.
520 */
521 spin_lock_init(&mp->m_ail_lock);
522 error = xfs_trans_ail_init(mp);
523 if (error) {
524 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
525 goto error;
526 }
527
528 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * skip log recovery on a norecovery mount. pretend it all
530 * just worked.
531 */
532 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100533 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000536 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Eric Sandeen65be6052006-01-11 15:34:19 +1100538 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000541 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (error) {
543 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
David Chinner249a8c12008-02-05 12:13:32 +1100544 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 }
547
548 /* Normal transactions can now occur */
549 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
550
551 /* End mounting message in xfs_log_mount_finish */
552 return 0;
David Chinner249a8c12008-02-05 12:13:32 +1100553error:
554 xfs_log_unmount_dealloc(mp);
555 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556} /* xfs_log_mount */
557
558/*
559 * Finish the recovery of the file system. This is separate from
560 * the xfs_log_mount() call, because it depends on the code in
561 * xfs_mountfs() to read in the root and real-time bitmap inodes
562 * between calling xfs_log_mount() and here.
563 *
564 * mp - ubiquitous xfs mount point structure
565 */
566int
567xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
568{
569 int error;
570
571 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
572 error = xlog_recover_finish(mp->m_log, mfsi_flags);
573 else {
574 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000575 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 return error;
579}
580
581/*
582 * Unmount processing for the log.
583 */
584int
585xfs_log_unmount(xfs_mount_t *mp)
586{
587 int error;
588
589 error = xfs_log_unmount_write(mp);
590 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100591 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/*
595 * Final log writes as part of unmount.
596 *
597 * Mark the filesystem clean as unmount happens. Note that during relocation
598 * this routine needs to be executed as part of source-bag while the
599 * deallocation must not be done until source-end.
600 */
601
602/*
603 * Unmount record used to have a string "Unmount filesystem--" in the
604 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
605 * We just write the magic number now since that particular field isn't
606 * currently architecture converted and "nUmount" is a bit foo.
607 * As far as I know, there weren't any dependencies on the old behaviour.
608 */
609
610int
611xfs_log_unmount_write(xfs_mount_t *mp)
612{
613 xlog_t *log = mp->m_log;
614 xlog_in_core_t *iclog;
615#ifdef DEBUG
616 xlog_in_core_t *first_iclog;
617#endif
618 xfs_log_iovec_t reg[1];
619 xfs_log_ticket_t tic = NULL;
620 xfs_lsn_t lsn;
621 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* the data section must be 32 bit size aligned */
624 struct {
625 __uint16_t magic;
626 __uint16_t pad1;
627 __uint32_t pad2; /* may as well make it 64 bits */
628 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * Don't write out unmount record on read-only mounts.
632 * Or, if we are doing a forced umount (typically because of IO errors).
633 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000634 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636
637 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
638
639#ifdef DEBUG
640 first_iclog = iclog = log->l_iclog;
641 do {
642 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
643 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
644 ASSERT(iclog->ic_offset == 0);
645 }
646 iclog = iclog->ic_next;
647 } while (iclog != first_iclog);
648#endif
649 if (! (XLOG_FORCED_SHUTDOWN(log))) {
650 reg[0].i_addr = (void*)&magic;
651 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000652 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Tim Shimmin955e47a2006-09-28 11:04:16 +1000654 error = xfs_log_reserve(mp, 600, 1, &tic,
655 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!error) {
657 /* remove inited flag */
658 ((xlog_ticket_t *)tic)->t_flags = 0;
659 error = xlog_write(mp, reg, 1, tic, &lsn,
660 NULL, XLOG_UNMOUNT_TRANS);
661 /*
662 * At this point, we're umounting anyway,
663 * so there's no point in transitioning log state
664 * to IOERROR. Just continue...
665 */
666 }
667
668 if (error) {
669 xfs_fs_cmn_err(CE_ALERT, mp,
670 "xfs_log_unmount: unmount record failed");
671 }
672
673
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000674 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100676 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000677 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xlog_state_want_sync(log, iclog);
679 (void) xlog_state_release_iclog(log, iclog);
680
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000681 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
683 iclog->ic_state == XLOG_STATE_DIRTY)) {
684 if (!XLOG_FORCED_SHUTDOWN(log)) {
685 sv_wait(&iclog->ic_forcesema, PMEM,
686 &log->l_icloglock, s);
687 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000688 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000691 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000693 if (tic) {
694 xlog_trace_loggrant(log, tic, "unmount rec");
695 xlog_ungrant_log_space(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xlog_state_put_ticket(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 } else {
699 /*
700 * We're already in forced_shutdown mode, couldn't
701 * even attempt to write out the unmount transaction.
702 *
703 * Go through the motions of sync'ing and releasing
704 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000705 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * be in progress. Do this as a separate section of
707 * code so we'll know if we ever get stuck here that
708 * we're in this odd situation of trying to unmount
709 * a file system that went into forced_shutdown as
710 * the result of an unmount..
711 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000712 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100714 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000715 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 xlog_state_want_sync(log, iclog);
718 (void) xlog_state_release_iclog(log, iclog);
719
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000720 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
723 || iclog->ic_state == XLOG_STATE_DIRTY
724 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
725
726 sv_wait(&iclog->ic_forcesema, PMEM,
727 &log->l_icloglock, s);
728 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000729 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 }
732
733 return 0;
734} /* xfs_log_unmount_write */
735
736/*
737 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100738 *
739 * We need to stop the aild from running before we destroy
740 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
742void
743xfs_log_unmount_dealloc(xfs_mount_t *mp)
744{
David Chinner249a8c12008-02-05 12:13:32 +1100745 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000746 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * Write region vectors to log. The write happens using the space reservation
751 * of the ticket (tic). It is not a requirement that all writes for a given
752 * transaction occur with one call to xfs_log_write().
753 */
754int
755xfs_log_write(xfs_mount_t * mp,
756 xfs_log_iovec_t reg[],
757 int nentries,
758 xfs_log_ticket_t tic,
759 xfs_lsn_t *start_lsn)
760{
761 int error;
762 xlog_t *log = mp->m_log;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (XLOG_FORCED_SHUTDOWN(log))
765 return XFS_ERROR(EIO);
766
767 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000768 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100770 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771} /* xfs_log_write */
772
773
774void
775xfs_log_move_tail(xfs_mount_t *mp,
776 xfs_lsn_t tail_lsn)
777{
778 xlog_ticket_t *tic;
779 xlog_t *log = mp->m_log;
780 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (XLOG_FORCED_SHUTDOWN(log))
783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (tail_lsn == 0) {
786 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000787 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000789 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000792 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
795 * tail_lsn.
796 */
797 if (tail_lsn != 1) {
798 log->l_tail_lsn = tail_lsn;
799 }
800
801 if ((tic = log->l_write_headq)) {
802#ifdef DEBUG
803 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
804 panic("Recovery problem");
805#endif
806 cycle = log->l_grant_write_cycle;
807 bytes = log->l_grant_write_bytes;
808 free_bytes = xlog_space_left(log, cycle, bytes);
809 do {
810 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
811
812 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
813 break;
814 tail_lsn = 0;
815 free_bytes -= tic->t_unit_res;
816 sv_signal(&tic->t_sema);
817 tic = tic->t_next;
818 } while (tic != log->l_write_headq);
819 }
820 if ((tic = log->l_reserve_headq)) {
821#ifdef DEBUG
822 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
823 panic("Recovery problem");
824#endif
825 cycle = log->l_grant_reserve_cycle;
826 bytes = log->l_grant_reserve_bytes;
827 free_bytes = xlog_space_left(log, cycle, bytes);
828 do {
829 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
830 need_bytes = tic->t_unit_res*tic->t_cnt;
831 else
832 need_bytes = tic->t_unit_res;
833 if (free_bytes < need_bytes && tail_lsn != 1)
834 break;
835 tail_lsn = 0;
836 free_bytes -= need_bytes;
837 sv_signal(&tic->t_sema);
838 tic = tic->t_next;
839 } while (tic != log->l_reserve_headq);
840 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000841 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842} /* xfs_log_move_tail */
843
844/*
845 * Determine if we have a transaction that has gone to disk
846 * that needs to be covered. Log activity needs to be idle (no AIL and
847 * nothing in the iclogs). And, we need to be in the right state indicating
848 * something has gone out.
849 */
850int
851xfs_log_need_covered(xfs_mount_t *mp)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int needed = 0, gen;
854 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
David Chinner92821e22007-05-24 15:26:31 +1000856 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return 0;
858
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000859 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
861 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
862 && !xfs_trans_first_ail(mp, &gen)
863 && xlog_iclogs_empty(log)) {
864 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
865 log->l_covered_state = XLOG_STATE_COVER_DONE;
866 else {
867 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
868 log->l_covered_state = XLOG_STATE_COVER_DONE2;
869 }
870 needed = 1;
871 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000872 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100873 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/******************************************************************************
877 *
878 * local routines
879 *
880 ******************************************************************************
881 */
882
883/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
884 * The log manager must keep track of the last LR which was committed
885 * to disk. The lsn of this LR will become the new tail_lsn whenever
886 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
887 * the situation where stuff could be written into the log but nothing
888 * was ever in the AIL when asked. Eventually, we panic since the
889 * tail hits the head.
890 *
891 * We may be holding the log iclog lock upon entering this routine.
892 */
893xfs_lsn_t
894xlog_assign_tail_lsn(xfs_mount_t *mp)
895{
896 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xlog_t *log = mp->m_log;
898
899 tail_lsn = xfs_trans_tail_ail(mp);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000900 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (tail_lsn != 0) {
902 log->l_tail_lsn = tail_lsn;
903 } else {
904 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
905 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000906 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 return tail_lsn;
909} /* xlog_assign_tail_lsn */
910
911
912/*
913 * Return the space in the log between the tail and the head. The head
914 * is passed in the cycle/bytes formal parms. In the special case where
915 * the reserve head has wrapped passed the tail, this calculation is no
916 * longer valid. In this case, just return 0 which means there is no space
917 * in the log. This works for all places where this function is called
918 * with the reserve head. Of course, if the write head were to ever
919 * wrap the tail, we should blow up. Rather than catch this case here,
920 * we depend on other ASSERTions in other parts of the code. XXXmiken
921 *
922 * This code also handles the case where the reservation head is behind
923 * the tail. The details of this case are described below, but the end
924 * result is that we return the size of the log as the amount of space left.
925 */
David Chinnera8272ce2007-11-23 16:28:09 +1100926STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927xlog_space_left(xlog_t *log, int cycle, int bytes)
928{
929 int free_bytes;
930 int tail_bytes;
931 int tail_cycle;
932
933 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
934 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
935 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
936 free_bytes = log->l_logsize - (bytes - tail_bytes);
937 } else if ((tail_cycle + 1) < cycle) {
938 return 0;
939 } else if (tail_cycle < cycle) {
940 ASSERT(tail_cycle == (cycle - 1));
941 free_bytes = tail_bytes - bytes;
942 } else {
943 /*
944 * The reservation head is behind the tail.
945 * In this case we just want to return the size of the
946 * log as the amount of space left.
947 */
948 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
949 "xlog_space_left: head behind tail\n"
950 " tail_cycle = %d, tail_bytes = %d\n"
951 " GH cycle = %d, GH bytes = %d",
952 tail_cycle, tail_bytes, cycle, bytes);
953 ASSERT(0);
954 free_bytes = log->l_logsize;
955 }
956 return free_bytes;
957} /* xlog_space_left */
958
959
960/*
961 * Log function which is called when an io completes.
962 *
963 * The log manager needs its own routine, in order to control what
964 * happens with the buffer after the write completes.
965 */
966void
967xlog_iodone(xfs_buf_t *bp)
968{
969 xlog_in_core_t *iclog;
970 xlog_t *l;
971 int aborted;
972
973 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
974 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
975 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
976 aborted = 0;
977
978 /*
979 * Some versions of cpp barf on the recursive definition of
980 * ic_log -> hic_fields.ic_log and expand ic_log twice when
981 * it is passed through two macros. Workaround broken cpp.
982 */
983 l = iclog->ic_log;
984
985 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000986 * If the ordered flag has been removed by a lower
987 * layer, it means the underlyin device no longer supports
988 * barrier I/O. Warn loudly and turn off barriers.
989 */
990 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
991 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
992 xfs_fs_cmn_err(CE_WARN, l->l_mp,
993 "xlog_iodone: Barriers are no longer supported"
994 " by device. Disabling barriers\n");
995 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
996 }
997
998 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * Race to shutdown the filesystem if we see an error.
1000 */
1001 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1002 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1003 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1004 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001005 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /*
1007 * This flag will be propagated to the trans-committed
1008 * callback routines to let them know that the log-commit
1009 * didn't succeed.
1010 */
1011 aborted = XFS_LI_ABORTED;
1012 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1013 aborted = XFS_LI_ABORTED;
1014 }
David Chinner3db296f2007-05-14 18:24:16 +10001015
1016 /* log I/O is always issued ASYNC */
1017 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001019 /*
1020 * do not reference the buffer (bp) here as we could race
1021 * with it being freed after writing the unmount record to the
1022 * log.
1023 */
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025} /* xlog_iodone */
1026
1027/*
1028 * The bdstrat callback function for log bufs. This gives us a central
1029 * place to trap bufs in case we get hit by a log I/O error and need to
1030 * shutdown. Actually, in practice, even when we didn't get a log error,
1031 * we transition the iclogs to IOERROR state *after* flushing all existing
1032 * iclogs to disk. This is because we don't want anymore new transactions to be
1033 * started or completed afterwards.
1034 */
1035STATIC int
1036xlog_bdstrat_cb(struct xfs_buf *bp)
1037{
1038 xlog_in_core_t *iclog;
1039
1040 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1041
1042 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1043 /* note for irix bstrat will need struct bdevsw passed
1044 * Fix the following macro if the code ever is merged
1045 */
1046 XFS_bdstrat(bp);
1047 return 0;
1048 }
1049
1050 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1051 XFS_BUF_ERROR(bp, EIO);
1052 XFS_BUF_STALE(bp);
1053 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001054 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056
1057}
1058
1059/*
1060 * Return size of each in-core log record buffer.
1061 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001062 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 *
1064 * If the filesystem blocksize is too large, we may need to choose a
1065 * larger size since the directory code currently logs entire blocks.
1066 */
1067
1068STATIC void
1069xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1070 xlog_t *log)
1071{
1072 int size;
1073 int xhdrs;
1074
Eric Sandeen1cb51252007-08-16 16:24:43 +10001075 if (mp->m_logbufs <= 0)
1076 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1077 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001078 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /*
1081 * Buffer size passed in from mount system call.
1082 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001083 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 size = log->l_iclog_size = mp->m_logbsize;
1085 log->l_iclog_size_log = 0;
1086 while (size != 1) {
1087 log->l_iclog_size_log++;
1088 size >>= 1;
1089 }
1090
Eric Sandeen62118702008-03-06 13:44:28 +11001091 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* # headers = size / 32K
1093 * one header holds cycles from 32K of data
1094 */
1095
1096 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1097 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1098 xhdrs++;
1099 log->l_iclog_hsize = xhdrs << BBSHIFT;
1100 log->l_iclog_heads = xhdrs;
1101 } else {
1102 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1103 log->l_iclog_hsize = BBSIZE;
1104 log->l_iclog_heads = 1;
1105 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001106 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
Eric Sandeen1cb51252007-08-16 16:24:43 +10001109 /* All machines use 32KB buffers by default. */
1110 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1111 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /* the default log size is 16k or 32k which is one header sector */
1114 log->l_iclog_hsize = BBSIZE;
1115 log->l_iclog_heads = 1;
1116
1117 /*
1118 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1119 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1120 */
1121 if (mp->m_sb.sb_blocksize >= 16*1024) {
1122 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1123 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001124 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 switch (mp->m_sb.sb_blocksize) {
1126 case 16*1024: /* 16 KB */
1127 log->l_iclog_bufs = 3;
1128 break;
1129 case 32*1024: /* 32 KB */
1130 log->l_iclog_bufs = 4;
1131 break;
1132 case 64*1024: /* 64 KB */
1133 log->l_iclog_bufs = 8;
1134 break;
1135 default:
1136 xlog_panic("XFS: Invalid blocksize");
1137 break;
1138 }
1139 }
1140 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001141
1142done: /* are we being asked to make the sizes selected above visible? */
1143 if (mp->m_logbufs == 0)
1144 mp->m_logbufs = log->l_iclog_bufs;
1145 if (mp->m_logbsize == 0)
1146 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147} /* xlog_get_iclog_buffer_size */
1148
1149
1150/*
1151 * This routine initializes some of the log structure for a given mount point.
1152 * Its primary purpose is to fill in enough, so recovery can occur. However,
1153 * some other stuff may be filled in too.
1154 */
1155STATIC xlog_t *
1156xlog_alloc_log(xfs_mount_t *mp,
1157 xfs_buftarg_t *log_target,
1158 xfs_daddr_t blk_offset,
1159 int num_bblks)
1160{
1161 xlog_t *log;
1162 xlog_rec_header_t *head;
1163 xlog_in_core_t **iclogp;
1164 xlog_in_core_t *iclog, *prev_iclog=NULL;
1165 xfs_buf_t *bp;
1166 int i;
1167 int iclogsize;
1168
1169 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1170
1171 log->l_mp = mp;
1172 log->l_targ = log_target;
1173 log->l_logsize = BBTOB(num_bblks);
1174 log->l_logBBstart = blk_offset;
1175 log->l_logBBsize = num_bblks;
1176 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1177 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1178
1179 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001180 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1182 log->l_last_sync_lsn = log->l_tail_lsn;
1183 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1184 log->l_grant_reserve_cycle = 1;
1185 log->l_grant_write_cycle = 1;
1186
Eric Sandeen62118702008-03-06 13:44:28 +11001187 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1189 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1190 /* for larger sector sizes, must have v2 or external log */
1191 ASSERT(log->l_sectbb_log == 0 ||
1192 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001193 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1195 }
1196 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1197
1198 xlog_get_iclog_buffer_size(mp, log);
1199
1200 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1201 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1202 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1203 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1204 ASSERT(XFS_BUF_ISBUSY(bp));
1205 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1206 log->l_xbuf = bp;
1207
Eric Sandeen007c61c2007-10-11 17:43:56 +10001208 spin_lock_init(&log->l_icloglock);
1209 spin_lock_init(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 initnsema(&log->l_flushsema, 0, "ic-flush");
1211 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1212
1213 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1214 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1215
1216 iclogp = &log->l_iclog;
1217 /*
1218 * The amount of memory to allocate for the iclog structure is
1219 * rather funky due to the way the structure is defined. It is
1220 * done this way so that we can use different sizes for machines
1221 * with different amounts of memory. See the definition of
1222 * xlog_in_core_t in xfs_log_priv.h for details.
1223 */
1224 iclogsize = log->l_iclog_size;
1225 ASSERT(log->l_iclog_size >= 4096);
1226 for (i=0; i < log->l_iclog_bufs; i++) {
1227 *iclogp = (xlog_in_core_t *)
1228 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1229 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 iclog->ic_prev = prev_iclog;
1231 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001232
1233 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1234 if (!XFS_BUF_CPSEMA(bp))
1235 ASSERT(0);
1236 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1237 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1238 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1239 iclog->ic_bp = bp;
1240 iclog->hic_data = bp->b_addr;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1243
1244 head = &iclog->ic_header;
1245 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001246 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1247 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001248 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001249 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001251 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1256 iclog->ic_state = XLOG_STATE_ACTIVE;
1257 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001258 atomic_set(&iclog->ic_refcnt, 0);
1259 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 iclog->ic_callback_tail = &(iclog->ic_callback);
1261 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1262
1263 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1264 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1265 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1266 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1267
1268 iclogp = &iclog->ic_next;
1269 }
1270 *iclogp = log->l_iclog; /* complete ring */
1271 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1272
1273 return log;
1274} /* xlog_alloc_log */
1275
1276
1277/*
1278 * Write out the commit record of a transaction associated with the given
1279 * ticket. Return the lsn of the commit record.
1280 */
1281STATIC int
1282xlog_commit_record(xfs_mount_t *mp,
1283 xlog_ticket_t *ticket,
1284 xlog_in_core_t **iclog,
1285 xfs_lsn_t *commitlsnp)
1286{
1287 int error;
1288 xfs_log_iovec_t reg[1];
1289
1290 reg[0].i_addr = NULL;
1291 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001292 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 ASSERT_ALWAYS(iclog);
1295 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1296 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001297 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300} /* xlog_commit_record */
1301
1302
1303/*
1304 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1305 * log space. This code pushes on the lsn which would supposedly free up
1306 * the 25% which we want to leave free. We may need to adopt a policy which
1307 * pushes on an lsn which is further along in the log once we reach the high
1308 * water mark. In this manner, we would be creating a low water mark.
1309 */
David Chinnera8272ce2007-11-23 16:28:09 +11001310STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311xlog_grant_push_ail(xfs_mount_t *mp,
1312 int need_bytes)
1313{
1314 xlog_t *log = mp->m_log; /* pointer to the log */
1315 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1316 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1317 int free_blocks; /* free blocks left to write to */
1318 int free_bytes; /* free bytes left to write to */
1319 int threshold_block; /* block in lsn we'd like to be at */
1320 int threshold_cycle; /* lsn cycle we'd like to be at */
1321 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1324
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001325 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 free_bytes = xlog_space_left(log,
1327 log->l_grant_reserve_cycle,
1328 log->l_grant_reserve_bytes);
1329 tail_lsn = log->l_tail_lsn;
1330 free_blocks = BTOBBT(free_bytes);
1331
1332 /*
1333 * Set the threshold for the minimum number of free blocks in the
1334 * log to the maximum of what the caller needs, one quarter of the
1335 * log, and 256 blocks.
1336 */
1337 free_threshold = BTOBB(need_bytes);
1338 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1339 free_threshold = MAX(free_threshold, 256);
1340 if (free_blocks < free_threshold) {
1341 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1342 threshold_cycle = CYCLE_LSN(tail_lsn);
1343 if (threshold_block >= log->l_logBBsize) {
1344 threshold_block -= log->l_logBBsize;
1345 threshold_cycle += 1;
1346 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001347 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /* Don't pass in an lsn greater than the lsn of the last
1350 * log record known to be on disk.
1351 */
1352 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1353 threshold_lsn = log->l_last_sync_lsn;
1354 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001355 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
1358 * Get the transaction layer to kick the dirty buffers out to
1359 * disk asynchronously. No point in trying to do this if
1360 * the filesystem is shutting down.
1361 */
1362 if (threshold_lsn &&
1363 !XLOG_FORCED_SHUTDOWN(log))
1364 xfs_trans_push_ail(mp, threshold_lsn);
1365} /* xlog_grant_push_ail */
1366
1367
1368/*
1369 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1370 * fashion. Previously, we should have moved the current iclog
1371 * ptr in the log to point to the next available iclog. This allows further
1372 * write to continue while this code syncs out an iclog ready to go.
1373 * Before an in-core log can be written out, the data section must be scanned
1374 * to save away the 1st word of each BBSIZE block into the header. We replace
1375 * it with the current cycle count. Each BBSIZE block is tagged with the
1376 * cycle count because there in an implicit assumption that drives will
1377 * guarantee that entire 512 byte blocks get written at once. In other words,
1378 * we can't have part of a 512 byte block written and part not written. By
1379 * tagging each block, we will know which blocks are valid when recovering
1380 * after an unclean shutdown.
1381 *
1382 * This routine is single threaded on the iclog. No other thread can be in
1383 * this routine with the same iclog. Changing contents of iclog can there-
1384 * fore be done without grabbing the state machine lock. Updating the global
1385 * log will require grabbing the lock though.
1386 *
1387 * The entire log manager uses a logical block numbering scheme. Only
1388 * log_sync (and then only bwrite()) know about the fact that the log may
1389 * not start with block zero on a given device. The log block start offset
1390 * is added immediately before calling bwrite().
1391 */
1392
David Chinnera8272ce2007-11-23 16:28:09 +11001393STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394xlog_sync(xlog_t *log,
1395 xlog_in_core_t *iclog)
1396{
1397 xfs_caddr_t dptr; /* pointer to byte sized element */
1398 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001399 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 uint count; /* byte count of bwrite */
1401 uint count_init; /* initial count before roundup */
1402 int roundoff; /* roundoff to BB or stripe */
1403 int split = 0; /* split write into two regions */
1404 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001405 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001408 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* Add for LR header */
1411 count_init = log->l_iclog_hsize + iclog->ic_offset;
1412
1413 /* Round out the log write size */
1414 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1415 /* we have a v2 stripe unit to use */
1416 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1417 } else {
1418 count = BBTOB(BTOBB(count_init));
1419 }
1420 roundoff = count - count_init;
1421 ASSERT(roundoff >= 0);
1422 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1423 roundoff < log->l_mp->m_sb.sb_logsunit)
1424 ||
1425 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1426 roundoff < BBTOB(1)));
1427
1428 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001429 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001430 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001431 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* put cycle number in every block */
1434 xlog_pack_data(log, iclog, roundoff);
1435
1436 /* real byte length */
1437 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001438 iclog->ic_header.h_len =
1439 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001441 iclog->ic_header.h_len =
1442 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Nathan Scottf5faad72006-07-28 17:04:44 +10001445 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1447 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001448 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1451
1452 /* Do we need to split this write into 2 parts? */
1453 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1454 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1455 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1456 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1457 } else {
1458 iclog->ic_bwritecnt = 1;
1459 }
David Chinner511105b2007-05-24 15:21:57 +10001460 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001462 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 XFS_BUF_BUSY(bp);
1464 XFS_BUF_ASYNC(bp);
1465 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001466 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001467 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001469 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001470 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1473 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1474
1475 xlog_verify_iclog(log, iclog, count, B_TRUE);
1476
1477 /* account for log which doesn't start at block #0 */
1478 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1479 /*
1480 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1481 * is shutting down.
1482 */
1483 XFS_BUF_WRITE(bp);
1484
1485 if ((error = XFS_bwrite(bp))) {
1486 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1487 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001488 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001491 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1493 (unsigned long)1);
1494 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1495 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1496 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1497 (__psint_t)count), split);
1498 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001499 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 XFS_BUF_BUSY(bp);
1501 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001502 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1503 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 dptr = XFS_BUF_PTR(bp);
1505 /*
1506 * Bump the cycle numbers at the start of each block
1507 * since this part of the buffer is at the start of
1508 * a new cycle. Watch out for the header magic number
1509 * case, though.
1510 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001511 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001512 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001513 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001514 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dptr += BBSIZE;
1516 }
1517
1518 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1519 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1520
Nathan Scottc41564b2006-03-29 08:55:14 +10001521 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1523 XFS_BUF_WRITE(bp);
1524 if ((error = XFS_bwrite(bp))) {
1525 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1526 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001527 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531} /* xlog_sync */
1532
1533
1534/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001535 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
David Chinnera8272ce2007-11-23 16:28:09 +11001537STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001538xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 xlog_in_core_t *iclog, *next_iclog;
1541 xlog_ticket_t *tic, *next_tic;
1542 int i;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 iclog = log->l_iclog;
1545 for (i=0; i<log->l_iclog_bufs; i++) {
1546 sv_destroy(&iclog->ic_forcesema);
1547 sv_destroy(&iclog->ic_writesema);
1548 xfs_buf_free(iclog->ic_bp);
1549#ifdef XFS_LOG_TRACE
1550 if (iclog->ic_trace != NULL) {
1551 ktrace_free(iclog->ic_trace);
1552 }
1553#endif
1554 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 kmem_free(iclog, sizeof(xlog_in_core_t));
1556 iclog = next_iclog;
1557 }
1558 freesema(&log->l_flushsema);
1559 spinlock_destroy(&log->l_icloglock);
1560 spinlock_destroy(&log->l_grant_lock);
1561
1562 /* XXXsup take a look at this again. */
1563 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1564 !XLOG_FORCED_SHUTDOWN(log)) {
1565 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001566 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 log->l_ticket_cnt, log->l_ticket_tcnt);
1568 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1569
1570 } else {
1571 tic = log->l_unmount_free;
1572 while (tic) {
1573 next_tic = tic->t_next;
Tim Shimmine6a4b372007-11-23 16:30:42 +11001574 kmem_free(tic, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 tic = next_tic;
1576 }
1577 }
1578 xfs_buf_free(log->l_xbuf);
1579#ifdef XFS_LOG_TRACE
1580 if (log->l_trace != NULL) {
1581 ktrace_free(log->l_trace);
1582 }
1583 if (log->l_grant_trace != NULL) {
1584 ktrace_free(log->l_grant_trace);
1585 }
1586#endif
1587 log->l_mp->m_log = NULL;
1588 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001589} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591/*
1592 * Update counters atomically now that memcpy is done.
1593 */
1594/* ARGSUSED */
1595static inline void
1596xlog_state_finish_copy(xlog_t *log,
1597 xlog_in_core_t *iclog,
1598 int record_cnt,
1599 int copy_bytes)
1600{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001601 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001603 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 iclog->ic_offset += copy_bytes;
1605
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001606 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607} /* xlog_state_finish_copy */
1608
1609
1610
1611
1612/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001613 * print out info relating to regions written which consume
1614 * the reservation
1615 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001616STATIC void
1617xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1618{
1619 uint i;
1620 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1621
1622 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1623 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1624 "bformat",
1625 "bchunk",
1626 "efi_format",
1627 "efd_format",
1628 "iformat",
1629 "icore",
1630 "iext",
1631 "ibroot",
1632 "ilocal",
1633 "iattr_ext",
1634 "iattr_broot",
1635 "iattr_local",
1636 "qformat",
1637 "dquot",
1638 "quotaoff",
1639 "LR header",
1640 "unmount",
1641 "commit",
1642 "trans header"
1643 };
1644 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1645 "SETATTR_NOT_SIZE",
1646 "SETATTR_SIZE",
1647 "INACTIVE",
1648 "CREATE",
1649 "CREATE_TRUNC",
1650 "TRUNCATE_FILE",
1651 "REMOVE",
1652 "LINK",
1653 "RENAME",
1654 "MKDIR",
1655 "RMDIR",
1656 "SYMLINK",
1657 "SET_DMATTRS",
1658 "GROWFS",
1659 "STRAT_WRITE",
1660 "DIOSTRAT",
1661 "WRITE_SYNC",
1662 "WRITEID",
1663 "ADDAFORK",
1664 "ATTRINVAL",
1665 "ATRUNCATE",
1666 "ATTR_SET",
1667 "ATTR_RM",
1668 "ATTR_FLAG",
1669 "CLEAR_AGI_BUCKET",
1670 "QM_SBCHANGE",
1671 "DUMMY1",
1672 "DUMMY2",
1673 "QM_QUOTAOFF",
1674 "QM_DQALLOC",
1675 "QM_SETQLIM",
1676 "QM_DQCLUSTER",
1677 "QM_QINOCREATE",
1678 "QM_QUOTAOFF_END",
1679 "SB_UNIT",
1680 "FSYNC_TS",
1681 "GROWFSRT_ALLOC",
1682 "GROWFSRT_ZERO",
1683 "GROWFSRT_FREE",
1684 "SWAPEXT"
1685 };
1686
1687 xfs_fs_cmn_err(CE_WARN, mp,
1688 "xfs_log_write: reservation summary:\n"
1689 " trans type = %s (%u)\n"
1690 " unit res = %d bytes\n"
1691 " current res = %d bytes\n"
1692 " total reg = %u bytes (o/flow = %u bytes)\n"
1693 " ophdrs = %u (ophdr space = %u bytes)\n"
1694 " ophdr + reg = %u bytes\n"
1695 " num regions = %u\n",
1696 ((ticket->t_trans_type <= 0 ||
1697 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1698 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1699 ticket->t_trans_type,
1700 ticket->t_unit_res,
1701 ticket->t_curr_res,
1702 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1703 ticket->t_res_num_ophdrs, ophdr_spc,
1704 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001705 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001706 ticket->t_res_num);
1707
1708 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001709 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001710 cmn_err(CE_WARN,
1711 "region[%u]: %s - %u bytes\n",
1712 i,
1713 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1714 "bad-rtype" : res_type_str[r_type-1]),
1715 ticket->t_res_arr[i].r_len);
1716 }
1717}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001718
1719/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * Write some region out to in-core log
1721 *
1722 * This will be called when writing externally provided regions or when
1723 * writing out a commit record for a given transaction.
1724 *
1725 * General algorithm:
1726 * 1. Find total length of this write. This may include adding to the
1727 * lengths passed in.
1728 * 2. Check whether we violate the tickets reservation.
1729 * 3. While writing to this iclog
1730 * A. Reserve as much space in this iclog as can get
1731 * B. If this is first write, save away start lsn
1732 * C. While writing this region:
1733 * 1. If first write of transaction, write start record
1734 * 2. Write log operation header (header per region)
1735 * 3. Find out if we can fit entire region into this iclog
1736 * 4. Potentially, verify destination memcpy ptr
1737 * 5. Memcpy (partial) region
1738 * 6. If partial copy, release iclog; otherwise, continue
1739 * copying more regions into current iclog
1740 * 4. Mark want sync bit (in simulation mode)
1741 * 5. Release iclog for potential flush to on-disk log.
1742 *
1743 * ERRORS:
1744 * 1. Panic if reservation is overrun. This should never happen since
1745 * reservation amounts are generated internal to the filesystem.
1746 * NOTES:
1747 * 1. Tickets are single threaded data structures.
1748 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1749 * syncing routine. When a single log_write region needs to span
1750 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1751 * on all log operation writes which don't contain the end of the
1752 * region. The XLOG_END_TRANS bit is used for the in-core log
1753 * operation which contains the end of the continued log_write region.
1754 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1755 * we don't really know exactly how much space will be used. As a result,
1756 * we don't update ic_offset until the end when we know exactly how many
1757 * bytes have been written out.
1758 */
David Chinnera8272ce2007-11-23 16:28:09 +11001759STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760xlog_write(xfs_mount_t * mp,
1761 xfs_log_iovec_t reg[],
1762 int nentries,
1763 xfs_log_ticket_t tic,
1764 xfs_lsn_t *start_lsn,
1765 xlog_in_core_t **commit_iclog,
1766 uint flags)
1767{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001768 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001770 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 __psint_t ptr; /* copy address into data region */
1773 int len; /* # xlog_write() bytes 2 still copy */
1774 int index; /* region index currently copying */
1775 int log_offset; /* offset (from 0) into data region */
1776 int start_rec_copy; /* # bytes to copy for start record */
1777 int partial_copy; /* did we split a region? */
1778 int partial_copy_len;/* # bytes copied if split region */
1779 int need_copy; /* # bytes need to memcpy this region */
1780 int copy_len; /* # bytes actually memcpy'ing */
1781 int copy_off; /* # bytes from entry start */
1782 int contwr; /* continued write of in-core log? */
1783 int error;
1784 int record_cnt = 0, data_cnt = 0;
1785
1786 partial_copy_len = partial_copy = 0;
1787
1788 /* Calculate potential maximum space. Each region gets its own
1789 * xlog_op_header_t and may need to be double word aligned.
1790 */
1791 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001792 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001794 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 for (index = 0; index < nentries; index++) {
1798 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001799 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001801 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803 contwr = *start_lsn = 0;
1804
1805 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001806 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#ifdef DEBUG
1808 xlog_panic(
1809 "xfs_log_write: reservation ran out. Need to up reservation");
1810#else
1811 /* Customer configurable panic */
1812 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1813 "xfs_log_write: reservation ran out. Need to up reservation");
1814 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001815 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816#endif
1817 } else
1818 ticket->t_curr_res -= len;
1819
1820 for (index = 0; index < nentries; ) {
1821 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1822 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001823 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 ASSERT(log_offset <= iclog->ic_size - 1);
1826 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1827
1828 /* start_lsn is the first lsn written to. That's all we need. */
1829 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001830 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 /* This loop writes out as many regions as can fit in the amount
1833 * of space which was allocated by xlog_state_get_iclog_space().
1834 */
1835 while (index < nentries) {
1836 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1837 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1838 start_rec_copy = 0;
1839
1840 /* If first write for transaction, insert start record.
1841 * We can't be trying to commit if we are inited. We can't
1842 * have any "partial_copy" if we are inited.
1843 */
1844 if (ticket->t_flags & XLOG_TIC_INITED) {
1845 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001846 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 logop_head->oh_clientid = ticket->t_clientid;
1848 logop_head->oh_len = 0;
1849 logop_head->oh_flags = XLOG_START_TRANS;
1850 logop_head->oh_res2 = 0;
1851 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1852 record_cnt++;
1853
1854 start_rec_copy = sizeof(xlog_op_header_t);
1855 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1856 }
1857
1858 /* Copy log operation header directly into data section */
1859 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001860 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 logop_head->oh_clientid = ticket->t_clientid;
1862 logop_head->oh_res2 = 0;
1863
1864 /* header copied directly */
1865 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1866
1867 /* are we copying a commit or unmount record? */
1868 logop_head->oh_flags = flags;
1869
1870 /*
1871 * We've seen logs corrupted with bad transaction client
1872 * ids. This makes sure that XFS doesn't generate them on.
1873 * Turn this into an EIO and shut down the filesystem.
1874 */
1875 switch (logop_head->oh_clientid) {
1876 case XFS_TRANSACTION:
1877 case XFS_VOLUME:
1878 case XFS_LOG:
1879 break;
1880 default:
1881 xfs_fs_cmn_err(CE_WARN, mp,
1882 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1883 logop_head->oh_clientid, tic);
1884 return XFS_ERROR(EIO);
1885 }
1886
1887 /* Partial write last time? => (partial_copy != 0)
1888 * need_copy is the amount we'd like to copy if everything could
1889 * fit in the current memcpy.
1890 */
1891 need_copy = reg[index].i_len - partial_copy_len;
1892
1893 copy_off = partial_copy_len;
1894 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001895 copy_len = need_copy;
1896 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (partial_copy)
1898 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1899 partial_copy_len = partial_copy = 0;
1900 } else { /* partial write */
1901 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001902 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1904 if (partial_copy)
1905 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1906 partial_copy_len += copy_len;
1907 partial_copy++;
1908 len += sizeof(xlog_op_header_t); /* from splitting of region */
1909 /* account for new log op header */
1910 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001911 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 xlog_verify_dest_ptr(log, ptr);
1914
1915 /* copy region */
1916 ASSERT(copy_len >= 0);
1917 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1918 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1919
1920 /* make copy_len total bytes copied, including headers */
1921 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1922 record_cnt++;
1923 data_cnt += contwr ? copy_len : 0;
1924 if (partial_copy) { /* copied partial region */
1925 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1926 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1927 record_cnt = data_cnt = 0;
1928 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001929 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break; /* don't increment index */
1931 } else { /* copied entire region */
1932 index++;
1933 partial_copy_len = partial_copy = 0;
1934
1935 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1936 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1937 record_cnt = data_cnt = 0;
1938 xlog_state_want_sync(log, iclog);
1939 if (commit_iclog) {
1940 ASSERT(flags & XLOG_COMMIT_TRANS);
1941 *commit_iclog = iclog;
1942 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (index == nentries)
1945 return 0; /* we are done */
1946 else
1947 break;
1948 }
1949 } /* if (partial_copy) */
1950 } /* while (index < nentries) */
1951 } /* for (index = 0; index < nentries; ) */
1952 ASSERT(len == 0);
1953
1954 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1955 if (commit_iclog) {
1956 ASSERT(flags & XLOG_COMMIT_TRANS);
1957 *commit_iclog = iclog;
1958 return 0;
1959 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001960 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961} /* xlog_write */
1962
1963
1964/*****************************************************************************
1965 *
1966 * State Machine functions
1967 *
1968 *****************************************************************************
1969 */
1970
1971/* Clean iclogs starting from the head. This ordering must be
1972 * maintained, so an iclog doesn't become ACTIVE beyond one that
1973 * is SYNCING. This is also required to maintain the notion that we use
1974 * a counting semaphore to hold off would be writers to the log when every
1975 * iclog is trying to sync to disk.
1976 *
1977 * State Change: DIRTY -> ACTIVE
1978 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001979STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980xlog_state_clean_log(xlog_t *log)
1981{
1982 xlog_in_core_t *iclog;
1983 int changed = 0;
1984
1985 iclog = log->l_iclog;
1986 do {
1987 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1988 iclog->ic_state = XLOG_STATE_ACTIVE;
1989 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001990 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * If the number of ops in this iclog indicate it just
1993 * contains the dummy transaction, we can
1994 * change state into IDLE (the second time around).
1995 * Otherwise we should change the state into
1996 * NEED a dummy.
1997 * We don't need to cover the dummy.
1998 */
1999 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002000 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2001 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 changed = 1;
2003 } else {
2004 /*
2005 * We have two dirty iclogs so start over
2006 * This could also be num of ops indicates
2007 * this is not the dummy going out.
2008 */
2009 changed = 2;
2010 }
2011 iclog->ic_header.h_num_logops = 0;
2012 memset(iclog->ic_header.h_cycle_data, 0,
2013 sizeof(iclog->ic_header.h_cycle_data));
2014 iclog->ic_header.h_lsn = 0;
2015 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2016 /* do nothing */;
2017 else
2018 break; /* stop cleaning */
2019 iclog = iclog->ic_next;
2020 } while (iclog != log->l_iclog);
2021
2022 /* log is locked when we are called */
2023 /*
2024 * Change state for the dummy log recording.
2025 * We usually go to NEED. But we go to NEED2 if the changed indicates
2026 * we are done writing the dummy record.
2027 * If we are done with the second dummy recored (DONE2), then
2028 * we go to IDLE.
2029 */
2030 if (changed) {
2031 switch (log->l_covered_state) {
2032 case XLOG_STATE_COVER_IDLE:
2033 case XLOG_STATE_COVER_NEED:
2034 case XLOG_STATE_COVER_NEED2:
2035 log->l_covered_state = XLOG_STATE_COVER_NEED;
2036 break;
2037
2038 case XLOG_STATE_COVER_DONE:
2039 if (changed == 1)
2040 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2041 else
2042 log->l_covered_state = XLOG_STATE_COVER_NEED;
2043 break;
2044
2045 case XLOG_STATE_COVER_DONE2:
2046 if (changed == 1)
2047 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2048 else
2049 log->l_covered_state = XLOG_STATE_COVER_NEED;
2050 break;
2051
2052 default:
2053 ASSERT(0);
2054 }
2055 }
2056} /* xlog_state_clean_log */
2057
2058STATIC xfs_lsn_t
2059xlog_get_lowest_lsn(
2060 xlog_t *log)
2061{
2062 xlog_in_core_t *lsn_log;
2063 xfs_lsn_t lowest_lsn, lsn;
2064
2065 lsn_log = log->l_iclog;
2066 lowest_lsn = 0;
2067 do {
2068 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002069 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if ((lsn && !lowest_lsn) ||
2071 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2072 lowest_lsn = lsn;
2073 }
2074 }
2075 lsn_log = lsn_log->ic_next;
2076 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002077 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080
2081STATIC void
2082xlog_state_do_callback(
2083 xlog_t *log,
2084 int aborted,
2085 xlog_in_core_t *ciclog)
2086{
2087 xlog_in_core_t *iclog;
2088 xlog_in_core_t *first_iclog; /* used to know when we've
2089 * processed all iclogs once */
2090 xfs_log_callback_t *cb, *cb_next;
2091 int flushcnt = 0;
2092 xfs_lsn_t lowest_lsn;
2093 int ioerrors; /* counter: iclogs with errors */
2094 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2095 int funcdidcallbacks; /* flag: function did callbacks */
2096 int repeats; /* for issuing console warnings if
2097 * looping too many times */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002099 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 first_iclog = iclog = log->l_iclog;
2101 ioerrors = 0;
2102 funcdidcallbacks = 0;
2103 repeats = 0;
2104
2105 do {
2106 /*
2107 * Scan all iclogs starting with the one pointed to by the
2108 * log. Reset this starting point each time the log is
2109 * unlocked (during callbacks).
2110 *
2111 * Keep looping through iclogs until one full pass is made
2112 * without running any callbacks.
2113 */
2114 first_iclog = log->l_iclog;
2115 iclog = log->l_iclog;
2116 loopdidcallbacks = 0;
2117 repeats++;
2118
2119 do {
2120
2121 /* skip all iclogs in the ACTIVE & DIRTY states */
2122 if (iclog->ic_state &
2123 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2124 iclog = iclog->ic_next;
2125 continue;
2126 }
2127
2128 /*
2129 * Between marking a filesystem SHUTDOWN and stopping
2130 * the log, we do flush all iclogs to disk (if there
2131 * wasn't a log I/O error). So, we do want things to
2132 * go smoothly in case of just a SHUTDOWN w/o a
2133 * LOG_IO_ERROR.
2134 */
2135 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2136 /*
2137 * Can only perform callbacks in order. Since
2138 * this iclog is not in the DONE_SYNC/
2139 * DO_CALLBACK state, we skip the rest and
2140 * just try to clean up. If we set our iclog
2141 * to DO_CALLBACK, we will not process it when
2142 * we retry since a previous iclog is in the
2143 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002144 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 */
2146 if (!(iclog->ic_state &
2147 (XLOG_STATE_DONE_SYNC |
2148 XLOG_STATE_DO_CALLBACK))) {
2149 if (ciclog && (ciclog->ic_state ==
2150 XLOG_STATE_DONE_SYNC)) {
2151 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2152 }
2153 break;
2154 }
2155 /*
2156 * We now have an iclog that is in either the
2157 * DO_CALLBACK or DONE_SYNC states. The other
2158 * states (WANT_SYNC, SYNCING, or CALLBACK were
2159 * caught by the above if and are going to
2160 * clean (i.e. we aren't doing their callbacks)
2161 * see the above if.
2162 */
2163
2164 /*
2165 * We will do one more check here to see if we
2166 * have chased our tail around.
2167 */
2168
2169 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002170 if (lowest_lsn &&
2171 XFS_LSN_CMP(lowest_lsn,
2172 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 iclog = iclog->ic_next;
2174 continue; /* Leave this iclog for
2175 * another thread */
2176 }
2177
2178 iclog->ic_state = XLOG_STATE_CALLBACK;
2179
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002180 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002183 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 * No one else can be here except us.
2185 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002186 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002187 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2188 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2189 log->l_last_sync_lsn =
2190 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002191 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002194 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 ioerrors++;
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
David Chinner114d23a2008-04-10 12:18:39 +10002198 /*
2199 * Keep processing entries in the callback list until
2200 * we come around and it is empty. We need to
2201 * atomically see that the list is empty and change the
2202 * state to DIRTY so that we don't miss any more
2203 * callbacks being added.
2204 */
2205 spin_lock(&iclog->ic_callback_lock);
2206 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002207 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 iclog->ic_callback_tail = &(iclog->ic_callback);
2209 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002210 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002213 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 cb_next = cb->cb_next;
2215 cb->cb_func(cb->cb_arg, aborted);
2216 }
David Chinner114d23a2008-04-10 12:18:39 +10002217 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 cb = iclog->ic_callback;
2219 }
2220
2221 loopdidcallbacks++;
2222 funcdidcallbacks++;
2223
David Chinner114d23a2008-04-10 12:18:39 +10002224 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002225 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002226 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2228 iclog->ic_state = XLOG_STATE_DIRTY;
2229
2230 /*
2231 * Transition from DIRTY to ACTIVE if applicable.
2232 * NOP if STATE_IOERROR.
2233 */
2234 xlog_state_clean_log(log);
2235
2236 /* wake up threads waiting in xfs_log_force() */
2237 sv_broadcast(&iclog->ic_forcesema);
2238
2239 iclog = iclog->ic_next;
2240 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002241
2242 if (repeats > 5000) {
2243 flushcnt += repeats;
2244 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002246 "%s: possible infinite loop (%d iterations)",
2247 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249 } while (!ioerrors && loopdidcallbacks);
2250
2251 /*
2252 * make one last gasp attempt to see if iclogs are being left in
2253 * limbo..
2254 */
2255#ifdef DEBUG
2256 if (funcdidcallbacks) {
2257 first_iclog = iclog = log->l_iclog;
2258 do {
2259 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2260 /*
2261 * Terminate the loop if iclogs are found in states
2262 * which will cause other threads to clean up iclogs.
2263 *
2264 * SYNCING - i/o completion will go through logs
2265 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002266 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * IOERROR - give up hope all ye who enter here
2268 */
2269 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2270 iclog->ic_state == XLOG_STATE_SYNCING ||
2271 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2272 iclog->ic_state == XLOG_STATE_IOERROR )
2273 break;
2274 iclog = iclog->ic_next;
2275 } while (first_iclog != iclog);
2276 }
2277#endif
2278
Nathan Scotta3c66852006-09-28 11:02:14 +10002279 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2281 flushcnt = log->l_flushcnt;
2282 log->l_flushcnt = 0;
2283 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002284 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 while (flushcnt--)
2286 vsema(&log->l_flushsema);
2287} /* xlog_state_do_callback */
2288
2289
2290/*
2291 * Finish transitioning this iclog to the dirty state.
2292 *
2293 * Make sure that we completely execute this routine only when this is
2294 * the last call to the iclog. There is a good chance that iclog flushes,
2295 * when we reach the end of the physical log, get turned into 2 separate
2296 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2297 * routine. By using the reference count bwritecnt, we guarantee that only
2298 * the second completion goes through.
2299 *
2300 * Callbacks could take time, so they are done outside the scope of the
2301 * global state machine log lock. Assume that the calls to cvsema won't
2302 * take a long time. At least we know it won't sleep.
2303 */
David Chinnera8272ce2007-11-23 16:28:09 +11002304STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305xlog_state_done_syncing(
2306 xlog_in_core_t *iclog,
2307 int aborted)
2308{
2309 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002311 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2314 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002315 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2317
2318
2319 /*
2320 * If we got an error, either on the first buffer, or in the case of
2321 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2322 * and none should ever be attempted to be written to disk
2323 * again.
2324 */
2325 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2326 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002327 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return;
2329 }
2330 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2331 }
2332
2333 /*
2334 * Someone could be sleeping prior to writing out the next
2335 * iclog buffer, we wake them all, one will get to do the
2336 * I/O, the others get to wait for the result.
2337 */
2338 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002339 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2341} /* xlog_state_done_syncing */
2342
2343
2344/*
2345 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2346 * sleep. The flush semaphore is set to the number of in-core buffers and
2347 * decremented around disk syncing. Therefore, if all buffers are syncing,
2348 * this semaphore will cause new writes to sleep until a sync completes.
2349 * Otherwise, this code just does p() followed by v(). This approximates
2350 * a sleep/wakeup except we can't race.
2351 *
2352 * The in-core logs are used in a circular fashion. They are not used
2353 * out-of-order even when an iclog past the head is free.
2354 *
2355 * return:
2356 * * log_offset where xlog_write() can start writing into the in-core
2357 * log's data space.
2358 * * in-core log pointer to which xlog_write() should write.
2359 * * boolean indicating this is a continued write to an in-core log.
2360 * If this is the last write, then the in-core log's offset field
2361 * needs to be incremented, depending on the amount of data which
2362 * is copied.
2363 */
David Chinnera8272ce2007-11-23 16:28:09 +11002364STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365xlog_state_get_iclog_space(xlog_t *log,
2366 int len,
2367 xlog_in_core_t **iclogp,
2368 xlog_ticket_t *ticket,
2369 int *continued_write,
2370 int *logoffsetp)
2371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 int log_offset;
2373 xlog_rec_header_t *head;
2374 xlog_in_core_t *iclog;
2375 int error;
2376
2377restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002378 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002380 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return XFS_ERROR(EIO);
2382 }
2383
2384 iclog = log->l_iclog;
2385 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2386 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002387 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2389 XFS_STATS_INC(xs_log_noiclogs);
2390 /* Ensure that log writes happen */
2391 psema(&log->l_flushsema, PINOD);
2392 goto restart;
2393 }
2394 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2395 head = &iclog->ic_header;
2396
David Chinner155cc6b2008-03-06 13:44:14 +11002397 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 log_offset = iclog->ic_offset;
2399
2400 /* On the 1st write to an iclog, figure out lsn. This works
2401 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2402 * committing to. If the offset is set, that's how many blocks
2403 * must be written.
2404 */
2405 if (log_offset == 0) {
2406 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002407 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002408 log->l_iclog_hsize,
2409 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002410 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2411 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002412 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 ASSERT(log->l_curr_block >= 0);
2414 }
2415
2416 /* If there is enough room to write everything, then do it. Otherwise,
2417 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2418 * bit is on, so this will get flushed out. Don't update ic_offset
2419 * until you know exactly how many bytes get copied. Therefore, wait
2420 * until later to update ic_offset.
2421 *
2422 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2423 * can fit into remaining data section.
2424 */
2425 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2426 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2427
2428 /* If I'm the only one writing to this iclog, sync it to disk */
David Chinner155cc6b2008-03-06 13:44:14 +11002429 if (atomic_read(&iclog->ic_refcnt) == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002430 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002432 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002434 atomic_dec(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002435 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437 goto restart;
2438 }
2439
2440 /* Do we have enough room to write the full amount in the remainder
2441 * of this iclog? Or must we continue a write on the next iclog and
2442 * mark this iclog as completely taken? In the case where we switch
2443 * iclogs (to mark it taken), this particular iclog will release/sync
2444 * to disk in xlog_write().
2445 */
2446 if (len <= iclog->ic_size - iclog->ic_offset) {
2447 *continued_write = 0;
2448 iclog->ic_offset += len;
2449 } else {
2450 *continued_write = 1;
2451 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2452 }
2453 *iclogp = iclog;
2454
2455 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002456 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 *logoffsetp = log_offset;
2459 return 0;
2460} /* xlog_state_get_iclog_space */
2461
2462/*
2463 * Atomically get the log space required for a log ticket.
2464 *
2465 * Once a ticket gets put onto the reserveq, it will only return after
2466 * the needed reservation is satisfied.
2467 */
2468STATIC int
2469xlog_grant_log_space(xlog_t *log,
2470 xlog_ticket_t *tic)
2471{
2472 int free_bytes;
2473 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474#ifdef DEBUG
2475 xfs_lsn_t tail_lsn;
2476#endif
2477
2478
2479#ifdef DEBUG
2480 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2481 panic("grant Recovery problem");
2482#endif
2483
2484 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002485 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2487
2488 /* something is already sleeping; insert new transaction at end */
2489 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002490 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 xlog_trace_loggrant(log, tic,
2492 "xlog_grant_log_space: sleep 1");
2493 /*
2494 * Gotta check this before going to sleep, while we're
2495 * holding the grant lock.
2496 */
2497 if (XLOG_FORCED_SHUTDOWN(log))
2498 goto error_return;
2499
2500 XFS_STATS_INC(xs_sleep_logspace);
2501 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2502 /*
2503 * If we got an error, and the filesystem is shutting down,
2504 * we'll catch it down below. So just continue...
2505 */
2506 xlog_trace_loggrant(log, tic,
2507 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002508 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
2510 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2511 need_bytes = tic->t_unit_res*tic->t_ocnt;
2512 else
2513 need_bytes = tic->t_unit_res;
2514
2515redo:
2516 if (XLOG_FORCED_SHUTDOWN(log))
2517 goto error_return;
2518
2519 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2520 log->l_grant_reserve_bytes);
2521 if (free_bytes < need_bytes) {
2522 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002523 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 xlog_trace_loggrant(log, tic,
2525 "xlog_grant_log_space: sleep 2");
2526 XFS_STATS_INC(xs_sleep_logspace);
2527 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2528
2529 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002530 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 goto error_return;
2532 }
2533
2534 xlog_trace_loggrant(log, tic,
2535 "xlog_grant_log_space: wake 2");
2536 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002537 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 goto redo;
2539 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002540 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002543 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544#ifdef DEBUG
2545 tail_lsn = log->l_tail_lsn;
2546 /*
2547 * Check to make sure the grant write head didn't just over lap the
2548 * tail. If the cycles are the same, we can't be overlapping.
2549 * Otherwise, make sure that the cycles differ by exactly one and
2550 * check the byte count.
2551 */
2552 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2553 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2554 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2555 }
2556#endif
2557 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2558 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002559 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return 0;
2561
2562 error_return:
2563 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002564 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2566 /*
2567 * If we are failing, make sure the ticket doesn't have any
2568 * current reservations. We don't want to add this back when
2569 * the ticket/transaction gets cancelled.
2570 */
2571 tic->t_curr_res = 0;
2572 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002573 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return XFS_ERROR(EIO);
2575} /* xlog_grant_log_space */
2576
2577
2578/*
2579 * Replenish the byte reservation required by moving the grant write head.
2580 *
2581 *
2582 */
2583STATIC int
2584xlog_regrant_write_log_space(xlog_t *log,
2585 xlog_ticket_t *tic)
2586{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 int free_bytes, need_bytes;
2588 xlog_ticket_t *ntic;
2589#ifdef DEBUG
2590 xfs_lsn_t tail_lsn;
2591#endif
2592
2593 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002594 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599#ifdef DEBUG
2600 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2601 panic("regrant Recovery problem");
2602#endif
2603
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002604 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2606
2607 if (XLOG_FORCED_SHUTDOWN(log))
2608 goto error_return;
2609
2610 /* If there are other waiters on the queue then give them a
2611 * chance at logspace before us. Wake up the first waiters,
2612 * if we do not wake up all the waiters then go to sleep waiting
2613 * for more free space, otherwise try to get some space for
2614 * this transaction.
2615 */
2616
2617 if ((ntic = log->l_write_headq)) {
2618 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2619 log->l_grant_write_bytes);
2620 do {
2621 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2622
2623 if (free_bytes < ntic->t_unit_res)
2624 break;
2625 free_bytes -= ntic->t_unit_res;
2626 sv_signal(&ntic->t_sema);
2627 ntic = ntic->t_next;
2628 } while (ntic != log->l_write_headq);
2629
2630 if (ntic != log->l_write_headq) {
2631 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002632 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 xlog_trace_loggrant(log, tic,
2635 "xlog_regrant_write_log_space: sleep 1");
2636 XFS_STATS_INC(xs_sleep_logspace);
2637 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2638 &log->l_grant_lock, s);
2639
2640 /* If we're shutting down, this tic is already
2641 * off the queue */
2642 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002643 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 goto error_return;
2645 }
2646
2647 xlog_trace_loggrant(log, tic,
2648 "xlog_regrant_write_log_space: wake 1");
2649 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002650 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652 }
2653
2654 need_bytes = tic->t_unit_res;
2655
2656redo:
2657 if (XLOG_FORCED_SHUTDOWN(log))
2658 goto error_return;
2659
2660 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2661 log->l_grant_write_bytes);
2662 if (free_bytes < need_bytes) {
2663 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002664 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 XFS_STATS_INC(xs_sleep_logspace);
2666 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2667
2668 /* If we're shutting down, this tic is already off the queue */
2669 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002670 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 goto error_return;
2672 }
2673
2674 xlog_trace_loggrant(log, tic,
2675 "xlog_regrant_write_log_space: wake 2");
2676 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002677 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 goto redo;
2679 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002680 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002682 /* we've got enough space */
2683 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684#ifdef DEBUG
2685 tail_lsn = log->l_tail_lsn;
2686 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2687 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2688 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2689 }
2690#endif
2691
2692 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2693 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002694 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697
2698 error_return:
2699 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002700 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2702 /*
2703 * If we are failing, make sure the ticket doesn't have any
2704 * current reservations. We don't want to add this back when
2705 * the ticket/transaction gets cancelled.
2706 */
2707 tic->t_curr_res = 0;
2708 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002709 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return XFS_ERROR(EIO);
2711} /* xlog_regrant_write_log_space */
2712
2713
2714/* The first cnt-1 times through here we don't need to
2715 * move the grant write head because the permanent
2716 * reservation has reserved cnt times the unit amount.
2717 * Release part of current permanent unit reservation and
2718 * reset current reservation to be one units worth. Also
2719 * move grant reservation head forward.
2720 */
2721STATIC void
2722xlog_regrant_reserve_log_space(xlog_t *log,
2723 xlog_ticket_t *ticket)
2724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 xlog_trace_loggrant(log, ticket,
2726 "xlog_regrant_reserve_log_space: enter");
2727 if (ticket->t_cnt > 0)
2728 ticket->t_cnt--;
2729
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002730 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002731 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002733 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 xlog_trace_loggrant(log, ticket,
2735 "xlog_regrant_reserve_log_space: sub current res");
2736 xlog_verify_grant_head(log, 1);
2737
2738 /* just return if we still have some of the pre-reserved space */
2739 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002740 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return;
2742 }
2743
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002744 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 xlog_trace_loggrant(log, ticket,
2746 "xlog_regrant_reserve_log_space: exit");
2747 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002748 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002750 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751} /* xlog_regrant_reserve_log_space */
2752
2753
2754/*
2755 * Give back the space left from a reservation.
2756 *
2757 * All the information we need to make a correct determination of space left
2758 * is present. For non-permanent reservations, things are quite easy. The
2759 * count should have been decremented to zero. We only need to deal with the
2760 * space remaining in the current reservation part of the ticket. If the
2761 * ticket contains a permanent reservation, there may be left over space which
2762 * needs to be released. A count of N means that N-1 refills of the current
2763 * reservation can be done before we need to ask for more space. The first
2764 * one goes to fill up the first current reservation. Once we run out of
2765 * space, the count will stay at zero and the only space remaining will be
2766 * in the current reservation field.
2767 */
2768STATIC void
2769xlog_ungrant_log_space(xlog_t *log,
2770 xlog_ticket_t *ticket)
2771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (ticket->t_cnt > 0)
2773 ticket->t_cnt--;
2774
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002775 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2777
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002778 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2781
2782 /* If this is a permanent reservation ticket, we may be able to free
2783 * up more space based on the remaining count.
2784 */
2785 if (ticket->t_cnt > 0) {
2786 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002787 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789
2790 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2791 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002792 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 xfs_log_move_tail(log->l_mp, 1);
2794} /* xlog_ungrant_log_space */
2795
2796
2797/*
2798 * Atomically put back used ticket.
2799 */
David Chinnera8272ce2007-11-23 16:28:09 +11002800STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801xlog_state_put_ticket(xlog_t *log,
2802 xlog_ticket_t *tic)
2803{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002804 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 xlog_ticket_put(log, tic);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002806 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807} /* xlog_state_put_ticket */
2808
2809/*
2810 * Flush iclog to disk if this is the last reference to the given iclog and
2811 * the WANT_SYNC bit is set.
2812 *
2813 * When this function is entered, the iclog is not necessarily in the
2814 * WANT_SYNC state. It may be sitting around waiting to get filled.
2815 *
2816 *
2817 */
David Chinnera8272ce2007-11-23 16:28:09 +11002818STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002819xlog_state_release_iclog(
2820 xlog_t *log,
2821 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 int sync = 0; /* do we sync? */
2824
David Chinner155cc6b2008-03-06 13:44:14 +11002825 if (iclog->ic_state & XLOG_STATE_IOERROR)
2826 return XFS_ERROR(EIO);
2827
2828 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2829 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2830 return 0;
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002833 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return XFS_ERROR(EIO);
2835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2837 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2838
David Chinner155cc6b2008-03-06 13:44:14 +11002839 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002840 /* update tail before writing to iclog */
2841 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 sync++;
2843 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002844 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2846 /* cycle incremented when incrementing curr_block */
2847 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002848 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /*
2851 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002852 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2854 * this iclog has consistent data, so we ignore IOERROR
2855 * flags after this point.
2856 */
David Chinnerb5893342008-03-06 13:44:06 +11002857 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860} /* xlog_state_release_iclog */
2861
2862
2863/*
2864 * This routine will mark the current iclog in the ring as WANT_SYNC
2865 * and move the current iclog pointer to the next iclog in the ring.
2866 * When this routine is called from xlog_state_get_iclog_space(), the
2867 * exact size of the iclog has not yet been determined. All we know is
2868 * that every data block. We have run out of space in this log record.
2869 */
2870STATIC void
2871xlog_state_switch_iclogs(xlog_t *log,
2872 xlog_in_core_t *iclog,
2873 int eventual_size)
2874{
2875 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2876 if (!eventual_size)
2877 eventual_size = iclog->ic_offset;
2878 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002879 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 log->l_prev_block = log->l_curr_block;
2881 log->l_prev_cycle = log->l_curr_cycle;
2882
2883 /* roll log?: ic_offset changed later */
2884 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2885
2886 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002887 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 log->l_mp->m_sb.sb_logsunit > 1) {
2889 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2890 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2891 }
2892
2893 if (log->l_curr_block >= log->l_logBBsize) {
2894 log->l_curr_cycle++;
2895 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2896 log->l_curr_cycle++;
2897 log->l_curr_block -= log->l_logBBsize;
2898 ASSERT(log->l_curr_block >= 0);
2899 }
2900 ASSERT(iclog == log->l_iclog);
2901 log->l_iclog = iclog->ic_next;
2902} /* xlog_state_switch_iclogs */
2903
2904
2905/*
2906 * Write out all data in the in-core log as of this exact moment in time.
2907 *
2908 * Data may be written to the in-core log during this call. However,
2909 * we don't guarantee this data will be written out. A change from past
2910 * implementation means this routine will *not* write out zero length LRs.
2911 *
2912 * Basically, we try and perform an intelligent scan of the in-core logs.
2913 * If we determine there is no flushable data, we just return. There is no
2914 * flushable data if:
2915 *
2916 * 1. the current iclog is active and has no data; the previous iclog
2917 * is in the active or dirty state.
2918 * 2. the current iclog is drity, and the previous iclog is in the
2919 * active or dirty state.
2920 *
2921 * We may sleep (call psema) if:
2922 *
2923 * 1. the current iclog is not in the active nor dirty state.
2924 * 2. the current iclog dirty, and the previous iclog is not in the
2925 * active nor dirty state.
2926 * 3. the current iclog is active, and there is another thread writing
2927 * to this particular iclog.
2928 * 4. a) the current iclog is active and has no other writers
2929 * b) when we return from flushing out this iclog, it is still
2930 * not in the active nor dirty state.
2931 */
2932STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002933xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 xlog_in_core_t *iclog;
2936 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002938 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 iclog = log->l_iclog;
2941 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002942 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return XFS_ERROR(EIO);
2944 }
2945
2946 /* If the head iclog is not active nor dirty, we just attach
2947 * ourselves to the head and go to sleep.
2948 */
2949 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2950 iclog->ic_state == XLOG_STATE_DIRTY) {
2951 /*
2952 * If the head is dirty or (active and empty), then
2953 * we need to look at the previous iclog. If the previous
2954 * iclog is active or dirty we are done. There is nothing
2955 * to sync out. Otherwise, we attach ourselves to the
2956 * previous iclog and go to sleep.
2957 */
2958 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002959 (atomic_read(&iclog->ic_refcnt) == 0
2960 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 iclog = iclog->ic_prev;
2962 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2963 iclog->ic_state == XLOG_STATE_DIRTY)
2964 goto no_sleep;
2965 else
2966 goto maybe_sleep;
2967 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002968 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 /* We are the only one with access to this
2970 * iclog. Flush it out now. There should
2971 * be a roundoff of zero to show that someone
2972 * has already taken care of the roundoff from
2973 * the previous sync.
2974 */
David Chinner155cc6b2008-03-06 13:44:14 +11002975 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002976 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002978 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 if (xlog_state_release_iclog(log, iclog))
2981 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002982 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002983 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002984 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 iclog->ic_state != XLOG_STATE_DIRTY)
2986 goto maybe_sleep;
2987 else
2988 goto no_sleep;
2989 } else {
2990 /* Someone else is writing to this iclog.
2991 * Use its call to flush out the data. However,
2992 * the other thread may not force out this LR,
2993 * so we mark it WANT_SYNC.
2994 */
2995 xlog_state_switch_iclogs(log, iclog, 0);
2996 goto maybe_sleep;
2997 }
2998 }
2999 }
3000
3001 /* By the time we come around again, the iclog could've been filled
3002 * which would give it another lsn. If we have a new lsn, just
3003 * return because the relevant data has been flushed.
3004 */
3005maybe_sleep:
3006 if (flags & XFS_LOG_SYNC) {
3007 /*
3008 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003009 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 * Then we check again after waking up, in case our
3011 * sleep was disturbed by a bad news.
3012 */
3013 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003014 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return XFS_ERROR(EIO);
3016 }
3017 XFS_STATS_INC(xs_log_force_sleep);
3018 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3019 /*
3020 * No need to grab the log lock here since we're
3021 * only deciding whether or not to return EIO
3022 * and the memory read should be atomic.
3023 */
3024 if (iclog->ic_state & XLOG_STATE_IOERROR)
3025 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003026 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 } else {
3029
3030no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003031 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033 return 0;
3034} /* xlog_state_sync_all */
3035
3036
3037/*
3038 * Used by code which implements synchronous log forces.
3039 *
3040 * Find in-core log with lsn.
3041 * If it is in the DIRTY state, just return.
3042 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3043 * state and go to sleep or return.
3044 * If it is in any other state, go to sleep or return.
3045 *
3046 * If filesystem activity goes to zero, the iclog will get flushed only by
3047 * bdflush().
3048 */
David Chinnera8272ce2007-11-23 16:28:09 +11003049STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050xlog_state_sync(xlog_t *log,
3051 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003052 uint flags,
3053 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 xlog_in_core_t *iclog;
3056 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003059 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 iclog = log->l_iclog;
3061
3062 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003063 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return XFS_ERROR(EIO);
3065 }
3066
3067 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003068 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3069 iclog = iclog->ic_next;
3070 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
3072
3073 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003074 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return 0;
3076 }
3077
3078 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3079 /*
3080 * We sleep here if we haven't already slept (e.g.
3081 * this is the first time we've looked at the correct
3082 * iclog buf) and the buffer before us is going to
3083 * be sync'ed. The reason for this is that if we
3084 * are doing sync transactions here, by waiting for
3085 * the previous I/O to complete, we can allow a few
3086 * more transactions into this iclog before we close
3087 * it down.
3088 *
3089 * Otherwise, we mark the buffer WANT_SYNC, and bump
3090 * up the refcnt so we can release the log (which drops
3091 * the ref count). The state switch keeps new transaction
3092 * commits from using this buffer. When the current commits
3093 * finish writing into the buffer, the refcount will drop to
3094 * zero and the buffer will go out then.
3095 */
3096 if (!already_slept &&
3097 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3098 XLOG_STATE_SYNCING))) {
3099 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3100 XFS_STATS_INC(xs_log_force_sleep);
3101 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3102 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003103 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 already_slept = 1;
3105 goto try_again;
3106 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003107 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003109 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (xlog_state_release_iclog(log, iclog))
3111 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003112 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003113 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
3115 }
3116
3117 if ((flags & XFS_LOG_SYNC) && /* sleep */
3118 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3119
3120 /*
3121 * Don't wait on the forcesema if we know that we've
3122 * gotten a log write error.
3123 */
3124 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003125 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return XFS_ERROR(EIO);
3127 }
3128 XFS_STATS_INC(xs_log_force_sleep);
3129 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3130 /*
3131 * No need to grab the log lock here since we're
3132 * only deciding whether or not to return EIO
3133 * and the memory read should be atomic.
3134 */
3135 if (iclog->ic_state & XLOG_STATE_IOERROR)
3136 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003137 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003139 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
3141 return 0;
3142
3143 } while (iclog != log->l_iclog);
3144
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003145 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147} /* xlog_state_sync */
3148
3149
3150/*
3151 * Called when we want to mark the current iclog as being ready to sync to
3152 * disk.
3153 */
David Chinnera8272ce2007-11-23 16:28:09 +11003154STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3156{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003157 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3160 xlog_state_switch_iclogs(log, iclog, 0);
3161 } else {
3162 ASSERT(iclog->ic_state &
3163 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3164 }
3165
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003166 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167} /* xlog_state_want_sync */
3168
3169
3170
3171/*****************************************************************************
3172 *
3173 * TICKET functions
3174 *
3175 *****************************************************************************
3176 */
3177
3178/*
3179 * Algorithm doesn't take into account page size. ;-(
3180 */
3181STATIC void
3182xlog_state_ticket_alloc(xlog_t *log)
3183{
3184 xlog_ticket_t *t_list;
3185 xlog_ticket_t *next;
3186 xfs_caddr_t buf;
Tim Shimmine6a4b372007-11-23 16:30:42 +11003187 uint i = (PAGE_SIZE / sizeof(xlog_ticket_t)) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 /*
3190 * The kmem_zalloc may sleep, so we shouldn't be holding the
3191 * global lock. XXXmiken: may want to use zone allocator.
3192 */
Tim Shimmine6a4b372007-11-23 16:30:42 +11003193 buf = (xfs_caddr_t) kmem_zalloc(PAGE_SIZE, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003195 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3198 t_list = (xlog_ticket_t *)buf;
3199 t_list->t_next = log->l_unmount_free;
3200 log->l_unmount_free = t_list++;
3201 log->l_ticket_cnt++;
3202 log->l_ticket_tcnt++;
3203
3204 /* Next ticket becomes first ticket attached to ticket free list */
3205 if (log->l_freelist != NULL) {
3206 ASSERT(log->l_tail != NULL);
3207 log->l_tail->t_next = t_list;
3208 } else {
3209 log->l_freelist = t_list;
3210 }
3211 log->l_ticket_cnt++;
3212 log->l_ticket_tcnt++;
3213
3214 /* Cycle through rest of alloc'ed memory, building up free Q */
3215 for ( ; i > 0; i--) {
3216 next = t_list + 1;
3217 t_list->t_next = next;
3218 t_list = next;
3219 log->l_ticket_cnt++;
3220 log->l_ticket_tcnt++;
3221 }
3222 t_list->t_next = NULL;
3223 log->l_tail = t_list;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003224 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225} /* xlog_state_ticket_alloc */
3226
3227
3228/*
3229 * Put ticket into free list
3230 *
3231 * Assumption: log lock is held around this call.
3232 */
3233STATIC void
3234xlog_ticket_put(xlog_t *log,
3235 xlog_ticket_t *ticket)
3236{
3237 sv_destroy(&ticket->t_sema);
3238
3239 /*
3240 * Don't think caching will make that much difference. It's
3241 * more important to make debug easier.
3242 */
3243#if 0
3244 /* real code will want to use LIFO for caching */
3245 ticket->t_next = log->l_freelist;
3246 log->l_freelist = ticket;
3247 /* no need to clear fields */
3248#else
3249 /* When we debug, it is easier if tickets are cycled */
3250 ticket->t_next = NULL;
Christoph Hellwig4b809162007-08-16 15:37:36 +10003251 if (log->l_tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 log->l_tail->t_next = ticket;
3253 } else {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003254 ASSERT(log->l_freelist == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 log->l_freelist = ticket;
3256 }
3257 log->l_tail = ticket;
3258#endif /* DEBUG */
3259 log->l_ticket_cnt++;
3260} /* xlog_ticket_put */
3261
3262
3263/*
3264 * Grab ticket off freelist or allocation some more
3265 */
David Chinnera8272ce2007-11-23 16:28:09 +11003266STATIC xlog_ticket_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267xlog_ticket_get(xlog_t *log,
3268 int unit_bytes,
3269 int cnt,
3270 char client,
3271 uint xflags)
3272{
3273 xlog_ticket_t *tic;
3274 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 alloc:
3277 if (log->l_freelist == NULL)
3278 xlog_state_ticket_alloc(log); /* potentially sleep */
3279
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003280 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (log->l_freelist == NULL) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003282 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 goto alloc;
3284 }
3285 tic = log->l_freelist;
3286 log->l_freelist = tic->t_next;
3287 if (log->l_freelist == NULL)
3288 log->l_tail = NULL;
3289 log->l_ticket_cnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003290 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 /*
3293 * Permanent reservations have up to 'cnt'-1 active log operations
3294 * in the log. A unit in this case is the amount of space for one
3295 * of these log operations. Normal reservations have a cnt of 1
3296 * and their unit amount is the total amount of space required.
3297 *
3298 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003299 * which occupy space in the on-disk log.
3300 *
3301 * Normal form of a transaction is:
3302 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3303 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3304 *
3305 * We need to account for all the leadup data and trailer data
3306 * around the transaction data.
3307 * And then we need to account for the worst case in terms of using
3308 * more space.
3309 * The worst case will happen if:
3310 * - the placement of the transaction happens to be such that the
3311 * roundoff is at its maximum
3312 * - the transaction data is synced before the commit record is synced
3313 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3314 * Therefore the commit record is in its own Log Record.
3315 * This can happen as the commit record is called with its
3316 * own region to xlog_write().
3317 * This then means that in the worst case, roundoff can happen for
3318 * the commit-rec as well.
3319 * The commit-rec is smaller than padding in this scenario and so it is
3320 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 */
3322
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003323 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003325 unit_bytes += sizeof(xfs_trans_header_t);
3326
3327 /* for start-rec */
3328 unit_bytes += sizeof(xlog_op_header_t);
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 /* for LR headers */
3331 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3332 unit_bytes += log->l_iclog_hsize * num_headers;
3333
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003334 /* for commit-rec LR header - note: padding will subsume the ophdr */
3335 unit_bytes += log->l_iclog_hsize;
3336
3337 /* for split-recs - ophdrs added when data split over LRs */
3338 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3339
3340 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003341 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003342 log->l_mp->m_sb.sb_logsunit > 1) {
3343 /* log su roundoff */
3344 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3345 } else {
3346 /* BB roundoff */
3347 unit_bytes += 2*BBSIZE;
3348 }
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 tic->t_unit_res = unit_bytes;
3351 tic->t_curr_res = unit_bytes;
3352 tic->t_cnt = cnt;
3353 tic->t_ocnt = cnt;
3354 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3355 tic->t_clientid = client;
3356 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003357 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (xflags & XFS_LOG_PERM_RESERV)
3359 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3360 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3361
Christoph Hellwig0adba532007-08-30 17:21:46 +10003362 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return tic;
3365} /* xlog_ticket_get */
3366
3367
3368/******************************************************************************
3369 *
3370 * Log debug routines
3371 *
3372 ******************************************************************************
3373 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003374#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375/*
3376 * Make sure that the destination ptr is within the valid data region of
3377 * one of the iclogs. This uses backup pointers stored in a different
3378 * part of the log in case we trash the log structure.
3379 */
3380void
3381xlog_verify_dest_ptr(xlog_t *log,
3382 __psint_t ptr)
3383{
3384 int i;
3385 int good_ptr = 0;
3386
3387 for (i=0; i < log->l_iclog_bufs; i++) {
3388 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3389 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3390 good_ptr++;
3391 }
3392 if (! good_ptr)
3393 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3394} /* xlog_verify_dest_ptr */
3395
3396STATIC void
3397xlog_verify_grant_head(xlog_t *log, int equals)
3398{
3399 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3400 if (equals)
3401 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3402 else
3403 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3404 } else {
3405 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3406 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3407 }
3408} /* xlog_verify_grant_head */
3409
3410/* check if it will fit */
3411STATIC void
3412xlog_verify_tail_lsn(xlog_t *log,
3413 xlog_in_core_t *iclog,
3414 xfs_lsn_t tail_lsn)
3415{
3416 int blocks;
3417
3418 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3419 blocks =
3420 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3421 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3422 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3423 } else {
3424 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3425
3426 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3427 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3428
3429 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3430 if (blocks < BTOBB(iclog->ic_offset) + 1)
3431 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3432 }
3433} /* xlog_verify_tail_lsn */
3434
3435/*
3436 * Perform a number of checks on the iclog before writing to disk.
3437 *
3438 * 1. Make sure the iclogs are still circular
3439 * 2. Make sure we have a good magic number
3440 * 3. Make sure we don't have magic numbers in the data
3441 * 4. Check fields of each log operation header for:
3442 * A. Valid client identifier
3443 * B. tid ptr value falls in valid ptr space (user space code)
3444 * C. Length in log record header is correct according to the
3445 * individual operation headers within record.
3446 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3447 * log, check the preceding blocks of the physical log to make sure all
3448 * the cycle numbers agree with the current cycle number.
3449 */
3450STATIC void
3451xlog_verify_iclog(xlog_t *log,
3452 xlog_in_core_t *iclog,
3453 int count,
3454 boolean_t syncing)
3455{
3456 xlog_op_header_t *ophead;
3457 xlog_in_core_t *icptr;
3458 xlog_in_core_2_t *xhdr;
3459 xfs_caddr_t ptr;
3460 xfs_caddr_t base_ptr;
3461 __psint_t field_offset;
3462 __uint8_t clientid;
3463 int len, i, j, k, op_len;
3464 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003467 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 icptr = log->l_iclog;
3469 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003470 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 xlog_panic("xlog_verify_iclog: invalid ptr");
3472 icptr = icptr->ic_next;
3473 }
3474 if (icptr != log->l_iclog)
3475 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003476 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003479 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 xlog_panic("xlog_verify_iclog: invalid magic num");
3481
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003482 ptr = (xfs_caddr_t) &iclog->ic_header;
3483 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003485 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 xlog_panic("xlog_verify_iclog: unexpected magic num");
3487 }
3488
3489 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003490 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 ptr = iclog->ic_datap;
3492 base_ptr = ptr;
3493 ophead = (xlog_op_header_t *)ptr;
3494 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3495 for (i = 0; i < len; i++) {
3496 ophead = (xlog_op_header_t *)ptr;
3497
3498 /* clientid is only 1 byte */
3499 field_offset = (__psint_t)
3500 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3501 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3502 clientid = ophead->oh_clientid;
3503 } else {
3504 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3505 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3506 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3507 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003508 clientid = xlog_get_client_id(
3509 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003511 clientid = xlog_get_client_id(
3512 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 }
3514 }
3515 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003516 cmn_err(CE_WARN, "xlog_verify_iclog: "
3517 "invalid clientid %d op 0x%p offset 0x%lx",
3518 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520 /* check length */
3521 field_offset = (__psint_t)
3522 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3523 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003524 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 } else {
3526 idx = BTOBBT((__psint_t)&ophead->oh_len -
3527 (__psint_t)iclog->ic_datap);
3528 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3529 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3530 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003531 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003533 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 }
3535 }
3536 ptr += sizeof(xlog_op_header_t) + op_len;
3537 }
3538} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003542 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 */
3544STATIC int
3545xlog_state_ioerror(
3546 xlog_t *log)
3547{
3548 xlog_in_core_t *iclog, *ic;
3549
3550 iclog = log->l_iclog;
3551 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3552 /*
3553 * Mark all the incore logs IOERROR.
3554 * From now on, no log flushes will result.
3555 */
3556 ic = iclog;
3557 do {
3558 ic->ic_state = XLOG_STATE_IOERROR;
3559 ic = ic->ic_next;
3560 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 }
3563 /*
3564 * Return non-zero, if state transition has already happened.
3565 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003566 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
3569/*
3570 * This is called from xfs_force_shutdown, when we're forcibly
3571 * shutting down the filesystem, typically because of an IO error.
3572 * Our main objectives here are to make sure that:
3573 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3574 * parties to find out, 'atomically'.
3575 * b. those who're sleeping on log reservations, pinned objects and
3576 * other resources get woken up, and be told the bad news.
3577 * c. nothing new gets queued up after (a) and (b) are done.
3578 * d. if !logerror, flush the iclogs to disk, then seal them off
3579 * for business.
3580 */
3581int
3582xfs_log_force_umount(
3583 struct xfs_mount *mp,
3584 int logerror)
3585{
3586 xlog_ticket_t *tic;
3587 xlog_t *log;
3588 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003589 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 log = mp->m_log;
3592
3593 /*
3594 * If this happens during log recovery, don't worry about
3595 * locking; the log isn't open for business yet.
3596 */
3597 if (!log ||
3598 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3599 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3600 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 }
3603
3604 /*
3605 * Somebody could've already done the hard work for us.
3606 * No need to get locks for this.
3607 */
3608 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3609 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003610 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
3612 retval = 0;
3613 /*
3614 * We must hold both the GRANT lock and the LOG lock,
3615 * before we mark the filesystem SHUTDOWN and wake
3616 * everybody up to tell the bad news.
3617 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003618 spin_lock(&log->l_grant_lock);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003619 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3621 XFS_BUF_DONE(mp->m_sb_bp);
3622 /*
3623 * This flag is sort of redundant because of the mount flag, but
3624 * it's good to maintain the separation between the log and the rest
3625 * of XFS.
3626 */
3627 log->l_flags |= XLOG_IO_ERROR;
3628
3629 /*
3630 * If we hit a log error, we want to mark all the iclogs IOERROR
3631 * while we're still holding the loglock.
3632 */
3633 if (logerror)
3634 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003635 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 /*
3638 * We don't want anybody waiting for log reservations
3639 * after this. That means we have to wake up everybody
3640 * queued up on reserve_headq as well as write_headq.
3641 * In addition, we make sure in xlog_{re}grant_log_space
3642 * that we don't enqueue anything once the SHUTDOWN flag
3643 * is set, and this action is protected by the GRANTLOCK.
3644 */
3645 if ((tic = log->l_reserve_headq)) {
3646 do {
3647 sv_signal(&tic->t_sema);
3648 tic = tic->t_next;
3649 } while (tic != log->l_reserve_headq);
3650 }
3651
3652 if ((tic = log->l_write_headq)) {
3653 do {
3654 sv_signal(&tic->t_sema);
3655 tic = tic->t_next;
3656 } while (tic != log->l_write_headq);
3657 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003658 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
3660 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3661 ASSERT(!logerror);
3662 /*
3663 * Force the incore logs to disk before shutting the
3664 * log down completely.
3665 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003666 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003667 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003669 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
3671 /*
3672 * Wake up everybody waiting on xfs_log_force.
3673 * Callback all log item committed functions as if the
3674 * log writes were completed.
3675 */
3676 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3677
3678#ifdef XFSERRORDEBUG
3679 {
3680 xlog_in_core_t *iclog;
3681
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003682 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 iclog = log->l_iclog;
3684 do {
3685 ASSERT(iclog->ic_callback == 0);
3686 iclog = iclog->ic_next;
3687 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003688 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 }
3690#endif
3691 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003692 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003695STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696xlog_iclogs_empty(xlog_t *log)
3697{
3698 xlog_in_core_t *iclog;
3699
3700 iclog = log->l_iclog;
3701 do {
3702 /* endianness does not matter here, zero is zero in
3703 * any language.
3704 */
3705 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 iclog = iclog->ic_next;
3708 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003709 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}