blob: 1fa9809338954d05c460ce2562a91eaeee6e92e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44
45#define xlog_write_adv_cnt(ptr, len, off, bytes) \
46 { (ptr) += (bytes); \
47 (len) -= (bytes); \
48 (off) += (bytes);}
49
50/* Local miscellaneous function prototypes */
51STATIC int xlog_bdstrat_cb(struct xfs_buf *);
52STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
53 xlog_in_core_t **, xfs_lsn_t *);
54STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
55 xfs_buftarg_t *log_target,
56 xfs_daddr_t blk_offset,
57 int num_bblks);
58STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
59STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100060STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
62 int nentries, xfs_log_ticket_t tic,
63 xfs_lsn_t *start_lsn,
64 xlog_in_core_t **commit_iclog,
65 uint flags);
66
67/* local state machine functions */
68STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
69STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
70STATIC int xlog_state_get_iclog_space(xlog_t *log,
71 int len,
72 xlog_in_core_t **iclog,
73 xlog_ticket_t *ticket,
74 int *continued_write,
75 int *logoffsetp);
76STATIC void xlog_state_put_ticket(xlog_t *log,
77 xlog_ticket_t *tic);
78STATIC int xlog_state_release_iclog(xlog_t *log,
79 xlog_in_core_t *iclog);
80STATIC void xlog_state_switch_iclogs(xlog_t *log,
81 xlog_in_core_t *iclog,
82 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110083STATIC int xlog_state_sync(xlog_t *log,
84 xfs_lsn_t lsn,
85 uint flags,
86 int *log_flushed);
87STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
89
90/* local functions to manipulate grant head */
91STATIC int xlog_grant_log_space(xlog_t *log,
92 xlog_ticket_t *xtic);
93STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
94 int need_bytes);
95STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
96 xlog_ticket_t *ticket);
97STATIC int xlog_regrant_write_log_space(xlog_t *log,
98 xlog_ticket_t *ticket);
99STATIC void xlog_ungrant_log_space(xlog_t *log,
100 xlog_ticket_t *ticket);
101
102
103/* local ticket functions */
104STATIC void xlog_state_ticket_alloc(xlog_t *log);
105STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
106 int unit_bytes,
107 int count,
108 char clientid,
109 uint flags);
110STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
111
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100112#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
114STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
115STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
116 int count, boolean_t syncing);
117STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
118 xfs_lsn_t tail_lsn);
119#else
120#define xlog_verify_dest_ptr(a,b)
121#define xlog_verify_grant_head(a,b)
122#define xlog_verify_iclog(a,b,c,d)
123#define xlog_verify_tail_lsn(a,b,c)
124#endif
125
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000126STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#if defined(XFS_LOG_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void
130xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
131{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000132 unsigned long cnts;
133
134 if (!log->l_grant_trace) {
135 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
136 if (!log->l_grant_trace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
138 }
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000139 /* ticket counts are 1 byte each */
140 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 ktrace_enter(log->l_grant_trace,
143 (void *)tic,
144 (void *)log->l_reserve_headq,
145 (void *)log->l_write_headq,
146 (void *)((unsigned long)log->l_grant_reserve_cycle),
147 (void *)((unsigned long)log->l_grant_reserve_bytes),
148 (void *)((unsigned long)log->l_grant_write_cycle),
149 (void *)((unsigned long)log->l_grant_write_bytes),
150 (void *)((unsigned long)log->l_curr_cycle),
151 (void *)((unsigned long)log->l_curr_block),
152 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
153 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
154 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000155 (void *)((unsigned long)tic->t_trans_type),
156 (void *)cnts,
157 (void *)((unsigned long)tic->t_curr_res),
158 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void
162xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!iclog->ic_trace)
165 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
166 ktrace_enter(iclog->ic_trace,
167 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100168 (void *)((unsigned long)current_pid()),
169 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
170 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
171 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
172 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#else
175#define xlog_trace_loggrant(log,tic,string)
176#define xlog_trace_iclog(iclog,state)
177#endif /* XFS_LOG_TRACE */
178
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100179
180static void
181xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
182{
183 if (*qp) {
184 tic->t_next = (*qp);
185 tic->t_prev = (*qp)->t_prev;
186 (*qp)->t_prev->t_next = tic;
187 (*qp)->t_prev = tic;
188 } else {
189 tic->t_prev = tic->t_next = tic;
190 *qp = tic;
191 }
192
193 tic->t_flags |= XLOG_TIC_IN_Q;
194}
195
196static void
197xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
198{
199 if (tic == tic->t_next) {
200 *qp = NULL;
201 } else {
202 *qp = tic->t_next;
203 tic->t_next->t_prev = tic->t_prev;
204 tic->t_prev->t_next = tic->t_next;
205 }
206
207 tic->t_next = tic->t_prev = NULL;
208 tic->t_flags &= ~XLOG_TIC_IN_Q;
209}
210
211static void
212xlog_grant_sub_space(struct log *log, int bytes)
213{
214 log->l_grant_write_bytes -= bytes;
215 if (log->l_grant_write_bytes < 0) {
216 log->l_grant_write_bytes += log->l_logsize;
217 log->l_grant_write_cycle--;
218 }
219
220 log->l_grant_reserve_bytes -= bytes;
221 if ((log)->l_grant_reserve_bytes < 0) {
222 log->l_grant_reserve_bytes += log->l_logsize;
223 log->l_grant_reserve_cycle--;
224 }
225
226}
227
228static void
229xlog_grant_add_space_write(struct log *log, int bytes)
230{
231 log->l_grant_write_bytes += bytes;
232 if (log->l_grant_write_bytes > log->l_logsize) {
233 log->l_grant_write_bytes -= log->l_logsize;
234 log->l_grant_write_cycle++;
235 }
236}
237
238static void
239xlog_grant_add_space_reserve(struct log *log, int bytes)
240{
241 log->l_grant_reserve_bytes += bytes;
242 if (log->l_grant_reserve_bytes > log->l_logsize) {
243 log->l_grant_reserve_bytes -= log->l_logsize;
244 log->l_grant_reserve_cycle++;
245 }
246}
247
248static inline void
249xlog_grant_add_space(struct log *log, int bytes)
250{
251 xlog_grant_add_space_write(log, bytes);
252 xlog_grant_add_space_reserve(log, bytes);
253}
254
Christoph Hellwig0adba532007-08-30 17:21:46 +1000255static void
256xlog_tic_reset_res(xlog_ticket_t *tic)
257{
258 tic->t_res_num = 0;
259 tic->t_res_arr_sum = 0;
260 tic->t_res_num_ophdrs = 0;
261}
262
263static void
264xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
265{
266 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
267 /* add to overflow and start again */
268 tic->t_res_o_flow += tic->t_res_arr_sum;
269 tic->t_res_num = 0;
270 tic->t_res_arr_sum = 0;
271 }
272
273 tic->t_res_arr[tic->t_res_num].r_len = len;
274 tic->t_res_arr[tic->t_res_num].r_type = type;
275 tic->t_res_arr_sum += len;
276 tic->t_res_num++;
277}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * NOTES:
281 *
282 * 1. currblock field gets updated at startup and after in-core logs
283 * marked as with WANT_SYNC.
284 */
285
286/*
287 * This routine is called when a user of a log manager ticket is done with
288 * the reservation. If the ticket was ever used, then a commit record for
289 * the associated transaction is written out as a log operation header with
290 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
291 * a given ticket. If the ticket was one with a permanent reservation, then
292 * a few operations are done differently. Permanent reservation tickets by
293 * default don't release the reservation. They just commit the current
294 * transaction with the belief that the reservation is still needed. A flag
295 * must be passed in before permanent reservations are actually released.
296 * When these type of tickets are not released, they need to be set into
297 * the inited state again. By doing this, a start record will be written
298 * out when the next write occurs.
299 */
300xfs_lsn_t
301xfs_log_done(xfs_mount_t *mp,
302 xfs_log_ticket_t xtic,
303 void **iclog,
304 uint flags)
305{
306 xlog_t *log = mp->m_log;
307 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
308 xfs_lsn_t lsn = 0;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (XLOG_FORCED_SHUTDOWN(log) ||
311 /*
312 * If nothing was ever written, don't write out commit record.
313 * If we get an error, just continue and give back the log ticket.
314 */
315 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
316 (xlog_commit_record(mp, ticket,
317 (xlog_in_core_t **)iclog, &lsn)))) {
318 lsn = (xfs_lsn_t) -1;
319 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
320 flags |= XFS_LOG_REL_PERM_RESERV;
321 }
322 }
323
324
325 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
326 (flags & XFS_LOG_REL_PERM_RESERV)) {
327 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000328 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * request has been made to release a permanent reservation.
330 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000331 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xlog_ungrant_log_space(log, ticket);
333 xlog_state_put_ticket(log, ticket);
334 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000335 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 xlog_regrant_reserve_log_space(log, ticket);
337 }
338
339 /* If this ticket was a permanent reservation and we aren't
340 * trying to release it, reset the inited flags; so next time
341 * we write, a start record will be written out.
342 */
343 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
344 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
345 ticket->t_flags |= XLOG_TIC_INITED;
346
347 return lsn;
348} /* xfs_log_done */
349
350
351/*
352 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
353 * the force is done synchronously.
354 *
355 * Asynchronous forces are implemented by setting the WANT_SYNC
356 * bit in the appropriate in-core log and then returning.
357 *
358 * Synchronous forces are implemented with a semaphore. All callers
359 * to force a given lsn to disk will wait on a semaphore attached to the
360 * specific in-core log. When given in-core log finally completes its
361 * write to disk, that thread will wake up all threads waiting on the
362 * semaphore.
363 */
364int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100365_xfs_log_force(
366 xfs_mount_t *mp,
367 xfs_lsn_t lsn,
368 uint flags,
369 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100371 xlog_t *log = mp->m_log;
372 int dummy;
373
374 if (!log_flushed)
375 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ASSERT(flags & XFS_LOG_FORCE);
378
379 XFS_STATS_INC(xs_log_force);
380
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100381 if (log->l_flags & XLOG_IO_ERROR)
382 return XFS_ERROR(EIO);
383 if (lsn == 0)
384 return xlog_state_sync_all(log, flags, log_flushed);
385 else
386 return xlog_state_sync(log, lsn, flags, log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387} /* xfs_log_force */
388
389/*
390 * Attaches a new iclog I/O completion callback routine during
391 * transaction commit. If the log is in error state, a non-zero
392 * return code is handed back and the caller is responsible for
393 * executing the callback at an appropriate time.
394 */
395int
396xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
397 void *iclog_hndl, /* iclog to hang callback off */
398 xfs_log_callback_t *cb)
399{
400 xlog_t *log = mp->m_log;
401 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000402 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cb->cb_next = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000405 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
407 if (!abortflg) {
408 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
409 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
410 cb->cb_next = NULL;
411 *(iclog->ic_callback_tail) = cb;
412 iclog->ic_callback_tail = &(cb->cb_next);
413 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000414 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return abortflg;
416} /* xfs_log_notify */
417
418int
419xfs_log_release_iclog(xfs_mount_t *mp,
420 void *iclog_hndl)
421{
422 xlog_t *log = mp->m_log;
423 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
424
425 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000426 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100427 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 return 0;
431}
432
433/*
434 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
435 * to the reservation.
436 * 2. Potentially, push buffers at tail of log to disk.
437 *
438 * Each reservation is going to reserve extra space for a log record header.
439 * When writes happen to the on-disk log, we don't subtract the length of the
440 * log record header from any reservation. By wasting space in each
441 * reservation, we prevent over allocation problems.
442 */
443int
444xfs_log_reserve(xfs_mount_t *mp,
445 int unit_bytes,
446 int cnt,
447 xfs_log_ticket_t *ticket,
448 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000449 uint flags,
450 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 xlog_t *log = mp->m_log;
453 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100454 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
457 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
458
459 if (XLOG_FORCED_SHUTDOWN(log))
460 return XFS_ERROR(EIO);
461
462 XFS_STATS_INC(xs_try_logspace);
463
464 if (*ticket != NULL) {
465 ASSERT(flags & XFS_LOG_PERM_RESERV);
466 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000467 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
469 retval = xlog_regrant_write_log_space(log, internal_ticket);
470 } else {
471 /* may sleep if need to allocate more tickets */
472 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
473 client, flags);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000474 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000476 xlog_trace_loggrant(log, internal_ticket,
477 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
478 "xfs_log_reserve: create new ticket (permanent trans)" :
479 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 xlog_grant_push_ail(mp,
481 (internal_ticket->t_unit_res *
482 internal_ticket->t_cnt));
483 retval = xlog_grant_log_space(log, internal_ticket);
484 }
485
486 return retval;
487} /* xfs_log_reserve */
488
489
490/*
491 * Mount a log filesystem
492 *
493 * mp - ubiquitous xfs mount point structure
494 * log_target - buftarg of on-disk log device
495 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
496 * num_bblocks - Number of BBSIZE blocks in on-disk log
497 *
498 * Return error or zero.
499 */
500int
David Chinner249a8c12008-02-05 12:13:32 +1100501xfs_log_mount(
502 xfs_mount_t *mp,
503 xfs_buftarg_t *log_target,
504 xfs_daddr_t blk_offset,
505 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
David Chinner249a8c12008-02-05 12:13:32 +1100507 int error;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
510 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
511 else {
512 cmn_err(CE_NOTE,
513 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
514 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000515 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
David Chinner249a8c12008-02-05 12:13:32 +1100521 * Initialize the AIL now we have a log.
522 */
523 spin_lock_init(&mp->m_ail_lock);
524 error = xfs_trans_ail_init(mp);
525 if (error) {
526 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
527 goto error;
528 }
529
530 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * skip log recovery on a norecovery mount. pretend it all
532 * just worked.
533 */
534 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100535 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000538 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Eric Sandeen65be6052006-01-11 15:34:19 +1100540 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000543 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (error) {
545 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
David Chinner249a8c12008-02-05 12:13:32 +1100546 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 }
549
550 /* Normal transactions can now occur */
551 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
552
553 /* End mounting message in xfs_log_mount_finish */
554 return 0;
David Chinner249a8c12008-02-05 12:13:32 +1100555error:
556 xfs_log_unmount_dealloc(mp);
557 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558} /* xfs_log_mount */
559
560/*
561 * Finish the recovery of the file system. This is separate from
562 * the xfs_log_mount() call, because it depends on the code in
563 * xfs_mountfs() to read in the root and real-time bitmap inodes
564 * between calling xfs_log_mount() and here.
565 *
566 * mp - ubiquitous xfs mount point structure
567 */
568int
569xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
570{
571 int error;
572
573 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
574 error = xlog_recover_finish(mp->m_log, mfsi_flags);
575 else {
576 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000577 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 return error;
581}
582
583/*
584 * Unmount processing for the log.
585 */
586int
587xfs_log_unmount(xfs_mount_t *mp)
588{
589 int error;
590
591 error = xfs_log_unmount_write(mp);
592 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100593 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596/*
597 * Final log writes as part of unmount.
598 *
599 * Mark the filesystem clean as unmount happens. Note that during relocation
600 * this routine needs to be executed as part of source-bag while the
601 * deallocation must not be done until source-end.
602 */
603
604/*
605 * Unmount record used to have a string "Unmount filesystem--" in the
606 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
607 * We just write the magic number now since that particular field isn't
608 * currently architecture converted and "nUmount" is a bit foo.
609 * As far as I know, there weren't any dependencies on the old behaviour.
610 */
611
612int
613xfs_log_unmount_write(xfs_mount_t *mp)
614{
615 xlog_t *log = mp->m_log;
616 xlog_in_core_t *iclog;
617#ifdef DEBUG
618 xlog_in_core_t *first_iclog;
619#endif
620 xfs_log_iovec_t reg[1];
621 xfs_log_ticket_t tic = NULL;
622 xfs_lsn_t lsn;
623 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* the data section must be 32 bit size aligned */
626 struct {
627 __uint16_t magic;
628 __uint16_t pad1;
629 __uint32_t pad2; /* may as well make it 64 bits */
630 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Don't write out unmount record on read-only mounts.
634 * Or, if we are doing a forced umount (typically because of IO errors).
635 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000636 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638
639 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
640
641#ifdef DEBUG
642 first_iclog = iclog = log->l_iclog;
643 do {
644 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
645 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
646 ASSERT(iclog->ic_offset == 0);
647 }
648 iclog = iclog->ic_next;
649 } while (iclog != first_iclog);
650#endif
651 if (! (XLOG_FORCED_SHUTDOWN(log))) {
652 reg[0].i_addr = (void*)&magic;
653 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000654 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Tim Shimmin955e47a2006-09-28 11:04:16 +1000656 error = xfs_log_reserve(mp, 600, 1, &tic,
657 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!error) {
659 /* remove inited flag */
660 ((xlog_ticket_t *)tic)->t_flags = 0;
661 error = xlog_write(mp, reg, 1, tic, &lsn,
662 NULL, XLOG_UNMOUNT_TRANS);
663 /*
664 * At this point, we're umounting anyway,
665 * so there's no point in transitioning log state
666 * to IOERROR. Just continue...
667 */
668 }
669
670 if (error) {
671 xfs_fs_cmn_err(CE_ALERT, mp,
672 "xfs_log_unmount: unmount record failed");
673 }
674
675
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000676 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100678 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000679 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xlog_state_want_sync(log, iclog);
681 (void) xlog_state_release_iclog(log, iclog);
682
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000683 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
685 iclog->ic_state == XLOG_STATE_DIRTY)) {
686 if (!XLOG_FORCED_SHUTDOWN(log)) {
687 sv_wait(&iclog->ic_forcesema, PMEM,
688 &log->l_icloglock, s);
689 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000690 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000693 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000695 if (tic) {
696 xlog_trace_loggrant(log, tic, "unmount rec");
697 xlog_ungrant_log_space(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 xlog_state_put_ticket(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
701 /*
702 * We're already in forced_shutdown mode, couldn't
703 * even attempt to write out the unmount transaction.
704 *
705 * Go through the motions of sync'ing and releasing
706 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000707 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * be in progress. Do this as a separate section of
709 * code so we'll know if we ever get stuck here that
710 * we're in this odd situation of trying to unmount
711 * a file system that went into forced_shutdown as
712 * the result of an unmount..
713 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000714 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100716 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000717 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 xlog_state_want_sync(log, iclog);
720 (void) xlog_state_release_iclog(log, iclog);
721
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000722 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
725 || iclog->ic_state == XLOG_STATE_DIRTY
726 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
727
728 sv_wait(&iclog->ic_forcesema, PMEM,
729 &log->l_icloglock, s);
730 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000731 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 }
734
735 return 0;
736} /* xfs_log_unmount_write */
737
738/*
739 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100740 *
741 * We need to stop the aild from running before we destroy
742 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744void
745xfs_log_unmount_dealloc(xfs_mount_t *mp)
746{
David Chinner249a8c12008-02-05 12:13:32 +1100747 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000748 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
752 * Write region vectors to log. The write happens using the space reservation
753 * of the ticket (tic). It is not a requirement that all writes for a given
754 * transaction occur with one call to xfs_log_write().
755 */
756int
757xfs_log_write(xfs_mount_t * mp,
758 xfs_log_iovec_t reg[],
759 int nentries,
760 xfs_log_ticket_t tic,
761 xfs_lsn_t *start_lsn)
762{
763 int error;
764 xlog_t *log = mp->m_log;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (XLOG_FORCED_SHUTDOWN(log))
767 return XFS_ERROR(EIO);
768
769 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000770 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100772 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773} /* xfs_log_write */
774
775
776void
777xfs_log_move_tail(xfs_mount_t *mp,
778 xfs_lsn_t tail_lsn)
779{
780 xlog_ticket_t *tic;
781 xlog_t *log = mp->m_log;
782 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (XLOG_FORCED_SHUTDOWN(log))
785 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (tail_lsn == 0) {
788 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000789 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000791 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000794 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
797 * tail_lsn.
798 */
799 if (tail_lsn != 1) {
800 log->l_tail_lsn = tail_lsn;
801 }
802
803 if ((tic = log->l_write_headq)) {
804#ifdef DEBUG
805 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
806 panic("Recovery problem");
807#endif
808 cycle = log->l_grant_write_cycle;
809 bytes = log->l_grant_write_bytes;
810 free_bytes = xlog_space_left(log, cycle, bytes);
811 do {
812 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
813
814 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
815 break;
816 tail_lsn = 0;
817 free_bytes -= tic->t_unit_res;
818 sv_signal(&tic->t_sema);
819 tic = tic->t_next;
820 } while (tic != log->l_write_headq);
821 }
822 if ((tic = log->l_reserve_headq)) {
823#ifdef DEBUG
824 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
825 panic("Recovery problem");
826#endif
827 cycle = log->l_grant_reserve_cycle;
828 bytes = log->l_grant_reserve_bytes;
829 free_bytes = xlog_space_left(log, cycle, bytes);
830 do {
831 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
832 need_bytes = tic->t_unit_res*tic->t_cnt;
833 else
834 need_bytes = tic->t_unit_res;
835 if (free_bytes < need_bytes && tail_lsn != 1)
836 break;
837 tail_lsn = 0;
838 free_bytes -= need_bytes;
839 sv_signal(&tic->t_sema);
840 tic = tic->t_next;
841 } while (tic != log->l_reserve_headq);
842 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000843 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844} /* xfs_log_move_tail */
845
846/*
847 * Determine if we have a transaction that has gone to disk
848 * that needs to be covered. Log activity needs to be idle (no AIL and
849 * nothing in the iclogs). And, we need to be in the right state indicating
850 * something has gone out.
851 */
852int
853xfs_log_need_covered(xfs_mount_t *mp)
854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int needed = 0, gen;
856 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David Chinner92821e22007-05-24 15:26:31 +1000858 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 0;
860
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000861 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
863 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
864 && !xfs_trans_first_ail(mp, &gen)
865 && xlog_iclogs_empty(log)) {
866 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
867 log->l_covered_state = XLOG_STATE_COVER_DONE;
868 else {
869 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
870 log->l_covered_state = XLOG_STATE_COVER_DONE2;
871 }
872 needed = 1;
873 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000874 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100875 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/******************************************************************************
879 *
880 * local routines
881 *
882 ******************************************************************************
883 */
884
885/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
886 * The log manager must keep track of the last LR which was committed
887 * to disk. The lsn of this LR will become the new tail_lsn whenever
888 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
889 * the situation where stuff could be written into the log but nothing
890 * was ever in the AIL when asked. Eventually, we panic since the
891 * tail hits the head.
892 *
893 * We may be holding the log iclog lock upon entering this routine.
894 */
895xfs_lsn_t
896xlog_assign_tail_lsn(xfs_mount_t *mp)
897{
898 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 xlog_t *log = mp->m_log;
900
901 tail_lsn = xfs_trans_tail_ail(mp);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000902 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (tail_lsn != 0) {
904 log->l_tail_lsn = tail_lsn;
905 } else {
906 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
907 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000908 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 return tail_lsn;
911} /* xlog_assign_tail_lsn */
912
913
914/*
915 * Return the space in the log between the tail and the head. The head
916 * is passed in the cycle/bytes formal parms. In the special case where
917 * the reserve head has wrapped passed the tail, this calculation is no
918 * longer valid. In this case, just return 0 which means there is no space
919 * in the log. This works for all places where this function is called
920 * with the reserve head. Of course, if the write head were to ever
921 * wrap the tail, we should blow up. Rather than catch this case here,
922 * we depend on other ASSERTions in other parts of the code. XXXmiken
923 *
924 * This code also handles the case where the reservation head is behind
925 * the tail. The details of this case are described below, but the end
926 * result is that we return the size of the log as the amount of space left.
927 */
David Chinnera8272ce2007-11-23 16:28:09 +1100928STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929xlog_space_left(xlog_t *log, int cycle, int bytes)
930{
931 int free_bytes;
932 int tail_bytes;
933 int tail_cycle;
934
935 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
936 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
937 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
938 free_bytes = log->l_logsize - (bytes - tail_bytes);
939 } else if ((tail_cycle + 1) < cycle) {
940 return 0;
941 } else if (tail_cycle < cycle) {
942 ASSERT(tail_cycle == (cycle - 1));
943 free_bytes = tail_bytes - bytes;
944 } else {
945 /*
946 * The reservation head is behind the tail.
947 * In this case we just want to return the size of the
948 * log as the amount of space left.
949 */
950 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
951 "xlog_space_left: head behind tail\n"
952 " tail_cycle = %d, tail_bytes = %d\n"
953 " GH cycle = %d, GH bytes = %d",
954 tail_cycle, tail_bytes, cycle, bytes);
955 ASSERT(0);
956 free_bytes = log->l_logsize;
957 }
958 return free_bytes;
959} /* xlog_space_left */
960
961
962/*
963 * Log function which is called when an io completes.
964 *
965 * The log manager needs its own routine, in order to control what
966 * happens with the buffer after the write completes.
967 */
968void
969xlog_iodone(xfs_buf_t *bp)
970{
971 xlog_in_core_t *iclog;
972 xlog_t *l;
973 int aborted;
974
975 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
976 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
977 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
978 aborted = 0;
979
980 /*
981 * Some versions of cpp barf on the recursive definition of
982 * ic_log -> hic_fields.ic_log and expand ic_log twice when
983 * it is passed through two macros. Workaround broken cpp.
984 */
985 l = iclog->ic_log;
986
987 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000988 * If the ordered flag has been removed by a lower
989 * layer, it means the underlyin device no longer supports
990 * barrier I/O. Warn loudly and turn off barriers.
991 */
992 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
993 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
994 xfs_fs_cmn_err(CE_WARN, l->l_mp,
995 "xlog_iodone: Barriers are no longer supported"
996 " by device. Disabling barriers\n");
997 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
998 }
999
1000 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * Race to shutdown the filesystem if we see an error.
1002 */
1003 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1004 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1005 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1006 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001007 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * This flag will be propagated to the trans-committed
1010 * callback routines to let them know that the log-commit
1011 * didn't succeed.
1012 */
1013 aborted = XFS_LI_ABORTED;
1014 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1015 aborted = XFS_LI_ABORTED;
1016 }
David Chinner3db296f2007-05-14 18:24:16 +10001017
1018 /* log I/O is always issued ASYNC */
1019 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001021 /*
1022 * do not reference the buffer (bp) here as we could race
1023 * with it being freed after writing the unmount record to the
1024 * log.
1025 */
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027} /* xlog_iodone */
1028
1029/*
1030 * The bdstrat callback function for log bufs. This gives us a central
1031 * place to trap bufs in case we get hit by a log I/O error and need to
1032 * shutdown. Actually, in practice, even when we didn't get a log error,
1033 * we transition the iclogs to IOERROR state *after* flushing all existing
1034 * iclogs to disk. This is because we don't want anymore new transactions to be
1035 * started or completed afterwards.
1036 */
1037STATIC int
1038xlog_bdstrat_cb(struct xfs_buf *bp)
1039{
1040 xlog_in_core_t *iclog;
1041
1042 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1043
1044 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1045 /* note for irix bstrat will need struct bdevsw passed
1046 * Fix the following macro if the code ever is merged
1047 */
1048 XFS_bdstrat(bp);
1049 return 0;
1050 }
1051
1052 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1053 XFS_BUF_ERROR(bp, EIO);
1054 XFS_BUF_STALE(bp);
1055 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001056 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058
1059}
1060
1061/*
1062 * Return size of each in-core log record buffer.
1063 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001064 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 *
1066 * If the filesystem blocksize is too large, we may need to choose a
1067 * larger size since the directory code currently logs entire blocks.
1068 */
1069
1070STATIC void
1071xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1072 xlog_t *log)
1073{
1074 int size;
1075 int xhdrs;
1076
Eric Sandeen1cb51252007-08-16 16:24:43 +10001077 if (mp->m_logbufs <= 0)
1078 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1079 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001080 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
1083 * Buffer size passed in from mount system call.
1084 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001085 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 size = log->l_iclog_size = mp->m_logbsize;
1087 log->l_iclog_size_log = 0;
1088 while (size != 1) {
1089 log->l_iclog_size_log++;
1090 size >>= 1;
1091 }
1092
Eric Sandeen62118702008-03-06 13:44:28 +11001093 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* # headers = size / 32K
1095 * one header holds cycles from 32K of data
1096 */
1097
1098 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1099 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1100 xhdrs++;
1101 log->l_iclog_hsize = xhdrs << BBSHIFT;
1102 log->l_iclog_heads = xhdrs;
1103 } else {
1104 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1105 log->l_iclog_hsize = BBSIZE;
1106 log->l_iclog_heads = 1;
1107 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001108 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Eric Sandeen1cb51252007-08-16 16:24:43 +10001111 /* All machines use 32KB buffers by default. */
1112 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1113 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /* the default log size is 16k or 32k which is one header sector */
1116 log->l_iclog_hsize = BBSIZE;
1117 log->l_iclog_heads = 1;
1118
1119 /*
1120 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1121 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1122 */
1123 if (mp->m_sb.sb_blocksize >= 16*1024) {
1124 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1125 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001126 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 switch (mp->m_sb.sb_blocksize) {
1128 case 16*1024: /* 16 KB */
1129 log->l_iclog_bufs = 3;
1130 break;
1131 case 32*1024: /* 32 KB */
1132 log->l_iclog_bufs = 4;
1133 break;
1134 case 64*1024: /* 64 KB */
1135 log->l_iclog_bufs = 8;
1136 break;
1137 default:
1138 xlog_panic("XFS: Invalid blocksize");
1139 break;
1140 }
1141 }
1142 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001143
1144done: /* are we being asked to make the sizes selected above visible? */
1145 if (mp->m_logbufs == 0)
1146 mp->m_logbufs = log->l_iclog_bufs;
1147 if (mp->m_logbsize == 0)
1148 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149} /* xlog_get_iclog_buffer_size */
1150
1151
1152/*
1153 * This routine initializes some of the log structure for a given mount point.
1154 * Its primary purpose is to fill in enough, so recovery can occur. However,
1155 * some other stuff may be filled in too.
1156 */
1157STATIC xlog_t *
1158xlog_alloc_log(xfs_mount_t *mp,
1159 xfs_buftarg_t *log_target,
1160 xfs_daddr_t blk_offset,
1161 int num_bblks)
1162{
1163 xlog_t *log;
1164 xlog_rec_header_t *head;
1165 xlog_in_core_t **iclogp;
1166 xlog_in_core_t *iclog, *prev_iclog=NULL;
1167 xfs_buf_t *bp;
1168 int i;
1169 int iclogsize;
1170
1171 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1172
1173 log->l_mp = mp;
1174 log->l_targ = log_target;
1175 log->l_logsize = BBTOB(num_bblks);
1176 log->l_logBBstart = blk_offset;
1177 log->l_logBBsize = num_bblks;
1178 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1179 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1180
1181 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001182 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1184 log->l_last_sync_lsn = log->l_tail_lsn;
1185 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1186 log->l_grant_reserve_cycle = 1;
1187 log->l_grant_write_cycle = 1;
1188
Eric Sandeen62118702008-03-06 13:44:28 +11001189 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1191 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1192 /* for larger sector sizes, must have v2 or external log */
1193 ASSERT(log->l_sectbb_log == 0 ||
1194 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001195 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1197 }
1198 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1199
1200 xlog_get_iclog_buffer_size(mp, log);
1201
1202 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1203 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1204 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1205 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1206 ASSERT(XFS_BUF_ISBUSY(bp));
1207 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1208 log->l_xbuf = bp;
1209
Eric Sandeen007c61c2007-10-11 17:43:56 +10001210 spin_lock_init(&log->l_icloglock);
1211 spin_lock_init(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 initnsema(&log->l_flushsema, 0, "ic-flush");
1213 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1214
1215 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1216 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1217
1218 iclogp = &log->l_iclog;
1219 /*
1220 * The amount of memory to allocate for the iclog structure is
1221 * rather funky due to the way the structure is defined. It is
1222 * done this way so that we can use different sizes for machines
1223 * with different amounts of memory. See the definition of
1224 * xlog_in_core_t in xfs_log_priv.h for details.
1225 */
1226 iclogsize = log->l_iclog_size;
1227 ASSERT(log->l_iclog_size >= 4096);
1228 for (i=0; i < log->l_iclog_bufs; i++) {
1229 *iclogp = (xlog_in_core_t *)
1230 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1231 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 iclog->ic_prev = prev_iclog;
1233 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001234
1235 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1236 if (!XFS_BUF_CPSEMA(bp))
1237 ASSERT(0);
1238 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1239 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1240 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1241 iclog->ic_bp = bp;
1242 iclog->hic_data = bp->b_addr;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1245
1246 head = &iclog->ic_header;
1247 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001248 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1249 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001250 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001251 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001253 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1258 iclog->ic_state = XLOG_STATE_ACTIVE;
1259 iclog->ic_log = log;
1260 iclog->ic_callback_tail = &(iclog->ic_callback);
1261 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1262
1263 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1264 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1265 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1266 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1267
1268 iclogp = &iclog->ic_next;
1269 }
1270 *iclogp = log->l_iclog; /* complete ring */
1271 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1272
1273 return log;
1274} /* xlog_alloc_log */
1275
1276
1277/*
1278 * Write out the commit record of a transaction associated with the given
1279 * ticket. Return the lsn of the commit record.
1280 */
1281STATIC int
1282xlog_commit_record(xfs_mount_t *mp,
1283 xlog_ticket_t *ticket,
1284 xlog_in_core_t **iclog,
1285 xfs_lsn_t *commitlsnp)
1286{
1287 int error;
1288 xfs_log_iovec_t reg[1];
1289
1290 reg[0].i_addr = NULL;
1291 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001292 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 ASSERT_ALWAYS(iclog);
1295 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1296 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001297 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300} /* xlog_commit_record */
1301
1302
1303/*
1304 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1305 * log space. This code pushes on the lsn which would supposedly free up
1306 * the 25% which we want to leave free. We may need to adopt a policy which
1307 * pushes on an lsn which is further along in the log once we reach the high
1308 * water mark. In this manner, we would be creating a low water mark.
1309 */
David Chinnera8272ce2007-11-23 16:28:09 +11001310STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311xlog_grant_push_ail(xfs_mount_t *mp,
1312 int need_bytes)
1313{
1314 xlog_t *log = mp->m_log; /* pointer to the log */
1315 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1316 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1317 int free_blocks; /* free blocks left to write to */
1318 int free_bytes; /* free bytes left to write to */
1319 int threshold_block; /* block in lsn we'd like to be at */
1320 int threshold_cycle; /* lsn cycle we'd like to be at */
1321 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1324
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001325 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 free_bytes = xlog_space_left(log,
1327 log->l_grant_reserve_cycle,
1328 log->l_grant_reserve_bytes);
1329 tail_lsn = log->l_tail_lsn;
1330 free_blocks = BTOBBT(free_bytes);
1331
1332 /*
1333 * Set the threshold for the minimum number of free blocks in the
1334 * log to the maximum of what the caller needs, one quarter of the
1335 * log, and 256 blocks.
1336 */
1337 free_threshold = BTOBB(need_bytes);
1338 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1339 free_threshold = MAX(free_threshold, 256);
1340 if (free_blocks < free_threshold) {
1341 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1342 threshold_cycle = CYCLE_LSN(tail_lsn);
1343 if (threshold_block >= log->l_logBBsize) {
1344 threshold_block -= log->l_logBBsize;
1345 threshold_cycle += 1;
1346 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001347 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /* Don't pass in an lsn greater than the lsn of the last
1350 * log record known to be on disk.
1351 */
1352 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1353 threshold_lsn = log->l_last_sync_lsn;
1354 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001355 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
1358 * Get the transaction layer to kick the dirty buffers out to
1359 * disk asynchronously. No point in trying to do this if
1360 * the filesystem is shutting down.
1361 */
1362 if (threshold_lsn &&
1363 !XLOG_FORCED_SHUTDOWN(log))
1364 xfs_trans_push_ail(mp, threshold_lsn);
1365} /* xlog_grant_push_ail */
1366
1367
1368/*
1369 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1370 * fashion. Previously, we should have moved the current iclog
1371 * ptr in the log to point to the next available iclog. This allows further
1372 * write to continue while this code syncs out an iclog ready to go.
1373 * Before an in-core log can be written out, the data section must be scanned
1374 * to save away the 1st word of each BBSIZE block into the header. We replace
1375 * it with the current cycle count. Each BBSIZE block is tagged with the
1376 * cycle count because there in an implicit assumption that drives will
1377 * guarantee that entire 512 byte blocks get written at once. In other words,
1378 * we can't have part of a 512 byte block written and part not written. By
1379 * tagging each block, we will know which blocks are valid when recovering
1380 * after an unclean shutdown.
1381 *
1382 * This routine is single threaded on the iclog. No other thread can be in
1383 * this routine with the same iclog. Changing contents of iclog can there-
1384 * fore be done without grabbing the state machine lock. Updating the global
1385 * log will require grabbing the lock though.
1386 *
1387 * The entire log manager uses a logical block numbering scheme. Only
1388 * log_sync (and then only bwrite()) know about the fact that the log may
1389 * not start with block zero on a given device. The log block start offset
1390 * is added immediately before calling bwrite().
1391 */
1392
David Chinnera8272ce2007-11-23 16:28:09 +11001393STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394xlog_sync(xlog_t *log,
1395 xlog_in_core_t *iclog)
1396{
1397 xfs_caddr_t dptr; /* pointer to byte sized element */
1398 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001399 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 uint count; /* byte count of bwrite */
1401 uint count_init; /* initial count before roundup */
1402 int roundoff; /* roundoff to BB or stripe */
1403 int split = 0; /* split write into two regions */
1404 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001405 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001408 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* Add for LR header */
1411 count_init = log->l_iclog_hsize + iclog->ic_offset;
1412
1413 /* Round out the log write size */
1414 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1415 /* we have a v2 stripe unit to use */
1416 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1417 } else {
1418 count = BBTOB(BTOBB(count_init));
1419 }
1420 roundoff = count - count_init;
1421 ASSERT(roundoff >= 0);
1422 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1423 roundoff < log->l_mp->m_sb.sb_logsunit)
1424 ||
1425 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1426 roundoff < BBTOB(1)));
1427
1428 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001429 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001430 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001431 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* put cycle number in every block */
1434 xlog_pack_data(log, iclog, roundoff);
1435
1436 /* real byte length */
1437 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001438 iclog->ic_header.h_len =
1439 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001441 iclog->ic_header.h_len =
1442 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Nathan Scottf5faad72006-07-28 17:04:44 +10001445 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1447 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001448 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1451
1452 /* Do we need to split this write into 2 parts? */
1453 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1454 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1455 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1456 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1457 } else {
1458 iclog->ic_bwritecnt = 1;
1459 }
David Chinner511105b2007-05-24 15:21:57 +10001460 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001462 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 XFS_BUF_BUSY(bp);
1464 XFS_BUF_ASYNC(bp);
1465 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001466 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001467 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001469 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001470 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1473 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1474
1475 xlog_verify_iclog(log, iclog, count, B_TRUE);
1476
1477 /* account for log which doesn't start at block #0 */
1478 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1479 /*
1480 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1481 * is shutting down.
1482 */
1483 XFS_BUF_WRITE(bp);
1484
1485 if ((error = XFS_bwrite(bp))) {
1486 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1487 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001488 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001491 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1493 (unsigned long)1);
1494 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1495 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1496 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1497 (__psint_t)count), split);
1498 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001499 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 XFS_BUF_BUSY(bp);
1501 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001502 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1503 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 dptr = XFS_BUF_PTR(bp);
1505 /*
1506 * Bump the cycle numbers at the start of each block
1507 * since this part of the buffer is at the start of
1508 * a new cycle. Watch out for the header magic number
1509 * case, though.
1510 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001511 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001512 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001513 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001514 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dptr += BBSIZE;
1516 }
1517
1518 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1519 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1520
Nathan Scottc41564b2006-03-29 08:55:14 +10001521 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1523 XFS_BUF_WRITE(bp);
1524 if ((error = XFS_bwrite(bp))) {
1525 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1526 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001527 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531} /* xlog_sync */
1532
1533
1534/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001535 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
David Chinnera8272ce2007-11-23 16:28:09 +11001537STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001538xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 xlog_in_core_t *iclog, *next_iclog;
1541 xlog_ticket_t *tic, *next_tic;
1542 int i;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 iclog = log->l_iclog;
1545 for (i=0; i<log->l_iclog_bufs; i++) {
1546 sv_destroy(&iclog->ic_forcesema);
1547 sv_destroy(&iclog->ic_writesema);
1548 xfs_buf_free(iclog->ic_bp);
1549#ifdef XFS_LOG_TRACE
1550 if (iclog->ic_trace != NULL) {
1551 ktrace_free(iclog->ic_trace);
1552 }
1553#endif
1554 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 kmem_free(iclog, sizeof(xlog_in_core_t));
1556 iclog = next_iclog;
1557 }
1558 freesema(&log->l_flushsema);
1559 spinlock_destroy(&log->l_icloglock);
1560 spinlock_destroy(&log->l_grant_lock);
1561
1562 /* XXXsup take a look at this again. */
1563 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1564 !XLOG_FORCED_SHUTDOWN(log)) {
1565 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001566 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 log->l_ticket_cnt, log->l_ticket_tcnt);
1568 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1569
1570 } else {
1571 tic = log->l_unmount_free;
1572 while (tic) {
1573 next_tic = tic->t_next;
Tim Shimmine6a4b372007-11-23 16:30:42 +11001574 kmem_free(tic, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 tic = next_tic;
1576 }
1577 }
1578 xfs_buf_free(log->l_xbuf);
1579#ifdef XFS_LOG_TRACE
1580 if (log->l_trace != NULL) {
1581 ktrace_free(log->l_trace);
1582 }
1583 if (log->l_grant_trace != NULL) {
1584 ktrace_free(log->l_grant_trace);
1585 }
1586#endif
1587 log->l_mp->m_log = NULL;
1588 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001589} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591/*
1592 * Update counters atomically now that memcpy is done.
1593 */
1594/* ARGSUSED */
1595static inline void
1596xlog_state_finish_copy(xlog_t *log,
1597 xlog_in_core_t *iclog,
1598 int record_cnt,
1599 int copy_bytes)
1600{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001601 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001603 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 iclog->ic_offset += copy_bytes;
1605
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001606 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607} /* xlog_state_finish_copy */
1608
1609
1610
1611
1612/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001613 * print out info relating to regions written which consume
1614 * the reservation
1615 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001616STATIC void
1617xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1618{
1619 uint i;
1620 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1621
1622 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1623 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1624 "bformat",
1625 "bchunk",
1626 "efi_format",
1627 "efd_format",
1628 "iformat",
1629 "icore",
1630 "iext",
1631 "ibroot",
1632 "ilocal",
1633 "iattr_ext",
1634 "iattr_broot",
1635 "iattr_local",
1636 "qformat",
1637 "dquot",
1638 "quotaoff",
1639 "LR header",
1640 "unmount",
1641 "commit",
1642 "trans header"
1643 };
1644 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1645 "SETATTR_NOT_SIZE",
1646 "SETATTR_SIZE",
1647 "INACTIVE",
1648 "CREATE",
1649 "CREATE_TRUNC",
1650 "TRUNCATE_FILE",
1651 "REMOVE",
1652 "LINK",
1653 "RENAME",
1654 "MKDIR",
1655 "RMDIR",
1656 "SYMLINK",
1657 "SET_DMATTRS",
1658 "GROWFS",
1659 "STRAT_WRITE",
1660 "DIOSTRAT",
1661 "WRITE_SYNC",
1662 "WRITEID",
1663 "ADDAFORK",
1664 "ATTRINVAL",
1665 "ATRUNCATE",
1666 "ATTR_SET",
1667 "ATTR_RM",
1668 "ATTR_FLAG",
1669 "CLEAR_AGI_BUCKET",
1670 "QM_SBCHANGE",
1671 "DUMMY1",
1672 "DUMMY2",
1673 "QM_QUOTAOFF",
1674 "QM_DQALLOC",
1675 "QM_SETQLIM",
1676 "QM_DQCLUSTER",
1677 "QM_QINOCREATE",
1678 "QM_QUOTAOFF_END",
1679 "SB_UNIT",
1680 "FSYNC_TS",
1681 "GROWFSRT_ALLOC",
1682 "GROWFSRT_ZERO",
1683 "GROWFSRT_FREE",
1684 "SWAPEXT"
1685 };
1686
1687 xfs_fs_cmn_err(CE_WARN, mp,
1688 "xfs_log_write: reservation summary:\n"
1689 " trans type = %s (%u)\n"
1690 " unit res = %d bytes\n"
1691 " current res = %d bytes\n"
1692 " total reg = %u bytes (o/flow = %u bytes)\n"
1693 " ophdrs = %u (ophdr space = %u bytes)\n"
1694 " ophdr + reg = %u bytes\n"
1695 " num regions = %u\n",
1696 ((ticket->t_trans_type <= 0 ||
1697 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1698 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1699 ticket->t_trans_type,
1700 ticket->t_unit_res,
1701 ticket->t_curr_res,
1702 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1703 ticket->t_res_num_ophdrs, ophdr_spc,
1704 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001705 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001706 ticket->t_res_num);
1707
1708 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001709 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001710 cmn_err(CE_WARN,
1711 "region[%u]: %s - %u bytes\n",
1712 i,
1713 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1714 "bad-rtype" : res_type_str[r_type-1]),
1715 ticket->t_res_arr[i].r_len);
1716 }
1717}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001718
1719/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * Write some region out to in-core log
1721 *
1722 * This will be called when writing externally provided regions or when
1723 * writing out a commit record for a given transaction.
1724 *
1725 * General algorithm:
1726 * 1. Find total length of this write. This may include adding to the
1727 * lengths passed in.
1728 * 2. Check whether we violate the tickets reservation.
1729 * 3. While writing to this iclog
1730 * A. Reserve as much space in this iclog as can get
1731 * B. If this is first write, save away start lsn
1732 * C. While writing this region:
1733 * 1. If first write of transaction, write start record
1734 * 2. Write log operation header (header per region)
1735 * 3. Find out if we can fit entire region into this iclog
1736 * 4. Potentially, verify destination memcpy ptr
1737 * 5. Memcpy (partial) region
1738 * 6. If partial copy, release iclog; otherwise, continue
1739 * copying more regions into current iclog
1740 * 4. Mark want sync bit (in simulation mode)
1741 * 5. Release iclog for potential flush to on-disk log.
1742 *
1743 * ERRORS:
1744 * 1. Panic if reservation is overrun. This should never happen since
1745 * reservation amounts are generated internal to the filesystem.
1746 * NOTES:
1747 * 1. Tickets are single threaded data structures.
1748 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1749 * syncing routine. When a single log_write region needs to span
1750 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1751 * on all log operation writes which don't contain the end of the
1752 * region. The XLOG_END_TRANS bit is used for the in-core log
1753 * operation which contains the end of the continued log_write region.
1754 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1755 * we don't really know exactly how much space will be used. As a result,
1756 * we don't update ic_offset until the end when we know exactly how many
1757 * bytes have been written out.
1758 */
David Chinnera8272ce2007-11-23 16:28:09 +11001759STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760xlog_write(xfs_mount_t * mp,
1761 xfs_log_iovec_t reg[],
1762 int nentries,
1763 xfs_log_ticket_t tic,
1764 xfs_lsn_t *start_lsn,
1765 xlog_in_core_t **commit_iclog,
1766 uint flags)
1767{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001768 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001770 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 __psint_t ptr; /* copy address into data region */
1773 int len; /* # xlog_write() bytes 2 still copy */
1774 int index; /* region index currently copying */
1775 int log_offset; /* offset (from 0) into data region */
1776 int start_rec_copy; /* # bytes to copy for start record */
1777 int partial_copy; /* did we split a region? */
1778 int partial_copy_len;/* # bytes copied if split region */
1779 int need_copy; /* # bytes need to memcpy this region */
1780 int copy_len; /* # bytes actually memcpy'ing */
1781 int copy_off; /* # bytes from entry start */
1782 int contwr; /* continued write of in-core log? */
1783 int error;
1784 int record_cnt = 0, data_cnt = 0;
1785
1786 partial_copy_len = partial_copy = 0;
1787
1788 /* Calculate potential maximum space. Each region gets its own
1789 * xlog_op_header_t and may need to be double word aligned.
1790 */
1791 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001792 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001794 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 for (index = 0; index < nentries; index++) {
1798 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001799 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001801 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803 contwr = *start_lsn = 0;
1804
1805 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001806 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#ifdef DEBUG
1808 xlog_panic(
1809 "xfs_log_write: reservation ran out. Need to up reservation");
1810#else
1811 /* Customer configurable panic */
1812 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1813 "xfs_log_write: reservation ran out. Need to up reservation");
1814 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001815 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816#endif
1817 } else
1818 ticket->t_curr_res -= len;
1819
1820 for (index = 0; index < nentries; ) {
1821 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1822 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001823 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 ASSERT(log_offset <= iclog->ic_size - 1);
1826 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1827
1828 /* start_lsn is the first lsn written to. That's all we need. */
1829 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001830 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 /* This loop writes out as many regions as can fit in the amount
1833 * of space which was allocated by xlog_state_get_iclog_space().
1834 */
1835 while (index < nentries) {
1836 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1837 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1838 start_rec_copy = 0;
1839
1840 /* If first write for transaction, insert start record.
1841 * We can't be trying to commit if we are inited. We can't
1842 * have any "partial_copy" if we are inited.
1843 */
1844 if (ticket->t_flags & XLOG_TIC_INITED) {
1845 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001846 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 logop_head->oh_clientid = ticket->t_clientid;
1848 logop_head->oh_len = 0;
1849 logop_head->oh_flags = XLOG_START_TRANS;
1850 logop_head->oh_res2 = 0;
1851 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1852 record_cnt++;
1853
1854 start_rec_copy = sizeof(xlog_op_header_t);
1855 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1856 }
1857
1858 /* Copy log operation header directly into data section */
1859 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001860 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 logop_head->oh_clientid = ticket->t_clientid;
1862 logop_head->oh_res2 = 0;
1863
1864 /* header copied directly */
1865 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1866
1867 /* are we copying a commit or unmount record? */
1868 logop_head->oh_flags = flags;
1869
1870 /*
1871 * We've seen logs corrupted with bad transaction client
1872 * ids. This makes sure that XFS doesn't generate them on.
1873 * Turn this into an EIO and shut down the filesystem.
1874 */
1875 switch (logop_head->oh_clientid) {
1876 case XFS_TRANSACTION:
1877 case XFS_VOLUME:
1878 case XFS_LOG:
1879 break;
1880 default:
1881 xfs_fs_cmn_err(CE_WARN, mp,
1882 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1883 logop_head->oh_clientid, tic);
1884 return XFS_ERROR(EIO);
1885 }
1886
1887 /* Partial write last time? => (partial_copy != 0)
1888 * need_copy is the amount we'd like to copy if everything could
1889 * fit in the current memcpy.
1890 */
1891 need_copy = reg[index].i_len - partial_copy_len;
1892
1893 copy_off = partial_copy_len;
1894 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001895 copy_len = need_copy;
1896 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (partial_copy)
1898 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1899 partial_copy_len = partial_copy = 0;
1900 } else { /* partial write */
1901 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001902 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1904 if (partial_copy)
1905 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1906 partial_copy_len += copy_len;
1907 partial_copy++;
1908 len += sizeof(xlog_op_header_t); /* from splitting of region */
1909 /* account for new log op header */
1910 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001911 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 xlog_verify_dest_ptr(log, ptr);
1914
1915 /* copy region */
1916 ASSERT(copy_len >= 0);
1917 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1918 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1919
1920 /* make copy_len total bytes copied, including headers */
1921 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1922 record_cnt++;
1923 data_cnt += contwr ? copy_len : 0;
1924 if (partial_copy) { /* copied partial region */
1925 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1926 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1927 record_cnt = data_cnt = 0;
1928 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001929 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break; /* don't increment index */
1931 } else { /* copied entire region */
1932 index++;
1933 partial_copy_len = partial_copy = 0;
1934
1935 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1936 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1937 record_cnt = data_cnt = 0;
1938 xlog_state_want_sync(log, iclog);
1939 if (commit_iclog) {
1940 ASSERT(flags & XLOG_COMMIT_TRANS);
1941 *commit_iclog = iclog;
1942 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (index == nentries)
1945 return 0; /* we are done */
1946 else
1947 break;
1948 }
1949 } /* if (partial_copy) */
1950 } /* while (index < nentries) */
1951 } /* for (index = 0; index < nentries; ) */
1952 ASSERT(len == 0);
1953
1954 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1955 if (commit_iclog) {
1956 ASSERT(flags & XLOG_COMMIT_TRANS);
1957 *commit_iclog = iclog;
1958 return 0;
1959 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001960 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961} /* xlog_write */
1962
1963
1964/*****************************************************************************
1965 *
1966 * State Machine functions
1967 *
1968 *****************************************************************************
1969 */
1970
1971/* Clean iclogs starting from the head. This ordering must be
1972 * maintained, so an iclog doesn't become ACTIVE beyond one that
1973 * is SYNCING. This is also required to maintain the notion that we use
1974 * a counting semaphore to hold off would be writers to the log when every
1975 * iclog is trying to sync to disk.
1976 *
1977 * State Change: DIRTY -> ACTIVE
1978 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001979STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980xlog_state_clean_log(xlog_t *log)
1981{
1982 xlog_in_core_t *iclog;
1983 int changed = 0;
1984
1985 iclog = log->l_iclog;
1986 do {
1987 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1988 iclog->ic_state = XLOG_STATE_ACTIVE;
1989 iclog->ic_offset = 0;
1990 iclog->ic_callback = NULL; /* don't need to free */
1991 /*
1992 * If the number of ops in this iclog indicate it just
1993 * contains the dummy transaction, we can
1994 * change state into IDLE (the second time around).
1995 * Otherwise we should change the state into
1996 * NEED a dummy.
1997 * We don't need to cover the dummy.
1998 */
1999 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002000 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2001 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 changed = 1;
2003 } else {
2004 /*
2005 * We have two dirty iclogs so start over
2006 * This could also be num of ops indicates
2007 * this is not the dummy going out.
2008 */
2009 changed = 2;
2010 }
2011 iclog->ic_header.h_num_logops = 0;
2012 memset(iclog->ic_header.h_cycle_data, 0,
2013 sizeof(iclog->ic_header.h_cycle_data));
2014 iclog->ic_header.h_lsn = 0;
2015 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2016 /* do nothing */;
2017 else
2018 break; /* stop cleaning */
2019 iclog = iclog->ic_next;
2020 } while (iclog != log->l_iclog);
2021
2022 /* log is locked when we are called */
2023 /*
2024 * Change state for the dummy log recording.
2025 * We usually go to NEED. But we go to NEED2 if the changed indicates
2026 * we are done writing the dummy record.
2027 * If we are done with the second dummy recored (DONE2), then
2028 * we go to IDLE.
2029 */
2030 if (changed) {
2031 switch (log->l_covered_state) {
2032 case XLOG_STATE_COVER_IDLE:
2033 case XLOG_STATE_COVER_NEED:
2034 case XLOG_STATE_COVER_NEED2:
2035 log->l_covered_state = XLOG_STATE_COVER_NEED;
2036 break;
2037
2038 case XLOG_STATE_COVER_DONE:
2039 if (changed == 1)
2040 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2041 else
2042 log->l_covered_state = XLOG_STATE_COVER_NEED;
2043 break;
2044
2045 case XLOG_STATE_COVER_DONE2:
2046 if (changed == 1)
2047 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2048 else
2049 log->l_covered_state = XLOG_STATE_COVER_NEED;
2050 break;
2051
2052 default:
2053 ASSERT(0);
2054 }
2055 }
2056} /* xlog_state_clean_log */
2057
2058STATIC xfs_lsn_t
2059xlog_get_lowest_lsn(
2060 xlog_t *log)
2061{
2062 xlog_in_core_t *lsn_log;
2063 xfs_lsn_t lowest_lsn, lsn;
2064
2065 lsn_log = log->l_iclog;
2066 lowest_lsn = 0;
2067 do {
2068 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002069 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if ((lsn && !lowest_lsn) ||
2071 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2072 lowest_lsn = lsn;
2073 }
2074 }
2075 lsn_log = lsn_log->ic_next;
2076 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002077 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080
2081STATIC void
2082xlog_state_do_callback(
2083 xlog_t *log,
2084 int aborted,
2085 xlog_in_core_t *ciclog)
2086{
2087 xlog_in_core_t *iclog;
2088 xlog_in_core_t *first_iclog; /* used to know when we've
2089 * processed all iclogs once */
2090 xfs_log_callback_t *cb, *cb_next;
2091 int flushcnt = 0;
2092 xfs_lsn_t lowest_lsn;
2093 int ioerrors; /* counter: iclogs with errors */
2094 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2095 int funcdidcallbacks; /* flag: function did callbacks */
2096 int repeats; /* for issuing console warnings if
2097 * looping too many times */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002099 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 first_iclog = iclog = log->l_iclog;
2101 ioerrors = 0;
2102 funcdidcallbacks = 0;
2103 repeats = 0;
2104
2105 do {
2106 /*
2107 * Scan all iclogs starting with the one pointed to by the
2108 * log. Reset this starting point each time the log is
2109 * unlocked (during callbacks).
2110 *
2111 * Keep looping through iclogs until one full pass is made
2112 * without running any callbacks.
2113 */
2114 first_iclog = log->l_iclog;
2115 iclog = log->l_iclog;
2116 loopdidcallbacks = 0;
2117 repeats++;
2118
2119 do {
2120
2121 /* skip all iclogs in the ACTIVE & DIRTY states */
2122 if (iclog->ic_state &
2123 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2124 iclog = iclog->ic_next;
2125 continue;
2126 }
2127
2128 /*
2129 * Between marking a filesystem SHUTDOWN and stopping
2130 * the log, we do flush all iclogs to disk (if there
2131 * wasn't a log I/O error). So, we do want things to
2132 * go smoothly in case of just a SHUTDOWN w/o a
2133 * LOG_IO_ERROR.
2134 */
2135 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2136 /*
2137 * Can only perform callbacks in order. Since
2138 * this iclog is not in the DONE_SYNC/
2139 * DO_CALLBACK state, we skip the rest and
2140 * just try to clean up. If we set our iclog
2141 * to DO_CALLBACK, we will not process it when
2142 * we retry since a previous iclog is in the
2143 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002144 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 */
2146 if (!(iclog->ic_state &
2147 (XLOG_STATE_DONE_SYNC |
2148 XLOG_STATE_DO_CALLBACK))) {
2149 if (ciclog && (ciclog->ic_state ==
2150 XLOG_STATE_DONE_SYNC)) {
2151 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2152 }
2153 break;
2154 }
2155 /*
2156 * We now have an iclog that is in either the
2157 * DO_CALLBACK or DONE_SYNC states. The other
2158 * states (WANT_SYNC, SYNCING, or CALLBACK were
2159 * caught by the above if and are going to
2160 * clean (i.e. we aren't doing their callbacks)
2161 * see the above if.
2162 */
2163
2164 /*
2165 * We will do one more check here to see if we
2166 * have chased our tail around.
2167 */
2168
2169 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002170 if (lowest_lsn &&
2171 XFS_LSN_CMP(lowest_lsn,
2172 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 iclog = iclog->ic_next;
2174 continue; /* Leave this iclog for
2175 * another thread */
2176 }
2177
2178 iclog->ic_state = XLOG_STATE_CALLBACK;
2179
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002180 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002183 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 * No one else can be here except us.
2185 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002186 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002187 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2188 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2189 log->l_last_sync_lsn =
2190 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002191 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /*
2194 * Keep processing entries in the callback list
2195 * until we come around and it is empty. We
2196 * need to atomically see that the list is
2197 * empty and change the state to DIRTY so that
2198 * we don't miss any more callbacks being added.
2199 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002200 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 } else {
2202 ioerrors++;
2203 }
2204 cb = iclog->ic_callback;
2205
Christoph Hellwig4b809162007-08-16 15:37:36 +10002206 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 iclog->ic_callback_tail = &(iclog->ic_callback);
2208 iclog->ic_callback = NULL;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002209 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002212 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 cb_next = cb->cb_next;
2214 cb->cb_func(cb->cb_arg, aborted);
2215 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002216 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 cb = iclog->ic_callback;
2218 }
2219
2220 loopdidcallbacks++;
2221 funcdidcallbacks++;
2222
Christoph Hellwig4b809162007-08-16 15:37:36 +10002223 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2225 iclog->ic_state = XLOG_STATE_DIRTY;
2226
2227 /*
2228 * Transition from DIRTY to ACTIVE if applicable.
2229 * NOP if STATE_IOERROR.
2230 */
2231 xlog_state_clean_log(log);
2232
2233 /* wake up threads waiting in xfs_log_force() */
2234 sv_broadcast(&iclog->ic_forcesema);
2235
2236 iclog = iclog->ic_next;
2237 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002238
2239 if (repeats > 5000) {
2240 flushcnt += repeats;
2241 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002243 "%s: possible infinite loop (%d iterations)",
2244 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 } while (!ioerrors && loopdidcallbacks);
2247
2248 /*
2249 * make one last gasp attempt to see if iclogs are being left in
2250 * limbo..
2251 */
2252#ifdef DEBUG
2253 if (funcdidcallbacks) {
2254 first_iclog = iclog = log->l_iclog;
2255 do {
2256 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2257 /*
2258 * Terminate the loop if iclogs are found in states
2259 * which will cause other threads to clean up iclogs.
2260 *
2261 * SYNCING - i/o completion will go through logs
2262 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002263 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 * IOERROR - give up hope all ye who enter here
2265 */
2266 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2267 iclog->ic_state == XLOG_STATE_SYNCING ||
2268 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2269 iclog->ic_state == XLOG_STATE_IOERROR )
2270 break;
2271 iclog = iclog->ic_next;
2272 } while (first_iclog != iclog);
2273 }
2274#endif
2275
Nathan Scotta3c66852006-09-28 11:02:14 +10002276 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2278 flushcnt = log->l_flushcnt;
2279 log->l_flushcnt = 0;
2280 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002281 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 while (flushcnt--)
2283 vsema(&log->l_flushsema);
2284} /* xlog_state_do_callback */
2285
2286
2287/*
2288 * Finish transitioning this iclog to the dirty state.
2289 *
2290 * Make sure that we completely execute this routine only when this is
2291 * the last call to the iclog. There is a good chance that iclog flushes,
2292 * when we reach the end of the physical log, get turned into 2 separate
2293 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2294 * routine. By using the reference count bwritecnt, we guarantee that only
2295 * the second completion goes through.
2296 *
2297 * Callbacks could take time, so they are done outside the scope of the
2298 * global state machine log lock. Assume that the calls to cvsema won't
2299 * take a long time. At least we know it won't sleep.
2300 */
David Chinnera8272ce2007-11-23 16:28:09 +11002301STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302xlog_state_done_syncing(
2303 xlog_in_core_t *iclog,
2304 int aborted)
2305{
2306 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002308 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2311 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002312 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2314
2315
2316 /*
2317 * If we got an error, either on the first buffer, or in the case of
2318 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2319 * and none should ever be attempted to be written to disk
2320 * again.
2321 */
2322 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2323 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002324 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return;
2326 }
2327 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2328 }
2329
2330 /*
2331 * Someone could be sleeping prior to writing out the next
2332 * iclog buffer, we wake them all, one will get to do the
2333 * I/O, the others get to wait for the result.
2334 */
2335 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002336 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2338} /* xlog_state_done_syncing */
2339
2340
2341/*
2342 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2343 * sleep. The flush semaphore is set to the number of in-core buffers and
2344 * decremented around disk syncing. Therefore, if all buffers are syncing,
2345 * this semaphore will cause new writes to sleep until a sync completes.
2346 * Otherwise, this code just does p() followed by v(). This approximates
2347 * a sleep/wakeup except we can't race.
2348 *
2349 * The in-core logs are used in a circular fashion. They are not used
2350 * out-of-order even when an iclog past the head is free.
2351 *
2352 * return:
2353 * * log_offset where xlog_write() can start writing into the in-core
2354 * log's data space.
2355 * * in-core log pointer to which xlog_write() should write.
2356 * * boolean indicating this is a continued write to an in-core log.
2357 * If this is the last write, then the in-core log's offset field
2358 * needs to be incremented, depending on the amount of data which
2359 * is copied.
2360 */
David Chinnera8272ce2007-11-23 16:28:09 +11002361STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362xlog_state_get_iclog_space(xlog_t *log,
2363 int len,
2364 xlog_in_core_t **iclogp,
2365 xlog_ticket_t *ticket,
2366 int *continued_write,
2367 int *logoffsetp)
2368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 int log_offset;
2370 xlog_rec_header_t *head;
2371 xlog_in_core_t *iclog;
2372 int error;
2373
2374restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002375 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002377 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return XFS_ERROR(EIO);
2379 }
2380
2381 iclog = log->l_iclog;
2382 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2383 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002384 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2386 XFS_STATS_INC(xs_log_noiclogs);
2387 /* Ensure that log writes happen */
2388 psema(&log->l_flushsema, PINOD);
2389 goto restart;
2390 }
2391 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2392 head = &iclog->ic_header;
2393
David Chinner155cc6b2008-03-06 13:44:14 +11002394 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 log_offset = iclog->ic_offset;
2396
2397 /* On the 1st write to an iclog, figure out lsn. This works
2398 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2399 * committing to. If the offset is set, that's how many blocks
2400 * must be written.
2401 */
2402 if (log_offset == 0) {
2403 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002404 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002405 log->l_iclog_hsize,
2406 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002407 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2408 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002409 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 ASSERT(log->l_curr_block >= 0);
2411 }
2412
2413 /* If there is enough room to write everything, then do it. Otherwise,
2414 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2415 * bit is on, so this will get flushed out. Don't update ic_offset
2416 * until you know exactly how many bytes get copied. Therefore, wait
2417 * until later to update ic_offset.
2418 *
2419 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2420 * can fit into remaining data section.
2421 */
2422 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2423 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2424
2425 /* If I'm the only one writing to this iclog, sync it to disk */
David Chinner155cc6b2008-03-06 13:44:14 +11002426 if (atomic_read(&iclog->ic_refcnt) == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002427 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002431 atomic_dec(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002432 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 goto restart;
2435 }
2436
2437 /* Do we have enough room to write the full amount in the remainder
2438 * of this iclog? Or must we continue a write on the next iclog and
2439 * mark this iclog as completely taken? In the case where we switch
2440 * iclogs (to mark it taken), this particular iclog will release/sync
2441 * to disk in xlog_write().
2442 */
2443 if (len <= iclog->ic_size - iclog->ic_offset) {
2444 *continued_write = 0;
2445 iclog->ic_offset += len;
2446 } else {
2447 *continued_write = 1;
2448 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2449 }
2450 *iclogp = iclog;
2451
2452 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002453 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 *logoffsetp = log_offset;
2456 return 0;
2457} /* xlog_state_get_iclog_space */
2458
2459/*
2460 * Atomically get the log space required for a log ticket.
2461 *
2462 * Once a ticket gets put onto the reserveq, it will only return after
2463 * the needed reservation is satisfied.
2464 */
2465STATIC int
2466xlog_grant_log_space(xlog_t *log,
2467 xlog_ticket_t *tic)
2468{
2469 int free_bytes;
2470 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471#ifdef DEBUG
2472 xfs_lsn_t tail_lsn;
2473#endif
2474
2475
2476#ifdef DEBUG
2477 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2478 panic("grant Recovery problem");
2479#endif
2480
2481 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002482 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2484
2485 /* something is already sleeping; insert new transaction at end */
2486 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002487 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 xlog_trace_loggrant(log, tic,
2489 "xlog_grant_log_space: sleep 1");
2490 /*
2491 * Gotta check this before going to sleep, while we're
2492 * holding the grant lock.
2493 */
2494 if (XLOG_FORCED_SHUTDOWN(log))
2495 goto error_return;
2496
2497 XFS_STATS_INC(xs_sleep_logspace);
2498 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2499 /*
2500 * If we got an error, and the filesystem is shutting down,
2501 * we'll catch it down below. So just continue...
2502 */
2503 xlog_trace_loggrant(log, tic,
2504 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002505 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2508 need_bytes = tic->t_unit_res*tic->t_ocnt;
2509 else
2510 need_bytes = tic->t_unit_res;
2511
2512redo:
2513 if (XLOG_FORCED_SHUTDOWN(log))
2514 goto error_return;
2515
2516 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2517 log->l_grant_reserve_bytes);
2518 if (free_bytes < need_bytes) {
2519 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002520 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 xlog_trace_loggrant(log, tic,
2522 "xlog_grant_log_space: sleep 2");
2523 XFS_STATS_INC(xs_sleep_logspace);
2524 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2525
2526 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002527 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 goto error_return;
2529 }
2530
2531 xlog_trace_loggrant(log, tic,
2532 "xlog_grant_log_space: wake 2");
2533 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002534 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 goto redo;
2536 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002537 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002540 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541#ifdef DEBUG
2542 tail_lsn = log->l_tail_lsn;
2543 /*
2544 * Check to make sure the grant write head didn't just over lap the
2545 * tail. If the cycles are the same, we can't be overlapping.
2546 * Otherwise, make sure that the cycles differ by exactly one and
2547 * check the byte count.
2548 */
2549 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2550 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2551 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2552 }
2553#endif
2554 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2555 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002556 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return 0;
2558
2559 error_return:
2560 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002561 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2563 /*
2564 * If we are failing, make sure the ticket doesn't have any
2565 * current reservations. We don't want to add this back when
2566 * the ticket/transaction gets cancelled.
2567 */
2568 tic->t_curr_res = 0;
2569 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002570 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return XFS_ERROR(EIO);
2572} /* xlog_grant_log_space */
2573
2574
2575/*
2576 * Replenish the byte reservation required by moving the grant write head.
2577 *
2578 *
2579 */
2580STATIC int
2581xlog_regrant_write_log_space(xlog_t *log,
2582 xlog_ticket_t *tic)
2583{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 int free_bytes, need_bytes;
2585 xlog_ticket_t *ntic;
2586#ifdef DEBUG
2587 xfs_lsn_t tail_lsn;
2588#endif
2589
2590 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002591 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596#ifdef DEBUG
2597 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2598 panic("regrant Recovery problem");
2599#endif
2600
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002601 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2603
2604 if (XLOG_FORCED_SHUTDOWN(log))
2605 goto error_return;
2606
2607 /* If there are other waiters on the queue then give them a
2608 * chance at logspace before us. Wake up the first waiters,
2609 * if we do not wake up all the waiters then go to sleep waiting
2610 * for more free space, otherwise try to get some space for
2611 * this transaction.
2612 */
2613
2614 if ((ntic = log->l_write_headq)) {
2615 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2616 log->l_grant_write_bytes);
2617 do {
2618 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2619
2620 if (free_bytes < ntic->t_unit_res)
2621 break;
2622 free_bytes -= ntic->t_unit_res;
2623 sv_signal(&ntic->t_sema);
2624 ntic = ntic->t_next;
2625 } while (ntic != log->l_write_headq);
2626
2627 if (ntic != log->l_write_headq) {
2628 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002629 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 xlog_trace_loggrant(log, tic,
2632 "xlog_regrant_write_log_space: sleep 1");
2633 XFS_STATS_INC(xs_sleep_logspace);
2634 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2635 &log->l_grant_lock, s);
2636
2637 /* If we're shutting down, this tic is already
2638 * off the queue */
2639 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002640 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 goto error_return;
2642 }
2643
2644 xlog_trace_loggrant(log, tic,
2645 "xlog_regrant_write_log_space: wake 1");
2646 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002647 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 }
2650
2651 need_bytes = tic->t_unit_res;
2652
2653redo:
2654 if (XLOG_FORCED_SHUTDOWN(log))
2655 goto error_return;
2656
2657 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2658 log->l_grant_write_bytes);
2659 if (free_bytes < need_bytes) {
2660 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002661 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 XFS_STATS_INC(xs_sleep_logspace);
2663 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2664
2665 /* If we're shutting down, this tic is already off the queue */
2666 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002667 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 goto error_return;
2669 }
2670
2671 xlog_trace_loggrant(log, tic,
2672 "xlog_regrant_write_log_space: wake 2");
2673 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002674 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 goto redo;
2676 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002677 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002679 /* we've got enough space */
2680 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681#ifdef DEBUG
2682 tail_lsn = log->l_tail_lsn;
2683 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2684 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2685 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2686 }
2687#endif
2688
2689 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2690 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002691 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694
2695 error_return:
2696 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002697 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2699 /*
2700 * If we are failing, make sure the ticket doesn't have any
2701 * current reservations. We don't want to add this back when
2702 * the ticket/transaction gets cancelled.
2703 */
2704 tic->t_curr_res = 0;
2705 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002706 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return XFS_ERROR(EIO);
2708} /* xlog_regrant_write_log_space */
2709
2710
2711/* The first cnt-1 times through here we don't need to
2712 * move the grant write head because the permanent
2713 * reservation has reserved cnt times the unit amount.
2714 * Release part of current permanent unit reservation and
2715 * reset current reservation to be one units worth. Also
2716 * move grant reservation head forward.
2717 */
2718STATIC void
2719xlog_regrant_reserve_log_space(xlog_t *log,
2720 xlog_ticket_t *ticket)
2721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 xlog_trace_loggrant(log, ticket,
2723 "xlog_regrant_reserve_log_space: enter");
2724 if (ticket->t_cnt > 0)
2725 ticket->t_cnt--;
2726
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002727 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002728 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002730 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 xlog_trace_loggrant(log, ticket,
2732 "xlog_regrant_reserve_log_space: sub current res");
2733 xlog_verify_grant_head(log, 1);
2734
2735 /* just return if we still have some of the pre-reserved space */
2736 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002737 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return;
2739 }
2740
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002741 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 xlog_trace_loggrant(log, ticket,
2743 "xlog_regrant_reserve_log_space: exit");
2744 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002745 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002747 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748} /* xlog_regrant_reserve_log_space */
2749
2750
2751/*
2752 * Give back the space left from a reservation.
2753 *
2754 * All the information we need to make a correct determination of space left
2755 * is present. For non-permanent reservations, things are quite easy. The
2756 * count should have been decremented to zero. We only need to deal with the
2757 * space remaining in the current reservation part of the ticket. If the
2758 * ticket contains a permanent reservation, there may be left over space which
2759 * needs to be released. A count of N means that N-1 refills of the current
2760 * reservation can be done before we need to ask for more space. The first
2761 * one goes to fill up the first current reservation. Once we run out of
2762 * space, the count will stay at zero and the only space remaining will be
2763 * in the current reservation field.
2764 */
2765STATIC void
2766xlog_ungrant_log_space(xlog_t *log,
2767 xlog_ticket_t *ticket)
2768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (ticket->t_cnt > 0)
2770 ticket->t_cnt--;
2771
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002772 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2774
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002775 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2778
2779 /* If this is a permanent reservation ticket, we may be able to free
2780 * up more space based on the remaining count.
2781 */
2782 if (ticket->t_cnt > 0) {
2783 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002784 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786
2787 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2788 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002789 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 xfs_log_move_tail(log->l_mp, 1);
2791} /* xlog_ungrant_log_space */
2792
2793
2794/*
2795 * Atomically put back used ticket.
2796 */
David Chinnera8272ce2007-11-23 16:28:09 +11002797STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798xlog_state_put_ticket(xlog_t *log,
2799 xlog_ticket_t *tic)
2800{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002801 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 xlog_ticket_put(log, tic);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002803 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804} /* xlog_state_put_ticket */
2805
2806/*
2807 * Flush iclog to disk if this is the last reference to the given iclog and
2808 * the WANT_SYNC bit is set.
2809 *
2810 * When this function is entered, the iclog is not necessarily in the
2811 * WANT_SYNC state. It may be sitting around waiting to get filled.
2812 *
2813 *
2814 */
David Chinnera8272ce2007-11-23 16:28:09 +11002815STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002816xlog_state_release_iclog(
2817 xlog_t *log,
2818 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 int sync = 0; /* do we sync? */
2821
David Chinner155cc6b2008-03-06 13:44:14 +11002822 if (iclog->ic_state & XLOG_STATE_IOERROR)
2823 return XFS_ERROR(EIO);
2824
2825 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2826 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2827 return 0;
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002830 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return XFS_ERROR(EIO);
2832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2834 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2835
David Chinner155cc6b2008-03-06 13:44:14 +11002836 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002837 /* update tail before writing to iclog */
2838 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 sync++;
2840 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002841 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2843 /* cycle incremented when incrementing curr_block */
2844 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002845 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 /*
2848 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002849 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2851 * this iclog has consistent data, so we ignore IOERROR
2852 * flags after this point.
2853 */
David Chinnerb5893342008-03-06 13:44:06 +11002854 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857} /* xlog_state_release_iclog */
2858
2859
2860/*
2861 * This routine will mark the current iclog in the ring as WANT_SYNC
2862 * and move the current iclog pointer to the next iclog in the ring.
2863 * When this routine is called from xlog_state_get_iclog_space(), the
2864 * exact size of the iclog has not yet been determined. All we know is
2865 * that every data block. We have run out of space in this log record.
2866 */
2867STATIC void
2868xlog_state_switch_iclogs(xlog_t *log,
2869 xlog_in_core_t *iclog,
2870 int eventual_size)
2871{
2872 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2873 if (!eventual_size)
2874 eventual_size = iclog->ic_offset;
2875 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002876 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 log->l_prev_block = log->l_curr_block;
2878 log->l_prev_cycle = log->l_curr_cycle;
2879
2880 /* roll log?: ic_offset changed later */
2881 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2882
2883 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002884 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 log->l_mp->m_sb.sb_logsunit > 1) {
2886 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2887 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2888 }
2889
2890 if (log->l_curr_block >= log->l_logBBsize) {
2891 log->l_curr_cycle++;
2892 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2893 log->l_curr_cycle++;
2894 log->l_curr_block -= log->l_logBBsize;
2895 ASSERT(log->l_curr_block >= 0);
2896 }
2897 ASSERT(iclog == log->l_iclog);
2898 log->l_iclog = iclog->ic_next;
2899} /* xlog_state_switch_iclogs */
2900
2901
2902/*
2903 * Write out all data in the in-core log as of this exact moment in time.
2904 *
2905 * Data may be written to the in-core log during this call. However,
2906 * we don't guarantee this data will be written out. A change from past
2907 * implementation means this routine will *not* write out zero length LRs.
2908 *
2909 * Basically, we try and perform an intelligent scan of the in-core logs.
2910 * If we determine there is no flushable data, we just return. There is no
2911 * flushable data if:
2912 *
2913 * 1. the current iclog is active and has no data; the previous iclog
2914 * is in the active or dirty state.
2915 * 2. the current iclog is drity, and the previous iclog is in the
2916 * active or dirty state.
2917 *
2918 * We may sleep (call psema) if:
2919 *
2920 * 1. the current iclog is not in the active nor dirty state.
2921 * 2. the current iclog dirty, and the previous iclog is not in the
2922 * active nor dirty state.
2923 * 3. the current iclog is active, and there is another thread writing
2924 * to this particular iclog.
2925 * 4. a) the current iclog is active and has no other writers
2926 * b) when we return from flushing out this iclog, it is still
2927 * not in the active nor dirty state.
2928 */
2929STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002930xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 xlog_in_core_t *iclog;
2933 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002935 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 iclog = log->l_iclog;
2938 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002939 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return XFS_ERROR(EIO);
2941 }
2942
2943 /* If the head iclog is not active nor dirty, we just attach
2944 * ourselves to the head and go to sleep.
2945 */
2946 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2947 iclog->ic_state == XLOG_STATE_DIRTY) {
2948 /*
2949 * If the head is dirty or (active and empty), then
2950 * we need to look at the previous iclog. If the previous
2951 * iclog is active or dirty we are done. There is nothing
2952 * to sync out. Otherwise, we attach ourselves to the
2953 * previous iclog and go to sleep.
2954 */
2955 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002956 (atomic_read(&iclog->ic_refcnt) == 0
2957 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 iclog = iclog->ic_prev;
2959 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2960 iclog->ic_state == XLOG_STATE_DIRTY)
2961 goto no_sleep;
2962 else
2963 goto maybe_sleep;
2964 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002965 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 /* We are the only one with access to this
2967 * iclog. Flush it out now. There should
2968 * be a roundoff of zero to show that someone
2969 * has already taken care of the roundoff from
2970 * the previous sync.
2971 */
David Chinner155cc6b2008-03-06 13:44:14 +11002972 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002973 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002975 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 if (xlog_state_release_iclog(log, iclog))
2978 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002979 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002980 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002981 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 iclog->ic_state != XLOG_STATE_DIRTY)
2983 goto maybe_sleep;
2984 else
2985 goto no_sleep;
2986 } else {
2987 /* Someone else is writing to this iclog.
2988 * Use its call to flush out the data. However,
2989 * the other thread may not force out this LR,
2990 * so we mark it WANT_SYNC.
2991 */
2992 xlog_state_switch_iclogs(log, iclog, 0);
2993 goto maybe_sleep;
2994 }
2995 }
2996 }
2997
2998 /* By the time we come around again, the iclog could've been filled
2999 * which would give it another lsn. If we have a new lsn, just
3000 * return because the relevant data has been flushed.
3001 */
3002maybe_sleep:
3003 if (flags & XFS_LOG_SYNC) {
3004 /*
3005 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003006 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 * Then we check again after waking up, in case our
3008 * sleep was disturbed by a bad news.
3009 */
3010 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003011 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return XFS_ERROR(EIO);
3013 }
3014 XFS_STATS_INC(xs_log_force_sleep);
3015 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3016 /*
3017 * No need to grab the log lock here since we're
3018 * only deciding whether or not to return EIO
3019 * and the memory read should be atomic.
3020 */
3021 if (iclog->ic_state & XLOG_STATE_IOERROR)
3022 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003023 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025 } else {
3026
3027no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003028 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030 return 0;
3031} /* xlog_state_sync_all */
3032
3033
3034/*
3035 * Used by code which implements synchronous log forces.
3036 *
3037 * Find in-core log with lsn.
3038 * If it is in the DIRTY state, just return.
3039 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3040 * state and go to sleep or return.
3041 * If it is in any other state, go to sleep or return.
3042 *
3043 * If filesystem activity goes to zero, the iclog will get flushed only by
3044 * bdflush().
3045 */
David Chinnera8272ce2007-11-23 16:28:09 +11003046STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047xlog_state_sync(xlog_t *log,
3048 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003049 uint flags,
3050 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
3052 xlog_in_core_t *iclog;
3053 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003056 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 iclog = log->l_iclog;
3058
3059 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003060 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return XFS_ERROR(EIO);
3062 }
3063
3064 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003065 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3066 iclog = iclog->ic_next;
3067 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069
3070 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003071 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return 0;
3073 }
3074
3075 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3076 /*
3077 * We sleep here if we haven't already slept (e.g.
3078 * this is the first time we've looked at the correct
3079 * iclog buf) and the buffer before us is going to
3080 * be sync'ed. The reason for this is that if we
3081 * are doing sync transactions here, by waiting for
3082 * the previous I/O to complete, we can allow a few
3083 * more transactions into this iclog before we close
3084 * it down.
3085 *
3086 * Otherwise, we mark the buffer WANT_SYNC, and bump
3087 * up the refcnt so we can release the log (which drops
3088 * the ref count). The state switch keeps new transaction
3089 * commits from using this buffer. When the current commits
3090 * finish writing into the buffer, the refcount will drop to
3091 * zero and the buffer will go out then.
3092 */
3093 if (!already_slept &&
3094 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3095 XLOG_STATE_SYNCING))) {
3096 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3097 XFS_STATS_INC(xs_log_force_sleep);
3098 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3099 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003100 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 already_slept = 1;
3102 goto try_again;
3103 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003104 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003106 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (xlog_state_release_iclog(log, iclog))
3108 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003109 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003110 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
3112 }
3113
3114 if ((flags & XFS_LOG_SYNC) && /* sleep */
3115 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3116
3117 /*
3118 * Don't wait on the forcesema if we know that we've
3119 * gotten a log write error.
3120 */
3121 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003122 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return XFS_ERROR(EIO);
3124 }
3125 XFS_STATS_INC(xs_log_force_sleep);
3126 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3127 /*
3128 * No need to grab the log lock here since we're
3129 * only deciding whether or not to return EIO
3130 * and the memory read should be atomic.
3131 */
3132 if (iclog->ic_state & XLOG_STATE_IOERROR)
3133 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003134 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003136 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
3138 return 0;
3139
3140 } while (iclog != log->l_iclog);
3141
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003142 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144} /* xlog_state_sync */
3145
3146
3147/*
3148 * Called when we want to mark the current iclog as being ready to sync to
3149 * disk.
3150 */
David Chinnera8272ce2007-11-23 16:28:09 +11003151STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3153{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003154 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3157 xlog_state_switch_iclogs(log, iclog, 0);
3158 } else {
3159 ASSERT(iclog->ic_state &
3160 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3161 }
3162
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003163 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164} /* xlog_state_want_sync */
3165
3166
3167
3168/*****************************************************************************
3169 *
3170 * TICKET functions
3171 *
3172 *****************************************************************************
3173 */
3174
3175/*
3176 * Algorithm doesn't take into account page size. ;-(
3177 */
3178STATIC void
3179xlog_state_ticket_alloc(xlog_t *log)
3180{
3181 xlog_ticket_t *t_list;
3182 xlog_ticket_t *next;
3183 xfs_caddr_t buf;
Tim Shimmine6a4b372007-11-23 16:30:42 +11003184 uint i = (PAGE_SIZE / sizeof(xlog_ticket_t)) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 /*
3187 * The kmem_zalloc may sleep, so we shouldn't be holding the
3188 * global lock. XXXmiken: may want to use zone allocator.
3189 */
Tim Shimmine6a4b372007-11-23 16:30:42 +11003190 buf = (xfs_caddr_t) kmem_zalloc(PAGE_SIZE, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003192 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3195 t_list = (xlog_ticket_t *)buf;
3196 t_list->t_next = log->l_unmount_free;
3197 log->l_unmount_free = t_list++;
3198 log->l_ticket_cnt++;
3199 log->l_ticket_tcnt++;
3200
3201 /* Next ticket becomes first ticket attached to ticket free list */
3202 if (log->l_freelist != NULL) {
3203 ASSERT(log->l_tail != NULL);
3204 log->l_tail->t_next = t_list;
3205 } else {
3206 log->l_freelist = t_list;
3207 }
3208 log->l_ticket_cnt++;
3209 log->l_ticket_tcnt++;
3210
3211 /* Cycle through rest of alloc'ed memory, building up free Q */
3212 for ( ; i > 0; i--) {
3213 next = t_list + 1;
3214 t_list->t_next = next;
3215 t_list = next;
3216 log->l_ticket_cnt++;
3217 log->l_ticket_tcnt++;
3218 }
3219 t_list->t_next = NULL;
3220 log->l_tail = t_list;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003221 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222} /* xlog_state_ticket_alloc */
3223
3224
3225/*
3226 * Put ticket into free list
3227 *
3228 * Assumption: log lock is held around this call.
3229 */
3230STATIC void
3231xlog_ticket_put(xlog_t *log,
3232 xlog_ticket_t *ticket)
3233{
3234 sv_destroy(&ticket->t_sema);
3235
3236 /*
3237 * Don't think caching will make that much difference. It's
3238 * more important to make debug easier.
3239 */
3240#if 0
3241 /* real code will want to use LIFO for caching */
3242 ticket->t_next = log->l_freelist;
3243 log->l_freelist = ticket;
3244 /* no need to clear fields */
3245#else
3246 /* When we debug, it is easier if tickets are cycled */
3247 ticket->t_next = NULL;
Christoph Hellwig4b809162007-08-16 15:37:36 +10003248 if (log->l_tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 log->l_tail->t_next = ticket;
3250 } else {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003251 ASSERT(log->l_freelist == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 log->l_freelist = ticket;
3253 }
3254 log->l_tail = ticket;
3255#endif /* DEBUG */
3256 log->l_ticket_cnt++;
3257} /* xlog_ticket_put */
3258
3259
3260/*
3261 * Grab ticket off freelist or allocation some more
3262 */
David Chinnera8272ce2007-11-23 16:28:09 +11003263STATIC xlog_ticket_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264xlog_ticket_get(xlog_t *log,
3265 int unit_bytes,
3266 int cnt,
3267 char client,
3268 uint xflags)
3269{
3270 xlog_ticket_t *tic;
3271 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 alloc:
3274 if (log->l_freelist == NULL)
3275 xlog_state_ticket_alloc(log); /* potentially sleep */
3276
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003277 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (log->l_freelist == NULL) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003279 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 goto alloc;
3281 }
3282 tic = log->l_freelist;
3283 log->l_freelist = tic->t_next;
3284 if (log->l_freelist == NULL)
3285 log->l_tail = NULL;
3286 log->l_ticket_cnt--;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003287 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 /*
3290 * Permanent reservations have up to 'cnt'-1 active log operations
3291 * in the log. A unit in this case is the amount of space for one
3292 * of these log operations. Normal reservations have a cnt of 1
3293 * and their unit amount is the total amount of space required.
3294 *
3295 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003296 * which occupy space in the on-disk log.
3297 *
3298 * Normal form of a transaction is:
3299 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3300 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3301 *
3302 * We need to account for all the leadup data and trailer data
3303 * around the transaction data.
3304 * And then we need to account for the worst case in terms of using
3305 * more space.
3306 * The worst case will happen if:
3307 * - the placement of the transaction happens to be such that the
3308 * roundoff is at its maximum
3309 * - the transaction data is synced before the commit record is synced
3310 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3311 * Therefore the commit record is in its own Log Record.
3312 * This can happen as the commit record is called with its
3313 * own region to xlog_write().
3314 * This then means that in the worst case, roundoff can happen for
3315 * the commit-rec as well.
3316 * The commit-rec is smaller than padding in this scenario and so it is
3317 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 */
3319
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003320 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003322 unit_bytes += sizeof(xfs_trans_header_t);
3323
3324 /* for start-rec */
3325 unit_bytes += sizeof(xlog_op_header_t);
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 /* for LR headers */
3328 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3329 unit_bytes += log->l_iclog_hsize * num_headers;
3330
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003331 /* for commit-rec LR header - note: padding will subsume the ophdr */
3332 unit_bytes += log->l_iclog_hsize;
3333
3334 /* for split-recs - ophdrs added when data split over LRs */
3335 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3336
3337 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003338 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003339 log->l_mp->m_sb.sb_logsunit > 1) {
3340 /* log su roundoff */
3341 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3342 } else {
3343 /* BB roundoff */
3344 unit_bytes += 2*BBSIZE;
3345 }
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 tic->t_unit_res = unit_bytes;
3348 tic->t_curr_res = unit_bytes;
3349 tic->t_cnt = cnt;
3350 tic->t_ocnt = cnt;
3351 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3352 tic->t_clientid = client;
3353 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003354 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (xflags & XFS_LOG_PERM_RESERV)
3356 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3357 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3358
Christoph Hellwig0adba532007-08-30 17:21:46 +10003359 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return tic;
3362} /* xlog_ticket_get */
3363
3364
3365/******************************************************************************
3366 *
3367 * Log debug routines
3368 *
3369 ******************************************************************************
3370 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003371#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372/*
3373 * Make sure that the destination ptr is within the valid data region of
3374 * one of the iclogs. This uses backup pointers stored in a different
3375 * part of the log in case we trash the log structure.
3376 */
3377void
3378xlog_verify_dest_ptr(xlog_t *log,
3379 __psint_t ptr)
3380{
3381 int i;
3382 int good_ptr = 0;
3383
3384 for (i=0; i < log->l_iclog_bufs; i++) {
3385 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3386 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3387 good_ptr++;
3388 }
3389 if (! good_ptr)
3390 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3391} /* xlog_verify_dest_ptr */
3392
3393STATIC void
3394xlog_verify_grant_head(xlog_t *log, int equals)
3395{
3396 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3397 if (equals)
3398 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3399 else
3400 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3401 } else {
3402 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3403 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3404 }
3405} /* xlog_verify_grant_head */
3406
3407/* check if it will fit */
3408STATIC void
3409xlog_verify_tail_lsn(xlog_t *log,
3410 xlog_in_core_t *iclog,
3411 xfs_lsn_t tail_lsn)
3412{
3413 int blocks;
3414
3415 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3416 blocks =
3417 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3418 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3419 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3420 } else {
3421 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3422
3423 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3424 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3425
3426 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3427 if (blocks < BTOBB(iclog->ic_offset) + 1)
3428 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3429 }
3430} /* xlog_verify_tail_lsn */
3431
3432/*
3433 * Perform a number of checks on the iclog before writing to disk.
3434 *
3435 * 1. Make sure the iclogs are still circular
3436 * 2. Make sure we have a good magic number
3437 * 3. Make sure we don't have magic numbers in the data
3438 * 4. Check fields of each log operation header for:
3439 * A. Valid client identifier
3440 * B. tid ptr value falls in valid ptr space (user space code)
3441 * C. Length in log record header is correct according to the
3442 * individual operation headers within record.
3443 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3444 * log, check the preceding blocks of the physical log to make sure all
3445 * the cycle numbers agree with the current cycle number.
3446 */
3447STATIC void
3448xlog_verify_iclog(xlog_t *log,
3449 xlog_in_core_t *iclog,
3450 int count,
3451 boolean_t syncing)
3452{
3453 xlog_op_header_t *ophead;
3454 xlog_in_core_t *icptr;
3455 xlog_in_core_2_t *xhdr;
3456 xfs_caddr_t ptr;
3457 xfs_caddr_t base_ptr;
3458 __psint_t field_offset;
3459 __uint8_t clientid;
3460 int len, i, j, k, op_len;
3461 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
3463 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003464 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 icptr = log->l_iclog;
3466 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003467 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 xlog_panic("xlog_verify_iclog: invalid ptr");
3469 icptr = icptr->ic_next;
3470 }
3471 if (icptr != log->l_iclog)
3472 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003473 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003476 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 xlog_panic("xlog_verify_iclog: invalid magic num");
3478
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003479 ptr = (xfs_caddr_t) &iclog->ic_header;
3480 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003482 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 xlog_panic("xlog_verify_iclog: unexpected magic num");
3484 }
3485
3486 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003487 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 ptr = iclog->ic_datap;
3489 base_ptr = ptr;
3490 ophead = (xlog_op_header_t *)ptr;
3491 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3492 for (i = 0; i < len; i++) {
3493 ophead = (xlog_op_header_t *)ptr;
3494
3495 /* clientid is only 1 byte */
3496 field_offset = (__psint_t)
3497 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3498 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3499 clientid = ophead->oh_clientid;
3500 } else {
3501 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3502 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3503 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3504 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003505 clientid = xlog_get_client_id(
3506 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003508 clientid = xlog_get_client_id(
3509 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 }
3511 }
3512 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003513 cmn_err(CE_WARN, "xlog_verify_iclog: "
3514 "invalid clientid %d op 0x%p offset 0x%lx",
3515 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 /* check length */
3518 field_offset = (__psint_t)
3519 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3520 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003521 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 } else {
3523 idx = BTOBBT((__psint_t)&ophead->oh_len -
3524 (__psint_t)iclog->ic_datap);
3525 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3526 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3527 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003528 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003530 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 }
3532 }
3533 ptr += sizeof(xlog_op_header_t) + op_len;
3534 }
3535} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003539 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 */
3541STATIC int
3542xlog_state_ioerror(
3543 xlog_t *log)
3544{
3545 xlog_in_core_t *iclog, *ic;
3546
3547 iclog = log->l_iclog;
3548 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3549 /*
3550 * Mark all the incore logs IOERROR.
3551 * From now on, no log flushes will result.
3552 */
3553 ic = iclog;
3554 do {
3555 ic->ic_state = XLOG_STATE_IOERROR;
3556 ic = ic->ic_next;
3557 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 /*
3561 * Return non-zero, if state transition has already happened.
3562 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003563 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
3566/*
3567 * This is called from xfs_force_shutdown, when we're forcibly
3568 * shutting down the filesystem, typically because of an IO error.
3569 * Our main objectives here are to make sure that:
3570 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3571 * parties to find out, 'atomically'.
3572 * b. those who're sleeping on log reservations, pinned objects and
3573 * other resources get woken up, and be told the bad news.
3574 * c. nothing new gets queued up after (a) and (b) are done.
3575 * d. if !logerror, flush the iclogs to disk, then seal them off
3576 * for business.
3577 */
3578int
3579xfs_log_force_umount(
3580 struct xfs_mount *mp,
3581 int logerror)
3582{
3583 xlog_ticket_t *tic;
3584 xlog_t *log;
3585 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003586 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 log = mp->m_log;
3589
3590 /*
3591 * If this happens during log recovery, don't worry about
3592 * locking; the log isn't open for business yet.
3593 */
3594 if (!log ||
3595 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3596 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3597 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
3600
3601 /*
3602 * Somebody could've already done the hard work for us.
3603 * No need to get locks for this.
3604 */
3605 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3606 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003607 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 }
3609 retval = 0;
3610 /*
3611 * We must hold both the GRANT lock and the LOG lock,
3612 * before we mark the filesystem SHUTDOWN and wake
3613 * everybody up to tell the bad news.
3614 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003615 spin_lock(&log->l_grant_lock);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003616 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3618 XFS_BUF_DONE(mp->m_sb_bp);
3619 /*
3620 * This flag is sort of redundant because of the mount flag, but
3621 * it's good to maintain the separation between the log and the rest
3622 * of XFS.
3623 */
3624 log->l_flags |= XLOG_IO_ERROR;
3625
3626 /*
3627 * If we hit a log error, we want to mark all the iclogs IOERROR
3628 * while we're still holding the loglock.
3629 */
3630 if (logerror)
3631 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003632 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 /*
3635 * We don't want anybody waiting for log reservations
3636 * after this. That means we have to wake up everybody
3637 * queued up on reserve_headq as well as write_headq.
3638 * In addition, we make sure in xlog_{re}grant_log_space
3639 * that we don't enqueue anything once the SHUTDOWN flag
3640 * is set, and this action is protected by the GRANTLOCK.
3641 */
3642 if ((tic = log->l_reserve_headq)) {
3643 do {
3644 sv_signal(&tic->t_sema);
3645 tic = tic->t_next;
3646 } while (tic != log->l_reserve_headq);
3647 }
3648
3649 if ((tic = log->l_write_headq)) {
3650 do {
3651 sv_signal(&tic->t_sema);
3652 tic = tic->t_next;
3653 } while (tic != log->l_write_headq);
3654 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003655 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3658 ASSERT(!logerror);
3659 /*
3660 * Force the incore logs to disk before shutting the
3661 * log down completely.
3662 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003663 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003664 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003666 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
3668 /*
3669 * Wake up everybody waiting on xfs_log_force.
3670 * Callback all log item committed functions as if the
3671 * log writes were completed.
3672 */
3673 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3674
3675#ifdef XFSERRORDEBUG
3676 {
3677 xlog_in_core_t *iclog;
3678
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003679 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 iclog = log->l_iclog;
3681 do {
3682 ASSERT(iclog->ic_callback == 0);
3683 iclog = iclog->ic_next;
3684 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003685 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 }
3687#endif
3688 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003689 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003692STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693xlog_iclogs_empty(xlog_t *log)
3694{
3695 xlog_in_core_t *iclog;
3696
3697 iclog = log->l_iclog;
3698 do {
3699 /* endianness does not matter here, zero is zero in
3700 * any language.
3701 */
3702 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 iclog = iclog->ic_next;
3705 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003706 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}