blob: f76c6d7cea21f9539b9ad21562d943c39c937fc7 [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);
Christoph Hellwig26430752009-02-12 19:55:48 +0100577 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100578 }
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);
Christoph Hellwig26430752009-02-12 19:55:48 +0100597 goto out_destroy_ail;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100605
606out_destroy_ail:
607 xfs_trans_ail_destroy(mp);
608out_free_log:
609 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100610out:
David Chinner249a8c12008-02-05 12:13:32 +1100611 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/*
615 * Finish the recovery of the file system. This is separate from
616 * the xfs_log_mount() call, because it depends on the code in
617 * xfs_mountfs() to read in the root and real-time bitmap inodes
618 * between calling xfs_log_mount() and here.
619 *
620 * mp - ubiquitous xfs mount point structure
621 */
622int
Christoph Hellwig42490232008-08-13 16:49:32 +1000623xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 int error;
626
627 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000628 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 else {
630 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000631 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 return error;
635}
636
637/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * Final log writes as part of unmount.
639 *
640 * Mark the filesystem clean as unmount happens. Note that during relocation
641 * this routine needs to be executed as part of source-bag while the
642 * deallocation must not be done until source-end.
643 */
644
645/*
646 * Unmount record used to have a string "Unmount filesystem--" in the
647 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
648 * We just write the magic number now since that particular field isn't
649 * currently architecture converted and "nUmount" is a bit foo.
650 * As far as I know, there weren't any dependencies on the old behaviour.
651 */
652
653int
654xfs_log_unmount_write(xfs_mount_t *mp)
655{
656 xlog_t *log = mp->m_log;
657 xlog_in_core_t *iclog;
658#ifdef DEBUG
659 xlog_in_core_t *first_iclog;
660#endif
661 xfs_log_iovec_t reg[1];
662 xfs_log_ticket_t tic = NULL;
663 xfs_lsn_t lsn;
664 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* the data section must be 32 bit size aligned */
667 struct {
668 __uint16_t magic;
669 __uint16_t pad1;
670 __uint32_t pad2; /* may as well make it 64 bits */
671 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Don't write out unmount record on read-only mounts.
675 * Or, if we are doing a forced umount (typically because of IO errors).
676 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000677 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
679
David Chinnerb911ca02008-04-10 12:24:30 +1000680 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
681 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683#ifdef DEBUG
684 first_iclog = iclog = log->l_iclog;
685 do {
686 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
687 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
688 ASSERT(iclog->ic_offset == 0);
689 }
690 iclog = iclog->ic_next;
691 } while (iclog != first_iclog);
692#endif
693 if (! (XLOG_FORCED_SHUTDOWN(log))) {
694 reg[0].i_addr = (void*)&magic;
695 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000696 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Tim Shimmin955e47a2006-09-28 11:04:16 +1000698 error = xfs_log_reserve(mp, 600, 1, &tic,
699 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (!error) {
701 /* remove inited flag */
702 ((xlog_ticket_t *)tic)->t_flags = 0;
703 error = xlog_write(mp, reg, 1, tic, &lsn,
704 NULL, XLOG_UNMOUNT_TRANS);
705 /*
706 * At this point, we're umounting anyway,
707 * so there's no point in transitioning log state
708 * to IOERROR. Just continue...
709 */
710 }
711
712 if (error) {
713 xfs_fs_cmn_err(CE_ALERT, mp,
714 "xfs_log_unmount: unmount record failed");
715 }
716
717
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000718 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100720 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100722 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000723 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000725 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
727 iclog->ic_state == XLOG_STATE_DIRTY)) {
728 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000729 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 &log->l_icloglock, s);
731 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000732 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000735 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000737 if (tic) {
738 xlog_trace_loggrant(log, tic, "unmount rec");
739 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100740 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 } else {
743 /*
744 * We're already in forced_shutdown mode, couldn't
745 * even attempt to write out the unmount transaction.
746 *
747 * Go through the motions of sync'ing and releasing
748 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000749 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * be in progress. Do this as a separate section of
751 * code so we'll know if we ever get stuck here that
752 * we're in this odd situation of trying to unmount
753 * a file system that went into forced_shutdown as
754 * the result of an unmount..
755 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000756 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100758 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100761 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000762 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000764 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
767 || iclog->ic_state == XLOG_STATE_DIRTY
768 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
769
David Chinner12017fa2008-08-13 16:34:31 +1000770 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 &log->l_icloglock, s);
772 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000773 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 }
776
David Chinner1bb7d6b2008-04-10 12:24:38 +1000777 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778} /* xfs_log_unmount_write */
779
780/*
781 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100782 *
783 * We need to stop the aild from running before we destroy
784 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
786void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100787xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
David Chinner249a8c12008-02-05 12:13:32 +1100789 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000790 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793/*
794 * Write region vectors to log. The write happens using the space reservation
795 * of the ticket (tic). It is not a requirement that all writes for a given
796 * transaction occur with one call to xfs_log_write().
797 */
798int
799xfs_log_write(xfs_mount_t * mp,
800 xfs_log_iovec_t reg[],
801 int nentries,
802 xfs_log_ticket_t tic,
803 xfs_lsn_t *start_lsn)
804{
805 int error;
806 xlog_t *log = mp->m_log;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (XLOG_FORCED_SHUTDOWN(log))
809 return XFS_ERROR(EIO);
810
811 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000812 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100814 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815} /* xfs_log_write */
816
817
818void
819xfs_log_move_tail(xfs_mount_t *mp,
820 xfs_lsn_t tail_lsn)
821{
822 xlog_ticket_t *tic;
823 xlog_t *log = mp->m_log;
824 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (XLOG_FORCED_SHUTDOWN(log))
827 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (tail_lsn == 0) {
830 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000831 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000833 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000836 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
839 * tail_lsn.
840 */
841 if (tail_lsn != 1) {
842 log->l_tail_lsn = tail_lsn;
843 }
844
845 if ((tic = log->l_write_headq)) {
846#ifdef DEBUG
847 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
848 panic("Recovery problem");
849#endif
850 cycle = log->l_grant_write_cycle;
851 bytes = log->l_grant_write_bytes;
852 free_bytes = xlog_space_left(log, cycle, bytes);
853 do {
854 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
855
856 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
857 break;
858 tail_lsn = 0;
859 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000860 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 tic = tic->t_next;
862 } while (tic != log->l_write_headq);
863 }
864 if ((tic = log->l_reserve_headq)) {
865#ifdef DEBUG
866 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
867 panic("Recovery problem");
868#endif
869 cycle = log->l_grant_reserve_cycle;
870 bytes = log->l_grant_reserve_bytes;
871 free_bytes = xlog_space_left(log, cycle, bytes);
872 do {
873 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
874 need_bytes = tic->t_unit_res*tic->t_cnt;
875 else
876 need_bytes = tic->t_unit_res;
877 if (free_bytes < need_bytes && tail_lsn != 1)
878 break;
879 tail_lsn = 0;
880 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000881 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 tic = tic->t_next;
883 } while (tic != log->l_reserve_headq);
884 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000885 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886} /* xfs_log_move_tail */
887
888/*
889 * Determine if we have a transaction that has gone to disk
890 * that needs to be covered. Log activity needs to be idle (no AIL and
891 * nothing in the iclogs). And, we need to be in the right state indicating
892 * something has gone out.
893 */
894int
895xfs_log_need_covered(xfs_mount_t *mp)
896{
David Chinner27d8d5f2008-10-30 17:38:39 +1100897 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
David Chinner92821e22007-05-24 15:26:31 +1000900 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 0;
902
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000903 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
905 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
David Chinnera9c21c12008-10-30 17:39:35 +1100906 && !xfs_trans_ail_tail(log->l_ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 && xlog_iclogs_empty(log)) {
908 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
909 log->l_covered_state = XLOG_STATE_COVER_DONE;
910 else {
911 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
912 log->l_covered_state = XLOG_STATE_COVER_DONE2;
913 }
914 needed = 1;
915 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000916 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100917 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920/******************************************************************************
921 *
922 * local routines
923 *
924 ******************************************************************************
925 */
926
927/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
928 * The log manager must keep track of the last LR which was committed
929 * to disk. The lsn of this LR will become the new tail_lsn whenever
930 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
931 * the situation where stuff could be written into the log but nothing
932 * was ever in the AIL when asked. Eventually, we panic since the
933 * tail hits the head.
934 *
935 * We may be holding the log iclog lock upon entering this routine.
936 */
937xfs_lsn_t
938xlog_assign_tail_lsn(xfs_mount_t *mp)
939{
940 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 xlog_t *log = mp->m_log;
942
David Chinner5b00f142008-10-30 17:39:00 +1100943 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000944 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (tail_lsn != 0) {
946 log->l_tail_lsn = tail_lsn;
947 } else {
948 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
949 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000950 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 return tail_lsn;
953} /* xlog_assign_tail_lsn */
954
955
956/*
957 * Return the space in the log between the tail and the head. The head
958 * is passed in the cycle/bytes formal parms. In the special case where
959 * the reserve head has wrapped passed the tail, this calculation is no
960 * longer valid. In this case, just return 0 which means there is no space
961 * in the log. This works for all places where this function is called
962 * with the reserve head. Of course, if the write head were to ever
963 * wrap the tail, we should blow up. Rather than catch this case here,
964 * we depend on other ASSERTions in other parts of the code. XXXmiken
965 *
966 * This code also handles the case where the reservation head is behind
967 * the tail. The details of this case are described below, but the end
968 * result is that we return the size of the log as the amount of space left.
969 */
David Chinnera8272ce2007-11-23 16:28:09 +1100970STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971xlog_space_left(xlog_t *log, int cycle, int bytes)
972{
973 int free_bytes;
974 int tail_bytes;
975 int tail_cycle;
976
977 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
978 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
979 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
980 free_bytes = log->l_logsize - (bytes - tail_bytes);
981 } else if ((tail_cycle + 1) < cycle) {
982 return 0;
983 } else if (tail_cycle < cycle) {
984 ASSERT(tail_cycle == (cycle - 1));
985 free_bytes = tail_bytes - bytes;
986 } else {
987 /*
988 * The reservation head is behind the tail.
989 * In this case we just want to return the size of the
990 * log as the amount of space left.
991 */
992 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
993 "xlog_space_left: head behind tail\n"
994 " tail_cycle = %d, tail_bytes = %d\n"
995 " GH cycle = %d, GH bytes = %d",
996 tail_cycle, tail_bytes, cycle, bytes);
997 ASSERT(0);
998 free_bytes = log->l_logsize;
999 }
1000 return free_bytes;
1001} /* xlog_space_left */
1002
1003
1004/*
1005 * Log function which is called when an io completes.
1006 *
1007 * The log manager needs its own routine, in order to control what
1008 * happens with the buffer after the write completes.
1009 */
1010void
1011xlog_iodone(xfs_buf_t *bp)
1012{
1013 xlog_in_core_t *iclog;
1014 xlog_t *l;
1015 int aborted;
1016
1017 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1018 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1019 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1020 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 l = iclog->ic_log;
1022
1023 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001024 * If the _XFS_BARRIER_FAILED flag was set by a lower
1025 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +10001026 * barrier I/O. Warn loudly and turn off barriers.
1027 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001028 if (bp->b_flags & _XFS_BARRIER_FAILED) {
1029 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001030 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1031 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1032 "xlog_iodone: Barriers are no longer supported"
1033 " by device. Disabling barriers\n");
1034 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1035 }
1036
1037 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * Race to shutdown the filesystem if we see an error.
1039 */
1040 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1041 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1042 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1043 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001044 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * This flag will be propagated to the trans-committed
1047 * callback routines to let them know that the log-commit
1048 * didn't succeed.
1049 */
1050 aborted = XFS_LI_ABORTED;
1051 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1052 aborted = XFS_LI_ABORTED;
1053 }
David Chinner3db296f2007-05-14 18:24:16 +10001054
1055 /* log I/O is always issued ASYNC */
1056 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001058 /*
1059 * do not reference the buffer (bp) here as we could race
1060 * with it being freed after writing the unmount record to the
1061 * log.
1062 */
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064} /* xlog_iodone */
1065
1066/*
1067 * The bdstrat callback function for log bufs. This gives us a central
1068 * place to trap bufs in case we get hit by a log I/O error and need to
1069 * shutdown. Actually, in practice, even when we didn't get a log error,
1070 * we transition the iclogs to IOERROR state *after* flushing all existing
1071 * iclogs to disk. This is because we don't want anymore new transactions to be
1072 * started or completed afterwards.
1073 */
1074STATIC int
1075xlog_bdstrat_cb(struct xfs_buf *bp)
1076{
1077 xlog_in_core_t *iclog;
1078
1079 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1080
1081 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1082 /* note for irix bstrat will need struct bdevsw passed
1083 * Fix the following macro if the code ever is merged
1084 */
1085 XFS_bdstrat(bp);
1086 return 0;
1087 }
1088
1089 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1090 XFS_BUF_ERROR(bp, EIO);
1091 XFS_BUF_STALE(bp);
1092 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001093 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095
1096}
1097
1098/*
1099 * Return size of each in-core log record buffer.
1100 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001101 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 *
1103 * If the filesystem blocksize is too large, we may need to choose a
1104 * larger size since the directory code currently logs entire blocks.
1105 */
1106
1107STATIC void
1108xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1109 xlog_t *log)
1110{
1111 int size;
1112 int xhdrs;
1113
Eric Sandeen1cb51252007-08-16 16:24:43 +10001114 if (mp->m_logbufs <= 0)
1115 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1116 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001117 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /*
1120 * Buffer size passed in from mount system call.
1121 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001122 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 size = log->l_iclog_size = mp->m_logbsize;
1124 log->l_iclog_size_log = 0;
1125 while (size != 1) {
1126 log->l_iclog_size_log++;
1127 size >>= 1;
1128 }
1129
Eric Sandeen62118702008-03-06 13:44:28 +11001130 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001131 /* # headers = size / 32k
1132 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
1134
1135 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1136 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1137 xhdrs++;
1138 log->l_iclog_hsize = xhdrs << BBSHIFT;
1139 log->l_iclog_heads = xhdrs;
1140 } else {
1141 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1142 log->l_iclog_hsize = BBSIZE;
1143 log->l_iclog_heads = 1;
1144 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001145 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001148 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001149 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1150 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* the default log size is 16k or 32k which is one header sector */
1153 log->l_iclog_hsize = BBSIZE;
1154 log->l_iclog_heads = 1;
1155
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001156done:
1157 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001158 if (mp->m_logbufs == 0)
1159 mp->m_logbufs = log->l_iclog_bufs;
1160 if (mp->m_logbsize == 0)
1161 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162} /* xlog_get_iclog_buffer_size */
1163
1164
1165/*
1166 * This routine initializes some of the log structure for a given mount point.
1167 * Its primary purpose is to fill in enough, so recovery can occur. However,
1168 * some other stuff may be filled in too.
1169 */
1170STATIC xlog_t *
1171xlog_alloc_log(xfs_mount_t *mp,
1172 xfs_buftarg_t *log_target,
1173 xfs_daddr_t blk_offset,
1174 int num_bblks)
1175{
1176 xlog_t *log;
1177 xlog_rec_header_t *head;
1178 xlog_in_core_t **iclogp;
1179 xlog_in_core_t *iclog, *prev_iclog=NULL;
1180 xfs_buf_t *bp;
1181 int i;
1182 int iclogsize;
1183
Dave Chinner644c3562008-11-10 16:50:24 +11001184 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1185 if (!log)
1186 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 log->l_mp = mp;
1189 log->l_targ = log_target;
1190 log->l_logsize = BBTOB(num_bblks);
1191 log->l_logBBstart = blk_offset;
1192 log->l_logBBsize = num_bblks;
1193 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1194 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1195
1196 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001197 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1199 log->l_last_sync_lsn = log->l_tail_lsn;
1200 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1201 log->l_grant_reserve_cycle = 1;
1202 log->l_grant_write_cycle = 1;
1203
Eric Sandeen62118702008-03-06 13:44:28 +11001204 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1206 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1207 /* for larger sector sizes, must have v2 or external log */
1208 ASSERT(log->l_sectbb_log == 0 ||
1209 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001210 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1212 }
1213 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1214
1215 xlog_get_iclog_buffer_size(mp, log);
1216
1217 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001218 if (!bp)
1219 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1221 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1222 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1223 ASSERT(XFS_BUF_ISBUSY(bp));
1224 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1225 log->l_xbuf = bp;
1226
Eric Sandeen007c61c2007-10-11 17:43:56 +10001227 spin_lock_init(&log->l_icloglock);
1228 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001229 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001231 xlog_trace_loggrant_alloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1233 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1234
1235 iclogp = &log->l_iclog;
1236 /*
1237 * The amount of memory to allocate for the iclog structure is
1238 * rather funky due to the way the structure is defined. It is
1239 * done this way so that we can use different sizes for machines
1240 * with different amounts of memory. See the definition of
1241 * xlog_in_core_t in xfs_log_priv.h for details.
1242 */
1243 iclogsize = log->l_iclog_size;
1244 ASSERT(log->l_iclog_size >= 4096);
1245 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001246 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1247 if (!*iclogp)
1248 goto out_free_iclog;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 iclog->ic_prev = prev_iclog;
1252 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001253
1254 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001255 if (!bp)
1256 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001257 if (!XFS_BUF_CPSEMA(bp))
1258 ASSERT(0);
1259 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1260 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1261 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1262 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001263 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001264#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 head = &iclog->ic_header;
1268 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001269 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1270 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001271 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001272 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001274 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1278 iclog->ic_state = XLOG_STATE_ACTIVE;
1279 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001280 atomic_set(&iclog->ic_refcnt, 0);
1281 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001283 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1286 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001287 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1288 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001290 xlog_trace_iclog_alloc(iclog);
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 iclogp = &iclog->ic_next;
1293 }
1294 *iclogp = log->l_iclog; /* complete ring */
1295 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1296
1297 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001298
1299out_free_iclog:
1300 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1301 prev_iclog = iclog->ic_next;
1302 if (iclog->ic_bp) {
1303 sv_destroy(&iclog->ic_force_wait);
1304 sv_destroy(&iclog->ic_write_wait);
1305 xfs_buf_free(iclog->ic_bp);
1306 xlog_trace_iclog_dealloc(iclog);
1307 }
1308 kmem_free(iclog);
1309 }
1310 spinlock_destroy(&log->l_icloglock);
1311 spinlock_destroy(&log->l_grant_lock);
1312 xlog_trace_loggrant_dealloc(log);
1313 xfs_buf_free(log->l_xbuf);
1314out_free_log:
1315 kmem_free(log);
1316 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317} /* xlog_alloc_log */
1318
1319
1320/*
1321 * Write out the commit record of a transaction associated with the given
1322 * ticket. Return the lsn of the commit record.
1323 */
1324STATIC int
1325xlog_commit_record(xfs_mount_t *mp,
1326 xlog_ticket_t *ticket,
1327 xlog_in_core_t **iclog,
1328 xfs_lsn_t *commitlsnp)
1329{
1330 int error;
1331 xfs_log_iovec_t reg[1];
1332
1333 reg[0].i_addr = NULL;
1334 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001335 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 ASSERT_ALWAYS(iclog);
1338 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1339 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001340 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001342 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343} /* xlog_commit_record */
1344
1345
1346/*
1347 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1348 * log space. This code pushes on the lsn which would supposedly free up
1349 * the 25% which we want to leave free. We may need to adopt a policy which
1350 * pushes on an lsn which is further along in the log once we reach the high
1351 * water mark. In this manner, we would be creating a low water mark.
1352 */
David Chinnera8272ce2007-11-23 16:28:09 +11001353STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354xlog_grant_push_ail(xfs_mount_t *mp,
1355 int need_bytes)
1356{
1357 xlog_t *log = mp->m_log; /* pointer to the log */
1358 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1359 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1360 int free_blocks; /* free blocks left to write to */
1361 int free_bytes; /* free bytes left to write to */
1362 int threshold_block; /* block in lsn we'd like to be at */
1363 int threshold_cycle; /* lsn cycle we'd like to be at */
1364 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1367
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001368 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 free_bytes = xlog_space_left(log,
1370 log->l_grant_reserve_cycle,
1371 log->l_grant_reserve_bytes);
1372 tail_lsn = log->l_tail_lsn;
1373 free_blocks = BTOBBT(free_bytes);
1374
1375 /*
1376 * Set the threshold for the minimum number of free blocks in the
1377 * log to the maximum of what the caller needs, one quarter of the
1378 * log, and 256 blocks.
1379 */
1380 free_threshold = BTOBB(need_bytes);
1381 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1382 free_threshold = MAX(free_threshold, 256);
1383 if (free_blocks < free_threshold) {
1384 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1385 threshold_cycle = CYCLE_LSN(tail_lsn);
1386 if (threshold_block >= log->l_logBBsize) {
1387 threshold_block -= log->l_logBBsize;
1388 threshold_cycle += 1;
1389 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001390 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* Don't pass in an lsn greater than the lsn of the last
1393 * log record known to be on disk.
1394 */
1395 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1396 threshold_lsn = log->l_last_sync_lsn;
1397 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001398 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * Get the transaction layer to kick the dirty buffers out to
1402 * disk asynchronously. No point in trying to do this if
1403 * the filesystem is shutting down.
1404 */
1405 if (threshold_lsn &&
1406 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001407 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408} /* xlog_grant_push_ail */
1409
1410
1411/*
1412 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1413 * fashion. Previously, we should have moved the current iclog
1414 * ptr in the log to point to the next available iclog. This allows further
1415 * write to continue while this code syncs out an iclog ready to go.
1416 * Before an in-core log can be written out, the data section must be scanned
1417 * to save away the 1st word of each BBSIZE block into the header. We replace
1418 * it with the current cycle count. Each BBSIZE block is tagged with the
1419 * cycle count because there in an implicit assumption that drives will
1420 * guarantee that entire 512 byte blocks get written at once. In other words,
1421 * we can't have part of a 512 byte block written and part not written. By
1422 * tagging each block, we will know which blocks are valid when recovering
1423 * after an unclean shutdown.
1424 *
1425 * This routine is single threaded on the iclog. No other thread can be in
1426 * this routine with the same iclog. Changing contents of iclog can there-
1427 * fore be done without grabbing the state machine lock. Updating the global
1428 * log will require grabbing the lock though.
1429 *
1430 * The entire log manager uses a logical block numbering scheme. Only
1431 * log_sync (and then only bwrite()) know about the fact that the log may
1432 * not start with block zero on a given device. The log block start offset
1433 * is added immediately before calling bwrite().
1434 */
1435
David Chinnera8272ce2007-11-23 16:28:09 +11001436STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437xlog_sync(xlog_t *log,
1438 xlog_in_core_t *iclog)
1439{
1440 xfs_caddr_t dptr; /* pointer to byte sized element */
1441 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001442 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 uint count; /* byte count of bwrite */
1444 uint count_init; /* initial count before roundup */
1445 int roundoff; /* roundoff to BB or stripe */
1446 int split = 0; /* split write into two regions */
1447 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001448 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001451 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 /* Add for LR header */
1454 count_init = log->l_iclog_hsize + iclog->ic_offset;
1455
1456 /* Round out the log write size */
1457 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1458 /* we have a v2 stripe unit to use */
1459 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1460 } else {
1461 count = BBTOB(BTOBB(count_init));
1462 }
1463 roundoff = count - count_init;
1464 ASSERT(roundoff >= 0);
1465 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1466 roundoff < log->l_mp->m_sb.sb_logsunit)
1467 ||
1468 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1469 roundoff < BBTOB(1)));
1470
1471 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001472 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001473 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001474 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /* put cycle number in every block */
1477 xlog_pack_data(log, iclog, roundoff);
1478
1479 /* real byte length */
1480 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001481 iclog->ic_header.h_len =
1482 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001484 iclog->ic_header.h_len =
1485 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487
Nathan Scottf5faad72006-07-28 17:04:44 +10001488 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1490 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001491 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1494
1495 /* Do we need to split this write into 2 parts? */
1496 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1497 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1498 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1499 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1500 } else {
1501 iclog->ic_bwritecnt = 1;
1502 }
David Chinner511105b2007-05-24 15:21:57 +10001503 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001505 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 XFS_BUF_BUSY(bp);
1507 XFS_BUF_ASYNC(bp);
1508 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001509 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001510 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001512 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001513 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1516 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1517
1518 xlog_verify_iclog(log, iclog, count, B_TRUE);
1519
1520 /* account for log which doesn't start at block #0 */
1521 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1522 /*
1523 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1524 * is shutting down.
1525 */
1526 XFS_BUF_WRITE(bp);
1527
1528 if ((error = XFS_bwrite(bp))) {
1529 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1530 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001531 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001534 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1536 (unsigned long)1);
1537 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1538 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1539 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1540 (__psint_t)count), split);
1541 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001542 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 XFS_BUF_BUSY(bp);
1544 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001545 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1546 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 dptr = XFS_BUF_PTR(bp);
1548 /*
1549 * Bump the cycle numbers at the start of each block
1550 * since this part of the buffer is at the start of
1551 * a new cycle. Watch out for the header magic number
1552 * case, though.
1553 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001554 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001555 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001556 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001557 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 dptr += BBSIZE;
1559 }
1560
1561 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1562 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1563
Nathan Scottc41564b2006-03-29 08:55:14 +10001564 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1566 XFS_BUF_WRITE(bp);
1567 if ((error = XFS_bwrite(bp))) {
1568 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1569 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001570 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574} /* xlog_sync */
1575
1576
1577/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001578 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 */
David Chinnera8272ce2007-11-23 16:28:09 +11001580STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001581xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int i;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 iclog = log->l_iclog;
1587 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001588 sv_destroy(&iclog->ic_force_wait);
1589 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 xfs_buf_free(iclog->ic_bp);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001591 xlog_trace_iclog_dealloc(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001593 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 iclog = next_iclog;
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 spinlock_destroy(&log->l_icloglock);
1597 spinlock_destroy(&log->l_grant_lock);
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 xfs_buf_free(log->l_xbuf);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001600 xlog_trace_loggrant_dealloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001602 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001603} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605/*
1606 * Update counters atomically now that memcpy is done.
1607 */
1608/* ARGSUSED */
1609static inline void
1610xlog_state_finish_copy(xlog_t *log,
1611 xlog_in_core_t *iclog,
1612 int record_cnt,
1613 int copy_bytes)
1614{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001615 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001617 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 iclog->ic_offset += copy_bytes;
1619
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001620 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621} /* xlog_state_finish_copy */
1622
1623
1624
1625
1626/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001627 * print out info relating to regions written which consume
1628 * the reservation
1629 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001630STATIC void
1631xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1632{
1633 uint i;
1634 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1635
1636 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1637 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1638 "bformat",
1639 "bchunk",
1640 "efi_format",
1641 "efd_format",
1642 "iformat",
1643 "icore",
1644 "iext",
1645 "ibroot",
1646 "ilocal",
1647 "iattr_ext",
1648 "iattr_broot",
1649 "iattr_local",
1650 "qformat",
1651 "dquot",
1652 "quotaoff",
1653 "LR header",
1654 "unmount",
1655 "commit",
1656 "trans header"
1657 };
1658 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1659 "SETATTR_NOT_SIZE",
1660 "SETATTR_SIZE",
1661 "INACTIVE",
1662 "CREATE",
1663 "CREATE_TRUNC",
1664 "TRUNCATE_FILE",
1665 "REMOVE",
1666 "LINK",
1667 "RENAME",
1668 "MKDIR",
1669 "RMDIR",
1670 "SYMLINK",
1671 "SET_DMATTRS",
1672 "GROWFS",
1673 "STRAT_WRITE",
1674 "DIOSTRAT",
1675 "WRITE_SYNC",
1676 "WRITEID",
1677 "ADDAFORK",
1678 "ATTRINVAL",
1679 "ATRUNCATE",
1680 "ATTR_SET",
1681 "ATTR_RM",
1682 "ATTR_FLAG",
1683 "CLEAR_AGI_BUCKET",
1684 "QM_SBCHANGE",
1685 "DUMMY1",
1686 "DUMMY2",
1687 "QM_QUOTAOFF",
1688 "QM_DQALLOC",
1689 "QM_SETQLIM",
1690 "QM_DQCLUSTER",
1691 "QM_QINOCREATE",
1692 "QM_QUOTAOFF_END",
1693 "SB_UNIT",
1694 "FSYNC_TS",
1695 "GROWFSRT_ALLOC",
1696 "GROWFSRT_ZERO",
1697 "GROWFSRT_FREE",
1698 "SWAPEXT"
1699 };
1700
1701 xfs_fs_cmn_err(CE_WARN, mp,
1702 "xfs_log_write: reservation summary:\n"
1703 " trans type = %s (%u)\n"
1704 " unit res = %d bytes\n"
1705 " current res = %d bytes\n"
1706 " total reg = %u bytes (o/flow = %u bytes)\n"
1707 " ophdrs = %u (ophdr space = %u bytes)\n"
1708 " ophdr + reg = %u bytes\n"
1709 " num regions = %u\n",
1710 ((ticket->t_trans_type <= 0 ||
1711 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1712 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1713 ticket->t_trans_type,
1714 ticket->t_unit_res,
1715 ticket->t_curr_res,
1716 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1717 ticket->t_res_num_ophdrs, ophdr_spc,
1718 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001719 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001720 ticket->t_res_num);
1721
1722 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001723 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001724 cmn_err(CE_WARN,
1725 "region[%u]: %s - %u bytes\n",
1726 i,
1727 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1728 "bad-rtype" : res_type_str[r_type-1]),
1729 ticket->t_res_arr[i].r_len);
1730 }
1731}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001732
1733/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * Write some region out to in-core log
1735 *
1736 * This will be called when writing externally provided regions or when
1737 * writing out a commit record for a given transaction.
1738 *
1739 * General algorithm:
1740 * 1. Find total length of this write. This may include adding to the
1741 * lengths passed in.
1742 * 2. Check whether we violate the tickets reservation.
1743 * 3. While writing to this iclog
1744 * A. Reserve as much space in this iclog as can get
1745 * B. If this is first write, save away start lsn
1746 * C. While writing this region:
1747 * 1. If first write of transaction, write start record
1748 * 2. Write log operation header (header per region)
1749 * 3. Find out if we can fit entire region into this iclog
1750 * 4. Potentially, verify destination memcpy ptr
1751 * 5. Memcpy (partial) region
1752 * 6. If partial copy, release iclog; otherwise, continue
1753 * copying more regions into current iclog
1754 * 4. Mark want sync bit (in simulation mode)
1755 * 5. Release iclog for potential flush to on-disk log.
1756 *
1757 * ERRORS:
1758 * 1. Panic if reservation is overrun. This should never happen since
1759 * reservation amounts are generated internal to the filesystem.
1760 * NOTES:
1761 * 1. Tickets are single threaded data structures.
1762 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1763 * syncing routine. When a single log_write region needs to span
1764 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1765 * on all log operation writes which don't contain the end of the
1766 * region. The XLOG_END_TRANS bit is used for the in-core log
1767 * operation which contains the end of the continued log_write region.
1768 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1769 * we don't really know exactly how much space will be used. As a result,
1770 * we don't update ic_offset until the end when we know exactly how many
1771 * bytes have been written out.
1772 */
David Chinnera8272ce2007-11-23 16:28:09 +11001773STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774xlog_write(xfs_mount_t * mp,
1775 xfs_log_iovec_t reg[],
1776 int nentries,
1777 xfs_log_ticket_t tic,
1778 xfs_lsn_t *start_lsn,
1779 xlog_in_core_t **commit_iclog,
1780 uint flags)
1781{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001782 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001784 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 __psint_t ptr; /* copy address into data region */
1787 int len; /* # xlog_write() bytes 2 still copy */
1788 int index; /* region index currently copying */
1789 int log_offset; /* offset (from 0) into data region */
1790 int start_rec_copy; /* # bytes to copy for start record */
1791 int partial_copy; /* did we split a region? */
1792 int partial_copy_len;/* # bytes copied if split region */
1793 int need_copy; /* # bytes need to memcpy this region */
1794 int copy_len; /* # bytes actually memcpy'ing */
1795 int copy_off; /* # bytes from entry start */
1796 int contwr; /* continued write of in-core log? */
1797 int error;
1798 int record_cnt = 0, data_cnt = 0;
1799
1800 partial_copy_len = partial_copy = 0;
1801
1802 /* Calculate potential maximum space. Each region gets its own
1803 * xlog_op_header_t and may need to be double word aligned.
1804 */
1805 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001806 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001808 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 for (index = 0; index < nentries; index++) {
1812 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001813 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001815 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 contwr = *start_lsn = 0;
1818
1819 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001820 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821#ifdef DEBUG
1822 xlog_panic(
1823 "xfs_log_write: reservation ran out. Need to up reservation");
1824#else
1825 /* Customer configurable panic */
1826 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1827 "xfs_log_write: reservation ran out. Need to up reservation");
1828 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001829 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830#endif
1831 } else
1832 ticket->t_curr_res -= len;
1833
1834 for (index = 0; index < nentries; ) {
1835 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1836 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001837 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 ASSERT(log_offset <= iclog->ic_size - 1);
1840 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1841
1842 /* start_lsn is the first lsn written to. That's all we need. */
1843 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001844 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 /* This loop writes out as many regions as can fit in the amount
1847 * of space which was allocated by xlog_state_get_iclog_space().
1848 */
1849 while (index < nentries) {
1850 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1851 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1852 start_rec_copy = 0;
1853
1854 /* If first write for transaction, insert start record.
1855 * We can't be trying to commit if we are inited. We can't
1856 * have any "partial_copy" if we are inited.
1857 */
1858 if (ticket->t_flags & XLOG_TIC_INITED) {
1859 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001860 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 logop_head->oh_clientid = ticket->t_clientid;
1862 logop_head->oh_len = 0;
1863 logop_head->oh_flags = XLOG_START_TRANS;
1864 logop_head->oh_res2 = 0;
1865 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1866 record_cnt++;
1867
1868 start_rec_copy = sizeof(xlog_op_header_t);
1869 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1870 }
1871
1872 /* Copy log operation header directly into data section */
1873 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001874 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 logop_head->oh_clientid = ticket->t_clientid;
1876 logop_head->oh_res2 = 0;
1877
1878 /* header copied directly */
1879 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1880
1881 /* are we copying a commit or unmount record? */
1882 logop_head->oh_flags = flags;
1883
1884 /*
1885 * We've seen logs corrupted with bad transaction client
1886 * ids. This makes sure that XFS doesn't generate them on.
1887 * Turn this into an EIO and shut down the filesystem.
1888 */
1889 switch (logop_head->oh_clientid) {
1890 case XFS_TRANSACTION:
1891 case XFS_VOLUME:
1892 case XFS_LOG:
1893 break;
1894 default:
1895 xfs_fs_cmn_err(CE_WARN, mp,
1896 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1897 logop_head->oh_clientid, tic);
1898 return XFS_ERROR(EIO);
1899 }
1900
1901 /* Partial write last time? => (partial_copy != 0)
1902 * need_copy is the amount we'd like to copy if everything could
1903 * fit in the current memcpy.
1904 */
1905 need_copy = reg[index].i_len - partial_copy_len;
1906
1907 copy_off = partial_copy_len;
1908 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001909 copy_len = need_copy;
1910 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (partial_copy)
1912 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1913 partial_copy_len = partial_copy = 0;
1914 } else { /* partial write */
1915 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001916 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1918 if (partial_copy)
1919 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1920 partial_copy_len += copy_len;
1921 partial_copy++;
1922 len += sizeof(xlog_op_header_t); /* from splitting of region */
1923 /* account for new log op header */
1924 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001925 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927 xlog_verify_dest_ptr(log, ptr);
1928
1929 /* copy region */
1930 ASSERT(copy_len >= 0);
1931 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1932 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1933
1934 /* make copy_len total bytes copied, including headers */
1935 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1936 record_cnt++;
1937 data_cnt += contwr ? copy_len : 0;
1938 if (partial_copy) { /* copied partial region */
1939 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1940 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1941 record_cnt = data_cnt = 0;
1942 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 break; /* don't increment index */
1945 } else { /* copied entire region */
1946 index++;
1947 partial_copy_len = partial_copy = 0;
1948
1949 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1950 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1951 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001952 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001954 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (commit_iclog) {
1956 ASSERT(flags & XLOG_COMMIT_TRANS);
1957 *commit_iclog = iclog;
1958 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001959 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (index == nentries)
1961 return 0; /* we are done */
1962 else
1963 break;
1964 }
1965 } /* if (partial_copy) */
1966 } /* while (index < nentries) */
1967 } /* for (index = 0; index < nentries; ) */
1968 ASSERT(len == 0);
1969
1970 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1971 if (commit_iclog) {
1972 ASSERT(flags & XLOG_COMMIT_TRANS);
1973 *commit_iclog = iclog;
1974 return 0;
1975 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001976 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977} /* xlog_write */
1978
1979
1980/*****************************************************************************
1981 *
1982 * State Machine functions
1983 *
1984 *****************************************************************************
1985 */
1986
1987/* Clean iclogs starting from the head. This ordering must be
1988 * maintained, so an iclog doesn't become ACTIVE beyond one that
1989 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001990 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 * iclog is trying to sync to disk.
1992 *
1993 * State Change: DIRTY -> ACTIVE
1994 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001995STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996xlog_state_clean_log(xlog_t *log)
1997{
1998 xlog_in_core_t *iclog;
1999 int changed = 0;
2000
2001 iclog = log->l_iclog;
2002 do {
2003 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2004 iclog->ic_state = XLOG_STATE_ACTIVE;
2005 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002006 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
2008 * If the number of ops in this iclog indicate it just
2009 * contains the dummy transaction, we can
2010 * change state into IDLE (the second time around).
2011 * Otherwise we should change the state into
2012 * NEED a dummy.
2013 * We don't need to cover the dummy.
2014 */
2015 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002016 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2017 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 changed = 1;
2019 } else {
2020 /*
2021 * We have two dirty iclogs so start over
2022 * This could also be num of ops indicates
2023 * this is not the dummy going out.
2024 */
2025 changed = 2;
2026 }
2027 iclog->ic_header.h_num_logops = 0;
2028 memset(iclog->ic_header.h_cycle_data, 0,
2029 sizeof(iclog->ic_header.h_cycle_data));
2030 iclog->ic_header.h_lsn = 0;
2031 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2032 /* do nothing */;
2033 else
2034 break; /* stop cleaning */
2035 iclog = iclog->ic_next;
2036 } while (iclog != log->l_iclog);
2037
2038 /* log is locked when we are called */
2039 /*
2040 * Change state for the dummy log recording.
2041 * We usually go to NEED. But we go to NEED2 if the changed indicates
2042 * we are done writing the dummy record.
2043 * If we are done with the second dummy recored (DONE2), then
2044 * we go to IDLE.
2045 */
2046 if (changed) {
2047 switch (log->l_covered_state) {
2048 case XLOG_STATE_COVER_IDLE:
2049 case XLOG_STATE_COVER_NEED:
2050 case XLOG_STATE_COVER_NEED2:
2051 log->l_covered_state = XLOG_STATE_COVER_NEED;
2052 break;
2053
2054 case XLOG_STATE_COVER_DONE:
2055 if (changed == 1)
2056 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2057 else
2058 log->l_covered_state = XLOG_STATE_COVER_NEED;
2059 break;
2060
2061 case XLOG_STATE_COVER_DONE2:
2062 if (changed == 1)
2063 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2064 else
2065 log->l_covered_state = XLOG_STATE_COVER_NEED;
2066 break;
2067
2068 default:
2069 ASSERT(0);
2070 }
2071 }
2072} /* xlog_state_clean_log */
2073
2074STATIC xfs_lsn_t
2075xlog_get_lowest_lsn(
2076 xlog_t *log)
2077{
2078 xlog_in_core_t *lsn_log;
2079 xfs_lsn_t lowest_lsn, lsn;
2080
2081 lsn_log = log->l_iclog;
2082 lowest_lsn = 0;
2083 do {
2084 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002085 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if ((lsn && !lowest_lsn) ||
2087 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2088 lowest_lsn = lsn;
2089 }
2090 }
2091 lsn_log = lsn_log->ic_next;
2092 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002093 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
2096
2097STATIC void
2098xlog_state_do_callback(
2099 xlog_t *log,
2100 int aborted,
2101 xlog_in_core_t *ciclog)
2102{
2103 xlog_in_core_t *iclog;
2104 xlog_in_core_t *first_iclog; /* used to know when we've
2105 * processed all iclogs once */
2106 xfs_log_callback_t *cb, *cb_next;
2107 int flushcnt = 0;
2108 xfs_lsn_t lowest_lsn;
2109 int ioerrors; /* counter: iclogs with errors */
2110 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2111 int funcdidcallbacks; /* flag: function did callbacks */
2112 int repeats; /* for issuing console warnings if
2113 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002114 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002116 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 first_iclog = iclog = log->l_iclog;
2118 ioerrors = 0;
2119 funcdidcallbacks = 0;
2120 repeats = 0;
2121
2122 do {
2123 /*
2124 * Scan all iclogs starting with the one pointed to by the
2125 * log. Reset this starting point each time the log is
2126 * unlocked (during callbacks).
2127 *
2128 * Keep looping through iclogs until one full pass is made
2129 * without running any callbacks.
2130 */
2131 first_iclog = log->l_iclog;
2132 iclog = log->l_iclog;
2133 loopdidcallbacks = 0;
2134 repeats++;
2135
2136 do {
2137
2138 /* skip all iclogs in the ACTIVE & DIRTY states */
2139 if (iclog->ic_state &
2140 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2141 iclog = iclog->ic_next;
2142 continue;
2143 }
2144
2145 /*
2146 * Between marking a filesystem SHUTDOWN and stopping
2147 * the log, we do flush all iclogs to disk (if there
2148 * wasn't a log I/O error). So, we do want things to
2149 * go smoothly in case of just a SHUTDOWN w/o a
2150 * LOG_IO_ERROR.
2151 */
2152 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2153 /*
2154 * Can only perform callbacks in order. Since
2155 * this iclog is not in the DONE_SYNC/
2156 * DO_CALLBACK state, we skip the rest and
2157 * just try to clean up. If we set our iclog
2158 * to DO_CALLBACK, we will not process it when
2159 * we retry since a previous iclog is in the
2160 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002161 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 */
2163 if (!(iclog->ic_state &
2164 (XLOG_STATE_DONE_SYNC |
2165 XLOG_STATE_DO_CALLBACK))) {
2166 if (ciclog && (ciclog->ic_state ==
2167 XLOG_STATE_DONE_SYNC)) {
2168 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2169 }
2170 break;
2171 }
2172 /*
2173 * We now have an iclog that is in either the
2174 * DO_CALLBACK or DONE_SYNC states. The other
2175 * states (WANT_SYNC, SYNCING, or CALLBACK were
2176 * caught by the above if and are going to
2177 * clean (i.e. we aren't doing their callbacks)
2178 * see the above if.
2179 */
2180
2181 /*
2182 * We will do one more check here to see if we
2183 * have chased our tail around.
2184 */
2185
2186 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002187 if (lowest_lsn &&
2188 XFS_LSN_CMP(lowest_lsn,
2189 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 iclog = iclog->ic_next;
2191 continue; /* Leave this iclog for
2192 * another thread */
2193 }
2194
2195 iclog->ic_state = XLOG_STATE_CALLBACK;
2196
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002197 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002200 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 * No one else can be here except us.
2202 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002203 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002204 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2205 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2206 log->l_last_sync_lsn =
2207 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002208 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002211 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 ioerrors++;
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
David Chinner114d23a2008-04-10 12:18:39 +10002215 /*
2216 * Keep processing entries in the callback list until
2217 * we come around and it is empty. We need to
2218 * atomically see that the list is empty and change the
2219 * state to DIRTY so that we don't miss any more
2220 * callbacks being added.
2221 */
2222 spin_lock(&iclog->ic_callback_lock);
2223 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002224 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 iclog->ic_callback_tail = &(iclog->ic_callback);
2226 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002227 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002230 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 cb_next = cb->cb_next;
2232 cb->cb_func(cb->cb_arg, aborted);
2233 }
David Chinner114d23a2008-04-10 12:18:39 +10002234 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 cb = iclog->ic_callback;
2236 }
2237
2238 loopdidcallbacks++;
2239 funcdidcallbacks++;
2240
David Chinner114d23a2008-04-10 12:18:39 +10002241 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002242 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002243 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2245 iclog->ic_state = XLOG_STATE_DIRTY;
2246
2247 /*
2248 * Transition from DIRTY to ACTIVE if applicable.
2249 * NOP if STATE_IOERROR.
2250 */
2251 xlog_state_clean_log(log);
2252
2253 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002254 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 iclog = iclog->ic_next;
2257 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002258
2259 if (repeats > 5000) {
2260 flushcnt += repeats;
2261 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002263 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002264 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266 } while (!ioerrors && loopdidcallbacks);
2267
2268 /*
2269 * make one last gasp attempt to see if iclogs are being left in
2270 * limbo..
2271 */
2272#ifdef DEBUG
2273 if (funcdidcallbacks) {
2274 first_iclog = iclog = log->l_iclog;
2275 do {
2276 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2277 /*
2278 * Terminate the loop if iclogs are found in states
2279 * which will cause other threads to clean up iclogs.
2280 *
2281 * SYNCING - i/o completion will go through logs
2282 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002283 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 * IOERROR - give up hope all ye who enter here
2285 */
2286 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2287 iclog->ic_state == XLOG_STATE_SYNCING ||
2288 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2289 iclog->ic_state == XLOG_STATE_IOERROR )
2290 break;
2291 iclog = iclog->ic_next;
2292 } while (first_iclog != iclog);
2293 }
2294#endif
2295
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002296 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2297 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002298 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002299
2300 if (wake)
2301 sv_broadcast(&log->l_flush_wait);
2302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304
2305/*
2306 * Finish transitioning this iclog to the dirty state.
2307 *
2308 * Make sure that we completely execute this routine only when this is
2309 * the last call to the iclog. There is a good chance that iclog flushes,
2310 * when we reach the end of the physical log, get turned into 2 separate
2311 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2312 * routine. By using the reference count bwritecnt, we guarantee that only
2313 * the second completion goes through.
2314 *
2315 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002316 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 */
David Chinnera8272ce2007-11-23 16:28:09 +11002318STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319xlog_state_done_syncing(
2320 xlog_in_core_t *iclog,
2321 int aborted)
2322{
2323 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002325 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2328 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002329 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2331
2332
2333 /*
2334 * If we got an error, either on the first buffer, or in the case of
2335 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2336 * and none should ever be attempted to be written to disk
2337 * again.
2338 */
2339 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2340 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002341 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return;
2343 }
2344 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2345 }
2346
2347 /*
2348 * Someone could be sleeping prior to writing out the next
2349 * iclog buffer, we wake them all, one will get to do the
2350 * I/O, the others get to wait for the result.
2351 */
David Chinner12017fa2008-08-13 16:34:31 +10002352 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002353 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2355} /* xlog_state_done_syncing */
2356
2357
2358/*
2359 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002360 * sleep. We wait on the flush queue on the head iclog as that should be
2361 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2362 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 *
2364 * The in-core logs are used in a circular fashion. They are not used
2365 * out-of-order even when an iclog past the head is free.
2366 *
2367 * return:
2368 * * log_offset where xlog_write() can start writing into the in-core
2369 * log's data space.
2370 * * in-core log pointer to which xlog_write() should write.
2371 * * boolean indicating this is a continued write to an in-core log.
2372 * If this is the last write, then the in-core log's offset field
2373 * needs to be incremented, depending on the amount of data which
2374 * is copied.
2375 */
David Chinnera8272ce2007-11-23 16:28:09 +11002376STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377xlog_state_get_iclog_space(xlog_t *log,
2378 int len,
2379 xlog_in_core_t **iclogp,
2380 xlog_ticket_t *ticket,
2381 int *continued_write,
2382 int *logoffsetp)
2383{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 int log_offset;
2385 xlog_rec_header_t *head;
2386 xlog_in_core_t *iclog;
2387 int error;
2388
2389restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002390 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002392 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return XFS_ERROR(EIO);
2394 }
2395
2396 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002397 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2399 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002400
2401 /* Wait for log writes to have flushed */
2402 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 goto restart;
2404 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 head = &iclog->ic_header;
2407
David Chinner155cc6b2008-03-06 13:44:14 +11002408 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 log_offset = iclog->ic_offset;
2410
2411 /* On the 1st write to an iclog, figure out lsn. This works
2412 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2413 * committing to. If the offset is set, that's how many blocks
2414 * must be written.
2415 */
2416 if (log_offset == 0) {
2417 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002418 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002419 log->l_iclog_hsize,
2420 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002421 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2422 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002423 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 ASSERT(log->l_curr_block >= 0);
2425 }
2426
2427 /* If there is enough room to write everything, then do it. Otherwise,
2428 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2429 * bit is on, so this will get flushed out. Don't update ic_offset
2430 * until you know exactly how many bytes get copied. Therefore, wait
2431 * until later to update ic_offset.
2432 *
2433 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2434 * can fit into remaining data section.
2435 */
2436 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2437 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2438
Dave Chinner49641f12008-07-11 17:43:55 +10002439 /*
2440 * If I'm the only one writing to this iclog, sync it to disk.
2441 * We need to do an atomic compare and decrement here to avoid
2442 * racing with concurrent atomic_dec_and_lock() calls in
2443 * xlog_state_release_iclog() when there is more than one
2444 * reference to the iclog.
2445 */
2446 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2447 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002448 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002449 error = xlog_state_release_iclog(log, iclog);
2450 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002451 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002453 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455 goto restart;
2456 }
2457
2458 /* Do we have enough room to write the full amount in the remainder
2459 * of this iclog? Or must we continue a write on the next iclog and
2460 * mark this iclog as completely taken? In the case where we switch
2461 * iclogs (to mark it taken), this particular iclog will release/sync
2462 * to disk in xlog_write().
2463 */
2464 if (len <= iclog->ic_size - iclog->ic_offset) {
2465 *continued_write = 0;
2466 iclog->ic_offset += len;
2467 } else {
2468 *continued_write = 1;
2469 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2470 }
2471 *iclogp = iclog;
2472
2473 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002474 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 *logoffsetp = log_offset;
2477 return 0;
2478} /* xlog_state_get_iclog_space */
2479
2480/*
2481 * Atomically get the log space required for a log ticket.
2482 *
2483 * Once a ticket gets put onto the reserveq, it will only return after
2484 * the needed reservation is satisfied.
2485 */
2486STATIC int
2487xlog_grant_log_space(xlog_t *log,
2488 xlog_ticket_t *tic)
2489{
2490 int free_bytes;
2491 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492#ifdef DEBUG
2493 xfs_lsn_t tail_lsn;
2494#endif
2495
2496
2497#ifdef DEBUG
2498 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2499 panic("grant Recovery problem");
2500#endif
2501
2502 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002503 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2505
2506 /* something is already sleeping; insert new transaction at end */
2507 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002508 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 xlog_trace_loggrant(log, tic,
2510 "xlog_grant_log_space: sleep 1");
2511 /*
2512 * Gotta check this before going to sleep, while we're
2513 * holding the grant lock.
2514 */
2515 if (XLOG_FORCED_SHUTDOWN(log))
2516 goto error_return;
2517
2518 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002519 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /*
2521 * If we got an error, and the filesystem is shutting down,
2522 * we'll catch it down below. So just continue...
2523 */
2524 xlog_trace_loggrant(log, tic,
2525 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002526 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2529 need_bytes = tic->t_unit_res*tic->t_ocnt;
2530 else
2531 need_bytes = tic->t_unit_res;
2532
2533redo:
2534 if (XLOG_FORCED_SHUTDOWN(log))
2535 goto error_return;
2536
2537 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2538 log->l_grant_reserve_bytes);
2539 if (free_bytes < need_bytes) {
2540 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002541 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 xlog_trace_loggrant(log, tic,
2543 "xlog_grant_log_space: sleep 2");
2544 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002545 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002548 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 goto error_return;
2550 }
2551
2552 xlog_trace_loggrant(log, tic,
2553 "xlog_grant_log_space: wake 2");
2554 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002555 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 goto redo;
2557 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002558 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002561 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562#ifdef DEBUG
2563 tail_lsn = log->l_tail_lsn;
2564 /*
2565 * Check to make sure the grant write head didn't just over lap the
2566 * tail. If the cycles are the same, we can't be overlapping.
2567 * Otherwise, make sure that the cycles differ by exactly one and
2568 * check the byte count.
2569 */
2570 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2571 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2572 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2573 }
2574#endif
2575 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2576 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002577 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return 0;
2579
2580 error_return:
2581 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002582 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2584 /*
2585 * If we are failing, make sure the ticket doesn't have any
2586 * current reservations. We don't want to add this back when
2587 * the ticket/transaction gets cancelled.
2588 */
2589 tic->t_curr_res = 0;
2590 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002591 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return XFS_ERROR(EIO);
2593} /* xlog_grant_log_space */
2594
2595
2596/*
2597 * Replenish the byte reservation required by moving the grant write head.
2598 *
2599 *
2600 */
2601STATIC int
2602xlog_regrant_write_log_space(xlog_t *log,
2603 xlog_ticket_t *tic)
2604{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 int free_bytes, need_bytes;
2606 xlog_ticket_t *ntic;
2607#ifdef DEBUG
2608 xfs_lsn_t tail_lsn;
2609#endif
2610
2611 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002612 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617#ifdef DEBUG
2618 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2619 panic("regrant Recovery problem");
2620#endif
2621
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002622 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2624
2625 if (XLOG_FORCED_SHUTDOWN(log))
2626 goto error_return;
2627
2628 /* If there are other waiters on the queue then give them a
2629 * chance at logspace before us. Wake up the first waiters,
2630 * if we do not wake up all the waiters then go to sleep waiting
2631 * for more free space, otherwise try to get some space for
2632 * this transaction.
2633 */
2634
2635 if ((ntic = log->l_write_headq)) {
2636 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2637 log->l_grant_write_bytes);
2638 do {
2639 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2640
2641 if (free_bytes < ntic->t_unit_res)
2642 break;
2643 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002644 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 ntic = ntic->t_next;
2646 } while (ntic != log->l_write_headq);
2647
2648 if (ntic != log->l_write_headq) {
2649 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002650 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 xlog_trace_loggrant(log, tic,
2653 "xlog_regrant_write_log_space: sleep 1");
2654 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002655 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 &log->l_grant_lock, s);
2657
2658 /* If we're shutting down, this tic is already
2659 * off the queue */
2660 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002661 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 goto error_return;
2663 }
2664
2665 xlog_trace_loggrant(log, tic,
2666 "xlog_regrant_write_log_space: wake 1");
2667 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002668 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 }
2671
2672 need_bytes = tic->t_unit_res;
2673
2674redo:
2675 if (XLOG_FORCED_SHUTDOWN(log))
2676 goto error_return;
2677
2678 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2679 log->l_grant_write_bytes);
2680 if (free_bytes < need_bytes) {
2681 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002682 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002684 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 /* If we're shutting down, this tic is already off the queue */
2687 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002688 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 goto error_return;
2690 }
2691
2692 xlog_trace_loggrant(log, tic,
2693 "xlog_regrant_write_log_space: wake 2");
2694 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002695 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 goto redo;
2697 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002698 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002700 /* we've got enough space */
2701 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702#ifdef DEBUG
2703 tail_lsn = log->l_tail_lsn;
2704 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2705 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2706 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2707 }
2708#endif
2709
2710 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2711 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002712 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715
2716 error_return:
2717 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002718 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2720 /*
2721 * If we are failing, make sure the ticket doesn't have any
2722 * current reservations. We don't want to add this back when
2723 * the ticket/transaction gets cancelled.
2724 */
2725 tic->t_curr_res = 0;
2726 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002727 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return XFS_ERROR(EIO);
2729} /* xlog_regrant_write_log_space */
2730
2731
2732/* The first cnt-1 times through here we don't need to
2733 * move the grant write head because the permanent
2734 * reservation has reserved cnt times the unit amount.
2735 * Release part of current permanent unit reservation and
2736 * reset current reservation to be one units worth. Also
2737 * move grant reservation head forward.
2738 */
2739STATIC void
2740xlog_regrant_reserve_log_space(xlog_t *log,
2741 xlog_ticket_t *ticket)
2742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 xlog_trace_loggrant(log, ticket,
2744 "xlog_regrant_reserve_log_space: enter");
2745 if (ticket->t_cnt > 0)
2746 ticket->t_cnt--;
2747
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002748 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002749 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002751 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 xlog_trace_loggrant(log, ticket,
2753 "xlog_regrant_reserve_log_space: sub current res");
2754 xlog_verify_grant_head(log, 1);
2755
2756 /* just return if we still have some of the pre-reserved space */
2757 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002758 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return;
2760 }
2761
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002762 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 xlog_trace_loggrant(log, ticket,
2764 "xlog_regrant_reserve_log_space: exit");
2765 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002766 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002768 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769} /* xlog_regrant_reserve_log_space */
2770
2771
2772/*
2773 * Give back the space left from a reservation.
2774 *
2775 * All the information we need to make a correct determination of space left
2776 * is present. For non-permanent reservations, things are quite easy. The
2777 * count should have been decremented to zero. We only need to deal with the
2778 * space remaining in the current reservation part of the ticket. If the
2779 * ticket contains a permanent reservation, there may be left over space which
2780 * needs to be released. A count of N means that N-1 refills of the current
2781 * reservation can be done before we need to ask for more space. The first
2782 * one goes to fill up the first current reservation. Once we run out of
2783 * space, the count will stay at zero and the only space remaining will be
2784 * in the current reservation field.
2785 */
2786STATIC void
2787xlog_ungrant_log_space(xlog_t *log,
2788 xlog_ticket_t *ticket)
2789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (ticket->t_cnt > 0)
2791 ticket->t_cnt--;
2792
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002793 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2795
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002796 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2799
2800 /* If this is a permanent reservation ticket, we may be able to free
2801 * up more space based on the remaining count.
2802 */
2803 if (ticket->t_cnt > 0) {
2804 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002805 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807
2808 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2809 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002810 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 xfs_log_move_tail(log->l_mp, 1);
2812} /* xlog_ungrant_log_space */
2813
2814
2815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * Flush iclog to disk if this is the last reference to the given iclog and
2817 * the WANT_SYNC bit is set.
2818 *
2819 * When this function is entered, the iclog is not necessarily in the
2820 * WANT_SYNC state. It may be sitting around waiting to get filled.
2821 *
2822 *
2823 */
David Chinnera8272ce2007-11-23 16:28:09 +11002824STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002825xlog_state_release_iclog(
2826 xlog_t *log,
2827 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 int sync = 0; /* do we sync? */
2830
David Chinner155cc6b2008-03-06 13:44:14 +11002831 if (iclog->ic_state & XLOG_STATE_IOERROR)
2832 return XFS_ERROR(EIO);
2833
2834 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2835 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2836 return 0;
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002839 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return XFS_ERROR(EIO);
2841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2843 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2844
David Chinner155cc6b2008-03-06 13:44:14 +11002845 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002846 /* update tail before writing to iclog */
2847 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 sync++;
2849 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002850 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2852 /* cycle incremented when incrementing curr_block */
2853 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002854 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 /*
2857 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002858 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2860 * this iclog has consistent data, so we ignore IOERROR
2861 * flags after this point.
2862 */
David Chinnerb5893342008-03-06 13:44:06 +11002863 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866} /* xlog_state_release_iclog */
2867
2868
2869/*
2870 * This routine will mark the current iclog in the ring as WANT_SYNC
2871 * and move the current iclog pointer to the next iclog in the ring.
2872 * When this routine is called from xlog_state_get_iclog_space(), the
2873 * exact size of the iclog has not yet been determined. All we know is
2874 * that every data block. We have run out of space in this log record.
2875 */
2876STATIC void
2877xlog_state_switch_iclogs(xlog_t *log,
2878 xlog_in_core_t *iclog,
2879 int eventual_size)
2880{
2881 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2882 if (!eventual_size)
2883 eventual_size = iclog->ic_offset;
2884 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002885 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 log->l_prev_block = log->l_curr_block;
2887 log->l_prev_cycle = log->l_curr_cycle;
2888
2889 /* roll log?: ic_offset changed later */
2890 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2891
2892 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002893 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 log->l_mp->m_sb.sb_logsunit > 1) {
2895 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2896 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2897 }
2898
2899 if (log->l_curr_block >= log->l_logBBsize) {
2900 log->l_curr_cycle++;
2901 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2902 log->l_curr_cycle++;
2903 log->l_curr_block -= log->l_logBBsize;
2904 ASSERT(log->l_curr_block >= 0);
2905 }
2906 ASSERT(iclog == log->l_iclog);
2907 log->l_iclog = iclog->ic_next;
2908} /* xlog_state_switch_iclogs */
2909
2910
2911/*
2912 * Write out all data in the in-core log as of this exact moment in time.
2913 *
2914 * Data may be written to the in-core log during this call. However,
2915 * we don't guarantee this data will be written out. A change from past
2916 * implementation means this routine will *not* write out zero length LRs.
2917 *
2918 * Basically, we try and perform an intelligent scan of the in-core logs.
2919 * If we determine there is no flushable data, we just return. There is no
2920 * flushable data if:
2921 *
2922 * 1. the current iclog is active and has no data; the previous iclog
2923 * is in the active or dirty state.
2924 * 2. the current iclog is drity, and the previous iclog is in the
2925 * active or dirty state.
2926 *
David Chinner12017fa2008-08-13 16:34:31 +10002927 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 *
2929 * 1. the current iclog is not in the active nor dirty state.
2930 * 2. the current iclog dirty, and the previous iclog is not in the
2931 * active nor dirty state.
2932 * 3. the current iclog is active, and there is another thread writing
2933 * to this particular iclog.
2934 * 4. a) the current iclog is active and has no other writers
2935 * b) when we return from flushing out this iclog, it is still
2936 * not in the active nor dirty state.
2937 */
2938STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002939xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 xlog_in_core_t *iclog;
2942 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002944 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 iclog = log->l_iclog;
2947 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002948 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return XFS_ERROR(EIO);
2950 }
2951
2952 /* If the head iclog is not active nor dirty, we just attach
2953 * ourselves to the head and go to sleep.
2954 */
2955 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2956 iclog->ic_state == XLOG_STATE_DIRTY) {
2957 /*
2958 * If the head is dirty or (active and empty), then
2959 * we need to look at the previous iclog. If the previous
2960 * iclog is active or dirty we are done. There is nothing
2961 * to sync out. Otherwise, we attach ourselves to the
2962 * previous iclog and go to sleep.
2963 */
2964 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002965 (atomic_read(&iclog->ic_refcnt) == 0
2966 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 iclog = iclog->ic_prev;
2968 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2969 iclog->ic_state == XLOG_STATE_DIRTY)
2970 goto no_sleep;
2971 else
2972 goto maybe_sleep;
2973 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002974 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 /* We are the only one with access to this
2976 * iclog. Flush it out now. There should
2977 * be a roundoff of zero to show that someone
2978 * has already taken care of the roundoff from
2979 * the previous sync.
2980 */
David Chinner155cc6b2008-03-06 13:44:14 +11002981 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002982 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002984 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 if (xlog_state_release_iclog(log, iclog))
2987 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002988 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002989 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002990 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 iclog->ic_state != XLOG_STATE_DIRTY)
2992 goto maybe_sleep;
2993 else
2994 goto no_sleep;
2995 } else {
2996 /* Someone else is writing to this iclog.
2997 * Use its call to flush out the data. However,
2998 * the other thread may not force out this LR,
2999 * so we mark it WANT_SYNC.
3000 */
3001 xlog_state_switch_iclogs(log, iclog, 0);
3002 goto maybe_sleep;
3003 }
3004 }
3005 }
3006
3007 /* By the time we come around again, the iclog could've been filled
3008 * which would give it another lsn. If we have a new lsn, just
3009 * return because the relevant data has been flushed.
3010 */
3011maybe_sleep:
3012 if (flags & XFS_LOG_SYNC) {
3013 /*
3014 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003015 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 * Then we check again after waking up, in case our
3017 * sleep was disturbed by a bad news.
3018 */
3019 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003020 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return XFS_ERROR(EIO);
3022 }
3023 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003024 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 /*
3026 * No need to grab the log lock here since we're
3027 * only deciding whether or not to return EIO
3028 * and the memory read should be atomic.
3029 */
3030 if (iclog->ic_state & XLOG_STATE_IOERROR)
3031 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003032 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 } else {
3035
3036no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003037 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 return 0;
3040} /* xlog_state_sync_all */
3041
3042
3043/*
3044 * Used by code which implements synchronous log forces.
3045 *
3046 * Find in-core log with lsn.
3047 * If it is in the DIRTY state, just return.
3048 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3049 * state and go to sleep or return.
3050 * If it is in any other state, go to sleep or return.
3051 *
3052 * If filesystem activity goes to zero, the iclog will get flushed only by
3053 * bdflush().
3054 */
David Chinnera8272ce2007-11-23 16:28:09 +11003055STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056xlog_state_sync(xlog_t *log,
3057 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003058 uint flags,
3059 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 xlog_in_core_t *iclog;
3062 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003065 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 iclog = log->l_iclog;
3067
3068 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003069 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return XFS_ERROR(EIO);
3071 }
3072
3073 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003074 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3075 iclog = iclog->ic_next;
3076 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
3078
3079 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003080 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 0;
3082 }
3083
3084 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3085 /*
3086 * We sleep here if we haven't already slept (e.g.
3087 * this is the first time we've looked at the correct
3088 * iclog buf) and the buffer before us is going to
3089 * be sync'ed. The reason for this is that if we
3090 * are doing sync transactions here, by waiting for
3091 * the previous I/O to complete, we can allow a few
3092 * more transactions into this iclog before we close
3093 * it down.
3094 *
3095 * Otherwise, we mark the buffer WANT_SYNC, and bump
3096 * up the refcnt so we can release the log (which drops
3097 * the ref count). The state switch keeps new transaction
3098 * commits from using this buffer. When the current commits
3099 * finish writing into the buffer, the refcount will drop to
3100 * zero and the buffer will go out then.
3101 */
3102 if (!already_slept &&
3103 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3104 XLOG_STATE_SYNCING))) {
3105 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3106 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003107 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003109 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 already_slept = 1;
3111 goto try_again;
3112 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003113 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003115 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if (xlog_state_release_iclog(log, iclog))
3117 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003118 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003119 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121 }
3122
3123 if ((flags & XFS_LOG_SYNC) && /* sleep */
3124 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3125
3126 /*
David Chinner12017fa2008-08-13 16:34:31 +10003127 * Don't wait on completion if we know that we've
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 * gotten a log write error.
3129 */
3130 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003131 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return XFS_ERROR(EIO);
3133 }
3134 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003135 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /*
3137 * No need to grab the log lock here since we're
3138 * only deciding whether or not to return EIO
3139 * and the memory read should be atomic.
3140 */
3141 if (iclog->ic_state & XLOG_STATE_IOERROR)
3142 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003143 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003145 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
3147 return 0;
3148
3149 } while (iclog != log->l_iclog);
3150
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003151 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153} /* xlog_state_sync */
3154
3155
3156/*
3157 * Called when we want to mark the current iclog as being ready to sync to
3158 * disk.
3159 */
David Chinnera8272ce2007-11-23 16:28:09 +11003160STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3162{
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003163 ASSERT(spin_is_locked(&log->l_icloglock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3166 xlog_state_switch_iclogs(log, iclog, 0);
3167 } else {
3168 ASSERT(iclog->ic_state &
3169 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3170 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173
3174/*****************************************************************************
3175 *
3176 * TICKET functions
3177 *
3178 *****************************************************************************
3179 */
3180
3181/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003182 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003184void
3185xfs_log_ticket_put(
3186 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003188 ASSERT(atomic_read(&ticket->t_ref) > 0);
3189 if (atomic_dec_and_test(&ticket->t_ref)) {
3190 sv_destroy(&ticket->t_wait);
3191 kmem_zone_free(xfs_log_ticket_zone, ticket);
3192 }
3193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003195xlog_ticket_t *
3196xfs_log_ticket_get(
3197 xlog_ticket_t *ticket)
3198{
3199 ASSERT(atomic_read(&ticket->t_ref) > 0);
3200 atomic_inc(&ticket->t_ref);
3201 return ticket;
3202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003205 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 */
David Chinnera8272ce2007-11-23 16:28:09 +11003207STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003208xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 int unit_bytes,
3210 int cnt,
3211 char client,
3212 uint xflags)
3213{
3214 xlog_ticket_t *tic;
3215 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
David Chinnereb01c9c2008-04-10 12:18:46 +10003217 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3218 if (!tic)
3219 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 /*
3222 * Permanent reservations have up to 'cnt'-1 active log operations
3223 * in the log. A unit in this case is the amount of space for one
3224 * of these log operations. Normal reservations have a cnt of 1
3225 * and their unit amount is the total amount of space required.
3226 *
3227 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003228 * which occupy space in the on-disk log.
3229 *
3230 * Normal form of a transaction is:
3231 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3232 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3233 *
3234 * We need to account for all the leadup data and trailer data
3235 * around the transaction data.
3236 * And then we need to account for the worst case in terms of using
3237 * more space.
3238 * The worst case will happen if:
3239 * - the placement of the transaction happens to be such that the
3240 * roundoff is at its maximum
3241 * - the transaction data is synced before the commit record is synced
3242 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3243 * Therefore the commit record is in its own Log Record.
3244 * This can happen as the commit record is called with its
3245 * own region to xlog_write().
3246 * This then means that in the worst case, roundoff can happen for
3247 * the commit-rec as well.
3248 * The commit-rec is smaller than padding in this scenario and so it is
3249 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 */
3251
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003252 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003254 unit_bytes += sizeof(xfs_trans_header_t);
3255
3256 /* for start-rec */
3257 unit_bytes += sizeof(xlog_op_header_t);
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 /* for LR headers */
3260 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3261 unit_bytes += log->l_iclog_hsize * num_headers;
3262
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003263 /* for commit-rec LR header - note: padding will subsume the ophdr */
3264 unit_bytes += log->l_iclog_hsize;
3265
3266 /* for split-recs - ophdrs added when data split over LRs */
3267 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3268
3269 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003270 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003271 log->l_mp->m_sb.sb_logsunit > 1) {
3272 /* log su roundoff */
3273 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3274 } else {
3275 /* BB roundoff */
3276 unit_bytes += 2*BBSIZE;
3277 }
3278
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003279 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 tic->t_unit_res = unit_bytes;
3281 tic->t_curr_res = unit_bytes;
3282 tic->t_cnt = cnt;
3283 tic->t_ocnt = cnt;
3284 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3285 tic->t_clientid = client;
3286 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003287 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 if (xflags & XFS_LOG_PERM_RESERV)
3289 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003290 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Christoph Hellwig0adba532007-08-30 17:21:46 +10003292 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297
3298/******************************************************************************
3299 *
3300 * Log debug routines
3301 *
3302 ******************************************************************************
3303 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003304#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305/*
3306 * Make sure that the destination ptr is within the valid data region of
3307 * one of the iclogs. This uses backup pointers stored in a different
3308 * part of the log in case we trash the log structure.
3309 */
3310void
3311xlog_verify_dest_ptr(xlog_t *log,
3312 __psint_t ptr)
3313{
3314 int i;
3315 int good_ptr = 0;
3316
3317 for (i=0; i < log->l_iclog_bufs; i++) {
3318 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3319 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3320 good_ptr++;
3321 }
3322 if (! good_ptr)
3323 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3324} /* xlog_verify_dest_ptr */
3325
3326STATIC void
3327xlog_verify_grant_head(xlog_t *log, int equals)
3328{
3329 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3330 if (equals)
3331 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3332 else
3333 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3334 } else {
3335 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3336 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3337 }
3338} /* xlog_verify_grant_head */
3339
3340/* check if it will fit */
3341STATIC void
3342xlog_verify_tail_lsn(xlog_t *log,
3343 xlog_in_core_t *iclog,
3344 xfs_lsn_t tail_lsn)
3345{
3346 int blocks;
3347
3348 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3349 blocks =
3350 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3351 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3352 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3353 } else {
3354 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3355
3356 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3357 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3358
3359 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3360 if (blocks < BTOBB(iclog->ic_offset) + 1)
3361 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3362 }
3363} /* xlog_verify_tail_lsn */
3364
3365/*
3366 * Perform a number of checks on the iclog before writing to disk.
3367 *
3368 * 1. Make sure the iclogs are still circular
3369 * 2. Make sure we have a good magic number
3370 * 3. Make sure we don't have magic numbers in the data
3371 * 4. Check fields of each log operation header for:
3372 * A. Valid client identifier
3373 * B. tid ptr value falls in valid ptr space (user space code)
3374 * C. Length in log record header is correct according to the
3375 * individual operation headers within record.
3376 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3377 * log, check the preceding blocks of the physical log to make sure all
3378 * the cycle numbers agree with the current cycle number.
3379 */
3380STATIC void
3381xlog_verify_iclog(xlog_t *log,
3382 xlog_in_core_t *iclog,
3383 int count,
3384 boolean_t syncing)
3385{
3386 xlog_op_header_t *ophead;
3387 xlog_in_core_t *icptr;
3388 xlog_in_core_2_t *xhdr;
3389 xfs_caddr_t ptr;
3390 xfs_caddr_t base_ptr;
3391 __psint_t field_offset;
3392 __uint8_t clientid;
3393 int len, i, j, k, op_len;
3394 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
3396 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003397 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 icptr = log->l_iclog;
3399 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003400 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 xlog_panic("xlog_verify_iclog: invalid ptr");
3402 icptr = icptr->ic_next;
3403 }
3404 if (icptr != log->l_iclog)
3405 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003406 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003409 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 xlog_panic("xlog_verify_iclog: invalid magic num");
3411
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003412 ptr = (xfs_caddr_t) &iclog->ic_header;
3413 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003415 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 xlog_panic("xlog_verify_iclog: unexpected magic num");
3417 }
3418
3419 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003420 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 ptr = iclog->ic_datap;
3422 base_ptr = ptr;
3423 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003424 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 for (i = 0; i < len; i++) {
3426 ophead = (xlog_op_header_t *)ptr;
3427
3428 /* clientid is only 1 byte */
3429 field_offset = (__psint_t)
3430 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3431 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3432 clientid = ophead->oh_clientid;
3433 } else {
3434 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3435 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3436 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3437 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003438 clientid = xlog_get_client_id(
3439 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003441 clientid = xlog_get_client_id(
3442 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444 }
3445 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003446 cmn_err(CE_WARN, "xlog_verify_iclog: "
3447 "invalid clientid %d op 0x%p offset 0x%lx",
3448 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 /* check length */
3451 field_offset = (__psint_t)
3452 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3453 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003454 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 } else {
3456 idx = BTOBBT((__psint_t)&ophead->oh_len -
3457 (__psint_t)iclog->ic_datap);
3458 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3459 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3460 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003461 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003463 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
3465 }
3466 ptr += sizeof(xlog_op_header_t) + op_len;
3467 }
3468} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003472 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 */
3474STATIC int
3475xlog_state_ioerror(
3476 xlog_t *log)
3477{
3478 xlog_in_core_t *iclog, *ic;
3479
3480 iclog = log->l_iclog;
3481 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3482 /*
3483 * Mark all the incore logs IOERROR.
3484 * From now on, no log flushes will result.
3485 */
3486 ic = iclog;
3487 do {
3488 ic->ic_state = XLOG_STATE_IOERROR;
3489 ic = ic->ic_next;
3490 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
3493 /*
3494 * Return non-zero, if state transition has already happened.
3495 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003496 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
3499/*
3500 * This is called from xfs_force_shutdown, when we're forcibly
3501 * shutting down the filesystem, typically because of an IO error.
3502 * Our main objectives here are to make sure that:
3503 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3504 * parties to find out, 'atomically'.
3505 * b. those who're sleeping on log reservations, pinned objects and
3506 * other resources get woken up, and be told the bad news.
3507 * c. nothing new gets queued up after (a) and (b) are done.
3508 * d. if !logerror, flush the iclogs to disk, then seal them off
3509 * for business.
3510 */
3511int
3512xfs_log_force_umount(
3513 struct xfs_mount *mp,
3514 int logerror)
3515{
3516 xlog_ticket_t *tic;
3517 xlog_t *log;
3518 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003519 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 log = mp->m_log;
3522
3523 /*
3524 * If this happens during log recovery, don't worry about
3525 * locking; the log isn't open for business yet.
3526 */
3527 if (!log ||
3528 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3529 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003530 if (mp->m_sb_bp)
3531 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 }
3534
3535 /*
3536 * Somebody could've already done the hard work for us.
3537 * No need to get locks for this.
3538 */
3539 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3540 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003541 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543 retval = 0;
3544 /*
3545 * We must hold both the GRANT lock and the LOG lock,
3546 * before we mark the filesystem SHUTDOWN and wake
3547 * everybody up to tell the bad news.
3548 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003549 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003550 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003552 if (mp->m_sb_bp)
3553 XFS_BUF_DONE(mp->m_sb_bp);
3554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 /*
3556 * This flag is sort of redundant because of the mount flag, but
3557 * it's good to maintain the separation between the log and the rest
3558 * of XFS.
3559 */
3560 log->l_flags |= XLOG_IO_ERROR;
3561
3562 /*
3563 * If we hit a log error, we want to mark all the iclogs IOERROR
3564 * while we're still holding the loglock.
3565 */
3566 if (logerror)
3567 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003568 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 /*
3571 * We don't want anybody waiting for log reservations
3572 * after this. That means we have to wake up everybody
3573 * queued up on reserve_headq as well as write_headq.
3574 * In addition, we make sure in xlog_{re}grant_log_space
3575 * that we don't enqueue anything once the SHUTDOWN flag
3576 * is set, and this action is protected by the GRANTLOCK.
3577 */
3578 if ((tic = log->l_reserve_headq)) {
3579 do {
David Chinner12017fa2008-08-13 16:34:31 +10003580 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 tic = tic->t_next;
3582 } while (tic != log->l_reserve_headq);
3583 }
3584
3585 if ((tic = log->l_write_headq)) {
3586 do {
David Chinner12017fa2008-08-13 16:34:31 +10003587 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 tic = tic->t_next;
3589 } while (tic != log->l_write_headq);
3590 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003591 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
3593 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3594 ASSERT(!logerror);
3595 /*
3596 * Force the incore logs to disk before shutting the
3597 * log down completely.
3598 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003599 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003600 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003602 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
3604 /*
3605 * Wake up everybody waiting on xfs_log_force.
3606 * Callback all log item committed functions as if the
3607 * log writes were completed.
3608 */
3609 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3610
3611#ifdef XFSERRORDEBUG
3612 {
3613 xlog_in_core_t *iclog;
3614
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003615 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 iclog = log->l_iclog;
3617 do {
3618 ASSERT(iclog->ic_callback == 0);
3619 iclog = iclog->ic_next;
3620 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003621 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
3623#endif
3624 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003625 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003628STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629xlog_iclogs_empty(xlog_t *log)
3630{
3631 xlog_in_core_t *iclog;
3632
3633 iclog = log->l_iclog;
3634 do {
3635 /* endianness does not matter here, zero is zero in
3636 * any language.
3637 */
3638 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 iclog = iclog->ic_next;
3641 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003642 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}