blob: 6195cc880101d2e340f6aa913d197511e408ba5f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Chinnereb01c9c2008-04-10 12:18:46 +100044kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define xlog_write_adv_cnt(ptr, len, off, bytes) \
47 { (ptr) += (bytes); \
48 (len) -= (bytes); \
49 (off) += (bytes);}
50
51/* Local miscellaneous function prototypes */
52STATIC int xlog_bdstrat_cb(struct xfs_buf *);
53STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54 xlog_in_core_t **, xfs_lsn_t *);
55STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
56 xfs_buftarg_t *log_target,
57 xfs_daddr_t blk_offset,
58 int num_bblks);
59STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
60STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100061STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
63 int nentries, xfs_log_ticket_t tic,
64 xfs_lsn_t *start_lsn,
65 xlog_in_core_t **commit_iclog,
66 uint flags);
67
68/* local state machine functions */
69STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71STATIC int xlog_state_get_iclog_space(xlog_t *log,
72 int len,
73 xlog_in_core_t **iclog,
74 xlog_ticket_t *ticket,
75 int *continued_write,
76 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077STATIC int xlog_state_release_iclog(xlog_t *log,
78 xlog_in_core_t *iclog);
79STATIC void xlog_state_switch_iclogs(xlog_t *log,
80 xlog_in_core_t *iclog,
81 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110082STATIC int xlog_state_sync(xlog_t *log,
83 xfs_lsn_t lsn,
84 uint flags,
85 int *log_flushed);
86STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
88
89/* local functions to manipulate grant head */
90STATIC int xlog_grant_log_space(xlog_t *log,
91 xlog_ticket_t *xtic);
92STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
93 int need_bytes);
94STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
95 xlog_ticket_t *ticket);
96STATIC int xlog_regrant_write_log_space(xlog_t *log,
97 xlog_ticket_t *ticket);
98STATIC void xlog_ungrant_log_space(xlog_t *log,
99 xlog_ticket_t *ticket);
100
101
102/* local ticket functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
104 int unit_bytes,
105 int count,
106 char clientid,
107 uint flags);
108STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
109
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100110#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
112STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
113STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
114 int count, boolean_t syncing);
115STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
116 xfs_lsn_t tail_lsn);
117#else
118#define xlog_verify_dest_ptr(a,b)
119#define xlog_verify_grant_head(a,b)
120#define xlog_verify_iclog(a,b,c,d)
121#define xlog_verify_tail_lsn(a,b,c)
122#endif
123
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000124STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#if defined(XFS_LOG_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127void
128xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
129{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000130 unsigned long cnts;
131
132 if (!log->l_grant_trace) {
133 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
134 if (!log->l_grant_trace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
136 }
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000137 /* ticket counts are 1 byte each */
138 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 ktrace_enter(log->l_grant_trace,
141 (void *)tic,
142 (void *)log->l_reserve_headq,
143 (void *)log->l_write_headq,
144 (void *)((unsigned long)log->l_grant_reserve_cycle),
145 (void *)((unsigned long)log->l_grant_reserve_bytes),
146 (void *)((unsigned long)log->l_grant_write_cycle),
147 (void *)((unsigned long)log->l_grant_write_bytes),
148 (void *)((unsigned long)log->l_curr_cycle),
149 (void *)((unsigned long)log->l_curr_block),
150 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
151 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
152 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000153 (void *)((unsigned long)tic->t_trans_type),
154 (void *)cnts,
155 (void *)((unsigned long)tic->t_curr_res),
156 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159void
160xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!iclog->ic_trace)
163 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
164 ktrace_enter(iclog->ic_trace,
165 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100166 (void *)((unsigned long)current_pid()),
167 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
168 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
169 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
170 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#else
173#define xlog_trace_loggrant(log,tic,string)
174#define xlog_trace_iclog(iclog,state)
175#endif /* XFS_LOG_TRACE */
176
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100177
178static void
179xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
180{
181 if (*qp) {
182 tic->t_next = (*qp);
183 tic->t_prev = (*qp)->t_prev;
184 (*qp)->t_prev->t_next = tic;
185 (*qp)->t_prev = tic;
186 } else {
187 tic->t_prev = tic->t_next = tic;
188 *qp = tic;
189 }
190
191 tic->t_flags |= XLOG_TIC_IN_Q;
192}
193
194static void
195xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
196{
197 if (tic == tic->t_next) {
198 *qp = NULL;
199 } else {
200 *qp = tic->t_next;
201 tic->t_next->t_prev = tic->t_prev;
202 tic->t_prev->t_next = tic->t_next;
203 }
204
205 tic->t_next = tic->t_prev = NULL;
206 tic->t_flags &= ~XLOG_TIC_IN_Q;
207}
208
209static void
210xlog_grant_sub_space(struct log *log, int bytes)
211{
212 log->l_grant_write_bytes -= bytes;
213 if (log->l_grant_write_bytes < 0) {
214 log->l_grant_write_bytes += log->l_logsize;
215 log->l_grant_write_cycle--;
216 }
217
218 log->l_grant_reserve_bytes -= bytes;
219 if ((log)->l_grant_reserve_bytes < 0) {
220 log->l_grant_reserve_bytes += log->l_logsize;
221 log->l_grant_reserve_cycle--;
222 }
223
224}
225
226static void
227xlog_grant_add_space_write(struct log *log, int bytes)
228{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000229 int tmp = log->l_logsize - log->l_grant_write_bytes;
230 if (tmp > bytes)
231 log->l_grant_write_bytes += bytes;
232 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100233 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000234 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100235 }
236}
237
238static void
239xlog_grant_add_space_reserve(struct log *log, int bytes)
240{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000241 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
242 if (tmp > bytes)
243 log->l_grant_reserve_bytes += bytes;
244 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100245 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000246 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100247 }
248}
249
250static inline void
251xlog_grant_add_space(struct log *log, int bytes)
252{
253 xlog_grant_add_space_write(log, bytes);
254 xlog_grant_add_space_reserve(log, bytes);
255}
256
Christoph Hellwig0adba532007-08-30 17:21:46 +1000257static void
258xlog_tic_reset_res(xlog_ticket_t *tic)
259{
260 tic->t_res_num = 0;
261 tic->t_res_arr_sum = 0;
262 tic->t_res_num_ophdrs = 0;
263}
264
265static void
266xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
267{
268 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
269 /* add to overflow and start again */
270 tic->t_res_o_flow += tic->t_res_arr_sum;
271 tic->t_res_num = 0;
272 tic->t_res_arr_sum = 0;
273 }
274
275 tic->t_res_arr[tic->t_res_num].r_len = len;
276 tic->t_res_arr[tic->t_res_num].r_type = type;
277 tic->t_res_arr_sum += len;
278 tic->t_res_num++;
279}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*
282 * NOTES:
283 *
284 * 1. currblock field gets updated at startup and after in-core logs
285 * marked as with WANT_SYNC.
286 */
287
288/*
289 * This routine is called when a user of a log manager ticket is done with
290 * the reservation. If the ticket was ever used, then a commit record for
291 * the associated transaction is written out as a log operation header with
292 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
293 * a given ticket. If the ticket was one with a permanent reservation, then
294 * a few operations are done differently. Permanent reservation tickets by
295 * default don't release the reservation. They just commit the current
296 * transaction with the belief that the reservation is still needed. A flag
297 * must be passed in before permanent reservations are actually released.
298 * When these type of tickets are not released, they need to be set into
299 * the inited state again. By doing this, a start record will be written
300 * out when the next write occurs.
301 */
302xfs_lsn_t
303xfs_log_done(xfs_mount_t *mp,
304 xfs_log_ticket_t xtic,
305 void **iclog,
306 uint flags)
307{
308 xlog_t *log = mp->m_log;
309 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
310 xfs_lsn_t lsn = 0;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (XLOG_FORCED_SHUTDOWN(log) ||
313 /*
314 * If nothing was ever written, don't write out commit record.
315 * If we get an error, just continue and give back the log ticket.
316 */
317 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
318 (xlog_commit_record(mp, ticket,
319 (xlog_in_core_t **)iclog, &lsn)))) {
320 lsn = (xfs_lsn_t) -1;
321 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
322 flags |= XFS_LOG_REL_PERM_RESERV;
323 }
324 }
325
326
327 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
328 (flags & XFS_LOG_REL_PERM_RESERV)) {
329 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000330 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * request has been made to release a permanent reservation.
332 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000333 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xlog_ungrant_log_space(log, ticket);
David Chinnereb01c9c2008-04-10 12:18:46 +1000335 xlog_ticket_put(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000337 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xlog_regrant_reserve_log_space(log, ticket);
339 }
340
341 /* If this ticket was a permanent reservation and we aren't
342 * trying to release it, reset the inited flags; so next time
343 * we write, a start record will be written out.
344 */
345 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
346 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
347 ticket->t_flags |= XLOG_TIC_INITED;
348
349 return lsn;
350} /* xfs_log_done */
351
352
353/*
354 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
355 * the force is done synchronously.
356 *
357 * Asynchronous forces are implemented by setting the WANT_SYNC
358 * bit in the appropriate in-core log and then returning.
359 *
360 * Synchronous forces are implemented with a semaphore. All callers
361 * to force a given lsn to disk will wait on a semaphore attached to the
362 * specific in-core log. When given in-core log finally completes its
363 * write to disk, that thread will wake up all threads waiting on the
364 * semaphore.
365 */
366int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100367_xfs_log_force(
368 xfs_mount_t *mp,
369 xfs_lsn_t lsn,
370 uint flags,
371 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100373 xlog_t *log = mp->m_log;
374 int dummy;
375
376 if (!log_flushed)
377 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 ASSERT(flags & XFS_LOG_FORCE);
380
381 XFS_STATS_INC(xs_log_force);
382
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100383 if (log->l_flags & XLOG_IO_ERROR)
384 return XFS_ERROR(EIO);
385 if (lsn == 0)
386 return xlog_state_sync_all(log, flags, log_flushed);
387 else
388 return xlog_state_sync(log, lsn, flags, log_flushed);
David Chinnerb911ca02008-04-10 12:24:30 +1000389} /* _xfs_log_force */
390
391/*
392 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
393 * about errors or whether the log was flushed or not. This is the normal
394 * interface to use when trying to unpin items or move the log forward.
395 */
396void
397xfs_log_force(
398 xfs_mount_t *mp,
399 xfs_lsn_t lsn,
400 uint flags)
401{
402 int error;
403 error = _xfs_log_force(mp, lsn, flags, NULL);
404 if (error) {
405 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
406 "error %d returned.", error);
407 }
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/*
412 * Attaches a new iclog I/O completion callback routine during
413 * transaction commit. If the log is in error state, a non-zero
414 * return code is handed back and the caller is responsible for
415 * executing the callback at an appropriate time.
416 */
417int
418xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
419 void *iclog_hndl, /* iclog to hang callback off */
420 xfs_log_callback_t *cb)
421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000423 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David Chinner114d23a2008-04-10 12:18:39 +1000425 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
427 if (!abortflg) {
428 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
429 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
430 cb->cb_next = NULL;
431 *(iclog->ic_callback_tail) = cb;
432 iclog->ic_callback_tail = &(cb->cb_next);
433 }
David Chinner114d23a2008-04-10 12:18:39 +1000434 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return abortflg;
436} /* xfs_log_notify */
437
438int
439xfs_log_release_iclog(xfs_mount_t *mp,
440 void *iclog_hndl)
441{
442 xlog_t *log = mp->m_log;
443 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
444
445 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000446 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100447 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 return 0;
451}
452
453/*
454 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
455 * to the reservation.
456 * 2. Potentially, push buffers at tail of log to disk.
457 *
458 * Each reservation is going to reserve extra space for a log record header.
459 * When writes happen to the on-disk log, we don't subtract the length of the
460 * log record header from any reservation. By wasting space in each
461 * reservation, we prevent over allocation problems.
462 */
463int
464xfs_log_reserve(xfs_mount_t *mp,
465 int unit_bytes,
466 int cnt,
467 xfs_log_ticket_t *ticket,
468 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000469 uint flags,
470 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 xlog_t *log = mp->m_log;
473 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100474 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
477 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
478
479 if (XLOG_FORCED_SHUTDOWN(log))
480 return XFS_ERROR(EIO);
481
482 XFS_STATS_INC(xs_try_logspace);
483
484 if (*ticket != NULL) {
485 ASSERT(flags & XFS_LOG_PERM_RESERV);
486 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000487 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
489 retval = xlog_regrant_write_log_space(log, internal_ticket);
490 } else {
491 /* may sleep if need to allocate more tickets */
492 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
493 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000494 if (!internal_ticket)
495 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000496 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000498 xlog_trace_loggrant(log, internal_ticket,
499 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
500 "xfs_log_reserve: create new ticket (permanent trans)" :
501 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 xlog_grant_push_ail(mp,
503 (internal_ticket->t_unit_res *
504 internal_ticket->t_cnt));
505 retval = xlog_grant_log_space(log, internal_ticket);
506 }
507
508 return retval;
509} /* xfs_log_reserve */
510
511
512/*
513 * Mount a log filesystem
514 *
515 * mp - ubiquitous xfs mount point structure
516 * log_target - buftarg of on-disk log device
517 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
518 * num_bblocks - Number of BBSIZE blocks in on-disk log
519 *
520 * Return error or zero.
521 */
522int
David Chinner249a8c12008-02-05 12:13:32 +1100523xfs_log_mount(
524 xfs_mount_t *mp,
525 xfs_buftarg_t *log_target,
526 xfs_daddr_t blk_offset,
527 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
David Chinner249a8c12008-02-05 12:13:32 +1100529 int error;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
532 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
533 else {
534 cmn_err(CE_NOTE,
535 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
536 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000537 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /*
David Chinner249a8c12008-02-05 12:13:32 +1100543 * Initialize the AIL now we have a log.
544 */
545 spin_lock_init(&mp->m_ail_lock);
546 error = xfs_trans_ail_init(mp);
547 if (error) {
548 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
549 goto error;
550 }
551
552 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * skip log recovery on a norecovery mount. pretend it all
554 * just worked.
555 */
556 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100557 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000560 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Sandeen65be6052006-01-11 15:34:19 +1100562 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000565 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (error) {
567 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
David Chinner249a8c12008-02-05 12:13:32 +1100568 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 }
571
572 /* Normal transactions can now occur */
573 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
574
575 /* End mounting message in xfs_log_mount_finish */
576 return 0;
David Chinner249a8c12008-02-05 12:13:32 +1100577error:
578 xfs_log_unmount_dealloc(mp);
579 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580} /* xfs_log_mount */
581
582/*
583 * Finish the recovery of the file system. This is separate from
584 * the xfs_log_mount() call, because it depends on the code in
585 * xfs_mountfs() to read in the root and real-time bitmap inodes
586 * between calling xfs_log_mount() and here.
587 *
588 * mp - ubiquitous xfs mount point structure
589 */
590int
591xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
592{
593 int error;
594
595 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
596 error = xlog_recover_finish(mp->m_log, mfsi_flags);
597 else {
598 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000599 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 return error;
603}
604
605/*
606 * Unmount processing for the log.
607 */
608int
609xfs_log_unmount(xfs_mount_t *mp)
610{
611 int error;
612
613 error = xfs_log_unmount_write(mp);
614 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100615 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/*
619 * Final log writes as part of unmount.
620 *
621 * Mark the filesystem clean as unmount happens. Note that during relocation
622 * this routine needs to be executed as part of source-bag while the
623 * deallocation must not be done until source-end.
624 */
625
626/*
627 * Unmount record used to have a string "Unmount filesystem--" in the
628 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
629 * We just write the magic number now since that particular field isn't
630 * currently architecture converted and "nUmount" is a bit foo.
631 * As far as I know, there weren't any dependencies on the old behaviour.
632 */
633
634int
635xfs_log_unmount_write(xfs_mount_t *mp)
636{
637 xlog_t *log = mp->m_log;
638 xlog_in_core_t *iclog;
639#ifdef DEBUG
640 xlog_in_core_t *first_iclog;
641#endif
642 xfs_log_iovec_t reg[1];
643 xfs_log_ticket_t tic = NULL;
644 xfs_lsn_t lsn;
645 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* the data section must be 32 bit size aligned */
648 struct {
649 __uint16_t magic;
650 __uint16_t pad1;
651 __uint32_t pad2; /* may as well make it 64 bits */
652 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * Don't write out unmount record on read-only mounts.
656 * Or, if we are doing a forced umount (typically because of IO errors).
657 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000658 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0;
660
David Chinnerb911ca02008-04-10 12:24:30 +1000661 error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
662 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664#ifdef DEBUG
665 first_iclog = iclog = log->l_iclog;
666 do {
667 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
668 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
669 ASSERT(iclog->ic_offset == 0);
670 }
671 iclog = iclog->ic_next;
672 } while (iclog != first_iclog);
673#endif
674 if (! (XLOG_FORCED_SHUTDOWN(log))) {
675 reg[0].i_addr = (void*)&magic;
676 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000677 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Tim Shimmin955e47a2006-09-28 11:04:16 +1000679 error = xfs_log_reserve(mp, 600, 1, &tic,
680 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (!error) {
682 /* remove inited flag */
683 ((xlog_ticket_t *)tic)->t_flags = 0;
684 error = xlog_write(mp, reg, 1, tic, &lsn,
685 NULL, XLOG_UNMOUNT_TRANS);
686 /*
687 * At this point, we're umounting anyway,
688 * so there's no point in transitioning log state
689 * to IOERROR. Just continue...
690 */
691 }
692
693 if (error) {
694 xfs_fs_cmn_err(CE_ALERT, mp,
695 "xfs_log_unmount: unmount record failed");
696 }
697
698
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000699 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100701 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000702 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xlog_state_want_sync(log, iclog);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000704 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000706 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
708 iclog->ic_state == XLOG_STATE_DIRTY)) {
709 if (!XLOG_FORCED_SHUTDOWN(log)) {
710 sv_wait(&iclog->ic_forcesema, PMEM,
711 &log->l_icloglock, s);
712 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000713 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000716 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000718 if (tic) {
719 xlog_trace_loggrant(log, tic, "unmount rec");
720 xlog_ungrant_log_space(log, tic);
David Chinnereb01c9c2008-04-10 12:18:46 +1000721 xlog_ticket_put(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 } else {
724 /*
725 * We're already in forced_shutdown mode, couldn't
726 * even attempt to write out the unmount transaction.
727 *
728 * Go through the motions of sync'ing and releasing
729 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000730 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * be in progress. Do this as a separate section of
732 * code so we'll know if we ever get stuck here that
733 * we're in this odd situation of trying to unmount
734 * a file system that went into forced_shutdown as
735 * the result of an unmount..
736 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000737 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100739 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000740 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 xlog_state_want_sync(log, iclog);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000743 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000745 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
748 || iclog->ic_state == XLOG_STATE_DIRTY
749 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
750
751 sv_wait(&iclog->ic_forcesema, PMEM,
752 &log->l_icloglock, s);
753 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000754 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 }
757
David Chinner1bb7d6b2008-04-10 12:24:38 +1000758 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759} /* xfs_log_unmount_write */
760
761/*
762 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100763 *
764 * We need to stop the aild from running before we destroy
765 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
767void
768xfs_log_unmount_dealloc(xfs_mount_t *mp)
769{
David Chinner249a8c12008-02-05 12:13:32 +1100770 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000771 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
775 * Write region vectors to log. The write happens using the space reservation
776 * of the ticket (tic). It is not a requirement that all writes for a given
777 * transaction occur with one call to xfs_log_write().
778 */
779int
780xfs_log_write(xfs_mount_t * mp,
781 xfs_log_iovec_t reg[],
782 int nentries,
783 xfs_log_ticket_t tic,
784 xfs_lsn_t *start_lsn)
785{
786 int error;
787 xlog_t *log = mp->m_log;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (XLOG_FORCED_SHUTDOWN(log))
790 return XFS_ERROR(EIO);
791
792 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000793 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100795 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796} /* xfs_log_write */
797
798
799void
800xfs_log_move_tail(xfs_mount_t *mp,
801 xfs_lsn_t tail_lsn)
802{
803 xlog_ticket_t *tic;
804 xlog_t *log = mp->m_log;
805 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (XLOG_FORCED_SHUTDOWN(log))
808 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (tail_lsn == 0) {
811 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000812 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000814 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000817 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
820 * tail_lsn.
821 */
822 if (tail_lsn != 1) {
823 log->l_tail_lsn = tail_lsn;
824 }
825
826 if ((tic = log->l_write_headq)) {
827#ifdef DEBUG
828 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
829 panic("Recovery problem");
830#endif
831 cycle = log->l_grant_write_cycle;
832 bytes = log->l_grant_write_bytes;
833 free_bytes = xlog_space_left(log, cycle, bytes);
834 do {
835 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
836
837 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
838 break;
839 tail_lsn = 0;
840 free_bytes -= tic->t_unit_res;
841 sv_signal(&tic->t_sema);
842 tic = tic->t_next;
843 } while (tic != log->l_write_headq);
844 }
845 if ((tic = log->l_reserve_headq)) {
846#ifdef DEBUG
847 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
848 panic("Recovery problem");
849#endif
850 cycle = log->l_grant_reserve_cycle;
851 bytes = log->l_grant_reserve_bytes;
852 free_bytes = xlog_space_left(log, cycle, bytes);
853 do {
854 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
855 need_bytes = tic->t_unit_res*tic->t_cnt;
856 else
857 need_bytes = tic->t_unit_res;
858 if (free_bytes < need_bytes && tail_lsn != 1)
859 break;
860 tail_lsn = 0;
861 free_bytes -= need_bytes;
862 sv_signal(&tic->t_sema);
863 tic = tic->t_next;
864 } while (tic != log->l_reserve_headq);
865 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000866 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867} /* xfs_log_move_tail */
868
869/*
870 * Determine if we have a transaction that has gone to disk
871 * that needs to be covered. Log activity needs to be idle (no AIL and
872 * nothing in the iclogs). And, we need to be in the right state indicating
873 * something has gone out.
874 */
875int
876xfs_log_need_covered(xfs_mount_t *mp)
877{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int needed = 0, gen;
879 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
David Chinner92821e22007-05-24 15:26:31 +1000881 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 0;
883
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000884 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
886 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
887 && !xfs_trans_first_ail(mp, &gen)
888 && xlog_iclogs_empty(log)) {
889 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
890 log->l_covered_state = XLOG_STATE_COVER_DONE;
891 else {
892 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
893 log->l_covered_state = XLOG_STATE_COVER_DONE2;
894 }
895 needed = 1;
896 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000897 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100898 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/******************************************************************************
902 *
903 * local routines
904 *
905 ******************************************************************************
906 */
907
908/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
909 * The log manager must keep track of the last LR which was committed
910 * to disk. The lsn of this LR will become the new tail_lsn whenever
911 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
912 * the situation where stuff could be written into the log but nothing
913 * was ever in the AIL when asked. Eventually, we panic since the
914 * tail hits the head.
915 *
916 * We may be holding the log iclog lock upon entering this routine.
917 */
918xfs_lsn_t
919xlog_assign_tail_lsn(xfs_mount_t *mp)
920{
921 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 xlog_t *log = mp->m_log;
923
924 tail_lsn = xfs_trans_tail_ail(mp);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000925 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (tail_lsn != 0) {
927 log->l_tail_lsn = tail_lsn;
928 } else {
929 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
930 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000931 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 return tail_lsn;
934} /* xlog_assign_tail_lsn */
935
936
937/*
938 * Return the space in the log between the tail and the head. The head
939 * is passed in the cycle/bytes formal parms. In the special case where
940 * the reserve head has wrapped passed the tail, this calculation is no
941 * longer valid. In this case, just return 0 which means there is no space
942 * in the log. This works for all places where this function is called
943 * with the reserve head. Of course, if the write head were to ever
944 * wrap the tail, we should blow up. Rather than catch this case here,
945 * we depend on other ASSERTions in other parts of the code. XXXmiken
946 *
947 * This code also handles the case where the reservation head is behind
948 * the tail. The details of this case are described below, but the end
949 * result is that we return the size of the log as the amount of space left.
950 */
David Chinnera8272ce2007-11-23 16:28:09 +1100951STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952xlog_space_left(xlog_t *log, int cycle, int bytes)
953{
954 int free_bytes;
955 int tail_bytes;
956 int tail_cycle;
957
958 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
959 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
960 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
961 free_bytes = log->l_logsize - (bytes - tail_bytes);
962 } else if ((tail_cycle + 1) < cycle) {
963 return 0;
964 } else if (tail_cycle < cycle) {
965 ASSERT(tail_cycle == (cycle - 1));
966 free_bytes = tail_bytes - bytes;
967 } else {
968 /*
969 * The reservation head is behind the tail.
970 * In this case we just want to return the size of the
971 * log as the amount of space left.
972 */
973 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
974 "xlog_space_left: head behind tail\n"
975 " tail_cycle = %d, tail_bytes = %d\n"
976 " GH cycle = %d, GH bytes = %d",
977 tail_cycle, tail_bytes, cycle, bytes);
978 ASSERT(0);
979 free_bytes = log->l_logsize;
980 }
981 return free_bytes;
982} /* xlog_space_left */
983
984
985/*
986 * Log function which is called when an io completes.
987 *
988 * The log manager needs its own routine, in order to control what
989 * happens with the buffer after the write completes.
990 */
991void
992xlog_iodone(xfs_buf_t *bp)
993{
994 xlog_in_core_t *iclog;
995 xlog_t *l;
996 int aborted;
997
998 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
999 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1000 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1001 aborted = 0;
1002
1003 /*
1004 * Some versions of cpp barf on the recursive definition of
1005 * ic_log -> hic_fields.ic_log and expand ic_log twice when
1006 * it is passed through two macros. Workaround broken cpp.
1007 */
1008 l = iclog->ic_log;
1009
1010 /*
David Chinner0bfefc42007-05-14 18:24:23 +10001011 * If the ordered flag has been removed by a lower
1012 * layer, it means the underlyin device no longer supports
1013 * barrier I/O. Warn loudly and turn off barriers.
1014 */
1015 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
1016 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1017 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1018 "xlog_iodone: Barriers are no longer supported"
1019 " by device. Disabling barriers\n");
1020 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1021 }
1022
1023 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * Race to shutdown the filesystem if we see an error.
1025 */
1026 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1027 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1028 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1029 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001030 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * This flag will be propagated to the trans-committed
1033 * callback routines to let them know that the log-commit
1034 * didn't succeed.
1035 */
1036 aborted = XFS_LI_ABORTED;
1037 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1038 aborted = XFS_LI_ABORTED;
1039 }
David Chinner3db296f2007-05-14 18:24:16 +10001040
1041 /* log I/O is always issued ASYNC */
1042 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001044 /*
1045 * do not reference the buffer (bp) here as we could race
1046 * with it being freed after writing the unmount record to the
1047 * log.
1048 */
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050} /* xlog_iodone */
1051
1052/*
1053 * The bdstrat callback function for log bufs. This gives us a central
1054 * place to trap bufs in case we get hit by a log I/O error and need to
1055 * shutdown. Actually, in practice, even when we didn't get a log error,
1056 * we transition the iclogs to IOERROR state *after* flushing all existing
1057 * iclogs to disk. This is because we don't want anymore new transactions to be
1058 * started or completed afterwards.
1059 */
1060STATIC int
1061xlog_bdstrat_cb(struct xfs_buf *bp)
1062{
1063 xlog_in_core_t *iclog;
1064
1065 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1066
1067 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1068 /* note for irix bstrat will need struct bdevsw passed
1069 * Fix the following macro if the code ever is merged
1070 */
1071 XFS_bdstrat(bp);
1072 return 0;
1073 }
1074
1075 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1076 XFS_BUF_ERROR(bp, EIO);
1077 XFS_BUF_STALE(bp);
1078 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001079 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081
1082}
1083
1084/*
1085 * Return size of each in-core log record buffer.
1086 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001087 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 *
1089 * If the filesystem blocksize is too large, we may need to choose a
1090 * larger size since the directory code currently logs entire blocks.
1091 */
1092
1093STATIC void
1094xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1095 xlog_t *log)
1096{
1097 int size;
1098 int xhdrs;
1099
Eric Sandeen1cb51252007-08-16 16:24:43 +10001100 if (mp->m_logbufs <= 0)
1101 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1102 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001103 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 /*
1106 * Buffer size passed in from mount system call.
1107 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001108 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 size = log->l_iclog_size = mp->m_logbsize;
1110 log->l_iclog_size_log = 0;
1111 while (size != 1) {
1112 log->l_iclog_size_log++;
1113 size >>= 1;
1114 }
1115
Eric Sandeen62118702008-03-06 13:44:28 +11001116 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* # headers = size / 32K
1118 * one header holds cycles from 32K of data
1119 */
1120
1121 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1122 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1123 xhdrs++;
1124 log->l_iclog_hsize = xhdrs << BBSHIFT;
1125 log->l_iclog_heads = xhdrs;
1126 } else {
1127 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1128 log->l_iclog_hsize = BBSIZE;
1129 log->l_iclog_heads = 1;
1130 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001131 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Eric Sandeen1cb51252007-08-16 16:24:43 +10001134 /* All machines use 32KB buffers by default. */
1135 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1136 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /* the default log size is 16k or 32k which is one header sector */
1139 log->l_iclog_hsize = BBSIZE;
1140 log->l_iclog_heads = 1;
1141
1142 /*
1143 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1144 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1145 */
1146 if (mp->m_sb.sb_blocksize >= 16*1024) {
1147 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1148 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001149 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 switch (mp->m_sb.sb_blocksize) {
1151 case 16*1024: /* 16 KB */
1152 log->l_iclog_bufs = 3;
1153 break;
1154 case 32*1024: /* 32 KB */
1155 log->l_iclog_bufs = 4;
1156 break;
1157 case 64*1024: /* 64 KB */
1158 log->l_iclog_bufs = 8;
1159 break;
1160 default:
1161 xlog_panic("XFS: Invalid blocksize");
1162 break;
1163 }
1164 }
1165 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001166
1167done: /* are we being asked to make the sizes selected above visible? */
1168 if (mp->m_logbufs == 0)
1169 mp->m_logbufs = log->l_iclog_bufs;
1170 if (mp->m_logbsize == 0)
1171 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172} /* xlog_get_iclog_buffer_size */
1173
1174
1175/*
1176 * This routine initializes some of the log structure for a given mount point.
1177 * Its primary purpose is to fill in enough, so recovery can occur. However,
1178 * some other stuff may be filled in too.
1179 */
1180STATIC xlog_t *
1181xlog_alloc_log(xfs_mount_t *mp,
1182 xfs_buftarg_t *log_target,
1183 xfs_daddr_t blk_offset,
1184 int num_bblks)
1185{
1186 xlog_t *log;
1187 xlog_rec_header_t *head;
1188 xlog_in_core_t **iclogp;
1189 xlog_in_core_t *iclog, *prev_iclog=NULL;
1190 xfs_buf_t *bp;
1191 int i;
1192 int iclogsize;
1193
1194 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1195
1196 log->l_mp = mp;
1197 log->l_targ = log_target;
1198 log->l_logsize = BBTOB(num_bblks);
1199 log->l_logBBstart = blk_offset;
1200 log->l_logBBsize = num_bblks;
1201 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1202 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1203
1204 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001205 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1207 log->l_last_sync_lsn = log->l_tail_lsn;
1208 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1209 log->l_grant_reserve_cycle = 1;
1210 log->l_grant_write_cycle = 1;
1211
Eric Sandeen62118702008-03-06 13:44:28 +11001212 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1214 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1215 /* for larger sector sizes, must have v2 or external log */
1216 ASSERT(log->l_sectbb_log == 0 ||
1217 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001218 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1220 }
1221 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1222
1223 xlog_get_iclog_buffer_size(mp, log);
1224
1225 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1226 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1227 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1228 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1229 ASSERT(XFS_BUF_ISBUSY(bp));
1230 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1231 log->l_xbuf = bp;
1232
Eric Sandeen007c61c2007-10-11 17:43:56 +10001233 spin_lock_init(&log->l_icloglock);
1234 spin_lock_init(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 initnsema(&log->l_flushsema, 0, "ic-flush");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1238 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1239
1240 iclogp = &log->l_iclog;
1241 /*
1242 * The amount of memory to allocate for the iclog structure is
1243 * rather funky due to the way the structure is defined. It is
1244 * done this way so that we can use different sizes for machines
1245 * with different amounts of memory. See the definition of
1246 * xlog_in_core_t in xfs_log_priv.h for details.
1247 */
1248 iclogsize = log->l_iclog_size;
1249 ASSERT(log->l_iclog_size >= 4096);
1250 for (i=0; i < log->l_iclog_bufs; i++) {
1251 *iclogp = (xlog_in_core_t *)
1252 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1253 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 iclog->ic_prev = prev_iclog;
1255 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001256
1257 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1258 if (!XFS_BUF_CPSEMA(bp))
1259 ASSERT(0);
1260 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1261 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1262 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1263 iclog->ic_bp = bp;
1264 iclog->hic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001265#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001267#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 head = &iclog->ic_header;
1269 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001270 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1271 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001272 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001273 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001275 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1279 iclog->ic_state = XLOG_STATE_ACTIVE;
1280 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001281 atomic_set(&iclog->ic_refcnt, 0);
1282 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 iclog->ic_callback_tail = &(iclog->ic_callback);
1284 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1285
1286 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1287 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1288 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1289 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1290
1291 iclogp = &iclog->ic_next;
1292 }
1293 *iclogp = log->l_iclog; /* complete ring */
1294 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1295
1296 return log;
1297} /* xlog_alloc_log */
1298
1299
1300/*
1301 * Write out the commit record of a transaction associated with the given
1302 * ticket. Return the lsn of the commit record.
1303 */
1304STATIC int
1305xlog_commit_record(xfs_mount_t *mp,
1306 xlog_ticket_t *ticket,
1307 xlog_in_core_t **iclog,
1308 xfs_lsn_t *commitlsnp)
1309{
1310 int error;
1311 xfs_log_iovec_t reg[1];
1312
1313 reg[0].i_addr = NULL;
1314 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001315 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 ASSERT_ALWAYS(iclog);
1318 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1319 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001320 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001322 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323} /* xlog_commit_record */
1324
1325
1326/*
1327 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1328 * log space. This code pushes on the lsn which would supposedly free up
1329 * the 25% which we want to leave free. We may need to adopt a policy which
1330 * pushes on an lsn which is further along in the log once we reach the high
1331 * water mark. In this manner, we would be creating a low water mark.
1332 */
David Chinnera8272ce2007-11-23 16:28:09 +11001333STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334xlog_grant_push_ail(xfs_mount_t *mp,
1335 int need_bytes)
1336{
1337 xlog_t *log = mp->m_log; /* pointer to the log */
1338 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1339 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1340 int free_blocks; /* free blocks left to write to */
1341 int free_bytes; /* free bytes left to write to */
1342 int threshold_block; /* block in lsn we'd like to be at */
1343 int threshold_cycle; /* lsn cycle we'd like to be at */
1344 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1347
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001348 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 free_bytes = xlog_space_left(log,
1350 log->l_grant_reserve_cycle,
1351 log->l_grant_reserve_bytes);
1352 tail_lsn = log->l_tail_lsn;
1353 free_blocks = BTOBBT(free_bytes);
1354
1355 /*
1356 * Set the threshold for the minimum number of free blocks in the
1357 * log to the maximum of what the caller needs, one quarter of the
1358 * log, and 256 blocks.
1359 */
1360 free_threshold = BTOBB(need_bytes);
1361 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1362 free_threshold = MAX(free_threshold, 256);
1363 if (free_blocks < free_threshold) {
1364 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1365 threshold_cycle = CYCLE_LSN(tail_lsn);
1366 if (threshold_block >= log->l_logBBsize) {
1367 threshold_block -= log->l_logBBsize;
1368 threshold_cycle += 1;
1369 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001370 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* Don't pass in an lsn greater than the lsn of the last
1373 * log record known to be on disk.
1374 */
1375 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1376 threshold_lsn = log->l_last_sync_lsn;
1377 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001378 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /*
1381 * Get the transaction layer to kick the dirty buffers out to
1382 * disk asynchronously. No point in trying to do this if
1383 * the filesystem is shutting down.
1384 */
1385 if (threshold_lsn &&
1386 !XLOG_FORCED_SHUTDOWN(log))
1387 xfs_trans_push_ail(mp, threshold_lsn);
1388} /* xlog_grant_push_ail */
1389
1390
1391/*
1392 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1393 * fashion. Previously, we should have moved the current iclog
1394 * ptr in the log to point to the next available iclog. This allows further
1395 * write to continue while this code syncs out an iclog ready to go.
1396 * Before an in-core log can be written out, the data section must be scanned
1397 * to save away the 1st word of each BBSIZE block into the header. We replace
1398 * it with the current cycle count. Each BBSIZE block is tagged with the
1399 * cycle count because there in an implicit assumption that drives will
1400 * guarantee that entire 512 byte blocks get written at once. In other words,
1401 * we can't have part of a 512 byte block written and part not written. By
1402 * tagging each block, we will know which blocks are valid when recovering
1403 * after an unclean shutdown.
1404 *
1405 * This routine is single threaded on the iclog. No other thread can be in
1406 * this routine with the same iclog. Changing contents of iclog can there-
1407 * fore be done without grabbing the state machine lock. Updating the global
1408 * log will require grabbing the lock though.
1409 *
1410 * The entire log manager uses a logical block numbering scheme. Only
1411 * log_sync (and then only bwrite()) know about the fact that the log may
1412 * not start with block zero on a given device. The log block start offset
1413 * is added immediately before calling bwrite().
1414 */
1415
David Chinnera8272ce2007-11-23 16:28:09 +11001416STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417xlog_sync(xlog_t *log,
1418 xlog_in_core_t *iclog)
1419{
1420 xfs_caddr_t dptr; /* pointer to byte sized element */
1421 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001422 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 uint count; /* byte count of bwrite */
1424 uint count_init; /* initial count before roundup */
1425 int roundoff; /* roundoff to BB or stripe */
1426 int split = 0; /* split write into two regions */
1427 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001428 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001431 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* Add for LR header */
1434 count_init = log->l_iclog_hsize + iclog->ic_offset;
1435
1436 /* Round out the log write size */
1437 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1438 /* we have a v2 stripe unit to use */
1439 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1440 } else {
1441 count = BBTOB(BTOBB(count_init));
1442 }
1443 roundoff = count - count_init;
1444 ASSERT(roundoff >= 0);
1445 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1446 roundoff < log->l_mp->m_sb.sb_logsunit)
1447 ||
1448 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1449 roundoff < BBTOB(1)));
1450
1451 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001452 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001453 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001454 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* put cycle number in every block */
1457 xlog_pack_data(log, iclog, roundoff);
1458
1459 /* real byte length */
1460 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001461 iclog->ic_header.h_len =
1462 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001464 iclog->ic_header.h_len =
1465 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Nathan Scottf5faad72006-07-28 17:04:44 +10001468 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1470 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001471 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1474
1475 /* Do we need to split this write into 2 parts? */
1476 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1477 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1478 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1479 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1480 } else {
1481 iclog->ic_bwritecnt = 1;
1482 }
David Chinner511105b2007-05-24 15:21:57 +10001483 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001485 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 XFS_BUF_BUSY(bp);
1487 XFS_BUF_ASYNC(bp);
1488 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001489 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001490 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001492 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001493 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1496 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1497
1498 xlog_verify_iclog(log, iclog, count, B_TRUE);
1499
1500 /* account for log which doesn't start at block #0 */
1501 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1502 /*
1503 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1504 * is shutting down.
1505 */
1506 XFS_BUF_WRITE(bp);
1507
1508 if ((error = XFS_bwrite(bp))) {
1509 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1510 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001511 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001514 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1516 (unsigned long)1);
1517 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1518 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1519 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1520 (__psint_t)count), split);
1521 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001522 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 XFS_BUF_BUSY(bp);
1524 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001525 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1526 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 dptr = XFS_BUF_PTR(bp);
1528 /*
1529 * Bump the cycle numbers at the start of each block
1530 * since this part of the buffer is at the start of
1531 * a new cycle. Watch out for the header magic number
1532 * case, though.
1533 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001534 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001535 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001536 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001537 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 dptr += BBSIZE;
1539 }
1540
1541 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1542 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1543
Nathan Scottc41564b2006-03-29 08:55:14 +10001544 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1546 XFS_BUF_WRITE(bp);
1547 if ((error = XFS_bwrite(bp))) {
1548 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1549 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001550 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554} /* xlog_sync */
1555
1556
1557/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001558 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 */
David Chinnera8272ce2007-11-23 16:28:09 +11001560STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001561xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int i;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 iclog = log->l_iclog;
1567 for (i=0; i<log->l_iclog_bufs; i++) {
1568 sv_destroy(&iclog->ic_forcesema);
1569 sv_destroy(&iclog->ic_writesema);
1570 xfs_buf_free(iclog->ic_bp);
1571#ifdef XFS_LOG_TRACE
1572 if (iclog->ic_trace != NULL) {
1573 ktrace_free(iclog->ic_trace);
1574 }
1575#endif
1576 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001577 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 iclog = next_iclog;
1579 }
1580 freesema(&log->l_flushsema);
1581 spinlock_destroy(&log->l_icloglock);
1582 spinlock_destroy(&log->l_grant_lock);
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 xfs_buf_free(log->l_xbuf);
1585#ifdef XFS_LOG_TRACE
1586 if (log->l_trace != NULL) {
1587 ktrace_free(log->l_trace);
1588 }
1589 if (log->l_grant_trace != NULL) {
1590 ktrace_free(log->l_grant_trace);
1591 }
1592#endif
1593 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001594 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001595} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597/*
1598 * Update counters atomically now that memcpy is done.
1599 */
1600/* ARGSUSED */
1601static inline void
1602xlog_state_finish_copy(xlog_t *log,
1603 xlog_in_core_t *iclog,
1604 int record_cnt,
1605 int copy_bytes)
1606{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001607 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001609 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 iclog->ic_offset += copy_bytes;
1611
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001612 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613} /* xlog_state_finish_copy */
1614
1615
1616
1617
1618/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001619 * print out info relating to regions written which consume
1620 * the reservation
1621 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001622STATIC void
1623xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1624{
1625 uint i;
1626 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1627
1628 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1629 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1630 "bformat",
1631 "bchunk",
1632 "efi_format",
1633 "efd_format",
1634 "iformat",
1635 "icore",
1636 "iext",
1637 "ibroot",
1638 "ilocal",
1639 "iattr_ext",
1640 "iattr_broot",
1641 "iattr_local",
1642 "qformat",
1643 "dquot",
1644 "quotaoff",
1645 "LR header",
1646 "unmount",
1647 "commit",
1648 "trans header"
1649 };
1650 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1651 "SETATTR_NOT_SIZE",
1652 "SETATTR_SIZE",
1653 "INACTIVE",
1654 "CREATE",
1655 "CREATE_TRUNC",
1656 "TRUNCATE_FILE",
1657 "REMOVE",
1658 "LINK",
1659 "RENAME",
1660 "MKDIR",
1661 "RMDIR",
1662 "SYMLINK",
1663 "SET_DMATTRS",
1664 "GROWFS",
1665 "STRAT_WRITE",
1666 "DIOSTRAT",
1667 "WRITE_SYNC",
1668 "WRITEID",
1669 "ADDAFORK",
1670 "ATTRINVAL",
1671 "ATRUNCATE",
1672 "ATTR_SET",
1673 "ATTR_RM",
1674 "ATTR_FLAG",
1675 "CLEAR_AGI_BUCKET",
1676 "QM_SBCHANGE",
1677 "DUMMY1",
1678 "DUMMY2",
1679 "QM_QUOTAOFF",
1680 "QM_DQALLOC",
1681 "QM_SETQLIM",
1682 "QM_DQCLUSTER",
1683 "QM_QINOCREATE",
1684 "QM_QUOTAOFF_END",
1685 "SB_UNIT",
1686 "FSYNC_TS",
1687 "GROWFSRT_ALLOC",
1688 "GROWFSRT_ZERO",
1689 "GROWFSRT_FREE",
1690 "SWAPEXT"
1691 };
1692
1693 xfs_fs_cmn_err(CE_WARN, mp,
1694 "xfs_log_write: reservation summary:\n"
1695 " trans type = %s (%u)\n"
1696 " unit res = %d bytes\n"
1697 " current res = %d bytes\n"
1698 " total reg = %u bytes (o/flow = %u bytes)\n"
1699 " ophdrs = %u (ophdr space = %u bytes)\n"
1700 " ophdr + reg = %u bytes\n"
1701 " num regions = %u\n",
1702 ((ticket->t_trans_type <= 0 ||
1703 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1704 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1705 ticket->t_trans_type,
1706 ticket->t_unit_res,
1707 ticket->t_curr_res,
1708 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1709 ticket->t_res_num_ophdrs, ophdr_spc,
1710 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001711 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001712 ticket->t_res_num);
1713
1714 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001715 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001716 cmn_err(CE_WARN,
1717 "region[%u]: %s - %u bytes\n",
1718 i,
1719 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1720 "bad-rtype" : res_type_str[r_type-1]),
1721 ticket->t_res_arr[i].r_len);
1722 }
1723}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001724
1725/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 * Write some region out to in-core log
1727 *
1728 * This will be called when writing externally provided regions or when
1729 * writing out a commit record for a given transaction.
1730 *
1731 * General algorithm:
1732 * 1. Find total length of this write. This may include adding to the
1733 * lengths passed in.
1734 * 2. Check whether we violate the tickets reservation.
1735 * 3. While writing to this iclog
1736 * A. Reserve as much space in this iclog as can get
1737 * B. If this is first write, save away start lsn
1738 * C. While writing this region:
1739 * 1. If first write of transaction, write start record
1740 * 2. Write log operation header (header per region)
1741 * 3. Find out if we can fit entire region into this iclog
1742 * 4. Potentially, verify destination memcpy ptr
1743 * 5. Memcpy (partial) region
1744 * 6. If partial copy, release iclog; otherwise, continue
1745 * copying more regions into current iclog
1746 * 4. Mark want sync bit (in simulation mode)
1747 * 5. Release iclog for potential flush to on-disk log.
1748 *
1749 * ERRORS:
1750 * 1. Panic if reservation is overrun. This should never happen since
1751 * reservation amounts are generated internal to the filesystem.
1752 * NOTES:
1753 * 1. Tickets are single threaded data structures.
1754 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1755 * syncing routine. When a single log_write region needs to span
1756 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1757 * on all log operation writes which don't contain the end of the
1758 * region. The XLOG_END_TRANS bit is used for the in-core log
1759 * operation which contains the end of the continued log_write region.
1760 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1761 * we don't really know exactly how much space will be used. As a result,
1762 * we don't update ic_offset until the end when we know exactly how many
1763 * bytes have been written out.
1764 */
David Chinnera8272ce2007-11-23 16:28:09 +11001765STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766xlog_write(xfs_mount_t * mp,
1767 xfs_log_iovec_t reg[],
1768 int nentries,
1769 xfs_log_ticket_t tic,
1770 xfs_lsn_t *start_lsn,
1771 xlog_in_core_t **commit_iclog,
1772 uint flags)
1773{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001774 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001776 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 __psint_t ptr; /* copy address into data region */
1779 int len; /* # xlog_write() bytes 2 still copy */
1780 int index; /* region index currently copying */
1781 int log_offset; /* offset (from 0) into data region */
1782 int start_rec_copy; /* # bytes to copy for start record */
1783 int partial_copy; /* did we split a region? */
1784 int partial_copy_len;/* # bytes copied if split region */
1785 int need_copy; /* # bytes need to memcpy this region */
1786 int copy_len; /* # bytes actually memcpy'ing */
1787 int copy_off; /* # bytes from entry start */
1788 int contwr; /* continued write of in-core log? */
1789 int error;
1790 int record_cnt = 0, data_cnt = 0;
1791
1792 partial_copy_len = partial_copy = 0;
1793
1794 /* Calculate potential maximum space. Each region gets its own
1795 * xlog_op_header_t and may need to be double word aligned.
1796 */
1797 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001798 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001800 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 for (index = 0; index < nentries; index++) {
1804 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001805 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001807 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809 contwr = *start_lsn = 0;
1810
1811 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001812 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813#ifdef DEBUG
1814 xlog_panic(
1815 "xfs_log_write: reservation ran out. Need to up reservation");
1816#else
1817 /* Customer configurable panic */
1818 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1819 "xfs_log_write: reservation ran out. Need to up reservation");
1820 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001821 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822#endif
1823 } else
1824 ticket->t_curr_res -= len;
1825
1826 for (index = 0; index < nentries; ) {
1827 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1828 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001829 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 ASSERT(log_offset <= iclog->ic_size - 1);
1832 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1833
1834 /* start_lsn is the first lsn written to. That's all we need. */
1835 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001836 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* This loop writes out as many regions as can fit in the amount
1839 * of space which was allocated by xlog_state_get_iclog_space().
1840 */
1841 while (index < nentries) {
1842 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1843 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1844 start_rec_copy = 0;
1845
1846 /* If first write for transaction, insert start record.
1847 * We can't be trying to commit if we are inited. We can't
1848 * have any "partial_copy" if we are inited.
1849 */
1850 if (ticket->t_flags & XLOG_TIC_INITED) {
1851 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001852 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 logop_head->oh_clientid = ticket->t_clientid;
1854 logop_head->oh_len = 0;
1855 logop_head->oh_flags = XLOG_START_TRANS;
1856 logop_head->oh_res2 = 0;
1857 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1858 record_cnt++;
1859
1860 start_rec_copy = sizeof(xlog_op_header_t);
1861 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1862 }
1863
1864 /* Copy log operation header directly into data section */
1865 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001866 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 logop_head->oh_clientid = ticket->t_clientid;
1868 logop_head->oh_res2 = 0;
1869
1870 /* header copied directly */
1871 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1872
1873 /* are we copying a commit or unmount record? */
1874 logop_head->oh_flags = flags;
1875
1876 /*
1877 * We've seen logs corrupted with bad transaction client
1878 * ids. This makes sure that XFS doesn't generate them on.
1879 * Turn this into an EIO and shut down the filesystem.
1880 */
1881 switch (logop_head->oh_clientid) {
1882 case XFS_TRANSACTION:
1883 case XFS_VOLUME:
1884 case XFS_LOG:
1885 break;
1886 default:
1887 xfs_fs_cmn_err(CE_WARN, mp,
1888 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1889 logop_head->oh_clientid, tic);
1890 return XFS_ERROR(EIO);
1891 }
1892
1893 /* Partial write last time? => (partial_copy != 0)
1894 * need_copy is the amount we'd like to copy if everything could
1895 * fit in the current memcpy.
1896 */
1897 need_copy = reg[index].i_len - partial_copy_len;
1898
1899 copy_off = partial_copy_len;
1900 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001901 copy_len = need_copy;
1902 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (partial_copy)
1904 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1905 partial_copy_len = partial_copy = 0;
1906 } else { /* partial write */
1907 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001908 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1910 if (partial_copy)
1911 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1912 partial_copy_len += copy_len;
1913 partial_copy++;
1914 len += sizeof(xlog_op_header_t); /* from splitting of region */
1915 /* account for new log op header */
1916 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001917 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919 xlog_verify_dest_ptr(log, ptr);
1920
1921 /* copy region */
1922 ASSERT(copy_len >= 0);
1923 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1924 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1925
1926 /* make copy_len total bytes copied, including headers */
1927 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1928 record_cnt++;
1929 data_cnt += contwr ? copy_len : 0;
1930 if (partial_copy) { /* copied partial region */
1931 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1932 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1933 record_cnt = data_cnt = 0;
1934 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001935 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 break; /* don't increment index */
1937 } else { /* copied entire region */
1938 index++;
1939 partial_copy_len = partial_copy = 0;
1940
1941 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1942 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1943 record_cnt = data_cnt = 0;
1944 xlog_state_want_sync(log, iclog);
1945 if (commit_iclog) {
1946 ASSERT(flags & XLOG_COMMIT_TRANS);
1947 *commit_iclog = iclog;
1948 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001949 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (index == nentries)
1951 return 0; /* we are done */
1952 else
1953 break;
1954 }
1955 } /* if (partial_copy) */
1956 } /* while (index < nentries) */
1957 } /* for (index = 0; index < nentries; ) */
1958 ASSERT(len == 0);
1959
1960 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1961 if (commit_iclog) {
1962 ASSERT(flags & XLOG_COMMIT_TRANS);
1963 *commit_iclog = iclog;
1964 return 0;
1965 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001966 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967} /* xlog_write */
1968
1969
1970/*****************************************************************************
1971 *
1972 * State Machine functions
1973 *
1974 *****************************************************************************
1975 */
1976
1977/* Clean iclogs starting from the head. This ordering must be
1978 * maintained, so an iclog doesn't become ACTIVE beyond one that
1979 * is SYNCING. This is also required to maintain the notion that we use
1980 * a counting semaphore to hold off would be writers to the log when every
1981 * iclog is trying to sync to disk.
1982 *
1983 * State Change: DIRTY -> ACTIVE
1984 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001985STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986xlog_state_clean_log(xlog_t *log)
1987{
1988 xlog_in_core_t *iclog;
1989 int changed = 0;
1990
1991 iclog = log->l_iclog;
1992 do {
1993 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1994 iclog->ic_state = XLOG_STATE_ACTIVE;
1995 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001996 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /*
1998 * If the number of ops in this iclog indicate it just
1999 * contains the dummy transaction, we can
2000 * change state into IDLE (the second time around).
2001 * Otherwise we should change the state into
2002 * NEED a dummy.
2003 * We don't need to cover the dummy.
2004 */
2005 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002006 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2007 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 changed = 1;
2009 } else {
2010 /*
2011 * We have two dirty iclogs so start over
2012 * This could also be num of ops indicates
2013 * this is not the dummy going out.
2014 */
2015 changed = 2;
2016 }
2017 iclog->ic_header.h_num_logops = 0;
2018 memset(iclog->ic_header.h_cycle_data, 0,
2019 sizeof(iclog->ic_header.h_cycle_data));
2020 iclog->ic_header.h_lsn = 0;
2021 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2022 /* do nothing */;
2023 else
2024 break; /* stop cleaning */
2025 iclog = iclog->ic_next;
2026 } while (iclog != log->l_iclog);
2027
2028 /* log is locked when we are called */
2029 /*
2030 * Change state for the dummy log recording.
2031 * We usually go to NEED. But we go to NEED2 if the changed indicates
2032 * we are done writing the dummy record.
2033 * If we are done with the second dummy recored (DONE2), then
2034 * we go to IDLE.
2035 */
2036 if (changed) {
2037 switch (log->l_covered_state) {
2038 case XLOG_STATE_COVER_IDLE:
2039 case XLOG_STATE_COVER_NEED:
2040 case XLOG_STATE_COVER_NEED2:
2041 log->l_covered_state = XLOG_STATE_COVER_NEED;
2042 break;
2043
2044 case XLOG_STATE_COVER_DONE:
2045 if (changed == 1)
2046 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2047 else
2048 log->l_covered_state = XLOG_STATE_COVER_NEED;
2049 break;
2050
2051 case XLOG_STATE_COVER_DONE2:
2052 if (changed == 1)
2053 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2054 else
2055 log->l_covered_state = XLOG_STATE_COVER_NEED;
2056 break;
2057
2058 default:
2059 ASSERT(0);
2060 }
2061 }
2062} /* xlog_state_clean_log */
2063
2064STATIC xfs_lsn_t
2065xlog_get_lowest_lsn(
2066 xlog_t *log)
2067{
2068 xlog_in_core_t *lsn_log;
2069 xfs_lsn_t lowest_lsn, lsn;
2070
2071 lsn_log = log->l_iclog;
2072 lowest_lsn = 0;
2073 do {
2074 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002075 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if ((lsn && !lowest_lsn) ||
2077 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2078 lowest_lsn = lsn;
2079 }
2080 }
2081 lsn_log = lsn_log->ic_next;
2082 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002083 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
2086
2087STATIC void
2088xlog_state_do_callback(
2089 xlog_t *log,
2090 int aborted,
2091 xlog_in_core_t *ciclog)
2092{
2093 xlog_in_core_t *iclog;
2094 xlog_in_core_t *first_iclog; /* used to know when we've
2095 * processed all iclogs once */
2096 xfs_log_callback_t *cb, *cb_next;
2097 int flushcnt = 0;
2098 xfs_lsn_t lowest_lsn;
2099 int ioerrors; /* counter: iclogs with errors */
2100 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2101 int funcdidcallbacks; /* flag: function did callbacks */
2102 int repeats; /* for issuing console warnings if
2103 * looping too many times */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002105 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 first_iclog = iclog = log->l_iclog;
2107 ioerrors = 0;
2108 funcdidcallbacks = 0;
2109 repeats = 0;
2110
2111 do {
2112 /*
2113 * Scan all iclogs starting with the one pointed to by the
2114 * log. Reset this starting point each time the log is
2115 * unlocked (during callbacks).
2116 *
2117 * Keep looping through iclogs until one full pass is made
2118 * without running any callbacks.
2119 */
2120 first_iclog = log->l_iclog;
2121 iclog = log->l_iclog;
2122 loopdidcallbacks = 0;
2123 repeats++;
2124
2125 do {
2126
2127 /* skip all iclogs in the ACTIVE & DIRTY states */
2128 if (iclog->ic_state &
2129 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2130 iclog = iclog->ic_next;
2131 continue;
2132 }
2133
2134 /*
2135 * Between marking a filesystem SHUTDOWN and stopping
2136 * the log, we do flush all iclogs to disk (if there
2137 * wasn't a log I/O error). So, we do want things to
2138 * go smoothly in case of just a SHUTDOWN w/o a
2139 * LOG_IO_ERROR.
2140 */
2141 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2142 /*
2143 * Can only perform callbacks in order. Since
2144 * this iclog is not in the DONE_SYNC/
2145 * DO_CALLBACK state, we skip the rest and
2146 * just try to clean up. If we set our iclog
2147 * to DO_CALLBACK, we will not process it when
2148 * we retry since a previous iclog is in the
2149 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002150 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 */
2152 if (!(iclog->ic_state &
2153 (XLOG_STATE_DONE_SYNC |
2154 XLOG_STATE_DO_CALLBACK))) {
2155 if (ciclog && (ciclog->ic_state ==
2156 XLOG_STATE_DONE_SYNC)) {
2157 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2158 }
2159 break;
2160 }
2161 /*
2162 * We now have an iclog that is in either the
2163 * DO_CALLBACK or DONE_SYNC states. The other
2164 * states (WANT_SYNC, SYNCING, or CALLBACK were
2165 * caught by the above if and are going to
2166 * clean (i.e. we aren't doing their callbacks)
2167 * see the above if.
2168 */
2169
2170 /*
2171 * We will do one more check here to see if we
2172 * have chased our tail around.
2173 */
2174
2175 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002176 if (lowest_lsn &&
2177 XFS_LSN_CMP(lowest_lsn,
2178 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 iclog = iclog->ic_next;
2180 continue; /* Leave this iclog for
2181 * another thread */
2182 }
2183
2184 iclog->ic_state = XLOG_STATE_CALLBACK;
2185
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002186 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002189 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * No one else can be here except us.
2191 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002192 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002193 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2194 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2195 log->l_last_sync_lsn =
2196 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002197 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002200 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 ioerrors++;
2202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
David Chinner114d23a2008-04-10 12:18:39 +10002204 /*
2205 * Keep processing entries in the callback list until
2206 * we come around and it is empty. We need to
2207 * atomically see that the list is empty and change the
2208 * state to DIRTY so that we don't miss any more
2209 * callbacks being added.
2210 */
2211 spin_lock(&iclog->ic_callback_lock);
2212 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002213 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 iclog->ic_callback_tail = &(iclog->ic_callback);
2215 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002216 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002219 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 cb_next = cb->cb_next;
2221 cb->cb_func(cb->cb_arg, aborted);
2222 }
David Chinner114d23a2008-04-10 12:18:39 +10002223 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 cb = iclog->ic_callback;
2225 }
2226
2227 loopdidcallbacks++;
2228 funcdidcallbacks++;
2229
David Chinner114d23a2008-04-10 12:18:39 +10002230 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002231 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002232 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2234 iclog->ic_state = XLOG_STATE_DIRTY;
2235
2236 /*
2237 * Transition from DIRTY to ACTIVE if applicable.
2238 * NOP if STATE_IOERROR.
2239 */
2240 xlog_state_clean_log(log);
2241
2242 /* wake up threads waiting in xfs_log_force() */
2243 sv_broadcast(&iclog->ic_forcesema);
2244
2245 iclog = iclog->ic_next;
2246 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002247
2248 if (repeats > 5000) {
2249 flushcnt += repeats;
2250 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002252 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002253 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 } while (!ioerrors && loopdidcallbacks);
2256
2257 /*
2258 * make one last gasp attempt to see if iclogs are being left in
2259 * limbo..
2260 */
2261#ifdef DEBUG
2262 if (funcdidcallbacks) {
2263 first_iclog = iclog = log->l_iclog;
2264 do {
2265 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2266 /*
2267 * Terminate the loop if iclogs are found in states
2268 * which will cause other threads to clean up iclogs.
2269 *
2270 * SYNCING - i/o completion will go through logs
2271 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002272 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 * IOERROR - give up hope all ye who enter here
2274 */
2275 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2276 iclog->ic_state == XLOG_STATE_SYNCING ||
2277 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2278 iclog->ic_state == XLOG_STATE_IOERROR )
2279 break;
2280 iclog = iclog->ic_next;
2281 } while (first_iclog != iclog);
2282 }
2283#endif
2284
Nathan Scotta3c66852006-09-28 11:02:14 +10002285 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2287 flushcnt = log->l_flushcnt;
2288 log->l_flushcnt = 0;
2289 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002290 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 while (flushcnt--)
2292 vsema(&log->l_flushsema);
2293} /* xlog_state_do_callback */
2294
2295
2296/*
2297 * Finish transitioning this iclog to the dirty state.
2298 *
2299 * Make sure that we completely execute this routine only when this is
2300 * the last call to the iclog. There is a good chance that iclog flushes,
2301 * when we reach the end of the physical log, get turned into 2 separate
2302 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2303 * routine. By using the reference count bwritecnt, we guarantee that only
2304 * the second completion goes through.
2305 *
2306 * Callbacks could take time, so they are done outside the scope of the
2307 * global state machine log lock. Assume that the calls to cvsema won't
2308 * take a long time. At least we know it won't sleep.
2309 */
David Chinnera8272ce2007-11-23 16:28:09 +11002310STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311xlog_state_done_syncing(
2312 xlog_in_core_t *iclog,
2313 int aborted)
2314{
2315 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002317 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2320 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002321 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2323
2324
2325 /*
2326 * If we got an error, either on the first buffer, or in the case of
2327 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2328 * and none should ever be attempted to be written to disk
2329 * again.
2330 */
2331 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2332 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002333 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return;
2335 }
2336 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2337 }
2338
2339 /*
2340 * Someone could be sleeping prior to writing out the next
2341 * iclog buffer, we wake them all, one will get to do the
2342 * I/O, the others get to wait for the result.
2343 */
2344 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002345 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2347} /* xlog_state_done_syncing */
2348
2349
2350/*
2351 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2352 * sleep. The flush semaphore is set to the number of in-core buffers and
2353 * decremented around disk syncing. Therefore, if all buffers are syncing,
2354 * this semaphore will cause new writes to sleep until a sync completes.
2355 * Otherwise, this code just does p() followed by v(). This approximates
2356 * a sleep/wakeup except we can't race.
2357 *
2358 * The in-core logs are used in a circular fashion. They are not used
2359 * out-of-order even when an iclog past the head is free.
2360 *
2361 * return:
2362 * * log_offset where xlog_write() can start writing into the in-core
2363 * log's data space.
2364 * * in-core log pointer to which xlog_write() should write.
2365 * * boolean indicating this is a continued write to an in-core log.
2366 * If this is the last write, then the in-core log's offset field
2367 * needs to be incremented, depending on the amount of data which
2368 * is copied.
2369 */
David Chinnera8272ce2007-11-23 16:28:09 +11002370STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371xlog_state_get_iclog_space(xlog_t *log,
2372 int len,
2373 xlog_in_core_t **iclogp,
2374 xlog_ticket_t *ticket,
2375 int *continued_write,
2376 int *logoffsetp)
2377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 int log_offset;
2379 xlog_rec_header_t *head;
2380 xlog_in_core_t *iclog;
2381 int error;
2382
2383restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002384 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002386 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return XFS_ERROR(EIO);
2388 }
2389
2390 iclog = log->l_iclog;
2391 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2392 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002393 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2395 XFS_STATS_INC(xs_log_noiclogs);
2396 /* Ensure that log writes happen */
2397 psema(&log->l_flushsema, PINOD);
2398 goto restart;
2399 }
2400 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2401 head = &iclog->ic_header;
2402
David Chinner155cc6b2008-03-06 13:44:14 +11002403 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 log_offset = iclog->ic_offset;
2405
2406 /* On the 1st write to an iclog, figure out lsn. This works
2407 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2408 * committing to. If the offset is set, that's how many blocks
2409 * must be written.
2410 */
2411 if (log_offset == 0) {
2412 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002413 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002414 log->l_iclog_hsize,
2415 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002416 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2417 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002418 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 ASSERT(log->l_curr_block >= 0);
2420 }
2421
2422 /* If there is enough room to write everything, then do it. Otherwise,
2423 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2424 * bit is on, so this will get flushed out. Don't update ic_offset
2425 * until you know exactly how many bytes get copied. Therefore, wait
2426 * until later to update ic_offset.
2427 *
2428 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2429 * can fit into remaining data section.
2430 */
2431 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2432 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2433
Dave Chinner49641f12008-07-11 17:43:55 +10002434 /*
2435 * If I'm the only one writing to this iclog, sync it to disk.
2436 * We need to do an atomic compare and decrement here to avoid
2437 * racing with concurrent atomic_dec_and_lock() calls in
2438 * xlog_state_release_iclog() when there is more than one
2439 * reference to the iclog.
2440 */
2441 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2442 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002443 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002444 error = xlog_state_release_iclog(log, iclog);
2445 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002446 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002448 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450 goto restart;
2451 }
2452
2453 /* Do we have enough room to write the full amount in the remainder
2454 * of this iclog? Or must we continue a write on the next iclog and
2455 * mark this iclog as completely taken? In the case where we switch
2456 * iclogs (to mark it taken), this particular iclog will release/sync
2457 * to disk in xlog_write().
2458 */
2459 if (len <= iclog->ic_size - iclog->ic_offset) {
2460 *continued_write = 0;
2461 iclog->ic_offset += len;
2462 } else {
2463 *continued_write = 1;
2464 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2465 }
2466 *iclogp = iclog;
2467
2468 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002469 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 *logoffsetp = log_offset;
2472 return 0;
2473} /* xlog_state_get_iclog_space */
2474
2475/*
2476 * Atomically get the log space required for a log ticket.
2477 *
2478 * Once a ticket gets put onto the reserveq, it will only return after
2479 * the needed reservation is satisfied.
2480 */
2481STATIC int
2482xlog_grant_log_space(xlog_t *log,
2483 xlog_ticket_t *tic)
2484{
2485 int free_bytes;
2486 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487#ifdef DEBUG
2488 xfs_lsn_t tail_lsn;
2489#endif
2490
2491
2492#ifdef DEBUG
2493 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2494 panic("grant Recovery problem");
2495#endif
2496
2497 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002498 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2500
2501 /* something is already sleeping; insert new transaction at end */
2502 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002503 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 xlog_trace_loggrant(log, tic,
2505 "xlog_grant_log_space: sleep 1");
2506 /*
2507 * Gotta check this before going to sleep, while we're
2508 * holding the grant lock.
2509 */
2510 if (XLOG_FORCED_SHUTDOWN(log))
2511 goto error_return;
2512
2513 XFS_STATS_INC(xs_sleep_logspace);
2514 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2515 /*
2516 * If we got an error, and the filesystem is shutting down,
2517 * we'll catch it down below. So just continue...
2518 */
2519 xlog_trace_loggrant(log, tic,
2520 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002521 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
2523 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2524 need_bytes = tic->t_unit_res*tic->t_ocnt;
2525 else
2526 need_bytes = tic->t_unit_res;
2527
2528redo:
2529 if (XLOG_FORCED_SHUTDOWN(log))
2530 goto error_return;
2531
2532 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2533 log->l_grant_reserve_bytes);
2534 if (free_bytes < need_bytes) {
2535 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002536 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 xlog_trace_loggrant(log, tic,
2538 "xlog_grant_log_space: sleep 2");
2539 XFS_STATS_INC(xs_sleep_logspace);
2540 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2541
2542 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002543 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 goto error_return;
2545 }
2546
2547 xlog_trace_loggrant(log, tic,
2548 "xlog_grant_log_space: wake 2");
2549 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002550 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 goto redo;
2552 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002553 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002556 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557#ifdef DEBUG
2558 tail_lsn = log->l_tail_lsn;
2559 /*
2560 * Check to make sure the grant write head didn't just over lap the
2561 * tail. If the cycles are the same, we can't be overlapping.
2562 * Otherwise, make sure that the cycles differ by exactly one and
2563 * check the byte count.
2564 */
2565 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2566 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2567 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2568 }
2569#endif
2570 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2571 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002572 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return 0;
2574
2575 error_return:
2576 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002577 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2579 /*
2580 * If we are failing, make sure the ticket doesn't have any
2581 * current reservations. We don't want to add this back when
2582 * the ticket/transaction gets cancelled.
2583 */
2584 tic->t_curr_res = 0;
2585 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002586 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return XFS_ERROR(EIO);
2588} /* xlog_grant_log_space */
2589
2590
2591/*
2592 * Replenish the byte reservation required by moving the grant write head.
2593 *
2594 *
2595 */
2596STATIC int
2597xlog_regrant_write_log_space(xlog_t *log,
2598 xlog_ticket_t *tic)
2599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 int free_bytes, need_bytes;
2601 xlog_ticket_t *ntic;
2602#ifdef DEBUG
2603 xfs_lsn_t tail_lsn;
2604#endif
2605
2606 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002607 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612#ifdef DEBUG
2613 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2614 panic("regrant Recovery problem");
2615#endif
2616
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002617 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2619
2620 if (XLOG_FORCED_SHUTDOWN(log))
2621 goto error_return;
2622
2623 /* If there are other waiters on the queue then give them a
2624 * chance at logspace before us. Wake up the first waiters,
2625 * if we do not wake up all the waiters then go to sleep waiting
2626 * for more free space, otherwise try to get some space for
2627 * this transaction.
2628 */
2629
2630 if ((ntic = log->l_write_headq)) {
2631 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2632 log->l_grant_write_bytes);
2633 do {
2634 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2635
2636 if (free_bytes < ntic->t_unit_res)
2637 break;
2638 free_bytes -= ntic->t_unit_res;
2639 sv_signal(&ntic->t_sema);
2640 ntic = ntic->t_next;
2641 } while (ntic != log->l_write_headq);
2642
2643 if (ntic != log->l_write_headq) {
2644 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002645 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 xlog_trace_loggrant(log, tic,
2648 "xlog_regrant_write_log_space: sleep 1");
2649 XFS_STATS_INC(xs_sleep_logspace);
2650 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2651 &log->l_grant_lock, s);
2652
2653 /* If we're shutting down, this tic is already
2654 * off the queue */
2655 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002656 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 goto error_return;
2658 }
2659
2660 xlog_trace_loggrant(log, tic,
2661 "xlog_regrant_write_log_space: wake 1");
2662 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002663 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 }
2665 }
2666
2667 need_bytes = tic->t_unit_res;
2668
2669redo:
2670 if (XLOG_FORCED_SHUTDOWN(log))
2671 goto error_return;
2672
2673 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2674 log->l_grant_write_bytes);
2675 if (free_bytes < need_bytes) {
2676 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002677 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 XFS_STATS_INC(xs_sleep_logspace);
2679 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2680
2681 /* If we're shutting down, this tic is already off the queue */
2682 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002683 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 goto error_return;
2685 }
2686
2687 xlog_trace_loggrant(log, tic,
2688 "xlog_regrant_write_log_space: wake 2");
2689 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002690 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 goto redo;
2692 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002693 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002695 /* we've got enough space */
2696 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697#ifdef DEBUG
2698 tail_lsn = log->l_tail_lsn;
2699 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2700 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2701 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2702 }
2703#endif
2704
2705 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2706 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002707 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710
2711 error_return:
2712 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002713 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2715 /*
2716 * If we are failing, make sure the ticket doesn't have any
2717 * current reservations. We don't want to add this back when
2718 * the ticket/transaction gets cancelled.
2719 */
2720 tic->t_curr_res = 0;
2721 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002722 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return XFS_ERROR(EIO);
2724} /* xlog_regrant_write_log_space */
2725
2726
2727/* The first cnt-1 times through here we don't need to
2728 * move the grant write head because the permanent
2729 * reservation has reserved cnt times the unit amount.
2730 * Release part of current permanent unit reservation and
2731 * reset current reservation to be one units worth. Also
2732 * move grant reservation head forward.
2733 */
2734STATIC void
2735xlog_regrant_reserve_log_space(xlog_t *log,
2736 xlog_ticket_t *ticket)
2737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 xlog_trace_loggrant(log, ticket,
2739 "xlog_regrant_reserve_log_space: enter");
2740 if (ticket->t_cnt > 0)
2741 ticket->t_cnt--;
2742
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002743 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002744 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002746 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 xlog_trace_loggrant(log, ticket,
2748 "xlog_regrant_reserve_log_space: sub current res");
2749 xlog_verify_grant_head(log, 1);
2750
2751 /* just return if we still have some of the pre-reserved space */
2752 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002753 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return;
2755 }
2756
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002757 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 xlog_trace_loggrant(log, ticket,
2759 "xlog_regrant_reserve_log_space: exit");
2760 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002761 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002763 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764} /* xlog_regrant_reserve_log_space */
2765
2766
2767/*
2768 * Give back the space left from a reservation.
2769 *
2770 * All the information we need to make a correct determination of space left
2771 * is present. For non-permanent reservations, things are quite easy. The
2772 * count should have been decremented to zero. We only need to deal with the
2773 * space remaining in the current reservation part of the ticket. If the
2774 * ticket contains a permanent reservation, there may be left over space which
2775 * needs to be released. A count of N means that N-1 refills of the current
2776 * reservation can be done before we need to ask for more space. The first
2777 * one goes to fill up the first current reservation. Once we run out of
2778 * space, the count will stay at zero and the only space remaining will be
2779 * in the current reservation field.
2780 */
2781STATIC void
2782xlog_ungrant_log_space(xlog_t *log,
2783 xlog_ticket_t *ticket)
2784{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (ticket->t_cnt > 0)
2786 ticket->t_cnt--;
2787
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002788 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2790
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002791 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2794
2795 /* If this is a permanent reservation ticket, we may be able to free
2796 * up more space based on the remaining count.
2797 */
2798 if (ticket->t_cnt > 0) {
2799 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002800 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802
2803 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2804 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002805 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 xfs_log_move_tail(log->l_mp, 1);
2807} /* xlog_ungrant_log_space */
2808
2809
2810/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 * Flush iclog to disk if this is the last reference to the given iclog and
2812 * the WANT_SYNC bit is set.
2813 *
2814 * When this function is entered, the iclog is not necessarily in the
2815 * WANT_SYNC state. It may be sitting around waiting to get filled.
2816 *
2817 *
2818 */
David Chinnera8272ce2007-11-23 16:28:09 +11002819STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002820xlog_state_release_iclog(
2821 xlog_t *log,
2822 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 int sync = 0; /* do we sync? */
2825
David Chinner155cc6b2008-03-06 13:44:14 +11002826 if (iclog->ic_state & XLOG_STATE_IOERROR)
2827 return XFS_ERROR(EIO);
2828
2829 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2830 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2831 return 0;
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002834 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return XFS_ERROR(EIO);
2836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2838 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2839
David Chinner155cc6b2008-03-06 13:44:14 +11002840 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002841 /* update tail before writing to iclog */
2842 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 sync++;
2844 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002845 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2847 /* cycle incremented when incrementing curr_block */
2848 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002849 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 /*
2852 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002853 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2855 * this iclog has consistent data, so we ignore IOERROR
2856 * flags after this point.
2857 */
David Chinnerb5893342008-03-06 13:44:06 +11002858 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861} /* xlog_state_release_iclog */
2862
2863
2864/*
2865 * This routine will mark the current iclog in the ring as WANT_SYNC
2866 * and move the current iclog pointer to the next iclog in the ring.
2867 * When this routine is called from xlog_state_get_iclog_space(), the
2868 * exact size of the iclog has not yet been determined. All we know is
2869 * that every data block. We have run out of space in this log record.
2870 */
2871STATIC void
2872xlog_state_switch_iclogs(xlog_t *log,
2873 xlog_in_core_t *iclog,
2874 int eventual_size)
2875{
2876 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2877 if (!eventual_size)
2878 eventual_size = iclog->ic_offset;
2879 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002880 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 log->l_prev_block = log->l_curr_block;
2882 log->l_prev_cycle = log->l_curr_cycle;
2883
2884 /* roll log?: ic_offset changed later */
2885 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2886
2887 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002888 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 log->l_mp->m_sb.sb_logsunit > 1) {
2890 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2891 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2892 }
2893
2894 if (log->l_curr_block >= log->l_logBBsize) {
2895 log->l_curr_cycle++;
2896 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2897 log->l_curr_cycle++;
2898 log->l_curr_block -= log->l_logBBsize;
2899 ASSERT(log->l_curr_block >= 0);
2900 }
2901 ASSERT(iclog == log->l_iclog);
2902 log->l_iclog = iclog->ic_next;
2903} /* xlog_state_switch_iclogs */
2904
2905
2906/*
2907 * Write out all data in the in-core log as of this exact moment in time.
2908 *
2909 * Data may be written to the in-core log during this call. However,
2910 * we don't guarantee this data will be written out. A change from past
2911 * implementation means this routine will *not* write out zero length LRs.
2912 *
2913 * Basically, we try and perform an intelligent scan of the in-core logs.
2914 * If we determine there is no flushable data, we just return. There is no
2915 * flushable data if:
2916 *
2917 * 1. the current iclog is active and has no data; the previous iclog
2918 * is in the active or dirty state.
2919 * 2. the current iclog is drity, and the previous iclog is in the
2920 * active or dirty state.
2921 *
2922 * We may sleep (call psema) if:
2923 *
2924 * 1. the current iclog is not in the active nor dirty state.
2925 * 2. the current iclog dirty, and the previous iclog is not in the
2926 * active nor dirty state.
2927 * 3. the current iclog is active, and there is another thread writing
2928 * to this particular iclog.
2929 * 4. a) the current iclog is active and has no other writers
2930 * b) when we return from flushing out this iclog, it is still
2931 * not in the active nor dirty state.
2932 */
2933STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002934xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 xlog_in_core_t *iclog;
2937 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002939 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 iclog = log->l_iclog;
2942 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002943 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return XFS_ERROR(EIO);
2945 }
2946
2947 /* If the head iclog is not active nor dirty, we just attach
2948 * ourselves to the head and go to sleep.
2949 */
2950 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2951 iclog->ic_state == XLOG_STATE_DIRTY) {
2952 /*
2953 * If the head is dirty or (active and empty), then
2954 * we need to look at the previous iclog. If the previous
2955 * iclog is active or dirty we are done. There is nothing
2956 * to sync out. Otherwise, we attach ourselves to the
2957 * previous iclog and go to sleep.
2958 */
2959 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002960 (atomic_read(&iclog->ic_refcnt) == 0
2961 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 iclog = iclog->ic_prev;
2963 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2964 iclog->ic_state == XLOG_STATE_DIRTY)
2965 goto no_sleep;
2966 else
2967 goto maybe_sleep;
2968 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002969 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 /* We are the only one with access to this
2971 * iclog. Flush it out now. There should
2972 * be a roundoff of zero to show that someone
2973 * has already taken care of the roundoff from
2974 * the previous sync.
2975 */
David Chinner155cc6b2008-03-06 13:44:14 +11002976 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002977 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002979 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 if (xlog_state_release_iclog(log, iclog))
2982 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002983 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002984 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002985 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 iclog->ic_state != XLOG_STATE_DIRTY)
2987 goto maybe_sleep;
2988 else
2989 goto no_sleep;
2990 } else {
2991 /* Someone else is writing to this iclog.
2992 * Use its call to flush out the data. However,
2993 * the other thread may not force out this LR,
2994 * so we mark it WANT_SYNC.
2995 */
2996 xlog_state_switch_iclogs(log, iclog, 0);
2997 goto maybe_sleep;
2998 }
2999 }
3000 }
3001
3002 /* By the time we come around again, the iclog could've been filled
3003 * which would give it another lsn. If we have a new lsn, just
3004 * return because the relevant data has been flushed.
3005 */
3006maybe_sleep:
3007 if (flags & XFS_LOG_SYNC) {
3008 /*
3009 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003010 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 * Then we check again after waking up, in case our
3012 * sleep was disturbed by a bad news.
3013 */
3014 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003015 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return XFS_ERROR(EIO);
3017 }
3018 XFS_STATS_INC(xs_log_force_sleep);
3019 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3020 /*
3021 * No need to grab the log lock here since we're
3022 * only deciding whether or not to return EIO
3023 * and the memory read should be atomic.
3024 */
3025 if (iclog->ic_state & XLOG_STATE_IOERROR)
3026 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003027 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 } else {
3030
3031no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003032 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034 return 0;
3035} /* xlog_state_sync_all */
3036
3037
3038/*
3039 * Used by code which implements synchronous log forces.
3040 *
3041 * Find in-core log with lsn.
3042 * If it is in the DIRTY state, just return.
3043 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3044 * state and go to sleep or return.
3045 * If it is in any other state, go to sleep or return.
3046 *
3047 * If filesystem activity goes to zero, the iclog will get flushed only by
3048 * bdflush().
3049 */
David Chinnera8272ce2007-11-23 16:28:09 +11003050STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051xlog_state_sync(xlog_t *log,
3052 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003053 uint flags,
3054 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 xlog_in_core_t *iclog;
3057 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003060 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 iclog = log->l_iclog;
3062
3063 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003064 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return XFS_ERROR(EIO);
3066 }
3067
3068 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003069 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3070 iclog = iclog->ic_next;
3071 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
3073
3074 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003075 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return 0;
3077 }
3078
3079 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3080 /*
3081 * We sleep here if we haven't already slept (e.g.
3082 * this is the first time we've looked at the correct
3083 * iclog buf) and the buffer before us is going to
3084 * be sync'ed. The reason for this is that if we
3085 * are doing sync transactions here, by waiting for
3086 * the previous I/O to complete, we can allow a few
3087 * more transactions into this iclog before we close
3088 * it down.
3089 *
3090 * Otherwise, we mark the buffer WANT_SYNC, and bump
3091 * up the refcnt so we can release the log (which drops
3092 * the ref count). The state switch keeps new transaction
3093 * commits from using this buffer. When the current commits
3094 * finish writing into the buffer, the refcount will drop to
3095 * zero and the buffer will go out then.
3096 */
3097 if (!already_slept &&
3098 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3099 XLOG_STATE_SYNCING))) {
3100 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3101 XFS_STATS_INC(xs_log_force_sleep);
3102 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3103 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003104 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 already_slept = 1;
3106 goto try_again;
3107 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003108 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003110 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (xlog_state_release_iclog(log, iclog))
3112 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003113 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003114 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
3116 }
3117
3118 if ((flags & XFS_LOG_SYNC) && /* sleep */
3119 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3120
3121 /*
3122 * Don't wait on the forcesema if we know that we've
3123 * gotten a log write error.
3124 */
3125 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003126 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return XFS_ERROR(EIO);
3128 }
3129 XFS_STATS_INC(xs_log_force_sleep);
3130 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3131 /*
3132 * No need to grab the log lock here since we're
3133 * only deciding whether or not to return EIO
3134 * and the memory read should be atomic.
3135 */
3136 if (iclog->ic_state & XLOG_STATE_IOERROR)
3137 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003138 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003140 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
3142 return 0;
3143
3144 } while (iclog != log->l_iclog);
3145
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003146 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148} /* xlog_state_sync */
3149
3150
3151/*
3152 * Called when we want to mark the current iclog as being ready to sync to
3153 * disk.
3154 */
David Chinnera8272ce2007-11-23 16:28:09 +11003155STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3157{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003158 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3161 xlog_state_switch_iclogs(log, iclog, 0);
3162 } else {
3163 ASSERT(iclog->ic_state &
3164 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3165 }
3166
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003167 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168} /* xlog_state_want_sync */
3169
3170
3171
3172/*****************************************************************************
3173 *
3174 * TICKET functions
3175 *
3176 *****************************************************************************
3177 */
3178
3179/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003180 * Free a used ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 */
3182STATIC void
3183xlog_ticket_put(xlog_t *log,
3184 xlog_ticket_t *ticket)
3185{
3186 sv_destroy(&ticket->t_sema);
David Chinnereb01c9c2008-04-10 12:18:46 +10003187 kmem_zone_free(xfs_log_ticket_zone, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188} /* xlog_ticket_put */
3189
3190
3191/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003192 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 */
David Chinnera8272ce2007-11-23 16:28:09 +11003194STATIC xlog_ticket_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195xlog_ticket_get(xlog_t *log,
3196 int unit_bytes,
3197 int cnt,
3198 char client,
3199 uint xflags)
3200{
3201 xlog_ticket_t *tic;
3202 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
David Chinnereb01c9c2008-04-10 12:18:46 +10003204 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3205 if (!tic)
3206 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 /*
3209 * Permanent reservations have up to 'cnt'-1 active log operations
3210 * in the log. A unit in this case is the amount of space for one
3211 * of these log operations. Normal reservations have a cnt of 1
3212 * and their unit amount is the total amount of space required.
3213 *
3214 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003215 * which occupy space in the on-disk log.
3216 *
3217 * Normal form of a transaction is:
3218 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3219 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3220 *
3221 * We need to account for all the leadup data and trailer data
3222 * around the transaction data.
3223 * And then we need to account for the worst case in terms of using
3224 * more space.
3225 * The worst case will happen if:
3226 * - the placement of the transaction happens to be such that the
3227 * roundoff is at its maximum
3228 * - the transaction data is synced before the commit record is synced
3229 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3230 * Therefore the commit record is in its own Log Record.
3231 * This can happen as the commit record is called with its
3232 * own region to xlog_write().
3233 * This then means that in the worst case, roundoff can happen for
3234 * the commit-rec as well.
3235 * The commit-rec is smaller than padding in this scenario and so it is
3236 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 */
3238
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003239 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003241 unit_bytes += sizeof(xfs_trans_header_t);
3242
3243 /* for start-rec */
3244 unit_bytes += sizeof(xlog_op_header_t);
3245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 /* for LR headers */
3247 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3248 unit_bytes += log->l_iclog_hsize * num_headers;
3249
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003250 /* for commit-rec LR header - note: padding will subsume the ophdr */
3251 unit_bytes += log->l_iclog_hsize;
3252
3253 /* for split-recs - ophdrs added when data split over LRs */
3254 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3255
3256 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003257 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003258 log->l_mp->m_sb.sb_logsunit > 1) {
3259 /* log su roundoff */
3260 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3261 } else {
3262 /* BB roundoff */
3263 unit_bytes += 2*BBSIZE;
3264 }
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 tic->t_unit_res = unit_bytes;
3267 tic->t_curr_res = unit_bytes;
3268 tic->t_cnt = cnt;
3269 tic->t_ocnt = cnt;
3270 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3271 tic->t_clientid = client;
3272 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003273 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (xflags & XFS_LOG_PERM_RESERV)
3275 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3276 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3277
Christoph Hellwig0adba532007-08-30 17:21:46 +10003278 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return tic;
3281} /* xlog_ticket_get */
3282
3283
3284/******************************************************************************
3285 *
3286 * Log debug routines
3287 *
3288 ******************************************************************************
3289 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003290#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291/*
3292 * Make sure that the destination ptr is within the valid data region of
3293 * one of the iclogs. This uses backup pointers stored in a different
3294 * part of the log in case we trash the log structure.
3295 */
3296void
3297xlog_verify_dest_ptr(xlog_t *log,
3298 __psint_t ptr)
3299{
3300 int i;
3301 int good_ptr = 0;
3302
3303 for (i=0; i < log->l_iclog_bufs; i++) {
3304 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3305 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3306 good_ptr++;
3307 }
3308 if (! good_ptr)
3309 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3310} /* xlog_verify_dest_ptr */
3311
3312STATIC void
3313xlog_verify_grant_head(xlog_t *log, int equals)
3314{
3315 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3316 if (equals)
3317 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3318 else
3319 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3320 } else {
3321 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3322 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3323 }
3324} /* xlog_verify_grant_head */
3325
3326/* check if it will fit */
3327STATIC void
3328xlog_verify_tail_lsn(xlog_t *log,
3329 xlog_in_core_t *iclog,
3330 xfs_lsn_t tail_lsn)
3331{
3332 int blocks;
3333
3334 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3335 blocks =
3336 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3337 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3338 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3339 } else {
3340 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3341
3342 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3343 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3344
3345 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3346 if (blocks < BTOBB(iclog->ic_offset) + 1)
3347 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3348 }
3349} /* xlog_verify_tail_lsn */
3350
3351/*
3352 * Perform a number of checks on the iclog before writing to disk.
3353 *
3354 * 1. Make sure the iclogs are still circular
3355 * 2. Make sure we have a good magic number
3356 * 3. Make sure we don't have magic numbers in the data
3357 * 4. Check fields of each log operation header for:
3358 * A. Valid client identifier
3359 * B. tid ptr value falls in valid ptr space (user space code)
3360 * C. Length in log record header is correct according to the
3361 * individual operation headers within record.
3362 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3363 * log, check the preceding blocks of the physical log to make sure all
3364 * the cycle numbers agree with the current cycle number.
3365 */
3366STATIC void
3367xlog_verify_iclog(xlog_t *log,
3368 xlog_in_core_t *iclog,
3369 int count,
3370 boolean_t syncing)
3371{
3372 xlog_op_header_t *ophead;
3373 xlog_in_core_t *icptr;
3374 xlog_in_core_2_t *xhdr;
3375 xfs_caddr_t ptr;
3376 xfs_caddr_t base_ptr;
3377 __psint_t field_offset;
3378 __uint8_t clientid;
3379 int len, i, j, k, op_len;
3380 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003383 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 icptr = log->l_iclog;
3385 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003386 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 xlog_panic("xlog_verify_iclog: invalid ptr");
3388 icptr = icptr->ic_next;
3389 }
3390 if (icptr != log->l_iclog)
3391 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003392 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003395 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 xlog_panic("xlog_verify_iclog: invalid magic num");
3397
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003398 ptr = (xfs_caddr_t) &iclog->ic_header;
3399 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003401 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 xlog_panic("xlog_verify_iclog: unexpected magic num");
3403 }
3404
3405 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003406 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 ptr = iclog->ic_datap;
3408 base_ptr = ptr;
3409 ophead = (xlog_op_header_t *)ptr;
3410 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3411 for (i = 0; i < len; i++) {
3412 ophead = (xlog_op_header_t *)ptr;
3413
3414 /* clientid is only 1 byte */
3415 field_offset = (__psint_t)
3416 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3417 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3418 clientid = ophead->oh_clientid;
3419 } else {
3420 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3421 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3422 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3423 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003424 clientid = xlog_get_client_id(
3425 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003427 clientid = xlog_get_client_id(
3428 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
3430 }
3431 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003432 cmn_err(CE_WARN, "xlog_verify_iclog: "
3433 "invalid clientid %d op 0x%p offset 0x%lx",
3434 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 /* check length */
3437 field_offset = (__psint_t)
3438 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3439 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003440 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 } else {
3442 idx = BTOBBT((__psint_t)&ophead->oh_len -
3443 (__psint_t)iclog->ic_datap);
3444 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3445 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3446 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003447 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003449 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
3451 }
3452 ptr += sizeof(xlog_op_header_t) + op_len;
3453 }
3454} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003455#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003458 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 */
3460STATIC int
3461xlog_state_ioerror(
3462 xlog_t *log)
3463{
3464 xlog_in_core_t *iclog, *ic;
3465
3466 iclog = log->l_iclog;
3467 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3468 /*
3469 * Mark all the incore logs IOERROR.
3470 * From now on, no log flushes will result.
3471 */
3472 ic = iclog;
3473 do {
3474 ic->ic_state = XLOG_STATE_IOERROR;
3475 ic = ic->ic_next;
3476 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
3479 /*
3480 * Return non-zero, if state transition has already happened.
3481 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003482 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
3485/*
3486 * This is called from xfs_force_shutdown, when we're forcibly
3487 * shutting down the filesystem, typically because of an IO error.
3488 * Our main objectives here are to make sure that:
3489 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3490 * parties to find out, 'atomically'.
3491 * b. those who're sleeping on log reservations, pinned objects and
3492 * other resources get woken up, and be told the bad news.
3493 * c. nothing new gets queued up after (a) and (b) are done.
3494 * d. if !logerror, flush the iclogs to disk, then seal them off
3495 * for business.
3496 */
3497int
3498xfs_log_force_umount(
3499 struct xfs_mount *mp,
3500 int logerror)
3501{
3502 xlog_ticket_t *tic;
3503 xlog_t *log;
3504 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003505 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
3507 log = mp->m_log;
3508
3509 /*
3510 * If this happens during log recovery, don't worry about
3511 * locking; the log isn't open for business yet.
3512 */
3513 if (!log ||
3514 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3515 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3516 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 }
3519
3520 /*
3521 * Somebody could've already done the hard work for us.
3522 * No need to get locks for this.
3523 */
3524 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3525 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003526 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528 retval = 0;
3529 /*
3530 * We must hold both the GRANT lock and the LOG lock,
3531 * before we mark the filesystem SHUTDOWN and wake
3532 * everybody up to tell the bad news.
3533 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003534 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003535 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3537 XFS_BUF_DONE(mp->m_sb_bp);
3538 /*
3539 * This flag is sort of redundant because of the mount flag, but
3540 * it's good to maintain the separation between the log and the rest
3541 * of XFS.
3542 */
3543 log->l_flags |= XLOG_IO_ERROR;
3544
3545 /*
3546 * If we hit a log error, we want to mark all the iclogs IOERROR
3547 * while we're still holding the loglock.
3548 */
3549 if (logerror)
3550 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003551 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 /*
3554 * We don't want anybody waiting for log reservations
3555 * after this. That means we have to wake up everybody
3556 * queued up on reserve_headq as well as write_headq.
3557 * In addition, we make sure in xlog_{re}grant_log_space
3558 * that we don't enqueue anything once the SHUTDOWN flag
3559 * is set, and this action is protected by the GRANTLOCK.
3560 */
3561 if ((tic = log->l_reserve_headq)) {
3562 do {
3563 sv_signal(&tic->t_sema);
3564 tic = tic->t_next;
3565 } while (tic != log->l_reserve_headq);
3566 }
3567
3568 if ((tic = log->l_write_headq)) {
3569 do {
3570 sv_signal(&tic->t_sema);
3571 tic = tic->t_next;
3572 } while (tic != log->l_write_headq);
3573 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003574 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
3576 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3577 ASSERT(!logerror);
3578 /*
3579 * Force the incore logs to disk before shutting the
3580 * log down completely.
3581 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003582 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003583 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003585 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 }
3587 /*
3588 * Wake up everybody waiting on xfs_log_force.
3589 * Callback all log item committed functions as if the
3590 * log writes were completed.
3591 */
3592 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3593
3594#ifdef XFSERRORDEBUG
3595 {
3596 xlog_in_core_t *iclog;
3597
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003598 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 iclog = log->l_iclog;
3600 do {
3601 ASSERT(iclog->ic_callback == 0);
3602 iclog = iclog->ic_next;
3603 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003604 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
3606#endif
3607 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003608 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003611STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612xlog_iclogs_empty(xlog_t *log)
3613{
3614 xlog_in_core_t *iclog;
3615
3616 iclog = log->l_iclog;
3617 do {
3618 /* endianness does not matter here, zero is zero in
3619 * any language.
3620 */
3621 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 iclog = iclog->ic_next;
3624 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003625 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}