blob: f4726f702a9ea51e84ba08622c7f322dffab3a7c [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 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100103STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int unit_bytes,
105 int count,
106 char clientid,
107 uint flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100109#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
111STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
112STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
113 int count, boolean_t syncing);
114STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
115 xfs_lsn_t tail_lsn);
116#else
117#define xlog_verify_dest_ptr(a,b)
118#define xlog_verify_grant_head(a,b)
119#define xlog_verify_iclog(a,b,c,d)
120#define xlog_verify_tail_lsn(a,b,c)
121#endif
122
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000123STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#if defined(XFS_LOG_TRACE)
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000126
127#define XLOG_TRACE_LOGGRANT_SIZE 2048
128#define XLOG_TRACE_ICLOG_SIZE 256
129
130void
131xlog_trace_loggrant_alloc(xlog_t *log)
132{
133 log->l_grant_trace = ktrace_alloc(XLOG_TRACE_LOGGRANT_SIZE, KM_NOFS);
134}
135
136void
137xlog_trace_loggrant_dealloc(xlog_t *log)
138{
139 ktrace_free(log->l_grant_trace);
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142void
143xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
144{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000145 unsigned long cnts;
146
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000147 /* ticket counts are 1 byte each */
148 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 ktrace_enter(log->l_grant_trace,
151 (void *)tic,
152 (void *)log->l_reserve_headq,
153 (void *)log->l_write_headq,
154 (void *)((unsigned long)log->l_grant_reserve_cycle),
155 (void *)((unsigned long)log->l_grant_reserve_bytes),
156 (void *)((unsigned long)log->l_grant_write_cycle),
157 (void *)((unsigned long)log->l_grant_write_bytes),
158 (void *)((unsigned long)log->l_curr_cycle),
159 (void *)((unsigned long)log->l_curr_block),
160 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
161 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
162 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000163 (void *)((unsigned long)tic->t_trans_type),
164 (void *)cnts,
165 (void *)((unsigned long)tic->t_curr_res),
166 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169void
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000170xlog_trace_iclog_alloc(xlog_in_core_t *iclog)
171{
172 iclog->ic_trace = ktrace_alloc(XLOG_TRACE_ICLOG_SIZE, KM_NOFS);
173}
174
175void
176xlog_trace_iclog_dealloc(xlog_in_core_t *iclog)
177{
178 ktrace_free(iclog->ic_trace);
179}
180
181void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ktrace_enter(iclog->ic_trace,
185 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100186 (void *)((unsigned long)current_pid()),
187 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
188 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
189 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
190 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#else
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000193
194#define xlog_trace_loggrant_alloc(log)
195#define xlog_trace_loggrant_dealloc(log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#define xlog_trace_loggrant(log,tic,string)
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000197
198#define xlog_trace_iclog_alloc(iclog)
199#define xlog_trace_iclog_dealloc(iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define xlog_trace_iclog(iclog,state)
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +1000201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#endif /* XFS_LOG_TRACE */
203
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100204
205static void
206xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
207{
208 if (*qp) {
209 tic->t_next = (*qp);
210 tic->t_prev = (*qp)->t_prev;
211 (*qp)->t_prev->t_next = tic;
212 (*qp)->t_prev = tic;
213 } else {
214 tic->t_prev = tic->t_next = tic;
215 *qp = tic;
216 }
217
218 tic->t_flags |= XLOG_TIC_IN_Q;
219}
220
221static void
222xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
223{
224 if (tic == tic->t_next) {
225 *qp = NULL;
226 } else {
227 *qp = tic->t_next;
228 tic->t_next->t_prev = tic->t_prev;
229 tic->t_prev->t_next = tic->t_next;
230 }
231
232 tic->t_next = tic->t_prev = NULL;
233 tic->t_flags &= ~XLOG_TIC_IN_Q;
234}
235
236static void
237xlog_grant_sub_space(struct log *log, int bytes)
238{
239 log->l_grant_write_bytes -= bytes;
240 if (log->l_grant_write_bytes < 0) {
241 log->l_grant_write_bytes += log->l_logsize;
242 log->l_grant_write_cycle--;
243 }
244
245 log->l_grant_reserve_bytes -= bytes;
246 if ((log)->l_grant_reserve_bytes < 0) {
247 log->l_grant_reserve_bytes += log->l_logsize;
248 log->l_grant_reserve_cycle--;
249 }
250
251}
252
253static void
254xlog_grant_add_space_write(struct log *log, int bytes)
255{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000256 int tmp = log->l_logsize - log->l_grant_write_bytes;
257 if (tmp > bytes)
258 log->l_grant_write_bytes += bytes;
259 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100260 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000261 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100262 }
263}
264
265static void
266xlog_grant_add_space_reserve(struct log *log, int bytes)
267{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000268 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
269 if (tmp > bytes)
270 log->l_grant_reserve_bytes += bytes;
271 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100272 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000273 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100274 }
275}
276
277static inline void
278xlog_grant_add_space(struct log *log, int bytes)
279{
280 xlog_grant_add_space_write(log, bytes);
281 xlog_grant_add_space_reserve(log, bytes);
282}
283
Christoph Hellwig0adba532007-08-30 17:21:46 +1000284static void
285xlog_tic_reset_res(xlog_ticket_t *tic)
286{
287 tic->t_res_num = 0;
288 tic->t_res_arr_sum = 0;
289 tic->t_res_num_ophdrs = 0;
290}
291
292static void
293xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
294{
295 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
296 /* add to overflow and start again */
297 tic->t_res_o_flow += tic->t_res_arr_sum;
298 tic->t_res_num = 0;
299 tic->t_res_arr_sum = 0;
300 }
301
302 tic->t_res_arr[tic->t_res_num].r_len = len;
303 tic->t_res_arr[tic->t_res_num].r_type = type;
304 tic->t_res_arr_sum += len;
305 tic->t_res_num++;
306}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
309 * NOTES:
310 *
311 * 1. currblock field gets updated at startup and after in-core logs
312 * marked as with WANT_SYNC.
313 */
314
315/*
316 * This routine is called when a user of a log manager ticket is done with
317 * the reservation. If the ticket was ever used, then a commit record for
318 * the associated transaction is written out as a log operation header with
319 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
320 * a given ticket. If the ticket was one with a permanent reservation, then
321 * a few operations are done differently. Permanent reservation tickets by
322 * default don't release the reservation. They just commit the current
323 * transaction with the belief that the reservation is still needed. A flag
324 * must be passed in before permanent reservations are actually released.
325 * When these type of tickets are not released, they need to be set into
326 * the inited state again. By doing this, a start record will be written
327 * out when the next write occurs.
328 */
329xfs_lsn_t
330xfs_log_done(xfs_mount_t *mp,
331 xfs_log_ticket_t xtic,
332 void **iclog,
333 uint flags)
334{
335 xlog_t *log = mp->m_log;
336 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
337 xfs_lsn_t lsn = 0;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (XLOG_FORCED_SHUTDOWN(log) ||
340 /*
341 * If nothing was ever written, don't write out commit record.
342 * If we get an error, just continue and give back the log ticket.
343 */
344 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
345 (xlog_commit_record(mp, ticket,
346 (xlog_in_core_t **)iclog, &lsn)))) {
347 lsn = (xfs_lsn_t) -1;
348 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
349 flags |= XFS_LOG_REL_PERM_RESERV;
350 }
351 }
352
353
354 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
355 (flags & XFS_LOG_REL_PERM_RESERV)) {
356 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000357 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * request has been made to release a permanent reservation.
359 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000360 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100362 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000364 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000366 /* If this ticket was a permanent reservation and we aren't
367 * trying to release it, reset the inited flags; so next time
368 * we write, a start record will be written out.
369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 return lsn;
374} /* xfs_log_done */
375
376
377/*
378 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
379 * the force is done synchronously.
380 *
381 * Asynchronous forces are implemented by setting the WANT_SYNC
382 * bit in the appropriate in-core log and then returning.
383 *
David Chinner12017fa2008-08-13 16:34:31 +1000384 * Synchronous forces are implemented with a signal variable. All callers
385 * to force a given lsn to disk will wait on a the sv attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * specific in-core log. When given in-core log finally completes its
387 * write to disk, that thread will wake up all threads waiting on the
David Chinner12017fa2008-08-13 16:34:31 +1000388 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100391_xfs_log_force(
392 xfs_mount_t *mp,
393 xfs_lsn_t lsn,
394 uint flags,
395 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100397 xlog_t *log = mp->m_log;
398 int dummy;
399
400 if (!log_flushed)
401 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ASSERT(flags & XFS_LOG_FORCE);
404
405 XFS_STATS_INC(xs_log_force);
406
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100407 if (log->l_flags & XLOG_IO_ERROR)
408 return XFS_ERROR(EIO);
409 if (lsn == 0)
410 return xlog_state_sync_all(log, flags, log_flushed);
411 else
412 return xlog_state_sync(log, lsn, flags, log_flushed);
David Chinnerb911ca02008-04-10 12:24:30 +1000413} /* _xfs_log_force */
414
415/*
416 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
417 * about errors or whether the log was flushed or not. This is the normal
418 * interface to use when trying to unpin items or move the log forward.
419 */
420void
421xfs_log_force(
422 xfs_mount_t *mp,
423 xfs_lsn_t lsn,
424 uint flags)
425{
426 int error;
427 error = _xfs_log_force(mp, lsn, flags, NULL);
428 if (error) {
429 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
430 "error %d returned.", error);
431 }
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435/*
436 * Attaches a new iclog I/O completion callback routine during
437 * transaction commit. If the log is in error state, a non-zero
438 * return code is handed back and the caller is responsible for
439 * executing the callback at an appropriate time.
440 */
441int
442xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
443 void *iclog_hndl, /* iclog to hang callback off */
444 xfs_log_callback_t *cb)
445{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000447 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
David Chinner114d23a2008-04-10 12:18:39 +1000449 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
451 if (!abortflg) {
452 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
453 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
454 cb->cb_next = NULL;
455 *(iclog->ic_callback_tail) = cb;
456 iclog->ic_callback_tail = &(cb->cb_next);
457 }
David Chinner114d23a2008-04-10 12:18:39 +1000458 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return abortflg;
460} /* xfs_log_notify */
461
462int
463xfs_log_release_iclog(xfs_mount_t *mp,
464 void *iclog_hndl)
465{
466 xlog_t *log = mp->m_log;
467 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
468
469 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000470 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100471 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 return 0;
475}
476
477/*
478 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
479 * to the reservation.
480 * 2. Potentially, push buffers at tail of log to disk.
481 *
482 * Each reservation is going to reserve extra space for a log record header.
483 * When writes happen to the on-disk log, we don't subtract the length of the
484 * log record header from any reservation. By wasting space in each
485 * reservation, we prevent over allocation problems.
486 */
487int
488xfs_log_reserve(xfs_mount_t *mp,
489 int unit_bytes,
490 int cnt,
491 xfs_log_ticket_t *ticket,
492 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000493 uint flags,
494 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 xlog_t *log = mp->m_log;
497 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100498 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
501 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
502
503 if (XLOG_FORCED_SHUTDOWN(log))
504 return XFS_ERROR(EIO);
505
506 XFS_STATS_INC(xs_try_logspace);
507
508 if (*ticket != NULL) {
509 ASSERT(flags & XFS_LOG_PERM_RESERV);
510 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000511 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
513 retval = xlog_regrant_write_log_space(log, internal_ticket);
514 } else {
515 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100516 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000518 if (!internal_ticket)
519 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000520 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000522 xlog_trace_loggrant(log, internal_ticket,
523 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
524 "xfs_log_reserve: create new ticket (permanent trans)" :
525 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 xlog_grant_push_ail(mp,
527 (internal_ticket->t_unit_res *
528 internal_ticket->t_cnt));
529 retval = xlog_grant_log_space(log, internal_ticket);
530 }
531
532 return retval;
533} /* xfs_log_reserve */
534
535
536/*
537 * Mount a log filesystem
538 *
539 * mp - ubiquitous xfs mount point structure
540 * log_target - buftarg of on-disk log device
541 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
542 * num_bblocks - Number of BBSIZE blocks in on-disk log
543 *
544 * Return error or zero.
545 */
546int
David Chinner249a8c12008-02-05 12:13:32 +1100547xfs_log_mount(
548 xfs_mount_t *mp,
549 xfs_buftarg_t *log_target,
550 xfs_daddr_t blk_offset,
551 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
David Chinner249a8c12008-02-05 12:13:32 +1100553 int error;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
556 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
557 else {
558 cmn_err(CE_NOTE,
559 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
560 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000561 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinner644c3562008-11-10 16:50:24 +1100565 if (!mp->m_log) {
566 cmn_err(CE_WARN, "XFS: Log allocation failed: No memory!");
567 error = ENOMEM;
568 goto out;
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
David Chinner249a8c12008-02-05 12:13:32 +1100572 * Initialize the AIL now we have a log.
573 */
David Chinner249a8c12008-02-05 12:13:32 +1100574 error = xfs_trans_ail_init(mp);
575 if (error) {
576 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
577 goto error;
578 }
David Chinnera9c21c12008-10-30 17:39:35 +1100579 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100580
581 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * skip log recovery on a norecovery mount. pretend it all
583 * just worked.
584 */
585 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100586 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000589 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Eric Sandeen65be6052006-01-11 15:34:19 +1100591 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000594 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (error) {
596 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
David Chinner249a8c12008-02-05 12:13:32 +1100597 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 }
600
601 /* Normal transactions can now occur */
602 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
603
604 /* End mounting message in xfs_log_mount_finish */
605 return 0;
David Chinner249a8c12008-02-05 12:13:32 +1100606error:
607 xfs_log_unmount_dealloc(mp);
Dave Chinner644c3562008-11-10 16:50:24 +1100608out:
David Chinner249a8c12008-02-05 12:13:32 +1100609 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610} /* xfs_log_mount */
611
612/*
613 * Finish the recovery of the file system. This is separate from
614 * the xfs_log_mount() call, because it depends on the code in
615 * xfs_mountfs() to read in the root and real-time bitmap inodes
616 * between calling xfs_log_mount() and here.
617 *
618 * mp - ubiquitous xfs mount point structure
619 */
620int
Christoph Hellwig42490232008-08-13 16:49:32 +1000621xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 int error;
624
625 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000626 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 else {
628 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000629 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 return error;
633}
634
635/*
636 * Unmount processing for the log.
637 */
638int
639xfs_log_unmount(xfs_mount_t *mp)
640{
641 int error;
642
643 error = xfs_log_unmount_write(mp);
644 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100645 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
649 * Final log writes as part of unmount.
650 *
651 * Mark the filesystem clean as unmount happens. Note that during relocation
652 * this routine needs to be executed as part of source-bag while the
653 * deallocation must not be done until source-end.
654 */
655
656/*
657 * Unmount record used to have a string "Unmount filesystem--" in the
658 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
659 * We just write the magic number now since that particular field isn't
660 * currently architecture converted and "nUmount" is a bit foo.
661 * As far as I know, there weren't any dependencies on the old behaviour.
662 */
663
664int
665xfs_log_unmount_write(xfs_mount_t *mp)
666{
667 xlog_t *log = mp->m_log;
668 xlog_in_core_t *iclog;
669#ifdef DEBUG
670 xlog_in_core_t *first_iclog;
671#endif
672 xfs_log_iovec_t reg[1];
673 xfs_log_ticket_t tic = NULL;
674 xfs_lsn_t lsn;
675 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* the data section must be 32 bit size aligned */
678 struct {
679 __uint16_t magic;
680 __uint16_t pad1;
681 __uint32_t pad2; /* may as well make it 64 bits */
682 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Don't write out unmount record on read-only mounts.
686 * Or, if we are doing a forced umount (typically because of IO errors).
687 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000688 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690
David Chinnerb911ca02008-04-10 12:24:30 +1000691 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
692 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694#ifdef DEBUG
695 first_iclog = iclog = log->l_iclog;
696 do {
697 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
698 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
699 ASSERT(iclog->ic_offset == 0);
700 }
701 iclog = iclog->ic_next;
702 } while (iclog != first_iclog);
703#endif
704 if (! (XLOG_FORCED_SHUTDOWN(log))) {
705 reg[0].i_addr = (void*)&magic;
706 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000707 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Tim Shimmin955e47a2006-09-28 11:04:16 +1000709 error = xfs_log_reserve(mp, 600, 1, &tic,
710 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!error) {
712 /* remove inited flag */
713 ((xlog_ticket_t *)tic)->t_flags = 0;
714 error = xlog_write(mp, reg, 1, tic, &lsn,
715 NULL, XLOG_UNMOUNT_TRANS);
716 /*
717 * At this point, we're umounting anyway,
718 * so there's no point in transitioning log state
719 * to IOERROR. Just continue...
720 */
721 }
722
723 if (error) {
724 xfs_fs_cmn_err(CE_ALERT, mp,
725 "xfs_log_unmount: unmount record failed");
726 }
727
728
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000729 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100731 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100733 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000734 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000736 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
738 iclog->ic_state == XLOG_STATE_DIRTY)) {
739 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000740 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 &log->l_icloglock, s);
742 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000743 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000746 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000748 if (tic) {
749 xlog_trace_loggrant(log, tic, "unmount rec");
750 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100751 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 } else {
754 /*
755 * We're already in forced_shutdown mode, couldn't
756 * even attempt to write out the unmount transaction.
757 *
758 * Go through the motions of sync'ing and releasing
759 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000760 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * be in progress. Do this as a separate section of
762 * code so we'll know if we ever get stuck here that
763 * we're in this odd situation of trying to unmount
764 * a file system that went into forced_shutdown as
765 * the result of an unmount..
766 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000767 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100769 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100772 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000773 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000775 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
778 || iclog->ic_state == XLOG_STATE_DIRTY
779 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
780
David Chinner12017fa2008-08-13 16:34:31 +1000781 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 &log->l_icloglock, s);
783 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000784 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 }
787
David Chinner1bb7d6b2008-04-10 12:24:38 +1000788 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789} /* xfs_log_unmount_write */
790
791/*
792 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100793 *
794 * We need to stop the aild from running before we destroy
795 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
797void
798xfs_log_unmount_dealloc(xfs_mount_t *mp)
799{
David Chinner249a8c12008-02-05 12:13:32 +1100800 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000801 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
805 * Write region vectors to log. The write happens using the space reservation
806 * of the ticket (tic). It is not a requirement that all writes for a given
807 * transaction occur with one call to xfs_log_write().
808 */
809int
810xfs_log_write(xfs_mount_t * mp,
811 xfs_log_iovec_t reg[],
812 int nentries,
813 xfs_log_ticket_t tic,
814 xfs_lsn_t *start_lsn)
815{
816 int error;
817 xlog_t *log = mp->m_log;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (XLOG_FORCED_SHUTDOWN(log))
820 return XFS_ERROR(EIO);
821
822 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000823 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100825 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826} /* xfs_log_write */
827
828
829void
830xfs_log_move_tail(xfs_mount_t *mp,
831 xfs_lsn_t tail_lsn)
832{
833 xlog_ticket_t *tic;
834 xlog_t *log = mp->m_log;
835 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (XLOG_FORCED_SHUTDOWN(log))
838 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (tail_lsn == 0) {
841 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000842 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000844 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000847 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
850 * tail_lsn.
851 */
852 if (tail_lsn != 1) {
853 log->l_tail_lsn = tail_lsn;
854 }
855
856 if ((tic = log->l_write_headq)) {
857#ifdef DEBUG
858 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
859 panic("Recovery problem");
860#endif
861 cycle = log->l_grant_write_cycle;
862 bytes = log->l_grant_write_bytes;
863 free_bytes = xlog_space_left(log, cycle, bytes);
864 do {
865 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
866
867 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
868 break;
869 tail_lsn = 0;
870 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000871 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 tic = tic->t_next;
873 } while (tic != log->l_write_headq);
874 }
875 if ((tic = log->l_reserve_headq)) {
876#ifdef DEBUG
877 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
878 panic("Recovery problem");
879#endif
880 cycle = log->l_grant_reserve_cycle;
881 bytes = log->l_grant_reserve_bytes;
882 free_bytes = xlog_space_left(log, cycle, bytes);
883 do {
884 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
885 need_bytes = tic->t_unit_res*tic->t_cnt;
886 else
887 need_bytes = tic->t_unit_res;
888 if (free_bytes < need_bytes && tail_lsn != 1)
889 break;
890 tail_lsn = 0;
891 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000892 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 tic = tic->t_next;
894 } while (tic != log->l_reserve_headq);
895 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000896 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897} /* xfs_log_move_tail */
898
899/*
900 * Determine if we have a transaction that has gone to disk
901 * that needs to be covered. Log activity needs to be idle (no AIL and
902 * nothing in the iclogs). And, we need to be in the right state indicating
903 * something has gone out.
904 */
905int
906xfs_log_need_covered(xfs_mount_t *mp)
907{
David Chinner27d8d5f2008-10-30 17:38:39 +1100908 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
David Chinner92821e22007-05-24 15:26:31 +1000911 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000914 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
916 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
David Chinnera9c21c12008-10-30 17:39:35 +1100917 && !xfs_trans_ail_tail(log->l_ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 && xlog_iclogs_empty(log)) {
919 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
920 log->l_covered_state = XLOG_STATE_COVER_DONE;
921 else {
922 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
923 log->l_covered_state = XLOG_STATE_COVER_DONE2;
924 }
925 needed = 1;
926 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000927 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100928 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931/******************************************************************************
932 *
933 * local routines
934 *
935 ******************************************************************************
936 */
937
938/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
939 * The log manager must keep track of the last LR which was committed
940 * to disk. The lsn of this LR will become the new tail_lsn whenever
941 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
942 * the situation where stuff could be written into the log but nothing
943 * was ever in the AIL when asked. Eventually, we panic since the
944 * tail hits the head.
945 *
946 * We may be holding the log iclog lock upon entering this routine.
947 */
948xfs_lsn_t
949xlog_assign_tail_lsn(xfs_mount_t *mp)
950{
951 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 xlog_t *log = mp->m_log;
953
David Chinner5b00f142008-10-30 17:39:00 +1100954 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000955 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (tail_lsn != 0) {
957 log->l_tail_lsn = tail_lsn;
958 } else {
959 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
960 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000961 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 return tail_lsn;
964} /* xlog_assign_tail_lsn */
965
966
967/*
968 * Return the space in the log between the tail and the head. The head
969 * is passed in the cycle/bytes formal parms. In the special case where
970 * the reserve head has wrapped passed the tail, this calculation is no
971 * longer valid. In this case, just return 0 which means there is no space
972 * in the log. This works for all places where this function is called
973 * with the reserve head. Of course, if the write head were to ever
974 * wrap the tail, we should blow up. Rather than catch this case here,
975 * we depend on other ASSERTions in other parts of the code. XXXmiken
976 *
977 * This code also handles the case where the reservation head is behind
978 * the tail. The details of this case are described below, but the end
979 * result is that we return the size of the log as the amount of space left.
980 */
David Chinnera8272ce2007-11-23 16:28:09 +1100981STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982xlog_space_left(xlog_t *log, int cycle, int bytes)
983{
984 int free_bytes;
985 int tail_bytes;
986 int tail_cycle;
987
988 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
989 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
990 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
991 free_bytes = log->l_logsize - (bytes - tail_bytes);
992 } else if ((tail_cycle + 1) < cycle) {
993 return 0;
994 } else if (tail_cycle < cycle) {
995 ASSERT(tail_cycle == (cycle - 1));
996 free_bytes = tail_bytes - bytes;
997 } else {
998 /*
999 * The reservation head is behind the tail.
1000 * In this case we just want to return the size of the
1001 * log as the amount of space left.
1002 */
1003 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
1004 "xlog_space_left: head behind tail\n"
1005 " tail_cycle = %d, tail_bytes = %d\n"
1006 " GH cycle = %d, GH bytes = %d",
1007 tail_cycle, tail_bytes, cycle, bytes);
1008 ASSERT(0);
1009 free_bytes = log->l_logsize;
1010 }
1011 return free_bytes;
1012} /* xlog_space_left */
1013
1014
1015/*
1016 * Log function which is called when an io completes.
1017 *
1018 * The log manager needs its own routine, in order to control what
1019 * happens with the buffer after the write completes.
1020 */
1021void
1022xlog_iodone(xfs_buf_t *bp)
1023{
1024 xlog_in_core_t *iclog;
1025 xlog_t *l;
1026 int aborted;
1027
1028 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1029 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1030 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1031 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 l = iclog->ic_log;
1033
1034 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001035 * If the _XFS_BARRIER_FAILED flag was set by a lower
1036 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +10001037 * barrier I/O. Warn loudly and turn off barriers.
1038 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001039 if (bp->b_flags & _XFS_BARRIER_FAILED) {
1040 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001041 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1042 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1043 "xlog_iodone: Barriers are no longer supported"
1044 " by device. Disabling barriers\n");
1045 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1046 }
1047
1048 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * Race to shutdown the filesystem if we see an error.
1050 */
1051 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1052 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1053 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1054 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001055 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
1057 * This flag will be propagated to the trans-committed
1058 * callback routines to let them know that the log-commit
1059 * didn't succeed.
1060 */
1061 aborted = XFS_LI_ABORTED;
1062 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1063 aborted = XFS_LI_ABORTED;
1064 }
David Chinner3db296f2007-05-14 18:24:16 +10001065
1066 /* log I/O is always issued ASYNC */
1067 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001069 /*
1070 * do not reference the buffer (bp) here as we could race
1071 * with it being freed after writing the unmount record to the
1072 * log.
1073 */
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075} /* xlog_iodone */
1076
1077/*
1078 * The bdstrat callback function for log bufs. This gives us a central
1079 * place to trap bufs in case we get hit by a log I/O error and need to
1080 * shutdown. Actually, in practice, even when we didn't get a log error,
1081 * we transition the iclogs to IOERROR state *after* flushing all existing
1082 * iclogs to disk. This is because we don't want anymore new transactions to be
1083 * started or completed afterwards.
1084 */
1085STATIC int
1086xlog_bdstrat_cb(struct xfs_buf *bp)
1087{
1088 xlog_in_core_t *iclog;
1089
1090 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1091
1092 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1093 /* note for irix bstrat will need struct bdevsw passed
1094 * Fix the following macro if the code ever is merged
1095 */
1096 XFS_bdstrat(bp);
1097 return 0;
1098 }
1099
1100 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1101 XFS_BUF_ERROR(bp, EIO);
1102 XFS_BUF_STALE(bp);
1103 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001104 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106
1107}
1108
1109/*
1110 * Return size of each in-core log record buffer.
1111 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001112 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
1114 * If the filesystem blocksize is too large, we may need to choose a
1115 * larger size since the directory code currently logs entire blocks.
1116 */
1117
1118STATIC void
1119xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1120 xlog_t *log)
1121{
1122 int size;
1123 int xhdrs;
1124
Eric Sandeen1cb51252007-08-16 16:24:43 +10001125 if (mp->m_logbufs <= 0)
1126 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1127 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001128 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /*
1131 * Buffer size passed in from mount system call.
1132 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001133 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 size = log->l_iclog_size = mp->m_logbsize;
1135 log->l_iclog_size_log = 0;
1136 while (size != 1) {
1137 log->l_iclog_size_log++;
1138 size >>= 1;
1139 }
1140
Eric Sandeen62118702008-03-06 13:44:28 +11001141 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /* # headers = size / 32K
1143 * one header holds cycles from 32K of data
1144 */
1145
1146 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1147 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1148 xhdrs++;
1149 log->l_iclog_hsize = xhdrs << BBSHIFT;
1150 log->l_iclog_heads = xhdrs;
1151 } else {
1152 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1153 log->l_iclog_hsize = BBSIZE;
1154 log->l_iclog_heads = 1;
1155 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001156 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
Eric Sandeen1cb51252007-08-16 16:24:43 +10001159 /* All machines use 32KB buffers by default. */
1160 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1161 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /* the default log size is 16k or 32k which is one header sector */
1164 log->l_iclog_hsize = BBSIZE;
1165 log->l_iclog_heads = 1;
1166
1167 /*
1168 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1169 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1170 */
1171 if (mp->m_sb.sb_blocksize >= 16*1024) {
1172 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1173 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001174 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 switch (mp->m_sb.sb_blocksize) {
1176 case 16*1024: /* 16 KB */
1177 log->l_iclog_bufs = 3;
1178 break;
1179 case 32*1024: /* 32 KB */
1180 log->l_iclog_bufs = 4;
1181 break;
1182 case 64*1024: /* 64 KB */
1183 log->l_iclog_bufs = 8;
1184 break;
1185 default:
1186 xlog_panic("XFS: Invalid blocksize");
1187 break;
1188 }
1189 }
1190 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001191
1192done: /* are we being asked to make the sizes selected above visible? */
1193 if (mp->m_logbufs == 0)
1194 mp->m_logbufs = log->l_iclog_bufs;
1195 if (mp->m_logbsize == 0)
1196 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197} /* xlog_get_iclog_buffer_size */
1198
1199
1200/*
1201 * This routine initializes some of the log structure for a given mount point.
1202 * Its primary purpose is to fill in enough, so recovery can occur. However,
1203 * some other stuff may be filled in too.
1204 */
1205STATIC xlog_t *
1206xlog_alloc_log(xfs_mount_t *mp,
1207 xfs_buftarg_t *log_target,
1208 xfs_daddr_t blk_offset,
1209 int num_bblks)
1210{
1211 xlog_t *log;
1212 xlog_rec_header_t *head;
1213 xlog_in_core_t **iclogp;
1214 xlog_in_core_t *iclog, *prev_iclog=NULL;
1215 xfs_buf_t *bp;
1216 int i;
1217 int iclogsize;
1218
Dave Chinner644c3562008-11-10 16:50:24 +11001219 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1220 if (!log)
1221 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 log->l_mp = mp;
1224 log->l_targ = log_target;
1225 log->l_logsize = BBTOB(num_bblks);
1226 log->l_logBBstart = blk_offset;
1227 log->l_logBBsize = num_bblks;
1228 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1229 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1230
1231 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001232 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1234 log->l_last_sync_lsn = log->l_tail_lsn;
1235 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1236 log->l_grant_reserve_cycle = 1;
1237 log->l_grant_write_cycle = 1;
1238
Eric Sandeen62118702008-03-06 13:44:28 +11001239 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1241 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1242 /* for larger sector sizes, must have v2 or external log */
1243 ASSERT(log->l_sectbb_log == 0 ||
1244 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001245 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1247 }
1248 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1249
1250 xlog_get_iclog_buffer_size(mp, log);
1251
1252 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001253 if (!bp)
1254 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1256 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1257 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1258 ASSERT(XFS_BUF_ISBUSY(bp));
1259 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1260 log->l_xbuf = bp;
1261
Eric Sandeen007c61c2007-10-11 17:43:56 +10001262 spin_lock_init(&log->l_icloglock);
1263 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001264 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001266 xlog_trace_loggrant_alloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1268 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1269
1270 iclogp = &log->l_iclog;
1271 /*
1272 * The amount of memory to allocate for the iclog structure is
1273 * rather funky due to the way the structure is defined. It is
1274 * done this way so that we can use different sizes for machines
1275 * with different amounts of memory. See the definition of
1276 * xlog_in_core_t in xfs_log_priv.h for details.
1277 */
1278 iclogsize = log->l_iclog_size;
1279 ASSERT(log->l_iclog_size >= 4096);
1280 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001281 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1282 if (!*iclogp)
1283 goto out_free_iclog;
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 iclog->ic_prev = prev_iclog;
1287 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001288
1289 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001290 if (!bp)
1291 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001292 if (!XFS_BUF_CPSEMA(bp))
1293 ASSERT(0);
1294 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1295 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1296 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1297 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001298 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001299#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 head = &iclog->ic_header;
1303 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001304 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1305 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001306 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001307 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001309 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1313 iclog->ic_state = XLOG_STATE_ACTIVE;
1314 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001315 atomic_set(&iclog->ic_refcnt, 0);
1316 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001318 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1321 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001322 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1323 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001325 xlog_trace_iclog_alloc(iclog);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 iclogp = &iclog->ic_next;
1328 }
1329 *iclogp = log->l_iclog; /* complete ring */
1330 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1331
1332 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001333
1334out_free_iclog:
1335 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1336 prev_iclog = iclog->ic_next;
1337 if (iclog->ic_bp) {
1338 sv_destroy(&iclog->ic_force_wait);
1339 sv_destroy(&iclog->ic_write_wait);
1340 xfs_buf_free(iclog->ic_bp);
1341 xlog_trace_iclog_dealloc(iclog);
1342 }
1343 kmem_free(iclog);
1344 }
1345 spinlock_destroy(&log->l_icloglock);
1346 spinlock_destroy(&log->l_grant_lock);
1347 xlog_trace_loggrant_dealloc(log);
1348 xfs_buf_free(log->l_xbuf);
1349out_free_log:
1350 kmem_free(log);
1351 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352} /* xlog_alloc_log */
1353
1354
1355/*
1356 * Write out the commit record of a transaction associated with the given
1357 * ticket. Return the lsn of the commit record.
1358 */
1359STATIC int
1360xlog_commit_record(xfs_mount_t *mp,
1361 xlog_ticket_t *ticket,
1362 xlog_in_core_t **iclog,
1363 xfs_lsn_t *commitlsnp)
1364{
1365 int error;
1366 xfs_log_iovec_t reg[1];
1367
1368 reg[0].i_addr = NULL;
1369 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001370 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 ASSERT_ALWAYS(iclog);
1373 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1374 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001375 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001377 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378} /* xlog_commit_record */
1379
1380
1381/*
1382 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1383 * log space. This code pushes on the lsn which would supposedly free up
1384 * the 25% which we want to leave free. We may need to adopt a policy which
1385 * pushes on an lsn which is further along in the log once we reach the high
1386 * water mark. In this manner, we would be creating a low water mark.
1387 */
David Chinnera8272ce2007-11-23 16:28:09 +11001388STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389xlog_grant_push_ail(xfs_mount_t *mp,
1390 int need_bytes)
1391{
1392 xlog_t *log = mp->m_log; /* pointer to the log */
1393 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1394 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1395 int free_blocks; /* free blocks left to write to */
1396 int free_bytes; /* free bytes left to write to */
1397 int threshold_block; /* block in lsn we'd like to be at */
1398 int threshold_cycle; /* lsn cycle we'd like to be at */
1399 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1402
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001403 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 free_bytes = xlog_space_left(log,
1405 log->l_grant_reserve_cycle,
1406 log->l_grant_reserve_bytes);
1407 tail_lsn = log->l_tail_lsn;
1408 free_blocks = BTOBBT(free_bytes);
1409
1410 /*
1411 * Set the threshold for the minimum number of free blocks in the
1412 * log to the maximum of what the caller needs, one quarter of the
1413 * log, and 256 blocks.
1414 */
1415 free_threshold = BTOBB(need_bytes);
1416 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1417 free_threshold = MAX(free_threshold, 256);
1418 if (free_blocks < free_threshold) {
1419 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1420 threshold_cycle = CYCLE_LSN(tail_lsn);
1421 if (threshold_block >= log->l_logBBsize) {
1422 threshold_block -= log->l_logBBsize;
1423 threshold_cycle += 1;
1424 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001425 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /* Don't pass in an lsn greater than the lsn of the last
1428 * log record known to be on disk.
1429 */
1430 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1431 threshold_lsn = log->l_last_sync_lsn;
1432 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001433 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /*
1436 * Get the transaction layer to kick the dirty buffers out to
1437 * disk asynchronously. No point in trying to do this if
1438 * the filesystem is shutting down.
1439 */
1440 if (threshold_lsn &&
1441 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001442 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443} /* xlog_grant_push_ail */
1444
1445
1446/*
1447 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1448 * fashion. Previously, we should have moved the current iclog
1449 * ptr in the log to point to the next available iclog. This allows further
1450 * write to continue while this code syncs out an iclog ready to go.
1451 * Before an in-core log can be written out, the data section must be scanned
1452 * to save away the 1st word of each BBSIZE block into the header. We replace
1453 * it with the current cycle count. Each BBSIZE block is tagged with the
1454 * cycle count because there in an implicit assumption that drives will
1455 * guarantee that entire 512 byte blocks get written at once. In other words,
1456 * we can't have part of a 512 byte block written and part not written. By
1457 * tagging each block, we will know which blocks are valid when recovering
1458 * after an unclean shutdown.
1459 *
1460 * This routine is single threaded on the iclog. No other thread can be in
1461 * this routine with the same iclog. Changing contents of iclog can there-
1462 * fore be done without grabbing the state machine lock. Updating the global
1463 * log will require grabbing the lock though.
1464 *
1465 * The entire log manager uses a logical block numbering scheme. Only
1466 * log_sync (and then only bwrite()) know about the fact that the log may
1467 * not start with block zero on a given device. The log block start offset
1468 * is added immediately before calling bwrite().
1469 */
1470
David Chinnera8272ce2007-11-23 16:28:09 +11001471STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472xlog_sync(xlog_t *log,
1473 xlog_in_core_t *iclog)
1474{
1475 xfs_caddr_t dptr; /* pointer to byte sized element */
1476 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001477 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 uint count; /* byte count of bwrite */
1479 uint count_init; /* initial count before roundup */
1480 int roundoff; /* roundoff to BB or stripe */
1481 int split = 0; /* split write into two regions */
1482 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001483 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001486 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /* Add for LR header */
1489 count_init = log->l_iclog_hsize + iclog->ic_offset;
1490
1491 /* Round out the log write size */
1492 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1493 /* we have a v2 stripe unit to use */
1494 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1495 } else {
1496 count = BBTOB(BTOBB(count_init));
1497 }
1498 roundoff = count - count_init;
1499 ASSERT(roundoff >= 0);
1500 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1501 roundoff < log->l_mp->m_sb.sb_logsunit)
1502 ||
1503 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1504 roundoff < BBTOB(1)));
1505
1506 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001507 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001508 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001509 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 /* put cycle number in every block */
1512 xlog_pack_data(log, iclog, roundoff);
1513
1514 /* real byte length */
1515 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001516 iclog->ic_header.h_len =
1517 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001519 iclog->ic_header.h_len =
1520 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Nathan Scottf5faad72006-07-28 17:04:44 +10001523 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1525 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001526 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1529
1530 /* Do we need to split this write into 2 parts? */
1531 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1532 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1533 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1534 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1535 } else {
1536 iclog->ic_bwritecnt = 1;
1537 }
David Chinner511105b2007-05-24 15:21:57 +10001538 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001540 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 XFS_BUF_BUSY(bp);
1542 XFS_BUF_ASYNC(bp);
1543 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001544 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001545 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001547 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001548 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1551 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1552
1553 xlog_verify_iclog(log, iclog, count, B_TRUE);
1554
1555 /* account for log which doesn't start at block #0 */
1556 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1557 /*
1558 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1559 * is shutting down.
1560 */
1561 XFS_BUF_WRITE(bp);
1562
1563 if ((error = XFS_bwrite(bp))) {
1564 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1565 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001566 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001569 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1571 (unsigned long)1);
1572 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1573 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1574 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1575 (__psint_t)count), split);
1576 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001577 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 XFS_BUF_BUSY(bp);
1579 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001580 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1581 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 dptr = XFS_BUF_PTR(bp);
1583 /*
1584 * Bump the cycle numbers at the start of each block
1585 * since this part of the buffer is at the start of
1586 * a new cycle. Watch out for the header magic number
1587 * case, though.
1588 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001589 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001590 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001591 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001592 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 dptr += BBSIZE;
1594 }
1595
1596 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1597 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1598
Nathan Scottc41564b2006-03-29 08:55:14 +10001599 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1601 XFS_BUF_WRITE(bp);
1602 if ((error = XFS_bwrite(bp))) {
1603 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1604 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001605 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609} /* xlog_sync */
1610
1611
1612/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001613 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
David Chinnera8272ce2007-11-23 16:28:09 +11001615STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001616xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int i;
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 iclog = log->l_iclog;
1622 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001623 sv_destroy(&iclog->ic_force_wait);
1624 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 xfs_buf_free(iclog->ic_bp);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001626 xlog_trace_iclog_dealloc(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001628 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 iclog = next_iclog;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 spinlock_destroy(&log->l_icloglock);
1632 spinlock_destroy(&log->l_grant_lock);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 xfs_buf_free(log->l_xbuf);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001635 xlog_trace_loggrant_dealloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001637 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001638} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640/*
1641 * Update counters atomically now that memcpy is done.
1642 */
1643/* ARGSUSED */
1644static inline void
1645xlog_state_finish_copy(xlog_t *log,
1646 xlog_in_core_t *iclog,
1647 int record_cnt,
1648 int copy_bytes)
1649{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001650 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001652 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 iclog->ic_offset += copy_bytes;
1654
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001655 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656} /* xlog_state_finish_copy */
1657
1658
1659
1660
1661/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001662 * print out info relating to regions written which consume
1663 * the reservation
1664 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001665STATIC void
1666xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1667{
1668 uint i;
1669 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1670
1671 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1672 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1673 "bformat",
1674 "bchunk",
1675 "efi_format",
1676 "efd_format",
1677 "iformat",
1678 "icore",
1679 "iext",
1680 "ibroot",
1681 "ilocal",
1682 "iattr_ext",
1683 "iattr_broot",
1684 "iattr_local",
1685 "qformat",
1686 "dquot",
1687 "quotaoff",
1688 "LR header",
1689 "unmount",
1690 "commit",
1691 "trans header"
1692 };
1693 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1694 "SETATTR_NOT_SIZE",
1695 "SETATTR_SIZE",
1696 "INACTIVE",
1697 "CREATE",
1698 "CREATE_TRUNC",
1699 "TRUNCATE_FILE",
1700 "REMOVE",
1701 "LINK",
1702 "RENAME",
1703 "MKDIR",
1704 "RMDIR",
1705 "SYMLINK",
1706 "SET_DMATTRS",
1707 "GROWFS",
1708 "STRAT_WRITE",
1709 "DIOSTRAT",
1710 "WRITE_SYNC",
1711 "WRITEID",
1712 "ADDAFORK",
1713 "ATTRINVAL",
1714 "ATRUNCATE",
1715 "ATTR_SET",
1716 "ATTR_RM",
1717 "ATTR_FLAG",
1718 "CLEAR_AGI_BUCKET",
1719 "QM_SBCHANGE",
1720 "DUMMY1",
1721 "DUMMY2",
1722 "QM_QUOTAOFF",
1723 "QM_DQALLOC",
1724 "QM_SETQLIM",
1725 "QM_DQCLUSTER",
1726 "QM_QINOCREATE",
1727 "QM_QUOTAOFF_END",
1728 "SB_UNIT",
1729 "FSYNC_TS",
1730 "GROWFSRT_ALLOC",
1731 "GROWFSRT_ZERO",
1732 "GROWFSRT_FREE",
1733 "SWAPEXT"
1734 };
1735
1736 xfs_fs_cmn_err(CE_WARN, mp,
1737 "xfs_log_write: reservation summary:\n"
1738 " trans type = %s (%u)\n"
1739 " unit res = %d bytes\n"
1740 " current res = %d bytes\n"
1741 " total reg = %u bytes (o/flow = %u bytes)\n"
1742 " ophdrs = %u (ophdr space = %u bytes)\n"
1743 " ophdr + reg = %u bytes\n"
1744 " num regions = %u\n",
1745 ((ticket->t_trans_type <= 0 ||
1746 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1747 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1748 ticket->t_trans_type,
1749 ticket->t_unit_res,
1750 ticket->t_curr_res,
1751 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1752 ticket->t_res_num_ophdrs, ophdr_spc,
1753 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001754 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001755 ticket->t_res_num);
1756
1757 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001758 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001759 cmn_err(CE_WARN,
1760 "region[%u]: %s - %u bytes\n",
1761 i,
1762 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1763 "bad-rtype" : res_type_str[r_type-1]),
1764 ticket->t_res_arr[i].r_len);
1765 }
1766}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001767
1768/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * Write some region out to in-core log
1770 *
1771 * This will be called when writing externally provided regions or when
1772 * writing out a commit record for a given transaction.
1773 *
1774 * General algorithm:
1775 * 1. Find total length of this write. This may include adding to the
1776 * lengths passed in.
1777 * 2. Check whether we violate the tickets reservation.
1778 * 3. While writing to this iclog
1779 * A. Reserve as much space in this iclog as can get
1780 * B. If this is first write, save away start lsn
1781 * C. While writing this region:
1782 * 1. If first write of transaction, write start record
1783 * 2. Write log operation header (header per region)
1784 * 3. Find out if we can fit entire region into this iclog
1785 * 4. Potentially, verify destination memcpy ptr
1786 * 5. Memcpy (partial) region
1787 * 6. If partial copy, release iclog; otherwise, continue
1788 * copying more regions into current iclog
1789 * 4. Mark want sync bit (in simulation mode)
1790 * 5. Release iclog for potential flush to on-disk log.
1791 *
1792 * ERRORS:
1793 * 1. Panic if reservation is overrun. This should never happen since
1794 * reservation amounts are generated internal to the filesystem.
1795 * NOTES:
1796 * 1. Tickets are single threaded data structures.
1797 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1798 * syncing routine. When a single log_write region needs to span
1799 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1800 * on all log operation writes which don't contain the end of the
1801 * region. The XLOG_END_TRANS bit is used for the in-core log
1802 * operation which contains the end of the continued log_write region.
1803 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1804 * we don't really know exactly how much space will be used. As a result,
1805 * we don't update ic_offset until the end when we know exactly how many
1806 * bytes have been written out.
1807 */
David Chinnera8272ce2007-11-23 16:28:09 +11001808STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809xlog_write(xfs_mount_t * mp,
1810 xfs_log_iovec_t reg[],
1811 int nentries,
1812 xfs_log_ticket_t tic,
1813 xfs_lsn_t *start_lsn,
1814 xlog_in_core_t **commit_iclog,
1815 uint flags)
1816{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001817 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001819 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 __psint_t ptr; /* copy address into data region */
1822 int len; /* # xlog_write() bytes 2 still copy */
1823 int index; /* region index currently copying */
1824 int log_offset; /* offset (from 0) into data region */
1825 int start_rec_copy; /* # bytes to copy for start record */
1826 int partial_copy; /* did we split a region? */
1827 int partial_copy_len;/* # bytes copied if split region */
1828 int need_copy; /* # bytes need to memcpy this region */
1829 int copy_len; /* # bytes actually memcpy'ing */
1830 int copy_off; /* # bytes from entry start */
1831 int contwr; /* continued write of in-core log? */
1832 int error;
1833 int record_cnt = 0, data_cnt = 0;
1834
1835 partial_copy_len = partial_copy = 0;
1836
1837 /* Calculate potential maximum space. Each region gets its own
1838 * xlog_op_header_t and may need to be double word aligned.
1839 */
1840 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001841 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001843 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 for (index = 0; index < nentries; index++) {
1847 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001848 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001850 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852 contwr = *start_lsn = 0;
1853
1854 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001855 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856#ifdef DEBUG
1857 xlog_panic(
1858 "xfs_log_write: reservation ran out. Need to up reservation");
1859#else
1860 /* Customer configurable panic */
1861 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1862 "xfs_log_write: reservation ran out. Need to up reservation");
1863 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001864 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865#endif
1866 } else
1867 ticket->t_curr_res -= len;
1868
1869 for (index = 0; index < nentries; ) {
1870 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1871 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001872 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 ASSERT(log_offset <= iclog->ic_size - 1);
1875 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1876
1877 /* start_lsn is the first lsn written to. That's all we need. */
1878 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001879 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 /* This loop writes out as many regions as can fit in the amount
1882 * of space which was allocated by xlog_state_get_iclog_space().
1883 */
1884 while (index < nentries) {
1885 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1886 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1887 start_rec_copy = 0;
1888
1889 /* If first write for transaction, insert start record.
1890 * We can't be trying to commit if we are inited. We can't
1891 * have any "partial_copy" if we are inited.
1892 */
1893 if (ticket->t_flags & XLOG_TIC_INITED) {
1894 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001895 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 logop_head->oh_clientid = ticket->t_clientid;
1897 logop_head->oh_len = 0;
1898 logop_head->oh_flags = XLOG_START_TRANS;
1899 logop_head->oh_res2 = 0;
1900 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1901 record_cnt++;
1902
1903 start_rec_copy = sizeof(xlog_op_header_t);
1904 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1905 }
1906
1907 /* Copy log operation header directly into data section */
1908 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001909 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 logop_head->oh_clientid = ticket->t_clientid;
1911 logop_head->oh_res2 = 0;
1912
1913 /* header copied directly */
1914 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1915
1916 /* are we copying a commit or unmount record? */
1917 logop_head->oh_flags = flags;
1918
1919 /*
1920 * We've seen logs corrupted with bad transaction client
1921 * ids. This makes sure that XFS doesn't generate them on.
1922 * Turn this into an EIO and shut down the filesystem.
1923 */
1924 switch (logop_head->oh_clientid) {
1925 case XFS_TRANSACTION:
1926 case XFS_VOLUME:
1927 case XFS_LOG:
1928 break;
1929 default:
1930 xfs_fs_cmn_err(CE_WARN, mp,
1931 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1932 logop_head->oh_clientid, tic);
1933 return XFS_ERROR(EIO);
1934 }
1935
1936 /* Partial write last time? => (partial_copy != 0)
1937 * need_copy is the amount we'd like to copy if everything could
1938 * fit in the current memcpy.
1939 */
1940 need_copy = reg[index].i_len - partial_copy_len;
1941
1942 copy_off = partial_copy_len;
1943 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001944 copy_len = need_copy;
1945 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (partial_copy)
1947 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1948 partial_copy_len = partial_copy = 0;
1949 } else { /* partial write */
1950 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001951 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1953 if (partial_copy)
1954 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1955 partial_copy_len += copy_len;
1956 partial_copy++;
1957 len += sizeof(xlog_op_header_t); /* from splitting of region */
1958 /* account for new log op header */
1959 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001960 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962 xlog_verify_dest_ptr(log, ptr);
1963
1964 /* copy region */
1965 ASSERT(copy_len >= 0);
1966 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1967 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1968
1969 /* make copy_len total bytes copied, including headers */
1970 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1971 record_cnt++;
1972 data_cnt += contwr ? copy_len : 0;
1973 if (partial_copy) { /* copied partial region */
1974 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1975 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1976 record_cnt = data_cnt = 0;
1977 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001978 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break; /* don't increment index */
1980 } else { /* copied entire region */
1981 index++;
1982 partial_copy_len = partial_copy = 0;
1983
1984 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1985 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1986 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001987 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001989 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (commit_iclog) {
1991 ASSERT(flags & XLOG_COMMIT_TRANS);
1992 *commit_iclog = iclog;
1993 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001994 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (index == nentries)
1996 return 0; /* we are done */
1997 else
1998 break;
1999 }
2000 } /* if (partial_copy) */
2001 } /* while (index < nentries) */
2002 } /* for (index = 0; index < nentries; ) */
2003 ASSERT(len == 0);
2004
2005 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2006 if (commit_iclog) {
2007 ASSERT(flags & XLOG_COMMIT_TRANS);
2008 *commit_iclog = iclog;
2009 return 0;
2010 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002011 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012} /* xlog_write */
2013
2014
2015/*****************************************************************************
2016 *
2017 * State Machine functions
2018 *
2019 *****************************************************************************
2020 */
2021
2022/* Clean iclogs starting from the head. This ordering must be
2023 * maintained, so an iclog doesn't become ACTIVE beyond one that
2024 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002025 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 * iclog is trying to sync to disk.
2027 *
2028 * State Change: DIRTY -> ACTIVE
2029 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002030STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031xlog_state_clean_log(xlog_t *log)
2032{
2033 xlog_in_core_t *iclog;
2034 int changed = 0;
2035
2036 iclog = log->l_iclog;
2037 do {
2038 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2039 iclog->ic_state = XLOG_STATE_ACTIVE;
2040 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002041 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * If the number of ops in this iclog indicate it just
2044 * contains the dummy transaction, we can
2045 * change state into IDLE (the second time around).
2046 * Otherwise we should change the state into
2047 * NEED a dummy.
2048 * We don't need to cover the dummy.
2049 */
2050 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002051 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2052 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 changed = 1;
2054 } else {
2055 /*
2056 * We have two dirty iclogs so start over
2057 * This could also be num of ops indicates
2058 * this is not the dummy going out.
2059 */
2060 changed = 2;
2061 }
2062 iclog->ic_header.h_num_logops = 0;
2063 memset(iclog->ic_header.h_cycle_data, 0,
2064 sizeof(iclog->ic_header.h_cycle_data));
2065 iclog->ic_header.h_lsn = 0;
2066 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2067 /* do nothing */;
2068 else
2069 break; /* stop cleaning */
2070 iclog = iclog->ic_next;
2071 } while (iclog != log->l_iclog);
2072
2073 /* log is locked when we are called */
2074 /*
2075 * Change state for the dummy log recording.
2076 * We usually go to NEED. But we go to NEED2 if the changed indicates
2077 * we are done writing the dummy record.
2078 * If we are done with the second dummy recored (DONE2), then
2079 * we go to IDLE.
2080 */
2081 if (changed) {
2082 switch (log->l_covered_state) {
2083 case XLOG_STATE_COVER_IDLE:
2084 case XLOG_STATE_COVER_NEED:
2085 case XLOG_STATE_COVER_NEED2:
2086 log->l_covered_state = XLOG_STATE_COVER_NEED;
2087 break;
2088
2089 case XLOG_STATE_COVER_DONE:
2090 if (changed == 1)
2091 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2092 else
2093 log->l_covered_state = XLOG_STATE_COVER_NEED;
2094 break;
2095
2096 case XLOG_STATE_COVER_DONE2:
2097 if (changed == 1)
2098 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2099 else
2100 log->l_covered_state = XLOG_STATE_COVER_NEED;
2101 break;
2102
2103 default:
2104 ASSERT(0);
2105 }
2106 }
2107} /* xlog_state_clean_log */
2108
2109STATIC xfs_lsn_t
2110xlog_get_lowest_lsn(
2111 xlog_t *log)
2112{
2113 xlog_in_core_t *lsn_log;
2114 xfs_lsn_t lowest_lsn, lsn;
2115
2116 lsn_log = log->l_iclog;
2117 lowest_lsn = 0;
2118 do {
2119 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002120 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if ((lsn && !lowest_lsn) ||
2122 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2123 lowest_lsn = lsn;
2124 }
2125 }
2126 lsn_log = lsn_log->ic_next;
2127 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002128 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
2131
2132STATIC void
2133xlog_state_do_callback(
2134 xlog_t *log,
2135 int aborted,
2136 xlog_in_core_t *ciclog)
2137{
2138 xlog_in_core_t *iclog;
2139 xlog_in_core_t *first_iclog; /* used to know when we've
2140 * processed all iclogs once */
2141 xfs_log_callback_t *cb, *cb_next;
2142 int flushcnt = 0;
2143 xfs_lsn_t lowest_lsn;
2144 int ioerrors; /* counter: iclogs with errors */
2145 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2146 int funcdidcallbacks; /* flag: function did callbacks */
2147 int repeats; /* for issuing console warnings if
2148 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002149 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002151 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 first_iclog = iclog = log->l_iclog;
2153 ioerrors = 0;
2154 funcdidcallbacks = 0;
2155 repeats = 0;
2156
2157 do {
2158 /*
2159 * Scan all iclogs starting with the one pointed to by the
2160 * log. Reset this starting point each time the log is
2161 * unlocked (during callbacks).
2162 *
2163 * Keep looping through iclogs until one full pass is made
2164 * without running any callbacks.
2165 */
2166 first_iclog = log->l_iclog;
2167 iclog = log->l_iclog;
2168 loopdidcallbacks = 0;
2169 repeats++;
2170
2171 do {
2172
2173 /* skip all iclogs in the ACTIVE & DIRTY states */
2174 if (iclog->ic_state &
2175 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2176 iclog = iclog->ic_next;
2177 continue;
2178 }
2179
2180 /*
2181 * Between marking a filesystem SHUTDOWN and stopping
2182 * the log, we do flush all iclogs to disk (if there
2183 * wasn't a log I/O error). So, we do want things to
2184 * go smoothly in case of just a SHUTDOWN w/o a
2185 * LOG_IO_ERROR.
2186 */
2187 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2188 /*
2189 * Can only perform callbacks in order. Since
2190 * this iclog is not in the DONE_SYNC/
2191 * DO_CALLBACK state, we skip the rest and
2192 * just try to clean up. If we set our iclog
2193 * to DO_CALLBACK, we will not process it when
2194 * we retry since a previous iclog is in the
2195 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002196 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 */
2198 if (!(iclog->ic_state &
2199 (XLOG_STATE_DONE_SYNC |
2200 XLOG_STATE_DO_CALLBACK))) {
2201 if (ciclog && (ciclog->ic_state ==
2202 XLOG_STATE_DONE_SYNC)) {
2203 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2204 }
2205 break;
2206 }
2207 /*
2208 * We now have an iclog that is in either the
2209 * DO_CALLBACK or DONE_SYNC states. The other
2210 * states (WANT_SYNC, SYNCING, or CALLBACK were
2211 * caught by the above if and are going to
2212 * clean (i.e. we aren't doing their callbacks)
2213 * see the above if.
2214 */
2215
2216 /*
2217 * We will do one more check here to see if we
2218 * have chased our tail around.
2219 */
2220
2221 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002222 if (lowest_lsn &&
2223 XFS_LSN_CMP(lowest_lsn,
2224 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 iclog = iclog->ic_next;
2226 continue; /* Leave this iclog for
2227 * another thread */
2228 }
2229
2230 iclog->ic_state = XLOG_STATE_CALLBACK;
2231
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002232 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002235 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 * No one else can be here except us.
2237 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002238 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002239 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2240 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2241 log->l_last_sync_lsn =
2242 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002243 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002246 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 ioerrors++;
2248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
David Chinner114d23a2008-04-10 12:18:39 +10002250 /*
2251 * Keep processing entries in the callback list until
2252 * we come around and it is empty. We need to
2253 * atomically see that the list is empty and change the
2254 * state to DIRTY so that we don't miss any more
2255 * callbacks being added.
2256 */
2257 spin_lock(&iclog->ic_callback_lock);
2258 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002259 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 iclog->ic_callback_tail = &(iclog->ic_callback);
2261 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002262 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002265 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 cb_next = cb->cb_next;
2267 cb->cb_func(cb->cb_arg, aborted);
2268 }
David Chinner114d23a2008-04-10 12:18:39 +10002269 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 cb = iclog->ic_callback;
2271 }
2272
2273 loopdidcallbacks++;
2274 funcdidcallbacks++;
2275
David Chinner114d23a2008-04-10 12:18:39 +10002276 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002277 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002278 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2280 iclog->ic_state = XLOG_STATE_DIRTY;
2281
2282 /*
2283 * Transition from DIRTY to ACTIVE if applicable.
2284 * NOP if STATE_IOERROR.
2285 */
2286 xlog_state_clean_log(log);
2287
2288 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002289 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 iclog = iclog->ic_next;
2292 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002293
2294 if (repeats > 5000) {
2295 flushcnt += repeats;
2296 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002298 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002299 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 } while (!ioerrors && loopdidcallbacks);
2302
2303 /*
2304 * make one last gasp attempt to see if iclogs are being left in
2305 * limbo..
2306 */
2307#ifdef DEBUG
2308 if (funcdidcallbacks) {
2309 first_iclog = iclog = log->l_iclog;
2310 do {
2311 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2312 /*
2313 * Terminate the loop if iclogs are found in states
2314 * which will cause other threads to clean up iclogs.
2315 *
2316 * SYNCING - i/o completion will go through logs
2317 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002318 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 * IOERROR - give up hope all ye who enter here
2320 */
2321 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2322 iclog->ic_state == XLOG_STATE_SYNCING ||
2323 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2324 iclog->ic_state == XLOG_STATE_IOERROR )
2325 break;
2326 iclog = iclog->ic_next;
2327 } while (first_iclog != iclog);
2328 }
2329#endif
2330
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002331 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2332 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002333 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002334
2335 if (wake)
2336 sv_broadcast(&log->l_flush_wait);
2337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339
2340/*
2341 * Finish transitioning this iclog to the dirty state.
2342 *
2343 * Make sure that we completely execute this routine only when this is
2344 * the last call to the iclog. There is a good chance that iclog flushes,
2345 * when we reach the end of the physical log, get turned into 2 separate
2346 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2347 * routine. By using the reference count bwritecnt, we guarantee that only
2348 * the second completion goes through.
2349 *
2350 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002351 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 */
David Chinnera8272ce2007-11-23 16:28:09 +11002353STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354xlog_state_done_syncing(
2355 xlog_in_core_t *iclog,
2356 int aborted)
2357{
2358 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002360 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2363 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002364 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2366
2367
2368 /*
2369 * If we got an error, either on the first buffer, or in the case of
2370 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2371 * and none should ever be attempted to be written to disk
2372 * again.
2373 */
2374 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2375 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002376 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return;
2378 }
2379 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2380 }
2381
2382 /*
2383 * Someone could be sleeping prior to writing out the next
2384 * iclog buffer, we wake them all, one will get to do the
2385 * I/O, the others get to wait for the result.
2386 */
David Chinner12017fa2008-08-13 16:34:31 +10002387 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002388 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2390} /* xlog_state_done_syncing */
2391
2392
2393/*
2394 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002395 * sleep. We wait on the flush queue on the head iclog as that should be
2396 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2397 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 *
2399 * The in-core logs are used in a circular fashion. They are not used
2400 * out-of-order even when an iclog past the head is free.
2401 *
2402 * return:
2403 * * log_offset where xlog_write() can start writing into the in-core
2404 * log's data space.
2405 * * in-core log pointer to which xlog_write() should write.
2406 * * boolean indicating this is a continued write to an in-core log.
2407 * If this is the last write, then the in-core log's offset field
2408 * needs to be incremented, depending on the amount of data which
2409 * is copied.
2410 */
David Chinnera8272ce2007-11-23 16:28:09 +11002411STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412xlog_state_get_iclog_space(xlog_t *log,
2413 int len,
2414 xlog_in_core_t **iclogp,
2415 xlog_ticket_t *ticket,
2416 int *continued_write,
2417 int *logoffsetp)
2418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 int log_offset;
2420 xlog_rec_header_t *head;
2421 xlog_in_core_t *iclog;
2422 int error;
2423
2424restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002425 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002427 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return XFS_ERROR(EIO);
2429 }
2430
2431 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002432 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2434 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002435
2436 /* Wait for log writes to have flushed */
2437 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto restart;
2439 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 head = &iclog->ic_header;
2442
David Chinner155cc6b2008-03-06 13:44:14 +11002443 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 log_offset = iclog->ic_offset;
2445
2446 /* On the 1st write to an iclog, figure out lsn. This works
2447 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2448 * committing to. If the offset is set, that's how many blocks
2449 * must be written.
2450 */
2451 if (log_offset == 0) {
2452 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002453 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002454 log->l_iclog_hsize,
2455 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002456 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2457 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002458 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 ASSERT(log->l_curr_block >= 0);
2460 }
2461
2462 /* If there is enough room to write everything, then do it. Otherwise,
2463 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2464 * bit is on, so this will get flushed out. Don't update ic_offset
2465 * until you know exactly how many bytes get copied. Therefore, wait
2466 * until later to update ic_offset.
2467 *
2468 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2469 * can fit into remaining data section.
2470 */
2471 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2472 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2473
Dave Chinner49641f12008-07-11 17:43:55 +10002474 /*
2475 * If I'm the only one writing to this iclog, sync it to disk.
2476 * We need to do an atomic compare and decrement here to avoid
2477 * racing with concurrent atomic_dec_and_lock() calls in
2478 * xlog_state_release_iclog() when there is more than one
2479 * reference to the iclog.
2480 */
2481 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2482 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002483 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002484 error = xlog_state_release_iclog(log, iclog);
2485 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002486 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002488 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490 goto restart;
2491 }
2492
2493 /* Do we have enough room to write the full amount in the remainder
2494 * of this iclog? Or must we continue a write on the next iclog and
2495 * mark this iclog as completely taken? In the case where we switch
2496 * iclogs (to mark it taken), this particular iclog will release/sync
2497 * to disk in xlog_write().
2498 */
2499 if (len <= iclog->ic_size - iclog->ic_offset) {
2500 *continued_write = 0;
2501 iclog->ic_offset += len;
2502 } else {
2503 *continued_write = 1;
2504 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2505 }
2506 *iclogp = iclog;
2507
2508 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002509 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 *logoffsetp = log_offset;
2512 return 0;
2513} /* xlog_state_get_iclog_space */
2514
2515/*
2516 * Atomically get the log space required for a log ticket.
2517 *
2518 * Once a ticket gets put onto the reserveq, it will only return after
2519 * the needed reservation is satisfied.
2520 */
2521STATIC int
2522xlog_grant_log_space(xlog_t *log,
2523 xlog_ticket_t *tic)
2524{
2525 int free_bytes;
2526 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527#ifdef DEBUG
2528 xfs_lsn_t tail_lsn;
2529#endif
2530
2531
2532#ifdef DEBUG
2533 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2534 panic("grant Recovery problem");
2535#endif
2536
2537 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002538 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2540
2541 /* something is already sleeping; insert new transaction at end */
2542 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002543 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 xlog_trace_loggrant(log, tic,
2545 "xlog_grant_log_space: sleep 1");
2546 /*
2547 * Gotta check this before going to sleep, while we're
2548 * holding the grant lock.
2549 */
2550 if (XLOG_FORCED_SHUTDOWN(log))
2551 goto error_return;
2552
2553 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002554 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /*
2556 * If we got an error, and the filesystem is shutting down,
2557 * we'll catch it down below. So just continue...
2558 */
2559 xlog_trace_loggrant(log, tic,
2560 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002561 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2564 need_bytes = tic->t_unit_res*tic->t_ocnt;
2565 else
2566 need_bytes = tic->t_unit_res;
2567
2568redo:
2569 if (XLOG_FORCED_SHUTDOWN(log))
2570 goto error_return;
2571
2572 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2573 log->l_grant_reserve_bytes);
2574 if (free_bytes < need_bytes) {
2575 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002576 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 xlog_trace_loggrant(log, tic,
2578 "xlog_grant_log_space: sleep 2");
2579 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002580 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002583 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 goto error_return;
2585 }
2586
2587 xlog_trace_loggrant(log, tic,
2588 "xlog_grant_log_space: wake 2");
2589 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002590 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 goto redo;
2592 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002593 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002596 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597#ifdef DEBUG
2598 tail_lsn = log->l_tail_lsn;
2599 /*
2600 * Check to make sure the grant write head didn't just over lap the
2601 * tail. If the cycles are the same, we can't be overlapping.
2602 * Otherwise, make sure that the cycles differ by exactly one and
2603 * check the byte count.
2604 */
2605 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2606 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2607 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2608 }
2609#endif
2610 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2611 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002612 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return 0;
2614
2615 error_return:
2616 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002617 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2619 /*
2620 * If we are failing, make sure the ticket doesn't have any
2621 * current reservations. We don't want to add this back when
2622 * the ticket/transaction gets cancelled.
2623 */
2624 tic->t_curr_res = 0;
2625 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002626 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return XFS_ERROR(EIO);
2628} /* xlog_grant_log_space */
2629
2630
2631/*
2632 * Replenish the byte reservation required by moving the grant write head.
2633 *
2634 *
2635 */
2636STATIC int
2637xlog_regrant_write_log_space(xlog_t *log,
2638 xlog_ticket_t *tic)
2639{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 int free_bytes, need_bytes;
2641 xlog_ticket_t *ntic;
2642#ifdef DEBUG
2643 xfs_lsn_t tail_lsn;
2644#endif
2645
2646 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002647 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652#ifdef DEBUG
2653 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2654 panic("regrant Recovery problem");
2655#endif
2656
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002657 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2659
2660 if (XLOG_FORCED_SHUTDOWN(log))
2661 goto error_return;
2662
2663 /* If there are other waiters on the queue then give them a
2664 * chance at logspace before us. Wake up the first waiters,
2665 * if we do not wake up all the waiters then go to sleep waiting
2666 * for more free space, otherwise try to get some space for
2667 * this transaction.
2668 */
2669
2670 if ((ntic = log->l_write_headq)) {
2671 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2672 log->l_grant_write_bytes);
2673 do {
2674 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2675
2676 if (free_bytes < ntic->t_unit_res)
2677 break;
2678 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002679 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 ntic = ntic->t_next;
2681 } while (ntic != log->l_write_headq);
2682
2683 if (ntic != log->l_write_headq) {
2684 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002685 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 xlog_trace_loggrant(log, tic,
2688 "xlog_regrant_write_log_space: sleep 1");
2689 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002690 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 &log->l_grant_lock, s);
2692
2693 /* If we're shutting down, this tic is already
2694 * off the queue */
2695 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002696 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 goto error_return;
2698 }
2699
2700 xlog_trace_loggrant(log, tic,
2701 "xlog_regrant_write_log_space: wake 1");
2702 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002703 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
2705 }
2706
2707 need_bytes = tic->t_unit_res;
2708
2709redo:
2710 if (XLOG_FORCED_SHUTDOWN(log))
2711 goto error_return;
2712
2713 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2714 log->l_grant_write_bytes);
2715 if (free_bytes < need_bytes) {
2716 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002717 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002719 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 /* If we're shutting down, this tic is already off the queue */
2722 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002723 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 goto error_return;
2725 }
2726
2727 xlog_trace_loggrant(log, tic,
2728 "xlog_regrant_write_log_space: wake 2");
2729 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002730 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 goto redo;
2732 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002733 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002735 /* we've got enough space */
2736 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737#ifdef DEBUG
2738 tail_lsn = log->l_tail_lsn;
2739 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2740 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2741 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2742 }
2743#endif
2744
2745 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2746 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002747 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750
2751 error_return:
2752 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002753 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2755 /*
2756 * If we are failing, make sure the ticket doesn't have any
2757 * current reservations. We don't want to add this back when
2758 * the ticket/transaction gets cancelled.
2759 */
2760 tic->t_curr_res = 0;
2761 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002762 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return XFS_ERROR(EIO);
2764} /* xlog_regrant_write_log_space */
2765
2766
2767/* The first cnt-1 times through here we don't need to
2768 * move the grant write head because the permanent
2769 * reservation has reserved cnt times the unit amount.
2770 * Release part of current permanent unit reservation and
2771 * reset current reservation to be one units worth. Also
2772 * move grant reservation head forward.
2773 */
2774STATIC void
2775xlog_regrant_reserve_log_space(xlog_t *log,
2776 xlog_ticket_t *ticket)
2777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 xlog_trace_loggrant(log, ticket,
2779 "xlog_regrant_reserve_log_space: enter");
2780 if (ticket->t_cnt > 0)
2781 ticket->t_cnt--;
2782
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002783 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002784 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002786 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 xlog_trace_loggrant(log, ticket,
2788 "xlog_regrant_reserve_log_space: sub current res");
2789 xlog_verify_grant_head(log, 1);
2790
2791 /* just return if we still have some of the pre-reserved space */
2792 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002793 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return;
2795 }
2796
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002797 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 xlog_trace_loggrant(log, ticket,
2799 "xlog_regrant_reserve_log_space: exit");
2800 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002801 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002803 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804} /* xlog_regrant_reserve_log_space */
2805
2806
2807/*
2808 * Give back the space left from a reservation.
2809 *
2810 * All the information we need to make a correct determination of space left
2811 * is present. For non-permanent reservations, things are quite easy. The
2812 * count should have been decremented to zero. We only need to deal with the
2813 * space remaining in the current reservation part of the ticket. If the
2814 * ticket contains a permanent reservation, there may be left over space which
2815 * needs to be released. A count of N means that N-1 refills of the current
2816 * reservation can be done before we need to ask for more space. The first
2817 * one goes to fill up the first current reservation. Once we run out of
2818 * space, the count will stay at zero and the only space remaining will be
2819 * in the current reservation field.
2820 */
2821STATIC void
2822xlog_ungrant_log_space(xlog_t *log,
2823 xlog_ticket_t *ticket)
2824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (ticket->t_cnt > 0)
2826 ticket->t_cnt--;
2827
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002828 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2830
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002831 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2834
2835 /* If this is a permanent reservation ticket, we may be able to free
2836 * up more space based on the remaining count.
2837 */
2838 if (ticket->t_cnt > 0) {
2839 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002840 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842
2843 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2844 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002845 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 xfs_log_move_tail(log->l_mp, 1);
2847} /* xlog_ungrant_log_space */
2848
2849
2850/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 * Flush iclog to disk if this is the last reference to the given iclog and
2852 * the WANT_SYNC bit is set.
2853 *
2854 * When this function is entered, the iclog is not necessarily in the
2855 * WANT_SYNC state. It may be sitting around waiting to get filled.
2856 *
2857 *
2858 */
David Chinnera8272ce2007-11-23 16:28:09 +11002859STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002860xlog_state_release_iclog(
2861 xlog_t *log,
2862 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 int sync = 0; /* do we sync? */
2865
David Chinner155cc6b2008-03-06 13:44:14 +11002866 if (iclog->ic_state & XLOG_STATE_IOERROR)
2867 return XFS_ERROR(EIO);
2868
2869 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2870 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2871 return 0;
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002874 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return XFS_ERROR(EIO);
2876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2878 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2879
David Chinner155cc6b2008-03-06 13:44:14 +11002880 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002881 /* update tail before writing to iclog */
2882 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 sync++;
2884 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002885 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2887 /* cycle incremented when incrementing curr_block */
2888 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002889 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 /*
2892 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002893 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2895 * this iclog has consistent data, so we ignore IOERROR
2896 * flags after this point.
2897 */
David Chinnerb5893342008-03-06 13:44:06 +11002898 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901} /* xlog_state_release_iclog */
2902
2903
2904/*
2905 * This routine will mark the current iclog in the ring as WANT_SYNC
2906 * and move the current iclog pointer to the next iclog in the ring.
2907 * When this routine is called from xlog_state_get_iclog_space(), the
2908 * exact size of the iclog has not yet been determined. All we know is
2909 * that every data block. We have run out of space in this log record.
2910 */
2911STATIC void
2912xlog_state_switch_iclogs(xlog_t *log,
2913 xlog_in_core_t *iclog,
2914 int eventual_size)
2915{
2916 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2917 if (!eventual_size)
2918 eventual_size = iclog->ic_offset;
2919 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002920 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 log->l_prev_block = log->l_curr_block;
2922 log->l_prev_cycle = log->l_curr_cycle;
2923
2924 /* roll log?: ic_offset changed later */
2925 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2926
2927 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002928 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 log->l_mp->m_sb.sb_logsunit > 1) {
2930 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2931 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2932 }
2933
2934 if (log->l_curr_block >= log->l_logBBsize) {
2935 log->l_curr_cycle++;
2936 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2937 log->l_curr_cycle++;
2938 log->l_curr_block -= log->l_logBBsize;
2939 ASSERT(log->l_curr_block >= 0);
2940 }
2941 ASSERT(iclog == log->l_iclog);
2942 log->l_iclog = iclog->ic_next;
2943} /* xlog_state_switch_iclogs */
2944
2945
2946/*
2947 * Write out all data in the in-core log as of this exact moment in time.
2948 *
2949 * Data may be written to the in-core log during this call. However,
2950 * we don't guarantee this data will be written out. A change from past
2951 * implementation means this routine will *not* write out zero length LRs.
2952 *
2953 * Basically, we try and perform an intelligent scan of the in-core logs.
2954 * If we determine there is no flushable data, we just return. There is no
2955 * flushable data if:
2956 *
2957 * 1. the current iclog is active and has no data; the previous iclog
2958 * is in the active or dirty state.
2959 * 2. the current iclog is drity, and the previous iclog is in the
2960 * active or dirty state.
2961 *
David Chinner12017fa2008-08-13 16:34:31 +10002962 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 *
2964 * 1. the current iclog is not in the active nor dirty state.
2965 * 2. the current iclog dirty, and the previous iclog is not in the
2966 * active nor dirty state.
2967 * 3. the current iclog is active, and there is another thread writing
2968 * to this particular iclog.
2969 * 4. a) the current iclog is active and has no other writers
2970 * b) when we return from flushing out this iclog, it is still
2971 * not in the active nor dirty state.
2972 */
2973STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002974xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 xlog_in_core_t *iclog;
2977 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002979 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 iclog = log->l_iclog;
2982 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002983 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return XFS_ERROR(EIO);
2985 }
2986
2987 /* If the head iclog is not active nor dirty, we just attach
2988 * ourselves to the head and go to sleep.
2989 */
2990 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2991 iclog->ic_state == XLOG_STATE_DIRTY) {
2992 /*
2993 * If the head is dirty or (active and empty), then
2994 * we need to look at the previous iclog. If the previous
2995 * iclog is active or dirty we are done. There is nothing
2996 * to sync out. Otherwise, we attach ourselves to the
2997 * previous iclog and go to sleep.
2998 */
2999 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11003000 (atomic_read(&iclog->ic_refcnt) == 0
3001 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 iclog = iclog->ic_prev;
3003 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
3004 iclog->ic_state == XLOG_STATE_DIRTY)
3005 goto no_sleep;
3006 else
3007 goto maybe_sleep;
3008 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003009 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 /* We are the only one with access to this
3011 * iclog. Flush it out now. There should
3012 * be a roundoff of zero to show that someone
3013 * has already taken care of the roundoff from
3014 * the previous sync.
3015 */
David Chinner155cc6b2008-03-06 13:44:14 +11003016 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003017 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003019 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021 if (xlog_state_release_iclog(log, iclog))
3022 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003023 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003024 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003025 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 iclog->ic_state != XLOG_STATE_DIRTY)
3027 goto maybe_sleep;
3028 else
3029 goto no_sleep;
3030 } else {
3031 /* Someone else is writing to this iclog.
3032 * Use its call to flush out the data. However,
3033 * the other thread may not force out this LR,
3034 * so we mark it WANT_SYNC.
3035 */
3036 xlog_state_switch_iclogs(log, iclog, 0);
3037 goto maybe_sleep;
3038 }
3039 }
3040 }
3041
3042 /* By the time we come around again, the iclog could've been filled
3043 * which would give it another lsn. If we have a new lsn, just
3044 * return because the relevant data has been flushed.
3045 */
3046maybe_sleep:
3047 if (flags & XFS_LOG_SYNC) {
3048 /*
3049 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003050 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 * Then we check again after waking up, in case our
3052 * sleep was disturbed by a bad news.
3053 */
3054 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003055 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return XFS_ERROR(EIO);
3057 }
3058 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003059 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 /*
3061 * No need to grab the log lock here since we're
3062 * only deciding whether or not to return EIO
3063 * and the memory read should be atomic.
3064 */
3065 if (iclog->ic_state & XLOG_STATE_IOERROR)
3066 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003067 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 } else {
3070
3071no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003072 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
3074 return 0;
3075} /* xlog_state_sync_all */
3076
3077
3078/*
3079 * Used by code which implements synchronous log forces.
3080 *
3081 * Find in-core log with lsn.
3082 * If it is in the DIRTY state, just return.
3083 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3084 * state and go to sleep or return.
3085 * If it is in any other state, go to sleep or return.
3086 *
3087 * If filesystem activity goes to zero, the iclog will get flushed only by
3088 * bdflush().
3089 */
David Chinnera8272ce2007-11-23 16:28:09 +11003090STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091xlog_state_sync(xlog_t *log,
3092 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003093 uint flags,
3094 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
3096 xlog_in_core_t *iclog;
3097 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003100 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 iclog = log->l_iclog;
3102
3103 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003104 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return XFS_ERROR(EIO);
3106 }
3107
3108 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003109 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3110 iclog = iclog->ic_next;
3111 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113
3114 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003115 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return 0;
3117 }
3118
3119 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3120 /*
3121 * We sleep here if we haven't already slept (e.g.
3122 * this is the first time we've looked at the correct
3123 * iclog buf) and the buffer before us is going to
3124 * be sync'ed. The reason for this is that if we
3125 * are doing sync transactions here, by waiting for
3126 * the previous I/O to complete, we can allow a few
3127 * more transactions into this iclog before we close
3128 * it down.
3129 *
3130 * Otherwise, we mark the buffer WANT_SYNC, and bump
3131 * up the refcnt so we can release the log (which drops
3132 * the ref count). The state switch keeps new transaction
3133 * commits from using this buffer. When the current commits
3134 * finish writing into the buffer, the refcount will drop to
3135 * zero and the buffer will go out then.
3136 */
3137 if (!already_slept &&
3138 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3139 XLOG_STATE_SYNCING))) {
3140 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3141 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003142 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003144 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 already_slept = 1;
3146 goto try_again;
3147 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003148 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003150 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (xlog_state_release_iclog(log, iclog))
3152 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003153 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003154 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 }
3157
3158 if ((flags & XFS_LOG_SYNC) && /* sleep */
3159 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3160
3161 /*
David Chinner12017fa2008-08-13 16:34:31 +10003162 * Don't wait on completion if we know that we've
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 * gotten a log write error.
3164 */
3165 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003166 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return XFS_ERROR(EIO);
3168 }
3169 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003170 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 /*
3172 * No need to grab the log lock here since we're
3173 * only deciding whether or not to return EIO
3174 * and the memory read should be atomic.
3175 */
3176 if (iclog->ic_state & XLOG_STATE_IOERROR)
3177 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003178 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003180 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
3182 return 0;
3183
3184 } while (iclog != log->l_iclog);
3185
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003186 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188} /* xlog_state_sync */
3189
3190
3191/*
3192 * Called when we want to mark the current iclog as being ready to sync to
3193 * disk.
3194 */
David Chinnera8272ce2007-11-23 16:28:09 +11003195STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3197{
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003198 ASSERT(spin_is_locked(&log->l_icloglock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3201 xlog_state_switch_iclogs(log, iclog, 0);
3202 } else {
3203 ASSERT(iclog->ic_state &
3204 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3205 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208
3209/*****************************************************************************
3210 *
3211 * TICKET functions
3212 *
3213 *****************************************************************************
3214 */
3215
3216/*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003217 * Free a used ticket when it's refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003219void
3220xfs_log_ticket_put(
3221 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003223 ASSERT(atomic_read(&ticket->t_ref) > 0);
3224 if (atomic_dec_and_test(&ticket->t_ref)) {
3225 sv_destroy(&ticket->t_wait);
3226 kmem_zone_free(xfs_log_ticket_zone, ticket);
3227 }
3228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003230xlog_ticket_t *
3231xfs_log_ticket_get(
3232 xlog_ticket_t *ticket)
3233{
3234 ASSERT(atomic_read(&ticket->t_ref) > 0);
3235 atomic_inc(&ticket->t_ref);
3236 return ticket;
3237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003240 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 */
David Chinnera8272ce2007-11-23 16:28:09 +11003242STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003243xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 int unit_bytes,
3245 int cnt,
3246 char client,
3247 uint xflags)
3248{
3249 xlog_ticket_t *tic;
3250 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
David Chinnereb01c9c2008-04-10 12:18:46 +10003252 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3253 if (!tic)
3254 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
3256 /*
3257 * Permanent reservations have up to 'cnt'-1 active log operations
3258 * in the log. A unit in this case is the amount of space for one
3259 * of these log operations. Normal reservations have a cnt of 1
3260 * and their unit amount is the total amount of space required.
3261 *
3262 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003263 * which occupy space in the on-disk log.
3264 *
3265 * Normal form of a transaction is:
3266 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3267 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3268 *
3269 * We need to account for all the leadup data and trailer data
3270 * around the transaction data.
3271 * And then we need to account for the worst case in terms of using
3272 * more space.
3273 * The worst case will happen if:
3274 * - the placement of the transaction happens to be such that the
3275 * roundoff is at its maximum
3276 * - the transaction data is synced before the commit record is synced
3277 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3278 * Therefore the commit record is in its own Log Record.
3279 * This can happen as the commit record is called with its
3280 * own region to xlog_write().
3281 * This then means that in the worst case, roundoff can happen for
3282 * the commit-rec as well.
3283 * The commit-rec is smaller than padding in this scenario and so it is
3284 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 */
3286
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003287 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003289 unit_bytes += sizeof(xfs_trans_header_t);
3290
3291 /* for start-rec */
3292 unit_bytes += sizeof(xlog_op_header_t);
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 /* for LR headers */
3295 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3296 unit_bytes += log->l_iclog_hsize * num_headers;
3297
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003298 /* for commit-rec LR header - note: padding will subsume the ophdr */
3299 unit_bytes += log->l_iclog_hsize;
3300
3301 /* for split-recs - ophdrs added when data split over LRs */
3302 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3303
3304 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003305 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003306 log->l_mp->m_sb.sb_logsunit > 1) {
3307 /* log su roundoff */
3308 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3309 } else {
3310 /* BB roundoff */
3311 unit_bytes += 2*BBSIZE;
3312 }
3313
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003314 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 tic->t_unit_res = unit_bytes;
3316 tic->t_curr_res = unit_bytes;
3317 tic->t_cnt = cnt;
3318 tic->t_ocnt = cnt;
3319 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3320 tic->t_clientid = client;
3321 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003322 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (xflags & XFS_LOG_PERM_RESERV)
3324 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003325 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Christoph Hellwig0adba532007-08-30 17:21:46 +10003327 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
3332
3333/******************************************************************************
3334 *
3335 * Log debug routines
3336 *
3337 ******************************************************************************
3338 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003339#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340/*
3341 * Make sure that the destination ptr is within the valid data region of
3342 * one of the iclogs. This uses backup pointers stored in a different
3343 * part of the log in case we trash the log structure.
3344 */
3345void
3346xlog_verify_dest_ptr(xlog_t *log,
3347 __psint_t ptr)
3348{
3349 int i;
3350 int good_ptr = 0;
3351
3352 for (i=0; i < log->l_iclog_bufs; i++) {
3353 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3354 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3355 good_ptr++;
3356 }
3357 if (! good_ptr)
3358 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3359} /* xlog_verify_dest_ptr */
3360
3361STATIC void
3362xlog_verify_grant_head(xlog_t *log, int equals)
3363{
3364 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3365 if (equals)
3366 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3367 else
3368 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3369 } else {
3370 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3371 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3372 }
3373} /* xlog_verify_grant_head */
3374
3375/* check if it will fit */
3376STATIC void
3377xlog_verify_tail_lsn(xlog_t *log,
3378 xlog_in_core_t *iclog,
3379 xfs_lsn_t tail_lsn)
3380{
3381 int blocks;
3382
3383 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3384 blocks =
3385 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3386 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3387 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3388 } else {
3389 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3390
3391 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3392 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3393
3394 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3395 if (blocks < BTOBB(iclog->ic_offset) + 1)
3396 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3397 }
3398} /* xlog_verify_tail_lsn */
3399
3400/*
3401 * Perform a number of checks on the iclog before writing to disk.
3402 *
3403 * 1. Make sure the iclogs are still circular
3404 * 2. Make sure we have a good magic number
3405 * 3. Make sure we don't have magic numbers in the data
3406 * 4. Check fields of each log operation header for:
3407 * A. Valid client identifier
3408 * B. tid ptr value falls in valid ptr space (user space code)
3409 * C. Length in log record header is correct according to the
3410 * individual operation headers within record.
3411 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3412 * log, check the preceding blocks of the physical log to make sure all
3413 * the cycle numbers agree with the current cycle number.
3414 */
3415STATIC void
3416xlog_verify_iclog(xlog_t *log,
3417 xlog_in_core_t *iclog,
3418 int count,
3419 boolean_t syncing)
3420{
3421 xlog_op_header_t *ophead;
3422 xlog_in_core_t *icptr;
3423 xlog_in_core_2_t *xhdr;
3424 xfs_caddr_t ptr;
3425 xfs_caddr_t base_ptr;
3426 __psint_t field_offset;
3427 __uint8_t clientid;
3428 int len, i, j, k, op_len;
3429 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003432 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 icptr = log->l_iclog;
3434 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003435 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 xlog_panic("xlog_verify_iclog: invalid ptr");
3437 icptr = icptr->ic_next;
3438 }
3439 if (icptr != log->l_iclog)
3440 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003441 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003444 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 xlog_panic("xlog_verify_iclog: invalid magic num");
3446
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003447 ptr = (xfs_caddr_t) &iclog->ic_header;
3448 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003450 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 xlog_panic("xlog_verify_iclog: unexpected magic num");
3452 }
3453
3454 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003455 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 ptr = iclog->ic_datap;
3457 base_ptr = ptr;
3458 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003459 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 for (i = 0; i < len; i++) {
3461 ophead = (xlog_op_header_t *)ptr;
3462
3463 /* clientid is only 1 byte */
3464 field_offset = (__psint_t)
3465 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3466 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3467 clientid = ophead->oh_clientid;
3468 } else {
3469 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3470 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3471 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3472 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003473 clientid = xlog_get_client_id(
3474 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003476 clientid = xlog_get_client_id(
3477 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
3479 }
3480 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003481 cmn_err(CE_WARN, "xlog_verify_iclog: "
3482 "invalid clientid %d op 0x%p offset 0x%lx",
3483 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 /* check length */
3486 field_offset = (__psint_t)
3487 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3488 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003489 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 } else {
3491 idx = BTOBBT((__psint_t)&ophead->oh_len -
3492 (__psint_t)iclog->ic_datap);
3493 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3494 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3495 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003496 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003498 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 }
3500 }
3501 ptr += sizeof(xlog_op_header_t) + op_len;
3502 }
3503} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003504#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
3506/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003507 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 */
3509STATIC int
3510xlog_state_ioerror(
3511 xlog_t *log)
3512{
3513 xlog_in_core_t *iclog, *ic;
3514
3515 iclog = log->l_iclog;
3516 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3517 /*
3518 * Mark all the incore logs IOERROR.
3519 * From now on, no log flushes will result.
3520 */
3521 ic = iclog;
3522 do {
3523 ic->ic_state = XLOG_STATE_IOERROR;
3524 ic = ic->ic_next;
3525 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528 /*
3529 * Return non-zero, if state transition has already happened.
3530 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003531 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532}
3533
3534/*
3535 * This is called from xfs_force_shutdown, when we're forcibly
3536 * shutting down the filesystem, typically because of an IO error.
3537 * Our main objectives here are to make sure that:
3538 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3539 * parties to find out, 'atomically'.
3540 * b. those who're sleeping on log reservations, pinned objects and
3541 * other resources get woken up, and be told the bad news.
3542 * c. nothing new gets queued up after (a) and (b) are done.
3543 * d. if !logerror, flush the iclogs to disk, then seal them off
3544 * for business.
3545 */
3546int
3547xfs_log_force_umount(
3548 struct xfs_mount *mp,
3549 int logerror)
3550{
3551 xlog_ticket_t *tic;
3552 xlog_t *log;
3553 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003554 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 log = mp->m_log;
3557
3558 /*
3559 * If this happens during log recovery, don't worry about
3560 * locking; the log isn't open for business yet.
3561 */
3562 if (!log ||
3563 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3564 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003565 if (mp->m_sb_bp)
3566 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
3569
3570 /*
3571 * Somebody could've already done the hard work for us.
3572 * No need to get locks for this.
3573 */
3574 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3575 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003576 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
3578 retval = 0;
3579 /*
3580 * We must hold both the GRANT lock and the LOG lock,
3581 * before we mark the filesystem SHUTDOWN and wake
3582 * everybody up to tell the bad news.
3583 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003584 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003585 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003587 if (mp->m_sb_bp)
3588 XFS_BUF_DONE(mp->m_sb_bp);
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 /*
3591 * This flag is sort of redundant because of the mount flag, but
3592 * it's good to maintain the separation between the log and the rest
3593 * of XFS.
3594 */
3595 log->l_flags |= XLOG_IO_ERROR;
3596
3597 /*
3598 * If we hit a log error, we want to mark all the iclogs IOERROR
3599 * while we're still holding the loglock.
3600 */
3601 if (logerror)
3602 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003603 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 /*
3606 * We don't want anybody waiting for log reservations
3607 * after this. That means we have to wake up everybody
3608 * queued up on reserve_headq as well as write_headq.
3609 * In addition, we make sure in xlog_{re}grant_log_space
3610 * that we don't enqueue anything once the SHUTDOWN flag
3611 * is set, and this action is protected by the GRANTLOCK.
3612 */
3613 if ((tic = log->l_reserve_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_reserve_headq);
3618 }
3619
3620 if ((tic = log->l_write_headq)) {
3621 do {
David Chinner12017fa2008-08-13 16:34:31 +10003622 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 tic = tic->t_next;
3624 } while (tic != log->l_write_headq);
3625 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003626 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3629 ASSERT(!logerror);
3630 /*
3631 * Force the incore logs to disk before shutting the
3632 * log down completely.
3633 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003634 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003635 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003637 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
3639 /*
3640 * Wake up everybody waiting on xfs_log_force.
3641 * Callback all log item committed functions as if the
3642 * log writes were completed.
3643 */
3644 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3645
3646#ifdef XFSERRORDEBUG
3647 {
3648 xlog_in_core_t *iclog;
3649
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003650 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 iclog = log->l_iclog;
3652 do {
3653 ASSERT(iclog->ic_callback == 0);
3654 iclog = iclog->ic_next;
3655 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003656 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
3658#endif
3659 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003660 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003663STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664xlog_iclogs_empty(xlog_t *log)
3665{
3666 xlog_in_core_t *iclog;
3667
3668 iclog = log->l_iclog;
3669 do {
3670 /* endianness does not matter here, zero is zero in
3671 * any language.
3672 */
3673 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 iclog = iclog->ic_next;
3676 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003677 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}