blob: 4c86a26330a054b634d60145f16a22a1113fc87a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44
45#define xlog_write_adv_cnt(ptr, len, off, bytes) \
46 { (ptr) += (bytes); \
47 (len) -= (bytes); \
48 (off) += (bytes);}
49
50/* Local miscellaneous function prototypes */
51STATIC int xlog_bdstrat_cb(struct xfs_buf *);
52STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
53 xlog_in_core_t **, xfs_lsn_t *);
54STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
55 xfs_buftarg_t *log_target,
56 xfs_daddr_t blk_offset,
57 int num_bblks);
58STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
59STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100060STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
62 int nentries, xfs_log_ticket_t tic,
63 xfs_lsn_t *start_lsn,
64 xlog_in_core_t **commit_iclog,
65 uint flags);
66
67/* local state machine functions */
68STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
69STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
70STATIC int xlog_state_get_iclog_space(xlog_t *log,
71 int len,
72 xlog_in_core_t **iclog,
73 xlog_ticket_t *ticket,
74 int *continued_write,
75 int *logoffsetp);
76STATIC void xlog_state_put_ticket(xlog_t *log,
77 xlog_ticket_t *tic);
78STATIC int xlog_state_release_iclog(xlog_t *log,
79 xlog_in_core_t *iclog);
80STATIC void xlog_state_switch_iclogs(xlog_t *log,
81 xlog_in_core_t *iclog,
82 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110083STATIC int xlog_state_sync(xlog_t *log,
84 xfs_lsn_t lsn,
85 uint flags,
86 int *log_flushed);
87STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
89
90/* local functions to manipulate grant head */
91STATIC int xlog_grant_log_space(xlog_t *log,
92 xlog_ticket_t *xtic);
93STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
94 int need_bytes);
95STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
96 xlog_ticket_t *ticket);
97STATIC int xlog_regrant_write_log_space(xlog_t *log,
98 xlog_ticket_t *ticket);
99STATIC void xlog_ungrant_log_space(xlog_t *log,
100 xlog_ticket_t *ticket);
101
102
103/* local ticket functions */
104STATIC void xlog_state_ticket_alloc(xlog_t *log);
105STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
106 int unit_bytes,
107 int count,
108 char clientid,
109 uint flags);
110STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
111
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100112#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
114STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
115STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
116 int count, boolean_t syncing);
117STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
118 xfs_lsn_t tail_lsn);
119#else
120#define xlog_verify_dest_ptr(a,b)
121#define xlog_verify_grant_head(a,b)
122#define xlog_verify_iclog(a,b,c,d)
123#define xlog_verify_tail_lsn(a,b,c)
124#endif
125
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000126STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#if defined(XFS_LOG_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void
130xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
131{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000132 unsigned long cnts;
133
134 if (!log->l_grant_trace) {
135 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
136 if (!log->l_grant_trace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
138 }
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000139 /* ticket counts are 1 byte each */
140 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 ktrace_enter(log->l_grant_trace,
143 (void *)tic,
144 (void *)log->l_reserve_headq,
145 (void *)log->l_write_headq,
146 (void *)((unsigned long)log->l_grant_reserve_cycle),
147 (void *)((unsigned long)log->l_grant_reserve_bytes),
148 (void *)((unsigned long)log->l_grant_write_cycle),
149 (void *)((unsigned long)log->l_grant_write_bytes),
150 (void *)((unsigned long)log->l_curr_cycle),
151 (void *)((unsigned long)log->l_curr_block),
152 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
153 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
154 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000155 (void *)((unsigned long)tic->t_trans_type),
156 (void *)cnts,
157 (void *)((unsigned long)tic->t_curr_res),
158 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void
162xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!iclog->ic_trace)
165 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
166 ktrace_enter(iclog->ic_trace,
167 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100168 (void *)((unsigned long)current_pid()),
169 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
170 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
171 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
172 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#else
175#define xlog_trace_loggrant(log,tic,string)
176#define xlog_trace_iclog(iclog,state)
177#endif /* XFS_LOG_TRACE */
178
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100179
180static void
181xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
182{
183 if (*qp) {
184 tic->t_next = (*qp);
185 tic->t_prev = (*qp)->t_prev;
186 (*qp)->t_prev->t_next = tic;
187 (*qp)->t_prev = tic;
188 } else {
189 tic->t_prev = tic->t_next = tic;
190 *qp = tic;
191 }
192
193 tic->t_flags |= XLOG_TIC_IN_Q;
194}
195
196static void
197xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
198{
199 if (tic == tic->t_next) {
200 *qp = NULL;
201 } else {
202 *qp = tic->t_next;
203 tic->t_next->t_prev = tic->t_prev;
204 tic->t_prev->t_next = tic->t_next;
205 }
206
207 tic->t_next = tic->t_prev = NULL;
208 tic->t_flags &= ~XLOG_TIC_IN_Q;
209}
210
211static void
212xlog_grant_sub_space(struct log *log, int bytes)
213{
214 log->l_grant_write_bytes -= bytes;
215 if (log->l_grant_write_bytes < 0) {
216 log->l_grant_write_bytes += log->l_logsize;
217 log->l_grant_write_cycle--;
218 }
219
220 log->l_grant_reserve_bytes -= bytes;
221 if ((log)->l_grant_reserve_bytes < 0) {
222 log->l_grant_reserve_bytes += log->l_logsize;
223 log->l_grant_reserve_cycle--;
224 }
225
226}
227
228static void
229xlog_grant_add_space_write(struct log *log, int bytes)
230{
231 log->l_grant_write_bytes += bytes;
232 if (log->l_grant_write_bytes > log->l_logsize) {
233 log->l_grant_write_bytes -= log->l_logsize;
234 log->l_grant_write_cycle++;
235 }
236}
237
238static void
239xlog_grant_add_space_reserve(struct log *log, int bytes)
240{
241 log->l_grant_reserve_bytes += bytes;
242 if (log->l_grant_reserve_bytes > log->l_logsize) {
243 log->l_grant_reserve_bytes -= log->l_logsize;
244 log->l_grant_reserve_cycle++;
245 }
246}
247
248static inline void
249xlog_grant_add_space(struct log *log, int bytes)
250{
251 xlog_grant_add_space_write(log, bytes);
252 xlog_grant_add_space_reserve(log, bytes);
253}
254
Christoph Hellwig0adba532007-08-30 17:21:46 +1000255static void
256xlog_tic_reset_res(xlog_ticket_t *tic)
257{
258 tic->t_res_num = 0;
259 tic->t_res_arr_sum = 0;
260 tic->t_res_num_ophdrs = 0;
261}
262
263static void
264xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
265{
266 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
267 /* add to overflow and start again */
268 tic->t_res_o_flow += tic->t_res_arr_sum;
269 tic->t_res_num = 0;
270 tic->t_res_arr_sum = 0;
271 }
272
273 tic->t_res_arr[tic->t_res_num].r_len = len;
274 tic->t_res_arr[tic->t_res_num].r_type = type;
275 tic->t_res_arr_sum += len;
276 tic->t_res_num++;
277}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * NOTES:
281 *
282 * 1. currblock field gets updated at startup and after in-core logs
283 * marked as with WANT_SYNC.
284 */
285
286/*
287 * This routine is called when a user of a log manager ticket is done with
288 * the reservation. If the ticket was ever used, then a commit record for
289 * the associated transaction is written out as a log operation header with
290 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
291 * a given ticket. If the ticket was one with a permanent reservation, then
292 * a few operations are done differently. Permanent reservation tickets by
293 * default don't release the reservation. They just commit the current
294 * transaction with the belief that the reservation is still needed. A flag
295 * must be passed in before permanent reservations are actually released.
296 * When these type of tickets are not released, they need to be set into
297 * the inited state again. By doing this, a start record will be written
298 * out when the next write occurs.
299 */
300xfs_lsn_t
301xfs_log_done(xfs_mount_t *mp,
302 xfs_log_ticket_t xtic,
303 void **iclog,
304 uint flags)
305{
306 xlog_t *log = mp->m_log;
307 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
308 xfs_lsn_t lsn = 0;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (XLOG_FORCED_SHUTDOWN(log) ||
311 /*
312 * If nothing was ever written, don't write out commit record.
313 * If we get an error, just continue and give back the log ticket.
314 */
315 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
316 (xlog_commit_record(mp, ticket,
317 (xlog_in_core_t **)iclog, &lsn)))) {
318 lsn = (xfs_lsn_t) -1;
319 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
320 flags |= XFS_LOG_REL_PERM_RESERV;
321 }
322 }
323
324
325 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
326 (flags & XFS_LOG_REL_PERM_RESERV)) {
327 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000328 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * request has been made to release a permanent reservation.
330 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000331 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xlog_ungrant_log_space(log, ticket);
333 xlog_state_put_ticket(log, ticket);
334 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000335 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 xlog_regrant_reserve_log_space(log, ticket);
337 }
338
339 /* If this ticket was a permanent reservation and we aren't
340 * trying to release it, reset the inited flags; so next time
341 * we write, a start record will be written out.
342 */
343 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
344 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
345 ticket->t_flags |= XLOG_TIC_INITED;
346
347 return lsn;
348} /* xfs_log_done */
349
350
351/*
352 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
353 * the force is done synchronously.
354 *
355 * Asynchronous forces are implemented by setting the WANT_SYNC
356 * bit in the appropriate in-core log and then returning.
357 *
358 * Synchronous forces are implemented with a semaphore. All callers
359 * to force a given lsn to disk will wait on a semaphore attached to the
360 * specific in-core log. When given in-core log finally completes its
361 * write to disk, that thread will wake up all threads waiting on the
362 * semaphore.
363 */
364int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100365_xfs_log_force(
366 xfs_mount_t *mp,
367 xfs_lsn_t lsn,
368 uint flags,
369 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100371 xlog_t *log = mp->m_log;
372 int dummy;
373
374 if (!log_flushed)
375 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ASSERT(flags & XFS_LOG_FORCE);
378
379 XFS_STATS_INC(xs_log_force);
380
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100381 if (log->l_flags & XLOG_IO_ERROR)
382 return XFS_ERROR(EIO);
383 if (lsn == 0)
384 return xlog_state_sync_all(log, flags, log_flushed);
385 else
386 return xlog_state_sync(log, lsn, flags, log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387} /* xfs_log_force */
388
389/*
390 * Attaches a new iclog I/O completion callback routine during
391 * transaction commit. If the log is in error state, a non-zero
392 * return code is handed back and the caller is responsible for
393 * executing the callback at an appropriate time.
394 */
395int
396xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
397 void *iclog_hndl, /* iclog to hang callback off */
398 xfs_log_callback_t *cb)
399{
400 xlog_t *log = mp->m_log;
401 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000402 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cb->cb_next = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000405 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
407 if (!abortflg) {
408 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
409 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
410 cb->cb_next = NULL;
411 *(iclog->ic_callback_tail) = cb;
412 iclog->ic_callback_tail = &(cb->cb_next);
413 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000414 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return abortflg;
416} /* xfs_log_notify */
417
418int
419xfs_log_release_iclog(xfs_mount_t *mp,
420 void *iclog_hndl)
421{
422 xlog_t *log = mp->m_log;
423 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
424
425 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000426 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100427 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 return 0;
431}
432
433/*
434 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
435 * to the reservation.
436 * 2. Potentially, push buffers at tail of log to disk.
437 *
438 * Each reservation is going to reserve extra space for a log record header.
439 * When writes happen to the on-disk log, we don't subtract the length of the
440 * log record header from any reservation. By wasting space in each
441 * reservation, we prevent over allocation problems.
442 */
443int
444xfs_log_reserve(xfs_mount_t *mp,
445 int unit_bytes,
446 int cnt,
447 xfs_log_ticket_t *ticket,
448 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000449 uint flags,
450 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 xlog_t *log = mp->m_log;
453 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100454 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
457 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
458
459 if (XLOG_FORCED_SHUTDOWN(log))
460 return XFS_ERROR(EIO);
461
462 XFS_STATS_INC(xs_try_logspace);
463
464 if (*ticket != NULL) {
465 ASSERT(flags & XFS_LOG_PERM_RESERV);
466 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000467 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
469 retval = xlog_regrant_write_log_space(log, internal_ticket);
470 } else {
471 /* may sleep if need to allocate more tickets */
472 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
473 client, flags);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000474 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000476 xlog_trace_loggrant(log, internal_ticket,
477 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
478 "xfs_log_reserve: create new ticket (permanent trans)" :
479 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 xlog_grant_push_ail(mp,
481 (internal_ticket->t_unit_res *
482 internal_ticket->t_cnt));
483 retval = xlog_grant_log_space(log, internal_ticket);
484 }
485
486 return retval;
487} /* xfs_log_reserve */
488
489
490/*
491 * Mount a log filesystem
492 *
493 * mp - ubiquitous xfs mount point structure
494 * log_target - buftarg of on-disk log device
495 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
496 * num_bblocks - Number of BBSIZE blocks in on-disk log
497 *
498 * Return error or zero.
499 */
500int
501xfs_log_mount(xfs_mount_t *mp,
502 xfs_buftarg_t *log_target,
503 xfs_daddr_t blk_offset,
504 int num_bblks)
505{
506 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
507 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
508 else {
509 cmn_err(CE_NOTE,
510 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
511 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000512 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * skip log recovery on a norecovery mount. pretend it all
519 * just worked.
520 */
521 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000522 int error, readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000525 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Eric Sandeen65be6052006-01-11 15:34:19 +1100527 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000530 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (error) {
532 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Nathan Scottc41564b2006-03-29 08:55:14 +1000533 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return error;
535 }
536 }
537
538 /* Normal transactions can now occur */
539 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
540
541 /* End mounting message in xfs_log_mount_finish */
542 return 0;
543} /* xfs_log_mount */
544
545/*
546 * Finish the recovery of the file system. This is separate from
547 * the xfs_log_mount() call, because it depends on the code in
548 * xfs_mountfs() to read in the root and real-time bitmap inodes
549 * between calling xfs_log_mount() and here.
550 *
551 * mp - ubiquitous xfs mount point structure
552 */
553int
554xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
555{
556 int error;
557
558 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
559 error = xlog_recover_finish(mp->m_log, mfsi_flags);
560 else {
561 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000562 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 return error;
566}
567
568/*
569 * Unmount processing for the log.
570 */
571int
572xfs_log_unmount(xfs_mount_t *mp)
573{
574 int error;
575
576 error = xfs_log_unmount_write(mp);
577 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
582 * Final log writes as part of unmount.
583 *
584 * Mark the filesystem clean as unmount happens. Note that during relocation
585 * this routine needs to be executed as part of source-bag while the
586 * deallocation must not be done until source-end.
587 */
588
589/*
590 * Unmount record used to have a string "Unmount filesystem--" in the
591 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
592 * We just write the magic number now since that particular field isn't
593 * currently architecture converted and "nUmount" is a bit foo.
594 * As far as I know, there weren't any dependencies on the old behaviour.
595 */
596
597int
598xfs_log_unmount_write(xfs_mount_t *mp)
599{
600 xlog_t *log = mp->m_log;
601 xlog_in_core_t *iclog;
602#ifdef DEBUG
603 xlog_in_core_t *first_iclog;
604#endif
605 xfs_log_iovec_t reg[1];
606 xfs_log_ticket_t tic = NULL;
607 xfs_lsn_t lsn;
608 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* the data section must be 32 bit size aligned */
611 struct {
612 __uint16_t magic;
613 __uint16_t pad1;
614 __uint32_t pad2; /* may as well make it 64 bits */
615 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /*
618 * Don't write out unmount record on read-only mounts.
619 * Or, if we are doing a forced umount (typically because of IO errors).
620 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000621 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623
624 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
625
626#ifdef DEBUG
627 first_iclog = iclog = log->l_iclog;
628 do {
629 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
630 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
631 ASSERT(iclog->ic_offset == 0);
632 }
633 iclog = iclog->ic_next;
634 } while (iclog != first_iclog);
635#endif
636 if (! (XLOG_FORCED_SHUTDOWN(log))) {
637 reg[0].i_addr = (void*)&magic;
638 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000639 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Tim Shimmin955e47a2006-09-28 11:04:16 +1000641 error = xfs_log_reserve(mp, 600, 1, &tic,
642 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!error) {
644 /* remove inited flag */
645 ((xlog_ticket_t *)tic)->t_flags = 0;
646 error = xlog_write(mp, reg, 1, tic, &lsn,
647 NULL, XLOG_UNMOUNT_TRANS);
648 /*
649 * At this point, we're umounting anyway,
650 * so there's no point in transitioning log state
651 * to IOERROR. Just continue...
652 */
653 }
654
655 if (error) {
656 xfs_fs_cmn_err(CE_ALERT, mp,
657 "xfs_log_unmount: unmount record failed");
658 }
659
660
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000661 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 iclog = log->l_iclog;
663 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000664 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xlog_state_want_sync(log, iclog);
666 (void) xlog_state_release_iclog(log, iclog);
667
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000668 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
670 iclog->ic_state == XLOG_STATE_DIRTY)) {
671 if (!XLOG_FORCED_SHUTDOWN(log)) {
672 sv_wait(&iclog->ic_forcesema, PMEM,
673 &log->l_icloglock, s);
674 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000675 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000678 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000680 if (tic) {
681 xlog_trace_loggrant(log, tic, "unmount rec");
682 xlog_ungrant_log_space(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xlog_state_put_ticket(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 } else {
686 /*
687 * We're already in forced_shutdown mode, couldn't
688 * even attempt to write out the unmount transaction.
689 *
690 * Go through the motions of sync'ing and releasing
691 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000692 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * be in progress. Do this as a separate section of
694 * code so we'll know if we ever get stuck here that
695 * we're in this odd situation of trying to unmount
696 * a file system that went into forced_shutdown as
697 * the result of an unmount..
698 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000699 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 iclog = log->l_iclog;
701 iclog->ic_refcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000702 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 xlog_state_want_sync(log, iclog);
705 (void) xlog_state_release_iclog(log, iclog);
706
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000707 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
710 || iclog->ic_state == XLOG_STATE_DIRTY
711 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
712
713 sv_wait(&iclog->ic_forcesema, PMEM,
714 &log->l_icloglock, s);
715 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000716 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 }
719
720 return 0;
721} /* xfs_log_unmount_write */
722
723/*
724 * Deallocate log structures for unmount/relocation.
725 */
726void
727xfs_log_unmount_dealloc(xfs_mount_t *mp)
728{
Nathan Scottc41564b2006-03-29 08:55:14 +1000729 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/*
733 * Write region vectors to log. The write happens using the space reservation
734 * of the ticket (tic). It is not a requirement that all writes for a given
735 * transaction occur with one call to xfs_log_write().
736 */
737int
738xfs_log_write(xfs_mount_t * mp,
739 xfs_log_iovec_t reg[],
740 int nentries,
741 xfs_log_ticket_t tic,
742 xfs_lsn_t *start_lsn)
743{
744 int error;
745 xlog_t *log = mp->m_log;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (XLOG_FORCED_SHUTDOWN(log))
748 return XFS_ERROR(EIO);
749
750 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000751 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754} /* xfs_log_write */
755
756
757void
758xfs_log_move_tail(xfs_mount_t *mp,
759 xfs_lsn_t tail_lsn)
760{
761 xlog_ticket_t *tic;
762 xlog_t *log = mp->m_log;
763 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (XLOG_FORCED_SHUTDOWN(log))
766 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (tail_lsn == 0) {
769 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000770 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000772 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000775 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
778 * tail_lsn.
779 */
780 if (tail_lsn != 1) {
781 log->l_tail_lsn = tail_lsn;
782 }
783
784 if ((tic = log->l_write_headq)) {
785#ifdef DEBUG
786 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
787 panic("Recovery problem");
788#endif
789 cycle = log->l_grant_write_cycle;
790 bytes = log->l_grant_write_bytes;
791 free_bytes = xlog_space_left(log, cycle, bytes);
792 do {
793 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
794
795 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
796 break;
797 tail_lsn = 0;
798 free_bytes -= tic->t_unit_res;
799 sv_signal(&tic->t_sema);
800 tic = tic->t_next;
801 } while (tic != log->l_write_headq);
802 }
803 if ((tic = log->l_reserve_headq)) {
804#ifdef DEBUG
805 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
806 panic("Recovery problem");
807#endif
808 cycle = log->l_grant_reserve_cycle;
809 bytes = log->l_grant_reserve_bytes;
810 free_bytes = xlog_space_left(log, cycle, bytes);
811 do {
812 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
813 need_bytes = tic->t_unit_res*tic->t_cnt;
814 else
815 need_bytes = tic->t_unit_res;
816 if (free_bytes < need_bytes && tail_lsn != 1)
817 break;
818 tail_lsn = 0;
819 free_bytes -= need_bytes;
820 sv_signal(&tic->t_sema);
821 tic = tic->t_next;
822 } while (tic != log->l_reserve_headq);
823 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000824 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825} /* xfs_log_move_tail */
826
827/*
828 * Determine if we have a transaction that has gone to disk
829 * that needs to be covered. Log activity needs to be idle (no AIL and
830 * nothing in the iclogs). And, we need to be in the right state indicating
831 * something has gone out.
832 */
833int
834xfs_log_need_covered(xfs_mount_t *mp)
835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int needed = 0, gen;
837 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David Chinner92821e22007-05-24 15:26:31 +1000839 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000842 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
844 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
845 && !xfs_trans_first_ail(mp, &gen)
846 && xlog_iclogs_empty(log)) {
847 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
848 log->l_covered_state = XLOG_STATE_COVER_DONE;
849 else {
850 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
851 log->l_covered_state = XLOG_STATE_COVER_DONE2;
852 }
853 needed = 1;
854 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000855 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100856 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/******************************************************************************
860 *
861 * local routines
862 *
863 ******************************************************************************
864 */
865
866/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
867 * The log manager must keep track of the last LR which was committed
868 * to disk. The lsn of this LR will become the new tail_lsn whenever
869 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
870 * the situation where stuff could be written into the log but nothing
871 * was ever in the AIL when asked. Eventually, we panic since the
872 * tail hits the head.
873 *
874 * We may be holding the log iclog lock upon entering this routine.
875 */
876xfs_lsn_t
877xlog_assign_tail_lsn(xfs_mount_t *mp)
878{
879 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 xlog_t *log = mp->m_log;
881
882 tail_lsn = xfs_trans_tail_ail(mp);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000883 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (tail_lsn != 0) {
885 log->l_tail_lsn = tail_lsn;
886 } else {
887 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
888 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000889 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 return tail_lsn;
892} /* xlog_assign_tail_lsn */
893
894
895/*
896 * Return the space in the log between the tail and the head. The head
897 * is passed in the cycle/bytes formal parms. In the special case where
898 * the reserve head has wrapped passed the tail, this calculation is no
899 * longer valid. In this case, just return 0 which means there is no space
900 * in the log. This works for all places where this function is called
901 * with the reserve head. Of course, if the write head were to ever
902 * wrap the tail, we should blow up. Rather than catch this case here,
903 * we depend on other ASSERTions in other parts of the code. XXXmiken
904 *
905 * This code also handles the case where the reservation head is behind
906 * the tail. The details of this case are described below, but the end
907 * result is that we return the size of the log as the amount of space left.
908 */
David Chinnera8272ce2007-11-23 16:28:09 +1100909STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910xlog_space_left(xlog_t *log, int cycle, int bytes)
911{
912 int free_bytes;
913 int tail_bytes;
914 int tail_cycle;
915
916 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
917 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
918 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
919 free_bytes = log->l_logsize - (bytes - tail_bytes);
920 } else if ((tail_cycle + 1) < cycle) {
921 return 0;
922 } else if (tail_cycle < cycle) {
923 ASSERT(tail_cycle == (cycle - 1));
924 free_bytes = tail_bytes - bytes;
925 } else {
926 /*
927 * The reservation head is behind the tail.
928 * In this case we just want to return the size of the
929 * log as the amount of space left.
930 */
931 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
932 "xlog_space_left: head behind tail\n"
933 " tail_cycle = %d, tail_bytes = %d\n"
934 " GH cycle = %d, GH bytes = %d",
935 tail_cycle, tail_bytes, cycle, bytes);
936 ASSERT(0);
937 free_bytes = log->l_logsize;
938 }
939 return free_bytes;
940} /* xlog_space_left */
941
942
943/*
944 * Log function which is called when an io completes.
945 *
946 * The log manager needs its own routine, in order to control what
947 * happens with the buffer after the write completes.
948 */
949void
950xlog_iodone(xfs_buf_t *bp)
951{
952 xlog_in_core_t *iclog;
953 xlog_t *l;
954 int aborted;
955
956 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
957 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
958 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
959 aborted = 0;
960
961 /*
962 * Some versions of cpp barf on the recursive definition of
963 * ic_log -> hic_fields.ic_log and expand ic_log twice when
964 * it is passed through two macros. Workaround broken cpp.
965 */
966 l = iclog->ic_log;
967
968 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000969 * If the ordered flag has been removed by a lower
970 * layer, it means the underlyin device no longer supports
971 * barrier I/O. Warn loudly and turn off barriers.
972 */
973 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
974 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
975 xfs_fs_cmn_err(CE_WARN, l->l_mp,
976 "xlog_iodone: Barriers are no longer supported"
977 " by device. Disabling barriers\n");
978 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
979 }
980
981 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Race to shutdown the filesystem if we see an error.
983 */
984 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
985 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
986 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
987 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000988 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /*
990 * This flag will be propagated to the trans-committed
991 * callback routines to let them know that the log-commit
992 * didn't succeed.
993 */
994 aborted = XFS_LI_ABORTED;
995 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
996 aborted = XFS_LI_ABORTED;
997 }
David Chinner3db296f2007-05-14 18:24:16 +1000998
999 /* log I/O is always issued ASYNC */
1000 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001002 /*
1003 * do not reference the buffer (bp) here as we could race
1004 * with it being freed after writing the unmount record to the
1005 * log.
1006 */
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008} /* xlog_iodone */
1009
1010/*
1011 * The bdstrat callback function for log bufs. This gives us a central
1012 * place to trap bufs in case we get hit by a log I/O error and need to
1013 * shutdown. Actually, in practice, even when we didn't get a log error,
1014 * we transition the iclogs to IOERROR state *after* flushing all existing
1015 * iclogs to disk. This is because we don't want anymore new transactions to be
1016 * started or completed afterwards.
1017 */
1018STATIC int
1019xlog_bdstrat_cb(struct xfs_buf *bp)
1020{
1021 xlog_in_core_t *iclog;
1022
1023 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1024
1025 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1026 /* note for irix bstrat will need struct bdevsw passed
1027 * Fix the following macro if the code ever is merged
1028 */
1029 XFS_bdstrat(bp);
1030 return 0;
1031 }
1032
1033 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1034 XFS_BUF_ERROR(bp, EIO);
1035 XFS_BUF_STALE(bp);
1036 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001037 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039
1040}
1041
1042/*
1043 * Return size of each in-core log record buffer.
1044 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001045 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 *
1047 * If the filesystem blocksize is too large, we may need to choose a
1048 * larger size since the directory code currently logs entire blocks.
1049 */
1050
1051STATIC void
1052xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1053 xlog_t *log)
1054{
1055 int size;
1056 int xhdrs;
1057
Eric Sandeen1cb51252007-08-16 16:24:43 +10001058 if (mp->m_logbufs <= 0)
1059 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1060 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001061 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /*
1064 * Buffer size passed in from mount system call.
1065 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001066 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 size = log->l_iclog_size = mp->m_logbsize;
1068 log->l_iclog_size_log = 0;
1069 while (size != 1) {
1070 log->l_iclog_size_log++;
1071 size >>= 1;
1072 }
1073
1074 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1075 /* # headers = size / 32K
1076 * one header holds cycles from 32K of data
1077 */
1078
1079 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1080 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1081 xhdrs++;
1082 log->l_iclog_hsize = xhdrs << BBSHIFT;
1083 log->l_iclog_heads = xhdrs;
1084 } else {
1085 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1086 log->l_iclog_hsize = BBSIZE;
1087 log->l_iclog_heads = 1;
1088 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001089 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
Eric Sandeen1cb51252007-08-16 16:24:43 +10001092 /* All machines use 32KB buffers by default. */
1093 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1094 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /* the default log size is 16k or 32k which is one header sector */
1097 log->l_iclog_hsize = BBSIZE;
1098 log->l_iclog_heads = 1;
1099
1100 /*
1101 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1102 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1103 */
1104 if (mp->m_sb.sb_blocksize >= 16*1024) {
1105 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1106 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001107 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 switch (mp->m_sb.sb_blocksize) {
1109 case 16*1024: /* 16 KB */
1110 log->l_iclog_bufs = 3;
1111 break;
1112 case 32*1024: /* 32 KB */
1113 log->l_iclog_bufs = 4;
1114 break;
1115 case 64*1024: /* 64 KB */
1116 log->l_iclog_bufs = 8;
1117 break;
1118 default:
1119 xlog_panic("XFS: Invalid blocksize");
1120 break;
1121 }
1122 }
1123 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001124
1125done: /* are we being asked to make the sizes selected above visible? */
1126 if (mp->m_logbufs == 0)
1127 mp->m_logbufs = log->l_iclog_bufs;
1128 if (mp->m_logbsize == 0)
1129 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130} /* xlog_get_iclog_buffer_size */
1131
1132
1133/*
1134 * This routine initializes some of the log structure for a given mount point.
1135 * Its primary purpose is to fill in enough, so recovery can occur. However,
1136 * some other stuff may be filled in too.
1137 */
1138STATIC xlog_t *
1139xlog_alloc_log(xfs_mount_t *mp,
1140 xfs_buftarg_t *log_target,
1141 xfs_daddr_t blk_offset,
1142 int num_bblks)
1143{
1144 xlog_t *log;
1145 xlog_rec_header_t *head;
1146 xlog_in_core_t **iclogp;
1147 xlog_in_core_t *iclog, *prev_iclog=NULL;
1148 xfs_buf_t *bp;
1149 int i;
1150 int iclogsize;
1151
1152 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1153
1154 log->l_mp = mp;
1155 log->l_targ = log_target;
1156 log->l_logsize = BBTOB(num_bblks);
1157 log->l_logBBstart = blk_offset;
1158 log->l_logBBsize = num_bblks;
1159 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1160 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1161
1162 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001163 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1165 log->l_last_sync_lsn = log->l_tail_lsn;
1166 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1167 log->l_grant_reserve_cycle = 1;
1168 log->l_grant_write_cycle = 1;
1169
1170 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1171 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1172 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1173 /* for larger sector sizes, must have v2 or external log */
1174 ASSERT(log->l_sectbb_log == 0 ||
1175 log->l_logBBstart == 0 ||
1176 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1177 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1178 }
1179 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1180
1181 xlog_get_iclog_buffer_size(mp, log);
1182
1183 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1184 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1185 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1186 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1187 ASSERT(XFS_BUF_ISBUSY(bp));
1188 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1189 log->l_xbuf = bp;
1190
Eric Sandeen007c61c2007-10-11 17:43:56 +10001191 spin_lock_init(&log->l_icloglock);
1192 spin_lock_init(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 initnsema(&log->l_flushsema, 0, "ic-flush");
1194 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1195
1196 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1197 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1198
1199 iclogp = &log->l_iclog;
1200 /*
1201 * The amount of memory to allocate for the iclog structure is
1202 * rather funky due to the way the structure is defined. It is
1203 * done this way so that we can use different sizes for machines
1204 * with different amounts of memory. See the definition of
1205 * xlog_in_core_t in xfs_log_priv.h for details.
1206 */
1207 iclogsize = log->l_iclog_size;
1208 ASSERT(log->l_iclog_size >= 4096);
1209 for (i=0; i < log->l_iclog_bufs; i++) {
1210 *iclogp = (xlog_in_core_t *)
1211 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1212 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 iclog->ic_prev = prev_iclog;
1214 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001215
1216 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1217 if (!XFS_BUF_CPSEMA(bp))
1218 ASSERT(0);
1219 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1220 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1221 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1222 iclog->ic_bp = bp;
1223 iclog->hic_data = bp->b_addr;
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1226
1227 head = &iclog->ic_header;
1228 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001229 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1230 head->h_version = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001232 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001234 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1239 iclog->ic_state = XLOG_STATE_ACTIVE;
1240 iclog->ic_log = log;
1241 iclog->ic_callback_tail = &(iclog->ic_callback);
1242 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1243
1244 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1245 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1246 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1247 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1248
1249 iclogp = &iclog->ic_next;
1250 }
1251 *iclogp = log->l_iclog; /* complete ring */
1252 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1253
1254 return log;
1255} /* xlog_alloc_log */
1256
1257
1258/*
1259 * Write out the commit record of a transaction associated with the given
1260 * ticket. Return the lsn of the commit record.
1261 */
1262STATIC int
1263xlog_commit_record(xfs_mount_t *mp,
1264 xlog_ticket_t *ticket,
1265 xlog_in_core_t **iclog,
1266 xfs_lsn_t *commitlsnp)
1267{
1268 int error;
1269 xfs_log_iovec_t reg[1];
1270
1271 reg[0].i_addr = NULL;
1272 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001273 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 ASSERT_ALWAYS(iclog);
1276 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1277 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001278 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001280 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281} /* xlog_commit_record */
1282
1283
1284/*
1285 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1286 * log space. This code pushes on the lsn which would supposedly free up
1287 * the 25% which we want to leave free. We may need to adopt a policy which
1288 * pushes on an lsn which is further along in the log once we reach the high
1289 * water mark. In this manner, we would be creating a low water mark.
1290 */
David Chinnera8272ce2007-11-23 16:28:09 +11001291STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292xlog_grant_push_ail(xfs_mount_t *mp,
1293 int need_bytes)
1294{
1295 xlog_t *log = mp->m_log; /* pointer to the log */
1296 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1297 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1298 int free_blocks; /* free blocks left to write to */
1299 int free_bytes; /* free bytes left to write to */
1300 int threshold_block; /* block in lsn we'd like to be at */
1301 int threshold_cycle; /* lsn cycle we'd like to be at */
1302 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1305
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001306 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 free_bytes = xlog_space_left(log,
1308 log->l_grant_reserve_cycle,
1309 log->l_grant_reserve_bytes);
1310 tail_lsn = log->l_tail_lsn;
1311 free_blocks = BTOBBT(free_bytes);
1312
1313 /*
1314 * Set the threshold for the minimum number of free blocks in the
1315 * log to the maximum of what the caller needs, one quarter of the
1316 * log, and 256 blocks.
1317 */
1318 free_threshold = BTOBB(need_bytes);
1319 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1320 free_threshold = MAX(free_threshold, 256);
1321 if (free_blocks < free_threshold) {
1322 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1323 threshold_cycle = CYCLE_LSN(tail_lsn);
1324 if (threshold_block >= log->l_logBBsize) {
1325 threshold_block -= log->l_logBBsize;
1326 threshold_cycle += 1;
1327 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001328 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Don't pass in an lsn greater than the lsn of the last
1331 * log record known to be on disk.
1332 */
1333 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1334 threshold_lsn = log->l_last_sync_lsn;
1335 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001336 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /*
1339 * Get the transaction layer to kick the dirty buffers out to
1340 * disk asynchronously. No point in trying to do this if
1341 * the filesystem is shutting down.
1342 */
1343 if (threshold_lsn &&
1344 !XLOG_FORCED_SHUTDOWN(log))
1345 xfs_trans_push_ail(mp, threshold_lsn);
1346} /* xlog_grant_push_ail */
1347
1348
1349/*
1350 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1351 * fashion. Previously, we should have moved the current iclog
1352 * ptr in the log to point to the next available iclog. This allows further
1353 * write to continue while this code syncs out an iclog ready to go.
1354 * Before an in-core log can be written out, the data section must be scanned
1355 * to save away the 1st word of each BBSIZE block into the header. We replace
1356 * it with the current cycle count. Each BBSIZE block is tagged with the
1357 * cycle count because there in an implicit assumption that drives will
1358 * guarantee that entire 512 byte blocks get written at once. In other words,
1359 * we can't have part of a 512 byte block written and part not written. By
1360 * tagging each block, we will know which blocks are valid when recovering
1361 * after an unclean shutdown.
1362 *
1363 * This routine is single threaded on the iclog. No other thread can be in
1364 * this routine with the same iclog. Changing contents of iclog can there-
1365 * fore be done without grabbing the state machine lock. Updating the global
1366 * log will require grabbing the lock though.
1367 *
1368 * The entire log manager uses a logical block numbering scheme. Only
1369 * log_sync (and then only bwrite()) know about the fact that the log may
1370 * not start with block zero on a given device. The log block start offset
1371 * is added immediately before calling bwrite().
1372 */
1373
David Chinnera8272ce2007-11-23 16:28:09 +11001374STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375xlog_sync(xlog_t *log,
1376 xlog_in_core_t *iclog)
1377{
1378 xfs_caddr_t dptr; /* pointer to byte sized element */
1379 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001380 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 uint count; /* byte count of bwrite */
1382 uint count_init; /* initial count before roundup */
1383 int roundoff; /* roundoff to BB or stripe */
1384 int split = 0; /* split write into two regions */
1385 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1387
1388 XFS_STATS_INC(xs_log_writes);
1389 ASSERT(iclog->ic_refcnt == 0);
1390
1391 /* Add for LR header */
1392 count_init = log->l_iclog_hsize + iclog->ic_offset;
1393
1394 /* Round out the log write size */
1395 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1396 /* we have a v2 stripe unit to use */
1397 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1398 } else {
1399 count = BBTOB(BTOBB(count_init));
1400 }
1401 roundoff = count - count_init;
1402 ASSERT(roundoff >= 0);
1403 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1404 roundoff < log->l_mp->m_sb.sb_logsunit)
1405 ||
1406 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1407 roundoff < BBTOB(1)));
1408
1409 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001410 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001411 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001412 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /* put cycle number in every block */
1415 xlog_pack_data(log, iclog, roundoff);
1416
1417 /* real byte length */
1418 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001419 iclog->ic_header.h_len =
1420 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001422 iclog->ic_header.h_len =
1423 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
Nathan Scottf5faad72006-07-28 17:04:44 +10001426 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1428 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001429 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1432
1433 /* Do we need to split this write into 2 parts? */
1434 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1435 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1436 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1437 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1438 } else {
1439 iclog->ic_bwritecnt = 1;
1440 }
David Chinner511105b2007-05-24 15:21:57 +10001441 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001443 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 XFS_BUF_BUSY(bp);
1445 XFS_BUF_ASYNC(bp);
1446 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001447 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001448 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001450 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001451 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1454 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1455
1456 xlog_verify_iclog(log, iclog, count, B_TRUE);
1457
1458 /* account for log which doesn't start at block #0 */
1459 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1460 /*
1461 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1462 * is shutting down.
1463 */
1464 XFS_BUF_WRITE(bp);
1465
1466 if ((error = XFS_bwrite(bp))) {
1467 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1468 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001469 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001472 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1474 (unsigned long)1);
1475 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1476 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1477 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1478 (__psint_t)count), split);
1479 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001480 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 XFS_BUF_BUSY(bp);
1482 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001483 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1484 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 dptr = XFS_BUF_PTR(bp);
1486 /*
1487 * Bump the cycle numbers at the start of each block
1488 * since this part of the buffer is at the start of
1489 * a new cycle. Watch out for the header magic number
1490 * case, though.
1491 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001492 for (i = 0; i < split; i += BBSIZE) {
1493 be32_add((__be32 *)dptr, 1);
1494 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
1495 be32_add((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 dptr += BBSIZE;
1497 }
1498
1499 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1500 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1501
Nathan Scottc41564b2006-03-29 08:55:14 +10001502 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1504 XFS_BUF_WRITE(bp);
1505 if ((error = XFS_bwrite(bp))) {
1506 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1507 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001508 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512} /* xlog_sync */
1513
1514
1515/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001516 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
David Chinnera8272ce2007-11-23 16:28:09 +11001518STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001519xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 xlog_in_core_t *iclog, *next_iclog;
1522 xlog_ticket_t *tic, *next_tic;
1523 int i;
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 iclog = log->l_iclog;
1526 for (i=0; i<log->l_iclog_bufs; i++) {
1527 sv_destroy(&iclog->ic_forcesema);
1528 sv_destroy(&iclog->ic_writesema);
1529 xfs_buf_free(iclog->ic_bp);
1530#ifdef XFS_LOG_TRACE
1531 if (iclog->ic_trace != NULL) {
1532 ktrace_free(iclog->ic_trace);
1533 }
1534#endif
1535 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 kmem_free(iclog, sizeof(xlog_in_core_t));
1537 iclog = next_iclog;
1538 }
1539 freesema(&log->l_flushsema);
1540 spinlock_destroy(&log->l_icloglock);
1541 spinlock_destroy(&log->l_grant_lock);
1542
1543 /* XXXsup take a look at this again. */
1544 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1545 !XLOG_FORCED_SHUTDOWN(log)) {
1546 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001547 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 log->l_ticket_cnt, log->l_ticket_tcnt);
1549 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1550
1551 } else {
1552 tic = log->l_unmount_free;
1553 while (tic) {
1554 next_tic = tic->t_next;
Tim Shimmine6a4b372007-11-23 16:30:42 +11001555 kmem_free(tic, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 tic = next_tic;
1557 }
1558 }
1559 xfs_buf_free(log->l_xbuf);
1560#ifdef XFS_LOG_TRACE
1561 if (log->l_trace != NULL) {
1562 ktrace_free(log->l_trace);
1563 }
1564 if (log->l_grant_trace != NULL) {
1565 ktrace_free(log->l_grant_trace);
1566 }
1567#endif
1568 log->l_mp->m_log = NULL;
1569 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001570} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572/*
1573 * Update counters atomically now that memcpy is done.
1574 */
1575/* ARGSUSED */
1576static inline void
1577xlog_state_finish_copy(xlog_t *log,
1578 xlog_in_core_t *iclog,
1579 int record_cnt,
1580 int copy_bytes)
1581{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001582 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001584 be32_add(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 iclog->ic_offset += copy_bytes;
1586
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001587 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588} /* xlog_state_finish_copy */
1589
1590
1591
1592
1593/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001594 * print out info relating to regions written which consume
1595 * the reservation
1596 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001597STATIC void
1598xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1599{
1600 uint i;
1601 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1602
1603 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1604 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1605 "bformat",
1606 "bchunk",
1607 "efi_format",
1608 "efd_format",
1609 "iformat",
1610 "icore",
1611 "iext",
1612 "ibroot",
1613 "ilocal",
1614 "iattr_ext",
1615 "iattr_broot",
1616 "iattr_local",
1617 "qformat",
1618 "dquot",
1619 "quotaoff",
1620 "LR header",
1621 "unmount",
1622 "commit",
1623 "trans header"
1624 };
1625 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1626 "SETATTR_NOT_SIZE",
1627 "SETATTR_SIZE",
1628 "INACTIVE",
1629 "CREATE",
1630 "CREATE_TRUNC",
1631 "TRUNCATE_FILE",
1632 "REMOVE",
1633 "LINK",
1634 "RENAME",
1635 "MKDIR",
1636 "RMDIR",
1637 "SYMLINK",
1638 "SET_DMATTRS",
1639 "GROWFS",
1640 "STRAT_WRITE",
1641 "DIOSTRAT",
1642 "WRITE_SYNC",
1643 "WRITEID",
1644 "ADDAFORK",
1645 "ATTRINVAL",
1646 "ATRUNCATE",
1647 "ATTR_SET",
1648 "ATTR_RM",
1649 "ATTR_FLAG",
1650 "CLEAR_AGI_BUCKET",
1651 "QM_SBCHANGE",
1652 "DUMMY1",
1653 "DUMMY2",
1654 "QM_QUOTAOFF",
1655 "QM_DQALLOC",
1656 "QM_SETQLIM",
1657 "QM_DQCLUSTER",
1658 "QM_QINOCREATE",
1659 "QM_QUOTAOFF_END",
1660 "SB_UNIT",
1661 "FSYNC_TS",
1662 "GROWFSRT_ALLOC",
1663 "GROWFSRT_ZERO",
1664 "GROWFSRT_FREE",
1665 "SWAPEXT"
1666 };
1667
1668 xfs_fs_cmn_err(CE_WARN, mp,
1669 "xfs_log_write: reservation summary:\n"
1670 " trans type = %s (%u)\n"
1671 " unit res = %d bytes\n"
1672 " current res = %d bytes\n"
1673 " total reg = %u bytes (o/flow = %u bytes)\n"
1674 " ophdrs = %u (ophdr space = %u bytes)\n"
1675 " ophdr + reg = %u bytes\n"
1676 " num regions = %u\n",
1677 ((ticket->t_trans_type <= 0 ||
1678 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1679 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1680 ticket->t_trans_type,
1681 ticket->t_unit_res,
1682 ticket->t_curr_res,
1683 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1684 ticket->t_res_num_ophdrs, ophdr_spc,
1685 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001686 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001687 ticket->t_res_num);
1688
1689 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001690 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001691 cmn_err(CE_WARN,
1692 "region[%u]: %s - %u bytes\n",
1693 i,
1694 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1695 "bad-rtype" : res_type_str[r_type-1]),
1696 ticket->t_res_arr[i].r_len);
1697 }
1698}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001699
1700/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * Write some region out to in-core log
1702 *
1703 * This will be called when writing externally provided regions or when
1704 * writing out a commit record for a given transaction.
1705 *
1706 * General algorithm:
1707 * 1. Find total length of this write. This may include adding to the
1708 * lengths passed in.
1709 * 2. Check whether we violate the tickets reservation.
1710 * 3. While writing to this iclog
1711 * A. Reserve as much space in this iclog as can get
1712 * B. If this is first write, save away start lsn
1713 * C. While writing this region:
1714 * 1. If first write of transaction, write start record
1715 * 2. Write log operation header (header per region)
1716 * 3. Find out if we can fit entire region into this iclog
1717 * 4. Potentially, verify destination memcpy ptr
1718 * 5. Memcpy (partial) region
1719 * 6. If partial copy, release iclog; otherwise, continue
1720 * copying more regions into current iclog
1721 * 4. Mark want sync bit (in simulation mode)
1722 * 5. Release iclog for potential flush to on-disk log.
1723 *
1724 * ERRORS:
1725 * 1. Panic if reservation is overrun. This should never happen since
1726 * reservation amounts are generated internal to the filesystem.
1727 * NOTES:
1728 * 1. Tickets are single threaded data structures.
1729 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1730 * syncing routine. When a single log_write region needs to span
1731 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1732 * on all log operation writes which don't contain the end of the
1733 * region. The XLOG_END_TRANS bit is used for the in-core log
1734 * operation which contains the end of the continued log_write region.
1735 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1736 * we don't really know exactly how much space will be used. As a result,
1737 * we don't update ic_offset until the end when we know exactly how many
1738 * bytes have been written out.
1739 */
David Chinnera8272ce2007-11-23 16:28:09 +11001740STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741xlog_write(xfs_mount_t * mp,
1742 xfs_log_iovec_t reg[],
1743 int nentries,
1744 xfs_log_ticket_t tic,
1745 xfs_lsn_t *start_lsn,
1746 xlog_in_core_t **commit_iclog,
1747 uint flags)
1748{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001749 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001751 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 __psint_t ptr; /* copy address into data region */
1754 int len; /* # xlog_write() bytes 2 still copy */
1755 int index; /* region index currently copying */
1756 int log_offset; /* offset (from 0) into data region */
1757 int start_rec_copy; /* # bytes to copy for start record */
1758 int partial_copy; /* did we split a region? */
1759 int partial_copy_len;/* # bytes copied if split region */
1760 int need_copy; /* # bytes need to memcpy this region */
1761 int copy_len; /* # bytes actually memcpy'ing */
1762 int copy_off; /* # bytes from entry start */
1763 int contwr; /* continued write of in-core log? */
1764 int error;
1765 int record_cnt = 0, data_cnt = 0;
1766
1767 partial_copy_len = partial_copy = 0;
1768
1769 /* Calculate potential maximum space. Each region gets its own
1770 * xlog_op_header_t and may need to be double word aligned.
1771 */
1772 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001773 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001775 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 for (index = 0; index < nentries; index++) {
1779 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001780 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001782 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 contwr = *start_lsn = 0;
1785
1786 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001787 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788#ifdef DEBUG
1789 xlog_panic(
1790 "xfs_log_write: reservation ran out. Need to up reservation");
1791#else
1792 /* Customer configurable panic */
1793 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1794 "xfs_log_write: reservation ran out. Need to up reservation");
1795 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001796 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797#endif
1798 } else
1799 ticket->t_curr_res -= len;
1800
1801 for (index = 0; index < nentries; ) {
1802 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1803 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 ASSERT(log_offset <= iclog->ic_size - 1);
1807 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1808
1809 /* start_lsn is the first lsn written to. That's all we need. */
1810 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001811 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /* This loop writes out as many regions as can fit in the amount
1814 * of space which was allocated by xlog_state_get_iclog_space().
1815 */
1816 while (index < nentries) {
1817 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1818 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1819 start_rec_copy = 0;
1820
1821 /* If first write for transaction, insert start record.
1822 * We can't be trying to commit if we are inited. We can't
1823 * have any "partial_copy" if we are inited.
1824 */
1825 if (ticket->t_flags & XLOG_TIC_INITED) {
1826 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001827 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 logop_head->oh_clientid = ticket->t_clientid;
1829 logop_head->oh_len = 0;
1830 logop_head->oh_flags = XLOG_START_TRANS;
1831 logop_head->oh_res2 = 0;
1832 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1833 record_cnt++;
1834
1835 start_rec_copy = sizeof(xlog_op_header_t);
1836 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1837 }
1838
1839 /* Copy log operation header directly into data section */
1840 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001841 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 logop_head->oh_clientid = ticket->t_clientid;
1843 logop_head->oh_res2 = 0;
1844
1845 /* header copied directly */
1846 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1847
1848 /* are we copying a commit or unmount record? */
1849 logop_head->oh_flags = flags;
1850
1851 /*
1852 * We've seen logs corrupted with bad transaction client
1853 * ids. This makes sure that XFS doesn't generate them on.
1854 * Turn this into an EIO and shut down the filesystem.
1855 */
1856 switch (logop_head->oh_clientid) {
1857 case XFS_TRANSACTION:
1858 case XFS_VOLUME:
1859 case XFS_LOG:
1860 break;
1861 default:
1862 xfs_fs_cmn_err(CE_WARN, mp,
1863 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1864 logop_head->oh_clientid, tic);
1865 return XFS_ERROR(EIO);
1866 }
1867
1868 /* Partial write last time? => (partial_copy != 0)
1869 * need_copy is the amount we'd like to copy if everything could
1870 * fit in the current memcpy.
1871 */
1872 need_copy = reg[index].i_len - partial_copy_len;
1873
1874 copy_off = partial_copy_len;
1875 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001876 copy_len = need_copy;
1877 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (partial_copy)
1879 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1880 partial_copy_len = partial_copy = 0;
1881 } else { /* partial write */
1882 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001883 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1885 if (partial_copy)
1886 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1887 partial_copy_len += copy_len;
1888 partial_copy++;
1889 len += sizeof(xlog_op_header_t); /* from splitting of region */
1890 /* account for new log op header */
1891 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001892 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 xlog_verify_dest_ptr(log, ptr);
1895
1896 /* copy region */
1897 ASSERT(copy_len >= 0);
1898 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1899 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1900
1901 /* make copy_len total bytes copied, including headers */
1902 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1903 record_cnt++;
1904 data_cnt += contwr ? copy_len : 0;
1905 if (partial_copy) { /* copied partial region */
1906 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1907 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1908 record_cnt = data_cnt = 0;
1909 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001910 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 break; /* don't increment index */
1912 } else { /* copied entire region */
1913 index++;
1914 partial_copy_len = partial_copy = 0;
1915
1916 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1917 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1918 record_cnt = data_cnt = 0;
1919 xlog_state_want_sync(log, iclog);
1920 if (commit_iclog) {
1921 ASSERT(flags & XLOG_COMMIT_TRANS);
1922 *commit_iclog = iclog;
1923 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001924 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (index == nentries)
1926 return 0; /* we are done */
1927 else
1928 break;
1929 }
1930 } /* if (partial_copy) */
1931 } /* while (index < nentries) */
1932 } /* for (index = 0; index < nentries; ) */
1933 ASSERT(len == 0);
1934
1935 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1936 if (commit_iclog) {
1937 ASSERT(flags & XLOG_COMMIT_TRANS);
1938 *commit_iclog = iclog;
1939 return 0;
1940 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001941 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942} /* xlog_write */
1943
1944
1945/*****************************************************************************
1946 *
1947 * State Machine functions
1948 *
1949 *****************************************************************************
1950 */
1951
1952/* Clean iclogs starting from the head. This ordering must be
1953 * maintained, so an iclog doesn't become ACTIVE beyond one that
1954 * is SYNCING. This is also required to maintain the notion that we use
1955 * a counting semaphore to hold off would be writers to the log when every
1956 * iclog is trying to sync to disk.
1957 *
1958 * State Change: DIRTY -> ACTIVE
1959 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001960STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961xlog_state_clean_log(xlog_t *log)
1962{
1963 xlog_in_core_t *iclog;
1964 int changed = 0;
1965
1966 iclog = log->l_iclog;
1967 do {
1968 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1969 iclog->ic_state = XLOG_STATE_ACTIVE;
1970 iclog->ic_offset = 0;
1971 iclog->ic_callback = NULL; /* don't need to free */
1972 /*
1973 * If the number of ops in this iclog indicate it just
1974 * contains the dummy transaction, we can
1975 * change state into IDLE (the second time around).
1976 * Otherwise we should change the state into
1977 * NEED a dummy.
1978 * We don't need to cover the dummy.
1979 */
1980 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001981 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1982 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 changed = 1;
1984 } else {
1985 /*
1986 * We have two dirty iclogs so start over
1987 * This could also be num of ops indicates
1988 * this is not the dummy going out.
1989 */
1990 changed = 2;
1991 }
1992 iclog->ic_header.h_num_logops = 0;
1993 memset(iclog->ic_header.h_cycle_data, 0,
1994 sizeof(iclog->ic_header.h_cycle_data));
1995 iclog->ic_header.h_lsn = 0;
1996 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1997 /* do nothing */;
1998 else
1999 break; /* stop cleaning */
2000 iclog = iclog->ic_next;
2001 } while (iclog != log->l_iclog);
2002
2003 /* log is locked when we are called */
2004 /*
2005 * Change state for the dummy log recording.
2006 * We usually go to NEED. But we go to NEED2 if the changed indicates
2007 * we are done writing the dummy record.
2008 * If we are done with the second dummy recored (DONE2), then
2009 * we go to IDLE.
2010 */
2011 if (changed) {
2012 switch (log->l_covered_state) {
2013 case XLOG_STATE_COVER_IDLE:
2014 case XLOG_STATE_COVER_NEED:
2015 case XLOG_STATE_COVER_NEED2:
2016 log->l_covered_state = XLOG_STATE_COVER_NEED;
2017 break;
2018
2019 case XLOG_STATE_COVER_DONE:
2020 if (changed == 1)
2021 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2022 else
2023 log->l_covered_state = XLOG_STATE_COVER_NEED;
2024 break;
2025
2026 case XLOG_STATE_COVER_DONE2:
2027 if (changed == 1)
2028 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2029 else
2030 log->l_covered_state = XLOG_STATE_COVER_NEED;
2031 break;
2032
2033 default:
2034 ASSERT(0);
2035 }
2036 }
2037} /* xlog_state_clean_log */
2038
2039STATIC xfs_lsn_t
2040xlog_get_lowest_lsn(
2041 xlog_t *log)
2042{
2043 xlog_in_core_t *lsn_log;
2044 xfs_lsn_t lowest_lsn, lsn;
2045
2046 lsn_log = log->l_iclog;
2047 lowest_lsn = 0;
2048 do {
2049 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002050 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if ((lsn && !lowest_lsn) ||
2052 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2053 lowest_lsn = lsn;
2054 }
2055 }
2056 lsn_log = lsn_log->ic_next;
2057 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002058 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061
2062STATIC void
2063xlog_state_do_callback(
2064 xlog_t *log,
2065 int aborted,
2066 xlog_in_core_t *ciclog)
2067{
2068 xlog_in_core_t *iclog;
2069 xlog_in_core_t *first_iclog; /* used to know when we've
2070 * processed all iclogs once */
2071 xfs_log_callback_t *cb, *cb_next;
2072 int flushcnt = 0;
2073 xfs_lsn_t lowest_lsn;
2074 int ioerrors; /* counter: iclogs with errors */
2075 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2076 int funcdidcallbacks; /* flag: function did callbacks */
2077 int repeats; /* for issuing console warnings if
2078 * looping too many times */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002080 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 first_iclog = iclog = log->l_iclog;
2082 ioerrors = 0;
2083 funcdidcallbacks = 0;
2084 repeats = 0;
2085
2086 do {
2087 /*
2088 * Scan all iclogs starting with the one pointed to by the
2089 * log. Reset this starting point each time the log is
2090 * unlocked (during callbacks).
2091 *
2092 * Keep looping through iclogs until one full pass is made
2093 * without running any callbacks.
2094 */
2095 first_iclog = log->l_iclog;
2096 iclog = log->l_iclog;
2097 loopdidcallbacks = 0;
2098 repeats++;
2099
2100 do {
2101
2102 /* skip all iclogs in the ACTIVE & DIRTY states */
2103 if (iclog->ic_state &
2104 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2105 iclog = iclog->ic_next;
2106 continue;
2107 }
2108
2109 /*
2110 * Between marking a filesystem SHUTDOWN and stopping
2111 * the log, we do flush all iclogs to disk (if there
2112 * wasn't a log I/O error). So, we do want things to
2113 * go smoothly in case of just a SHUTDOWN w/o a
2114 * LOG_IO_ERROR.
2115 */
2116 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2117 /*
2118 * Can only perform callbacks in order. Since
2119 * this iclog is not in the DONE_SYNC/
2120 * DO_CALLBACK state, we skip the rest and
2121 * just try to clean up. If we set our iclog
2122 * to DO_CALLBACK, we will not process it when
2123 * we retry since a previous iclog is in the
2124 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002125 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 */
2127 if (!(iclog->ic_state &
2128 (XLOG_STATE_DONE_SYNC |
2129 XLOG_STATE_DO_CALLBACK))) {
2130 if (ciclog && (ciclog->ic_state ==
2131 XLOG_STATE_DONE_SYNC)) {
2132 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2133 }
2134 break;
2135 }
2136 /*
2137 * We now have an iclog that is in either the
2138 * DO_CALLBACK or DONE_SYNC states. The other
2139 * states (WANT_SYNC, SYNCING, or CALLBACK were
2140 * caught by the above if and are going to
2141 * clean (i.e. we aren't doing their callbacks)
2142 * see the above if.
2143 */
2144
2145 /*
2146 * We will do one more check here to see if we
2147 * have chased our tail around.
2148 */
2149
2150 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002151 if (lowest_lsn &&
2152 XFS_LSN_CMP(lowest_lsn,
2153 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 iclog = iclog->ic_next;
2155 continue; /* Leave this iclog for
2156 * another thread */
2157 }
2158
2159 iclog->ic_state = XLOG_STATE_CALLBACK;
2160
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002161 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002164 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * No one else can be here except us.
2166 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002167 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002168 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2169 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2170 log->l_last_sync_lsn =
2171 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002172 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 /*
2175 * Keep processing entries in the callback list
2176 * until we come around and it is empty. We
2177 * need to atomically see that the list is
2178 * empty and change the state to DIRTY so that
2179 * we don't miss any more callbacks being added.
2180 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002181 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 } else {
2183 ioerrors++;
2184 }
2185 cb = iclog->ic_callback;
2186
Christoph Hellwig4b809162007-08-16 15:37:36 +10002187 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 iclog->ic_callback_tail = &(iclog->ic_callback);
2189 iclog->ic_callback = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002190 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002193 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 cb_next = cb->cb_next;
2195 cb->cb_func(cb->cb_arg, aborted);
2196 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002197 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 cb = iclog->ic_callback;
2199 }
2200
2201 loopdidcallbacks++;
2202 funcdidcallbacks++;
2203
Christoph Hellwig4b809162007-08-16 15:37:36 +10002204 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2206 iclog->ic_state = XLOG_STATE_DIRTY;
2207
2208 /*
2209 * Transition from DIRTY to ACTIVE if applicable.
2210 * NOP if STATE_IOERROR.
2211 */
2212 xlog_state_clean_log(log);
2213
2214 /* wake up threads waiting in xfs_log_force() */
2215 sv_broadcast(&iclog->ic_forcesema);
2216
2217 iclog = iclog->ic_next;
2218 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002219
2220 if (repeats > 5000) {
2221 flushcnt += repeats;
2222 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002224 "%s: possible infinite loop (%d iterations)",
2225 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227 } while (!ioerrors && loopdidcallbacks);
2228
2229 /*
2230 * make one last gasp attempt to see if iclogs are being left in
2231 * limbo..
2232 */
2233#ifdef DEBUG
2234 if (funcdidcallbacks) {
2235 first_iclog = iclog = log->l_iclog;
2236 do {
2237 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2238 /*
2239 * Terminate the loop if iclogs are found in states
2240 * which will cause other threads to clean up iclogs.
2241 *
2242 * SYNCING - i/o completion will go through logs
2243 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002244 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 * IOERROR - give up hope all ye who enter here
2246 */
2247 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2248 iclog->ic_state == XLOG_STATE_SYNCING ||
2249 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2250 iclog->ic_state == XLOG_STATE_IOERROR )
2251 break;
2252 iclog = iclog->ic_next;
2253 } while (first_iclog != iclog);
2254 }
2255#endif
2256
Nathan Scotta3c66852006-09-28 11:02:14 +10002257 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2259 flushcnt = log->l_flushcnt;
2260 log->l_flushcnt = 0;
2261 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002262 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 while (flushcnt--)
2264 vsema(&log->l_flushsema);
2265} /* xlog_state_do_callback */
2266
2267
2268/*
2269 * Finish transitioning this iclog to the dirty state.
2270 *
2271 * Make sure that we completely execute this routine only when this is
2272 * the last call to the iclog. There is a good chance that iclog flushes,
2273 * when we reach the end of the physical log, get turned into 2 separate
2274 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2275 * routine. By using the reference count bwritecnt, we guarantee that only
2276 * the second completion goes through.
2277 *
2278 * Callbacks could take time, so they are done outside the scope of the
2279 * global state machine log lock. Assume that the calls to cvsema won't
2280 * take a long time. At least we know it won't sleep.
2281 */
David Chinnera8272ce2007-11-23 16:28:09 +11002282STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283xlog_state_done_syncing(
2284 xlog_in_core_t *iclog,
2285 int aborted)
2286{
2287 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002289 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2292 iclog->ic_state == XLOG_STATE_IOERROR);
2293 ASSERT(iclog->ic_refcnt == 0);
2294 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2295
2296
2297 /*
2298 * If we got an error, either on the first buffer, or in the case of
2299 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2300 * and none should ever be attempted to be written to disk
2301 * again.
2302 */
2303 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2304 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002305 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return;
2307 }
2308 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2309 }
2310
2311 /*
2312 * Someone could be sleeping prior to writing out the next
2313 * iclog buffer, we wake them all, one will get to do the
2314 * I/O, the others get to wait for the result.
2315 */
2316 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002317 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2319} /* xlog_state_done_syncing */
2320
2321
2322/*
2323 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2324 * sleep. The flush semaphore is set to the number of in-core buffers and
2325 * decremented around disk syncing. Therefore, if all buffers are syncing,
2326 * this semaphore will cause new writes to sleep until a sync completes.
2327 * Otherwise, this code just does p() followed by v(). This approximates
2328 * a sleep/wakeup except we can't race.
2329 *
2330 * The in-core logs are used in a circular fashion. They are not used
2331 * out-of-order even when an iclog past the head is free.
2332 *
2333 * return:
2334 * * log_offset where xlog_write() can start writing into the in-core
2335 * log's data space.
2336 * * in-core log pointer to which xlog_write() should write.
2337 * * boolean indicating this is a continued write to an in-core log.
2338 * If this is the last write, then the in-core log's offset field
2339 * needs to be incremented, depending on the amount of data which
2340 * is copied.
2341 */
David Chinnera8272ce2007-11-23 16:28:09 +11002342STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343xlog_state_get_iclog_space(xlog_t *log,
2344 int len,
2345 xlog_in_core_t **iclogp,
2346 xlog_ticket_t *ticket,
2347 int *continued_write,
2348 int *logoffsetp)
2349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int log_offset;
2351 xlog_rec_header_t *head;
2352 xlog_in_core_t *iclog;
2353 int error;
2354
2355restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002356 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002358 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return XFS_ERROR(EIO);
2360 }
2361
2362 iclog = log->l_iclog;
2363 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2364 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002365 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2367 XFS_STATS_INC(xs_log_noiclogs);
2368 /* Ensure that log writes happen */
2369 psema(&log->l_flushsema, PINOD);
2370 goto restart;
2371 }
2372 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2373 head = &iclog->ic_header;
2374
2375 iclog->ic_refcnt++; /* prevents sync */
2376 log_offset = iclog->ic_offset;
2377
2378 /* On the 1st write to an iclog, figure out lsn. This works
2379 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2380 * committing to. If the offset is set, that's how many blocks
2381 * must be written.
2382 */
2383 if (log_offset == 0) {
2384 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002385 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002386 log->l_iclog_hsize,
2387 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002388 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2389 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002390 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 ASSERT(log->l_curr_block >= 0);
2392 }
2393
2394 /* If there is enough room to write everything, then do it. Otherwise,
2395 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2396 * bit is on, so this will get flushed out. Don't update ic_offset
2397 * until you know exactly how many bytes get copied. Therefore, wait
2398 * until later to update ic_offset.
2399 *
2400 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2401 * can fit into remaining data section.
2402 */
2403 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2404 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2405
2406 /* If I'm the only one writing to this iclog, sync it to disk */
2407 if (iclog->ic_refcnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002408 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002410 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 } else {
2412 iclog->ic_refcnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002413 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415 goto restart;
2416 }
2417
2418 /* Do we have enough room to write the full amount in the remainder
2419 * of this iclog? Or must we continue a write on the next iclog and
2420 * mark this iclog as completely taken? In the case where we switch
2421 * iclogs (to mark it taken), this particular iclog will release/sync
2422 * to disk in xlog_write().
2423 */
2424 if (len <= iclog->ic_size - iclog->ic_offset) {
2425 *continued_write = 0;
2426 iclog->ic_offset += len;
2427 } else {
2428 *continued_write = 1;
2429 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2430 }
2431 *iclogp = iclog;
2432
2433 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002434 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 *logoffsetp = log_offset;
2437 return 0;
2438} /* xlog_state_get_iclog_space */
2439
2440/*
2441 * Atomically get the log space required for a log ticket.
2442 *
2443 * Once a ticket gets put onto the reserveq, it will only return after
2444 * the needed reservation is satisfied.
2445 */
2446STATIC int
2447xlog_grant_log_space(xlog_t *log,
2448 xlog_ticket_t *tic)
2449{
2450 int free_bytes;
2451 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452#ifdef DEBUG
2453 xfs_lsn_t tail_lsn;
2454#endif
2455
2456
2457#ifdef DEBUG
2458 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2459 panic("grant Recovery problem");
2460#endif
2461
2462 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002463 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2465
2466 /* something is already sleeping; insert new transaction at end */
2467 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002468 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 xlog_trace_loggrant(log, tic,
2470 "xlog_grant_log_space: sleep 1");
2471 /*
2472 * Gotta check this before going to sleep, while we're
2473 * holding the grant lock.
2474 */
2475 if (XLOG_FORCED_SHUTDOWN(log))
2476 goto error_return;
2477
2478 XFS_STATS_INC(xs_sleep_logspace);
2479 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2480 /*
2481 * If we got an error, and the filesystem is shutting down,
2482 * we'll catch it down below. So just continue...
2483 */
2484 xlog_trace_loggrant(log, tic,
2485 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002486 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2489 need_bytes = tic->t_unit_res*tic->t_ocnt;
2490 else
2491 need_bytes = tic->t_unit_res;
2492
2493redo:
2494 if (XLOG_FORCED_SHUTDOWN(log))
2495 goto error_return;
2496
2497 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2498 log->l_grant_reserve_bytes);
2499 if (free_bytes < need_bytes) {
2500 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002501 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 xlog_trace_loggrant(log, tic,
2503 "xlog_grant_log_space: sleep 2");
2504 XFS_STATS_INC(xs_sleep_logspace);
2505 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2506
2507 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002508 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 goto error_return;
2510 }
2511
2512 xlog_trace_loggrant(log, tic,
2513 "xlog_grant_log_space: wake 2");
2514 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002515 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 goto redo;
2517 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002518 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002521 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522#ifdef DEBUG
2523 tail_lsn = log->l_tail_lsn;
2524 /*
2525 * Check to make sure the grant write head didn't just over lap the
2526 * tail. If the cycles are the same, we can't be overlapping.
2527 * Otherwise, make sure that the cycles differ by exactly one and
2528 * check the byte count.
2529 */
2530 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2531 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2532 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2533 }
2534#endif
2535 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2536 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002537 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return 0;
2539
2540 error_return:
2541 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002542 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2544 /*
2545 * If we are failing, make sure the ticket doesn't have any
2546 * current reservations. We don't want to add this back when
2547 * the ticket/transaction gets cancelled.
2548 */
2549 tic->t_curr_res = 0;
2550 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002551 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return XFS_ERROR(EIO);
2553} /* xlog_grant_log_space */
2554
2555
2556/*
2557 * Replenish the byte reservation required by moving the grant write head.
2558 *
2559 *
2560 */
2561STATIC int
2562xlog_regrant_write_log_space(xlog_t *log,
2563 xlog_ticket_t *tic)
2564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 int free_bytes, need_bytes;
2566 xlog_ticket_t *ntic;
2567#ifdef DEBUG
2568 xfs_lsn_t tail_lsn;
2569#endif
2570
2571 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002572 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577#ifdef DEBUG
2578 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2579 panic("regrant Recovery problem");
2580#endif
2581
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002582 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2584
2585 if (XLOG_FORCED_SHUTDOWN(log))
2586 goto error_return;
2587
2588 /* If there are other waiters on the queue then give them a
2589 * chance at logspace before us. Wake up the first waiters,
2590 * if we do not wake up all the waiters then go to sleep waiting
2591 * for more free space, otherwise try to get some space for
2592 * this transaction.
2593 */
2594
2595 if ((ntic = log->l_write_headq)) {
2596 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2597 log->l_grant_write_bytes);
2598 do {
2599 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2600
2601 if (free_bytes < ntic->t_unit_res)
2602 break;
2603 free_bytes -= ntic->t_unit_res;
2604 sv_signal(&ntic->t_sema);
2605 ntic = ntic->t_next;
2606 } while (ntic != log->l_write_headq);
2607
2608 if (ntic != log->l_write_headq) {
2609 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002610 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 xlog_trace_loggrant(log, tic,
2613 "xlog_regrant_write_log_space: sleep 1");
2614 XFS_STATS_INC(xs_sleep_logspace);
2615 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2616 &log->l_grant_lock, s);
2617
2618 /* If we're shutting down, this tic is already
2619 * off the queue */
2620 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002621 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 goto error_return;
2623 }
2624
2625 xlog_trace_loggrant(log, tic,
2626 "xlog_regrant_write_log_space: wake 1");
2627 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002628 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
2630 }
2631
2632 need_bytes = tic->t_unit_res;
2633
2634redo:
2635 if (XLOG_FORCED_SHUTDOWN(log))
2636 goto error_return;
2637
2638 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2639 log->l_grant_write_bytes);
2640 if (free_bytes < need_bytes) {
2641 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002642 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 XFS_STATS_INC(xs_sleep_logspace);
2644 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2645
2646 /* If we're shutting down, this tic is already off the queue */
2647 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002648 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 goto error_return;
2650 }
2651
2652 xlog_trace_loggrant(log, tic,
2653 "xlog_regrant_write_log_space: wake 2");
2654 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002655 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 goto redo;
2657 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002658 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002660 /* we've got enough space */
2661 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662#ifdef DEBUG
2663 tail_lsn = log->l_tail_lsn;
2664 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2665 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2666 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2667 }
2668#endif
2669
2670 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2671 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002672 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675
2676 error_return:
2677 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002678 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2680 /*
2681 * If we are failing, make sure the ticket doesn't have any
2682 * current reservations. We don't want to add this back when
2683 * the ticket/transaction gets cancelled.
2684 */
2685 tic->t_curr_res = 0;
2686 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002687 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return XFS_ERROR(EIO);
2689} /* xlog_regrant_write_log_space */
2690
2691
2692/* The first cnt-1 times through here we don't need to
2693 * move the grant write head because the permanent
2694 * reservation has reserved cnt times the unit amount.
2695 * Release part of current permanent unit reservation and
2696 * reset current reservation to be one units worth. Also
2697 * move grant reservation head forward.
2698 */
2699STATIC void
2700xlog_regrant_reserve_log_space(xlog_t *log,
2701 xlog_ticket_t *ticket)
2702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 xlog_trace_loggrant(log, ticket,
2704 "xlog_regrant_reserve_log_space: enter");
2705 if (ticket->t_cnt > 0)
2706 ticket->t_cnt--;
2707
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002708 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002709 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002711 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 xlog_trace_loggrant(log, ticket,
2713 "xlog_regrant_reserve_log_space: sub current res");
2714 xlog_verify_grant_head(log, 1);
2715
2716 /* just return if we still have some of the pre-reserved space */
2717 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002718 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return;
2720 }
2721
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002722 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 xlog_trace_loggrant(log, ticket,
2724 "xlog_regrant_reserve_log_space: exit");
2725 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002726 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002728 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729} /* xlog_regrant_reserve_log_space */
2730
2731
2732/*
2733 * Give back the space left from a reservation.
2734 *
2735 * All the information we need to make a correct determination of space left
2736 * is present. For non-permanent reservations, things are quite easy. The
2737 * count should have been decremented to zero. We only need to deal with the
2738 * space remaining in the current reservation part of the ticket. If the
2739 * ticket contains a permanent reservation, there may be left over space which
2740 * needs to be released. A count of N means that N-1 refills of the current
2741 * reservation can be done before we need to ask for more space. The first
2742 * one goes to fill up the first current reservation. Once we run out of
2743 * space, the count will stay at zero and the only space remaining will be
2744 * in the current reservation field.
2745 */
2746STATIC void
2747xlog_ungrant_log_space(xlog_t *log,
2748 xlog_ticket_t *ticket)
2749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 if (ticket->t_cnt > 0)
2751 ticket->t_cnt--;
2752
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002753 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2755
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002756 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2759
2760 /* If this is a permanent reservation ticket, we may be able to free
2761 * up more space based on the remaining count.
2762 */
2763 if (ticket->t_cnt > 0) {
2764 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002765 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
2767
2768 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2769 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002770 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 xfs_log_move_tail(log->l_mp, 1);
2772} /* xlog_ungrant_log_space */
2773
2774
2775/*
2776 * Atomically put back used ticket.
2777 */
David Chinnera8272ce2007-11-23 16:28:09 +11002778STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779xlog_state_put_ticket(xlog_t *log,
2780 xlog_ticket_t *tic)
2781{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002782 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 xlog_ticket_put(log, tic);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002784 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785} /* xlog_state_put_ticket */
2786
2787/*
2788 * Flush iclog to disk if this is the last reference to the given iclog and
2789 * the WANT_SYNC bit is set.
2790 *
2791 * When this function is entered, the iclog is not necessarily in the
2792 * WANT_SYNC state. It may be sitting around waiting to get filled.
2793 *
2794 *
2795 */
David Chinnera8272ce2007-11-23 16:28:09 +11002796STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797xlog_state_release_iclog(xlog_t *log,
2798 xlog_in_core_t *iclog)
2799{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 int sync = 0; /* do we sync? */
2801
2802 xlog_assign_tail_lsn(log->l_mp);
2803
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002804 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002807 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return XFS_ERROR(EIO);
2809 }
2810
2811 ASSERT(iclog->ic_refcnt > 0);
2812 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2813 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2814
2815 if (--iclog->ic_refcnt == 0 &&
2816 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2817 sync++;
2818 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002819 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2821 /* cycle incremented when incrementing curr_block */
2822 }
2823
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002824 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 /*
2827 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002828 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2830 * this iclog has consistent data, so we ignore IOERROR
2831 * flags after this point.
2832 */
2833 if (sync) {
2834 return xlog_sync(log, iclog);
2835 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838} /* xlog_state_release_iclog */
2839
2840
2841/*
2842 * This routine will mark the current iclog in the ring as WANT_SYNC
2843 * and move the current iclog pointer to the next iclog in the ring.
2844 * When this routine is called from xlog_state_get_iclog_space(), the
2845 * exact size of the iclog has not yet been determined. All we know is
2846 * that every data block. We have run out of space in this log record.
2847 */
2848STATIC void
2849xlog_state_switch_iclogs(xlog_t *log,
2850 xlog_in_core_t *iclog,
2851 int eventual_size)
2852{
2853 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2854 if (!eventual_size)
2855 eventual_size = iclog->ic_offset;
2856 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002857 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 log->l_prev_block = log->l_curr_block;
2859 log->l_prev_cycle = log->l_curr_cycle;
2860
2861 /* roll log?: ic_offset changed later */
2862 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2863
2864 /* Round up to next log-sunit */
2865 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2866 log->l_mp->m_sb.sb_logsunit > 1) {
2867 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2868 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2869 }
2870
2871 if (log->l_curr_block >= log->l_logBBsize) {
2872 log->l_curr_cycle++;
2873 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2874 log->l_curr_cycle++;
2875 log->l_curr_block -= log->l_logBBsize;
2876 ASSERT(log->l_curr_block >= 0);
2877 }
2878 ASSERT(iclog == log->l_iclog);
2879 log->l_iclog = iclog->ic_next;
2880} /* xlog_state_switch_iclogs */
2881
2882
2883/*
2884 * Write out all data in the in-core log as of this exact moment in time.
2885 *
2886 * Data may be written to the in-core log during this call. However,
2887 * we don't guarantee this data will be written out. A change from past
2888 * implementation means this routine will *not* write out zero length LRs.
2889 *
2890 * Basically, we try and perform an intelligent scan of the in-core logs.
2891 * If we determine there is no flushable data, we just return. There is no
2892 * flushable data if:
2893 *
2894 * 1. the current iclog is active and has no data; the previous iclog
2895 * is in the active or dirty state.
2896 * 2. the current iclog is drity, and the previous iclog is in the
2897 * active or dirty state.
2898 *
2899 * We may sleep (call psema) if:
2900 *
2901 * 1. the current iclog is not in the active nor dirty state.
2902 * 2. the current iclog dirty, and the previous iclog is not in the
2903 * active nor dirty state.
2904 * 3. the current iclog is active, and there is another thread writing
2905 * to this particular iclog.
2906 * 4. a) the current iclog is active and has no other writers
2907 * b) when we return from flushing out this iclog, it is still
2908 * not in the active nor dirty state.
2909 */
2910STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002911xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 xlog_in_core_t *iclog;
2914 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002916 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 iclog = log->l_iclog;
2919 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002920 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return XFS_ERROR(EIO);
2922 }
2923
2924 /* If the head iclog is not active nor dirty, we just attach
2925 * ourselves to the head and go to sleep.
2926 */
2927 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2928 iclog->ic_state == XLOG_STATE_DIRTY) {
2929 /*
2930 * If the head is dirty or (active and empty), then
2931 * we need to look at the previous iclog. If the previous
2932 * iclog is active or dirty we are done. There is nothing
2933 * to sync out. Otherwise, we attach ourselves to the
2934 * previous iclog and go to sleep.
2935 */
2936 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2937 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2938 iclog = iclog->ic_prev;
2939 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2940 iclog->ic_state == XLOG_STATE_DIRTY)
2941 goto no_sleep;
2942 else
2943 goto maybe_sleep;
2944 } else {
2945 if (iclog->ic_refcnt == 0) {
2946 /* We are the only one with access to this
2947 * iclog. Flush it out now. There should
2948 * be a roundoff of zero to show that someone
2949 * has already taken care of the roundoff from
2950 * the previous sync.
2951 */
2952 iclog->ic_refcnt++;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002953 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002955 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 if (xlog_state_release_iclog(log, iclog))
2958 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002959 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002960 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002961 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 iclog->ic_state != XLOG_STATE_DIRTY)
2963 goto maybe_sleep;
2964 else
2965 goto no_sleep;
2966 } else {
2967 /* Someone else is writing to this iclog.
2968 * Use its call to flush out the data. However,
2969 * the other thread may not force out this LR,
2970 * so we mark it WANT_SYNC.
2971 */
2972 xlog_state_switch_iclogs(log, iclog, 0);
2973 goto maybe_sleep;
2974 }
2975 }
2976 }
2977
2978 /* By the time we come around again, the iclog could've been filled
2979 * which would give it another lsn. If we have a new lsn, just
2980 * return because the relevant data has been flushed.
2981 */
2982maybe_sleep:
2983 if (flags & XFS_LOG_SYNC) {
2984 /*
2985 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002986 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 * Then we check again after waking up, in case our
2988 * sleep was disturbed by a bad news.
2989 */
2990 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002991 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return XFS_ERROR(EIO);
2993 }
2994 XFS_STATS_INC(xs_log_force_sleep);
2995 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
2996 /*
2997 * No need to grab the log lock here since we're
2998 * only deciding whether or not to return EIO
2999 * and the memory read should be atomic.
3000 */
3001 if (iclog->ic_state & XLOG_STATE_IOERROR)
3002 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003003 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005 } else {
3006
3007no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003008 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
3010 return 0;
3011} /* xlog_state_sync_all */
3012
3013
3014/*
3015 * Used by code which implements synchronous log forces.
3016 *
3017 * Find in-core log with lsn.
3018 * If it is in the DIRTY state, just return.
3019 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3020 * state and go to sleep or return.
3021 * If it is in any other state, go to sleep or return.
3022 *
3023 * If filesystem activity goes to zero, the iclog will get flushed only by
3024 * bdflush().
3025 */
David Chinnera8272ce2007-11-23 16:28:09 +11003026STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027xlog_state_sync(xlog_t *log,
3028 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003029 uint flags,
3030 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 xlog_in_core_t *iclog;
3033 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003036 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 iclog = log->l_iclog;
3038
3039 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003040 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return XFS_ERROR(EIO);
3042 }
3043
3044 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003045 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3046 iclog = iclog->ic_next;
3047 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049
3050 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003051 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return 0;
3053 }
3054
3055 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3056 /*
3057 * We sleep here if we haven't already slept (e.g.
3058 * this is the first time we've looked at the correct
3059 * iclog buf) and the buffer before us is going to
3060 * be sync'ed. The reason for this is that if we
3061 * are doing sync transactions here, by waiting for
3062 * the previous I/O to complete, we can allow a few
3063 * more transactions into this iclog before we close
3064 * it down.
3065 *
3066 * Otherwise, we mark the buffer WANT_SYNC, and bump
3067 * up the refcnt so we can release the log (which drops
3068 * the ref count). The state switch keeps new transaction
3069 * commits from using this buffer. When the current commits
3070 * finish writing into the buffer, the refcount will drop to
3071 * zero and the buffer will go out then.
3072 */
3073 if (!already_slept &&
3074 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3075 XLOG_STATE_SYNCING))) {
3076 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3077 XFS_STATS_INC(xs_log_force_sleep);
3078 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3079 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003080 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 already_slept = 1;
3082 goto try_again;
3083 } else {
3084 iclog->ic_refcnt++;
3085 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003086 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (xlog_state_release_iclog(log, iclog))
3088 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003089 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003090 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 }
3093
3094 if ((flags & XFS_LOG_SYNC) && /* sleep */
3095 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3096
3097 /*
3098 * Don't wait on the forcesema if we know that we've
3099 * gotten a log write error.
3100 */
3101 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003102 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return XFS_ERROR(EIO);
3104 }
3105 XFS_STATS_INC(xs_log_force_sleep);
3106 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3107 /*
3108 * No need to grab the log lock here since we're
3109 * only deciding whether or not to return EIO
3110 * and the memory read should be atomic.
3111 */
3112 if (iclog->ic_state & XLOG_STATE_IOERROR)
3113 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003114 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003116 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 }
3118 return 0;
3119
3120 } while (iclog != log->l_iclog);
3121
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003122 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124} /* xlog_state_sync */
3125
3126
3127/*
3128 * Called when we want to mark the current iclog as being ready to sync to
3129 * disk.
3130 */
David Chinnera8272ce2007-11-23 16:28:09 +11003131STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3133{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003134 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3137 xlog_state_switch_iclogs(log, iclog, 0);
3138 } else {
3139 ASSERT(iclog->ic_state &
3140 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3141 }
3142
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003143 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144} /* xlog_state_want_sync */
3145
3146
3147
3148/*****************************************************************************
3149 *
3150 * TICKET functions
3151 *
3152 *****************************************************************************
3153 */
3154
3155/*
3156 * Algorithm doesn't take into account page size. ;-(
3157 */
3158STATIC void
3159xlog_state_ticket_alloc(xlog_t *log)
3160{
3161 xlog_ticket_t *t_list;
3162 xlog_ticket_t *next;
3163 xfs_caddr_t buf;
Tim Shimmine6a4b372007-11-23 16:30:42 +11003164 uint i = (PAGE_SIZE / sizeof(xlog_ticket_t)) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 /*
3167 * The kmem_zalloc may sleep, so we shouldn't be holding the
3168 * global lock. XXXmiken: may want to use zone allocator.
3169 */
Tim Shimmine6a4b372007-11-23 16:30:42 +11003170 buf = (xfs_caddr_t) kmem_zalloc(PAGE_SIZE, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003172 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
3174 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3175 t_list = (xlog_ticket_t *)buf;
3176 t_list->t_next = log->l_unmount_free;
3177 log->l_unmount_free = t_list++;
3178 log->l_ticket_cnt++;
3179 log->l_ticket_tcnt++;
3180
3181 /* Next ticket becomes first ticket attached to ticket free list */
3182 if (log->l_freelist != NULL) {
3183 ASSERT(log->l_tail != NULL);
3184 log->l_tail->t_next = t_list;
3185 } else {
3186 log->l_freelist = t_list;
3187 }
3188 log->l_ticket_cnt++;
3189 log->l_ticket_tcnt++;
3190
3191 /* Cycle through rest of alloc'ed memory, building up free Q */
3192 for ( ; i > 0; i--) {
3193 next = t_list + 1;
3194 t_list->t_next = next;
3195 t_list = next;
3196 log->l_ticket_cnt++;
3197 log->l_ticket_tcnt++;
3198 }
3199 t_list->t_next = NULL;
3200 log->l_tail = t_list;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003201 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202} /* xlog_state_ticket_alloc */
3203
3204
3205/*
3206 * Put ticket into free list
3207 *
3208 * Assumption: log lock is held around this call.
3209 */
3210STATIC void
3211xlog_ticket_put(xlog_t *log,
3212 xlog_ticket_t *ticket)
3213{
3214 sv_destroy(&ticket->t_sema);
3215
3216 /*
3217 * Don't think caching will make that much difference. It's
3218 * more important to make debug easier.
3219 */
3220#if 0
3221 /* real code will want to use LIFO for caching */
3222 ticket->t_next = log->l_freelist;
3223 log->l_freelist = ticket;
3224 /* no need to clear fields */
3225#else
3226 /* When we debug, it is easier if tickets are cycled */
3227 ticket->t_next = NULL;
Christoph Hellwig4b809162007-08-16 15:37:36 +10003228 if (log->l_tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 log->l_tail->t_next = ticket;
3230 } else {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003231 ASSERT(log->l_freelist == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 log->l_freelist = ticket;
3233 }
3234 log->l_tail = ticket;
3235#endif /* DEBUG */
3236 log->l_ticket_cnt++;
3237} /* xlog_ticket_put */
3238
3239
3240/*
3241 * Grab ticket off freelist or allocation some more
3242 */
David Chinnera8272ce2007-11-23 16:28:09 +11003243STATIC xlog_ticket_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244xlog_ticket_get(xlog_t *log,
3245 int unit_bytes,
3246 int cnt,
3247 char client,
3248 uint xflags)
3249{
3250 xlog_ticket_t *tic;
3251 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 alloc:
3254 if (log->l_freelist == NULL)
3255 xlog_state_ticket_alloc(log); /* potentially sleep */
3256
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003257 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (log->l_freelist == NULL) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003259 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 goto alloc;
3261 }
3262 tic = log->l_freelist;
3263 log->l_freelist = tic->t_next;
3264 if (log->l_freelist == NULL)
3265 log->l_tail = NULL;
3266 log->l_ticket_cnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003267 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 /*
3270 * Permanent reservations have up to 'cnt'-1 active log operations
3271 * in the log. A unit in this case is the amount of space for one
3272 * of these log operations. Normal reservations have a cnt of 1
3273 * and their unit amount is the total amount of space required.
3274 *
3275 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003276 * which occupy space in the on-disk log.
3277 *
3278 * Normal form of a transaction is:
3279 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3280 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3281 *
3282 * We need to account for all the leadup data and trailer data
3283 * around the transaction data.
3284 * And then we need to account for the worst case in terms of using
3285 * more space.
3286 * The worst case will happen if:
3287 * - the placement of the transaction happens to be such that the
3288 * roundoff is at its maximum
3289 * - the transaction data is synced before the commit record is synced
3290 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3291 * Therefore the commit record is in its own Log Record.
3292 * This can happen as the commit record is called with its
3293 * own region to xlog_write().
3294 * This then means that in the worst case, roundoff can happen for
3295 * the commit-rec as well.
3296 * The commit-rec is smaller than padding in this scenario and so it is
3297 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 */
3299
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003300 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003302 unit_bytes += sizeof(xfs_trans_header_t);
3303
3304 /* for start-rec */
3305 unit_bytes += sizeof(xlog_op_header_t);
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 /* for LR headers */
3308 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3309 unit_bytes += log->l_iclog_hsize * num_headers;
3310
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003311 /* for commit-rec LR header - note: padding will subsume the ophdr */
3312 unit_bytes += log->l_iclog_hsize;
3313
3314 /* for split-recs - ophdrs added when data split over LRs */
3315 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3316
3317 /* for roundoff padding for transaction data and one for commit record */
3318 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3319 log->l_mp->m_sb.sb_logsunit > 1) {
3320 /* log su roundoff */
3321 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3322 } else {
3323 /* BB roundoff */
3324 unit_bytes += 2*BBSIZE;
3325 }
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 tic->t_unit_res = unit_bytes;
3328 tic->t_curr_res = unit_bytes;
3329 tic->t_cnt = cnt;
3330 tic->t_ocnt = cnt;
3331 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3332 tic->t_clientid = client;
3333 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003334 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 if (xflags & XFS_LOG_PERM_RESERV)
3336 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3337 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3338
Christoph Hellwig0adba532007-08-30 17:21:46 +10003339 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return tic;
3342} /* xlog_ticket_get */
3343
3344
3345/******************************************************************************
3346 *
3347 * Log debug routines
3348 *
3349 ******************************************************************************
3350 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003351#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352/*
3353 * Make sure that the destination ptr is within the valid data region of
3354 * one of the iclogs. This uses backup pointers stored in a different
3355 * part of the log in case we trash the log structure.
3356 */
3357void
3358xlog_verify_dest_ptr(xlog_t *log,
3359 __psint_t ptr)
3360{
3361 int i;
3362 int good_ptr = 0;
3363
3364 for (i=0; i < log->l_iclog_bufs; i++) {
3365 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3366 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3367 good_ptr++;
3368 }
3369 if (! good_ptr)
3370 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3371} /* xlog_verify_dest_ptr */
3372
3373STATIC void
3374xlog_verify_grant_head(xlog_t *log, int equals)
3375{
3376 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3377 if (equals)
3378 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3379 else
3380 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3381 } else {
3382 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3383 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3384 }
3385} /* xlog_verify_grant_head */
3386
3387/* check if it will fit */
3388STATIC void
3389xlog_verify_tail_lsn(xlog_t *log,
3390 xlog_in_core_t *iclog,
3391 xfs_lsn_t tail_lsn)
3392{
3393 int blocks;
3394
3395 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3396 blocks =
3397 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3398 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3399 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3400 } else {
3401 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3402
3403 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3404 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3405
3406 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3407 if (blocks < BTOBB(iclog->ic_offset) + 1)
3408 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3409 }
3410} /* xlog_verify_tail_lsn */
3411
3412/*
3413 * Perform a number of checks on the iclog before writing to disk.
3414 *
3415 * 1. Make sure the iclogs are still circular
3416 * 2. Make sure we have a good magic number
3417 * 3. Make sure we don't have magic numbers in the data
3418 * 4. Check fields of each log operation header for:
3419 * A. Valid client identifier
3420 * B. tid ptr value falls in valid ptr space (user space code)
3421 * C. Length in log record header is correct according to the
3422 * individual operation headers within record.
3423 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3424 * log, check the preceding blocks of the physical log to make sure all
3425 * the cycle numbers agree with the current cycle number.
3426 */
3427STATIC void
3428xlog_verify_iclog(xlog_t *log,
3429 xlog_in_core_t *iclog,
3430 int count,
3431 boolean_t syncing)
3432{
3433 xlog_op_header_t *ophead;
3434 xlog_in_core_t *icptr;
3435 xlog_in_core_2_t *xhdr;
3436 xfs_caddr_t ptr;
3437 xfs_caddr_t base_ptr;
3438 __psint_t field_offset;
3439 __uint8_t clientid;
3440 int len, i, j, k, op_len;
3441 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003444 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 icptr = log->l_iclog;
3446 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003447 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 xlog_panic("xlog_verify_iclog: invalid ptr");
3449 icptr = icptr->ic_next;
3450 }
3451 if (icptr != log->l_iclog)
3452 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003453 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003456 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 xlog_panic("xlog_verify_iclog: invalid magic num");
3458
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003459 ptr = (xfs_caddr_t) &iclog->ic_header;
3460 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003462 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 xlog_panic("xlog_verify_iclog: unexpected magic num");
3464 }
3465
3466 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003467 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 ptr = iclog->ic_datap;
3469 base_ptr = ptr;
3470 ophead = (xlog_op_header_t *)ptr;
3471 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3472 for (i = 0; i < len; i++) {
3473 ophead = (xlog_op_header_t *)ptr;
3474
3475 /* clientid is only 1 byte */
3476 field_offset = (__psint_t)
3477 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3478 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3479 clientid = ophead->oh_clientid;
3480 } else {
3481 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3482 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3483 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3484 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003485 clientid = xlog_get_client_id(
3486 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003488 clientid = xlog_get_client_id(
3489 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
3491 }
3492 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003493 cmn_err(CE_WARN, "xlog_verify_iclog: "
3494 "invalid clientid %d op 0x%p offset 0x%lx",
3495 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497 /* check length */
3498 field_offset = (__psint_t)
3499 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3500 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003501 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 } else {
3503 idx = BTOBBT((__psint_t)&ophead->oh_len -
3504 (__psint_t)iclog->ic_datap);
3505 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3506 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3507 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003508 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003510 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
3512 }
3513 ptr += sizeof(xlog_op_header_t) + op_len;
3514 }
3515} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003516#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003519 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 */
3521STATIC int
3522xlog_state_ioerror(
3523 xlog_t *log)
3524{
3525 xlog_in_core_t *iclog, *ic;
3526
3527 iclog = log->l_iclog;
3528 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3529 /*
3530 * Mark all the incore logs IOERROR.
3531 * From now on, no log flushes will result.
3532 */
3533 ic = iclog;
3534 do {
3535 ic->ic_state = XLOG_STATE_IOERROR;
3536 ic = ic->ic_next;
3537 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
3540 /*
3541 * Return non-zero, if state transition has already happened.
3542 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003543 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
3546/*
3547 * This is called from xfs_force_shutdown, when we're forcibly
3548 * shutting down the filesystem, typically because of an IO error.
3549 * Our main objectives here are to make sure that:
3550 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3551 * parties to find out, 'atomically'.
3552 * b. those who're sleeping on log reservations, pinned objects and
3553 * other resources get woken up, and be told the bad news.
3554 * c. nothing new gets queued up after (a) and (b) are done.
3555 * d. if !logerror, flush the iclogs to disk, then seal them off
3556 * for business.
3557 */
3558int
3559xfs_log_force_umount(
3560 struct xfs_mount *mp,
3561 int logerror)
3562{
3563 xlog_ticket_t *tic;
3564 xlog_t *log;
3565 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003566 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
3568 log = mp->m_log;
3569
3570 /*
3571 * If this happens during log recovery, don't worry about
3572 * locking; the log isn't open for business yet.
3573 */
3574 if (!log ||
3575 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3576 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3577 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
3580
3581 /*
3582 * Somebody could've already done the hard work for us.
3583 * No need to get locks for this.
3584 */
3585 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3586 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003587 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 }
3589 retval = 0;
3590 /*
3591 * We must hold both the GRANT lock and the LOG lock,
3592 * before we mark the filesystem SHUTDOWN and wake
3593 * everybody up to tell the bad news.
3594 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003595 spin_lock(&log->l_grant_lock);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003596 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3598 XFS_BUF_DONE(mp->m_sb_bp);
3599 /*
3600 * This flag is sort of redundant because of the mount flag, but
3601 * it's good to maintain the separation between the log and the rest
3602 * of XFS.
3603 */
3604 log->l_flags |= XLOG_IO_ERROR;
3605
3606 /*
3607 * If we hit a log error, we want to mark all the iclogs IOERROR
3608 * while we're still holding the loglock.
3609 */
3610 if (logerror)
3611 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003612 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 /*
3615 * We don't want anybody waiting for log reservations
3616 * after this. That means we have to wake up everybody
3617 * queued up on reserve_headq as well as write_headq.
3618 * In addition, we make sure in xlog_{re}grant_log_space
3619 * that we don't enqueue anything once the SHUTDOWN flag
3620 * is set, and this action is protected by the GRANTLOCK.
3621 */
3622 if ((tic = log->l_reserve_headq)) {
3623 do {
3624 sv_signal(&tic->t_sema);
3625 tic = tic->t_next;
3626 } while (tic != log->l_reserve_headq);
3627 }
3628
3629 if ((tic = log->l_write_headq)) {
3630 do {
3631 sv_signal(&tic->t_sema);
3632 tic = tic->t_next;
3633 } while (tic != log->l_write_headq);
3634 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003635 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3638 ASSERT(!logerror);
3639 /*
3640 * Force the incore logs to disk before shutting the
3641 * log down completely.
3642 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003643 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003644 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003646 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 }
3648 /*
3649 * Wake up everybody waiting on xfs_log_force.
3650 * Callback all log item committed functions as if the
3651 * log writes were completed.
3652 */
3653 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3654
3655#ifdef XFSERRORDEBUG
3656 {
3657 xlog_in_core_t *iclog;
3658
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003659 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 iclog = log->l_iclog;
3661 do {
3662 ASSERT(iclog->ic_callback == 0);
3663 iclog = iclog->ic_next;
3664 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003665 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
3667#endif
3668 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003669 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003672STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673xlog_iclogs_empty(xlog_t *log)
3674{
3675 xlog_in_core_t *iclog;
3676
3677 iclog = log->l_iclog;
3678 do {
3679 /* endianness does not matter here, zero is zero in
3680 * any language.
3681 */
3682 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 iclog = iclog->ic_next;
3685 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003686 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}