blob: 4f16be4b6ee5907295fcba07a2aac778866b68c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
David Chinnereb01c9c2008-04-10 12:18:46 +100045kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define xlog_write_adv_cnt(ptr, len, off, bytes) \
48 { (ptr) += (bytes); \
49 (len) -= (bytes); \
50 (off) += (bytes);}
51
52/* Local miscellaneous function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54 xlog_in_core_t **, xfs_lsn_t *);
55STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
56 xfs_buftarg_t *log_target,
57 xfs_daddr_t blk_offset,
58 int num_bblks);
59STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
60STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100061STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
63 int nentries, xfs_log_ticket_t tic,
64 xfs_lsn_t *start_lsn,
65 xlog_in_core_t **commit_iclog,
66 uint flags);
67
68/* local state machine functions */
69STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71STATIC int xlog_state_get_iclog_space(xlog_t *log,
72 int len,
73 xlog_in_core_t **iclog,
74 xlog_ticket_t *ticket,
75 int *continued_write,
76 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077STATIC int xlog_state_release_iclog(xlog_t *log,
78 xlog_in_core_t *iclog);
79STATIC void xlog_state_switch_iclogs(xlog_t *log,
80 xlog_in_core_t *iclog,
81 int eventual_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
83
84/* local functions to manipulate grant head */
85STATIC int xlog_grant_log_space(xlog_t *log,
86 xlog_ticket_t *xtic);
87STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
88 int need_bytes);
89STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
90 xlog_ticket_t *ticket);
91STATIC int xlog_regrant_write_log_space(xlog_t *log,
92 xlog_ticket_t *ticket);
93STATIC void xlog_ungrant_log_space(xlog_t *log,
94 xlog_ticket_t *ticket);
95
96
97/* local ticket functions */
Dave Chinnercc09c0d2008-11-17 17:37:10 +110098STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int unit_bytes,
100 int count,
101 char clientid,
102 uint flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100104#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
106STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
107STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
108 int count, boolean_t syncing);
109STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
110 xfs_lsn_t tail_lsn);
111#else
112#define xlog_verify_dest_ptr(a,b)
113#define xlog_verify_grant_head(a,b)
114#define xlog_verify_iclog(a,b,c,d)
115#define xlog_verify_tail_lsn(a,b,c)
116#endif
117
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000118STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100120
121static void
122xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
123{
124 if (*qp) {
125 tic->t_next = (*qp);
126 tic->t_prev = (*qp)->t_prev;
127 (*qp)->t_prev->t_next = tic;
128 (*qp)->t_prev = tic;
129 } else {
130 tic->t_prev = tic->t_next = tic;
131 *qp = tic;
132 }
133
134 tic->t_flags |= XLOG_TIC_IN_Q;
135}
136
137static void
138xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
139{
140 if (tic == tic->t_next) {
141 *qp = NULL;
142 } else {
143 *qp = tic->t_next;
144 tic->t_next->t_prev = tic->t_prev;
145 tic->t_prev->t_next = tic->t_next;
146 }
147
148 tic->t_next = tic->t_prev = NULL;
149 tic->t_flags &= ~XLOG_TIC_IN_Q;
150}
151
152static void
153xlog_grant_sub_space(struct log *log, int bytes)
154{
155 log->l_grant_write_bytes -= bytes;
156 if (log->l_grant_write_bytes < 0) {
157 log->l_grant_write_bytes += log->l_logsize;
158 log->l_grant_write_cycle--;
159 }
160
161 log->l_grant_reserve_bytes -= bytes;
162 if ((log)->l_grant_reserve_bytes < 0) {
163 log->l_grant_reserve_bytes += log->l_logsize;
164 log->l_grant_reserve_cycle--;
165 }
166
167}
168
169static void
170xlog_grant_add_space_write(struct log *log, int bytes)
171{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000172 int tmp = log->l_logsize - log->l_grant_write_bytes;
173 if (tmp > bytes)
174 log->l_grant_write_bytes += bytes;
175 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100176 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000177 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100178 }
179}
180
181static void
182xlog_grant_add_space_reserve(struct log *log, int bytes)
183{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000184 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
185 if (tmp > bytes)
186 log->l_grant_reserve_bytes += bytes;
187 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100188 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000189 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100190 }
191}
192
193static inline void
194xlog_grant_add_space(struct log *log, int bytes)
195{
196 xlog_grant_add_space_write(log, bytes);
197 xlog_grant_add_space_reserve(log, bytes);
198}
199
Christoph Hellwig0adba532007-08-30 17:21:46 +1000200static void
201xlog_tic_reset_res(xlog_ticket_t *tic)
202{
203 tic->t_res_num = 0;
204 tic->t_res_arr_sum = 0;
205 tic->t_res_num_ophdrs = 0;
206}
207
208static void
209xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
210{
211 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
212 /* add to overflow and start again */
213 tic->t_res_o_flow += tic->t_res_arr_sum;
214 tic->t_res_num = 0;
215 tic->t_res_arr_sum = 0;
216 }
217
218 tic->t_res_arr[tic->t_res_num].r_len = len;
219 tic->t_res_arr[tic->t_res_num].r_type = type;
220 tic->t_res_arr_sum += len;
221 tic->t_res_num++;
222}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * NOTES:
226 *
227 * 1. currblock field gets updated at startup and after in-core logs
228 * marked as with WANT_SYNC.
229 */
230
231/*
232 * This routine is called when a user of a log manager ticket is done with
233 * the reservation. If the ticket was ever used, then a commit record for
234 * the associated transaction is written out as a log operation header with
235 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
236 * a given ticket. If the ticket was one with a permanent reservation, then
237 * a few operations are done differently. Permanent reservation tickets by
238 * default don't release the reservation. They just commit the current
239 * transaction with the belief that the reservation is still needed. A flag
240 * must be passed in before permanent reservations are actually released.
241 * When these type of tickets are not released, they need to be set into
242 * the inited state again. By doing this, a start record will be written
243 * out when the next write occurs.
244 */
245xfs_lsn_t
246xfs_log_done(xfs_mount_t *mp,
247 xfs_log_ticket_t xtic,
248 void **iclog,
249 uint flags)
250{
251 xlog_t *log = mp->m_log;
252 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
253 xfs_lsn_t lsn = 0;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (XLOG_FORCED_SHUTDOWN(log) ||
256 /*
257 * If nothing was ever written, don't write out commit record.
258 * If we get an error, just continue and give back the log ticket.
259 */
260 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
261 (xlog_commit_record(mp, ticket,
262 (xlog_in_core_t **)iclog, &lsn)))) {
263 lsn = (xfs_lsn_t) -1;
264 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
265 flags |= XFS_LOG_REL_PERM_RESERV;
266 }
267 }
268
269
270 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
271 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000272 trace_xfs_log_done_nonperm(log, ticket);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000275 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * request has been made to release a permanent reservation.
277 */
278 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100279 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000281 trace_xfs_log_done_perm(log, ticket);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000284 /* If this ticket was a permanent reservation and we aren't
285 * trying to release it, reset the inited flags; so next time
286 * we write, a start record will be written out.
287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 return lsn;
292} /* xfs_log_done */
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
295 * Attaches a new iclog I/O completion callback routine during
296 * transaction commit. If the log is in error state, a non-zero
297 * return code is handed back and the caller is responsible for
298 * executing the callback at an appropriate time.
299 */
300int
301xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
302 void *iclog_hndl, /* iclog to hang callback off */
303 xfs_log_callback_t *cb)
304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000306 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
David Chinner114d23a2008-04-10 12:18:39 +1000308 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
310 if (!abortflg) {
311 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
312 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
313 cb->cb_next = NULL;
314 *(iclog->ic_callback_tail) = cb;
315 iclog->ic_callback_tail = &(cb->cb_next);
316 }
David Chinner114d23a2008-04-10 12:18:39 +1000317 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return abortflg;
319} /* xfs_log_notify */
320
321int
322xfs_log_release_iclog(xfs_mount_t *mp,
323 void *iclog_hndl)
324{
325 xlog_t *log = mp->m_log;
326 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
327
328 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000329 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100330 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 return 0;
334}
335
336/*
337 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
338 * to the reservation.
339 * 2. Potentially, push buffers at tail of log to disk.
340 *
341 * Each reservation is going to reserve extra space for a log record header.
342 * When writes happen to the on-disk log, we don't subtract the length of the
343 * log record header from any reservation. By wasting space in each
344 * reservation, we prevent over allocation problems.
345 */
346int
347xfs_log_reserve(xfs_mount_t *mp,
348 int unit_bytes,
349 int cnt,
350 xfs_log_ticket_t *ticket,
351 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000352 uint flags,
353 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 xlog_t *log = mp->m_log;
356 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100357 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
360 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
361
362 if (XLOG_FORCED_SHUTDOWN(log))
363 return XFS_ERROR(EIO);
364
365 XFS_STATS_INC(xs_try_logspace);
366
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (*ticket != NULL) {
369 ASSERT(flags & XFS_LOG_PERM_RESERV);
370 internal_ticket = (xlog_ticket_t *)*ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000371
372 trace_xfs_log_reserve(log, internal_ticket);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
375 retval = xlog_regrant_write_log_space(log, internal_ticket);
376 } else {
377 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100378 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000380 if (!internal_ticket)
381 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000382 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000384
385 trace_xfs_log_reserve(log, internal_ticket);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 xlog_grant_push_ail(mp,
388 (internal_ticket->t_unit_res *
389 internal_ticket->t_cnt));
390 retval = xlog_grant_log_space(log, internal_ticket);
391 }
392
393 return retval;
394} /* xfs_log_reserve */
395
396
397/*
398 * Mount a log filesystem
399 *
400 * mp - ubiquitous xfs mount point structure
401 * log_target - buftarg of on-disk log device
402 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
403 * num_bblocks - Number of BBSIZE blocks in on-disk log
404 *
405 * Return error or zero.
406 */
407int
David Chinner249a8c12008-02-05 12:13:32 +1100408xfs_log_mount(
409 xfs_mount_t *mp,
410 xfs_buftarg_t *log_target,
411 xfs_daddr_t blk_offset,
412 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
David Chinner249a8c12008-02-05 12:13:32 +1100414 int error;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
417 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
418 else {
419 cmn_err(CE_NOTE,
420 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
421 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000422 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200426 if (IS_ERR(mp->m_log)) {
427 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100428 goto out;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
David Chinner249a8c12008-02-05 12:13:32 +1100432 * Initialize the AIL now we have a log.
433 */
David Chinner249a8c12008-02-05 12:13:32 +1100434 error = xfs_trans_ail_init(mp);
435 if (error) {
436 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100437 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100438 }
David Chinnera9c21c12008-10-30 17:39:35 +1100439 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100440
441 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * skip log recovery on a norecovery mount. pretend it all
443 * just worked.
444 */
445 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100446 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000449 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Eric Sandeen65be6052006-01-11 15:34:19 +1100451 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000454 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (error) {
456 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100457 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 }
460
461 /* Normal transactions can now occur */
462 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100465
466out_destroy_ail:
467 xfs_trans_ail_destroy(mp);
468out_free_log:
469 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100470out:
David Chinner249a8c12008-02-05 12:13:32 +1100471 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474/*
475 * Finish the recovery of the file system. This is separate from
476 * the xfs_log_mount() call, because it depends on the code in
477 * xfs_mountfs() to read in the root and real-time bitmap inodes
478 * between calling xfs_log_mount() and here.
479 *
480 * mp - ubiquitous xfs mount point structure
481 */
482int
Christoph Hellwig42490232008-08-13 16:49:32 +1000483xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 int error;
486
487 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000488 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 else {
490 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000491 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
494 return error;
495}
496
497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * Final log writes as part of unmount.
499 *
500 * Mark the filesystem clean as unmount happens. Note that during relocation
501 * this routine needs to be executed as part of source-bag while the
502 * deallocation must not be done until source-end.
503 */
504
505/*
506 * Unmount record used to have a string "Unmount filesystem--" in the
507 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
508 * We just write the magic number now since that particular field isn't
509 * currently architecture converted and "nUmount" is a bit foo.
510 * As far as I know, there weren't any dependencies on the old behaviour.
511 */
512
513int
514xfs_log_unmount_write(xfs_mount_t *mp)
515{
516 xlog_t *log = mp->m_log;
517 xlog_in_core_t *iclog;
518#ifdef DEBUG
519 xlog_in_core_t *first_iclog;
520#endif
521 xfs_log_iovec_t reg[1];
522 xfs_log_ticket_t tic = NULL;
523 xfs_lsn_t lsn;
524 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* the data section must be 32 bit size aligned */
527 struct {
528 __uint16_t magic;
529 __uint16_t pad1;
530 __uint32_t pad2; /* may as well make it 64 bits */
531 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
534 * Don't write out unmount record on read-only mounts.
535 * Or, if we are doing a forced umount (typically because of IO errors).
536 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000537 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000540 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000541 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543#ifdef DEBUG
544 first_iclog = iclog = log->l_iclog;
545 do {
546 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
547 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
548 ASSERT(iclog->ic_offset == 0);
549 }
550 iclog = iclog->ic_next;
551 } while (iclog != first_iclog);
552#endif
553 if (! (XLOG_FORCED_SHUTDOWN(log))) {
554 reg[0].i_addr = (void*)&magic;
555 reg[0].i_len = sizeof(magic);
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000556 reg[0].i_type = XLOG_REG_TYPE_UNMOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Tim Shimmin955e47a2006-09-28 11:04:16 +1000558 error = xfs_log_reserve(mp, 600, 1, &tic,
559 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (!error) {
561 /* remove inited flag */
562 ((xlog_ticket_t *)tic)->t_flags = 0;
563 error = xlog_write(mp, reg, 1, tic, &lsn,
564 NULL, XLOG_UNMOUNT_TRANS);
565 /*
566 * At this point, we're umounting anyway,
567 * so there's no point in transitioning log state
568 * to IOERROR. Just continue...
569 */
570 }
571
572 if (error) {
573 xfs_fs_cmn_err(CE_ALERT, mp,
574 "xfs_log_unmount: unmount record failed");
575 }
576
577
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000578 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100580 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100582 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000583 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000585 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
587 iclog->ic_state == XLOG_STATE_DIRTY)) {
588 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000589 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 &log->l_icloglock, s);
591 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000592 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000595 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000597 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000598 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000599 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100600 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 } else {
603 /*
604 * We're already in forced_shutdown mode, couldn't
605 * even attempt to write out the unmount transaction.
606 *
607 * Go through the motions of sync'ing and releasing
608 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000609 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * be in progress. Do this as a separate section of
611 * code so we'll know if we ever get stuck here that
612 * we're in this odd situation of trying to unmount
613 * a file system that went into forced_shutdown as
614 * the result of an unmount..
615 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000616 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100618 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100621 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000622 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000624 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
627 || iclog->ic_state == XLOG_STATE_DIRTY
628 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
629
David Chinner12017fa2008-08-13 16:34:31 +1000630 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 &log->l_icloglock, s);
632 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000633 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 }
636
David Chinner1bb7d6b2008-04-10 12:24:38 +1000637 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638} /* xfs_log_unmount_write */
639
640/*
641 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100642 *
643 * We need to stop the aild from running before we destroy
644 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
646void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100647xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
David Chinner249a8c12008-02-05 12:13:32 +1100649 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000650 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/*
654 * Write region vectors to log. The write happens using the space reservation
655 * of the ticket (tic). It is not a requirement that all writes for a given
656 * transaction occur with one call to xfs_log_write().
657 */
658int
659xfs_log_write(xfs_mount_t * mp,
660 xfs_log_iovec_t reg[],
661 int nentries,
662 xfs_log_ticket_t tic,
663 xfs_lsn_t *start_lsn)
664{
665 int error;
666 xlog_t *log = mp->m_log;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (XLOG_FORCED_SHUTDOWN(log))
669 return XFS_ERROR(EIO);
670
671 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000672 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100674 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675} /* xfs_log_write */
676
677
678void
679xfs_log_move_tail(xfs_mount_t *mp,
680 xfs_lsn_t tail_lsn)
681{
682 xlog_ticket_t *tic;
683 xlog_t *log = mp->m_log;
684 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (XLOG_FORCED_SHUTDOWN(log))
687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (tail_lsn == 0) {
690 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000691 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000693 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000696 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
699 * tail_lsn.
700 */
701 if (tail_lsn != 1) {
702 log->l_tail_lsn = tail_lsn;
703 }
704
705 if ((tic = log->l_write_headq)) {
706#ifdef DEBUG
707 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
708 panic("Recovery problem");
709#endif
710 cycle = log->l_grant_write_cycle;
711 bytes = log->l_grant_write_bytes;
712 free_bytes = xlog_space_left(log, cycle, bytes);
713 do {
714 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
715
716 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
717 break;
718 tail_lsn = 0;
719 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000720 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 tic = tic->t_next;
722 } while (tic != log->l_write_headq);
723 }
724 if ((tic = log->l_reserve_headq)) {
725#ifdef DEBUG
726 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
727 panic("Recovery problem");
728#endif
729 cycle = log->l_grant_reserve_cycle;
730 bytes = log->l_grant_reserve_bytes;
731 free_bytes = xlog_space_left(log, cycle, bytes);
732 do {
733 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
734 need_bytes = tic->t_unit_res*tic->t_cnt;
735 else
736 need_bytes = tic->t_unit_res;
737 if (free_bytes < need_bytes && tail_lsn != 1)
738 break;
739 tail_lsn = 0;
740 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000741 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 tic = tic->t_next;
743 } while (tic != log->l_reserve_headq);
744 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000745 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746} /* xfs_log_move_tail */
747
748/*
749 * Determine if we have a transaction that has gone to disk
750 * that needs to be covered. Log activity needs to be idle (no AIL and
751 * nothing in the iclogs). And, we need to be in the right state indicating
752 * something has gone out.
753 */
754int
755xfs_log_need_covered(xfs_mount_t *mp)
756{
David Chinner27d8d5f2008-10-30 17:38:39 +1100757 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David Chinner92821e22007-05-24 15:26:31 +1000760 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 0;
762
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000763 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
765 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
David Chinnera9c21c12008-10-30 17:39:35 +1100766 && !xfs_trans_ail_tail(log->l_ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 && xlog_iclogs_empty(log)) {
768 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
769 log->l_covered_state = XLOG_STATE_COVER_DONE;
770 else {
771 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
772 log->l_covered_state = XLOG_STATE_COVER_DONE2;
773 }
774 needed = 1;
775 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000776 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100777 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/******************************************************************************
781 *
782 * local routines
783 *
784 ******************************************************************************
785 */
786
787/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
788 * The log manager must keep track of the last LR which was committed
789 * to disk. The lsn of this LR will become the new tail_lsn whenever
790 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
791 * the situation where stuff could be written into the log but nothing
792 * was ever in the AIL when asked. Eventually, we panic since the
793 * tail hits the head.
794 *
795 * We may be holding the log iclog lock upon entering this routine.
796 */
797xfs_lsn_t
798xlog_assign_tail_lsn(xfs_mount_t *mp)
799{
800 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 xlog_t *log = mp->m_log;
802
David Chinner5b00f142008-10-30 17:39:00 +1100803 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000804 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (tail_lsn != 0) {
806 log->l_tail_lsn = tail_lsn;
807 } else {
808 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
809 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000810 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 return tail_lsn;
813} /* xlog_assign_tail_lsn */
814
815
816/*
817 * Return the space in the log between the tail and the head. The head
818 * is passed in the cycle/bytes formal parms. In the special case where
819 * the reserve head has wrapped passed the tail, this calculation is no
820 * longer valid. In this case, just return 0 which means there is no space
821 * in the log. This works for all places where this function is called
822 * with the reserve head. Of course, if the write head were to ever
823 * wrap the tail, we should blow up. Rather than catch this case here,
824 * we depend on other ASSERTions in other parts of the code. XXXmiken
825 *
826 * This code also handles the case where the reservation head is behind
827 * the tail. The details of this case are described below, but the end
828 * result is that we return the size of the log as the amount of space left.
829 */
David Chinnera8272ce2007-11-23 16:28:09 +1100830STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831xlog_space_left(xlog_t *log, int cycle, int bytes)
832{
833 int free_bytes;
834 int tail_bytes;
835 int tail_cycle;
836
837 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
838 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
839 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
840 free_bytes = log->l_logsize - (bytes - tail_bytes);
841 } else if ((tail_cycle + 1) < cycle) {
842 return 0;
843 } else if (tail_cycle < cycle) {
844 ASSERT(tail_cycle == (cycle - 1));
845 free_bytes = tail_bytes - bytes;
846 } else {
847 /*
848 * The reservation head is behind the tail.
849 * In this case we just want to return the size of the
850 * log as the amount of space left.
851 */
852 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
853 "xlog_space_left: head behind tail\n"
854 " tail_cycle = %d, tail_bytes = %d\n"
855 " GH cycle = %d, GH bytes = %d",
856 tail_cycle, tail_bytes, cycle, bytes);
857 ASSERT(0);
858 free_bytes = log->l_logsize;
859 }
860 return free_bytes;
861} /* xlog_space_left */
862
863
864/*
865 * Log function which is called when an io completes.
866 *
867 * The log manager needs its own routine, in order to control what
868 * happens with the buffer after the write completes.
869 */
870void
871xlog_iodone(xfs_buf_t *bp)
872{
873 xlog_in_core_t *iclog;
874 xlog_t *l;
875 int aborted;
876
877 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
878 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
879 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
880 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 l = iclog->ic_log;
882
883 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100884 * If the _XFS_BARRIER_FAILED flag was set by a lower
885 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +1000886 * barrier I/O. Warn loudly and turn off barriers.
887 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100888 if (bp->b_flags & _XFS_BARRIER_FAILED) {
889 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000890 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
891 xfs_fs_cmn_err(CE_WARN, l->l_mp,
892 "xlog_iodone: Barriers are no longer supported"
893 " by device. Disabling barriers\n");
David Chinner0bfefc42007-05-14 18:24:23 +1000894 }
895
896 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * Race to shutdown the filesystem if we see an error.
898 */
899 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
900 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
901 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
902 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000903 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * This flag will be propagated to the trans-committed
906 * callback routines to let them know that the log-commit
907 * didn't succeed.
908 */
909 aborted = XFS_LI_ABORTED;
910 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
911 aborted = XFS_LI_ABORTED;
912 }
David Chinner3db296f2007-05-14 18:24:16 +1000913
914 /* log I/O is always issued ASYNC */
915 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000917 /*
918 * do not reference the buffer (bp) here as we could race
919 * with it being freed after writing the unmount record to the
920 * log.
921 */
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923} /* xlog_iodone */
924
925/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * Return size of each in-core log record buffer.
927 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200928 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
930 * If the filesystem blocksize is too large, we may need to choose a
931 * larger size since the directory code currently logs entire blocks.
932 */
933
934STATIC void
935xlog_get_iclog_buffer_size(xfs_mount_t *mp,
936 xlog_t *log)
937{
938 int size;
939 int xhdrs;
940
Eric Sandeen1cb51252007-08-16 16:24:43 +1000941 if (mp->m_logbufs <= 0)
942 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
943 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100944 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 /*
947 * Buffer size passed in from mount system call.
948 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100949 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 size = log->l_iclog_size = mp->m_logbsize;
951 log->l_iclog_size_log = 0;
952 while (size != 1) {
953 log->l_iclog_size_log++;
954 size >>= 1;
955 }
956
Eric Sandeen62118702008-03-06 13:44:28 +1100957 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200958 /* # headers = size / 32k
959 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
961
962 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
963 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
964 xhdrs++;
965 log->l_iclog_hsize = xhdrs << BBSHIFT;
966 log->l_iclog_heads = xhdrs;
967 } else {
968 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
969 log->l_iclog_hsize = BBSIZE;
970 log->l_iclog_heads = 1;
971 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100972 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200975 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +1000976 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
977 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /* the default log size is 16k or 32k which is one header sector */
980 log->l_iclog_hsize = BBSIZE;
981 log->l_iclog_heads = 1;
982
Christoph Hellwig7153f8b2009-02-09 08:36:46 +0100983done:
984 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100985 if (mp->m_logbufs == 0)
986 mp->m_logbufs = log->l_iclog_bufs;
987 if (mp->m_logbsize == 0)
988 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989} /* xlog_get_iclog_buffer_size */
990
991
992/*
993 * This routine initializes some of the log structure for a given mount point.
994 * Its primary purpose is to fill in enough, so recovery can occur. However,
995 * some other stuff may be filled in too.
996 */
997STATIC xlog_t *
998xlog_alloc_log(xfs_mount_t *mp,
999 xfs_buftarg_t *log_target,
1000 xfs_daddr_t blk_offset,
1001 int num_bblks)
1002{
1003 xlog_t *log;
1004 xlog_rec_header_t *head;
1005 xlog_in_core_t **iclogp;
1006 xlog_in_core_t *iclog, *prev_iclog=NULL;
1007 xfs_buf_t *bp;
1008 int i;
1009 int iclogsize;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001010 int error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Dave Chinner644c3562008-11-10 16:50:24 +11001012 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001013 if (!log) {
1014 xlog_warn("XFS: Log allocation failed: No memory!");
1015 goto out;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 log->l_mp = mp;
1019 log->l_targ = log_target;
1020 log->l_logsize = BBTOB(num_bblks);
1021 log->l_logBBstart = blk_offset;
1022 log->l_logBBsize = num_bblks;
1023 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1024 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1025
1026 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001027 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1029 log->l_last_sync_lsn = log->l_tail_lsn;
1030 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1031 log->l_grant_reserve_cycle = 1;
1032 log->l_grant_write_cycle = 1;
1033
Dave Chinnera6cb7672009-04-06 18:39:27 +02001034 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001035 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001037 if (log->l_sectbb_log < 0 ||
1038 log->l_sectbb_log > mp->m_sectbb_log) {
1039 xlog_warn("XFS: Log sector size (0x%x) out of range.",
1040 log->l_sectbb_log);
1041 goto out_free_log;
1042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* for larger sector sizes, must have v2 or external log */
Dave Chinnera6cb7672009-04-06 18:39:27 +02001045 if (log->l_sectbb_log != 0 &&
1046 (log->l_logBBstart != 0 &&
1047 !xfs_sb_version_haslogv2(&mp->m_sb))) {
1048 xlog_warn("XFS: log sector size (0x%x) invalid "
1049 "for configuration.", log->l_sectbb_log);
1050 goto out_free_log;
1051 }
1052 if (mp->m_sb.sb_logsectlog < BBSHIFT) {
1053 xlog_warn("XFS: Log sector log (0x%x) too small.",
1054 mp->m_sb.sb_logsectlog);
1055 goto out_free_log;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1059
1060 xlog_get_iclog_buffer_size(mp, log);
1061
Dave Chinnera6cb7672009-04-06 18:39:27 +02001062 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001064 if (!bp)
1065 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1068 ASSERT(XFS_BUF_ISBUSY(bp));
1069 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1070 log->l_xbuf = bp;
1071
Eric Sandeen007c61c2007-10-11 17:43:56 +10001072 spin_lock_init(&log->l_icloglock);
1073 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001074 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1077 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1078
1079 iclogp = &log->l_iclog;
1080 /*
1081 * The amount of memory to allocate for the iclog structure is
1082 * rather funky due to the way the structure is defined. It is
1083 * done this way so that we can use different sizes for machines
1084 * with different amounts of memory. See the definition of
1085 * xlog_in_core_t in xfs_log_priv.h for details.
1086 */
1087 iclogsize = log->l_iclog_size;
1088 ASSERT(log->l_iclog_size >= 4096);
1089 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001090 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1091 if (!*iclogp)
1092 goto out_free_iclog;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 iclog->ic_prev = prev_iclog;
1096 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001097
1098 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001099 if (!bp)
1100 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001101 if (!XFS_BUF_CPSEMA(bp))
1102 ASSERT(0);
1103 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001104 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1105 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001106 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001107#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 head = &iclog->ic_header;
1111 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001112 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1113 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001114 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001115 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001117 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1121 iclog->ic_state = XLOG_STATE_ACTIVE;
1122 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001123 atomic_set(&iclog->ic_refcnt, 0);
1124 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001126 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1129 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001130 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1131 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 iclogp = &iclog->ic_next;
1134 }
1135 *iclogp = log->l_iclog; /* complete ring */
1136 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1137
1138 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001139
1140out_free_iclog:
1141 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1142 prev_iclog = iclog->ic_next;
1143 if (iclog->ic_bp) {
1144 sv_destroy(&iclog->ic_force_wait);
1145 sv_destroy(&iclog->ic_write_wait);
1146 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001147 }
1148 kmem_free(iclog);
1149 }
1150 spinlock_destroy(&log->l_icloglock);
1151 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001152 xfs_buf_free(log->l_xbuf);
1153out_free_log:
1154 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001155out:
1156 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157} /* xlog_alloc_log */
1158
1159
1160/*
1161 * Write out the commit record of a transaction associated with the given
1162 * ticket. Return the lsn of the commit record.
1163 */
1164STATIC int
1165xlog_commit_record(xfs_mount_t *mp,
1166 xlog_ticket_t *ticket,
1167 xlog_in_core_t **iclog,
1168 xfs_lsn_t *commitlsnp)
1169{
1170 int error;
1171 xfs_log_iovec_t reg[1];
1172
1173 reg[0].i_addr = NULL;
1174 reg[0].i_len = 0;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +00001175 reg[0].i_type = XLOG_REG_TYPE_COMMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 ASSERT_ALWAYS(iclog);
1178 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1179 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001180 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001182 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183} /* xlog_commit_record */
1184
1185
1186/*
1187 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1188 * log space. This code pushes on the lsn which would supposedly free up
1189 * the 25% which we want to leave free. We may need to adopt a policy which
1190 * pushes on an lsn which is further along in the log once we reach the high
1191 * water mark. In this manner, we would be creating a low water mark.
1192 */
David Chinnera8272ce2007-11-23 16:28:09 +11001193STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194xlog_grant_push_ail(xfs_mount_t *mp,
1195 int need_bytes)
1196{
1197 xlog_t *log = mp->m_log; /* pointer to the log */
1198 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1199 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1200 int free_blocks; /* free blocks left to write to */
1201 int free_bytes; /* free bytes left to write to */
1202 int threshold_block; /* block in lsn we'd like to be at */
1203 int threshold_cycle; /* lsn cycle we'd like to be at */
1204 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1207
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001208 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 free_bytes = xlog_space_left(log,
1210 log->l_grant_reserve_cycle,
1211 log->l_grant_reserve_bytes);
1212 tail_lsn = log->l_tail_lsn;
1213 free_blocks = BTOBBT(free_bytes);
1214
1215 /*
1216 * Set the threshold for the minimum number of free blocks in the
1217 * log to the maximum of what the caller needs, one quarter of the
1218 * log, and 256 blocks.
1219 */
1220 free_threshold = BTOBB(need_bytes);
1221 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1222 free_threshold = MAX(free_threshold, 256);
1223 if (free_blocks < free_threshold) {
1224 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1225 threshold_cycle = CYCLE_LSN(tail_lsn);
1226 if (threshold_block >= log->l_logBBsize) {
1227 threshold_block -= log->l_logBBsize;
1228 threshold_cycle += 1;
1229 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001230 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Don't pass in an lsn greater than the lsn of the last
1233 * log record known to be on disk.
1234 */
1235 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1236 threshold_lsn = log->l_last_sync_lsn;
1237 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001238 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * Get the transaction layer to kick the dirty buffers out to
1242 * disk asynchronously. No point in trying to do this if
1243 * the filesystem is shutting down.
1244 */
1245 if (threshold_lsn &&
1246 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001247 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248} /* xlog_grant_push_ail */
1249
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001250/*
1251 * The bdstrat callback function for log bufs. This gives us a central
1252 * place to trap bufs in case we get hit by a log I/O error and need to
1253 * shutdown. Actually, in practice, even when we didn't get a log error,
1254 * we transition the iclogs to IOERROR state *after* flushing all existing
1255 * iclogs to disk. This is because we don't want anymore new transactions to be
1256 * started or completed afterwards.
1257 */
1258STATIC int
1259xlog_bdstrat(
1260 struct xfs_buf *bp)
1261{
1262 struct xlog_in_core *iclog;
1263
1264 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1265 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1266 XFS_BUF_ERROR(bp, EIO);
1267 XFS_BUF_STALE(bp);
1268 xfs_biodone(bp);
1269 /*
1270 * It would seem logical to return EIO here, but we rely on
1271 * the log state machine to propagate I/O errors instead of
1272 * doing it here.
1273 */
1274 return 0;
1275 }
1276
1277 bp->b_flags |= _XBF_RUN_QUEUES;
1278 xfs_buf_iorequest(bp);
1279 return 0;
1280}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282/*
1283 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1284 * fashion. Previously, we should have moved the current iclog
1285 * ptr in the log to point to the next available iclog. This allows further
1286 * write to continue while this code syncs out an iclog ready to go.
1287 * Before an in-core log can be written out, the data section must be scanned
1288 * to save away the 1st word of each BBSIZE block into the header. We replace
1289 * it with the current cycle count. Each BBSIZE block is tagged with the
1290 * cycle count because there in an implicit assumption that drives will
1291 * guarantee that entire 512 byte blocks get written at once. In other words,
1292 * we can't have part of a 512 byte block written and part not written. By
1293 * tagging each block, we will know which blocks are valid when recovering
1294 * after an unclean shutdown.
1295 *
1296 * This routine is single threaded on the iclog. No other thread can be in
1297 * this routine with the same iclog. Changing contents of iclog can there-
1298 * fore be done without grabbing the state machine lock. Updating the global
1299 * log will require grabbing the lock though.
1300 *
1301 * The entire log manager uses a logical block numbering scheme. Only
1302 * log_sync (and then only bwrite()) know about the fact that the log may
1303 * not start with block zero on a given device. The log block start offset
1304 * is added immediately before calling bwrite().
1305 */
1306
David Chinnera8272ce2007-11-23 16:28:09 +11001307STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308xlog_sync(xlog_t *log,
1309 xlog_in_core_t *iclog)
1310{
1311 xfs_caddr_t dptr; /* pointer to byte sized element */
1312 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001313 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 uint count; /* byte count of bwrite */
1315 uint count_init; /* initial count before roundup */
1316 int roundoff; /* roundoff to BB or stripe */
1317 int split = 0; /* split write into two regions */
1318 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001319 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001322 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /* Add for LR header */
1325 count_init = log->l_iclog_hsize + iclog->ic_offset;
1326
1327 /* Round out the log write size */
1328 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1329 /* we have a v2 stripe unit to use */
1330 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1331 } else {
1332 count = BBTOB(BTOBB(count_init));
1333 }
1334 roundoff = count - count_init;
1335 ASSERT(roundoff >= 0);
1336 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1337 roundoff < log->l_mp->m_sb.sb_logsunit)
1338 ||
1339 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1340 roundoff < BBTOB(1)));
1341
1342 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001343 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001344 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001345 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /* put cycle number in every block */
1348 xlog_pack_data(log, iclog, roundoff);
1349
1350 /* real byte length */
1351 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001352 iclog->ic_header.h_len =
1353 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001355 iclog->ic_header.h_len =
1356 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358
Nathan Scottf5faad72006-07-28 17:04:44 +10001359 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1361 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001362 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1365
1366 /* Do we need to split this write into 2 parts? */
1367 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1368 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1369 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1370 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1371 } else {
1372 iclog->ic_bwritecnt = 1;
1373 }
David Chinner511105b2007-05-24 15:21:57 +10001374 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001376 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 XFS_BUF_BUSY(bp);
1378 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001379 bp->b_flags |= XBF_LOG_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001381 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001382 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001384 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001385 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1388 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1389
1390 xlog_verify_iclog(log, iclog, count, B_TRUE);
1391
1392 /* account for log which doesn't start at block #0 */
1393 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1394 /*
1395 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1396 * is shutting down.
1397 */
1398 XFS_BUF_WRITE(bp);
1399
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001400 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1402 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001406 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1408 (unsigned long)1);
1409 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1410 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1411 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1412 (__psint_t)count), split);
1413 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001414 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 XFS_BUF_BUSY(bp);
1416 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001417 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001418 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1419 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 dptr = XFS_BUF_PTR(bp);
1421 /*
1422 * Bump the cycle numbers at the start of each block
1423 * since this part of the buffer is at the start of
1424 * a new cycle. Watch out for the header magic number
1425 * case, though.
1426 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001427 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001428 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001429 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001430 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 dptr += BBSIZE;
1432 }
1433
1434 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1435 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1436
Nathan Scottc41564b2006-03-29 08:55:14 +10001437 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1439 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001440 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1442 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447} /* xlog_sync */
1448
1449
1450/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001451 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
David Chinnera8272ce2007-11-23 16:28:09 +11001453STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001454xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int i;
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 iclog = log->l_iclog;
1460 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001461 sv_destroy(&iclog->ic_force_wait);
1462 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001465 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 iclog = next_iclog;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 spinlock_destroy(&log->l_icloglock);
1469 spinlock_destroy(&log->l_grant_lock);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001473 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001474} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476/*
1477 * Update counters atomically now that memcpy is done.
1478 */
1479/* ARGSUSED */
1480static inline void
1481xlog_state_finish_copy(xlog_t *log,
1482 xlog_in_core_t *iclog,
1483 int record_cnt,
1484 int copy_bytes)
1485{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001486 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001488 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 iclog->ic_offset += copy_bytes;
1490
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001491 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492} /* xlog_state_finish_copy */
1493
1494
1495
1496
1497/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001498 * print out info relating to regions written which consume
1499 * the reservation
1500 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001501STATIC void
1502xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1503{
1504 uint i;
1505 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1506
1507 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1508 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1509 "bformat",
1510 "bchunk",
1511 "efi_format",
1512 "efd_format",
1513 "iformat",
1514 "icore",
1515 "iext",
1516 "ibroot",
1517 "ilocal",
1518 "iattr_ext",
1519 "iattr_broot",
1520 "iattr_local",
1521 "qformat",
1522 "dquot",
1523 "quotaoff",
1524 "LR header",
1525 "unmount",
1526 "commit",
1527 "trans header"
1528 };
1529 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1530 "SETATTR_NOT_SIZE",
1531 "SETATTR_SIZE",
1532 "INACTIVE",
1533 "CREATE",
1534 "CREATE_TRUNC",
1535 "TRUNCATE_FILE",
1536 "REMOVE",
1537 "LINK",
1538 "RENAME",
1539 "MKDIR",
1540 "RMDIR",
1541 "SYMLINK",
1542 "SET_DMATTRS",
1543 "GROWFS",
1544 "STRAT_WRITE",
1545 "DIOSTRAT",
1546 "WRITE_SYNC",
1547 "WRITEID",
1548 "ADDAFORK",
1549 "ATTRINVAL",
1550 "ATRUNCATE",
1551 "ATTR_SET",
1552 "ATTR_RM",
1553 "ATTR_FLAG",
1554 "CLEAR_AGI_BUCKET",
1555 "QM_SBCHANGE",
1556 "DUMMY1",
1557 "DUMMY2",
1558 "QM_QUOTAOFF",
1559 "QM_DQALLOC",
1560 "QM_SETQLIM",
1561 "QM_DQCLUSTER",
1562 "QM_QINOCREATE",
1563 "QM_QUOTAOFF_END",
1564 "SB_UNIT",
1565 "FSYNC_TS",
1566 "GROWFSRT_ALLOC",
1567 "GROWFSRT_ZERO",
1568 "GROWFSRT_FREE",
1569 "SWAPEXT"
1570 };
1571
1572 xfs_fs_cmn_err(CE_WARN, mp,
1573 "xfs_log_write: reservation summary:\n"
1574 " trans type = %s (%u)\n"
1575 " unit res = %d bytes\n"
1576 " current res = %d bytes\n"
1577 " total reg = %u bytes (o/flow = %u bytes)\n"
1578 " ophdrs = %u (ophdr space = %u bytes)\n"
1579 " ophdr + reg = %u bytes\n"
1580 " num regions = %u\n",
1581 ((ticket->t_trans_type <= 0 ||
1582 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1583 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1584 ticket->t_trans_type,
1585 ticket->t_unit_res,
1586 ticket->t_curr_res,
1587 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1588 ticket->t_res_num_ophdrs, ophdr_spc,
1589 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001590 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001591 ticket->t_res_num);
1592
1593 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001594 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001595 cmn_err(CE_WARN,
1596 "region[%u]: %s - %u bytes\n",
1597 i,
1598 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1599 "bad-rtype" : res_type_str[r_type-1]),
1600 ticket->t_res_arr[i].r_len);
1601 }
1602}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001603
1604/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 * Write some region out to in-core log
1606 *
1607 * This will be called when writing externally provided regions or when
1608 * writing out a commit record for a given transaction.
1609 *
1610 * General algorithm:
1611 * 1. Find total length of this write. This may include adding to the
1612 * lengths passed in.
1613 * 2. Check whether we violate the tickets reservation.
1614 * 3. While writing to this iclog
1615 * A. Reserve as much space in this iclog as can get
1616 * B. If this is first write, save away start lsn
1617 * C. While writing this region:
1618 * 1. If first write of transaction, write start record
1619 * 2. Write log operation header (header per region)
1620 * 3. Find out if we can fit entire region into this iclog
1621 * 4. Potentially, verify destination memcpy ptr
1622 * 5. Memcpy (partial) region
1623 * 6. If partial copy, release iclog; otherwise, continue
1624 * copying more regions into current iclog
1625 * 4. Mark want sync bit (in simulation mode)
1626 * 5. Release iclog for potential flush to on-disk log.
1627 *
1628 * ERRORS:
1629 * 1. Panic if reservation is overrun. This should never happen since
1630 * reservation amounts are generated internal to the filesystem.
1631 * NOTES:
1632 * 1. Tickets are single threaded data structures.
1633 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1634 * syncing routine. When a single log_write region needs to span
1635 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1636 * on all log operation writes which don't contain the end of the
1637 * region. The XLOG_END_TRANS bit is used for the in-core log
1638 * operation which contains the end of the continued log_write region.
1639 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1640 * we don't really know exactly how much space will be used. As a result,
1641 * we don't update ic_offset until the end when we know exactly how many
1642 * bytes have been written out.
1643 */
David Chinnera8272ce2007-11-23 16:28:09 +11001644STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645xlog_write(xfs_mount_t * mp,
1646 xfs_log_iovec_t reg[],
1647 int nentries,
1648 xfs_log_ticket_t tic,
1649 xfs_lsn_t *start_lsn,
1650 xlog_in_core_t **commit_iclog,
1651 uint flags)
1652{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001653 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001655 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 __psint_t ptr; /* copy address into data region */
1658 int len; /* # xlog_write() bytes 2 still copy */
1659 int index; /* region index currently copying */
1660 int log_offset; /* offset (from 0) into data region */
1661 int start_rec_copy; /* # bytes to copy for start record */
1662 int partial_copy; /* did we split a region? */
1663 int partial_copy_len;/* # bytes copied if split region */
1664 int need_copy; /* # bytes need to memcpy this region */
1665 int copy_len; /* # bytes actually memcpy'ing */
1666 int copy_off; /* # bytes from entry start */
1667 int contwr; /* continued write of in-core log? */
1668 int error;
1669 int record_cnt = 0, data_cnt = 0;
1670
1671 partial_copy_len = partial_copy = 0;
1672
1673 /* Calculate potential maximum space. Each region gets its own
1674 * xlog_op_header_t and may need to be double word aligned.
1675 */
1676 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001677 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001679 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 for (index = 0; index < nentries; index++) {
1683 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001684 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001686 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688 contwr = *start_lsn = 0;
1689
1690 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001691 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692#ifdef DEBUG
1693 xlog_panic(
1694 "xfs_log_write: reservation ran out. Need to up reservation");
1695#else
1696 /* Customer configurable panic */
1697 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1698 "xfs_log_write: reservation ran out. Need to up reservation");
1699 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001700 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701#endif
1702 } else
1703 ticket->t_curr_res -= len;
1704
1705 for (index = 0; index < nentries; ) {
1706 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1707 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001708 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 ASSERT(log_offset <= iclog->ic_size - 1);
1711 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1712
1713 /* start_lsn is the first lsn written to. That's all we need. */
1714 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001715 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /* This loop writes out as many regions as can fit in the amount
1718 * of space which was allocated by xlog_state_get_iclog_space().
1719 */
1720 while (index < nentries) {
1721 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1722 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1723 start_rec_copy = 0;
1724
1725 /* If first write for transaction, insert start record.
1726 * We can't be trying to commit if we are inited. We can't
1727 * have any "partial_copy" if we are inited.
1728 */
1729 if (ticket->t_flags & XLOG_TIC_INITED) {
1730 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001731 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 logop_head->oh_clientid = ticket->t_clientid;
1733 logop_head->oh_len = 0;
1734 logop_head->oh_flags = XLOG_START_TRANS;
1735 logop_head->oh_res2 = 0;
1736 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1737 record_cnt++;
1738
1739 start_rec_copy = sizeof(xlog_op_header_t);
1740 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1741 }
1742
1743 /* Copy log operation header directly into data section */
1744 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001745 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 logop_head->oh_clientid = ticket->t_clientid;
1747 logop_head->oh_res2 = 0;
1748
1749 /* header copied directly */
1750 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1751
1752 /* are we copying a commit or unmount record? */
1753 logop_head->oh_flags = flags;
1754
1755 /*
1756 * We've seen logs corrupted with bad transaction client
1757 * ids. This makes sure that XFS doesn't generate them on.
1758 * Turn this into an EIO and shut down the filesystem.
1759 */
1760 switch (logop_head->oh_clientid) {
1761 case XFS_TRANSACTION:
1762 case XFS_VOLUME:
1763 case XFS_LOG:
1764 break;
1765 default:
1766 xfs_fs_cmn_err(CE_WARN, mp,
1767 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1768 logop_head->oh_clientid, tic);
1769 return XFS_ERROR(EIO);
1770 }
1771
1772 /* Partial write last time? => (partial_copy != 0)
1773 * need_copy is the amount we'd like to copy if everything could
1774 * fit in the current memcpy.
1775 */
1776 need_copy = reg[index].i_len - partial_copy_len;
1777
1778 copy_off = partial_copy_len;
1779 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001780 copy_len = need_copy;
1781 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (partial_copy)
1783 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1784 partial_copy_len = partial_copy = 0;
1785 } else { /* partial write */
1786 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001787 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1789 if (partial_copy)
1790 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1791 partial_copy_len += copy_len;
1792 partial_copy++;
1793 len += sizeof(xlog_op_header_t); /* from splitting of region */
1794 /* account for new log op header */
1795 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001796 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 xlog_verify_dest_ptr(log, ptr);
1799
1800 /* copy region */
1801 ASSERT(copy_len >= 0);
1802 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1803 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1804
1805 /* make copy_len total bytes copied, including headers */
1806 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1807 record_cnt++;
1808 data_cnt += contwr ? copy_len : 0;
1809 if (partial_copy) { /* copied partial region */
1810 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1811 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1812 record_cnt = data_cnt = 0;
1813 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001814 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break; /* don't increment index */
1816 } else { /* copied entire region */
1817 index++;
1818 partial_copy_len = partial_copy = 0;
1819
1820 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1821 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1822 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001823 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001825 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (commit_iclog) {
1827 ASSERT(flags & XLOG_COMMIT_TRANS);
1828 *commit_iclog = iclog;
1829 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001830 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (index == nentries)
1832 return 0; /* we are done */
1833 else
1834 break;
1835 }
1836 } /* if (partial_copy) */
1837 } /* while (index < nentries) */
1838 } /* for (index = 0; index < nentries; ) */
1839 ASSERT(len == 0);
1840
1841 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1842 if (commit_iclog) {
1843 ASSERT(flags & XLOG_COMMIT_TRANS);
1844 *commit_iclog = iclog;
1845 return 0;
1846 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001847 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848} /* xlog_write */
1849
1850
1851/*****************************************************************************
1852 *
1853 * State Machine functions
1854 *
1855 *****************************************************************************
1856 */
1857
1858/* Clean iclogs starting from the head. This ordering must be
1859 * maintained, so an iclog doesn't become ACTIVE beyond one that
1860 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001861 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * iclog is trying to sync to disk.
1863 *
1864 * State Change: DIRTY -> ACTIVE
1865 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001866STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867xlog_state_clean_log(xlog_t *log)
1868{
1869 xlog_in_core_t *iclog;
1870 int changed = 0;
1871
1872 iclog = log->l_iclog;
1873 do {
1874 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1875 iclog->ic_state = XLOG_STATE_ACTIVE;
1876 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001877 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /*
1879 * If the number of ops in this iclog indicate it just
1880 * contains the dummy transaction, we can
1881 * change state into IDLE (the second time around).
1882 * Otherwise we should change the state into
1883 * NEED a dummy.
1884 * We don't need to cover the dummy.
1885 */
1886 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001887 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1888 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 changed = 1;
1890 } else {
1891 /*
1892 * We have two dirty iclogs so start over
1893 * This could also be num of ops indicates
1894 * this is not the dummy going out.
1895 */
1896 changed = 2;
1897 }
1898 iclog->ic_header.h_num_logops = 0;
1899 memset(iclog->ic_header.h_cycle_data, 0,
1900 sizeof(iclog->ic_header.h_cycle_data));
1901 iclog->ic_header.h_lsn = 0;
1902 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1903 /* do nothing */;
1904 else
1905 break; /* stop cleaning */
1906 iclog = iclog->ic_next;
1907 } while (iclog != log->l_iclog);
1908
1909 /* log is locked when we are called */
1910 /*
1911 * Change state for the dummy log recording.
1912 * We usually go to NEED. But we go to NEED2 if the changed indicates
1913 * we are done writing the dummy record.
1914 * If we are done with the second dummy recored (DONE2), then
1915 * we go to IDLE.
1916 */
1917 if (changed) {
1918 switch (log->l_covered_state) {
1919 case XLOG_STATE_COVER_IDLE:
1920 case XLOG_STATE_COVER_NEED:
1921 case XLOG_STATE_COVER_NEED2:
1922 log->l_covered_state = XLOG_STATE_COVER_NEED;
1923 break;
1924
1925 case XLOG_STATE_COVER_DONE:
1926 if (changed == 1)
1927 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1928 else
1929 log->l_covered_state = XLOG_STATE_COVER_NEED;
1930 break;
1931
1932 case XLOG_STATE_COVER_DONE2:
1933 if (changed == 1)
1934 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1935 else
1936 log->l_covered_state = XLOG_STATE_COVER_NEED;
1937 break;
1938
1939 default:
1940 ASSERT(0);
1941 }
1942 }
1943} /* xlog_state_clean_log */
1944
1945STATIC xfs_lsn_t
1946xlog_get_lowest_lsn(
1947 xlog_t *log)
1948{
1949 xlog_in_core_t *lsn_log;
1950 xfs_lsn_t lowest_lsn, lsn;
1951
1952 lsn_log = log->l_iclog;
1953 lowest_lsn = 0;
1954 do {
1955 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001956 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if ((lsn && !lowest_lsn) ||
1958 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
1959 lowest_lsn = lsn;
1960 }
1961 }
1962 lsn_log = lsn_log->ic_next;
1963 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01001964 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967
1968STATIC void
1969xlog_state_do_callback(
1970 xlog_t *log,
1971 int aborted,
1972 xlog_in_core_t *ciclog)
1973{
1974 xlog_in_core_t *iclog;
1975 xlog_in_core_t *first_iclog; /* used to know when we've
1976 * processed all iclogs once */
1977 xfs_log_callback_t *cb, *cb_next;
1978 int flushcnt = 0;
1979 xfs_lsn_t lowest_lsn;
1980 int ioerrors; /* counter: iclogs with errors */
1981 int loopdidcallbacks; /* flag: inner loop did callbacks*/
1982 int funcdidcallbacks; /* flag: function did callbacks */
1983 int repeats; /* for issuing console warnings if
1984 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001985 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001987 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 first_iclog = iclog = log->l_iclog;
1989 ioerrors = 0;
1990 funcdidcallbacks = 0;
1991 repeats = 0;
1992
1993 do {
1994 /*
1995 * Scan all iclogs starting with the one pointed to by the
1996 * log. Reset this starting point each time the log is
1997 * unlocked (during callbacks).
1998 *
1999 * Keep looping through iclogs until one full pass is made
2000 * without running any callbacks.
2001 */
2002 first_iclog = log->l_iclog;
2003 iclog = log->l_iclog;
2004 loopdidcallbacks = 0;
2005 repeats++;
2006
2007 do {
2008
2009 /* skip all iclogs in the ACTIVE & DIRTY states */
2010 if (iclog->ic_state &
2011 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2012 iclog = iclog->ic_next;
2013 continue;
2014 }
2015
2016 /*
2017 * Between marking a filesystem SHUTDOWN and stopping
2018 * the log, we do flush all iclogs to disk (if there
2019 * wasn't a log I/O error). So, we do want things to
2020 * go smoothly in case of just a SHUTDOWN w/o a
2021 * LOG_IO_ERROR.
2022 */
2023 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2024 /*
2025 * Can only perform callbacks in order. Since
2026 * this iclog is not in the DONE_SYNC/
2027 * DO_CALLBACK state, we skip the rest and
2028 * just try to clean up. If we set our iclog
2029 * to DO_CALLBACK, we will not process it when
2030 * we retry since a previous iclog is in the
2031 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002032 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
2034 if (!(iclog->ic_state &
2035 (XLOG_STATE_DONE_SYNC |
2036 XLOG_STATE_DO_CALLBACK))) {
2037 if (ciclog && (ciclog->ic_state ==
2038 XLOG_STATE_DONE_SYNC)) {
2039 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2040 }
2041 break;
2042 }
2043 /*
2044 * We now have an iclog that is in either the
2045 * DO_CALLBACK or DONE_SYNC states. The other
2046 * states (WANT_SYNC, SYNCING, or CALLBACK were
2047 * caught by the above if and are going to
2048 * clean (i.e. we aren't doing their callbacks)
2049 * see the above if.
2050 */
2051
2052 /*
2053 * We will do one more check here to see if we
2054 * have chased our tail around.
2055 */
2056
2057 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002058 if (lowest_lsn &&
2059 XFS_LSN_CMP(lowest_lsn,
2060 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 iclog = iclog->ic_next;
2062 continue; /* Leave this iclog for
2063 * another thread */
2064 }
2065
2066 iclog->ic_state = XLOG_STATE_CALLBACK;
2067
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002068 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002071 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 * No one else can be here except us.
2073 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002074 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002075 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2076 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2077 log->l_last_sync_lsn =
2078 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002079 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002082 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 ioerrors++;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
David Chinner114d23a2008-04-10 12:18:39 +10002086 /*
2087 * Keep processing entries in the callback list until
2088 * we come around and it is empty. We need to
2089 * atomically see that the list is empty and change the
2090 * state to DIRTY so that we don't miss any more
2091 * callbacks being added.
2092 */
2093 spin_lock(&iclog->ic_callback_lock);
2094 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002095 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 iclog->ic_callback_tail = &(iclog->ic_callback);
2097 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002098 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002101 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 cb_next = cb->cb_next;
2103 cb->cb_func(cb->cb_arg, aborted);
2104 }
David Chinner114d23a2008-04-10 12:18:39 +10002105 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 cb = iclog->ic_callback;
2107 }
2108
2109 loopdidcallbacks++;
2110 funcdidcallbacks++;
2111
David Chinner114d23a2008-04-10 12:18:39 +10002112 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002113 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002114 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2116 iclog->ic_state = XLOG_STATE_DIRTY;
2117
2118 /*
2119 * Transition from DIRTY to ACTIVE if applicable.
2120 * NOP if STATE_IOERROR.
2121 */
2122 xlog_state_clean_log(log);
2123
2124 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002125 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 iclog = iclog->ic_next;
2128 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002129
2130 if (repeats > 5000) {
2131 flushcnt += repeats;
2132 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002134 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002135 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137 } while (!ioerrors && loopdidcallbacks);
2138
2139 /*
2140 * make one last gasp attempt to see if iclogs are being left in
2141 * limbo..
2142 */
2143#ifdef DEBUG
2144 if (funcdidcallbacks) {
2145 first_iclog = iclog = log->l_iclog;
2146 do {
2147 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2148 /*
2149 * Terminate the loop if iclogs are found in states
2150 * which will cause other threads to clean up iclogs.
2151 *
2152 * SYNCING - i/o completion will go through logs
2153 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002154 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * IOERROR - give up hope all ye who enter here
2156 */
2157 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2158 iclog->ic_state == XLOG_STATE_SYNCING ||
2159 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2160 iclog->ic_state == XLOG_STATE_IOERROR )
2161 break;
2162 iclog = iclog->ic_next;
2163 } while (first_iclog != iclog);
2164 }
2165#endif
2166
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002167 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2168 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002169 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002170
2171 if (wake)
2172 sv_broadcast(&log->l_flush_wait);
2173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175
2176/*
2177 * Finish transitioning this iclog to the dirty state.
2178 *
2179 * Make sure that we completely execute this routine only when this is
2180 * the last call to the iclog. There is a good chance that iclog flushes,
2181 * when we reach the end of the physical log, get turned into 2 separate
2182 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2183 * routine. By using the reference count bwritecnt, we guarantee that only
2184 * the second completion goes through.
2185 *
2186 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002187 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 */
David Chinnera8272ce2007-11-23 16:28:09 +11002189STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190xlog_state_done_syncing(
2191 xlog_in_core_t *iclog,
2192 int aborted)
2193{
2194 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002196 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2199 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002200 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2202
2203
2204 /*
2205 * If we got an error, either on the first buffer, or in the case of
2206 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2207 * and none should ever be attempted to be written to disk
2208 * again.
2209 */
2210 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2211 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002212 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return;
2214 }
2215 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2216 }
2217
2218 /*
2219 * Someone could be sleeping prior to writing out the next
2220 * iclog buffer, we wake them all, one will get to do the
2221 * I/O, the others get to wait for the result.
2222 */
David Chinner12017fa2008-08-13 16:34:31 +10002223 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002224 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2226} /* xlog_state_done_syncing */
2227
2228
2229/*
2230 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002231 * sleep. We wait on the flush queue on the head iclog as that should be
2232 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2233 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 *
2235 * The in-core logs are used in a circular fashion. They are not used
2236 * out-of-order even when an iclog past the head is free.
2237 *
2238 * return:
2239 * * log_offset where xlog_write() can start writing into the in-core
2240 * log's data space.
2241 * * in-core log pointer to which xlog_write() should write.
2242 * * boolean indicating this is a continued write to an in-core log.
2243 * If this is the last write, then the in-core log's offset field
2244 * needs to be incremented, depending on the amount of data which
2245 * is copied.
2246 */
David Chinnera8272ce2007-11-23 16:28:09 +11002247STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248xlog_state_get_iclog_space(xlog_t *log,
2249 int len,
2250 xlog_in_core_t **iclogp,
2251 xlog_ticket_t *ticket,
2252 int *continued_write,
2253 int *logoffsetp)
2254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int log_offset;
2256 xlog_rec_header_t *head;
2257 xlog_in_core_t *iclog;
2258 int error;
2259
2260restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002261 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002263 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return XFS_ERROR(EIO);
2265 }
2266
2267 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002268 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002270
2271 /* Wait for log writes to have flushed */
2272 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 goto restart;
2274 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 head = &iclog->ic_header;
2277
David Chinner155cc6b2008-03-06 13:44:14 +11002278 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 log_offset = iclog->ic_offset;
2280
2281 /* On the 1st write to an iclog, figure out lsn. This works
2282 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2283 * committing to. If the offset is set, that's how many blocks
2284 * must be written.
2285 */
2286 if (log_offset == 0) {
2287 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002288 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002289 log->l_iclog_hsize,
2290 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002291 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2292 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002293 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 ASSERT(log->l_curr_block >= 0);
2295 }
2296
2297 /* If there is enough room to write everything, then do it. Otherwise,
2298 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2299 * bit is on, so this will get flushed out. Don't update ic_offset
2300 * until you know exactly how many bytes get copied. Therefore, wait
2301 * until later to update ic_offset.
2302 *
2303 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2304 * can fit into remaining data section.
2305 */
2306 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2307 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2308
Dave Chinner49641f12008-07-11 17:43:55 +10002309 /*
2310 * If I'm the only one writing to this iclog, sync it to disk.
2311 * We need to do an atomic compare and decrement here to avoid
2312 * racing with concurrent atomic_dec_and_lock() calls in
2313 * xlog_state_release_iclog() when there is more than one
2314 * reference to the iclog.
2315 */
2316 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2317 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002318 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002319 error = xlog_state_release_iclog(log, iclog);
2320 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002321 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002323 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 goto restart;
2326 }
2327
2328 /* Do we have enough room to write the full amount in the remainder
2329 * of this iclog? Or must we continue a write on the next iclog and
2330 * mark this iclog as completely taken? In the case where we switch
2331 * iclogs (to mark it taken), this particular iclog will release/sync
2332 * to disk in xlog_write().
2333 */
2334 if (len <= iclog->ic_size - iclog->ic_offset) {
2335 *continued_write = 0;
2336 iclog->ic_offset += len;
2337 } else {
2338 *continued_write = 1;
2339 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2340 }
2341 *iclogp = iclog;
2342
2343 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002344 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 *logoffsetp = log_offset;
2347 return 0;
2348} /* xlog_state_get_iclog_space */
2349
2350/*
2351 * Atomically get the log space required for a log ticket.
2352 *
2353 * Once a ticket gets put onto the reserveq, it will only return after
2354 * the needed reservation is satisfied.
2355 */
2356STATIC int
2357xlog_grant_log_space(xlog_t *log,
2358 xlog_ticket_t *tic)
2359{
2360 int free_bytes;
2361 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362#ifdef DEBUG
2363 xfs_lsn_t tail_lsn;
2364#endif
2365
2366
2367#ifdef DEBUG
2368 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2369 panic("grant Recovery problem");
2370#endif
2371
2372 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002373 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002374
2375 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 /* something is already sleeping; insert new transaction at end */
2378 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002379 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002380
2381 trace_xfs_log_grant_sleep1(log, tic);
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
2384 * Gotta check this before going to sleep, while we're
2385 * holding the grant lock.
2386 */
2387 if (XLOG_FORCED_SHUTDOWN(log))
2388 goto error_return;
2389
2390 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002391 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /*
2393 * If we got an error, and the filesystem is shutting down,
2394 * we'll catch it down below. So just continue...
2395 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002396 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002397 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2400 need_bytes = tic->t_unit_res*tic->t_ocnt;
2401 else
2402 need_bytes = tic->t_unit_res;
2403
2404redo:
2405 if (XLOG_FORCED_SHUTDOWN(log))
2406 goto error_return;
2407
2408 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2409 log->l_grant_reserve_bytes);
2410 if (free_bytes < need_bytes) {
2411 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002412 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002413
2414 trace_xfs_log_grant_sleep2(log, tic);
2415
Dave Chinner9d7fef72009-04-06 18:42:59 +02002416 spin_unlock(&log->l_grant_lock);
2417 xlog_grant_push_ail(log->l_mp, need_bytes);
2418 spin_lock(&log->l_grant_lock);
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002421 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Dave Chinner9d7fef72009-04-06 18:42:59 +02002423 spin_lock(&log->l_grant_lock);
2424 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002427 trace_xfs_log_grant_wake2(log, tic);
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 goto redo;
2430 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002431 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002434 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435#ifdef DEBUG
2436 tail_lsn = log->l_tail_lsn;
2437 /*
2438 * Check to make sure the grant write head didn't just over lap the
2439 * tail. If the cycles are the same, we can't be overlapping.
2440 * Otherwise, make sure that the cycles differ by exactly one and
2441 * check the byte count.
2442 */
2443 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2444 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2445 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2446 }
2447#endif
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002448 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002450 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return 0;
2452
2453 error_return:
2454 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002455 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002456
2457 trace_xfs_log_grant_error(log, tic);
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /*
2460 * If we are failing, make sure the ticket doesn't have any
2461 * current reservations. We don't want to add this back when
2462 * the ticket/transaction gets cancelled.
2463 */
2464 tic->t_curr_res = 0;
2465 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002466 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return XFS_ERROR(EIO);
2468} /* xlog_grant_log_space */
2469
2470
2471/*
2472 * Replenish the byte reservation required by moving the grant write head.
2473 *
2474 *
2475 */
2476STATIC int
2477xlog_regrant_write_log_space(xlog_t *log,
2478 xlog_ticket_t *tic)
2479{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 int free_bytes, need_bytes;
2481 xlog_ticket_t *ntic;
2482#ifdef DEBUG
2483 xfs_lsn_t tail_lsn;
2484#endif
2485
2486 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002487 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492#ifdef DEBUG
2493 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2494 panic("regrant Recovery problem");
2495#endif
2496
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002497 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002498
2499 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 if (XLOG_FORCED_SHUTDOWN(log))
2502 goto error_return;
2503
2504 /* If there are other waiters on the queue then give them a
2505 * chance at logspace before us. Wake up the first waiters,
2506 * if we do not wake up all the waiters then go to sleep waiting
2507 * for more free space, otherwise try to get some space for
2508 * this transaction.
2509 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002510 need_bytes = tic->t_unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if ((ntic = log->l_write_headq)) {
2512 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2513 log->l_grant_write_bytes);
2514 do {
2515 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2516
2517 if (free_bytes < ntic->t_unit_res)
2518 break;
2519 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002520 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 ntic = ntic->t_next;
2522 } while (ntic != log->l_write_headq);
2523
2524 if (ntic != log->l_write_headq) {
2525 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002526 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002528 trace_xfs_log_regrant_write_sleep1(log, tic);
2529
Dave Chinner9d7fef72009-04-06 18:42:59 +02002530 spin_unlock(&log->l_grant_lock);
2531 xlog_grant_push_ail(log->l_mp, need_bytes);
2532 spin_lock(&log->l_grant_lock);
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002535 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 &log->l_grant_lock, s);
2537
2538 /* If we're shutting down, this tic is already
2539 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002540 spin_lock(&log->l_grant_lock);
2541 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002544 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
2546 }
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548redo:
2549 if (XLOG_FORCED_SHUTDOWN(log))
2550 goto error_return;
2551
2552 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2553 log->l_grant_write_bytes);
2554 if (free_bytes < need_bytes) {
2555 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002556 xlog_ins_ticketq(&log->l_write_headq, tic);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002557 spin_unlock(&log->l_grant_lock);
2558 xlog_grant_push_ail(log->l_mp, need_bytes);
2559 spin_lock(&log->l_grant_lock);
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002562 trace_xfs_log_regrant_write_sleep2(log, tic);
2563
David Chinner12017fa2008-08-13 16:34:31 +10002564 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002567 spin_lock(&log->l_grant_lock);
2568 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002571 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 goto redo;
2573 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002574 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002576 /* we've got enough space */
2577 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578#ifdef DEBUG
2579 tail_lsn = log->l_tail_lsn;
2580 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2581 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2582 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2583 }
2584#endif
2585
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002586 trace_xfs_log_regrant_write_exit(log, tic);
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002589 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592
2593 error_return:
2594 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002595 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002596
2597 trace_xfs_log_regrant_write_error(log, tic);
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /*
2600 * If we are failing, make sure the ticket doesn't have any
2601 * current reservations. We don't want to add this back when
2602 * the ticket/transaction gets cancelled.
2603 */
2604 tic->t_curr_res = 0;
2605 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002606 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return XFS_ERROR(EIO);
2608} /* xlog_regrant_write_log_space */
2609
2610
2611/* The first cnt-1 times through here we don't need to
2612 * move the grant write head because the permanent
2613 * reservation has reserved cnt times the unit amount.
2614 * Release part of current permanent unit reservation and
2615 * reset current reservation to be one units worth. Also
2616 * move grant reservation head forward.
2617 */
2618STATIC void
2619xlog_regrant_reserve_log_space(xlog_t *log,
2620 xlog_ticket_t *ticket)
2621{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002622 trace_xfs_log_regrant_reserve_enter(log, ticket);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (ticket->t_cnt > 0)
2625 ticket->t_cnt--;
2626
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002627 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002628 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002630 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002631
2632 trace_xfs_log_regrant_reserve_sub(log, ticket);
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 xlog_verify_grant_head(log, 1);
2635
2636 /* just return if we still have some of the pre-reserved space */
2637 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002638 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return;
2640 }
2641
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002642 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002643
2644 trace_xfs_log_regrant_reserve_exit(log, ticket);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002647 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002649 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650} /* xlog_regrant_reserve_log_space */
2651
2652
2653/*
2654 * Give back the space left from a reservation.
2655 *
2656 * All the information we need to make a correct determination of space left
2657 * is present. For non-permanent reservations, things are quite easy. The
2658 * count should have been decremented to zero. We only need to deal with the
2659 * space remaining in the current reservation part of the ticket. If the
2660 * ticket contains a permanent reservation, there may be left over space which
2661 * needs to be released. A count of N means that N-1 refills of the current
2662 * reservation can be done before we need to ask for more space. The first
2663 * one goes to fill up the first current reservation. Once we run out of
2664 * space, the count will stay at zero and the only space remaining will be
2665 * in the current reservation field.
2666 */
2667STATIC void
2668xlog_ungrant_log_space(xlog_t *log,
2669 xlog_ticket_t *ticket)
2670{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (ticket->t_cnt > 0)
2672 ticket->t_cnt--;
2673
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002674 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002675 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002677 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002679 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 /* If this is a permanent reservation ticket, we may be able to free
2682 * up more space based on the remaining count.
2683 */
2684 if (ticket->t_cnt > 0) {
2685 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002686 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002689 trace_xfs_log_ungrant_exit(log, ticket);
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002692 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 xfs_log_move_tail(log->l_mp, 1);
2694} /* xlog_ungrant_log_space */
2695
2696
2697/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * Flush iclog to disk if this is the last reference to the given iclog and
2699 * the WANT_SYNC bit is set.
2700 *
2701 * When this function is entered, the iclog is not necessarily in the
2702 * WANT_SYNC state. It may be sitting around waiting to get filled.
2703 *
2704 *
2705 */
David Chinnera8272ce2007-11-23 16:28:09 +11002706STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002707xlog_state_release_iclog(
2708 xlog_t *log,
2709 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 int sync = 0; /* do we sync? */
2712
David Chinner155cc6b2008-03-06 13:44:14 +11002713 if (iclog->ic_state & XLOG_STATE_IOERROR)
2714 return XFS_ERROR(EIO);
2715
2716 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2717 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2718 return 0;
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002721 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return XFS_ERROR(EIO);
2723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2725 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2726
David Chinner155cc6b2008-03-06 13:44:14 +11002727 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002728 /* update tail before writing to iclog */
2729 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 sync++;
2731 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002732 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2734 /* cycle incremented when incrementing curr_block */
2735 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002736 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 /*
2739 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002740 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2742 * this iclog has consistent data, so we ignore IOERROR
2743 * flags after this point.
2744 */
David Chinnerb5893342008-03-06 13:44:06 +11002745 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748} /* xlog_state_release_iclog */
2749
2750
2751/*
2752 * This routine will mark the current iclog in the ring as WANT_SYNC
2753 * and move the current iclog pointer to the next iclog in the ring.
2754 * When this routine is called from xlog_state_get_iclog_space(), the
2755 * exact size of the iclog has not yet been determined. All we know is
2756 * that every data block. We have run out of space in this log record.
2757 */
2758STATIC void
2759xlog_state_switch_iclogs(xlog_t *log,
2760 xlog_in_core_t *iclog,
2761 int eventual_size)
2762{
2763 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2764 if (!eventual_size)
2765 eventual_size = iclog->ic_offset;
2766 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002767 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 log->l_prev_block = log->l_curr_block;
2769 log->l_prev_cycle = log->l_curr_cycle;
2770
2771 /* roll log?: ic_offset changed later */
2772 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2773
2774 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002775 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 log->l_mp->m_sb.sb_logsunit > 1) {
2777 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2778 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2779 }
2780
2781 if (log->l_curr_block >= log->l_logBBsize) {
2782 log->l_curr_cycle++;
2783 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2784 log->l_curr_cycle++;
2785 log->l_curr_block -= log->l_logBBsize;
2786 ASSERT(log->l_curr_block >= 0);
2787 }
2788 ASSERT(iclog == log->l_iclog);
2789 log->l_iclog = iclog->ic_next;
2790} /* xlog_state_switch_iclogs */
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792/*
2793 * Write out all data in the in-core log as of this exact moment in time.
2794 *
2795 * Data may be written to the in-core log during this call. However,
2796 * we don't guarantee this data will be written out. A change from past
2797 * implementation means this routine will *not* write out zero length LRs.
2798 *
2799 * Basically, we try and perform an intelligent scan of the in-core logs.
2800 * If we determine there is no flushable data, we just return. There is no
2801 * flushable data if:
2802 *
2803 * 1. the current iclog is active and has no data; the previous iclog
2804 * is in the active or dirty state.
2805 * 2. the current iclog is drity, and the previous iclog is in the
2806 * active or dirty state.
2807 *
David Chinner12017fa2008-08-13 16:34:31 +10002808 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 *
2810 * 1. the current iclog is not in the active nor dirty state.
2811 * 2. the current iclog dirty, and the previous iclog is not in the
2812 * active nor dirty state.
2813 * 3. the current iclog is active, and there is another thread writing
2814 * to this particular iclog.
2815 * 4. a) the current iclog is active and has no other writers
2816 * b) when we return from flushing out this iclog, it is still
2817 * not in the active nor dirty state.
2818 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002819int
2820_xfs_log_force(
2821 struct xfs_mount *mp,
2822 uint flags,
2823 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002825 struct log *log = mp->m_log;
2826 struct xlog_in_core *iclog;
2827 xfs_lsn_t lsn;
2828
2829 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002831 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 iclog = log->l_iclog;
2834 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002835 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return XFS_ERROR(EIO);
2837 }
2838
2839 /* If the head iclog is not active nor dirty, we just attach
2840 * ourselves to the head and go to sleep.
2841 */
2842 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2843 iclog->ic_state == XLOG_STATE_DIRTY) {
2844 /*
2845 * If the head is dirty or (active and empty), then
2846 * we need to look at the previous iclog. If the previous
2847 * iclog is active or dirty we are done. There is nothing
2848 * to sync out. Otherwise, we attach ourselves to the
2849 * previous iclog and go to sleep.
2850 */
2851 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002852 (atomic_read(&iclog->ic_refcnt) == 0
2853 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 iclog = iclog->ic_prev;
2855 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2856 iclog->ic_state == XLOG_STATE_DIRTY)
2857 goto no_sleep;
2858 else
2859 goto maybe_sleep;
2860 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002861 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 /* We are the only one with access to this
2863 * iclog. Flush it out now. There should
2864 * be a roundoff of zero to show that someone
2865 * has already taken care of the roundoff from
2866 * the previous sync.
2867 */
David Chinner155cc6b2008-03-06 13:44:14 +11002868 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002869 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002871 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 if (xlog_state_release_iclog(log, iclog))
2874 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002875
2876 if (log_flushed)
2877 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002878 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002879 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 iclog->ic_state != XLOG_STATE_DIRTY)
2881 goto maybe_sleep;
2882 else
2883 goto no_sleep;
2884 } else {
2885 /* Someone else is writing to this iclog.
2886 * Use its call to flush out the data. However,
2887 * the other thread may not force out this LR,
2888 * so we mark it WANT_SYNC.
2889 */
2890 xlog_state_switch_iclogs(log, iclog, 0);
2891 goto maybe_sleep;
2892 }
2893 }
2894 }
2895
2896 /* By the time we come around again, the iclog could've been filled
2897 * which would give it another lsn. If we have a new lsn, just
2898 * return because the relevant data has been flushed.
2899 */
2900maybe_sleep:
2901 if (flags & XFS_LOG_SYNC) {
2902 /*
2903 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002904 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 * Then we check again after waking up, in case our
2906 * sleep was disturbed by a bad news.
2907 */
2908 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002909 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return XFS_ERROR(EIO);
2911 }
2912 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10002913 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 /*
2915 * No need to grab the log lock here since we're
2916 * only deciding whether or not to return EIO
2917 * and the memory read should be atomic.
2918 */
2919 if (iclog->ic_state & XLOG_STATE_IOERROR)
2920 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002921 if (log_flushed)
2922 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 } else {
2924
2925no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002926 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002929}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002932 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2933 * about errors or whether the log was flushed or not. This is the normal
2934 * interface to use when trying to unpin items or move the log forward.
2935 */
2936void
2937xfs_log_force(
2938 xfs_mount_t *mp,
2939 uint flags)
2940{
2941 int error;
2942
2943 error = _xfs_log_force(mp, flags, NULL);
2944 if (error) {
2945 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
2946 "error %d returned.", error);
2947 }
2948}
2949
2950/*
2951 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 *
2953 * Find in-core log with lsn.
2954 * If it is in the DIRTY state, just return.
2955 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2956 * state and go to sleep or return.
2957 * If it is in any other state, go to sleep or return.
2958 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002959 * Synchronous forces are implemented with a signal variable. All callers
2960 * to force a given lsn to disk will wait on a the sv attached to the
2961 * specific in-core log. When given in-core log finally completes its
2962 * write to disk, that thread will wake up all threads waiting on the
2963 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002965int
2966_xfs_log_force_lsn(
2967 struct xfs_mount *mp,
2968 xfs_lsn_t lsn,
2969 uint flags,
2970 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002972 struct log *log = mp->m_log;
2973 struct xlog_in_core *iclog;
2974 int already_slept = 0;
2975
2976 ASSERT(lsn != 0);
2977
2978 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002981 spin_lock(&log->l_icloglock);
2982 iclog = log->l_iclog;
2983 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002984 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002985 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002988 do {
2989 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
2990 iclog = iclog->ic_next;
2991 continue;
2992 }
2993
2994 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2995 spin_unlock(&log->l_icloglock);
2996 return 0;
2997 }
2998
2999 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3000 /*
3001 * We sleep here if we haven't already slept (e.g.
3002 * this is the first time we've looked at the correct
3003 * iclog buf) and the buffer before us is going to
3004 * be sync'ed. The reason for this is that if we
3005 * are doing sync transactions here, by waiting for
3006 * the previous I/O to complete, we can allow a few
3007 * more transactions into this iclog before we close
3008 * it down.
3009 *
3010 * Otherwise, we mark the buffer WANT_SYNC, and bump
3011 * up the refcnt so we can release the log (which
3012 * drops the ref count). The state switch keeps new
3013 * transaction commits from using this buffer. When
3014 * the current commits finish writing into the buffer,
3015 * the refcount will drop to zero and the buffer will
3016 * go out then.
3017 */
3018 if (!already_slept &&
3019 (iclog->ic_prev->ic_state &
3020 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3021 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3022
3023 XFS_STATS_INC(xs_log_force_sleep);
3024
3025 sv_wait(&iclog->ic_prev->ic_write_wait,
3026 PSWP, &log->l_icloglock, s);
3027 if (log_flushed)
3028 *log_flushed = 1;
3029 already_slept = 1;
3030 goto try_again;
3031 }
David Chinner155cc6b2008-03-06 13:44:14 +11003032 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003034 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (xlog_state_release_iclog(log, iclog))
3036 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003037 if (log_flushed)
3038 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003039 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003042 if ((flags & XFS_LOG_SYNC) && /* sleep */
3043 !(iclog->ic_state &
3044 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3045 /*
3046 * Don't wait on completion if we know that we've
3047 * gotten a log write error.
3048 */
3049 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3050 spin_unlock(&log->l_icloglock);
3051 return XFS_ERROR(EIO);
3052 }
3053 XFS_STATS_INC(xs_log_force_sleep);
3054 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3055 /*
3056 * No need to grab the log lock here since we're
3057 * only deciding whether or not to return EIO
3058 * and the memory read should be atomic.
3059 */
3060 if (iclog->ic_state & XLOG_STATE_IOERROR)
3061 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003063 if (log_flushed)
3064 *log_flushed = 1;
3065 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003066 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003068
3069 return 0;
3070 } while (iclog != log->l_iclog);
3071
3072 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003076/*
3077 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3078 * about errors or whether the log was flushed or not. This is the normal
3079 * interface to use when trying to unpin items or move the log forward.
3080 */
3081void
3082xfs_log_force_lsn(
3083 xfs_mount_t *mp,
3084 xfs_lsn_t lsn,
3085 uint flags)
3086{
3087 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003089 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3090 if (error) {
3091 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3092 "error %d returned.", error);
3093 }
3094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096/*
3097 * Called when we want to mark the current iclog as being ready to sync to
3098 * disk.
3099 */
David Chinnera8272ce2007-11-23 16:28:09 +11003100STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3102{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003103 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3106 xlog_state_switch_iclogs(log, iclog, 0);
3107 } else {
3108 ASSERT(iclog->ic_state &
3109 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3110 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113
3114/*****************************************************************************
3115 *
3116 * TICKET functions
3117 *
3118 *****************************************************************************
3119 */
3120
3121/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003122 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003124void
3125xfs_log_ticket_put(
3126 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003128 ASSERT(atomic_read(&ticket->t_ref) > 0);
3129 if (atomic_dec_and_test(&ticket->t_ref)) {
3130 sv_destroy(&ticket->t_wait);
3131 kmem_zone_free(xfs_log_ticket_zone, ticket);
3132 }
3133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003135xlog_ticket_t *
3136xfs_log_ticket_get(
3137 xlog_ticket_t *ticket)
3138{
3139 ASSERT(atomic_read(&ticket->t_ref) > 0);
3140 atomic_inc(&ticket->t_ref);
3141 return ticket;
3142}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003145 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 */
David Chinnera8272ce2007-11-23 16:28:09 +11003147STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003148xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 int unit_bytes,
3150 int cnt,
3151 char client,
3152 uint xflags)
3153{
3154 xlog_ticket_t *tic;
3155 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
David Chinnereb01c9c2008-04-10 12:18:46 +10003157 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3158 if (!tic)
3159 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /*
3162 * Permanent reservations have up to 'cnt'-1 active log operations
3163 * in the log. A unit in this case is the amount of space for one
3164 * of these log operations. Normal reservations have a cnt of 1
3165 * and their unit amount is the total amount of space required.
3166 *
3167 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003168 * which occupy space in the on-disk log.
3169 *
3170 * Normal form of a transaction is:
3171 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3172 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3173 *
3174 * We need to account for all the leadup data and trailer data
3175 * around the transaction data.
3176 * And then we need to account for the worst case in terms of using
3177 * more space.
3178 * The worst case will happen if:
3179 * - the placement of the transaction happens to be such that the
3180 * roundoff is at its maximum
3181 * - the transaction data is synced before the commit record is synced
3182 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3183 * Therefore the commit record is in its own Log Record.
3184 * This can happen as the commit record is called with its
3185 * own region to xlog_write().
3186 * This then means that in the worst case, roundoff can happen for
3187 * the commit-rec as well.
3188 * The commit-rec is smaller than padding in this scenario and so it is
3189 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 */
3191
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003192 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003194 unit_bytes += sizeof(xfs_trans_header_t);
3195
3196 /* for start-rec */
3197 unit_bytes += sizeof(xlog_op_header_t);
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 /* for LR headers */
3200 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3201 unit_bytes += log->l_iclog_hsize * num_headers;
3202
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003203 /* for commit-rec LR header - note: padding will subsume the ophdr */
3204 unit_bytes += log->l_iclog_hsize;
3205
3206 /* for split-recs - ophdrs added when data split over LRs */
3207 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3208
3209 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003210 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003211 log->l_mp->m_sb.sb_logsunit > 1) {
3212 /* log su roundoff */
3213 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3214 } else {
3215 /* BB roundoff */
3216 unit_bytes += 2*BBSIZE;
3217 }
3218
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003219 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 tic->t_unit_res = unit_bytes;
3221 tic->t_curr_res = unit_bytes;
3222 tic->t_cnt = cnt;
3223 tic->t_ocnt = cnt;
3224 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3225 tic->t_clientid = client;
3226 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003227 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 if (xflags & XFS_LOG_PERM_RESERV)
3229 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003230 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Christoph Hellwig0adba532007-08-30 17:21:46 +10003232 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237
3238/******************************************************************************
3239 *
3240 * Log debug routines
3241 *
3242 ******************************************************************************
3243 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003244#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245/*
3246 * Make sure that the destination ptr is within the valid data region of
3247 * one of the iclogs. This uses backup pointers stored in a different
3248 * part of the log in case we trash the log structure.
3249 */
3250void
3251xlog_verify_dest_ptr(xlog_t *log,
3252 __psint_t ptr)
3253{
3254 int i;
3255 int good_ptr = 0;
3256
3257 for (i=0; i < log->l_iclog_bufs; i++) {
3258 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3259 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3260 good_ptr++;
3261 }
3262 if (! good_ptr)
3263 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3264} /* xlog_verify_dest_ptr */
3265
3266STATIC void
3267xlog_verify_grant_head(xlog_t *log, int equals)
3268{
3269 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3270 if (equals)
3271 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3272 else
3273 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3274 } else {
3275 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3276 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3277 }
3278} /* xlog_verify_grant_head */
3279
3280/* check if it will fit */
3281STATIC void
3282xlog_verify_tail_lsn(xlog_t *log,
3283 xlog_in_core_t *iclog,
3284 xfs_lsn_t tail_lsn)
3285{
3286 int blocks;
3287
3288 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3289 blocks =
3290 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3291 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3292 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3293 } else {
3294 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3295
3296 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3297 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3298
3299 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3300 if (blocks < BTOBB(iclog->ic_offset) + 1)
3301 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3302 }
3303} /* xlog_verify_tail_lsn */
3304
3305/*
3306 * Perform a number of checks on the iclog before writing to disk.
3307 *
3308 * 1. Make sure the iclogs are still circular
3309 * 2. Make sure we have a good magic number
3310 * 3. Make sure we don't have magic numbers in the data
3311 * 4. Check fields of each log operation header for:
3312 * A. Valid client identifier
3313 * B. tid ptr value falls in valid ptr space (user space code)
3314 * C. Length in log record header is correct according to the
3315 * individual operation headers within record.
3316 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3317 * log, check the preceding blocks of the physical log to make sure all
3318 * the cycle numbers agree with the current cycle number.
3319 */
3320STATIC void
3321xlog_verify_iclog(xlog_t *log,
3322 xlog_in_core_t *iclog,
3323 int count,
3324 boolean_t syncing)
3325{
3326 xlog_op_header_t *ophead;
3327 xlog_in_core_t *icptr;
3328 xlog_in_core_2_t *xhdr;
3329 xfs_caddr_t ptr;
3330 xfs_caddr_t base_ptr;
3331 __psint_t field_offset;
3332 __uint8_t clientid;
3333 int len, i, j, k, op_len;
3334 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003337 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 icptr = log->l_iclog;
3339 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003340 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 xlog_panic("xlog_verify_iclog: invalid ptr");
3342 icptr = icptr->ic_next;
3343 }
3344 if (icptr != log->l_iclog)
3345 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003346 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003349 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 xlog_panic("xlog_verify_iclog: invalid magic num");
3351
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003352 ptr = (xfs_caddr_t) &iclog->ic_header;
3353 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003355 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 xlog_panic("xlog_verify_iclog: unexpected magic num");
3357 }
3358
3359 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003360 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 ptr = iclog->ic_datap;
3362 base_ptr = ptr;
3363 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003364 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 for (i = 0; i < len; i++) {
3366 ophead = (xlog_op_header_t *)ptr;
3367
3368 /* clientid is only 1 byte */
3369 field_offset = (__psint_t)
3370 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3371 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3372 clientid = ophead->oh_clientid;
3373 } else {
3374 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3375 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3376 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3377 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003378 clientid = xlog_get_client_id(
3379 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003381 clientid = xlog_get_client_id(
3382 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 }
3384 }
3385 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003386 cmn_err(CE_WARN, "xlog_verify_iclog: "
3387 "invalid clientid %d op 0x%p offset 0x%lx",
3388 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 /* check length */
3391 field_offset = (__psint_t)
3392 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3393 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003394 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 } else {
3396 idx = BTOBBT((__psint_t)&ophead->oh_len -
3397 (__psint_t)iclog->ic_datap);
3398 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3399 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3400 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003401 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003403 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
3405 }
3406 ptr += sizeof(xlog_op_header_t) + op_len;
3407 }
3408} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003409#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
3411/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003412 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 */
3414STATIC int
3415xlog_state_ioerror(
3416 xlog_t *log)
3417{
3418 xlog_in_core_t *iclog, *ic;
3419
3420 iclog = log->l_iclog;
3421 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3422 /*
3423 * Mark all the incore logs IOERROR.
3424 * From now on, no log flushes will result.
3425 */
3426 ic = iclog;
3427 do {
3428 ic->ic_state = XLOG_STATE_IOERROR;
3429 ic = ic->ic_next;
3430 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
3433 /*
3434 * Return non-zero, if state transition has already happened.
3435 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003436 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
3439/*
3440 * This is called from xfs_force_shutdown, when we're forcibly
3441 * shutting down the filesystem, typically because of an IO error.
3442 * Our main objectives here are to make sure that:
3443 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3444 * parties to find out, 'atomically'.
3445 * b. those who're sleeping on log reservations, pinned objects and
3446 * other resources get woken up, and be told the bad news.
3447 * c. nothing new gets queued up after (a) and (b) are done.
3448 * d. if !logerror, flush the iclogs to disk, then seal them off
3449 * for business.
3450 */
3451int
3452xfs_log_force_umount(
3453 struct xfs_mount *mp,
3454 int logerror)
3455{
3456 xlog_ticket_t *tic;
3457 xlog_t *log;
3458 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 log = mp->m_log;
3461
3462 /*
3463 * If this happens during log recovery, don't worry about
3464 * locking; the log isn't open for business yet.
3465 */
3466 if (!log ||
3467 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3468 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003469 if (mp->m_sb_bp)
3470 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
3473
3474 /*
3475 * Somebody could've already done the hard work for us.
3476 * No need to get locks for this.
3477 */
3478 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3479 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003480 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
3482 retval = 0;
3483 /*
3484 * We must hold both the GRANT lock and the LOG lock,
3485 * before we mark the filesystem SHUTDOWN and wake
3486 * everybody up to tell the bad news.
3487 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003488 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003489 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003491 if (mp->m_sb_bp)
3492 XFS_BUF_DONE(mp->m_sb_bp);
3493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 /*
3495 * This flag is sort of redundant because of the mount flag, but
3496 * it's good to maintain the separation between the log and the rest
3497 * of XFS.
3498 */
3499 log->l_flags |= XLOG_IO_ERROR;
3500
3501 /*
3502 * If we hit a log error, we want to mark all the iclogs IOERROR
3503 * while we're still holding the loglock.
3504 */
3505 if (logerror)
3506 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003507 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509 /*
3510 * We don't want anybody waiting for log reservations
3511 * after this. That means we have to wake up everybody
3512 * queued up on reserve_headq as well as write_headq.
3513 * In addition, we make sure in xlog_{re}grant_log_space
3514 * that we don't enqueue anything once the SHUTDOWN flag
3515 * is set, and this action is protected by the GRANTLOCK.
3516 */
3517 if ((tic = log->l_reserve_headq)) {
3518 do {
David Chinner12017fa2008-08-13 16:34:31 +10003519 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 tic = tic->t_next;
3521 } while (tic != log->l_reserve_headq);
3522 }
3523
3524 if ((tic = log->l_write_headq)) {
3525 do {
David Chinner12017fa2008-08-13 16:34:31 +10003526 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 tic = tic->t_next;
3528 } while (tic != log->l_write_headq);
3529 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003530 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003532 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 ASSERT(!logerror);
3534 /*
3535 * Force the incore logs to disk before shutting the
3536 * log down completely.
3537 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003538 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3539
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003540 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003542 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
3544 /*
3545 * Wake up everybody waiting on xfs_log_force.
3546 * Callback all log item committed functions as if the
3547 * log writes were completed.
3548 */
3549 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3550
3551#ifdef XFSERRORDEBUG
3552 {
3553 xlog_in_core_t *iclog;
3554
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003555 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 iclog = log->l_iclog;
3557 do {
3558 ASSERT(iclog->ic_callback == 0);
3559 iclog = iclog->ic_next;
3560 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003561 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 }
3563#endif
3564 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003565 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003568STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569xlog_iclogs_empty(xlog_t *log)
3570{
3571 xlog_in_core_t *iclog;
3572
3573 iclog = log->l_iclog;
3574 do {
3575 /* endianness does not matter here, zero is zero in
3576 * any language.
3577 */
3578 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 iclog = iclog->ic_next;
3581 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003582 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}