blob: c8f300897728efc0c3b003ca7fda45cb00e30de1 [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/*
638 * Unmount processing for the log.
639 */
640int
641xfs_log_unmount(xfs_mount_t *mp)
642{
643 int error;
644
645 error = xfs_log_unmount_write(mp);
646 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
651 * Final log writes as part of unmount.
652 *
653 * Mark the filesystem clean as unmount happens. Note that during relocation
654 * this routine needs to be executed as part of source-bag while the
655 * deallocation must not be done until source-end.
656 */
657
658/*
659 * Unmount record used to have a string "Unmount filesystem--" in the
660 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
661 * We just write the magic number now since that particular field isn't
662 * currently architecture converted and "nUmount" is a bit foo.
663 * As far as I know, there weren't any dependencies on the old behaviour.
664 */
665
666int
667xfs_log_unmount_write(xfs_mount_t *mp)
668{
669 xlog_t *log = mp->m_log;
670 xlog_in_core_t *iclog;
671#ifdef DEBUG
672 xlog_in_core_t *first_iclog;
673#endif
674 xfs_log_iovec_t reg[1];
675 xfs_log_ticket_t tic = NULL;
676 xfs_lsn_t lsn;
677 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* the data section must be 32 bit size aligned */
680 struct {
681 __uint16_t magic;
682 __uint16_t pad1;
683 __uint32_t pad2; /* may as well make it 64 bits */
684 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * Don't write out unmount record on read-only mounts.
688 * Or, if we are doing a forced umount (typically because of IO errors).
689 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000690 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692
David Chinnerb911ca02008-04-10 12:24:30 +1000693 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
694 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696#ifdef DEBUG
697 first_iclog = iclog = log->l_iclog;
698 do {
699 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
700 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
701 ASSERT(iclog->ic_offset == 0);
702 }
703 iclog = iclog->ic_next;
704 } while (iclog != first_iclog);
705#endif
706 if (! (XLOG_FORCED_SHUTDOWN(log))) {
707 reg[0].i_addr = (void*)&magic;
708 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000709 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Tim Shimmin955e47a2006-09-28 11:04:16 +1000711 error = xfs_log_reserve(mp, 600, 1, &tic,
712 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!error) {
714 /* remove inited flag */
715 ((xlog_ticket_t *)tic)->t_flags = 0;
716 error = xlog_write(mp, reg, 1, tic, &lsn,
717 NULL, XLOG_UNMOUNT_TRANS);
718 /*
719 * At this point, we're umounting anyway,
720 * so there's no point in transitioning log state
721 * to IOERROR. Just continue...
722 */
723 }
724
725 if (error) {
726 xfs_fs_cmn_err(CE_ALERT, mp,
727 "xfs_log_unmount: unmount record failed");
728 }
729
730
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000731 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100733 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100735 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000736 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000738 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
740 iclog->ic_state == XLOG_STATE_DIRTY)) {
741 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000742 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 &log->l_icloglock, s);
744 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000745 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000748 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000750 if (tic) {
751 xlog_trace_loggrant(log, tic, "unmount rec");
752 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100753 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else {
756 /*
757 * We're already in forced_shutdown mode, couldn't
758 * even attempt to write out the unmount transaction.
759 *
760 * Go through the motions of sync'ing and releasing
761 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000762 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * be in progress. Do this as a separate section of
764 * code so we'll know if we ever get stuck here that
765 * we're in this odd situation of trying to unmount
766 * a file system that went into forced_shutdown as
767 * the result of an unmount..
768 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000769 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100771 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100774 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000775 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000777 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
780 || iclog->ic_state == XLOG_STATE_DIRTY
781 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
782
David Chinner12017fa2008-08-13 16:34:31 +1000783 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 &log->l_icloglock, s);
785 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000786 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 }
789
David Chinner1bb7d6b2008-04-10 12:24:38 +1000790 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791} /* xfs_log_unmount_write */
792
793/*
794 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100795 *
796 * We need to stop the aild from running before we destroy
797 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
799void
800xfs_log_unmount_dealloc(xfs_mount_t *mp)
801{
David Chinner249a8c12008-02-05 12:13:32 +1100802 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000803 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806/*
807 * Write region vectors to log. The write happens using the space reservation
808 * of the ticket (tic). It is not a requirement that all writes for a given
809 * transaction occur with one call to xfs_log_write().
810 */
811int
812xfs_log_write(xfs_mount_t * mp,
813 xfs_log_iovec_t reg[],
814 int nentries,
815 xfs_log_ticket_t tic,
816 xfs_lsn_t *start_lsn)
817{
818 int error;
819 xlog_t *log = mp->m_log;
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (XLOG_FORCED_SHUTDOWN(log))
822 return XFS_ERROR(EIO);
823
824 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000825 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100827 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828} /* xfs_log_write */
829
830
831void
832xfs_log_move_tail(xfs_mount_t *mp,
833 xfs_lsn_t tail_lsn)
834{
835 xlog_ticket_t *tic;
836 xlog_t *log = mp->m_log;
837 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (XLOG_FORCED_SHUTDOWN(log))
840 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (tail_lsn == 0) {
843 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000844 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000846 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000849 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
852 * tail_lsn.
853 */
854 if (tail_lsn != 1) {
855 log->l_tail_lsn = tail_lsn;
856 }
857
858 if ((tic = log->l_write_headq)) {
859#ifdef DEBUG
860 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
861 panic("Recovery problem");
862#endif
863 cycle = log->l_grant_write_cycle;
864 bytes = log->l_grant_write_bytes;
865 free_bytes = xlog_space_left(log, cycle, bytes);
866 do {
867 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
868
869 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
870 break;
871 tail_lsn = 0;
872 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000873 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 tic = tic->t_next;
875 } while (tic != log->l_write_headq);
876 }
877 if ((tic = log->l_reserve_headq)) {
878#ifdef DEBUG
879 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
880 panic("Recovery problem");
881#endif
882 cycle = log->l_grant_reserve_cycle;
883 bytes = log->l_grant_reserve_bytes;
884 free_bytes = xlog_space_left(log, cycle, bytes);
885 do {
886 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
887 need_bytes = tic->t_unit_res*tic->t_cnt;
888 else
889 need_bytes = tic->t_unit_res;
890 if (free_bytes < need_bytes && tail_lsn != 1)
891 break;
892 tail_lsn = 0;
893 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000894 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 tic = tic->t_next;
896 } while (tic != log->l_reserve_headq);
897 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000898 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899} /* xfs_log_move_tail */
900
901/*
902 * Determine if we have a transaction that has gone to disk
903 * that needs to be covered. Log activity needs to be idle (no AIL and
904 * nothing in the iclogs). And, we need to be in the right state indicating
905 * something has gone out.
906 */
907int
908xfs_log_need_covered(xfs_mount_t *mp)
909{
David Chinner27d8d5f2008-10-30 17:38:39 +1100910 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
David Chinner92821e22007-05-24 15:26:31 +1000913 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return 0;
915
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000916 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
918 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
David Chinnera9c21c12008-10-30 17:39:35 +1100919 && !xfs_trans_ail_tail(log->l_ailp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 && xlog_iclogs_empty(log)) {
921 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
922 log->l_covered_state = XLOG_STATE_COVER_DONE;
923 else {
924 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
925 log->l_covered_state = XLOG_STATE_COVER_DONE2;
926 }
927 needed = 1;
928 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000929 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100930 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933/******************************************************************************
934 *
935 * local routines
936 *
937 ******************************************************************************
938 */
939
940/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
941 * The log manager must keep track of the last LR which was committed
942 * to disk. The lsn of this LR will become the new tail_lsn whenever
943 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
944 * the situation where stuff could be written into the log but nothing
945 * was ever in the AIL when asked. Eventually, we panic since the
946 * tail hits the head.
947 *
948 * We may be holding the log iclog lock upon entering this routine.
949 */
950xfs_lsn_t
951xlog_assign_tail_lsn(xfs_mount_t *mp)
952{
953 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 xlog_t *log = mp->m_log;
955
David Chinner5b00f142008-10-30 17:39:00 +1100956 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000957 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (tail_lsn != 0) {
959 log->l_tail_lsn = tail_lsn;
960 } else {
961 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
962 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000963 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 return tail_lsn;
966} /* xlog_assign_tail_lsn */
967
968
969/*
970 * Return the space in the log between the tail and the head. The head
971 * is passed in the cycle/bytes formal parms. In the special case where
972 * the reserve head has wrapped passed the tail, this calculation is no
973 * longer valid. In this case, just return 0 which means there is no space
974 * in the log. This works for all places where this function is called
975 * with the reserve head. Of course, if the write head were to ever
976 * wrap the tail, we should blow up. Rather than catch this case here,
977 * we depend on other ASSERTions in other parts of the code. XXXmiken
978 *
979 * This code also handles the case where the reservation head is behind
980 * the tail. The details of this case are described below, but the end
981 * result is that we return the size of the log as the amount of space left.
982 */
David Chinnera8272ce2007-11-23 16:28:09 +1100983STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984xlog_space_left(xlog_t *log, int cycle, int bytes)
985{
986 int free_bytes;
987 int tail_bytes;
988 int tail_cycle;
989
990 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
991 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
992 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
993 free_bytes = log->l_logsize - (bytes - tail_bytes);
994 } else if ((tail_cycle + 1) < cycle) {
995 return 0;
996 } else if (tail_cycle < cycle) {
997 ASSERT(tail_cycle == (cycle - 1));
998 free_bytes = tail_bytes - bytes;
999 } else {
1000 /*
1001 * The reservation head is behind the tail.
1002 * In this case we just want to return the size of the
1003 * log as the amount of space left.
1004 */
1005 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
1006 "xlog_space_left: head behind tail\n"
1007 " tail_cycle = %d, tail_bytes = %d\n"
1008 " GH cycle = %d, GH bytes = %d",
1009 tail_cycle, tail_bytes, cycle, bytes);
1010 ASSERT(0);
1011 free_bytes = log->l_logsize;
1012 }
1013 return free_bytes;
1014} /* xlog_space_left */
1015
1016
1017/*
1018 * Log function which is called when an io completes.
1019 *
1020 * The log manager needs its own routine, in order to control what
1021 * happens with the buffer after the write completes.
1022 */
1023void
1024xlog_iodone(xfs_buf_t *bp)
1025{
1026 xlog_in_core_t *iclog;
1027 xlog_t *l;
1028 int aborted;
1029
1030 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1031 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1032 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1033 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 l = iclog->ic_log;
1035
1036 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001037 * If the _XFS_BARRIER_FAILED flag was set by a lower
1038 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +10001039 * barrier I/O. Warn loudly and turn off barriers.
1040 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001041 if (bp->b_flags & _XFS_BARRIER_FAILED) {
1042 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001043 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1044 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1045 "xlog_iodone: Barriers are no longer supported"
1046 " by device. Disabling barriers\n");
1047 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1048 }
1049
1050 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * Race to shutdown the filesystem if we see an error.
1052 */
1053 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1054 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1055 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1056 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001057 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
1059 * This flag will be propagated to the trans-committed
1060 * callback routines to let them know that the log-commit
1061 * didn't succeed.
1062 */
1063 aborted = XFS_LI_ABORTED;
1064 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1065 aborted = XFS_LI_ABORTED;
1066 }
David Chinner3db296f2007-05-14 18:24:16 +10001067
1068 /* log I/O is always issued ASYNC */
1069 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001071 /*
1072 * do not reference the buffer (bp) here as we could race
1073 * with it being freed after writing the unmount record to the
1074 * log.
1075 */
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077} /* xlog_iodone */
1078
1079/*
1080 * The bdstrat callback function for log bufs. This gives us a central
1081 * place to trap bufs in case we get hit by a log I/O error and need to
1082 * shutdown. Actually, in practice, even when we didn't get a log error,
1083 * we transition the iclogs to IOERROR state *after* flushing all existing
1084 * iclogs to disk. This is because we don't want anymore new transactions to be
1085 * started or completed afterwards.
1086 */
1087STATIC int
1088xlog_bdstrat_cb(struct xfs_buf *bp)
1089{
1090 xlog_in_core_t *iclog;
1091
1092 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1093
1094 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1095 /* note for irix bstrat will need struct bdevsw passed
1096 * Fix the following macro if the code ever is merged
1097 */
1098 XFS_bdstrat(bp);
1099 return 0;
1100 }
1101
1102 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1103 XFS_BUF_ERROR(bp, EIO);
1104 XFS_BUF_STALE(bp);
1105 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001106 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108
1109}
1110
1111/*
1112 * Return size of each in-core log record buffer.
1113 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001114 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *
1116 * If the filesystem blocksize is too large, we may need to choose a
1117 * larger size since the directory code currently logs entire blocks.
1118 */
1119
1120STATIC void
1121xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1122 xlog_t *log)
1123{
1124 int size;
1125 int xhdrs;
1126
Eric Sandeen1cb51252007-08-16 16:24:43 +10001127 if (mp->m_logbufs <= 0)
1128 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1129 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001130 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /*
1133 * Buffer size passed in from mount system call.
1134 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001135 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 size = log->l_iclog_size = mp->m_logbsize;
1137 log->l_iclog_size_log = 0;
1138 while (size != 1) {
1139 log->l_iclog_size_log++;
1140 size >>= 1;
1141 }
1142
Eric Sandeen62118702008-03-06 13:44:28 +11001143 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* # headers = size / 32K
1145 * one header holds cycles from 32K of data
1146 */
1147
1148 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1149 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1150 xhdrs++;
1151 log->l_iclog_hsize = xhdrs << BBSHIFT;
1152 log->l_iclog_heads = xhdrs;
1153 } else {
1154 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1155 log->l_iclog_hsize = BBSIZE;
1156 log->l_iclog_heads = 1;
1157 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001158 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
Eric Sandeen1cb51252007-08-16 16:24:43 +10001161 /* All machines use 32KB buffers by default. */
1162 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1163 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* the default log size is 16k or 32k which is one header sector */
1166 log->l_iclog_hsize = BBSIZE;
1167 log->l_iclog_heads = 1;
1168
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001169done:
1170 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001171 if (mp->m_logbufs == 0)
1172 mp->m_logbufs = log->l_iclog_bufs;
1173 if (mp->m_logbsize == 0)
1174 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175} /* xlog_get_iclog_buffer_size */
1176
1177
1178/*
1179 * This routine initializes some of the log structure for a given mount point.
1180 * Its primary purpose is to fill in enough, so recovery can occur. However,
1181 * some other stuff may be filled in too.
1182 */
1183STATIC xlog_t *
1184xlog_alloc_log(xfs_mount_t *mp,
1185 xfs_buftarg_t *log_target,
1186 xfs_daddr_t blk_offset,
1187 int num_bblks)
1188{
1189 xlog_t *log;
1190 xlog_rec_header_t *head;
1191 xlog_in_core_t **iclogp;
1192 xlog_in_core_t *iclog, *prev_iclog=NULL;
1193 xfs_buf_t *bp;
1194 int i;
1195 int iclogsize;
1196
Dave Chinner644c3562008-11-10 16:50:24 +11001197 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1198 if (!log)
1199 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 log->l_mp = mp;
1202 log->l_targ = log_target;
1203 log->l_logsize = BBTOB(num_bblks);
1204 log->l_logBBstart = blk_offset;
1205 log->l_logBBsize = num_bblks;
1206 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1207 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1208
1209 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001210 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1212 log->l_last_sync_lsn = log->l_tail_lsn;
1213 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1214 log->l_grant_reserve_cycle = 1;
1215 log->l_grant_write_cycle = 1;
1216
Eric Sandeen62118702008-03-06 13:44:28 +11001217 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1219 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1220 /* for larger sector sizes, must have v2 or external log */
1221 ASSERT(log->l_sectbb_log == 0 ||
1222 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001223 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1225 }
1226 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1227
1228 xlog_get_iclog_buffer_size(mp, log);
1229
1230 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001231 if (!bp)
1232 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1234 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1235 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1236 ASSERT(XFS_BUF_ISBUSY(bp));
1237 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1238 log->l_xbuf = bp;
1239
Eric Sandeen007c61c2007-10-11 17:43:56 +10001240 spin_lock_init(&log->l_icloglock);
1241 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001242 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001244 xlog_trace_loggrant_alloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1246 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1247
1248 iclogp = &log->l_iclog;
1249 /*
1250 * The amount of memory to allocate for the iclog structure is
1251 * rather funky due to the way the structure is defined. It is
1252 * done this way so that we can use different sizes for machines
1253 * with different amounts of memory. See the definition of
1254 * xlog_in_core_t in xfs_log_priv.h for details.
1255 */
1256 iclogsize = log->l_iclog_size;
1257 ASSERT(log->l_iclog_size >= 4096);
1258 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001259 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1260 if (!*iclogp)
1261 goto out_free_iclog;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 iclog->ic_prev = prev_iclog;
1265 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001266
1267 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001268 if (!bp)
1269 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001270 if (!XFS_BUF_CPSEMA(bp))
1271 ASSERT(0);
1272 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1273 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1274 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1275 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001276 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001277#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001279#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 head = &iclog->ic_header;
1281 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001282 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1283 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001284 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001285 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001287 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1291 iclog->ic_state = XLOG_STATE_ACTIVE;
1292 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001293 atomic_set(&iclog->ic_refcnt, 0);
1294 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001296 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1299 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001300 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1301 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001303 xlog_trace_iclog_alloc(iclog);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 iclogp = &iclog->ic_next;
1306 }
1307 *iclogp = log->l_iclog; /* complete ring */
1308 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1309
1310 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001311
1312out_free_iclog:
1313 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1314 prev_iclog = iclog->ic_next;
1315 if (iclog->ic_bp) {
1316 sv_destroy(&iclog->ic_force_wait);
1317 sv_destroy(&iclog->ic_write_wait);
1318 xfs_buf_free(iclog->ic_bp);
1319 xlog_trace_iclog_dealloc(iclog);
1320 }
1321 kmem_free(iclog);
1322 }
1323 spinlock_destroy(&log->l_icloglock);
1324 spinlock_destroy(&log->l_grant_lock);
1325 xlog_trace_loggrant_dealloc(log);
1326 xfs_buf_free(log->l_xbuf);
1327out_free_log:
1328 kmem_free(log);
1329 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330} /* xlog_alloc_log */
1331
1332
1333/*
1334 * Write out the commit record of a transaction associated with the given
1335 * ticket. Return the lsn of the commit record.
1336 */
1337STATIC int
1338xlog_commit_record(xfs_mount_t *mp,
1339 xlog_ticket_t *ticket,
1340 xlog_in_core_t **iclog,
1341 xfs_lsn_t *commitlsnp)
1342{
1343 int error;
1344 xfs_log_iovec_t reg[1];
1345
1346 reg[0].i_addr = NULL;
1347 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001348 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 ASSERT_ALWAYS(iclog);
1351 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1352 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001353 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001355 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356} /* xlog_commit_record */
1357
1358
1359/*
1360 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1361 * log space. This code pushes on the lsn which would supposedly free up
1362 * the 25% which we want to leave free. We may need to adopt a policy which
1363 * pushes on an lsn which is further along in the log once we reach the high
1364 * water mark. In this manner, we would be creating a low water mark.
1365 */
David Chinnera8272ce2007-11-23 16:28:09 +11001366STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367xlog_grant_push_ail(xfs_mount_t *mp,
1368 int need_bytes)
1369{
1370 xlog_t *log = mp->m_log; /* pointer to the log */
1371 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1372 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1373 int free_blocks; /* free blocks left to write to */
1374 int free_bytes; /* free bytes left to write to */
1375 int threshold_block; /* block in lsn we'd like to be at */
1376 int threshold_cycle; /* lsn cycle we'd like to be at */
1377 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1380
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001381 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 free_bytes = xlog_space_left(log,
1383 log->l_grant_reserve_cycle,
1384 log->l_grant_reserve_bytes);
1385 tail_lsn = log->l_tail_lsn;
1386 free_blocks = BTOBBT(free_bytes);
1387
1388 /*
1389 * Set the threshold for the minimum number of free blocks in the
1390 * log to the maximum of what the caller needs, one quarter of the
1391 * log, and 256 blocks.
1392 */
1393 free_threshold = BTOBB(need_bytes);
1394 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1395 free_threshold = MAX(free_threshold, 256);
1396 if (free_blocks < free_threshold) {
1397 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1398 threshold_cycle = CYCLE_LSN(tail_lsn);
1399 if (threshold_block >= log->l_logBBsize) {
1400 threshold_block -= log->l_logBBsize;
1401 threshold_cycle += 1;
1402 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001403 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* Don't pass in an lsn greater than the lsn of the last
1406 * log record known to be on disk.
1407 */
1408 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1409 threshold_lsn = log->l_last_sync_lsn;
1410 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001411 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /*
1414 * Get the transaction layer to kick the dirty buffers out to
1415 * disk asynchronously. No point in trying to do this if
1416 * the filesystem is shutting down.
1417 */
1418 if (threshold_lsn &&
1419 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001420 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421} /* xlog_grant_push_ail */
1422
1423
1424/*
1425 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1426 * fashion. Previously, we should have moved the current iclog
1427 * ptr in the log to point to the next available iclog. This allows further
1428 * write to continue while this code syncs out an iclog ready to go.
1429 * Before an in-core log can be written out, the data section must be scanned
1430 * to save away the 1st word of each BBSIZE block into the header. We replace
1431 * it with the current cycle count. Each BBSIZE block is tagged with the
1432 * cycle count because there in an implicit assumption that drives will
1433 * guarantee that entire 512 byte blocks get written at once. In other words,
1434 * we can't have part of a 512 byte block written and part not written. By
1435 * tagging each block, we will know which blocks are valid when recovering
1436 * after an unclean shutdown.
1437 *
1438 * This routine is single threaded on the iclog. No other thread can be in
1439 * this routine with the same iclog. Changing contents of iclog can there-
1440 * fore be done without grabbing the state machine lock. Updating the global
1441 * log will require grabbing the lock though.
1442 *
1443 * The entire log manager uses a logical block numbering scheme. Only
1444 * log_sync (and then only bwrite()) know about the fact that the log may
1445 * not start with block zero on a given device. The log block start offset
1446 * is added immediately before calling bwrite().
1447 */
1448
David Chinnera8272ce2007-11-23 16:28:09 +11001449STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450xlog_sync(xlog_t *log,
1451 xlog_in_core_t *iclog)
1452{
1453 xfs_caddr_t dptr; /* pointer to byte sized element */
1454 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001455 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 uint count; /* byte count of bwrite */
1457 uint count_init; /* initial count before roundup */
1458 int roundoff; /* roundoff to BB or stripe */
1459 int split = 0; /* split write into two regions */
1460 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001461 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001464 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /* Add for LR header */
1467 count_init = log->l_iclog_hsize + iclog->ic_offset;
1468
1469 /* Round out the log write size */
1470 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1471 /* we have a v2 stripe unit to use */
1472 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1473 } else {
1474 count = BBTOB(BTOBB(count_init));
1475 }
1476 roundoff = count - count_init;
1477 ASSERT(roundoff >= 0);
1478 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1479 roundoff < log->l_mp->m_sb.sb_logsunit)
1480 ||
1481 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1482 roundoff < BBTOB(1)));
1483
1484 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001485 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001486 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001487 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 /* put cycle number in every block */
1490 xlog_pack_data(log, iclog, roundoff);
1491
1492 /* real byte length */
1493 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001494 iclog->ic_header.h_len =
1495 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001497 iclog->ic_header.h_len =
1498 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500
Nathan Scottf5faad72006-07-28 17:04:44 +10001501 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1503 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001504 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1507
1508 /* Do we need to split this write into 2 parts? */
1509 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1510 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1511 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1512 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1513 } else {
1514 iclog->ic_bwritecnt = 1;
1515 }
David Chinner511105b2007-05-24 15:21:57 +10001516 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001518 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 XFS_BUF_BUSY(bp);
1520 XFS_BUF_ASYNC(bp);
1521 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001522 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001523 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001525 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001526 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1529 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1530
1531 xlog_verify_iclog(log, iclog, count, B_TRUE);
1532
1533 /* account for log which doesn't start at block #0 */
1534 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1535 /*
1536 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1537 * is shutting down.
1538 */
1539 XFS_BUF_WRITE(bp);
1540
1541 if ((error = XFS_bwrite(bp))) {
1542 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1543 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001544 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001547 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1549 (unsigned long)1);
1550 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1551 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1552 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1553 (__psint_t)count), split);
1554 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001555 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 XFS_BUF_BUSY(bp);
1557 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001558 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1559 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 dptr = XFS_BUF_PTR(bp);
1561 /*
1562 * Bump the cycle numbers at the start of each block
1563 * since this part of the buffer is at the start of
1564 * a new cycle. Watch out for the header magic number
1565 * case, though.
1566 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001567 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001568 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001569 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001570 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 dptr += BBSIZE;
1572 }
1573
1574 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1575 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1576
Nathan Scottc41564b2006-03-29 08:55:14 +10001577 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1579 XFS_BUF_WRITE(bp);
1580 if ((error = XFS_bwrite(bp))) {
1581 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1582 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001583 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587} /* xlog_sync */
1588
1589
1590/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001591 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
David Chinnera8272ce2007-11-23 16:28:09 +11001593STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001594xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int i;
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 iclog = log->l_iclog;
1600 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001601 sv_destroy(&iclog->ic_force_wait);
1602 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 xfs_buf_free(iclog->ic_bp);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001604 xlog_trace_iclog_dealloc(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001606 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 iclog = next_iclog;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 spinlock_destroy(&log->l_icloglock);
1610 spinlock_destroy(&log->l_grant_lock);
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 xfs_buf_free(log->l_xbuf);
Lachlan McIlroy31bd61f2008-09-17 16:45:37 +10001613 xlog_trace_loggrant_dealloc(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001615 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001616} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618/*
1619 * Update counters atomically now that memcpy is done.
1620 */
1621/* ARGSUSED */
1622static inline void
1623xlog_state_finish_copy(xlog_t *log,
1624 xlog_in_core_t *iclog,
1625 int record_cnt,
1626 int copy_bytes)
1627{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001628 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001630 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 iclog->ic_offset += copy_bytes;
1632
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001633 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634} /* xlog_state_finish_copy */
1635
1636
1637
1638
1639/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001640 * print out info relating to regions written which consume
1641 * the reservation
1642 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001643STATIC void
1644xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1645{
1646 uint i;
1647 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1648
1649 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1650 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1651 "bformat",
1652 "bchunk",
1653 "efi_format",
1654 "efd_format",
1655 "iformat",
1656 "icore",
1657 "iext",
1658 "ibroot",
1659 "ilocal",
1660 "iattr_ext",
1661 "iattr_broot",
1662 "iattr_local",
1663 "qformat",
1664 "dquot",
1665 "quotaoff",
1666 "LR header",
1667 "unmount",
1668 "commit",
1669 "trans header"
1670 };
1671 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1672 "SETATTR_NOT_SIZE",
1673 "SETATTR_SIZE",
1674 "INACTIVE",
1675 "CREATE",
1676 "CREATE_TRUNC",
1677 "TRUNCATE_FILE",
1678 "REMOVE",
1679 "LINK",
1680 "RENAME",
1681 "MKDIR",
1682 "RMDIR",
1683 "SYMLINK",
1684 "SET_DMATTRS",
1685 "GROWFS",
1686 "STRAT_WRITE",
1687 "DIOSTRAT",
1688 "WRITE_SYNC",
1689 "WRITEID",
1690 "ADDAFORK",
1691 "ATTRINVAL",
1692 "ATRUNCATE",
1693 "ATTR_SET",
1694 "ATTR_RM",
1695 "ATTR_FLAG",
1696 "CLEAR_AGI_BUCKET",
1697 "QM_SBCHANGE",
1698 "DUMMY1",
1699 "DUMMY2",
1700 "QM_QUOTAOFF",
1701 "QM_DQALLOC",
1702 "QM_SETQLIM",
1703 "QM_DQCLUSTER",
1704 "QM_QINOCREATE",
1705 "QM_QUOTAOFF_END",
1706 "SB_UNIT",
1707 "FSYNC_TS",
1708 "GROWFSRT_ALLOC",
1709 "GROWFSRT_ZERO",
1710 "GROWFSRT_FREE",
1711 "SWAPEXT"
1712 };
1713
1714 xfs_fs_cmn_err(CE_WARN, mp,
1715 "xfs_log_write: reservation summary:\n"
1716 " trans type = %s (%u)\n"
1717 " unit res = %d bytes\n"
1718 " current res = %d bytes\n"
1719 " total reg = %u bytes (o/flow = %u bytes)\n"
1720 " ophdrs = %u (ophdr space = %u bytes)\n"
1721 " ophdr + reg = %u bytes\n"
1722 " num regions = %u\n",
1723 ((ticket->t_trans_type <= 0 ||
1724 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1725 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1726 ticket->t_trans_type,
1727 ticket->t_unit_res,
1728 ticket->t_curr_res,
1729 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1730 ticket->t_res_num_ophdrs, ophdr_spc,
1731 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001732 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001733 ticket->t_res_num);
1734
1735 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001736 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001737 cmn_err(CE_WARN,
1738 "region[%u]: %s - %u bytes\n",
1739 i,
1740 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1741 "bad-rtype" : res_type_str[r_type-1]),
1742 ticket->t_res_arr[i].r_len);
1743 }
1744}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001745
1746/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * Write some region out to in-core log
1748 *
1749 * This will be called when writing externally provided regions or when
1750 * writing out a commit record for a given transaction.
1751 *
1752 * General algorithm:
1753 * 1. Find total length of this write. This may include adding to the
1754 * lengths passed in.
1755 * 2. Check whether we violate the tickets reservation.
1756 * 3. While writing to this iclog
1757 * A. Reserve as much space in this iclog as can get
1758 * B. If this is first write, save away start lsn
1759 * C. While writing this region:
1760 * 1. If first write of transaction, write start record
1761 * 2. Write log operation header (header per region)
1762 * 3. Find out if we can fit entire region into this iclog
1763 * 4. Potentially, verify destination memcpy ptr
1764 * 5. Memcpy (partial) region
1765 * 6. If partial copy, release iclog; otherwise, continue
1766 * copying more regions into current iclog
1767 * 4. Mark want sync bit (in simulation mode)
1768 * 5. Release iclog for potential flush to on-disk log.
1769 *
1770 * ERRORS:
1771 * 1. Panic if reservation is overrun. This should never happen since
1772 * reservation amounts are generated internal to the filesystem.
1773 * NOTES:
1774 * 1. Tickets are single threaded data structures.
1775 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1776 * syncing routine. When a single log_write region needs to span
1777 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1778 * on all log operation writes which don't contain the end of the
1779 * region. The XLOG_END_TRANS bit is used for the in-core log
1780 * operation which contains the end of the continued log_write region.
1781 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1782 * we don't really know exactly how much space will be used. As a result,
1783 * we don't update ic_offset until the end when we know exactly how many
1784 * bytes have been written out.
1785 */
David Chinnera8272ce2007-11-23 16:28:09 +11001786STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787xlog_write(xfs_mount_t * mp,
1788 xfs_log_iovec_t reg[],
1789 int nentries,
1790 xfs_log_ticket_t tic,
1791 xfs_lsn_t *start_lsn,
1792 xlog_in_core_t **commit_iclog,
1793 uint flags)
1794{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001795 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001797 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 __psint_t ptr; /* copy address into data region */
1800 int len; /* # xlog_write() bytes 2 still copy */
1801 int index; /* region index currently copying */
1802 int log_offset; /* offset (from 0) into data region */
1803 int start_rec_copy; /* # bytes to copy for start record */
1804 int partial_copy; /* did we split a region? */
1805 int partial_copy_len;/* # bytes copied if split region */
1806 int need_copy; /* # bytes need to memcpy this region */
1807 int copy_len; /* # bytes actually memcpy'ing */
1808 int copy_off; /* # bytes from entry start */
1809 int contwr; /* continued write of in-core log? */
1810 int error;
1811 int record_cnt = 0, data_cnt = 0;
1812
1813 partial_copy_len = partial_copy = 0;
1814
1815 /* Calculate potential maximum space. Each region gets its own
1816 * xlog_op_header_t and may need to be double word aligned.
1817 */
1818 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001819 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001821 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 for (index = 0; index < nentries; index++) {
1825 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001826 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001828 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830 contwr = *start_lsn = 0;
1831
1832 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001833 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834#ifdef DEBUG
1835 xlog_panic(
1836 "xfs_log_write: reservation ran out. Need to up reservation");
1837#else
1838 /* Customer configurable panic */
1839 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1840 "xfs_log_write: reservation ran out. Need to up reservation");
1841 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001842 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843#endif
1844 } else
1845 ticket->t_curr_res -= len;
1846
1847 for (index = 0; index < nentries; ) {
1848 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1849 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001850 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 ASSERT(log_offset <= iclog->ic_size - 1);
1853 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1854
1855 /* start_lsn is the first lsn written to. That's all we need. */
1856 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001857 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /* This loop writes out as many regions as can fit in the amount
1860 * of space which was allocated by xlog_state_get_iclog_space().
1861 */
1862 while (index < nentries) {
1863 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1864 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1865 start_rec_copy = 0;
1866
1867 /* If first write for transaction, insert start record.
1868 * We can't be trying to commit if we are inited. We can't
1869 * have any "partial_copy" if we are inited.
1870 */
1871 if (ticket->t_flags & XLOG_TIC_INITED) {
1872 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001873 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 logop_head->oh_clientid = ticket->t_clientid;
1875 logop_head->oh_len = 0;
1876 logop_head->oh_flags = XLOG_START_TRANS;
1877 logop_head->oh_res2 = 0;
1878 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1879 record_cnt++;
1880
1881 start_rec_copy = sizeof(xlog_op_header_t);
1882 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1883 }
1884
1885 /* Copy log operation header directly into data section */
1886 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001887 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 logop_head->oh_clientid = ticket->t_clientid;
1889 logop_head->oh_res2 = 0;
1890
1891 /* header copied directly */
1892 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1893
1894 /* are we copying a commit or unmount record? */
1895 logop_head->oh_flags = flags;
1896
1897 /*
1898 * We've seen logs corrupted with bad transaction client
1899 * ids. This makes sure that XFS doesn't generate them on.
1900 * Turn this into an EIO and shut down the filesystem.
1901 */
1902 switch (logop_head->oh_clientid) {
1903 case XFS_TRANSACTION:
1904 case XFS_VOLUME:
1905 case XFS_LOG:
1906 break;
1907 default:
1908 xfs_fs_cmn_err(CE_WARN, mp,
1909 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1910 logop_head->oh_clientid, tic);
1911 return XFS_ERROR(EIO);
1912 }
1913
1914 /* Partial write last time? => (partial_copy != 0)
1915 * need_copy is the amount we'd like to copy if everything could
1916 * fit in the current memcpy.
1917 */
1918 need_copy = reg[index].i_len - partial_copy_len;
1919
1920 copy_off = partial_copy_len;
1921 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001922 copy_len = need_copy;
1923 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (partial_copy)
1925 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1926 partial_copy_len = partial_copy = 0;
1927 } else { /* partial write */
1928 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001929 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1931 if (partial_copy)
1932 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1933 partial_copy_len += copy_len;
1934 partial_copy++;
1935 len += sizeof(xlog_op_header_t); /* from splitting of region */
1936 /* account for new log op header */
1937 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001938 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940 xlog_verify_dest_ptr(log, ptr);
1941
1942 /* copy region */
1943 ASSERT(copy_len >= 0);
1944 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1945 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1946
1947 /* make copy_len total bytes copied, including headers */
1948 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1949 record_cnt++;
1950 data_cnt += contwr ? copy_len : 0;
1951 if (partial_copy) { /* copied partial region */
1952 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1953 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1954 record_cnt = data_cnt = 0;
1955 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001956 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 break; /* don't increment index */
1958 } else { /* copied entire region */
1959 index++;
1960 partial_copy_len = partial_copy = 0;
1961
1962 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1963 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1964 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001965 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001967 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (commit_iclog) {
1969 ASSERT(flags & XLOG_COMMIT_TRANS);
1970 *commit_iclog = iclog;
1971 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001972 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (index == nentries)
1974 return 0; /* we are done */
1975 else
1976 break;
1977 }
1978 } /* if (partial_copy) */
1979 } /* while (index < nentries) */
1980 } /* for (index = 0; index < nentries; ) */
1981 ASSERT(len == 0);
1982
1983 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1984 if (commit_iclog) {
1985 ASSERT(flags & XLOG_COMMIT_TRANS);
1986 *commit_iclog = iclog;
1987 return 0;
1988 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001989 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990} /* xlog_write */
1991
1992
1993/*****************************************************************************
1994 *
1995 * State Machine functions
1996 *
1997 *****************************************************************************
1998 */
1999
2000/* Clean iclogs starting from the head. This ordering must be
2001 * maintained, so an iclog doesn't become ACTIVE beyond one that
2002 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002003 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 * iclog is trying to sync to disk.
2005 *
2006 * State Change: DIRTY -> ACTIVE
2007 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002008STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009xlog_state_clean_log(xlog_t *log)
2010{
2011 xlog_in_core_t *iclog;
2012 int changed = 0;
2013
2014 iclog = log->l_iclog;
2015 do {
2016 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2017 iclog->ic_state = XLOG_STATE_ACTIVE;
2018 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002019 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /*
2021 * If the number of ops in this iclog indicate it just
2022 * contains the dummy transaction, we can
2023 * change state into IDLE (the second time around).
2024 * Otherwise we should change the state into
2025 * NEED a dummy.
2026 * We don't need to cover the dummy.
2027 */
2028 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002029 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2030 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 changed = 1;
2032 } else {
2033 /*
2034 * We have two dirty iclogs so start over
2035 * This could also be num of ops indicates
2036 * this is not the dummy going out.
2037 */
2038 changed = 2;
2039 }
2040 iclog->ic_header.h_num_logops = 0;
2041 memset(iclog->ic_header.h_cycle_data, 0,
2042 sizeof(iclog->ic_header.h_cycle_data));
2043 iclog->ic_header.h_lsn = 0;
2044 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2045 /* do nothing */;
2046 else
2047 break; /* stop cleaning */
2048 iclog = iclog->ic_next;
2049 } while (iclog != log->l_iclog);
2050
2051 /* log is locked when we are called */
2052 /*
2053 * Change state for the dummy log recording.
2054 * We usually go to NEED. But we go to NEED2 if the changed indicates
2055 * we are done writing the dummy record.
2056 * If we are done with the second dummy recored (DONE2), then
2057 * we go to IDLE.
2058 */
2059 if (changed) {
2060 switch (log->l_covered_state) {
2061 case XLOG_STATE_COVER_IDLE:
2062 case XLOG_STATE_COVER_NEED:
2063 case XLOG_STATE_COVER_NEED2:
2064 log->l_covered_state = XLOG_STATE_COVER_NEED;
2065 break;
2066
2067 case XLOG_STATE_COVER_DONE:
2068 if (changed == 1)
2069 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2070 else
2071 log->l_covered_state = XLOG_STATE_COVER_NEED;
2072 break;
2073
2074 case XLOG_STATE_COVER_DONE2:
2075 if (changed == 1)
2076 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2077 else
2078 log->l_covered_state = XLOG_STATE_COVER_NEED;
2079 break;
2080
2081 default:
2082 ASSERT(0);
2083 }
2084 }
2085} /* xlog_state_clean_log */
2086
2087STATIC xfs_lsn_t
2088xlog_get_lowest_lsn(
2089 xlog_t *log)
2090{
2091 xlog_in_core_t *lsn_log;
2092 xfs_lsn_t lowest_lsn, lsn;
2093
2094 lsn_log = log->l_iclog;
2095 lowest_lsn = 0;
2096 do {
2097 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002098 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if ((lsn && !lowest_lsn) ||
2100 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2101 lowest_lsn = lsn;
2102 }
2103 }
2104 lsn_log = lsn_log->ic_next;
2105 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002106 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
2109
2110STATIC void
2111xlog_state_do_callback(
2112 xlog_t *log,
2113 int aborted,
2114 xlog_in_core_t *ciclog)
2115{
2116 xlog_in_core_t *iclog;
2117 xlog_in_core_t *first_iclog; /* used to know when we've
2118 * processed all iclogs once */
2119 xfs_log_callback_t *cb, *cb_next;
2120 int flushcnt = 0;
2121 xfs_lsn_t lowest_lsn;
2122 int ioerrors; /* counter: iclogs with errors */
2123 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2124 int funcdidcallbacks; /* flag: function did callbacks */
2125 int repeats; /* for issuing console warnings if
2126 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002127 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002129 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 first_iclog = iclog = log->l_iclog;
2131 ioerrors = 0;
2132 funcdidcallbacks = 0;
2133 repeats = 0;
2134
2135 do {
2136 /*
2137 * Scan all iclogs starting with the one pointed to by the
2138 * log. Reset this starting point each time the log is
2139 * unlocked (during callbacks).
2140 *
2141 * Keep looping through iclogs until one full pass is made
2142 * without running any callbacks.
2143 */
2144 first_iclog = log->l_iclog;
2145 iclog = log->l_iclog;
2146 loopdidcallbacks = 0;
2147 repeats++;
2148
2149 do {
2150
2151 /* skip all iclogs in the ACTIVE & DIRTY states */
2152 if (iclog->ic_state &
2153 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2154 iclog = iclog->ic_next;
2155 continue;
2156 }
2157
2158 /*
2159 * Between marking a filesystem SHUTDOWN and stopping
2160 * the log, we do flush all iclogs to disk (if there
2161 * wasn't a log I/O error). So, we do want things to
2162 * go smoothly in case of just a SHUTDOWN w/o a
2163 * LOG_IO_ERROR.
2164 */
2165 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2166 /*
2167 * Can only perform callbacks in order. Since
2168 * this iclog is not in the DONE_SYNC/
2169 * DO_CALLBACK state, we skip the rest and
2170 * just try to clean up. If we set our iclog
2171 * to DO_CALLBACK, we will not process it when
2172 * we retry since a previous iclog is in the
2173 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002174 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
2176 if (!(iclog->ic_state &
2177 (XLOG_STATE_DONE_SYNC |
2178 XLOG_STATE_DO_CALLBACK))) {
2179 if (ciclog && (ciclog->ic_state ==
2180 XLOG_STATE_DONE_SYNC)) {
2181 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2182 }
2183 break;
2184 }
2185 /*
2186 * We now have an iclog that is in either the
2187 * DO_CALLBACK or DONE_SYNC states. The other
2188 * states (WANT_SYNC, SYNCING, or CALLBACK were
2189 * caught by the above if and are going to
2190 * clean (i.e. we aren't doing their callbacks)
2191 * see the above if.
2192 */
2193
2194 /*
2195 * We will do one more check here to see if we
2196 * have chased our tail around.
2197 */
2198
2199 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002200 if (lowest_lsn &&
2201 XFS_LSN_CMP(lowest_lsn,
2202 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 iclog = iclog->ic_next;
2204 continue; /* Leave this iclog for
2205 * another thread */
2206 }
2207
2208 iclog->ic_state = XLOG_STATE_CALLBACK;
2209
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002210 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002213 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * No one else can be here except us.
2215 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002216 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002217 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2218 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2219 log->l_last_sync_lsn =
2220 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002221 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002224 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 ioerrors++;
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
David Chinner114d23a2008-04-10 12:18:39 +10002228 /*
2229 * Keep processing entries in the callback list until
2230 * we come around and it is empty. We need to
2231 * atomically see that the list is empty and change the
2232 * state to DIRTY so that we don't miss any more
2233 * callbacks being added.
2234 */
2235 spin_lock(&iclog->ic_callback_lock);
2236 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002237 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 iclog->ic_callback_tail = &(iclog->ic_callback);
2239 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002240 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002243 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 cb_next = cb->cb_next;
2245 cb->cb_func(cb->cb_arg, aborted);
2246 }
David Chinner114d23a2008-04-10 12:18:39 +10002247 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 cb = iclog->ic_callback;
2249 }
2250
2251 loopdidcallbacks++;
2252 funcdidcallbacks++;
2253
David Chinner114d23a2008-04-10 12:18:39 +10002254 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002255 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002256 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2258 iclog->ic_state = XLOG_STATE_DIRTY;
2259
2260 /*
2261 * Transition from DIRTY to ACTIVE if applicable.
2262 * NOP if STATE_IOERROR.
2263 */
2264 xlog_state_clean_log(log);
2265
2266 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002267 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 iclog = iclog->ic_next;
2270 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002271
2272 if (repeats > 5000) {
2273 flushcnt += repeats;
2274 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002276 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002277 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279 } while (!ioerrors && loopdidcallbacks);
2280
2281 /*
2282 * make one last gasp attempt to see if iclogs are being left in
2283 * limbo..
2284 */
2285#ifdef DEBUG
2286 if (funcdidcallbacks) {
2287 first_iclog = iclog = log->l_iclog;
2288 do {
2289 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2290 /*
2291 * Terminate the loop if iclogs are found in states
2292 * which will cause other threads to clean up iclogs.
2293 *
2294 * SYNCING - i/o completion will go through logs
2295 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002296 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * IOERROR - give up hope all ye who enter here
2298 */
2299 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2300 iclog->ic_state == XLOG_STATE_SYNCING ||
2301 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2302 iclog->ic_state == XLOG_STATE_IOERROR )
2303 break;
2304 iclog = iclog->ic_next;
2305 } while (first_iclog != iclog);
2306 }
2307#endif
2308
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002309 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2310 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002311 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002312
2313 if (wake)
2314 sv_broadcast(&log->l_flush_wait);
2315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317
2318/*
2319 * Finish transitioning this iclog to the dirty state.
2320 *
2321 * Make sure that we completely execute this routine only when this is
2322 * the last call to the iclog. There is a good chance that iclog flushes,
2323 * when we reach the end of the physical log, get turned into 2 separate
2324 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2325 * routine. By using the reference count bwritecnt, we guarantee that only
2326 * the second completion goes through.
2327 *
2328 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002329 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 */
David Chinnera8272ce2007-11-23 16:28:09 +11002331STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332xlog_state_done_syncing(
2333 xlog_in_core_t *iclog,
2334 int aborted)
2335{
2336 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002338 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2341 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002342 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2344
2345
2346 /*
2347 * If we got an error, either on the first buffer, or in the case of
2348 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2349 * and none should ever be attempted to be written to disk
2350 * again.
2351 */
2352 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2353 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002354 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return;
2356 }
2357 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2358 }
2359
2360 /*
2361 * Someone could be sleeping prior to writing out the next
2362 * iclog buffer, we wake them all, one will get to do the
2363 * I/O, the others get to wait for the result.
2364 */
David Chinner12017fa2008-08-13 16:34:31 +10002365 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002366 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2368} /* xlog_state_done_syncing */
2369
2370
2371/*
2372 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002373 * sleep. We wait on the flush queue on the head iclog as that should be
2374 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2375 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 *
2377 * The in-core logs are used in a circular fashion. They are not used
2378 * out-of-order even when an iclog past the head is free.
2379 *
2380 * return:
2381 * * log_offset where xlog_write() can start writing into the in-core
2382 * log's data space.
2383 * * in-core log pointer to which xlog_write() should write.
2384 * * boolean indicating this is a continued write to an in-core log.
2385 * If this is the last write, then the in-core log's offset field
2386 * needs to be incremented, depending on the amount of data which
2387 * is copied.
2388 */
David Chinnera8272ce2007-11-23 16:28:09 +11002389STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390xlog_state_get_iclog_space(xlog_t *log,
2391 int len,
2392 xlog_in_core_t **iclogp,
2393 xlog_ticket_t *ticket,
2394 int *continued_write,
2395 int *logoffsetp)
2396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 int log_offset;
2398 xlog_rec_header_t *head;
2399 xlog_in_core_t *iclog;
2400 int error;
2401
2402restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002403 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002405 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return XFS_ERROR(EIO);
2407 }
2408
2409 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002410 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2412 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002413
2414 /* Wait for log writes to have flushed */
2415 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 goto restart;
2417 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 head = &iclog->ic_header;
2420
David Chinner155cc6b2008-03-06 13:44:14 +11002421 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 log_offset = iclog->ic_offset;
2423
2424 /* On the 1st write to an iclog, figure out lsn. This works
2425 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2426 * committing to. If the offset is set, that's how many blocks
2427 * must be written.
2428 */
2429 if (log_offset == 0) {
2430 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002431 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002432 log->l_iclog_hsize,
2433 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002434 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2435 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002436 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 ASSERT(log->l_curr_block >= 0);
2438 }
2439
2440 /* If there is enough room to write everything, then do it. Otherwise,
2441 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2442 * bit is on, so this will get flushed out. Don't update ic_offset
2443 * until you know exactly how many bytes get copied. Therefore, wait
2444 * until later to update ic_offset.
2445 *
2446 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2447 * can fit into remaining data section.
2448 */
2449 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2450 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2451
Dave Chinner49641f12008-07-11 17:43:55 +10002452 /*
2453 * If I'm the only one writing to this iclog, sync it to disk.
2454 * We need to do an atomic compare and decrement here to avoid
2455 * racing with concurrent atomic_dec_and_lock() calls in
2456 * xlog_state_release_iclog() when there is more than one
2457 * reference to the iclog.
2458 */
2459 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2460 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002461 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002462 error = xlog_state_release_iclog(log, iclog);
2463 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002466 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468 goto restart;
2469 }
2470
2471 /* Do we have enough room to write the full amount in the remainder
2472 * of this iclog? Or must we continue a write on the next iclog and
2473 * mark this iclog as completely taken? In the case where we switch
2474 * iclogs (to mark it taken), this particular iclog will release/sync
2475 * to disk in xlog_write().
2476 */
2477 if (len <= iclog->ic_size - iclog->ic_offset) {
2478 *continued_write = 0;
2479 iclog->ic_offset += len;
2480 } else {
2481 *continued_write = 1;
2482 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2483 }
2484 *iclogp = iclog;
2485
2486 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002487 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 *logoffsetp = log_offset;
2490 return 0;
2491} /* xlog_state_get_iclog_space */
2492
2493/*
2494 * Atomically get the log space required for a log ticket.
2495 *
2496 * Once a ticket gets put onto the reserveq, it will only return after
2497 * the needed reservation is satisfied.
2498 */
2499STATIC int
2500xlog_grant_log_space(xlog_t *log,
2501 xlog_ticket_t *tic)
2502{
2503 int free_bytes;
2504 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505#ifdef DEBUG
2506 xfs_lsn_t tail_lsn;
2507#endif
2508
2509
2510#ifdef DEBUG
2511 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2512 panic("grant Recovery problem");
2513#endif
2514
2515 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002516 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2518
2519 /* something is already sleeping; insert new transaction at end */
2520 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002521 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 xlog_trace_loggrant(log, tic,
2523 "xlog_grant_log_space: sleep 1");
2524 /*
2525 * Gotta check this before going to sleep, while we're
2526 * holding the grant lock.
2527 */
2528 if (XLOG_FORCED_SHUTDOWN(log))
2529 goto error_return;
2530
2531 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002532 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /*
2534 * If we got an error, and the filesystem is shutting down,
2535 * we'll catch it down below. So just continue...
2536 */
2537 xlog_trace_loggrant(log, tic,
2538 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002539 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2542 need_bytes = tic->t_unit_res*tic->t_ocnt;
2543 else
2544 need_bytes = tic->t_unit_res;
2545
2546redo:
2547 if (XLOG_FORCED_SHUTDOWN(log))
2548 goto error_return;
2549
2550 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2551 log->l_grant_reserve_bytes);
2552 if (free_bytes < need_bytes) {
2553 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002554 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 xlog_trace_loggrant(log, tic,
2556 "xlog_grant_log_space: sleep 2");
2557 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002558 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002561 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 goto error_return;
2563 }
2564
2565 xlog_trace_loggrant(log, tic,
2566 "xlog_grant_log_space: wake 2");
2567 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002568 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 goto redo;
2570 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002571 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002574 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575#ifdef DEBUG
2576 tail_lsn = log->l_tail_lsn;
2577 /*
2578 * Check to make sure the grant write head didn't just over lap the
2579 * tail. If the cycles are the same, we can't be overlapping.
2580 * Otherwise, make sure that the cycles differ by exactly one and
2581 * check the byte count.
2582 */
2583 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2584 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2585 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2586 }
2587#endif
2588 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2589 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002590 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return 0;
2592
2593 error_return:
2594 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002595 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2597 /*
2598 * If we are failing, make sure the ticket doesn't have any
2599 * current reservations. We don't want to add this back when
2600 * the ticket/transaction gets cancelled.
2601 */
2602 tic->t_curr_res = 0;
2603 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002604 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return XFS_ERROR(EIO);
2606} /* xlog_grant_log_space */
2607
2608
2609/*
2610 * Replenish the byte reservation required by moving the grant write head.
2611 *
2612 *
2613 */
2614STATIC int
2615xlog_regrant_write_log_space(xlog_t *log,
2616 xlog_ticket_t *tic)
2617{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 int free_bytes, need_bytes;
2619 xlog_ticket_t *ntic;
2620#ifdef DEBUG
2621 xfs_lsn_t tail_lsn;
2622#endif
2623
2624 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002625 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630#ifdef DEBUG
2631 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2632 panic("regrant Recovery problem");
2633#endif
2634
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002635 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2637
2638 if (XLOG_FORCED_SHUTDOWN(log))
2639 goto error_return;
2640
2641 /* If there are other waiters on the queue then give them a
2642 * chance at logspace before us. Wake up the first waiters,
2643 * if we do not wake up all the waiters then go to sleep waiting
2644 * for more free space, otherwise try to get some space for
2645 * this transaction.
2646 */
2647
2648 if ((ntic = log->l_write_headq)) {
2649 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2650 log->l_grant_write_bytes);
2651 do {
2652 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2653
2654 if (free_bytes < ntic->t_unit_res)
2655 break;
2656 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002657 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 ntic = ntic->t_next;
2659 } while (ntic != log->l_write_headq);
2660
2661 if (ntic != log->l_write_headq) {
2662 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002663 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 xlog_trace_loggrant(log, tic,
2666 "xlog_regrant_write_log_space: sleep 1");
2667 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002668 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 &log->l_grant_lock, s);
2670
2671 /* If we're shutting down, this tic is already
2672 * off the queue */
2673 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002674 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 goto error_return;
2676 }
2677
2678 xlog_trace_loggrant(log, tic,
2679 "xlog_regrant_write_log_space: wake 1");
2680 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002681 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683 }
2684
2685 need_bytes = tic->t_unit_res;
2686
2687redo:
2688 if (XLOG_FORCED_SHUTDOWN(log))
2689 goto error_return;
2690
2691 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2692 log->l_grant_write_bytes);
2693 if (free_bytes < need_bytes) {
2694 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002695 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002697 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 /* If we're shutting down, this tic is already off the queue */
2700 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002701 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto error_return;
2703 }
2704
2705 xlog_trace_loggrant(log, tic,
2706 "xlog_regrant_write_log_space: wake 2");
2707 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002708 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 goto redo;
2710 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002711 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002713 /* we've got enough space */
2714 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715#ifdef DEBUG
2716 tail_lsn = log->l_tail_lsn;
2717 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2718 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2719 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2720 }
2721#endif
2722
2723 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2724 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002725 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728
2729 error_return:
2730 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002731 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2733 /*
2734 * If we are failing, make sure the ticket doesn't have any
2735 * current reservations. We don't want to add this back when
2736 * the ticket/transaction gets cancelled.
2737 */
2738 tic->t_curr_res = 0;
2739 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002740 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return XFS_ERROR(EIO);
2742} /* xlog_regrant_write_log_space */
2743
2744
2745/* The first cnt-1 times through here we don't need to
2746 * move the grant write head because the permanent
2747 * reservation has reserved cnt times the unit amount.
2748 * Release part of current permanent unit reservation and
2749 * reset current reservation to be one units worth. Also
2750 * move grant reservation head forward.
2751 */
2752STATIC void
2753xlog_regrant_reserve_log_space(xlog_t *log,
2754 xlog_ticket_t *ticket)
2755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 xlog_trace_loggrant(log, ticket,
2757 "xlog_regrant_reserve_log_space: enter");
2758 if (ticket->t_cnt > 0)
2759 ticket->t_cnt--;
2760
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002761 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002762 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002764 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 xlog_trace_loggrant(log, ticket,
2766 "xlog_regrant_reserve_log_space: sub current res");
2767 xlog_verify_grant_head(log, 1);
2768
2769 /* just return if we still have some of the pre-reserved space */
2770 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002771 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return;
2773 }
2774
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002775 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 xlog_trace_loggrant(log, ticket,
2777 "xlog_regrant_reserve_log_space: exit");
2778 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002779 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002781 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782} /* xlog_regrant_reserve_log_space */
2783
2784
2785/*
2786 * Give back the space left from a reservation.
2787 *
2788 * All the information we need to make a correct determination of space left
2789 * is present. For non-permanent reservations, things are quite easy. The
2790 * count should have been decremented to zero. We only need to deal with the
2791 * space remaining in the current reservation part of the ticket. If the
2792 * ticket contains a permanent reservation, there may be left over space which
2793 * needs to be released. A count of N means that N-1 refills of the current
2794 * reservation can be done before we need to ask for more space. The first
2795 * one goes to fill up the first current reservation. Once we run out of
2796 * space, the count will stay at zero and the only space remaining will be
2797 * in the current reservation field.
2798 */
2799STATIC void
2800xlog_ungrant_log_space(xlog_t *log,
2801 xlog_ticket_t *ticket)
2802{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (ticket->t_cnt > 0)
2804 ticket->t_cnt--;
2805
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002806 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2808
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002809 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2812
2813 /* If this is a permanent reservation ticket, we may be able to free
2814 * up more space based on the remaining count.
2815 */
2816 if (ticket->t_cnt > 0) {
2817 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002818 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820
2821 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2822 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002823 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 xfs_log_move_tail(log->l_mp, 1);
2825} /* xlog_ungrant_log_space */
2826
2827
2828/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 * Flush iclog to disk if this is the last reference to the given iclog and
2830 * the WANT_SYNC bit is set.
2831 *
2832 * When this function is entered, the iclog is not necessarily in the
2833 * WANT_SYNC state. It may be sitting around waiting to get filled.
2834 *
2835 *
2836 */
David Chinnera8272ce2007-11-23 16:28:09 +11002837STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002838xlog_state_release_iclog(
2839 xlog_t *log,
2840 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 int sync = 0; /* do we sync? */
2843
David Chinner155cc6b2008-03-06 13:44:14 +11002844 if (iclog->ic_state & XLOG_STATE_IOERROR)
2845 return XFS_ERROR(EIO);
2846
2847 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2848 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2849 return 0;
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002852 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return XFS_ERROR(EIO);
2854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2856 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2857
David Chinner155cc6b2008-03-06 13:44:14 +11002858 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002859 /* update tail before writing to iclog */
2860 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 sync++;
2862 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002863 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2865 /* cycle incremented when incrementing curr_block */
2866 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002867 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 /*
2870 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002871 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2873 * this iclog has consistent data, so we ignore IOERROR
2874 * flags after this point.
2875 */
David Chinnerb5893342008-03-06 13:44:06 +11002876 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879} /* xlog_state_release_iclog */
2880
2881
2882/*
2883 * This routine will mark the current iclog in the ring as WANT_SYNC
2884 * and move the current iclog pointer to the next iclog in the ring.
2885 * When this routine is called from xlog_state_get_iclog_space(), the
2886 * exact size of the iclog has not yet been determined. All we know is
2887 * that every data block. We have run out of space in this log record.
2888 */
2889STATIC void
2890xlog_state_switch_iclogs(xlog_t *log,
2891 xlog_in_core_t *iclog,
2892 int eventual_size)
2893{
2894 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2895 if (!eventual_size)
2896 eventual_size = iclog->ic_offset;
2897 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002898 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 log->l_prev_block = log->l_curr_block;
2900 log->l_prev_cycle = log->l_curr_cycle;
2901
2902 /* roll log?: ic_offset changed later */
2903 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2904
2905 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002906 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 log->l_mp->m_sb.sb_logsunit > 1) {
2908 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2909 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2910 }
2911
2912 if (log->l_curr_block >= log->l_logBBsize) {
2913 log->l_curr_cycle++;
2914 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2915 log->l_curr_cycle++;
2916 log->l_curr_block -= log->l_logBBsize;
2917 ASSERT(log->l_curr_block >= 0);
2918 }
2919 ASSERT(iclog == log->l_iclog);
2920 log->l_iclog = iclog->ic_next;
2921} /* xlog_state_switch_iclogs */
2922
2923
2924/*
2925 * Write out all data in the in-core log as of this exact moment in time.
2926 *
2927 * Data may be written to the in-core log during this call. However,
2928 * we don't guarantee this data will be written out. A change from past
2929 * implementation means this routine will *not* write out zero length LRs.
2930 *
2931 * Basically, we try and perform an intelligent scan of the in-core logs.
2932 * If we determine there is no flushable data, we just return. There is no
2933 * flushable data if:
2934 *
2935 * 1. the current iclog is active and has no data; the previous iclog
2936 * is in the active or dirty state.
2937 * 2. the current iclog is drity, and the previous iclog is in the
2938 * active or dirty state.
2939 *
David Chinner12017fa2008-08-13 16:34:31 +10002940 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 *
2942 * 1. the current iclog is not in the active nor dirty state.
2943 * 2. the current iclog dirty, and the previous iclog is not in the
2944 * active nor dirty state.
2945 * 3. the current iclog is active, and there is another thread writing
2946 * to this particular iclog.
2947 * 4. a) the current iclog is active and has no other writers
2948 * b) when we return from flushing out this iclog, it is still
2949 * not in the active nor dirty state.
2950 */
2951STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002952xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 xlog_in_core_t *iclog;
2955 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002957 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 iclog = log->l_iclog;
2960 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002961 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return XFS_ERROR(EIO);
2963 }
2964
2965 /* If the head iclog is not active nor dirty, we just attach
2966 * ourselves to the head and go to sleep.
2967 */
2968 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2969 iclog->ic_state == XLOG_STATE_DIRTY) {
2970 /*
2971 * If the head is dirty or (active and empty), then
2972 * we need to look at the previous iclog. If the previous
2973 * iclog is active or dirty we are done. There is nothing
2974 * to sync out. Otherwise, we attach ourselves to the
2975 * previous iclog and go to sleep.
2976 */
2977 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002978 (atomic_read(&iclog->ic_refcnt) == 0
2979 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 iclog = iclog->ic_prev;
2981 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2982 iclog->ic_state == XLOG_STATE_DIRTY)
2983 goto no_sleep;
2984 else
2985 goto maybe_sleep;
2986 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002987 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 /* We are the only one with access to this
2989 * iclog. Flush it out now. There should
2990 * be a roundoff of zero to show that someone
2991 * has already taken care of the roundoff from
2992 * the previous sync.
2993 */
David Chinner155cc6b2008-03-06 13:44:14 +11002994 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002995 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002997 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 if (xlog_state_release_iclog(log, iclog))
3000 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003001 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003002 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003003 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 iclog->ic_state != XLOG_STATE_DIRTY)
3005 goto maybe_sleep;
3006 else
3007 goto no_sleep;
3008 } else {
3009 /* Someone else is writing to this iclog.
3010 * Use its call to flush out the data. However,
3011 * the other thread may not force out this LR,
3012 * so we mark it WANT_SYNC.
3013 */
3014 xlog_state_switch_iclogs(log, iclog, 0);
3015 goto maybe_sleep;
3016 }
3017 }
3018 }
3019
3020 /* By the time we come around again, the iclog could've been filled
3021 * which would give it another lsn. If we have a new lsn, just
3022 * return because the relevant data has been flushed.
3023 */
3024maybe_sleep:
3025 if (flags & XFS_LOG_SYNC) {
3026 /*
3027 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003028 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 * Then we check again after waking up, in case our
3030 * sleep was disturbed by a bad news.
3031 */
3032 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003033 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return XFS_ERROR(EIO);
3035 }
3036 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003037 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 /*
3039 * No need to grab the log lock here since we're
3040 * only deciding whether or not to return EIO
3041 * and the memory read should be atomic.
3042 */
3043 if (iclog->ic_state & XLOG_STATE_IOERROR)
3044 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003045 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047 } else {
3048
3049no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003050 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
3052 return 0;
3053} /* xlog_state_sync_all */
3054
3055
3056/*
3057 * Used by code which implements synchronous log forces.
3058 *
3059 * Find in-core log with lsn.
3060 * If it is in the DIRTY state, just return.
3061 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3062 * state and go to sleep or return.
3063 * If it is in any other state, go to sleep or return.
3064 *
3065 * If filesystem activity goes to zero, the iclog will get flushed only by
3066 * bdflush().
3067 */
David Chinnera8272ce2007-11-23 16:28:09 +11003068STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069xlog_state_sync(xlog_t *log,
3070 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003071 uint flags,
3072 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 xlog_in_core_t *iclog;
3075 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003078 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 iclog = log->l_iclog;
3080
3081 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003082 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return XFS_ERROR(EIO);
3084 }
3085
3086 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003087 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3088 iclog = iclog->ic_next;
3089 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091
3092 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003093 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return 0;
3095 }
3096
3097 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3098 /*
3099 * We sleep here if we haven't already slept (e.g.
3100 * this is the first time we've looked at the correct
3101 * iclog buf) and the buffer before us is going to
3102 * be sync'ed. The reason for this is that if we
3103 * are doing sync transactions here, by waiting for
3104 * the previous I/O to complete, we can allow a few
3105 * more transactions into this iclog before we close
3106 * it down.
3107 *
3108 * Otherwise, we mark the buffer WANT_SYNC, and bump
3109 * up the refcnt so we can release the log (which drops
3110 * the ref count). The state switch keeps new transaction
3111 * commits from using this buffer. When the current commits
3112 * finish writing into the buffer, the refcount will drop to
3113 * zero and the buffer will go out then.
3114 */
3115 if (!already_slept &&
3116 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3117 XLOG_STATE_SYNCING))) {
3118 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3119 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003120 sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003122 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 already_slept = 1;
3124 goto try_again;
3125 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003126 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003128 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (xlog_state_release_iclog(log, iclog))
3130 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003131 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003132 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
3134 }
3135
3136 if ((flags & XFS_LOG_SYNC) && /* sleep */
3137 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3138
3139 /*
David Chinner12017fa2008-08-13 16:34:31 +10003140 * Don't wait on completion if we know that we've
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 * gotten a log write error.
3142 */
3143 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003144 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return XFS_ERROR(EIO);
3146 }
3147 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003148 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /*
3150 * No need to grab the log lock here since we're
3151 * only deciding whether or not to return EIO
3152 * and the memory read should be atomic.
3153 */
3154 if (iclog->ic_state & XLOG_STATE_IOERROR)
3155 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003156 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003158 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160 return 0;
3161
3162 } while (iclog != log->l_iclog);
3163
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003164 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166} /* xlog_state_sync */
3167
3168
3169/*
3170 * Called when we want to mark the current iclog as being ready to sync to
3171 * disk.
3172 */
David Chinnera8272ce2007-11-23 16:28:09 +11003173STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3175{
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003176 ASSERT(spin_is_locked(&log->l_icloglock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3179 xlog_state_switch_iclogs(log, iclog, 0);
3180 } else {
3181 ASSERT(iclog->ic_state &
3182 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3183 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186
3187/*****************************************************************************
3188 *
3189 * TICKET functions
3190 *
3191 *****************************************************************************
3192 */
3193
3194/*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003195 * Free a used ticket when it's refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003197void
3198xfs_log_ticket_put(
3199 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003201 ASSERT(atomic_read(&ticket->t_ref) > 0);
3202 if (atomic_dec_and_test(&ticket->t_ref)) {
3203 sv_destroy(&ticket->t_wait);
3204 kmem_zone_free(xfs_log_ticket_zone, ticket);
3205 }
3206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003208xlog_ticket_t *
3209xfs_log_ticket_get(
3210 xlog_ticket_t *ticket)
3211{
3212 ASSERT(atomic_read(&ticket->t_ref) > 0);
3213 atomic_inc(&ticket->t_ref);
3214 return ticket;
3215}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003218 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 */
David Chinnera8272ce2007-11-23 16:28:09 +11003220STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003221xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 int unit_bytes,
3223 int cnt,
3224 char client,
3225 uint xflags)
3226{
3227 xlog_ticket_t *tic;
3228 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
David Chinnereb01c9c2008-04-10 12:18:46 +10003230 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3231 if (!tic)
3232 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 /*
3235 * Permanent reservations have up to 'cnt'-1 active log operations
3236 * in the log. A unit in this case is the amount of space for one
3237 * of these log operations. Normal reservations have a cnt of 1
3238 * and their unit amount is the total amount of space required.
3239 *
3240 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003241 * which occupy space in the on-disk log.
3242 *
3243 * Normal form of a transaction is:
3244 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3245 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3246 *
3247 * We need to account for all the leadup data and trailer data
3248 * around the transaction data.
3249 * And then we need to account for the worst case in terms of using
3250 * more space.
3251 * The worst case will happen if:
3252 * - the placement of the transaction happens to be such that the
3253 * roundoff is at its maximum
3254 * - the transaction data is synced before the commit record is synced
3255 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3256 * Therefore the commit record is in its own Log Record.
3257 * This can happen as the commit record is called with its
3258 * own region to xlog_write().
3259 * This then means that in the worst case, roundoff can happen for
3260 * the commit-rec as well.
3261 * The commit-rec is smaller than padding in this scenario and so it is
3262 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 */
3264
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003265 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003267 unit_bytes += sizeof(xfs_trans_header_t);
3268
3269 /* for start-rec */
3270 unit_bytes += sizeof(xlog_op_header_t);
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 /* for LR headers */
3273 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3274 unit_bytes += log->l_iclog_hsize * num_headers;
3275
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003276 /* for commit-rec LR header - note: padding will subsume the ophdr */
3277 unit_bytes += log->l_iclog_hsize;
3278
3279 /* for split-recs - ophdrs added when data split over LRs */
3280 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3281
3282 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003283 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003284 log->l_mp->m_sb.sb_logsunit > 1) {
3285 /* log su roundoff */
3286 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3287 } else {
3288 /* BB roundoff */
3289 unit_bytes += 2*BBSIZE;
3290 }
3291
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003292 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 tic->t_unit_res = unit_bytes;
3294 tic->t_curr_res = unit_bytes;
3295 tic->t_cnt = cnt;
3296 tic->t_ocnt = cnt;
3297 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3298 tic->t_clientid = client;
3299 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003300 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (xflags & XFS_LOG_PERM_RESERV)
3302 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003303 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Christoph Hellwig0adba532007-08-30 17:21:46 +10003305 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310
3311/******************************************************************************
3312 *
3313 * Log debug routines
3314 *
3315 ******************************************************************************
3316 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003317#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318/*
3319 * Make sure that the destination ptr is within the valid data region of
3320 * one of the iclogs. This uses backup pointers stored in a different
3321 * part of the log in case we trash the log structure.
3322 */
3323void
3324xlog_verify_dest_ptr(xlog_t *log,
3325 __psint_t ptr)
3326{
3327 int i;
3328 int good_ptr = 0;
3329
3330 for (i=0; i < log->l_iclog_bufs; i++) {
3331 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3332 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3333 good_ptr++;
3334 }
3335 if (! good_ptr)
3336 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3337} /* xlog_verify_dest_ptr */
3338
3339STATIC void
3340xlog_verify_grant_head(xlog_t *log, int equals)
3341{
3342 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3343 if (equals)
3344 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3345 else
3346 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3347 } else {
3348 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3349 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3350 }
3351} /* xlog_verify_grant_head */
3352
3353/* check if it will fit */
3354STATIC void
3355xlog_verify_tail_lsn(xlog_t *log,
3356 xlog_in_core_t *iclog,
3357 xfs_lsn_t tail_lsn)
3358{
3359 int blocks;
3360
3361 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3362 blocks =
3363 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3364 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3365 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3366 } else {
3367 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3368
3369 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3370 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3371
3372 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3373 if (blocks < BTOBB(iclog->ic_offset) + 1)
3374 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3375 }
3376} /* xlog_verify_tail_lsn */
3377
3378/*
3379 * Perform a number of checks on the iclog before writing to disk.
3380 *
3381 * 1. Make sure the iclogs are still circular
3382 * 2. Make sure we have a good magic number
3383 * 3. Make sure we don't have magic numbers in the data
3384 * 4. Check fields of each log operation header for:
3385 * A. Valid client identifier
3386 * B. tid ptr value falls in valid ptr space (user space code)
3387 * C. Length in log record header is correct according to the
3388 * individual operation headers within record.
3389 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3390 * log, check the preceding blocks of the physical log to make sure all
3391 * the cycle numbers agree with the current cycle number.
3392 */
3393STATIC void
3394xlog_verify_iclog(xlog_t *log,
3395 xlog_in_core_t *iclog,
3396 int count,
3397 boolean_t syncing)
3398{
3399 xlog_op_header_t *ophead;
3400 xlog_in_core_t *icptr;
3401 xlog_in_core_2_t *xhdr;
3402 xfs_caddr_t ptr;
3403 xfs_caddr_t base_ptr;
3404 __psint_t field_offset;
3405 __uint8_t clientid;
3406 int len, i, j, k, op_len;
3407 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003410 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 icptr = log->l_iclog;
3412 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003413 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 xlog_panic("xlog_verify_iclog: invalid ptr");
3415 icptr = icptr->ic_next;
3416 }
3417 if (icptr != log->l_iclog)
3418 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003419 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
3421 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003422 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 xlog_panic("xlog_verify_iclog: invalid magic num");
3424
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003425 ptr = (xfs_caddr_t) &iclog->ic_header;
3426 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003428 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 xlog_panic("xlog_verify_iclog: unexpected magic num");
3430 }
3431
3432 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003433 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 ptr = iclog->ic_datap;
3435 base_ptr = ptr;
3436 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003437 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 for (i = 0; i < len; i++) {
3439 ophead = (xlog_op_header_t *)ptr;
3440
3441 /* clientid is only 1 byte */
3442 field_offset = (__psint_t)
3443 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3444 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3445 clientid = ophead->oh_clientid;
3446 } else {
3447 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3448 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3449 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3450 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003451 clientid = xlog_get_client_id(
3452 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003454 clientid = xlog_get_client_id(
3455 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457 }
3458 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003459 cmn_err(CE_WARN, "xlog_verify_iclog: "
3460 "invalid clientid %d op 0x%p offset 0x%lx",
3461 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
3463 /* check length */
3464 field_offset = (__psint_t)
3465 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3466 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003467 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 } else {
3469 idx = BTOBBT((__psint_t)&ophead->oh_len -
3470 (__psint_t)iclog->ic_datap);
3471 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3472 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3473 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003474 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003476 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478 }
3479 ptr += sizeof(xlog_op_header_t) + op_len;
3480 }
3481} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003482#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003485 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 */
3487STATIC int
3488xlog_state_ioerror(
3489 xlog_t *log)
3490{
3491 xlog_in_core_t *iclog, *ic;
3492
3493 iclog = log->l_iclog;
3494 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3495 /*
3496 * Mark all the incore logs IOERROR.
3497 * From now on, no log flushes will result.
3498 */
3499 ic = iclog;
3500 do {
3501 ic->ic_state = XLOG_STATE_IOERROR;
3502 ic = ic->ic_next;
3503 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
3506 /*
3507 * Return non-zero, if state transition has already happened.
3508 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003509 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
3512/*
3513 * This is called from xfs_force_shutdown, when we're forcibly
3514 * shutting down the filesystem, typically because of an IO error.
3515 * Our main objectives here are to make sure that:
3516 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3517 * parties to find out, 'atomically'.
3518 * b. those who're sleeping on log reservations, pinned objects and
3519 * other resources get woken up, and be told the bad news.
3520 * c. nothing new gets queued up after (a) and (b) are done.
3521 * d. if !logerror, flush the iclogs to disk, then seal them off
3522 * for business.
3523 */
3524int
3525xfs_log_force_umount(
3526 struct xfs_mount *mp,
3527 int logerror)
3528{
3529 xlog_ticket_t *tic;
3530 xlog_t *log;
3531 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003532 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 log = mp->m_log;
3535
3536 /*
3537 * If this happens during log recovery, don't worry about
3538 * locking; the log isn't open for business yet.
3539 */
3540 if (!log ||
3541 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3542 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003543 if (mp->m_sb_bp)
3544 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
3547
3548 /*
3549 * Somebody could've already done the hard work for us.
3550 * No need to get locks for this.
3551 */
3552 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3553 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003554 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556 retval = 0;
3557 /*
3558 * We must hold both the GRANT lock and the LOG lock,
3559 * before we mark the filesystem SHUTDOWN and wake
3560 * everybody up to tell the bad news.
3561 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003562 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003563 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003565 if (mp->m_sb_bp)
3566 XFS_BUF_DONE(mp->m_sb_bp);
3567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 /*
3569 * This flag is sort of redundant because of the mount flag, but
3570 * it's good to maintain the separation between the log and the rest
3571 * of XFS.
3572 */
3573 log->l_flags |= XLOG_IO_ERROR;
3574
3575 /*
3576 * If we hit a log error, we want to mark all the iclogs IOERROR
3577 * while we're still holding the loglock.
3578 */
3579 if (logerror)
3580 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003581 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 /*
3584 * We don't want anybody waiting for log reservations
3585 * after this. That means we have to wake up everybody
3586 * queued up on reserve_headq as well as write_headq.
3587 * In addition, we make sure in xlog_{re}grant_log_space
3588 * that we don't enqueue anything once the SHUTDOWN flag
3589 * is set, and this action is protected by the GRANTLOCK.
3590 */
3591 if ((tic = log->l_reserve_headq)) {
3592 do {
David Chinner12017fa2008-08-13 16:34:31 +10003593 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 tic = tic->t_next;
3595 } while (tic != log->l_reserve_headq);
3596 }
3597
3598 if ((tic = log->l_write_headq)) {
3599 do {
David Chinner12017fa2008-08-13 16:34:31 +10003600 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 tic = tic->t_next;
3602 } while (tic != log->l_write_headq);
3603 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003604 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3607 ASSERT(!logerror);
3608 /*
3609 * Force the incore logs to disk before shutting the
3610 * log down completely.
3611 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003612 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003613 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003615 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617 /*
3618 * Wake up everybody waiting on xfs_log_force.
3619 * Callback all log item committed functions as if the
3620 * log writes were completed.
3621 */
3622 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3623
3624#ifdef XFSERRORDEBUG
3625 {
3626 xlog_in_core_t *iclog;
3627
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003628 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 iclog = log->l_iclog;
3630 do {
3631 ASSERT(iclog->ic_callback == 0);
3632 iclog = iclog->ic_next;
3633 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003634 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636#endif
3637 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003638 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003641STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642xlog_iclogs_empty(xlog_t *log)
3643{
3644 xlog_in_core_t *iclog;
3645
3646 iclog = log->l_iclog;
3647 do {
3648 /* endianness does not matter here, zero is zero in
3649 * any language.
3650 */
3651 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 iclog = iclog->ic_next;
3654 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003655 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}