blob: 4cb1792040e38260bca7abb5f4c602611cfc228b [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);
1444 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001445 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001446 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001448 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001449 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1452 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1453
1454 xlog_verify_iclog(log, iclog, count, B_TRUE);
1455
1456 /* account for log which doesn't start at block #0 */
1457 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1458 /*
1459 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1460 * is shutting down.
1461 */
1462 XFS_BUF_WRITE(bp);
1463
1464 if ((error = XFS_bwrite(bp))) {
1465 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1466 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001467 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001470 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1472 (unsigned long)1);
1473 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1474 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1475 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1476 (__psint_t)count), split);
1477 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001478 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 XFS_BUF_BUSY(bp);
1480 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001481 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1482 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 dptr = XFS_BUF_PTR(bp);
1484 /*
1485 * Bump the cycle numbers at the start of each block
1486 * since this part of the buffer is at the start of
1487 * a new cycle. Watch out for the header magic number
1488 * case, though.
1489 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001490 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001491 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001492 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001493 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 dptr += BBSIZE;
1495 }
1496
1497 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1498 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1499
Nathan Scottc41564b2006-03-29 08:55:14 +10001500 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1502 XFS_BUF_WRITE(bp);
1503 if ((error = XFS_bwrite(bp))) {
1504 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1505 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001506 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510} /* xlog_sync */
1511
1512
1513/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001514 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
David Chinnera8272ce2007-11-23 16:28:09 +11001516STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001517xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int i;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 iclog = log->l_iclog;
1523 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001524 sv_destroy(&iclog->ic_force_wait);
1525 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001528 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 iclog = next_iclog;
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 spinlock_destroy(&log->l_icloglock);
1532 spinlock_destroy(&log->l_grant_lock);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001536 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001537} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539/*
1540 * Update counters atomically now that memcpy is done.
1541 */
1542/* ARGSUSED */
1543static inline void
1544xlog_state_finish_copy(xlog_t *log,
1545 xlog_in_core_t *iclog,
1546 int record_cnt,
1547 int copy_bytes)
1548{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001549 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001551 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 iclog->ic_offset += copy_bytes;
1553
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001554 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555} /* xlog_state_finish_copy */
1556
1557
1558
1559
1560/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001561 * print out info relating to regions written which consume
1562 * the reservation
1563 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001564STATIC void
1565xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1566{
1567 uint i;
1568 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1569
1570 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1571 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1572 "bformat",
1573 "bchunk",
1574 "efi_format",
1575 "efd_format",
1576 "iformat",
1577 "icore",
1578 "iext",
1579 "ibroot",
1580 "ilocal",
1581 "iattr_ext",
1582 "iattr_broot",
1583 "iattr_local",
1584 "qformat",
1585 "dquot",
1586 "quotaoff",
1587 "LR header",
1588 "unmount",
1589 "commit",
1590 "trans header"
1591 };
1592 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1593 "SETATTR_NOT_SIZE",
1594 "SETATTR_SIZE",
1595 "INACTIVE",
1596 "CREATE",
1597 "CREATE_TRUNC",
1598 "TRUNCATE_FILE",
1599 "REMOVE",
1600 "LINK",
1601 "RENAME",
1602 "MKDIR",
1603 "RMDIR",
1604 "SYMLINK",
1605 "SET_DMATTRS",
1606 "GROWFS",
1607 "STRAT_WRITE",
1608 "DIOSTRAT",
1609 "WRITE_SYNC",
1610 "WRITEID",
1611 "ADDAFORK",
1612 "ATTRINVAL",
1613 "ATRUNCATE",
1614 "ATTR_SET",
1615 "ATTR_RM",
1616 "ATTR_FLAG",
1617 "CLEAR_AGI_BUCKET",
1618 "QM_SBCHANGE",
1619 "DUMMY1",
1620 "DUMMY2",
1621 "QM_QUOTAOFF",
1622 "QM_DQALLOC",
1623 "QM_SETQLIM",
1624 "QM_DQCLUSTER",
1625 "QM_QINOCREATE",
1626 "QM_QUOTAOFF_END",
1627 "SB_UNIT",
1628 "FSYNC_TS",
1629 "GROWFSRT_ALLOC",
1630 "GROWFSRT_ZERO",
1631 "GROWFSRT_FREE",
1632 "SWAPEXT"
1633 };
1634
1635 xfs_fs_cmn_err(CE_WARN, mp,
1636 "xfs_log_write: reservation summary:\n"
1637 " trans type = %s (%u)\n"
1638 " unit res = %d bytes\n"
1639 " current res = %d bytes\n"
1640 " total reg = %u bytes (o/flow = %u bytes)\n"
1641 " ophdrs = %u (ophdr space = %u bytes)\n"
1642 " ophdr + reg = %u bytes\n"
1643 " num regions = %u\n",
1644 ((ticket->t_trans_type <= 0 ||
1645 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1646 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1647 ticket->t_trans_type,
1648 ticket->t_unit_res,
1649 ticket->t_curr_res,
1650 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1651 ticket->t_res_num_ophdrs, ophdr_spc,
1652 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001653 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001654 ticket->t_res_num);
1655
1656 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001657 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001658 cmn_err(CE_WARN,
1659 "region[%u]: %s - %u bytes\n",
1660 i,
1661 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1662 "bad-rtype" : res_type_str[r_type-1]),
1663 ticket->t_res_arr[i].r_len);
1664 }
1665}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001666
1667/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * Write some region out to in-core log
1669 *
1670 * This will be called when writing externally provided regions or when
1671 * writing out a commit record for a given transaction.
1672 *
1673 * General algorithm:
1674 * 1. Find total length of this write. This may include adding to the
1675 * lengths passed in.
1676 * 2. Check whether we violate the tickets reservation.
1677 * 3. While writing to this iclog
1678 * A. Reserve as much space in this iclog as can get
1679 * B. If this is first write, save away start lsn
1680 * C. While writing this region:
1681 * 1. If first write of transaction, write start record
1682 * 2. Write log operation header (header per region)
1683 * 3. Find out if we can fit entire region into this iclog
1684 * 4. Potentially, verify destination memcpy ptr
1685 * 5. Memcpy (partial) region
1686 * 6. If partial copy, release iclog; otherwise, continue
1687 * copying more regions into current iclog
1688 * 4. Mark want sync bit (in simulation mode)
1689 * 5. Release iclog for potential flush to on-disk log.
1690 *
1691 * ERRORS:
1692 * 1. Panic if reservation is overrun. This should never happen since
1693 * reservation amounts are generated internal to the filesystem.
1694 * NOTES:
1695 * 1. Tickets are single threaded data structures.
1696 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1697 * syncing routine. When a single log_write region needs to span
1698 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1699 * on all log operation writes which don't contain the end of the
1700 * region. The XLOG_END_TRANS bit is used for the in-core log
1701 * operation which contains the end of the continued log_write region.
1702 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1703 * we don't really know exactly how much space will be used. As a result,
1704 * we don't update ic_offset until the end when we know exactly how many
1705 * bytes have been written out.
1706 */
David Chinnera8272ce2007-11-23 16:28:09 +11001707STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708xlog_write(xfs_mount_t * mp,
1709 xfs_log_iovec_t reg[],
1710 int nentries,
1711 xfs_log_ticket_t tic,
1712 xfs_lsn_t *start_lsn,
1713 xlog_in_core_t **commit_iclog,
1714 uint flags)
1715{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001716 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001718 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 __psint_t ptr; /* copy address into data region */
1721 int len; /* # xlog_write() bytes 2 still copy */
1722 int index; /* region index currently copying */
1723 int log_offset; /* offset (from 0) into data region */
1724 int start_rec_copy; /* # bytes to copy for start record */
1725 int partial_copy; /* did we split a region? */
1726 int partial_copy_len;/* # bytes copied if split region */
1727 int need_copy; /* # bytes need to memcpy this region */
1728 int copy_len; /* # bytes actually memcpy'ing */
1729 int copy_off; /* # bytes from entry start */
1730 int contwr; /* continued write of in-core log? */
1731 int error;
1732 int record_cnt = 0, data_cnt = 0;
1733
1734 partial_copy_len = partial_copy = 0;
1735
1736 /* Calculate potential maximum space. Each region gets its own
1737 * xlog_op_header_t and may need to be double word aligned.
1738 */
1739 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001740 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001742 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 for (index = 0; index < nentries; index++) {
1746 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001747 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001749 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751 contwr = *start_lsn = 0;
1752
1753 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001754 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755#ifdef DEBUG
1756 xlog_panic(
1757 "xfs_log_write: reservation ran out. Need to up reservation");
1758#else
1759 /* Customer configurable panic */
1760 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1761 "xfs_log_write: reservation ran out. Need to up reservation");
1762 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001763 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764#endif
1765 } else
1766 ticket->t_curr_res -= len;
1767
1768 for (index = 0; index < nentries; ) {
1769 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1770 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001771 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 ASSERT(log_offset <= iclog->ic_size - 1);
1774 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1775
1776 /* start_lsn is the first lsn written to. That's all we need. */
1777 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001778 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* This loop writes out as many regions as can fit in the amount
1781 * of space which was allocated by xlog_state_get_iclog_space().
1782 */
1783 while (index < nentries) {
1784 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1785 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1786 start_rec_copy = 0;
1787
1788 /* If first write for transaction, insert start record.
1789 * We can't be trying to commit if we are inited. We can't
1790 * have any "partial_copy" if we are inited.
1791 */
1792 if (ticket->t_flags & XLOG_TIC_INITED) {
1793 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001794 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 logop_head->oh_clientid = ticket->t_clientid;
1796 logop_head->oh_len = 0;
1797 logop_head->oh_flags = XLOG_START_TRANS;
1798 logop_head->oh_res2 = 0;
1799 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1800 record_cnt++;
1801
1802 start_rec_copy = sizeof(xlog_op_header_t);
1803 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1804 }
1805
1806 /* Copy log operation header directly into data section */
1807 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001808 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 logop_head->oh_clientid = ticket->t_clientid;
1810 logop_head->oh_res2 = 0;
1811
1812 /* header copied directly */
1813 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1814
1815 /* are we copying a commit or unmount record? */
1816 logop_head->oh_flags = flags;
1817
1818 /*
1819 * We've seen logs corrupted with bad transaction client
1820 * ids. This makes sure that XFS doesn't generate them on.
1821 * Turn this into an EIO and shut down the filesystem.
1822 */
1823 switch (logop_head->oh_clientid) {
1824 case XFS_TRANSACTION:
1825 case XFS_VOLUME:
1826 case XFS_LOG:
1827 break;
1828 default:
1829 xfs_fs_cmn_err(CE_WARN, mp,
1830 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1831 logop_head->oh_clientid, tic);
1832 return XFS_ERROR(EIO);
1833 }
1834
1835 /* Partial write last time? => (partial_copy != 0)
1836 * need_copy is the amount we'd like to copy if everything could
1837 * fit in the current memcpy.
1838 */
1839 need_copy = reg[index].i_len - partial_copy_len;
1840
1841 copy_off = partial_copy_len;
1842 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001843 copy_len = need_copy;
1844 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (partial_copy)
1846 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1847 partial_copy_len = partial_copy = 0;
1848 } else { /* partial write */
1849 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001850 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1852 if (partial_copy)
1853 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1854 partial_copy_len += copy_len;
1855 partial_copy++;
1856 len += sizeof(xlog_op_header_t); /* from splitting of region */
1857 /* account for new log op header */
1858 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001859 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861 xlog_verify_dest_ptr(log, ptr);
1862
1863 /* copy region */
1864 ASSERT(copy_len >= 0);
1865 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1866 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1867
1868 /* make copy_len total bytes copied, including headers */
1869 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1870 record_cnt++;
1871 data_cnt += contwr ? copy_len : 0;
1872 if (partial_copy) { /* copied partial region */
1873 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1874 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1875 record_cnt = data_cnt = 0;
1876 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001877 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break; /* don't increment index */
1879 } else { /* copied entire region */
1880 index++;
1881 partial_copy_len = partial_copy = 0;
1882
1883 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1884 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1885 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001886 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001888 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (commit_iclog) {
1890 ASSERT(flags & XLOG_COMMIT_TRANS);
1891 *commit_iclog = iclog;
1892 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001893 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (index == nentries)
1895 return 0; /* we are done */
1896 else
1897 break;
1898 }
1899 } /* if (partial_copy) */
1900 } /* while (index < nentries) */
1901 } /* for (index = 0; index < nentries; ) */
1902 ASSERT(len == 0);
1903
1904 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1905 if (commit_iclog) {
1906 ASSERT(flags & XLOG_COMMIT_TRANS);
1907 *commit_iclog = iclog;
1908 return 0;
1909 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001910 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911} /* xlog_write */
1912
1913
1914/*****************************************************************************
1915 *
1916 * State Machine functions
1917 *
1918 *****************************************************************************
1919 */
1920
1921/* Clean iclogs starting from the head. This ordering must be
1922 * maintained, so an iclog doesn't become ACTIVE beyond one that
1923 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001924 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * iclog is trying to sync to disk.
1926 *
1927 * State Change: DIRTY -> ACTIVE
1928 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001929STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930xlog_state_clean_log(xlog_t *log)
1931{
1932 xlog_in_core_t *iclog;
1933 int changed = 0;
1934
1935 iclog = log->l_iclog;
1936 do {
1937 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1938 iclog->ic_state = XLOG_STATE_ACTIVE;
1939 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001940 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /*
1942 * If the number of ops in this iclog indicate it just
1943 * contains the dummy transaction, we can
1944 * change state into IDLE (the second time around).
1945 * Otherwise we should change the state into
1946 * NEED a dummy.
1947 * We don't need to cover the dummy.
1948 */
1949 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001950 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1951 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 changed = 1;
1953 } else {
1954 /*
1955 * We have two dirty iclogs so start over
1956 * This could also be num of ops indicates
1957 * this is not the dummy going out.
1958 */
1959 changed = 2;
1960 }
1961 iclog->ic_header.h_num_logops = 0;
1962 memset(iclog->ic_header.h_cycle_data, 0,
1963 sizeof(iclog->ic_header.h_cycle_data));
1964 iclog->ic_header.h_lsn = 0;
1965 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1966 /* do nothing */;
1967 else
1968 break; /* stop cleaning */
1969 iclog = iclog->ic_next;
1970 } while (iclog != log->l_iclog);
1971
1972 /* log is locked when we are called */
1973 /*
1974 * Change state for the dummy log recording.
1975 * We usually go to NEED. But we go to NEED2 if the changed indicates
1976 * we are done writing the dummy record.
1977 * If we are done with the second dummy recored (DONE2), then
1978 * we go to IDLE.
1979 */
1980 if (changed) {
1981 switch (log->l_covered_state) {
1982 case XLOG_STATE_COVER_IDLE:
1983 case XLOG_STATE_COVER_NEED:
1984 case XLOG_STATE_COVER_NEED2:
1985 log->l_covered_state = XLOG_STATE_COVER_NEED;
1986 break;
1987
1988 case XLOG_STATE_COVER_DONE:
1989 if (changed == 1)
1990 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1991 else
1992 log->l_covered_state = XLOG_STATE_COVER_NEED;
1993 break;
1994
1995 case XLOG_STATE_COVER_DONE2:
1996 if (changed == 1)
1997 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1998 else
1999 log->l_covered_state = XLOG_STATE_COVER_NEED;
2000 break;
2001
2002 default:
2003 ASSERT(0);
2004 }
2005 }
2006} /* xlog_state_clean_log */
2007
2008STATIC xfs_lsn_t
2009xlog_get_lowest_lsn(
2010 xlog_t *log)
2011{
2012 xlog_in_core_t *lsn_log;
2013 xfs_lsn_t lowest_lsn, lsn;
2014
2015 lsn_log = log->l_iclog;
2016 lowest_lsn = 0;
2017 do {
2018 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002019 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if ((lsn && !lowest_lsn) ||
2021 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2022 lowest_lsn = lsn;
2023 }
2024 }
2025 lsn_log = lsn_log->ic_next;
2026 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002027 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
2030
2031STATIC void
2032xlog_state_do_callback(
2033 xlog_t *log,
2034 int aborted,
2035 xlog_in_core_t *ciclog)
2036{
2037 xlog_in_core_t *iclog;
2038 xlog_in_core_t *first_iclog; /* used to know when we've
2039 * processed all iclogs once */
2040 xfs_log_callback_t *cb, *cb_next;
2041 int flushcnt = 0;
2042 xfs_lsn_t lowest_lsn;
2043 int ioerrors; /* counter: iclogs with errors */
2044 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2045 int funcdidcallbacks; /* flag: function did callbacks */
2046 int repeats; /* for issuing console warnings if
2047 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002048 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002050 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 first_iclog = iclog = log->l_iclog;
2052 ioerrors = 0;
2053 funcdidcallbacks = 0;
2054 repeats = 0;
2055
2056 do {
2057 /*
2058 * Scan all iclogs starting with the one pointed to by the
2059 * log. Reset this starting point each time the log is
2060 * unlocked (during callbacks).
2061 *
2062 * Keep looping through iclogs until one full pass is made
2063 * without running any callbacks.
2064 */
2065 first_iclog = log->l_iclog;
2066 iclog = log->l_iclog;
2067 loopdidcallbacks = 0;
2068 repeats++;
2069
2070 do {
2071
2072 /* skip all iclogs in the ACTIVE & DIRTY states */
2073 if (iclog->ic_state &
2074 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2075 iclog = iclog->ic_next;
2076 continue;
2077 }
2078
2079 /*
2080 * Between marking a filesystem SHUTDOWN and stopping
2081 * the log, we do flush all iclogs to disk (if there
2082 * wasn't a log I/O error). So, we do want things to
2083 * go smoothly in case of just a SHUTDOWN w/o a
2084 * LOG_IO_ERROR.
2085 */
2086 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2087 /*
2088 * Can only perform callbacks in order. Since
2089 * this iclog is not in the DONE_SYNC/
2090 * DO_CALLBACK state, we skip the rest and
2091 * just try to clean up. If we set our iclog
2092 * to DO_CALLBACK, we will not process it when
2093 * we retry since a previous iclog is in the
2094 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002095 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 */
2097 if (!(iclog->ic_state &
2098 (XLOG_STATE_DONE_SYNC |
2099 XLOG_STATE_DO_CALLBACK))) {
2100 if (ciclog && (ciclog->ic_state ==
2101 XLOG_STATE_DONE_SYNC)) {
2102 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2103 }
2104 break;
2105 }
2106 /*
2107 * We now have an iclog that is in either the
2108 * DO_CALLBACK or DONE_SYNC states. The other
2109 * states (WANT_SYNC, SYNCING, or CALLBACK were
2110 * caught by the above if and are going to
2111 * clean (i.e. we aren't doing their callbacks)
2112 * see the above if.
2113 */
2114
2115 /*
2116 * We will do one more check here to see if we
2117 * have chased our tail around.
2118 */
2119
2120 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002121 if (lowest_lsn &&
2122 XFS_LSN_CMP(lowest_lsn,
2123 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 iclog = iclog->ic_next;
2125 continue; /* Leave this iclog for
2126 * another thread */
2127 }
2128
2129 iclog->ic_state = XLOG_STATE_CALLBACK;
2130
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002131 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002134 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 * No one else can be here except us.
2136 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002137 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002138 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2139 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2140 log->l_last_sync_lsn =
2141 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002142 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002145 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 ioerrors++;
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
David Chinner114d23a2008-04-10 12:18:39 +10002149 /*
2150 * Keep processing entries in the callback list until
2151 * we come around and it is empty. We need to
2152 * atomically see that the list is empty and change the
2153 * state to DIRTY so that we don't miss any more
2154 * callbacks being added.
2155 */
2156 spin_lock(&iclog->ic_callback_lock);
2157 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002158 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 iclog->ic_callback_tail = &(iclog->ic_callback);
2160 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002161 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002164 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 cb_next = cb->cb_next;
2166 cb->cb_func(cb->cb_arg, aborted);
2167 }
David Chinner114d23a2008-04-10 12:18:39 +10002168 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 cb = iclog->ic_callback;
2170 }
2171
2172 loopdidcallbacks++;
2173 funcdidcallbacks++;
2174
David Chinner114d23a2008-04-10 12:18:39 +10002175 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002176 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002177 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2179 iclog->ic_state = XLOG_STATE_DIRTY;
2180
2181 /*
2182 * Transition from DIRTY to ACTIVE if applicable.
2183 * NOP if STATE_IOERROR.
2184 */
2185 xlog_state_clean_log(log);
2186
2187 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002188 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 iclog = iclog->ic_next;
2191 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002192
2193 if (repeats > 5000) {
2194 flushcnt += repeats;
2195 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002197 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002198 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 } while (!ioerrors && loopdidcallbacks);
2201
2202 /*
2203 * make one last gasp attempt to see if iclogs are being left in
2204 * limbo..
2205 */
2206#ifdef DEBUG
2207 if (funcdidcallbacks) {
2208 first_iclog = iclog = log->l_iclog;
2209 do {
2210 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2211 /*
2212 * Terminate the loop if iclogs are found in states
2213 * which will cause other threads to clean up iclogs.
2214 *
2215 * SYNCING - i/o completion will go through logs
2216 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002217 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * IOERROR - give up hope all ye who enter here
2219 */
2220 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2221 iclog->ic_state == XLOG_STATE_SYNCING ||
2222 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2223 iclog->ic_state == XLOG_STATE_IOERROR )
2224 break;
2225 iclog = iclog->ic_next;
2226 } while (first_iclog != iclog);
2227 }
2228#endif
2229
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002230 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2231 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002232 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002233
2234 if (wake)
2235 sv_broadcast(&log->l_flush_wait);
2236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238
2239/*
2240 * Finish transitioning this iclog to the dirty state.
2241 *
2242 * Make sure that we completely execute this routine only when this is
2243 * the last call to the iclog. There is a good chance that iclog flushes,
2244 * when we reach the end of the physical log, get turned into 2 separate
2245 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2246 * routine. By using the reference count bwritecnt, we guarantee that only
2247 * the second completion goes through.
2248 *
2249 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002250 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 */
David Chinnera8272ce2007-11-23 16:28:09 +11002252STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253xlog_state_done_syncing(
2254 xlog_in_core_t *iclog,
2255 int aborted)
2256{
2257 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002259 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2262 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002263 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2265
2266
2267 /*
2268 * If we got an error, either on the first buffer, or in the case of
2269 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2270 * and none should ever be attempted to be written to disk
2271 * again.
2272 */
2273 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2274 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002275 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return;
2277 }
2278 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2279 }
2280
2281 /*
2282 * Someone could be sleeping prior to writing out the next
2283 * iclog buffer, we wake them all, one will get to do the
2284 * I/O, the others get to wait for the result.
2285 */
David Chinner12017fa2008-08-13 16:34:31 +10002286 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002287 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2289} /* xlog_state_done_syncing */
2290
2291
2292/*
2293 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002294 * sleep. We wait on the flush queue on the head iclog as that should be
2295 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2296 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 *
2298 * The in-core logs are used in a circular fashion. They are not used
2299 * out-of-order even when an iclog past the head is free.
2300 *
2301 * return:
2302 * * log_offset where xlog_write() can start writing into the in-core
2303 * log's data space.
2304 * * in-core log pointer to which xlog_write() should write.
2305 * * boolean indicating this is a continued write to an in-core log.
2306 * If this is the last write, then the in-core log's offset field
2307 * needs to be incremented, depending on the amount of data which
2308 * is copied.
2309 */
David Chinnera8272ce2007-11-23 16:28:09 +11002310STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311xlog_state_get_iclog_space(xlog_t *log,
2312 int len,
2313 xlog_in_core_t **iclogp,
2314 xlog_ticket_t *ticket,
2315 int *continued_write,
2316 int *logoffsetp)
2317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 int log_offset;
2319 xlog_rec_header_t *head;
2320 xlog_in_core_t *iclog;
2321 int error;
2322
2323restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002324 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002326 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return XFS_ERROR(EIO);
2328 }
2329
2330 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002331 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002333
2334 /* Wait for log writes to have flushed */
2335 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto restart;
2337 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 head = &iclog->ic_header;
2340
David Chinner155cc6b2008-03-06 13:44:14 +11002341 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 log_offset = iclog->ic_offset;
2343
2344 /* On the 1st write to an iclog, figure out lsn. This works
2345 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2346 * committing to. If the offset is set, that's how many blocks
2347 * must be written.
2348 */
2349 if (log_offset == 0) {
2350 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002351 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002352 log->l_iclog_hsize,
2353 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002354 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2355 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002356 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 ASSERT(log->l_curr_block >= 0);
2358 }
2359
2360 /* If there is enough room to write everything, then do it. Otherwise,
2361 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2362 * bit is on, so this will get flushed out. Don't update ic_offset
2363 * until you know exactly how many bytes get copied. Therefore, wait
2364 * until later to update ic_offset.
2365 *
2366 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2367 * can fit into remaining data section.
2368 */
2369 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2370 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2371
Dave Chinner49641f12008-07-11 17:43:55 +10002372 /*
2373 * If I'm the only one writing to this iclog, sync it to disk.
2374 * We need to do an atomic compare and decrement here to avoid
2375 * racing with concurrent atomic_dec_and_lock() calls in
2376 * xlog_state_release_iclog() when there is more than one
2377 * reference to the iclog.
2378 */
2379 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2380 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002381 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002382 error = xlog_state_release_iclog(log, iclog);
2383 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002384 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002386 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388 goto restart;
2389 }
2390
2391 /* Do we have enough room to write the full amount in the remainder
2392 * of this iclog? Or must we continue a write on the next iclog and
2393 * mark this iclog as completely taken? In the case where we switch
2394 * iclogs (to mark it taken), this particular iclog will release/sync
2395 * to disk in xlog_write().
2396 */
2397 if (len <= iclog->ic_size - iclog->ic_offset) {
2398 *continued_write = 0;
2399 iclog->ic_offset += len;
2400 } else {
2401 *continued_write = 1;
2402 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2403 }
2404 *iclogp = iclog;
2405
2406 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002407 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 *logoffsetp = log_offset;
2410 return 0;
2411} /* xlog_state_get_iclog_space */
2412
2413/*
2414 * Atomically get the log space required for a log ticket.
2415 *
2416 * Once a ticket gets put onto the reserveq, it will only return after
2417 * the needed reservation is satisfied.
2418 */
2419STATIC int
2420xlog_grant_log_space(xlog_t *log,
2421 xlog_ticket_t *tic)
2422{
2423 int free_bytes;
2424 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425#ifdef DEBUG
2426 xfs_lsn_t tail_lsn;
2427#endif
2428
2429
2430#ifdef DEBUG
2431 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2432 panic("grant Recovery problem");
2433#endif
2434
2435 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002436 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002437
2438 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 /* something is already sleeping; insert new transaction at end */
2441 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002442 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002443
2444 trace_xfs_log_grant_sleep1(log, tic);
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /*
2447 * Gotta check this before going to sleep, while we're
2448 * holding the grant lock.
2449 */
2450 if (XLOG_FORCED_SHUTDOWN(log))
2451 goto error_return;
2452
2453 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002454 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /*
2456 * If we got an error, and the filesystem is shutting down,
2457 * we'll catch it down below. So just continue...
2458 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002459 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002460 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2463 need_bytes = tic->t_unit_res*tic->t_ocnt;
2464 else
2465 need_bytes = tic->t_unit_res;
2466
2467redo:
2468 if (XLOG_FORCED_SHUTDOWN(log))
2469 goto error_return;
2470
2471 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2472 log->l_grant_reserve_bytes);
2473 if (free_bytes < need_bytes) {
2474 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002475 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002476
2477 trace_xfs_log_grant_sleep2(log, tic);
2478
Dave Chinner9d7fef72009-04-06 18:42:59 +02002479 spin_unlock(&log->l_grant_lock);
2480 xlog_grant_push_ail(log->l_mp, need_bytes);
2481 spin_lock(&log->l_grant_lock);
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002484 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Dave Chinner9d7fef72009-04-06 18:42:59 +02002486 spin_lock(&log->l_grant_lock);
2487 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002490 trace_xfs_log_grant_wake2(log, tic);
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto redo;
2493 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002494 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002497 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498#ifdef DEBUG
2499 tail_lsn = log->l_tail_lsn;
2500 /*
2501 * Check to make sure the grant write head didn't just over lap the
2502 * tail. If the cycles are the same, we can't be overlapping.
2503 * Otherwise, make sure that the cycles differ by exactly one and
2504 * check the byte count.
2505 */
2506 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2507 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2508 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2509 }
2510#endif
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002511 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002513 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return 0;
2515
2516 error_return:
2517 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002518 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002519
2520 trace_xfs_log_grant_error(log, tic);
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 /*
2523 * If we are failing, make sure the ticket doesn't have any
2524 * current reservations. We don't want to add this back when
2525 * the ticket/transaction gets cancelled.
2526 */
2527 tic->t_curr_res = 0;
2528 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002529 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return XFS_ERROR(EIO);
2531} /* xlog_grant_log_space */
2532
2533
2534/*
2535 * Replenish the byte reservation required by moving the grant write head.
2536 *
2537 *
2538 */
2539STATIC int
2540xlog_regrant_write_log_space(xlog_t *log,
2541 xlog_ticket_t *tic)
2542{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 int free_bytes, need_bytes;
2544 xlog_ticket_t *ntic;
2545#ifdef DEBUG
2546 xfs_lsn_t tail_lsn;
2547#endif
2548
2549 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002550 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555#ifdef DEBUG
2556 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2557 panic("regrant Recovery problem");
2558#endif
2559
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002560 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002561
2562 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 if (XLOG_FORCED_SHUTDOWN(log))
2565 goto error_return;
2566
2567 /* If there are other waiters on the queue then give them a
2568 * chance at logspace before us. Wake up the first waiters,
2569 * if we do not wake up all the waiters then go to sleep waiting
2570 * for more free space, otherwise try to get some space for
2571 * this transaction.
2572 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002573 need_bytes = tic->t_unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if ((ntic = log->l_write_headq)) {
2575 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2576 log->l_grant_write_bytes);
2577 do {
2578 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2579
2580 if (free_bytes < ntic->t_unit_res)
2581 break;
2582 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002583 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 ntic = ntic->t_next;
2585 } while (ntic != log->l_write_headq);
2586
2587 if (ntic != log->l_write_headq) {
2588 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002589 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002591 trace_xfs_log_regrant_write_sleep1(log, tic);
2592
Dave Chinner9d7fef72009-04-06 18:42:59 +02002593 spin_unlock(&log->l_grant_lock);
2594 xlog_grant_push_ail(log->l_mp, need_bytes);
2595 spin_lock(&log->l_grant_lock);
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002598 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 &log->l_grant_lock, s);
2600
2601 /* If we're shutting down, this tic is already
2602 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002603 spin_lock(&log->l_grant_lock);
2604 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002607 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609 }
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611redo:
2612 if (XLOG_FORCED_SHUTDOWN(log))
2613 goto error_return;
2614
2615 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2616 log->l_grant_write_bytes);
2617 if (free_bytes < need_bytes) {
2618 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002619 xlog_ins_ticketq(&log->l_write_headq, tic);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002620 spin_unlock(&log->l_grant_lock);
2621 xlog_grant_push_ail(log->l_mp, need_bytes);
2622 spin_lock(&log->l_grant_lock);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002625 trace_xfs_log_regrant_write_sleep2(log, tic);
2626
David Chinner12017fa2008-08-13 16:34:31 +10002627 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002630 spin_lock(&log->l_grant_lock);
2631 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002634 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 goto redo;
2636 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002637 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002639 /* we've got enough space */
2640 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641#ifdef DEBUG
2642 tail_lsn = log->l_tail_lsn;
2643 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2644 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2645 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2646 }
2647#endif
2648
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002649 trace_xfs_log_regrant_write_exit(log, tic);
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002652 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655
2656 error_return:
2657 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002658 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002659
2660 trace_xfs_log_regrant_write_error(log, tic);
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /*
2663 * If we are failing, make sure the ticket doesn't have any
2664 * current reservations. We don't want to add this back when
2665 * the ticket/transaction gets cancelled.
2666 */
2667 tic->t_curr_res = 0;
2668 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002669 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return XFS_ERROR(EIO);
2671} /* xlog_regrant_write_log_space */
2672
2673
2674/* The first cnt-1 times through here we don't need to
2675 * move the grant write head because the permanent
2676 * reservation has reserved cnt times the unit amount.
2677 * Release part of current permanent unit reservation and
2678 * reset current reservation to be one units worth. Also
2679 * move grant reservation head forward.
2680 */
2681STATIC void
2682xlog_regrant_reserve_log_space(xlog_t *log,
2683 xlog_ticket_t *ticket)
2684{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002685 trace_xfs_log_regrant_reserve_enter(log, ticket);
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (ticket->t_cnt > 0)
2688 ticket->t_cnt--;
2689
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002690 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002691 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002693 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002694
2695 trace_xfs_log_regrant_reserve_sub(log, ticket);
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 xlog_verify_grant_head(log, 1);
2698
2699 /* just return if we still have some of the pre-reserved space */
2700 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002701 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 return;
2703 }
2704
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002705 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002706
2707 trace_xfs_log_regrant_reserve_exit(log, ticket);
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002710 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002712 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713} /* xlog_regrant_reserve_log_space */
2714
2715
2716/*
2717 * Give back the space left from a reservation.
2718 *
2719 * All the information we need to make a correct determination of space left
2720 * is present. For non-permanent reservations, things are quite easy. The
2721 * count should have been decremented to zero. We only need to deal with the
2722 * space remaining in the current reservation part of the ticket. If the
2723 * ticket contains a permanent reservation, there may be left over space which
2724 * needs to be released. A count of N means that N-1 refills of the current
2725 * reservation can be done before we need to ask for more space. The first
2726 * one goes to fill up the first current reservation. Once we run out of
2727 * space, the count will stay at zero and the only space remaining will be
2728 * in the current reservation field.
2729 */
2730STATIC void
2731xlog_ungrant_log_space(xlog_t *log,
2732 xlog_ticket_t *ticket)
2733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (ticket->t_cnt > 0)
2735 ticket->t_cnt--;
2736
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002737 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002738 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002740 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002742 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 /* If this is a permanent reservation ticket, we may be able to free
2745 * up more space based on the remaining count.
2746 */
2747 if (ticket->t_cnt > 0) {
2748 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002749 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
2751
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002752 trace_xfs_log_ungrant_exit(log, ticket);
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002755 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 xfs_log_move_tail(log->l_mp, 1);
2757} /* xlog_ungrant_log_space */
2758
2759
2760/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 * Flush iclog to disk if this is the last reference to the given iclog and
2762 * the WANT_SYNC bit is set.
2763 *
2764 * When this function is entered, the iclog is not necessarily in the
2765 * WANT_SYNC state. It may be sitting around waiting to get filled.
2766 *
2767 *
2768 */
David Chinnera8272ce2007-11-23 16:28:09 +11002769STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002770xlog_state_release_iclog(
2771 xlog_t *log,
2772 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 int sync = 0; /* do we sync? */
2775
David Chinner155cc6b2008-03-06 13:44:14 +11002776 if (iclog->ic_state & XLOG_STATE_IOERROR)
2777 return XFS_ERROR(EIO);
2778
2779 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2780 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2781 return 0;
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002784 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return XFS_ERROR(EIO);
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2788 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2789
David Chinner155cc6b2008-03-06 13:44:14 +11002790 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002791 /* update tail before writing to iclog */
2792 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 sync++;
2794 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002795 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2797 /* cycle incremented when incrementing curr_block */
2798 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002799 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 /*
2802 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002803 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2805 * this iclog has consistent data, so we ignore IOERROR
2806 * flags after this point.
2807 */
David Chinnerb5893342008-03-06 13:44:06 +11002808 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811} /* xlog_state_release_iclog */
2812
2813
2814/*
2815 * This routine will mark the current iclog in the ring as WANT_SYNC
2816 * and move the current iclog pointer to the next iclog in the ring.
2817 * When this routine is called from xlog_state_get_iclog_space(), the
2818 * exact size of the iclog has not yet been determined. All we know is
2819 * that every data block. We have run out of space in this log record.
2820 */
2821STATIC void
2822xlog_state_switch_iclogs(xlog_t *log,
2823 xlog_in_core_t *iclog,
2824 int eventual_size)
2825{
2826 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2827 if (!eventual_size)
2828 eventual_size = iclog->ic_offset;
2829 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002830 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 log->l_prev_block = log->l_curr_block;
2832 log->l_prev_cycle = log->l_curr_cycle;
2833
2834 /* roll log?: ic_offset changed later */
2835 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2836
2837 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002838 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 log->l_mp->m_sb.sb_logsunit > 1) {
2840 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2841 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2842 }
2843
2844 if (log->l_curr_block >= log->l_logBBsize) {
2845 log->l_curr_cycle++;
2846 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2847 log->l_curr_cycle++;
2848 log->l_curr_block -= log->l_logBBsize;
2849 ASSERT(log->l_curr_block >= 0);
2850 }
2851 ASSERT(iclog == log->l_iclog);
2852 log->l_iclog = iclog->ic_next;
2853} /* xlog_state_switch_iclogs */
2854
2855
2856/*
2857 * Write out all data in the in-core log as of this exact moment in time.
2858 *
2859 * Data may be written to the in-core log during this call. However,
2860 * we don't guarantee this data will be written out. A change from past
2861 * implementation means this routine will *not* write out zero length LRs.
2862 *
2863 * Basically, we try and perform an intelligent scan of the in-core logs.
2864 * If we determine there is no flushable data, we just return. There is no
2865 * flushable data if:
2866 *
2867 * 1. the current iclog is active and has no data; the previous iclog
2868 * is in the active or dirty state.
2869 * 2. the current iclog is drity, and the previous iclog is in the
2870 * active or dirty state.
2871 *
David Chinner12017fa2008-08-13 16:34:31 +10002872 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 *
2874 * 1. the current iclog is not in the active nor dirty state.
2875 * 2. the current iclog dirty, and the previous iclog is not in the
2876 * active nor dirty state.
2877 * 3. the current iclog is active, and there is another thread writing
2878 * to this particular iclog.
2879 * 4. a) the current iclog is active and has no other writers
2880 * b) when we return from flushing out this iclog, it is still
2881 * not in the active nor dirty state.
2882 */
2883STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002884xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 xlog_in_core_t *iclog;
2887 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002889 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 iclog = log->l_iclog;
2892 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002893 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return XFS_ERROR(EIO);
2895 }
2896
2897 /* If the head iclog is not active nor dirty, we just attach
2898 * ourselves to the head and go to sleep.
2899 */
2900 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2901 iclog->ic_state == XLOG_STATE_DIRTY) {
2902 /*
2903 * If the head is dirty or (active and empty), then
2904 * we need to look at the previous iclog. If the previous
2905 * iclog is active or dirty we are done. There is nothing
2906 * to sync out. Otherwise, we attach ourselves to the
2907 * previous iclog and go to sleep.
2908 */
2909 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002910 (atomic_read(&iclog->ic_refcnt) == 0
2911 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 iclog = iclog->ic_prev;
2913 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2914 iclog->ic_state == XLOG_STATE_DIRTY)
2915 goto no_sleep;
2916 else
2917 goto maybe_sleep;
2918 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002919 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /* We are the only one with access to this
2921 * iclog. Flush it out now. There should
2922 * be a roundoff of zero to show that someone
2923 * has already taken care of the roundoff from
2924 * the previous sync.
2925 */
David Chinner155cc6b2008-03-06 13:44:14 +11002926 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002927 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002929 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 if (xlog_state_release_iclog(log, iclog))
2932 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002933 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002934 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002935 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 iclog->ic_state != XLOG_STATE_DIRTY)
2937 goto maybe_sleep;
2938 else
2939 goto no_sleep;
2940 } else {
2941 /* Someone else is writing to this iclog.
2942 * Use its call to flush out the data. However,
2943 * the other thread may not force out this LR,
2944 * so we mark it WANT_SYNC.
2945 */
2946 xlog_state_switch_iclogs(log, iclog, 0);
2947 goto maybe_sleep;
2948 }
2949 }
2950 }
2951
2952 /* By the time we come around again, the iclog could've been filled
2953 * which would give it another lsn. If we have a new lsn, just
2954 * return because the relevant data has been flushed.
2955 */
2956maybe_sleep:
2957 if (flags & XFS_LOG_SYNC) {
2958 /*
2959 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002960 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 * Then we check again after waking up, in case our
2962 * sleep was disturbed by a bad news.
2963 */
2964 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002965 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return XFS_ERROR(EIO);
2967 }
2968 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10002969 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 /*
2971 * No need to grab the log lock here since we're
2972 * only deciding whether or not to return EIO
2973 * and the memory read should be atomic.
2974 */
2975 if (iclog->ic_state & XLOG_STATE_IOERROR)
2976 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002977 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 } else {
2980
2981no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002982 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984 return 0;
2985} /* xlog_state_sync_all */
2986
2987
2988/*
2989 * Used by code which implements synchronous log forces.
2990 *
2991 * Find in-core log with lsn.
2992 * If it is in the DIRTY state, just return.
2993 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2994 * state and go to sleep or return.
2995 * If it is in any other state, go to sleep or return.
2996 *
2997 * If filesystem activity goes to zero, the iclog will get flushed only by
2998 * bdflush().
2999 */
David Chinnera8272ce2007-11-23 16:28:09 +11003000STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001xlog_state_sync(xlog_t *log,
3002 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003003 uint flags,
3004 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 xlog_in_core_t *iclog;
3007 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003010 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 iclog = log->l_iclog;
3012
3013 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003014 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return XFS_ERROR(EIO);
3016 }
3017
3018 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003019 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3020 iclog = iclog->ic_next;
3021 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
3023
3024 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003025 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return 0;
3027 }
3028
3029 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3030 /*
3031 * We sleep here if we haven't already slept (e.g.
3032 * this is the first time we've looked at the correct
3033 * iclog buf) and the buffer before us is going to
3034 * be sync'ed. The reason for this is that if we
3035 * are doing sync transactions here, by waiting for
3036 * the previous I/O to complete, we can allow a few
3037 * more transactions into this iclog before we close
3038 * it down.
3039 *
3040 * Otherwise, we mark the buffer WANT_SYNC, and bump
3041 * up the refcnt so we can release the log (which drops
3042 * the ref count). The state switch keeps new transaction
3043 * commits from using this buffer. When the current commits
3044 * finish writing into the buffer, the refcount will drop to
3045 * zero and the buffer will go out then.
3046 */
3047 if (!already_slept &&
3048 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3049 XLOG_STATE_SYNCING))) {
3050 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3051 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003052 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003054 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 already_slept = 1;
3056 goto try_again;
3057 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003058 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003060 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (xlog_state_release_iclog(log, iclog))
3062 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003063 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003064 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
3066 }
3067
3068 if ((flags & XFS_LOG_SYNC) && /* sleep */
3069 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3070
3071 /*
David Chinner12017fa2008-08-13 16:34:31 +10003072 * Don't wait on completion if we know that we've
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 * gotten a log write error.
3074 */
3075 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003076 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return XFS_ERROR(EIO);
3078 }
3079 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003080 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 /*
3082 * No need to grab the log lock here since we're
3083 * only deciding whether or not to return EIO
3084 * and the memory read should be atomic.
3085 */
3086 if (iclog->ic_state & XLOG_STATE_IOERROR)
3087 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003088 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003090 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 return 0;
3093
3094 } while (iclog != log->l_iclog);
3095
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003096 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098} /* xlog_state_sync */
3099
3100
3101/*
3102 * Called when we want to mark the current iclog as being ready to sync to
3103 * disk.
3104 */
David Chinnera8272ce2007-11-23 16:28:09 +11003105STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3107{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003108 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3111 xlog_state_switch_iclogs(log, iclog, 0);
3112 } else {
3113 ASSERT(iclog->ic_state &
3114 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3115 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118
3119/*****************************************************************************
3120 *
3121 * TICKET functions
3122 *
3123 *****************************************************************************
3124 */
3125
3126/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003127 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003129void
3130xfs_log_ticket_put(
3131 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003133 ASSERT(atomic_read(&ticket->t_ref) > 0);
3134 if (atomic_dec_and_test(&ticket->t_ref)) {
3135 sv_destroy(&ticket->t_wait);
3136 kmem_zone_free(xfs_log_ticket_zone, ticket);
3137 }
3138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003140xlog_ticket_t *
3141xfs_log_ticket_get(
3142 xlog_ticket_t *ticket)
3143{
3144 ASSERT(atomic_read(&ticket->t_ref) > 0);
3145 atomic_inc(&ticket->t_ref);
3146 return ticket;
3147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003150 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 */
David Chinnera8272ce2007-11-23 16:28:09 +11003152STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003153xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 int unit_bytes,
3155 int cnt,
3156 char client,
3157 uint xflags)
3158{
3159 xlog_ticket_t *tic;
3160 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
David Chinnereb01c9c2008-04-10 12:18:46 +10003162 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3163 if (!tic)
3164 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 /*
3167 * Permanent reservations have up to 'cnt'-1 active log operations
3168 * in the log. A unit in this case is the amount of space for one
3169 * of these log operations. Normal reservations have a cnt of 1
3170 * and their unit amount is the total amount of space required.
3171 *
3172 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003173 * which occupy space in the on-disk log.
3174 *
3175 * Normal form of a transaction is:
3176 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3177 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3178 *
3179 * We need to account for all the leadup data and trailer data
3180 * around the transaction data.
3181 * And then we need to account for the worst case in terms of using
3182 * more space.
3183 * The worst case will happen if:
3184 * - the placement of the transaction happens to be such that the
3185 * roundoff is at its maximum
3186 * - the transaction data is synced before the commit record is synced
3187 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3188 * Therefore the commit record is in its own Log Record.
3189 * This can happen as the commit record is called with its
3190 * own region to xlog_write().
3191 * This then means that in the worst case, roundoff can happen for
3192 * the commit-rec as well.
3193 * The commit-rec is smaller than padding in this scenario and so it is
3194 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 */
3196
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003197 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003199 unit_bytes += sizeof(xfs_trans_header_t);
3200
3201 /* for start-rec */
3202 unit_bytes += sizeof(xlog_op_header_t);
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 /* for LR headers */
3205 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3206 unit_bytes += log->l_iclog_hsize * num_headers;
3207
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003208 /* for commit-rec LR header - note: padding will subsume the ophdr */
3209 unit_bytes += log->l_iclog_hsize;
3210
3211 /* for split-recs - ophdrs added when data split over LRs */
3212 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3213
3214 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003215 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003216 log->l_mp->m_sb.sb_logsunit > 1) {
3217 /* log su roundoff */
3218 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3219 } else {
3220 /* BB roundoff */
3221 unit_bytes += 2*BBSIZE;
3222 }
3223
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003224 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 tic->t_unit_res = unit_bytes;
3226 tic->t_curr_res = unit_bytes;
3227 tic->t_cnt = cnt;
3228 tic->t_ocnt = cnt;
3229 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3230 tic->t_clientid = client;
3231 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003232 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (xflags & XFS_LOG_PERM_RESERV)
3234 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003235 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Christoph Hellwig0adba532007-08-30 17:21:46 +10003237 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242
3243/******************************************************************************
3244 *
3245 * Log debug routines
3246 *
3247 ******************************************************************************
3248 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003249#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250/*
3251 * Make sure that the destination ptr is within the valid data region of
3252 * one of the iclogs. This uses backup pointers stored in a different
3253 * part of the log in case we trash the log structure.
3254 */
3255void
3256xlog_verify_dest_ptr(xlog_t *log,
3257 __psint_t ptr)
3258{
3259 int i;
3260 int good_ptr = 0;
3261
3262 for (i=0; i < log->l_iclog_bufs; i++) {
3263 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3264 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3265 good_ptr++;
3266 }
3267 if (! good_ptr)
3268 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3269} /* xlog_verify_dest_ptr */
3270
3271STATIC void
3272xlog_verify_grant_head(xlog_t *log, int equals)
3273{
3274 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3275 if (equals)
3276 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3277 else
3278 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3279 } else {
3280 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3281 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3282 }
3283} /* xlog_verify_grant_head */
3284
3285/* check if it will fit */
3286STATIC void
3287xlog_verify_tail_lsn(xlog_t *log,
3288 xlog_in_core_t *iclog,
3289 xfs_lsn_t tail_lsn)
3290{
3291 int blocks;
3292
3293 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3294 blocks =
3295 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3296 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3297 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3298 } else {
3299 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3300
3301 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3302 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3303
3304 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3305 if (blocks < BTOBB(iclog->ic_offset) + 1)
3306 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3307 }
3308} /* xlog_verify_tail_lsn */
3309
3310/*
3311 * Perform a number of checks on the iclog before writing to disk.
3312 *
3313 * 1. Make sure the iclogs are still circular
3314 * 2. Make sure we have a good magic number
3315 * 3. Make sure we don't have magic numbers in the data
3316 * 4. Check fields of each log operation header for:
3317 * A. Valid client identifier
3318 * B. tid ptr value falls in valid ptr space (user space code)
3319 * C. Length in log record header is correct according to the
3320 * individual operation headers within record.
3321 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3322 * log, check the preceding blocks of the physical log to make sure all
3323 * the cycle numbers agree with the current cycle number.
3324 */
3325STATIC void
3326xlog_verify_iclog(xlog_t *log,
3327 xlog_in_core_t *iclog,
3328 int count,
3329 boolean_t syncing)
3330{
3331 xlog_op_header_t *ophead;
3332 xlog_in_core_t *icptr;
3333 xlog_in_core_2_t *xhdr;
3334 xfs_caddr_t ptr;
3335 xfs_caddr_t base_ptr;
3336 __psint_t field_offset;
3337 __uint8_t clientid;
3338 int len, i, j, k, op_len;
3339 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003342 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 icptr = log->l_iclog;
3344 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003345 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 xlog_panic("xlog_verify_iclog: invalid ptr");
3347 icptr = icptr->ic_next;
3348 }
3349 if (icptr != log->l_iclog)
3350 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003351 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
3353 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003354 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 xlog_panic("xlog_verify_iclog: invalid magic num");
3356
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003357 ptr = (xfs_caddr_t) &iclog->ic_header;
3358 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003360 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 xlog_panic("xlog_verify_iclog: unexpected magic num");
3362 }
3363
3364 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003365 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 ptr = iclog->ic_datap;
3367 base_ptr = ptr;
3368 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003369 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 for (i = 0; i < len; i++) {
3371 ophead = (xlog_op_header_t *)ptr;
3372
3373 /* clientid is only 1 byte */
3374 field_offset = (__psint_t)
3375 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3376 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3377 clientid = ophead->oh_clientid;
3378 } else {
3379 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3380 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3381 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3382 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003383 clientid = xlog_get_client_id(
3384 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003386 clientid = xlog_get_client_id(
3387 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 }
3389 }
3390 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003391 cmn_err(CE_WARN, "xlog_verify_iclog: "
3392 "invalid clientid %d op 0x%p offset 0x%lx",
3393 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 /* check length */
3396 field_offset = (__psint_t)
3397 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3398 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003399 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 } else {
3401 idx = BTOBBT((__psint_t)&ophead->oh_len -
3402 (__psint_t)iclog->ic_datap);
3403 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3404 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3405 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003406 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003408 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
3410 }
3411 ptr += sizeof(xlog_op_header_t) + op_len;
3412 }
3413} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003414#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003417 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 */
3419STATIC int
3420xlog_state_ioerror(
3421 xlog_t *log)
3422{
3423 xlog_in_core_t *iclog, *ic;
3424
3425 iclog = log->l_iclog;
3426 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3427 /*
3428 * Mark all the incore logs IOERROR.
3429 * From now on, no log flushes will result.
3430 */
3431 ic = iclog;
3432 do {
3433 ic->ic_state = XLOG_STATE_IOERROR;
3434 ic = ic->ic_next;
3435 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 }
3438 /*
3439 * Return non-zero, if state transition has already happened.
3440 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003441 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442}
3443
3444/*
3445 * This is called from xfs_force_shutdown, when we're forcibly
3446 * shutting down the filesystem, typically because of an IO error.
3447 * Our main objectives here are to make sure that:
3448 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3449 * parties to find out, 'atomically'.
3450 * b. those who're sleeping on log reservations, pinned objects and
3451 * other resources get woken up, and be told the bad news.
3452 * c. nothing new gets queued up after (a) and (b) are done.
3453 * d. if !logerror, flush the iclogs to disk, then seal them off
3454 * for business.
3455 */
3456int
3457xfs_log_force_umount(
3458 struct xfs_mount *mp,
3459 int logerror)
3460{
3461 xlog_ticket_t *tic;
3462 xlog_t *log;
3463 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003464 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 log = mp->m_log;
3467
3468 /*
3469 * If this happens during log recovery, don't worry about
3470 * locking; the log isn't open for business yet.
3471 */
3472 if (!log ||
3473 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3474 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003475 if (mp->m_sb_bp)
3476 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
3479
3480 /*
3481 * Somebody could've already done the hard work for us.
3482 * No need to get locks for this.
3483 */
3484 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3485 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003486 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
3488 retval = 0;
3489 /*
3490 * We must hold both the GRANT lock and the LOG lock,
3491 * before we mark the filesystem SHUTDOWN and wake
3492 * everybody up to tell the bad news.
3493 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003494 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003495 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003497 if (mp->m_sb_bp)
3498 XFS_BUF_DONE(mp->m_sb_bp);
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 /*
3501 * This flag is sort of redundant because of the mount flag, but
3502 * it's good to maintain the separation between the log and the rest
3503 * of XFS.
3504 */
3505 log->l_flags |= XLOG_IO_ERROR;
3506
3507 /*
3508 * If we hit a log error, we want to mark all the iclogs IOERROR
3509 * while we're still holding the loglock.
3510 */
3511 if (logerror)
3512 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003513 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
3515 /*
3516 * We don't want anybody waiting for log reservations
3517 * after this. That means we have to wake up everybody
3518 * queued up on reserve_headq as well as write_headq.
3519 * In addition, we make sure in xlog_{re}grant_log_space
3520 * that we don't enqueue anything once the SHUTDOWN flag
3521 * is set, and this action is protected by the GRANTLOCK.
3522 */
3523 if ((tic = log->l_reserve_headq)) {
3524 do {
David Chinner12017fa2008-08-13 16:34:31 +10003525 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 tic = tic->t_next;
3527 } while (tic != log->l_reserve_headq);
3528 }
3529
3530 if ((tic = log->l_write_headq)) {
3531 do {
David Chinner12017fa2008-08-13 16:34:31 +10003532 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 tic = tic->t_next;
3534 } while (tic != log->l_write_headq);
3535 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003536 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3539 ASSERT(!logerror);
3540 /*
3541 * Force the incore logs to disk before shutting the
3542 * log down completely.
3543 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003544 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003545 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003547 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
3549 /*
3550 * Wake up everybody waiting on xfs_log_force.
3551 * Callback all log item committed functions as if the
3552 * log writes were completed.
3553 */
3554 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3555
3556#ifdef XFSERRORDEBUG
3557 {
3558 xlog_in_core_t *iclog;
3559
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003560 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 iclog = log->l_iclog;
3562 do {
3563 ASSERT(iclog->ic_callback == 0);
3564 iclog = iclog->ic_next;
3565 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003566 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
3568#endif
3569 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003570 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003573STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574xlog_iclogs_empty(xlog_t *log)
3575{
3576 xlog_in_core_t *iclog;
3577
3578 iclog = log->l_iclog;
3579 do {
3580 /* endianness does not matter here, zero is zero in
3581 * any language.
3582 */
3583 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 iclog = iclog->ic_next;
3586 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003587 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588}