blob: f1f1f36876e4178895ce98663ad4675639f52e6e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44
45#define xlog_write_adv_cnt(ptr, len, off, bytes) \
46 { (ptr) += (bytes); \
47 (len) -= (bytes); \
48 (off) += (bytes);}
49
50/* Local miscellaneous function prototypes */
51STATIC int xlog_bdstrat_cb(struct xfs_buf *);
52STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
53 xlog_in_core_t **, xfs_lsn_t *);
54STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
55 xfs_buftarg_t *log_target,
56 xfs_daddr_t blk_offset,
57 int num_bblks);
58STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
59STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100060STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
62 int nentries, xfs_log_ticket_t tic,
63 xfs_lsn_t *start_lsn,
64 xlog_in_core_t **commit_iclog,
65 uint flags);
66
67/* local state machine functions */
68STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
69STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
70STATIC int xlog_state_get_iclog_space(xlog_t *log,
71 int len,
72 xlog_in_core_t **iclog,
73 xlog_ticket_t *ticket,
74 int *continued_write,
75 int *logoffsetp);
76STATIC void xlog_state_put_ticket(xlog_t *log,
77 xlog_ticket_t *tic);
78STATIC int xlog_state_release_iclog(xlog_t *log,
79 xlog_in_core_t *iclog);
80STATIC void xlog_state_switch_iclogs(xlog_t *log,
81 xlog_in_core_t *iclog,
82 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110083STATIC int xlog_state_sync(xlog_t *log,
84 xfs_lsn_t lsn,
85 uint flags,
86 int *log_flushed);
87STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
89
90/* local functions to manipulate grant head */
91STATIC int xlog_grant_log_space(xlog_t *log,
92 xlog_ticket_t *xtic);
93STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
94 int need_bytes);
95STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
96 xlog_ticket_t *ticket);
97STATIC int xlog_regrant_write_log_space(xlog_t *log,
98 xlog_ticket_t *ticket);
99STATIC void xlog_ungrant_log_space(xlog_t *log,
100 xlog_ticket_t *ticket);
101
102
103/* local ticket functions */
104STATIC void xlog_state_ticket_alloc(xlog_t *log);
105STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
106 int unit_bytes,
107 int count,
108 char clientid,
109 uint flags);
110STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
111
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100112#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
114STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
115STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
116 int count, boolean_t syncing);
117STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
118 xfs_lsn_t tail_lsn);
119#else
120#define xlog_verify_dest_ptr(a,b)
121#define xlog_verify_grant_head(a,b)
122#define xlog_verify_iclog(a,b,c,d)
123#define xlog_verify_tail_lsn(a,b,c)
124#endif
125
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000126STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#if defined(XFS_LOG_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void
130xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
131{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000132 unsigned long cnts;
133
134 if (!log->l_grant_trace) {
135 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
136 if (!log->l_grant_trace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
138 }
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000139 /* ticket counts are 1 byte each */
140 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 ktrace_enter(log->l_grant_trace,
143 (void *)tic,
144 (void *)log->l_reserve_headq,
145 (void *)log->l_write_headq,
146 (void *)((unsigned long)log->l_grant_reserve_cycle),
147 (void *)((unsigned long)log->l_grant_reserve_bytes),
148 (void *)((unsigned long)log->l_grant_write_cycle),
149 (void *)((unsigned long)log->l_grant_write_bytes),
150 (void *)((unsigned long)log->l_curr_cycle),
151 (void *)((unsigned long)log->l_curr_block),
152 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
153 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
154 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000155 (void *)((unsigned long)tic->t_trans_type),
156 (void *)cnts,
157 (void *)((unsigned long)tic->t_curr_res),
158 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void
162xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!iclog->ic_trace)
165 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
166 ktrace_enter(iclog->ic_trace,
167 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100168 (void *)((unsigned long)current_pid()),
169 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
170 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
171 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
172 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#else
175#define xlog_trace_loggrant(log,tic,string)
176#define xlog_trace_iclog(iclog,state)
177#endif /* XFS_LOG_TRACE */
178
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100179
180static void
181xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
182{
183 if (*qp) {
184 tic->t_next = (*qp);
185 tic->t_prev = (*qp)->t_prev;
186 (*qp)->t_prev->t_next = tic;
187 (*qp)->t_prev = tic;
188 } else {
189 tic->t_prev = tic->t_next = tic;
190 *qp = tic;
191 }
192
193 tic->t_flags |= XLOG_TIC_IN_Q;
194}
195
196static void
197xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
198{
199 if (tic == tic->t_next) {
200 *qp = NULL;
201 } else {
202 *qp = tic->t_next;
203 tic->t_next->t_prev = tic->t_prev;
204 tic->t_prev->t_next = tic->t_next;
205 }
206
207 tic->t_next = tic->t_prev = NULL;
208 tic->t_flags &= ~XLOG_TIC_IN_Q;
209}
210
211static void
212xlog_grant_sub_space(struct log *log, int bytes)
213{
214 log->l_grant_write_bytes -= bytes;
215 if (log->l_grant_write_bytes < 0) {
216 log->l_grant_write_bytes += log->l_logsize;
217 log->l_grant_write_cycle--;
218 }
219
220 log->l_grant_reserve_bytes -= bytes;
221 if ((log)->l_grant_reserve_bytes < 0) {
222 log->l_grant_reserve_bytes += log->l_logsize;
223 log->l_grant_reserve_cycle--;
224 }
225
226}
227
228static void
229xlog_grant_add_space_write(struct log *log, int bytes)
230{
231 log->l_grant_write_bytes += bytes;
232 if (log->l_grant_write_bytes > log->l_logsize) {
233 log->l_grant_write_bytes -= log->l_logsize;
234 log->l_grant_write_cycle++;
235 }
236}
237
238static void
239xlog_grant_add_space_reserve(struct log *log, int bytes)
240{
241 log->l_grant_reserve_bytes += bytes;
242 if (log->l_grant_reserve_bytes > log->l_logsize) {
243 log->l_grant_reserve_bytes -= log->l_logsize;
244 log->l_grant_reserve_cycle++;
245 }
246}
247
248static inline void
249xlog_grant_add_space(struct log *log, int bytes)
250{
251 xlog_grant_add_space_write(log, bytes);
252 xlog_grant_add_space_reserve(log, bytes);
253}
254
Christoph Hellwig0adba532007-08-30 17:21:46 +1000255static void
256xlog_tic_reset_res(xlog_ticket_t *tic)
257{
258 tic->t_res_num = 0;
259 tic->t_res_arr_sum = 0;
260 tic->t_res_num_ophdrs = 0;
261}
262
263static void
264xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
265{
266 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
267 /* add to overflow and start again */
268 tic->t_res_o_flow += tic->t_res_arr_sum;
269 tic->t_res_num = 0;
270 tic->t_res_arr_sum = 0;
271 }
272
273 tic->t_res_arr[tic->t_res_num].r_len = len;
274 tic->t_res_arr[tic->t_res_num].r_type = type;
275 tic->t_res_arr_sum += len;
276 tic->t_res_num++;
277}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * NOTES:
281 *
282 * 1. currblock field gets updated at startup and after in-core logs
283 * marked as with WANT_SYNC.
284 */
285
286/*
287 * This routine is called when a user of a log manager ticket is done with
288 * the reservation. If the ticket was ever used, then a commit record for
289 * the associated transaction is written out as a log operation header with
290 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
291 * a given ticket. If the ticket was one with a permanent reservation, then
292 * a few operations are done differently. Permanent reservation tickets by
293 * default don't release the reservation. They just commit the current
294 * transaction with the belief that the reservation is still needed. A flag
295 * must be passed in before permanent reservations are actually released.
296 * When these type of tickets are not released, they need to be set into
297 * the inited state again. By doing this, a start record will be written
298 * out when the next write occurs.
299 */
300xfs_lsn_t
301xfs_log_done(xfs_mount_t *mp,
302 xfs_log_ticket_t xtic,
303 void **iclog,
304 uint flags)
305{
306 xlog_t *log = mp->m_log;
307 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
308 xfs_lsn_t lsn = 0;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (XLOG_FORCED_SHUTDOWN(log) ||
311 /*
312 * If nothing was ever written, don't write out commit record.
313 * If we get an error, just continue and give back the log ticket.
314 */
315 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
316 (xlog_commit_record(mp, ticket,
317 (xlog_in_core_t **)iclog, &lsn)))) {
318 lsn = (xfs_lsn_t) -1;
319 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
320 flags |= XFS_LOG_REL_PERM_RESERV;
321 }
322 }
323
324
325 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
326 (flags & XFS_LOG_REL_PERM_RESERV)) {
327 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000328 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * request has been made to release a permanent reservation.
330 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000331 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xlog_ungrant_log_space(log, ticket);
333 xlog_state_put_ticket(log, ticket);
334 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000335 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 xlog_regrant_reserve_log_space(log, ticket);
337 }
338
339 /* If this ticket was a permanent reservation and we aren't
340 * trying to release it, reset the inited flags; so next time
341 * we write, a start record will be written out.
342 */
343 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
344 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
345 ticket->t_flags |= XLOG_TIC_INITED;
346
347 return lsn;
348} /* xfs_log_done */
349
350
351/*
352 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
353 * the force is done synchronously.
354 *
355 * Asynchronous forces are implemented by setting the WANT_SYNC
356 * bit in the appropriate in-core log and then returning.
357 *
358 * Synchronous forces are implemented with a semaphore. All callers
359 * to force a given lsn to disk will wait on a semaphore attached to the
360 * specific in-core log. When given in-core log finally completes its
361 * write to disk, that thread will wake up all threads waiting on the
362 * semaphore.
363 */
364int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100365_xfs_log_force(
366 xfs_mount_t *mp,
367 xfs_lsn_t lsn,
368 uint flags,
369 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100371 xlog_t *log = mp->m_log;
372 int dummy;
373
374 if (!log_flushed)
375 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ASSERT(flags & XFS_LOG_FORCE);
378
379 XFS_STATS_INC(xs_log_force);
380
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100381 if (log->l_flags & XLOG_IO_ERROR)
382 return XFS_ERROR(EIO);
383 if (lsn == 0)
384 return xlog_state_sync_all(log, flags, log_flushed);
385 else
386 return xlog_state_sync(log, lsn, flags, log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387} /* xfs_log_force */
388
389/*
390 * Attaches a new iclog I/O completion callback routine during
391 * transaction commit. If the log is in error state, a non-zero
392 * return code is handed back and the caller is responsible for
393 * executing the callback at an appropriate time.
394 */
395int
396xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
397 void *iclog_hndl, /* iclog to hang callback off */
398 xfs_log_callback_t *cb)
399{
400 xlog_t *log = mp->m_log;
401 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000402 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cb->cb_next = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000405 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
407 if (!abortflg) {
408 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
409 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
410 cb->cb_next = NULL;
411 *(iclog->ic_callback_tail) = cb;
412 iclog->ic_callback_tail = &(cb->cb_next);
413 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000414 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return abortflg;
416} /* xfs_log_notify */
417
418int
419xfs_log_release_iclog(xfs_mount_t *mp,
420 void *iclog_hndl)
421{
422 xlog_t *log = mp->m_log;
423 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
424
425 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000426 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100427 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 return 0;
431}
432
433/*
434 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
435 * to the reservation.
436 * 2. Potentially, push buffers at tail of log to disk.
437 *
438 * Each reservation is going to reserve extra space for a log record header.
439 * When writes happen to the on-disk log, we don't subtract the length of the
440 * log record header from any reservation. By wasting space in each
441 * reservation, we prevent over allocation problems.
442 */
443int
444xfs_log_reserve(xfs_mount_t *mp,
445 int unit_bytes,
446 int cnt,
447 xfs_log_ticket_t *ticket,
448 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000449 uint flags,
450 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 xlog_t *log = mp->m_log;
453 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100454 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
457 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
458
459 if (XLOG_FORCED_SHUTDOWN(log))
460 return XFS_ERROR(EIO);
461
462 XFS_STATS_INC(xs_try_logspace);
463
464 if (*ticket != NULL) {
465 ASSERT(flags & XFS_LOG_PERM_RESERV);
466 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000467 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
469 retval = xlog_regrant_write_log_space(log, internal_ticket);
470 } else {
471 /* may sleep if need to allocate more tickets */
472 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
473 client, flags);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000474 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000476 xlog_trace_loggrant(log, internal_ticket,
477 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
478 "xfs_log_reserve: create new ticket (permanent trans)" :
479 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 xlog_grant_push_ail(mp,
481 (internal_ticket->t_unit_res *
482 internal_ticket->t_cnt));
483 retval = xlog_grant_log_space(log, internal_ticket);
484 }
485
486 return retval;
487} /* xfs_log_reserve */
488
489
490/*
491 * Mount a log filesystem
492 *
493 * mp - ubiquitous xfs mount point structure
494 * log_target - buftarg of on-disk log device
495 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
496 * num_bblocks - Number of BBSIZE blocks in on-disk log
497 *
498 * Return error or zero.
499 */
500int
501xfs_log_mount(xfs_mount_t *mp,
502 xfs_buftarg_t *log_target,
503 xfs_daddr_t blk_offset,
504 int num_bblks)
505{
506 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
507 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
508 else {
509 cmn_err(CE_NOTE,
510 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
511 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000512 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * skip log recovery on a norecovery mount. pretend it all
519 * just worked.
520 */
521 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000522 int error, readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000525 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Eric Sandeen65be6052006-01-11 15:34:19 +1100527 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000530 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (error) {
532 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Nathan Scottc41564b2006-03-29 08:55:14 +1000533 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return error;
535 }
536 }
537
538 /* Normal transactions can now occur */
539 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
540
541 /* End mounting message in xfs_log_mount_finish */
542 return 0;
543} /* xfs_log_mount */
544
545/*
546 * Finish the recovery of the file system. This is separate from
547 * the xfs_log_mount() call, because it depends on the code in
548 * xfs_mountfs() to read in the root and real-time bitmap inodes
549 * between calling xfs_log_mount() and here.
550 *
551 * mp - ubiquitous xfs mount point structure
552 */
553int
554xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
555{
556 int error;
557
558 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
559 error = xlog_recover_finish(mp->m_log, mfsi_flags);
560 else {
561 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000562 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 return error;
566}
567
568/*
569 * Unmount processing for the log.
570 */
571int
572xfs_log_unmount(xfs_mount_t *mp)
573{
574 int error;
575
576 error = xfs_log_unmount_write(mp);
577 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
582 * Final log writes as part of unmount.
583 *
584 * Mark the filesystem clean as unmount happens. Note that during relocation
585 * this routine needs to be executed as part of source-bag while the
586 * deallocation must not be done until source-end.
587 */
588
589/*
590 * Unmount record used to have a string "Unmount filesystem--" in the
591 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
592 * We just write the magic number now since that particular field isn't
593 * currently architecture converted and "nUmount" is a bit foo.
594 * As far as I know, there weren't any dependencies on the old behaviour.
595 */
596
597int
598xfs_log_unmount_write(xfs_mount_t *mp)
599{
600 xlog_t *log = mp->m_log;
601 xlog_in_core_t *iclog;
602#ifdef DEBUG
603 xlog_in_core_t *first_iclog;
604#endif
605 xfs_log_iovec_t reg[1];
606 xfs_log_ticket_t tic = NULL;
607 xfs_lsn_t lsn;
608 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* the data section must be 32 bit size aligned */
611 struct {
612 __uint16_t magic;
613 __uint16_t pad1;
614 __uint32_t pad2; /* may as well make it 64 bits */
615 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /*
618 * Don't write out unmount record on read-only mounts.
619 * Or, if we are doing a forced umount (typically because of IO errors).
620 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000621 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623
624 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
625
626#ifdef DEBUG
627 first_iclog = iclog = log->l_iclog;
628 do {
629 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
630 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
631 ASSERT(iclog->ic_offset == 0);
632 }
633 iclog = iclog->ic_next;
634 } while (iclog != first_iclog);
635#endif
636 if (! (XLOG_FORCED_SHUTDOWN(log))) {
637 reg[0].i_addr = (void*)&magic;
638 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000639 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Tim Shimmin955e47a2006-09-28 11:04:16 +1000641 error = xfs_log_reserve(mp, 600, 1, &tic,
642 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!error) {
644 /* remove inited flag */
645 ((xlog_ticket_t *)tic)->t_flags = 0;
646 error = xlog_write(mp, reg, 1, tic, &lsn,
647 NULL, XLOG_UNMOUNT_TRANS);
648 /*
649 * At this point, we're umounting anyway,
650 * so there's no point in transitioning log state
651 * to IOERROR. Just continue...
652 */
653 }
654
655 if (error) {
656 xfs_fs_cmn_err(CE_ALERT, mp,
657 "xfs_log_unmount: unmount record failed");
658 }
659
660
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000661 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 iclog = log->l_iclog;
663 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000664 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xlog_state_want_sync(log, iclog);
666 (void) xlog_state_release_iclog(log, iclog);
667
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000668 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
670 iclog->ic_state == XLOG_STATE_DIRTY)) {
671 if (!XLOG_FORCED_SHUTDOWN(log)) {
672 sv_wait(&iclog->ic_forcesema, PMEM,
673 &log->l_icloglock, s);
674 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000675 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000678 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000680 if (tic) {
681 xlog_trace_loggrant(log, tic, "unmount rec");
682 xlog_ungrant_log_space(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xlog_state_put_ticket(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 } else {
686 /*
687 * We're already in forced_shutdown mode, couldn't
688 * even attempt to write out the unmount transaction.
689 *
690 * Go through the motions of sync'ing and releasing
691 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000692 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * be in progress. Do this as a separate section of
694 * code so we'll know if we ever get stuck here that
695 * we're in this odd situation of trying to unmount
696 * a file system that went into forced_shutdown as
697 * the result of an unmount..
698 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000699 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 iclog = log->l_iclog;
701 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000702 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 xlog_state_want_sync(log, iclog);
705 (void) xlog_state_release_iclog(log, iclog);
706
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000707 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
710 || iclog->ic_state == XLOG_STATE_DIRTY
711 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
712
713 sv_wait(&iclog->ic_forcesema, PMEM,
714 &log->l_icloglock, s);
715 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000716 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 }
719
720 return 0;
721} /* xfs_log_unmount_write */
722
723/*
724 * Deallocate log structures for unmount/relocation.
725 */
726void
727xfs_log_unmount_dealloc(xfs_mount_t *mp)
728{
Nathan Scottc41564b2006-03-29 08:55:14 +1000729 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/*
733 * Write region vectors to log. The write happens using the space reservation
734 * of the ticket (tic). It is not a requirement that all writes for a given
735 * transaction occur with one call to xfs_log_write().
736 */
737int
738xfs_log_write(xfs_mount_t * mp,
739 xfs_log_iovec_t reg[],
740 int nentries,
741 xfs_log_ticket_t tic,
742 xfs_lsn_t *start_lsn)
743{
744 int error;
745 xlog_t *log = mp->m_log;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (XLOG_FORCED_SHUTDOWN(log))
748 return XFS_ERROR(EIO);
749
750 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000751 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754} /* xfs_log_write */
755
756
757void
758xfs_log_move_tail(xfs_mount_t *mp,
759 xfs_lsn_t tail_lsn)
760{
761 xlog_ticket_t *tic;
762 xlog_t *log = mp->m_log;
763 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (XLOG_FORCED_SHUTDOWN(log))
766 return;
767 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
768
769 if (tail_lsn == 0) {
770 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000771 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000773 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000776 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
779 * tail_lsn.
780 */
781 if (tail_lsn != 1) {
782 log->l_tail_lsn = tail_lsn;
783 }
784
785 if ((tic = log->l_write_headq)) {
786#ifdef DEBUG
787 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
788 panic("Recovery problem");
789#endif
790 cycle = log->l_grant_write_cycle;
791 bytes = log->l_grant_write_bytes;
792 free_bytes = xlog_space_left(log, cycle, bytes);
793 do {
794 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
795
796 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
797 break;
798 tail_lsn = 0;
799 free_bytes -= tic->t_unit_res;
800 sv_signal(&tic->t_sema);
801 tic = tic->t_next;
802 } while (tic != log->l_write_headq);
803 }
804 if ((tic = log->l_reserve_headq)) {
805#ifdef DEBUG
806 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
807 panic("Recovery problem");
808#endif
809 cycle = log->l_grant_reserve_cycle;
810 bytes = log->l_grant_reserve_bytes;
811 free_bytes = xlog_space_left(log, cycle, bytes);
812 do {
813 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
814 need_bytes = tic->t_unit_res*tic->t_cnt;
815 else
816 need_bytes = tic->t_unit_res;
817 if (free_bytes < need_bytes && tail_lsn != 1)
818 break;
819 tail_lsn = 0;
820 free_bytes -= need_bytes;
821 sv_signal(&tic->t_sema);
822 tic = tic->t_next;
823 } while (tic != log->l_reserve_headq);
824 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000825 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826} /* xfs_log_move_tail */
827
828/*
829 * Determine if we have a transaction that has gone to disk
830 * that needs to be covered. Log activity needs to be idle (no AIL and
831 * nothing in the iclogs). And, we need to be in the right state indicating
832 * something has gone out.
833 */
834int
835xfs_log_need_covered(xfs_mount_t *mp)
836{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int needed = 0, gen;
838 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
David Chinner92821e22007-05-24 15:26:31 +1000840 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000843 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
845 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
846 && !xfs_trans_first_ail(mp, &gen)
847 && xlog_iclogs_empty(log)) {
848 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
849 log->l_covered_state = XLOG_STATE_COVER_DONE;
850 else {
851 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
852 log->l_covered_state = XLOG_STATE_COVER_DONE2;
853 }
854 needed = 1;
855 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000856 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100857 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/******************************************************************************
861 *
862 * local routines
863 *
864 ******************************************************************************
865 */
866
867/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
868 * The log manager must keep track of the last LR which was committed
869 * to disk. The lsn of this LR will become the new tail_lsn whenever
870 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
871 * the situation where stuff could be written into the log but nothing
872 * was ever in the AIL when asked. Eventually, we panic since the
873 * tail hits the head.
874 *
875 * We may be holding the log iclog lock upon entering this routine.
876 */
877xfs_lsn_t
878xlog_assign_tail_lsn(xfs_mount_t *mp)
879{
880 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 xlog_t *log = mp->m_log;
882
883 tail_lsn = xfs_trans_tail_ail(mp);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000884 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (tail_lsn != 0) {
886 log->l_tail_lsn = tail_lsn;
887 } else {
888 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
889 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000890 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return tail_lsn;
893} /* xlog_assign_tail_lsn */
894
895
896/*
897 * Return the space in the log between the tail and the head. The head
898 * is passed in the cycle/bytes formal parms. In the special case where
899 * the reserve head has wrapped passed the tail, this calculation is no
900 * longer valid. In this case, just return 0 which means there is no space
901 * in the log. This works for all places where this function is called
902 * with the reserve head. Of course, if the write head were to ever
903 * wrap the tail, we should blow up. Rather than catch this case here,
904 * we depend on other ASSERTions in other parts of the code. XXXmiken
905 *
906 * This code also handles the case where the reservation head is behind
907 * the tail. The details of this case are described below, but the end
908 * result is that we return the size of the log as the amount of space left.
909 */
910int
911xlog_space_left(xlog_t *log, int cycle, int bytes)
912{
913 int free_bytes;
914 int tail_bytes;
915 int tail_cycle;
916
917 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
918 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
919 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
920 free_bytes = log->l_logsize - (bytes - tail_bytes);
921 } else if ((tail_cycle + 1) < cycle) {
922 return 0;
923 } else if (tail_cycle < cycle) {
924 ASSERT(tail_cycle == (cycle - 1));
925 free_bytes = tail_bytes - bytes;
926 } else {
927 /*
928 * The reservation head is behind the tail.
929 * In this case we just want to return the size of the
930 * log as the amount of space left.
931 */
932 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
933 "xlog_space_left: head behind tail\n"
934 " tail_cycle = %d, tail_bytes = %d\n"
935 " GH cycle = %d, GH bytes = %d",
936 tail_cycle, tail_bytes, cycle, bytes);
937 ASSERT(0);
938 free_bytes = log->l_logsize;
939 }
940 return free_bytes;
941} /* xlog_space_left */
942
943
944/*
945 * Log function which is called when an io completes.
946 *
947 * The log manager needs its own routine, in order to control what
948 * happens with the buffer after the write completes.
949 */
950void
951xlog_iodone(xfs_buf_t *bp)
952{
953 xlog_in_core_t *iclog;
954 xlog_t *l;
955 int aborted;
956
957 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
958 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
959 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
960 aborted = 0;
961
962 /*
963 * Some versions of cpp barf on the recursive definition of
964 * ic_log -> hic_fields.ic_log and expand ic_log twice when
965 * it is passed through two macros. Workaround broken cpp.
966 */
967 l = iclog->ic_log;
968
969 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000970 * If the ordered flag has been removed by a lower
971 * layer, it means the underlyin device no longer supports
972 * barrier I/O. Warn loudly and turn off barriers.
973 */
974 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
975 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
976 xfs_fs_cmn_err(CE_WARN, l->l_mp,
977 "xlog_iodone: Barriers are no longer supported"
978 " by device. Disabling barriers\n");
979 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
980 }
981
982 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * Race to shutdown the filesystem if we see an error.
984 */
985 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
986 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
987 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
988 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000989 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
991 * This flag will be propagated to the trans-committed
992 * callback routines to let them know that the log-commit
993 * didn't succeed.
994 */
995 aborted = XFS_LI_ABORTED;
996 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
997 aborted = XFS_LI_ABORTED;
998 }
David Chinner3db296f2007-05-14 18:24:16 +1000999
1000 /* log I/O is always issued ASYNC */
1001 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001003 /*
1004 * do not reference the buffer (bp) here as we could race
1005 * with it being freed after writing the unmount record to the
1006 * log.
1007 */
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009} /* xlog_iodone */
1010
1011/*
1012 * The bdstrat callback function for log bufs. This gives us a central
1013 * place to trap bufs in case we get hit by a log I/O error and need to
1014 * shutdown. Actually, in practice, even when we didn't get a log error,
1015 * we transition the iclogs to IOERROR state *after* flushing all existing
1016 * iclogs to disk. This is because we don't want anymore new transactions to be
1017 * started or completed afterwards.
1018 */
1019STATIC int
1020xlog_bdstrat_cb(struct xfs_buf *bp)
1021{
1022 xlog_in_core_t *iclog;
1023
1024 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1025
1026 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1027 /* note for irix bstrat will need struct bdevsw passed
1028 * Fix the following macro if the code ever is merged
1029 */
1030 XFS_bdstrat(bp);
1031 return 0;
1032 }
1033
1034 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1035 XFS_BUF_ERROR(bp, EIO);
1036 XFS_BUF_STALE(bp);
1037 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001038 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040
1041}
1042
1043/*
1044 * Return size of each in-core log record buffer.
1045 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001046 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *
1048 * If the filesystem blocksize is too large, we may need to choose a
1049 * larger size since the directory code currently logs entire blocks.
1050 */
1051
1052STATIC void
1053xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1054 xlog_t *log)
1055{
1056 int size;
1057 int xhdrs;
1058
Eric Sandeen1cb51252007-08-16 16:24:43 +10001059 if (mp->m_logbufs <= 0)
1060 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1061 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001062 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /*
1065 * Buffer size passed in from mount system call.
1066 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001067 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 size = log->l_iclog_size = mp->m_logbsize;
1069 log->l_iclog_size_log = 0;
1070 while (size != 1) {
1071 log->l_iclog_size_log++;
1072 size >>= 1;
1073 }
1074
1075 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1076 /* # headers = size / 32K
1077 * one header holds cycles from 32K of data
1078 */
1079
1080 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1081 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1082 xhdrs++;
1083 log->l_iclog_hsize = xhdrs << BBSHIFT;
1084 log->l_iclog_heads = xhdrs;
1085 } else {
1086 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1087 log->l_iclog_hsize = BBSIZE;
1088 log->l_iclog_heads = 1;
1089 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001090 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
Eric Sandeen1cb51252007-08-16 16:24:43 +10001093 /* All machines use 32KB buffers by default. */
1094 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1095 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /* the default log size is 16k or 32k which is one header sector */
1098 log->l_iclog_hsize = BBSIZE;
1099 log->l_iclog_heads = 1;
1100
1101 /*
1102 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1103 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1104 */
1105 if (mp->m_sb.sb_blocksize >= 16*1024) {
1106 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1107 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001108 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 switch (mp->m_sb.sb_blocksize) {
1110 case 16*1024: /* 16 KB */
1111 log->l_iclog_bufs = 3;
1112 break;
1113 case 32*1024: /* 32 KB */
1114 log->l_iclog_bufs = 4;
1115 break;
1116 case 64*1024: /* 64 KB */
1117 log->l_iclog_bufs = 8;
1118 break;
1119 default:
1120 xlog_panic("XFS: Invalid blocksize");
1121 break;
1122 }
1123 }
1124 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001125
1126done: /* are we being asked to make the sizes selected above visible? */
1127 if (mp->m_logbufs == 0)
1128 mp->m_logbufs = log->l_iclog_bufs;
1129 if (mp->m_logbsize == 0)
1130 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131} /* xlog_get_iclog_buffer_size */
1132
1133
1134/*
1135 * This routine initializes some of the log structure for a given mount point.
1136 * Its primary purpose is to fill in enough, so recovery can occur. However,
1137 * some other stuff may be filled in too.
1138 */
1139STATIC xlog_t *
1140xlog_alloc_log(xfs_mount_t *mp,
1141 xfs_buftarg_t *log_target,
1142 xfs_daddr_t blk_offset,
1143 int num_bblks)
1144{
1145 xlog_t *log;
1146 xlog_rec_header_t *head;
1147 xlog_in_core_t **iclogp;
1148 xlog_in_core_t *iclog, *prev_iclog=NULL;
1149 xfs_buf_t *bp;
1150 int i;
1151 int iclogsize;
1152
1153 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1154
1155 log->l_mp = mp;
1156 log->l_targ = log_target;
1157 log->l_logsize = BBTOB(num_bblks);
1158 log->l_logBBstart = blk_offset;
1159 log->l_logBBsize = num_bblks;
1160 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1161 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1162
1163 log->l_prev_block = -1;
1164 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1165 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1166 log->l_last_sync_lsn = log->l_tail_lsn;
1167 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1168 log->l_grant_reserve_cycle = 1;
1169 log->l_grant_write_cycle = 1;
1170
1171 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1172 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1173 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1174 /* for larger sector sizes, must have v2 or external log */
1175 ASSERT(log->l_sectbb_log == 0 ||
1176 log->l_logBBstart == 0 ||
1177 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1178 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1179 }
1180 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1181
1182 xlog_get_iclog_buffer_size(mp, log);
1183
1184 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1185 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1186 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1187 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1188 ASSERT(XFS_BUF_ISBUSY(bp));
1189 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1190 log->l_xbuf = bp;
1191
1192 spinlock_init(&log->l_icloglock, "iclog");
1193 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1194 initnsema(&log->l_flushsema, 0, "ic-flush");
1195 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1196
1197 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1198 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1199
1200 iclogp = &log->l_iclog;
1201 /*
1202 * The amount of memory to allocate for the iclog structure is
1203 * rather funky due to the way the structure is defined. It is
1204 * done this way so that we can use different sizes for machines
1205 * with different amounts of memory. See the definition of
1206 * xlog_in_core_t in xfs_log_priv.h for details.
1207 */
1208 iclogsize = log->l_iclog_size;
1209 ASSERT(log->l_iclog_size >= 4096);
1210 for (i=0; i < log->l_iclog_bufs; i++) {
1211 *iclogp = (xlog_in_core_t *)
1212 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1213 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 iclog->ic_prev = prev_iclog;
1215 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001216
1217 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1218 if (!XFS_BUF_CPSEMA(bp))
1219 ASSERT(0);
1220 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1221 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1222 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1223 iclog->ic_bp = bp;
1224 iclog->hic_data = bp->b_addr;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1227
1228 head = &iclog->ic_header;
1229 memset(head, 0, sizeof(xlog_rec_header_t));
1230 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1231 INT_SET(head->h_version, ARCH_CONVERT,
1232 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1233 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1234 /* new fields */
1235 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1236 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1240 iclog->ic_state = XLOG_STATE_ACTIVE;
1241 iclog->ic_log = log;
1242 iclog->ic_callback_tail = &(iclog->ic_callback);
1243 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1244
1245 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1246 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1247 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1248 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1249
1250 iclogp = &iclog->ic_next;
1251 }
1252 *iclogp = log->l_iclog; /* complete ring */
1253 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1254
1255 return log;
1256} /* xlog_alloc_log */
1257
1258
1259/*
1260 * Write out the commit record of a transaction associated with the given
1261 * ticket. Return the lsn of the commit record.
1262 */
1263STATIC int
1264xlog_commit_record(xfs_mount_t *mp,
1265 xlog_ticket_t *ticket,
1266 xlog_in_core_t **iclog,
1267 xfs_lsn_t *commitlsnp)
1268{
1269 int error;
1270 xfs_log_iovec_t reg[1];
1271
1272 reg[0].i_addr = NULL;
1273 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001274 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 ASSERT_ALWAYS(iclog);
1277 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1278 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001279 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001281 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282} /* xlog_commit_record */
1283
1284
1285/*
1286 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1287 * log space. This code pushes on the lsn which would supposedly free up
1288 * the 25% which we want to leave free. We may need to adopt a policy which
1289 * pushes on an lsn which is further along in the log once we reach the high
1290 * water mark. In this manner, we would be creating a low water mark.
1291 */
1292void
1293xlog_grant_push_ail(xfs_mount_t *mp,
1294 int need_bytes)
1295{
1296 xlog_t *log = mp->m_log; /* pointer to the log */
1297 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1298 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1299 int free_blocks; /* free blocks left to write to */
1300 int free_bytes; /* free bytes left to write to */
1301 int threshold_block; /* block in lsn we'd like to be at */
1302 int threshold_cycle; /* lsn cycle we'd like to be at */
1303 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1306
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001307 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 free_bytes = xlog_space_left(log,
1309 log->l_grant_reserve_cycle,
1310 log->l_grant_reserve_bytes);
1311 tail_lsn = log->l_tail_lsn;
1312 free_blocks = BTOBBT(free_bytes);
1313
1314 /*
1315 * Set the threshold for the minimum number of free blocks in the
1316 * log to the maximum of what the caller needs, one quarter of the
1317 * log, and 256 blocks.
1318 */
1319 free_threshold = BTOBB(need_bytes);
1320 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1321 free_threshold = MAX(free_threshold, 256);
1322 if (free_blocks < free_threshold) {
1323 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1324 threshold_cycle = CYCLE_LSN(tail_lsn);
1325 if (threshold_block >= log->l_logBBsize) {
1326 threshold_block -= log->l_logBBsize;
1327 threshold_cycle += 1;
1328 }
1329 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1330 threshold_block);
1331
1332 /* Don't pass in an lsn greater than the lsn of the last
1333 * log record known to be on disk.
1334 */
1335 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1336 threshold_lsn = log->l_last_sync_lsn;
1337 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001338 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /*
1341 * Get the transaction layer to kick the dirty buffers out to
1342 * disk asynchronously. No point in trying to do this if
1343 * the filesystem is shutting down.
1344 */
1345 if (threshold_lsn &&
1346 !XLOG_FORCED_SHUTDOWN(log))
1347 xfs_trans_push_ail(mp, threshold_lsn);
1348} /* xlog_grant_push_ail */
1349
1350
1351/*
1352 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1353 * fashion. Previously, we should have moved the current iclog
1354 * ptr in the log to point to the next available iclog. This allows further
1355 * write to continue while this code syncs out an iclog ready to go.
1356 * Before an in-core log can be written out, the data section must be scanned
1357 * to save away the 1st word of each BBSIZE block into the header. We replace
1358 * it with the current cycle count. Each BBSIZE block is tagged with the
1359 * cycle count because there in an implicit assumption that drives will
1360 * guarantee that entire 512 byte blocks get written at once. In other words,
1361 * we can't have part of a 512 byte block written and part not written. By
1362 * tagging each block, we will know which blocks are valid when recovering
1363 * after an unclean shutdown.
1364 *
1365 * This routine is single threaded on the iclog. No other thread can be in
1366 * this routine with the same iclog. Changing contents of iclog can there-
1367 * fore be done without grabbing the state machine lock. Updating the global
1368 * log will require grabbing the lock though.
1369 *
1370 * The entire log manager uses a logical block numbering scheme. Only
1371 * log_sync (and then only bwrite()) know about the fact that the log may
1372 * not start with block zero on a given device. The log block start offset
1373 * is added immediately before calling bwrite().
1374 */
1375
1376int
1377xlog_sync(xlog_t *log,
1378 xlog_in_core_t *iclog)
1379{
1380 xfs_caddr_t dptr; /* pointer to byte sized element */
1381 xfs_buf_t *bp;
1382 int i, ops;
1383 uint count; /* byte count of bwrite */
1384 uint count_init; /* initial count before roundup */
1385 int roundoff; /* roundoff to BB or stripe */
1386 int split = 0; /* split write into two regions */
1387 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1389
1390 XFS_STATS_INC(xs_log_writes);
1391 ASSERT(iclog->ic_refcnt == 0);
1392
1393 /* Add for LR header */
1394 count_init = log->l_iclog_hsize + iclog->ic_offset;
1395
1396 /* Round out the log write size */
1397 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1398 /* we have a v2 stripe unit to use */
1399 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1400 } else {
1401 count = BBTOB(BTOBB(count_init));
1402 }
1403 roundoff = count - count_init;
1404 ASSERT(roundoff >= 0);
1405 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1406 roundoff < log->l_mp->m_sb.sb_logsunit)
1407 ||
1408 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1409 roundoff < BBTOB(1)));
1410
1411 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001412 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001413 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001414 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /* put cycle number in every block */
1417 xlog_pack_data(log, iclog, roundoff);
1418
1419 /* real byte length */
1420 if (v2) {
1421 INT_SET(iclog->ic_header.h_len,
1422 ARCH_CONVERT,
1423 iclog->ic_offset + roundoff);
1424 } else {
1425 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1426 }
1427
1428 /* put ops count in correct order */
1429 ops = iclog->ic_header.h_num_logops;
1430 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1431
Nathan Scottf5faad72006-07-28 17:04:44 +10001432 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1434 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1435 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1436
1437 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1438
1439 /* Do we need to split this write into 2 parts? */
1440 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1441 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1442 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1443 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1444 } else {
1445 iclog->ic_bwritecnt = 1;
1446 }
David Chinner511105b2007-05-24 15:21:57 +10001447 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001449 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 XFS_BUF_BUSY(bp);
1451 XFS_BUF_ASYNC(bp);
1452 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001453 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001454 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001456 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001457 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1460 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1461
1462 xlog_verify_iclog(log, iclog, count, B_TRUE);
1463
1464 /* account for log which doesn't start at block #0 */
1465 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1466 /*
1467 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1468 * is shutting down.
1469 */
1470 XFS_BUF_WRITE(bp);
1471
1472 if ((error = XFS_bwrite(bp))) {
1473 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1474 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001475 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001478 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1480 (unsigned long)1);
1481 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1482 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1483 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1484 (__psint_t)count), split);
1485 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001486 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 XFS_BUF_BUSY(bp);
1488 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001489 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1490 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 dptr = XFS_BUF_PTR(bp);
1492 /*
1493 * Bump the cycle numbers at the start of each block
1494 * since this part of the buffer is at the start of
1495 * a new cycle. Watch out for the header magic number
1496 * case, though.
1497 */
1498 for (i=0; i<split; i += BBSIZE) {
1499 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1500 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1501 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1502 dptr += BBSIZE;
1503 }
1504
1505 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1506 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1507
Nathan Scottc41564b2006-03-29 08:55:14 +10001508 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1510 XFS_BUF_WRITE(bp);
1511 if ((error = XFS_bwrite(bp))) {
1512 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1513 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001514 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518} /* xlog_sync */
1519
1520
1521/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001522 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 */
1524void
Nathan Scottc41564b2006-03-29 08:55:14 +10001525xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 xlog_in_core_t *iclog, *next_iclog;
1528 xlog_ticket_t *tic, *next_tic;
1529 int i;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 iclog = log->l_iclog;
1532 for (i=0; i<log->l_iclog_bufs; i++) {
1533 sv_destroy(&iclog->ic_forcesema);
1534 sv_destroy(&iclog->ic_writesema);
1535 xfs_buf_free(iclog->ic_bp);
1536#ifdef XFS_LOG_TRACE
1537 if (iclog->ic_trace != NULL) {
1538 ktrace_free(iclog->ic_trace);
1539 }
1540#endif
1541 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 kmem_free(iclog, sizeof(xlog_in_core_t));
1543 iclog = next_iclog;
1544 }
1545 freesema(&log->l_flushsema);
1546 spinlock_destroy(&log->l_icloglock);
1547 spinlock_destroy(&log->l_grant_lock);
1548
1549 /* XXXsup take a look at this again. */
1550 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1551 !XLOG_FORCED_SHUTDOWN(log)) {
1552 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001553 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 log->l_ticket_cnt, log->l_ticket_tcnt);
1555 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1556
1557 } else {
1558 tic = log->l_unmount_free;
1559 while (tic) {
1560 next_tic = tic->t_next;
1561 kmem_free(tic, NBPP);
1562 tic = next_tic;
1563 }
1564 }
1565 xfs_buf_free(log->l_xbuf);
1566#ifdef XFS_LOG_TRACE
1567 if (log->l_trace != NULL) {
1568 ktrace_free(log->l_trace);
1569 }
1570 if (log->l_grant_trace != NULL) {
1571 ktrace_free(log->l_grant_trace);
1572 }
1573#endif
1574 log->l_mp->m_log = NULL;
1575 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001576} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578/*
1579 * Update counters atomically now that memcpy is done.
1580 */
1581/* ARGSUSED */
1582static inline void
1583xlog_state_finish_copy(xlog_t *log,
1584 xlog_in_core_t *iclog,
1585 int record_cnt,
1586 int copy_bytes)
1587{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001588 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 iclog->ic_header.h_num_logops += record_cnt;
1591 iclog->ic_offset += copy_bytes;
1592
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001593 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594} /* xlog_state_finish_copy */
1595
1596
1597
1598
1599/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001600 * print out info relating to regions written which consume
1601 * the reservation
1602 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001603STATIC void
1604xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1605{
1606 uint i;
1607 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1608
1609 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1610 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1611 "bformat",
1612 "bchunk",
1613 "efi_format",
1614 "efd_format",
1615 "iformat",
1616 "icore",
1617 "iext",
1618 "ibroot",
1619 "ilocal",
1620 "iattr_ext",
1621 "iattr_broot",
1622 "iattr_local",
1623 "qformat",
1624 "dquot",
1625 "quotaoff",
1626 "LR header",
1627 "unmount",
1628 "commit",
1629 "trans header"
1630 };
1631 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1632 "SETATTR_NOT_SIZE",
1633 "SETATTR_SIZE",
1634 "INACTIVE",
1635 "CREATE",
1636 "CREATE_TRUNC",
1637 "TRUNCATE_FILE",
1638 "REMOVE",
1639 "LINK",
1640 "RENAME",
1641 "MKDIR",
1642 "RMDIR",
1643 "SYMLINK",
1644 "SET_DMATTRS",
1645 "GROWFS",
1646 "STRAT_WRITE",
1647 "DIOSTRAT",
1648 "WRITE_SYNC",
1649 "WRITEID",
1650 "ADDAFORK",
1651 "ATTRINVAL",
1652 "ATRUNCATE",
1653 "ATTR_SET",
1654 "ATTR_RM",
1655 "ATTR_FLAG",
1656 "CLEAR_AGI_BUCKET",
1657 "QM_SBCHANGE",
1658 "DUMMY1",
1659 "DUMMY2",
1660 "QM_QUOTAOFF",
1661 "QM_DQALLOC",
1662 "QM_SETQLIM",
1663 "QM_DQCLUSTER",
1664 "QM_QINOCREATE",
1665 "QM_QUOTAOFF_END",
1666 "SB_UNIT",
1667 "FSYNC_TS",
1668 "GROWFSRT_ALLOC",
1669 "GROWFSRT_ZERO",
1670 "GROWFSRT_FREE",
1671 "SWAPEXT"
1672 };
1673
1674 xfs_fs_cmn_err(CE_WARN, mp,
1675 "xfs_log_write: reservation summary:\n"
1676 " trans type = %s (%u)\n"
1677 " unit res = %d bytes\n"
1678 " current res = %d bytes\n"
1679 " total reg = %u bytes (o/flow = %u bytes)\n"
1680 " ophdrs = %u (ophdr space = %u bytes)\n"
1681 " ophdr + reg = %u bytes\n"
1682 " num regions = %u\n",
1683 ((ticket->t_trans_type <= 0 ||
1684 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1685 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1686 ticket->t_trans_type,
1687 ticket->t_unit_res,
1688 ticket->t_curr_res,
1689 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1690 ticket->t_res_num_ophdrs, ophdr_spc,
1691 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001692 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001693 ticket->t_res_num);
1694
1695 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001696 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001697 cmn_err(CE_WARN,
1698 "region[%u]: %s - %u bytes\n",
1699 i,
1700 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1701 "bad-rtype" : res_type_str[r_type-1]),
1702 ticket->t_res_arr[i].r_len);
1703 }
1704}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001705
1706/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * Write some region out to in-core log
1708 *
1709 * This will be called when writing externally provided regions or when
1710 * writing out a commit record for a given transaction.
1711 *
1712 * General algorithm:
1713 * 1. Find total length of this write. This may include adding to the
1714 * lengths passed in.
1715 * 2. Check whether we violate the tickets reservation.
1716 * 3. While writing to this iclog
1717 * A. Reserve as much space in this iclog as can get
1718 * B. If this is first write, save away start lsn
1719 * C. While writing this region:
1720 * 1. If first write of transaction, write start record
1721 * 2. Write log operation header (header per region)
1722 * 3. Find out if we can fit entire region into this iclog
1723 * 4. Potentially, verify destination memcpy ptr
1724 * 5. Memcpy (partial) region
1725 * 6. If partial copy, release iclog; otherwise, continue
1726 * copying more regions into current iclog
1727 * 4. Mark want sync bit (in simulation mode)
1728 * 5. Release iclog for potential flush to on-disk log.
1729 *
1730 * ERRORS:
1731 * 1. Panic if reservation is overrun. This should never happen since
1732 * reservation amounts are generated internal to the filesystem.
1733 * NOTES:
1734 * 1. Tickets are single threaded data structures.
1735 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1736 * syncing routine. When a single log_write region needs to span
1737 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1738 * on all log operation writes which don't contain the end of the
1739 * region. The XLOG_END_TRANS bit is used for the in-core log
1740 * operation which contains the end of the continued log_write region.
1741 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1742 * we don't really know exactly how much space will be used. As a result,
1743 * we don't update ic_offset until the end when we know exactly how many
1744 * bytes have been written out.
1745 */
1746int
1747xlog_write(xfs_mount_t * mp,
1748 xfs_log_iovec_t reg[],
1749 int nentries,
1750 xfs_log_ticket_t tic,
1751 xfs_lsn_t *start_lsn,
1752 xlog_in_core_t **commit_iclog,
1753 uint flags)
1754{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001755 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001757 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 __psint_t ptr; /* copy address into data region */
1760 int len; /* # xlog_write() bytes 2 still copy */
1761 int index; /* region index currently copying */
1762 int log_offset; /* offset (from 0) into data region */
1763 int start_rec_copy; /* # bytes to copy for start record */
1764 int partial_copy; /* did we split a region? */
1765 int partial_copy_len;/* # bytes copied if split region */
1766 int need_copy; /* # bytes need to memcpy this region */
1767 int copy_len; /* # bytes actually memcpy'ing */
1768 int copy_off; /* # bytes from entry start */
1769 int contwr; /* continued write of in-core log? */
1770 int error;
1771 int record_cnt = 0, data_cnt = 0;
1772
1773 partial_copy_len = partial_copy = 0;
1774
1775 /* Calculate potential maximum space. Each region gets its own
1776 * xlog_op_header_t and may need to be double word aligned.
1777 */
1778 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001779 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001781 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 for (index = 0; index < nentries; index++) {
1785 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001786 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001788 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 contwr = *start_lsn = 0;
1791
1792 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001793 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794#ifdef DEBUG
1795 xlog_panic(
1796 "xfs_log_write: reservation ran out. Need to up reservation");
1797#else
1798 /* Customer configurable panic */
1799 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1800 "xfs_log_write: reservation ran out. Need to up reservation");
1801 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001802 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803#endif
1804 } else
1805 ticket->t_curr_res -= len;
1806
1807 for (index = 0; index < nentries; ) {
1808 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1809 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001810 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 ASSERT(log_offset <= iclog->ic_size - 1);
1813 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1814
1815 /* start_lsn is the first lsn written to. That's all we need. */
1816 if (! *start_lsn)
1817 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1818
1819 /* This loop writes out as many regions as can fit in the amount
1820 * of space which was allocated by xlog_state_get_iclog_space().
1821 */
1822 while (index < nentries) {
1823 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1824 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1825 start_rec_copy = 0;
1826
1827 /* If first write for transaction, insert start record.
1828 * We can't be trying to commit if we are inited. We can't
1829 * have any "partial_copy" if we are inited.
1830 */
1831 if (ticket->t_flags & XLOG_TIC_INITED) {
1832 logop_head = (xlog_op_header_t *)ptr;
1833 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1834 logop_head->oh_clientid = ticket->t_clientid;
1835 logop_head->oh_len = 0;
1836 logop_head->oh_flags = XLOG_START_TRANS;
1837 logop_head->oh_res2 = 0;
1838 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1839 record_cnt++;
1840
1841 start_rec_copy = sizeof(xlog_op_header_t);
1842 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1843 }
1844
1845 /* Copy log operation header directly into data section */
1846 logop_head = (xlog_op_header_t *)ptr;
1847 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1848 logop_head->oh_clientid = ticket->t_clientid;
1849 logop_head->oh_res2 = 0;
1850
1851 /* header copied directly */
1852 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1853
1854 /* are we copying a commit or unmount record? */
1855 logop_head->oh_flags = flags;
1856
1857 /*
1858 * We've seen logs corrupted with bad transaction client
1859 * ids. This makes sure that XFS doesn't generate them on.
1860 * Turn this into an EIO and shut down the filesystem.
1861 */
1862 switch (logop_head->oh_clientid) {
1863 case XFS_TRANSACTION:
1864 case XFS_VOLUME:
1865 case XFS_LOG:
1866 break;
1867 default:
1868 xfs_fs_cmn_err(CE_WARN, mp,
1869 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1870 logop_head->oh_clientid, tic);
1871 return XFS_ERROR(EIO);
1872 }
1873
1874 /* Partial write last time? => (partial_copy != 0)
1875 * need_copy is the amount we'd like to copy if everything could
1876 * fit in the current memcpy.
1877 */
1878 need_copy = reg[index].i_len - partial_copy_len;
1879
1880 copy_off = partial_copy_len;
1881 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1882 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1883 if (partial_copy)
1884 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1885 partial_copy_len = partial_copy = 0;
1886 } else { /* partial write */
1887 copy_len = iclog->ic_size - log_offset;
1888 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1889 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1890 if (partial_copy)
1891 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1892 partial_copy_len += copy_len;
1893 partial_copy++;
1894 len += sizeof(xlog_op_header_t); /* from splitting of region */
1895 /* account for new log op header */
1896 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001897 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899 xlog_verify_dest_ptr(log, ptr);
1900
1901 /* copy region */
1902 ASSERT(copy_len >= 0);
1903 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1904 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1905
1906 /* make copy_len total bytes copied, including headers */
1907 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1908 record_cnt++;
1909 data_cnt += contwr ? copy_len : 0;
1910 if (partial_copy) { /* copied partial region */
1911 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1912 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1913 record_cnt = data_cnt = 0;
1914 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001915 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 break; /* don't increment index */
1917 } else { /* copied entire region */
1918 index++;
1919 partial_copy_len = partial_copy = 0;
1920
1921 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1922 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1923 record_cnt = data_cnt = 0;
1924 xlog_state_want_sync(log, iclog);
1925 if (commit_iclog) {
1926 ASSERT(flags & XLOG_COMMIT_TRANS);
1927 *commit_iclog = iclog;
1928 } else 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 if (index == nentries)
1931 return 0; /* we are done */
1932 else
1933 break;
1934 }
1935 } /* if (partial_copy) */
1936 } /* while (index < nentries) */
1937 } /* for (index = 0; index < nentries; ) */
1938 ASSERT(len == 0);
1939
1940 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1941 if (commit_iclog) {
1942 ASSERT(flags & XLOG_COMMIT_TRANS);
1943 *commit_iclog = iclog;
1944 return 0;
1945 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001946 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947} /* xlog_write */
1948
1949
1950/*****************************************************************************
1951 *
1952 * State Machine functions
1953 *
1954 *****************************************************************************
1955 */
1956
1957/* Clean iclogs starting from the head. This ordering must be
1958 * maintained, so an iclog doesn't become ACTIVE beyond one that
1959 * is SYNCING. This is also required to maintain the notion that we use
1960 * a counting semaphore to hold off would be writers to the log when every
1961 * iclog is trying to sync to disk.
1962 *
1963 * State Change: DIRTY -> ACTIVE
1964 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001965STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966xlog_state_clean_log(xlog_t *log)
1967{
1968 xlog_in_core_t *iclog;
1969 int changed = 0;
1970
1971 iclog = log->l_iclog;
1972 do {
1973 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1974 iclog->ic_state = XLOG_STATE_ACTIVE;
1975 iclog->ic_offset = 0;
1976 iclog->ic_callback = NULL; /* don't need to free */
1977 /*
1978 * If the number of ops in this iclog indicate it just
1979 * contains the dummy transaction, we can
1980 * change state into IDLE (the second time around).
1981 * Otherwise we should change the state into
1982 * NEED a dummy.
1983 * We don't need to cover the dummy.
1984 */
1985 if (!changed &&
1986 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1987 changed = 1;
1988 } else {
1989 /*
1990 * We have two dirty iclogs so start over
1991 * This could also be num of ops indicates
1992 * this is not the dummy going out.
1993 */
1994 changed = 2;
1995 }
1996 iclog->ic_header.h_num_logops = 0;
1997 memset(iclog->ic_header.h_cycle_data, 0,
1998 sizeof(iclog->ic_header.h_cycle_data));
1999 iclog->ic_header.h_lsn = 0;
2000 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2001 /* do nothing */;
2002 else
2003 break; /* stop cleaning */
2004 iclog = iclog->ic_next;
2005 } while (iclog != log->l_iclog);
2006
2007 /* log is locked when we are called */
2008 /*
2009 * Change state for the dummy log recording.
2010 * We usually go to NEED. But we go to NEED2 if the changed indicates
2011 * we are done writing the dummy record.
2012 * If we are done with the second dummy recored (DONE2), then
2013 * we go to IDLE.
2014 */
2015 if (changed) {
2016 switch (log->l_covered_state) {
2017 case XLOG_STATE_COVER_IDLE:
2018 case XLOG_STATE_COVER_NEED:
2019 case XLOG_STATE_COVER_NEED2:
2020 log->l_covered_state = XLOG_STATE_COVER_NEED;
2021 break;
2022
2023 case XLOG_STATE_COVER_DONE:
2024 if (changed == 1)
2025 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2026 else
2027 log->l_covered_state = XLOG_STATE_COVER_NEED;
2028 break;
2029
2030 case XLOG_STATE_COVER_DONE2:
2031 if (changed == 1)
2032 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2033 else
2034 log->l_covered_state = XLOG_STATE_COVER_NEED;
2035 break;
2036
2037 default:
2038 ASSERT(0);
2039 }
2040 }
2041} /* xlog_state_clean_log */
2042
2043STATIC xfs_lsn_t
2044xlog_get_lowest_lsn(
2045 xlog_t *log)
2046{
2047 xlog_in_core_t *lsn_log;
2048 xfs_lsn_t lowest_lsn, lsn;
2049
2050 lsn_log = log->l_iclog;
2051 lowest_lsn = 0;
2052 do {
2053 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2054 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
2055 if ((lsn && !lowest_lsn) ||
2056 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2057 lowest_lsn = lsn;
2058 }
2059 }
2060 lsn_log = lsn_log->ic_next;
2061 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002062 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
2065
2066STATIC void
2067xlog_state_do_callback(
2068 xlog_t *log,
2069 int aborted,
2070 xlog_in_core_t *ciclog)
2071{
2072 xlog_in_core_t *iclog;
2073 xlog_in_core_t *first_iclog; /* used to know when we've
2074 * processed all iclogs once */
2075 xfs_log_callback_t *cb, *cb_next;
2076 int flushcnt = 0;
2077 xfs_lsn_t lowest_lsn;
2078 int ioerrors; /* counter: iclogs with errors */
2079 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2080 int funcdidcallbacks; /* flag: function did callbacks */
2081 int repeats; /* for issuing console warnings if
2082 * looping too many times */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002084 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 first_iclog = iclog = log->l_iclog;
2086 ioerrors = 0;
2087 funcdidcallbacks = 0;
2088 repeats = 0;
2089
2090 do {
2091 /*
2092 * Scan all iclogs starting with the one pointed to by the
2093 * log. Reset this starting point each time the log is
2094 * unlocked (during callbacks).
2095 *
2096 * Keep looping through iclogs until one full pass is made
2097 * without running any callbacks.
2098 */
2099 first_iclog = log->l_iclog;
2100 iclog = log->l_iclog;
2101 loopdidcallbacks = 0;
2102 repeats++;
2103
2104 do {
2105
2106 /* skip all iclogs in the ACTIVE & DIRTY states */
2107 if (iclog->ic_state &
2108 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2109 iclog = iclog->ic_next;
2110 continue;
2111 }
2112
2113 /*
2114 * Between marking a filesystem SHUTDOWN and stopping
2115 * the log, we do flush all iclogs to disk (if there
2116 * wasn't a log I/O error). So, we do want things to
2117 * go smoothly in case of just a SHUTDOWN w/o a
2118 * LOG_IO_ERROR.
2119 */
2120 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2121 /*
2122 * Can only perform callbacks in order. Since
2123 * this iclog is not in the DONE_SYNC/
2124 * DO_CALLBACK state, we skip the rest and
2125 * just try to clean up. If we set our iclog
2126 * to DO_CALLBACK, we will not process it when
2127 * we retry since a previous iclog is in the
2128 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002129 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 */
2131 if (!(iclog->ic_state &
2132 (XLOG_STATE_DONE_SYNC |
2133 XLOG_STATE_DO_CALLBACK))) {
2134 if (ciclog && (ciclog->ic_state ==
2135 XLOG_STATE_DONE_SYNC)) {
2136 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2137 }
2138 break;
2139 }
2140 /*
2141 * We now have an iclog that is in either the
2142 * DO_CALLBACK or DONE_SYNC states. The other
2143 * states (WANT_SYNC, SYNCING, or CALLBACK were
2144 * caught by the above if and are going to
2145 * clean (i.e. we aren't doing their callbacks)
2146 * see the above if.
2147 */
2148
2149 /*
2150 * We will do one more check here to see if we
2151 * have chased our tail around.
2152 */
2153
2154 lowest_lsn = xlog_get_lowest_lsn(log);
2155 if (lowest_lsn && (
2156 XFS_LSN_CMP(
2157 lowest_lsn,
2158 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2159 )<0)) {
2160 iclog = iclog->ic_next;
2161 continue; /* Leave this iclog for
2162 * another thread */
2163 }
2164
2165 iclog->ic_state = XLOG_STATE_CALLBACK;
2166
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002167 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002170 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 * No one else can be here except us.
2172 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002173 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 ASSERT(XFS_LSN_CMP(
2175 log->l_last_sync_lsn,
2176 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2177 )<=0);
2178 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002179 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /*
2182 * Keep processing entries in the callback list
2183 * until we come around and it is empty. We
2184 * need to atomically see that the list is
2185 * empty and change the state to DIRTY so that
2186 * we don't miss any more callbacks being added.
2187 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002188 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 } else {
2190 ioerrors++;
2191 }
2192 cb = iclog->ic_callback;
2193
Christoph Hellwig4b809162007-08-16 15:37:36 +10002194 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 iclog->ic_callback_tail = &(iclog->ic_callback);
2196 iclog->ic_callback = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002197 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002200 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 cb_next = cb->cb_next;
2202 cb->cb_func(cb->cb_arg, aborted);
2203 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002204 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 cb = iclog->ic_callback;
2206 }
2207
2208 loopdidcallbacks++;
2209 funcdidcallbacks++;
2210
Christoph Hellwig4b809162007-08-16 15:37:36 +10002211 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2213 iclog->ic_state = XLOG_STATE_DIRTY;
2214
2215 /*
2216 * Transition from DIRTY to ACTIVE if applicable.
2217 * NOP if STATE_IOERROR.
2218 */
2219 xlog_state_clean_log(log);
2220
2221 /* wake up threads waiting in xfs_log_force() */
2222 sv_broadcast(&iclog->ic_forcesema);
2223
2224 iclog = iclog->ic_next;
2225 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002226
2227 if (repeats > 5000) {
2228 flushcnt += repeats;
2229 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002231 "%s: possible infinite loop (%d iterations)",
2232 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234 } while (!ioerrors && loopdidcallbacks);
2235
2236 /*
2237 * make one last gasp attempt to see if iclogs are being left in
2238 * limbo..
2239 */
2240#ifdef DEBUG
2241 if (funcdidcallbacks) {
2242 first_iclog = iclog = log->l_iclog;
2243 do {
2244 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2245 /*
2246 * Terminate the loop if iclogs are found in states
2247 * which will cause other threads to clean up iclogs.
2248 *
2249 * SYNCING - i/o completion will go through logs
2250 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002251 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * IOERROR - give up hope all ye who enter here
2253 */
2254 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2255 iclog->ic_state == XLOG_STATE_SYNCING ||
2256 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2257 iclog->ic_state == XLOG_STATE_IOERROR )
2258 break;
2259 iclog = iclog->ic_next;
2260 } while (first_iclog != iclog);
2261 }
2262#endif
2263
Nathan Scotta3c66852006-09-28 11:02:14 +10002264 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2266 flushcnt = log->l_flushcnt;
2267 log->l_flushcnt = 0;
2268 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002269 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 while (flushcnt--)
2271 vsema(&log->l_flushsema);
2272} /* xlog_state_do_callback */
2273
2274
2275/*
2276 * Finish transitioning this iclog to the dirty state.
2277 *
2278 * Make sure that we completely execute this routine only when this is
2279 * the last call to the iclog. There is a good chance that iclog flushes,
2280 * when we reach the end of the physical log, get turned into 2 separate
2281 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2282 * routine. By using the reference count bwritecnt, we guarantee that only
2283 * the second completion goes through.
2284 *
2285 * Callbacks could take time, so they are done outside the scope of the
2286 * global state machine log lock. Assume that the calls to cvsema won't
2287 * take a long time. At least we know it won't sleep.
2288 */
2289void
2290xlog_state_done_syncing(
2291 xlog_in_core_t *iclog,
2292 int aborted)
2293{
2294 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002296 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2299 iclog->ic_state == XLOG_STATE_IOERROR);
2300 ASSERT(iclog->ic_refcnt == 0);
2301 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2302
2303
2304 /*
2305 * If we got an error, either on the first buffer, or in the case of
2306 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2307 * and none should ever be attempted to be written to disk
2308 * again.
2309 */
2310 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2311 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002312 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return;
2314 }
2315 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2316 }
2317
2318 /*
2319 * Someone could be sleeping prior to writing out the next
2320 * iclog buffer, we wake them all, one will get to do the
2321 * I/O, the others get to wait for the result.
2322 */
2323 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002324 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2326} /* xlog_state_done_syncing */
2327
2328
2329/*
2330 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2331 * sleep. The flush semaphore is set to the number of in-core buffers and
2332 * decremented around disk syncing. Therefore, if all buffers are syncing,
2333 * this semaphore will cause new writes to sleep until a sync completes.
2334 * Otherwise, this code just does p() followed by v(). This approximates
2335 * a sleep/wakeup except we can't race.
2336 *
2337 * The in-core logs are used in a circular fashion. They are not used
2338 * out-of-order even when an iclog past the head is free.
2339 *
2340 * return:
2341 * * log_offset where xlog_write() can start writing into the in-core
2342 * log's data space.
2343 * * in-core log pointer to which xlog_write() should write.
2344 * * boolean indicating this is a continued write to an in-core log.
2345 * If this is the last write, then the in-core log's offset field
2346 * needs to be incremented, depending on the amount of data which
2347 * is copied.
2348 */
2349int
2350xlog_state_get_iclog_space(xlog_t *log,
2351 int len,
2352 xlog_in_core_t **iclogp,
2353 xlog_ticket_t *ticket,
2354 int *continued_write,
2355 int *logoffsetp)
2356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 int log_offset;
2358 xlog_rec_header_t *head;
2359 xlog_in_core_t *iclog;
2360 int error;
2361
2362restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002363 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002365 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return XFS_ERROR(EIO);
2367 }
2368
2369 iclog = log->l_iclog;
2370 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2371 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002372 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2374 XFS_STATS_INC(xs_log_noiclogs);
2375 /* Ensure that log writes happen */
2376 psema(&log->l_flushsema, PINOD);
2377 goto restart;
2378 }
2379 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2380 head = &iclog->ic_header;
2381
2382 iclog->ic_refcnt++; /* prevents sync */
2383 log_offset = iclog->ic_offset;
2384
2385 /* On the 1st write to an iclog, figure out lsn. This works
2386 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2387 * committing to. If the offset is set, that's how many blocks
2388 * must be written.
2389 */
2390 if (log_offset == 0) {
2391 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002392 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002393 log->l_iclog_hsize,
2394 XLOG_REG_TYPE_LRHEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2396 ASSIGN_LSN(head->h_lsn, log);
2397 ASSERT(log->l_curr_block >= 0);
2398 }
2399
2400 /* If there is enough room to write everything, then do it. Otherwise,
2401 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2402 * bit is on, so this will get flushed out. Don't update ic_offset
2403 * until you know exactly how many bytes get copied. Therefore, wait
2404 * until later to update ic_offset.
2405 *
2406 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2407 * can fit into remaining data section.
2408 */
2409 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2410 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2411
2412 /* If I'm the only one writing to this iclog, sync it to disk */
2413 if (iclog->ic_refcnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002414 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002416 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 } else {
2418 iclog->ic_refcnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002419 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421 goto restart;
2422 }
2423
2424 /* Do we have enough room to write the full amount in the remainder
2425 * of this iclog? Or must we continue a write on the next iclog and
2426 * mark this iclog as completely taken? In the case where we switch
2427 * iclogs (to mark it taken), this particular iclog will release/sync
2428 * to disk in xlog_write().
2429 */
2430 if (len <= iclog->ic_size - iclog->ic_offset) {
2431 *continued_write = 0;
2432 iclog->ic_offset += len;
2433 } else {
2434 *continued_write = 1;
2435 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2436 }
2437 *iclogp = iclog;
2438
2439 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002440 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 *logoffsetp = log_offset;
2443 return 0;
2444} /* xlog_state_get_iclog_space */
2445
2446/*
2447 * Atomically get the log space required for a log ticket.
2448 *
2449 * Once a ticket gets put onto the reserveq, it will only return after
2450 * the needed reservation is satisfied.
2451 */
2452STATIC int
2453xlog_grant_log_space(xlog_t *log,
2454 xlog_ticket_t *tic)
2455{
2456 int free_bytes;
2457 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458#ifdef DEBUG
2459 xfs_lsn_t tail_lsn;
2460#endif
2461
2462
2463#ifdef DEBUG
2464 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2465 panic("grant Recovery problem");
2466#endif
2467
2468 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002469 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2471
2472 /* something is already sleeping; insert new transaction at end */
2473 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002474 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 xlog_trace_loggrant(log, tic,
2476 "xlog_grant_log_space: sleep 1");
2477 /*
2478 * Gotta check this before going to sleep, while we're
2479 * holding the grant lock.
2480 */
2481 if (XLOG_FORCED_SHUTDOWN(log))
2482 goto error_return;
2483
2484 XFS_STATS_INC(xs_sleep_logspace);
2485 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2486 /*
2487 * If we got an error, and the filesystem is shutting down,
2488 * we'll catch it down below. So just continue...
2489 */
2490 xlog_trace_loggrant(log, tic,
2491 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002492 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2495 need_bytes = tic->t_unit_res*tic->t_ocnt;
2496 else
2497 need_bytes = tic->t_unit_res;
2498
2499redo:
2500 if (XLOG_FORCED_SHUTDOWN(log))
2501 goto error_return;
2502
2503 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2504 log->l_grant_reserve_bytes);
2505 if (free_bytes < need_bytes) {
2506 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002507 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 xlog_trace_loggrant(log, tic,
2509 "xlog_grant_log_space: sleep 2");
2510 XFS_STATS_INC(xs_sleep_logspace);
2511 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2512
2513 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002514 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 goto error_return;
2516 }
2517
2518 xlog_trace_loggrant(log, tic,
2519 "xlog_grant_log_space: wake 2");
2520 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002521 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 goto redo;
2523 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002524 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002527 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528#ifdef DEBUG
2529 tail_lsn = log->l_tail_lsn;
2530 /*
2531 * Check to make sure the grant write head didn't just over lap the
2532 * tail. If the cycles are the same, we can't be overlapping.
2533 * Otherwise, make sure that the cycles differ by exactly one and
2534 * check the byte count.
2535 */
2536 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2537 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2538 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2539 }
2540#endif
2541 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2542 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002543 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return 0;
2545
2546 error_return:
2547 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002548 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2550 /*
2551 * If we are failing, make sure the ticket doesn't have any
2552 * current reservations. We don't want to add this back when
2553 * the ticket/transaction gets cancelled.
2554 */
2555 tic->t_curr_res = 0;
2556 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002557 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 return XFS_ERROR(EIO);
2559} /* xlog_grant_log_space */
2560
2561
2562/*
2563 * Replenish the byte reservation required by moving the grant write head.
2564 *
2565 *
2566 */
2567STATIC int
2568xlog_regrant_write_log_space(xlog_t *log,
2569 xlog_ticket_t *tic)
2570{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 int free_bytes, need_bytes;
2572 xlog_ticket_t *ntic;
2573#ifdef DEBUG
2574 xfs_lsn_t tail_lsn;
2575#endif
2576
2577 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002578 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583#ifdef DEBUG
2584 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2585 panic("regrant Recovery problem");
2586#endif
2587
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002588 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2590
2591 if (XLOG_FORCED_SHUTDOWN(log))
2592 goto error_return;
2593
2594 /* If there are other waiters on the queue then give them a
2595 * chance at logspace before us. Wake up the first waiters,
2596 * if we do not wake up all the waiters then go to sleep waiting
2597 * for more free space, otherwise try to get some space for
2598 * this transaction.
2599 */
2600
2601 if ((ntic = log->l_write_headq)) {
2602 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2603 log->l_grant_write_bytes);
2604 do {
2605 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2606
2607 if (free_bytes < ntic->t_unit_res)
2608 break;
2609 free_bytes -= ntic->t_unit_res;
2610 sv_signal(&ntic->t_sema);
2611 ntic = ntic->t_next;
2612 } while (ntic != log->l_write_headq);
2613
2614 if (ntic != log->l_write_headq) {
2615 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002616 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 xlog_trace_loggrant(log, tic,
2619 "xlog_regrant_write_log_space: sleep 1");
2620 XFS_STATS_INC(xs_sleep_logspace);
2621 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2622 &log->l_grant_lock, s);
2623
2624 /* If we're shutting down, this tic is already
2625 * off the queue */
2626 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002627 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 goto error_return;
2629 }
2630
2631 xlog_trace_loggrant(log, tic,
2632 "xlog_regrant_write_log_space: wake 1");
2633 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002634 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
2636 }
2637
2638 need_bytes = tic->t_unit_res;
2639
2640redo:
2641 if (XLOG_FORCED_SHUTDOWN(log))
2642 goto error_return;
2643
2644 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2645 log->l_grant_write_bytes);
2646 if (free_bytes < need_bytes) {
2647 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002648 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 XFS_STATS_INC(xs_sleep_logspace);
2650 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2651
2652 /* If we're shutting down, this tic is already off the queue */
2653 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002654 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 goto error_return;
2656 }
2657
2658 xlog_trace_loggrant(log, tic,
2659 "xlog_regrant_write_log_space: wake 2");
2660 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002661 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 goto redo;
2663 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002664 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002666 /* we've got enough space */
2667 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668#ifdef DEBUG
2669 tail_lsn = log->l_tail_lsn;
2670 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2671 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2672 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2673 }
2674#endif
2675
2676 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2677 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002678 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681
2682 error_return:
2683 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002684 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2686 /*
2687 * If we are failing, make sure the ticket doesn't have any
2688 * current reservations. We don't want to add this back when
2689 * the ticket/transaction gets cancelled.
2690 */
2691 tic->t_curr_res = 0;
2692 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002693 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return XFS_ERROR(EIO);
2695} /* xlog_regrant_write_log_space */
2696
2697
2698/* The first cnt-1 times through here we don't need to
2699 * move the grant write head because the permanent
2700 * reservation has reserved cnt times the unit amount.
2701 * Release part of current permanent unit reservation and
2702 * reset current reservation to be one units worth. Also
2703 * move grant reservation head forward.
2704 */
2705STATIC void
2706xlog_regrant_reserve_log_space(xlog_t *log,
2707 xlog_ticket_t *ticket)
2708{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 xlog_trace_loggrant(log, ticket,
2710 "xlog_regrant_reserve_log_space: enter");
2711 if (ticket->t_cnt > 0)
2712 ticket->t_cnt--;
2713
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002714 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002715 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002717 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 xlog_trace_loggrant(log, ticket,
2719 "xlog_regrant_reserve_log_space: sub current res");
2720 xlog_verify_grant_head(log, 1);
2721
2722 /* just return if we still have some of the pre-reserved space */
2723 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002724 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return;
2726 }
2727
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002728 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 xlog_trace_loggrant(log, ticket,
2730 "xlog_regrant_reserve_log_space: exit");
2731 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002732 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002734 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735} /* xlog_regrant_reserve_log_space */
2736
2737
2738/*
2739 * Give back the space left from a reservation.
2740 *
2741 * All the information we need to make a correct determination of space left
2742 * is present. For non-permanent reservations, things are quite easy. The
2743 * count should have been decremented to zero. We only need to deal with the
2744 * space remaining in the current reservation part of the ticket. If the
2745 * ticket contains a permanent reservation, there may be left over space which
2746 * needs to be released. A count of N means that N-1 refills of the current
2747 * reservation can be done before we need to ask for more space. The first
2748 * one goes to fill up the first current reservation. Once we run out of
2749 * space, the count will stay at zero and the only space remaining will be
2750 * in the current reservation field.
2751 */
2752STATIC void
2753xlog_ungrant_log_space(xlog_t *log,
2754 xlog_ticket_t *ticket)
2755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (ticket->t_cnt > 0)
2757 ticket->t_cnt--;
2758
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002759 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2761
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002762 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2765
2766 /* If this is a permanent reservation ticket, we may be able to free
2767 * up more space based on the remaining count.
2768 */
2769 if (ticket->t_cnt > 0) {
2770 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002771 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773
2774 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2775 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002776 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 xfs_log_move_tail(log->l_mp, 1);
2778} /* xlog_ungrant_log_space */
2779
2780
2781/*
2782 * Atomically put back used ticket.
2783 */
2784void
2785xlog_state_put_ticket(xlog_t *log,
2786 xlog_ticket_t *tic)
2787{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002788 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 xlog_ticket_put(log, tic);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002790 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791} /* xlog_state_put_ticket */
2792
2793/*
2794 * Flush iclog to disk if this is the last reference to the given iclog and
2795 * the WANT_SYNC bit is set.
2796 *
2797 * When this function is entered, the iclog is not necessarily in the
2798 * WANT_SYNC state. It may be sitting around waiting to get filled.
2799 *
2800 *
2801 */
2802int
2803xlog_state_release_iclog(xlog_t *log,
2804 xlog_in_core_t *iclog)
2805{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 int sync = 0; /* do we sync? */
2807
2808 xlog_assign_tail_lsn(log->l_mp);
2809
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002810 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002813 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return XFS_ERROR(EIO);
2815 }
2816
2817 ASSERT(iclog->ic_refcnt > 0);
2818 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2819 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2820
2821 if (--iclog->ic_refcnt == 0 &&
2822 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2823 sync++;
2824 iclog->ic_state = XLOG_STATE_SYNCING;
2825 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2826 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2827 /* cycle incremented when incrementing curr_block */
2828 }
2829
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002830 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 /*
2833 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002834 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2836 * this iclog has consistent data, so we ignore IOERROR
2837 * flags after this point.
2838 */
2839 if (sync) {
2840 return xlog_sync(log, iclog);
2841 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844} /* xlog_state_release_iclog */
2845
2846
2847/*
2848 * This routine will mark the current iclog in the ring as WANT_SYNC
2849 * and move the current iclog pointer to the next iclog in the ring.
2850 * When this routine is called from xlog_state_get_iclog_space(), the
2851 * exact size of the iclog has not yet been determined. All we know is
2852 * that every data block. We have run out of space in this log record.
2853 */
2854STATIC void
2855xlog_state_switch_iclogs(xlog_t *log,
2856 xlog_in_core_t *iclog,
2857 int eventual_size)
2858{
2859 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2860 if (!eventual_size)
2861 eventual_size = iclog->ic_offset;
2862 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2863 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2864 log->l_prev_block = log->l_curr_block;
2865 log->l_prev_cycle = log->l_curr_cycle;
2866
2867 /* roll log?: ic_offset changed later */
2868 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2869
2870 /* Round up to next log-sunit */
2871 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2872 log->l_mp->m_sb.sb_logsunit > 1) {
2873 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2874 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2875 }
2876
2877 if (log->l_curr_block >= log->l_logBBsize) {
2878 log->l_curr_cycle++;
2879 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2880 log->l_curr_cycle++;
2881 log->l_curr_block -= log->l_logBBsize;
2882 ASSERT(log->l_curr_block >= 0);
2883 }
2884 ASSERT(iclog == log->l_iclog);
2885 log->l_iclog = iclog->ic_next;
2886} /* xlog_state_switch_iclogs */
2887
2888
2889/*
2890 * Write out all data in the in-core log as of this exact moment in time.
2891 *
2892 * Data may be written to the in-core log during this call. However,
2893 * we don't guarantee this data will be written out. A change from past
2894 * implementation means this routine will *not* write out zero length LRs.
2895 *
2896 * Basically, we try and perform an intelligent scan of the in-core logs.
2897 * If we determine there is no flushable data, we just return. There is no
2898 * flushable data if:
2899 *
2900 * 1. the current iclog is active and has no data; the previous iclog
2901 * is in the active or dirty state.
2902 * 2. the current iclog is drity, and the previous iclog is in the
2903 * active or dirty state.
2904 *
2905 * We may sleep (call psema) if:
2906 *
2907 * 1. the current iclog is not in the active nor dirty state.
2908 * 2. the current iclog dirty, and the previous iclog is not in the
2909 * active nor dirty state.
2910 * 3. the current iclog is active, and there is another thread writing
2911 * to this particular iclog.
2912 * 4. a) the current iclog is active and has no other writers
2913 * b) when we return from flushing out this iclog, it is still
2914 * not in the active nor dirty state.
2915 */
2916STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002917xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 xlog_in_core_t *iclog;
2920 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002922 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 iclog = log->l_iclog;
2925 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002926 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return XFS_ERROR(EIO);
2928 }
2929
2930 /* If the head iclog is not active nor dirty, we just attach
2931 * ourselves to the head and go to sleep.
2932 */
2933 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2934 iclog->ic_state == XLOG_STATE_DIRTY) {
2935 /*
2936 * If the head is dirty or (active and empty), then
2937 * we need to look at the previous iclog. If the previous
2938 * iclog is active or dirty we are done. There is nothing
2939 * to sync out. Otherwise, we attach ourselves to the
2940 * previous iclog and go to sleep.
2941 */
2942 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2943 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2944 iclog = iclog->ic_prev;
2945 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2946 iclog->ic_state == XLOG_STATE_DIRTY)
2947 goto no_sleep;
2948 else
2949 goto maybe_sleep;
2950 } else {
2951 if (iclog->ic_refcnt == 0) {
2952 /* We are the only one with access to this
2953 * iclog. Flush it out now. There should
2954 * be a roundoff of zero to show that someone
2955 * has already taken care of the roundoff from
2956 * the previous sync.
2957 */
2958 iclog->ic_refcnt++;
2959 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2960 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002961 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 if (xlog_state_release_iclog(log, iclog))
2964 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002965 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002966 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2968 iclog->ic_state != XLOG_STATE_DIRTY)
2969 goto maybe_sleep;
2970 else
2971 goto no_sleep;
2972 } else {
2973 /* Someone else is writing to this iclog.
2974 * Use its call to flush out the data. However,
2975 * the other thread may not force out this LR,
2976 * so we mark it WANT_SYNC.
2977 */
2978 xlog_state_switch_iclogs(log, iclog, 0);
2979 goto maybe_sleep;
2980 }
2981 }
2982 }
2983
2984 /* By the time we come around again, the iclog could've been filled
2985 * which would give it another lsn. If we have a new lsn, just
2986 * return because the relevant data has been flushed.
2987 */
2988maybe_sleep:
2989 if (flags & XFS_LOG_SYNC) {
2990 /*
2991 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002992 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 * Then we check again after waking up, in case our
2994 * sleep was disturbed by a bad news.
2995 */
2996 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002997 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return XFS_ERROR(EIO);
2999 }
3000 XFS_STATS_INC(xs_log_force_sleep);
3001 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3002 /*
3003 * No need to grab the log lock here since we're
3004 * only deciding whether or not to return EIO
3005 * and the memory read should be atomic.
3006 */
3007 if (iclog->ic_state & XLOG_STATE_IOERROR)
3008 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003009 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 } else {
3012
3013no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003014 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016 return 0;
3017} /* xlog_state_sync_all */
3018
3019
3020/*
3021 * Used by code which implements synchronous log forces.
3022 *
3023 * Find in-core log with lsn.
3024 * If it is in the DIRTY state, just return.
3025 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3026 * state and go to sleep or return.
3027 * If it is in any other state, go to sleep or return.
3028 *
3029 * If filesystem activity goes to zero, the iclog will get flushed only by
3030 * bdflush().
3031 */
3032int
3033xlog_state_sync(xlog_t *log,
3034 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003035 uint flags,
3036 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 xlog_in_core_t *iclog;
3039 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003042 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 iclog = log->l_iclog;
3044
3045 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003046 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return XFS_ERROR(EIO);
3048 }
3049
3050 do {
3051 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
3052 iclog = iclog->ic_next;
3053 continue;
3054 }
3055
3056 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003057 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return 0;
3059 }
3060
3061 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3062 /*
3063 * We sleep here if we haven't already slept (e.g.
3064 * this is the first time we've looked at the correct
3065 * iclog buf) and the buffer before us is going to
3066 * be sync'ed. The reason for this is that if we
3067 * are doing sync transactions here, by waiting for
3068 * the previous I/O to complete, we can allow a few
3069 * more transactions into this iclog before we close
3070 * it down.
3071 *
3072 * Otherwise, we mark the buffer WANT_SYNC, and bump
3073 * up the refcnt so we can release the log (which drops
3074 * the ref count). The state switch keeps new transaction
3075 * commits from using this buffer. When the current commits
3076 * finish writing into the buffer, the refcount will drop to
3077 * zero and the buffer will go out then.
3078 */
3079 if (!already_slept &&
3080 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3081 XLOG_STATE_SYNCING))) {
3082 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3083 XFS_STATS_INC(xs_log_force_sleep);
3084 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3085 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003086 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 already_slept = 1;
3088 goto try_again;
3089 } else {
3090 iclog->ic_refcnt++;
3091 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003092 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (xlog_state_release_iclog(log, iclog))
3094 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003095 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003096 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 }
3099
3100 if ((flags & XFS_LOG_SYNC) && /* sleep */
3101 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3102
3103 /*
3104 * Don't wait on the forcesema if we know that we've
3105 * gotten a log write error.
3106 */
3107 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003108 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return XFS_ERROR(EIO);
3110 }
3111 XFS_STATS_INC(xs_log_force_sleep);
3112 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3113 /*
3114 * No need to grab the log lock here since we're
3115 * only deciding whether or not to return EIO
3116 * and the memory read should be atomic.
3117 */
3118 if (iclog->ic_state & XLOG_STATE_IOERROR)
3119 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003120 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003122 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 }
3124 return 0;
3125
3126 } while (iclog != log->l_iclog);
3127
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003128 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130} /* xlog_state_sync */
3131
3132
3133/*
3134 * Called when we want to mark the current iclog as being ready to sync to
3135 * disk.
3136 */
3137void
3138xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3139{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003140 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3143 xlog_state_switch_iclogs(log, iclog, 0);
3144 } else {
3145 ASSERT(iclog->ic_state &
3146 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3147 }
3148
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003149 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150} /* xlog_state_want_sync */
3151
3152
3153
3154/*****************************************************************************
3155 *
3156 * TICKET functions
3157 *
3158 *****************************************************************************
3159 */
3160
3161/*
3162 * Algorithm doesn't take into account page size. ;-(
3163 */
3164STATIC void
3165xlog_state_ticket_alloc(xlog_t *log)
3166{
3167 xlog_ticket_t *t_list;
3168 xlog_ticket_t *next;
3169 xfs_caddr_t buf;
3170 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 /*
3173 * The kmem_zalloc may sleep, so we shouldn't be holding the
3174 * global lock. XXXmiken: may want to use zone allocator.
3175 */
3176 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3177
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003178 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3181 t_list = (xlog_ticket_t *)buf;
3182 t_list->t_next = log->l_unmount_free;
3183 log->l_unmount_free = t_list++;
3184 log->l_ticket_cnt++;
3185 log->l_ticket_tcnt++;
3186
3187 /* Next ticket becomes first ticket attached to ticket free list */
3188 if (log->l_freelist != NULL) {
3189 ASSERT(log->l_tail != NULL);
3190 log->l_tail->t_next = t_list;
3191 } else {
3192 log->l_freelist = t_list;
3193 }
3194 log->l_ticket_cnt++;
3195 log->l_ticket_tcnt++;
3196
3197 /* Cycle through rest of alloc'ed memory, building up free Q */
3198 for ( ; i > 0; i--) {
3199 next = t_list + 1;
3200 t_list->t_next = next;
3201 t_list = next;
3202 log->l_ticket_cnt++;
3203 log->l_ticket_tcnt++;
3204 }
3205 t_list->t_next = NULL;
3206 log->l_tail = t_list;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003207 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208} /* xlog_state_ticket_alloc */
3209
3210
3211/*
3212 * Put ticket into free list
3213 *
3214 * Assumption: log lock is held around this call.
3215 */
3216STATIC void
3217xlog_ticket_put(xlog_t *log,
3218 xlog_ticket_t *ticket)
3219{
3220 sv_destroy(&ticket->t_sema);
3221
3222 /*
3223 * Don't think caching will make that much difference. It's
3224 * more important to make debug easier.
3225 */
3226#if 0
3227 /* real code will want to use LIFO for caching */
3228 ticket->t_next = log->l_freelist;
3229 log->l_freelist = ticket;
3230 /* no need to clear fields */
3231#else
3232 /* When we debug, it is easier if tickets are cycled */
3233 ticket->t_next = NULL;
Christoph Hellwig4b809162007-08-16 15:37:36 +10003234 if (log->l_tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 log->l_tail->t_next = ticket;
3236 } else {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003237 ASSERT(log->l_freelist == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 log->l_freelist = ticket;
3239 }
3240 log->l_tail = ticket;
3241#endif /* DEBUG */
3242 log->l_ticket_cnt++;
3243} /* xlog_ticket_put */
3244
3245
3246/*
3247 * Grab ticket off freelist or allocation some more
3248 */
3249xlog_ticket_t *
3250xlog_ticket_get(xlog_t *log,
3251 int unit_bytes,
3252 int cnt,
3253 char client,
3254 uint xflags)
3255{
3256 xlog_ticket_t *tic;
3257 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 alloc:
3260 if (log->l_freelist == NULL)
3261 xlog_state_ticket_alloc(log); /* potentially sleep */
3262
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003263 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (log->l_freelist == NULL) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003265 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 goto alloc;
3267 }
3268 tic = log->l_freelist;
3269 log->l_freelist = tic->t_next;
3270 if (log->l_freelist == NULL)
3271 log->l_tail = NULL;
3272 log->l_ticket_cnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003273 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 /*
3276 * Permanent reservations have up to 'cnt'-1 active log operations
3277 * in the log. A unit in this case is the amount of space for one
3278 * of these log operations. Normal reservations have a cnt of 1
3279 * and their unit amount is the total amount of space required.
3280 *
3281 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003282 * which occupy space in the on-disk log.
3283 *
3284 * Normal form of a transaction is:
3285 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3286 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3287 *
3288 * We need to account for all the leadup data and trailer data
3289 * around the transaction data.
3290 * And then we need to account for the worst case in terms of using
3291 * more space.
3292 * The worst case will happen if:
3293 * - the placement of the transaction happens to be such that the
3294 * roundoff is at its maximum
3295 * - the transaction data is synced before the commit record is synced
3296 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3297 * Therefore the commit record is in its own Log Record.
3298 * This can happen as the commit record is called with its
3299 * own region to xlog_write().
3300 * This then means that in the worst case, roundoff can happen for
3301 * the commit-rec as well.
3302 * The commit-rec is smaller than padding in this scenario and so it is
3303 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 */
3305
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003306 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003308 unit_bytes += sizeof(xfs_trans_header_t);
3309
3310 /* for start-rec */
3311 unit_bytes += sizeof(xlog_op_header_t);
3312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /* for LR headers */
3314 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3315 unit_bytes += log->l_iclog_hsize * num_headers;
3316
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003317 /* for commit-rec LR header - note: padding will subsume the ophdr */
3318 unit_bytes += log->l_iclog_hsize;
3319
3320 /* for split-recs - ophdrs added when data split over LRs */
3321 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3322
3323 /* for roundoff padding for transaction data and one for commit record */
3324 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3325 log->l_mp->m_sb.sb_logsunit > 1) {
3326 /* log su roundoff */
3327 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3328 } else {
3329 /* BB roundoff */
3330 unit_bytes += 2*BBSIZE;
3331 }
3332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 tic->t_unit_res = unit_bytes;
3334 tic->t_curr_res = unit_bytes;
3335 tic->t_cnt = cnt;
3336 tic->t_ocnt = cnt;
3337 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3338 tic->t_clientid = client;
3339 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003340 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 if (xflags & XFS_LOG_PERM_RESERV)
3342 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3343 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3344
Christoph Hellwig0adba532007-08-30 17:21:46 +10003345 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return tic;
3348} /* xlog_ticket_get */
3349
3350
3351/******************************************************************************
3352 *
3353 * Log debug routines
3354 *
3355 ******************************************************************************
3356 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003357#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358/*
3359 * Make sure that the destination ptr is within the valid data region of
3360 * one of the iclogs. This uses backup pointers stored in a different
3361 * part of the log in case we trash the log structure.
3362 */
3363void
3364xlog_verify_dest_ptr(xlog_t *log,
3365 __psint_t ptr)
3366{
3367 int i;
3368 int good_ptr = 0;
3369
3370 for (i=0; i < log->l_iclog_bufs; i++) {
3371 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3372 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3373 good_ptr++;
3374 }
3375 if (! good_ptr)
3376 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3377} /* xlog_verify_dest_ptr */
3378
3379STATIC void
3380xlog_verify_grant_head(xlog_t *log, int equals)
3381{
3382 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3383 if (equals)
3384 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3385 else
3386 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3387 } else {
3388 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3389 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3390 }
3391} /* xlog_verify_grant_head */
3392
3393/* check if it will fit */
3394STATIC void
3395xlog_verify_tail_lsn(xlog_t *log,
3396 xlog_in_core_t *iclog,
3397 xfs_lsn_t tail_lsn)
3398{
3399 int blocks;
3400
3401 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3402 blocks =
3403 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3404 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3405 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3406 } else {
3407 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3408
3409 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3410 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3411
3412 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3413 if (blocks < BTOBB(iclog->ic_offset) + 1)
3414 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3415 }
3416} /* xlog_verify_tail_lsn */
3417
3418/*
3419 * Perform a number of checks on the iclog before writing to disk.
3420 *
3421 * 1. Make sure the iclogs are still circular
3422 * 2. Make sure we have a good magic number
3423 * 3. Make sure we don't have magic numbers in the data
3424 * 4. Check fields of each log operation header for:
3425 * A. Valid client identifier
3426 * B. tid ptr value falls in valid ptr space (user space code)
3427 * C. Length in log record header is correct according to the
3428 * individual operation headers within record.
3429 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3430 * log, check the preceding blocks of the physical log to make sure all
3431 * the cycle numbers agree with the current cycle number.
3432 */
3433STATIC void
3434xlog_verify_iclog(xlog_t *log,
3435 xlog_in_core_t *iclog,
3436 int count,
3437 boolean_t syncing)
3438{
3439 xlog_op_header_t *ophead;
3440 xlog_in_core_t *icptr;
3441 xlog_in_core_2_t *xhdr;
3442 xfs_caddr_t ptr;
3443 xfs_caddr_t base_ptr;
3444 __psint_t field_offset;
3445 __uint8_t clientid;
3446 int len, i, j, k, op_len;
3447 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003450 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 icptr = log->l_iclog;
3452 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003453 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 xlog_panic("xlog_verify_iclog: invalid ptr");
3455 icptr = icptr->ic_next;
3456 }
3457 if (icptr != log->l_iclog)
3458 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003459 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 /* check log magic numbers */
3462 ptr = (xfs_caddr_t) &(iclog->ic_header);
3463 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3464 xlog_panic("xlog_verify_iclog: invalid magic num");
3465
3466 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3467 ptr += BBSIZE) {
3468 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3469 xlog_panic("xlog_verify_iclog: unexpected magic num");
3470 }
3471
3472 /* check fields */
3473 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3474 ptr = iclog->ic_datap;
3475 base_ptr = ptr;
3476 ophead = (xlog_op_header_t *)ptr;
3477 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3478 for (i = 0; i < len; i++) {
3479 ophead = (xlog_op_header_t *)ptr;
3480
3481 /* clientid is only 1 byte */
3482 field_offset = (__psint_t)
3483 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3484 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3485 clientid = ophead->oh_clientid;
3486 } else {
3487 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3488 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3489 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3490 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3491 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3492 } else {
3493 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3494 }
3495 }
3496 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003497 cmn_err(CE_WARN, "xlog_verify_iclog: "
3498 "invalid clientid %d op 0x%p offset 0x%lx",
3499 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
3501 /* check length */
3502 field_offset = (__psint_t)
3503 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3504 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3505 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3506 } else {
3507 idx = BTOBBT((__psint_t)&ophead->oh_len -
3508 (__psint_t)iclog->ic_datap);
3509 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3510 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3511 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3512 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3513 } else {
3514 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3515 }
3516 }
3517 ptr += sizeof(xlog_op_header_t) + op_len;
3518 }
3519} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003523 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 */
3525STATIC int
3526xlog_state_ioerror(
3527 xlog_t *log)
3528{
3529 xlog_in_core_t *iclog, *ic;
3530
3531 iclog = log->l_iclog;
3532 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3533 /*
3534 * Mark all the incore logs IOERROR.
3535 * From now on, no log flushes will result.
3536 */
3537 ic = iclog;
3538 do {
3539 ic->ic_state = XLOG_STATE_IOERROR;
3540 ic = ic->ic_next;
3541 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
3544 /*
3545 * Return non-zero, if state transition has already happened.
3546 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003547 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
3550/*
3551 * This is called from xfs_force_shutdown, when we're forcibly
3552 * shutting down the filesystem, typically because of an IO error.
3553 * Our main objectives here are to make sure that:
3554 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3555 * parties to find out, 'atomically'.
3556 * b. those who're sleeping on log reservations, pinned objects and
3557 * other resources get woken up, and be told the bad news.
3558 * c. nothing new gets queued up after (a) and (b) are done.
3559 * d. if !logerror, flush the iclogs to disk, then seal them off
3560 * for business.
3561 */
3562int
3563xfs_log_force_umount(
3564 struct xfs_mount *mp,
3565 int logerror)
3566{
3567 xlog_ticket_t *tic;
3568 xlog_t *log;
3569 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003570 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 log = mp->m_log;
3573
3574 /*
3575 * If this happens during log recovery, don't worry about
3576 * locking; the log isn't open for business yet.
3577 */
3578 if (!log ||
3579 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3580 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3581 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
3584
3585 /*
3586 * Somebody could've already done the hard work for us.
3587 * No need to get locks for this.
3588 */
3589 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3590 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003591 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
3593 retval = 0;
3594 /*
3595 * We must hold both the GRANT lock and the LOG lock,
3596 * before we mark the filesystem SHUTDOWN and wake
3597 * everybody up to tell the bad news.
3598 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003599 spin_lock(&log->l_grant_lock);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003600 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3602 XFS_BUF_DONE(mp->m_sb_bp);
3603 /*
3604 * This flag is sort of redundant because of the mount flag, but
3605 * it's good to maintain the separation between the log and the rest
3606 * of XFS.
3607 */
3608 log->l_flags |= XLOG_IO_ERROR;
3609
3610 /*
3611 * If we hit a log error, we want to mark all the iclogs IOERROR
3612 * while we're still holding the loglock.
3613 */
3614 if (logerror)
3615 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003616 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
3618 /*
3619 * We don't want anybody waiting for log reservations
3620 * after this. That means we have to wake up everybody
3621 * queued up on reserve_headq as well as write_headq.
3622 * In addition, we make sure in xlog_{re}grant_log_space
3623 * that we don't enqueue anything once the SHUTDOWN flag
3624 * is set, and this action is protected by the GRANTLOCK.
3625 */
3626 if ((tic = log->l_reserve_headq)) {
3627 do {
3628 sv_signal(&tic->t_sema);
3629 tic = tic->t_next;
3630 } while (tic != log->l_reserve_headq);
3631 }
3632
3633 if ((tic = log->l_write_headq)) {
3634 do {
3635 sv_signal(&tic->t_sema);
3636 tic = tic->t_next;
3637 } while (tic != log->l_write_headq);
3638 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003639 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3642 ASSERT(!logerror);
3643 /*
3644 * Force the incore logs to disk before shutting the
3645 * log down completely.
3646 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003647 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003648 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003650 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 }
3652 /*
3653 * Wake up everybody waiting on xfs_log_force.
3654 * Callback all log item committed functions as if the
3655 * log writes were completed.
3656 */
3657 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3658
3659#ifdef XFSERRORDEBUG
3660 {
3661 xlog_in_core_t *iclog;
3662
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003663 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 iclog = log->l_iclog;
3665 do {
3666 ASSERT(iclog->ic_callback == 0);
3667 iclog = iclog->ic_next;
3668 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003669 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
3671#endif
3672 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003673 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003676STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677xlog_iclogs_empty(xlog_t *log)
3678{
3679 xlog_in_core_t *iclog;
3680
3681 iclog = log->l_iclog;
3682 do {
3683 /* endianness does not matter here, zero is zero in
3684 * any language.
3685 */
3686 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 iclog = iclog->ic_next;
3689 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003690 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}