blob: 2e35077ff6b2a0700bbc19a6997f87cc12f369b8 [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
David Chinner249a8c12008-02-05 12:13:32 +1100501xfs_log_mount(
502 xfs_mount_t *mp,
503 xfs_buftarg_t *log_target,
504 xfs_daddr_t blk_offset,
505 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
David Chinner249a8c12008-02-05 12:13:32 +1100507 int error;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
510 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
511 else {
512 cmn_err(CE_NOTE,
513 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
514 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000515 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
David Chinner249a8c12008-02-05 12:13:32 +1100521 * Initialize the AIL now we have a log.
522 */
523 spin_lock_init(&mp->m_ail_lock);
524 error = xfs_trans_ail_init(mp);
525 if (error) {
526 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
527 goto error;
528 }
529
530 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * skip log recovery on a norecovery mount. pretend it all
532 * just worked.
533 */
534 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100535 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000538 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Eric Sandeen65be6052006-01-11 15:34:19 +1100540 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000543 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (error) {
545 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
David Chinner249a8c12008-02-05 12:13:32 +1100546 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 }
549
550 /* Normal transactions can now occur */
551 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
552
553 /* End mounting message in xfs_log_mount_finish */
554 return 0;
David Chinner249a8c12008-02-05 12:13:32 +1100555error:
556 xfs_log_unmount_dealloc(mp);
557 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558} /* xfs_log_mount */
559
560/*
561 * Finish the recovery of the file system. This is separate from
562 * the xfs_log_mount() call, because it depends on the code in
563 * xfs_mountfs() to read in the root and real-time bitmap inodes
564 * between calling xfs_log_mount() and here.
565 *
566 * mp - ubiquitous xfs mount point structure
567 */
568int
569xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
570{
571 int error;
572
573 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
574 error = xlog_recover_finish(mp->m_log, mfsi_flags);
575 else {
576 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000577 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 return error;
581}
582
583/*
584 * Unmount processing for the log.
585 */
586int
587xfs_log_unmount(xfs_mount_t *mp)
588{
589 int error;
590
591 error = xfs_log_unmount_write(mp);
592 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100593 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596/*
597 * Final log writes as part of unmount.
598 *
599 * Mark the filesystem clean as unmount happens. Note that during relocation
600 * this routine needs to be executed as part of source-bag while the
601 * deallocation must not be done until source-end.
602 */
603
604/*
605 * Unmount record used to have a string "Unmount filesystem--" in the
606 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
607 * We just write the magic number now since that particular field isn't
608 * currently architecture converted and "nUmount" is a bit foo.
609 * As far as I know, there weren't any dependencies on the old behaviour.
610 */
611
612int
613xfs_log_unmount_write(xfs_mount_t *mp)
614{
615 xlog_t *log = mp->m_log;
616 xlog_in_core_t *iclog;
617#ifdef DEBUG
618 xlog_in_core_t *first_iclog;
619#endif
620 xfs_log_iovec_t reg[1];
621 xfs_log_ticket_t tic = NULL;
622 xfs_lsn_t lsn;
623 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* the data section must be 32 bit size aligned */
626 struct {
627 __uint16_t magic;
628 __uint16_t pad1;
629 __uint32_t pad2; /* may as well make it 64 bits */
630 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Don't write out unmount record on read-only mounts.
634 * Or, if we are doing a forced umount (typically because of IO errors).
635 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000636 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638
639 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
640
641#ifdef DEBUG
642 first_iclog = iclog = log->l_iclog;
643 do {
644 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
645 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
646 ASSERT(iclog->ic_offset == 0);
647 }
648 iclog = iclog->ic_next;
649 } while (iclog != first_iclog);
650#endif
651 if (! (XLOG_FORCED_SHUTDOWN(log))) {
652 reg[0].i_addr = (void*)&magic;
653 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000654 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Tim Shimmin955e47a2006-09-28 11:04:16 +1000656 error = xfs_log_reserve(mp, 600, 1, &tic,
657 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!error) {
659 /* remove inited flag */
660 ((xlog_ticket_t *)tic)->t_flags = 0;
661 error = xlog_write(mp, reg, 1, tic, &lsn,
662 NULL, XLOG_UNMOUNT_TRANS);
663 /*
664 * At this point, we're umounting anyway,
665 * so there's no point in transitioning log state
666 * to IOERROR. Just continue...
667 */
668 }
669
670 if (error) {
671 xfs_fs_cmn_err(CE_ALERT, mp,
672 "xfs_log_unmount: unmount record failed");
673 }
674
675
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000676 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 iclog = log->l_iclog;
678 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000679 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xlog_state_want_sync(log, iclog);
681 (void) xlog_state_release_iclog(log, iclog);
682
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000683 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
685 iclog->ic_state == XLOG_STATE_DIRTY)) {
686 if (!XLOG_FORCED_SHUTDOWN(log)) {
687 sv_wait(&iclog->ic_forcesema, PMEM,
688 &log->l_icloglock, s);
689 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000690 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000693 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000695 if (tic) {
696 xlog_trace_loggrant(log, tic, "unmount rec");
697 xlog_ungrant_log_space(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 xlog_state_put_ticket(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
701 /*
702 * We're already in forced_shutdown mode, couldn't
703 * even attempt to write out the unmount transaction.
704 *
705 * Go through the motions of sync'ing and releasing
706 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000707 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * be in progress. Do this as a separate section of
709 * code so we'll know if we ever get stuck here that
710 * we're in this odd situation of trying to unmount
711 * a file system that went into forced_shutdown as
712 * the result of an unmount..
713 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000714 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 iclog = log->l_iclog;
716 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000717 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 xlog_state_want_sync(log, iclog);
720 (void) xlog_state_release_iclog(log, iclog);
721
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000722 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
725 || iclog->ic_state == XLOG_STATE_DIRTY
726 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
727
728 sv_wait(&iclog->ic_forcesema, PMEM,
729 &log->l_icloglock, s);
730 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000731 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 }
734
735 return 0;
736} /* xfs_log_unmount_write */
737
738/*
739 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100740 *
741 * We need to stop the aild from running before we destroy
742 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744void
745xfs_log_unmount_dealloc(xfs_mount_t *mp)
746{
David Chinner249a8c12008-02-05 12:13:32 +1100747 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000748 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
752 * Write region vectors to log. The write happens using the space reservation
753 * of the ticket (tic). It is not a requirement that all writes for a given
754 * transaction occur with one call to xfs_log_write().
755 */
756int
757xfs_log_write(xfs_mount_t * mp,
758 xfs_log_iovec_t reg[],
759 int nentries,
760 xfs_log_ticket_t tic,
761 xfs_lsn_t *start_lsn)
762{
763 int error;
764 xlog_t *log = mp->m_log;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (XLOG_FORCED_SHUTDOWN(log))
767 return XFS_ERROR(EIO);
768
769 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000770 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100772 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773} /* xfs_log_write */
774
775
776void
777xfs_log_move_tail(xfs_mount_t *mp,
778 xfs_lsn_t tail_lsn)
779{
780 xlog_ticket_t *tic;
781 xlog_t *log = mp->m_log;
782 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (XLOG_FORCED_SHUTDOWN(log))
785 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (tail_lsn == 0) {
788 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000789 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000791 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000794 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
797 * tail_lsn.
798 */
799 if (tail_lsn != 1) {
800 log->l_tail_lsn = tail_lsn;
801 }
802
803 if ((tic = log->l_write_headq)) {
804#ifdef DEBUG
805 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
806 panic("Recovery problem");
807#endif
808 cycle = log->l_grant_write_cycle;
809 bytes = log->l_grant_write_bytes;
810 free_bytes = xlog_space_left(log, cycle, bytes);
811 do {
812 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
813
814 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
815 break;
816 tail_lsn = 0;
817 free_bytes -= tic->t_unit_res;
818 sv_signal(&tic->t_sema);
819 tic = tic->t_next;
820 } while (tic != log->l_write_headq);
821 }
822 if ((tic = log->l_reserve_headq)) {
823#ifdef DEBUG
824 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
825 panic("Recovery problem");
826#endif
827 cycle = log->l_grant_reserve_cycle;
828 bytes = log->l_grant_reserve_bytes;
829 free_bytes = xlog_space_left(log, cycle, bytes);
830 do {
831 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
832 need_bytes = tic->t_unit_res*tic->t_cnt;
833 else
834 need_bytes = tic->t_unit_res;
835 if (free_bytes < need_bytes && tail_lsn != 1)
836 break;
837 tail_lsn = 0;
838 free_bytes -= need_bytes;
839 sv_signal(&tic->t_sema);
840 tic = tic->t_next;
841 } while (tic != log->l_reserve_headq);
842 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000843 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844} /* xfs_log_move_tail */
845
846/*
847 * Determine if we have a transaction that has gone to disk
848 * that needs to be covered. Log activity needs to be idle (no AIL and
849 * nothing in the iclogs). And, we need to be in the right state indicating
850 * something has gone out.
851 */
852int
853xfs_log_need_covered(xfs_mount_t *mp)
854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int needed = 0, gen;
856 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David Chinner92821e22007-05-24 15:26:31 +1000858 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 0;
860
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000861 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
863 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
864 && !xfs_trans_first_ail(mp, &gen)
865 && xlog_iclogs_empty(log)) {
866 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
867 log->l_covered_state = XLOG_STATE_COVER_DONE;
868 else {
869 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
870 log->l_covered_state = XLOG_STATE_COVER_DONE2;
871 }
872 needed = 1;
873 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000874 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100875 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/******************************************************************************
879 *
880 * local routines
881 *
882 ******************************************************************************
883 */
884
885/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
886 * The log manager must keep track of the last LR which was committed
887 * to disk. The lsn of this LR will become the new tail_lsn whenever
888 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
889 * the situation where stuff could be written into the log but nothing
890 * was ever in the AIL when asked. Eventually, we panic since the
891 * tail hits the head.
892 *
893 * We may be holding the log iclog lock upon entering this routine.
894 */
895xfs_lsn_t
896xlog_assign_tail_lsn(xfs_mount_t *mp)
897{
898 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 xlog_t *log = mp->m_log;
900
901 tail_lsn = xfs_trans_tail_ail(mp);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000902 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (tail_lsn != 0) {
904 log->l_tail_lsn = tail_lsn;
905 } else {
906 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
907 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000908 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 return tail_lsn;
911} /* xlog_assign_tail_lsn */
912
913
914/*
915 * Return the space in the log between the tail and the head. The head
916 * is passed in the cycle/bytes formal parms. In the special case where
917 * the reserve head has wrapped passed the tail, this calculation is no
918 * longer valid. In this case, just return 0 which means there is no space
919 * in the log. This works for all places where this function is called
920 * with the reserve head. Of course, if the write head were to ever
921 * wrap the tail, we should blow up. Rather than catch this case here,
922 * we depend on other ASSERTions in other parts of the code. XXXmiken
923 *
924 * This code also handles the case where the reservation head is behind
925 * the tail. The details of this case are described below, but the end
926 * result is that we return the size of the log as the amount of space left.
927 */
David Chinnera8272ce2007-11-23 16:28:09 +1100928STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929xlog_space_left(xlog_t *log, int cycle, int bytes)
930{
931 int free_bytes;
932 int tail_bytes;
933 int tail_cycle;
934
935 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
936 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
937 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
938 free_bytes = log->l_logsize - (bytes - tail_bytes);
939 } else if ((tail_cycle + 1) < cycle) {
940 return 0;
941 } else if (tail_cycle < cycle) {
942 ASSERT(tail_cycle == (cycle - 1));
943 free_bytes = tail_bytes - bytes;
944 } else {
945 /*
946 * The reservation head is behind the tail.
947 * In this case we just want to return the size of the
948 * log as the amount of space left.
949 */
950 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
951 "xlog_space_left: head behind tail\n"
952 " tail_cycle = %d, tail_bytes = %d\n"
953 " GH cycle = %d, GH bytes = %d",
954 tail_cycle, tail_bytes, cycle, bytes);
955 ASSERT(0);
956 free_bytes = log->l_logsize;
957 }
958 return free_bytes;
959} /* xlog_space_left */
960
961
962/*
963 * Log function which is called when an io completes.
964 *
965 * The log manager needs its own routine, in order to control what
966 * happens with the buffer after the write completes.
967 */
968void
969xlog_iodone(xfs_buf_t *bp)
970{
971 xlog_in_core_t *iclog;
972 xlog_t *l;
973 int aborted;
974
975 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
976 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
977 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
978 aborted = 0;
979
980 /*
981 * Some versions of cpp barf on the recursive definition of
982 * ic_log -> hic_fields.ic_log and expand ic_log twice when
983 * it is passed through two macros. Workaround broken cpp.
984 */
985 l = iclog->ic_log;
986
987 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000988 * If the ordered flag has been removed by a lower
989 * layer, it means the underlyin device no longer supports
990 * barrier I/O. Warn loudly and turn off barriers.
991 */
992 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
993 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
994 xfs_fs_cmn_err(CE_WARN, l->l_mp,
995 "xlog_iodone: Barriers are no longer supported"
996 " by device. Disabling barriers\n");
997 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
998 }
999
1000 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * Race to shutdown the filesystem if we see an error.
1002 */
1003 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1004 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1005 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1006 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001007 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * This flag will be propagated to the trans-committed
1010 * callback routines to let them know that the log-commit
1011 * didn't succeed.
1012 */
1013 aborted = XFS_LI_ABORTED;
1014 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1015 aborted = XFS_LI_ABORTED;
1016 }
David Chinner3db296f2007-05-14 18:24:16 +10001017
1018 /* log I/O is always issued ASYNC */
1019 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001021 /*
1022 * do not reference the buffer (bp) here as we could race
1023 * with it being freed after writing the unmount record to the
1024 * log.
1025 */
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027} /* xlog_iodone */
1028
1029/*
1030 * The bdstrat callback function for log bufs. This gives us a central
1031 * place to trap bufs in case we get hit by a log I/O error and need to
1032 * shutdown. Actually, in practice, even when we didn't get a log error,
1033 * we transition the iclogs to IOERROR state *after* flushing all existing
1034 * iclogs to disk. This is because we don't want anymore new transactions to be
1035 * started or completed afterwards.
1036 */
1037STATIC int
1038xlog_bdstrat_cb(struct xfs_buf *bp)
1039{
1040 xlog_in_core_t *iclog;
1041
1042 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1043
1044 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1045 /* note for irix bstrat will need struct bdevsw passed
1046 * Fix the following macro if the code ever is merged
1047 */
1048 XFS_bdstrat(bp);
1049 return 0;
1050 }
1051
1052 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1053 XFS_BUF_ERROR(bp, EIO);
1054 XFS_BUF_STALE(bp);
1055 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001056 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058
1059}
1060
1061/*
1062 * Return size of each in-core log record buffer.
1063 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001064 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 *
1066 * If the filesystem blocksize is too large, we may need to choose a
1067 * larger size since the directory code currently logs entire blocks.
1068 */
1069
1070STATIC void
1071xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1072 xlog_t *log)
1073{
1074 int size;
1075 int xhdrs;
1076
Eric Sandeen1cb51252007-08-16 16:24:43 +10001077 if (mp->m_logbufs <= 0)
1078 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1079 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001080 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
1083 * Buffer size passed in from mount system call.
1084 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001085 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 size = log->l_iclog_size = mp->m_logbsize;
1087 log->l_iclog_size_log = 0;
1088 while (size != 1) {
1089 log->l_iclog_size_log++;
1090 size >>= 1;
1091 }
1092
Eric Sandeen62118702008-03-06 13:44:28 +11001093 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* # headers = size / 32K
1095 * one header holds cycles from 32K of data
1096 */
1097
1098 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1099 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1100 xhdrs++;
1101 log->l_iclog_hsize = xhdrs << BBSHIFT;
1102 log->l_iclog_heads = xhdrs;
1103 } else {
1104 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1105 log->l_iclog_hsize = BBSIZE;
1106 log->l_iclog_heads = 1;
1107 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001108 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Eric Sandeen1cb51252007-08-16 16:24:43 +10001111 /* All machines use 32KB buffers by default. */
1112 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1113 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /* the default log size is 16k or 32k which is one header sector */
1116 log->l_iclog_hsize = BBSIZE;
1117 log->l_iclog_heads = 1;
1118
1119 /*
1120 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1121 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1122 */
1123 if (mp->m_sb.sb_blocksize >= 16*1024) {
1124 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1125 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001126 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 switch (mp->m_sb.sb_blocksize) {
1128 case 16*1024: /* 16 KB */
1129 log->l_iclog_bufs = 3;
1130 break;
1131 case 32*1024: /* 32 KB */
1132 log->l_iclog_bufs = 4;
1133 break;
1134 case 64*1024: /* 64 KB */
1135 log->l_iclog_bufs = 8;
1136 break;
1137 default:
1138 xlog_panic("XFS: Invalid blocksize");
1139 break;
1140 }
1141 }
1142 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001143
1144done: /* are we being asked to make the sizes selected above visible? */
1145 if (mp->m_logbufs == 0)
1146 mp->m_logbufs = log->l_iclog_bufs;
1147 if (mp->m_logbsize == 0)
1148 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149} /* xlog_get_iclog_buffer_size */
1150
1151
1152/*
1153 * This routine initializes some of the log structure for a given mount point.
1154 * Its primary purpose is to fill in enough, so recovery can occur. However,
1155 * some other stuff may be filled in too.
1156 */
1157STATIC xlog_t *
1158xlog_alloc_log(xfs_mount_t *mp,
1159 xfs_buftarg_t *log_target,
1160 xfs_daddr_t blk_offset,
1161 int num_bblks)
1162{
1163 xlog_t *log;
1164 xlog_rec_header_t *head;
1165 xlog_in_core_t **iclogp;
1166 xlog_in_core_t *iclog, *prev_iclog=NULL;
1167 xfs_buf_t *bp;
1168 int i;
1169 int iclogsize;
1170
1171 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1172
1173 log->l_mp = mp;
1174 log->l_targ = log_target;
1175 log->l_logsize = BBTOB(num_bblks);
1176 log->l_logBBstart = blk_offset;
1177 log->l_logBBsize = num_bblks;
1178 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1179 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1180
1181 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001182 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1184 log->l_last_sync_lsn = log->l_tail_lsn;
1185 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1186 log->l_grant_reserve_cycle = 1;
1187 log->l_grant_write_cycle = 1;
1188
Eric Sandeen62118702008-03-06 13:44:28 +11001189 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1191 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1192 /* for larger sector sizes, must have v2 or external log */
1193 ASSERT(log->l_sectbb_log == 0 ||
1194 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001195 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1197 }
1198 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1199
1200 xlog_get_iclog_buffer_size(mp, log);
1201
1202 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1203 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1204 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1205 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1206 ASSERT(XFS_BUF_ISBUSY(bp));
1207 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1208 log->l_xbuf = bp;
1209
Eric Sandeen007c61c2007-10-11 17:43:56 +10001210 spin_lock_init(&log->l_icloglock);
1211 spin_lock_init(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 initnsema(&log->l_flushsema, 0, "ic-flush");
1213 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1214
1215 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1216 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1217
1218 iclogp = &log->l_iclog;
1219 /*
1220 * The amount of memory to allocate for the iclog structure is
1221 * rather funky due to the way the structure is defined. It is
1222 * done this way so that we can use different sizes for machines
1223 * with different amounts of memory. See the definition of
1224 * xlog_in_core_t in xfs_log_priv.h for details.
1225 */
1226 iclogsize = log->l_iclog_size;
1227 ASSERT(log->l_iclog_size >= 4096);
1228 for (i=0; i < log->l_iclog_bufs; i++) {
1229 *iclogp = (xlog_in_core_t *)
1230 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1231 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 iclog->ic_prev = prev_iclog;
1233 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001234
1235 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1236 if (!XFS_BUF_CPSEMA(bp))
1237 ASSERT(0);
1238 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1239 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1240 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1241 iclog->ic_bp = bp;
1242 iclog->hic_data = bp->b_addr;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1245
1246 head = &iclog->ic_header;
1247 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001248 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1249 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001250 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001251 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001253 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1258 iclog->ic_state = XLOG_STATE_ACTIVE;
1259 iclog->ic_log = log;
1260 iclog->ic_callback_tail = &(iclog->ic_callback);
1261 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1262
1263 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1264 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1265 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1266 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1267
1268 iclogp = &iclog->ic_next;
1269 }
1270 *iclogp = log->l_iclog; /* complete ring */
1271 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1272
1273 return log;
1274} /* xlog_alloc_log */
1275
1276
1277/*
1278 * Write out the commit record of a transaction associated with the given
1279 * ticket. Return the lsn of the commit record.
1280 */
1281STATIC int
1282xlog_commit_record(xfs_mount_t *mp,
1283 xlog_ticket_t *ticket,
1284 xlog_in_core_t **iclog,
1285 xfs_lsn_t *commitlsnp)
1286{
1287 int error;
1288 xfs_log_iovec_t reg[1];
1289
1290 reg[0].i_addr = NULL;
1291 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001292 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 ASSERT_ALWAYS(iclog);
1295 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1296 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001297 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300} /* xlog_commit_record */
1301
1302
1303/*
1304 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1305 * log space. This code pushes on the lsn which would supposedly free up
1306 * the 25% which we want to leave free. We may need to adopt a policy which
1307 * pushes on an lsn which is further along in the log once we reach the high
1308 * water mark. In this manner, we would be creating a low water mark.
1309 */
David Chinnera8272ce2007-11-23 16:28:09 +11001310STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311xlog_grant_push_ail(xfs_mount_t *mp,
1312 int need_bytes)
1313{
1314 xlog_t *log = mp->m_log; /* pointer to the log */
1315 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1316 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1317 int free_blocks; /* free blocks left to write to */
1318 int free_bytes; /* free bytes left to write to */
1319 int threshold_block; /* block in lsn we'd like to be at */
1320 int threshold_cycle; /* lsn cycle we'd like to be at */
1321 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1324
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001325 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 free_bytes = xlog_space_left(log,
1327 log->l_grant_reserve_cycle,
1328 log->l_grant_reserve_bytes);
1329 tail_lsn = log->l_tail_lsn;
1330 free_blocks = BTOBBT(free_bytes);
1331
1332 /*
1333 * Set the threshold for the minimum number of free blocks in the
1334 * log to the maximum of what the caller needs, one quarter of the
1335 * log, and 256 blocks.
1336 */
1337 free_threshold = BTOBB(need_bytes);
1338 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1339 free_threshold = MAX(free_threshold, 256);
1340 if (free_blocks < free_threshold) {
1341 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1342 threshold_cycle = CYCLE_LSN(tail_lsn);
1343 if (threshold_block >= log->l_logBBsize) {
1344 threshold_block -= log->l_logBBsize;
1345 threshold_cycle += 1;
1346 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001347 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /* Don't pass in an lsn greater than the lsn of the last
1350 * log record known to be on disk.
1351 */
1352 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1353 threshold_lsn = log->l_last_sync_lsn;
1354 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001355 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
1358 * Get the transaction layer to kick the dirty buffers out to
1359 * disk asynchronously. No point in trying to do this if
1360 * the filesystem is shutting down.
1361 */
1362 if (threshold_lsn &&
1363 !XLOG_FORCED_SHUTDOWN(log))
1364 xfs_trans_push_ail(mp, threshold_lsn);
1365} /* xlog_grant_push_ail */
1366
1367
1368/*
1369 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1370 * fashion. Previously, we should have moved the current iclog
1371 * ptr in the log to point to the next available iclog. This allows further
1372 * write to continue while this code syncs out an iclog ready to go.
1373 * Before an in-core log can be written out, the data section must be scanned
1374 * to save away the 1st word of each BBSIZE block into the header. We replace
1375 * it with the current cycle count. Each BBSIZE block is tagged with the
1376 * cycle count because there in an implicit assumption that drives will
1377 * guarantee that entire 512 byte blocks get written at once. In other words,
1378 * we can't have part of a 512 byte block written and part not written. By
1379 * tagging each block, we will know which blocks are valid when recovering
1380 * after an unclean shutdown.
1381 *
1382 * This routine is single threaded on the iclog. No other thread can be in
1383 * this routine with the same iclog. Changing contents of iclog can there-
1384 * fore be done without grabbing the state machine lock. Updating the global
1385 * log will require grabbing the lock though.
1386 *
1387 * The entire log manager uses a logical block numbering scheme. Only
1388 * log_sync (and then only bwrite()) know about the fact that the log may
1389 * not start with block zero on a given device. The log block start offset
1390 * is added immediately before calling bwrite().
1391 */
1392
David Chinnera8272ce2007-11-23 16:28:09 +11001393STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394xlog_sync(xlog_t *log,
1395 xlog_in_core_t *iclog)
1396{
1397 xfs_caddr_t dptr; /* pointer to byte sized element */
1398 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001399 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 uint count; /* byte count of bwrite */
1401 uint count_init; /* initial count before roundup */
1402 int roundoff; /* roundoff to BB or stripe */
1403 int split = 0; /* split write into two regions */
1404 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001405 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 XFS_STATS_INC(xs_log_writes);
1408 ASSERT(iclog->ic_refcnt == 0);
1409
1410 /* Add for LR header */
1411 count_init = log->l_iclog_hsize + iclog->ic_offset;
1412
1413 /* Round out the log write size */
1414 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1415 /* we have a v2 stripe unit to use */
1416 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1417 } else {
1418 count = BBTOB(BTOBB(count_init));
1419 }
1420 roundoff = count - count_init;
1421 ASSERT(roundoff >= 0);
1422 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1423 roundoff < log->l_mp->m_sb.sb_logsunit)
1424 ||
1425 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1426 roundoff < BBTOB(1)));
1427
1428 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001429 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001430 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001431 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* put cycle number in every block */
1434 xlog_pack_data(log, iclog, roundoff);
1435
1436 /* real byte length */
1437 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001438 iclog->ic_header.h_len =
1439 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001441 iclog->ic_header.h_len =
1442 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Nathan Scottf5faad72006-07-28 17:04:44 +10001445 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1447 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001448 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1451
1452 /* Do we need to split this write into 2 parts? */
1453 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1454 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1455 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1456 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1457 } else {
1458 iclog->ic_bwritecnt = 1;
1459 }
David Chinner511105b2007-05-24 15:21:57 +10001460 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001462 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 XFS_BUF_BUSY(bp);
1464 XFS_BUF_ASYNC(bp);
1465 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001466 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001467 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001469 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001470 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1473 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1474
1475 xlog_verify_iclog(log, iclog, count, B_TRUE);
1476
1477 /* account for log which doesn't start at block #0 */
1478 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1479 /*
1480 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1481 * is shutting down.
1482 */
1483 XFS_BUF_WRITE(bp);
1484
1485 if ((error = XFS_bwrite(bp))) {
1486 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1487 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001488 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001491 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1493 (unsigned long)1);
1494 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1495 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1496 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1497 (__psint_t)count), split);
1498 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001499 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 XFS_BUF_BUSY(bp);
1501 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001502 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1503 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 dptr = XFS_BUF_PTR(bp);
1505 /*
1506 * Bump the cycle numbers at the start of each block
1507 * since this part of the buffer is at the start of
1508 * a new cycle. Watch out for the header magic number
1509 * case, though.
1510 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001511 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001512 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001513 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001514 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dptr += BBSIZE;
1516 }
1517
1518 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1519 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1520
Nathan Scottc41564b2006-03-29 08:55:14 +10001521 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1523 XFS_BUF_WRITE(bp);
1524 if ((error = XFS_bwrite(bp))) {
1525 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1526 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001527 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531} /* xlog_sync */
1532
1533
1534/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001535 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
David Chinnera8272ce2007-11-23 16:28:09 +11001537STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001538xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 xlog_in_core_t *iclog, *next_iclog;
1541 xlog_ticket_t *tic, *next_tic;
1542 int i;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 iclog = log->l_iclog;
1545 for (i=0; i<log->l_iclog_bufs; i++) {
1546 sv_destroy(&iclog->ic_forcesema);
1547 sv_destroy(&iclog->ic_writesema);
1548 xfs_buf_free(iclog->ic_bp);
1549#ifdef XFS_LOG_TRACE
1550 if (iclog->ic_trace != NULL) {
1551 ktrace_free(iclog->ic_trace);
1552 }
1553#endif
1554 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 kmem_free(iclog, sizeof(xlog_in_core_t));
1556 iclog = next_iclog;
1557 }
1558 freesema(&log->l_flushsema);
1559 spinlock_destroy(&log->l_icloglock);
1560 spinlock_destroy(&log->l_grant_lock);
1561
1562 /* XXXsup take a look at this again. */
1563 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1564 !XLOG_FORCED_SHUTDOWN(log)) {
1565 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001566 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 log->l_ticket_cnt, log->l_ticket_tcnt);
1568 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1569
1570 } else {
1571 tic = log->l_unmount_free;
1572 while (tic) {
1573 next_tic = tic->t_next;
Tim Shimmine6a4b372007-11-23 16:30:42 +11001574 kmem_free(tic, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 tic = next_tic;
1576 }
1577 }
1578 xfs_buf_free(log->l_xbuf);
1579#ifdef XFS_LOG_TRACE
1580 if (log->l_trace != NULL) {
1581 ktrace_free(log->l_trace);
1582 }
1583 if (log->l_grant_trace != NULL) {
1584 ktrace_free(log->l_grant_trace);
1585 }
1586#endif
1587 log->l_mp->m_log = NULL;
1588 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001589} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591/*
1592 * Update counters atomically now that memcpy is done.
1593 */
1594/* ARGSUSED */
1595static inline void
1596xlog_state_finish_copy(xlog_t *log,
1597 xlog_in_core_t *iclog,
1598 int record_cnt,
1599 int copy_bytes)
1600{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001601 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001603 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 iclog->ic_offset += copy_bytes;
1605
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001606 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607} /* xlog_state_finish_copy */
1608
1609
1610
1611
1612/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001613 * print out info relating to regions written which consume
1614 * the reservation
1615 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001616STATIC void
1617xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1618{
1619 uint i;
1620 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1621
1622 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1623 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1624 "bformat",
1625 "bchunk",
1626 "efi_format",
1627 "efd_format",
1628 "iformat",
1629 "icore",
1630 "iext",
1631 "ibroot",
1632 "ilocal",
1633 "iattr_ext",
1634 "iattr_broot",
1635 "iattr_local",
1636 "qformat",
1637 "dquot",
1638 "quotaoff",
1639 "LR header",
1640 "unmount",
1641 "commit",
1642 "trans header"
1643 };
1644 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1645 "SETATTR_NOT_SIZE",
1646 "SETATTR_SIZE",
1647 "INACTIVE",
1648 "CREATE",
1649 "CREATE_TRUNC",
1650 "TRUNCATE_FILE",
1651 "REMOVE",
1652 "LINK",
1653 "RENAME",
1654 "MKDIR",
1655 "RMDIR",
1656 "SYMLINK",
1657 "SET_DMATTRS",
1658 "GROWFS",
1659 "STRAT_WRITE",
1660 "DIOSTRAT",
1661 "WRITE_SYNC",
1662 "WRITEID",
1663 "ADDAFORK",
1664 "ATTRINVAL",
1665 "ATRUNCATE",
1666 "ATTR_SET",
1667 "ATTR_RM",
1668 "ATTR_FLAG",
1669 "CLEAR_AGI_BUCKET",
1670 "QM_SBCHANGE",
1671 "DUMMY1",
1672 "DUMMY2",
1673 "QM_QUOTAOFF",
1674 "QM_DQALLOC",
1675 "QM_SETQLIM",
1676 "QM_DQCLUSTER",
1677 "QM_QINOCREATE",
1678 "QM_QUOTAOFF_END",
1679 "SB_UNIT",
1680 "FSYNC_TS",
1681 "GROWFSRT_ALLOC",
1682 "GROWFSRT_ZERO",
1683 "GROWFSRT_FREE",
1684 "SWAPEXT"
1685 };
1686
1687 xfs_fs_cmn_err(CE_WARN, mp,
1688 "xfs_log_write: reservation summary:\n"
1689 " trans type = %s (%u)\n"
1690 " unit res = %d bytes\n"
1691 " current res = %d bytes\n"
1692 " total reg = %u bytes (o/flow = %u bytes)\n"
1693 " ophdrs = %u (ophdr space = %u bytes)\n"
1694 " ophdr + reg = %u bytes\n"
1695 " num regions = %u\n",
1696 ((ticket->t_trans_type <= 0 ||
1697 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1698 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1699 ticket->t_trans_type,
1700 ticket->t_unit_res,
1701 ticket->t_curr_res,
1702 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1703 ticket->t_res_num_ophdrs, ophdr_spc,
1704 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001705 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001706 ticket->t_res_num);
1707
1708 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001709 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001710 cmn_err(CE_WARN,
1711 "region[%u]: %s - %u bytes\n",
1712 i,
1713 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1714 "bad-rtype" : res_type_str[r_type-1]),
1715 ticket->t_res_arr[i].r_len);
1716 }
1717}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001718
1719/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * Write some region out to in-core log
1721 *
1722 * This will be called when writing externally provided regions or when
1723 * writing out a commit record for a given transaction.
1724 *
1725 * General algorithm:
1726 * 1. Find total length of this write. This may include adding to the
1727 * lengths passed in.
1728 * 2. Check whether we violate the tickets reservation.
1729 * 3. While writing to this iclog
1730 * A. Reserve as much space in this iclog as can get
1731 * B. If this is first write, save away start lsn
1732 * C. While writing this region:
1733 * 1. If first write of transaction, write start record
1734 * 2. Write log operation header (header per region)
1735 * 3. Find out if we can fit entire region into this iclog
1736 * 4. Potentially, verify destination memcpy ptr
1737 * 5. Memcpy (partial) region
1738 * 6. If partial copy, release iclog; otherwise, continue
1739 * copying more regions into current iclog
1740 * 4. Mark want sync bit (in simulation mode)
1741 * 5. Release iclog for potential flush to on-disk log.
1742 *
1743 * ERRORS:
1744 * 1. Panic if reservation is overrun. This should never happen since
1745 * reservation amounts are generated internal to the filesystem.
1746 * NOTES:
1747 * 1. Tickets are single threaded data structures.
1748 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1749 * syncing routine. When a single log_write region needs to span
1750 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1751 * on all log operation writes which don't contain the end of the
1752 * region. The XLOG_END_TRANS bit is used for the in-core log
1753 * operation which contains the end of the continued log_write region.
1754 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1755 * we don't really know exactly how much space will be used. As a result,
1756 * we don't update ic_offset until the end when we know exactly how many
1757 * bytes have been written out.
1758 */
David Chinnera8272ce2007-11-23 16:28:09 +11001759STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760xlog_write(xfs_mount_t * mp,
1761 xfs_log_iovec_t reg[],
1762 int nentries,
1763 xfs_log_ticket_t tic,
1764 xfs_lsn_t *start_lsn,
1765 xlog_in_core_t **commit_iclog,
1766 uint flags)
1767{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001768 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001770 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 __psint_t ptr; /* copy address into data region */
1773 int len; /* # xlog_write() bytes 2 still copy */
1774 int index; /* region index currently copying */
1775 int log_offset; /* offset (from 0) into data region */
1776 int start_rec_copy; /* # bytes to copy for start record */
1777 int partial_copy; /* did we split a region? */
1778 int partial_copy_len;/* # bytes copied if split region */
1779 int need_copy; /* # bytes need to memcpy this region */
1780 int copy_len; /* # bytes actually memcpy'ing */
1781 int copy_off; /* # bytes from entry start */
1782 int contwr; /* continued write of in-core log? */
1783 int error;
1784 int record_cnt = 0, data_cnt = 0;
1785
1786 partial_copy_len = partial_copy = 0;
1787
1788 /* Calculate potential maximum space. Each region gets its own
1789 * xlog_op_header_t and may need to be double word aligned.
1790 */
1791 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001792 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001794 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 for (index = 0; index < nentries; index++) {
1798 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001799 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001801 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803 contwr = *start_lsn = 0;
1804
1805 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001806 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#ifdef DEBUG
1808 xlog_panic(
1809 "xfs_log_write: reservation ran out. Need to up reservation");
1810#else
1811 /* Customer configurable panic */
1812 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1813 "xfs_log_write: reservation ran out. Need to up reservation");
1814 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001815 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816#endif
1817 } else
1818 ticket->t_curr_res -= len;
1819
1820 for (index = 0; index < nentries; ) {
1821 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1822 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001823 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 ASSERT(log_offset <= iclog->ic_size - 1);
1826 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1827
1828 /* start_lsn is the first lsn written to. That's all we need. */
1829 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001830 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 /* This loop writes out as many regions as can fit in the amount
1833 * of space which was allocated by xlog_state_get_iclog_space().
1834 */
1835 while (index < nentries) {
1836 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1837 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1838 start_rec_copy = 0;
1839
1840 /* If first write for transaction, insert start record.
1841 * We can't be trying to commit if we are inited. We can't
1842 * have any "partial_copy" if we are inited.
1843 */
1844 if (ticket->t_flags & XLOG_TIC_INITED) {
1845 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001846 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 logop_head->oh_clientid = ticket->t_clientid;
1848 logop_head->oh_len = 0;
1849 logop_head->oh_flags = XLOG_START_TRANS;
1850 logop_head->oh_res2 = 0;
1851 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1852 record_cnt++;
1853
1854 start_rec_copy = sizeof(xlog_op_header_t);
1855 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1856 }
1857
1858 /* Copy log operation header directly into data section */
1859 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001860 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 logop_head->oh_clientid = ticket->t_clientid;
1862 logop_head->oh_res2 = 0;
1863
1864 /* header copied directly */
1865 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1866
1867 /* are we copying a commit or unmount record? */
1868 logop_head->oh_flags = flags;
1869
1870 /*
1871 * We've seen logs corrupted with bad transaction client
1872 * ids. This makes sure that XFS doesn't generate them on.
1873 * Turn this into an EIO and shut down the filesystem.
1874 */
1875 switch (logop_head->oh_clientid) {
1876 case XFS_TRANSACTION:
1877 case XFS_VOLUME:
1878 case XFS_LOG:
1879 break;
1880 default:
1881 xfs_fs_cmn_err(CE_WARN, mp,
1882 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1883 logop_head->oh_clientid, tic);
1884 return XFS_ERROR(EIO);
1885 }
1886
1887 /* Partial write last time? => (partial_copy != 0)
1888 * need_copy is the amount we'd like to copy if everything could
1889 * fit in the current memcpy.
1890 */
1891 need_copy = reg[index].i_len - partial_copy_len;
1892
1893 copy_off = partial_copy_len;
1894 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001895 copy_len = need_copy;
1896 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (partial_copy)
1898 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1899 partial_copy_len = partial_copy = 0;
1900 } else { /* partial write */
1901 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001902 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1904 if (partial_copy)
1905 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1906 partial_copy_len += copy_len;
1907 partial_copy++;
1908 len += sizeof(xlog_op_header_t); /* from splitting of region */
1909 /* account for new log op header */
1910 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001911 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 xlog_verify_dest_ptr(log, ptr);
1914
1915 /* copy region */
1916 ASSERT(copy_len >= 0);
1917 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1918 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1919
1920 /* make copy_len total bytes copied, including headers */
1921 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1922 record_cnt++;
1923 data_cnt += contwr ? copy_len : 0;
1924 if (partial_copy) { /* copied partial region */
1925 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1926 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1927 record_cnt = data_cnt = 0;
1928 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001929 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break; /* don't increment index */
1931 } else { /* copied entire region */
1932 index++;
1933 partial_copy_len = partial_copy = 0;
1934
1935 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1936 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1937 record_cnt = data_cnt = 0;
1938 xlog_state_want_sync(log, iclog);
1939 if (commit_iclog) {
1940 ASSERT(flags & XLOG_COMMIT_TRANS);
1941 *commit_iclog = iclog;
1942 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (index == nentries)
1945 return 0; /* we are done */
1946 else
1947 break;
1948 }
1949 } /* if (partial_copy) */
1950 } /* while (index < nentries) */
1951 } /* for (index = 0; index < nentries; ) */
1952 ASSERT(len == 0);
1953
1954 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1955 if (commit_iclog) {
1956 ASSERT(flags & XLOG_COMMIT_TRANS);
1957 *commit_iclog = iclog;
1958 return 0;
1959 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001960 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961} /* xlog_write */
1962
1963
1964/*****************************************************************************
1965 *
1966 * State Machine functions
1967 *
1968 *****************************************************************************
1969 */
1970
1971/* Clean iclogs starting from the head. This ordering must be
1972 * maintained, so an iclog doesn't become ACTIVE beyond one that
1973 * is SYNCING. This is also required to maintain the notion that we use
1974 * a counting semaphore to hold off would be writers to the log when every
1975 * iclog is trying to sync to disk.
1976 *
1977 * State Change: DIRTY -> ACTIVE
1978 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001979STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980xlog_state_clean_log(xlog_t *log)
1981{
1982 xlog_in_core_t *iclog;
1983 int changed = 0;
1984
1985 iclog = log->l_iclog;
1986 do {
1987 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1988 iclog->ic_state = XLOG_STATE_ACTIVE;
1989 iclog->ic_offset = 0;
1990 iclog->ic_callback = NULL; /* don't need to free */
1991 /*
1992 * If the number of ops in this iclog indicate it just
1993 * contains the dummy transaction, we can
1994 * change state into IDLE (the second time around).
1995 * Otherwise we should change the state into
1996 * NEED a dummy.
1997 * We don't need to cover the dummy.
1998 */
1999 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002000 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2001 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 changed = 1;
2003 } else {
2004 /*
2005 * We have two dirty iclogs so start over
2006 * This could also be num of ops indicates
2007 * this is not the dummy going out.
2008 */
2009 changed = 2;
2010 }
2011 iclog->ic_header.h_num_logops = 0;
2012 memset(iclog->ic_header.h_cycle_data, 0,
2013 sizeof(iclog->ic_header.h_cycle_data));
2014 iclog->ic_header.h_lsn = 0;
2015 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2016 /* do nothing */;
2017 else
2018 break; /* stop cleaning */
2019 iclog = iclog->ic_next;
2020 } while (iclog != log->l_iclog);
2021
2022 /* log is locked when we are called */
2023 /*
2024 * Change state for the dummy log recording.
2025 * We usually go to NEED. But we go to NEED2 if the changed indicates
2026 * we are done writing the dummy record.
2027 * If we are done with the second dummy recored (DONE2), then
2028 * we go to IDLE.
2029 */
2030 if (changed) {
2031 switch (log->l_covered_state) {
2032 case XLOG_STATE_COVER_IDLE:
2033 case XLOG_STATE_COVER_NEED:
2034 case XLOG_STATE_COVER_NEED2:
2035 log->l_covered_state = XLOG_STATE_COVER_NEED;
2036 break;
2037
2038 case XLOG_STATE_COVER_DONE:
2039 if (changed == 1)
2040 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2041 else
2042 log->l_covered_state = XLOG_STATE_COVER_NEED;
2043 break;
2044
2045 case XLOG_STATE_COVER_DONE2:
2046 if (changed == 1)
2047 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2048 else
2049 log->l_covered_state = XLOG_STATE_COVER_NEED;
2050 break;
2051
2052 default:
2053 ASSERT(0);
2054 }
2055 }
2056} /* xlog_state_clean_log */
2057
2058STATIC xfs_lsn_t
2059xlog_get_lowest_lsn(
2060 xlog_t *log)
2061{
2062 xlog_in_core_t *lsn_log;
2063 xfs_lsn_t lowest_lsn, lsn;
2064
2065 lsn_log = log->l_iclog;
2066 lowest_lsn = 0;
2067 do {
2068 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002069 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if ((lsn && !lowest_lsn) ||
2071 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2072 lowest_lsn = lsn;
2073 }
2074 }
2075 lsn_log = lsn_log->ic_next;
2076 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002077 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080
2081STATIC void
2082xlog_state_do_callback(
2083 xlog_t *log,
2084 int aborted,
2085 xlog_in_core_t *ciclog)
2086{
2087 xlog_in_core_t *iclog;
2088 xlog_in_core_t *first_iclog; /* used to know when we've
2089 * processed all iclogs once */
2090 xfs_log_callback_t *cb, *cb_next;
2091 int flushcnt = 0;
2092 xfs_lsn_t lowest_lsn;
2093 int ioerrors; /* counter: iclogs with errors */
2094 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2095 int funcdidcallbacks; /* flag: function did callbacks */
2096 int repeats; /* for issuing console warnings if
2097 * looping too many times */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002099 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 first_iclog = iclog = log->l_iclog;
2101 ioerrors = 0;
2102 funcdidcallbacks = 0;
2103 repeats = 0;
2104
2105 do {
2106 /*
2107 * Scan all iclogs starting with the one pointed to by the
2108 * log. Reset this starting point each time the log is
2109 * unlocked (during callbacks).
2110 *
2111 * Keep looping through iclogs until one full pass is made
2112 * without running any callbacks.
2113 */
2114 first_iclog = log->l_iclog;
2115 iclog = log->l_iclog;
2116 loopdidcallbacks = 0;
2117 repeats++;
2118
2119 do {
2120
2121 /* skip all iclogs in the ACTIVE & DIRTY states */
2122 if (iclog->ic_state &
2123 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2124 iclog = iclog->ic_next;
2125 continue;
2126 }
2127
2128 /*
2129 * Between marking a filesystem SHUTDOWN and stopping
2130 * the log, we do flush all iclogs to disk (if there
2131 * wasn't a log I/O error). So, we do want things to
2132 * go smoothly in case of just a SHUTDOWN w/o a
2133 * LOG_IO_ERROR.
2134 */
2135 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2136 /*
2137 * Can only perform callbacks in order. Since
2138 * this iclog is not in the DONE_SYNC/
2139 * DO_CALLBACK state, we skip the rest and
2140 * just try to clean up. If we set our iclog
2141 * to DO_CALLBACK, we will not process it when
2142 * we retry since a previous iclog is in the
2143 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002144 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 */
2146 if (!(iclog->ic_state &
2147 (XLOG_STATE_DONE_SYNC |
2148 XLOG_STATE_DO_CALLBACK))) {
2149 if (ciclog && (ciclog->ic_state ==
2150 XLOG_STATE_DONE_SYNC)) {
2151 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2152 }
2153 break;
2154 }
2155 /*
2156 * We now have an iclog that is in either the
2157 * DO_CALLBACK or DONE_SYNC states. The other
2158 * states (WANT_SYNC, SYNCING, or CALLBACK were
2159 * caught by the above if and are going to
2160 * clean (i.e. we aren't doing their callbacks)
2161 * see the above if.
2162 */
2163
2164 /*
2165 * We will do one more check here to see if we
2166 * have chased our tail around.
2167 */
2168
2169 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002170 if (lowest_lsn &&
2171 XFS_LSN_CMP(lowest_lsn,
2172 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 iclog = iclog->ic_next;
2174 continue; /* Leave this iclog for
2175 * another thread */
2176 }
2177
2178 iclog->ic_state = XLOG_STATE_CALLBACK;
2179
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002180 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002183 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 * No one else can be here except us.
2185 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002186 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002187 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2188 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2189 log->l_last_sync_lsn =
2190 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002191 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /*
2194 * Keep processing entries in the callback list
2195 * until we come around and it is empty. We
2196 * need to atomically see that the list is
2197 * empty and change the state to DIRTY so that
2198 * we don't miss any more callbacks being added.
2199 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002200 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 } else {
2202 ioerrors++;
2203 }
2204 cb = iclog->ic_callback;
2205
Christoph Hellwig4b809162007-08-16 15:37:36 +10002206 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 iclog->ic_callback_tail = &(iclog->ic_callback);
2208 iclog->ic_callback = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002209 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002212 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 cb_next = cb->cb_next;
2214 cb->cb_func(cb->cb_arg, aborted);
2215 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002216 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 cb = iclog->ic_callback;
2218 }
2219
2220 loopdidcallbacks++;
2221 funcdidcallbacks++;
2222
Christoph Hellwig4b809162007-08-16 15:37:36 +10002223 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2225 iclog->ic_state = XLOG_STATE_DIRTY;
2226
2227 /*
2228 * Transition from DIRTY to ACTIVE if applicable.
2229 * NOP if STATE_IOERROR.
2230 */
2231 xlog_state_clean_log(log);
2232
2233 /* wake up threads waiting in xfs_log_force() */
2234 sv_broadcast(&iclog->ic_forcesema);
2235
2236 iclog = iclog->ic_next;
2237 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002238
2239 if (repeats > 5000) {
2240 flushcnt += repeats;
2241 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002243 "%s: possible infinite loop (%d iterations)",
2244 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 } while (!ioerrors && loopdidcallbacks);
2247
2248 /*
2249 * make one last gasp attempt to see if iclogs are being left in
2250 * limbo..
2251 */
2252#ifdef DEBUG
2253 if (funcdidcallbacks) {
2254 first_iclog = iclog = log->l_iclog;
2255 do {
2256 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2257 /*
2258 * Terminate the loop if iclogs are found in states
2259 * which will cause other threads to clean up iclogs.
2260 *
2261 * SYNCING - i/o completion will go through logs
2262 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002263 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 * IOERROR - give up hope all ye who enter here
2265 */
2266 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2267 iclog->ic_state == XLOG_STATE_SYNCING ||
2268 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2269 iclog->ic_state == XLOG_STATE_IOERROR )
2270 break;
2271 iclog = iclog->ic_next;
2272 } while (first_iclog != iclog);
2273 }
2274#endif
2275
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002276 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2278 flushcnt = log->l_flushcnt;
2279 log->l_flushcnt = 0;
2280 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002281 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 while (flushcnt--)
2283 vsema(&log->l_flushsema);
2284} /* xlog_state_do_callback */
2285
2286
2287/*
2288 * Finish transitioning this iclog to the dirty state.
2289 *
2290 * Make sure that we completely execute this routine only when this is
2291 * the last call to the iclog. There is a good chance that iclog flushes,
2292 * when we reach the end of the physical log, get turned into 2 separate
2293 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2294 * routine. By using the reference count bwritecnt, we guarantee that only
2295 * the second completion goes through.
2296 *
2297 * Callbacks could take time, so they are done outside the scope of the
2298 * global state machine log lock. Assume that the calls to cvsema won't
2299 * take a long time. At least we know it won't sleep.
2300 */
David Chinnera8272ce2007-11-23 16:28:09 +11002301STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302xlog_state_done_syncing(
2303 xlog_in_core_t *iclog,
2304 int aborted)
2305{
2306 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002308 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2311 iclog->ic_state == XLOG_STATE_IOERROR);
2312 ASSERT(iclog->ic_refcnt == 0);
2313 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2314
2315
2316 /*
2317 * If we got an error, either on the first buffer, or in the case of
2318 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2319 * and none should ever be attempted to be written to disk
2320 * again.
2321 */
2322 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2323 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002324 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return;
2326 }
2327 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2328 }
2329
2330 /*
2331 * Someone could be sleeping prior to writing out the next
2332 * iclog buffer, we wake them all, one will get to do the
2333 * I/O, the others get to wait for the result.
2334 */
2335 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002336 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2338} /* xlog_state_done_syncing */
2339
2340
2341/*
2342 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2343 * sleep. The flush semaphore is set to the number of in-core buffers and
2344 * decremented around disk syncing. Therefore, if all buffers are syncing,
2345 * this semaphore will cause new writes to sleep until a sync completes.
2346 * Otherwise, this code just does p() followed by v(). This approximates
2347 * a sleep/wakeup except we can't race.
2348 *
2349 * The in-core logs are used in a circular fashion. They are not used
2350 * out-of-order even when an iclog past the head is free.
2351 *
2352 * return:
2353 * * log_offset where xlog_write() can start writing into the in-core
2354 * log's data space.
2355 * * in-core log pointer to which xlog_write() should write.
2356 * * boolean indicating this is a continued write to an in-core log.
2357 * If this is the last write, then the in-core log's offset field
2358 * needs to be incremented, depending on the amount of data which
2359 * is copied.
2360 */
David Chinnera8272ce2007-11-23 16:28:09 +11002361STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362xlog_state_get_iclog_space(xlog_t *log,
2363 int len,
2364 xlog_in_core_t **iclogp,
2365 xlog_ticket_t *ticket,
2366 int *continued_write,
2367 int *logoffsetp)
2368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 int log_offset;
2370 xlog_rec_header_t *head;
2371 xlog_in_core_t *iclog;
2372 int error;
2373
2374restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002375 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002377 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return XFS_ERROR(EIO);
2379 }
2380
2381 iclog = log->l_iclog;
2382 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2383 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002384 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2386 XFS_STATS_INC(xs_log_noiclogs);
2387 /* Ensure that log writes happen */
2388 psema(&log->l_flushsema, PINOD);
2389 goto restart;
2390 }
2391 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2392 head = &iclog->ic_header;
2393
2394 iclog->ic_refcnt++; /* prevents sync */
2395 log_offset = iclog->ic_offset;
2396
2397 /* On the 1st write to an iclog, figure out lsn. This works
2398 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2399 * committing to. If the offset is set, that's how many blocks
2400 * must be written.
2401 */
2402 if (log_offset == 0) {
2403 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002404 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002405 log->l_iclog_hsize,
2406 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002407 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2408 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002409 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 ASSERT(log->l_curr_block >= 0);
2411 }
2412
2413 /* If there is enough room to write everything, then do it. Otherwise,
2414 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2415 * bit is on, so this will get flushed out. Don't update ic_offset
2416 * until you know exactly how many bytes get copied. Therefore, wait
2417 * until later to update ic_offset.
2418 *
2419 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2420 * can fit into remaining data section.
2421 */
2422 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2423 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2424
2425 /* If I'm the only one writing to this iclog, sync it to disk */
2426 if (iclog->ic_refcnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002427 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 } else {
2431 iclog->ic_refcnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002432 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 goto restart;
2435 }
2436
2437 /* Do we have enough room to write the full amount in the remainder
2438 * of this iclog? Or must we continue a write on the next iclog and
2439 * mark this iclog as completely taken? In the case where we switch
2440 * iclogs (to mark it taken), this particular iclog will release/sync
2441 * to disk in xlog_write().
2442 */
2443 if (len <= iclog->ic_size - iclog->ic_offset) {
2444 *continued_write = 0;
2445 iclog->ic_offset += len;
2446 } else {
2447 *continued_write = 1;
2448 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2449 }
2450 *iclogp = iclog;
2451
2452 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002453 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 *logoffsetp = log_offset;
2456 return 0;
2457} /* xlog_state_get_iclog_space */
2458
2459/*
2460 * Atomically get the log space required for a log ticket.
2461 *
2462 * Once a ticket gets put onto the reserveq, it will only return after
2463 * the needed reservation is satisfied.
2464 */
2465STATIC int
2466xlog_grant_log_space(xlog_t *log,
2467 xlog_ticket_t *tic)
2468{
2469 int free_bytes;
2470 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471#ifdef DEBUG
2472 xfs_lsn_t tail_lsn;
2473#endif
2474
2475
2476#ifdef DEBUG
2477 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2478 panic("grant Recovery problem");
2479#endif
2480
2481 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002482 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2484
2485 /* something is already sleeping; insert new transaction at end */
2486 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002487 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 xlog_trace_loggrant(log, tic,
2489 "xlog_grant_log_space: sleep 1");
2490 /*
2491 * Gotta check this before going to sleep, while we're
2492 * holding the grant lock.
2493 */
2494 if (XLOG_FORCED_SHUTDOWN(log))
2495 goto error_return;
2496
2497 XFS_STATS_INC(xs_sleep_logspace);
2498 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2499 /*
2500 * If we got an error, and the filesystem is shutting down,
2501 * we'll catch it down below. So just continue...
2502 */
2503 xlog_trace_loggrant(log, tic,
2504 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002505 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2508 need_bytes = tic->t_unit_res*tic->t_ocnt;
2509 else
2510 need_bytes = tic->t_unit_res;
2511
2512redo:
2513 if (XLOG_FORCED_SHUTDOWN(log))
2514 goto error_return;
2515
2516 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2517 log->l_grant_reserve_bytes);
2518 if (free_bytes < need_bytes) {
2519 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002520 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 xlog_trace_loggrant(log, tic,
2522 "xlog_grant_log_space: sleep 2");
2523 XFS_STATS_INC(xs_sleep_logspace);
2524 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2525
2526 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002527 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 goto error_return;
2529 }
2530
2531 xlog_trace_loggrant(log, tic,
2532 "xlog_grant_log_space: wake 2");
2533 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002534 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 goto redo;
2536 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002537 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002540 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541#ifdef DEBUG
2542 tail_lsn = log->l_tail_lsn;
2543 /*
2544 * Check to make sure the grant write head didn't just over lap the
2545 * tail. If the cycles are the same, we can't be overlapping.
2546 * Otherwise, make sure that the cycles differ by exactly one and
2547 * check the byte count.
2548 */
2549 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2550 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2551 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2552 }
2553#endif
2554 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2555 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002556 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return 0;
2558
2559 error_return:
2560 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002561 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2563 /*
2564 * If we are failing, make sure the ticket doesn't have any
2565 * current reservations. We don't want to add this back when
2566 * the ticket/transaction gets cancelled.
2567 */
2568 tic->t_curr_res = 0;
2569 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002570 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return XFS_ERROR(EIO);
2572} /* xlog_grant_log_space */
2573
2574
2575/*
2576 * Replenish the byte reservation required by moving the grant write head.
2577 *
2578 *
2579 */
2580STATIC int
2581xlog_regrant_write_log_space(xlog_t *log,
2582 xlog_ticket_t *tic)
2583{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 int free_bytes, need_bytes;
2585 xlog_ticket_t *ntic;
2586#ifdef DEBUG
2587 xfs_lsn_t tail_lsn;
2588#endif
2589
2590 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002591 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596#ifdef DEBUG
2597 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2598 panic("regrant Recovery problem");
2599#endif
2600
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002601 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2603
2604 if (XLOG_FORCED_SHUTDOWN(log))
2605 goto error_return;
2606
2607 /* If there are other waiters on the queue then give them a
2608 * chance at logspace before us. Wake up the first waiters,
2609 * if we do not wake up all the waiters then go to sleep waiting
2610 * for more free space, otherwise try to get some space for
2611 * this transaction.
2612 */
2613
2614 if ((ntic = log->l_write_headq)) {
2615 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2616 log->l_grant_write_bytes);
2617 do {
2618 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2619
2620 if (free_bytes < ntic->t_unit_res)
2621 break;
2622 free_bytes -= ntic->t_unit_res;
2623 sv_signal(&ntic->t_sema);
2624 ntic = ntic->t_next;
2625 } while (ntic != log->l_write_headq);
2626
2627 if (ntic != log->l_write_headq) {
2628 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002629 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 xlog_trace_loggrant(log, tic,
2632 "xlog_regrant_write_log_space: sleep 1");
2633 XFS_STATS_INC(xs_sleep_logspace);
2634 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2635 &log->l_grant_lock, s);
2636
2637 /* If we're shutting down, this tic is already
2638 * off the queue */
2639 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002640 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 goto error_return;
2642 }
2643
2644 xlog_trace_loggrant(log, tic,
2645 "xlog_regrant_write_log_space: wake 1");
2646 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002647 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 }
2650
2651 need_bytes = tic->t_unit_res;
2652
2653redo:
2654 if (XLOG_FORCED_SHUTDOWN(log))
2655 goto error_return;
2656
2657 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2658 log->l_grant_write_bytes);
2659 if (free_bytes < need_bytes) {
2660 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002661 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 XFS_STATS_INC(xs_sleep_logspace);
2663 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2664
2665 /* If we're shutting down, this tic is already off the queue */
2666 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002667 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 goto error_return;
2669 }
2670
2671 xlog_trace_loggrant(log, tic,
2672 "xlog_regrant_write_log_space: wake 2");
2673 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002674 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 goto redo;
2676 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002677 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002679 /* we've got enough space */
2680 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681#ifdef DEBUG
2682 tail_lsn = log->l_tail_lsn;
2683 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2684 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2685 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2686 }
2687#endif
2688
2689 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2690 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002691 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694
2695 error_return:
2696 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002697 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2699 /*
2700 * If we are failing, make sure the ticket doesn't have any
2701 * current reservations. We don't want to add this back when
2702 * the ticket/transaction gets cancelled.
2703 */
2704 tic->t_curr_res = 0;
2705 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002706 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return XFS_ERROR(EIO);
2708} /* xlog_regrant_write_log_space */
2709
2710
2711/* The first cnt-1 times through here we don't need to
2712 * move the grant write head because the permanent
2713 * reservation has reserved cnt times the unit amount.
2714 * Release part of current permanent unit reservation and
2715 * reset current reservation to be one units worth. Also
2716 * move grant reservation head forward.
2717 */
2718STATIC void
2719xlog_regrant_reserve_log_space(xlog_t *log,
2720 xlog_ticket_t *ticket)
2721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 xlog_trace_loggrant(log, ticket,
2723 "xlog_regrant_reserve_log_space: enter");
2724 if (ticket->t_cnt > 0)
2725 ticket->t_cnt--;
2726
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002727 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002728 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002730 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 xlog_trace_loggrant(log, ticket,
2732 "xlog_regrant_reserve_log_space: sub current res");
2733 xlog_verify_grant_head(log, 1);
2734
2735 /* just return if we still have some of the pre-reserved space */
2736 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002737 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return;
2739 }
2740
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002741 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 xlog_trace_loggrant(log, ticket,
2743 "xlog_regrant_reserve_log_space: exit");
2744 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002745 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002747 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748} /* xlog_regrant_reserve_log_space */
2749
2750
2751/*
2752 * Give back the space left from a reservation.
2753 *
2754 * All the information we need to make a correct determination of space left
2755 * is present. For non-permanent reservations, things are quite easy. The
2756 * count should have been decremented to zero. We only need to deal with the
2757 * space remaining in the current reservation part of the ticket. If the
2758 * ticket contains a permanent reservation, there may be left over space which
2759 * needs to be released. A count of N means that N-1 refills of the current
2760 * reservation can be done before we need to ask for more space. The first
2761 * one goes to fill up the first current reservation. Once we run out of
2762 * space, the count will stay at zero and the only space remaining will be
2763 * in the current reservation field.
2764 */
2765STATIC void
2766xlog_ungrant_log_space(xlog_t *log,
2767 xlog_ticket_t *ticket)
2768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (ticket->t_cnt > 0)
2770 ticket->t_cnt--;
2771
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002772 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2774
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002775 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2778
2779 /* If this is a permanent reservation ticket, we may be able to free
2780 * up more space based on the remaining count.
2781 */
2782 if (ticket->t_cnt > 0) {
2783 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002784 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786
2787 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2788 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002789 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 xfs_log_move_tail(log->l_mp, 1);
2791} /* xlog_ungrant_log_space */
2792
2793
2794/*
2795 * Atomically put back used ticket.
2796 */
David Chinnera8272ce2007-11-23 16:28:09 +11002797STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798xlog_state_put_ticket(xlog_t *log,
2799 xlog_ticket_t *tic)
2800{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002801 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 xlog_ticket_put(log, tic);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002803 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804} /* xlog_state_put_ticket */
2805
2806/*
2807 * Flush iclog to disk if this is the last reference to the given iclog and
2808 * the WANT_SYNC bit is set.
2809 *
2810 * When this function is entered, the iclog is not necessarily in the
2811 * WANT_SYNC state. It may be sitting around waiting to get filled.
2812 *
2813 *
2814 */
David Chinnera8272ce2007-11-23 16:28:09 +11002815STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002816xlog_state_release_iclog(
2817 xlog_t *log,
2818 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 int sync = 0; /* do we sync? */
2821
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002822 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002824 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return XFS_ERROR(EIO);
2826 }
2827
2828 ASSERT(iclog->ic_refcnt > 0);
2829 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2830 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2831
2832 if (--iclog->ic_refcnt == 0 &&
2833 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002834 /* update tail before writing to iclog */
2835 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 sync++;
2837 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002838 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2840 /* cycle incremented when incrementing curr_block */
2841 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002842 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 /*
2845 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002846 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2848 * this iclog has consistent data, so we ignore IOERROR
2849 * flags after this point.
2850 */
David Chinnerb5893342008-03-06 13:44:06 +11002851 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854} /* xlog_state_release_iclog */
2855
2856
2857/*
2858 * This routine will mark the current iclog in the ring as WANT_SYNC
2859 * and move the current iclog pointer to the next iclog in the ring.
2860 * When this routine is called from xlog_state_get_iclog_space(), the
2861 * exact size of the iclog has not yet been determined. All we know is
2862 * that every data block. We have run out of space in this log record.
2863 */
2864STATIC void
2865xlog_state_switch_iclogs(xlog_t *log,
2866 xlog_in_core_t *iclog,
2867 int eventual_size)
2868{
2869 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2870 if (!eventual_size)
2871 eventual_size = iclog->ic_offset;
2872 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002873 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 log->l_prev_block = log->l_curr_block;
2875 log->l_prev_cycle = log->l_curr_cycle;
2876
2877 /* roll log?: ic_offset changed later */
2878 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2879
2880 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002881 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 log->l_mp->m_sb.sb_logsunit > 1) {
2883 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2884 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2885 }
2886
2887 if (log->l_curr_block >= log->l_logBBsize) {
2888 log->l_curr_cycle++;
2889 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2890 log->l_curr_cycle++;
2891 log->l_curr_block -= log->l_logBBsize;
2892 ASSERT(log->l_curr_block >= 0);
2893 }
2894 ASSERT(iclog == log->l_iclog);
2895 log->l_iclog = iclog->ic_next;
2896} /* xlog_state_switch_iclogs */
2897
2898
2899/*
2900 * Write out all data in the in-core log as of this exact moment in time.
2901 *
2902 * Data may be written to the in-core log during this call. However,
2903 * we don't guarantee this data will be written out. A change from past
2904 * implementation means this routine will *not* write out zero length LRs.
2905 *
2906 * Basically, we try and perform an intelligent scan of the in-core logs.
2907 * If we determine there is no flushable data, we just return. There is no
2908 * flushable data if:
2909 *
2910 * 1. the current iclog is active and has no data; the previous iclog
2911 * is in the active or dirty state.
2912 * 2. the current iclog is drity, and the previous iclog is in the
2913 * active or dirty state.
2914 *
2915 * We may sleep (call psema) if:
2916 *
2917 * 1. the current iclog is not in the active nor dirty state.
2918 * 2. the current iclog dirty, and the previous iclog is not in the
2919 * active nor dirty state.
2920 * 3. the current iclog is active, and there is another thread writing
2921 * to this particular iclog.
2922 * 4. a) the current iclog is active and has no other writers
2923 * b) when we return from flushing out this iclog, it is still
2924 * not in the active nor dirty state.
2925 */
2926STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002927xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 xlog_in_core_t *iclog;
2930 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002932 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 iclog = log->l_iclog;
2935 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002936 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return XFS_ERROR(EIO);
2938 }
2939
2940 /* If the head iclog is not active nor dirty, we just attach
2941 * ourselves to the head and go to sleep.
2942 */
2943 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2944 iclog->ic_state == XLOG_STATE_DIRTY) {
2945 /*
2946 * If the head is dirty or (active and empty), then
2947 * we need to look at the previous iclog. If the previous
2948 * iclog is active or dirty we are done. There is nothing
2949 * to sync out. Otherwise, we attach ourselves to the
2950 * previous iclog and go to sleep.
2951 */
2952 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2953 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2954 iclog = iclog->ic_prev;
2955 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2956 iclog->ic_state == XLOG_STATE_DIRTY)
2957 goto no_sleep;
2958 else
2959 goto maybe_sleep;
2960 } else {
2961 if (iclog->ic_refcnt == 0) {
2962 /* We are the only one with access to this
2963 * iclog. Flush it out now. There should
2964 * be a roundoff of zero to show that someone
2965 * has already taken care of the roundoff from
2966 * the previous sync.
2967 */
2968 iclog->ic_refcnt++;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002969 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002971 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 if (xlog_state_release_iclog(log, iclog))
2974 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002975 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002976 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002977 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 iclog->ic_state != XLOG_STATE_DIRTY)
2979 goto maybe_sleep;
2980 else
2981 goto no_sleep;
2982 } else {
2983 /* Someone else is writing to this iclog.
2984 * Use its call to flush out the data. However,
2985 * the other thread may not force out this LR,
2986 * so we mark it WANT_SYNC.
2987 */
2988 xlog_state_switch_iclogs(log, iclog, 0);
2989 goto maybe_sleep;
2990 }
2991 }
2992 }
2993
2994 /* By the time we come around again, the iclog could've been filled
2995 * which would give it another lsn. If we have a new lsn, just
2996 * return because the relevant data has been flushed.
2997 */
2998maybe_sleep:
2999 if (flags & XFS_LOG_SYNC) {
3000 /*
3001 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003002 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 * Then we check again after waking up, in case our
3004 * sleep was disturbed by a bad news.
3005 */
3006 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003007 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return XFS_ERROR(EIO);
3009 }
3010 XFS_STATS_INC(xs_log_force_sleep);
3011 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3012 /*
3013 * No need to grab the log lock here since we're
3014 * only deciding whether or not to return EIO
3015 * and the memory read should be atomic.
3016 */
3017 if (iclog->ic_state & XLOG_STATE_IOERROR)
3018 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003019 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021 } else {
3022
3023no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003024 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
3026 return 0;
3027} /* xlog_state_sync_all */
3028
3029
3030/*
3031 * Used by code which implements synchronous log forces.
3032 *
3033 * Find in-core log with lsn.
3034 * If it is in the DIRTY state, just return.
3035 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3036 * state and go to sleep or return.
3037 * If it is in any other state, go to sleep or return.
3038 *
3039 * If filesystem activity goes to zero, the iclog will get flushed only by
3040 * bdflush().
3041 */
David Chinnera8272ce2007-11-23 16:28:09 +11003042STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043xlog_state_sync(xlog_t *log,
3044 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003045 uint flags,
3046 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
3048 xlog_in_core_t *iclog;
3049 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003052 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 iclog = log->l_iclog;
3054
3055 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003056 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return XFS_ERROR(EIO);
3058 }
3059
3060 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003061 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3062 iclog = iclog->ic_next;
3063 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065
3066 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003067 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return 0;
3069 }
3070
3071 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3072 /*
3073 * We sleep here if we haven't already slept (e.g.
3074 * this is the first time we've looked at the correct
3075 * iclog buf) and the buffer before us is going to
3076 * be sync'ed. The reason for this is that if we
3077 * are doing sync transactions here, by waiting for
3078 * the previous I/O to complete, we can allow a few
3079 * more transactions into this iclog before we close
3080 * it down.
3081 *
3082 * Otherwise, we mark the buffer WANT_SYNC, and bump
3083 * up the refcnt so we can release the log (which drops
3084 * the ref count). The state switch keeps new transaction
3085 * commits from using this buffer. When the current commits
3086 * finish writing into the buffer, the refcount will drop to
3087 * zero and the buffer will go out then.
3088 */
3089 if (!already_slept &&
3090 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3091 XLOG_STATE_SYNCING))) {
3092 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3093 XFS_STATS_INC(xs_log_force_sleep);
3094 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3095 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003096 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 already_slept = 1;
3098 goto try_again;
3099 } else {
3100 iclog->ic_refcnt++;
3101 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003102 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (xlog_state_release_iclog(log, iclog))
3104 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003105 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003106 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
3108 }
3109
3110 if ((flags & XFS_LOG_SYNC) && /* sleep */
3111 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3112
3113 /*
3114 * Don't wait on the forcesema if we know that we've
3115 * gotten a log write error.
3116 */
3117 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003118 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return XFS_ERROR(EIO);
3120 }
3121 XFS_STATS_INC(xs_log_force_sleep);
3122 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3123 /*
3124 * No need to grab the log lock here since we're
3125 * only deciding whether or not to return EIO
3126 * and the memory read should be atomic.
3127 */
3128 if (iclog->ic_state & XLOG_STATE_IOERROR)
3129 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003130 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003132 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
3134 return 0;
3135
3136 } while (iclog != log->l_iclog);
3137
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003138 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140} /* xlog_state_sync */
3141
3142
3143/*
3144 * Called when we want to mark the current iclog as being ready to sync to
3145 * disk.
3146 */
David Chinnera8272ce2007-11-23 16:28:09 +11003147STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3149{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003150 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3153 xlog_state_switch_iclogs(log, iclog, 0);
3154 } else {
3155 ASSERT(iclog->ic_state &
3156 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3157 }
3158
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003159 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160} /* xlog_state_want_sync */
3161
3162
3163
3164/*****************************************************************************
3165 *
3166 * TICKET functions
3167 *
3168 *****************************************************************************
3169 */
3170
3171/*
3172 * Algorithm doesn't take into account page size. ;-(
3173 */
3174STATIC void
3175xlog_state_ticket_alloc(xlog_t *log)
3176{
3177 xlog_ticket_t *t_list;
3178 xlog_ticket_t *next;
3179 xfs_caddr_t buf;
Tim Shimmine6a4b372007-11-23 16:30:42 +11003180 uint i = (PAGE_SIZE / sizeof(xlog_ticket_t)) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 /*
3183 * The kmem_zalloc may sleep, so we shouldn't be holding the
3184 * global lock. XXXmiken: may want to use zone allocator.
3185 */
Tim Shimmine6a4b372007-11-23 16:30:42 +11003186 buf = (xfs_caddr_t) kmem_zalloc(PAGE_SIZE, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003188 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3191 t_list = (xlog_ticket_t *)buf;
3192 t_list->t_next = log->l_unmount_free;
3193 log->l_unmount_free = t_list++;
3194 log->l_ticket_cnt++;
3195 log->l_ticket_tcnt++;
3196
3197 /* Next ticket becomes first ticket attached to ticket free list */
3198 if (log->l_freelist != NULL) {
3199 ASSERT(log->l_tail != NULL);
3200 log->l_tail->t_next = t_list;
3201 } else {
3202 log->l_freelist = t_list;
3203 }
3204 log->l_ticket_cnt++;
3205 log->l_ticket_tcnt++;
3206
3207 /* Cycle through rest of alloc'ed memory, building up free Q */
3208 for ( ; i > 0; i--) {
3209 next = t_list + 1;
3210 t_list->t_next = next;
3211 t_list = next;
3212 log->l_ticket_cnt++;
3213 log->l_ticket_tcnt++;
3214 }
3215 t_list->t_next = NULL;
3216 log->l_tail = t_list;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003217 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218} /* xlog_state_ticket_alloc */
3219
3220
3221/*
3222 * Put ticket into free list
3223 *
3224 * Assumption: log lock is held around this call.
3225 */
3226STATIC void
3227xlog_ticket_put(xlog_t *log,
3228 xlog_ticket_t *ticket)
3229{
3230 sv_destroy(&ticket->t_sema);
3231
3232 /*
3233 * Don't think caching will make that much difference. It's
3234 * more important to make debug easier.
3235 */
3236#if 0
3237 /* real code will want to use LIFO for caching */
3238 ticket->t_next = log->l_freelist;
3239 log->l_freelist = ticket;
3240 /* no need to clear fields */
3241#else
3242 /* When we debug, it is easier if tickets are cycled */
3243 ticket->t_next = NULL;
Christoph Hellwig4b809162007-08-16 15:37:36 +10003244 if (log->l_tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 log->l_tail->t_next = ticket;
3246 } else {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003247 ASSERT(log->l_freelist == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 log->l_freelist = ticket;
3249 }
3250 log->l_tail = ticket;
3251#endif /* DEBUG */
3252 log->l_ticket_cnt++;
3253} /* xlog_ticket_put */
3254
3255
3256/*
3257 * Grab ticket off freelist or allocation some more
3258 */
David Chinnera8272ce2007-11-23 16:28:09 +11003259STATIC xlog_ticket_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260xlog_ticket_get(xlog_t *log,
3261 int unit_bytes,
3262 int cnt,
3263 char client,
3264 uint xflags)
3265{
3266 xlog_ticket_t *tic;
3267 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 alloc:
3270 if (log->l_freelist == NULL)
3271 xlog_state_ticket_alloc(log); /* potentially sleep */
3272
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003273 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (log->l_freelist == NULL) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003275 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 goto alloc;
3277 }
3278 tic = log->l_freelist;
3279 log->l_freelist = tic->t_next;
3280 if (log->l_freelist == NULL)
3281 log->l_tail = NULL;
3282 log->l_ticket_cnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003283 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 /*
3286 * Permanent reservations have up to 'cnt'-1 active log operations
3287 * in the log. A unit in this case is the amount of space for one
3288 * of these log operations. Normal reservations have a cnt of 1
3289 * and their unit amount is the total amount of space required.
3290 *
3291 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003292 * which occupy space in the on-disk log.
3293 *
3294 * Normal form of a transaction is:
3295 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3296 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3297 *
3298 * We need to account for all the leadup data and trailer data
3299 * around the transaction data.
3300 * And then we need to account for the worst case in terms of using
3301 * more space.
3302 * The worst case will happen if:
3303 * - the placement of the transaction happens to be such that the
3304 * roundoff is at its maximum
3305 * - the transaction data is synced before the commit record is synced
3306 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3307 * Therefore the commit record is in its own Log Record.
3308 * This can happen as the commit record is called with its
3309 * own region to xlog_write().
3310 * This then means that in the worst case, roundoff can happen for
3311 * the commit-rec as well.
3312 * The commit-rec is smaller than padding in this scenario and so it is
3313 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 */
3315
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003316 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003318 unit_bytes += sizeof(xfs_trans_header_t);
3319
3320 /* for start-rec */
3321 unit_bytes += sizeof(xlog_op_header_t);
3322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 /* for LR headers */
3324 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3325 unit_bytes += log->l_iclog_hsize * num_headers;
3326
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003327 /* for commit-rec LR header - note: padding will subsume the ophdr */
3328 unit_bytes += log->l_iclog_hsize;
3329
3330 /* for split-recs - ophdrs added when data split over LRs */
3331 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3332
3333 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003334 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003335 log->l_mp->m_sb.sb_logsunit > 1) {
3336 /* log su roundoff */
3337 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3338 } else {
3339 /* BB roundoff */
3340 unit_bytes += 2*BBSIZE;
3341 }
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 tic->t_unit_res = unit_bytes;
3344 tic->t_curr_res = unit_bytes;
3345 tic->t_cnt = cnt;
3346 tic->t_ocnt = cnt;
3347 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3348 tic->t_clientid = client;
3349 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003350 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 if (xflags & XFS_LOG_PERM_RESERV)
3352 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3353 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3354
Christoph Hellwig0adba532007-08-30 17:21:46 +10003355 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return tic;
3358} /* xlog_ticket_get */
3359
3360
3361/******************************************************************************
3362 *
3363 * Log debug routines
3364 *
3365 ******************************************************************************
3366 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003367#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368/*
3369 * Make sure that the destination ptr is within the valid data region of
3370 * one of the iclogs. This uses backup pointers stored in a different
3371 * part of the log in case we trash the log structure.
3372 */
3373void
3374xlog_verify_dest_ptr(xlog_t *log,
3375 __psint_t ptr)
3376{
3377 int i;
3378 int good_ptr = 0;
3379
3380 for (i=0; i < log->l_iclog_bufs; i++) {
3381 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3382 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3383 good_ptr++;
3384 }
3385 if (! good_ptr)
3386 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3387} /* xlog_verify_dest_ptr */
3388
3389STATIC void
3390xlog_verify_grant_head(xlog_t *log, int equals)
3391{
3392 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3393 if (equals)
3394 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3395 else
3396 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3397 } else {
3398 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3399 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3400 }
3401} /* xlog_verify_grant_head */
3402
3403/* check if it will fit */
3404STATIC void
3405xlog_verify_tail_lsn(xlog_t *log,
3406 xlog_in_core_t *iclog,
3407 xfs_lsn_t tail_lsn)
3408{
3409 int blocks;
3410
3411 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3412 blocks =
3413 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3414 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3415 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3416 } else {
3417 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3418
3419 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3420 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3421
3422 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3423 if (blocks < BTOBB(iclog->ic_offset) + 1)
3424 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3425 }
3426} /* xlog_verify_tail_lsn */
3427
3428/*
3429 * Perform a number of checks on the iclog before writing to disk.
3430 *
3431 * 1. Make sure the iclogs are still circular
3432 * 2. Make sure we have a good magic number
3433 * 3. Make sure we don't have magic numbers in the data
3434 * 4. Check fields of each log operation header for:
3435 * A. Valid client identifier
3436 * B. tid ptr value falls in valid ptr space (user space code)
3437 * C. Length in log record header is correct according to the
3438 * individual operation headers within record.
3439 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3440 * log, check the preceding blocks of the physical log to make sure all
3441 * the cycle numbers agree with the current cycle number.
3442 */
3443STATIC void
3444xlog_verify_iclog(xlog_t *log,
3445 xlog_in_core_t *iclog,
3446 int count,
3447 boolean_t syncing)
3448{
3449 xlog_op_header_t *ophead;
3450 xlog_in_core_t *icptr;
3451 xlog_in_core_2_t *xhdr;
3452 xfs_caddr_t ptr;
3453 xfs_caddr_t base_ptr;
3454 __psint_t field_offset;
3455 __uint8_t clientid;
3456 int len, i, j, k, op_len;
3457 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003460 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 icptr = log->l_iclog;
3462 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003463 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 xlog_panic("xlog_verify_iclog: invalid ptr");
3465 icptr = icptr->ic_next;
3466 }
3467 if (icptr != log->l_iclog)
3468 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003469 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003472 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 xlog_panic("xlog_verify_iclog: invalid magic num");
3474
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003475 ptr = (xfs_caddr_t) &iclog->ic_header;
3476 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003478 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 xlog_panic("xlog_verify_iclog: unexpected magic num");
3480 }
3481
3482 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003483 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 ptr = iclog->ic_datap;
3485 base_ptr = ptr;
3486 ophead = (xlog_op_header_t *)ptr;
3487 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3488 for (i = 0; i < len; i++) {
3489 ophead = (xlog_op_header_t *)ptr;
3490
3491 /* clientid is only 1 byte */
3492 field_offset = (__psint_t)
3493 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3494 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3495 clientid = ophead->oh_clientid;
3496 } else {
3497 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3498 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3499 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3500 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003501 clientid = xlog_get_client_id(
3502 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003504 clientid = xlog_get_client_id(
3505 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507 }
3508 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003509 cmn_err(CE_WARN, "xlog_verify_iclog: "
3510 "invalid clientid %d op 0x%p offset 0x%lx",
3511 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 /* check length */
3514 field_offset = (__psint_t)
3515 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3516 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003517 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 } else {
3519 idx = BTOBBT((__psint_t)&ophead->oh_len -
3520 (__psint_t)iclog->ic_datap);
3521 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3522 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3523 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003524 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003526 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528 }
3529 ptr += sizeof(xlog_op_header_t) + op_len;
3530 }
3531} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003535 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 */
3537STATIC int
3538xlog_state_ioerror(
3539 xlog_t *log)
3540{
3541 xlog_in_core_t *iclog, *ic;
3542
3543 iclog = log->l_iclog;
3544 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3545 /*
3546 * Mark all the incore logs IOERROR.
3547 * From now on, no log flushes will result.
3548 */
3549 ic = iclog;
3550 do {
3551 ic->ic_state = XLOG_STATE_IOERROR;
3552 ic = ic->ic_next;
3553 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556 /*
3557 * Return non-zero, if state transition has already happened.
3558 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003559 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
3562/*
3563 * This is called from xfs_force_shutdown, when we're forcibly
3564 * shutting down the filesystem, typically because of an IO error.
3565 * Our main objectives here are to make sure that:
3566 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3567 * parties to find out, 'atomically'.
3568 * b. those who're sleeping on log reservations, pinned objects and
3569 * other resources get woken up, and be told the bad news.
3570 * c. nothing new gets queued up after (a) and (b) are done.
3571 * d. if !logerror, flush the iclogs to disk, then seal them off
3572 * for business.
3573 */
3574int
3575xfs_log_force_umount(
3576 struct xfs_mount *mp,
3577 int logerror)
3578{
3579 xlog_ticket_t *tic;
3580 xlog_t *log;
3581 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003582 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584 log = mp->m_log;
3585
3586 /*
3587 * If this happens during log recovery, don't worry about
3588 * locking; the log isn't open for business yet.
3589 */
3590 if (!log ||
3591 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3592 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3593 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
3596
3597 /*
3598 * Somebody could've already done the hard work for us.
3599 * No need to get locks for this.
3600 */
3601 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3602 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003603 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605 retval = 0;
3606 /*
3607 * We must hold both the GRANT lock and the LOG lock,
3608 * before we mark the filesystem SHUTDOWN and wake
3609 * everybody up to tell the bad news.
3610 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003611 spin_lock(&log->l_grant_lock);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003612 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3614 XFS_BUF_DONE(mp->m_sb_bp);
3615 /*
3616 * This flag is sort of redundant because of the mount flag, but
3617 * it's good to maintain the separation between the log and the rest
3618 * of XFS.
3619 */
3620 log->l_flags |= XLOG_IO_ERROR;
3621
3622 /*
3623 * If we hit a log error, we want to mark all the iclogs IOERROR
3624 * while we're still holding the loglock.
3625 */
3626 if (logerror)
3627 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003628 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 /*
3631 * We don't want anybody waiting for log reservations
3632 * after this. That means we have to wake up everybody
3633 * queued up on reserve_headq as well as write_headq.
3634 * In addition, we make sure in xlog_{re}grant_log_space
3635 * that we don't enqueue anything once the SHUTDOWN flag
3636 * is set, and this action is protected by the GRANTLOCK.
3637 */
3638 if ((tic = log->l_reserve_headq)) {
3639 do {
3640 sv_signal(&tic->t_sema);
3641 tic = tic->t_next;
3642 } while (tic != log->l_reserve_headq);
3643 }
3644
3645 if ((tic = log->l_write_headq)) {
3646 do {
3647 sv_signal(&tic->t_sema);
3648 tic = tic->t_next;
3649 } while (tic != log->l_write_headq);
3650 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003651 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3654 ASSERT(!logerror);
3655 /*
3656 * Force the incore logs to disk before shutting the
3657 * log down completely.
3658 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003659 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003660 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003662 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 }
3664 /*
3665 * Wake up everybody waiting on xfs_log_force.
3666 * Callback all log item committed functions as if the
3667 * log writes were completed.
3668 */
3669 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3670
3671#ifdef XFSERRORDEBUG
3672 {
3673 xlog_in_core_t *iclog;
3674
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003675 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 iclog = log->l_iclog;
3677 do {
3678 ASSERT(iclog->ic_callback == 0);
3679 iclog = iclog->ic_next;
3680 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003681 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
3683#endif
3684 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003685 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686}
3687
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003688STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689xlog_iclogs_empty(xlog_t *log)
3690{
3691 xlog_in_core_t *iclog;
3692
3693 iclog = log->l_iclog;
3694 do {
3695 /* endianness does not matter here, zero is zero in
3696 * any language.
3697 */
3698 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 iclog = iclog->ic_next;
3701 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003702 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}