blob: 92c20a8d9e69c80388c64c58b3b64d69d9a6ae74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Chinnereb01c9c2008-04-10 12:18:46 +100044kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define xlog_write_adv_cnt(ptr, len, off, bytes) \
47 { (ptr) += (bytes); \
48 (len) -= (bytes); \
49 (off) += (bytes);}
50
51/* Local miscellaneous function prototypes */
52STATIC int xlog_bdstrat_cb(struct xfs_buf *);
53STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54 xlog_in_core_t **, xfs_lsn_t *);
55STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
56 xfs_buftarg_t *log_target,
57 xfs_daddr_t blk_offset,
58 int num_bblks);
59STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
60STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100061STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
63 int nentries, xfs_log_ticket_t tic,
64 xfs_lsn_t *start_lsn,
65 xlog_in_core_t **commit_iclog,
66 uint flags);
67
68/* local state machine functions */
69STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71STATIC int xlog_state_get_iclog_space(xlog_t *log,
72 int len,
73 xlog_in_core_t **iclog,
74 xlog_ticket_t *ticket,
75 int *continued_write,
76 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077STATIC int xlog_state_release_iclog(xlog_t *log,
78 xlog_in_core_t *iclog);
79STATIC void xlog_state_switch_iclogs(xlog_t *log,
80 xlog_in_core_t *iclog,
81 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110082STATIC int xlog_state_sync(xlog_t *log,
83 xfs_lsn_t lsn,
84 uint flags,
85 int *log_flushed);
86STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
88
89/* local functions to manipulate grant head */
90STATIC int xlog_grant_log_space(xlog_t *log,
91 xlog_ticket_t *xtic);
92STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
93 int need_bytes);
94STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
95 xlog_ticket_t *ticket);
96STATIC int xlog_regrant_write_log_space(xlog_t *log,
97 xlog_ticket_t *ticket);
98STATIC void xlog_ungrant_log_space(xlog_t *log,
99 xlog_ticket_t *ticket);
100
101
102/* local ticket functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
104 int unit_bytes,
105 int count,
106 char clientid,
107 uint flags);
108STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
109
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100110#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
112STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
113STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
114 int count, boolean_t syncing);
115STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
116 xfs_lsn_t tail_lsn);
117#else
118#define xlog_verify_dest_ptr(a,b)
119#define xlog_verify_grant_head(a,b)
120#define xlog_verify_iclog(a,b,c,d)
121#define xlog_verify_tail_lsn(a,b,c)
122#endif
123
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000124STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#if defined(XFS_LOG_TRACE)
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000127
128#define XLOG_TRACE_LOGGRANT_SIZE 2048
129#define XLOG_TRACE_ICLOG_SIZE 256
130
131void
132xlog_trace_loggrant_alloc(xlog_t *log)
133{
134 log->l_grant_trace = ktrace_alloc(XLOG_TRACE_LOGGRANT_SIZE, KM_NOFS);
135}
136
137void
138xlog_trace_loggrant_dealloc(xlog_t *log)
139{
140 ktrace_free(log->l_grant_trace);
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143void
144xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
145{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000146 unsigned long cnts;
147
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000148 /* ticket counts are 1 byte each */
149 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 ktrace_enter(log->l_grant_trace,
152 (void *)tic,
153 (void *)log->l_reserve_headq,
154 (void *)log->l_write_headq,
155 (void *)((unsigned long)log->l_grant_reserve_cycle),
156 (void *)((unsigned long)log->l_grant_reserve_bytes),
157 (void *)((unsigned long)log->l_grant_write_cycle),
158 (void *)((unsigned long)log->l_grant_write_bytes),
159 (void *)((unsigned long)log->l_curr_cycle),
160 (void *)((unsigned long)log->l_curr_block),
161 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
162 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
163 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000164 (void *)((unsigned long)tic->t_trans_type),
165 (void *)cnts,
166 (void *)((unsigned long)tic->t_curr_res),
167 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170void
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000171xlog_trace_iclog_alloc(xlog_in_core_t *iclog)
172{
173 iclog->ic_trace = ktrace_alloc(XLOG_TRACE_ICLOG_SIZE, KM_NOFS);
174}
175
176void
177xlog_trace_iclog_dealloc(xlog_in_core_t *iclog)
178{
179 ktrace_free(iclog->ic_trace);
180}
181
182void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
184{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ktrace_enter(iclog->ic_trace,
186 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100187 (void *)((unsigned long)current_pid()),
188 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
189 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
190 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
191 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#else
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000194
195#define xlog_trace_loggrant_alloc(log)
196#define xlog_trace_loggrant_dealloc(log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#define xlog_trace_loggrant(log,tic,string)
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000198
199#define xlog_trace_iclog_alloc(iclog)
200#define xlog_trace_iclog_dealloc(iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define xlog_trace_iclog(iclog,state)
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#endif /* XFS_LOG_TRACE */
204
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100205
206static void
207xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
208{
209 if (*qp) {
210 tic->t_next = (*qp);
211 tic->t_prev = (*qp)->t_prev;
212 (*qp)->t_prev->t_next = tic;
213 (*qp)->t_prev = tic;
214 } else {
215 tic->t_prev = tic->t_next = tic;
216 *qp = tic;
217 }
218
219 tic->t_flags |= XLOG_TIC_IN_Q;
220}
221
222static void
223xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
224{
225 if (tic == tic->t_next) {
226 *qp = NULL;
227 } else {
228 *qp = tic->t_next;
229 tic->t_next->t_prev = tic->t_prev;
230 tic->t_prev->t_next = tic->t_next;
231 }
232
233 tic->t_next = tic->t_prev = NULL;
234 tic->t_flags &= ~XLOG_TIC_IN_Q;
235}
236
237static void
238xlog_grant_sub_space(struct log *log, int bytes)
239{
240 log->l_grant_write_bytes -= bytes;
241 if (log->l_grant_write_bytes < 0) {
242 log->l_grant_write_bytes += log->l_logsize;
243 log->l_grant_write_cycle--;
244 }
245
246 log->l_grant_reserve_bytes -= bytes;
247 if ((log)->l_grant_reserve_bytes < 0) {
248 log->l_grant_reserve_bytes += log->l_logsize;
249 log->l_grant_reserve_cycle--;
250 }
251
252}
253
254static void
255xlog_grant_add_space_write(struct log *log, int bytes)
256{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000257 int tmp = log->l_logsize - log->l_grant_write_bytes;
258 if (tmp > bytes)
259 log->l_grant_write_bytes += bytes;
260 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100261 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000262 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100263 }
264}
265
266static void
267xlog_grant_add_space_reserve(struct log *log, int bytes)
268{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000269 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
270 if (tmp > bytes)
271 log->l_grant_reserve_bytes += bytes;
272 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100273 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000274 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100275 }
276}
277
278static inline void
279xlog_grant_add_space(struct log *log, int bytes)
280{
281 xlog_grant_add_space_write(log, bytes);
282 xlog_grant_add_space_reserve(log, bytes);
283}
284
Christoph Hellwig0adba532007-08-30 17:21:46 +1000285static void
286xlog_tic_reset_res(xlog_ticket_t *tic)
287{
288 tic->t_res_num = 0;
289 tic->t_res_arr_sum = 0;
290 tic->t_res_num_ophdrs = 0;
291}
292
293static void
294xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
295{
296 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
297 /* add to overflow and start again */
298 tic->t_res_o_flow += tic->t_res_arr_sum;
299 tic->t_res_num = 0;
300 tic->t_res_arr_sum = 0;
301 }
302
303 tic->t_res_arr[tic->t_res_num].r_len = len;
304 tic->t_res_arr[tic->t_res_num].r_type = type;
305 tic->t_res_arr_sum += len;
306 tic->t_res_num++;
307}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * NOTES:
311 *
312 * 1. currblock field gets updated at startup and after in-core logs
313 * marked as with WANT_SYNC.
314 */
315
316/*
317 * This routine is called when a user of a log manager ticket is done with
318 * the reservation. If the ticket was ever used, then a commit record for
319 * the associated transaction is written out as a log operation header with
320 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
321 * a given ticket. If the ticket was one with a permanent reservation, then
322 * a few operations are done differently. Permanent reservation tickets by
323 * default don't release the reservation. They just commit the current
324 * transaction with the belief that the reservation is still needed. A flag
325 * must be passed in before permanent reservations are actually released.
326 * When these type of tickets are not released, they need to be set into
327 * the inited state again. By doing this, a start record will be written
328 * out when the next write occurs.
329 */
330xfs_lsn_t
331xfs_log_done(xfs_mount_t *mp,
332 xfs_log_ticket_t xtic,
333 void **iclog,
334 uint flags)
335{
336 xlog_t *log = mp->m_log;
337 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
338 xfs_lsn_t lsn = 0;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (XLOG_FORCED_SHUTDOWN(log) ||
341 /*
342 * If nothing was ever written, don't write out commit record.
343 * If we get an error, just continue and give back the log ticket.
344 */
345 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
346 (xlog_commit_record(mp, ticket,
347 (xlog_in_core_t **)iclog, &lsn)))) {
348 lsn = (xfs_lsn_t) -1;
349 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
350 flags |= XFS_LOG_REL_PERM_RESERV;
351 }
352 }
353
354
355 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
356 (flags & XFS_LOG_REL_PERM_RESERV)) {
357 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000358 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * request has been made to release a permanent reservation.
360 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000361 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 xlog_ungrant_log_space(log, ticket);
David Chinnereb01c9c2008-04-10 12:18:46 +1000363 xlog_ticket_put(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000365 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000367 /* If this ticket was a permanent reservation and we aren't
368 * trying to release it, reset the inited flags; so next time
369 * we write, a start record will be written out.
370 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 return lsn;
375} /* xfs_log_done */
376
377
378/*
379 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
380 * the force is done synchronously.
381 *
382 * Asynchronous forces are implemented by setting the WANT_SYNC
383 * bit in the appropriate in-core log and then returning.
384 *
David Chinner12017fa2008-08-13 16:34:31 +1000385 * Synchronous forces are implemented with a signal variable. All callers
386 * to force a given lsn to disk will wait on a the sv attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * specific in-core log. When given in-core log finally completes its
388 * write to disk, that thread will wake up all threads waiting on the
David Chinner12017fa2008-08-13 16:34:31 +1000389 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100392_xfs_log_force(
393 xfs_mount_t *mp,
394 xfs_lsn_t lsn,
395 uint flags,
396 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100398 xlog_t *log = mp->m_log;
399 int dummy;
400
401 if (!log_flushed)
402 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 ASSERT(flags & XFS_LOG_FORCE);
405
406 XFS_STATS_INC(xs_log_force);
407
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100408 if (log->l_flags & XLOG_IO_ERROR)
409 return XFS_ERROR(EIO);
410 if (lsn == 0)
411 return xlog_state_sync_all(log, flags, log_flushed);
412 else
413 return xlog_state_sync(log, lsn, flags, log_flushed);
David Chinnerb911ca02008-04-10 12:24:30 +1000414} /* _xfs_log_force */
415
416/*
417 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
418 * about errors or whether the log was flushed or not. This is the normal
419 * interface to use when trying to unpin items or move the log forward.
420 */
421void
422xfs_log_force(
423 xfs_mount_t *mp,
424 xfs_lsn_t lsn,
425 uint flags)
426{
427 int error;
428 error = _xfs_log_force(mp, lsn, flags, NULL);
429 if (error) {
430 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
431 "error %d returned.", error);
432 }
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436/*
437 * Attaches a new iclog I/O completion callback routine during
438 * transaction commit. If the log is in error state, a non-zero
439 * return code is handed back and the caller is responsible for
440 * executing the callback at an appropriate time.
441 */
442int
443xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
444 void *iclog_hndl, /* iclog to hang callback off */
445 xfs_log_callback_t *cb)
446{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000448 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
David Chinner114d23a2008-04-10 12:18:39 +1000450 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
452 if (!abortflg) {
453 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
454 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
455 cb->cb_next = NULL;
456 *(iclog->ic_callback_tail) = cb;
457 iclog->ic_callback_tail = &(cb->cb_next);
458 }
David Chinner114d23a2008-04-10 12:18:39 +1000459 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return abortflg;
461} /* xfs_log_notify */
462
463int
464xfs_log_release_iclog(xfs_mount_t *mp,
465 void *iclog_hndl)
466{
467 xlog_t *log = mp->m_log;
468 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
469
470 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000471 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100472 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 return 0;
476}
477
478/*
479 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
480 * to the reservation.
481 * 2. Potentially, push buffers at tail of log to disk.
482 *
483 * Each reservation is going to reserve extra space for a log record header.
484 * When writes happen to the on-disk log, we don't subtract the length of the
485 * log record header from any reservation. By wasting space in each
486 * reservation, we prevent over allocation problems.
487 */
488int
489xfs_log_reserve(xfs_mount_t *mp,
490 int unit_bytes,
491 int cnt,
492 xfs_log_ticket_t *ticket,
493 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000494 uint flags,
495 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 xlog_t *log = mp->m_log;
498 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100499 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
502 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
503
504 if (XLOG_FORCED_SHUTDOWN(log))
505 return XFS_ERROR(EIO);
506
507 XFS_STATS_INC(xs_try_logspace);
508
509 if (*ticket != NULL) {
510 ASSERT(flags & XFS_LOG_PERM_RESERV);
511 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000512 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
514 retval = xlog_regrant_write_log_space(log, internal_ticket);
515 } else {
516 /* may sleep if need to allocate more tickets */
517 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
518 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000519 if (!internal_ticket)
520 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000521 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000523 xlog_trace_loggrant(log, internal_ticket,
524 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
525 "xfs_log_reserve: create new ticket (permanent trans)" :
526 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 xlog_grant_push_ail(mp,
528 (internal_ticket->t_unit_res *
529 internal_ticket->t_cnt));
530 retval = xlog_grant_log_space(log, internal_ticket);
531 }
532
533 return retval;
534} /* xfs_log_reserve */
535
536
537/*
538 * Mount a log filesystem
539 *
540 * mp - ubiquitous xfs mount point structure
541 * log_target - buftarg of on-disk log device
542 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
543 * num_bblocks - Number of BBSIZE blocks in on-disk log
544 *
545 * Return error or zero.
546 */
547int
David Chinner249a8c12008-02-05 12:13:32 +1100548xfs_log_mount(
549 xfs_mount_t *mp,
550 xfs_buftarg_t *log_target,
551 xfs_daddr_t blk_offset,
552 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
David Chinner249a8c12008-02-05 12:13:32 +1100554 int error;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
557 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
558 else {
559 cmn_err(CE_NOTE,
560 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
561 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000562 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinner644c3562008-11-10 16:50:24 +1100566 if (!mp->m_log) {
567 cmn_err(CE_WARN, "XFS: Log allocation failed: No memory!");
568 error = ENOMEM;
569 goto out;
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /*
David Chinner249a8c12008-02-05 12:13:32 +1100573 * Initialize the AIL now we have a log.
574 */
David Chinner249a8c12008-02-05 12:13:32 +1100575 error = xfs_trans_ail_init(mp);
576 if (error) {
577 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
578 goto error;
579 }
David Chinnera9c21c12008-10-30 17:39:35 +1100580 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100581
582 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * skip log recovery on a norecovery mount. pretend it all
584 * just worked.
585 */
586 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100587 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000590 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Eric Sandeen65be6052006-01-11 15:34:19 +1100592 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000595 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (error) {
597 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
David Chinner249a8c12008-02-05 12:13:32 +1100598 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 }
601
602 /* Normal transactions can now occur */
603 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
604
605 /* End mounting message in xfs_log_mount_finish */
606 return 0;
David Chinner249a8c12008-02-05 12:13:32 +1100607error:
608 xfs_log_unmount_dealloc(mp);
Dave Chinner644c3562008-11-10 16:50:24 +1100609out:
David Chinner249a8c12008-02-05 12:13:32 +1100610 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611} /* xfs_log_mount */
612
613/*
614 * Finish the recovery of the file system. This is separate from
615 * the xfs_log_mount() call, because it depends on the code in
616 * xfs_mountfs() to read in the root and real-time bitmap inodes
617 * between calling xfs_log_mount() and here.
618 *
619 * mp - ubiquitous xfs mount point structure
620 */
621int
Christoph Hellwig42490232008-08-13 16:49:32 +1000622xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 int error;
625
626 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000627 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 else {
629 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000630 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
633 return error;
634}
635
636/*
637 * Unmount processing for the log.
638 */
639int
640xfs_log_unmount(xfs_mount_t *mp)
641{
642 int error;
643
644 error = xfs_log_unmount_write(mp);
645 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100646 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649/*
650 * Final log writes as part of unmount.
651 *
652 * Mark the filesystem clean as unmount happens. Note that during relocation
653 * this routine needs to be executed as part of source-bag while the
654 * deallocation must not be done until source-end.
655 */
656
657/*
658 * Unmount record used to have a string "Unmount filesystem--" in the
659 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
660 * We just write the magic number now since that particular field isn't
661 * currently architecture converted and "nUmount" is a bit foo.
662 * As far as I know, there weren't any dependencies on the old behaviour.
663 */
664
665int
666xfs_log_unmount_write(xfs_mount_t *mp)
667{
668 xlog_t *log = mp->m_log;
669 xlog_in_core_t *iclog;
670#ifdef DEBUG
671 xlog_in_core_t *first_iclog;
672#endif
673 xfs_log_iovec_t reg[1];
674 xfs_log_ticket_t tic = NULL;
675 xfs_lsn_t lsn;
676 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /* the data section must be 32 bit size aligned */
679 struct {
680 __uint16_t magic;
681 __uint16_t pad1;
682 __uint32_t pad2; /* may as well make it 64 bits */
683 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
686 * Don't write out unmount record on read-only mounts.
687 * Or, if we are doing a forced umount (typically because of IO errors).
688 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000689 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691
David Chinnerb911ca02008-04-10 12:24:30 +1000692 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
693 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695#ifdef DEBUG
696 first_iclog = iclog = log->l_iclog;
697 do {
698 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
699 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
700 ASSERT(iclog->ic_offset == 0);
701 }
702 iclog = iclog->ic_next;
703 } while (iclog != first_iclog);
704#endif
705 if (! (XLOG_FORCED_SHUTDOWN(log))) {
706 reg[0].i_addr = (void*)&magic;
707 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000708 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Tim Shimmin955e47a2006-09-28 11:04:16 +1000710 error = xfs_log_reserve(mp, 600, 1, &tic,
711 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!error) {
713 /* remove inited flag */
714 ((xlog_ticket_t *)tic)->t_flags = 0;
715 error = xlog_write(mp, reg, 1, tic, &lsn,
716 NULL, XLOG_UNMOUNT_TRANS);
717 /*
718 * At this point, we're umounting anyway,
719 * so there's no point in transitioning log state
720 * to IOERROR. Just continue...
721 */
722 }
723
724 if (error) {
725 xfs_fs_cmn_err(CE_ALERT, mp,
726 "xfs_log_unmount: unmount record failed");
727 }
728
729
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000730 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100732 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000733 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 xlog_state_want_sync(log, iclog);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000735 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000737 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
739 iclog->ic_state == XLOG_STATE_DIRTY)) {
740 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000741 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 &log->l_icloglock, s);
743 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000744 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000747 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000749 if (tic) {
750 xlog_trace_loggrant(log, tic, "unmount rec");
751 xlog_ungrant_log_space(log, tic);
David Chinnereb01c9c2008-04-10 12:18:46 +1000752 xlog_ticket_put(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 } else {
755 /*
756 * We're already in forced_shutdown mode, couldn't
757 * even attempt to write out the unmount transaction.
758 *
759 * Go through the motions of sync'ing and releasing
760 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000761 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * be in progress. Do this as a separate section of
763 * code so we'll know if we ever get stuck here that
764 * we're in this odd situation of trying to unmount
765 * a file system that went into forced_shutdown as
766 * the result of an unmount..
767 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000768 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100770 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000771 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 xlog_state_want_sync(log, iclog);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000774 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000776 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
779 || iclog->ic_state == XLOG_STATE_DIRTY
780 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
781
David Chinner12017fa2008-08-13 16:34:31 +1000782 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 &log->l_icloglock, s);
784 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000785 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 }
788
David Chinner1bb7d6b2008-04-10 12:24:38 +1000789 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790} /* xfs_log_unmount_write */
791
792/*
793 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100794 *
795 * We need to stop the aild from running before we destroy
796 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
798void
799xfs_log_unmount_dealloc(xfs_mount_t *mp)
800{
David Chinner249a8c12008-02-05 12:13:32 +1100801 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000802 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805/*
806 * Write region vectors to log. The write happens using the space reservation
807 * of the ticket (tic). It is not a requirement that all writes for a given
808 * transaction occur with one call to xfs_log_write().
809 */
810int
811xfs_log_write(xfs_mount_t * mp,
812 xfs_log_iovec_t reg[],
813 int nentries,
814 xfs_log_ticket_t tic,
815 xfs_lsn_t *start_lsn)
816{
817 int error;
818 xlog_t *log = mp->m_log;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (XLOG_FORCED_SHUTDOWN(log))
821 return XFS_ERROR(EIO);
822
823 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000824 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100826 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827} /* xfs_log_write */
828
829
830void
831xfs_log_move_tail(xfs_mount_t *mp,
832 xfs_lsn_t tail_lsn)
833{
834 xlog_ticket_t *tic;
835 xlog_t *log = mp->m_log;
836 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (XLOG_FORCED_SHUTDOWN(log))
839 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (tail_lsn == 0) {
842 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000843 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000845 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000848 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
851 * tail_lsn.
852 */
853 if (tail_lsn != 1) {
854 log->l_tail_lsn = tail_lsn;
855 }
856
857 if ((tic = log->l_write_headq)) {
858#ifdef DEBUG
859 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
860 panic("Recovery problem");
861#endif
862 cycle = log->l_grant_write_cycle;
863 bytes = log->l_grant_write_bytes;
864 free_bytes = xlog_space_left(log, cycle, bytes);
865 do {
866 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
867
868 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
869 break;
870 tail_lsn = 0;
871 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000872 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 tic = tic->t_next;
874 } while (tic != log->l_write_headq);
875 }
876 if ((tic = log->l_reserve_headq)) {
877#ifdef DEBUG
878 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
879 panic("Recovery problem");
880#endif
881 cycle = log->l_grant_reserve_cycle;
882 bytes = log->l_grant_reserve_bytes;
883 free_bytes = xlog_space_left(log, cycle, bytes);
884 do {
885 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
886 need_bytes = tic->t_unit_res*tic->t_cnt;
887 else
888 need_bytes = tic->t_unit_res;
889 if (free_bytes < need_bytes && tail_lsn != 1)
890 break;
891 tail_lsn = 0;
892 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000893 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 tic = tic->t_next;
895 } while (tic != log->l_reserve_headq);
896 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000897 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898} /* xfs_log_move_tail */
899
900/*
901 * Determine if we have a transaction that has gone to disk
902 * that needs to be covered. Log activity needs to be idle (no AIL and
903 * nothing in the iclogs). And, we need to be in the right state indicating
904 * something has gone out.
905 */
906int
907xfs_log_need_covered(xfs_mount_t *mp)
908{
David Chinner27d8d5f2008-10-30 17:38:39 +1100909 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
David Chinner92821e22007-05-24 15:26:31 +1000912 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return 0;
914
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000915 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
917 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
David Chinnera9c21c12008-10-30 17:39:35 +1100918 && !xfs_trans_ail_tail(log->l_ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 && xlog_iclogs_empty(log)) {
920 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
921 log->l_covered_state = XLOG_STATE_COVER_DONE;
922 else {
923 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
924 log->l_covered_state = XLOG_STATE_COVER_DONE2;
925 }
926 needed = 1;
927 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000928 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100929 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932/******************************************************************************
933 *
934 * local routines
935 *
936 ******************************************************************************
937 */
938
939/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
940 * The log manager must keep track of the last LR which was committed
941 * to disk. The lsn of this LR will become the new tail_lsn whenever
942 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
943 * the situation where stuff could be written into the log but nothing
944 * was ever in the AIL when asked. Eventually, we panic since the
945 * tail hits the head.
946 *
947 * We may be holding the log iclog lock upon entering this routine.
948 */
949xfs_lsn_t
950xlog_assign_tail_lsn(xfs_mount_t *mp)
951{
952 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 xlog_t *log = mp->m_log;
954
David Chinner5b00f142008-10-30 17:39:00 +1100955 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000956 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (tail_lsn != 0) {
958 log->l_tail_lsn = tail_lsn;
959 } else {
960 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
961 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000962 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 return tail_lsn;
965} /* xlog_assign_tail_lsn */
966
967
968/*
969 * Return the space in the log between the tail and the head. The head
970 * is passed in the cycle/bytes formal parms. In the special case where
971 * the reserve head has wrapped passed the tail, this calculation is no
972 * longer valid. In this case, just return 0 which means there is no space
973 * in the log. This works for all places where this function is called
974 * with the reserve head. Of course, if the write head were to ever
975 * wrap the tail, we should blow up. Rather than catch this case here,
976 * we depend on other ASSERTions in other parts of the code. XXXmiken
977 *
978 * This code also handles the case where the reservation head is behind
979 * the tail. The details of this case are described below, but the end
980 * result is that we return the size of the log as the amount of space left.
981 */
David Chinnera8272ce2007-11-23 16:28:09 +1100982STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983xlog_space_left(xlog_t *log, int cycle, int bytes)
984{
985 int free_bytes;
986 int tail_bytes;
987 int tail_cycle;
988
989 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
990 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
991 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
992 free_bytes = log->l_logsize - (bytes - tail_bytes);
993 } else if ((tail_cycle + 1) < cycle) {
994 return 0;
995 } else if (tail_cycle < cycle) {
996 ASSERT(tail_cycle == (cycle - 1));
997 free_bytes = tail_bytes - bytes;
998 } else {
999 /*
1000 * The reservation head is behind the tail.
1001 * In this case we just want to return the size of the
1002 * log as the amount of space left.
1003 */
1004 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
1005 "xlog_space_left: head behind tail\n"
1006 " tail_cycle = %d, tail_bytes = %d\n"
1007 " GH cycle = %d, GH bytes = %d",
1008 tail_cycle, tail_bytes, cycle, bytes);
1009 ASSERT(0);
1010 free_bytes = log->l_logsize;
1011 }
1012 return free_bytes;
1013} /* xlog_space_left */
1014
1015
1016/*
1017 * Log function which is called when an io completes.
1018 *
1019 * The log manager needs its own routine, in order to control what
1020 * happens with the buffer after the write completes.
1021 */
1022void
1023xlog_iodone(xfs_buf_t *bp)
1024{
1025 xlog_in_core_t *iclog;
1026 xlog_t *l;
1027 int aborted;
1028
1029 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1030 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1031 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1032 aborted = 0;
1033
1034 /*
1035 * Some versions of cpp barf on the recursive definition of
1036 * ic_log -> hic_fields.ic_log and expand ic_log twice when
1037 * it is passed through two macros. Workaround broken cpp.
1038 */
1039 l = iclog->ic_log;
1040
1041 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001042 * If the _XFS_BARRIER_FAILED flag was set by a lower
1043 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +10001044 * barrier I/O. Warn loudly and turn off barriers.
1045 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001046 if (bp->b_flags & _XFS_BARRIER_FAILED) {
1047 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001048 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1049 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1050 "xlog_iodone: Barriers are no longer supported"
1051 " by device. Disabling barriers\n");
1052 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1053 }
1054
1055 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 * Race to shutdown the filesystem if we see an error.
1057 */
1058 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1059 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1060 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1061 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001062 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /*
1064 * This flag will be propagated to the trans-committed
1065 * callback routines to let them know that the log-commit
1066 * didn't succeed.
1067 */
1068 aborted = XFS_LI_ABORTED;
1069 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1070 aborted = XFS_LI_ABORTED;
1071 }
David Chinner3db296f2007-05-14 18:24:16 +10001072
1073 /* log I/O is always issued ASYNC */
1074 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001076 /*
1077 * do not reference the buffer (bp) here as we could race
1078 * with it being freed after writing the unmount record to the
1079 * log.
1080 */
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082} /* xlog_iodone */
1083
1084/*
1085 * The bdstrat callback function for log bufs. This gives us a central
1086 * place to trap bufs in case we get hit by a log I/O error and need to
1087 * shutdown. Actually, in practice, even when we didn't get a log error,
1088 * we transition the iclogs to IOERROR state *after* flushing all existing
1089 * iclogs to disk. This is because we don't want anymore new transactions to be
1090 * started or completed afterwards.
1091 */
1092STATIC int
1093xlog_bdstrat_cb(struct xfs_buf *bp)
1094{
1095 xlog_in_core_t *iclog;
1096
1097 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1098
1099 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1100 /* note for irix bstrat will need struct bdevsw passed
1101 * Fix the following macro if the code ever is merged
1102 */
1103 XFS_bdstrat(bp);
1104 return 0;
1105 }
1106
1107 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1108 XFS_BUF_ERROR(bp, EIO);
1109 XFS_BUF_STALE(bp);
1110 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001111 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113
1114}
1115
1116/*
1117 * Return size of each in-core log record buffer.
1118 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001119 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 *
1121 * If the filesystem blocksize is too large, we may need to choose a
1122 * larger size since the directory code currently logs entire blocks.
1123 */
1124
1125STATIC void
1126xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1127 xlog_t *log)
1128{
1129 int size;
1130 int xhdrs;
1131
Eric Sandeen1cb51252007-08-16 16:24:43 +10001132 if (mp->m_logbufs <= 0)
1133 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1134 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001135 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /*
1138 * Buffer size passed in from mount system call.
1139 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001140 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 size = log->l_iclog_size = mp->m_logbsize;
1142 log->l_iclog_size_log = 0;
1143 while (size != 1) {
1144 log->l_iclog_size_log++;
1145 size >>= 1;
1146 }
1147
Eric Sandeen62118702008-03-06 13:44:28 +11001148 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* # headers = size / 32K
1150 * one header holds cycles from 32K of data
1151 */
1152
1153 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1154 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1155 xhdrs++;
1156 log->l_iclog_hsize = xhdrs << BBSHIFT;
1157 log->l_iclog_heads = xhdrs;
1158 } else {
1159 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1160 log->l_iclog_hsize = BBSIZE;
1161 log->l_iclog_heads = 1;
1162 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001163 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
Eric Sandeen1cb51252007-08-16 16:24:43 +10001166 /* All machines use 32KB buffers by default. */
1167 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1168 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* the default log size is 16k or 32k which is one header sector */
1171 log->l_iclog_hsize = BBSIZE;
1172 log->l_iclog_heads = 1;
1173
1174 /*
1175 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1176 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1177 */
1178 if (mp->m_sb.sb_blocksize >= 16*1024) {
1179 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1180 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001181 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 switch (mp->m_sb.sb_blocksize) {
1183 case 16*1024: /* 16 KB */
1184 log->l_iclog_bufs = 3;
1185 break;
1186 case 32*1024: /* 32 KB */
1187 log->l_iclog_bufs = 4;
1188 break;
1189 case 64*1024: /* 64 KB */
1190 log->l_iclog_bufs = 8;
1191 break;
1192 default:
1193 xlog_panic("XFS: Invalid blocksize");
1194 break;
1195 }
1196 }
1197 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001198
1199done: /* are we being asked to make the sizes selected above visible? */
1200 if (mp->m_logbufs == 0)
1201 mp->m_logbufs = log->l_iclog_bufs;
1202 if (mp->m_logbsize == 0)
1203 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204} /* xlog_get_iclog_buffer_size */
1205
1206
1207/*
1208 * This routine initializes some of the log structure for a given mount point.
1209 * Its primary purpose is to fill in enough, so recovery can occur. However,
1210 * some other stuff may be filled in too.
1211 */
1212STATIC xlog_t *
1213xlog_alloc_log(xfs_mount_t *mp,
1214 xfs_buftarg_t *log_target,
1215 xfs_daddr_t blk_offset,
1216 int num_bblks)
1217{
1218 xlog_t *log;
1219 xlog_rec_header_t *head;
1220 xlog_in_core_t **iclogp;
1221 xlog_in_core_t *iclog, *prev_iclog=NULL;
1222 xfs_buf_t *bp;
1223 int i;
1224 int iclogsize;
1225
Dave Chinner644c3562008-11-10 16:50:24 +11001226 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1227 if (!log)
1228 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 log->l_mp = mp;
1231 log->l_targ = log_target;
1232 log->l_logsize = BBTOB(num_bblks);
1233 log->l_logBBstart = blk_offset;
1234 log->l_logBBsize = num_bblks;
1235 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1236 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1237
1238 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001239 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1241 log->l_last_sync_lsn = log->l_tail_lsn;
1242 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1243 log->l_grant_reserve_cycle = 1;
1244 log->l_grant_write_cycle = 1;
1245
Eric Sandeen62118702008-03-06 13:44:28 +11001246 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1248 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1249 /* for larger sector sizes, must have v2 or external log */
1250 ASSERT(log->l_sectbb_log == 0 ||
1251 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001252 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1254 }
1255 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1256
1257 xlog_get_iclog_buffer_size(mp, log);
1258
1259 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001260 if (!bp)
1261 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1263 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1264 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1265 ASSERT(XFS_BUF_ISBUSY(bp));
1266 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1267 log->l_xbuf = bp;
1268
Eric Sandeen007c61c2007-10-11 17:43:56 +10001269 spin_lock_init(&log->l_icloglock);
1270 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001271 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001273 xlog_trace_loggrant_alloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1275 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1276
1277 iclogp = &log->l_iclog;
1278 /*
1279 * The amount of memory to allocate for the iclog structure is
1280 * rather funky due to the way the structure is defined. It is
1281 * done this way so that we can use different sizes for machines
1282 * with different amounts of memory. See the definition of
1283 * xlog_in_core_t in xfs_log_priv.h for details.
1284 */
1285 iclogsize = log->l_iclog_size;
1286 ASSERT(log->l_iclog_size >= 4096);
1287 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001288 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1289 if (!*iclogp)
1290 goto out_free_iclog;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 iclog->ic_prev = prev_iclog;
1294 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001295
1296 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001297 if (!bp)
1298 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001299 if (!XFS_BUF_CPSEMA(bp))
1300 ASSERT(0);
1301 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1302 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1303 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1304 iclog->ic_bp = bp;
1305 iclog->hic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001306#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 head = &iclog->ic_header;
1310 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001311 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1312 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001313 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001314 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001316 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1320 iclog->ic_state = XLOG_STATE_ACTIVE;
1321 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001322 atomic_set(&iclog->ic_refcnt, 0);
1323 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 iclog->ic_callback_tail = &(iclog->ic_callback);
1325 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1326
1327 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1328 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001329 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1330 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001332 xlog_trace_iclog_alloc(iclog);
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 iclogp = &iclog->ic_next;
1335 }
1336 *iclogp = log->l_iclog; /* complete ring */
1337 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1338
1339 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001340
1341out_free_iclog:
1342 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1343 prev_iclog = iclog->ic_next;
1344 if (iclog->ic_bp) {
1345 sv_destroy(&iclog->ic_force_wait);
1346 sv_destroy(&iclog->ic_write_wait);
1347 xfs_buf_free(iclog->ic_bp);
1348 xlog_trace_iclog_dealloc(iclog);
1349 }
1350 kmem_free(iclog);
1351 }
1352 spinlock_destroy(&log->l_icloglock);
1353 spinlock_destroy(&log->l_grant_lock);
1354 xlog_trace_loggrant_dealloc(log);
1355 xfs_buf_free(log->l_xbuf);
1356out_free_log:
1357 kmem_free(log);
1358 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359} /* xlog_alloc_log */
1360
1361
1362/*
1363 * Write out the commit record of a transaction associated with the given
1364 * ticket. Return the lsn of the commit record.
1365 */
1366STATIC int
1367xlog_commit_record(xfs_mount_t *mp,
1368 xlog_ticket_t *ticket,
1369 xlog_in_core_t **iclog,
1370 xfs_lsn_t *commitlsnp)
1371{
1372 int error;
1373 xfs_log_iovec_t reg[1];
1374
1375 reg[0].i_addr = NULL;
1376 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001377 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 ASSERT_ALWAYS(iclog);
1380 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1381 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001382 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001384 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385} /* xlog_commit_record */
1386
1387
1388/*
1389 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1390 * log space. This code pushes on the lsn which would supposedly free up
1391 * the 25% which we want to leave free. We may need to adopt a policy which
1392 * pushes on an lsn which is further along in the log once we reach the high
1393 * water mark. In this manner, we would be creating a low water mark.
1394 */
David Chinnera8272ce2007-11-23 16:28:09 +11001395STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396xlog_grant_push_ail(xfs_mount_t *mp,
1397 int need_bytes)
1398{
1399 xlog_t *log = mp->m_log; /* pointer to the log */
1400 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1401 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1402 int free_blocks; /* free blocks left to write to */
1403 int free_bytes; /* free bytes left to write to */
1404 int threshold_block; /* block in lsn we'd like to be at */
1405 int threshold_cycle; /* lsn cycle we'd like to be at */
1406 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1409
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001410 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 free_bytes = xlog_space_left(log,
1412 log->l_grant_reserve_cycle,
1413 log->l_grant_reserve_bytes);
1414 tail_lsn = log->l_tail_lsn;
1415 free_blocks = BTOBBT(free_bytes);
1416
1417 /*
1418 * Set the threshold for the minimum number of free blocks in the
1419 * log to the maximum of what the caller needs, one quarter of the
1420 * log, and 256 blocks.
1421 */
1422 free_threshold = BTOBB(need_bytes);
1423 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1424 free_threshold = MAX(free_threshold, 256);
1425 if (free_blocks < free_threshold) {
1426 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1427 threshold_cycle = CYCLE_LSN(tail_lsn);
1428 if (threshold_block >= log->l_logBBsize) {
1429 threshold_block -= log->l_logBBsize;
1430 threshold_cycle += 1;
1431 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001432 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* Don't pass in an lsn greater than the lsn of the last
1435 * log record known to be on disk.
1436 */
1437 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1438 threshold_lsn = log->l_last_sync_lsn;
1439 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001440 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /*
1443 * Get the transaction layer to kick the dirty buffers out to
1444 * disk asynchronously. No point in trying to do this if
1445 * the filesystem is shutting down.
1446 */
1447 if (threshold_lsn &&
1448 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001449 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450} /* xlog_grant_push_ail */
1451
1452
1453/*
1454 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1455 * fashion. Previously, we should have moved the current iclog
1456 * ptr in the log to point to the next available iclog. This allows further
1457 * write to continue while this code syncs out an iclog ready to go.
1458 * Before an in-core log can be written out, the data section must be scanned
1459 * to save away the 1st word of each BBSIZE block into the header. We replace
1460 * it with the current cycle count. Each BBSIZE block is tagged with the
1461 * cycle count because there in an implicit assumption that drives will
1462 * guarantee that entire 512 byte blocks get written at once. In other words,
1463 * we can't have part of a 512 byte block written and part not written. By
1464 * tagging each block, we will know which blocks are valid when recovering
1465 * after an unclean shutdown.
1466 *
1467 * This routine is single threaded on the iclog. No other thread can be in
1468 * this routine with the same iclog. Changing contents of iclog can there-
1469 * fore be done without grabbing the state machine lock. Updating the global
1470 * log will require grabbing the lock though.
1471 *
1472 * The entire log manager uses a logical block numbering scheme. Only
1473 * log_sync (and then only bwrite()) know about the fact that the log may
1474 * not start with block zero on a given device. The log block start offset
1475 * is added immediately before calling bwrite().
1476 */
1477
David Chinnera8272ce2007-11-23 16:28:09 +11001478STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479xlog_sync(xlog_t *log,
1480 xlog_in_core_t *iclog)
1481{
1482 xfs_caddr_t dptr; /* pointer to byte sized element */
1483 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001484 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 uint count; /* byte count of bwrite */
1486 uint count_init; /* initial count before roundup */
1487 int roundoff; /* roundoff to BB or stripe */
1488 int split = 0; /* split write into two regions */
1489 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001490 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001493 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /* Add for LR header */
1496 count_init = log->l_iclog_hsize + iclog->ic_offset;
1497
1498 /* Round out the log write size */
1499 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1500 /* we have a v2 stripe unit to use */
1501 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1502 } else {
1503 count = BBTOB(BTOBB(count_init));
1504 }
1505 roundoff = count - count_init;
1506 ASSERT(roundoff >= 0);
1507 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1508 roundoff < log->l_mp->m_sb.sb_logsunit)
1509 ||
1510 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1511 roundoff < BBTOB(1)));
1512
1513 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001514 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001515 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001516 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /* put cycle number in every block */
1519 xlog_pack_data(log, iclog, roundoff);
1520
1521 /* real byte length */
1522 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001523 iclog->ic_header.h_len =
1524 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001526 iclog->ic_header.h_len =
1527 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
Nathan Scottf5faad72006-07-28 17:04:44 +10001530 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1532 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001533 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1536
1537 /* Do we need to split this write into 2 parts? */
1538 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1539 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1540 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1541 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1542 } else {
1543 iclog->ic_bwritecnt = 1;
1544 }
David Chinner511105b2007-05-24 15:21:57 +10001545 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001547 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 XFS_BUF_BUSY(bp);
1549 XFS_BUF_ASYNC(bp);
1550 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001551 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001552 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001554 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001555 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1558 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1559
1560 xlog_verify_iclog(log, iclog, count, B_TRUE);
1561
1562 /* account for log which doesn't start at block #0 */
1563 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1564 /*
1565 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1566 * is shutting down.
1567 */
1568 XFS_BUF_WRITE(bp);
1569
1570 if ((error = XFS_bwrite(bp))) {
1571 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1572 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001573 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001576 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1578 (unsigned long)1);
1579 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1580 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1581 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1582 (__psint_t)count), split);
1583 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001584 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 XFS_BUF_BUSY(bp);
1586 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001587 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1588 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 dptr = XFS_BUF_PTR(bp);
1590 /*
1591 * Bump the cycle numbers at the start of each block
1592 * since this part of the buffer is at the start of
1593 * a new cycle. Watch out for the header magic number
1594 * case, though.
1595 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001596 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001597 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001598 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001599 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 dptr += BBSIZE;
1601 }
1602
1603 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1604 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1605
Nathan Scottc41564b2006-03-29 08:55:14 +10001606 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1608 XFS_BUF_WRITE(bp);
1609 if ((error = XFS_bwrite(bp))) {
1610 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1611 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001612 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616} /* xlog_sync */
1617
1618
1619/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001620 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
David Chinnera8272ce2007-11-23 16:28:09 +11001622STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001623xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int i;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 iclog = log->l_iclog;
1629 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001630 sv_destroy(&iclog->ic_force_wait);
1631 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 xfs_buf_free(iclog->ic_bp);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001633 xlog_trace_iclog_dealloc(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001635 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 iclog = next_iclog;
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 spinlock_destroy(&log->l_icloglock);
1639 spinlock_destroy(&log->l_grant_lock);
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 xfs_buf_free(log->l_xbuf);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001642 xlog_trace_loggrant_dealloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001644 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001645} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647/*
1648 * Update counters atomically now that memcpy is done.
1649 */
1650/* ARGSUSED */
1651static inline void
1652xlog_state_finish_copy(xlog_t *log,
1653 xlog_in_core_t *iclog,
1654 int record_cnt,
1655 int copy_bytes)
1656{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001657 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001659 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 iclog->ic_offset += copy_bytes;
1661
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001662 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663} /* xlog_state_finish_copy */
1664
1665
1666
1667
1668/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001669 * print out info relating to regions written which consume
1670 * the reservation
1671 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001672STATIC void
1673xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1674{
1675 uint i;
1676 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1677
1678 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1679 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1680 "bformat",
1681 "bchunk",
1682 "efi_format",
1683 "efd_format",
1684 "iformat",
1685 "icore",
1686 "iext",
1687 "ibroot",
1688 "ilocal",
1689 "iattr_ext",
1690 "iattr_broot",
1691 "iattr_local",
1692 "qformat",
1693 "dquot",
1694 "quotaoff",
1695 "LR header",
1696 "unmount",
1697 "commit",
1698 "trans header"
1699 };
1700 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1701 "SETATTR_NOT_SIZE",
1702 "SETATTR_SIZE",
1703 "INACTIVE",
1704 "CREATE",
1705 "CREATE_TRUNC",
1706 "TRUNCATE_FILE",
1707 "REMOVE",
1708 "LINK",
1709 "RENAME",
1710 "MKDIR",
1711 "RMDIR",
1712 "SYMLINK",
1713 "SET_DMATTRS",
1714 "GROWFS",
1715 "STRAT_WRITE",
1716 "DIOSTRAT",
1717 "WRITE_SYNC",
1718 "WRITEID",
1719 "ADDAFORK",
1720 "ATTRINVAL",
1721 "ATRUNCATE",
1722 "ATTR_SET",
1723 "ATTR_RM",
1724 "ATTR_FLAG",
1725 "CLEAR_AGI_BUCKET",
1726 "QM_SBCHANGE",
1727 "DUMMY1",
1728 "DUMMY2",
1729 "QM_QUOTAOFF",
1730 "QM_DQALLOC",
1731 "QM_SETQLIM",
1732 "QM_DQCLUSTER",
1733 "QM_QINOCREATE",
1734 "QM_QUOTAOFF_END",
1735 "SB_UNIT",
1736 "FSYNC_TS",
1737 "GROWFSRT_ALLOC",
1738 "GROWFSRT_ZERO",
1739 "GROWFSRT_FREE",
1740 "SWAPEXT"
1741 };
1742
1743 xfs_fs_cmn_err(CE_WARN, mp,
1744 "xfs_log_write: reservation summary:\n"
1745 " trans type = %s (%u)\n"
1746 " unit res = %d bytes\n"
1747 " current res = %d bytes\n"
1748 " total reg = %u bytes (o/flow = %u bytes)\n"
1749 " ophdrs = %u (ophdr space = %u bytes)\n"
1750 " ophdr + reg = %u bytes\n"
1751 " num regions = %u\n",
1752 ((ticket->t_trans_type <= 0 ||
1753 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1754 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1755 ticket->t_trans_type,
1756 ticket->t_unit_res,
1757 ticket->t_curr_res,
1758 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1759 ticket->t_res_num_ophdrs, ophdr_spc,
1760 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001761 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001762 ticket->t_res_num);
1763
1764 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001765 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001766 cmn_err(CE_WARN,
1767 "region[%u]: %s - %u bytes\n",
1768 i,
1769 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1770 "bad-rtype" : res_type_str[r_type-1]),
1771 ticket->t_res_arr[i].r_len);
1772 }
1773}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001774
1775/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * Write some region out to in-core log
1777 *
1778 * This will be called when writing externally provided regions or when
1779 * writing out a commit record for a given transaction.
1780 *
1781 * General algorithm:
1782 * 1. Find total length of this write. This may include adding to the
1783 * lengths passed in.
1784 * 2. Check whether we violate the tickets reservation.
1785 * 3. While writing to this iclog
1786 * A. Reserve as much space in this iclog as can get
1787 * B. If this is first write, save away start lsn
1788 * C. While writing this region:
1789 * 1. If first write of transaction, write start record
1790 * 2. Write log operation header (header per region)
1791 * 3. Find out if we can fit entire region into this iclog
1792 * 4. Potentially, verify destination memcpy ptr
1793 * 5. Memcpy (partial) region
1794 * 6. If partial copy, release iclog; otherwise, continue
1795 * copying more regions into current iclog
1796 * 4. Mark want sync bit (in simulation mode)
1797 * 5. Release iclog for potential flush to on-disk log.
1798 *
1799 * ERRORS:
1800 * 1. Panic if reservation is overrun. This should never happen since
1801 * reservation amounts are generated internal to the filesystem.
1802 * NOTES:
1803 * 1. Tickets are single threaded data structures.
1804 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1805 * syncing routine. When a single log_write region needs to span
1806 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1807 * on all log operation writes which don't contain the end of the
1808 * region. The XLOG_END_TRANS bit is used for the in-core log
1809 * operation which contains the end of the continued log_write region.
1810 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1811 * we don't really know exactly how much space will be used. As a result,
1812 * we don't update ic_offset until the end when we know exactly how many
1813 * bytes have been written out.
1814 */
David Chinnera8272ce2007-11-23 16:28:09 +11001815STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816xlog_write(xfs_mount_t * mp,
1817 xfs_log_iovec_t reg[],
1818 int nentries,
1819 xfs_log_ticket_t tic,
1820 xfs_lsn_t *start_lsn,
1821 xlog_in_core_t **commit_iclog,
1822 uint flags)
1823{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001824 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001826 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 __psint_t ptr; /* copy address into data region */
1829 int len; /* # xlog_write() bytes 2 still copy */
1830 int index; /* region index currently copying */
1831 int log_offset; /* offset (from 0) into data region */
1832 int start_rec_copy; /* # bytes to copy for start record */
1833 int partial_copy; /* did we split a region? */
1834 int partial_copy_len;/* # bytes copied if split region */
1835 int need_copy; /* # bytes need to memcpy this region */
1836 int copy_len; /* # bytes actually memcpy'ing */
1837 int copy_off; /* # bytes from entry start */
1838 int contwr; /* continued write of in-core log? */
1839 int error;
1840 int record_cnt = 0, data_cnt = 0;
1841
1842 partial_copy_len = partial_copy = 0;
1843
1844 /* Calculate potential maximum space. Each region gets its own
1845 * xlog_op_header_t and may need to be double word aligned.
1846 */
1847 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001848 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001850 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 for (index = 0; index < nentries; index++) {
1854 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001855 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001857 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 contwr = *start_lsn = 0;
1860
1861 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001862 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863#ifdef DEBUG
1864 xlog_panic(
1865 "xfs_log_write: reservation ran out. Need to up reservation");
1866#else
1867 /* Customer configurable panic */
1868 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1869 "xfs_log_write: reservation ran out. Need to up reservation");
1870 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001871 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872#endif
1873 } else
1874 ticket->t_curr_res -= len;
1875
1876 for (index = 0; index < nentries; ) {
1877 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1878 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001879 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 ASSERT(log_offset <= iclog->ic_size - 1);
1882 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1883
1884 /* start_lsn is the first lsn written to. That's all we need. */
1885 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001886 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /* This loop writes out as many regions as can fit in the amount
1889 * of space which was allocated by xlog_state_get_iclog_space().
1890 */
1891 while (index < nentries) {
1892 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1893 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1894 start_rec_copy = 0;
1895
1896 /* If first write for transaction, insert start record.
1897 * We can't be trying to commit if we are inited. We can't
1898 * have any "partial_copy" if we are inited.
1899 */
1900 if (ticket->t_flags & XLOG_TIC_INITED) {
1901 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001902 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 logop_head->oh_clientid = ticket->t_clientid;
1904 logop_head->oh_len = 0;
1905 logop_head->oh_flags = XLOG_START_TRANS;
1906 logop_head->oh_res2 = 0;
1907 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1908 record_cnt++;
1909
1910 start_rec_copy = sizeof(xlog_op_header_t);
1911 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1912 }
1913
1914 /* Copy log operation header directly into data section */
1915 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001916 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 logop_head->oh_clientid = ticket->t_clientid;
1918 logop_head->oh_res2 = 0;
1919
1920 /* header copied directly */
1921 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1922
1923 /* are we copying a commit or unmount record? */
1924 logop_head->oh_flags = flags;
1925
1926 /*
1927 * We've seen logs corrupted with bad transaction client
1928 * ids. This makes sure that XFS doesn't generate them on.
1929 * Turn this into an EIO and shut down the filesystem.
1930 */
1931 switch (logop_head->oh_clientid) {
1932 case XFS_TRANSACTION:
1933 case XFS_VOLUME:
1934 case XFS_LOG:
1935 break;
1936 default:
1937 xfs_fs_cmn_err(CE_WARN, mp,
1938 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1939 logop_head->oh_clientid, tic);
1940 return XFS_ERROR(EIO);
1941 }
1942
1943 /* Partial write last time? => (partial_copy != 0)
1944 * need_copy is the amount we'd like to copy if everything could
1945 * fit in the current memcpy.
1946 */
1947 need_copy = reg[index].i_len - partial_copy_len;
1948
1949 copy_off = partial_copy_len;
1950 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001951 copy_len = need_copy;
1952 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (partial_copy)
1954 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1955 partial_copy_len = partial_copy = 0;
1956 } else { /* partial write */
1957 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001958 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1960 if (partial_copy)
1961 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1962 partial_copy_len += copy_len;
1963 partial_copy++;
1964 len += sizeof(xlog_op_header_t); /* from splitting of region */
1965 /* account for new log op header */
1966 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001967 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969 xlog_verify_dest_ptr(log, ptr);
1970
1971 /* copy region */
1972 ASSERT(copy_len >= 0);
1973 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1974 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1975
1976 /* make copy_len total bytes copied, including headers */
1977 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1978 record_cnt++;
1979 data_cnt += contwr ? copy_len : 0;
1980 if (partial_copy) { /* copied partial region */
1981 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1982 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1983 record_cnt = data_cnt = 0;
1984 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001985 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 break; /* don't increment index */
1987 } else { /* copied entire region */
1988 index++;
1989 partial_copy_len = partial_copy = 0;
1990
1991 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1992 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1993 record_cnt = data_cnt = 0;
1994 xlog_state_want_sync(log, iclog);
1995 if (commit_iclog) {
1996 ASSERT(flags & XLOG_COMMIT_TRANS);
1997 *commit_iclog = iclog;
1998 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001999 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (index == nentries)
2001 return 0; /* we are done */
2002 else
2003 break;
2004 }
2005 } /* if (partial_copy) */
2006 } /* while (index < nentries) */
2007 } /* for (index = 0; index < nentries; ) */
2008 ASSERT(len == 0);
2009
2010 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2011 if (commit_iclog) {
2012 ASSERT(flags & XLOG_COMMIT_TRANS);
2013 *commit_iclog = iclog;
2014 return 0;
2015 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002016 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017} /* xlog_write */
2018
2019
2020/*****************************************************************************
2021 *
2022 * State Machine functions
2023 *
2024 *****************************************************************************
2025 */
2026
2027/* Clean iclogs starting from the head. This ordering must be
2028 * maintained, so an iclog doesn't become ACTIVE beyond one that
2029 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002030 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 * iclog is trying to sync to disk.
2032 *
2033 * State Change: DIRTY -> ACTIVE
2034 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002035STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036xlog_state_clean_log(xlog_t *log)
2037{
2038 xlog_in_core_t *iclog;
2039 int changed = 0;
2040
2041 iclog = log->l_iclog;
2042 do {
2043 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2044 iclog->ic_state = XLOG_STATE_ACTIVE;
2045 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002046 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /*
2048 * If the number of ops in this iclog indicate it just
2049 * contains the dummy transaction, we can
2050 * change state into IDLE (the second time around).
2051 * Otherwise we should change the state into
2052 * NEED a dummy.
2053 * We don't need to cover the dummy.
2054 */
2055 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002056 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2057 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 changed = 1;
2059 } else {
2060 /*
2061 * We have two dirty iclogs so start over
2062 * This could also be num of ops indicates
2063 * this is not the dummy going out.
2064 */
2065 changed = 2;
2066 }
2067 iclog->ic_header.h_num_logops = 0;
2068 memset(iclog->ic_header.h_cycle_data, 0,
2069 sizeof(iclog->ic_header.h_cycle_data));
2070 iclog->ic_header.h_lsn = 0;
2071 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2072 /* do nothing */;
2073 else
2074 break; /* stop cleaning */
2075 iclog = iclog->ic_next;
2076 } while (iclog != log->l_iclog);
2077
2078 /* log is locked when we are called */
2079 /*
2080 * Change state for the dummy log recording.
2081 * We usually go to NEED. But we go to NEED2 if the changed indicates
2082 * we are done writing the dummy record.
2083 * If we are done with the second dummy recored (DONE2), then
2084 * we go to IDLE.
2085 */
2086 if (changed) {
2087 switch (log->l_covered_state) {
2088 case XLOG_STATE_COVER_IDLE:
2089 case XLOG_STATE_COVER_NEED:
2090 case XLOG_STATE_COVER_NEED2:
2091 log->l_covered_state = XLOG_STATE_COVER_NEED;
2092 break;
2093
2094 case XLOG_STATE_COVER_DONE:
2095 if (changed == 1)
2096 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2097 else
2098 log->l_covered_state = XLOG_STATE_COVER_NEED;
2099 break;
2100
2101 case XLOG_STATE_COVER_DONE2:
2102 if (changed == 1)
2103 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2104 else
2105 log->l_covered_state = XLOG_STATE_COVER_NEED;
2106 break;
2107
2108 default:
2109 ASSERT(0);
2110 }
2111 }
2112} /* xlog_state_clean_log */
2113
2114STATIC xfs_lsn_t
2115xlog_get_lowest_lsn(
2116 xlog_t *log)
2117{
2118 xlog_in_core_t *lsn_log;
2119 xfs_lsn_t lowest_lsn, lsn;
2120
2121 lsn_log = log->l_iclog;
2122 lowest_lsn = 0;
2123 do {
2124 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002125 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if ((lsn && !lowest_lsn) ||
2127 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2128 lowest_lsn = lsn;
2129 }
2130 }
2131 lsn_log = lsn_log->ic_next;
2132 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002133 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
2136
2137STATIC void
2138xlog_state_do_callback(
2139 xlog_t *log,
2140 int aborted,
2141 xlog_in_core_t *ciclog)
2142{
2143 xlog_in_core_t *iclog;
2144 xlog_in_core_t *first_iclog; /* used to know when we've
2145 * processed all iclogs once */
2146 xfs_log_callback_t *cb, *cb_next;
2147 int flushcnt = 0;
2148 xfs_lsn_t lowest_lsn;
2149 int ioerrors; /* counter: iclogs with errors */
2150 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2151 int funcdidcallbacks; /* flag: function did callbacks */
2152 int repeats; /* for issuing console warnings if
2153 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002154 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002156 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 first_iclog = iclog = log->l_iclog;
2158 ioerrors = 0;
2159 funcdidcallbacks = 0;
2160 repeats = 0;
2161
2162 do {
2163 /*
2164 * Scan all iclogs starting with the one pointed to by the
2165 * log. Reset this starting point each time the log is
2166 * unlocked (during callbacks).
2167 *
2168 * Keep looping through iclogs until one full pass is made
2169 * without running any callbacks.
2170 */
2171 first_iclog = log->l_iclog;
2172 iclog = log->l_iclog;
2173 loopdidcallbacks = 0;
2174 repeats++;
2175
2176 do {
2177
2178 /* skip all iclogs in the ACTIVE & DIRTY states */
2179 if (iclog->ic_state &
2180 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2181 iclog = iclog->ic_next;
2182 continue;
2183 }
2184
2185 /*
2186 * Between marking a filesystem SHUTDOWN and stopping
2187 * the log, we do flush all iclogs to disk (if there
2188 * wasn't a log I/O error). So, we do want things to
2189 * go smoothly in case of just a SHUTDOWN w/o a
2190 * LOG_IO_ERROR.
2191 */
2192 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2193 /*
2194 * Can only perform callbacks in order. Since
2195 * this iclog is not in the DONE_SYNC/
2196 * DO_CALLBACK state, we skip the rest and
2197 * just try to clean up. If we set our iclog
2198 * to DO_CALLBACK, we will not process it when
2199 * we retry since a previous iclog is in the
2200 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002201 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 */
2203 if (!(iclog->ic_state &
2204 (XLOG_STATE_DONE_SYNC |
2205 XLOG_STATE_DO_CALLBACK))) {
2206 if (ciclog && (ciclog->ic_state ==
2207 XLOG_STATE_DONE_SYNC)) {
2208 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2209 }
2210 break;
2211 }
2212 /*
2213 * We now have an iclog that is in either the
2214 * DO_CALLBACK or DONE_SYNC states. The other
2215 * states (WANT_SYNC, SYNCING, or CALLBACK were
2216 * caught by the above if and are going to
2217 * clean (i.e. we aren't doing their callbacks)
2218 * see the above if.
2219 */
2220
2221 /*
2222 * We will do one more check here to see if we
2223 * have chased our tail around.
2224 */
2225
2226 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002227 if (lowest_lsn &&
2228 XFS_LSN_CMP(lowest_lsn,
2229 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 iclog = iclog->ic_next;
2231 continue; /* Leave this iclog for
2232 * another thread */
2233 }
2234
2235 iclog->ic_state = XLOG_STATE_CALLBACK;
2236
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002237 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002240 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 * No one else can be here except us.
2242 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002243 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002244 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2245 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2246 log->l_last_sync_lsn =
2247 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002248 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002251 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 ioerrors++;
2253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
David Chinner114d23a2008-04-10 12:18:39 +10002255 /*
2256 * Keep processing entries in the callback list until
2257 * we come around and it is empty. We need to
2258 * atomically see that the list is empty and change the
2259 * state to DIRTY so that we don't miss any more
2260 * callbacks being added.
2261 */
2262 spin_lock(&iclog->ic_callback_lock);
2263 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002264 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 iclog->ic_callback_tail = &(iclog->ic_callback);
2266 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002267 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002270 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 cb_next = cb->cb_next;
2272 cb->cb_func(cb->cb_arg, aborted);
2273 }
David Chinner114d23a2008-04-10 12:18:39 +10002274 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 cb = iclog->ic_callback;
2276 }
2277
2278 loopdidcallbacks++;
2279 funcdidcallbacks++;
2280
David Chinner114d23a2008-04-10 12:18:39 +10002281 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002282 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002283 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2285 iclog->ic_state = XLOG_STATE_DIRTY;
2286
2287 /*
2288 * Transition from DIRTY to ACTIVE if applicable.
2289 * NOP if STATE_IOERROR.
2290 */
2291 xlog_state_clean_log(log);
2292
2293 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002294 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 iclog = iclog->ic_next;
2297 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002298
2299 if (repeats > 5000) {
2300 flushcnt += repeats;
2301 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002303 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002304 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 } while (!ioerrors && loopdidcallbacks);
2307
2308 /*
2309 * make one last gasp attempt to see if iclogs are being left in
2310 * limbo..
2311 */
2312#ifdef DEBUG
2313 if (funcdidcallbacks) {
2314 first_iclog = iclog = log->l_iclog;
2315 do {
2316 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2317 /*
2318 * Terminate the loop if iclogs are found in states
2319 * which will cause other threads to clean up iclogs.
2320 *
2321 * SYNCING - i/o completion will go through logs
2322 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002323 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 * IOERROR - give up hope all ye who enter here
2325 */
2326 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2327 iclog->ic_state == XLOG_STATE_SYNCING ||
2328 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2329 iclog->ic_state == XLOG_STATE_IOERROR )
2330 break;
2331 iclog = iclog->ic_next;
2332 } while (first_iclog != iclog);
2333 }
2334#endif
2335
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002336 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2337 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002338 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002339
2340 if (wake)
2341 sv_broadcast(&log->l_flush_wait);
2342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344
2345/*
2346 * Finish transitioning this iclog to the dirty state.
2347 *
2348 * Make sure that we completely execute this routine only when this is
2349 * the last call to the iclog. There is a good chance that iclog flushes,
2350 * when we reach the end of the physical log, get turned into 2 separate
2351 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2352 * routine. By using the reference count bwritecnt, we guarantee that only
2353 * the second completion goes through.
2354 *
2355 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002356 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
David Chinnera8272ce2007-11-23 16:28:09 +11002358STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359xlog_state_done_syncing(
2360 xlog_in_core_t *iclog,
2361 int aborted)
2362{
2363 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002365 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2368 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002369 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2371
2372
2373 /*
2374 * If we got an error, either on the first buffer, or in the case of
2375 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2376 * and none should ever be attempted to be written to disk
2377 * again.
2378 */
2379 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2380 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002381 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return;
2383 }
2384 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2385 }
2386
2387 /*
2388 * Someone could be sleeping prior to writing out the next
2389 * iclog buffer, we wake them all, one will get to do the
2390 * I/O, the others get to wait for the result.
2391 */
David Chinner12017fa2008-08-13 16:34:31 +10002392 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002393 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2395} /* xlog_state_done_syncing */
2396
2397
2398/*
2399 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002400 * sleep. We wait on the flush queue on the head iclog as that should be
2401 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2402 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 *
2404 * The in-core logs are used in a circular fashion. They are not used
2405 * out-of-order even when an iclog past the head is free.
2406 *
2407 * return:
2408 * * log_offset where xlog_write() can start writing into the in-core
2409 * log's data space.
2410 * * in-core log pointer to which xlog_write() should write.
2411 * * boolean indicating this is a continued write to an in-core log.
2412 * If this is the last write, then the in-core log's offset field
2413 * needs to be incremented, depending on the amount of data which
2414 * is copied.
2415 */
David Chinnera8272ce2007-11-23 16:28:09 +11002416STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417xlog_state_get_iclog_space(xlog_t *log,
2418 int len,
2419 xlog_in_core_t **iclogp,
2420 xlog_ticket_t *ticket,
2421 int *continued_write,
2422 int *logoffsetp)
2423{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int log_offset;
2425 xlog_rec_header_t *head;
2426 xlog_in_core_t *iclog;
2427 int error;
2428
2429restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002430 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002432 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return XFS_ERROR(EIO);
2434 }
2435
2436 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002437 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2439 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002440
2441 /* Wait for log writes to have flushed */
2442 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto restart;
2444 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 head = &iclog->ic_header;
2447
David Chinner155cc6b2008-03-06 13:44:14 +11002448 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 log_offset = iclog->ic_offset;
2450
2451 /* On the 1st write to an iclog, figure out lsn. This works
2452 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2453 * committing to. If the offset is set, that's how many blocks
2454 * must be written.
2455 */
2456 if (log_offset == 0) {
2457 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002458 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002459 log->l_iclog_hsize,
2460 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002461 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2462 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002463 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 ASSERT(log->l_curr_block >= 0);
2465 }
2466
2467 /* If there is enough room to write everything, then do it. Otherwise,
2468 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2469 * bit is on, so this will get flushed out. Don't update ic_offset
2470 * until you know exactly how many bytes get copied. Therefore, wait
2471 * until later to update ic_offset.
2472 *
2473 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2474 * can fit into remaining data section.
2475 */
2476 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2477 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2478
Dave Chinner49641f12008-07-11 17:43:55 +10002479 /*
2480 * If I'm the only one writing to this iclog, sync it to disk.
2481 * We need to do an atomic compare and decrement here to avoid
2482 * racing with concurrent atomic_dec_and_lock() calls in
2483 * xlog_state_release_iclog() when there is more than one
2484 * reference to the iclog.
2485 */
2486 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2487 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002488 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002489 error = xlog_state_release_iclog(log, iclog);
2490 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002491 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002493 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495 goto restart;
2496 }
2497
2498 /* Do we have enough room to write the full amount in the remainder
2499 * of this iclog? Or must we continue a write on the next iclog and
2500 * mark this iclog as completely taken? In the case where we switch
2501 * iclogs (to mark it taken), this particular iclog will release/sync
2502 * to disk in xlog_write().
2503 */
2504 if (len <= iclog->ic_size - iclog->ic_offset) {
2505 *continued_write = 0;
2506 iclog->ic_offset += len;
2507 } else {
2508 *continued_write = 1;
2509 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2510 }
2511 *iclogp = iclog;
2512
2513 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002514 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 *logoffsetp = log_offset;
2517 return 0;
2518} /* xlog_state_get_iclog_space */
2519
2520/*
2521 * Atomically get the log space required for a log ticket.
2522 *
2523 * Once a ticket gets put onto the reserveq, it will only return after
2524 * the needed reservation is satisfied.
2525 */
2526STATIC int
2527xlog_grant_log_space(xlog_t *log,
2528 xlog_ticket_t *tic)
2529{
2530 int free_bytes;
2531 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532#ifdef DEBUG
2533 xfs_lsn_t tail_lsn;
2534#endif
2535
2536
2537#ifdef DEBUG
2538 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2539 panic("grant Recovery problem");
2540#endif
2541
2542 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002543 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2545
2546 /* something is already sleeping; insert new transaction at end */
2547 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002548 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 xlog_trace_loggrant(log, tic,
2550 "xlog_grant_log_space: sleep 1");
2551 /*
2552 * Gotta check this before going to sleep, while we're
2553 * holding the grant lock.
2554 */
2555 if (XLOG_FORCED_SHUTDOWN(log))
2556 goto error_return;
2557
2558 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002559 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 /*
2561 * If we got an error, and the filesystem is shutting down,
2562 * we'll catch it down below. So just continue...
2563 */
2564 xlog_trace_loggrant(log, tic,
2565 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002566 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2569 need_bytes = tic->t_unit_res*tic->t_ocnt;
2570 else
2571 need_bytes = tic->t_unit_res;
2572
2573redo:
2574 if (XLOG_FORCED_SHUTDOWN(log))
2575 goto error_return;
2576
2577 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2578 log->l_grant_reserve_bytes);
2579 if (free_bytes < need_bytes) {
2580 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002581 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 xlog_trace_loggrant(log, tic,
2583 "xlog_grant_log_space: sleep 2");
2584 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002585 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002588 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 goto error_return;
2590 }
2591
2592 xlog_trace_loggrant(log, tic,
2593 "xlog_grant_log_space: wake 2");
2594 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002595 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 goto redo;
2597 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002598 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002601 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602#ifdef DEBUG
2603 tail_lsn = log->l_tail_lsn;
2604 /*
2605 * Check to make sure the grant write head didn't just over lap the
2606 * tail. If the cycles are the same, we can't be overlapping.
2607 * Otherwise, make sure that the cycles differ by exactly one and
2608 * check the byte count.
2609 */
2610 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2611 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2612 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2613 }
2614#endif
2615 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2616 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002617 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return 0;
2619
2620 error_return:
2621 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002622 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2624 /*
2625 * If we are failing, make sure the ticket doesn't have any
2626 * current reservations. We don't want to add this back when
2627 * the ticket/transaction gets cancelled.
2628 */
2629 tic->t_curr_res = 0;
2630 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002631 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return XFS_ERROR(EIO);
2633} /* xlog_grant_log_space */
2634
2635
2636/*
2637 * Replenish the byte reservation required by moving the grant write head.
2638 *
2639 *
2640 */
2641STATIC int
2642xlog_regrant_write_log_space(xlog_t *log,
2643 xlog_ticket_t *tic)
2644{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 int free_bytes, need_bytes;
2646 xlog_ticket_t *ntic;
2647#ifdef DEBUG
2648 xfs_lsn_t tail_lsn;
2649#endif
2650
2651 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002652 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657#ifdef DEBUG
2658 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2659 panic("regrant Recovery problem");
2660#endif
2661
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002662 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2664
2665 if (XLOG_FORCED_SHUTDOWN(log))
2666 goto error_return;
2667
2668 /* If there are other waiters on the queue then give them a
2669 * chance at logspace before us. Wake up the first waiters,
2670 * if we do not wake up all the waiters then go to sleep waiting
2671 * for more free space, otherwise try to get some space for
2672 * this transaction.
2673 */
2674
2675 if ((ntic = log->l_write_headq)) {
2676 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2677 log->l_grant_write_bytes);
2678 do {
2679 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2680
2681 if (free_bytes < ntic->t_unit_res)
2682 break;
2683 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002684 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 ntic = ntic->t_next;
2686 } while (ntic != log->l_write_headq);
2687
2688 if (ntic != log->l_write_headq) {
2689 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002690 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 xlog_trace_loggrant(log, tic,
2693 "xlog_regrant_write_log_space: sleep 1");
2694 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002695 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 &log->l_grant_lock, s);
2697
2698 /* If we're shutting down, this tic is already
2699 * off the queue */
2700 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002701 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto error_return;
2703 }
2704
2705 xlog_trace_loggrant(log, tic,
2706 "xlog_regrant_write_log_space: wake 1");
2707 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002708 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710 }
2711
2712 need_bytes = tic->t_unit_res;
2713
2714redo:
2715 if (XLOG_FORCED_SHUTDOWN(log))
2716 goto error_return;
2717
2718 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2719 log->l_grant_write_bytes);
2720 if (free_bytes < need_bytes) {
2721 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002722 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002724 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 /* If we're shutting down, this tic is already off the queue */
2727 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002728 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 goto error_return;
2730 }
2731
2732 xlog_trace_loggrant(log, tic,
2733 "xlog_regrant_write_log_space: wake 2");
2734 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002735 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 goto redo;
2737 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002738 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002740 /* we've got enough space */
2741 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742#ifdef DEBUG
2743 tail_lsn = log->l_tail_lsn;
2744 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2745 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2746 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2747 }
2748#endif
2749
2750 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2751 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002752 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755
2756 error_return:
2757 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002758 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2760 /*
2761 * If we are failing, make sure the ticket doesn't have any
2762 * current reservations. We don't want to add this back when
2763 * the ticket/transaction gets cancelled.
2764 */
2765 tic->t_curr_res = 0;
2766 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002767 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return XFS_ERROR(EIO);
2769} /* xlog_regrant_write_log_space */
2770
2771
2772/* The first cnt-1 times through here we don't need to
2773 * move the grant write head because the permanent
2774 * reservation has reserved cnt times the unit amount.
2775 * Release part of current permanent unit reservation and
2776 * reset current reservation to be one units worth. Also
2777 * move grant reservation head forward.
2778 */
2779STATIC void
2780xlog_regrant_reserve_log_space(xlog_t *log,
2781 xlog_ticket_t *ticket)
2782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 xlog_trace_loggrant(log, ticket,
2784 "xlog_regrant_reserve_log_space: enter");
2785 if (ticket->t_cnt > 0)
2786 ticket->t_cnt--;
2787
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002788 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002789 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002791 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 xlog_trace_loggrant(log, ticket,
2793 "xlog_regrant_reserve_log_space: sub current res");
2794 xlog_verify_grant_head(log, 1);
2795
2796 /* just return if we still have some of the pre-reserved space */
2797 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002798 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return;
2800 }
2801
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002802 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 xlog_trace_loggrant(log, ticket,
2804 "xlog_regrant_reserve_log_space: exit");
2805 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002806 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002808 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809} /* xlog_regrant_reserve_log_space */
2810
2811
2812/*
2813 * Give back the space left from a reservation.
2814 *
2815 * All the information we need to make a correct determination of space left
2816 * is present. For non-permanent reservations, things are quite easy. The
2817 * count should have been decremented to zero. We only need to deal with the
2818 * space remaining in the current reservation part of the ticket. If the
2819 * ticket contains a permanent reservation, there may be left over space which
2820 * needs to be released. A count of N means that N-1 refills of the current
2821 * reservation can be done before we need to ask for more space. The first
2822 * one goes to fill up the first current reservation. Once we run out of
2823 * space, the count will stay at zero and the only space remaining will be
2824 * in the current reservation field.
2825 */
2826STATIC void
2827xlog_ungrant_log_space(xlog_t *log,
2828 xlog_ticket_t *ticket)
2829{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (ticket->t_cnt > 0)
2831 ticket->t_cnt--;
2832
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002833 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2835
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002836 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2839
2840 /* If this is a permanent reservation ticket, we may be able to free
2841 * up more space based on the remaining count.
2842 */
2843 if (ticket->t_cnt > 0) {
2844 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002845 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847
2848 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2849 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002850 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 xfs_log_move_tail(log->l_mp, 1);
2852} /* xlog_ungrant_log_space */
2853
2854
2855/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 * Flush iclog to disk if this is the last reference to the given iclog and
2857 * the WANT_SYNC bit is set.
2858 *
2859 * When this function is entered, the iclog is not necessarily in the
2860 * WANT_SYNC state. It may be sitting around waiting to get filled.
2861 *
2862 *
2863 */
David Chinnera8272ce2007-11-23 16:28:09 +11002864STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002865xlog_state_release_iclog(
2866 xlog_t *log,
2867 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 int sync = 0; /* do we sync? */
2870
David Chinner155cc6b2008-03-06 13:44:14 +11002871 if (iclog->ic_state & XLOG_STATE_IOERROR)
2872 return XFS_ERROR(EIO);
2873
2874 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2875 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2876 return 0;
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002879 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return XFS_ERROR(EIO);
2881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2883 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2884
David Chinner155cc6b2008-03-06 13:44:14 +11002885 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002886 /* update tail before writing to iclog */
2887 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 sync++;
2889 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002890 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2892 /* cycle incremented when incrementing curr_block */
2893 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002894 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 /*
2897 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002898 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2900 * this iclog has consistent data, so we ignore IOERROR
2901 * flags after this point.
2902 */
David Chinnerb5893342008-03-06 13:44:06 +11002903 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906} /* xlog_state_release_iclog */
2907
2908
2909/*
2910 * This routine will mark the current iclog in the ring as WANT_SYNC
2911 * and move the current iclog pointer to the next iclog in the ring.
2912 * When this routine is called from xlog_state_get_iclog_space(), the
2913 * exact size of the iclog has not yet been determined. All we know is
2914 * that every data block. We have run out of space in this log record.
2915 */
2916STATIC void
2917xlog_state_switch_iclogs(xlog_t *log,
2918 xlog_in_core_t *iclog,
2919 int eventual_size)
2920{
2921 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2922 if (!eventual_size)
2923 eventual_size = iclog->ic_offset;
2924 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002925 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 log->l_prev_block = log->l_curr_block;
2927 log->l_prev_cycle = log->l_curr_cycle;
2928
2929 /* roll log?: ic_offset changed later */
2930 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2931
2932 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002933 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 log->l_mp->m_sb.sb_logsunit > 1) {
2935 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2936 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2937 }
2938
2939 if (log->l_curr_block >= log->l_logBBsize) {
2940 log->l_curr_cycle++;
2941 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2942 log->l_curr_cycle++;
2943 log->l_curr_block -= log->l_logBBsize;
2944 ASSERT(log->l_curr_block >= 0);
2945 }
2946 ASSERT(iclog == log->l_iclog);
2947 log->l_iclog = iclog->ic_next;
2948} /* xlog_state_switch_iclogs */
2949
2950
2951/*
2952 * Write out all data in the in-core log as of this exact moment in time.
2953 *
2954 * Data may be written to the in-core log during this call. However,
2955 * we don't guarantee this data will be written out. A change from past
2956 * implementation means this routine will *not* write out zero length LRs.
2957 *
2958 * Basically, we try and perform an intelligent scan of the in-core logs.
2959 * If we determine there is no flushable data, we just return. There is no
2960 * flushable data if:
2961 *
2962 * 1. the current iclog is active and has no data; the previous iclog
2963 * is in the active or dirty state.
2964 * 2. the current iclog is drity, and the previous iclog is in the
2965 * active or dirty state.
2966 *
David Chinner12017fa2008-08-13 16:34:31 +10002967 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 *
2969 * 1. the current iclog is not in the active nor dirty state.
2970 * 2. the current iclog dirty, and the previous iclog is not in the
2971 * active nor dirty state.
2972 * 3. the current iclog is active, and there is another thread writing
2973 * to this particular iclog.
2974 * 4. a) the current iclog is active and has no other writers
2975 * b) when we return from flushing out this iclog, it is still
2976 * not in the active nor dirty state.
2977 */
2978STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002979xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
2981 xlog_in_core_t *iclog;
2982 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002984 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 iclog = log->l_iclog;
2987 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002988 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return XFS_ERROR(EIO);
2990 }
2991
2992 /* If the head iclog is not active nor dirty, we just attach
2993 * ourselves to the head and go to sleep.
2994 */
2995 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2996 iclog->ic_state == XLOG_STATE_DIRTY) {
2997 /*
2998 * If the head is dirty or (active and empty), then
2999 * we need to look at the previous iclog. If the previous
3000 * iclog is active or dirty we are done. There is nothing
3001 * to sync out. Otherwise, we attach ourselves to the
3002 * previous iclog and go to sleep.
3003 */
3004 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11003005 (atomic_read(&iclog->ic_refcnt) == 0
3006 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 iclog = iclog->ic_prev;
3008 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
3009 iclog->ic_state == XLOG_STATE_DIRTY)
3010 goto no_sleep;
3011 else
3012 goto maybe_sleep;
3013 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003014 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 /* We are the only one with access to this
3016 * iclog. Flush it out now. There should
3017 * be a roundoff of zero to show that someone
3018 * has already taken care of the roundoff from
3019 * the previous sync.
3020 */
David Chinner155cc6b2008-03-06 13:44:14 +11003021 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003022 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003024 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 if (xlog_state_release_iclog(log, iclog))
3027 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003028 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003029 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003030 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 iclog->ic_state != XLOG_STATE_DIRTY)
3032 goto maybe_sleep;
3033 else
3034 goto no_sleep;
3035 } else {
3036 /* Someone else is writing to this iclog.
3037 * Use its call to flush out the data. However,
3038 * the other thread may not force out this LR,
3039 * so we mark it WANT_SYNC.
3040 */
3041 xlog_state_switch_iclogs(log, iclog, 0);
3042 goto maybe_sleep;
3043 }
3044 }
3045 }
3046
3047 /* By the time we come around again, the iclog could've been filled
3048 * which would give it another lsn. If we have a new lsn, just
3049 * return because the relevant data has been flushed.
3050 */
3051maybe_sleep:
3052 if (flags & XFS_LOG_SYNC) {
3053 /*
3054 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003055 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 * Then we check again after waking up, in case our
3057 * sleep was disturbed by a bad news.
3058 */
3059 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003060 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return XFS_ERROR(EIO);
3062 }
3063 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003064 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 /*
3066 * No need to grab the log lock here since we're
3067 * only deciding whether or not to return EIO
3068 * and the memory read should be atomic.
3069 */
3070 if (iclog->ic_state & XLOG_STATE_IOERROR)
3071 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003072 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 } else {
3075
3076no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003077 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 return 0;
3080} /* xlog_state_sync_all */
3081
3082
3083/*
3084 * Used by code which implements synchronous log forces.
3085 *
3086 * Find in-core log with lsn.
3087 * If it is in the DIRTY state, just return.
3088 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3089 * state and go to sleep or return.
3090 * If it is in any other state, go to sleep or return.
3091 *
3092 * If filesystem activity goes to zero, the iclog will get flushed only by
3093 * bdflush().
3094 */
David Chinnera8272ce2007-11-23 16:28:09 +11003095STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096xlog_state_sync(xlog_t *log,
3097 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003098 uint flags,
3099 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 xlog_in_core_t *iclog;
3102 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003105 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 iclog = log->l_iclog;
3107
3108 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003109 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return XFS_ERROR(EIO);
3111 }
3112
3113 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003114 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3115 iclog = iclog->ic_next;
3116 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 }
3118
3119 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003120 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 0;
3122 }
3123
3124 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3125 /*
3126 * We sleep here if we haven't already slept (e.g.
3127 * this is the first time we've looked at the correct
3128 * iclog buf) and the buffer before us is going to
3129 * be sync'ed. The reason for this is that if we
3130 * are doing sync transactions here, by waiting for
3131 * the previous I/O to complete, we can allow a few
3132 * more transactions into this iclog before we close
3133 * it down.
3134 *
3135 * Otherwise, we mark the buffer WANT_SYNC, and bump
3136 * up the refcnt so we can release the log (which drops
3137 * the ref count). The state switch keeps new transaction
3138 * commits from using this buffer. When the current commits
3139 * finish writing into the buffer, the refcount will drop to
3140 * zero and the buffer will go out then.
3141 */
3142 if (!already_slept &&
3143 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3144 XLOG_STATE_SYNCING))) {
3145 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3146 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003147 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003149 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 already_slept = 1;
3151 goto try_again;
3152 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003153 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003155 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (xlog_state_release_iclog(log, iclog))
3157 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003158 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003159 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
3161 }
3162
3163 if ((flags & XFS_LOG_SYNC) && /* sleep */
3164 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3165
3166 /*
David Chinner12017fa2008-08-13 16:34:31 +10003167 * Don't wait on completion if we know that we've
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 * gotten a log write error.
3169 */
3170 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003171 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return XFS_ERROR(EIO);
3173 }
3174 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003175 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 /*
3177 * No need to grab the log lock here since we're
3178 * only deciding whether or not to return EIO
3179 * and the memory read should be atomic.
3180 */
3181 if (iclog->ic_state & XLOG_STATE_IOERROR)
3182 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003183 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003185 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187 return 0;
3188
3189 } while (iclog != log->l_iclog);
3190
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003191 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193} /* xlog_state_sync */
3194
3195
3196/*
3197 * Called when we want to mark the current iclog as being ready to sync to
3198 * disk.
3199 */
David Chinnera8272ce2007-11-23 16:28:09 +11003200STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3202{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003203 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3206 xlog_state_switch_iclogs(log, iclog, 0);
3207 } else {
3208 ASSERT(iclog->ic_state &
3209 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3210 }
3211
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003212 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213} /* xlog_state_want_sync */
3214
3215
3216
3217/*****************************************************************************
3218 *
3219 * TICKET functions
3220 *
3221 *****************************************************************************
3222 */
3223
3224/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003225 * Free a used ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 */
3227STATIC void
3228xlog_ticket_put(xlog_t *log,
3229 xlog_ticket_t *ticket)
3230{
David Chinner12017fa2008-08-13 16:34:31 +10003231 sv_destroy(&ticket->t_wait);
David Chinnereb01c9c2008-04-10 12:18:46 +10003232 kmem_zone_free(xfs_log_ticket_zone, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233} /* xlog_ticket_put */
3234
3235
3236/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003237 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 */
David Chinnera8272ce2007-11-23 16:28:09 +11003239STATIC xlog_ticket_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240xlog_ticket_get(xlog_t *log,
3241 int unit_bytes,
3242 int cnt,
3243 char client,
3244 uint xflags)
3245{
3246 xlog_ticket_t *tic;
3247 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
David Chinnereb01c9c2008-04-10 12:18:46 +10003249 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3250 if (!tic)
3251 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 /*
3254 * Permanent reservations have up to 'cnt'-1 active log operations
3255 * in the log. A unit in this case is the amount of space for one
3256 * of these log operations. Normal reservations have a cnt of 1
3257 * and their unit amount is the total amount of space required.
3258 *
3259 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003260 * which occupy space in the on-disk log.
3261 *
3262 * Normal form of a transaction is:
3263 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3264 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3265 *
3266 * We need to account for all the leadup data and trailer data
3267 * around the transaction data.
3268 * And then we need to account for the worst case in terms of using
3269 * more space.
3270 * The worst case will happen if:
3271 * - the placement of the transaction happens to be such that the
3272 * roundoff is at its maximum
3273 * - the transaction data is synced before the commit record is synced
3274 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3275 * Therefore the commit record is in its own Log Record.
3276 * This can happen as the commit record is called with its
3277 * own region to xlog_write().
3278 * This then means that in the worst case, roundoff can happen for
3279 * the commit-rec as well.
3280 * The commit-rec is smaller than padding in this scenario and so it is
3281 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 */
3283
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003284 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003286 unit_bytes += sizeof(xfs_trans_header_t);
3287
3288 /* for start-rec */
3289 unit_bytes += sizeof(xlog_op_header_t);
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 /* for LR headers */
3292 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3293 unit_bytes += log->l_iclog_hsize * num_headers;
3294
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003295 /* for commit-rec LR header - note: padding will subsume the ophdr */
3296 unit_bytes += log->l_iclog_hsize;
3297
3298 /* for split-recs - ophdrs added when data split over LRs */
3299 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3300
3301 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003302 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003303 log->l_mp->m_sb.sb_logsunit > 1) {
3304 /* log su roundoff */
3305 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3306 } else {
3307 /* BB roundoff */
3308 unit_bytes += 2*BBSIZE;
3309 }
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 tic->t_unit_res = unit_bytes;
3312 tic->t_curr_res = unit_bytes;
3313 tic->t_cnt = cnt;
3314 tic->t_ocnt = cnt;
3315 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3316 tic->t_clientid = client;
3317 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003318 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (xflags & XFS_LOG_PERM_RESERV)
3320 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003321 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Christoph Hellwig0adba532007-08-30 17:21:46 +10003323 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return tic;
3326} /* xlog_ticket_get */
3327
3328
3329/******************************************************************************
3330 *
3331 * Log debug routines
3332 *
3333 ******************************************************************************
3334 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003335#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336/*
3337 * Make sure that the destination ptr is within the valid data region of
3338 * one of the iclogs. This uses backup pointers stored in a different
3339 * part of the log in case we trash the log structure.
3340 */
3341void
3342xlog_verify_dest_ptr(xlog_t *log,
3343 __psint_t ptr)
3344{
3345 int i;
3346 int good_ptr = 0;
3347
3348 for (i=0; i < log->l_iclog_bufs; i++) {
3349 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3350 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3351 good_ptr++;
3352 }
3353 if (! good_ptr)
3354 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3355} /* xlog_verify_dest_ptr */
3356
3357STATIC void
3358xlog_verify_grant_head(xlog_t *log, int equals)
3359{
3360 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3361 if (equals)
3362 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3363 else
3364 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3365 } else {
3366 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3367 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3368 }
3369} /* xlog_verify_grant_head */
3370
3371/* check if it will fit */
3372STATIC void
3373xlog_verify_tail_lsn(xlog_t *log,
3374 xlog_in_core_t *iclog,
3375 xfs_lsn_t tail_lsn)
3376{
3377 int blocks;
3378
3379 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3380 blocks =
3381 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3382 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3383 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3384 } else {
3385 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3386
3387 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3388 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3389
3390 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3391 if (blocks < BTOBB(iclog->ic_offset) + 1)
3392 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3393 }
3394} /* xlog_verify_tail_lsn */
3395
3396/*
3397 * Perform a number of checks on the iclog before writing to disk.
3398 *
3399 * 1. Make sure the iclogs are still circular
3400 * 2. Make sure we have a good magic number
3401 * 3. Make sure we don't have magic numbers in the data
3402 * 4. Check fields of each log operation header for:
3403 * A. Valid client identifier
3404 * B. tid ptr value falls in valid ptr space (user space code)
3405 * C. Length in log record header is correct according to the
3406 * individual operation headers within record.
3407 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3408 * log, check the preceding blocks of the physical log to make sure all
3409 * the cycle numbers agree with the current cycle number.
3410 */
3411STATIC void
3412xlog_verify_iclog(xlog_t *log,
3413 xlog_in_core_t *iclog,
3414 int count,
3415 boolean_t syncing)
3416{
3417 xlog_op_header_t *ophead;
3418 xlog_in_core_t *icptr;
3419 xlog_in_core_2_t *xhdr;
3420 xfs_caddr_t ptr;
3421 xfs_caddr_t base_ptr;
3422 __psint_t field_offset;
3423 __uint8_t clientid;
3424 int len, i, j, k, op_len;
3425 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003428 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 icptr = log->l_iclog;
3430 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003431 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 xlog_panic("xlog_verify_iclog: invalid ptr");
3433 icptr = icptr->ic_next;
3434 }
3435 if (icptr != log->l_iclog)
3436 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003437 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003440 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 xlog_panic("xlog_verify_iclog: invalid magic num");
3442
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003443 ptr = (xfs_caddr_t) &iclog->ic_header;
3444 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003446 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 xlog_panic("xlog_verify_iclog: unexpected magic num");
3448 }
3449
3450 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003451 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 ptr = iclog->ic_datap;
3453 base_ptr = ptr;
3454 ophead = (xlog_op_header_t *)ptr;
3455 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3456 for (i = 0; i < len; i++) {
3457 ophead = (xlog_op_header_t *)ptr;
3458
3459 /* clientid is only 1 byte */
3460 field_offset = (__psint_t)
3461 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3462 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3463 clientid = ophead->oh_clientid;
3464 } else {
3465 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3466 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3467 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3468 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003469 clientid = xlog_get_client_id(
3470 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003472 clientid = xlog_get_client_id(
3473 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
3475 }
3476 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003477 cmn_err(CE_WARN, "xlog_verify_iclog: "
3478 "invalid clientid %d op 0x%p offset 0x%lx",
3479 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 /* check length */
3482 field_offset = (__psint_t)
3483 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3484 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003485 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 } else {
3487 idx = BTOBBT((__psint_t)&ophead->oh_len -
3488 (__psint_t)iclog->ic_datap);
3489 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3490 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3491 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003492 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003494 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
3496 }
3497 ptr += sizeof(xlog_op_header_t) + op_len;
3498 }
3499} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003503 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 */
3505STATIC int
3506xlog_state_ioerror(
3507 xlog_t *log)
3508{
3509 xlog_in_core_t *iclog, *ic;
3510
3511 iclog = log->l_iclog;
3512 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3513 /*
3514 * Mark all the incore logs IOERROR.
3515 * From now on, no log flushes will result.
3516 */
3517 ic = iclog;
3518 do {
3519 ic->ic_state = XLOG_STATE_IOERROR;
3520 ic = ic->ic_next;
3521 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
3524 /*
3525 * Return non-zero, if state transition has already happened.
3526 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003527 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
3530/*
3531 * This is called from xfs_force_shutdown, when we're forcibly
3532 * shutting down the filesystem, typically because of an IO error.
3533 * Our main objectives here are to make sure that:
3534 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3535 * parties to find out, 'atomically'.
3536 * b. those who're sleeping on log reservations, pinned objects and
3537 * other resources get woken up, and be told the bad news.
3538 * c. nothing new gets queued up after (a) and (b) are done.
3539 * d. if !logerror, flush the iclogs to disk, then seal them off
3540 * for business.
3541 */
3542int
3543xfs_log_force_umount(
3544 struct xfs_mount *mp,
3545 int logerror)
3546{
3547 xlog_ticket_t *tic;
3548 xlog_t *log;
3549 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003550 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 log = mp->m_log;
3553
3554 /*
3555 * If this happens during log recovery, don't worry about
3556 * locking; the log isn't open for business yet.
3557 */
3558 if (!log ||
3559 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3560 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3561 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
3564
3565 /*
3566 * Somebody could've already done the hard work for us.
3567 * No need to get locks for this.
3568 */
3569 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3570 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003571 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
3573 retval = 0;
3574 /*
3575 * We must hold both the GRANT lock and the LOG lock,
3576 * before we mark the filesystem SHUTDOWN and wake
3577 * everybody up to tell the bad news.
3578 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003579 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003580 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3582 XFS_BUF_DONE(mp->m_sb_bp);
3583 /*
3584 * This flag is sort of redundant because of the mount flag, but
3585 * it's good to maintain the separation between the log and the rest
3586 * of XFS.
3587 */
3588 log->l_flags |= XLOG_IO_ERROR;
3589
3590 /*
3591 * If we hit a log error, we want to mark all the iclogs IOERROR
3592 * while we're still holding the loglock.
3593 */
3594 if (logerror)
3595 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003596 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 /*
3599 * We don't want anybody waiting for log reservations
3600 * after this. That means we have to wake up everybody
3601 * queued up on reserve_headq as well as write_headq.
3602 * In addition, we make sure in xlog_{re}grant_log_space
3603 * that we don't enqueue anything once the SHUTDOWN flag
3604 * is set, and this action is protected by the GRANTLOCK.
3605 */
3606 if ((tic = log->l_reserve_headq)) {
3607 do {
David Chinner12017fa2008-08-13 16:34:31 +10003608 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 tic = tic->t_next;
3610 } while (tic != log->l_reserve_headq);
3611 }
3612
3613 if ((tic = log->l_write_headq)) {
3614 do {
David Chinner12017fa2008-08-13 16:34:31 +10003615 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 tic = tic->t_next;
3617 } while (tic != log->l_write_headq);
3618 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003619 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
3621 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3622 ASSERT(!logerror);
3623 /*
3624 * Force the incore logs to disk before shutting the
3625 * log down completely.
3626 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003627 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003628 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003630 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
3632 /*
3633 * Wake up everybody waiting on xfs_log_force.
3634 * Callback all log item committed functions as if the
3635 * log writes were completed.
3636 */
3637 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3638
3639#ifdef XFSERRORDEBUG
3640 {
3641 xlog_in_core_t *iclog;
3642
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003643 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 iclog = log->l_iclog;
3645 do {
3646 ASSERT(iclog->ic_callback == 0);
3647 iclog = iclog->ic_next;
3648 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003649 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
3651#endif
3652 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003653 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003656STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657xlog_iclogs_empty(xlog_t *log)
3658{
3659 xlog_in_core_t *iclog;
3660
3661 iclog = log->l_iclog;
3662 do {
3663 /* endianness does not matter here, zero is zero in
3664 * any language.
3665 */
3666 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 iclog = iclog->ic_next;
3669 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003670 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}