blob: 600b5b06aaebb405420524c1c6f50ed17e199ef3 [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 */
53STATIC int xlog_bdstrat_cb(struct xfs_buf *);
54STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
55 xlog_in_core_t **, xfs_lsn_t *);
56STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
57 xfs_buftarg_t *log_target,
58 xfs_daddr_t blk_offset,
59 int num_bblks);
60STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
61STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100062STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
64 int nentries, xfs_log_ticket_t tic,
65 xfs_lsn_t *start_lsn,
66 xlog_in_core_t **commit_iclog,
67 uint flags);
68
69/* local state machine functions */
70STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
71STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
72STATIC int xlog_state_get_iclog_space(xlog_t *log,
73 int len,
74 xlog_in_core_t **iclog,
75 xlog_ticket_t *ticket,
76 int *continued_write,
77 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078STATIC int xlog_state_release_iclog(xlog_t *log,
79 xlog_in_core_t *iclog);
80STATIC void xlog_state_switch_iclogs(xlog_t *log,
81 xlog_in_core_t *iclog,
82 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110083STATIC int xlog_state_sync(xlog_t *log,
84 xfs_lsn_t lsn,
85 uint flags,
86 int *log_flushed);
87STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
89
90/* local functions to manipulate grant head */
91STATIC int xlog_grant_log_space(xlog_t *log,
92 xlog_ticket_t *xtic);
93STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
94 int need_bytes);
95STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
96 xlog_ticket_t *ticket);
97STATIC int xlog_regrant_write_log_space(xlog_t *log,
98 xlog_ticket_t *ticket);
99STATIC void xlog_ungrant_log_space(xlog_t *log,
100 xlog_ticket_t *ticket);
101
102
103/* local ticket functions */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100104STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int unit_bytes,
106 int count,
107 char clientid,
108 uint flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100110#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
112STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
113STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
114 int count, boolean_t syncing);
115STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
116 xfs_lsn_t tail_lsn);
117#else
118#define xlog_verify_dest_ptr(a,b)
119#define xlog_verify_grant_head(a,b)
120#define xlog_verify_iclog(a,b,c,d)
121#define xlog_verify_tail_lsn(a,b,c)
122#endif
123
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000124STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100126
127static void
128xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
129{
130 if (*qp) {
131 tic->t_next = (*qp);
132 tic->t_prev = (*qp)->t_prev;
133 (*qp)->t_prev->t_next = tic;
134 (*qp)->t_prev = tic;
135 } else {
136 tic->t_prev = tic->t_next = tic;
137 *qp = tic;
138 }
139
140 tic->t_flags |= XLOG_TIC_IN_Q;
141}
142
143static void
144xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
145{
146 if (tic == tic->t_next) {
147 *qp = NULL;
148 } else {
149 *qp = tic->t_next;
150 tic->t_next->t_prev = tic->t_prev;
151 tic->t_prev->t_next = tic->t_next;
152 }
153
154 tic->t_next = tic->t_prev = NULL;
155 tic->t_flags &= ~XLOG_TIC_IN_Q;
156}
157
158static void
159xlog_grant_sub_space(struct log *log, int bytes)
160{
161 log->l_grant_write_bytes -= bytes;
162 if (log->l_grant_write_bytes < 0) {
163 log->l_grant_write_bytes += log->l_logsize;
164 log->l_grant_write_cycle--;
165 }
166
167 log->l_grant_reserve_bytes -= bytes;
168 if ((log)->l_grant_reserve_bytes < 0) {
169 log->l_grant_reserve_bytes += log->l_logsize;
170 log->l_grant_reserve_cycle--;
171 }
172
173}
174
175static void
176xlog_grant_add_space_write(struct log *log, int bytes)
177{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000178 int tmp = log->l_logsize - log->l_grant_write_bytes;
179 if (tmp > bytes)
180 log->l_grant_write_bytes += bytes;
181 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100182 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000183 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100184 }
185}
186
187static void
188xlog_grant_add_space_reserve(struct log *log, int bytes)
189{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000190 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
191 if (tmp > bytes)
192 log->l_grant_reserve_bytes += bytes;
193 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100194 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000195 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100196 }
197}
198
199static inline void
200xlog_grant_add_space(struct log *log, int bytes)
201{
202 xlog_grant_add_space_write(log, bytes);
203 xlog_grant_add_space_reserve(log, bytes);
204}
205
Christoph Hellwig0adba532007-08-30 17:21:46 +1000206static void
207xlog_tic_reset_res(xlog_ticket_t *tic)
208{
209 tic->t_res_num = 0;
210 tic->t_res_arr_sum = 0;
211 tic->t_res_num_ophdrs = 0;
212}
213
214static void
215xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
216{
217 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
218 /* add to overflow and start again */
219 tic->t_res_o_flow += tic->t_res_arr_sum;
220 tic->t_res_num = 0;
221 tic->t_res_arr_sum = 0;
222 }
223
224 tic->t_res_arr[tic->t_res_num].r_len = len;
225 tic->t_res_arr[tic->t_res_num].r_type = type;
226 tic->t_res_arr_sum += len;
227 tic->t_res_num++;
228}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
231 * NOTES:
232 *
233 * 1. currblock field gets updated at startup and after in-core logs
234 * marked as with WANT_SYNC.
235 */
236
237/*
238 * This routine is called when a user of a log manager ticket is done with
239 * the reservation. If the ticket was ever used, then a commit record for
240 * the associated transaction is written out as a log operation header with
241 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
242 * a given ticket. If the ticket was one with a permanent reservation, then
243 * a few operations are done differently. Permanent reservation tickets by
244 * default don't release the reservation. They just commit the current
245 * transaction with the belief that the reservation is still needed. A flag
246 * must be passed in before permanent reservations are actually released.
247 * When these type of tickets are not released, they need to be set into
248 * the inited state again. By doing this, a start record will be written
249 * out when the next write occurs.
250 */
251xfs_lsn_t
252xfs_log_done(xfs_mount_t *mp,
253 xfs_log_ticket_t xtic,
254 void **iclog,
255 uint flags)
256{
257 xlog_t *log = mp->m_log;
258 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
259 xfs_lsn_t lsn = 0;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (XLOG_FORCED_SHUTDOWN(log) ||
262 /*
263 * If nothing was ever written, don't write out commit record.
264 * If we get an error, just continue and give back the log ticket.
265 */
266 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
267 (xlog_commit_record(mp, ticket,
268 (xlog_in_core_t **)iclog, &lsn)))) {
269 lsn = (xfs_lsn_t) -1;
270 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
271 flags |= XFS_LOG_REL_PERM_RESERV;
272 }
273 }
274
275
276 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
277 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000278 trace_xfs_log_done_nonperm(log, ticket);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000281 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * request has been made to release a permanent reservation.
283 */
284 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100285 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000287 trace_xfs_log_done_perm(log, ticket);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000290 /* If this ticket was a permanent reservation and we aren't
291 * trying to release it, reset the inited flags; so next time
292 * we write, a start record will be written out.
293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 return lsn;
298} /* xfs_log_done */
299
300
301/*
302 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
303 * the force is done synchronously.
304 *
305 * Asynchronous forces are implemented by setting the WANT_SYNC
306 * bit in the appropriate in-core log and then returning.
307 *
David Chinner12017fa2008-08-13 16:34:31 +1000308 * Synchronous forces are implemented with a signal variable. All callers
309 * to force a given lsn to disk will wait on a the sv attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * specific in-core log. When given in-core log finally completes its
311 * write to disk, that thread will wake up all threads waiting on the
David Chinner12017fa2008-08-13 16:34:31 +1000312 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100315_xfs_log_force(
316 xfs_mount_t *mp,
317 xfs_lsn_t lsn,
318 uint flags,
319 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100321 xlog_t *log = mp->m_log;
322 int dummy;
323
324 if (!log_flushed)
325 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 ASSERT(flags & XFS_LOG_FORCE);
328
329 XFS_STATS_INC(xs_log_force);
330
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100331 if (log->l_flags & XLOG_IO_ERROR)
332 return XFS_ERROR(EIO);
333 if (lsn == 0)
334 return xlog_state_sync_all(log, flags, log_flushed);
335 else
336 return xlog_state_sync(log, lsn, flags, log_flushed);
David Chinnerb911ca02008-04-10 12:24:30 +1000337} /* _xfs_log_force */
338
339/*
340 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
341 * about errors or whether the log was flushed or not. This is the normal
342 * interface to use when trying to unpin items or move the log forward.
343 */
344void
345xfs_log_force(
346 xfs_mount_t *mp,
347 xfs_lsn_t lsn,
348 uint flags)
349{
350 int error;
351 error = _xfs_log_force(mp, lsn, flags, NULL);
352 if (error) {
353 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
354 "error %d returned.", error);
355 }
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/*
360 * Attaches a new iclog I/O completion callback routine during
361 * transaction commit. If the log is in error state, a non-zero
362 * return code is handed back and the caller is responsible for
363 * executing the callback at an appropriate time.
364 */
365int
366xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
367 void *iclog_hndl, /* iclog to hang callback off */
368 xfs_log_callback_t *cb)
369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000371 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Chinner114d23a2008-04-10 12:18:39 +1000373 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
375 if (!abortflg) {
376 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
377 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
378 cb->cb_next = NULL;
379 *(iclog->ic_callback_tail) = cb;
380 iclog->ic_callback_tail = &(cb->cb_next);
381 }
David Chinner114d23a2008-04-10 12:18:39 +1000382 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return abortflg;
384} /* xfs_log_notify */
385
386int
387xfs_log_release_iclog(xfs_mount_t *mp,
388 void *iclog_hndl)
389{
390 xlog_t *log = mp->m_log;
391 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
392
393 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000394 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100395 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 return 0;
399}
400
401/*
402 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
403 * to the reservation.
404 * 2. Potentially, push buffers at tail of log to disk.
405 *
406 * Each reservation is going to reserve extra space for a log record header.
407 * When writes happen to the on-disk log, we don't subtract the length of the
408 * log record header from any reservation. By wasting space in each
409 * reservation, we prevent over allocation problems.
410 */
411int
412xfs_log_reserve(xfs_mount_t *mp,
413 int unit_bytes,
414 int cnt,
415 xfs_log_ticket_t *ticket,
416 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000417 uint flags,
418 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 xlog_t *log = mp->m_log;
421 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100422 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
425 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
426
427 if (XLOG_FORCED_SHUTDOWN(log))
428 return XFS_ERROR(EIO);
429
430 XFS_STATS_INC(xs_try_logspace);
431
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (*ticket != NULL) {
434 ASSERT(flags & XFS_LOG_PERM_RESERV);
435 internal_ticket = (xlog_ticket_t *)*ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000436
437 trace_xfs_log_reserve(log, internal_ticket);
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
440 retval = xlog_regrant_write_log_space(log, internal_ticket);
441 } else {
442 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100443 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000445 if (!internal_ticket)
446 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000447 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000449
450 trace_xfs_log_reserve(log, internal_ticket);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 xlog_grant_push_ail(mp,
453 (internal_ticket->t_unit_res *
454 internal_ticket->t_cnt));
455 retval = xlog_grant_log_space(log, internal_ticket);
456 }
457
458 return retval;
459} /* xfs_log_reserve */
460
461
462/*
463 * Mount a log filesystem
464 *
465 * mp - ubiquitous xfs mount point structure
466 * log_target - buftarg of on-disk log device
467 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
468 * num_bblocks - Number of BBSIZE blocks in on-disk log
469 *
470 * Return error or zero.
471 */
472int
David Chinner249a8c12008-02-05 12:13:32 +1100473xfs_log_mount(
474 xfs_mount_t *mp,
475 xfs_buftarg_t *log_target,
476 xfs_daddr_t blk_offset,
477 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
David Chinner249a8c12008-02-05 12:13:32 +1100479 int error;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
482 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
483 else {
484 cmn_err(CE_NOTE,
485 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
486 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000487 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200491 if (IS_ERR(mp->m_log)) {
492 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100493 goto out;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
David Chinner249a8c12008-02-05 12:13:32 +1100497 * Initialize the AIL now we have a log.
498 */
David Chinner249a8c12008-02-05 12:13:32 +1100499 error = xfs_trans_ail_init(mp);
500 if (error) {
501 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100502 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100503 }
David Chinnera9c21c12008-10-30 17:39:35 +1100504 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100505
506 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * skip log recovery on a norecovery mount. pretend it all
508 * just worked.
509 */
510 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100511 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000514 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Eric Sandeen65be6052006-01-11 15:34:19 +1100516 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000519 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (error) {
521 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100522 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 }
525
526 /* Normal transactions can now occur */
527 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100530
531out_destroy_ail:
532 xfs_trans_ail_destroy(mp);
533out_free_log:
534 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100535out:
David Chinner249a8c12008-02-05 12:13:32 +1100536 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100537}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539/*
540 * Finish the recovery of the file system. This is separate from
541 * the xfs_log_mount() call, because it depends on the code in
542 * xfs_mountfs() to read in the root and real-time bitmap inodes
543 * between calling xfs_log_mount() and here.
544 *
545 * mp - ubiquitous xfs mount point structure
546 */
547int
Christoph Hellwig42490232008-08-13 16:49:32 +1000548xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 int error;
551
552 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000553 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 else {
555 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000556 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 return error;
560}
561
562/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * Final log writes as part of unmount.
564 *
565 * Mark the filesystem clean as unmount happens. Note that during relocation
566 * this routine needs to be executed as part of source-bag while the
567 * deallocation must not be done until source-end.
568 */
569
570/*
571 * Unmount record used to have a string "Unmount filesystem--" in the
572 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
573 * We just write the magic number now since that particular field isn't
574 * currently architecture converted and "nUmount" is a bit foo.
575 * As far as I know, there weren't any dependencies on the old behaviour.
576 */
577
578int
579xfs_log_unmount_write(xfs_mount_t *mp)
580{
581 xlog_t *log = mp->m_log;
582 xlog_in_core_t *iclog;
583#ifdef DEBUG
584 xlog_in_core_t *first_iclog;
585#endif
586 xfs_log_iovec_t reg[1];
587 xfs_log_ticket_t tic = NULL;
588 xfs_lsn_t lsn;
589 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /* the data section must be 32 bit size aligned */
592 struct {
593 __uint16_t magic;
594 __uint16_t pad1;
595 __uint32_t pad2; /* may as well make it 64 bits */
596 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Don't write out unmount record on read-only mounts.
600 * Or, if we are doing a forced umount (typically because of IO errors).
601 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000602 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604
David Chinnerb911ca02008-04-10 12:24:30 +1000605 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
606 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608#ifdef DEBUG
609 first_iclog = iclog = log->l_iclog;
610 do {
611 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
612 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
613 ASSERT(iclog->ic_offset == 0);
614 }
615 iclog = iclog->ic_next;
616 } while (iclog != first_iclog);
617#endif
618 if (! (XLOG_FORCED_SHUTDOWN(log))) {
619 reg[0].i_addr = (void*)&magic;
620 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000621 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Tim Shimmin955e47a2006-09-28 11:04:16 +1000623 error = xfs_log_reserve(mp, 600, 1, &tic,
624 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!error) {
626 /* remove inited flag */
627 ((xlog_ticket_t *)tic)->t_flags = 0;
628 error = xlog_write(mp, reg, 1, tic, &lsn,
629 NULL, XLOG_UNMOUNT_TRANS);
630 /*
631 * At this point, we're umounting anyway,
632 * so there's no point in transitioning log state
633 * to IOERROR. Just continue...
634 */
635 }
636
637 if (error) {
638 xfs_fs_cmn_err(CE_ALERT, mp,
639 "xfs_log_unmount: unmount record failed");
640 }
641
642
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000643 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100645 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100647 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000648 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000650 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
652 iclog->ic_state == XLOG_STATE_DIRTY)) {
653 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000654 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 &log->l_icloglock, s);
656 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000657 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000660 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000662 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000663 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000664 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100665 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 } else {
668 /*
669 * We're already in forced_shutdown mode, couldn't
670 * even attempt to write out the unmount transaction.
671 *
672 * Go through the motions of sync'ing and releasing
673 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000674 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 * be in progress. Do this as a separate section of
676 * code so we'll know if we ever get stuck here that
677 * we're in this odd situation of trying to unmount
678 * a file system that went into forced_shutdown as
679 * the result of an unmount..
680 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000681 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100683 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100686 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000687 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000689 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
692 || iclog->ic_state == XLOG_STATE_DIRTY
693 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
694
David Chinner12017fa2008-08-13 16:34:31 +1000695 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 &log->l_icloglock, s);
697 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000698 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700 }
701
David Chinner1bb7d6b2008-04-10 12:24:38 +1000702 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703} /* xfs_log_unmount_write */
704
705/*
706 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100707 *
708 * We need to stop the aild from running before we destroy
709 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
711void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100712xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
David Chinner249a8c12008-02-05 12:13:32 +1100714 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000715 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/*
719 * Write region vectors to log. The write happens using the space reservation
720 * of the ticket (tic). It is not a requirement that all writes for a given
721 * transaction occur with one call to xfs_log_write().
722 */
723int
724xfs_log_write(xfs_mount_t * mp,
725 xfs_log_iovec_t reg[],
726 int nentries,
727 xfs_log_ticket_t tic,
728 xfs_lsn_t *start_lsn)
729{
730 int error;
731 xlog_t *log = mp->m_log;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (XLOG_FORCED_SHUTDOWN(log))
734 return XFS_ERROR(EIO);
735
736 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000737 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100739 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740} /* xfs_log_write */
741
742
743void
744xfs_log_move_tail(xfs_mount_t *mp,
745 xfs_lsn_t tail_lsn)
746{
747 xlog_ticket_t *tic;
748 xlog_t *log = mp->m_log;
749 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (XLOG_FORCED_SHUTDOWN(log))
752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (tail_lsn == 0) {
755 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000756 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000758 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000761 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
764 * tail_lsn.
765 */
766 if (tail_lsn != 1) {
767 log->l_tail_lsn = tail_lsn;
768 }
769
770 if ((tic = log->l_write_headq)) {
771#ifdef DEBUG
772 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
773 panic("Recovery problem");
774#endif
775 cycle = log->l_grant_write_cycle;
776 bytes = log->l_grant_write_bytes;
777 free_bytes = xlog_space_left(log, cycle, bytes);
778 do {
779 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
780
781 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
782 break;
783 tail_lsn = 0;
784 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000785 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 tic = tic->t_next;
787 } while (tic != log->l_write_headq);
788 }
789 if ((tic = log->l_reserve_headq)) {
790#ifdef DEBUG
791 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
792 panic("Recovery problem");
793#endif
794 cycle = log->l_grant_reserve_cycle;
795 bytes = log->l_grant_reserve_bytes;
796 free_bytes = xlog_space_left(log, cycle, bytes);
797 do {
798 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
799 need_bytes = tic->t_unit_res*tic->t_cnt;
800 else
801 need_bytes = tic->t_unit_res;
802 if (free_bytes < need_bytes && tail_lsn != 1)
803 break;
804 tail_lsn = 0;
805 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000806 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 tic = tic->t_next;
808 } while (tic != log->l_reserve_headq);
809 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000810 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811} /* xfs_log_move_tail */
812
813/*
814 * Determine if we have a transaction that has gone to disk
815 * that needs to be covered. Log activity needs to be idle (no AIL and
816 * nothing in the iclogs). And, we need to be in the right state indicating
817 * something has gone out.
818 */
819int
820xfs_log_need_covered(xfs_mount_t *mp)
821{
David Chinner27d8d5f2008-10-30 17:38:39 +1100822 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
David Chinner92821e22007-05-24 15:26:31 +1000825 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000828 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
830 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
David Chinnera9c21c12008-10-30 17:39:35 +1100831 && !xfs_trans_ail_tail(log->l_ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 && xlog_iclogs_empty(log)) {
833 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
834 log->l_covered_state = XLOG_STATE_COVER_DONE;
835 else {
836 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
837 log->l_covered_state = XLOG_STATE_COVER_DONE2;
838 }
839 needed = 1;
840 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000841 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100842 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845/******************************************************************************
846 *
847 * local routines
848 *
849 ******************************************************************************
850 */
851
852/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
853 * The log manager must keep track of the last LR which was committed
854 * to disk. The lsn of this LR will become the new tail_lsn whenever
855 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
856 * the situation where stuff could be written into the log but nothing
857 * was ever in the AIL when asked. Eventually, we panic since the
858 * tail hits the head.
859 *
860 * We may be holding the log iclog lock upon entering this routine.
861 */
862xfs_lsn_t
863xlog_assign_tail_lsn(xfs_mount_t *mp)
864{
865 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 xlog_t *log = mp->m_log;
867
David Chinner5b00f142008-10-30 17:39:00 +1100868 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000869 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (tail_lsn != 0) {
871 log->l_tail_lsn = tail_lsn;
872 } else {
873 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
874 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000875 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 return tail_lsn;
878} /* xlog_assign_tail_lsn */
879
880
881/*
882 * Return the space in the log between the tail and the head. The head
883 * is passed in the cycle/bytes formal parms. In the special case where
884 * the reserve head has wrapped passed the tail, this calculation is no
885 * longer valid. In this case, just return 0 which means there is no space
886 * in the log. This works for all places where this function is called
887 * with the reserve head. Of course, if the write head were to ever
888 * wrap the tail, we should blow up. Rather than catch this case here,
889 * we depend on other ASSERTions in other parts of the code. XXXmiken
890 *
891 * This code also handles the case where the reservation head is behind
892 * the tail. The details of this case are described below, but the end
893 * result is that we return the size of the log as the amount of space left.
894 */
David Chinnera8272ce2007-11-23 16:28:09 +1100895STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896xlog_space_left(xlog_t *log, int cycle, int bytes)
897{
898 int free_bytes;
899 int tail_bytes;
900 int tail_cycle;
901
902 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
903 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
904 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
905 free_bytes = log->l_logsize - (bytes - tail_bytes);
906 } else if ((tail_cycle + 1) < cycle) {
907 return 0;
908 } else if (tail_cycle < cycle) {
909 ASSERT(tail_cycle == (cycle - 1));
910 free_bytes = tail_bytes - bytes;
911 } else {
912 /*
913 * The reservation head is behind the tail.
914 * In this case we just want to return the size of the
915 * log as the amount of space left.
916 */
917 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
918 "xlog_space_left: head behind tail\n"
919 " tail_cycle = %d, tail_bytes = %d\n"
920 " GH cycle = %d, GH bytes = %d",
921 tail_cycle, tail_bytes, cycle, bytes);
922 ASSERT(0);
923 free_bytes = log->l_logsize;
924 }
925 return free_bytes;
926} /* xlog_space_left */
927
928
929/*
930 * Log function which is called when an io completes.
931 *
932 * The log manager needs its own routine, in order to control what
933 * happens with the buffer after the write completes.
934 */
935void
936xlog_iodone(xfs_buf_t *bp)
937{
938 xlog_in_core_t *iclog;
939 xlog_t *l;
940 int aborted;
941
942 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
943 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
944 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
945 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 l = iclog->ic_log;
947
948 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100949 * If the _XFS_BARRIER_FAILED flag was set by a lower
950 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +1000951 * barrier I/O. Warn loudly and turn off barriers.
952 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100953 if (bp->b_flags & _XFS_BARRIER_FAILED) {
954 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000955 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
956 xfs_fs_cmn_err(CE_WARN, l->l_mp,
957 "xlog_iodone: Barriers are no longer supported"
958 " by device. Disabling barriers\n");
David Chinner0bfefc42007-05-14 18:24:23 +1000959 }
960
961 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * Race to shutdown the filesystem if we see an error.
963 */
964 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
965 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
966 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
967 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000968 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /*
970 * This flag will be propagated to the trans-committed
971 * callback routines to let them know that the log-commit
972 * didn't succeed.
973 */
974 aborted = XFS_LI_ABORTED;
975 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
976 aborted = XFS_LI_ABORTED;
977 }
David Chinner3db296f2007-05-14 18:24:16 +1000978
979 /* log I/O is always issued ASYNC */
980 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000982 /*
983 * do not reference the buffer (bp) here as we could race
984 * with it being freed after writing the unmount record to the
985 * log.
986 */
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988} /* xlog_iodone */
989
990/*
991 * The bdstrat callback function for log bufs. This gives us a central
992 * place to trap bufs in case we get hit by a log I/O error and need to
993 * shutdown. Actually, in practice, even when we didn't get a log error,
994 * we transition the iclogs to IOERROR state *after* flushing all existing
995 * iclogs to disk. This is because we don't want anymore new transactions to be
996 * started or completed afterwards.
997 */
998STATIC int
999xlog_bdstrat_cb(struct xfs_buf *bp)
1000{
1001 xlog_in_core_t *iclog;
1002
1003 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1004
1005 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1006 /* note for irix bstrat will need struct bdevsw passed
1007 * Fix the following macro if the code ever is merged
1008 */
1009 XFS_bdstrat(bp);
1010 return 0;
1011 }
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 XFS_BUF_ERROR(bp, EIO);
1014 XFS_BUF_STALE(bp);
1015 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001016 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/*
1020 * Return size of each in-core log record buffer.
1021 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001022 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 *
1024 * If the filesystem blocksize is too large, we may need to choose a
1025 * larger size since the directory code currently logs entire blocks.
1026 */
1027
1028STATIC void
1029xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1030 xlog_t *log)
1031{
1032 int size;
1033 int xhdrs;
1034
Eric Sandeen1cb51252007-08-16 16:24:43 +10001035 if (mp->m_logbufs <= 0)
1036 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1037 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001038 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /*
1041 * Buffer size passed in from mount system call.
1042 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001043 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 size = log->l_iclog_size = mp->m_logbsize;
1045 log->l_iclog_size_log = 0;
1046 while (size != 1) {
1047 log->l_iclog_size_log++;
1048 size >>= 1;
1049 }
1050
Eric Sandeen62118702008-03-06 13:44:28 +11001051 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001052 /* # headers = size / 32k
1053 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
1055
1056 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1057 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1058 xhdrs++;
1059 log->l_iclog_hsize = xhdrs << BBSHIFT;
1060 log->l_iclog_heads = xhdrs;
1061 } else {
1062 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1063 log->l_iclog_hsize = BBSIZE;
1064 log->l_iclog_heads = 1;
1065 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001066 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001069 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001070 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1071 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /* the default log size is 16k or 32k which is one header sector */
1074 log->l_iclog_hsize = BBSIZE;
1075 log->l_iclog_heads = 1;
1076
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001077done:
1078 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001079 if (mp->m_logbufs == 0)
1080 mp->m_logbufs = log->l_iclog_bufs;
1081 if (mp->m_logbsize == 0)
1082 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083} /* xlog_get_iclog_buffer_size */
1084
1085
1086/*
1087 * This routine initializes some of the log structure for a given mount point.
1088 * Its primary purpose is to fill in enough, so recovery can occur. However,
1089 * some other stuff may be filled in too.
1090 */
1091STATIC xlog_t *
1092xlog_alloc_log(xfs_mount_t *mp,
1093 xfs_buftarg_t *log_target,
1094 xfs_daddr_t blk_offset,
1095 int num_bblks)
1096{
1097 xlog_t *log;
1098 xlog_rec_header_t *head;
1099 xlog_in_core_t **iclogp;
1100 xlog_in_core_t *iclog, *prev_iclog=NULL;
1101 xfs_buf_t *bp;
1102 int i;
1103 int iclogsize;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001104 int error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Dave Chinner644c3562008-11-10 16:50:24 +11001106 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001107 if (!log) {
1108 xlog_warn("XFS: Log allocation failed: No memory!");
1109 goto out;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 log->l_mp = mp;
1113 log->l_targ = log_target;
1114 log->l_logsize = BBTOB(num_bblks);
1115 log->l_logBBstart = blk_offset;
1116 log->l_logBBsize = num_bblks;
1117 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1118 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1119
1120 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001121 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1123 log->l_last_sync_lsn = log->l_tail_lsn;
1124 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1125 log->l_grant_reserve_cycle = 1;
1126 log->l_grant_write_cycle = 1;
1127
Dave Chinnera6cb7672009-04-06 18:39:27 +02001128 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001129 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001131 if (log->l_sectbb_log < 0 ||
1132 log->l_sectbb_log > mp->m_sectbb_log) {
1133 xlog_warn("XFS: Log sector size (0x%x) out of range.",
1134 log->l_sectbb_log);
1135 goto out_free_log;
1136 }
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* for larger sector sizes, must have v2 or external log */
Dave Chinnera6cb7672009-04-06 18:39:27 +02001139 if (log->l_sectbb_log != 0 &&
1140 (log->l_logBBstart != 0 &&
1141 !xfs_sb_version_haslogv2(&mp->m_sb))) {
1142 xlog_warn("XFS: log sector size (0x%x) invalid "
1143 "for configuration.", log->l_sectbb_log);
1144 goto out_free_log;
1145 }
1146 if (mp->m_sb.sb_logsectlog < BBSHIFT) {
1147 xlog_warn("XFS: Log sector log (0x%x) too small.",
1148 mp->m_sb.sb_logsectlog);
1149 goto out_free_log;
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1153
1154 xlog_get_iclog_buffer_size(mp, log);
1155
Dave Chinnera6cb7672009-04-06 18:39:27 +02001156 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001158 if (!bp)
1159 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1161 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1162 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1163 ASSERT(XFS_BUF_ISBUSY(bp));
1164 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1165 log->l_xbuf = bp;
1166
Eric Sandeen007c61c2007-10-11 17:43:56 +10001167 spin_lock_init(&log->l_icloglock);
1168 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001169 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1172 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1173
1174 iclogp = &log->l_iclog;
1175 /*
1176 * The amount of memory to allocate for the iclog structure is
1177 * rather funky due to the way the structure is defined. It is
1178 * done this way so that we can use different sizes for machines
1179 * with different amounts of memory. See the definition of
1180 * xlog_in_core_t in xfs_log_priv.h for details.
1181 */
1182 iclogsize = log->l_iclog_size;
1183 ASSERT(log->l_iclog_size >= 4096);
1184 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001185 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1186 if (!*iclogp)
1187 goto out_free_iclog;
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 iclog->ic_prev = prev_iclog;
1191 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001192
1193 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001194 if (!bp)
1195 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001196 if (!XFS_BUF_CPSEMA(bp))
1197 ASSERT(0);
1198 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1199 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1200 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1201 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001202 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001203#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 head = &iclog->ic_header;
1207 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001208 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1209 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001210 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001211 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001213 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1217 iclog->ic_state = XLOG_STATE_ACTIVE;
1218 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001219 atomic_set(&iclog->ic_refcnt, 0);
1220 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001222 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1225 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001226 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1227 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 iclogp = &iclog->ic_next;
1230 }
1231 *iclogp = log->l_iclog; /* complete ring */
1232 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1233
1234 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001235
1236out_free_iclog:
1237 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1238 prev_iclog = iclog->ic_next;
1239 if (iclog->ic_bp) {
1240 sv_destroy(&iclog->ic_force_wait);
1241 sv_destroy(&iclog->ic_write_wait);
1242 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001243 }
1244 kmem_free(iclog);
1245 }
1246 spinlock_destroy(&log->l_icloglock);
1247 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001248 xfs_buf_free(log->l_xbuf);
1249out_free_log:
1250 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001251out:
1252 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253} /* xlog_alloc_log */
1254
1255
1256/*
1257 * Write out the commit record of a transaction associated with the given
1258 * ticket. Return the lsn of the commit record.
1259 */
1260STATIC int
1261xlog_commit_record(xfs_mount_t *mp,
1262 xlog_ticket_t *ticket,
1263 xlog_in_core_t **iclog,
1264 xfs_lsn_t *commitlsnp)
1265{
1266 int error;
1267 xfs_log_iovec_t reg[1];
1268
1269 reg[0].i_addr = NULL;
1270 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001271 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 ASSERT_ALWAYS(iclog);
1274 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1275 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001276 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001278 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279} /* xlog_commit_record */
1280
1281
1282/*
1283 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1284 * log space. This code pushes on the lsn which would supposedly free up
1285 * the 25% which we want to leave free. We may need to adopt a policy which
1286 * pushes on an lsn which is further along in the log once we reach the high
1287 * water mark. In this manner, we would be creating a low water mark.
1288 */
David Chinnera8272ce2007-11-23 16:28:09 +11001289STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290xlog_grant_push_ail(xfs_mount_t *mp,
1291 int need_bytes)
1292{
1293 xlog_t *log = mp->m_log; /* pointer to the log */
1294 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1295 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1296 int free_blocks; /* free blocks left to write to */
1297 int free_bytes; /* free bytes left to write to */
1298 int threshold_block; /* block in lsn we'd like to be at */
1299 int threshold_cycle; /* lsn cycle we'd like to be at */
1300 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1303
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001304 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 free_bytes = xlog_space_left(log,
1306 log->l_grant_reserve_cycle,
1307 log->l_grant_reserve_bytes);
1308 tail_lsn = log->l_tail_lsn;
1309 free_blocks = BTOBBT(free_bytes);
1310
1311 /*
1312 * Set the threshold for the minimum number of free blocks in the
1313 * log to the maximum of what the caller needs, one quarter of the
1314 * log, and 256 blocks.
1315 */
1316 free_threshold = BTOBB(need_bytes);
1317 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1318 free_threshold = MAX(free_threshold, 256);
1319 if (free_blocks < free_threshold) {
1320 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1321 threshold_cycle = CYCLE_LSN(tail_lsn);
1322 if (threshold_block >= log->l_logBBsize) {
1323 threshold_block -= log->l_logBBsize;
1324 threshold_cycle += 1;
1325 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001326 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /* Don't pass in an lsn greater than the lsn of the last
1329 * log record known to be on disk.
1330 */
1331 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1332 threshold_lsn = log->l_last_sync_lsn;
1333 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001334 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /*
1337 * Get the transaction layer to kick the dirty buffers out to
1338 * disk asynchronously. No point in trying to do this if
1339 * the filesystem is shutting down.
1340 */
1341 if (threshold_lsn &&
1342 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001343 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344} /* xlog_grant_push_ail */
1345
1346
1347/*
1348 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1349 * fashion. Previously, we should have moved the current iclog
1350 * ptr in the log to point to the next available iclog. This allows further
1351 * write to continue while this code syncs out an iclog ready to go.
1352 * Before an in-core log can be written out, the data section must be scanned
1353 * to save away the 1st word of each BBSIZE block into the header. We replace
1354 * it with the current cycle count. Each BBSIZE block is tagged with the
1355 * cycle count because there in an implicit assumption that drives will
1356 * guarantee that entire 512 byte blocks get written at once. In other words,
1357 * we can't have part of a 512 byte block written and part not written. By
1358 * tagging each block, we will know which blocks are valid when recovering
1359 * after an unclean shutdown.
1360 *
1361 * This routine is single threaded on the iclog. No other thread can be in
1362 * this routine with the same iclog. Changing contents of iclog can there-
1363 * fore be done without grabbing the state machine lock. Updating the global
1364 * log will require grabbing the lock though.
1365 *
1366 * The entire log manager uses a logical block numbering scheme. Only
1367 * log_sync (and then only bwrite()) know about the fact that the log may
1368 * not start with block zero on a given device. The log block start offset
1369 * is added immediately before calling bwrite().
1370 */
1371
David Chinnera8272ce2007-11-23 16:28:09 +11001372STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373xlog_sync(xlog_t *log,
1374 xlog_in_core_t *iclog)
1375{
1376 xfs_caddr_t dptr; /* pointer to byte sized element */
1377 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001378 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 uint count; /* byte count of bwrite */
1380 uint count_init; /* initial count before roundup */
1381 int roundoff; /* roundoff to BB or stripe */
1382 int split = 0; /* split write into two regions */
1383 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001384 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001387 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /* Add for LR header */
1390 count_init = log->l_iclog_hsize + iclog->ic_offset;
1391
1392 /* Round out the log write size */
1393 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1394 /* we have a v2 stripe unit to use */
1395 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1396 } else {
1397 count = BBTOB(BTOBB(count_init));
1398 }
1399 roundoff = count - count_init;
1400 ASSERT(roundoff >= 0);
1401 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1402 roundoff < log->l_mp->m_sb.sb_logsunit)
1403 ||
1404 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1405 roundoff < BBTOB(1)));
1406
1407 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001408 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001409 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001410 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* put cycle number in every block */
1413 xlog_pack_data(log, iclog, roundoff);
1414
1415 /* real byte length */
1416 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001417 iclog->ic_header.h_len =
1418 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001420 iclog->ic_header.h_len =
1421 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
Nathan Scottf5faad72006-07-28 17:04:44 +10001424 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1426 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001427 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1430
1431 /* Do we need to split this write into 2 parts? */
1432 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1433 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1434 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1435 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1436 } else {
1437 iclog->ic_bwritecnt = 1;
1438 }
David Chinner511105b2007-05-24 15:21:57 +10001439 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001441 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 XFS_BUF_BUSY(bp);
1443 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001444 bp->b_flags |= XBF_LOG_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001446 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001447 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001449 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001450 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1453 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1454
1455 xlog_verify_iclog(log, iclog, count, B_TRUE);
1456
1457 /* account for log which doesn't start at block #0 */
1458 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1459 /*
1460 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1461 * is shutting down.
1462 */
1463 XFS_BUF_WRITE(bp);
1464
1465 if ((error = XFS_bwrite(bp))) {
1466 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1467 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001468 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001471 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1473 (unsigned long)1);
1474 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1475 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1476 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1477 (__psint_t)count), split);
1478 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001479 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 XFS_BUF_BUSY(bp);
1481 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001482 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001483 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1484 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 dptr = XFS_BUF_PTR(bp);
1486 /*
1487 * Bump the cycle numbers at the start of each block
1488 * since this part of the buffer is at the start of
1489 * a new cycle. Watch out for the header magic number
1490 * case, though.
1491 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001492 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001493 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001494 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001495 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 dptr += BBSIZE;
1497 }
1498
1499 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1500 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1501
Nathan Scottc41564b2006-03-29 08:55:14 +10001502 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1504 XFS_BUF_WRITE(bp);
1505 if ((error = XFS_bwrite(bp))) {
1506 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1507 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001508 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512} /* xlog_sync */
1513
1514
1515/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001516 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
David Chinnera8272ce2007-11-23 16:28:09 +11001518STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001519xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int i;
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 iclog = log->l_iclog;
1525 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001526 sv_destroy(&iclog->ic_force_wait);
1527 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001530 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 iclog = next_iclog;
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 spinlock_destroy(&log->l_icloglock);
1534 spinlock_destroy(&log->l_grant_lock);
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001538 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001539} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541/*
1542 * Update counters atomically now that memcpy is done.
1543 */
1544/* ARGSUSED */
1545static inline void
1546xlog_state_finish_copy(xlog_t *log,
1547 xlog_in_core_t *iclog,
1548 int record_cnt,
1549 int copy_bytes)
1550{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001551 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001553 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 iclog->ic_offset += copy_bytes;
1555
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001556 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557} /* xlog_state_finish_copy */
1558
1559
1560
1561
1562/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001563 * print out info relating to regions written which consume
1564 * the reservation
1565 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001566STATIC void
1567xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1568{
1569 uint i;
1570 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1571
1572 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1573 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1574 "bformat",
1575 "bchunk",
1576 "efi_format",
1577 "efd_format",
1578 "iformat",
1579 "icore",
1580 "iext",
1581 "ibroot",
1582 "ilocal",
1583 "iattr_ext",
1584 "iattr_broot",
1585 "iattr_local",
1586 "qformat",
1587 "dquot",
1588 "quotaoff",
1589 "LR header",
1590 "unmount",
1591 "commit",
1592 "trans header"
1593 };
1594 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1595 "SETATTR_NOT_SIZE",
1596 "SETATTR_SIZE",
1597 "INACTIVE",
1598 "CREATE",
1599 "CREATE_TRUNC",
1600 "TRUNCATE_FILE",
1601 "REMOVE",
1602 "LINK",
1603 "RENAME",
1604 "MKDIR",
1605 "RMDIR",
1606 "SYMLINK",
1607 "SET_DMATTRS",
1608 "GROWFS",
1609 "STRAT_WRITE",
1610 "DIOSTRAT",
1611 "WRITE_SYNC",
1612 "WRITEID",
1613 "ADDAFORK",
1614 "ATTRINVAL",
1615 "ATRUNCATE",
1616 "ATTR_SET",
1617 "ATTR_RM",
1618 "ATTR_FLAG",
1619 "CLEAR_AGI_BUCKET",
1620 "QM_SBCHANGE",
1621 "DUMMY1",
1622 "DUMMY2",
1623 "QM_QUOTAOFF",
1624 "QM_DQALLOC",
1625 "QM_SETQLIM",
1626 "QM_DQCLUSTER",
1627 "QM_QINOCREATE",
1628 "QM_QUOTAOFF_END",
1629 "SB_UNIT",
1630 "FSYNC_TS",
1631 "GROWFSRT_ALLOC",
1632 "GROWFSRT_ZERO",
1633 "GROWFSRT_FREE",
1634 "SWAPEXT"
1635 };
1636
1637 xfs_fs_cmn_err(CE_WARN, mp,
1638 "xfs_log_write: reservation summary:\n"
1639 " trans type = %s (%u)\n"
1640 " unit res = %d bytes\n"
1641 " current res = %d bytes\n"
1642 " total reg = %u bytes (o/flow = %u bytes)\n"
1643 " ophdrs = %u (ophdr space = %u bytes)\n"
1644 " ophdr + reg = %u bytes\n"
1645 " num regions = %u\n",
1646 ((ticket->t_trans_type <= 0 ||
1647 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1648 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1649 ticket->t_trans_type,
1650 ticket->t_unit_res,
1651 ticket->t_curr_res,
1652 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1653 ticket->t_res_num_ophdrs, ophdr_spc,
1654 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001655 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001656 ticket->t_res_num);
1657
1658 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001659 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001660 cmn_err(CE_WARN,
1661 "region[%u]: %s - %u bytes\n",
1662 i,
1663 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1664 "bad-rtype" : res_type_str[r_type-1]),
1665 ticket->t_res_arr[i].r_len);
1666 }
1667}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001668
1669/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 * Write some region out to in-core log
1671 *
1672 * This will be called when writing externally provided regions or when
1673 * writing out a commit record for a given transaction.
1674 *
1675 * General algorithm:
1676 * 1. Find total length of this write. This may include adding to the
1677 * lengths passed in.
1678 * 2. Check whether we violate the tickets reservation.
1679 * 3. While writing to this iclog
1680 * A. Reserve as much space in this iclog as can get
1681 * B. If this is first write, save away start lsn
1682 * C. While writing this region:
1683 * 1. If first write of transaction, write start record
1684 * 2. Write log operation header (header per region)
1685 * 3. Find out if we can fit entire region into this iclog
1686 * 4. Potentially, verify destination memcpy ptr
1687 * 5. Memcpy (partial) region
1688 * 6. If partial copy, release iclog; otherwise, continue
1689 * copying more regions into current iclog
1690 * 4. Mark want sync bit (in simulation mode)
1691 * 5. Release iclog for potential flush to on-disk log.
1692 *
1693 * ERRORS:
1694 * 1. Panic if reservation is overrun. This should never happen since
1695 * reservation amounts are generated internal to the filesystem.
1696 * NOTES:
1697 * 1. Tickets are single threaded data structures.
1698 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1699 * syncing routine. When a single log_write region needs to span
1700 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1701 * on all log operation writes which don't contain the end of the
1702 * region. The XLOG_END_TRANS bit is used for the in-core log
1703 * operation which contains the end of the continued log_write region.
1704 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1705 * we don't really know exactly how much space will be used. As a result,
1706 * we don't update ic_offset until the end when we know exactly how many
1707 * bytes have been written out.
1708 */
David Chinnera8272ce2007-11-23 16:28:09 +11001709STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710xlog_write(xfs_mount_t * mp,
1711 xfs_log_iovec_t reg[],
1712 int nentries,
1713 xfs_log_ticket_t tic,
1714 xfs_lsn_t *start_lsn,
1715 xlog_in_core_t **commit_iclog,
1716 uint flags)
1717{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001718 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001720 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 __psint_t ptr; /* copy address into data region */
1723 int len; /* # xlog_write() bytes 2 still copy */
1724 int index; /* region index currently copying */
1725 int log_offset; /* offset (from 0) into data region */
1726 int start_rec_copy; /* # bytes to copy for start record */
1727 int partial_copy; /* did we split a region? */
1728 int partial_copy_len;/* # bytes copied if split region */
1729 int need_copy; /* # bytes need to memcpy this region */
1730 int copy_len; /* # bytes actually memcpy'ing */
1731 int copy_off; /* # bytes from entry start */
1732 int contwr; /* continued write of in-core log? */
1733 int error;
1734 int record_cnt = 0, data_cnt = 0;
1735
1736 partial_copy_len = partial_copy = 0;
1737
1738 /* Calculate potential maximum space. Each region gets its own
1739 * xlog_op_header_t and may need to be double word aligned.
1740 */
1741 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001742 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001744 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 for (index = 0; index < nentries; index++) {
1748 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001749 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001751 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753 contwr = *start_lsn = 0;
1754
1755 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001756 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757#ifdef DEBUG
1758 xlog_panic(
1759 "xfs_log_write: reservation ran out. Need to up reservation");
1760#else
1761 /* Customer configurable panic */
1762 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1763 "xfs_log_write: reservation ran out. Need to up reservation");
1764 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001765 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766#endif
1767 } else
1768 ticket->t_curr_res -= len;
1769
1770 for (index = 0; index < nentries; ) {
1771 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1772 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001773 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 ASSERT(log_offset <= iclog->ic_size - 1);
1776 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1777
1778 /* start_lsn is the first lsn written to. That's all we need. */
1779 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001780 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /* This loop writes out as many regions as can fit in the amount
1783 * of space which was allocated by xlog_state_get_iclog_space().
1784 */
1785 while (index < nentries) {
1786 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1787 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1788 start_rec_copy = 0;
1789
1790 /* If first write for transaction, insert start record.
1791 * We can't be trying to commit if we are inited. We can't
1792 * have any "partial_copy" if we are inited.
1793 */
1794 if (ticket->t_flags & XLOG_TIC_INITED) {
1795 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001796 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 logop_head->oh_clientid = ticket->t_clientid;
1798 logop_head->oh_len = 0;
1799 logop_head->oh_flags = XLOG_START_TRANS;
1800 logop_head->oh_res2 = 0;
1801 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1802 record_cnt++;
1803
1804 start_rec_copy = sizeof(xlog_op_header_t);
1805 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1806 }
1807
1808 /* Copy log operation header directly into data section */
1809 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001810 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 logop_head->oh_clientid = ticket->t_clientid;
1812 logop_head->oh_res2 = 0;
1813
1814 /* header copied directly */
1815 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1816
1817 /* are we copying a commit or unmount record? */
1818 logop_head->oh_flags = flags;
1819
1820 /*
1821 * We've seen logs corrupted with bad transaction client
1822 * ids. This makes sure that XFS doesn't generate them on.
1823 * Turn this into an EIO and shut down the filesystem.
1824 */
1825 switch (logop_head->oh_clientid) {
1826 case XFS_TRANSACTION:
1827 case XFS_VOLUME:
1828 case XFS_LOG:
1829 break;
1830 default:
1831 xfs_fs_cmn_err(CE_WARN, mp,
1832 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1833 logop_head->oh_clientid, tic);
1834 return XFS_ERROR(EIO);
1835 }
1836
1837 /* Partial write last time? => (partial_copy != 0)
1838 * need_copy is the amount we'd like to copy if everything could
1839 * fit in the current memcpy.
1840 */
1841 need_copy = reg[index].i_len - partial_copy_len;
1842
1843 copy_off = partial_copy_len;
1844 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001845 copy_len = need_copy;
1846 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (partial_copy)
1848 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1849 partial_copy_len = partial_copy = 0;
1850 } else { /* partial write */
1851 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001852 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1854 if (partial_copy)
1855 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1856 partial_copy_len += copy_len;
1857 partial_copy++;
1858 len += sizeof(xlog_op_header_t); /* from splitting of region */
1859 /* account for new log op header */
1860 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001861 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 xlog_verify_dest_ptr(log, ptr);
1864
1865 /* copy region */
1866 ASSERT(copy_len >= 0);
1867 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1868 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1869
1870 /* make copy_len total bytes copied, including headers */
1871 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1872 record_cnt++;
1873 data_cnt += contwr ? copy_len : 0;
1874 if (partial_copy) { /* copied partial region */
1875 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1876 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1877 record_cnt = data_cnt = 0;
1878 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001879 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 break; /* don't increment index */
1881 } else { /* copied entire region */
1882 index++;
1883 partial_copy_len = partial_copy = 0;
1884
1885 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1886 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1887 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001888 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001890 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (commit_iclog) {
1892 ASSERT(flags & XLOG_COMMIT_TRANS);
1893 *commit_iclog = iclog;
1894 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001895 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (index == nentries)
1897 return 0; /* we are done */
1898 else
1899 break;
1900 }
1901 } /* if (partial_copy) */
1902 } /* while (index < nentries) */
1903 } /* for (index = 0; index < nentries; ) */
1904 ASSERT(len == 0);
1905
1906 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1907 if (commit_iclog) {
1908 ASSERT(flags & XLOG_COMMIT_TRANS);
1909 *commit_iclog = iclog;
1910 return 0;
1911 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001912 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913} /* xlog_write */
1914
1915
1916/*****************************************************************************
1917 *
1918 * State Machine functions
1919 *
1920 *****************************************************************************
1921 */
1922
1923/* Clean iclogs starting from the head. This ordering must be
1924 * maintained, so an iclog doesn't become ACTIVE beyond one that
1925 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001926 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 * iclog is trying to sync to disk.
1928 *
1929 * State Change: DIRTY -> ACTIVE
1930 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001931STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932xlog_state_clean_log(xlog_t *log)
1933{
1934 xlog_in_core_t *iclog;
1935 int changed = 0;
1936
1937 iclog = log->l_iclog;
1938 do {
1939 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1940 iclog->ic_state = XLOG_STATE_ACTIVE;
1941 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001942 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /*
1944 * If the number of ops in this iclog indicate it just
1945 * contains the dummy transaction, we can
1946 * change state into IDLE (the second time around).
1947 * Otherwise we should change the state into
1948 * NEED a dummy.
1949 * We don't need to cover the dummy.
1950 */
1951 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001952 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1953 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 changed = 1;
1955 } else {
1956 /*
1957 * We have two dirty iclogs so start over
1958 * This could also be num of ops indicates
1959 * this is not the dummy going out.
1960 */
1961 changed = 2;
1962 }
1963 iclog->ic_header.h_num_logops = 0;
1964 memset(iclog->ic_header.h_cycle_data, 0,
1965 sizeof(iclog->ic_header.h_cycle_data));
1966 iclog->ic_header.h_lsn = 0;
1967 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1968 /* do nothing */;
1969 else
1970 break; /* stop cleaning */
1971 iclog = iclog->ic_next;
1972 } while (iclog != log->l_iclog);
1973
1974 /* log is locked when we are called */
1975 /*
1976 * Change state for the dummy log recording.
1977 * We usually go to NEED. But we go to NEED2 if the changed indicates
1978 * we are done writing the dummy record.
1979 * If we are done with the second dummy recored (DONE2), then
1980 * we go to IDLE.
1981 */
1982 if (changed) {
1983 switch (log->l_covered_state) {
1984 case XLOG_STATE_COVER_IDLE:
1985 case XLOG_STATE_COVER_NEED:
1986 case XLOG_STATE_COVER_NEED2:
1987 log->l_covered_state = XLOG_STATE_COVER_NEED;
1988 break;
1989
1990 case XLOG_STATE_COVER_DONE:
1991 if (changed == 1)
1992 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1993 else
1994 log->l_covered_state = XLOG_STATE_COVER_NEED;
1995 break;
1996
1997 case XLOG_STATE_COVER_DONE2:
1998 if (changed == 1)
1999 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2000 else
2001 log->l_covered_state = XLOG_STATE_COVER_NEED;
2002 break;
2003
2004 default:
2005 ASSERT(0);
2006 }
2007 }
2008} /* xlog_state_clean_log */
2009
2010STATIC xfs_lsn_t
2011xlog_get_lowest_lsn(
2012 xlog_t *log)
2013{
2014 xlog_in_core_t *lsn_log;
2015 xfs_lsn_t lowest_lsn, lsn;
2016
2017 lsn_log = log->l_iclog;
2018 lowest_lsn = 0;
2019 do {
2020 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002021 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if ((lsn && !lowest_lsn) ||
2023 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2024 lowest_lsn = lsn;
2025 }
2026 }
2027 lsn_log = lsn_log->ic_next;
2028 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002029 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032
2033STATIC void
2034xlog_state_do_callback(
2035 xlog_t *log,
2036 int aborted,
2037 xlog_in_core_t *ciclog)
2038{
2039 xlog_in_core_t *iclog;
2040 xlog_in_core_t *first_iclog; /* used to know when we've
2041 * processed all iclogs once */
2042 xfs_log_callback_t *cb, *cb_next;
2043 int flushcnt = 0;
2044 xfs_lsn_t lowest_lsn;
2045 int ioerrors; /* counter: iclogs with errors */
2046 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2047 int funcdidcallbacks; /* flag: function did callbacks */
2048 int repeats; /* for issuing console warnings if
2049 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002050 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002052 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 first_iclog = iclog = log->l_iclog;
2054 ioerrors = 0;
2055 funcdidcallbacks = 0;
2056 repeats = 0;
2057
2058 do {
2059 /*
2060 * Scan all iclogs starting with the one pointed to by the
2061 * log. Reset this starting point each time the log is
2062 * unlocked (during callbacks).
2063 *
2064 * Keep looping through iclogs until one full pass is made
2065 * without running any callbacks.
2066 */
2067 first_iclog = log->l_iclog;
2068 iclog = log->l_iclog;
2069 loopdidcallbacks = 0;
2070 repeats++;
2071
2072 do {
2073
2074 /* skip all iclogs in the ACTIVE & DIRTY states */
2075 if (iclog->ic_state &
2076 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2077 iclog = iclog->ic_next;
2078 continue;
2079 }
2080
2081 /*
2082 * Between marking a filesystem SHUTDOWN and stopping
2083 * the log, we do flush all iclogs to disk (if there
2084 * wasn't a log I/O error). So, we do want things to
2085 * go smoothly in case of just a SHUTDOWN w/o a
2086 * LOG_IO_ERROR.
2087 */
2088 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2089 /*
2090 * Can only perform callbacks in order. Since
2091 * this iclog is not in the DONE_SYNC/
2092 * DO_CALLBACK state, we skip the rest and
2093 * just try to clean up. If we set our iclog
2094 * to DO_CALLBACK, we will not process it when
2095 * we retry since a previous iclog is in the
2096 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002097 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 */
2099 if (!(iclog->ic_state &
2100 (XLOG_STATE_DONE_SYNC |
2101 XLOG_STATE_DO_CALLBACK))) {
2102 if (ciclog && (ciclog->ic_state ==
2103 XLOG_STATE_DONE_SYNC)) {
2104 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2105 }
2106 break;
2107 }
2108 /*
2109 * We now have an iclog that is in either the
2110 * DO_CALLBACK or DONE_SYNC states. The other
2111 * states (WANT_SYNC, SYNCING, or CALLBACK were
2112 * caught by the above if and are going to
2113 * clean (i.e. we aren't doing their callbacks)
2114 * see the above if.
2115 */
2116
2117 /*
2118 * We will do one more check here to see if we
2119 * have chased our tail around.
2120 */
2121
2122 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002123 if (lowest_lsn &&
2124 XFS_LSN_CMP(lowest_lsn,
2125 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 iclog = iclog->ic_next;
2127 continue; /* Leave this iclog for
2128 * another thread */
2129 }
2130
2131 iclog->ic_state = XLOG_STATE_CALLBACK;
2132
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002133 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002136 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 * No one else can be here except us.
2138 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002139 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002140 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2141 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2142 log->l_last_sync_lsn =
2143 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002144 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002147 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 ioerrors++;
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
David Chinner114d23a2008-04-10 12:18:39 +10002151 /*
2152 * Keep processing entries in the callback list until
2153 * we come around and it is empty. We need to
2154 * atomically see that the list is empty and change the
2155 * state to DIRTY so that we don't miss any more
2156 * callbacks being added.
2157 */
2158 spin_lock(&iclog->ic_callback_lock);
2159 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002160 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 iclog->ic_callback_tail = &(iclog->ic_callback);
2162 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002163 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002166 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 cb_next = cb->cb_next;
2168 cb->cb_func(cb->cb_arg, aborted);
2169 }
David Chinner114d23a2008-04-10 12:18:39 +10002170 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 cb = iclog->ic_callback;
2172 }
2173
2174 loopdidcallbacks++;
2175 funcdidcallbacks++;
2176
David Chinner114d23a2008-04-10 12:18:39 +10002177 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002178 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002179 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2181 iclog->ic_state = XLOG_STATE_DIRTY;
2182
2183 /*
2184 * Transition from DIRTY to ACTIVE if applicable.
2185 * NOP if STATE_IOERROR.
2186 */
2187 xlog_state_clean_log(log);
2188
2189 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002190 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 iclog = iclog->ic_next;
2193 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002194
2195 if (repeats > 5000) {
2196 flushcnt += repeats;
2197 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002199 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002200 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202 } while (!ioerrors && loopdidcallbacks);
2203
2204 /*
2205 * make one last gasp attempt to see if iclogs are being left in
2206 * limbo..
2207 */
2208#ifdef DEBUG
2209 if (funcdidcallbacks) {
2210 first_iclog = iclog = log->l_iclog;
2211 do {
2212 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2213 /*
2214 * Terminate the loop if iclogs are found in states
2215 * which will cause other threads to clean up iclogs.
2216 *
2217 * SYNCING - i/o completion will go through logs
2218 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002219 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 * IOERROR - give up hope all ye who enter here
2221 */
2222 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2223 iclog->ic_state == XLOG_STATE_SYNCING ||
2224 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2225 iclog->ic_state == XLOG_STATE_IOERROR )
2226 break;
2227 iclog = iclog->ic_next;
2228 } while (first_iclog != iclog);
2229 }
2230#endif
2231
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002232 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2233 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002234 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002235
2236 if (wake)
2237 sv_broadcast(&log->l_flush_wait);
2238}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240
2241/*
2242 * Finish transitioning this iclog to the dirty state.
2243 *
2244 * Make sure that we completely execute this routine only when this is
2245 * the last call to the iclog. There is a good chance that iclog flushes,
2246 * when we reach the end of the physical log, get turned into 2 separate
2247 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2248 * routine. By using the reference count bwritecnt, we guarantee that only
2249 * the second completion goes through.
2250 *
2251 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002252 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 */
David Chinnera8272ce2007-11-23 16:28:09 +11002254STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255xlog_state_done_syncing(
2256 xlog_in_core_t *iclog,
2257 int aborted)
2258{
2259 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002261 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2264 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002265 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2267
2268
2269 /*
2270 * If we got an error, either on the first buffer, or in the case of
2271 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2272 * and none should ever be attempted to be written to disk
2273 * again.
2274 */
2275 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2276 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002277 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return;
2279 }
2280 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2281 }
2282
2283 /*
2284 * Someone could be sleeping prior to writing out the next
2285 * iclog buffer, we wake them all, one will get to do the
2286 * I/O, the others get to wait for the result.
2287 */
David Chinner12017fa2008-08-13 16:34:31 +10002288 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002289 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2291} /* xlog_state_done_syncing */
2292
2293
2294/*
2295 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002296 * sleep. We wait on the flush queue on the head iclog as that should be
2297 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2298 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 *
2300 * The in-core logs are used in a circular fashion. They are not used
2301 * out-of-order even when an iclog past the head is free.
2302 *
2303 * return:
2304 * * log_offset where xlog_write() can start writing into the in-core
2305 * log's data space.
2306 * * in-core log pointer to which xlog_write() should write.
2307 * * boolean indicating this is a continued write to an in-core log.
2308 * If this is the last write, then the in-core log's offset field
2309 * needs to be incremented, depending on the amount of data which
2310 * is copied.
2311 */
David Chinnera8272ce2007-11-23 16:28:09 +11002312STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313xlog_state_get_iclog_space(xlog_t *log,
2314 int len,
2315 xlog_in_core_t **iclogp,
2316 xlog_ticket_t *ticket,
2317 int *continued_write,
2318 int *logoffsetp)
2319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 int log_offset;
2321 xlog_rec_header_t *head;
2322 xlog_in_core_t *iclog;
2323 int error;
2324
2325restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002326 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002328 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return XFS_ERROR(EIO);
2330 }
2331
2332 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002333 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002335
2336 /* Wait for log writes to have flushed */
2337 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto restart;
2339 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 head = &iclog->ic_header;
2342
David Chinner155cc6b2008-03-06 13:44:14 +11002343 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 log_offset = iclog->ic_offset;
2345
2346 /* On the 1st write to an iclog, figure out lsn. This works
2347 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2348 * committing to. If the offset is set, that's how many blocks
2349 * must be written.
2350 */
2351 if (log_offset == 0) {
2352 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002353 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002354 log->l_iclog_hsize,
2355 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002356 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2357 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002358 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 ASSERT(log->l_curr_block >= 0);
2360 }
2361
2362 /* If there is enough room to write everything, then do it. Otherwise,
2363 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2364 * bit is on, so this will get flushed out. Don't update ic_offset
2365 * until you know exactly how many bytes get copied. Therefore, wait
2366 * until later to update ic_offset.
2367 *
2368 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2369 * can fit into remaining data section.
2370 */
2371 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2372 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2373
Dave Chinner49641f12008-07-11 17:43:55 +10002374 /*
2375 * If I'm the only one writing to this iclog, sync it to disk.
2376 * We need to do an atomic compare and decrement here to avoid
2377 * racing with concurrent atomic_dec_and_lock() calls in
2378 * xlog_state_release_iclog() when there is more than one
2379 * reference to the iclog.
2380 */
2381 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2382 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002383 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002384 error = xlog_state_release_iclog(log, iclog);
2385 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002386 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002388 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390 goto restart;
2391 }
2392
2393 /* Do we have enough room to write the full amount in the remainder
2394 * of this iclog? Or must we continue a write on the next iclog and
2395 * mark this iclog as completely taken? In the case where we switch
2396 * iclogs (to mark it taken), this particular iclog will release/sync
2397 * to disk in xlog_write().
2398 */
2399 if (len <= iclog->ic_size - iclog->ic_offset) {
2400 *continued_write = 0;
2401 iclog->ic_offset += len;
2402 } else {
2403 *continued_write = 1;
2404 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2405 }
2406 *iclogp = iclog;
2407
2408 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002409 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 *logoffsetp = log_offset;
2412 return 0;
2413} /* xlog_state_get_iclog_space */
2414
2415/*
2416 * Atomically get the log space required for a log ticket.
2417 *
2418 * Once a ticket gets put onto the reserveq, it will only return after
2419 * the needed reservation is satisfied.
2420 */
2421STATIC int
2422xlog_grant_log_space(xlog_t *log,
2423 xlog_ticket_t *tic)
2424{
2425 int free_bytes;
2426 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427#ifdef DEBUG
2428 xfs_lsn_t tail_lsn;
2429#endif
2430
2431
2432#ifdef DEBUG
2433 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2434 panic("grant Recovery problem");
2435#endif
2436
2437 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002438 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002439
2440 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /* something is already sleeping; insert new transaction at end */
2443 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002444 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002445
2446 trace_xfs_log_grant_sleep1(log, tic);
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /*
2449 * Gotta check this before going to sleep, while we're
2450 * holding the grant lock.
2451 */
2452 if (XLOG_FORCED_SHUTDOWN(log))
2453 goto error_return;
2454
2455 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002456 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /*
2458 * If we got an error, and the filesystem is shutting down,
2459 * we'll catch it down below. So just continue...
2460 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002461 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002462 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2465 need_bytes = tic->t_unit_res*tic->t_ocnt;
2466 else
2467 need_bytes = tic->t_unit_res;
2468
2469redo:
2470 if (XLOG_FORCED_SHUTDOWN(log))
2471 goto error_return;
2472
2473 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2474 log->l_grant_reserve_bytes);
2475 if (free_bytes < need_bytes) {
2476 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002477 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002478
2479 trace_xfs_log_grant_sleep2(log, tic);
2480
Dave Chinner9d7fef72009-04-06 18:42:59 +02002481 spin_unlock(&log->l_grant_lock);
2482 xlog_grant_push_ail(log->l_mp, need_bytes);
2483 spin_lock(&log->l_grant_lock);
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002486 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Dave Chinner9d7fef72009-04-06 18:42:59 +02002488 spin_lock(&log->l_grant_lock);
2489 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002492 trace_xfs_log_grant_wake2(log, tic);
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 goto redo;
2495 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002496 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002499 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500#ifdef DEBUG
2501 tail_lsn = log->l_tail_lsn;
2502 /*
2503 * Check to make sure the grant write head didn't just over lap the
2504 * tail. If the cycles are the same, we can't be overlapping.
2505 * Otherwise, make sure that the cycles differ by exactly one and
2506 * check the byte count.
2507 */
2508 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2509 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2510 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2511 }
2512#endif
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002513 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002515 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
2517
2518 error_return:
2519 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002520 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002521
2522 trace_xfs_log_grant_error(log, tic);
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /*
2525 * If we are failing, make sure the ticket doesn't have any
2526 * current reservations. We don't want to add this back when
2527 * the ticket/transaction gets cancelled.
2528 */
2529 tic->t_curr_res = 0;
2530 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002531 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return XFS_ERROR(EIO);
2533} /* xlog_grant_log_space */
2534
2535
2536/*
2537 * Replenish the byte reservation required by moving the grant write head.
2538 *
2539 *
2540 */
2541STATIC int
2542xlog_regrant_write_log_space(xlog_t *log,
2543 xlog_ticket_t *tic)
2544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 int free_bytes, need_bytes;
2546 xlog_ticket_t *ntic;
2547#ifdef DEBUG
2548 xfs_lsn_t tail_lsn;
2549#endif
2550
2551 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002552 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557#ifdef DEBUG
2558 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2559 panic("regrant Recovery problem");
2560#endif
2561
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002562 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002563
2564 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 if (XLOG_FORCED_SHUTDOWN(log))
2567 goto error_return;
2568
2569 /* If there are other waiters on the queue then give them a
2570 * chance at logspace before us. Wake up the first waiters,
2571 * if we do not wake up all the waiters then go to sleep waiting
2572 * for more free space, otherwise try to get some space for
2573 * this transaction.
2574 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002575 need_bytes = tic->t_unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if ((ntic = log->l_write_headq)) {
2577 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2578 log->l_grant_write_bytes);
2579 do {
2580 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2581
2582 if (free_bytes < ntic->t_unit_res)
2583 break;
2584 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002585 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 ntic = ntic->t_next;
2587 } while (ntic != log->l_write_headq);
2588
2589 if (ntic != log->l_write_headq) {
2590 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002591 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002593 trace_xfs_log_regrant_write_sleep1(log, tic);
2594
Dave Chinner9d7fef72009-04-06 18:42:59 +02002595 spin_unlock(&log->l_grant_lock);
2596 xlog_grant_push_ail(log->l_mp, need_bytes);
2597 spin_lock(&log->l_grant_lock);
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002600 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 &log->l_grant_lock, s);
2602
2603 /* If we're shutting down, this tic is already
2604 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002605 spin_lock(&log->l_grant_lock);
2606 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002609 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
2611 }
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613redo:
2614 if (XLOG_FORCED_SHUTDOWN(log))
2615 goto error_return;
2616
2617 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2618 log->l_grant_write_bytes);
2619 if (free_bytes < need_bytes) {
2620 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002621 xlog_ins_ticketq(&log->l_write_headq, tic);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002622 spin_unlock(&log->l_grant_lock);
2623 xlog_grant_push_ail(log->l_mp, need_bytes);
2624 spin_lock(&log->l_grant_lock);
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002627 trace_xfs_log_regrant_write_sleep2(log, tic);
2628
David Chinner12017fa2008-08-13 16:34:31 +10002629 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002632 spin_lock(&log->l_grant_lock);
2633 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002636 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 goto redo;
2638 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002639 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002641 /* we've got enough space */
2642 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643#ifdef DEBUG
2644 tail_lsn = log->l_tail_lsn;
2645 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2646 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2647 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2648 }
2649#endif
2650
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002651 trace_xfs_log_regrant_write_exit(log, tic);
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002654 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657
2658 error_return:
2659 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002660 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002661
2662 trace_xfs_log_regrant_write_error(log, tic);
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 /*
2665 * If we are failing, make sure the ticket doesn't have any
2666 * current reservations. We don't want to add this back when
2667 * the ticket/transaction gets cancelled.
2668 */
2669 tic->t_curr_res = 0;
2670 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002671 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return XFS_ERROR(EIO);
2673} /* xlog_regrant_write_log_space */
2674
2675
2676/* The first cnt-1 times through here we don't need to
2677 * move the grant write head because the permanent
2678 * reservation has reserved cnt times the unit amount.
2679 * Release part of current permanent unit reservation and
2680 * reset current reservation to be one units worth. Also
2681 * move grant reservation head forward.
2682 */
2683STATIC void
2684xlog_regrant_reserve_log_space(xlog_t *log,
2685 xlog_ticket_t *ticket)
2686{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002687 trace_xfs_log_regrant_reserve_enter(log, ticket);
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (ticket->t_cnt > 0)
2690 ticket->t_cnt--;
2691
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002692 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002693 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002695 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002696
2697 trace_xfs_log_regrant_reserve_sub(log, ticket);
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 xlog_verify_grant_head(log, 1);
2700
2701 /* just return if we still have some of the pre-reserved space */
2702 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002703 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return;
2705 }
2706
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002707 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002708
2709 trace_xfs_log_regrant_reserve_exit(log, ticket);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002712 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002714 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715} /* xlog_regrant_reserve_log_space */
2716
2717
2718/*
2719 * Give back the space left from a reservation.
2720 *
2721 * All the information we need to make a correct determination of space left
2722 * is present. For non-permanent reservations, things are quite easy. The
2723 * count should have been decremented to zero. We only need to deal with the
2724 * space remaining in the current reservation part of the ticket. If the
2725 * ticket contains a permanent reservation, there may be left over space which
2726 * needs to be released. A count of N means that N-1 refills of the current
2727 * reservation can be done before we need to ask for more space. The first
2728 * one goes to fill up the first current reservation. Once we run out of
2729 * space, the count will stay at zero and the only space remaining will be
2730 * in the current reservation field.
2731 */
2732STATIC void
2733xlog_ungrant_log_space(xlog_t *log,
2734 xlog_ticket_t *ticket)
2735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 if (ticket->t_cnt > 0)
2737 ticket->t_cnt--;
2738
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002739 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002740 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002742 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002744 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 /* If this is a permanent reservation ticket, we may be able to free
2747 * up more space based on the remaining count.
2748 */
2749 if (ticket->t_cnt > 0) {
2750 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002751 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002754 trace_xfs_log_ungrant_exit(log, ticket);
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002757 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 xfs_log_move_tail(log->l_mp, 1);
2759} /* xlog_ungrant_log_space */
2760
2761
2762/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 * Flush iclog to disk if this is the last reference to the given iclog and
2764 * the WANT_SYNC bit is set.
2765 *
2766 * When this function is entered, the iclog is not necessarily in the
2767 * WANT_SYNC state. It may be sitting around waiting to get filled.
2768 *
2769 *
2770 */
David Chinnera8272ce2007-11-23 16:28:09 +11002771STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002772xlog_state_release_iclog(
2773 xlog_t *log,
2774 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 int sync = 0; /* do we sync? */
2777
David Chinner155cc6b2008-03-06 13:44:14 +11002778 if (iclog->ic_state & XLOG_STATE_IOERROR)
2779 return XFS_ERROR(EIO);
2780
2781 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2782 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2783 return 0;
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002786 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return XFS_ERROR(EIO);
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2790 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2791
David Chinner155cc6b2008-03-06 13:44:14 +11002792 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002793 /* update tail before writing to iclog */
2794 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 sync++;
2796 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002797 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2799 /* cycle incremented when incrementing curr_block */
2800 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002801 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 /*
2804 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002805 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2807 * this iclog has consistent data, so we ignore IOERROR
2808 * flags after this point.
2809 */
David Chinnerb5893342008-03-06 13:44:06 +11002810 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813} /* xlog_state_release_iclog */
2814
2815
2816/*
2817 * This routine will mark the current iclog in the ring as WANT_SYNC
2818 * and move the current iclog pointer to the next iclog in the ring.
2819 * When this routine is called from xlog_state_get_iclog_space(), the
2820 * exact size of the iclog has not yet been determined. All we know is
2821 * that every data block. We have run out of space in this log record.
2822 */
2823STATIC void
2824xlog_state_switch_iclogs(xlog_t *log,
2825 xlog_in_core_t *iclog,
2826 int eventual_size)
2827{
2828 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2829 if (!eventual_size)
2830 eventual_size = iclog->ic_offset;
2831 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002832 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 log->l_prev_block = log->l_curr_block;
2834 log->l_prev_cycle = log->l_curr_cycle;
2835
2836 /* roll log?: ic_offset changed later */
2837 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2838
2839 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002840 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 log->l_mp->m_sb.sb_logsunit > 1) {
2842 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2843 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2844 }
2845
2846 if (log->l_curr_block >= log->l_logBBsize) {
2847 log->l_curr_cycle++;
2848 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2849 log->l_curr_cycle++;
2850 log->l_curr_block -= log->l_logBBsize;
2851 ASSERT(log->l_curr_block >= 0);
2852 }
2853 ASSERT(iclog == log->l_iclog);
2854 log->l_iclog = iclog->ic_next;
2855} /* xlog_state_switch_iclogs */
2856
2857
2858/*
2859 * Write out all data in the in-core log as of this exact moment in time.
2860 *
2861 * Data may be written to the in-core log during this call. However,
2862 * we don't guarantee this data will be written out. A change from past
2863 * implementation means this routine will *not* write out zero length LRs.
2864 *
2865 * Basically, we try and perform an intelligent scan of the in-core logs.
2866 * If we determine there is no flushable data, we just return. There is no
2867 * flushable data if:
2868 *
2869 * 1. the current iclog is active and has no data; the previous iclog
2870 * is in the active or dirty state.
2871 * 2. the current iclog is drity, and the previous iclog is in the
2872 * active or dirty state.
2873 *
David Chinner12017fa2008-08-13 16:34:31 +10002874 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 *
2876 * 1. the current iclog is not in the active nor dirty state.
2877 * 2. the current iclog dirty, and the previous iclog is not in the
2878 * active nor dirty state.
2879 * 3. the current iclog is active, and there is another thread writing
2880 * to this particular iclog.
2881 * 4. a) the current iclog is active and has no other writers
2882 * b) when we return from flushing out this iclog, it is still
2883 * not in the active nor dirty state.
2884 */
2885STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002886xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 xlog_in_core_t *iclog;
2889 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002891 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 iclog = log->l_iclog;
2894 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002895 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return XFS_ERROR(EIO);
2897 }
2898
2899 /* If the head iclog is not active nor dirty, we just attach
2900 * ourselves to the head and go to sleep.
2901 */
2902 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2903 iclog->ic_state == XLOG_STATE_DIRTY) {
2904 /*
2905 * If the head is dirty or (active and empty), then
2906 * we need to look at the previous iclog. If the previous
2907 * iclog is active or dirty we are done. There is nothing
2908 * to sync out. Otherwise, we attach ourselves to the
2909 * previous iclog and go to sleep.
2910 */
2911 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002912 (atomic_read(&iclog->ic_refcnt) == 0
2913 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 iclog = iclog->ic_prev;
2915 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2916 iclog->ic_state == XLOG_STATE_DIRTY)
2917 goto no_sleep;
2918 else
2919 goto maybe_sleep;
2920 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002921 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /* We are the only one with access to this
2923 * iclog. Flush it out now. There should
2924 * be a roundoff of zero to show that someone
2925 * has already taken care of the roundoff from
2926 * the previous sync.
2927 */
David Chinner155cc6b2008-03-06 13:44:14 +11002928 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002929 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002931 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 if (xlog_state_release_iclog(log, iclog))
2934 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002935 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002936 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002937 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 iclog->ic_state != XLOG_STATE_DIRTY)
2939 goto maybe_sleep;
2940 else
2941 goto no_sleep;
2942 } else {
2943 /* Someone else is writing to this iclog.
2944 * Use its call to flush out the data. However,
2945 * the other thread may not force out this LR,
2946 * so we mark it WANT_SYNC.
2947 */
2948 xlog_state_switch_iclogs(log, iclog, 0);
2949 goto maybe_sleep;
2950 }
2951 }
2952 }
2953
2954 /* By the time we come around again, the iclog could've been filled
2955 * which would give it another lsn. If we have a new lsn, just
2956 * return because the relevant data has been flushed.
2957 */
2958maybe_sleep:
2959 if (flags & XFS_LOG_SYNC) {
2960 /*
2961 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002962 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 * Then we check again after waking up, in case our
2964 * sleep was disturbed by a bad news.
2965 */
2966 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002967 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return XFS_ERROR(EIO);
2969 }
2970 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10002971 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /*
2973 * No need to grab the log lock here since we're
2974 * only deciding whether or not to return EIO
2975 * and the memory read should be atomic.
2976 */
2977 if (iclog->ic_state & XLOG_STATE_IOERROR)
2978 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002979 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 } else {
2982
2983no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002984 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986 return 0;
2987} /* xlog_state_sync_all */
2988
2989
2990/*
2991 * Used by code which implements synchronous log forces.
2992 *
2993 * Find in-core log with lsn.
2994 * If it is in the DIRTY state, just return.
2995 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2996 * state and go to sleep or return.
2997 * If it is in any other state, go to sleep or return.
2998 *
2999 * If filesystem activity goes to zero, the iclog will get flushed only by
3000 * bdflush().
3001 */
David Chinnera8272ce2007-11-23 16:28:09 +11003002STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003xlog_state_sync(xlog_t *log,
3004 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003005 uint flags,
3006 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 xlog_in_core_t *iclog;
3009 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003012 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 iclog = log->l_iclog;
3014
3015 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003016 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return XFS_ERROR(EIO);
3018 }
3019
3020 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003021 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3022 iclog = iclog->ic_next;
3023 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
3025
3026 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003027 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return 0;
3029 }
3030
3031 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3032 /*
3033 * We sleep here if we haven't already slept (e.g.
3034 * this is the first time we've looked at the correct
3035 * iclog buf) and the buffer before us is going to
3036 * be sync'ed. The reason for this is that if we
3037 * are doing sync transactions here, by waiting for
3038 * the previous I/O to complete, we can allow a few
3039 * more transactions into this iclog before we close
3040 * it down.
3041 *
3042 * Otherwise, we mark the buffer WANT_SYNC, and bump
3043 * up the refcnt so we can release the log (which drops
3044 * the ref count). The state switch keeps new transaction
3045 * commits from using this buffer. When the current commits
3046 * finish writing into the buffer, the refcount will drop to
3047 * zero and the buffer will go out then.
3048 */
3049 if (!already_slept &&
3050 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3051 XLOG_STATE_SYNCING))) {
3052 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3053 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003054 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003056 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 already_slept = 1;
3058 goto try_again;
3059 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003060 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003062 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (xlog_state_release_iclog(log, iclog))
3064 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003065 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003066 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
3068 }
3069
3070 if ((flags & XFS_LOG_SYNC) && /* sleep */
3071 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3072
3073 /*
David Chinner12017fa2008-08-13 16:34:31 +10003074 * Don't wait on completion if we know that we've
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 * gotten a log write error.
3076 */
3077 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003078 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return XFS_ERROR(EIO);
3080 }
3081 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003082 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 /*
3084 * No need to grab the log lock here since we're
3085 * only deciding whether or not to return EIO
3086 * and the memory read should be atomic.
3087 */
3088 if (iclog->ic_state & XLOG_STATE_IOERROR)
3089 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003090 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003092 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094 return 0;
3095
3096 } while (iclog != log->l_iclog);
3097
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003098 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100} /* xlog_state_sync */
3101
3102
3103/*
3104 * Called when we want to mark the current iclog as being ready to sync to
3105 * disk.
3106 */
David Chinnera8272ce2007-11-23 16:28:09 +11003107STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3109{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003110 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3113 xlog_state_switch_iclogs(log, iclog, 0);
3114 } else {
3115 ASSERT(iclog->ic_state &
3116 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3117 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120
3121/*****************************************************************************
3122 *
3123 * TICKET functions
3124 *
3125 *****************************************************************************
3126 */
3127
3128/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003129 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003131void
3132xfs_log_ticket_put(
3133 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003135 ASSERT(atomic_read(&ticket->t_ref) > 0);
3136 if (atomic_dec_and_test(&ticket->t_ref)) {
3137 sv_destroy(&ticket->t_wait);
3138 kmem_zone_free(xfs_log_ticket_zone, ticket);
3139 }
3140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003142xlog_ticket_t *
3143xfs_log_ticket_get(
3144 xlog_ticket_t *ticket)
3145{
3146 ASSERT(atomic_read(&ticket->t_ref) > 0);
3147 atomic_inc(&ticket->t_ref);
3148 return ticket;
3149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003152 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 */
David Chinnera8272ce2007-11-23 16:28:09 +11003154STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003155xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 int unit_bytes,
3157 int cnt,
3158 char client,
3159 uint xflags)
3160{
3161 xlog_ticket_t *tic;
3162 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
David Chinnereb01c9c2008-04-10 12:18:46 +10003164 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3165 if (!tic)
3166 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 /*
3169 * Permanent reservations have up to 'cnt'-1 active log operations
3170 * in the log. A unit in this case is the amount of space for one
3171 * of these log operations. Normal reservations have a cnt of 1
3172 * and their unit amount is the total amount of space required.
3173 *
3174 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003175 * which occupy space in the on-disk log.
3176 *
3177 * Normal form of a transaction is:
3178 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3179 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3180 *
3181 * We need to account for all the leadup data and trailer data
3182 * around the transaction data.
3183 * And then we need to account for the worst case in terms of using
3184 * more space.
3185 * The worst case will happen if:
3186 * - the placement of the transaction happens to be such that the
3187 * roundoff is at its maximum
3188 * - the transaction data is synced before the commit record is synced
3189 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3190 * Therefore the commit record is in its own Log Record.
3191 * This can happen as the commit record is called with its
3192 * own region to xlog_write().
3193 * This then means that in the worst case, roundoff can happen for
3194 * the commit-rec as well.
3195 * The commit-rec is smaller than padding in this scenario and so it is
3196 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 */
3198
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003199 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003201 unit_bytes += sizeof(xfs_trans_header_t);
3202
3203 /* for start-rec */
3204 unit_bytes += sizeof(xlog_op_header_t);
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 /* for LR headers */
3207 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3208 unit_bytes += log->l_iclog_hsize * num_headers;
3209
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003210 /* for commit-rec LR header - note: padding will subsume the ophdr */
3211 unit_bytes += log->l_iclog_hsize;
3212
3213 /* for split-recs - ophdrs added when data split over LRs */
3214 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3215
3216 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003217 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003218 log->l_mp->m_sb.sb_logsunit > 1) {
3219 /* log su roundoff */
3220 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3221 } else {
3222 /* BB roundoff */
3223 unit_bytes += 2*BBSIZE;
3224 }
3225
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003226 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 tic->t_unit_res = unit_bytes;
3228 tic->t_curr_res = unit_bytes;
3229 tic->t_cnt = cnt;
3230 tic->t_ocnt = cnt;
3231 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3232 tic->t_clientid = client;
3233 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003234 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (xflags & XFS_LOG_PERM_RESERV)
3236 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003237 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Christoph Hellwig0adba532007-08-30 17:21:46 +10003239 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
3244
3245/******************************************************************************
3246 *
3247 * Log debug routines
3248 *
3249 ******************************************************************************
3250 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003251#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252/*
3253 * Make sure that the destination ptr is within the valid data region of
3254 * one of the iclogs. This uses backup pointers stored in a different
3255 * part of the log in case we trash the log structure.
3256 */
3257void
3258xlog_verify_dest_ptr(xlog_t *log,
3259 __psint_t ptr)
3260{
3261 int i;
3262 int good_ptr = 0;
3263
3264 for (i=0; i < log->l_iclog_bufs; i++) {
3265 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3266 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3267 good_ptr++;
3268 }
3269 if (! good_ptr)
3270 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3271} /* xlog_verify_dest_ptr */
3272
3273STATIC void
3274xlog_verify_grant_head(xlog_t *log, int equals)
3275{
3276 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3277 if (equals)
3278 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3279 else
3280 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3281 } else {
3282 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3283 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3284 }
3285} /* xlog_verify_grant_head */
3286
3287/* check if it will fit */
3288STATIC void
3289xlog_verify_tail_lsn(xlog_t *log,
3290 xlog_in_core_t *iclog,
3291 xfs_lsn_t tail_lsn)
3292{
3293 int blocks;
3294
3295 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3296 blocks =
3297 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3298 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3299 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3300 } else {
3301 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3302
3303 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3304 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3305
3306 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3307 if (blocks < BTOBB(iclog->ic_offset) + 1)
3308 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3309 }
3310} /* xlog_verify_tail_lsn */
3311
3312/*
3313 * Perform a number of checks on the iclog before writing to disk.
3314 *
3315 * 1. Make sure the iclogs are still circular
3316 * 2. Make sure we have a good magic number
3317 * 3. Make sure we don't have magic numbers in the data
3318 * 4. Check fields of each log operation header for:
3319 * A. Valid client identifier
3320 * B. tid ptr value falls in valid ptr space (user space code)
3321 * C. Length in log record header is correct according to the
3322 * individual operation headers within record.
3323 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3324 * log, check the preceding blocks of the physical log to make sure all
3325 * the cycle numbers agree with the current cycle number.
3326 */
3327STATIC void
3328xlog_verify_iclog(xlog_t *log,
3329 xlog_in_core_t *iclog,
3330 int count,
3331 boolean_t syncing)
3332{
3333 xlog_op_header_t *ophead;
3334 xlog_in_core_t *icptr;
3335 xlog_in_core_2_t *xhdr;
3336 xfs_caddr_t ptr;
3337 xfs_caddr_t base_ptr;
3338 __psint_t field_offset;
3339 __uint8_t clientid;
3340 int len, i, j, k, op_len;
3341 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003344 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 icptr = log->l_iclog;
3346 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003347 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 xlog_panic("xlog_verify_iclog: invalid ptr");
3349 icptr = icptr->ic_next;
3350 }
3351 if (icptr != log->l_iclog)
3352 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003353 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
3355 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003356 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 xlog_panic("xlog_verify_iclog: invalid magic num");
3358
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003359 ptr = (xfs_caddr_t) &iclog->ic_header;
3360 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003362 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 xlog_panic("xlog_verify_iclog: unexpected magic num");
3364 }
3365
3366 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003367 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 ptr = iclog->ic_datap;
3369 base_ptr = ptr;
3370 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003371 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 for (i = 0; i < len; i++) {
3373 ophead = (xlog_op_header_t *)ptr;
3374
3375 /* clientid is only 1 byte */
3376 field_offset = (__psint_t)
3377 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3378 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3379 clientid = ophead->oh_clientid;
3380 } else {
3381 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3382 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3383 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3384 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003385 clientid = xlog_get_client_id(
3386 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003388 clientid = xlog_get_client_id(
3389 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391 }
3392 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003393 cmn_err(CE_WARN, "xlog_verify_iclog: "
3394 "invalid clientid %d op 0x%p offset 0x%lx",
3395 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 /* check length */
3398 field_offset = (__psint_t)
3399 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3400 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003401 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 } else {
3403 idx = BTOBBT((__psint_t)&ophead->oh_len -
3404 (__psint_t)iclog->ic_datap);
3405 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3406 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3407 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003408 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003410 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 }
3412 }
3413 ptr += sizeof(xlog_op_header_t) + op_len;
3414 }
3415} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003416#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
3418/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003419 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 */
3421STATIC int
3422xlog_state_ioerror(
3423 xlog_t *log)
3424{
3425 xlog_in_core_t *iclog, *ic;
3426
3427 iclog = log->l_iclog;
3428 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3429 /*
3430 * Mark all the incore logs IOERROR.
3431 * From now on, no log flushes will result.
3432 */
3433 ic = iclog;
3434 do {
3435 ic->ic_state = XLOG_STATE_IOERROR;
3436 ic = ic->ic_next;
3437 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
3440 /*
3441 * Return non-zero, if state transition has already happened.
3442 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003443 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
3446/*
3447 * This is called from xfs_force_shutdown, when we're forcibly
3448 * shutting down the filesystem, typically because of an IO error.
3449 * Our main objectives here are to make sure that:
3450 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3451 * parties to find out, 'atomically'.
3452 * b. those who're sleeping on log reservations, pinned objects and
3453 * other resources get woken up, and be told the bad news.
3454 * c. nothing new gets queued up after (a) and (b) are done.
3455 * d. if !logerror, flush the iclogs to disk, then seal them off
3456 * for business.
3457 */
3458int
3459xfs_log_force_umount(
3460 struct xfs_mount *mp,
3461 int logerror)
3462{
3463 xlog_ticket_t *tic;
3464 xlog_t *log;
3465 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003466 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468 log = mp->m_log;
3469
3470 /*
3471 * If this happens during log recovery, don't worry about
3472 * locking; the log isn't open for business yet.
3473 */
3474 if (!log ||
3475 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3476 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003477 if (mp->m_sb_bp)
3478 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 }
3481
3482 /*
3483 * Somebody could've already done the hard work for us.
3484 * No need to get locks for this.
3485 */
3486 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3487 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003488 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
3490 retval = 0;
3491 /*
3492 * We must hold both the GRANT lock and the LOG lock,
3493 * before we mark the filesystem SHUTDOWN and wake
3494 * everybody up to tell the bad news.
3495 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003496 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003497 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003499 if (mp->m_sb_bp)
3500 XFS_BUF_DONE(mp->m_sb_bp);
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 /*
3503 * This flag is sort of redundant because of the mount flag, but
3504 * it's good to maintain the separation between the log and the rest
3505 * of XFS.
3506 */
3507 log->l_flags |= XLOG_IO_ERROR;
3508
3509 /*
3510 * If we hit a log error, we want to mark all the iclogs IOERROR
3511 * while we're still holding the loglock.
3512 */
3513 if (logerror)
3514 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003515 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 /*
3518 * We don't want anybody waiting for log reservations
3519 * after this. That means we have to wake up everybody
3520 * queued up on reserve_headq as well as write_headq.
3521 * In addition, we make sure in xlog_{re}grant_log_space
3522 * that we don't enqueue anything once the SHUTDOWN flag
3523 * is set, and this action is protected by the GRANTLOCK.
3524 */
3525 if ((tic = log->l_reserve_headq)) {
3526 do {
David Chinner12017fa2008-08-13 16:34:31 +10003527 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 tic = tic->t_next;
3529 } while (tic != log->l_reserve_headq);
3530 }
3531
3532 if ((tic = log->l_write_headq)) {
3533 do {
David Chinner12017fa2008-08-13 16:34:31 +10003534 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 tic = tic->t_next;
3536 } while (tic != log->l_write_headq);
3537 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003538 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3541 ASSERT(!logerror);
3542 /*
3543 * Force the incore logs to disk before shutting the
3544 * log down completely.
3545 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003546 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003547 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003549 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 }
3551 /*
3552 * Wake up everybody waiting on xfs_log_force.
3553 * Callback all log item committed functions as if the
3554 * log writes were completed.
3555 */
3556 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3557
3558#ifdef XFSERRORDEBUG
3559 {
3560 xlog_in_core_t *iclog;
3561
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003562 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 iclog = log->l_iclog;
3564 do {
3565 ASSERT(iclog->ic_callback == 0);
3566 iclog = iclog->ic_next;
3567 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003568 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570#endif
3571 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003572 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003575STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576xlog_iclogs_empty(xlog_t *log)
3577{
3578 xlog_in_core_t *iclog;
3579
3580 iclog = log->l_iclog;
3581 do {
3582 /* endianness does not matter here, zero is zero in
3583 * any language.
3584 */
3585 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 iclog = iclog->ic_next;
3588 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003589 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}