blob: 20118ddadef6bb64ee06b69bfcacdec73079fab3 [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"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
David Chinnereb01c9c2008-04-10 12:18:46 +100045kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define xlog_write_adv_cnt(ptr, len, off, bytes) \
48 { (ptr) += (bytes); \
49 (len) -= (bytes); \
50 (off) += (bytes);}
51
52/* Local miscellaneous function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54 xlog_in_core_t **, xfs_lsn_t *);
55STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
56 xfs_buftarg_t *log_target,
57 xfs_daddr_t blk_offset,
58 int num_bblks);
59STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
60STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100061STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
63 int nentries, xfs_log_ticket_t tic,
64 xfs_lsn_t *start_lsn,
65 xlog_in_core_t **commit_iclog,
66 uint flags);
67
68/* local state machine functions */
69STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71STATIC int xlog_state_get_iclog_space(xlog_t *log,
72 int len,
73 xlog_in_core_t **iclog,
74 xlog_ticket_t *ticket,
75 int *continued_write,
76 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077STATIC int xlog_state_release_iclog(xlog_t *log,
78 xlog_in_core_t *iclog);
79STATIC void xlog_state_switch_iclogs(xlog_t *log,
80 xlog_in_core_t *iclog,
81 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110082STATIC int xlog_state_sync(xlog_t *log,
83 xfs_lsn_t lsn,
84 uint flags,
85 int *log_flushed);
86STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
88
89/* local functions to manipulate grant head */
90STATIC int xlog_grant_log_space(xlog_t *log,
91 xlog_ticket_t *xtic);
92STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
93 int need_bytes);
94STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
95 xlog_ticket_t *ticket);
96STATIC int xlog_regrant_write_log_space(xlog_t *log,
97 xlog_ticket_t *ticket);
98STATIC void xlog_ungrant_log_space(xlog_t *log,
99 xlog_ticket_t *ticket);
100
101
102/* local ticket functions */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100103STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int unit_bytes,
105 int count,
106 char clientid,
107 uint flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100109#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
111STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
112STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
113 int count, boolean_t syncing);
114STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
115 xfs_lsn_t tail_lsn);
116#else
117#define xlog_verify_dest_ptr(a,b)
118#define xlog_verify_grant_head(a,b)
119#define xlog_verify_iclog(a,b,c,d)
120#define xlog_verify_tail_lsn(a,b,c)
121#endif
122
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000123STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100125
126static void
127xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
128{
129 if (*qp) {
130 tic->t_next = (*qp);
131 tic->t_prev = (*qp)->t_prev;
132 (*qp)->t_prev->t_next = tic;
133 (*qp)->t_prev = tic;
134 } else {
135 tic->t_prev = tic->t_next = tic;
136 *qp = tic;
137 }
138
139 tic->t_flags |= XLOG_TIC_IN_Q;
140}
141
142static void
143xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
144{
145 if (tic == tic->t_next) {
146 *qp = NULL;
147 } else {
148 *qp = tic->t_next;
149 tic->t_next->t_prev = tic->t_prev;
150 tic->t_prev->t_next = tic->t_next;
151 }
152
153 tic->t_next = tic->t_prev = NULL;
154 tic->t_flags &= ~XLOG_TIC_IN_Q;
155}
156
157static void
158xlog_grant_sub_space(struct log *log, int bytes)
159{
160 log->l_grant_write_bytes -= bytes;
161 if (log->l_grant_write_bytes < 0) {
162 log->l_grant_write_bytes += log->l_logsize;
163 log->l_grant_write_cycle--;
164 }
165
166 log->l_grant_reserve_bytes -= bytes;
167 if ((log)->l_grant_reserve_bytes < 0) {
168 log->l_grant_reserve_bytes += log->l_logsize;
169 log->l_grant_reserve_cycle--;
170 }
171
172}
173
174static void
175xlog_grant_add_space_write(struct log *log, int bytes)
176{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000177 int tmp = log->l_logsize - log->l_grant_write_bytes;
178 if (tmp > bytes)
179 log->l_grant_write_bytes += bytes;
180 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100181 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000182 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100183 }
184}
185
186static void
187xlog_grant_add_space_reserve(struct log *log, int bytes)
188{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000189 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
190 if (tmp > bytes)
191 log->l_grant_reserve_bytes += bytes;
192 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100193 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000194 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100195 }
196}
197
198static inline void
199xlog_grant_add_space(struct log *log, int bytes)
200{
201 xlog_grant_add_space_write(log, bytes);
202 xlog_grant_add_space_reserve(log, bytes);
203}
204
Christoph Hellwig0adba532007-08-30 17:21:46 +1000205static void
206xlog_tic_reset_res(xlog_ticket_t *tic)
207{
208 tic->t_res_num = 0;
209 tic->t_res_arr_sum = 0;
210 tic->t_res_num_ophdrs = 0;
211}
212
213static void
214xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
215{
216 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
217 /* add to overflow and start again */
218 tic->t_res_o_flow += tic->t_res_arr_sum;
219 tic->t_res_num = 0;
220 tic->t_res_arr_sum = 0;
221 }
222
223 tic->t_res_arr[tic->t_res_num].r_len = len;
224 tic->t_res_arr[tic->t_res_num].r_type = type;
225 tic->t_res_arr_sum += len;
226 tic->t_res_num++;
227}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * NOTES:
231 *
232 * 1. currblock field gets updated at startup and after in-core logs
233 * marked as with WANT_SYNC.
234 */
235
236/*
237 * This routine is called when a user of a log manager ticket is done with
238 * the reservation. If the ticket was ever used, then a commit record for
239 * the associated transaction is written out as a log operation header with
240 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
241 * a given ticket. If the ticket was one with a permanent reservation, then
242 * a few operations are done differently. Permanent reservation tickets by
243 * default don't release the reservation. They just commit the current
244 * transaction with the belief that the reservation is still needed. A flag
245 * must be passed in before permanent reservations are actually released.
246 * When these type of tickets are not released, they need to be set into
247 * the inited state again. By doing this, a start record will be written
248 * out when the next write occurs.
249 */
250xfs_lsn_t
251xfs_log_done(xfs_mount_t *mp,
252 xfs_log_ticket_t xtic,
253 void **iclog,
254 uint flags)
255{
256 xlog_t *log = mp->m_log;
257 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
258 xfs_lsn_t lsn = 0;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (XLOG_FORCED_SHUTDOWN(log) ||
261 /*
262 * If nothing was ever written, don't write out commit record.
263 * If we get an error, just continue and give back the log ticket.
264 */
265 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
266 (xlog_commit_record(mp, ticket,
267 (xlog_in_core_t **)iclog, &lsn)))) {
268 lsn = (xfs_lsn_t) -1;
269 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
270 flags |= XFS_LOG_REL_PERM_RESERV;
271 }
272 }
273
274
275 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
276 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000277 trace_xfs_log_done_nonperm(log, ticket);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000280 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * request has been made to release a permanent reservation.
282 */
283 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100284 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000286 trace_xfs_log_done_perm(log, ticket);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000289 /* If this ticket was a permanent reservation and we aren't
290 * trying to release it, reset the inited flags; so next time
291 * we write, a start record will be written out.
292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 return lsn;
297} /* xfs_log_done */
298
299
300/*
301 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
302 * the force is done synchronously.
303 *
304 * Asynchronous forces are implemented by setting the WANT_SYNC
305 * bit in the appropriate in-core log and then returning.
306 *
David Chinner12017fa2008-08-13 16:34:31 +1000307 * Synchronous forces are implemented with a signal variable. All callers
308 * to force a given lsn to disk will wait on a the sv attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * specific in-core log. When given in-core log finally completes its
310 * write to disk, that thread will wake up all threads waiting on the
David Chinner12017fa2008-08-13 16:34:31 +1000311 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100314_xfs_log_force(
315 xfs_mount_t *mp,
316 xfs_lsn_t lsn,
317 uint flags,
318 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100320 xlog_t *log = mp->m_log;
321 int dummy;
322
323 if (!log_flushed)
324 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 ASSERT(flags & XFS_LOG_FORCE);
327
328 XFS_STATS_INC(xs_log_force);
329
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100330 if (log->l_flags & XLOG_IO_ERROR)
331 return XFS_ERROR(EIO);
332 if (lsn == 0)
333 return xlog_state_sync_all(log, flags, log_flushed);
334 else
335 return xlog_state_sync(log, lsn, flags, log_flushed);
David Chinnerb911ca02008-04-10 12:24:30 +1000336} /* _xfs_log_force */
337
338/*
339 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
340 * about errors or whether the log was flushed or not. This is the normal
341 * interface to use when trying to unpin items or move the log forward.
342 */
343void
344xfs_log_force(
345 xfs_mount_t *mp,
346 xfs_lsn_t lsn,
347 uint flags)
348{
349 int error;
350 error = _xfs_log_force(mp, lsn, flags, NULL);
351 if (error) {
352 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
353 "error %d returned.", error);
354 }
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358/*
359 * Attaches a new iclog I/O completion callback routine during
360 * transaction commit. If the log is in error state, a non-zero
361 * return code is handed back and the caller is responsible for
362 * executing the callback at an appropriate time.
363 */
364int
365xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
366 void *iclog_hndl, /* iclog to hang callback off */
367 xfs_log_callback_t *cb)
368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000370 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David Chinner114d23a2008-04-10 12:18:39 +1000372 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
374 if (!abortflg) {
375 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
376 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
377 cb->cb_next = NULL;
378 *(iclog->ic_callback_tail) = cb;
379 iclog->ic_callback_tail = &(cb->cb_next);
380 }
David Chinner114d23a2008-04-10 12:18:39 +1000381 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return abortflg;
383} /* xfs_log_notify */
384
385int
386xfs_log_release_iclog(xfs_mount_t *mp,
387 void *iclog_hndl)
388{
389 xlog_t *log = mp->m_log;
390 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
391
392 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000393 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100394 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 return 0;
398}
399
400/*
401 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
402 * to the reservation.
403 * 2. Potentially, push buffers at tail of log to disk.
404 *
405 * Each reservation is going to reserve extra space for a log record header.
406 * When writes happen to the on-disk log, we don't subtract the length of the
407 * log record header from any reservation. By wasting space in each
408 * reservation, we prevent over allocation problems.
409 */
410int
411xfs_log_reserve(xfs_mount_t *mp,
412 int unit_bytes,
413 int cnt,
414 xfs_log_ticket_t *ticket,
415 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000416 uint flags,
417 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 xlog_t *log = mp->m_log;
420 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100421 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
424 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
425
426 if (XLOG_FORCED_SHUTDOWN(log))
427 return XFS_ERROR(EIO);
428
429 XFS_STATS_INC(xs_try_logspace);
430
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (*ticket != NULL) {
433 ASSERT(flags & XFS_LOG_PERM_RESERV);
434 internal_ticket = (xlog_ticket_t *)*ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000435
436 trace_xfs_log_reserve(log, internal_ticket);
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
439 retval = xlog_regrant_write_log_space(log, internal_ticket);
440 } else {
441 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100442 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000444 if (!internal_ticket)
445 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000446 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000448
449 trace_xfs_log_reserve(log, internal_ticket);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 xlog_grant_push_ail(mp,
452 (internal_ticket->t_unit_res *
453 internal_ticket->t_cnt));
454 retval = xlog_grant_log_space(log, internal_ticket);
455 }
456
457 return retval;
458} /* xfs_log_reserve */
459
460
461/*
462 * Mount a log filesystem
463 *
464 * mp - ubiquitous xfs mount point structure
465 * log_target - buftarg of on-disk log device
466 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
467 * num_bblocks - Number of BBSIZE blocks in on-disk log
468 *
469 * Return error or zero.
470 */
471int
David Chinner249a8c12008-02-05 12:13:32 +1100472xfs_log_mount(
473 xfs_mount_t *mp,
474 xfs_buftarg_t *log_target,
475 xfs_daddr_t blk_offset,
476 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
David Chinner249a8c12008-02-05 12:13:32 +1100478 int error;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
481 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
482 else {
483 cmn_err(CE_NOTE,
484 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
485 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000486 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200490 if (IS_ERR(mp->m_log)) {
491 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100492 goto out;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /*
David Chinner249a8c12008-02-05 12:13:32 +1100496 * Initialize the AIL now we have a log.
497 */
David Chinner249a8c12008-02-05 12:13:32 +1100498 error = xfs_trans_ail_init(mp);
499 if (error) {
500 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100501 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100502 }
David Chinnera9c21c12008-10-30 17:39:35 +1100503 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100504
505 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * skip log recovery on a norecovery mount. pretend it all
507 * just worked.
508 */
509 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100510 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000513 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Eric Sandeen65be6052006-01-11 15:34:19 +1100515 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000518 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (error) {
520 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100521 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 }
524
525 /* Normal transactions can now occur */
526 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100529
530out_destroy_ail:
531 xfs_trans_ail_destroy(mp);
532out_free_log:
533 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100534out:
David Chinner249a8c12008-02-05 12:13:32 +1100535 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100536}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538/*
539 * Finish the recovery of the file system. This is separate from
540 * the xfs_log_mount() call, because it depends on the code in
541 * xfs_mountfs() to read in the root and real-time bitmap inodes
542 * between calling xfs_log_mount() and here.
543 *
544 * mp - ubiquitous xfs mount point structure
545 */
546int
Christoph Hellwig42490232008-08-13 16:49:32 +1000547xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 int error;
550
551 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000552 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 else {
554 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000555 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
558 return error;
559}
560
561/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * Final log writes as part of unmount.
563 *
564 * Mark the filesystem clean as unmount happens. Note that during relocation
565 * this routine needs to be executed as part of source-bag while the
566 * deallocation must not be done until source-end.
567 */
568
569/*
570 * Unmount record used to have a string "Unmount filesystem--" in the
571 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
572 * We just write the magic number now since that particular field isn't
573 * currently architecture converted and "nUmount" is a bit foo.
574 * As far as I know, there weren't any dependencies on the old behaviour.
575 */
576
577int
578xfs_log_unmount_write(xfs_mount_t *mp)
579{
580 xlog_t *log = mp->m_log;
581 xlog_in_core_t *iclog;
582#ifdef DEBUG
583 xlog_in_core_t *first_iclog;
584#endif
585 xfs_log_iovec_t reg[1];
586 xfs_log_ticket_t tic = NULL;
587 xfs_lsn_t lsn;
588 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* the data section must be 32 bit size aligned */
591 struct {
592 __uint16_t magic;
593 __uint16_t pad1;
594 __uint32_t pad2; /* may as well make it 64 bits */
595 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * Don't write out unmount record on read-only mounts.
599 * Or, if we are doing a forced umount (typically because of IO errors).
600 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000601 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603
David Chinnerb911ca02008-04-10 12:24:30 +1000604 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
605 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607#ifdef DEBUG
608 first_iclog = iclog = log->l_iclog;
609 do {
610 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
611 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
612 ASSERT(iclog->ic_offset == 0);
613 }
614 iclog = iclog->ic_next;
615 } while (iclog != first_iclog);
616#endif
617 if (! (XLOG_FORCED_SHUTDOWN(log))) {
618 reg[0].i_addr = (void*)&magic;
619 reg[0].i_len = sizeof(magic);
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000620 reg[0].i_type = XLOG_REG_TYPE_UNMOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Tim Shimmin955e47a2006-09-28 11:04:16 +1000622 error = xfs_log_reserve(mp, 600, 1, &tic,
623 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!error) {
625 /* remove inited flag */
626 ((xlog_ticket_t *)tic)->t_flags = 0;
627 error = xlog_write(mp, reg, 1, tic, &lsn,
628 NULL, XLOG_UNMOUNT_TRANS);
629 /*
630 * At this point, we're umounting anyway,
631 * so there's no point in transitioning log state
632 * to IOERROR. Just continue...
633 */
634 }
635
636 if (error) {
637 xfs_fs_cmn_err(CE_ALERT, mp,
638 "xfs_log_unmount: unmount record failed");
639 }
640
641
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000642 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100644 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100646 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000647 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000649 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
651 iclog->ic_state == XLOG_STATE_DIRTY)) {
652 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000653 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 &log->l_icloglock, s);
655 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000656 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000659 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000661 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000662 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000663 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100664 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 } else {
667 /*
668 * We're already in forced_shutdown mode, couldn't
669 * even attempt to write out the unmount transaction.
670 *
671 * Go through the motions of sync'ing and releasing
672 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000673 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * be in progress. Do this as a separate section of
675 * code so we'll know if we ever get stuck here that
676 * we're in this odd situation of trying to unmount
677 * a file system that went into forced_shutdown as
678 * the result of an unmount..
679 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000680 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100682 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100685 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000686 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000688 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
691 || iclog->ic_state == XLOG_STATE_DIRTY
692 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
693
David Chinner12017fa2008-08-13 16:34:31 +1000694 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 &log->l_icloglock, s);
696 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000697 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 }
700
David Chinner1bb7d6b2008-04-10 12:24:38 +1000701 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702} /* xfs_log_unmount_write */
703
704/*
705 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100706 *
707 * We need to stop the aild from running before we destroy
708 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
710void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100711xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
David Chinner249a8c12008-02-05 12:13:32 +1100713 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000714 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717/*
718 * Write region vectors to log. The write happens using the space reservation
719 * of the ticket (tic). It is not a requirement that all writes for a given
720 * transaction occur with one call to xfs_log_write().
721 */
722int
723xfs_log_write(xfs_mount_t * mp,
724 xfs_log_iovec_t reg[],
725 int nentries,
726 xfs_log_ticket_t tic,
727 xfs_lsn_t *start_lsn)
728{
729 int error;
730 xlog_t *log = mp->m_log;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (XLOG_FORCED_SHUTDOWN(log))
733 return XFS_ERROR(EIO);
734
735 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000736 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100738 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739} /* xfs_log_write */
740
741
742void
743xfs_log_move_tail(xfs_mount_t *mp,
744 xfs_lsn_t tail_lsn)
745{
746 xlog_ticket_t *tic;
747 xlog_t *log = mp->m_log;
748 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (XLOG_FORCED_SHUTDOWN(log))
751 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (tail_lsn == 0) {
754 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000755 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000757 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000760 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
763 * tail_lsn.
764 */
765 if (tail_lsn != 1) {
766 log->l_tail_lsn = tail_lsn;
767 }
768
769 if ((tic = log->l_write_headq)) {
770#ifdef DEBUG
771 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
772 panic("Recovery problem");
773#endif
774 cycle = log->l_grant_write_cycle;
775 bytes = log->l_grant_write_bytes;
776 free_bytes = xlog_space_left(log, cycle, bytes);
777 do {
778 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
779
780 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
781 break;
782 tail_lsn = 0;
783 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000784 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 tic = tic->t_next;
786 } while (tic != log->l_write_headq);
787 }
788 if ((tic = log->l_reserve_headq)) {
789#ifdef DEBUG
790 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
791 panic("Recovery problem");
792#endif
793 cycle = log->l_grant_reserve_cycle;
794 bytes = log->l_grant_reserve_bytes;
795 free_bytes = xlog_space_left(log, cycle, bytes);
796 do {
797 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
798 need_bytes = tic->t_unit_res*tic->t_cnt;
799 else
800 need_bytes = tic->t_unit_res;
801 if (free_bytes < need_bytes && tail_lsn != 1)
802 break;
803 tail_lsn = 0;
804 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000805 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 tic = tic->t_next;
807 } while (tic != log->l_reserve_headq);
808 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000809 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810} /* xfs_log_move_tail */
811
812/*
813 * Determine if we have a transaction that has gone to disk
814 * that needs to be covered. Log activity needs to be idle (no AIL and
815 * nothing in the iclogs). And, we need to be in the right state indicating
816 * something has gone out.
817 */
818int
819xfs_log_need_covered(xfs_mount_t *mp)
820{
David Chinner27d8d5f2008-10-30 17:38:39 +1100821 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
David Chinner92821e22007-05-24 15:26:31 +1000824 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000827 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
829 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
David Chinnera9c21c12008-10-30 17:39:35 +1100830 && !xfs_trans_ail_tail(log->l_ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 && xlog_iclogs_empty(log)) {
832 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
833 log->l_covered_state = XLOG_STATE_COVER_DONE;
834 else {
835 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
836 log->l_covered_state = XLOG_STATE_COVER_DONE2;
837 }
838 needed = 1;
839 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000840 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100841 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844/******************************************************************************
845 *
846 * local routines
847 *
848 ******************************************************************************
849 */
850
851/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
852 * The log manager must keep track of the last LR which was committed
853 * to disk. The lsn of this LR will become the new tail_lsn whenever
854 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
855 * the situation where stuff could be written into the log but nothing
856 * was ever in the AIL when asked. Eventually, we panic since the
857 * tail hits the head.
858 *
859 * We may be holding the log iclog lock upon entering this routine.
860 */
861xfs_lsn_t
862xlog_assign_tail_lsn(xfs_mount_t *mp)
863{
864 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 xlog_t *log = mp->m_log;
866
David Chinner5b00f142008-10-30 17:39:00 +1100867 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000868 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (tail_lsn != 0) {
870 log->l_tail_lsn = tail_lsn;
871 } else {
872 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
873 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000874 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 return tail_lsn;
877} /* xlog_assign_tail_lsn */
878
879
880/*
881 * Return the space in the log between the tail and the head. The head
882 * is passed in the cycle/bytes formal parms. In the special case where
883 * the reserve head has wrapped passed the tail, this calculation is no
884 * longer valid. In this case, just return 0 which means there is no space
885 * in the log. This works for all places where this function is called
886 * with the reserve head. Of course, if the write head were to ever
887 * wrap the tail, we should blow up. Rather than catch this case here,
888 * we depend on other ASSERTions in other parts of the code. XXXmiken
889 *
890 * This code also handles the case where the reservation head is behind
891 * the tail. The details of this case are described below, but the end
892 * result is that we return the size of the log as the amount of space left.
893 */
David Chinnera8272ce2007-11-23 16:28:09 +1100894STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895xlog_space_left(xlog_t *log, int cycle, int bytes)
896{
897 int free_bytes;
898 int tail_bytes;
899 int tail_cycle;
900
901 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
902 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
903 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
904 free_bytes = log->l_logsize - (bytes - tail_bytes);
905 } else if ((tail_cycle + 1) < cycle) {
906 return 0;
907 } else if (tail_cycle < cycle) {
908 ASSERT(tail_cycle == (cycle - 1));
909 free_bytes = tail_bytes - bytes;
910 } else {
911 /*
912 * The reservation head is behind the tail.
913 * In this case we just want to return the size of the
914 * log as the amount of space left.
915 */
916 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
917 "xlog_space_left: head behind tail\n"
918 " tail_cycle = %d, tail_bytes = %d\n"
919 " GH cycle = %d, GH bytes = %d",
920 tail_cycle, tail_bytes, cycle, bytes);
921 ASSERT(0);
922 free_bytes = log->l_logsize;
923 }
924 return free_bytes;
925} /* xlog_space_left */
926
927
928/*
929 * Log function which is called when an io completes.
930 *
931 * The log manager needs its own routine, in order to control what
932 * happens with the buffer after the write completes.
933 */
934void
935xlog_iodone(xfs_buf_t *bp)
936{
937 xlog_in_core_t *iclog;
938 xlog_t *l;
939 int aborted;
940
941 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
942 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
943 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
944 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 l = iclog->ic_log;
946
947 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100948 * If the _XFS_BARRIER_FAILED flag was set by a lower
949 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +1000950 * barrier I/O. Warn loudly and turn off barriers.
951 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100952 if (bp->b_flags & _XFS_BARRIER_FAILED) {
953 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000954 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
955 xfs_fs_cmn_err(CE_WARN, l->l_mp,
956 "xlog_iodone: Barriers are no longer supported"
957 " by device. Disabling barriers\n");
David Chinner0bfefc42007-05-14 18:24:23 +1000958 }
959
960 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * Race to shutdown the filesystem if we see an error.
962 */
963 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
964 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
965 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
966 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000967 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /*
969 * This flag will be propagated to the trans-committed
970 * callback routines to let them know that the log-commit
971 * didn't succeed.
972 */
973 aborted = XFS_LI_ABORTED;
974 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
975 aborted = XFS_LI_ABORTED;
976 }
David Chinner3db296f2007-05-14 18:24:16 +1000977
978 /* log I/O is always issued ASYNC */
979 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000981 /*
982 * do not reference the buffer (bp) here as we could race
983 * with it being freed after writing the unmount record to the
984 * log.
985 */
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987} /* xlog_iodone */
988
989/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * Return size of each in-core log record buffer.
991 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200992 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *
994 * If the filesystem blocksize is too large, we may need to choose a
995 * larger size since the directory code currently logs entire blocks.
996 */
997
998STATIC void
999xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1000 xlog_t *log)
1001{
1002 int size;
1003 int xhdrs;
1004
Eric Sandeen1cb51252007-08-16 16:24:43 +10001005 if (mp->m_logbufs <= 0)
1006 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1007 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001008 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 /*
1011 * Buffer size passed in from mount system call.
1012 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001013 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 size = log->l_iclog_size = mp->m_logbsize;
1015 log->l_iclog_size_log = 0;
1016 while (size != 1) {
1017 log->l_iclog_size_log++;
1018 size >>= 1;
1019 }
1020
Eric Sandeen62118702008-03-06 13:44:28 +11001021 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001022 /* # headers = size / 32k
1023 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
1025
1026 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1027 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1028 xhdrs++;
1029 log->l_iclog_hsize = xhdrs << BBSHIFT;
1030 log->l_iclog_heads = xhdrs;
1031 } else {
1032 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1033 log->l_iclog_hsize = BBSIZE;
1034 log->l_iclog_heads = 1;
1035 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001036 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001039 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001040 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1041 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /* the default log size is 16k or 32k which is one header sector */
1044 log->l_iclog_hsize = BBSIZE;
1045 log->l_iclog_heads = 1;
1046
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001047done:
1048 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001049 if (mp->m_logbufs == 0)
1050 mp->m_logbufs = log->l_iclog_bufs;
1051 if (mp->m_logbsize == 0)
1052 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053} /* xlog_get_iclog_buffer_size */
1054
1055
1056/*
1057 * This routine initializes some of the log structure for a given mount point.
1058 * Its primary purpose is to fill in enough, so recovery can occur. However,
1059 * some other stuff may be filled in too.
1060 */
1061STATIC xlog_t *
1062xlog_alloc_log(xfs_mount_t *mp,
1063 xfs_buftarg_t *log_target,
1064 xfs_daddr_t blk_offset,
1065 int num_bblks)
1066{
1067 xlog_t *log;
1068 xlog_rec_header_t *head;
1069 xlog_in_core_t **iclogp;
1070 xlog_in_core_t *iclog, *prev_iclog=NULL;
1071 xfs_buf_t *bp;
1072 int i;
1073 int iclogsize;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001074 int error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Dave Chinner644c3562008-11-10 16:50:24 +11001076 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001077 if (!log) {
1078 xlog_warn("XFS: Log allocation failed: No memory!");
1079 goto out;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 log->l_mp = mp;
1083 log->l_targ = log_target;
1084 log->l_logsize = BBTOB(num_bblks);
1085 log->l_logBBstart = blk_offset;
1086 log->l_logBBsize = num_bblks;
1087 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1088 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1089
1090 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001091 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1093 log->l_last_sync_lsn = log->l_tail_lsn;
1094 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1095 log->l_grant_reserve_cycle = 1;
1096 log->l_grant_write_cycle = 1;
1097
Dave Chinnera6cb7672009-04-06 18:39:27 +02001098 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001099 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001101 if (log->l_sectbb_log < 0 ||
1102 log->l_sectbb_log > mp->m_sectbb_log) {
1103 xlog_warn("XFS: Log sector size (0x%x) out of range.",
1104 log->l_sectbb_log);
1105 goto out_free_log;
1106 }
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* for larger sector sizes, must have v2 or external log */
Dave Chinnera6cb7672009-04-06 18:39:27 +02001109 if (log->l_sectbb_log != 0 &&
1110 (log->l_logBBstart != 0 &&
1111 !xfs_sb_version_haslogv2(&mp->m_sb))) {
1112 xlog_warn("XFS: log sector size (0x%x) invalid "
1113 "for configuration.", log->l_sectbb_log);
1114 goto out_free_log;
1115 }
1116 if (mp->m_sb.sb_logsectlog < BBSHIFT) {
1117 xlog_warn("XFS: Log sector log (0x%x) too small.",
1118 mp->m_sb.sb_logsectlog);
1119 goto out_free_log;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1123
1124 xlog_get_iclog_buffer_size(mp, log);
1125
Dave Chinnera6cb7672009-04-06 18:39:27 +02001126 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001128 if (!bp)
1129 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1132 ASSERT(XFS_BUF_ISBUSY(bp));
1133 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1134 log->l_xbuf = bp;
1135
Eric Sandeen007c61c2007-10-11 17:43:56 +10001136 spin_lock_init(&log->l_icloglock);
1137 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001138 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1141 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1142
1143 iclogp = &log->l_iclog;
1144 /*
1145 * The amount of memory to allocate for the iclog structure is
1146 * rather funky due to the way the structure is defined. It is
1147 * done this way so that we can use different sizes for machines
1148 * with different amounts of memory. See the definition of
1149 * xlog_in_core_t in xfs_log_priv.h for details.
1150 */
1151 iclogsize = log->l_iclog_size;
1152 ASSERT(log->l_iclog_size >= 4096);
1153 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001154 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1155 if (!*iclogp)
1156 goto out_free_iclog;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 iclog->ic_prev = prev_iclog;
1160 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001161
1162 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001163 if (!bp)
1164 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001165 if (!XFS_BUF_CPSEMA(bp))
1166 ASSERT(0);
1167 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001168 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1169 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001170 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001171#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 head = &iclog->ic_header;
1175 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001176 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1177 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001178 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001179 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001181 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1185 iclog->ic_state = XLOG_STATE_ACTIVE;
1186 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001187 atomic_set(&iclog->ic_refcnt, 0);
1188 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001190 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1193 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001194 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1195 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 iclogp = &iclog->ic_next;
1198 }
1199 *iclogp = log->l_iclog; /* complete ring */
1200 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1201
1202 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001203
1204out_free_iclog:
1205 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1206 prev_iclog = iclog->ic_next;
1207 if (iclog->ic_bp) {
1208 sv_destroy(&iclog->ic_force_wait);
1209 sv_destroy(&iclog->ic_write_wait);
1210 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001211 }
1212 kmem_free(iclog);
1213 }
1214 spinlock_destroy(&log->l_icloglock);
1215 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001216 xfs_buf_free(log->l_xbuf);
1217out_free_log:
1218 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001219out:
1220 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221} /* xlog_alloc_log */
1222
1223
1224/*
1225 * Write out the commit record of a transaction associated with the given
1226 * ticket. Return the lsn of the commit record.
1227 */
1228STATIC int
1229xlog_commit_record(xfs_mount_t *mp,
1230 xlog_ticket_t *ticket,
1231 xlog_in_core_t **iclog,
1232 xfs_lsn_t *commitlsnp)
1233{
1234 int error;
1235 xfs_log_iovec_t reg[1];
1236
1237 reg[0].i_addr = NULL;
1238 reg[0].i_len = 0;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +00001239 reg[0].i_type = XLOG_REG_TYPE_COMMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 ASSERT_ALWAYS(iclog);
1242 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1243 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001244 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001246 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247} /* xlog_commit_record */
1248
1249
1250/*
1251 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1252 * log space. This code pushes on the lsn which would supposedly free up
1253 * the 25% which we want to leave free. We may need to adopt a policy which
1254 * pushes on an lsn which is further along in the log once we reach the high
1255 * water mark. In this manner, we would be creating a low water mark.
1256 */
David Chinnera8272ce2007-11-23 16:28:09 +11001257STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258xlog_grant_push_ail(xfs_mount_t *mp,
1259 int need_bytes)
1260{
1261 xlog_t *log = mp->m_log; /* pointer to the log */
1262 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1263 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1264 int free_blocks; /* free blocks left to write to */
1265 int free_bytes; /* free bytes left to write to */
1266 int threshold_block; /* block in lsn we'd like to be at */
1267 int threshold_cycle; /* lsn cycle we'd like to be at */
1268 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1271
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001272 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 free_bytes = xlog_space_left(log,
1274 log->l_grant_reserve_cycle,
1275 log->l_grant_reserve_bytes);
1276 tail_lsn = log->l_tail_lsn;
1277 free_blocks = BTOBBT(free_bytes);
1278
1279 /*
1280 * Set the threshold for the minimum number of free blocks in the
1281 * log to the maximum of what the caller needs, one quarter of the
1282 * log, and 256 blocks.
1283 */
1284 free_threshold = BTOBB(need_bytes);
1285 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1286 free_threshold = MAX(free_threshold, 256);
1287 if (free_blocks < free_threshold) {
1288 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1289 threshold_cycle = CYCLE_LSN(tail_lsn);
1290 if (threshold_block >= log->l_logBBsize) {
1291 threshold_block -= log->l_logBBsize;
1292 threshold_cycle += 1;
1293 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001294 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /* Don't pass in an lsn greater than the lsn of the last
1297 * log record known to be on disk.
1298 */
1299 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1300 threshold_lsn = log->l_last_sync_lsn;
1301 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001302 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /*
1305 * Get the transaction layer to kick the dirty buffers out to
1306 * disk asynchronously. No point in trying to do this if
1307 * the filesystem is shutting down.
1308 */
1309 if (threshold_lsn &&
1310 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001311 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312} /* xlog_grant_push_ail */
1313
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001314/*
1315 * The bdstrat callback function for log bufs. This gives us a central
1316 * place to trap bufs in case we get hit by a log I/O error and need to
1317 * shutdown. Actually, in practice, even when we didn't get a log error,
1318 * we transition the iclogs to IOERROR state *after* flushing all existing
1319 * iclogs to disk. This is because we don't want anymore new transactions to be
1320 * started or completed afterwards.
1321 */
1322STATIC int
1323xlog_bdstrat(
1324 struct xfs_buf *bp)
1325{
1326 struct xlog_in_core *iclog;
1327
1328 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1329 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1330 XFS_BUF_ERROR(bp, EIO);
1331 XFS_BUF_STALE(bp);
1332 xfs_biodone(bp);
1333 /*
1334 * It would seem logical to return EIO here, but we rely on
1335 * the log state machine to propagate I/O errors instead of
1336 * doing it here.
1337 */
1338 return 0;
1339 }
1340
1341 bp->b_flags |= _XBF_RUN_QUEUES;
1342 xfs_buf_iorequest(bp);
1343 return 0;
1344}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346/*
1347 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1348 * fashion. Previously, we should have moved the current iclog
1349 * ptr in the log to point to the next available iclog. This allows further
1350 * write to continue while this code syncs out an iclog ready to go.
1351 * Before an in-core log can be written out, the data section must be scanned
1352 * to save away the 1st word of each BBSIZE block into the header. We replace
1353 * it with the current cycle count. Each BBSIZE block is tagged with the
1354 * cycle count because there in an implicit assumption that drives will
1355 * guarantee that entire 512 byte blocks get written at once. In other words,
1356 * we can't have part of a 512 byte block written and part not written. By
1357 * tagging each block, we will know which blocks are valid when recovering
1358 * after an unclean shutdown.
1359 *
1360 * This routine is single threaded on the iclog. No other thread can be in
1361 * this routine with the same iclog. Changing contents of iclog can there-
1362 * fore be done without grabbing the state machine lock. Updating the global
1363 * log will require grabbing the lock though.
1364 *
1365 * The entire log manager uses a logical block numbering scheme. Only
1366 * log_sync (and then only bwrite()) know about the fact that the log may
1367 * not start with block zero on a given device. The log block start offset
1368 * is added immediately before calling bwrite().
1369 */
1370
David Chinnera8272ce2007-11-23 16:28:09 +11001371STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372xlog_sync(xlog_t *log,
1373 xlog_in_core_t *iclog)
1374{
1375 xfs_caddr_t dptr; /* pointer to byte sized element */
1376 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001377 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 uint count; /* byte count of bwrite */
1379 uint count_init; /* initial count before roundup */
1380 int roundoff; /* roundoff to BB or stripe */
1381 int split = 0; /* split write into two regions */
1382 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001383 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001386 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* Add for LR header */
1389 count_init = log->l_iclog_hsize + iclog->ic_offset;
1390
1391 /* Round out the log write size */
1392 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1393 /* we have a v2 stripe unit to use */
1394 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1395 } else {
1396 count = BBTOB(BTOBB(count_init));
1397 }
1398 roundoff = count - count_init;
1399 ASSERT(roundoff >= 0);
1400 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1401 roundoff < log->l_mp->m_sb.sb_logsunit)
1402 ||
1403 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1404 roundoff < BBTOB(1)));
1405
1406 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001407 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001408 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001409 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* put cycle number in every block */
1412 xlog_pack_data(log, iclog, roundoff);
1413
1414 /* real byte length */
1415 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001416 iclog->ic_header.h_len =
1417 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001419 iclog->ic_header.h_len =
1420 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422
Nathan Scottf5faad72006-07-28 17:04:44 +10001423 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1425 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001426 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1429
1430 /* Do we need to split this write into 2 parts? */
1431 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1432 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1433 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1434 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1435 } else {
1436 iclog->ic_bwritecnt = 1;
1437 }
David Chinner511105b2007-05-24 15:21:57 +10001438 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001440 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 XFS_BUF_BUSY(bp);
1442 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001443 bp->b_flags |= XBF_LOG_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001445 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001446 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001448 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001449 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1452 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1453
1454 xlog_verify_iclog(log, iclog, count, B_TRUE);
1455
1456 /* account for log which doesn't start at block #0 */
1457 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1458 /*
1459 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1460 * is shutting down.
1461 */
1462 XFS_BUF_WRITE(bp);
1463
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001464 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1466 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001467 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001470 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1472 (unsigned long)1);
1473 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1474 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1475 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1476 (__psint_t)count), split);
1477 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001478 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 XFS_BUF_BUSY(bp);
1480 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001481 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001482 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1483 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 dptr = XFS_BUF_PTR(bp);
1485 /*
1486 * Bump the cycle numbers at the start of each block
1487 * since this part of the buffer is at the start of
1488 * a new cycle. Watch out for the header magic number
1489 * case, though.
1490 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001491 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001492 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001493 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001494 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 dptr += BBSIZE;
1496 }
1497
1498 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1499 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1500
Nathan Scottc41564b2006-03-29 08:55:14 +10001501 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1503 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001504 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1506 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001507 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511} /* xlog_sync */
1512
1513
1514/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001515 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 */
David Chinnera8272ce2007-11-23 16:28:09 +11001517STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001518xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 int i;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 iclog = log->l_iclog;
1524 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001525 sv_destroy(&iclog->ic_force_wait);
1526 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001529 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 iclog = next_iclog;
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spinlock_destroy(&log->l_icloglock);
1533 spinlock_destroy(&log->l_grant_lock);
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001537 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001538} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540/*
1541 * Update counters atomically now that memcpy is done.
1542 */
1543/* ARGSUSED */
1544static inline void
1545xlog_state_finish_copy(xlog_t *log,
1546 xlog_in_core_t *iclog,
1547 int record_cnt,
1548 int copy_bytes)
1549{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001550 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001552 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 iclog->ic_offset += copy_bytes;
1554
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001555 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556} /* xlog_state_finish_copy */
1557
1558
1559
1560
1561/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001562 * print out info relating to regions written which consume
1563 * the reservation
1564 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001565STATIC void
1566xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1567{
1568 uint i;
1569 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1570
1571 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1572 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1573 "bformat",
1574 "bchunk",
1575 "efi_format",
1576 "efd_format",
1577 "iformat",
1578 "icore",
1579 "iext",
1580 "ibroot",
1581 "ilocal",
1582 "iattr_ext",
1583 "iattr_broot",
1584 "iattr_local",
1585 "qformat",
1586 "dquot",
1587 "quotaoff",
1588 "LR header",
1589 "unmount",
1590 "commit",
1591 "trans header"
1592 };
1593 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1594 "SETATTR_NOT_SIZE",
1595 "SETATTR_SIZE",
1596 "INACTIVE",
1597 "CREATE",
1598 "CREATE_TRUNC",
1599 "TRUNCATE_FILE",
1600 "REMOVE",
1601 "LINK",
1602 "RENAME",
1603 "MKDIR",
1604 "RMDIR",
1605 "SYMLINK",
1606 "SET_DMATTRS",
1607 "GROWFS",
1608 "STRAT_WRITE",
1609 "DIOSTRAT",
1610 "WRITE_SYNC",
1611 "WRITEID",
1612 "ADDAFORK",
1613 "ATTRINVAL",
1614 "ATRUNCATE",
1615 "ATTR_SET",
1616 "ATTR_RM",
1617 "ATTR_FLAG",
1618 "CLEAR_AGI_BUCKET",
1619 "QM_SBCHANGE",
1620 "DUMMY1",
1621 "DUMMY2",
1622 "QM_QUOTAOFF",
1623 "QM_DQALLOC",
1624 "QM_SETQLIM",
1625 "QM_DQCLUSTER",
1626 "QM_QINOCREATE",
1627 "QM_QUOTAOFF_END",
1628 "SB_UNIT",
1629 "FSYNC_TS",
1630 "GROWFSRT_ALLOC",
1631 "GROWFSRT_ZERO",
1632 "GROWFSRT_FREE",
1633 "SWAPEXT"
1634 };
1635
1636 xfs_fs_cmn_err(CE_WARN, mp,
1637 "xfs_log_write: reservation summary:\n"
1638 " trans type = %s (%u)\n"
1639 " unit res = %d bytes\n"
1640 " current res = %d bytes\n"
1641 " total reg = %u bytes (o/flow = %u bytes)\n"
1642 " ophdrs = %u (ophdr space = %u bytes)\n"
1643 " ophdr + reg = %u bytes\n"
1644 " num regions = %u\n",
1645 ((ticket->t_trans_type <= 0 ||
1646 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1647 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1648 ticket->t_trans_type,
1649 ticket->t_unit_res,
1650 ticket->t_curr_res,
1651 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1652 ticket->t_res_num_ophdrs, ophdr_spc,
1653 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001654 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001655 ticket->t_res_num);
1656
1657 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001658 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001659 cmn_err(CE_WARN,
1660 "region[%u]: %s - %u bytes\n",
1661 i,
1662 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1663 "bad-rtype" : res_type_str[r_type-1]),
1664 ticket->t_res_arr[i].r_len);
1665 }
1666}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001667
1668/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * Write some region out to in-core log
1670 *
1671 * This will be called when writing externally provided regions or when
1672 * writing out a commit record for a given transaction.
1673 *
1674 * General algorithm:
1675 * 1. Find total length of this write. This may include adding to the
1676 * lengths passed in.
1677 * 2. Check whether we violate the tickets reservation.
1678 * 3. While writing to this iclog
1679 * A. Reserve as much space in this iclog as can get
1680 * B. If this is first write, save away start lsn
1681 * C. While writing this region:
1682 * 1. If first write of transaction, write start record
1683 * 2. Write log operation header (header per region)
1684 * 3. Find out if we can fit entire region into this iclog
1685 * 4. Potentially, verify destination memcpy ptr
1686 * 5. Memcpy (partial) region
1687 * 6. If partial copy, release iclog; otherwise, continue
1688 * copying more regions into current iclog
1689 * 4. Mark want sync bit (in simulation mode)
1690 * 5. Release iclog for potential flush to on-disk log.
1691 *
1692 * ERRORS:
1693 * 1. Panic if reservation is overrun. This should never happen since
1694 * reservation amounts are generated internal to the filesystem.
1695 * NOTES:
1696 * 1. Tickets are single threaded data structures.
1697 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1698 * syncing routine. When a single log_write region needs to span
1699 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1700 * on all log operation writes which don't contain the end of the
1701 * region. The XLOG_END_TRANS bit is used for the in-core log
1702 * operation which contains the end of the continued log_write region.
1703 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1704 * we don't really know exactly how much space will be used. As a result,
1705 * we don't update ic_offset until the end when we know exactly how many
1706 * bytes have been written out.
1707 */
David Chinnera8272ce2007-11-23 16:28:09 +11001708STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709xlog_write(xfs_mount_t * mp,
1710 xfs_log_iovec_t reg[],
1711 int nentries,
1712 xfs_log_ticket_t tic,
1713 xfs_lsn_t *start_lsn,
1714 xlog_in_core_t **commit_iclog,
1715 uint flags)
1716{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001717 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001719 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 __psint_t ptr; /* copy address into data region */
1722 int len; /* # xlog_write() bytes 2 still copy */
1723 int index; /* region index currently copying */
1724 int log_offset; /* offset (from 0) into data region */
1725 int start_rec_copy; /* # bytes to copy for start record */
1726 int partial_copy; /* did we split a region? */
1727 int partial_copy_len;/* # bytes copied if split region */
1728 int need_copy; /* # bytes need to memcpy this region */
1729 int copy_len; /* # bytes actually memcpy'ing */
1730 int copy_off; /* # bytes from entry start */
1731 int contwr; /* continued write of in-core log? */
1732 int error;
1733 int record_cnt = 0, data_cnt = 0;
1734
1735 partial_copy_len = partial_copy = 0;
1736
1737 /* Calculate potential maximum space. Each region gets its own
1738 * xlog_op_header_t and may need to be double word aligned.
1739 */
1740 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001741 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001743 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 for (index = 0; index < nentries; index++) {
1747 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001748 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001750 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 contwr = *start_lsn = 0;
1753
1754 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001755 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756#ifdef DEBUG
1757 xlog_panic(
1758 "xfs_log_write: reservation ran out. Need to up reservation");
1759#else
1760 /* Customer configurable panic */
1761 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1762 "xfs_log_write: reservation ran out. Need to up reservation");
1763 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001764 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765#endif
1766 } else
1767 ticket->t_curr_res -= len;
1768
1769 for (index = 0; index < nentries; ) {
1770 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1771 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001772 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 ASSERT(log_offset <= iclog->ic_size - 1);
1775 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1776
1777 /* start_lsn is the first lsn written to. That's all we need. */
1778 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001779 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* This loop writes out as many regions as can fit in the amount
1782 * of space which was allocated by xlog_state_get_iclog_space().
1783 */
1784 while (index < nentries) {
1785 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1786 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1787 start_rec_copy = 0;
1788
1789 /* If first write for transaction, insert start record.
1790 * We can't be trying to commit if we are inited. We can't
1791 * have any "partial_copy" if we are inited.
1792 */
1793 if (ticket->t_flags & XLOG_TIC_INITED) {
1794 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001795 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 logop_head->oh_clientid = ticket->t_clientid;
1797 logop_head->oh_len = 0;
1798 logop_head->oh_flags = XLOG_START_TRANS;
1799 logop_head->oh_res2 = 0;
1800 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1801 record_cnt++;
1802
1803 start_rec_copy = sizeof(xlog_op_header_t);
1804 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1805 }
1806
1807 /* Copy log operation header directly into data section */
1808 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001809 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 logop_head->oh_clientid = ticket->t_clientid;
1811 logop_head->oh_res2 = 0;
1812
1813 /* header copied directly */
1814 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1815
1816 /* are we copying a commit or unmount record? */
1817 logop_head->oh_flags = flags;
1818
1819 /*
1820 * We've seen logs corrupted with bad transaction client
1821 * ids. This makes sure that XFS doesn't generate them on.
1822 * Turn this into an EIO and shut down the filesystem.
1823 */
1824 switch (logop_head->oh_clientid) {
1825 case XFS_TRANSACTION:
1826 case XFS_VOLUME:
1827 case XFS_LOG:
1828 break;
1829 default:
1830 xfs_fs_cmn_err(CE_WARN, mp,
1831 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1832 logop_head->oh_clientid, tic);
1833 return XFS_ERROR(EIO);
1834 }
1835
1836 /* Partial write last time? => (partial_copy != 0)
1837 * need_copy is the amount we'd like to copy if everything could
1838 * fit in the current memcpy.
1839 */
1840 need_copy = reg[index].i_len - partial_copy_len;
1841
1842 copy_off = partial_copy_len;
1843 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001844 copy_len = need_copy;
1845 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (partial_copy)
1847 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1848 partial_copy_len = partial_copy = 0;
1849 } else { /* partial write */
1850 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001851 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1853 if (partial_copy)
1854 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1855 partial_copy_len += copy_len;
1856 partial_copy++;
1857 len += sizeof(xlog_op_header_t); /* from splitting of region */
1858 /* account for new log op header */
1859 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001860 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862 xlog_verify_dest_ptr(log, ptr);
1863
1864 /* copy region */
1865 ASSERT(copy_len >= 0);
1866 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1867 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1868
1869 /* make copy_len total bytes copied, including headers */
1870 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1871 record_cnt++;
1872 data_cnt += contwr ? copy_len : 0;
1873 if (partial_copy) { /* copied partial region */
1874 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1875 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1876 record_cnt = data_cnt = 0;
1877 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001878 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 break; /* don't increment index */
1880 } else { /* copied entire region */
1881 index++;
1882 partial_copy_len = partial_copy = 0;
1883
1884 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1885 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1886 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001887 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001889 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (commit_iclog) {
1891 ASSERT(flags & XLOG_COMMIT_TRANS);
1892 *commit_iclog = iclog;
1893 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001894 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (index == nentries)
1896 return 0; /* we are done */
1897 else
1898 break;
1899 }
1900 } /* if (partial_copy) */
1901 } /* while (index < nentries) */
1902 } /* for (index = 0; index < nentries; ) */
1903 ASSERT(len == 0);
1904
1905 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1906 if (commit_iclog) {
1907 ASSERT(flags & XLOG_COMMIT_TRANS);
1908 *commit_iclog = iclog;
1909 return 0;
1910 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001911 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912} /* xlog_write */
1913
1914
1915/*****************************************************************************
1916 *
1917 * State Machine functions
1918 *
1919 *****************************************************************************
1920 */
1921
1922/* Clean iclogs starting from the head. This ordering must be
1923 * maintained, so an iclog doesn't become ACTIVE beyond one that
1924 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001925 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 * iclog is trying to sync to disk.
1927 *
1928 * State Change: DIRTY -> ACTIVE
1929 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001930STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931xlog_state_clean_log(xlog_t *log)
1932{
1933 xlog_in_core_t *iclog;
1934 int changed = 0;
1935
1936 iclog = log->l_iclog;
1937 do {
1938 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1939 iclog->ic_state = XLOG_STATE_ACTIVE;
1940 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001941 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /*
1943 * If the number of ops in this iclog indicate it just
1944 * contains the dummy transaction, we can
1945 * change state into IDLE (the second time around).
1946 * Otherwise we should change the state into
1947 * NEED a dummy.
1948 * We don't need to cover the dummy.
1949 */
1950 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001951 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1952 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 changed = 1;
1954 } else {
1955 /*
1956 * We have two dirty iclogs so start over
1957 * This could also be num of ops indicates
1958 * this is not the dummy going out.
1959 */
1960 changed = 2;
1961 }
1962 iclog->ic_header.h_num_logops = 0;
1963 memset(iclog->ic_header.h_cycle_data, 0,
1964 sizeof(iclog->ic_header.h_cycle_data));
1965 iclog->ic_header.h_lsn = 0;
1966 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1967 /* do nothing */;
1968 else
1969 break; /* stop cleaning */
1970 iclog = iclog->ic_next;
1971 } while (iclog != log->l_iclog);
1972
1973 /* log is locked when we are called */
1974 /*
1975 * Change state for the dummy log recording.
1976 * We usually go to NEED. But we go to NEED2 if the changed indicates
1977 * we are done writing the dummy record.
1978 * If we are done with the second dummy recored (DONE2), then
1979 * we go to IDLE.
1980 */
1981 if (changed) {
1982 switch (log->l_covered_state) {
1983 case XLOG_STATE_COVER_IDLE:
1984 case XLOG_STATE_COVER_NEED:
1985 case XLOG_STATE_COVER_NEED2:
1986 log->l_covered_state = XLOG_STATE_COVER_NEED;
1987 break;
1988
1989 case XLOG_STATE_COVER_DONE:
1990 if (changed == 1)
1991 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1992 else
1993 log->l_covered_state = XLOG_STATE_COVER_NEED;
1994 break;
1995
1996 case XLOG_STATE_COVER_DONE2:
1997 if (changed == 1)
1998 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1999 else
2000 log->l_covered_state = XLOG_STATE_COVER_NEED;
2001 break;
2002
2003 default:
2004 ASSERT(0);
2005 }
2006 }
2007} /* xlog_state_clean_log */
2008
2009STATIC xfs_lsn_t
2010xlog_get_lowest_lsn(
2011 xlog_t *log)
2012{
2013 xlog_in_core_t *lsn_log;
2014 xfs_lsn_t lowest_lsn, lsn;
2015
2016 lsn_log = log->l_iclog;
2017 lowest_lsn = 0;
2018 do {
2019 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002020 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if ((lsn && !lowest_lsn) ||
2022 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2023 lowest_lsn = lsn;
2024 }
2025 }
2026 lsn_log = lsn_log->ic_next;
2027 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002028 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
2031
2032STATIC void
2033xlog_state_do_callback(
2034 xlog_t *log,
2035 int aborted,
2036 xlog_in_core_t *ciclog)
2037{
2038 xlog_in_core_t *iclog;
2039 xlog_in_core_t *first_iclog; /* used to know when we've
2040 * processed all iclogs once */
2041 xfs_log_callback_t *cb, *cb_next;
2042 int flushcnt = 0;
2043 xfs_lsn_t lowest_lsn;
2044 int ioerrors; /* counter: iclogs with errors */
2045 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2046 int funcdidcallbacks; /* flag: function did callbacks */
2047 int repeats; /* for issuing console warnings if
2048 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002049 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002051 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 first_iclog = iclog = log->l_iclog;
2053 ioerrors = 0;
2054 funcdidcallbacks = 0;
2055 repeats = 0;
2056
2057 do {
2058 /*
2059 * Scan all iclogs starting with the one pointed to by the
2060 * log. Reset this starting point each time the log is
2061 * unlocked (during callbacks).
2062 *
2063 * Keep looping through iclogs until one full pass is made
2064 * without running any callbacks.
2065 */
2066 first_iclog = log->l_iclog;
2067 iclog = log->l_iclog;
2068 loopdidcallbacks = 0;
2069 repeats++;
2070
2071 do {
2072
2073 /* skip all iclogs in the ACTIVE & DIRTY states */
2074 if (iclog->ic_state &
2075 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2076 iclog = iclog->ic_next;
2077 continue;
2078 }
2079
2080 /*
2081 * Between marking a filesystem SHUTDOWN and stopping
2082 * the log, we do flush all iclogs to disk (if there
2083 * wasn't a log I/O error). So, we do want things to
2084 * go smoothly in case of just a SHUTDOWN w/o a
2085 * LOG_IO_ERROR.
2086 */
2087 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2088 /*
2089 * Can only perform callbacks in order. Since
2090 * this iclog is not in the DONE_SYNC/
2091 * DO_CALLBACK state, we skip the rest and
2092 * just try to clean up. If we set our iclog
2093 * to DO_CALLBACK, we will not process it when
2094 * we retry since a previous iclog is in the
2095 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002096 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 */
2098 if (!(iclog->ic_state &
2099 (XLOG_STATE_DONE_SYNC |
2100 XLOG_STATE_DO_CALLBACK))) {
2101 if (ciclog && (ciclog->ic_state ==
2102 XLOG_STATE_DONE_SYNC)) {
2103 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2104 }
2105 break;
2106 }
2107 /*
2108 * We now have an iclog that is in either the
2109 * DO_CALLBACK or DONE_SYNC states. The other
2110 * states (WANT_SYNC, SYNCING, or CALLBACK were
2111 * caught by the above if and are going to
2112 * clean (i.e. we aren't doing their callbacks)
2113 * see the above if.
2114 */
2115
2116 /*
2117 * We will do one more check here to see if we
2118 * have chased our tail around.
2119 */
2120
2121 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002122 if (lowest_lsn &&
2123 XFS_LSN_CMP(lowest_lsn,
2124 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 iclog = iclog->ic_next;
2126 continue; /* Leave this iclog for
2127 * another thread */
2128 }
2129
2130 iclog->ic_state = XLOG_STATE_CALLBACK;
2131
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002132 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002135 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 * No one else can be here except us.
2137 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002138 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002139 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2140 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2141 log->l_last_sync_lsn =
2142 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002143 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002146 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 ioerrors++;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Chinner114d23a2008-04-10 12:18:39 +10002150 /*
2151 * Keep processing entries in the callback list until
2152 * we come around and it is empty. We need to
2153 * atomically see that the list is empty and change the
2154 * state to DIRTY so that we don't miss any more
2155 * callbacks being added.
2156 */
2157 spin_lock(&iclog->ic_callback_lock);
2158 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002159 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 iclog->ic_callback_tail = &(iclog->ic_callback);
2161 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002162 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002165 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 cb_next = cb->cb_next;
2167 cb->cb_func(cb->cb_arg, aborted);
2168 }
David Chinner114d23a2008-04-10 12:18:39 +10002169 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 cb = iclog->ic_callback;
2171 }
2172
2173 loopdidcallbacks++;
2174 funcdidcallbacks++;
2175
David Chinner114d23a2008-04-10 12:18:39 +10002176 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002177 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002178 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2180 iclog->ic_state = XLOG_STATE_DIRTY;
2181
2182 /*
2183 * Transition from DIRTY to ACTIVE if applicable.
2184 * NOP if STATE_IOERROR.
2185 */
2186 xlog_state_clean_log(log);
2187
2188 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002189 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 iclog = iclog->ic_next;
2192 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002193
2194 if (repeats > 5000) {
2195 flushcnt += repeats;
2196 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002198 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002199 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 } while (!ioerrors && loopdidcallbacks);
2202
2203 /*
2204 * make one last gasp attempt to see if iclogs are being left in
2205 * limbo..
2206 */
2207#ifdef DEBUG
2208 if (funcdidcallbacks) {
2209 first_iclog = iclog = log->l_iclog;
2210 do {
2211 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2212 /*
2213 * Terminate the loop if iclogs are found in states
2214 * which will cause other threads to clean up iclogs.
2215 *
2216 * SYNCING - i/o completion will go through logs
2217 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002218 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 * IOERROR - give up hope all ye who enter here
2220 */
2221 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2222 iclog->ic_state == XLOG_STATE_SYNCING ||
2223 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2224 iclog->ic_state == XLOG_STATE_IOERROR )
2225 break;
2226 iclog = iclog->ic_next;
2227 } while (first_iclog != iclog);
2228 }
2229#endif
2230
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002231 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2232 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002233 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002234
2235 if (wake)
2236 sv_broadcast(&log->l_flush_wait);
2237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239
2240/*
2241 * Finish transitioning this iclog to the dirty state.
2242 *
2243 * Make sure that we completely execute this routine only when this is
2244 * the last call to the iclog. There is a good chance that iclog flushes,
2245 * when we reach the end of the physical log, get turned into 2 separate
2246 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2247 * routine. By using the reference count bwritecnt, we guarantee that only
2248 * the second completion goes through.
2249 *
2250 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002251 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 */
David Chinnera8272ce2007-11-23 16:28:09 +11002253STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254xlog_state_done_syncing(
2255 xlog_in_core_t *iclog,
2256 int aborted)
2257{
2258 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002260 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2263 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002264 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2266
2267
2268 /*
2269 * If we got an error, either on the first buffer, or in the case of
2270 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2271 * and none should ever be attempted to be written to disk
2272 * again.
2273 */
2274 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2275 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002276 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return;
2278 }
2279 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2280 }
2281
2282 /*
2283 * Someone could be sleeping prior to writing out the next
2284 * iclog buffer, we wake them all, one will get to do the
2285 * I/O, the others get to wait for the result.
2286 */
David Chinner12017fa2008-08-13 16:34:31 +10002287 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002288 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2290} /* xlog_state_done_syncing */
2291
2292
2293/*
2294 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002295 * sleep. We wait on the flush queue on the head iclog as that should be
2296 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2297 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 *
2299 * The in-core logs are used in a circular fashion. They are not used
2300 * out-of-order even when an iclog past the head is free.
2301 *
2302 * return:
2303 * * log_offset where xlog_write() can start writing into the in-core
2304 * log's data space.
2305 * * in-core log pointer to which xlog_write() should write.
2306 * * boolean indicating this is a continued write to an in-core log.
2307 * If this is the last write, then the in-core log's offset field
2308 * needs to be incremented, depending on the amount of data which
2309 * is copied.
2310 */
David Chinnera8272ce2007-11-23 16:28:09 +11002311STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312xlog_state_get_iclog_space(xlog_t *log,
2313 int len,
2314 xlog_in_core_t **iclogp,
2315 xlog_ticket_t *ticket,
2316 int *continued_write,
2317 int *logoffsetp)
2318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 int log_offset;
2320 xlog_rec_header_t *head;
2321 xlog_in_core_t *iclog;
2322 int error;
2323
2324restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002325 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002327 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return XFS_ERROR(EIO);
2329 }
2330
2331 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002332 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002334
2335 /* Wait for log writes to have flushed */
2336 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 goto restart;
2338 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 head = &iclog->ic_header;
2341
David Chinner155cc6b2008-03-06 13:44:14 +11002342 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 log_offset = iclog->ic_offset;
2344
2345 /* On the 1st write to an iclog, figure out lsn. This works
2346 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2347 * committing to. If the offset is set, that's how many blocks
2348 * must be written.
2349 */
2350 if (log_offset == 0) {
2351 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002352 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002353 log->l_iclog_hsize,
2354 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002355 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2356 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002357 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 ASSERT(log->l_curr_block >= 0);
2359 }
2360
2361 /* If there is enough room to write everything, then do it. Otherwise,
2362 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2363 * bit is on, so this will get flushed out. Don't update ic_offset
2364 * until you know exactly how many bytes get copied. Therefore, wait
2365 * until later to update ic_offset.
2366 *
2367 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2368 * can fit into remaining data section.
2369 */
2370 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2371 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2372
Dave Chinner49641f12008-07-11 17:43:55 +10002373 /*
2374 * If I'm the only one writing to this iclog, sync it to disk.
2375 * We need to do an atomic compare and decrement here to avoid
2376 * racing with concurrent atomic_dec_and_lock() calls in
2377 * xlog_state_release_iclog() when there is more than one
2378 * reference to the iclog.
2379 */
2380 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2381 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002382 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002383 error = xlog_state_release_iclog(log, iclog);
2384 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002385 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002387 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389 goto restart;
2390 }
2391
2392 /* Do we have enough room to write the full amount in the remainder
2393 * of this iclog? Or must we continue a write on the next iclog and
2394 * mark this iclog as completely taken? In the case where we switch
2395 * iclogs (to mark it taken), this particular iclog will release/sync
2396 * to disk in xlog_write().
2397 */
2398 if (len <= iclog->ic_size - iclog->ic_offset) {
2399 *continued_write = 0;
2400 iclog->ic_offset += len;
2401 } else {
2402 *continued_write = 1;
2403 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2404 }
2405 *iclogp = iclog;
2406
2407 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002408 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 *logoffsetp = log_offset;
2411 return 0;
2412} /* xlog_state_get_iclog_space */
2413
2414/*
2415 * Atomically get the log space required for a log ticket.
2416 *
2417 * Once a ticket gets put onto the reserveq, it will only return after
2418 * the needed reservation is satisfied.
2419 */
2420STATIC int
2421xlog_grant_log_space(xlog_t *log,
2422 xlog_ticket_t *tic)
2423{
2424 int free_bytes;
2425 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426#ifdef DEBUG
2427 xfs_lsn_t tail_lsn;
2428#endif
2429
2430
2431#ifdef DEBUG
2432 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2433 panic("grant Recovery problem");
2434#endif
2435
2436 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002437 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002438
2439 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 /* something is already sleeping; insert new transaction at end */
2442 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002443 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002444
2445 trace_xfs_log_grant_sleep1(log, tic);
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /*
2448 * Gotta check this before going to sleep, while we're
2449 * holding the grant lock.
2450 */
2451 if (XLOG_FORCED_SHUTDOWN(log))
2452 goto error_return;
2453
2454 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002455 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /*
2457 * If we got an error, and the filesystem is shutting down,
2458 * we'll catch it down below. So just continue...
2459 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002460 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002461 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2464 need_bytes = tic->t_unit_res*tic->t_ocnt;
2465 else
2466 need_bytes = tic->t_unit_res;
2467
2468redo:
2469 if (XLOG_FORCED_SHUTDOWN(log))
2470 goto error_return;
2471
2472 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2473 log->l_grant_reserve_bytes);
2474 if (free_bytes < need_bytes) {
2475 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002476 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002477
2478 trace_xfs_log_grant_sleep2(log, tic);
2479
Dave Chinner9d7fef72009-04-06 18:42:59 +02002480 spin_unlock(&log->l_grant_lock);
2481 xlog_grant_push_ail(log->l_mp, need_bytes);
2482 spin_lock(&log->l_grant_lock);
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002485 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Dave Chinner9d7fef72009-04-06 18:42:59 +02002487 spin_lock(&log->l_grant_lock);
2488 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002491 trace_xfs_log_grant_wake2(log, tic);
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 goto redo;
2494 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002495 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002498 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499#ifdef DEBUG
2500 tail_lsn = log->l_tail_lsn;
2501 /*
2502 * Check to make sure the grant write head didn't just over lap the
2503 * tail. If the cycles are the same, we can't be overlapping.
2504 * Otherwise, make sure that the cycles differ by exactly one and
2505 * check the byte count.
2506 */
2507 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2508 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2509 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2510 }
2511#endif
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002512 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002514 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return 0;
2516
2517 error_return:
2518 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002519 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002520
2521 trace_xfs_log_grant_error(log, tic);
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /*
2524 * If we are failing, make sure the ticket doesn't have any
2525 * current reservations. We don't want to add this back when
2526 * the ticket/transaction gets cancelled.
2527 */
2528 tic->t_curr_res = 0;
2529 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002530 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return XFS_ERROR(EIO);
2532} /* xlog_grant_log_space */
2533
2534
2535/*
2536 * Replenish the byte reservation required by moving the grant write head.
2537 *
2538 *
2539 */
2540STATIC int
2541xlog_regrant_write_log_space(xlog_t *log,
2542 xlog_ticket_t *tic)
2543{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 int free_bytes, need_bytes;
2545 xlog_ticket_t *ntic;
2546#ifdef DEBUG
2547 xfs_lsn_t tail_lsn;
2548#endif
2549
2550 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002551 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556#ifdef DEBUG
2557 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2558 panic("regrant Recovery problem");
2559#endif
2560
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002561 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002562
2563 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 if (XLOG_FORCED_SHUTDOWN(log))
2566 goto error_return;
2567
2568 /* If there are other waiters on the queue then give them a
2569 * chance at logspace before us. Wake up the first waiters,
2570 * if we do not wake up all the waiters then go to sleep waiting
2571 * for more free space, otherwise try to get some space for
2572 * this transaction.
2573 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002574 need_bytes = tic->t_unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if ((ntic = log->l_write_headq)) {
2576 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2577 log->l_grant_write_bytes);
2578 do {
2579 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2580
2581 if (free_bytes < ntic->t_unit_res)
2582 break;
2583 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002584 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 ntic = ntic->t_next;
2586 } while (ntic != log->l_write_headq);
2587
2588 if (ntic != log->l_write_headq) {
2589 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002590 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002592 trace_xfs_log_regrant_write_sleep1(log, tic);
2593
Dave Chinner9d7fef72009-04-06 18:42:59 +02002594 spin_unlock(&log->l_grant_lock);
2595 xlog_grant_push_ail(log->l_mp, need_bytes);
2596 spin_lock(&log->l_grant_lock);
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002599 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 &log->l_grant_lock, s);
2601
2602 /* If we're shutting down, this tic is already
2603 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002604 spin_lock(&log->l_grant_lock);
2605 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002608 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610 }
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612redo:
2613 if (XLOG_FORCED_SHUTDOWN(log))
2614 goto error_return;
2615
2616 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2617 log->l_grant_write_bytes);
2618 if (free_bytes < need_bytes) {
2619 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002620 xlog_ins_ticketq(&log->l_write_headq, tic);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002621 spin_unlock(&log->l_grant_lock);
2622 xlog_grant_push_ail(log->l_mp, need_bytes);
2623 spin_lock(&log->l_grant_lock);
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002626 trace_xfs_log_regrant_write_sleep2(log, tic);
2627
David Chinner12017fa2008-08-13 16:34:31 +10002628 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002631 spin_lock(&log->l_grant_lock);
2632 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002635 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 goto redo;
2637 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002638 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002640 /* we've got enough space */
2641 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642#ifdef DEBUG
2643 tail_lsn = log->l_tail_lsn;
2644 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2645 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2646 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2647 }
2648#endif
2649
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002650 trace_xfs_log_regrant_write_exit(log, tic);
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002653 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656
2657 error_return:
2658 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002659 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002660
2661 trace_xfs_log_regrant_write_error(log, tic);
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 /*
2664 * If we are failing, make sure the ticket doesn't have any
2665 * current reservations. We don't want to add this back when
2666 * the ticket/transaction gets cancelled.
2667 */
2668 tic->t_curr_res = 0;
2669 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002670 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return XFS_ERROR(EIO);
2672} /* xlog_regrant_write_log_space */
2673
2674
2675/* The first cnt-1 times through here we don't need to
2676 * move the grant write head because the permanent
2677 * reservation has reserved cnt times the unit amount.
2678 * Release part of current permanent unit reservation and
2679 * reset current reservation to be one units worth. Also
2680 * move grant reservation head forward.
2681 */
2682STATIC void
2683xlog_regrant_reserve_log_space(xlog_t *log,
2684 xlog_ticket_t *ticket)
2685{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002686 trace_xfs_log_regrant_reserve_enter(log, ticket);
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (ticket->t_cnt > 0)
2689 ticket->t_cnt--;
2690
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002691 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002692 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002694 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002695
2696 trace_xfs_log_regrant_reserve_sub(log, ticket);
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 xlog_verify_grant_head(log, 1);
2699
2700 /* just return if we still have some of the pre-reserved space */
2701 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002702 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return;
2704 }
2705
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002706 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002707
2708 trace_xfs_log_regrant_reserve_exit(log, ticket);
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002711 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002713 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714} /* xlog_regrant_reserve_log_space */
2715
2716
2717/*
2718 * Give back the space left from a reservation.
2719 *
2720 * All the information we need to make a correct determination of space left
2721 * is present. For non-permanent reservations, things are quite easy. The
2722 * count should have been decremented to zero. We only need to deal with the
2723 * space remaining in the current reservation part of the ticket. If the
2724 * ticket contains a permanent reservation, there may be left over space which
2725 * needs to be released. A count of N means that N-1 refills of the current
2726 * reservation can be done before we need to ask for more space. The first
2727 * one goes to fill up the first current reservation. Once we run out of
2728 * space, the count will stay at zero and the only space remaining will be
2729 * in the current reservation field.
2730 */
2731STATIC void
2732xlog_ungrant_log_space(xlog_t *log,
2733 xlog_ticket_t *ticket)
2734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 if (ticket->t_cnt > 0)
2736 ticket->t_cnt--;
2737
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002738 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002739 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002741 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002743 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 /* If this is a permanent reservation ticket, we may be able to free
2746 * up more space based on the remaining count.
2747 */
2748 if (ticket->t_cnt > 0) {
2749 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002750 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002753 trace_xfs_log_ungrant_exit(log, ticket);
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002756 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 xfs_log_move_tail(log->l_mp, 1);
2758} /* xlog_ungrant_log_space */
2759
2760
2761/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 * Flush iclog to disk if this is the last reference to the given iclog and
2763 * the WANT_SYNC bit is set.
2764 *
2765 * When this function is entered, the iclog is not necessarily in the
2766 * WANT_SYNC state. It may be sitting around waiting to get filled.
2767 *
2768 *
2769 */
David Chinnera8272ce2007-11-23 16:28:09 +11002770STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002771xlog_state_release_iclog(
2772 xlog_t *log,
2773 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 int sync = 0; /* do we sync? */
2776
David Chinner155cc6b2008-03-06 13:44:14 +11002777 if (iclog->ic_state & XLOG_STATE_IOERROR)
2778 return XFS_ERROR(EIO);
2779
2780 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2781 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2782 return 0;
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002785 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return XFS_ERROR(EIO);
2787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2789 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2790
David Chinner155cc6b2008-03-06 13:44:14 +11002791 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002792 /* update tail before writing to iclog */
2793 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 sync++;
2795 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002796 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2798 /* cycle incremented when incrementing curr_block */
2799 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002800 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 /*
2803 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002804 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2806 * this iclog has consistent data, so we ignore IOERROR
2807 * flags after this point.
2808 */
David Chinnerb5893342008-03-06 13:44:06 +11002809 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812} /* xlog_state_release_iclog */
2813
2814
2815/*
2816 * This routine will mark the current iclog in the ring as WANT_SYNC
2817 * and move the current iclog pointer to the next iclog in the ring.
2818 * When this routine is called from xlog_state_get_iclog_space(), the
2819 * exact size of the iclog has not yet been determined. All we know is
2820 * that every data block. We have run out of space in this log record.
2821 */
2822STATIC void
2823xlog_state_switch_iclogs(xlog_t *log,
2824 xlog_in_core_t *iclog,
2825 int eventual_size)
2826{
2827 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2828 if (!eventual_size)
2829 eventual_size = iclog->ic_offset;
2830 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002831 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 log->l_prev_block = log->l_curr_block;
2833 log->l_prev_cycle = log->l_curr_cycle;
2834
2835 /* roll log?: ic_offset changed later */
2836 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2837
2838 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002839 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 log->l_mp->m_sb.sb_logsunit > 1) {
2841 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2842 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2843 }
2844
2845 if (log->l_curr_block >= log->l_logBBsize) {
2846 log->l_curr_cycle++;
2847 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2848 log->l_curr_cycle++;
2849 log->l_curr_block -= log->l_logBBsize;
2850 ASSERT(log->l_curr_block >= 0);
2851 }
2852 ASSERT(iclog == log->l_iclog);
2853 log->l_iclog = iclog->ic_next;
2854} /* xlog_state_switch_iclogs */
2855
2856
2857/*
2858 * Write out all data in the in-core log as of this exact moment in time.
2859 *
2860 * Data may be written to the in-core log during this call. However,
2861 * we don't guarantee this data will be written out. A change from past
2862 * implementation means this routine will *not* write out zero length LRs.
2863 *
2864 * Basically, we try and perform an intelligent scan of the in-core logs.
2865 * If we determine there is no flushable data, we just return. There is no
2866 * flushable data if:
2867 *
2868 * 1. the current iclog is active and has no data; the previous iclog
2869 * is in the active or dirty state.
2870 * 2. the current iclog is drity, and the previous iclog is in the
2871 * active or dirty state.
2872 *
David Chinner12017fa2008-08-13 16:34:31 +10002873 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 *
2875 * 1. the current iclog is not in the active nor dirty state.
2876 * 2. the current iclog dirty, and the previous iclog is not in the
2877 * active nor dirty state.
2878 * 3. the current iclog is active, and there is another thread writing
2879 * to this particular iclog.
2880 * 4. a) the current iclog is active and has no other writers
2881 * b) when we return from flushing out this iclog, it is still
2882 * not in the active nor dirty state.
2883 */
2884STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002885xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 xlog_in_core_t *iclog;
2888 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002890 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 iclog = log->l_iclog;
2893 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002894 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return XFS_ERROR(EIO);
2896 }
2897
2898 /* If the head iclog is not active nor dirty, we just attach
2899 * ourselves to the head and go to sleep.
2900 */
2901 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2902 iclog->ic_state == XLOG_STATE_DIRTY) {
2903 /*
2904 * If the head is dirty or (active and empty), then
2905 * we need to look at the previous iclog. If the previous
2906 * iclog is active or dirty we are done. There is nothing
2907 * to sync out. Otherwise, we attach ourselves to the
2908 * previous iclog and go to sleep.
2909 */
2910 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002911 (atomic_read(&iclog->ic_refcnt) == 0
2912 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 iclog = iclog->ic_prev;
2914 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2915 iclog->ic_state == XLOG_STATE_DIRTY)
2916 goto no_sleep;
2917 else
2918 goto maybe_sleep;
2919 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002920 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 /* We are the only one with access to this
2922 * iclog. Flush it out now. There should
2923 * be a roundoff of zero to show that someone
2924 * has already taken care of the roundoff from
2925 * the previous sync.
2926 */
David Chinner155cc6b2008-03-06 13:44:14 +11002927 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002928 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002930 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 if (xlog_state_release_iclog(log, iclog))
2933 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002934 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002935 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002936 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 iclog->ic_state != XLOG_STATE_DIRTY)
2938 goto maybe_sleep;
2939 else
2940 goto no_sleep;
2941 } else {
2942 /* Someone else is writing to this iclog.
2943 * Use its call to flush out the data. However,
2944 * the other thread may not force out this LR,
2945 * so we mark it WANT_SYNC.
2946 */
2947 xlog_state_switch_iclogs(log, iclog, 0);
2948 goto maybe_sleep;
2949 }
2950 }
2951 }
2952
2953 /* By the time we come around again, the iclog could've been filled
2954 * which would give it another lsn. If we have a new lsn, just
2955 * return because the relevant data has been flushed.
2956 */
2957maybe_sleep:
2958 if (flags & XFS_LOG_SYNC) {
2959 /*
2960 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002961 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 * Then we check again after waking up, in case our
2963 * sleep was disturbed by a bad news.
2964 */
2965 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002966 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return XFS_ERROR(EIO);
2968 }
2969 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10002970 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 /*
2972 * No need to grab the log lock here since we're
2973 * only deciding whether or not to return EIO
2974 * and the memory read should be atomic.
2975 */
2976 if (iclog->ic_state & XLOG_STATE_IOERROR)
2977 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002978 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 } else {
2981
2982no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002983 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
2985 return 0;
2986} /* xlog_state_sync_all */
2987
2988
2989/*
2990 * Used by code which implements synchronous log forces.
2991 *
2992 * Find in-core log with lsn.
2993 * If it is in the DIRTY state, just return.
2994 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2995 * state and go to sleep or return.
2996 * If it is in any other state, go to sleep or return.
2997 *
2998 * If filesystem activity goes to zero, the iclog will get flushed only by
2999 * bdflush().
3000 */
David Chinnera8272ce2007-11-23 16:28:09 +11003001STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002xlog_state_sync(xlog_t *log,
3003 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003004 uint flags,
3005 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 xlog_in_core_t *iclog;
3008 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003011 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 iclog = log->l_iclog;
3013
3014 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003015 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return XFS_ERROR(EIO);
3017 }
3018
3019 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003020 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3021 iclog = iclog->ic_next;
3022 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
3024
3025 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003026 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return 0;
3028 }
3029
3030 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3031 /*
3032 * We sleep here if we haven't already slept (e.g.
3033 * this is the first time we've looked at the correct
3034 * iclog buf) and the buffer before us is going to
3035 * be sync'ed. The reason for this is that if we
3036 * are doing sync transactions here, by waiting for
3037 * the previous I/O to complete, we can allow a few
3038 * more transactions into this iclog before we close
3039 * it down.
3040 *
3041 * Otherwise, we mark the buffer WANT_SYNC, and bump
3042 * up the refcnt so we can release the log (which drops
3043 * the ref count). The state switch keeps new transaction
3044 * commits from using this buffer. When the current commits
3045 * finish writing into the buffer, the refcount will drop to
3046 * zero and the buffer will go out then.
3047 */
3048 if (!already_slept &&
3049 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3050 XLOG_STATE_SYNCING))) {
3051 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3052 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003053 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003055 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 already_slept = 1;
3057 goto try_again;
3058 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003059 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003061 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 if (xlog_state_release_iclog(log, iclog))
3063 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003064 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003065 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
3067 }
3068
3069 if ((flags & XFS_LOG_SYNC) && /* sleep */
3070 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3071
3072 /*
David Chinner12017fa2008-08-13 16:34:31 +10003073 * Don't wait on completion if we know that we've
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 * gotten a log write error.
3075 */
3076 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003077 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return XFS_ERROR(EIO);
3079 }
3080 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003081 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 /*
3083 * No need to grab the log lock here since we're
3084 * only deciding whether or not to return EIO
3085 * and the memory read should be atomic.
3086 */
3087 if (iclog->ic_state & XLOG_STATE_IOERROR)
3088 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003089 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003091 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 return 0;
3094
3095 } while (iclog != log->l_iclog);
3096
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003097 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099} /* xlog_state_sync */
3100
3101
3102/*
3103 * Called when we want to mark the current iclog as being ready to sync to
3104 * disk.
3105 */
David Chinnera8272ce2007-11-23 16:28:09 +11003106STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3108{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003109 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3112 xlog_state_switch_iclogs(log, iclog, 0);
3113 } else {
3114 ASSERT(iclog->ic_state &
3115 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3116 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119
3120/*****************************************************************************
3121 *
3122 * TICKET functions
3123 *
3124 *****************************************************************************
3125 */
3126
3127/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003128 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003130void
3131xfs_log_ticket_put(
3132 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003134 ASSERT(atomic_read(&ticket->t_ref) > 0);
3135 if (atomic_dec_and_test(&ticket->t_ref)) {
3136 sv_destroy(&ticket->t_wait);
3137 kmem_zone_free(xfs_log_ticket_zone, ticket);
3138 }
3139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003141xlog_ticket_t *
3142xfs_log_ticket_get(
3143 xlog_ticket_t *ticket)
3144{
3145 ASSERT(atomic_read(&ticket->t_ref) > 0);
3146 atomic_inc(&ticket->t_ref);
3147 return ticket;
3148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003151 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 */
David Chinnera8272ce2007-11-23 16:28:09 +11003153STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003154xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 int unit_bytes,
3156 int cnt,
3157 char client,
3158 uint xflags)
3159{
3160 xlog_ticket_t *tic;
3161 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
David Chinnereb01c9c2008-04-10 12:18:46 +10003163 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3164 if (!tic)
3165 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
3167 /*
3168 * Permanent reservations have up to 'cnt'-1 active log operations
3169 * in the log. A unit in this case is the amount of space for one
3170 * of these log operations. Normal reservations have a cnt of 1
3171 * and their unit amount is the total amount of space required.
3172 *
3173 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003174 * which occupy space in the on-disk log.
3175 *
3176 * Normal form of a transaction is:
3177 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3178 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3179 *
3180 * We need to account for all the leadup data and trailer data
3181 * around the transaction data.
3182 * And then we need to account for the worst case in terms of using
3183 * more space.
3184 * The worst case will happen if:
3185 * - the placement of the transaction happens to be such that the
3186 * roundoff is at its maximum
3187 * - the transaction data is synced before the commit record is synced
3188 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3189 * Therefore the commit record is in its own Log Record.
3190 * This can happen as the commit record is called with its
3191 * own region to xlog_write().
3192 * This then means that in the worst case, roundoff can happen for
3193 * the commit-rec as well.
3194 * The commit-rec is smaller than padding in this scenario and so it is
3195 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 */
3197
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003198 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003200 unit_bytes += sizeof(xfs_trans_header_t);
3201
3202 /* for start-rec */
3203 unit_bytes += sizeof(xlog_op_header_t);
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 /* for LR headers */
3206 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3207 unit_bytes += log->l_iclog_hsize * num_headers;
3208
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003209 /* for commit-rec LR header - note: padding will subsume the ophdr */
3210 unit_bytes += log->l_iclog_hsize;
3211
3212 /* for split-recs - ophdrs added when data split over LRs */
3213 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3214
3215 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003216 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003217 log->l_mp->m_sb.sb_logsunit > 1) {
3218 /* log su roundoff */
3219 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3220 } else {
3221 /* BB roundoff */
3222 unit_bytes += 2*BBSIZE;
3223 }
3224
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003225 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 tic->t_unit_res = unit_bytes;
3227 tic->t_curr_res = unit_bytes;
3228 tic->t_cnt = cnt;
3229 tic->t_ocnt = cnt;
3230 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3231 tic->t_clientid = client;
3232 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003233 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (xflags & XFS_LOG_PERM_RESERV)
3235 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003236 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Christoph Hellwig0adba532007-08-30 17:21:46 +10003238 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243
3244/******************************************************************************
3245 *
3246 * Log debug routines
3247 *
3248 ******************************************************************************
3249 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003250#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251/*
3252 * Make sure that the destination ptr is within the valid data region of
3253 * one of the iclogs. This uses backup pointers stored in a different
3254 * part of the log in case we trash the log structure.
3255 */
3256void
3257xlog_verify_dest_ptr(xlog_t *log,
3258 __psint_t ptr)
3259{
3260 int i;
3261 int good_ptr = 0;
3262
3263 for (i=0; i < log->l_iclog_bufs; i++) {
3264 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3265 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3266 good_ptr++;
3267 }
3268 if (! good_ptr)
3269 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3270} /* xlog_verify_dest_ptr */
3271
3272STATIC void
3273xlog_verify_grant_head(xlog_t *log, int equals)
3274{
3275 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3276 if (equals)
3277 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3278 else
3279 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3280 } else {
3281 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3282 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3283 }
3284} /* xlog_verify_grant_head */
3285
3286/* check if it will fit */
3287STATIC void
3288xlog_verify_tail_lsn(xlog_t *log,
3289 xlog_in_core_t *iclog,
3290 xfs_lsn_t tail_lsn)
3291{
3292 int blocks;
3293
3294 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3295 blocks =
3296 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3297 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3298 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3299 } else {
3300 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3301
3302 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3303 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3304
3305 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3306 if (blocks < BTOBB(iclog->ic_offset) + 1)
3307 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3308 }
3309} /* xlog_verify_tail_lsn */
3310
3311/*
3312 * Perform a number of checks on the iclog before writing to disk.
3313 *
3314 * 1. Make sure the iclogs are still circular
3315 * 2. Make sure we have a good magic number
3316 * 3. Make sure we don't have magic numbers in the data
3317 * 4. Check fields of each log operation header for:
3318 * A. Valid client identifier
3319 * B. tid ptr value falls in valid ptr space (user space code)
3320 * C. Length in log record header is correct according to the
3321 * individual operation headers within record.
3322 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3323 * log, check the preceding blocks of the physical log to make sure all
3324 * the cycle numbers agree with the current cycle number.
3325 */
3326STATIC void
3327xlog_verify_iclog(xlog_t *log,
3328 xlog_in_core_t *iclog,
3329 int count,
3330 boolean_t syncing)
3331{
3332 xlog_op_header_t *ophead;
3333 xlog_in_core_t *icptr;
3334 xlog_in_core_2_t *xhdr;
3335 xfs_caddr_t ptr;
3336 xfs_caddr_t base_ptr;
3337 __psint_t field_offset;
3338 __uint8_t clientid;
3339 int len, i, j, k, op_len;
3340 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003343 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 icptr = log->l_iclog;
3345 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003346 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 xlog_panic("xlog_verify_iclog: invalid ptr");
3348 icptr = icptr->ic_next;
3349 }
3350 if (icptr != log->l_iclog)
3351 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003352 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003355 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 xlog_panic("xlog_verify_iclog: invalid magic num");
3357
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003358 ptr = (xfs_caddr_t) &iclog->ic_header;
3359 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003361 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 xlog_panic("xlog_verify_iclog: unexpected magic num");
3363 }
3364
3365 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003366 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 ptr = iclog->ic_datap;
3368 base_ptr = ptr;
3369 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003370 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 for (i = 0; i < len; i++) {
3372 ophead = (xlog_op_header_t *)ptr;
3373
3374 /* clientid is only 1 byte */
3375 field_offset = (__psint_t)
3376 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3377 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3378 clientid = ophead->oh_clientid;
3379 } else {
3380 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3381 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3382 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3383 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003384 clientid = xlog_get_client_id(
3385 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003387 clientid = xlog_get_client_id(
3388 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390 }
3391 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003392 cmn_err(CE_WARN, "xlog_verify_iclog: "
3393 "invalid clientid %d op 0x%p offset 0x%lx",
3394 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
3396 /* check length */
3397 field_offset = (__psint_t)
3398 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3399 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003400 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 } else {
3402 idx = BTOBBT((__psint_t)&ophead->oh_len -
3403 (__psint_t)iclog->ic_datap);
3404 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3405 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3406 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003407 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003409 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 }
3411 }
3412 ptr += sizeof(xlog_op_header_t) + op_len;
3413 }
3414} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003415#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003418 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 */
3420STATIC int
3421xlog_state_ioerror(
3422 xlog_t *log)
3423{
3424 xlog_in_core_t *iclog, *ic;
3425
3426 iclog = log->l_iclog;
3427 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3428 /*
3429 * Mark all the incore logs IOERROR.
3430 * From now on, no log flushes will result.
3431 */
3432 ic = iclog;
3433 do {
3434 ic->ic_state = XLOG_STATE_IOERROR;
3435 ic = ic->ic_next;
3436 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 }
3439 /*
3440 * Return non-zero, if state transition has already happened.
3441 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003442 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443}
3444
3445/*
3446 * This is called from xfs_force_shutdown, when we're forcibly
3447 * shutting down the filesystem, typically because of an IO error.
3448 * Our main objectives here are to make sure that:
3449 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3450 * parties to find out, 'atomically'.
3451 * b. those who're sleeping on log reservations, pinned objects and
3452 * other resources get woken up, and be told the bad news.
3453 * c. nothing new gets queued up after (a) and (b) are done.
3454 * d. if !logerror, flush the iclogs to disk, then seal them off
3455 * for business.
3456 */
3457int
3458xfs_log_force_umount(
3459 struct xfs_mount *mp,
3460 int logerror)
3461{
3462 xlog_ticket_t *tic;
3463 xlog_t *log;
3464 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003465 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 log = mp->m_log;
3468
3469 /*
3470 * If this happens during log recovery, don't worry about
3471 * locking; the log isn't open for business yet.
3472 */
3473 if (!log ||
3474 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3475 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003476 if (mp->m_sb_bp)
3477 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 }
3480
3481 /*
3482 * Somebody could've already done the hard work for us.
3483 * No need to get locks for this.
3484 */
3485 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3486 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003487 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
3489 retval = 0;
3490 /*
3491 * We must hold both the GRANT lock and the LOG lock,
3492 * before we mark the filesystem SHUTDOWN and wake
3493 * everybody up to tell the bad news.
3494 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003495 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003496 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003498 if (mp->m_sb_bp)
3499 XFS_BUF_DONE(mp->m_sb_bp);
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 /*
3502 * This flag is sort of redundant because of the mount flag, but
3503 * it's good to maintain the separation between the log and the rest
3504 * of XFS.
3505 */
3506 log->l_flags |= XLOG_IO_ERROR;
3507
3508 /*
3509 * If we hit a log error, we want to mark all the iclogs IOERROR
3510 * while we're still holding the loglock.
3511 */
3512 if (logerror)
3513 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003514 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
3516 /*
3517 * We don't want anybody waiting for log reservations
3518 * after this. That means we have to wake up everybody
3519 * queued up on reserve_headq as well as write_headq.
3520 * In addition, we make sure in xlog_{re}grant_log_space
3521 * that we don't enqueue anything once the SHUTDOWN flag
3522 * is set, and this action is protected by the GRANTLOCK.
3523 */
3524 if ((tic = log->l_reserve_headq)) {
3525 do {
David Chinner12017fa2008-08-13 16:34:31 +10003526 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 tic = tic->t_next;
3528 } while (tic != log->l_reserve_headq);
3529 }
3530
3531 if ((tic = log->l_write_headq)) {
3532 do {
David Chinner12017fa2008-08-13 16:34:31 +10003533 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 tic = tic->t_next;
3535 } while (tic != log->l_write_headq);
3536 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003537 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3540 ASSERT(!logerror);
3541 /*
3542 * Force the incore logs to disk before shutting the
3543 * log down completely.
3544 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003545 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003546 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003548 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
3550 /*
3551 * Wake up everybody waiting on xfs_log_force.
3552 * Callback all log item committed functions as if the
3553 * log writes were completed.
3554 */
3555 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3556
3557#ifdef XFSERRORDEBUG
3558 {
3559 xlog_in_core_t *iclog;
3560
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003561 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 iclog = log->l_iclog;
3563 do {
3564 ASSERT(iclog->ic_callback == 0);
3565 iclog = iclog->ic_next;
3566 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003567 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
3569#endif
3570 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003571 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003574STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575xlog_iclogs_empty(xlog_t *log)
3576{
3577 xlog_in_core_t *iclog;
3578
3579 iclog = log->l_iclog;
3580 do {
3581 /* endianness does not matter here, zero is zero in
3582 * any language.
3583 */
3584 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 iclog = iclog->ic_next;
3587 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003588 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}